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 struct bar
|
|
15 {
|
|
16 int p;
|
|
17 int q;
|
|
18 };
|
|
19
|
|
20 void
|
|
21 create_code (gcc_jit_context *ctxt, void *user_data)
|
|
22 {
|
|
23 /* Let's try to inject the equivalent of:
|
|
24 void
|
|
25 test_bogus_access (struct foo f)
|
|
26 {
|
|
27 f.x = f.p;
|
|
28 }
|
|
29 i.e. using the wrong struct for the RHS.
|
|
30 */
|
|
31 gcc_jit_type *void_type =
|
|
32 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
|
|
33 gcc_jit_type *int_type =
|
|
34 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
|
|
35
|
|
36 /* Map "struct foo". */
|
|
37 gcc_jit_field *x =
|
|
38 gcc_jit_context_new_field (ctxt,
|
|
39 NULL,
|
|
40 int_type,
|
|
41 "x");
|
|
42 gcc_jit_field *y =
|
|
43 gcc_jit_context_new_field (ctxt,
|
|
44 NULL,
|
|
45 int_type,
|
|
46 "y");
|
|
47 gcc_jit_field *foo_fields[] = {x, y};
|
|
48 gcc_jit_struct *struct_foo =
|
|
49 gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, foo_fields);
|
|
50
|
|
51 /* Map "struct bar". */
|
|
52 gcc_jit_field *p =
|
|
53 gcc_jit_context_new_field (ctxt,
|
|
54 NULL,
|
|
55 int_type,
|
|
56 "p");
|
|
57 gcc_jit_field *q =
|
|
58 gcc_jit_context_new_field (ctxt,
|
|
59 NULL,
|
|
60 int_type,
|
|
61 "q");
|
|
62 /* We don't actually need a gcc_jit_type for "struct bar" for the test. */
|
|
63 gcc_jit_field *bar_fields[] = {p, q};
|
|
64 (void)gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 2, bar_fields);
|
|
65
|
|
66 /* Build the test function. */
|
|
67 gcc_jit_param *param_f =
|
|
68 gcc_jit_context_new_param (ctxt, NULL,
|
|
69 gcc_jit_struct_as_type (struct_foo), "f");
|
|
70 gcc_jit_function *test_fn =
|
|
71 gcc_jit_context_new_function (ctxt, NULL,
|
|
72 GCC_JIT_FUNCTION_EXPORTED,
|
|
73 void_type,
|
|
74 "test_bogus_access",
|
|
75 1, ¶m_f,
|
|
76 0);
|
|
77
|
|
78 /* OK: f.x = ... */
|
|
79 gcc_jit_lvalue *lvalue =
|
|
80 gcc_jit_lvalue_access_field (
|
|
81 gcc_jit_param_as_lvalue (param_f),
|
|
82 NULL,
|
|
83 x);
|
|
84
|
|
85 /* Erroneous: ... = f.p; */
|
|
86 gcc_jit_rvalue *rvalue =
|
|
87 gcc_jit_rvalue_access_field (
|
|
88 gcc_jit_param_as_rvalue (param_f),
|
|
89 NULL,
|
|
90 p);
|
|
91
|
|
92 gcc_jit_block *block =
|
|
93 gcc_jit_function_new_block (test_fn, NULL);
|
|
94 gcc_jit_block_add_assignment (
|
|
95 block,
|
|
96 NULL,
|
|
97 lvalue, rvalue);
|
|
98 gcc_jit_block_end_with_void_return (block, NULL);
|
|
99 }
|
|
100
|
|
101 void
|
|
102 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
|
|
103 {
|
|
104 CHECK_VALUE (result, NULL);
|
|
105
|
|
106 /* Verify that the correct error message was emitted. */
|
|
107 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
|
|
108 "gcc_jit_rvalue_access_field:"
|
|
109 " p is not a field of struct foo");
|
|
110 }
|