xref: /aosp_15_r20/external/clang/test/SemaCXX/attr-selectany.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fms-compatibility -fms-extensions -fsyntax-only -verify -std=c++11 %s
2*67e74705SXin Li // MSVC produces similar diagnostics.
3*67e74705SXin Li 
foo()4*67e74705SXin Li __declspec(selectany) void foo() { } // expected-error{{'selectany' can only be applied to data items with external linkage}}
5*67e74705SXin Li 
6*67e74705SXin Li __declspec(selectany) int x1 = 1;
7*67e74705SXin Li 
8*67e74705SXin Li const __declspec(selectany) int x2 = 2; // expected-error{{'selectany' can only be applied to data items with external linkage}}
9*67e74705SXin Li 
10*67e74705SXin Li extern const __declspec(selectany) int x3 = 3;
11*67e74705SXin Li 
12*67e74705SXin Li extern const int x4;
13*67e74705SXin Li const __declspec(selectany) int x4 = 4;
14*67e74705SXin Li 
15*67e74705SXin Li // MSDN says this is incorrect, but MSVC doesn't diagnose it.
16*67e74705SXin Li extern __declspec(selectany) int x5;
17*67e74705SXin Li 
18*67e74705SXin Li static __declspec(selectany) int x6 = 2; // expected-error{{'selectany' can only be applied to data items with external linkage}}
19*67e74705SXin Li 
20*67e74705SXin Li // FIXME: MSVC accepts this and makes x7 externally visible and comdat, but keep
21*67e74705SXin Li // it as internal and not weak/linkonce.
22*67e74705SXin Li static int x7; // expected-note{{previous definition}}
23*67e74705SXin Li extern __declspec(selectany) int x7;  // expected-warning{{attribute declaration must precede definition}}
24*67e74705SXin Li 
asdf()25*67e74705SXin Li int asdf() { return x7; }
26*67e74705SXin Li 
27*67e74705SXin Li class X {
28*67e74705SXin Li  public:
X(int i)29*67e74705SXin Li   X(int i) { i++; };
30*67e74705SXin Li   int i;
31*67e74705SXin Li };
32*67e74705SXin Li 
33*67e74705SXin Li __declspec(selectany) X x(1);
34*67e74705SXin Li 
35*67e74705SXin Li namespace { class Internal {}; }
36*67e74705SXin Li __declspec(selectany) auto x8 = Internal(); // expected-error {{'selectany' can only be applied to data items with external linkage}}
37*67e74705SXin Li 
38*67e74705SXin Li 
39*67e74705SXin Li // The D3D11 headers do something like this.  MSVC doesn't error on this at
40*67e74705SXin Li // all, even without the __declspec(selectany), in violation of the standard.
41*67e74705SXin Li // We fall back to a warning for selectany to accept headers.
42*67e74705SXin Li struct SomeStruct {
43*67e74705SXin Li   int foo;
44*67e74705SXin Li };
45*67e74705SXin Li extern const __declspec(selectany) SomeStruct some_struct; // expected-warning {{default initialization of an object of const type 'const SomeStruct' without a user-provided default constructor is a Microsoft extension}}
46*67e74705SXin Li 
47*67e74705SXin Li // It should be possible to redeclare variables that were defined
48*67e74705SXin Li // __declspec(selectany) previously.
49*67e74705SXin Li extern const SomeStruct some_struct;
50*67e74705SXin Li 
51*67e74705SXin Li // Without selectany, this should stay an error.
52*67e74705SXin Li const SomeStruct some_struct2; // expected-error {{default initialization of an object of const type 'const SomeStruct' without a user-provided default constructor}}
53