1 #include <stdlib.h>
2 #include <stdio.h>
3 
4 #include "libgccjit.h"
5 
6 #include "harness.h"
7 
8 void
create_code(gcc_jit_context * ctxt,void * user_data)9 create_code (gcc_jit_context *ctxt, void *user_data)
10 {
11   /* Let's try to inject the equivalent of:
12 
13      void
14      test_caller (void (*some_fn_ptr) (int p))
15      {
16         called_function (); // missing arg
17      }
18 
19      and verify that the API complains about the missing argument.
20   */
21   gcc_jit_type *void_type =
22     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
23   gcc_jit_type *int_type =
24     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
25 
26   /* Build the function ptr type.  */
27   gcc_jit_type *fn_ptr_type =
28     gcc_jit_context_new_function_ptr_type (ctxt, NULL,
29 					   void_type,
30 					   1, &int_type, 0);
31 
32   /* Build the test_fn.  */
33   gcc_jit_param *param_fn_ptr =
34     gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
35 
36   gcc_jit_function *test_fn =
37     gcc_jit_context_new_function (ctxt, NULL,
38                                   GCC_JIT_FUNCTION_EXPORTED,
39                                   void_type,
40                                   "test_caller",
41                                   1, &param_fn_ptr,
42                                   0);
43 
44   gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
45   /* called_function ();  */
46   gcc_jit_block_add_eval (
47     block, NULL,
48     gcc_jit_context_new_call_through_ptr (
49       ctxt,
50       NULL,
51       gcc_jit_param_as_rvalue (param_fn_ptr),
52       0, NULL));
53   /* the above has not enough args.  */
54   gcc_jit_block_end_with_void_return (block, NULL);
55 }
56 
57 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)58 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
59 {
60   /* Ensure that mismatching arg count leads to the API giving a NULL
61      result back.  */
62   CHECK_VALUE (result, NULL);
63 
64   /* Verify that the correct error message was emitted.  */
65   CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
66 		      ("gcc_jit_context_new_call_through_ptr:"
67 		       " not enough arguments to fn_ptr: some_fn_ptr"
68 		       " (got 0 args, expected 1)"));
69 }
70 
71