blob: 30ea4f3089c70138692ae8bfaa72015368433eed [file] [log] [blame]
Nico Rieck60478662014-02-22 19:47:30 +00001// RUN: %clang_cc1 -triple i686-win32 -fsyntax-only -verify -std=c99 %s
2// RUN: %clang_cc1 -triple x86_64-win32 -fsyntax-only -verify -std=c11 %s
3// RUN: %clang_cc1 -triple i686-mingw32 -fsyntax-only -verify -std=c11 %s
4// RUN: %clang_cc1 -triple x86_64-mingw32 -fsyntax-only -verify -std=c99 %s
5
6// Invalid usage.
7__declspec(dllimport) typedef int typedef1; // expected-warning{{'dllimport' attribute only applies to variables and functions}}
8typedef __declspec(dllimport) int typedef2; // expected-warning{{'dllimport' attribute only applies to variables and functions}}
9typedef int __declspec(dllimport) typedef3; // expected-warning{{'dllimport' attribute only applies to variables and functions}}
10typedef __declspec(dllimport) void (*FunTy)(); // expected-warning{{'dllimport' attribute only applies to variables and functions}}
11enum __declspec(dllimport) Enum { EnumVal }; // expected-warning{{'dllimport' attribute only applies to variables and functions}}
12struct __declspec(dllimport) Record {}; // expected-warning{{'dllimport' attribute only applies to variables and functions}}
13
14
15
16//===----------------------------------------------------------------------===//
17// Globals
18//===----------------------------------------------------------------------===//
19
20// Import declaration.
21__declspec(dllimport) extern int ExternGlobalDecl;
22
Nico Rieck8e9791f2014-02-26 21:27:13 +000023// dllimport implies a declaration.
24__declspec(dllimport) int GlobalDecl;
Nico Riecke84f8db2014-03-23 21:24:01 +000025int **__attribute__((dllimport))* GlobalDeclChunkAttr;
26int GlobalDeclAttr __attribute__((dllimport));
Nico Rieck60478662014-02-22 19:47:30 +000027
David Majnemer0c43d802014-06-25 08:15:07 +000028// Address of variables can't be used for initialization in C language modes.
29int *VarForInit = &GlobalDecl; // expected-error{{initializer element is not a compile-time constant}}
30
Nico Rieck60478662014-02-22 19:47:30 +000031// Not allowed on definitions.
Nico Rieck8e9791f2014-02-26 21:27:13 +000032__declspec(dllimport) extern int ExternGlobalInit = 1; // expected-error{{definition of dllimport data}}
33__declspec(dllimport) int GlobalInit1 = 1; // expected-error{{definition of dllimport data}}
34int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dllimport data}}
Nico Rieck60478662014-02-22 19:47:30 +000035
36// Declare, then reject definition.
Nico Rieck82f0b062014-03-31 14:56:15 +000037__declspec(dllimport) extern int ExternGlobalDeclInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
38int ExternGlobalDeclInit = 1; // expected-warning{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
Nico Rieck60478662014-02-22 19:47:30 +000039
Nico Rieck82f0b062014-03-31 14:56:15 +000040__declspec(dllimport) int GlobalDeclInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
41int GlobalDeclInit = 1; // expected-warning{{'GlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
Nico Riecke84f8db2014-03-23 21:24:01 +000042
Nico Rieck82f0b062014-03-31 14:56:15 +000043int *__attribute__((dllimport)) GlobalDeclChunkAttrInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
44int *GlobalDeclChunkAttrInit = 0; // expected-warning{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
Nico Riecke84f8db2014-03-23 21:24:01 +000045
Nico Rieck82f0b062014-03-31 14:56:15 +000046int GlobalDeclAttrInit __attribute__((dllimport)); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
47int GlobalDeclAttrInit = 1; // expected-warning{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
Nico Riecke84f8db2014-03-23 21:24:01 +000048
Nico Rieck60478662014-02-22 19:47:30 +000049// Redeclarations
50__declspec(dllimport) extern int GlobalRedecl1;
51__declspec(dllimport) extern int GlobalRedecl1;
52
Nico Riecke84f8db2014-03-23 21:24:01 +000053__declspec(dllimport) int GlobalRedecl2a;
54__declspec(dllimport) int GlobalRedecl2a;
55
56int *__attribute__((dllimport)) GlobalRedecl2b;
57int *__attribute__((dllimport)) GlobalRedecl2b;
58
59int GlobalRedecl2c __attribute__((dllimport));
60int GlobalRedecl2c __attribute__((dllimport));
61
Nico Rieck82f0b062014-03-31 14:56:15 +000062// NB: MSVC issues a warning and makes GlobalRedecl3 dllexport. We follow GCC
63// and drop the dllimport with a warning.
64__declspec(dllimport) extern int GlobalRedecl3; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
65 extern int GlobalRedecl3; // expected-warning{{'GlobalRedecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
66
67 extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
68__declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
69
Nico Rieck8ca0bfc2014-03-31 14:56:58 +000070// External linkage is required.
71__declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
72
Nico Rieck60478662014-02-22 19:47:30 +000073// Import in local scope.
Nico Riecke84f8db2014-03-23 21:24:01 +000074__declspec(dllimport) float LocalRedecl1; // expected-note{{previous definition is here}}
75__declspec(dllimport) float LocalRedecl2; // expected-note{{previous definition is here}}
76__declspec(dllimport) float LocalRedecl3; // expected-note{{previous definition is here}}
Hans Wennborgf51dc3b2014-07-31 19:29:39 +000077__declspec(dllimport) float LocalRedecl4;
Nico Rieck60478662014-02-22 19:47:30 +000078void functionScope() {
Nico Riecke84f8db2014-03-23 21:24:01 +000079 __declspec(dllimport) int LocalRedecl1; // expected-error{{redefinition of 'LocalRedecl1' with a different type: 'int' vs 'float'}}
80 int *__attribute__((dllimport)) LocalRedecl2; // expected-error{{redefinition of 'LocalRedecl2' with a different type: 'int *' vs 'float'}}
81 int LocalRedecl3 __attribute__((dllimport)); // expected-error{{redefinition of 'LocalRedecl3' with a different type: 'int' vs 'float'}}
82
Nico Rieck8e9791f2014-02-26 21:27:13 +000083 __declspec(dllimport) int LocalVarDecl;
84 __declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}}
Nico Rieck60478662014-02-22 19:47:30 +000085 __declspec(dllimport) extern int ExternLocalVarDecl;
Nico Rieck8e9791f2014-02-26 21:27:13 +000086 __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}}
Nico Rieck8ca0bfc2014-03-31 14:56:58 +000087 __declspec(dllimport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllimport'}}
Hans Wennborgf51dc3b2014-07-31 19:29:39 +000088
89 // Local extern redeclaration does not drop the attribute.
90 extern float LocalRedecl4;
Nico Rieck60478662014-02-22 19:47:30 +000091}
92
93
94
95//===----------------------------------------------------------------------===//
96// Functions
97//===----------------------------------------------------------------------===//
98
99// Import function declaration. Check different placements.
100__attribute__((dllimport)) void decl1A(); // Sanity check with __attribute__
101__declspec(dllimport) void decl1B();
102
103void __attribute__((dllimport)) decl2A();
104void __declspec(dllimport) decl2B();
105
David Majnemer0c43d802014-06-25 08:15:07 +0000106// Address of functions can be used for initialization in C language modes.
107// However, the address of the thunk wrapping the function is used instead of
108// the address in the import address table.
109void (*FunForInit)() = &decl2A;
110
Nico Rieck60478662014-02-22 19:47:30 +0000111// Not allowed on function definitions.
Hans Wennborgb0f2f142014-05-15 22:07:49 +0000112__declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}}
Nico Rieck60478662014-02-22 19:47:30 +0000113
114// Import inline function.
Hans Wennborgb0f2f142014-05-15 22:07:49 +0000115__declspec(dllimport) inline void inlineFunc1() {}
116inline void __attribute__((dllimport)) inlineFunc2() {}
Nico Rieck60478662014-02-22 19:47:30 +0000117
118// Redeclarations
119__declspec(dllimport) void redecl1();
120__declspec(dllimport) void redecl1();
121
Nico Rieck82f0b062014-03-31 14:56:15 +0000122// NB: MSVC issues a warning and makes redecl2/redecl3 dllexport. We follow GCC
123// and drop the dllimport with a warning.
124__declspec(dllimport) void redecl2(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
125 void redecl2(); // expected-warning{{'redecl2' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
126
127__declspec(dllimport) void redecl3(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
Nico Rieck60478662014-02-22 19:47:30 +0000128 void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
Nico Rieck82f0b062014-03-31 14:56:15 +0000129
130 void redecl4(); // expected-note{{previous declaration is here}}
131__declspec(dllimport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllimport' attribute}}
Nico Rieck8ca0bfc2014-03-31 14:56:58 +0000132
Hans Wennborgf436b282014-05-22 15:46:15 +0000133// Inline redeclarations are fine.
134__declspec(dllimport) void redecl5();
Nico Rieckffd8a332014-05-23 19:07:49 +0000135 inline void redecl5() {}
136
137 void redecl6(); // expected-note{{previous declaration is here}}
138__declspec(dllimport) inline void redecl6() {} // expected-error{{redeclaration of 'redecl6' cannot add 'dllimport' attribute}}
Hans Wennborgf436b282014-05-22 15:46:15 +0000139
Nico Rieck8ca0bfc2014-03-31 14:56:58 +0000140// External linkage is required.
141__declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}