aboutsummaryrefslogtreecommitdiffhomepage
path: root/c.c
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2023-06-19 12:40:22 +0200
committerlemon <lsof@mailbox.org>2023-06-19 12:40:22 +0200
commit3bda62cd2460bb5a8d6cdff0dce9ad2fca26ee4f (patch)
tree475aa4ee12d41b3a3b53f183c6caa36318ed6f2a /c.c
parent013e4d624873cd47cc5ef2b801e13e9b669c7ae1 (diff)
frontend: disallow non-local decls in for initializer
Diffstat (limited to 'c.c')
-rw-r--r--c.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/c.c b/c.c
index fdcaa48..ec3d133 100644
--- a/c.c
+++ b/c.c
@@ -1617,7 +1617,7 @@ stmtterm(struct comp *cm)
static void block(struct comp *cm, struct function *fn);
static bool stmt(struct comp *cm, struct function *fn);
-static void localdecl(struct comp *cm, struct function *fn);
+static void localdecl(struct comp *cm, struct function *fn, bool forinit);
static bool
loopbody(struct comp *cm, struct function *fn, struct block *brk, struct block *cont)
@@ -1778,7 +1778,7 @@ stmt(struct comp *cm, struct function *fn)
envdown(cm, &e);
if (!match(cm, NULL, ';')) { /* init */
if (isdecltok(cm)) {
- localdecl(cm, fn);
+ localdecl(cm, fn, 1);
} else {
ex = commaexpr(cm);
EMITS expreffects(fn, &ex);
@@ -1866,7 +1866,7 @@ stmt(struct comp *cm, struct function *fn)
}
static void
-localdecl(struct comp *cm, struct function *fn)
+localdecl(struct comp *cm, struct function *fn, bool forini)
{
struct expr ini;
const bool doemit = fn->curblk;
@@ -1879,6 +1879,8 @@ localdecl(struct comp *cm, struct function *fn)
switch (decl.scls) {
case SCSTATIC:
+ if (forini)
+ error(&decl.span, "static declaration in 'for' loop initializer");
decl.id = ++staticid;
break;
case SCNONE:
@@ -1914,8 +1916,14 @@ localdecl(struct comp *cm, struct function *fn)
}
}
break;
- case SCTYPEDEF: break;
- case SCEXTERN: break;
+ case SCTYPEDEF:
+ if (forini)
+ error(&decl.span, "typedef in 'for' loop initializer");
+ break;
+ case SCEXTERN:
+ if (forini)
+ error(&decl.span, "extern declaration in 'for' loop initializer");
+ break;
default: assert(0);
}
Err:
@@ -1931,7 +1939,7 @@ block(struct comp *cm, struct function *fn)
while (!match(cm, &tk, '}')) {
if (isdecltok(cm))
- localdecl(cm, fn);
+ localdecl(cm, fn, 0);
else
stmt(cm, fn);
}