Unified · Split
| @@ -1094,6 +1094,7 @@ ST_FUNC void gexpr(void); · 1093 unmodified lines | ||
| 1094 | 1094 | ST_FUNC int expr_const(void); |
| 1095 | 1095 | ST_FUNC void gen_inline_functions(void); |
| 1096 | 1096 | ST_FUNC void decl(int l); |
| 1097 | +ST_FUNC void for_loop_init(); | |
| 1097 | 1098 | #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67 |
| 1098 | 1099 | ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size); |
| 1099 | 1100 | #endif |
| @@ -4376,8 +4376,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 4375 unmodified lines | ||
| 4376 | 4376 | next(); |
| 4377 | 4377 | skip('('); |
| 4378 | 4378 | if (tok != ';') { |
| 4379 | − gexpr(); | |
| 4380 | − vpop(); | |
| 4379 | + for_loop_init(); | |
| 4381 | 4380 | } |
| 4382 | 4381 | skip(';'); |
| 4383 | 4382 | d = ind; |
| @@ -5403,7 +5402,7 @@ ST_FUNC void gen_inline_functions(void) · 1019 unmodified lines | ||
| 5403 | 5402 | } |
| 5404 | 5403 | |
| 5405 | 5404 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 5406 | −ST_FUNC void decl(int l) | |
| 5405 | +static void decl0(int l, int is_for_loop_init) | |
| 5407 | 5406 | { |
| 5408 | 5407 | int v, has_init, r; |
| 5409 | 5408 | CType type, btype; |
| @@ -5412,6 +5411,11 @@ ST_FUNC void decl(int l) · 2 unmodified lines | ||
| 5412 | 5411 | |
| 5413 | 5412 | while (1) { |
| 5414 | 5413 | if (!parse_btype(&btype, &ad)) { |
| 5414 | + if (is_for_loop_init) { | |
| 5415 | + gexpr(); | |
| 5416 | + vpop(); | |
| 5417 | + return; | |
| 5418 | + } | |
| 5415 | 5419 | /* skip redundant ';' */ |
| 5416 | 5420 | /* XXX: find more elegant solution */ |
| 5417 | 5421 | if (tok == ';') { |
| @@ -5626,6 +5630,8 @@ ST_FUNC void decl(int l) · 208 unmodified lines | ||
| 5626 | 5630 | } |
| 5627 | 5631 | } |
| 5628 | 5632 | if (tok != ',') { |
| 5633 | + if (is_for_loop_init) | |
| 5634 | + return; | |
| 5629 | 5635 | skip(';'); |
| 5630 | 5636 | break; |
| 5631 | 5637 | } |
| @@ -5634,3 +5640,13 @@ ST_FUNC void decl(int l) · 2 unmodified lines | ||
| 5634 | 5640 | } |
| 5635 | 5641 | } |
| 5636 | 5642 | } |
| 5643 | + | |
| 5644 | +ST_FUNC void for_loop_init(void) | |
| 5645 | +{ | |
| 5646 | + decl0(VT_LOCAL, 1); | |
| 5647 | +} | |
| 5648 | + | |
| 5649 | +ST_FUNC void decl(int l) | |
| 5650 | +{ | |
| 5651 | + decl0(l, 0); | |
| 5652 | +} | |
| @@ -42,7 +42,7 @@ libtcc_test$(EXESUF): libtcc_test.c ../$(LIBTCC) · 41 unmodified lines | ||
| 42 | 42 | # copy only tcclib.h so GCC's stddef and stdarg will be used |
| 43 | 43 | test.ref: tcctest.c |
| 44 | 44 | cp -u ../include/tcclib.h . |
| 45 | − $(CC) -o tcctest.gcc $< -I. -w $(CFLAGS) | |
| 45 | + $(CC) -o tcctest.gcc $< -I. -w $(CFLAGS) -std=gnu99 | |
| 46 | 46 | ./tcctest.gcc > $@ |
| 47 | 47 | |
| 48 | 48 | # auto test |
| @@ -387,6 +387,10 @@ void loop_test() · 386 unmodified lines | ||
| 387 | 387 | } while (i < 10); |
| 388 | 388 | printf("\n"); |
| 389 | 389 | |
| 390 | + /* c99 for loop init test */ | |
| 391 | + for (size_t count = 1; count < 3; count++) | |
| 392 | + printf("count=%d\n", count); | |
| 393 | + | |
| 390 | 394 | /* break/continue tests */ |
| 391 | 395 | i = 0; |
| 392 | 396 | while (1) { |