1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 #include "libgccjit.h"
5 
6 #include "harness.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12   extern void
13   called_function (void);
14 
15 #ifdef __cplusplus
16 }
17 #endif
18 
19 void
create_code(gcc_jit_context * ctxt,void * user_data)20 create_code (gcc_jit_context *ctxt, void *user_data)
21 {
22   /* Let's try to inject the equivalent of:
23      extern void called_function ();
24 
25      void
26      test_caller (int a)
27      {
28         called_function (a);
29      }
30 
31      and verify that the API complains about the mismatching arg
32      counts.
33   */
34   gcc_jit_type *void_type =
35     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
36   gcc_jit_type *int_type =
37     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
38 
39   /* Declare the imported function.  */
40   gcc_jit_function *called_fn =
41     gcc_jit_context_new_function (ctxt, NULL,
42                                   GCC_JIT_FUNCTION_IMPORTED,
43                                   void_type,
44                                   "called_function",
45                                   0, NULL,
46                                   0);
47 
48   /* Build the test_fn.  */
49   gcc_jit_param *param_a =
50     gcc_jit_context_new_param (ctxt, NULL, int_type, "a");
51   gcc_jit_function *test_fn =
52     gcc_jit_context_new_function (ctxt, NULL,
53                                   GCC_JIT_FUNCTION_EXPORTED,
54                                   void_type,
55                                   "test_caller",
56                                   1, &param_a,
57                                   0);
58   gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
59   /* called_function (a);  */
60   gcc_jit_rvalue *arg = gcc_jit_param_as_rvalue (param_a);
61   gcc_jit_block_add_eval (
62     block, NULL,
63     gcc_jit_context_new_call (ctxt,
64                               NULL,
65                               called_fn,
66                               1, &arg));
67   /* the above has the wrong arg count.  */
68   gcc_jit_block_end_with_void_return (block, NULL);
69 }
70 
71 extern void
called_function(void)72 called_function (void)
73 {
74 }
75 
76 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)77 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
78 {
79   /* Ensure that mismatching arg count leads to the API giving a NULL
80      result back.  */
81   CHECK_VALUE (result, NULL);
82 
83   /* Verify that the correct error message was emitted.  */
84   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
85 		      ("gcc_jit_context_new_call:"
86 		       " too many arguments to function \"called_function\""
87 		       " (got 1 args, expected 0)"));
88 }
89 
90