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 (int p);
24 
25      void
26      test_caller ()
27      {
28         called_function (); // missing arg
29      }
30 
31      and verify that the API complains about the missing argument.
32   */
33   gcc_jit_type *void_type =
34     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
35   gcc_jit_type *int_type =
36     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
37 
38   /* Declare the imported function.  */
39   gcc_jit_param *param_p =
40     gcc_jit_context_new_param (ctxt, NULL, int_type, "p");
41   gcc_jit_function *called_fn =
42     gcc_jit_context_new_function (ctxt, NULL,
43                                   GCC_JIT_FUNCTION_IMPORTED,
44                                   void_type,
45                                   "called_function",
46                                   1, &param_p,
47                                   0);
48 
49   /* Build the test_fn.  */
50   gcc_jit_function *test_fn =
51     gcc_jit_context_new_function (ctxt, NULL,
52                                   GCC_JIT_FUNCTION_EXPORTED,
53                                   void_type,
54                                   "test_caller",
55                                   0, NULL,
56                                   0);
57   gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
58   /* called_function ();  */
59   gcc_jit_block_add_eval (
60     block, NULL,
61     gcc_jit_context_new_call (ctxt,
62                               NULL,
63                               called_fn,
64                               0, NULL));
65   /* the above has the wrong arg count.  */
66   gcc_jit_block_end_with_void_return (block, NULL);
67 }
68 
69 extern void
called_function(void)70 called_function (void)
71 {
72 }
73 
74 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)75 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
76 {
77   /* Ensure that mismatching arg count leads to the API giving a NULL
78      result back.  */
79   CHECK_VALUE (result, NULL);
80 
81   /* Verify that the correct error message was emitted.  */
82   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
83 		      ("gcc_jit_context_new_call:"
84 		       " not enough arguments to function \"called_function\""
85 		       " (got 0 args, expected 1)"));
86 }
87 
88