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

>From bf5f6b798533a60a10a8acc489ae29a9181d81b4 Mon Sep 17 00:00:00 2001
From: Daniel Petrovic <[email protected]>
Date: Fri, 13 Nov 2026 16:00:17 +0100
Subject: [PATCH] [clang] Accept mixed leading __declspec, GNU, and standard
 attributes

Follow up on 4e9d349ab86d by allowing __declspec in any order with GNU
and standard attributes at global and namespace scope and in linkage
specifications.
---
 clang/docs/ReleaseNotes.md                    |  2 +
 clang/lib/Parse/ParseDeclCXX.cpp              |  9 ++--
 clang/lib/Parse/Parser.cpp                    |  9 ++--
 clang/test/Parser/attr-order.cpp              | 48 +++++++++++++++++--
 .../Parser/extern-template-attributes.cpp     |  2 +-
 5 files changed, 59 insertions(+), 11 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 563b711fb89db0..da4fbb20d847c3 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -950,6 +950,8 @@ documentation](CIR/index.md) for more information regarding 
ClangIR's status and
 
 - Fixed rejection of standard attributes following GNU attributes on
   declarations inside namespaces. (#GH240537)
+- Clang now accepts leading ``__declspec``, GNU, and standard attributes in any
+  order at global and namespace scope, and in linkage specifications.
 
 - 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 c7135b2ff9ba6d..ff6fd169947733 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -229,7 +229,8 @@ void Parser::ParseInnerNamespace(const 
InnerNamespaceInfoList &InnerNSs,
       ParsedAttributes DeclAttrs(AttrFactory);
       ParsedAttributes DeclSpecAttrs(AttrFactory);
       while (MaybeParseCXX11Attributes(DeclAttrs) ||
-             MaybeParseGNUAttributes(DeclSpecAttrs))
+             MaybeParseGNUAttributes(DeclSpecAttrs) ||
+             MaybeParseMicrosoftDeclSpecs(DeclSpecAttrs))
         ;
       ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
     }
@@ -326,7 +327,8 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, 
DeclaratorContext Context) {
   ParsedAttributes DeclSpecAttrs(AttrFactory);
 
   while (MaybeParseCXX11Attributes(DeclAttrs) ||
-         MaybeParseGNUAttributes(DeclSpecAttrs))
+         MaybeParseGNUAttributes(DeclSpecAttrs) ||
+         MaybeParseMicrosoftDeclSpecs(DeclSpecAttrs))
     ;
 
   if (Tok.isNot(tok::l_brace)) {
@@ -379,7 +381,8 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, 
DeclaratorContext Context) {
       ParsedAttributes DeclAttrs(AttrFactory);
       ParsedAttributes DeclSpecAttrs(AttrFactory);
       while (MaybeParseCXX11Attributes(DeclAttrs) ||
-             MaybeParseGNUAttributes(DeclSpecAttrs))
+             MaybeParseGNUAttributes(DeclSpecAttrs) ||
+             MaybeParseMicrosoftDeclSpecs(DeclSpecAttrs))
         ;
       ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
       continue;
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index bf13cdc2f9aca5..c3e7bab2a63840 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -710,12 +710,13 @@ bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result,
 
   ParsedAttributes DeclAttrs(AttrFactory);
   ParsedAttributes DeclSpecAttrs(AttrFactory);
-  // GNU attributes are applied to the declaration specification while the
-  // standard attributes are applied to the declaration.  We parse the two
-  // attribute sets into different containters so we can apply them during
+  // GNU and Microsoft attributes apply to the declaration specification,
+  // while standard attributes apply to the declaration. We parse the two
+  // attribute sets into different containers so we can apply them during
   // the regular parsing process.
   while (MaybeParseCXX11Attributes(DeclAttrs) ||
-         MaybeParseGNUAttributes(DeclSpecAttrs))
+         MaybeParseGNUAttributes(DeclSpecAttrs) ||
+         MaybeParseMicrosoftDeclSpecs(DeclSpecAttrs))
     ;
 
   Result = ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
diff --git a/clang/test/Parser/attr-order.cpp b/clang/test/Parser/attr-order.cpp
index 75b9c9c89c6969..dae3254763351a 100644
--- a/clang/test/Parser/attr-order.cpp
+++ b/clang/test/Parser/attr-order.cpp
@@ -1,3 +1,4 @@
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -fdeclspec -Wno-ignored-attributes 
-verify %s
 // RUN: %clang_cc1 -fsyntax-only -fms-extensions -Wno-ignored-attributes 
-verify %s
 
 struct [[]] __attribute__((lockable)) __declspec(dllexport) A {}; // ok
@@ -13,9 +14,9 @@ struct [[]] __attribute__((lockable)) [[]] 
__declspec(dllexport) H {}; // ok
 [[noreturn]] __declspec(dllexport) __attribute__((cdecl)) void b(); // ok
 [[]] [[noreturn]] __attribute__((cdecl)) __declspec(dllexport) void c(); // ok
 
-__declspec(dllexport) [[noreturn]] __attribute__((cdecl)) void d(); // 
expected-error {{an attribute list cannot appear here}}
-__declspec(dllexport) __attribute__((cdecl)) [[noreturn]] void e(); // 
expected-error {{an attribute list cannot appear here}}
-__attribute__((cdecl)) __declspec(dllexport) [[noreturn]] void f(); // 
expected-error {{an attribute list cannot appear here}}
+__declspec(dllexport) [[noreturn]] __attribute__((cdecl)) void d(); // ok
+__declspec(dllexport) __attribute__((cdecl)) [[noreturn]] void e(); // ok
+__attribute__((cdecl)) __declspec(dllexport) [[noreturn]] void f(); // ok
 
 __attribute__((cdecl)) [[noreturn]] __declspec(dllexport) void g(); // ok
 
@@ -60,3 +61,44 @@ __attribute__((warn_unused_result)) [[maybe_unused]] int f();
 [[maybe_unused]] __attribute__((warn_unused_result)) [[]]
 __attribute__((nothrow)) [[nodiscard]] int h();
 }
+
+// Leading attribute permutations at global and namespace scope.
+[[]] __declspec() int declspecAfterStandard;
+__declspec() [[]] int standardAfterDeclspec;
+
+__attribute__(()) [[]] __declspec() int mixedGlobal0;
+[[]] __attribute__(()) __declspec() int mixedGlobal1;
+__attribute__(()) __declspec() [[]] int mixedGlobal2;
+__declspec() __attribute__(()) [[]] int mixedGlobal3;
+__declspec() [[]] __attribute__(()) int mixedGlobal4;
+[[]] __declspec() __attribute__(()) int mixedGlobal5;
+
+namespace Mixed {
+[[]] __declspec() int declspecAfterStandard;
+__declspec() [[]] int standardAfterDeclspec;
+__attribute__(()) [[]] __declspec() int mixedNamed0;
+[[]] __attribute__(()) __declspec() int mixedNamed1;
+__attribute__(()) __declspec() [[]] int mixedNamed2;
+__declspec() __attribute__(()) [[]] int mixedNamed3;
+__declspec() [[]] __attribute__(()) int mixedNamed4;
+[[]] __declspec() __attribute__(()) int mixedNamed5;
+}
+__declspec(align(32)) [[maybe_unused]] __attribute__((aligned(16)))
+__declspec() [[]] int preservedDeclspec;
+static_assert(__alignof__(preservedDeclspec) == 32, "declspec must be 
preserved");
+__declspec() [[maybe_unused]] __attribute__((aligned(64))) int preservedGNU;
+static_assert(__alignof__(preservedGNU) == 64, "GNU attribute must be 
preserved");
+__declspec() [[nodiscard]] __attribute__(()) int preservedStandard();
+void checkStandard() {
+  preservedStandard(); // expected-warning {{ignoring return value of function 
declared with 'nodiscard' attribute}}
+}
+
+// Mixed leading attributes also work in linkage specifications.
+extern "C" {
+__declspec() [[maybe_unused]] int linkageBlock0;
+__declspec() __attribute__(()) [[maybe_unused]] int linkageBlock1;
+__attribute__(()) __declspec() [[maybe_unused]] int linkageBlock2;
+}
+extern "C" __declspec() [[maybe_unused]] int linkageSingle0;
+extern "C" __declspec() __attribute__(()) [[maybe_unused]] int linkageSingle1;
+extern "C" __attribute__(()) __declspec() [[maybe_unused]] int linkageSingle2;
diff --git a/clang/test/Parser/extern-template-attributes.cpp 
b/clang/test/Parser/extern-template-attributes.cpp
index 1301f2f680ccb3..b0d17a6426a076 100644
--- a/clang/test/Parser/extern-template-attributes.cpp
+++ b/clang/test/Parser/extern-template-attributes.cpp
@@ -5,4 +5,4 @@ struct S {};
 
 [[deprecated]] extern template struct S<int>;              // expected-error 
{{an attribute list cannot appear here}}
 __attribute__((deprecated)) extern template struct S<int>; // expected-error 
{{an attribute list cannot appear here}}
-__declspec(deprecated) extern template struct S<int>;      // expected-error 
{{expected unqualified-id}}
+__declspec(deprecated) extern template struct S<int>;      // expected-error 
{{an attribute list cannot appear here}}

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

Reply via email to