mirror of
git://repo.or.cz/tinycc.git
synced 2026-07-03 18:08:40 +08:00
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:
parent
90e9e34bec
commit
0ee4989ed3
23
libtcc.c
23
libtcc.c
@ -751,12 +751,15 @@ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
|
|||||||
return tcc_compile(s, s->filetype, str, -1);
|
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)
|
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
|
||||||
{
|
{
|
||||||
if (!value)
|
const char *eq;
|
||||||
value = "1";
|
if (NULL == (eq = strchr(sym, '=')))
|
||||||
cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
|
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 */
|
/* undefine a preprocessor symbol */
|
||||||
@ -1713,16 +1716,6 @@ static const FlagDef options_m[] = {
|
|||||||
{ 0, 0, NULL }
|
{ 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)
|
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
|
||||||
{
|
{
|
||||||
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
|
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
|
||||||
@ -1863,7 +1856,7 @@ reparse:
|
|||||||
tcc_add_include_path(s, optarg);
|
tcc_add_include_path(s, optarg);
|
||||||
break;
|
break;
|
||||||
case TCC_OPTION_D:
|
case TCC_OPTION_D:
|
||||||
parse_option_D(s, optarg);
|
tcc_define_symbol(s, optarg, NULL);
|
||||||
break;
|
break;
|
||||||
case TCC_OPTION_U:
|
case TCC_OPTION_U:
|
||||||
tcc_undefine_symbol(s, optarg);
|
tcc_undefine_symbol(s, optarg);
|
||||||
|
|||||||
2
libtcc.h
2
libtcc.h
@ -45,7 +45,7 @@ LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname);
|
|||||||
/* add in system include path */
|
/* add in system include path */
|
||||||
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
|
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);
|
LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
|
||||||
|
|
||||||
/* undefine preprocess symbol 'sym' */
|
/* undefine preprocess symbol 'sym' */
|
||||||
|
|||||||
@ -104,26 +104,8 @@ void parse_args(TCCState *s)
|
|||||||
tcc_add_include_path(s, a+2);
|
tcc_add_include_path(s, a+2);
|
||||||
else if (a[1] == 'L')
|
else if (a[1] == 'L')
|
||||||
tcc_add_library_path(s, a+2);
|
tcc_add_library_path(s, a+2);
|
||||||
else if (a[1] == 'D') {
|
else if (a[1] == 'D')
|
||||||
#if defined(__linux__) \
|
tcc_define_symbol(s, a+2, NULL);
|
||||||
&& 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -273,32 +255,32 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
printf("mixed calls\n "), fflush(stdout);
|
printf("running fib with mixed calls\n "), fflush(stdout);
|
||||||
t = getclock_ms();
|
t = getclock_ms();
|
||||||
state_test();
|
state_test();
|
||||||
printf("\n(%u ms)\n", getclock_ms() - t);
|
printf("\n (%u ms)\n", getclock_ms() - t);
|
||||||
#endif
|
#endif
|
||||||
#if 1
|
#if 1
|
||||||
printf("threads\n "), fflush(stdout);
|
printf("running fib in threads\n "), fflush(stdout);
|
||||||
t = getclock_ms();
|
t = getclock_ms();
|
||||||
for (n = 0; n < M; ++n)
|
for (n = 0; n < M; ++n)
|
||||||
create_thread(thread_test_simple, n);
|
create_thread(thread_test_simple, n);
|
||||||
wait_threads(n);
|
wait_threads(n);
|
||||||
printf("\n(%u ms)\n", getclock_ms() - t);
|
printf("\n (%u ms)\n", getclock_ms() - t);
|
||||||
#endif
|
#endif
|
||||||
#if 1
|
#if 1
|
||||||
printf("tcc in threads\n "), fflush(stdout);
|
printf("running tcc.c in threads to run fib\n"), fflush(stdout);
|
||||||
t = getclock_ms();
|
t = getclock_ms();
|
||||||
for (n = 0; n < M; ++n)
|
for (n = 0; n < M; ++n)
|
||||||
create_thread(thread_test_complex, n);
|
create_thread(thread_test_complex, n);
|
||||||
wait_threads(n);
|
wait_threads(n);
|
||||||
printf("\n(%u ms)\n", getclock_ms() - t);
|
printf("\n (%u ms)\n", getclock_ms() - t);
|
||||||
#endif
|
#endif
|
||||||
#if 1
|
#if 1
|
||||||
printf("compiling tcc 10 times\n"), fflush(stdout);
|
printf("compiling tcc.c 10 times\n"), fflush(stdout);
|
||||||
t = getclock_ms();
|
t = getclock_ms();
|
||||||
time_tcc(10, argv[1]);
|
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
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user