All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] accept an empty attribute argument list
@ 2026-06-05 11:30 Dmitry Ilvokhin
  2026-06-05 15:26 ` Dan Carpenter
  2026-10-19 12:16 ` Dmitry Ilvokhin
  0 siblings, 2 replies; 4+ messages in thread
From: Dmitry Ilvokhin @ 2026-06-05 11:30 UTC (permalink / raw)
  To: Chris Li; +Cc: linux-sparse, Dmitry Ilvokhin, Dan Carpenter

GCC and clang accept an empty argument list for attributes, e.g.
__attribute__((nonnull())), and treat it the same as the bare
__attribute__((nonnull)). sparse instead rejects it with
"an expression is expected before ')'".

ignore_attribute() and recover_unknown_attribute() consume the optional
argument list with parens_expression(), which requires a non-empty
expression, so an empty () is rejected. The bare form is fine because it
takes no parentheses and skips that path.

This turned up in the Linux kernel: the cleanup.h guard macros expand
__nonnull_args() to __attribute__((__nonnull__())).

  https://lore.kernel.org/all/aiJi0WcYE8FZt-jO@stanley.mountain/

Reported-by: Dan Carpenter <error27@gmail.com>
Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
---
 parse.c                              | 17 +++++++++++++----
 validation/attribute-nonnull-empty.c | 12 ++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 validation/attribute-nonnull-empty.c

diff --git a/parse.c b/parse.c
index 9389079e..3b380156 100644
--- a/parse.c
+++ b/parse.c
@@ -1106,8 +1106,13 @@ static struct token *autotype_specifier(struct token *token, struct symbol *sym,
 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
 {
 	struct expression *expr = NULL;
-	if (match_op(token, '('))
-		token = parens_expression(token, &expr, "in attribute");
+	if (match_op(token, '(')) {
+		/* Accept an empty argument list, e.g. __attribute__((nonnull())) */
+		if (match_op(token->next, ')'))
+			token = token->next->next;
+		else
+			token = parens_expression(token, &expr, "in attribute");
+	}
 	return token;
 }
 
@@ -1362,8 +1367,12 @@ static struct token *recover_unknown_attribute(struct token *token)
 	if (Wunknown_attribute)
 		warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
 	token = token->next;
-	if (match_op(token, '('))
-		token = parens_expression(token, &expr, "in attribute");
+	if (match_op(token, '(')) {
+		if (match_op(token->next, ')'))
+			token = token->next->next;
+		else
+			token = parens_expression(token, &expr, "in attribute");
+	}
 	return token;
 }
 
diff --git a/validation/attribute-nonnull-empty.c b/validation/attribute-nonnull-empty.c
new file mode 100644
index 00000000..c4021f61
--- /dev/null
+++ b/validation/attribute-nonnull-empty.c
@@ -0,0 +1,12 @@
+/*
+ * An empty attribute argument list, like __attribute__((nonnull())), is
+ * accepted by GCC and Clang (equivalent to the bare __attribute__((nonnull)))
+ * and must be parsed by sparse too.
+ */
+extern void *bare (void *d, const void *s) __attribute__((nonnull));
+extern void *empty(void *d, const void *s) __attribute__((nonnull()));
+extern void *args (void *d, const void *s) __attribute__((nonnull(1, 2)));
+
+/*
+ * check-name: attribute with empty argument list
+ */
-- 
2.53.0-Meta


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

* Re: [PATCH] accept an empty attribute argument list
  2026-06-05 11:30 [PATCH] accept an empty attribute argument list Dmitry Ilvokhin
@ 2026-06-05 15:26 ` Dan Carpenter
  2026-10-19 12:16 ` Dmitry Ilvokhin
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-06-05 15:26 UTC (permalink / raw)
  To: Dmitry Ilvokhin; +Cc: Chris Li, linux-sparse

On Fri, Jun 05, 2026 at 11:30:45AM +0000, Dmitry Ilvokhin wrote:
> GCC and clang accept an empty argument list for attributes, e.g.
> __attribute__((nonnull())), and treat it the same as the bare
> __attribute__((nonnull)). sparse instead rejects it with
> "an expression is expected before ')'".
> 
> ignore_attribute() and recover_unknown_attribute() consume the optional
> argument list with parens_expression(), which requires a non-empty
> expression, so an empty () is rejected. The bare form is fine because it
> takes no parentheses and skips that path.
> 
> This turned up in the Linux kernel: the cleanup.h guard macros expand
> __nonnull_args() to __attribute__((__nonnull__())).
> 
>   https://lore.kernel.org/all/aiJi0WcYE8FZt-jO@stanley.mountain/
> 
> Reported-by: Dan Carpenter <error27@gmail.com>
> Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
> ---
>  parse.c                              | 17 +++++++++++++----
>  validation/attribute-nonnull-empty.c | 12 ++++++++++++
>  2 files changed, 25 insertions(+), 4 deletions(-)
>  create mode 100644 validation/attribute-nonnull-empty.c

Thanks so much!

Tested-by: Dan Carpenter <error27@gmail.com>

regards,
dan carpenter


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

* Re: [PATCH] accept an empty attribute argument list
  2026-06-05 11:30 [PATCH] accept an empty attribute argument list Dmitry Ilvokhin
  2026-06-05 15:26 ` Dan Carpenter
@ 2026-10-19 12:16 ` Dmitry Ilvokhin
  2026-10-19 17:49   ` Bart Van Assche
  1 sibling, 1 reply; 4+ messages in thread
From: Dmitry Ilvokhin @ 2026-10-19 12:16 UTC (permalink / raw)
  To: Chris Li; +Cc: linux-sparse, Dan Carpenter

On Fri, Jun 05, 2026 at 11:30:45AM +0000, Dmitry Ilvokhin wrote:
> GCC and clang accept an empty argument list for attributes, e.g.
> __attribute__((nonnull())), and treat it the same as the bare
> __attribute__((nonnull)). sparse instead rejects it with
> "an expression is expected before ')'".
> 
> ignore_attribute() and recover_unknown_attribute() consume the optional
> argument list with parens_expression(), which requires a non-empty
> expression, so an empty () is rejected. The bare form is fine because it
> takes no parentheses and skips that path.
> 
> This turned up in the Linux kernel: the cleanup.h guard macros expand
> __nonnull_args() to __attribute__((__nonnull__())).
> 
>   https://lore.kernel.org/all/aiJi0WcYE8FZt-jO@stanley.mountain/
> 
> Reported-by: Dan Carpenter <error27@gmail.com>
> Signed-off-by: Dmitry Ilvokhin <d@ilvokhin.com>
> ---
>  parse.c                              | 17 +++++++++++++----
>  validation/attribute-nonnull-empty.c | 12 ++++++++++++
>  2 files changed, 25 insertions(+), 4 deletions(-)
>  create mode 100644 validation/attribute-nonnull-empty.c
> 
> diff --git a/parse.c b/parse.c
> index 9389079e..3b380156 100644
> --- a/parse.c
> +++ b/parse.c
> @@ -1106,8 +1106,13 @@ static struct token *autotype_specifier(struct token *token, struct symbol *sym,
>  static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
>  {
>  	struct expression *expr = NULL;
> -	if (match_op(token, '('))
> -		token = parens_expression(token, &expr, "in attribute");
> +	if (match_op(token, '(')) {
> +		/* Accept an empty argument list, e.g. __attribute__((nonnull())) */
> +		if (match_op(token->next, ')'))
> +			token = token->next->next;
> +		else
> +			token = parens_expression(token, &expr, "in attribute");
> +	}
>  	return token;
>  }
>  
> @@ -1362,8 +1367,12 @@ static struct token *recover_unknown_attribute(struct token *token)
>  	if (Wunknown_attribute)
>  		warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
>  	token = token->next;
> -	if (match_op(token, '('))
> -		token = parens_expression(token, &expr, "in attribute");
> +	if (match_op(token, '(')) {
> +		if (match_op(token->next, ')'))
> +			token = token->next->next;
> +		else
> +			token = parens_expression(token, &expr, "in attribute");
> +	}
>  	return token;
>  }
>  
> diff --git a/validation/attribute-nonnull-empty.c b/validation/attribute-nonnull-empty.c
> new file mode 100644
> index 00000000..c4021f61
> --- /dev/null
> +++ b/validation/attribute-nonnull-empty.c
> @@ -0,0 +1,12 @@
> +/*
> + * An empty attribute argument list, like __attribute__((nonnull())), is
> + * accepted by GCC and Clang (equivalent to the bare __attribute__((nonnull)))
> + * and must be parsed by sparse too.
> + */
> +extern void *bare (void *d, const void *s) __attribute__((nonnull));
> +extern void *empty(void *d, const void *s) __attribute__((nonnull()));
> +extern void *args (void *d, const void *s) __attribute__((nonnull(1, 2)));
> +
> +/*
> + * check-name: attribute with empty argument list
> + */
> -- 
> 2.53.0-Meta
>

Friendly ping on the patch.

Recently, I had some time to migrate raw __attribute__((nonnull)) usages
to __nonnull_args() in Linux kernel, but then remembered Sparse still
can't parse it properly.

I can re-spin the patch if it is a preferred.

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

* Re: [PATCH] accept an empty attribute argument list
  2026-10-19 12:16 ` Dmitry Ilvokhin
@ 2026-10-19 17:49   ` Bart Van Assche
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-10-19 17:49 UTC (permalink / raw)
  To: Dmitry Ilvokhin, Chris Li; +Cc: linux-sparse, Dan Carpenter

On 10/19/26 5:16 AM, Dmitry Ilvokhin wrote:
> Friendly ping on the patch.
> 
> Recently, I had some time to migrate raw __attribute__((nonnull)) usages
> to __nonnull_args() in Linux kernel, but then remembered Sparse still
> can't parse it properly.

Hi,

I'm the new sparse maintainer and plan to apply your patch soon. Please 
help with reviewing my pending sparse patches. I will publish these on
the linux-sparse mailing list later today.

Thanks,

Bart.

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

end of thread, other threads:[~2026-10-19 17:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-05 11:30 [PATCH] accept an empty attribute argument list Dmitry Ilvokhin
2026-06-05 15:26 ` Dan Carpenter
2026-10-19 12:16 ` Dmitry Ilvokhin
2026-10-19 17:49   ` Bart Van Assche

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.