ref: ccf4a8a144773066b8f63ddba1b92ff57d22ffa8
parent: bb1e1b0015d417d616261d5acb438bd74de1919e
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 15 09:58:47 EDT 2014
Fix sizeof The grammar specifies that a type name must be enclosed between parenthesis, so this code uses a look ahead to can difference between a type name and an expression (that the grammar specifies it is a unary expression)
--- a/expr.c
+++ b/expr.c
@@ -369,30 +369,25 @@
{
register Node *np;
Type *tp;
- char paren, op, *err;
+ char op, *err;
uint8_t t;
switch (yytoken) {
case SIZEOF:
next();
- if (accept('('))
- paren = 1;
- switch (yytoken) {
- case TQUALIFIER: case TYPE:
+ if (yytoken == '(' && ahead() == TYPE) {
+ next();
tp = typename();
- break;
- default:
- tp = expr()->type;
+ expect(')');
+ } else {
+ tp = unary()->type;
/* TODO: Free memory */
- break;
}
- if (paren)
- expect(')');
return sizeofcode(tp);
case INC: case DEC:
op = (yytoken == INC) ? OINC : ODEC;
next();
- return incdec(unary(), op); /* TODO: unary or cast? */
+ return incdec(unary(), op);
case '!': op = OEXC; break;
case '&': op = OADDR; break;
case '*': op = OPTR; break;