[PATCH 5/6] context: stricter syntax for __context__ statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The expected syntax for the __context__ statement is:
	__context__(<expression>);
or
	__context__(<context>, <expression>);
but originally it was just:
	__context__ <expression>
In other words the parenthesis were not needed and are
still not needed when no context is given.

One problem with the current way to parse these statements is
that very few validation is done. For example, code like:
	__context__;
is silently accepted, as is:
	__context__ a, b;
which is of course not the same as:
	__context__(a,b);
And code like:
	__context__(,1);
results in a confusing error message:
	error: an expression is expected before ')'
	error: Expected ) in expression
	error: got ,

So, given that:
* the kernel has always used the syntax with parenthesis,
* the two arguments form requires the parenthesis and thus
  a function-like syntax
use a more direct, robust and simpl parsing which enforce
the function-like syntax for both forms.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 parse.c                   | 18 ++++++++----------
 validation/context-stmt.c | 25 ++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/parse.c b/parse.c
index 42b3fd20a..cdf034dea 100644
--- a/parse.c
+++ b/parse.c
@@ -2338,17 +2338,15 @@ static struct token *parse_goto_statement(struct token *token, struct statement
 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
 {
 	stmt->type = STMT_CONTEXT;
-	token = parse_expression(token->next, &stmt->expression);
-	if (stmt->expression
-	    && stmt->expression->type == EXPR_PREOP
-	    && stmt->expression->op == '('
-	    && stmt->expression->unop
-	    && stmt->expression->unop->type == EXPR_COMMA) {
-		struct expression *expr;
-		expr = stmt->expression->unop;
-		stmt->context = expr->left;
-		stmt->expression = expr->right;
+	token = token->next;
+	token = expect(token, '(', "after __context__ statement");
+	token = assignment_expression(token, &stmt->expression);
+	if (match_op(token, ',')) {
+		token = token->next;
+		stmt->context = stmt->expression;
+		token = assignment_expression(token, &stmt->expression);
 	}
+	token = expect(token, ')', "at end of __context__ statement");
 	return expect(token, ';', "at end of statement");
 }
 
diff --git a/validation/context-stmt.c b/validation/context-stmt.c
index 1f02c3a67..8cea6b5f2 100644
--- a/validation/context-stmt.c
+++ b/validation/context-stmt.c
@@ -9,6 +9,13 @@ static void foo(int x)
 
 	__context__;		// KO: no expression at all
 	__context__(;		// KO: no expression at all
+
+	__context__ 0;		// KO: need parens
+	__context__ x, 0;	// KO: need parens
+	__context__(x, 0;	// KO: unmatched parens
+	__context__ x, 0);	// KO: unmatched parens
+	__context__(0;		// KO: unmatched parens
+	__context__ 0);		// KO: unmatched parens
 }
 
 /*
@@ -16,11 +23,23 @@ static void foo(int x)
  * check-command: sparse -Wno-context $file
  *
  * check-error-start
-context-stmt.c:11:21: error: an expression is expected before ')'
-context-stmt.c:11:21: error: Expected ) in expression
+context-stmt.c:10:20: error: Expected ( after __context__ statement
+context-stmt.c:10:20: error: got ;
+context-stmt.c:11:21: error: Expected ) at end of __context__ statement
 context-stmt.c:11:21: error: got ;
+context-stmt.c:13:21: error: Expected ( after __context__ statement
+context-stmt.c:13:21: error: got 0
+context-stmt.c:14:21: error: Expected ( after __context__ statement
+context-stmt.c:14:21: error: got x
+context-stmt.c:15:25: error: Expected ) at end of __context__ statement
+context-stmt.c:15:25: error: got ;
+context-stmt.c:16:21: error: Expected ( after __context__ statement
+context-stmt.c:16:21: error: got x
+context-stmt.c:17:22: error: Expected ) at end of __context__ statement
+context-stmt.c:17:22: error: got ;
+context-stmt.c:18:21: error: Expected ( after __context__ statement
+context-stmt.c:18:21: error: got 0
 context-stmt.c:7:21: error: bad constant expression
 context-stmt.c:8:23: error: bad constant expression
-context-stmt.c:11:20: error: bad constant expression type
  * check-error-end
  */
-- 
2.17.0

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



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux