[clang] [Clang] disallow selectany on non-global-variable declarations (PR #189641)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 6 07:31:38 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:

I don't think anything is on fire with this issue such that we need to rush a fix; I'd recommend filing the issue with Microsoft, give them a few weeks to weigh in, and then re-evaluate at that point. If we hear nothing back from Microsoft, I think rejecting those examples is defensible even though Microsoft accepts them, but so is following Microsoft's behavior, so we'll just have to muddle through.

https://github.com/llvm/llvm-project/pull/189641


More information about the cfe-commits mailing list