diff options
| author | 2023-06-19 12:40:22 +0200 | |
|---|---|---|
| committer | 2023-06-19 12:40:22 +0200 | |
| commit | 3bda62cd2460bb5a8d6cdff0dce9ad2fca26ee4f (patch) | |
| tree | 475aa4ee12d41b3a3b53f183c6caa36318ed6f2a /c.c | |
| parent | 013e4d624873cd47cc5ef2b801e13e9b669c7ae1 (diff) | |
frontend: disallow non-local decls in for initializer
Diffstat (limited to 'c.c')
| -rw-r--r-- | c.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -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); } |