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 (int i)
15      {
16      }
17 
18      int
19      fn_two ()
20      {
21        return i * 2;
22      }
23 
24      and verify that the API complains about the use of the param
25      from the other function.  */
26   gcc_jit_type *void_type =
27     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
28   gcc_jit_type *int_type =
29     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
30 
31   gcc_jit_param *param =
32     gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
33 
34   gcc_jit_context_new_function (ctxt, NULL,
35 				GCC_JIT_FUNCTION_EXPORTED,
36 				void_type,
37 				"fn_one",
38 				1, &param,
39 				0);
40   gcc_jit_function *fn_two =
41     gcc_jit_context_new_function (ctxt, NULL,
42                                   GCC_JIT_FUNCTION_EXPORTED,
43                                   int_type,
44                                   "fn_two",
45                                   0, NULL,
46                                   0);
47 
48   gcc_jit_block *block = gcc_jit_function_new_block (fn_two, NULL);
49   /* "return i * 2;", using param i from the wrong function.  */
50   gcc_jit_block_end_with_return (
51     block,
52     NULL,
53     gcc_jit_context_new_binary_op (
54       ctxt, NULL,
55       GCC_JIT_BINARY_OP_MULT,
56       int_type,
57       gcc_jit_param_as_rvalue (param),
58       gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 2)));
59 }
60 
61 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)62 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
63 {
64   CHECK_VALUE (result, NULL);
65 
66   /* Verify that the correct error message was emitted.  */
67   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
68 		      ("gcc_jit_block_end_with_return:"
69 		       " rvalue i (type: int)"
70 		       " has scope limited to function fn_one"
71 		       " but was used within function fn_two"
72 		       " (in statement: return i * (int)2;)"));
73 }
74 
75