111
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3
|
|
4 #include "libgccjit.h"
|
|
5
|
|
6 #include "harness.h"
|
|
7
|
|
8 struct foo
|
|
9 {
|
|
10 int x;
|
|
11 int y;
|
|
12 };
|
|
13
|
|
14 void
|
|
15 create_code (gcc_jit_context *ctxt, void *user_data)
|
|
16 {
|
|
17 /* Let's try to inject the equivalent of:
|
|
18 void
|
|
19 test_bogus_dereference ()
|
|
20 {
|
|
21 struct foo tmp;
|
|
22 tmp->x = tmp->y;
|
|
23 }
|
|
24 i.e. where tmp is *not* a pointer.
|
|
25 */
|
|
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 /* Map "struct foo". */
|
|
32 gcc_jit_field *x =
|
|
33 gcc_jit_context_new_field (ctxt,
|
|
34 NULL,
|
|
35 int_type,
|
|
36 "x");
|
|
37 gcc_jit_field *y =
|
|
38 gcc_jit_context_new_field (ctxt,
|
|
39 NULL,
|
|
40 int_type,
|
|
41 "y");
|
|
42 gcc_jit_field *foo_fields[] = {x, y};
|
|
43 gcc_jit_struct *struct_foo =
|
|
44 gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);
|
|
45
|
|
46 /* Build the test function. */
|
|
47 gcc_jit_function *test_fn =
|
|
48 gcc_jit_context_new_function (ctxt, NULL,
|
|
49 GCC_JIT_FUNCTION_EXPORTED,
|
|
50 void_type,
|
|
51 "test_bogus_dereference",
|
|
52 0, NULL,
|
|
53 0);
|
|
54 gcc_jit_lvalue *tmp =
|
|
55 gcc_jit_function_new_local (test_fn, NULL,
|
|
56 gcc_jit_struct_as_type (struct_foo),
|
|
57 "tmp");
|
|
58
|
|
59 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
|
|
60
|
|
61 /* Erroneous: tmp->x = ... */
|
|
62 gcc_jit_lvalue *lvalue =
|
|
63 gcc_jit_rvalue_dereference_field (
|
|
64 gcc_jit_lvalue_as_rvalue (tmp),
|
|
65 NULL,
|
|
66 x);
|
|
67
|
|
68 /* Erroneous: ... = tmp->y; */
|
|
69 gcc_jit_rvalue *rvalue =
|
|
70 gcc_jit_lvalue_as_rvalue (
|
|
71 gcc_jit_rvalue_dereference_field (
|
|
72 gcc_jit_lvalue_as_rvalue (tmp),
|
|
73 NULL,
|
|
74 y));
|
|
75
|
|
76 gcc_jit_block_add_assignment (
|
|
77 block,
|
|
78 NULL,
|
|
79 lvalue, rvalue);
|
|
80
|
|
81 gcc_jit_block_end_with_void_return (block, NULL);
|
|
82 }
|
|
83
|
|
84 void
|
|
85 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
|
|
86 {
|
|
87 CHECK_VALUE (result, NULL);
|
|
88
|
|
89 /* Verify that the correct error message was emitted. */
|
|
90 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
|
|
91 ("gcc_jit_rvalue_dereference_field:"
|
|
92 " dereference of non-pointer tmp (type: struct foo)"
|
|
93 " when accessing ->x"));
|
|
94 }
|
|
95
|