Skip to content

Commit

Permalink
libgccjit: Add option to allow special characters in function names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
antoyo committed Mar 1, 2024
1 parent fbec9ec commit b6f163f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
8 changes: 6 additions & 2 deletions gcc/jit/docs/topics/contexts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,17 @@ String Options
copy of the underlying string, so it is valid to pass in a pointer to
an on-stack buffer.

There is just one string option specified this way:

.. macro:: GCC_JIT_STR_OPTION_PROGNAME

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

.. macro:: GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES

Special characters to allow in function names.
This string contains all characters that should not be rejected by
libgccjit. Ex.: ".$"

Boolean options
***************

Expand Down
17 changes: 15 additions & 2 deletions gcc/jit/jit-recording.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,18 @@ recording::context::set_output_ident (const char *ident)
record (memento);
}

const char*
recording::context::get_str_option (enum gcc_jit_str_option opt)
{
if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
{
add_error (NULL,
"unrecognized (enum gcc_jit_str_option) value: %i", opt);
return NULL;
}
return m_str_options[opt];
}

/* Set the given integer option for this context, or add an error if
it's not recognized.
Expand Down Expand Up @@ -1846,7 +1858,8 @@ recording::context::dump_to_file (const char *path, bool update_locations)

static const char * const
str_option_reproducer_strings[GCC_JIT_NUM_STR_OPTIONS] = {
"GCC_JIT_STR_OPTION_PROGNAME"
"GCC_JIT_STR_OPTION_PROGNAME",
"GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES",
};

static const char * const
Expand All @@ -1863,7 +1876,7 @@ static const char * const
"GCC_JIT_BOOL_OPTION_DUMP_SUMMARY",
"GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING",
"GCC_JIT_BOOL_OPTION_SELFCHECK_GC",
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES"
"GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES",
};

static const char * const
Expand Down
3 changes: 3 additions & 0 deletions gcc/jit/jit-recording.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ class context : public log_user
set_str_option (enum gcc_jit_str_option opt,
const char *value);

const char*
get_str_option (enum gcc_jit_str_option opt);

void
set_output_ident (const char *output_ident);

Expand Down
8 changes: 6 additions & 2 deletions gcc/jit/libgccjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1220,18 +1220,22 @@ gcc_jit_context_new_function (gcc_jit_context *ctxt,
Eventually we'll need some way to interact with e.g. C++ name
mangling. */
{
const char* special_chars_allowed
= ctxt->get_str_option (GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES);
/* Leading char: */
char ch = *name;
RETURN_NULL_IF_FAIL_PRINTF2 (
ISALPHA (ch) || ch == '_',
ISALPHA (ch) || ch == '_' || (special_chars_allowed
&& strchr (special_chars_allowed, ch)),
ctxt, loc,
"name \"%s\" contains invalid character: '%c'",
name, ch);
/* Subsequent chars: */
for (const char *ptr = name + 1; (ch = *ptr); ptr++)
{
RETURN_NULL_IF_FAIL_PRINTF2 (
ISALNUM (ch) || ch == '_',
ISALNUM (ch) || ch == '_' || (special_chars_allowed
&& strchr (special_chars_allowed, ch)),
ctxt, loc,
"name \"%s\" contains invalid character: '%c'",
name, ch);
Expand Down
3 changes: 3 additions & 0 deletions gcc/jit/libgccjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ enum gcc_jit_str_option
messages to stderr. If NULL, or default, "libgccjit.so" is used. */
GCC_JIT_STR_OPTION_PROGNAME,

/* Special characters to allow in function names. */
GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES,

GCC_JIT_NUM_STR_OPTIONS
};

Expand Down
41 changes: 41 additions & 0 deletions gcc/testsuite/jit.dg/test-special-chars.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdlib.h>
#include <stdio.h>

#include "libgccjit.h"

#include "harness.h"

void
create_code (gcc_jit_context *ctxt, void *user_data)
{
gcc_jit_context_set_str_option (ctxt,
GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, "$.");

/* Let's try to inject the equivalent of:
void
name$with.special_chars (void)
{
}
*/
gcc_jit_type *void_type =
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);

/* Build the test_fn. */
gcc_jit_function *test_fn =
gcc_jit_context_new_function (ctxt, NULL,
GCC_JIT_FUNCTION_EXPORTED,
void_type,
"name$with.special_chars",
0, NULL,
0);

gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
gcc_jit_block_end_with_void_return (
block, NULL);
}

void
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
{
CHECK_NON_NULL (result);
}

0 comments on commit b6f163f

Please sign in to comment.