aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/parse.c
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-03-05 20:21:51 +0100
committerChristopher Li <sparse@chrisli.org>2017-03-06 08:51:22 +0800
commit44632081a3b54299e8899954efcd7133ef093259 (patch)
tree839621fa83d0e8cd5bbe3eff06182133c53648c9 /parse.c
parentd4b88ffd878ca151cd03969d02522c456a264a7a (diff)
downloadsparse-dev-44632081a3b54299e8899954efcd7133ef093259.tar.gz
check the storage of C99 for-loop initializers
In C99, it is valid to declare a variable inside a for-loop initializer but only when the storage is local (automatic or register). Until now this was not enforced. Fix this, when parsing declarations in a for-loop context, by calling external_decl() with a validate method doing the appropriate check of the storage. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Signed-off-by: Christopher Li <sparse@chrisli.org>
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/parse.c b/parse.c
index d91a4bce..6ec1bab9 100644
--- a/parse.c
+++ b/parse.c
@@ -2229,6 +2229,17 @@ static struct token *parse_return_statement(struct token *token, struct statemen
return expression_statement(token->next, &stmt->ret_value);
}
+static void validate_for_loop_decl(struct symbol *sym)
+{
+ unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
+
+ if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
+ const char *name = show_ident(sym->ident);
+ sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
+ sym->ctype.modifiers &= ~MOD_STORAGE;
+ }
+}
+
static struct token *parse_for_statement(struct token *token, struct statement *stmt)
{
struct symbol_list *syms;
@@ -2242,7 +2253,7 @@ static struct token *parse_for_statement(struct token *token, struct statement *
e1 = NULL;
/* C99 variable declaration? */
if (lookup_type(token)) {
- token = external_declaration(token, &syms, NULL);
+ token = external_declaration(token, &syms, validate_for_loop_decl);
} else {
token = parse_expression(token, &e1);
token = expect(token, ';', "in 'for'");