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_ptr)
15 {
16 ((some_unspecified_fn_ptr_type)some_ptr) (42);
17 }
18
19 and verify that the API complains about the 42 not being a
20 function pointer. */
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 test_fn. */
29 gcc_jit_param *some_ptr =
30 gcc_jit_context_new_param (ctxt, NULL, void_ptr_type, "some_ptr");
31
32 gcc_jit_function *test_fn =
33 gcc_jit_context_new_function (ctxt, NULL,
34 GCC_JIT_FUNCTION_EXPORTED,
35 void_type,
36 "test_fn",
37 1, &some_ptr,
38 0);
39 gcc_jit_rvalue *arg =
40 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
41
42 /* ((some_unspecified_fn_ptr_type)some_ptr) (42); */
43 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
44 gcc_jit_block_add_eval (
45 block, NULL,
46 gcc_jit_context_new_call_through_ptr (
47 ctxt,
48 NULL,
49 /* This is not a function pointer. */
50 gcc_jit_param_as_rvalue (some_ptr),
51 1, &arg));
52 gcc_jit_block_end_with_void_return (block, NULL);
53 }
54
55 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)56 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
57 {
58 CHECK_VALUE (result, NULL);
59
60 /* Verify that the correct error message was emitted. */
61 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
62 ("gcc_jit_context_new_call_through_ptr:"
63 " fn_ptr is not a function ptr: some_ptr type: void *"));
64 }
65
66