]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Sema/dllimport.c
Vendor import of clang trunk r290819:
[FreeBSD/FreeBSD.git] / test / Sema / dllimport.c
1 // RUN: %clang_cc1 -triple i686-win32     -fsyntax-only -fms-extensions -verify -std=c99 -DMS %s
2 // RUN: %clang_cc1 -triple x86_64-win32   -fsyntax-only -fms-extensions -verify -std=c11 -DMS %s
3 // RUN: %clang_cc1 -triple i686-mingw32   -fsyntax-only -fms-extensions -verify -std=c11 -DGNU %s
4 // RUN: %clang_cc1 -triple x86_64-mingw32 -fsyntax-only -fms-extensions -verify -std=c99 -DGNU %s
5
6 // Invalid usage.
7 __declspec(dllimport) typedef int typedef1;
8 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
9 typedef __declspec(dllimport) int typedef2;
10 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
11 typedef int __declspec(dllimport) typedef3;
12 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
13 typedef __declspec(dllimport) void (*FunTy)();
14 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
15 enum __declspec(dllimport) Enum { EnumVal };
16 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
17 struct __declspec(dllimport) Record {};
18 // expected-warning@-1{{'dllimport' attribute only applies to variables and functions}}
19
20
21
22 //===----------------------------------------------------------------------===//
23 // Globals
24 //===----------------------------------------------------------------------===//
25
26 // Import declaration.
27 __declspec(dllimport) extern int ExternGlobalDecl;
28
29 // dllimport implies a declaration.
30 __declspec(dllimport) int GlobalDecl;
31 int **__attribute__((dllimport))* GlobalDeclChunkAttr;
32 int GlobalDeclAttr __attribute__((dllimport));
33
34 // Address of variables can't be used for initialization in C language modes.
35 int *VarForInit = &GlobalDecl; // expected-error{{initializer element is not a compile-time constant}}
36
37 // Not allowed on definitions.
38 __declspec(dllimport) extern int ExternGlobalInit = 1; // expected-error{{definition of dllimport data}}
39 __declspec(dllimport) int GlobalInit1 = 1; // expected-error{{definition of dllimport data}}
40 int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dllimport data}}
41
42 // Declare, then reject definition.
43 #ifdef GNU
44 // expected-note@+2{{previous attribute is here}}
45 #endif
46 __declspec(dllimport) extern int ExternGlobalDeclInit; // expected-note{{previous declaration is here}}
47 #ifdef MS
48 // expected-warning@+4{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
49 #else
50 // expected-warning@+2{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
51 #endif
52 int ExternGlobalDeclInit = 1;
53
54 #ifdef GNU
55 // expected-note@+2{{previous attribute is here}}
56 #endif
57 __declspec(dllimport) int GlobalDeclInit; // expected-note{{previous declaration is here}}
58 #ifdef MS
59 // expected-warning@+4{{'GlobalDeclInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
60 #else
61 // expected-warning@+2{{'GlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
62 #endif
63 int GlobalDeclInit = 1;
64
65 #ifdef GNU
66 // expected-note@+2{{previous attribute is here}}
67 #endif
68 int *__attribute__((dllimport)) GlobalDeclChunkAttrInit; // expected-note{{previous declaration is here}}
69 #ifdef MS
70 // expected-warning@+4{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
71 #else
72 // expected-warning@+2{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
73 #endif
74 int *GlobalDeclChunkAttrInit = 0;
75
76 #ifdef GNU
77 // expected-note@+2{{previous attribute is here}}
78 #endif
79 int GlobalDeclAttrInit __attribute__((dllimport)); // expected-note{{previous declaration is here}}
80 #ifdef MS
81 // expected-warning@+4{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
82 #else
83 // expected-warning@+2{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
84 #endif
85 int GlobalDeclAttrInit = 1;
86
87 // Redeclarations
88 __declspec(dllimport) extern int GlobalRedecl1;
89 __declspec(dllimport) extern int GlobalRedecl1;
90
91 __declspec(dllimport) int GlobalRedecl2a;
92 __declspec(dllimport) int GlobalRedecl2a;
93
94 int *__attribute__((dllimport)) GlobalRedecl2b;
95 int *__attribute__((dllimport)) GlobalRedecl2b;
96
97 int GlobalRedecl2c __attribute__((dllimport));
98 int GlobalRedecl2c __attribute__((dllimport));
99
100 // We follow GCC and drop the dllimport with a warning.
101 __declspec(dllimport) extern int GlobalRedecl3; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
102                       extern int GlobalRedecl3; // expected-warning{{'GlobalRedecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
103
104 // Adding an attribute on redeclaration.
105                       extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
106 int useGlobalRedecl4() { return GlobalRedecl4; }
107 __declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
108
109 // Allow with a warning if the decl hasn't been used yet.
110                       extern int GlobalRedecl5; // expected-note{{previous declaration is here}}
111 __declspec(dllimport) extern int GlobalRedecl5; // expected-warning{{redeclaration of 'GlobalRedecl5' should not add 'dllimport' attribute}}
112
113
114 // External linkage is required.
115 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
116
117 // Thread local variables are invalid.
118 __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}}
119
120 // Import in local scope.
121 __declspec(dllimport) float LocalRedecl1; // expected-note{{previous declaration is here}}
122 __declspec(dllimport) float LocalRedecl2; // expected-note{{previous declaration is here}}
123 __declspec(dllimport) float LocalRedecl3; // expected-note{{previous declaration is here}}
124 __declspec(dllimport) float LocalRedecl4;
125 void functionScope() {
126   __declspec(dllimport) int LocalRedecl1; // expected-error{{redeclaration of 'LocalRedecl1' with a different type: 'int' vs 'float'}}
127   int *__attribute__((dllimport)) LocalRedecl2; // expected-error{{redeclaration of 'LocalRedecl2' with a different type: 'int *' vs 'float'}}
128   int LocalRedecl3 __attribute__((dllimport)); // expected-error{{redeclaration of 'LocalRedecl3' with a different type: 'int' vs 'float'}}
129
130   __declspec(dllimport)        int LocalVarDecl;
131   __declspec(dllimport)        int LocalVarDef = 1; // expected-error{{definition of dllimport data}}
132   __declspec(dllimport) extern int ExternLocalVarDecl;
133   __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}}
134   __declspec(dllimport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllimport'}}
135
136   // Local extern redeclaration does not drop the attribute.
137   extern float LocalRedecl4;
138 }
139
140
141
142 //===----------------------------------------------------------------------===//
143 // Functions
144 //===----------------------------------------------------------------------===//
145
146 // Import function declaration. Check different placements.
147 __attribute__((dllimport)) void decl1A(); // Sanity check with __attribute__
148 __declspec(dllimport)      void decl1B();
149
150 void __attribute__((dllimport)) decl2A();
151 void __declspec(dllimport)      decl2B();
152
153 // Address of functions can be used for initialization in C language modes.
154 // However, the address of the thunk wrapping the function is used instead of
155 // the address in the import address table.
156 void (*FunForInit)() = &decl2A;
157
158 // Not allowed on function definitions.
159 __declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}}
160
161 // Import inline function.
162 #ifdef GNU
163 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
164 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
165 #endif
166 __declspec(dllimport) inline void inlineFunc1() {}
167 inline void __attribute__((dllimport)) inlineFunc2() {}
168
169 // Redeclarations
170 __declspec(dllimport) void redecl1();
171 __declspec(dllimport) void redecl1();
172
173 __declspec(dllimport) void redecl2(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
174                       void redecl2(); // expected-warning{{'redecl2' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
175
176 #ifdef GNU
177                       // expected-note@+2{{previous attribute is here}}
178 #endif
179                       __declspec(dllimport) void redecl3(); // expected-note{{previous declaration is here}}
180                       // NB: Both MSVC and Clang issue a warning and make redecl3 dllexport.
181 #ifdef MS
182                       // expected-warning@+4{{'redecl3' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
183 #else
184                       // expected-warning@+2{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
185 #endif
186                       void redecl3() {}
187
188                       void redecl4(); // expected-note{{previous declaration is here}}
189 void useRedecl4() { redecl4(); }
190 __declspec(dllimport) void redecl4(); // expected-warning{{redeclaration of 'redecl4' should not add 'dllimport' attribute}}
191
192 // Allow with a warning if the decl hasn't been used yet.
193                       void redecl5(); // expected-note{{previous declaration is here}}
194 __declspec(dllimport) void redecl5(); // expected-warning{{redeclaration of 'redecl5' should not add 'dllimport' attribute}}
195
196
197 // Inline redeclarations.
198 #ifdef GNU
199 // expected-warning@+3{{'redecl6' redeclared inline; 'dllimport' attribute ignored}}
200 #endif
201 __declspec(dllimport) void redecl6();
202                       inline void redecl6() {}
203
204 #ifdef MS
205 // expected-note@+5{{previous declaration is here}}
206 // expected-warning@+5{{redeclaration of 'redecl7' should not add 'dllimport' attribute}}
207 #else
208 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
209 #endif
210                       void redecl7();
211 __declspec(dllimport) inline void redecl7() {}
212
213 // PR31069: Don't crash trying to merge attributes for redeclaration of invalid decl.
214 void __declspec(dllimport) redecl8(unknowntype X); // expected-error{{unknown type name 'unknowntype'}}
215 void redecl8(unknowntype X) { } // expected-error{{unknown type name 'unknowntype'}}
216
217 // External linkage is required.
218 __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}
219
220 // Static locals don't count as having external linkage.
221 void staticLocalFunc() {
222   __declspec(dllimport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllimport'}}
223 }