| 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 |
const long integer = 10; |
| 14 |
|
| 15 |
void |
| 16 |
test_fn () |
| 17 |
{ |
| 18 |
integer = 12; |
| 19 |
} |
| 20 |
|
| 21 |
and verify that the API complains about assigning to a read-only |
| 22 |
variable. |
| 23 |
*/ |
| 24 |
gcc_jit_type *void_type = |
| 25 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); |
| 26 |
gcc_jit_type *long_type = |
| 27 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG); |
| 28 |
|
| 29 |
gcc_jit_function *func = |
| 30 |
gcc_jit_context_new_function (ctxt, NULL, |
| 31 |
GCC_JIT_FUNCTION_EXPORTED, |
| 32 |
void_type, |
| 33 |
"test_fn", |
| 34 |
0, NULL, |
| 35 |
0); |
| 36 |
gcc_jit_block *initial = |
| 37 |
gcc_jit_function_new_block (func, "initial"); |
| 38 |
|
| 39 |
gcc_jit_lvalue *integer = |
| 40 |
gcc_jit_context_new_global ( |
| 41 |
ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, long_type, "integer"); |
| 42 |
|
| 43 |
gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, long_type, 10); |
| 44 |
gcc_jit_global_set_initializer_rvalue (integer, ten); |
| 45 |
gcc_jit_global_set_readonly(integer); |
| 46 |
|
| 47 |
gcc_jit_rvalue *twelve = gcc_jit_context_new_rvalue_from_int (ctxt, long_type, 12); |
| 48 |
gcc_jit_block_add_assignment(initial, NULL, integer, twelve); |
| 49 |
|
| 50 |
gcc_jit_block_end_with_void_return (initial, NULL); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 55 |
{ |
| 56 |
CHECK_VALUE (result, NULL); |
| 57 |
|
| 58 |
/* Verify that the correct error messages were emitted. */ |
| 59 |
CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt), |
| 60 |
"gcc_jit_block_add_assignment:" |
| 61 |
" cannot assign to readonly variable: integer"); |
| 62 |
} |