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
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}}
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 // Import declaration.
27 __declspec(dllimport) extern int ExternGlobalDecl;
29 // dllimport implies a declaration.
30 __declspec(dllimport) int GlobalDecl;
31 int **__attribute__((dllimport))* GlobalDeclChunkAttr;
32 int GlobalDeclAttr __attribute__((dllimport));
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}}
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}}
42 // Declare, then reject definition.
44 // expected-note@+2{{previous attribute is here}}
46 __declspec(dllimport) extern int ExternGlobalDeclInit; // expected-note{{previous declaration is here}}
48 // expected-warning@+4{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
50 // expected-warning@+2{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
52 int ExternGlobalDeclInit = 1;
55 // expected-note@+2{{previous attribute is here}}
57 __declspec(dllimport) int GlobalDeclInit; // expected-note{{previous declaration is here}}
59 // expected-warning@+4{{'GlobalDeclInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
61 // expected-warning@+2{{'GlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
63 int GlobalDeclInit = 1;
66 // expected-note@+2{{previous attribute is here}}
68 int *__attribute__((dllimport)) GlobalDeclChunkAttrInit; // expected-note{{previous declaration is here}}
70 // expected-warning@+4{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
72 // expected-warning@+2{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
74 int *GlobalDeclChunkAttrInit = 0;
77 // expected-note@+2{{previous attribute is here}}
79 int GlobalDeclAttrInit __attribute__((dllimport)); // expected-note{{previous declaration is here}}
81 // expected-warning@+4{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
83 // expected-warning@+2{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
85 int GlobalDeclAttrInit = 1;
88 __declspec(dllimport) extern int GlobalRedecl1;
89 __declspec(dllimport) extern int GlobalRedecl1;
91 __declspec(dllimport) int GlobalRedecl2a;
92 __declspec(dllimport) int GlobalRedecl2a;
94 int *__attribute__((dllimport)) GlobalRedecl2b;
95 int *__attribute__((dllimport)) GlobalRedecl2b;
97 int GlobalRedecl2c __attribute__((dllimport));
98 int GlobalRedecl2c __attribute__((dllimport));
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}}
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}}
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}}
114 // External linkage is required.
115 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
117 // Thread local variables are invalid.
118 __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}}
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'}}
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'}}
136 // Local extern redeclaration does not drop the attribute.
137 extern float LocalRedecl4;
142 //===----------------------------------------------------------------------===//
144 //===----------------------------------------------------------------------===//
146 // Import function declaration. Check different placements.
147 __attribute__((dllimport)) void decl1A(); // Sanity check with __attribute__
148 __declspec(dllimport) void decl1B();
150 void __attribute__((dllimport)) decl2A();
151 void __declspec(dllimport) decl2B();
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;
158 // Not allowed on function definitions.
159 __declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}}
161 // Import inline function.
163 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
164 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
166 __declspec(dllimport) inline void inlineFunc1() {}
167 inline void __attribute__((dllimport)) inlineFunc2() {}
170 __declspec(dllimport) void redecl1();
171 __declspec(dllimport) void redecl1();
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}}
177 // expected-note@+2{{previous attribute is here}}
179 __declspec(dllimport) void redecl3(); // expected-note{{previous declaration is here}}
180 // NB: Both MSVC and Clang issue a warning and make redecl3 dllexport.
182 // expected-warning@+4{{'redecl3' redeclared without 'dllimport' attribute: 'dllexport' attribute added}}
184 // expected-warning@+2{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
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}}
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}}
197 // Inline redeclarations.
199 // expected-warning@+3{{'redecl6' redeclared inline; 'dllimport' attribute ignored}}
201 __declspec(dllimport) void redecl6();
202 inline void redecl6() {}
205 // expected-note@+5{{previous declaration is here}}
206 // expected-warning@+5{{redeclaration of 'redecl7' should not add 'dllimport' attribute}}
208 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
211 __declspec(dllimport) inline void redecl7() {}
213 // External linkage is required.
214 __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}
216 // Static locals don't count as having external linkage.
217 void staticLocalFunc() {
218 __declspec(dllimport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllimport'}}