linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Christopher Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 4/5] add a method to external_declaration()
Date: Sat, 18 Feb 2017 21:30:47 +0100	[thread overview]
Message-ID: <20170218203048.22276-5-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170218203048.22276-1-luc.vanoostenryck@gmail.com>

After parsing and validation, the symbols in the declaration
are added to the list given in argument, *if* they are not extern
symbols. The symbols that are extern are them not added to the list.

This is what is needed for usual declarations but ignoring extern
symbols make it impossible to emit a diagnostic in less usual
situation.

This is motivated by the validation of variable declaration inside
a for-loop initializer, which valid in C99 but only for variable
with local storage.

The changes consist in moving the part 'add the symbol to the list if
the symbol is not extern' to a separate function which will be called
by default.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 lib.c   |  2 +-
 parse.c | 22 +++++++++++++++-------
 parse.h |  3 ++-
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/lib.c b/lib.c
index d47325243..9badc09b3 100644
--- a/lib.c
+++ b/lib.c
@@ -1080,7 +1080,7 @@ static struct symbol_list *sparse_tokenstream(struct token *token)
 
 	// Parse the resulting C code
 	while (!eof_token(token))
-		token = external_declaration(token, &translation_unit_used_list);
+		token = external_declaration(token, NULL, &translation_unit_used_list);
 	return translation_unit_used_list;
 }
 
diff --git a/parse.c b/parse.c
index a4a126720..866186fd2 100644
--- a/parse.c
+++ b/parse.c
@@ -2230,7 +2230,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);
+		token = external_declaration(token, NULL, &syms);
 	} else {
 		token = parse_expression(token, &e1);
 		token = expect(token, ';', "in 'for'");
@@ -2457,7 +2457,7 @@ static struct token * statement_list(struct token *token, struct statement_list
 				seen_statement = 0;
 			}
 			stmt = alloc_statement(token->pos, STMT_DECLARATION);
-			token = external_declaration(token, &stmt->declaration);
+			token = external_declaration(token, NULL, &stmt->declaration);
 		} else {
 			seen_statement = Wdeclarationafterstatement;
 			token = statement(token, &stmt);
@@ -2785,7 +2785,15 @@ static struct token *toplevel_asm_declaration(struct token *token, struct symbol
 	return token;
 }
 
-struct token *external_declaration(struct token *token, struct symbol_list **list)
+static void add_stmt_decl(struct symbol_list **list, struct symbol *decl)
+{
+	if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
+		add_symbol(list, decl);
+		fn_local_symbol(decl);
+	}
+}
+
+struct token *external_declaration(struct token *token, add_decl_t add_decl, struct symbol_list **list)
 {
 	struct ident *ident = NULL;
 	struct symbol *decl;
@@ -2864,6 +2872,9 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 		sparse_error(token->pos, "void declaration");
 	}
 
+	if (!add_decl)
+		add_decl = add_stmt_decl;
+
 	for (;;) {
 		if (!is_typedef && match_op(token, '=')) {
 			if (decl->ctype.modifiers & MOD_EXTERN) {
@@ -2873,10 +2884,7 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 			token = initializer(&decl->initializer, token->next);
 		}
 		if (!is_typedef) {
-			if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
-				add_symbol(list, decl);
-				fn_local_symbol(decl);
-			}
+			add_decl(list, decl);
 		}
 		check_declaration(decl);
 		if (decl->same_symbol) {
diff --git a/parse.h b/parse.h
index a2b3e3889..d3199727e 100644
--- a/parse.h
+++ b/parse.h
@@ -129,7 +129,8 @@ extern int show_statement(struct statement *);
 extern void show_statement_list(struct statement_list *, const char *);
 extern int show_expression(struct expression *);
 
-extern struct token *external_declaration(struct token *token, struct symbol_list **list);
+typedef void (*add_decl_t)(struct symbol_list **list, struct symbol *decl);
+extern struct token *external_declaration(struct token *token, add_decl_t add_decl, struct symbol_list **list);
 
 extern struct symbol *ctype_integer(int size, int want_unsigned);
 
-- 
2.11.0


  parent reply	other threads:[~2017-02-18 20:30 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-18 20:30 [PATCH 0/5] more validation of C99 for-loop initializers Luc Van Oostenryck
2017-02-18 20:30 ` [PATCH 1/5] replace test for c99 " Luc Van Oostenryck
2017-02-18 22:37   ` Ramsay Jones
2017-02-19  1:10     ` Luc Van Oostenryck
2017-02-19 20:58       ` Ramsay Jones
2017-02-20  7:20         ` [PATCH v2 0/5] more validation of C99 " Luc Van Oostenryck
2017-02-20  7:20           ` [PATCH v2 1/5] replace test for c99 " Luc Van Oostenryck
2017-02-20 14:05             ` Ramsay Jones
2017-02-20  7:20           ` [PATCH v2 2/5] add test case for scope of C99 for-loop declarations Luc Van Oostenryck
2017-02-20  7:20           ` [PATCH v2 3/5] add test cases for storage of c99 " Luc Van Oostenryck
2017-02-20  7:20           ` [PATCH v2 4/5] add a method to external_declaration() Luc Van Oostenryck
2017-02-20  7:20           ` [PATCH v2 5/5] check the storage of C99 for-loop initializers Luc Van Oostenryck
2017-02-18 20:30 ` [PATCH 2/5] add test case for scope of C99 for-loop declarations Luc Van Oostenryck
2017-02-18 20:30 ` [PATCH 3/5] add test cases for storage of c99 " Luc Van Oostenryck
2017-02-18 20:30 ` Luc Van Oostenryck [this message]
2017-02-27 15:37   ` [PATCH 4/5] add a method to external_declaration() Christopher Li
2017-02-27 21:34     ` Luc Van Oostenryck
2017-02-28  9:46     ` Luc Van Oostenryck
2017-02-28 10:03       ` [PATCH v3 0/7] more validation of C99 for-loop initializers Luc Van Oostenryck
2017-02-28 10:03         ` [PATCH v3 1/7] replace test for c99 " Luc Van Oostenryck
2017-02-28 10:03         ` [PATCH v3 2/7] add test case for scope of C99 for-loop declarations Luc Van Oostenryck
2017-02-28 10:03         ` [PATCH v3 3/7] add test cases for storage of c99 " Luc Van Oostenryck
2017-02-28 10:04         ` [PATCH v3 4/7] add a method to external_declaration() Luc Van Oostenryck
2017-03-05 14:04           ` Christopher Li
2017-03-05 15:12             ` Luc Van Oostenryck
2017-03-06  1:13               ` Christopher Li
2017-03-05 19:21             ` [PATCH v4 0/6] more validation of C99 for-loop initializers Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 1/6] replace test for c99 " Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 2/6] add test case for scope of C99 for-loop declarations Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 3/6] add test cases for storage of c99 " Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 4/6] add an optional validation method to external_declaration() Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 5/6] check the storage of C99 for-loop initializers Luc Van Oostenryck
2017-03-05 19:21               ` [PATCH v4 6/6] move 'extern with initializer' validation after the validate method Luc Van Oostenryck
2017-03-06  0:59               ` [PATCH v4 0/6] more validation of C99 for-loop initializers Christopher Li
2017-03-06  1:08                 ` Luc Van Oostenryck
2017-02-28 10:04         ` [PATCH v3 5/7] check the storage " Luc Van Oostenryck
2017-03-05 14:26           ` Christopher Li
2017-03-05 15:24             ` Luc Van Oostenryck
2017-02-28 10:04         ` [PATCH v3 6/7] make process_decl() aware of the presence of an initializer Luc Van Oostenryck
2017-03-05 14:49           ` Christopher Li
2017-03-05 15:29             ` Luc Van Oostenryck
2017-02-28 10:04         ` [PATCH v3 7/7] move check extern with initializer to default_process_decl() Luc Van Oostenryck
2017-02-28 16:34         ` [PATCH v3 0/7] more validation of C99 for-loop initializers Christopher Li
2017-02-28 16:40           ` Luc Van Oostenryck
2017-02-18 20:30 ` [PATCH 5/5] check the storage " Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170218203048.22276-5-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).