[GNU] Allow a variable as an operand of _Alignof

This commit is contained in:
Rui Ueyama 2020-09-26 08:09:59 +09:00
parent 9df51789e7
commit 310a87e15e
2 changed files with 14 additions and 3 deletions

12
parse.c
View File

@ -2005,6 +2005,7 @@ static Node *funcall(Token **rest, Token *tok) {
// | "sizeof" "(" type-name ")"
// | "sizeof" unary
// | "_Alignof" "(" type-name ")"
// | "_Alignof" unary
// | ident func-args?
// | str
// | num
@ -2037,13 +2038,18 @@ static Node *primary(Token **rest, Token *tok) {
return new_num(node->ty->size, tok);
}
if (equal(tok, "_Alignof")) {
tok = skip(tok->next, "(");
Type *ty = typename(&tok, tok);
if (equal(tok, "_Alignof") && equal(tok->next, "(") && is_typename(tok->next->next)) {
Type *ty = typename(&tok, tok->next->next);
*rest = skip(tok, ")");
return new_num(ty->align, tok);
}
if (equal(tok, "_Alignof")) {
Node *node = unary(rest, tok->next);
add_type(node);
return new_num(node->ty->align, tok);
}
if (tok->kind == TK_IDENT) {
// Function call
if (equal(tok->next, "("))

View File

@ -30,6 +30,11 @@ int main() {
ASSERT(0, (long)(char *)&g4 % 4);
ASSERT(0, (long)(char *)&g5 % 8);
ASSERT(1, ({ char x; _Alignof(x); }));
ASSERT(4, ({ int x; _Alignof(x); }));
ASSERT(1, ({ char x; _Alignof x; }));
ASSERT(4, ({ int x; _Alignof x; }));
printf("OK\n");
return 0;
}