| 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 |
typedef void (*fn_ptr_iii) (int, int, int); |
| 14 |
typedef void (*fn_ptr_ifi) (int, float, int); |
| 15 |
void |
| 16 |
test_fn (void) |
| 17 |
{ |
| 18 |
fn_ptr_iii iii_ptr; |
| 19 |
fn_ptr_ifi ifi_ptr; |
| 20 |
|
| 21 |
iii_ptr = NULL; |
| 22 |
ifi_ptr = iii_ptr; |
| 23 |
} |
| 24 |
|
| 25 |
and verify that the API complains about the mismatching types |
| 26 |
in the second assignment (but not the first). |
| 27 |
*/ |
| 28 |
gcc_jit_type *void_type = |
| 29 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); |
| 30 |
gcc_jit_type *int_type = |
| 31 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 32 |
gcc_jit_type *float_type = |
| 33 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); |
| 34 |
|
| 35 |
gcc_jit_type *iii_types[] = {int_type, int_type, int_type}; |
| 36 |
gcc_jit_type *fn_ptr_type_iii |
| 37 |
= gcc_jit_context_new_function_ptr_type (ctxt, NULL, |
| 38 |
void_type, |
| 39 |
3, iii_types, |
| 40 |
0); |
| 41 |
|
| 42 |
gcc_jit_type *ifi_types[] = {int_type, float_type, int_type}; |
| 43 |
gcc_jit_type *fn_ptr_type_ifi |
| 44 |
= gcc_jit_context_new_function_ptr_type (ctxt, NULL, |
| 45 |
void_type, |
| 46 |
3, ifi_types, |
| 47 |
0); |
| 48 |
|
| 49 |
gcc_jit_function *test_fn = |
| 50 |
gcc_jit_context_new_function (ctxt, NULL, |
| 51 |
GCC_JIT_FUNCTION_EXPORTED, |
| 52 |
void_type, |
| 53 |
"test_fn", |
| 54 |
0, NULL, |
| 55 |
0); |
| 56 |
gcc_jit_lvalue *iii_ptr = |
| 57 |
gcc_jit_function_new_local ( |
| 58 |
test_fn, NULL, fn_ptr_type_iii, "iii_ptr"); |
| 59 |
gcc_jit_lvalue *ifi_ptr = |
| 60 |
gcc_jit_function_new_local ( |
| 61 |
test_fn, NULL, fn_ptr_type_ifi, "ifi_ptr"); |
| 62 |
|
| 63 |
gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL); |
| 64 |
|
| 65 |
/* iii_ptr = NULL; */ |
| 66 |
gcc_jit_block_add_assignment ( |
| 67 |
block, NULL, |
| 68 |
iii_ptr, |
| 69 |
gcc_jit_context_null (ctxt, fn_ptr_type_iii)); |
| 70 |
/* ifi_ptr = iii_ptr; */ |
| 71 |
gcc_jit_block_add_assignment ( |
| 72 |
block, NULL, |
| 73 |
ifi_ptr, |
| 74 |
gcc_jit_lvalue_as_rvalue (iii_ptr)); |
| 75 |
gcc_jit_block_end_with_void_return (block, NULL); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 80 |
{ |
| 81 |
CHECK_VALUE (result, NULL); |
| 82 |
|
| 83 |
/* Verify that the correct error message was emitted. */ |
| 84 |
CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), |
| 85 |
"gcc_jit_block_add_assignment:" |
| 86 |
" mismatching types:" |
| 87 |
" assignment to ifi_ptr" |
| 88 |
" (type: void (*) (int, float, int))" |
| 89 |
" from iii_ptr" |
| 90 |
" (type: void (*) (int, int, int))"); |
| 91 |
} |
| 92 |
|