1 #include <libgccjit.h>
2 #include "harness.h"
3 
4 void
create_code(gcc_jit_context * ctxt,void * user_data)5 create_code (gcc_jit_context *ctxt, void *user_data)
6 {
7   /* Try to inject the equivalent of:
8         static int idx;
9         void test_func (void)
10 	{
11 	  idx += (unsigned char)1; // mismatching type
12 	}
13      and verify that we don't get an ICE inside gimplification
14      due to the type mismatch.  */
15   gcc_jit_type *void_type =
16     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
17   gcc_jit_type *int_type =
18     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
19   gcc_jit_type *unsigned_char_type =
20     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR);
21   gcc_jit_function *test_func =
22     gcc_jit_context_new_function (ctxt, /* gcc_jit_context *ctxt */
23                                   NULL, /* gcc_jit_location *loc */
24                                   GCC_JIT_FUNCTION_EXPORTED, /* enum gcc_jit_function_kind kind */
25                                   void_type, /* gcc_jit_type *return_type */
26                                   "test_func", /* const char *name */
27                                   0, /* int num_params */
28                                   NULL, /* gcc_jit_param **params */
29                                   0); /* int is_variadic */
30   gcc_jit_block *block =
31     gcc_jit_function_new_block (test_func, "initial");
32 
33   gcc_jit_rvalue *unsigned_char_1 =
34     gcc_jit_context_new_rvalue_from_int (ctxt, /* gcc_jit_context *ctxt */
35                                          unsigned_char_type, /* gcc_jit_type *numeric_type */
36                                          1); /* int value */
37   gcc_jit_lvalue *idx =
38     gcc_jit_context_new_global (ctxt, /* gcc_jit_context *ctxt */
39                                 NULL, /* gcc_jit_location *loc */
40                                 GCC_JIT_GLOBAL_INTERNAL, /* enum gcc_jit_global_kind kind */
41                                 int_type, /* gcc_jit_type *type */
42                                 "idx"); /* const char *name */
43 
44   gcc_jit_block_add_assignment_op (block, /*gcc_jit_block *block */
45                                    NULL, /* gcc_jit_location *loc */
46                                    idx, /* gcc_jit_lvalue *lvalue */
47                                    GCC_JIT_BINARY_OP_PLUS, /* enum gcc_jit_binary_op op */
48                                    unsigned_char_1); /* gcc_jit_rvalue *rvalue */
49   gcc_jit_block_end_with_void_return (block, /*gcc_jit_block *block */
50 				      NULL);
51 }
52 
53 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)54 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
55 {
56   CHECK_VALUE (result, NULL);
57 
58   /* Verify that the correct error message was emitted.  */
59   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
60 		      "gcc_jit_block_add_assignment_op:"
61 		      " mismatching types:"
62 		      " assignment to idx (type: int)"
63 		      " involving (unsigned char)1 (type: unsigned char)")
64 }
65