Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 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}} |
| 8 | typedef __declspec(dllimport) int typedef2; // expected-warning{{'dllimport' attribute only applies to variables and functions}} |
| 9 | typedef int __declspec(dllimport) typedef3; // expected-warning{{'dllimport' attribute only applies to variables and functions}} |
| 10 | typedef __declspec(dllimport) void (*FunTy)(); // expected-warning{{'dllimport' attribute only applies to variables and functions}} |
| 11 | enum __declspec(dllimport) Enum { EnumVal }; // expected-warning{{'dllimport' attribute only applies to variables and functions}} |
| 12 | struct __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 Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 23 | // dllimport implies a declaration. |
| 24 | __declspec(dllimport) int GlobalDecl; |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 25 | int **__attribute__((dllimport))* GlobalDeclChunkAttr; |
| 26 | int GlobalDeclAttr __attribute__((dllimport)); |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 27 | |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame^] | 28 | // Address of variables can't be used for initialization in C language modes. |
| 29 | int *VarForInit = &GlobalDecl; // expected-error{{initializer element is not a compile-time constant}} |
| 30 | |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 31 | // Not allowed on definitions. |
Nico Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 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}} |
| 34 | int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dllimport data}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 35 | |
| 36 | // Declare, then reject definition. |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 37 | __declspec(dllimport) extern int ExternGlobalDeclInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} |
| 38 | int ExternGlobalDeclInit = 1; // expected-warning{{'ExternGlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 39 | |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 40 | __declspec(dllimport) int GlobalDeclInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} |
| 41 | int GlobalDeclInit = 1; // expected-warning{{'GlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 42 | |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 43 | int *__attribute__((dllimport)) GlobalDeclChunkAttrInit; // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} |
| 44 | int *GlobalDeclChunkAttrInit = 0; // expected-warning{{'GlobalDeclChunkAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 45 | |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 46 | int GlobalDeclAttrInit __attribute__((dllimport)); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}} |
| 47 | int GlobalDeclAttrInit = 1; // expected-warning{{'GlobalDeclAttrInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 48 | |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 49 | // Redeclarations |
| 50 | __declspec(dllimport) extern int GlobalRedecl1; |
| 51 | __declspec(dllimport) extern int GlobalRedecl1; |
| 52 | |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 53 | __declspec(dllimport) int GlobalRedecl2a; |
| 54 | __declspec(dllimport) int GlobalRedecl2a; |
| 55 | |
| 56 | int *__attribute__((dllimport)) GlobalRedecl2b; |
| 57 | int *__attribute__((dllimport)) GlobalRedecl2b; |
| 58 | |
| 59 | int GlobalRedecl2c __attribute__((dllimport)); |
| 60 | int GlobalRedecl2c __attribute__((dllimport)); |
| 61 | |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 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 | |
Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 70 | // External linkage is required. |
| 71 | __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}} |
| 72 | |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 73 | // Import in local scope. |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 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}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 77 | void functionScope() { |
Nico Rieck | e84f8db | 2014-03-23 21:24:01 +0000 | [diff] [blame] | 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 | |
Nico Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 82 | __declspec(dllimport) int LocalVarDecl; |
| 83 | __declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 84 | __declspec(dllimport) extern int ExternLocalVarDecl; |
Nico Rieck | 8e9791f | 2014-02-26 21:27:13 +0000 | [diff] [blame] | 85 | __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}} |
Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 86 | __declspec(dllimport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllimport'}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 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 | |
| 99 | void __attribute__((dllimport)) decl2A(); |
| 100 | void __declspec(dllimport) decl2B(); |
| 101 | |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame^] | 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. |
| 105 | void (*FunForInit)() = &decl2A; |
| 106 | |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 107 | // Not allowed on function definitions. |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 108 | __declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 109 | |
| 110 | // Import inline function. |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 111 | __declspec(dllimport) inline void inlineFunc1() {} |
| 112 | inline void __attribute__((dllimport)) inlineFunc2() {} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 113 | |
| 114 | // Redeclarations |
| 115 | __declspec(dllimport) void redecl1(); |
| 116 | __declspec(dllimport) void redecl1(); |
| 117 | |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 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}} |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 124 | void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}} |
Nico Rieck | 82f0b06 | 2014-03-31 14:56:15 +0000 | [diff] [blame] | 125 | |
| 126 | void redecl4(); // expected-note{{previous declaration is here}} |
| 127 | __declspec(dllimport) void redecl4(); // expected-error{{redeclaration of 'redecl4' cannot add 'dllimport' attribute}} |
Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 128 | |
Hans Wennborg | f436b28 | 2014-05-22 15:46:15 +0000 | [diff] [blame] | 129 | // Inline redeclarations are fine. |
| 130 | __declspec(dllimport) void redecl5(); |
Nico Rieck | ffd8a33 | 2014-05-23 19:07:49 +0000 | [diff] [blame] | 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}} |
Hans Wennborg | f436b28 | 2014-05-22 15:46:15 +0000 | [diff] [blame] | 135 | |
Nico Rieck | 8ca0bfc | 2014-03-31 14:56:58 +0000 | [diff] [blame] | 136 | // External linkage is required. |
| 137 | __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}} |