fixed preprocessor expression parsing

tcc-xref
bellard 2002-07-26 22:59:19 +00:00
parent 20f4085145
commit 540cd6fde7
1 changed files with 18 additions and 9 deletions

27
tcc.c
View File

@ -224,6 +224,7 @@ typedef struct TokenString {
struct BufferedFile *file; struct BufferedFile *file;
int ch, ch1, tok, tok1; int ch, ch1, tok, tok1;
CValue tokc, tok1c; CValue tokc, tok1c;
int return_linefeed; /* if true, line feed is returned as a token */
/* sections */ /* sections */
Section **sections; Section **sections;
@ -426,7 +427,8 @@ struct TCCState {
#define TOK_A_SHL 0x81 #define TOK_A_SHL 0x81
#define TOK_A_SAR 0x82 #define TOK_A_SAR 0x82
#define TOK_EOF (-1) /* end of file */ #define TOK_EOF (-1) /* end of file */
#define TOK_LINEFEED 10 /* line feed */
/* all identificators and strings have token above that */ /* all identificators and strings have token above that */
#define TOK_IDENT 256 #define TOK_IDENT 256
@ -1409,10 +1411,7 @@ int expr_preprocess(void)
TokenString str; TokenString str;
tok_str_new(&str); tok_str_new(&str);
while (1) { while (tok != TOK_LINEFEED && tok != TOK_EOF) {
skip_spaces();
if (ch == '\n')
break;
next(); /* do macro subst */ next(); /* do macro subst */
if (tok == TOK_DEFINED) { if (tok == TOK_DEFINED) {
next_nomacro(); next_nomacro();
@ -1507,7 +1506,7 @@ void parse_define(void)
tok_str_add(&str, 0); tok_str_add(&str, 0);
#ifdef PP_DEBUG #ifdef PP_DEBUG
printf("define %s %d: ", get_tok_str(v, NULL), t); printf("define %s %d: ", get_tok_str(v, NULL), t);
tok_print(str); tok_print(str.str);
#endif #endif
s = sym_push1(&define_stack, v, t, (int)str.str); s = sym_push1(&define_stack, v, t, (int)str.str);
s->next = first; s->next = first;
@ -1521,6 +1520,7 @@ void preprocess(void)
BufferedFile *f; BufferedFile *f;
Sym *s; Sym *s;
return_linefeed = 1; /* linefeed will be returned as a token */
cinp(); cinp();
next_nomacro(); next_nomacro();
redo: redo:
@ -1598,6 +1598,8 @@ void preprocess(void)
if (do_debug) { if (do_debug) {
put_stabs(file->filename, N_BINCL, 0, 0, 0); put_stabs(file->filename, N_BINCL, 0, 0, 0);
} }
ch = '\n';
goto the_end;
} else if (tok == TOK_IFNDEF) { } else if (tok == TOK_IFNDEF) {
c = 1; c = 1;
goto do_ifdef; goto do_ifdef;
@ -1659,8 +1661,10 @@ void preprocess(void)
error("#error"); error("#error");
} }
/* ignore other preprocess commands or #! for C scripts */ /* ignore other preprocess commands or #! for C scripts */
while (ch != '\n' && ch != -1) while (tok != TOK_LINEFEED && tok != TOK_EOF)
cinp(); next_nomacro();
the_end:
return_linefeed = 0;
} }
/* read a number in base b */ /* read a number in base b */
@ -2043,6 +2047,11 @@ void next_nomacro1(void)
/* skip spaces */ /* skip spaces */
while(1) { while(1) {
while (ch == '\n') { while (ch == '\n') {
/* during preprocessor parsing, '\n' is a token */
if (return_linefeed) {
tok = TOK_LINEFEED;
return;
}
cinp(); cinp();
while (ch == ' ' || ch == '\t') while (ch == ' ' || ch == '\t')
cinp(); cinp();
@ -2459,7 +2468,7 @@ void next(void)
} }
} }
#if defined(DEBUG) #if defined(DEBUG)
printf("token = %s\n", get_tok_str(tok, tokc)); printf("token = %s\n", get_tok_str(tok, &tokc));
#endif #endif
} }