Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 4/6] simplify & fix parsing of array declarators
Date: Fri, 24 Jul 2020 01:46:39 +0200
Message-ID: <20200723234641.78462-5-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20200723234641.78462-1-luc.vanoostenryck@gmail.com>

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


  parent reply index

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-23 23:46 [PATCH 0/6] fix parsing of C99's " 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 ` Luc Van Oostenryck [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200723234641.78462-5-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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