2017-07-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / jit.dg / test-error-call-with-mismatching-args.c
blob7306ffd0cbc5fc6ac212db3509eb716a6c467397
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 *ptr);
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 extern void called_function (void *ptr);
25 void
26 test_fn ()
28 called_function (42);
31 and verify that the API complains about the mismatching argument
32 type ("int" vs "void *"). */
33 gcc_jit_type *void_type =
34 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
35 gcc_jit_type *void_ptr_type =
36 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
37 gcc_jit_type *int_type =
38 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
40 /* Declare the imported function. */
41 gcc_jit_param *param =
42 gcc_jit_context_new_param (ctxt, NULL, void_ptr_type, "ptr");
43 gcc_jit_function *called_fn =
44 gcc_jit_context_new_function (ctxt, NULL,
45 GCC_JIT_FUNCTION_IMPORTED,
46 void_type,
47 "called_function",
48 1, &param,
49 0);
51 /* Build the test_fn. */
52 gcc_jit_function *test_fn =
53 gcc_jit_context_new_function (ctxt, NULL,
54 GCC_JIT_FUNCTION_EXPORTED,
55 void_type,
56 "test_fn",
57 0, NULL,
58 0);
59 /* called_function (42); */
60 gcc_jit_rvalue *arg =
61 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42);
63 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
64 gcc_jit_block_add_eval (
65 block, NULL,
66 gcc_jit_context_new_call (ctxt,
67 NULL,
68 called_fn,
69 1, &arg));
70 /* the above has the wrong type for argument 1. */
71 gcc_jit_block_end_with_void_return (block, NULL);
74 void
75 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
77 CHECK_VALUE (result, NULL);
79 /* Verify that the correct error message was emitted. */
80 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
81 ("gcc_jit_context_new_call:"
82 " mismatching types for argument 1"
83 " of function \"called_function\":"
84 " assignment to param ptr (type: void *)"
85 " from (int)42 (type: int)"));