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 int
14 test_fn ()
15 {
16 return i;
17 }
18
19 where "i" is a param that isn't associated with any function,
20 and verify that the API complains. */
21
22 gcc_jit_type *int_type =
23 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
24
25 gcc_jit_param *param =
26 gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
27
28 gcc_jit_function *test_fn =
29 gcc_jit_context_new_function (ctxt, NULL,
30 GCC_JIT_FUNCTION_EXPORTED,
31 int_type,
32 "test_fn",
33 0, NULL,
34 0);
35
36 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
37 /* "return i;", using param i from the wrong function. */
38 gcc_jit_block_end_with_return (block,
39 NULL,
40 gcc_jit_param_as_rvalue (param));
41 }
42
43 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)44 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
45 {
46 CHECK_VALUE (result, NULL);
47
48 /* Verify that the correct error message was emitted. */
49 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
50 ("gcc_jit_block_end_with_return:"
51 " param i (type: int)"
52 " was used within function test_fn"
53 " (in statement: return i;)"
54 " but is not associated with any function"))
55 }
56
57