1// 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 23// dllimport implies a declaration. 24__declspec(dllimport) int GlobalDecl; 25int **__attribute__((dllimport))* GlobalDeclChunkAttr; 26int GlobalDeclAttr __attribute__((dllimport)); 27 28// 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 31// Not allowed on definitions. 32__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}} 35 36// Declare, then reject definition. 37__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}} 39 40__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}} 42 43int *__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}} 45 46int 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}} 48 49// Redeclarations 50__declspec(dllimport) extern int GlobalRedecl1; 51__declspec(dllimport) extern int GlobalRedecl1; 52 53__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 62// 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 70// External linkage is required. 71__declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}} 72 73// Import in local scope. 74__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}} 77void functionScope() { 78 __declspec(dllimport) int LocalRedecl1; // expected-error{{redefinition of 'LocalRedecl1' with a different type: 'int' vs 'float'}} 79 int *__attribute__((dllimport)) LocalRedecl2; // expected-error{{redefinition of 'LocalRedecl2' with a different type: 'int *' vs 'float'}} 80 int LocalRedecl3 __attribute__((dllimport)); // expected-error{{redefinition of 'LocalRedecl3' with a different type: 'int' vs 'float'}} 81 82 __declspec(dllimport) int LocalVarDecl; 83 __declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}} 84 __declspec(dllimport) extern int ExternLocalVarDecl; 85 __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}} 86 __declspec(dllimport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllimport'}} 87} 88 89 90 91//===----------------------------------------------------------------------===// 92// Functions 93//===----------------------------------------------------------------------===// 94 95// Import function declaration. Check different placements. 96__attribute__((dllimport)) void decl1A(); // Sanity check with __attribute__ 97__declspec(dllimport) void decl1B(); 98 99void __attribute__((dllimport)) decl2A(); 100void __declspec(dllimport) decl2B(); 101 102// Address of functions can be used for initialization in C language modes. 103// However, the address of the thunk wrapping the function is used instead of 104// the address in the import address table. 105void (*FunForInit)() = &decl2A; 106 107// Not allowed on function definitions. 108__declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} 109 110// Import inline function. 111__declspec(dllimport) inline void inlineFunc1() {} 112inline void __attribute__((dllimport)) inlineFunc2() {} 113 114// Redeclarations 115__declspec(dllimport) void redecl1(); 116__declspec(dllimport) void redecl1(); 117 118// NB: MSVC issues a warning and makes redecl2/redecl3 dllexport. We follow GCC 119// and drop the dllimport with a warning. 120__declspec(dllimport) void redecl2(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} 121 void redecl2(); // expected-warning{{'redecl2' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} 122 123__declspec(dllimport) void redecl3(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} 124 void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} 125 126 void redecl4(); // expected-note{{previous declaration is here}} 127__declspec(dllimport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllimport' attribute}} 128 129// Inline redeclarations are fine. 130__declspec(dllimport) void redecl5(); 131 inline void redecl5() {} 132 133 void redecl6(); // expected-note{{previous declaration is here}} 134__declspec(dllimport) inline void redecl6() {} // expected-error{{redeclaration of 'redecl6' cannot add 'dllimport' attribute}} 135 136// External linkage is required. 137__declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}} 138