From: Fariborz Jahanian Date: Tue, 26 Oct 2010 22:47:47 +0000 (+0000) Subject: Patch to provide guard when initializing instances X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=92d835a86ac334768d0b75936201e4fea3941c1f;p=clang Patch to provide guard when initializing instances of static data member of a class template. Fixes //rdar :// 8562966 and pr8409. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@117410 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp index 1b42f6189e..6d0806424d 100644 --- a/lib/CodeGen/CGDeclCXX.cpp +++ b/lib/CodeGen/CGDeclCXX.cpp @@ -253,7 +253,15 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, SourceLocation()); llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); - EmitCXXGlobalVarDeclInit(*D, DeclPtr); + if (D->isStaticDataMember() && + D->getInstantiatedFromStaticDataMember() && D->getInit()){ + llvm::GlobalVariable *GV = dyn_cast(DeclPtr); + assert(GV && "GenerateCXXGlobalVarDeclInitFunc - GV is null"); + GV->setConstant(false); + EmitCXXStaticLocalInit(*D, GV); + } + else + EmitCXXGlobalVarDeclInit(*D, DeclPtr); FinishFunction(); } diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp index 4d0e05da4a..b6c40b64db 100644 --- a/lib/CodeGen/ItaniumCXXABI.cpp +++ b/lib/CodeGen/ItaniumCXXABI.cpp @@ -1092,9 +1092,14 @@ void ItaniumCXXABI::EmitStaticLocalInit(CodeGenFunction &CGF, // Create the guard variable. llvm::SmallString<256> GuardVName; getMangleContext().mangleItaniumGuardVariable(&D, GuardVName); + llvm::GlobalValue::LinkageTypes Linkage = GV->getLinkage(); + if (D.isStaticDataMember() && + D.getInstantiatedFromStaticDataMember()) + Linkage = llvm::GlobalVariable::WeakAnyLinkage; + llvm::GlobalVariable *GuardVariable = new llvm::GlobalVariable(CGM.getModule(), GuardTy, - false, GV->getLinkage(), + false, Linkage, llvm::ConstantInt::get(GuardTy, 0), GuardVName.str()); diff --git a/test/CodeGenCXX/specialized-static-data-mem-init.cpp b/test/CodeGenCXX/specialized-static-data-mem-init.cpp new file mode 100644 index 0000000000..8f5765bcbb --- /dev/null +++ b/test/CodeGenCXX/specialized-static-data-mem-init.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s +// rdar: // 8562966 +// pr8409 + +// CHECK: @_ZN1CIiE11needs_guardE = weak global +// CHECK: @_ZGVN1CIiE11needs_guardE = weak global + +struct K +{ + K(); + K(const K &); + ~K(); + void PrintNumK(); +}; + +template +struct C +{ + void Go() { needs_guard.PrintNumK(); } + static K needs_guard; +}; + +template K C::needs_guard; + +void F() +{ + C().Go(); +} +