2017-07-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / jit.dg / test-error-call-through-ptr-with-too-many-args.c
blobb8cb2f47e8a55e97ecf622bd7782dac8f5c32441
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 extern void
13 called_function (void);
15 #ifdef __cplusplus
17 #endif
19 void
20 create_code (gcc_jit_context *ctxt, void *user_data)
22 /* Let's try to inject the equivalent of:
23 void
24 test_caller (void (*some_fn_ptr) (void), int a)
26 some_fn_ptr (a);
29 and verify that the API complains about the mismatching arg
30 counts.
32 gcc_jit_type *void_type =
33 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
34 gcc_jit_type *int_type =
35 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
37 /* Build the function ptr type. */
38 gcc_jit_type *fn_ptr_type =
39 gcc_jit_context_new_function_ptr_type (ctxt, NULL,
40 void_type,
41 0, NULL, 0);
43 /* Build the test_fn. */
44 gcc_jit_param *param_fn_ptr =
45 gcc_jit_context_new_param (ctxt, NULL, fn_ptr_type, "some_fn_ptr");
46 gcc_jit_param *param_a =
47 gcc_jit_context_new_param (ctxt, NULL, int_type, "a");
48 gcc_jit_param *params[2];
49 params[0] = param_fn_ptr;
50 params[1] = param_a;
52 gcc_jit_function *test_fn =
53 gcc_jit_context_new_function (ctxt, NULL,
54 GCC_JIT_FUNCTION_EXPORTED,
55 void_type,
56 "test_caller",
57 2, params,
58 0);
59 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
61 /* some_fn_ptr (a); */
62 gcc_jit_rvalue *arg = gcc_jit_param_as_rvalue (param_a);
63 gcc_jit_block_add_eval (
64 block, NULL,
65 gcc_jit_context_new_call_through_ptr (
66 ctxt,
67 NULL,
68 gcc_jit_param_as_rvalue (param_fn_ptr),
69 1, &arg));
70 /* the above has too many args. */
71 gcc_jit_block_end_with_void_return (block, NULL);
74 void
75 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
77 /* Ensure that mismatching arg count leads to the API giving a NULL
78 result back. */
79 CHECK_VALUE (result, NULL);
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_through_ptr:"
84 " too many arguments to fn_ptr:"
85 " some_fn_ptr (got 1 args, expected 0)");