From 61e1f5cc721b67ea42d962024ebd0d323f7a7f93 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Thu, 15 May 2014 22:07:49 +0000 Subject: [PATCH] Allow dllimport/dllexport on inline functions and adjust the linkage. This is a step towards handling these attributes on classes (PR11170). Differential Revision: http://reviews.llvm.org/D3772 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@208925 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/AST/ASTContext.cpp | 38 +++++++++++++++++++--- lib/AST/Decl.cpp | 2 +- lib/CodeGen/CodeGenModule.cpp | 15 ++------- lib/Sema/SemaDecl.cpp | 20 ++++-------- lib/Sema/SemaDeclAttr.cpp | 15 --------- test/CodeGen/dllexport.c | 8 +++++ test/CodeGenCXX/dllexport.cpp | 25 ++++++++++++++ test/CodeGenCXX/dllimport.cpp | 25 ++++++++++++++ test/Sema/dllexport.c | 4 +-- test/Sema/dllimport.c | 6 ++-- test/SemaCXX/MicrosoftExtensions.cpp | 2 +- test/SemaCXX/dllexport.cpp | 10 +++--- test/SemaCXX/dllimport.cpp | 14 ++++---- 14 files changed, 121 insertions(+), 65 deletions(-) create mode 100644 test/CodeGen/dllexport.c create mode 100644 test/CodeGenCXX/dllexport.cpp create mode 100644 test/CodeGenCXX/dllimport.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 5d5e3849ed..4e434a4063 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2091,6 +2091,8 @@ def warn_attribute_invalid_on_definition : Warning< InGroup; def err_attribute_dll_redeclaration : Error< "redeclaration of %q0 cannot add %q1 attribute">; +def err_attribute_dllimport_function_definition : Error< + "dllimport cannot be applied to non-inline function definition">; def err_attribute_dllimport_data_definition : Error< "definition of dllimport data">; def err_attribute_weakref_not_static : Error< diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index aa74e1a692..2f40616669 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -7742,7 +7742,8 @@ QualType ASTContext::GetBuiltinType(unsigned Id, return getFunctionType(ResType, ArgTypes, EPI); } -GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { +static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, + const FunctionDecl *FD) { if (!FD->isExternallyVisible()) return GVA_Internal; @@ -7773,8 +7774,11 @@ GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { if (!FD->isInlined()) return External; - if ((!getLangOpts().CPlusPlus && !getLangOpts().MSVCCompat) || + if ((!Context.getLangOpts().CPlusPlus && !Context.getLangOpts().MSVCCompat && + !FD->hasAttr()) || FD->hasAttr()) { + // FIXME: This doesn't match gcc's behavior for dllexport inline functions. + // GNU or C99 inline semantics. Determine whether this symbol should be // externally visible. if (FD->isInlineDefinitionExternallyVisible()) @@ -7793,7 +7797,26 @@ GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { return GVA_DiscardableODR; } -GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { +static GVALinkage adjustGVALinkageForDLLAttribute(GVALinkage L, const Decl *D) { + // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx + // dllexport/dllimport on inline functions. + if (D->hasAttr()) { + if (L == GVA_DiscardableODR || L == GVA_StrongODR) + return GVA_AvailableExternally; + } else if (D->hasAttr()) { + if (L == GVA_DiscardableODR) + return GVA_StrongODR; + } + return L; +} + +GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { + return adjustGVALinkageForDLLAttribute(basicGVALinkageForFunction(*this, FD), + FD); +} + +static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, + const VarDecl *VD) { if (!VD->isExternallyVisible()) return GVA_Internal; @@ -7807,7 +7830,7 @@ GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { // enclosing function. if (LexicalContext) StaticLocalLinkage = - GetGVALinkageForFunction(cast(LexicalContext)); + Context.GetGVALinkageForFunction(cast(LexicalContext)); // GVA_StrongODR function linkage is stronger than what we need, // downgrade to GVA_DiscardableODR. @@ -7821,7 +7844,7 @@ GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { // Itanium-specified entry point, which has the normal linkage of the // variable. if (VD->getTLSKind() == VarDecl::TLS_Dynamic && - getTargetInfo().getTriple().isMacOSX()) + Context.getTargetInfo().getTriple().isMacOSX()) return GVA_Internal; switch (VD->getTemplateSpecializationKind()) { @@ -7842,6 +7865,11 @@ GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { llvm_unreachable("Invalid Linkage!"); } +GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { + return adjustGVALinkageForDLLAttribute(basicGVALinkageForVariable(*this, VD), + VD); +} + bool ASTContext::DeclMustBeEmitted(const Decl *D) { if (const VarDecl *VD = dyn_cast(D)) { if (!VD->isFileVarDecl()) diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 49466b2d98..101b164624 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -2572,7 +2572,7 @@ bool FunctionDecl::isMSExternInline() const { assert(isInlined() && "expected to get called on an inlined function!"); const ASTContext &Context = getASTContext(); - if (!Context.getLangOpts().MSVCCompat) + if (!Context.getLangOpts().MSVCCompat && !hasAttr()) return false; for (const FunctionDecl *FD = this; FD; FD = FD->getPreviousDecl()) diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 25399cc195..d9d2eb0fb5 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -1960,15 +1960,6 @@ llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( if (Linkage == GVA_AvailableExternally) return llvm::Function::AvailableExternallyLinkage; - // LinkOnceODRLinkage is insufficient if the symbol is required to exist in - // the symbol table. Promote the linkage to WeakODRLinkage to preserve the - // semantics of LinkOnceODRLinkage while providing visibility in the symbol - // table. - llvm::GlobalValue::LinkageTypes OnceLinkage = - llvm::GlobalValue::LinkOnceODRLinkage; - if (D->hasAttr() || D->hasAttr()) - OnceLinkage = llvm::GlobalVariable::WeakODRLinkage; - // Note that Apple's kernel linker doesn't support symbol // coalescing, so we need to avoid linkonce and weak linkages there. // Normally, this means we just map to internal, but for explicit @@ -1981,7 +1972,7 @@ llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( // merged with other definitions. c) C++ has the ODR, so we know the // definition is dependable. if (Linkage == GVA_DiscardableODR) - return !Context.getLangOpts().AppleKext ? OnceLinkage + return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage : llvm::Function::InternalLinkage; // An explicit instantiation of a template has weak linkage, since @@ -1995,14 +1986,14 @@ llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( // Destructor variants in the Microsoft C++ ABI are always linkonce_odr thunks // emitted on an as-needed basis. if (UseThunkForDtorVariant) - return OnceLinkage; + return llvm::GlobalValue::LinkOnceODRLinkage; // If required by the ABI, give definitions of static data members with inline // initializers at least linkonce_odr linkage. if (getCXXABI().isInlineInitializedStaticDataMemberLinkOnce() && isa(D) && isVarDeclInlineInitializedStaticDataMember(cast(D))) - return OnceLinkage; + return llvm::GlobalValue::LinkOnceODRLinkage; // C++ doesn't have tentative definitions and thus cannot have common // linkage. diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 2f434cedfe..3c19e9632f 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -9764,20 +9764,12 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { if (const FunctionProtoType *FPT = FD->getType()->getAs()) ResolveExceptionSpec(D->getLocation(), FPT); - // Checking attributes of current function definition - // dllimport attribute. - DLLImportAttr *DA = FD->getAttr(); - if (DA && (!FD->hasAttr())) { - // dllimport attribute cannot be directly applied to definition. - // Microsoft accepts dllimport for functions defined within class scope. - if (!DA->isInherited() && - !(LangOpts.MicrosoftExt && FD->getLexicalDeclContext()->isRecord())) { - Diag(FD->getLocation(), - diag::err_attribute_can_be_applied_only_to_symbol_declaration) - << DA; - FD->setInvalidDecl(); - return D; - } + // dllimport cannot be applied to non-inline function definitions. + if (FD->hasAttr() && !FD->isInlined()) { + assert(!FD->hasAttr()); + Diag(FD->getLocation(), diag::err_attribute_dllimport_function_definition); + FD->setInvalidDecl(); + return D; } // We want to attach documentation to original Decl (which might be // a function template). diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 5ca5a8fc26..9caf9189b9 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -3841,13 +3841,6 @@ static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { return; } - // Currently, the dllimport attribute is ignored for inlined functions. - // Warning is emitted. - if (FD && FD->isInlineSpecified()) { - S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); - return; - } - unsigned Index = Attr.getAttributeSpellingListIndex(); DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); if (NewAttr) @@ -3868,14 +3861,6 @@ DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, } static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { - // Currently, the dllexport attribute is ignored for inlined functions, unless - // the -fkeep-inline-functions flag has been used. Warning is emitted. - if (isa(D) && cast(D)->isInlineSpecified()) { - // FIXME: ... unless the -fkeep-inline-functions flag has been used. - S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); - return; - } - unsigned Index = Attr.getAttributeSpellingListIndex(); DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); if (NewAttr) diff --git a/test/CodeGen/dllexport.c b/test/CodeGen/dllexport.c new file mode 100644 index 0000000000..ce5636c172 --- /dev/null +++ b/test/CodeGen/dllexport.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -triple i686-pc-win32 -std=c99 -O2 -disable-llvm-optzns -emit-llvm < %s | FileCheck %s + +#define DLLEXPORT __declspec(dllexport) + +inline void DLLEXPORT f() {} +extern void DLLEXPORT f(); + +// CHECK: define weak_odr dllexport void @f() diff --git a/test/CodeGenCXX/dllexport.cpp b/test/CodeGenCXX/dllexport.cpp new file mode 100644 index 0000000000..fe9806893b --- /dev/null +++ b/test/CodeGenCXX/dllexport.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -triple i686-pc-win32 -x c++ -O2 -disable-llvm-optzns -emit-llvm < %s | FileCheck %s + +#define DLLEXPORT __declspec(dllexport) + +void DLLEXPORT a(); +// CHECK-DAG: declare dllexport void @"\01?a@@YAXXZ"() + +inline void DLLEXPORT b() {} +// CHECK-DAG: define weak_odr dllexport void @"\01?b@@YAXXZ"() + +template void c() {} +template void DLLEXPORT c(); +// CHECK-DAG: define weak_odr dllexport void @"\01??$c@H@@YAXXZ"() + +struct S { + void DLLEXPORT a() {} + // CHECK-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?a@S@@QAEXXZ" +}; + +void user() { + // FIXME: dllexported functions must be emitted even if they're not referenced in this TU. + a(); + b(); + &S::a; +} diff --git a/test/CodeGenCXX/dllimport.cpp b/test/CodeGenCXX/dllimport.cpp new file mode 100644 index 0000000000..d8e58f7190 --- /dev/null +++ b/test/CodeGenCXX/dllimport.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -triple i686-pc-win32 -x c++ -O2 -disable-llvm-optzns -emit-llvm < %s | FileCheck %s + +#define DLLIMPORT __declspec(dllimport) + +void DLLIMPORT a(); +// CHECK-DAG: declare dllimport void @"\01?a@@YAXXZ"() + +inline void DLLIMPORT b() {} +// CHECK-DAG: define available_externally dllimport void @"\01?b@@YAXXZ"() + +template inline void c() {} // FIXME: MSVC accepts this without 'inline' too. +template void DLLIMPORT c(); +// CHECK-DAG: define available_externally dllimport void @"\01??$c@H@@YAXXZ"() + +struct S { + void DLLIMPORT a() {} + // CHECK-DAG: define available_externally dllimport x86_thiscallcc void @"\01?a@S@@QAEXXZ" +}; + +void user(S* s) { + a(); + b(); + c(); + s->a(); +} diff --git a/test/Sema/dllexport.c b/test/Sema/dllexport.c index 8a71e5d725..3a80b0be15 100644 --- a/test/Sema/dllexport.c +++ b/test/Sema/dllexport.c @@ -69,10 +69,10 @@ void __declspec(dllexport) decl2B(); __declspec(dllexport) void def() {} // Export inline function. -__declspec(dllexport) inline void inlineFunc1() {} // expected-warning{{'dllexport' attribute ignored}} +__declspec(dllexport) inline void inlineFunc1() {} extern void inlineFunc1(); -inline void __attribute__((dllexport)) inlineFunc2() {} // expected-warning{{'dllexport' attribute ignored}} +inline void __attribute__((dllexport)) inlineFunc2() {} extern void inlineFunc2(); // Redeclarations diff --git a/test/Sema/dllimport.c b/test/Sema/dllimport.c index a4982940c6..fa3e2b385a 100644 --- a/test/Sema/dllimport.c +++ b/test/Sema/dllimport.c @@ -97,11 +97,11 @@ void __attribute__((dllimport)) decl2A(); void __declspec(dllimport) decl2B(); // Not allowed on function definitions. -__declspec(dllimport) void def() {} // expected-error{{'dllimport' attribute can be applied only to symbol declaration}} +__declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} // Import inline function. -__declspec(dllimport) inline void inlineFunc1() {} // expected-warning{{'dllimport' attribute ignored}} -inline void __attribute__((dllimport)) inlineFunc2() {} // expected-warning{{'dllimport' attribute ignored}} +__declspec(dllimport) inline void inlineFunc1() {} +inline void __attribute__((dllimport)) inlineFunc2() {} // Redeclarations __declspec(dllimport) void redecl1(); diff --git a/test/SemaCXX/MicrosoftExtensions.cpp b/test/SemaCXX/MicrosoftExtensions.cpp index 0f6ebbb069..3420d20cb8 100644 --- a/test/SemaCXX/MicrosoftExtensions.cpp +++ b/test/SemaCXX/MicrosoftExtensions.cpp @@ -122,7 +122,7 @@ __declspec(dllimport) void f(void) { } void f2(void); // expected-note{{previous declaration is here}} }; -__declspec(dllimport) void AAA::f2(void) { // expected-error{{'dllimport' attribute can be applied only to symbol}} +__declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}} // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}} } diff --git a/test/SemaCXX/dllexport.cpp b/test/SemaCXX/dllexport.cpp index 3558eef8b8..6d67ec25da 100644 --- a/test/SemaCXX/dllexport.cpp +++ b/test/SemaCXX/dllexport.cpp @@ -92,10 +92,10 @@ __declspec(dllexport) void def() {} extern "C" __declspec(dllexport) void externC() {} // Export inline function. -__declspec(dllexport) inline void inlineFunc1() {} // expected-warning{{'dllexport' attribute ignored}} -inline void __attribute__((dllexport)) inlineFunc2() {} // expected-warning{{'dllexport' attribute ignored}} +__declspec(dllexport) inline void inlineFunc1() {} +inline void __attribute__((dllexport)) inlineFunc2() {} -__declspec(dllexport) inline void inlineDecl(); // expected-warning{{'dllexport' attribute ignored}} +__declspec(dllexport) inline void inlineDecl(); void inlineDecl() {} __declspec(dllexport) void inlineDef(); @@ -188,7 +188,7 @@ template void exportedFuncTmpl(); // Export specialization of an exported function template. template<> __declspec(dllexport) void exportedFuncTmpl(); template<> __declspec(dllexport) void exportedFuncTmpl() {} -template<> __declspec(dllexport) inline void exportedFuncTmpl() {} // expected-warning{{'dllexport' attribute ignored}} +template<> __declspec(dllexport) inline void exportedFuncTmpl() {} // Not exporting specialization of an exported function template without // explicit dllexport. @@ -205,7 +205,7 @@ template __declspec(dllexport) void funcTmpl(); // Export specialization of a non-exported function template. template<> __declspec(dllexport) void funcTmpl(); template<> __declspec(dllexport) void funcTmpl() {} -template<> __declspec(dllexport) inline void funcTmpl() {} // expected-warning{{'dllexport' attribute ignored}} +template<> __declspec(dllexport) inline void funcTmpl() {} diff --git a/test/SemaCXX/dllimport.cpp b/test/SemaCXX/dllimport.cpp index 95e9e7d053..def18fa18e 100644 --- a/test/SemaCXX/dllimport.cpp +++ b/test/SemaCXX/dllimport.cpp @@ -116,14 +116,14 @@ void __attribute__((dllimport)) decl2A(); void __declspec(dllimport) decl2B(); // Not allowed on function definitions. -__declspec(dllimport) void def() {} // expected-error{{'dllimport' attribute can be applied only to symbol declaration}} +__declspec(dllimport) void def() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} // extern "C" extern "C" __declspec(dllexport) void externC(); // Import inline function. -__declspec(dllimport) inline void inlineFunc1() {} // expected-warning{{'dllimport' attribute ignored}} -inline void __attribute__((dllimport)) inlineFunc2() {} // expected-warning{{'dllimport' attribute ignored}} +__declspec(dllimport) inline void inlineFunc1() {} +inline void __attribute__((dllimport)) inlineFunc2() {} // Redeclarations __declspec(dllimport) void redecl1(); @@ -209,8 +209,8 @@ template __declspec(dllimport) void importedFuncTmpl(); // Import specialization of an imported function template. A definition must be // declared inline. template<> __declspec(dllimport) void importedFuncTmpl(); -template<> __declspec(dllimport) void importedFuncTmpl() {} // expected-error{{'dllimport' attribute can be applied only to symbol declaration}} -template<> __declspec(dllimport) inline void importedFuncTmpl() {} // expected-warning{{'dllimport' attribute ignored}} +template<> __declspec(dllimport) void importedFuncTmpl() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} +template<> __declspec(dllimport) inline void importedFuncTmpl() {} // Not importing specialization of an imported function template without // explicit dllimport. @@ -223,5 +223,5 @@ extern template __declspec(dllimport) void funcTmpl(); // Import specialization of a non-imported function template. A definition must // be declared inline. template<> __declspec(dllimport) void funcTmpl(); -template<> __declspec(dllimport) void funcTmpl() {} // expected-error{{'dllimport' attribute can be applied only to symbol declaration}} -template<> __declspec(dllimport) inline void funcTmpl() {} // expected-warning{{'dllimport' attribute ignored}} +template<> __declspec(dllimport) void funcTmpl() {} // expected-error{{dllimport cannot be applied to non-inline function definition}} +template<> __declspec(dllimport) inline void funcTmpl() {} -- 2.40.0