commit 15b64d5d2636103fdeafea92ea7886dbd23c6574
parent 5d10a2436b4df6c82723bb7174451ab267512155
Author: Zhaoming Luo <zhml@posteo.com>
Date: Tue, 2 Jun 2026 11:18:09 +0000
cc1: Error when no struct identifier and '{'
In c99 spec 6.7.2.1.1 `struct-or-union-specifier`, when there is an
identifier following `struct-or-union`, it's optional to have
`{ struct-declaration-list }` after the identifier; if the
identifier does not exist, there must be
`{ struct-declaration-list }`.
In structdcl(), in the error case where the token after
`struct-or-union` is neither an identifier nor '{', a placeholder
type is created in newtag() and returned.
Compiling 'struct const s { int v; };' gives error message:
```
test.c:1: error: expected ';' before 'const'
test.c:1: error: expected ';' before '{'
..
test.c:1: error: expected ';' before '{'
too many errors
```
Error when the token after `struct-or-union` is neither an
identifier nor '{'. The error message will be more understandable:
```
test.c:1: error: unexpected 'const'
```
Diffstat:
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/cmd/scc-cc/cc1/decl.c b/src/cmd/scc-cc/cc1/decl.c
@@ -833,8 +833,11 @@ structdcl(void)
sym = newtag();
tp = sym->type;
- if (!accept('{'))
+ if (!accept('{')) {
+ if (sym->name == NULL)
+ unexpected();
return tp;
+ }
ns = namespace;
namespace = tp->ns;
@@ -869,6 +872,8 @@ enumdcl(void)
namespace = NS_IDEN;
if (!accept('{')) {
+ if (sym->name == NULL)
+ unexpected();
namespace = ns;
return tp;
}