scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit b8ad553523ba96ddf4c363befb1a5397b18af440
parent 7dbb2d53125f9592cf579c0e948c56eb886a8a3c
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Thu, 29 Jan 2026 20:53:34 +0100

cc1: Fold correctly unary in sizeof

When a parenthesis happens after a sizeof it can mean a type name or
an unary expression. The code was correctly detecting the type name
case, but in the unary case it assumed that the unary expresion was
equal to ( expr ) but unary can be also ( expr ) postfix, failling
in cases like:

	sizeof(array)[0]

Diffstat:
Msrc/cmd/scc-cc/cc1/expr.c | 47+++++++++++++++++++++++++----------------------
Atests/cc/execute/0236-sizeof.c | 8++++++++
Mtests/cc/execute/scc-tests.lst | 1+
3 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/expr.c b/src/cmd/scc-cc/cc1/expr.c @@ -851,28 +851,6 @@ typeof(Node *np) static Node *unary(void); -static Type * -sizeexp(void) -{ - Type *tp; - - if (!accept('(')) - return typeof(unary()); - - switch (yytoken) { - case TYPE: - case TYPEIDEN: - tp = typename(); - break; - default: - tp = typeof(expr()); - break; - } - expect(')'); - - return tp; -} - static Node * postfix(Node *lp) { @@ -909,6 +887,31 @@ postfix(Node *lp) } } +static Type * +sizeexp(void) +{ + Node *np; + Type *tp; + + if (!accept('(')) + return typeof(unary()); + + switch (yytoken) { + case TYPE: + case TYPEIDEN: + tp = typename(); + expect(')'); + break; + default: + np = expr(); + expect(')'); + tp = typeof(postfix(np)); + break; + } + + return tp; +} + static Node *cast(void); static Node * diff --git a/tests/cc/execute/0236-sizeof.c b/tests/cc/execute/0236-sizeof.c @@ -0,0 +1,8 @@ + +int +main(void) +{ + int nomatches[128]; + + return sizeof(nomatches)[0] - sizeof(int); +} diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst @@ -226,3 +226,4 @@ 0233-ifelif.c 0234-macro.c 0235-comment.c +0236-sizeof.c