libtcc: accept tcc_define_symbol(s1, "sym=value", NULL)

Thus it can parse command-line -Dsym=value directly, for the
convenience of libtcc users or tcc itself

Also used in libtcc_test_mt.c to avoid strdup().
This commit is contained in:
grischka
2020-06-27 17:02:12 +02:00
parent 90e9e34bec
commit 0ee4989ed3
3 changed files with 19 additions and 44 deletions

View File

@@ -751,12 +751,15 @@ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
return tcc_compile(s, s->filetype, str, -1);
}
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
/* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
{
if (!value)
value = "1";
cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
const char *eq;
if (NULL == (eq = strchr(sym, '=')))
eq = strchr(sym, 0);
if (NULL == value)
value = *eq ? eq + 1 : "1";
cstr_printf(&s1->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value);
}
/* undefine a preprocessor symbol */
@@ -1713,16 +1716,6 @@ static const FlagDef options_m[] = {
{ 0, 0, NULL }
};
static void parse_option_D(TCCState *s1, const char *optarg)
{
char *sym = tcc_strdup(optarg);
char *value = strchr(sym, '=');
if (value)
*value++ = '\0';
tcc_define_symbol(s1, sym, value);
tcc_free(sym);
}
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
{
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
@@ -1863,7 +1856,7 @@ reparse:
tcc_add_include_path(s, optarg);
break;
case TCC_OPTION_D:
parse_option_D(s, optarg);
tcc_define_symbol(s, optarg, NULL);
break;
case TCC_OPTION_U:
tcc_undefine_symbol(s, optarg);