From beba3e80378f38f3d1e1ef030dacdd438373d3d5 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Mon, 20 May 2013 21:53:29 +0000 Subject: [PATCH] Warn on and drop dllimport attrs from variable definitions AsmPrinter::EmitLinkage() does not handle dllimport linkage. The LLVM verifier should also be fixed to reject this. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182320 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++-- lib/Sema/SemaDeclAttr.cpp | 5 ++--- lib/Sema/TargetAttributesSema.cpp | 9 +++++++++ test/Sema/dllimport-dllexport.c | 5 +++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index f1a179c259..f52d1f3162 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1957,8 +1957,8 @@ def err_attribute_weak_static : Error< "weak declaration cannot have internal linkage">; def err_attribute_selectany_non_extern_data : Error< "'selectany' can only be applied to data items with external linkage">; -def warn_attribute_weak_import_invalid_on_definition : Warning< - "'weak_import' attribute cannot be specified on a definition">, +def warn_attribute_invalid_on_definition : Warning< + "'%0' attribute cannot be specified on a definition">, InGroup; def err_attribute_weakref_not_static : Error< "weakref declaration must have internal linkage">; diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 627bc3d364..be4bd0e3b7 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -2725,9 +2725,8 @@ static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { bool isDef = false; if (!D->canBeWeakImported(isDef)) { if (isDef) - S.Diag(Attr.getLoc(), - diag::warn_attribute_weak_import_invalid_on_definition) - << "weak_import" << 2 /*variable and function*/; + S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) + << "weak_import"; else if (isa(D) || isa(D) || (S.Context.getTargetInfo().getTriple().isOSDarwin() && (isa(D) || isa(D)))) { diff --git a/lib/Sema/TargetAttributesSema.cpp b/lib/Sema/TargetAttributesSema.cpp index 2f7701227d..526399a278 100644 --- a/lib/Sema/TargetAttributesSema.cpp +++ b/lib/Sema/TargetAttributesSema.cpp @@ -161,6 +161,15 @@ 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/dllimport-dllexport.c b/test/Sema/dllimport-dllexport.c index 00c9df594b..80810d696e 100644 --- a/test/Sema/dllimport-dllexport.c +++ b/test/Sema/dllimport-dllexport.c @@ -41,3 +41,8 @@ void __attribute__((dllexport)) foo13(); extern int foo14 __attribute__((dllexport)); extern int foo14 __attribute__((dllimport)); // expected-warning{{dllimport attribute ignored}} + +__declspec(dllimport) int foo15 = 54; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} + +extern __declspec(dllimport) int foo17; +int foo17 = 54; // expected-warning{{'dllimport' attribute cannot be specified on a definition}} -- 2.40.0