mirror of
https://github.com/rui314/chibicc
synced 2024-12-01 18:26:56 +03:00
Replace remaining identifiers with 0 in macro constexpr
This commit is contained in:
parent
5cb2f89e6a
commit
a8d76ad435
12
preprocess.c
12
preprocess.c
@ -280,6 +280,18 @@ static long eval_const_expr(Token **rest, Token *tok) {
|
||||
if (expr->kind == TK_EOF)
|
||||
error_tok(start, "no expression");
|
||||
|
||||
// [https://www.sigbus.info/n1570#6.10.1p4] The standard requires
|
||||
// we replace remaining non-macro identifiers with "0" before
|
||||
// evaluating a constant expression. For example, `#if foo` is
|
||||
// equivalent to `#if 0` if foo is not defined.
|
||||
for (Token *t = expr; t->kind != TK_EOF; t = t->next) {
|
||||
if (t->kind == TK_IDENT) {
|
||||
Token *next = t->next;
|
||||
*t = *new_num_token(0, t);
|
||||
t->next = next;
|
||||
}
|
||||
}
|
||||
|
||||
Token *rest2;
|
||||
long val = const_expr(&rest2, expr);
|
||||
if (rest2->kind != TK_EOF)
|
||||
|
@ -283,6 +283,13 @@ int main() {
|
||||
#endif
|
||||
ASSERT(4, m);
|
||||
|
||||
#if no_such_symbol == 0
|
||||
m = 5;
|
||||
#else
|
||||
m = 6;
|
||||
#endif
|
||||
ASSERT(5, m);
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user