[clang] f9c2f82 - [Clang] Fix unknown type attributes diagnosed twice with [[]] spelling

Jun Zhang via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 12 06:12:40 PDT 2022


Author: Jun Zhang
Date: 2022-04-12T21:11:51+08:00
New Revision: f9c2f821d71b7bbb2544a489b7798bd173ea8907

URL: https://github.com/llvm/llvm-project/commit/f9c2f821d71b7bbb2544a489b7798bd173ea8907
DIFF: https://github.com/llvm/llvm-project/commit/f9c2f821d71b7bbb2544a489b7798bd173ea8907.diff

LOG: [Clang] Fix unknown type attributes diagnosed twice with [[]] spelling

Don't warn on unknown type attributes in Parser::ProhibitCXX11Attributes
for most cases, but left the diagnostic to the later checks.
module declaration and module import declaration are special cases.

Fixes https://github.com/llvm/llvm-project/issues/54817

Differential Revision: https://reviews.llvm.org/D123447

Added: 
    clang/test/SemaCXX/warn-once-on-unknown-attr.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Parse/Parser.h
    clang/lib/Parse/ParseDecl.cpp
    clang/lib/Parse/Parser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fbc1edb8e9e2a..a1ff6f8787bb1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -117,6 +117,8 @@ Bug Fixes
   `C++20 [dcl.fct.def.general]p2 <https://timsong-cpp.github.io/cppwp/n4868/dcl.fct.def#general-2.sentence-3>`_,
   Clang should not diagnose incomplete types in function definitions if the function body is "= delete;".
   This fixes Issue `Issue 52802 <https://github.com/llvm/llvm-project/issues/52802>`_.
+- Unknown type attributes with a ``[[]]`` spelling are no longer diagnosed twice.
+  This fixes Issue `Issue 54817 <https://github.com/llvm/llvm-project/issues/54817>`_.
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 3241931345297..d6cb68aaadca7 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -2633,8 +2633,12 @@ class Parser : public CodeCompletionHandler {
   // Forbid C++11 and C2x attributes that appear on certain syntactic locations
   // which standard permits but we don't supported yet, for example, attributes
   // appertain to decl specifiers.
+  // For the most cases we don't want to warn on unknown type attributes, but
+  // left them to later diagnoses. However, for a few cases like module
+  // declarations and module import declarations, we should do it.
   void ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
-                               bool DiagnoseEmptyAttrs = false);
+                               bool DiagnoseEmptyAttrs = false,
+                               bool WarnOnUnknownAttrs = false);
 
   /// Skip C++11 and C2x attributes and return the end location of the
   /// last one.

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 627b11fb6ac0c..f912d9d99efd9 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -1658,7 +1658,8 @@ void Parser::DiagnoseProhibitedAttributes(
 }
 
 void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
-                                     bool DiagnoseEmptyAttrs) {
+                                     bool DiagnoseEmptyAttrs,
+                                     bool WarnOnUnknownAttrs) {
 
   if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
     // An attribute list has been parsed, but it was empty.
@@ -1685,10 +1686,11 @@ void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
   for (const ParsedAttr &AL : Attrs) {
     if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
       continue;
-    if (AL.getKind() == ParsedAttr::UnknownAttribute)
-      Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
-          << AL << AL.getRange();
-    else {
+    if (AL.getKind() == ParsedAttr::UnknownAttribute) {
+      if (WarnOnUnknownAttrs)
+        Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
+            << AL << AL.getRange();
+    } else {
       Diag(AL.getLoc(), DiagID) << AL;
       AL.setInvalid();
     }

diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index f754388cdc7c8..2550cdd9f5bbf 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -2386,7 +2386,9 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
   // We don't support any module attributes yet; just parse them and diagnose.
   ParsedAttributes Attrs(AttrFactory);
   MaybeParseCXX11Attributes(Attrs);
-  ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr);
+  ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr,
+                          /*DiagnoseEmptyAttrs=*/false,
+                          /*WarnOnUnknownAttrs=*/true);
 
   ExpectAndConsumeSemi(diag::err_module_expected_semi);
 
@@ -2453,7 +2455,9 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
   ParsedAttributes Attrs(AttrFactory);
   MaybeParseCXX11Attributes(Attrs);
   // We don't support any module import attributes yet.
-  ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr);
+  ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr,
+                          /*DiagnoseEmptyAttrs=*/false,
+                          /*WarnOnUnknownAttrs=*/true);
 
   if (PP.hadModuleLoaderFatalFailure()) {
     // With a fatal failure in the module loader, we abort parsing.

diff  --git a/clang/test/SemaCXX/warn-once-on-unknown-attr.cpp b/clang/test/SemaCXX/warn-once-on-unknown-attr.cpp
new file mode 100644
index 0000000000000..5a7452c40d367
--- /dev/null
+++ b/clang/test/SemaCXX/warn-once-on-unknown-attr.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c2x -x c %s
+void foo() {
+  int [[attr]] i;             // expected-warning {{unknown attribute 'attr' ignored}}
+  (void)sizeof(int [[attr]]); // expected-warning {{unknown attribute 'attr' ignored}}
+}
+
+void bar() {
+  [[attr]];       // expected-warning {{unknown attribute 'attr' ignored}}
+  [[attr]] int i; // expected-warning {{unknown attribute 'attr' ignored}}
+}


        


More information about the cfe-commits mailing list