Skip to content

Commit ede1409

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 452abe1 commit ede1409

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

gcc/jit/docs/topics/contexts.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,18 @@ 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+
Alphabetic ASCII characters and underscores are always valid. Numeric
328+
ASCII characters are always valid after the initial character of the
329+
string. Use this entrypoint to specify additional ASCII characters that
330+
are valid throughout function names (ex.: ".$").
331+
327332
Boolean options
328333
***************
329334

gcc/jit/jit-recording.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,18 @@ recording::context::set_str_option (enum gcc_jit_str_option opt,
14241424
log_str_option (opt);
14251425
}
14261426

1427+
const char*
1428+
recording::context::get_str_option (enum gcc_jit_str_option opt)
1429+
{
1430+
if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
1431+
{
1432+
add_error (NULL,
1433+
"unrecognized (enum gcc_jit_str_option) value: %i", opt);
1434+
return NULL;
1435+
}
1436+
return m_str_options[opt];
1437+
}
1438+
14271439
/* Set the given integer option for this context, or add an error if
14281440
it's not recognized.
14291441
@@ -1765,7 +1777,8 @@ recording::context::dump_to_file (const char *path, bool update_locations)
17651777

17661778
static const char * const
17671779
str_option_reproducer_strings[GCC_JIT_NUM_STR_OPTIONS] = {
1768-
"GCC_JIT_STR_OPTION_PROGNAME"
1780+
"GCC_JIT_STR_OPTION_PROGNAME",
1781+
"GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES",
17691782
};
17701783

17711784
static const char * const
@@ -1782,7 +1795,7 @@ static const char * const
17821795
"GCC_JIT_BOOL_OPTION_DUMP_SUMMARY",
17831796
"GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING",
17841797
"GCC_JIT_BOOL_OPTION_SELFCHECK_GC",
1785-
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES"
1798+
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES",
17861799
};
17871800

17881801
static const char * const

gcc/jit/jit-recording.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ class context : public log_user
255255
set_str_option (enum gcc_jit_str_option opt,
256256
const char *value);
257257

258+
const char*
259+
get_str_option (enum gcc_jit_str_option opt);
260+
258261
void
259262
set_int_option (enum gcc_jit_int_option opt,
260263
int value);

gcc/jit/libgccjit.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,18 +1201,22 @@ gcc_jit_context_new_function (gcc_jit_context *ctxt,
12011201
Eventually we'll need some way to interact with e.g. C++ name
12021202
mangling. */
12031203
{
1204+
const char* special_chars_allowed
1205+
= ctxt->get_str_option (GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES);
12041206
/* Leading char: */
12051207
char ch = *name;
12061208
RETURN_NULL_IF_FAIL_PRINTF2 (
1207-
ISALPHA (ch) || ch == '_',
1209+
ISALPHA (ch) || ch == '_' || (special_chars_allowed
1210+
&& strchr (special_chars_allowed, ch)),
12081211
ctxt, loc,
12091212
"name \"%s\" contains invalid character: '%c'",
12101213
name, ch);
12111214
/* Subsequent chars: */
12121215
for (const char *ptr = name + 1; (ch = *ptr); ptr++)
12131216
{
12141217
RETURN_NULL_IF_FAIL_PRINTF2 (
1215-
ISALNUM (ch) || ch == '_',
1218+
ISALNUM (ch) || ch == '_' || (special_chars_allowed
1219+
&& strchr (special_chars_allowed, ch)),
12161220
ctxt, loc,
12171221
"name \"%s\" contains invalid character: '%c'",
12181222
name, ch);

gcc/jit/libgccjit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ enum gcc_jit_str_option
177177
messages to stderr. If NULL, or default, "libgccjit.so" is used. */
178178
GCC_JIT_STR_OPTION_PROGNAME,
179179

180+
/* Special characters to allow in function names. */
181+
GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES,
182+
180183
GCC_JIT_NUM_STR_OPTIONS
181184
};
182185

Lines changed: 41 additions & 0 deletions
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)