History

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().

grischka committed 6 years ago

Commit 0ee49891 parent 90e9e343 files · +19 −44Browse files

Unified · Split

3 files
  1. libtcc.c +8 −15
  2. libtcc.h +1 −1
  3. tests/libtcc_test_mt.c +10 −28
libtcc.c+8−15
@@ -751,12 +751,15 @@ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) · 750 unmodified lines
751751 return tcc_compile(s, s->filetype, str, -1);
752752 }
753753
754−/* define a preprocessor symbol. A value can also be provided with the '=' operator */
754+/* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */
755755 LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
756756 {
757− if (!value)
758− value = "1";
759− cstr_printf(&s1->cmdline_defs, "#define %s %s\n", sym, value);
757+ const char *eq;
758+ if (NULL == (eq = strchr(sym, '=')))
759+ eq = strchr(sym, 0);
760+ if (NULL == value)
761+ value = *eq ? eq + 1 : "1";
762+ cstr_printf(&s1->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value);
760763 }
761764
762765 /* undefine a preprocessor symbol */
@@ -1713,16 +1716,6 @@ static const FlagDef options_m[] = { · 950 unmodified lines
17131716 { 0, 0, NULL }
17141717 };
17151718
1716−static void parse_option_D(TCCState *s1, const char *optarg)
1717−{
1718− char *sym = tcc_strdup(optarg);
1719− char *value = strchr(sym, '=');
1720− if (value)
1721− *value++ = '\0';
1722− tcc_define_symbol(s1, sym, value);
1723− tcc_free(sym);
1724−}
1725−
17261719 static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
17271720 {
17281721 struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
@@ -1863,7 +1856,7 @@ reparse: · 134 unmodified lines
18631856 tcc_add_include_path(s, optarg);
18641857 break;
18651858 case TCC_OPTION_D:
1866− parse_option_D(s, optarg);
1859+ tcc_define_symbol(s, optarg, NULL);
18671860 break;
18681861 case TCC_OPTION_U:
18691862 tcc_undefine_symbol(s, optarg);
libtcc.h+1−1
@@ -45,7 +45,7 @@ LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname); · 44 unmodified lines
4545 /* add in system include path */
4646 LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname);
4747
48−/* define preprocessor symbol 'sym'. Can put optional value */
48+/* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */
4949 LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value);
5050
5151 /* undefine preprocess symbol 'sym' */
tests/libtcc_test_mt.c+10−28
@@ -104,26 +104,8 @@ void parse_args(TCCState *s) · 103 unmodified lines
104104 tcc_add_include_path(s, a+2);
105105 else if (a[1] == 'L')
106106 tcc_add_library_path(s, a+2);
107− else if (a[1] == 'D') {
108−#if defined(__linux__) \
109− && defined(__STDC_VERSION__) \
110− && (__STDC_VERSION__ >= 201112L)
111− /*
112− * gcc -std=c11 no longer declare strdup which fails with
113− * coredump (at least on Linux x64).
114− */
115− extern char* strdup(const char*);
116−#endif
117− char *dup = strdup(a);
118− char *eq = strchr(dup+2, '=');
119− if (eq) {
120− *eq = 0;
121− tcc_define_symbol(s, dup+2, eq+1);
122− *eq = '=';
123− } else
124− tcc_define_symbol(s, dup+2, NULL);
125− free(dup);
126− }
107+ else if (a[1] == 'D')
108+ tcc_define_symbol(s, a+2, NULL);
127109 }
128110 }
129111 }
@@ -273,32 +255,32 @@ int main(int argc, char **argv) · 143 unmodified lines
273255 }
274256
275257 #if 1
276− printf("mixed calls\n "), fflush(stdout);
258+ printf("running fib with mixed calls\n "), fflush(stdout);
277259 t = getclock_ms();
278260 state_test();
279− printf("\n(%u ms)\n", getclock_ms() - t);
261+ printf("\n (%u ms)\n", getclock_ms() - t);
280262 #endif
281263 #if 1
282− printf("threads\n "), fflush(stdout);
264+ printf("running fib in threads\n "), fflush(stdout);
283265 t = getclock_ms();
284266 for (n = 0; n < M; ++n)
285267 create_thread(thread_test_simple, n);
286268 wait_threads(n);
287− printf("\n(%u ms)\n", getclock_ms() - t);
269+ printf("\n (%u ms)\n", getclock_ms() - t);
288270 #endif
289271 #if 1
290− printf("tcc in threads\n "), fflush(stdout);
272+ printf("running tcc.c in threads to run fib\n"), fflush(stdout);
291273 t = getclock_ms();
292274 for (n = 0; n < M; ++n)
293275 create_thread(thread_test_complex, n);
294276 wait_threads(n);
295− printf("\n(%u ms)\n", getclock_ms() - t);
277+ printf("\n (%u ms)\n", getclock_ms() - t);
296278 #endif
297279 #if 1
298− printf("compiling tcc 10 times\n"), fflush(stdout);
280+ printf("compiling tcc.c 10 times\n"), fflush(stdout);
299281 t = getclock_ms();
300282 time_tcc(10, argv[1]);
301− printf("(%u ms)\n", (getclock_ms() - t) / 10), fflush(stdout);
283+ printf(" (%u ms)\n", getclock_ms() - t), fflush(stdout);
302284 #endif
303285 return 0;
304286 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t