Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 0/6] fix parsing of C99's array declarators
@ 2020-07-23 23:46 Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 1/6] add testcase for comma in array declarator Luc Van Oostenryck
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

C99's array declarators were only partially parsed.
This series completes and simplify this parsing.

Luc Van Oostenryck (6):
  add testcase for comma in array declarator
  do not accept comma expressions in array declarator
  add testcases for C99 array declarators
  simplify & fix parsing of array declarators
  remove now unused match_idents()
  allow [*] in array declarators

 parse.c                                      | 53 ++++++--------------
 symbol.h                                     |  2 +-
 validation/abstract-array-declarator-quals.c | 21 ++++++++
 validation/abstract-array-declarator-star.c  |  8 +++
 validation/abstract-array-declarator.c       | 11 ++++
 5 files changed, 57 insertions(+), 38 deletions(-)
 create mode 100644 validation/abstract-array-declarator-quals.c
 create mode 100644 validation/abstract-array-declarator-star.c
 create mode 100644 validation/abstract-array-declarator.c

-- 
2.27.0


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

* [PATCH 1/6] add testcase for comma in array declarator
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 2/6] do not accept comma expressions " Luc Van Oostenryck
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Comma expressions are not allowed for the size in an array
declarator. Add a testcase for this.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 validation/abstract-array-declarator.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 validation/abstract-array-declarator.c

diff --git a/validation/abstract-array-declarator.c b/validation/abstract-array-declarator.c
new file mode 100644
index 000000000000..f230e5862ae9
--- /dev/null
+++ b/validation/abstract-array-declarator.c
@@ -0,0 +1,12 @@
+void f77(int a[1, 2]);
+void c99(int a[(1, 2)]);
+
+/*
+ * check-name: abstract-array-declarator
+ * check-known-to-fail
+ *
+ * check-error-start
+abstract-array-declarator.c:1:17: error: Expected ] in abstract_array_declarator
+abstract-array-declarator.c:1:17: error: got ,
+ * check-error-end
+ */
-- 
2.27.0


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

* [PATCH 2/6] do not accept comma expressions in array declarator
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 1/6] add testcase for comma in array declarator Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 3/6] add testcases for C99 array declarators Luc Van Oostenryck
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Comma expressions are not allowed for the size in an array
declarator.

So, change the parsing of these expressions to only accept
assignment-expressions.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                                | 2 +-
 validation/abstract-array-declarator.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/parse.c b/parse.c
index cc5dbd522b3e..182f4ad30b9c 100644
--- a/parse.c
+++ b/parse.c
@@ -1743,7 +1743,7 @@ static struct token *abstract_array_declarator(struct token *token, struct symbo
 
 	if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
 		token = abstract_array_static_declarator(token->next, &has_static);
-	token = parse_expression(token, &expr);
+	token = assignment_expression(token, &expr);
 	sym->array_size = expr;
 	return token;
 }
diff --git a/validation/abstract-array-declarator.c b/validation/abstract-array-declarator.c
index f230e5862ae9..ca182373b866 100644
--- a/validation/abstract-array-declarator.c
+++ b/validation/abstract-array-declarator.c
@@ -3,7 +3,6 @@ void c99(int a[(1, 2)]);
 
 /*
  * check-name: abstract-array-declarator
- * check-known-to-fail
  *
  * check-error-start
 abstract-array-declarator.c:1:17: error: Expected ] in abstract_array_declarator
-- 
2.27.0


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

* [PATCH 3/6] add testcases for C99 array declarators
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 1/6] add testcase for comma in array declarator Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 2/6] do not accept comma expressions " Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 4/6] simplify & fix parsing of " Luc Van Oostenryck
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

C99 introduced some funky new array declarators, those with
'restrict' or 'static' inside the brackets.

Add some testcases for them.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 validation/abstract-array-declarator-quals.c | 22 ++++++++++++++++++++
 validation/abstract-array-declarator-star.c  |  9 ++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 validation/abstract-array-declarator-quals.c
 create mode 100644 validation/abstract-array-declarator-star.c

diff --git a/validation/abstract-array-declarator-quals.c b/validation/abstract-array-declarator-quals.c
new file mode 100644
index 000000000000..85a35a2aca7c
--- /dev/null
+++ b/validation/abstract-array-declarator-quals.c
@@ -0,0 +1,22 @@
+#define N 2
+
+void ok1(int []);
+void ok2(int [N]);
+void ok3(int [const volatile restrict]);
+void ok4(int [const volatile restrict N]);
+void ok5(int [static N]);
+void ok6(int [static const volatile restrict N]);
+void ok7(int [const volatile restrict static N]);
+
+void ok1(int a[]);
+void ok2(int a[N]);
+void ok3(int a[const volatile restrict]);
+void ok4(int a[const volatile restrict N]);
+void ok5(int a[static N]);
+void ok6(int a[static const volatile restrict N]);
+void ok7(int a[const volatile restrict static N]);
+
+/*
+ * check-name: abstract-array-declarator-quals
+ * check-known-to-fail
+ */
diff --git a/validation/abstract-array-declarator-star.c b/validation/abstract-array-declarator-star.c
new file mode 100644
index 000000000000..fdbdff19840d
--- /dev/null
+++ b/validation/abstract-array-declarator-star.c
@@ -0,0 +1,9 @@
+void ok8(int [*]);
+
+void ok8(int a[*]);
+void ok9(int a[const volatile restrict *]);
+
+/*
+ * check-name: abstract-array-declarator-star
+ * check-known-to-fail
+ */
-- 
2.27.0


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

* [PATCH 4/6] simplify & fix parsing of array declarators
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
                   ` (2 preceding siblings ...)
  2020-07-23 23:46 ` [PATCH 3/6] add testcases for C99 array declarators Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 5/6] remove now unused match_idents() Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 6/6] allow [*] in array declarators Luc Van Oostenryck
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Any type qualifier is valid inside an abstract-array-declarator
but currently only 'restrict' is accepted. Also the parsing of
this is somehow more complex than needed and done by comparing
the identifiers instead of being driven by the keyword table.

So, simplify & fix the parsing of these declarators by:
1) using the keyword type KW_QUALIFIER to identify all type
   qualifier at once.
2) add a new keyword type just for 'static'
3) folding the helper abstract_array_static_declarator() into
   the main function: abstract_array_declarator().

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                                      | 28 +++++++-------------
 symbol.h                                     |  2 +-
 validation/abstract-array-declarator-quals.c |  1 -
 3 files changed, 11 insertions(+), 20 deletions(-)

diff --git a/parse.c b/parse.c
index 182f4ad30b9c..ec69e0c6e9ca 100644
--- a/parse.c
+++ b/parse.c
@@ -171,7 +171,7 @@ static struct symbol_op register_op = {
 };
 
 static struct symbol_op static_op = {
-	.type = KW_MODIFIER,
+	.type = KW_MODIFIER|KW_STATIC,
 	.declarator = static_specifier,
 };
 
@@ -1721,28 +1721,20 @@ static struct token *declaration_specifiers(struct token *token, struct decl_sta
 	return token;
 }
 
-static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
-{
-	while (token->ident == &static_ident) {
-		if (*has_static)
-			sparse_error(token->pos, "duplicate array static declarator");
-
-		*has_static = 1;
-		token = token->next;
-	}
-	return token;
-
-}
-
 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
 {
 	struct expression *expr = NULL;
 	int has_static = 0;
 
-	token = abstract_array_static_declarator(token, &has_static);
-
-	if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
-		token = abstract_array_static_declarator(token->next, &has_static);
+	while (token_type(token) == TOKEN_IDENT) {
+		struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF);
+		if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER)))
+			break;
+		if (has_static && (sym->op->type & KW_STATIC))
+			sparse_error(token->pos, "duplicate array static declarator");
+		has_static |= (sym->op->type & KW_STATIC);
+		token = token->next;
+	}
 	token = assignment_expression(token, &expr);
 	sym->array_size = expr;
 	return token;
diff --git a/symbol.h b/symbol.h
index c2b60ce91c27..147306481c20 100644
--- a/symbol.h
+++ b/symbol.h
@@ -81,7 +81,7 @@ enum keyword {
 	KW_BUILTIN	= 1 << 4,
 	KW_ASM		= 1 << 5,
 	KW_MODE		= 1 << 6,
-     // KW UNUSED	= 1 << 7,
+	KW_STATIC	= 1 << 7,
      // KW UNUSED	= 1 << 8,
 	KW_EXACT	= 1 << 9,
 };
diff --git a/validation/abstract-array-declarator-quals.c b/validation/abstract-array-declarator-quals.c
index 85a35a2aca7c..e69df902b895 100644
--- a/validation/abstract-array-declarator-quals.c
+++ b/validation/abstract-array-declarator-quals.c
@@ -18,5 +18,4 @@ void ok7(int a[const volatile restrict static N]);
 
 /*
  * check-name: abstract-array-declarator-quals
- * check-known-to-fail
  */
-- 
2.27.0


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

* [PATCH 5/6] remove now unused match_idents()
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
                   ` (3 preceding siblings ...)
  2020-07-23 23:46 ` [PATCH 4/6] simplify & fix parsing of " Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  2020-07-23 23:46 ` [PATCH 6/6] allow [*] in array declarators Luc Van Oostenryck
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

match_idents() is now unused and identifier matching should
preferably be done via the keyword table.

So, remove this function.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c | 18 ------------------
 1 file changed, 18 deletions(-)

diff --git a/parse.c b/parse.c
index ec69e0c6e9ca..c7ca3dce7f6b 100644
--- a/parse.c
+++ b/parse.c
@@ -678,24 +678,6 @@ static void fn_local_symbol(struct symbol *sym)
 		add_symbol(function_symbol_list, sym);
 }
 
-static int SENTINEL_ATTR match_idents(struct token *token, ...)
-{
-	va_list args;
-	struct ident * next;
-
-	if (token_type(token) != TOKEN_IDENT)
-		return 0;
-
-	va_start(args, token);
-	do {
-		next = va_arg(args, struct ident *);
-	} while (next && token->ident != next);
-	va_end(args);
-
-	return next && token->ident == next;
-}
-
-
 struct statement *alloc_statement(struct position pos, int type)
 {
 	struct statement *stmt = __alloc_statement(0);
-- 
2.27.0


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

* [PATCH 6/6] allow [*] in array declarators
  2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
                   ` (4 preceding siblings ...)
  2020-07-23 23:46 ` [PATCH 5/6] remove now unused match_idents() Luc Van Oostenryck
@ 2020-07-23 23:46 ` Luc Van Oostenryck
  5 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2020-07-23 23:46 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

Since C99, a '*' is allowed in an abstract array declarator to
specify that the array is a VLA with a yet-to-be-determined size.

So, accept this construction (but still ignore it for now).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                                     | 7 ++++++-
 validation/abstract-array-declarator-star.c | 1 -
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/parse.c b/parse.c
index c7ca3dce7f6b..6db70f1a95e9 100644
--- a/parse.c
+++ b/parse.c
@@ -1717,7 +1717,12 @@ static struct token *abstract_array_declarator(struct token *token, struct symbo
 		has_static |= (sym->op->type & KW_STATIC);
 		token = token->next;
 	}
-	token = assignment_expression(token, &expr);
+	if (match_op(token, '*') && match_op(token->next, ']')) {
+		// FIXME: '[*]' is treated like '[]'
+		token = token->next;
+	} else {
+		token = assignment_expression(token, &expr);
+	}
 	sym->array_size = expr;
 	return token;
 }
diff --git a/validation/abstract-array-declarator-star.c b/validation/abstract-array-declarator-star.c
index fdbdff19840d..fc42da3ae6c9 100644
--- a/validation/abstract-array-declarator-star.c
+++ b/validation/abstract-array-declarator-star.c
@@ -5,5 +5,4 @@ void ok9(int a[const volatile restrict *]);
 
 /*
  * check-name: abstract-array-declarator-star
- * check-known-to-fail
  */
-- 
2.27.0


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

end of thread, back to index

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's array declarators Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 1/6] add testcase for comma in array declarator Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 2/6] do not accept comma expressions " Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 3/6] add testcases for C99 array declarators Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 4/6] simplify & fix parsing of " Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 5/6] remove now unused match_idents() Luc Van Oostenryck
2020-07-23 23:46 ` [PATCH 6/6] allow [*] in array declarators Luc Van Oostenryck

Linux-Sparse Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sparse/0 linux-sparse/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sparse linux-sparse/ https://lore.kernel.org/linux-sparse \
		linux-sparse@vger.kernel.org
	public-inbox-index linux-sparse

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sparse


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git