fix defined() handling

This commit is contained in:
bellard 2001-11-08 01:26:37 +00:00
parent a254461560
commit cdf6247518

39
tcc.c
View File

@ -496,8 +496,8 @@ char *get_str(c)
return str; return str;
} }
/* return 1 if next token is defined, 0 otherwise. */ /* return next token without macro substitution */
int is_defined() void next_nomacro()
{ {
Sym *s; Sym *s;
/* hack to avoid replacing id by defined value */ /* hack to avoid replacing id by defined value */
@ -505,7 +505,6 @@ int is_defined()
define_stack = 0; define_stack = 0;
next(); next();
define_stack = s; define_stack = s;
return sym_find1(define_stack, tok) != 0;
} }
/* XXX: not correct yet (need to ensure it is constant) */ /* XXX: not correct yet (need to ensure it is constant) */
@ -528,20 +527,21 @@ int expr_preprocess()
void preprocess() void preprocess()
{ {
char *str; char *str;
int size, n, c; int size, n, c, v;
char buf[1024], *q, *p; char buf[1024], *q, *p;
char buf1[1024]; char buf1[1024];
FILE *f; FILE *f;
cinp(); cinp();
next(); /* XXX: should pass parameter to avoid macro subst */ next_nomacro();
redo: redo:
if (tok == TOK_DEFINE) { if (tok == TOK_DEFINE) {
next(); /* XXX: should pass parameter to avoid macro subst */ next_nomacro(); /* XXX: should pass parameter to avoid macro subst */
skip_spaces(); skip_spaces();
/* now 'tok' is the macro symbol */ /* now 'tok' is the macro symbol */
/* a space is inserted after each macro */ /* a space is inserted after each macro */
str = get_str(' '); str = get_str(' ');
printf("define %s '%s'\n", get_tok_str(tok), str);
sym_push1(&define_stack, tok, 0, (int)str); sym_push1(&define_stack, tok, 0, (int)str);
} else if (tok == TOK_INCLUDE) { } else if (tok == TOK_INCLUDE) {
skip_spaces(); skip_spaces();
@ -594,15 +594,17 @@ void preprocess()
line_num = 1; line_num = 1;
} }
} else if (tok == TOK_IFNDEF) { } else if (tok == TOK_IFNDEF) {
c = !is_defined(); c = 1;
goto do_ifdef; goto do_ifdef;
} else if (tok == TOK_IF) { } else if (tok == TOK_IF) {
/* XXX: incorrect constant parsing now */
c = expr_preprocess(); c = expr_preprocess();
goto do_ifdef; goto do_if;
} else if (tok == TOK_IFDEF) { } else if (tok == TOK_IFDEF) {
c = is_defined(); c = 0;
do_ifdef: do_ifdef:
next_nomacro();
c = (sym_find1(define_stack, tok) != 0) ^ c;
do_if:
if (ifdef_stack_ptr >= ifdef_stack + IFDEF_STACK_SIZE) if (ifdef_stack_ptr >= ifdef_stack + IFDEF_STACK_SIZE)
error("memory full"); error("memory full");
*ifdef_stack_ptr++ = c; *ifdef_stack_ptr++ = c;
@ -1693,6 +1695,16 @@ void unary()
next(); next();
} }
*(char *)glo++ = 0; *(char *)glo++ = 0;
} else if (tok == TOK_DEFINED) {
/* XXX: should only be used in preprocess expr parsing */
next_nomacro();
t = tok;
if (t == '(')
next_nomacro();
vset(VT_CONST, sym_find1(define_stack, tok) != 0);
next();
if (t == '(')
skip(')');
} else { } else {
t = tok; t = tok;
next(); next();
@ -1753,13 +1765,6 @@ void unary()
vpush(); vpush();
unary(); unary();
gen_op('-'); gen_op('-');
} else if (t == TOK_DEFINED) {
/* XXX: should only be used in preprocess expr parsing */
if (tok != '(')
expect("(");
vset(VT_CONST, is_defined());
next();
skip(')');
} else } else
{ {
s = sym_find(t); s = sym_find(t);