[clang] 1a73654 - [Clang] disallow attributes after namespace identifier (#121614)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 9 07:01:36 PST 2025


Author: Oleksandr T.
Date: 2025-01-09T17:01:30+02:00
New Revision: 1a73654b3212b623ac21b9deb3aeaadc6906b7e4

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

LOG: [Clang] disallow attributes after namespace identifier (#121614)

Fixes #121407

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseDeclCXX.cpp
    clang/test/Parser/namespace-attributes.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e7e4e37282e527..c50260b5488512 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -553,6 +553,8 @@ Attribute Changes in Clang
 - Clang now permits the usage of the placement new operator in ``[[msvc::constexpr]]``
   context outside of the std namespace. (#GH74924)
 
+- Clang now disallows the use of attributes after the namespace name. (#GH121407)
+
 Improvements to Clang's diagnostics
 -----------------------------------
 

diff  --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index f30603feb65c5d..fddb8a5729ee8c 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -81,26 +81,16 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
 
   ParsedAttributes attrs(AttrFactory);
 
-  auto ReadAttributes = [&] {
-    bool MoreToParse;
-    do {
-      MoreToParse = false;
-      if (Tok.is(tok::kw___attribute)) {
-        ParseGNUAttributes(attrs);
-        MoreToParse = true;
-      }
-      if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
+  while (MaybeParseGNUAttributes(attrs) || isAllowedCXX11AttributeSpecifier()) {
+    if (isAllowedCXX11AttributeSpecifier()) {
+      if (getLangOpts().CPlusPlus11)
         Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
                                     ? diag::warn_cxx14_compat_ns_enum_attribute
                                     : diag::ext_ns_enum_attribute)
             << 0 /*namespace*/;
-        ParseCXX11Attributes(attrs);
-        MoreToParse = true;
-      }
-    } while (MoreToParse);
-  };
-
-  ReadAttributes();
+      ParseCXX11Attributes(attrs);
+    }
+  }
 
   if (Tok.is(tok::identifier)) {
     Ident = Tok.getIdentifierInfo();
@@ -126,7 +116,9 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
     }
   }
 
-  ReadAttributes();
+  DiagnoseAndSkipCXX11Attributes();
+  MaybeParseGNUAttributes(attrs);
+  DiagnoseAndSkipCXX11Attributes();
 
   SourceLocation attrLoc = attrs.Range.getBegin();
 

diff  --git a/clang/test/Parser/namespace-attributes.cpp b/clang/test/Parser/namespace-attributes.cpp
index 9f925b742dfebd..11bf8711cfad5d 100644
--- a/clang/test/Parser/namespace-attributes.cpp
+++ b/clang/test/Parser/namespace-attributes.cpp
@@ -4,38 +4,34 @@ namespace __attribute__(()) A
 {
 }
 
-namespace A __attribute__(())
+namespace A __attribute__(()) [[]] // expected-error {{an attribute list cannot appear here}}
 {
 }
 
-namespace __attribute__(()) [[]] A
-{
-}
-
-namespace [[]] __attribute__(()) A
+namespace A [[]] __attribute__(()) // expected-error {{an attribute list cannot appear here}}
 {
 }
 
-namespace A __attribute__(()) [[]]
+namespace [[]] A __attribute__(())
 {
 }
 
-namespace A [[]] __attribute__(())
+namespace [[]] __attribute__(()) A
 {
 }
 
-namespace [[]] A __attribute__(())
+namespace __attribute__(()) [[]] A
 {
 }
 
-namespace __attribute__(()) A [[]]
+namespace __attribute__(()) A [[]] // expected-error {{an attribute list cannot appear here}}
 {
 }
 
-namespace A::B __attribute__(()) // expected-error{{attributes cannot be specified on a nested namespace definition}}
+namespace A::B __attribute__(()) // expected-error {{attributes cannot be specified on a nested namespace definition}}
 {
 }
 
-namespace __attribute__(()) A::B // expected-error{{attributes cannot be specified on a nested namespace definition}}
+namespace __attribute__(()) A::B // expected-error {{attributes cannot be specified on a nested namespace definition}}
 {
 }


        


More information about the cfe-commits mailing list