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);

View File

@ -45,7 +45,7 @@ LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname);
/* add in system include path */
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
/* define preprocessor symbol 'sym'. Can put optional value */
/* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */
LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
/* undefine preprocess symbol 'sym' */

View File

@ -104,26 +104,8 @@ void parse_args(TCCState *s)
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
else if (a[1] == 'D') {
#if defined(__linux__) \
&& defined(__STDC_VERSION__) \
&& (__STDC_VERSION__ >= 201112L)
/*
* gcc -std=c11 no longer declare strdup which fails with
* coredump (at least on Linux x64).
*/
extern char* strdup(const char*);
#endif
char *dup = strdup(a);
char *eq = strchr(dup+2, '=');
if (eq) {
*eq = 0;
tcc_define_symbol(s, dup+2, eq+1);
*eq = '=';
} else
tcc_define_symbol(s, dup+2, NULL);
free(dup);
}
else if (a[1] == 'D')
tcc_define_symbol(s, a+2, NULL);
}
}
}
@ -273,32 +255,32 @@ int main(int argc, char **argv)
}
#if 1
printf("mixed calls\n "), fflush(stdout);
printf("running fib with mixed calls\n "), fflush(stdout);
t = getclock_ms();
state_test();
printf("\n(%u ms)\n", getclock_ms() - t);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
printf("threads\n "), fflush(stdout);
printf("running fib in threads\n "), fflush(stdout);
t = getclock_ms();
for (n = 0; n < M; ++n)
create_thread(thread_test_simple, n);
wait_threads(n);
printf("\n(%u ms)\n", getclock_ms() - t);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
printf("tcc in threads\n "), fflush(stdout);
printf("running tcc.c in threads to run fib\n"), fflush(stdout);
t = getclock_ms();
for (n = 0; n < M; ++n)
create_thread(thread_test_complex, n);
wait_threads(n);
printf("\n(%u ms)\n", getclock_ms() - t);
printf("\n (%u ms)\n", getclock_ms() - t);
#endif
#if 1
printf("compiling tcc 10 times\n"), fflush(stdout);
printf("compiling tcc.c 10 times\n"), fflush(stdout);
t = getclock_ms();
time_tcc(10, argv[1]);
printf("(%u ms)\n", (getclock_ms() - t) / 10), fflush(stdout);
printf(" (%u ms)\n", getclock_ms() - t), fflush(stdout);
#endif
return 0;
}