1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 #include "libgccjit.h"
5 
6 #include "harness.h"
7 
8 void
create_code(gcc_jit_context * ctxt,void * user_data)9 create_code (gcc_jit_context *ctxt, void *user_data)
10 {
11   /* Let's try to inject the equivalent of:
12 
13      void
14      fn_one ()
15      {
16        int i;
17      }
18 
19      int
20      fn_two ()
21      {
22        return i;
23      }
24 
25      and verify that the API complains about the use of the local
26      from the other function.  */
27   gcc_jit_type *void_type =
28     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
29   gcc_jit_type *int_type =
30     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
31 
32   gcc_jit_function *fn_one =
33     gcc_jit_context_new_function (ctxt, NULL,
34 				  GCC_JIT_FUNCTION_EXPORTED,
35 				  void_type,
36 				  "fn_one",
37 				  0, NULL,
38 				  0);
39   gcc_jit_lvalue *local =
40     gcc_jit_function_new_local (fn_one, NULL, int_type, "i");
41 
42   gcc_jit_function *fn_two =
43     gcc_jit_context_new_function (ctxt, NULL,
44                                   GCC_JIT_FUNCTION_EXPORTED,
45                                   int_type,
46                                   "fn_two",
47                                   0, NULL,
48                                   0);
49 
50   gcc_jit_block *block = gcc_jit_function_new_block (fn_two, NULL);
51   /* "return i;", using local i from the wrong function.  */
52   gcc_jit_block_end_with_return (block,
53 				 NULL,
54 				 gcc_jit_lvalue_as_rvalue (local));
55 }
56 
57 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)58 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
59 {
60   CHECK_VALUE (result, NULL);
61 
62   /* Verify that the correct error message was emitted.  */
63   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
64 		      ("gcc_jit_block_end_with_return:"
65 		       " rvalue i (type: int)"
66 		       " has scope limited to function fn_one"
67 		       " but was used within function fn_two"
68 		       " (in statement: return i;)"));
69 }
70 
71