]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
attribs: Fix wrong error with -Wno-attribute=A::b [PR103649]
authorMarek Polacek <polacek@redhat.com>
Thu, 16 Dec 2021 21:29:41 +0000 (16:29 -0500)
committerMarek Polacek <polacek@redhat.com>
Fri, 17 Dec 2021 22:56:26 +0000 (17:56 -0500)
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.  And since unknown attributes
can be rather complex (see for example omp::{directive,sequence}), we
must skip parsing their arguments.  To that end, I'm using max_length
with value -2.

Also let's not warn about things like

  [[vendor::assume(true)]];

because they may have some meaning (this is reminiscent of C++ Portable
Assumptions).

PR c/103649

gcc/ChangeLog:

* attribs.c (handle_ignored_attributes_option): Create the fake
attribute with max_length == -2.
(attribute_ignored_p): New overloads.
* attribs.h (attribute_ignored_p): Declare them.
* tree-core.h (struct attribute_spec): Document that max_length
can be -2.

gcc/c/ChangeLog:

* c-decl.c (c_warn_unused_attributes): Don't warn for
attribute_ignored_p.
* c-parser.c (c_parser_std_attribute): Skip parsing of the attribute
arguments when the attribute is ignored.

gcc/cp/ChangeLog:

* parser.c (cp_parser_declaration): Don't warn for attribute_ignored_p.
(cp_parser_std_attribute): Skip parsing of the attribute
arguments when the attribute is ignored.

gcc/testsuite/ChangeLog:

* c-c++-common/Wno-attributes-6.c: New test.

gcc/attribs.c
gcc/attribs.h
gcc/c/c-decl.c
gcc/c/c-parser.c
gcc/cp/parser.c
gcc/testsuite/c-c++-common/Wno-attributes-6.c [new file with mode: 0644]
gcc/tree-core.h

index 29703e75fba14d68521bf422e26b052367361676..9e7b7c1abd202864a9ad4d5c2cd7be7ec859316c 100644 (file)
@@ -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, -2, 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);
@@ -569,6 +569,32 @@ attr_namespace_ignored_p (tree ns)
   return r && r->ignored_p;
 }
 
+/* Return true if the attribute ATTR should not be warned about.  */
+
+bool
+attribute_ignored_p (tree attr)
+{
+  if (!cxx11_attribute_p (attr))
+    return false;
+  if (tree ns = get_attribute_namespace (attr))
+    {
+      if (attr_namespace_ignored_p (ns))
+       return true;
+      const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attr));
+      if (as && as->max_length == -2)
+       return true;
+    }
+  return false;
+}
+
+/* Like above, but takes an attribute_spec AS, which must be nonnull.  */
+
+bool
+attribute_ignored_p (const attribute_spec *const as)
+{
+  return as->max_length == -2;
+}
+
 /* Process the attributes listed in ATTRIBUTES and install them in *NODE,
    which is either a DECL (including a TYPE_DECL) or a TYPE.  If a DECL,
    it should be modified in place; if a TYPE, a copy should be created
index f5899d83c0be80cfc3806d9b4d6ced537b102494..4928b126f59c1073185f2fe35a06f17ee0c4d238 100644 (file)
@@ -39,6 +39,8 @@ extern tree get_attribute_name (const_tree);
 extern tree get_attribute_namespace (const_tree);
 extern void apply_tm_attr (tree, tree);
 extern tree make_attribute (const char *, const char *, tree);
+extern bool attribute_ignored_p (tree);
+extern bool attribute_ignored_p (const attribute_spec *const);
 
 extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *,
                                                             const char *,
index 4b5481ce8f4fb21c4de873c923306f37695619e4..516e3c26e217a7311a000b615a55a80453565cd1 100644 (file)
@@ -4643,7 +4643,7 @@ c_warn_unused_attributes (tree attrs)
         constraint violation.  */
       pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
               get_attribute_name (t));
-    else
+    else if (!attribute_ignored_p (t))
       warning (OPT_Wattributes, "%qE attribute ignored",
               get_attribute_name (t));
 }
index d7e5f051ac0a31c8ef9a277acf7f81b56ec87467..b09ad307acdafa3e25b8112318788e91df7f966f 100644 (file)
@@ -4943,7 +4943,9 @@ c_parser_std_attribute (c_parser *parser, bool for_tm)
        parens.skip_until_found_close (parser);
        return error_mark_node;
       }
-    if (as)
+    /* If this is a fake attribute created to handle -Wno-attributes,
+       we must skip parsing the arguments.  */
+    if (as && !attribute_ignored_p (as))
       {
        bool takes_identifier
          = (ns != NULL_TREE
index 44eed7ea63877e9a04e423450c14283f8c70ec44..33fb40a5b59a3fb09f0c35ea6073658be31b3b81 100644 (file)
@@ -14776,7 +14776,7 @@ cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
            }
        }
 
-      if (std_attrs != NULL_TREE)
+      if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
        warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
                    OPT_Wattributes, "attribute ignored");
       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
@@ -28979,7 +28979,9 @@ cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
       /* A GNU attribute that takes an identifier in parameter.  */
       attr_flag = id_attr;
 
-    if (as == NULL)
+    /* If this is a fake attribute created to handle -Wno-attributes,
+       we must skip parsing the arguments.  */
+    if (as == NULL || attribute_ignored_p (as))
       {
        if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
          {
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 (file)
index 0000000..ca3c7be
--- /dev/null
@@ -0,0 +1,16 @@
+/* PR c/103649 */
+/* { dg-do compile { target { c || c++11 } } } */
+/* { dg-additional-options "-Wno-attributes=foo::bar" } */
+/* { dg-additional-options "-Wno-attributes=baz::" } */
+/* { dg-additional-options "-Wno-attributes=womp::womp" } */
+/* { dg-additional-options "-Wno-attributes=qux::foo" } */
+/* { dg-additional-options "-Wno-attributes=vendor::assume" } */
+
+[[vendor::assume(1 + 1 == 2)]];
+[[foo::bar(1, 2)]];
+[[baz::bar(1, 2)]];
+[[foo::bar(1, 2)]] void f1();
+[[baz::bar(1, 2)]] void f2();
+[[qux::foo({t})]] void f3(); 
+[[womp::womp (another::directive (threadprivate (t)))]] void f4();
+[[womp::womp (another::directive (threadprivate (t)))]];
index 91ae5237d7eb44bd624e9a5f349b750ed87c1dfe..9b37a065d18979d7c64d84a7b60cd73c5c809ac4 100644 (file)
@@ -2077,7 +2077,9 @@ struct attribute_spec {
   /* The minimum length of the list of arguments of the attribute.  */
   int min_length;
   /* The maximum length of the list of arguments of the attribute
-     (-1 for no maximum).  */
+     (-1 for no maximum).  It can also be -2 for fake attributes
+     created for the sake of -Wno-attributes; in that case, we
+     should skip the balanced token sequence when parsing the attribute.  */
   int max_length;
   /* Whether this attribute requires a DECL.  If it does, it will be passed
      from types of DECLs, function return types and array element types to