Skip to content

Commit efdc80a

Browse files
committed
libgccjit: Add type checks in gcc_jit_block_add_assignment_op
gcc/jit/ChangeLog: * libgccjit.cc (RETURN_IF_FAIL_PRINTF3): New macro. (gcc_jit_block_add_assignment_op): Add numeric checks. gcc/testsuite/ChangeLog: * jit.dg/test-error-bad-assignment-op.c: New test.
1 parent 04b67ac commit efdc80a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

gcc/jit/libgccjit.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ struct gcc_jit_extended_asm : public gcc::jit::recording::extended_asm
267267
} \
268268
JIT_END_STMT
269269

270+
#define RETURN_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
271+
JIT_BEGIN_STMT \
272+
if (!(TEST_EXPR)) \
273+
{ \
274+
jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
275+
__func__, (A0), (A1), (A2)); \
276+
return; \
277+
} \
278+
JIT_END_STMT
279+
270280
#define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
271281
JIT_BEGIN_STMT \
272282
if (!(TEST_EXPR)) \
@@ -2984,6 +2994,17 @@ gcc_jit_block_add_assignment_op (gcc_jit_block *block,
29842994
lvalue->get_type ()->get_debug_string (),
29852995
rvalue->get_debug_string (),
29862996
rvalue->get_type ()->get_debug_string ());
2997+
// TODO: check if it is a numeric vector?
2998+
RETURN_IF_FAIL_PRINTF3 (
2999+
lvalue->get_type ()->is_numeric (), ctxt, loc,
3000+
"gcc_jit_block_add_assignment_op %s has non-numeric lvalue %s (type: %s)",
3001+
gcc::jit::binary_op_reproducer_strings[op],
3002+
lvalue->get_debug_string (), lvalue->get_type ()->get_debug_string ());
3003+
RETURN_IF_FAIL_PRINTF3 (
3004+
rvalue->get_type ()->is_numeric (), ctxt, loc,
3005+
"gcc_jit_block_add_assignment_op %s has non-numeric rvalue %s (type: %s)",
3006+
gcc::jit::binary_op_reproducer_strings[op],
3007+
rvalue->get_debug_string (), rvalue->get_type ()->get_debug_string ());
29873008

29883009
gcc::jit::recording::statement *stmt = block->add_assignment_op (loc, lvalue, op, rvalue);
29893010

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
void
14+
test_fn ()
15+
{
16+
const char *variable;
17+
variable += "test";
18+
}
19+
20+
and verify that the API complains about the mismatching types
21+
in the assignments.
22+
*/
23+
gcc_jit_type *void_type =
24+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
25+
gcc_jit_type *const_char_ptr_type =
26+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
27+
28+
gcc_jit_function *func =
29+
gcc_jit_context_new_function (ctxt, NULL,
30+
GCC_JIT_FUNCTION_EXPORTED,
31+
void_type,
32+
"test_fn",
33+
0, NULL,
34+
0);
35+
36+
gcc_jit_lvalue *variable = gcc_jit_function_new_local (func, NULL, const_char_ptr_type, "variable");
37+
gcc_jit_block *initial =
38+
gcc_jit_function_new_block (func, "initial");
39+
gcc_jit_rvalue *string =
40+
gcc_jit_context_new_string_literal (ctxt, "test");
41+
gcc_jit_block_add_assignment_op (initial, NULL, variable, GCC_JIT_BINARY_OP_PLUS, string);
42+
43+
gcc_jit_block_end_with_void_return (initial, NULL);
44+
}
45+
46+
void
47+
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
48+
{
49+
CHECK_VALUE (result, NULL);
50+
51+
/* Verify that the correct error messages were emitted. */
52+
CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
53+
"gcc_jit_block_add_assignment_op:"
54+
" gcc_jit_block_add_assignment_op GCC_JIT_BINARY_OP_PLUS"
55+
" has non-numeric lvalue variable (type: const char *)");
56+
}
57+

0 commit comments

Comments
 (0)