attribs: Fix wrong error with -Wno-attribute=A::b [PR103649]

Message ID 20211216223555.820800-1-polacek@redhat.com
State New
Headers
Series attribs: Fix wrong error with -Wno-attribute=A::b [PR103649] |

Commit Message

Marek Polacek Dec. 16, 2021, 10:35 p.m. UTC
  My patch to implement -Wno-attribute=A::b caused a bogus error when
parsing

  [[foo::bar(1, 2)]];

when -Wno-attributes=foo::bar was specified on the command line, because
when we create a fake foo::bar attribute and insert it into our attribute
table, it is created with max_length == 0 which doesn't allow any args.
That is wrong -- we know nothing about the attribute, so we shouldn't
require any specific number of arguments.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c/103649

gcc/ChangeLog:

	* attribs.c (handle_ignored_attributes_option): Create the fake
	attribute with max_length == -1.

gcc/testsuite/ChangeLog:

	* c-c++-common/Wno-attributes-6.c: New test.
---
 gcc/attribs.c                                 | 2 +-
 gcc/testsuite/c-c++-common/Wno-attributes-6.c | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/c-c++-common/Wno-attributes-6.c


base-commit: f91814c22595e1db642140efe030caf2c092ab6f
  

Comments

Jakub Jelinek Dec. 16, 2021, 10:53 p.m. UTC | #1
On Thu, Dec 16, 2021 at 05:35:55PM -0500, Marek Polacek wrote:
> My patch to implement -Wno-attribute=A::b caused a bogus error when
> parsing
> 
>   [[foo::bar(1, 2)]];
> 
> when -Wno-attributes=foo::bar was specified on the command line, because
> when we create a fake foo::bar attribute and insert it into our attribute
> table, it is created with max_length == 0 which doesn't allow any args.
> That is wrong -- we know nothing about the attribute, so we shouldn't
> require any specific number of arguments.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> 	PR c/103649
> 
> gcc/ChangeLog:
> 
> 	* attribs.c (handle_ignored_attributes_option): Create the fake
> 	attribute with max_length == -1.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* c-c++-common/Wno-attributes-6.c: New test.

I'm afraid this still changes behavior.  0, -1 range attribute arguments
are parsed normally, while unknown attributes have the balanced token
sequence skipped.
E.g. the omp::{directive,sequence} attribute arguments are much more complex
than what the normal parsing can handle.
Can you make max -2 instead and special case it in the C and C++ FE
attribute handling and in a testcase try something that is a balanced token
sequence but not really valid when parsed as ordinary attributes' arguments?

> --- a/gcc/attribs.c
> +++ b/gcc/attribs.c
> @@ -304,7 +304,7 @@ handle_ignored_attributes_option (vec<char *> *v)
>  	 We can't free it here, so squirrel away the pointers.  */
>        attribute_spec *table = new attribute_spec[2];
>        ignored_attributes_table.safe_push (table);
> -      table[0] = { attr, 0, 0, false, false, false, false, nullptr, nullptr };
> +      table[0] = { attr, 0, -1, false, false, false, false, nullptr, nullptr };
>        table[1] = { nullptr, 0, 0, false, false, false, false, nullptr,
>  		   nullptr };
>        register_scoped_attributes (table, IDENTIFIER_POINTER (vendor_id), !attr);
> diff --git a/gcc/testsuite/c-c++-common/Wno-attributes-6.c b/gcc/testsuite/c-c++-common/Wno-attributes-6.c
> new file mode 100644
> index 00000000000..02cdaaa1e89
> --- /dev/null
> +++ b/gcc/testsuite/c-c++-common/Wno-attributes-6.c
> @@ -0,0 +1,9 @@
> +/* PR c/103649 */
> +/* { dg-do compile { target { c || c++11 } } } */
> +/* { dg-additional-options "-Wno-attributes=foo::bar" } */
> +/* { dg-additional-options "-Wno-attributes=baz::" } */
> +
> +[[foo::bar(1, 2)]]; /* { dg-warning "attribute ignored" } */
> +[[baz::bar(1, 2)]]; /* { dg-warning "attribute ignored" } */
> +[[foo::bar(1, 2)]] void f1();
> +[[baz::bar(1, 2)]] void f2();
> 
> base-commit: f91814c22595e1db642140efe030caf2c092ab6f
> -- 
> 2.33.1

	Jakub
  

Patch

diff --git a/gcc/attribs.c b/gcc/attribs.c
index 29703e75fba..4933f020f1a 100644
--- a/gcc/attribs.c
+++ b/gcc/attribs.c
@@ -304,7 +304,7 @@  handle_ignored_attributes_option (vec<char *> *v)
 	 We can't free it here, so squirrel away the pointers.  */
       attribute_spec *table = new attribute_spec[2];
       ignored_attributes_table.safe_push (table);
-      table[0] = { attr, 0, 0, false, false, false, false, nullptr, nullptr };
+      table[0] = { attr, 0, -1, false, false, false, false, nullptr, nullptr };
       table[1] = { nullptr, 0, 0, false, false, false, false, nullptr,
 		   nullptr };
       register_scoped_attributes (table, IDENTIFIER_POINTER (vendor_id), !attr);
diff --git a/gcc/testsuite/c-c++-common/Wno-attributes-6.c b/gcc/testsuite/c-c++-common/Wno-attributes-6.c
new file mode 100644
index 00000000000..02cdaaa1e89
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wno-attributes-6.c
@@ -0,0 +1,9 @@ 
+/* PR c/103649 */
+/* { dg-do compile { target { c || c++11 } } } */
+/* { dg-additional-options "-Wno-attributes=foo::bar" } */
+/* { dg-additional-options "-Wno-attributes=baz::" } */
+
+[[foo::bar(1, 2)]]; /* { dg-warning "attribute ignored" } */
+[[baz::bar(1, 2)]]; /* { dg-warning "attribute ignored" } */
+[[foo::bar(1, 2)]] void f1();
+[[baz::bar(1, 2)]] void f2();