[clang] [Clang] Accept mixed __declspec and standard attributes (PR #242362)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 07:49:11 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Daniel Petrovic (daniel-petrovic)
<details>
<summary>Changes</summary>
Follow up on 4e9d349ab86d, which allowed mixed GNU and standard attributes inside namespaces. Also accept leading __declspec attributes in any order with GNU and standard attributes at global and namespace scope.
---
Full diff: https://github.com/llvm/llvm-project/pull/242362.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+2)
- (modified) clang/lib/Parse/ParseDeclCXX.cpp (+2-1)
- (modified) clang/lib/Parse/Parser.cpp (+5-4)
- (modified) clang/test/Parser/attr-order.cpp (+35-3)
- (modified) clang/test/Parser/extern-template-attributes.cpp (+1-1)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 14382a06db34c..90bc2d45f0583 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -943,6 +943,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.
- 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 c7135b2ff9ba6..0898e1c397ed2 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);
}
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index bf13cdc2f9aca..c3e7bab2a6384 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 75b9c9c89c696..7fde1ef4b765c 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,34 @@ __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}}
+}
diff --git a/clang/test/Parser/extern-template-attributes.cpp b/clang/test/Parser/extern-template-attributes.cpp
index 1301f2f680ccb..b0d17a6426a07 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}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242362
More information about the cfe-commits
mailing list