From: Nico Rieck Date: Wed, 26 Feb 2014 21:27:13 +0000 (+0000) Subject: Sema: Definition of dllimport globals is not allowed X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b57e967568a258ac00884522fe439fbbbcfb2d20;p=clang Sema: Definition of dllimport globals is not allowed Upgrades the warning to an error and clarifies the message by treating the definition as error instead of the attribute. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202300 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 229e2bbb5a..eb2bc4f1f7 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2055,6 +2055,8 @@ def err_attribute_selectany_non_extern_data : Error< def warn_attribute_invalid_on_definition : Warning< "'%0' attribute cannot be specified on a definition">, InGroup; +def err_attribute_dllimport_data_definition : Error< + "definition of dllimport data">; def err_attribute_weakref_not_static : Error< "weakref declaration must have internal linkage">; def err_attribute_weakref_not_global_context : Error< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 266f66844e..601c16109f 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -8123,6 +8123,13 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, return; } + // dllimport cannot be used on variable definitions. + if (VDecl->hasAttr() && !VDecl->isStaticDataMember()) { + Diag(VDecl->getLocation(), diag::err_attribute_dllimport_data_definition); + VDecl->setInvalidDecl(); + return; + } + if (VDecl->isLocalVarDecl() && VDecl->hasExternalStorage()) { // C99 6.7.8p5. C++ has no such restriction, but that is a defect. Diag(VDecl->getLocation(), diag::err_block_extern_cant_init); diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index d61d63a9b9..2600a4fcb0 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -3793,15 +3793,6 @@ DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, if (D->hasAttr()) return NULL; - if (VarDecl *VD = dyn_cast(D)) { - if (VD->hasDefinition()) { - // dllimport cannot be applied to definitions. - Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) - << "dllimport"; - return NULL; - } - } - return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); } diff --git a/test/Sema/dllexport.c b/test/Sema/dllexport.c index fb209e1aa6..eb39fbe9bf 100644 --- a/test/Sema/dllexport.c +++ b/test/Sema/dllexport.c @@ -85,8 +85,8 @@ __declspec(dllexport) __declspec(dllimport) extern int PrecedenceExternGlobal2B; __attribute__((dllimport, dllexport)) int PrecedenceGlobal1A; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllimport) __declspec(dllexport) int PrecedenceGlobal1B; // expected-warning{{'dllimport' attribute ignored}} -__attribute__((dllexport, dllimport)) int PrecedenceGlobal2A; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} -__declspec(dllexport) __declspec(dllimport) int PrecedenceGlobal2B; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__attribute__((dllexport, dllimport)) int PrecedenceGlobal2A; // expected-warning{{'dllimport' attribute ignored}} +__declspec(dllexport) __declspec(dllimport) int PrecedenceGlobal2B; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllexport) extern int PrecedenceExternGlobalRedecl1; __declspec(dllimport) extern int PrecedenceExternGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}} @@ -95,7 +95,7 @@ __declspec(dllimport) extern int PrecedenceExternGlobalRedecl2; // expected-warn __declspec(dllexport) extern int PrecedenceExternGlobalRedecl2; __declspec(dllexport) extern int PrecedenceGlobalRedecl1; -__declspec(dllimport) int PrecedenceGlobalRedecl1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__declspec(dllimport) int PrecedenceGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllimport) extern int PrecedenceGlobalRedecl2; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllexport) int PrecedenceGlobalRedecl2; diff --git a/test/Sema/dllimport.c b/test/Sema/dllimport.c index ee1ad882b6..615f8f77b4 100644 --- a/test/Sema/dllimport.c +++ b/test/Sema/dllimport.c @@ -20,16 +20,17 @@ struct __declspec(dllimport) Record {}; // expected-warning{{'dllimport' attribu // Import declaration. __declspec(dllimport) extern int ExternGlobalDecl; -// dllimport implies a declaration. FIXME: This should not warn. -__declspec(dllimport) int GlobalDecl; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +// dllimport implies a declaration. +__declspec(dllimport) int GlobalDecl; // Not allowed on definitions. -__declspec(dllimport) int GlobalInit1 = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} -int __declspec(dllimport) GlobalInit2 = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__declspec(dllimport) extern int ExternGlobalInit = 1; // expected-error{{definition of dllimport data}} +__declspec(dllimport) int GlobalInit1 = 1; // expected-error{{definition of dllimport data}} +int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dllimport data}} // Declare, then reject definition. __declspec(dllimport) extern int ExternGlobalDeclInit; -int ExternGlobalDeclInit = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +int ExternGlobalDeclInit = 1; // expected-error{{definition of dllimport data}} // Redeclarations __declspec(dllimport) extern int GlobalRedecl1; @@ -37,7 +38,10 @@ __declspec(dllimport) extern int GlobalRedecl1; // Import in local scope. void functionScope() { + __declspec(dllimport) int LocalVarDecl; + __declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}} __declspec(dllimport) extern int ExternLocalVarDecl; + __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}} } diff --git a/test/SemaCXX/dllexport.cpp b/test/SemaCXX/dllexport.cpp index 114ebd02bb..6476383495 100644 --- a/test/SemaCXX/dllexport.cpp +++ b/test/SemaCXX/dllexport.cpp @@ -161,8 +161,8 @@ __declspec(dllexport) __declspec(dllimport) extern int PrecedenceExternGlobal2B; __attribute__((dllimport, dllexport)) int PrecedenceGlobal1A; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllimport) __declspec(dllexport) int PrecedenceGlobal1B; // expected-warning{{'dllimport' attribute ignored}} -__attribute__((dllexport, dllimport)) int PrecedenceGlobal2A; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} -__declspec(dllexport) __declspec(dllimport) int PrecedenceGlobal2B; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__attribute__((dllexport, dllimport)) int PrecedenceGlobal2A; // expected-warning{{'dllimport' attribute ignored}} +__declspec(dllexport) __declspec(dllimport) int PrecedenceGlobal2B; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllexport) extern int PrecedenceExternGlobalRedecl1; __declspec(dllimport) extern int PrecedenceExternGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}} @@ -171,7 +171,7 @@ __declspec(dllimport) extern int PrecedenceExternGlobalRedecl2; // expected-warn __declspec(dllexport) extern int PrecedenceExternGlobalRedecl2; __declspec(dllexport) extern int PrecedenceGlobalRedecl1; -__declspec(dllimport) int PrecedenceGlobalRedecl1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__declspec(dllimport) int PrecedenceGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllimport) extern int PrecedenceGlobalRedecl2; // expected-warning{{'dllimport' attribute ignored}} __declspec(dllexport) int PrecedenceGlobalRedecl2; diff --git a/test/SemaCXX/dllimport.cpp b/test/SemaCXX/dllimport.cpp index d04a4b990a..4a3ee202ad 100644 --- a/test/SemaCXX/dllimport.cpp +++ b/test/SemaCXX/dllimport.cpp @@ -32,16 +32,17 @@ enum __declspec(dllimport) Enum {}; // expected-warning{{'dllimport' attribute o // Import declaration. __declspec(dllimport) extern int ExternGlobalDecl; -// dllimport implies a declaration. FIXME: This should not warn. -__declspec(dllimport) int GlobalDecl; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +// dllimport implies a declaration. +__declspec(dllimport) int GlobalDecl; // Not allowed on definitions. -__declspec(dllimport) int GlobalInit1 = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} -int __declspec(dllimport) GlobalInit2 = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +__declspec(dllimport) extern int ExternGlobalInit = 1; // expected-error{{definition of dllimport data}} +__declspec(dllimport) int GlobalInit1 = 1; // expected-error{{definition of dllimport data}} +int __declspec(dllimport) GlobalInit2 = 1; // expected-error{{definition of dllimport data}} // Declare, then reject definition. __declspec(dllimport) extern int ExternGlobalDeclInit; -int ExternGlobalDeclInit = 1; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} +int ExternGlobalDeclInit = 1; // expected-error{{definition of dllimport data}} // Redeclarations __declspec(dllimport) extern int GlobalRedecl1; @@ -49,7 +50,10 @@ __declspec(dllimport) extern int GlobalRedecl1; // Import in local scope. void functionScope() { + __declspec(dllimport) int LocalVarDecl; + __declspec(dllimport) int LocalVarDef = 1; // expected-error{{definition of dllimport data}} __declspec(dllimport) extern int ExternLocalVarDecl; + __declspec(dllimport) extern int ExternLocalVarDef = 1; // expected-error{{definition of dllimport data}} }