mirror of https://github.com/rui314/chibicc
[GNU] Allow a variable as an operand of _Alignof
This commit is contained in:
parent
9df51789e7
commit
310a87e15e
12
parse.c
12
parse.c
|
@ -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, "("))
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue