History

fixed preprocessor expression parsing

bellard committed 24 years ago

Commit 540cd6f1 parent 20f40851 file · +18 −9Browse files

Unified · Split

tcc.c+18−9
@@ -224,6 +224,7 @@ typedef struct TokenString { · 223 unmodified lines
224224 struct BufferedFile *file;
225225 int ch, ch1, tok, tok1;
226226 CValue tokc, tok1c;
227+int return_linefeed; /* if true, line feed is returned as a token */
227228
228229 /* sections */
229230 Section **sections;
@@ -426,7 +427,8 @@ struct TCCState { · 196 unmodified lines
426427 #define TOK_A_SHL 0x81
427428 #define TOK_A_SAR 0x82
428429
429−#define TOK_EOF (-1) /* end of file */
430+#define TOK_EOF (-1) /* end of file */
431+#define TOK_LINEFEED 10 /* line feed */
430432
431433 /* all identificators and strings have token above that */
432434 #define TOK_IDENT 256
@@ -1409,10 +1411,7 @@ int expr_preprocess(void) · 976 unmodified lines
14091411 TokenString str;
14101412
14111413 tok_str_new(&str);
1412− while (1) {
1413− skip_spaces();
1414− if (ch == '\n')
1415− break;
1414+ while (tok != TOK_LINEFEED && tok != TOK_EOF) {
14161415 next(); /* do macro subst */
14171416 if (tok == TOK_DEFINED) {
14181417 next_nomacro();
@@ -1507,7 +1506,7 @@ void parse_define(void) · 88 unmodified lines
15071506 tok_str_add(&str, 0);
15081507 #ifdef PP_DEBUG
15091508 printf("define %s %d: ", get_tok_str(v, NULL), t);
1510− tok_print(str);
1509+ tok_print(str.str);
15111510 #endif
15121511 s = sym_push1(&define_stack, v, t, (int)str.str);
15131512 s->next = first;
@@ -1521,6 +1520,7 @@ void preprocess(void) · 7 unmodified lines
15211520 BufferedFile *f;
15221521 Sym *s;
15231522
1523+ return_linefeed = 1; /* linefeed will be returned as a token */
15241524 cinp();
15251525 next_nomacro();
15261526 redo:
@@ -1598,6 +1598,8 @@ void preprocess(void) · 71 unmodified lines
15981598 if (do_debug) {
15991599 put_stabs(file->filename, N_BINCL, 0, 0, 0);
16001600 }
1601+ ch = '\n';
1602+ goto the_end;
16011603 } else if (tok == TOK_IFNDEF) {
16021604 c = 1;
16031605 goto do_ifdef;
@@ -1659,8 +1661,10 @@ void preprocess(void) · 55 unmodified lines
16591661 error("#error");
16601662 }
16611663 /* ignore other preprocess commands or #! for C scripts */
1662− while (ch != '\n' && ch != -1)
1663− cinp();
1664+ while (tok != TOK_LINEFEED && tok != TOK_EOF)
1665+ next_nomacro();
1666+ the_end:
1667+ return_linefeed = 0;
16641668 }
16651669
16661670 /* read a number in base b */
@@ -2043,6 +2047,11 @@ void next_nomacro1(void) · 376 unmodified lines
20432047 /* skip spaces */
20442048 while(1) {
20452049 while (ch == '\n') {
2050+ /* during preprocessor parsing, '\n' is a token */
2051+ if (return_linefeed) {
2052+ tok = TOK_LINEFEED;
2053+ return;
2054+ }
20462055 cinp();
20472056 while (ch == ' ' || ch == '\t')
20482057 cinp();
@@ -2459,7 +2468,7 @@ void next(void) · 410 unmodified lines
24592468 }
24602469 }
24612470 #if defined(DEBUG)
2462− printf("token = %s\n", get_tok_str(tok, tokc));
2471+ printf("token = %s\n", get_tok_str(tok, &tokc));
24632472 #endif
24642473 }
24652474

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