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_fn (void (*some_fn_ptr) (void *))
15 {
16 some_fn_ptr (42);
17 }
18
19 and verify that the API complains about the mismatching argument
20 type ("int" vs "void *"). */
21 gcc_jit_type *void_type =
22 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
23 gcc_jit_type *void_ptr_type =
24 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
25 gcc_jit_type *int_type =
26 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
27
28 /* Build the function ptr type. */
29 gcc_jit_type *fn_ptr_type =
30 gcc_jit_context_new_function_ptr_type (ctxt, NULL,
31 void_type,
32 1, &void_ptr_type, 0);
33
34 /* Build the test_fn. */
35 gcc_jit_param *param_fn_ptr =
36 gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
37
38 gcc_jit_function *test_fn =
39 gcc_jit_context_new_function (ctxt, NULL,
40 GCC_JIT_FUNCTION_EXPORTED,
41 void_type,
42 "test_fn",
43 1, ¶m_fn_ptr,
44 0);
45 /* some_fn_ptr (42); */
46 gcc_jit_rvalue *arg =
47 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
48
49 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
50 gcc_jit_block_add_eval (
51 block, NULL,
52 gcc_jit_context_new_call_through_ptr (
53 ctxt,
54 NULL,
55 gcc_jit_param_as_rvalue (param_fn_ptr),
56 1, &arg));
57 /* the above has the wrong type for argument 1. */
58 gcc_jit_block_end_with_void_return (block, NULL);
59 }
60
61 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)62 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
63 {
64 CHECK_VALUE (result, NULL);
65
66 /* Verify that the correct error message was emitted. */
67 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
68 ("gcc_jit_context_new_call_through_ptr:"
69 " mismatching types for argument 1 of fn_ptr:"
70 " some_fn_ptr:"
71 " assignment to param 1 (type: void *)"
72 " from (int)42 (type: int)"));
73 }
74
75