From 0419e401d392d3664259a3ad1c25e2fb64abcb15 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 16 Dec 2014 04:52:14 +0000 Subject: [PATCH] AST: Fix the linkage of static vars in fn template specializations We that static variables in function template specializations were externally visible. The manglers assumed that externally visible static variables were numbered in Sema. We would end up mangling static variables in the same specialization with the same mangling number which would give all of them the same name. This fixes PR21904. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224316 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/Decl.cpp | 2 +- .../function-template-specialization.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 1c0c6f7667..e43c28af9b 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1192,7 +1192,7 @@ static LinkageInfo getLVForLocalDecl(const NamedDecl *D, } else { const FunctionDecl *FD = cast(OuterD); if (!FD->isInlined() && - FD->getTemplateSpecializationKind() == TSK_Undeclared) + !isTemplateInstantiation(FD->getTemplateSpecializationKind())) return LinkageInfo::none(); LV = getLVForDecl(FD, computation); diff --git a/test/CodeGenCXX/function-template-specialization.cpp b/test/CodeGenCXX/function-template-specialization.cpp index eb099df14d..7728f3dc74 100644 --- a/test/CodeGenCXX/function-template-specialization.cpp +++ b/test/CodeGenCXX/function-template-specialization.cpp @@ -1,4 +1,8 @@ // RUN: %clang_cc1 -emit-llvm -triple %itanium_abi_triple %s -o - | FileCheck %s + +// CHECK-DAG: _ZZN7PR219047GetDataIiEERKibE1i = internal global i32 4 +// CHECK-DAG: _ZZN7PR219047GetDataIiEERKibE1i_0 = internal global i32 2 + template T* next(T* ptr, const U& diff); @@ -24,3 +28,18 @@ void test2(int *iptr, double *dptr, int diff) { // CHECK: _Z4nextIdiEPT_S1_RKT0_ dptr = next(dptr, diff); } + +namespace PR21904 { +template +const int &GetData(bool); + +template <> +const int &GetData(bool b) { + static int i = 4; + if (b) { + static int i = 2; + return i; + } + return i; +} +}