1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "libgccjit.h"
6 
7 #include "harness.h"
8 
9 void
create_code(gcc_jit_context * ctxt,void * user_data)10 create_code (gcc_jit_context *ctxt, void *user_data)
11 {
12   /* Let's try to inject the equivalent of:
13       int
14       test_switch (int x)
15       {
16 	switch (x)
17 	  {
18 	  case (long long)0 ... (long long)5:
19 	     return 3;
20 	  default:
21 	     return 10;
22 	  }
23       }
24       and verify that the floating-point case is an error.
25    */
26   gcc_jit_type *t_int =
27     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
28   gcc_jit_type *t_long_long =
29     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG_LONG);
30   gcc_jit_type *return_type = t_int;
31   gcc_jit_param *x =
32     gcc_jit_context_new_param (ctxt, NULL, t_int, "x");
33   gcc_jit_param *params[1] = {x};
34   gcc_jit_function *func =
35     gcc_jit_context_new_function (ctxt, NULL,
36 				  GCC_JIT_FUNCTION_EXPORTED,
37 				  return_type,
38 				  "test_switch",
39 				  1, params, 0);
40 
41   gcc_jit_block *b_initial =
42     gcc_jit_function_new_block (func, "initial");
43 
44   gcc_jit_block *b_default =
45     gcc_jit_function_new_block (func, "default");
46   gcc_jit_block *b_case_0 =
47     gcc_jit_function_new_block (func, "case_0");
48 
49   /* Note the erroneous use of "t_float" here.  */
50   gcc_jit_case *cases[1] = {
51     gcc_jit_context_new_case (
52       ctxt,
53       gcc_jit_context_new_rvalue_from_int (ctxt, t_long_long, 0),
54       gcc_jit_context_new_rvalue_from_int (ctxt, t_long_long, 5),
55       b_case_0)
56   };
57 
58   gcc_jit_block_end_with_switch (
59     b_initial, NULL,
60     gcc_jit_param_as_rvalue (x),
61     b_default,
62     1,
63     cases);
64 
65   gcc_jit_block_end_with_return (
66     b_case_0, NULL,
67     gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3));
68   gcc_jit_block_end_with_return (
69     b_default, NULL,
70     gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10));
71 }
72 
73 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)74 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
75 {
76   CHECK_VALUE (result, NULL);
77 
78   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
79 		      "gcc_jit_block_end_with_switch:"
80 		      " mismatching types between case and expression:"
81 		      " cases[0]->min_value: (long long)0 (type: long long)"
82 		      " expr: x (type: int)");
83 }
84