* gcc-interface/decl.c (gnat_to_gnu_field): Do not set the alignment
[official-gcc.git] / gcc / testsuite / jit.dg / test-error-array-bounds.c
blob889c51737a419424f969ee439a6d5e89f7deb5bc
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 void
9 create_code (gcc_jit_context *ctxt, void *user_data)
11 /* Let's try to inject the equivalent of:
12 char
13 test_array_bounds (void)
15 char buffer[10];
16 return buffer[10];
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,
31 char_type, 10);
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,
39 char_type,
40 "test_array_bounds",
41 0, NULL,
42 0);
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),
57 index);
58 gcc_jit_block_end_with_return (
59 block, dummy_loc,
60 gcc_jit_lvalue_as_rvalue (read_of_the_buffer));
63 void
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 10 is above array bounds of"
72 " 'unsigned char[10]' [-Warray-bounds]");