Skip to content

Commit 8e01137

Browse files
committed
libgccjit: Add option to allow special characters in function names
gcc/jit/ChangeLog: * docs/topics/contexts.rst: Add documentation for new option. * jit-recording.cc (recording::context::get_bool_option): New method. * jit-recording.h (get_bool_option): New method. * libgccjit.cc (gcc_jit_context_new_function): Allow special characters in function names. * libgccjit.h (enum gcc_jit_bool_option): New option. gcc/testsuite/ChangeLog: * jit.dg/test-special-chars.c: New test.
1 parent 4e65f02 commit 8e01137

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

gcc/jit/docs/topics/contexts.rst

+4
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ Boolean options
453453
If true, the :type:`gcc_jit_context` will not clean up intermediate files
454454
written to the filesystem, and will display their location on stderr.
455455

456+
.. macro:: GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES
457+
458+
If true, allow special characters like . and $ in function names.
459+
456460
.. function:: void \
457461
gcc_jit_context_set_bool_allow_unreachable_blocks (gcc_jit_context *ctxt, \
458462
int bool_value)

gcc/jit/jit-recording.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,18 @@ recording::context::set_bool_option (enum gcc_jit_bool_option opt,
15111511
log_bool_option (opt);
15121512
}
15131513

1514+
int
1515+
recording::context::get_bool_option (enum gcc_jit_bool_option opt)
1516+
{
1517+
if (opt < 0 || opt >= GCC_JIT_NUM_BOOL_OPTIONS)
1518+
{
1519+
add_error (NULL,
1520+
"unrecognized (enum gcc_jit_bool_option) value: %i", opt);
1521+
return 0;
1522+
}
1523+
return m_bool_options[opt];
1524+
}
1525+
15141526
void
15151527
recording::context::set_inner_bool_option (enum inner_bool_option inner_opt,
15161528
int value)
@@ -1863,7 +1875,8 @@ static const char * const
18631875
"GCC_JIT_BOOL_OPTION_DUMP_SUMMARY",
18641876
"GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING",
18651877
"GCC_JIT_BOOL_OPTION_SELFCHECK_GC",
1866-
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES"
1878+
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES",
1879+
"GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES",
18671880
};
18681881

18691882
static const char * const

gcc/jit/jit-recording.h

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class context : public log_user
267267
set_bool_option (enum gcc_jit_bool_option opt,
268268
int value);
269269

270+
int
271+
get_bool_option (enum gcc_jit_bool_option opt);
272+
270273
void
271274
set_inner_bool_option (enum inner_bool_option inner_opt,
272275
int value);

gcc/jit/libgccjit.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -1220,18 +1220,22 @@ gcc_jit_context_new_function (gcc_jit_context *ctxt,
12201220
Eventually we'll need some way to interact with e.g. C++ name
12211221
mangling. */
12221222
{
1223+
int special_chars_allowed
1224+
= ctxt->get_bool_option (GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES);
12231225
/* Leading char: */
12241226
char ch = *name;
12251227
RETURN_NULL_IF_FAIL_PRINTF2 (
1226-
ISALPHA (ch) || ch == '_',
1228+
ISALPHA (ch) || ch == '_' || (special_chars_allowed
1229+
&& (ch == '.' || ch == '$' || ch == '*')),
12271230
ctxt, loc,
12281231
"name \"%s\" contains invalid character: '%c'",
12291232
name, ch);
12301233
/* Subsequent chars: */
12311234
for (const char *ptr = name + 1; (ch = *ptr); ptr++)
12321235
{
12331236
RETURN_NULL_IF_FAIL_PRINTF2 (
1234-
ISALNUM (ch) || ch == '_',
1237+
ISALNUM (ch) || ch == '_' || (special_chars_allowed
1238+
&& (ch == '.' || ch == '$' || ch == '*')),
12351239
ctxt, loc,
12361240
"name \"%s\" contains invalid character: '%c'",
12371241
name, ch);

gcc/jit/libgccjit.h

+3
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ enum gcc_jit_bool_option
244244
their location on stderr. */
245245
GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
246246

247+
/* If true, allow special characters like . and $ in function names. */
248+
GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES,
249+
247250
GCC_JIT_NUM_BOOL_OPTIONS
248251
};
249252

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
gcc_jit_context_set_bool_option (ctxt,
12+
GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, 1);
13+
14+
/* Let's try to inject the equivalent of:
15+
void
16+
name$with.special_chars (void)
17+
{
18+
}
19+
*/
20+
gcc_jit_type *void_type =
21+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
22+
23+
/* Build the test_fn. */
24+
gcc_jit_function *test_fn =
25+
gcc_jit_context_new_function (ctxt, NULL,
26+
GCC_JIT_FUNCTION_EXPORTED,
27+
void_type,
28+
"name$with.special_chars",
29+
0, NULL,
30+
0);
31+
32+
gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
33+
gcc_jit_block_end_with_void_return (
34+
block, NULL);
35+
}
36+
37+
void
38+
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
39+
{
40+
CHECK_NON_NULL (result);
41+
}

0 commit comments

Comments
 (0)