1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "libgccjit.h"
6 
7 #include "harness.h"
8 
9 void
create_code(gcc_jit_context * ctxt,void * user_data)10 create_code (gcc_jit_context *ctxt, void *user_data)
11 {
12   /* Let's try to inject the equivalent of:
13 
14      const char *
15      test_string_literal (void)
16      {
17         return "hello world";
18      }
19   */
20   gcc_jit_type *const_char_ptr_type =
21     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
22 
23   /* Build the test_fn.  */
24   gcc_jit_function *test_fn =
25     gcc_jit_context_new_function (ctxt, NULL,
26                                   GCC_JIT_FUNCTION_EXPORTED,
27                                   const_char_ptr_type,
28                                   "test_string_literal",
29                                   0, NULL,
30                                   0);
31   gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
32 
33   gcc_jit_block_end_with_return (
34     block, NULL,
35     gcc_jit_context_new_string_literal (ctxt, "hello world"));
36 }
37 
38 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)39 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
40 {
41   typedef const char *(*fn_type) (void);
42   CHECK_NON_NULL (result);
43   fn_type test_string_literal =
44     (fn_type)gcc_jit_result_get_code (result, "test_string_literal");
45   CHECK_NON_NULL (test_string_literal);
46 
47   /* Call the JIT-generated function.  */
48   const char *str = test_string_literal ();
49   CHECK_NON_NULL (str);
50   CHECK_VALUE (strcmp (str, "hello world"), 0);
51 }
52 
53