Skip to content

Commit b6f163f

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_str_option): New method. * jit-recording.h (get_str_option): New method. * libgccjit.cc (gcc_jit_context_new_function): Allow special characters in function names. * libgccjit.h (enum gcc_jit_str_option): New option. gcc/testsuite/ChangeLog: * jit.dg/test-special-chars.c: New test.
1 parent fbec9ec commit b6f163f

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

gcc/jit/docs/topics/contexts.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,17 @@ String Options
317317
copy of the underlying string, so it is valid to pass in a pointer to
318318
an on-stack buffer.
319319

320-
There is just one string option specified this way:
321-
322320
.. macro:: GCC_JIT_STR_OPTION_PROGNAME
323321

324322
The name of the program, for use as a prefix when printing error
325323
messages to stderr. If `NULL`, or default, "libgccjit.so" is used.
326324

325+
.. macro:: GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES
326+
327+
Special characters to allow in function names.
328+
This string contains all characters that should not be rejected by
329+
libgccjit. Ex.: ".$"
330+
327331
Boolean options
328332
***************
329333

gcc/jit/jit-recording.cc

+15-2
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,18 @@ recording::context::set_output_ident (const char *ident)
14711471
record (memento);
14721472
}
14731473

1474+
const char*
1475+
recording::context::get_str_option (enum gcc_jit_str_option opt)
1476+
{
1477+
if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
1478+
{
1479+
add_error (NULL,
1480+
"unrecognized (enum gcc_jit_str_option) value: %i", opt);
1481+
return NULL;
1482+
}
1483+
return m_str_options[opt];
1484+
}
1485+
14741486
/* Set the given integer option for this context, or add an error if
14751487
it's not recognized.
14761488
@@ -1846,7 +1858,8 @@ recording::context::dump_to_file (const char *path, bool update_locations)
18461858

18471859
static const char * const
18481860
str_option_reproducer_strings[GCC_JIT_NUM_STR_OPTIONS] = {
1849-
"GCC_JIT_STR_OPTION_PROGNAME"
1861+
"GCC_JIT_STR_OPTION_PROGNAME",
1862+
"GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES",
18501863
};
18511864

18521865
static const char * const
@@ -1863,7 +1876,7 @@ static const char * const
18631876
"GCC_JIT_BOOL_OPTION_DUMP_SUMMARY",
18641877
"GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING",
18651878
"GCC_JIT_BOOL_OPTION_SELFCHECK_GC",
1866-
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES"
1879+
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES",
18671880
};
18681881

18691882
static const char * const

gcc/jit/jit-recording.h

+3
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ class context : public log_user
256256
set_str_option (enum gcc_jit_str_option opt,
257257
const char *value);
258258

259+
const char*
260+
get_str_option (enum gcc_jit_str_option opt);
261+
259262
void
260263
set_output_ident (const char *output_ident);
261264

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+
const char* special_chars_allowed
1224+
= ctxt->get_str_option (GCC_JIT_STR_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+
&& strchr (special_chars_allowed, 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+
&& strchr (special_chars_allowed, 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
@@ -175,6 +175,9 @@ enum gcc_jit_str_option
175175
messages to stderr. If NULL, or default, "libgccjit.so" is used. */
176176
GCC_JIT_STR_OPTION_PROGNAME,
177177

178+
/* Special characters to allow in function names. */
179+
GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES,
180+
178181
GCC_JIT_NUM_STR_OPTIONS
179182
};
180183

+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_str_option (ctxt,
12+
GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, "$.");
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)