From 9610d77508bdca13e0475783ff404428611c9683 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 17 Jun 2013 20:04:51 +0000 Subject: [PATCH] Cleanup linkage computation for static locals. With this patch we assign VisibleNoLinkage to static locals in inline functions. This lets us simplify CodeGen a bit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184114 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/Decl.cpp | 6 +++++- lib/CodeGen/CGDecl.cpp | 8 +++----- test/CodeGenCXX/linkage.cpp | 11 +++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index a36fcf2507..c799161e9b 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1043,9 +1043,13 @@ static LinkageInfo getLVForLocalDecl(const NamedDecl *D, return LV; } + + if (!Var->isStaticLocal()) + return LinkageInfo::none(); } - if (!isa(D)) + ASTContext &Context = D->getASTContext(); + if (!Context.getLangOpts().CPlusPlus) return LinkageInfo::none(); const FunctionDecl *FD = getOutermostFunctionContext(D); diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index cb15748646..614eaab3f3 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -126,10 +126,8 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) { // If the function definition has some sort of weak linkage, its // static variables should also be weak so that they get properly - // uniqued. We can't do this in C, though, because there's no - // standard way to agree on which variables are the same (i.e. - // there's no mangling). - if (getLangOpts().CPlusPlus) { + // uniqued. + if (D.isExternallyVisible()) { const Decl *D = CurCodeDecl; while (true) { if (isa(D)) { @@ -143,7 +141,7 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) { } } // FIXME: Do we really only care about FunctionDecls here? - if (D && isa(D)) { + if (isa(D)) { llvm::GlobalValue::LinkageTypes ParentLinkage = CGM.getFunctionLinkage(cast(D)); if (llvm::GlobalValue::isWeakForLinker(ParentLinkage)) diff --git a/test/CodeGenCXX/linkage.cpp b/test/CodeGenCXX/linkage.cpp index 7c67029399..c7feaefeb8 100644 --- a/test/CodeGenCXX/linkage.cpp +++ b/test/CodeGenCXX/linkage.cpp @@ -209,3 +209,14 @@ namespace test16 { } void *test() { return foo::bar(); } } + +namespace test17 { + // CHECK-DAG: @_ZZN6test173fooILi42EEEPivE3bar = weak_odr + // CHECK-DAG: define weak_odr i32* @_ZN6test173fooILi42EEEPiv( + template + int *foo() { + static int bar; + return &bar; + } + template int *foo<42>(); +} -- 2.40.0