Replace remaining identifiers with 0 in macro constexpr

This commit is contained in:
Rui Ueyama 2020-03-31 22:16:56 +09:00
parent 5cb2f89e6a
commit a8d76ad435
2 changed files with 19 additions and 0 deletions

View File

@ -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)

View File

@ -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;
}