xref: /llvm-project/clang/test/Sema/dllimport.c (revision 634c8ef69a836f3436d027b03965965bad6f3ff0)
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
11 
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}}
25 
26 
27 
28 //===----------------------------------------------------------------------===//
29 // Globals
30 //===----------------------------------------------------------------------===//
31 
32 // Import declaration.
33 __declspec(dllimport) extern int ExternGlobalDecl;
34 
35 // dllimport implies a declaration.
36 __declspec(dllimport) int GlobalDecl;
37 int **__attribute__((dllimport))* GlobalDeclChunkAttr;
38 int GlobalDeclAttr __attribute__((dllimport));
39 
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}}
42 
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}}
47 
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;
59 
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;
70 
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;
81 
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;
92 
93 // Redeclarations
94 __declspec(dllimport) extern int GlobalRedecl1;
95 __declspec(dllimport) extern int GlobalRedecl1;
96 
97 __declspec(dllimport) int GlobalRedecl2a;
98 __declspec(dllimport) int GlobalRedecl2a;
99 
100 int *__attribute__((dllimport)) GlobalRedecl2b;
101 int *__attribute__((dllimport)) GlobalRedecl2b;
102 
103 int GlobalRedecl2c __attribute__((dllimport));
104 int GlobalRedecl2c __attribute__((dllimport));
105 
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}}
109 
110 // Adding an attribute on redeclaration.
111                       extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
useGlobalRedecl4(void)112 int useGlobalRedecl4(void) { return GlobalRedecl4; }
113 __declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
114 
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}}
118 
119 
120 // External linkage is required.
121 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
122 
123 // Thread local variables are invalid.
124 __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}}
125 
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;
functionScope(void)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'}}
135 
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'}}
141 
142   // Local extern redeclaration does not drop the attribute.
143   extern float LocalRedecl4;
144 }
145 
146 
147 
148 //===----------------------------------------------------------------------===//
149 // Functions
150 //===----------------------------------------------------------------------===//
151 
152 // Import function declaration. Check different placements.
153 __attribute__((dllimport)) void decl1A(void); // Basic check with __attribute__
154 __declspec(dllimport)      void decl1B(void);
155 
156 void __attribute__((dllimport)) decl2A(void);
157 void __declspec(dllimport)      decl2B(void);
158 
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;
163 
164 // Not allowed on function definitions.
def(void)165 __declspec(dllimport) void def(void) {} // expected-error{{dllimport cannot be applied to non-inline function definition}}
166 
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
inlineFunc1(void)172 __declspec(dllimport) inline void inlineFunc1(void) {}
inlineFunc2(void)173 inline void __attribute__((dllimport)) inlineFunc2(void) {}
174 
175 // Redeclarations
176 __declspec(dllimport) void redecl1(void);
177 __declspec(dllimport) void redecl1(void);
178 
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}}
181 
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
redecl3(void)192                       void redecl3(void) {}
193 
194                       void redecl4(void); // expected-note{{previous declaration is here}}
useRedecl4(void)195 void useRedecl4(void) { redecl4(); }
196 __declspec(dllimport) void redecl4(void); // expected-warning{{redeclaration of 'redecl4' should not add 'dllimport' attribute}}
197 
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}}
201 
202 
203 // Inline redeclarations.
204 #ifdef GNU
205 // expected-warning@+3{{'redecl6' redeclared inline; 'dllimport' attribute ignored}}
206 #endif
207 __declspec(dllimport) void redecl6(void);
redecl6(void)208                       inline void redecl6(void) {}
209 
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);
redecl7(void)217 __declspec(dllimport) inline void redecl7(void) {}
218 
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'}}
redecl8(unknowntype X)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}}
redecl9(void)226 int redecl9(void) {} // expected-error{{conflicting types for 'redecl9'}}
227 
228 // External linkage is required.
229 __declspec(dllimport) static int staticFunc(void); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}
230 
231 // Static locals don't count as having external linkage.
staticLocalFunc(void)232 void staticLocalFunc(void) {
233   __declspec(dllimport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllimport'}}
234 }
235