| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
#include "libgccjit.h" |
| 5 |
|
| 6 |
#include "harness.h" |
| 7 |
|
| 8 |
void |
| 9 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 10 |
{ |
| 11 |
/* Let's try to inject the equivalent of: |
| 12 |
|
| 13 |
struct foo; |
| 14 |
|
| 15 |
extern void called_function (struct foo *ptr); |
| 16 |
|
| 17 |
void |
| 18 |
test_fn () |
| 19 |
{ |
| 20 |
struct foo f; |
| 21 |
called_function (f); |
| 22 |
} |
| 23 |
|
| 24 |
and verify that we get a type error (foo * vs foo). |
| 25 |
*/ |
| 26 |
gcc_jit_type *void_type = |
| 27 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); |
| 28 |
gcc_jit_struct *struct_foo = |
| 29 |
gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 0, NULL); |
| 30 |
gcc_jit_type *foo_ptr = |
| 31 |
gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_foo)); |
| 32 |
gcc_jit_param *param = |
| 33 |
gcc_jit_context_new_param (ctxt, NULL, foo_ptr, "ptr"); |
| 34 |
|
| 35 |
gcc_jit_function *called_function = |
| 36 |
gcc_jit_context_new_function (ctxt, NULL, |
| 37 |
GCC_JIT_FUNCTION_IMPORTED, |
| 38 |
void_type, |
| 39 |
"called_function", |
| 40 |
1, ¶m, |
| 41 |
0); |
| 42 |
|
| 43 |
gcc_jit_function *test_fn = |
| 44 |
gcc_jit_context_new_function (ctxt, NULL, |
| 45 |
GCC_JIT_FUNCTION_EXPORTED, |
| 46 |
void_type, |
| 47 |
"test_fn", |
| 48 |
0, NULL, |
| 49 |
0); |
| 50 |
gcc_jit_lvalue *f = |
| 51 |
gcc_jit_function_new_local ( |
| 52 |
test_fn, NULL, gcc_jit_struct_as_type (struct_foo), "f"); |
| 53 |
|
| 54 |
gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL); |
| 55 |
|
| 56 |
gcc_jit_rvalue *arg = gcc_jit_lvalue_as_rvalue (f); |
| 57 |
|
| 58 |
gcc_jit_block_add_eval ( |
| 59 |
block, NULL, |
| 60 |
gcc_jit_context_new_call ( |
| 61 |
ctxt, NULL, |
| 62 |
called_function, |
| 63 |
1, &arg)); |
| 64 |
gcc_jit_block_end_with_void_return (block, NULL); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 69 |
{ |
| 70 |
CHECK_VALUE (result, NULL); |
| 71 |
|
| 72 |
/* Verify that the correct error message was emitted. */ |
| 73 |
CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), |
| 74 |
"gcc_jit_context_new_call:" |
| 75 |
" mismatching types for argument 1" |
| 76 |
" of function \"called_function\":" |
| 77 |
" assignment to param ptr (type: struct foo *)" |
| 78 |
" from f (type: struct foo)"); |
| 79 |
} |
| 80 |
|