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