https://github.com/daniel-petrovic updated 
https://github.com/llvm/llvm-project/pull/240734

>From 70990bc46537a444e52e3777920998eabd0c977f Mon Sep 17 00:00:00 2001
From: Daniel Petrovic <[email protected]>
Date: Tue, 10 Nov 2026 08:08:20 +0100
Subject: [PATCH] [Clang] Accept mixed GNU and standard attributes in namespace
 scope

Parse leading GNU and standard attributes in namespace scope using the
same loop as global declarations. Previously, a standard attribute
following a GNU attribute was rejected with "an attribute list cannot
appear here".

Fixes #240537.
---
 clang/docs/ReleaseNotes.md       |  3 +++
 clang/lib/Parse/ParseDeclCXX.cpp |  8 +++++---
 clang/test/Parser/attr-order.cpp | 16 ++++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index ae351da057cc8d..2a3ba538d24027 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -926,6 +926,9 @@ documentation](CIR/index.md) for more information regarding 
ClangIR's status and
 
 #### Bug Fixes to Attribute Support
 
+- Fixed rejection of standard attributes following GNU attributes on
+  declarations inside namespaces. (#GH240537)
+
 - Fixed a crash when using a zero-argument call to an undeclared
   `__CFStringMakeConstantString` as a format string. (#GH225034)
 
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index d78a630d0805f8..c7135b2ff9ba6d 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -227,9 +227,11 @@ void Parser::ParseInnerNamespace(const 
InnerNamespaceInfoList &InnerNSs,
     while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
            Tok.isNot(tok::eof)) {
       ParsedAttributes DeclAttrs(AttrFactory);
-      MaybeParseCXX11Attributes(DeclAttrs);
-      ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
-      ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs);
+      ParsedAttributes DeclSpecAttrs(AttrFactory);
+      while (MaybeParseCXX11Attributes(DeclAttrs) ||
+             MaybeParseGNUAttributes(DeclSpecAttrs))
+        ;
+      ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
     }
 
     // The caller is what called check -- we are simply calling
diff --git a/clang/test/Parser/attr-order.cpp b/clang/test/Parser/attr-order.cpp
index 21e30e5b6f9df1..75b9c9c89c6969 100644
--- a/clang/test/Parser/attr-order.cpp
+++ b/clang/test/Parser/attr-order.cpp
@@ -44,3 +44,19 @@ extern "C" {
 
 extern "C" __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int 
p(int); // ok
 extern "C" [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int 
q(int); // ok
+
+// Mixed GNU and standard attributes also work in namespace scope (GH240537).
+namespace {
+__attribute__((unused)) [[maybe_unused]] int x;
+[[maybe_unused]] __attribute__((unused)) int y;
+__attribute__((unused)) [[maybe_unused]]
+__attribute__((aligned(16))) [[]] int z;
+static_assert(__alignof__(z) == 16, "GNU attributes must be preserved");
+}
+
+namespace N {
+__attribute__((warn_unused_result)) [[maybe_unused]] int f();
+[[maybe_unused]] __attribute__((warn_unused_result)) int g();
+[[maybe_unused]] __attribute__((warn_unused_result)) [[]]
+__attribute__((nothrow)) [[nodiscard]] int h();
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to