Skip to contentSkip to conversation

fix parsing of C99's array declarators

PATCH New Luc Van Oostenryck 1 participant 7 messages Jul 23, 2020 Sparse checker

lore
Share & tools
lore
#0 lore raw

cover fix parsing of C99's array declarators

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
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/6 add testcase for comma in array declarator

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

---
 validation/abstract-array-declarator.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 validation/abstract-array-declarator.c
validation/abstract-array-declarator.c new file +12 -0
new file mode 100644
index 000000000000..f230e5862ae9
@@ -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
+ */
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/6 do not accept comma expressions in array declarator

In reply to 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.

---
 parse.c                                | 2 +-
 validation/abstract-array-declarator.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
parse.c +1 -1
index cc5dbd522b3e..182f4ad30b9c 100644
@@ -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;
 }
validation/abstract-array-declarator.c +0 -1
index f230e5862ae9..ca182373b866 100644
@@ -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
Signature · 1 line
2.27.0
#3 lore raw

PATCH 3/6 add testcases for C99 array declarators

In reply to Luc Van Oostenryck

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

Add some testcases for them.

---
 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
validation/abstract-array-declarator-quals.c new file +22 -0
new file mode 100644
index 000000000000..85a35a2aca7c
@@ -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
+ */
validation/abstract-array-declarator-star.c new file +9 -0
new file mode 100644
index 000000000000..fdbdff19840d
@@ -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
+ */
Signature · 1 line
2.27.0
#4 lore raw

PATCH 4/6 simplify & fix parsing of array declarators

In reply to 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().

---
 parse.c                                      | 28 +++++++-------------
 symbol.h                                     |  2 +-
 validation/abstract-array-declarator-quals.c |  1 -
 3 files changed, 11 insertions(+), 20 deletions(-)
parse.c +10 -18
index 182f4ad30b9c..ec69e0c6e9ca 100644
@@ -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;
symbol.h +1 -1
index c2b60ce91c27..147306481c20 100644
@@ -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,
 };
validation/abstract-array-declarator-quals.c +0 -1
index 85a35a2aca7c..e69df902b895 100644
@@ -18,5 +18,4 @@ void ok7(int a[const volatile restrict static N]);
 
 /*
  * check-name: abstract-array-declarator-quals
- * check-known-to-fail
  */
Signature · 1 line
2.27.0
#5 lore raw

PATCH 5/6 remove now unused match_idents()

In reply to Luc Van Oostenryck

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

So, remove this function.

---
 parse.c | 18 ------------------
 1 file changed, 18 deletions(-)
parse.c +0 -18
index ec69e0c6e9ca..c7ca3dce7f6b 100644
@@ -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);
Signature · 1 line
2.27.0
#6 lore raw

PATCH 6/6 allow [*] in array declarators

In reply to 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).

---
 parse.c                                     | 7 ++++++-
 validation/abstract-array-declarator-star.c | 1 -
 2 files changed, 6 insertions(+), 2 deletions(-)
parse.c +6 -1
index c7ca3dce7f6b..6db70f1a95e9 100644
@@ -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;
 }
validation/abstract-array-declarator-star.c +0 -1
index fdbdff19840d..fc42da3ae6c9 100644
@@ -5,5 +5,4 @@ void ok9(int a[const volatile restrict *]);
 
 /*
  * check-name: abstract-array-declarator-star
- * check-known-to-fail
  */
Signature · 1 line
2.27.0