9 create_code (gcc_jit_context
*ctxt
, void *user_data
)
11 /* Let's try to inject the equivalent of:
13 test_array_bounds (void)
18 with -Warray-bounds and -ftree-vrp and verify that the
19 out-of-bounds access is detected and reported as a jit error. */
20 gcc_jit_context_add_command_line_option (ctxt
, "-Warray-bounds");
21 gcc_jit_context_add_command_line_option (ctxt
, "-ftree-vrp");
23 /* Ensure that the error message doesn't contain colorization codes,
24 even if run at a TTY. */
25 gcc_jit_context_add_command_line_option (ctxt
, "-fdiagnostics-color=never");
27 gcc_jit_type
*char_type
=
28 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_CHAR
);
29 gcc_jit_type
*array_type
=
30 gcc_jit_context_new_array_type (ctxt
, NULL
,
32 gcc_jit_type
*int_type
=
33 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
35 /* Build the test_fn. */
36 gcc_jit_function
*test_fn
=
37 gcc_jit_context_new_function (ctxt
, NULL
,
38 GCC_JIT_FUNCTION_EXPORTED
,
43 gcc_jit_lvalue
*buffer
=
44 gcc_jit_function_new_local (test_fn
, NULL
, array_type
, "buffer");
46 /* tree-vrp.c:check_all_array_refs only checks array lookups that
47 have source locations. */
48 gcc_jit_location
*dummy_loc
=
49 gcc_jit_context_new_location (ctxt
, "dummy.c", 10, 4);
51 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
52 gcc_jit_rvalue
*index
=
53 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 10);
54 gcc_jit_lvalue
*read_of_the_buffer
=
55 gcc_jit_context_new_array_access (ctxt
, dummy_loc
,
56 gcc_jit_lvalue_as_rvalue (buffer
),
58 gcc_jit_block_end_with_return (
60 gcc_jit_lvalue_as_rvalue (read_of_the_buffer
));
64 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
66 /* Verify that the diagnostic led to the context failing... */
67 CHECK_VALUE (result
, NULL
);
69 /* ...and that the message was captured by the API. */
70 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt
),
71 "array subscript is above array bounds [-Warray-bounds]");