diff mbox

use function-like syntax for __range__

Message ID 20180501153817.41177-1-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Luc Van Oostenryck May 1, 2018, 3:38 p.m. UTC
One of sparse's extension to the C language is an operator
to check ranges. This operator takes 3 operands: the expression
to be checked and the bounds.

The syntax for this operator is such that the operands need to
be a 3-items comma separated expression. This is a bit weird
and doesn't play along very well with macros, for example.

Change the syntax to a 3-arguments function-like operator.

NB. Of course, this will break all existing uses of this
    extension not using parenthesis around the comma
    expression but there doesn't seems to be any.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                      |  5 ++++-
 validation/linear/range-op.c | 29 +++++++++++++++++++++++++++++
 validation/range-syntax.c    | 23 +++++++++++++++++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 validation/linear/range-op.c
 create mode 100644 validation/range-syntax.c
diff mbox

Patch

diff --git a/parse.c b/parse.c
index bb504d21a..4ecf97639 100644
--- a/parse.c
+++ b/parse.c
@@ -2367,11 +2367,14 @@  static struct token *parse_context_statement(struct token *token, struct stateme
 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
 {
 	stmt->type = STMT_RANGE;
-	token = assignment_expression(token->next, &stmt->range_expression);
+	token = token->next;
+	token = expect(token, '(', "after __range__ statement");
+	token = assignment_expression(token, &stmt->range_expression);
 	token = expect(token, ',', "after range expression");
 	token = assignment_expression(token, &stmt->range_low);
 	token = expect(token, ',', "after low range");
 	token = assignment_expression(token, &stmt->range_high);
+	token = expect(token, ')', "after range statement");
 	return expect(token, ';', "after range statement");
 }
 
diff --git a/validation/linear/range-op.c b/validation/linear/range-op.c
new file mode 100644
index 000000000..afc71a108
--- /dev/null
+++ b/validation/linear/range-op.c
@@ -0,0 +1,29 @@ 
+static void foo(int a)
+{
+	__range__(a, 0, 8);
+}
+
+static void bar(int a, int b, int c)
+{
+	__range__(a, b, c);
+}
+
+/*
+ * check-name: range-op
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-start
+foo:
+.L0:
+	<entry-point>
+	range-check %arg1 between $0..$8
+
+
+bar:
+.L2:
+	<entry-point>
+	range-check %arg1 between %arg2..%arg3
+
+
+ * check-output-end
+ */
diff --git a/validation/range-syntax.c b/validation/range-syntax.c
new file mode 100644
index 000000000..c43fff0e9
--- /dev/null
+++ b/validation/range-syntax.c
@@ -0,0 +1,23 @@ 
+
+static void ok(int a, int b, int c)
+{
+	__range__(a, 0, 8);
+	__range__(a, b, c);
+}
+
+static void ko(int a, int b, int c)
+{
+	__range__ a, 0, 8;
+	__range__ a, b, c;
+}
+
+/*
+ * check-name: range syntax
+ *
+ * check-error-start
+range-syntax.c:10:19: error: Expected ( after __range__ statement
+range-syntax.c:10:19: error: got a
+range-syntax.c:11:19: error: Expected ( after __range__ statement
+range-syntax.c:11:19: error: got a
+ * check-error-end
+ */