linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] check missing or duplicate goto labels
@ 2012-06-03 20:01 Xi Wang
  2012-06-04 23:10 ` Christopher Li
  0 siblings, 1 reply; 3+ messages in thread
From: Xi Wang @ 2012-06-03 20:01 UTC (permalink / raw)
  To: linux-sparse; +Cc: Xi Wang

This patch sets ->stmt of a SYM_LABEL to the corresponding label
statement.  If ->stmt was already set, it is a duplicate label.

On the other hand, if ->stmt of a goto label is not set during
evaluation, the label was never declared.
---
See a new test validation/goto-label.c for examples.
---
 evaluate.c              |   12 +++++++++++-
 parse.c                 |    6 +++++-
 validation/goto-label.c |   29 +++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 validation/goto-label.c

diff --git a/evaluate.c b/evaluate.c
index bebe968..0987a5e 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3312,6 +3312,16 @@ static void evaluate_switch_statement(struct statement *stmt)
 	} END_FOR_EACH_PTR(sym);
 }
 
+static void evaluate_goto_statement(struct statement *stmt)
+{
+	struct symbol *label = stmt->goto_label;
+
+	if (label && !label->stmt && !lookup_keyword(label->ident, NS_KEYWORD))
+		sparse_error(stmt->pos, "label '%s' was not declared", show_ident(label->ident));
+
+	evaluate_expression(stmt->goto_expression);
+}
+
 struct symbol *evaluate_statement(struct statement *stmt)
 {
 	if (!stmt)
@@ -3370,7 +3380,7 @@ struct symbol *evaluate_statement(struct statement *stmt)
 	case STMT_LABEL:
 		return evaluate_statement(stmt->label_statement);
 	case STMT_GOTO:
-		evaluate_expression(stmt->goto_expression);
+		evaluate_goto_statement(stmt);
 		return NULL;
 	case STMT_NONE:
 		break;
diff --git a/parse.c b/parse.c
index f8ade3e..8fbba6b 100644
--- a/parse.c
+++ b/parse.c
@@ -2276,8 +2276,12 @@ static struct token *statement(struct token *token, struct statement **tree)
 			return s->op->statement(token, stmt);
 
 		if (match_op(token->next, ':')) {
+			struct symbol *s = label_symbol(token);
 			stmt->type = STMT_LABEL;
-			stmt->label_identifier = label_symbol(token);
+			stmt->label_identifier = s;
+			if (s->stmt)
+				sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
+			s->stmt = stmt;
 			token = skip_attributes(token->next->next);
 			return statement(token, &stmt->label_statement);
 		}
diff --git a/validation/goto-label.c b/validation/goto-label.c
new file mode 100644
index 0000000..1196fde
--- /dev/null
+++ b/validation/goto-label.c
@@ -0,0 +1,29 @@
+void foo(void)
+{
+	goto a;
+a:
+a:
+	return;
+}
+
+void g(void)
+{
+	goto a;
+a:
+	return;
+}
+
+void bar(void)
+{
+	goto neverland;
+}
+
+/*
+ * check-name: goto labels
+ *
+ * check-error-start
+goto-label.c:5:1: error: label 'a' redefined
+goto-label.c:18:9: error: label 'neverland' was not declared
+ * check-error-end
+ */
+
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] check missing or duplicate goto labels
  2012-06-03 20:01 [PATCH] check missing or duplicate goto labels Xi Wang
@ 2012-06-04 23:10 ` Christopher Li
  2012-06-04 23:22   ` Xi Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Christopher Li @ 2012-06-04 23:10 UTC (permalink / raw)
  To: Xi Wang; +Cc: linux-sparse

On Sun, Jun 3, 2012 at 1:01 PM, Xi Wang <xi.wang@gmail.com> wrote:
> This patch sets ->stmt of a SYM_LABEL to the corresponding label
> statement.  If ->stmt was already set, it is a duplicate label.
>
> On the other hand, if ->stmt of a goto label is not set during
> evaluation, the label was never declared.

Looks great! Can you please send me a signed off line?

Thanks

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] check missing or duplicate goto labels
  2012-06-04 23:10 ` Christopher Li
@ 2012-06-04 23:22   ` Xi Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Xi Wang @ 2012-06-04 23:22 UTC (permalink / raw)
  To: Christopher Li; +Cc: linux-sparse

On Jun 4, 2012, at 7:10 PM, Christopher Li wrote:

> On Sun, Jun 3, 2012 at 1:01 PM, Xi Wang <xi.wang@gmail.com> wrote:
>> This patch sets ->stmt of a SYM_LABEL to the corresponding label
>> statement.  If ->stmt was already set, it is a duplicate label.
>> 
>> On the other hand, if ->stmt of a goto label is not set during
>> evaluation, the label was never declared.
> 
> Looks great! Can you please send me a signed off line?

Oh sorry how did I miss that..

Signed-off-by: Xi Wang <xi.wang@gmail.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-06-04 23:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-03 20:01 [PATCH] check missing or duplicate goto labels Xi Wang
2012-06-04 23:10 ` Christopher Li
2012-06-04 23:22   ` Xi Wang

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).