[clang] [Clang] disallow selectany on non-global-variable declarations (PR #189641)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 31 07:40:54 PDT 2026
================
@@ -53,3 +53,14 @@ extern const SomeStruct some_struct;
// Without selectany, this should stay an error.
const SomeStruct some_struct2; // expected-error {{default initialization of an object of const type 'const SomeStruct' without a user-provided default constructor}}
+
+struct __declspec(selectany) S1 {}; // expected-error {{'selectany' attribute only applies to global variables}}
+__declspec(selectany) struct S1 s1;
+
+void t() {
+ __declspec(selectany) int x; // expected-error {{'selectany' attribute only applies to global variables}}
+ __declspec(selectany) extern int y;
+}
+
+struct S2 {};
+struct __declspec(selectany) S2 s2; // expected-error {{'selectany' attribute only applies to global variables}}
----------------
AaronBallman wrote:
There's one more case to consider: https://godbolt.org/z/5zr9q75eK
It seems GCC and Clang both currently accept that case while MSVC rejects, but I can't tell whether that's intentional behavior for MSVC or not. A static data member is a global variable, at least notionally. But it does not have external linkage, which is what I think the attribute actually requires. So maybe it's an MSVC bug to reject or maybe it's a Clang/GCC bug to accept, or maybe this is an intentional distinction between the syntaxes. I think more research is needed to know what direction to go.
https://github.com/llvm/llvm-project/pull/189641
More information about the cfe-commits
mailing list