Home | History | Annotate | Download | only in Sema
      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 
      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 
     23 // dllimport implies a declaration.
     24 __declspec(dllimport) int GlobalDecl;
     25 int **__attribute__((dllimport))* GlobalDeclChunkAttr;
     26 int GlobalDeclAttr __attribute__((dllimport));
     27 
     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 
     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}}
     34 int __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}}
     38 int 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}}
     41 int GlobalDeclInit = 1; // expected-warning{{'GlobalDeclInit' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
     42 
     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}}
     45 
     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}}
     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 
     56 int *__attribute__((dllimport)) GlobalRedecl2b;
     57 int *__attribute__((dllimport)) GlobalRedecl2b;
     58 
     59 int GlobalRedecl2c __attribute__((dllimport));
     60 int 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 // Adding an attribute on redeclaration.
     68                       extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
     69 int useGlobalRedecl4() { return GlobalRedecl4; }
     70 __declspec(dllimport) extern int GlobalRedecl4; // expected-error{{redeclaration of 'GlobalRedecl4' cannot add 'dllimport' attribute}}
     71 
     72 // Allow with a warning if the decl hasn't been used yet.
     73                       extern int GlobalRedecl5; // expected-note{{previous declaration is here}}
     74 __declspec(dllimport) extern int GlobalRedecl5; // expected-warning{{redeclaration of 'GlobalRedecl5' should not add 'dllimport' attribute}}
     75 
     76 
     77 // External linkage is required.
     78 __declspec(dllimport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllimport'}}
     79 
     80 // Thread local variables are invalid.
     81 __declspec(dllimport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllimport'}}
     82 
     83 // Import in local scope.
     84 __declspec(dllimport) float LocalRedecl1; // expected-note{{previous declaration is here}}
     85 __declspec(dllimport) float LocalRedecl2; // expected-note{{previous declaration is here}}
     86 __declspec(dllimport) float LocalRedecl3; // expected-note{{previous declaration is here}}
     87 __declspec(dllimport) float LocalRedecl4;
     88 void functionScope() {
     89   __declspec(dllimport) int LocalRedecl1; // expected-error{{redeclaration of 'LocalRedecl1' with a different type: 'int' vs 'float'}}
     90   int *__attribute__((dllimport)) LocalRedecl2; // expected-error{{redeclaration of 'LocalRedecl2' with a different type: 'int *' vs 'float'}}
     91   int LocalRedecl3 __attribute__((dllimport)); // expected-error{{redeclaration of 'LocalRedecl3' with a different type: 'int' vs 'float'}}
     92 
     93   __declspec(dllimport)        int LocalVarDecl;
     94   __declspec(dllimport)        int LocalVarDef = 1; // expected-error{{definition of dllimport data}}
     95   __declspec(dllimport) extern int ExternLocalVarDecl;
     96   __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}}
     97   __declspec(dllimport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllimport'}}
     98 
     99   // Local extern redeclaration does not drop the attribute.
    100   extern float LocalRedecl4;
    101 }
    102 
    103 
    104 
    105 //===----------------------------------------------------------------------===//
    106 // Functions
    107 //===----------------------------------------------------------------------===//
    108 
    109 // Import function declaration. Check different placements.
    110 __attribute__((dllimport)) void decl1A(); // Sanity check with __attribute__
    111 __declspec(dllimport)      void decl1B();
    112 
    113 void __attribute__((dllimport)) decl2A();
    114 void __declspec(dllimport)      decl2B();
    115 
    116 // Address of functions can be used for initialization in C language modes.
    117 // However, the address of the thunk wrapping the function is used instead of
    118 // the address in the import address table.
    119 void (*FunForInit)() = &decl2A;
    120 
    121 // Not allowed on function definitions.
    122 __declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}}
    123 
    124 // Import inline function.
    125 #ifdef GNU
    126 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
    127 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
    128 #endif
    129 __declspec(dllimport) inline void inlineFunc1() {}
    130 inline void __attribute__((dllimport)) inlineFunc2() {}
    131 
    132 // Redeclarations
    133 __declspec(dllimport) void redecl1();
    134 __declspec(dllimport) void redecl1();
    135 
    136 // NB: MSVC issues a warning and makes redecl2/redecl3 dllexport. We follow GCC
    137 // and drop the dllimport with a warning.
    138 __declspec(dllimport) void redecl2(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
    139                       void redecl2(); // expected-warning{{'redecl2' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
    140 
    141 __declspec(dllimport) void redecl3(); // expected-note{{previous declaration is here}} expected-note{{previous attribute is here}}
    142                       void redecl3() {} // expected-warning{{'redecl3' redeclared without 'dllimport' attribute: previous 'dllimport' ignored}}
    143 
    144                       void redecl4(); // expected-note{{previous declaration is here}}
    145 void useRedecl4() { redecl4(); }
    146 __declspec(dllimport) void redecl4(); // expected-warning{{redeclaration of 'redecl4' should not add 'dllimport' attribute}}
    147 
    148 // Allow with a warning if the decl hasn't been used yet.
    149                       void redecl5(); // expected-note{{previous declaration is here}}
    150 __declspec(dllimport) void redecl5(); // expected-warning{{redeclaration of 'redecl5' should not add 'dllimport' attribute}}
    151 
    152 
    153 // Inline redeclarations.
    154 #ifdef GNU
    155 // expected-warning@+3{{'redecl6' redeclared inline; 'dllimport' attribute ignored}}
    156 #endif
    157 __declspec(dllimport) void redecl6();
    158                       inline void redecl6() {}
    159 
    160 #ifdef MS
    161 // expected-note@+5{{previous declaration is here}}
    162 // expected-warning@+5{{redeclaration of 'redecl7' should not add 'dllimport' attribute}}
    163 #else
    164 // expected-warning@+3{{'dllimport' attribute ignored on inline function}}
    165 #endif
    166                       void redecl7();
    167 __declspec(dllimport) inline void redecl7() {}
    168 
    169 // External linkage is required.
    170 __declspec(dllimport) static int staticFunc(); // expected-error{{'staticFunc' must have external linkage when declared 'dllimport'}}
    171 
    172 // Static locals don't count as having external linkage.
    173 void staticLocalFunc() {
    174   __declspec(dllimport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllimport'}}
    175 }
    176