diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2012-03-05 10:54:55 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2012-03-05 10:54:55 +0000 |
commit | 189fa748ec9e05820f7c5df8ff00aa3c7bc0546c (patch) | |
tree | 20583052017fe002b904ee90abb1f5cde9e6a733 | |
parent | 08a47fd708680fc67f8c1c80f64379b35d6f472c (diff) | |
download | bcm5719-llvm-189fa748ec9e05820f7c5df8ff00aa3c7bc0546c.tar.gz bcm5719-llvm-189fa748ec9e05820f7c5df8ff00aa3c7bc0546c.zip |
Fix a small difference in sema and codegen views of what needs to be output.
In the included testcase, soma thinks that we already have a definition after we
see the out of line decl. Codegen puts it in a deferred list, to be output if
a use is seen. This would break when we saw an explicit template instantiation
definition, since codegen would not be notified.
This patch adds a method to the consumer interface so that soma can notify
codegen that this decl is now required.
llvm-svn: 152024
-rw-r--r-- | clang/include/clang/AST/ASTConsumer.h | 5 | ||||
-rw-r--r-- | clang/include/clang/Frontend/MultiplexConsumer.h | 1 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenAction.cpp | 4 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 3 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.h | 5 | ||||
-rw-r--r-- | clang/lib/CodeGen/ModuleBuilder.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Frontend/MultiplexConsumer.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 12 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/pr12104.cpp | 14 |
9 files changed, 49 insertions, 4 deletions
diff --git a/clang/include/clang/AST/ASTConsumer.h b/clang/include/clang/AST/ASTConsumer.h index 3c0e0fc9dc3..6e35aefbe97 100644 --- a/clang/include/clang/AST/ASTConsumer.h +++ b/clang/include/clang/AST/ASTConsumer.h @@ -90,6 +90,11 @@ public: /// modified by the introduction of an implicit zero initializer. virtual void CompleteTentativeDefinition(VarDecl *D) {} + /// MarkVarRequired - Tell the consumer that this variable must be output. + /// This is needed when the definition is initially one that can be deferred, + /// but we then see an explicit template instantiation definition. + virtual void MarkVarRequired(VarDecl *D) {} + /// \brief Callback involved at the end of a translation unit to /// notify the consumer that a vtable for the given C++ class is /// required. diff --git a/clang/include/clang/Frontend/MultiplexConsumer.h b/clang/include/clang/Frontend/MultiplexConsumer.h index 8814aa31bba..c2eb1ccbda6 100644 --- a/clang/include/clang/Frontend/MultiplexConsumer.h +++ b/clang/include/clang/Frontend/MultiplexConsumer.h @@ -35,6 +35,7 @@ public: // ASTConsumer virtual void Initialize(ASTContext &Context); + virtual void MarkVarRequired(VarDecl *VD); virtual bool HandleTopLevelDecl(DeclGroupRef D); virtual void HandleInterestingDecl(DeclGroupRef D); virtual void HandleTranslationUnit(ASTContext &Ctx); diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 02e08602922..b669d994ad8 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -73,6 +73,10 @@ namespace clang { llvm::Module *takeModule() { return TheModule.take(); } llvm::Module *takeLinkModule() { return LinkModule.take(); } + virtual void MarkVarRequired(VarDecl *VD) { + Gen->MarkVarRequired(VD); + } + virtual void Initialize(ASTContext &Ctx) { Context = &Ctx; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 922a5df344f..b2bfab055b6 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1722,6 +1722,9 @@ static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, } } +void CodeGenModule::MarkVarRequired(VarDecl *VD) { + GetAddrOfGlobalVar(VD); +} void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index ba4887d057e..93eee444310 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -658,6 +658,11 @@ public: /// EmitTopLevelDecl - Emit code for a single top level declaration. void EmitTopLevelDecl(Decl *D); + /// MarkVarRequired - Tell the consumer that this variable must be output. + /// This is needed when the definition is initially one that can be deferred, + /// but we then see an explicit template instantiation definition. + void MarkVarRequired(VarDecl *VD); + /// AddUsedGlobal - Add a global which should be forced to be /// present in the object file; these are emitted to the llvm.used /// metadata global. diff --git a/clang/lib/CodeGen/ModuleBuilder.cpp b/clang/lib/CodeGen/ModuleBuilder.cpp index ddbe27bc176..f81f6236d62 100644 --- a/clang/lib/CodeGen/ModuleBuilder.cpp +++ b/clang/lib/CodeGen/ModuleBuilder.cpp @@ -59,6 +59,10 @@ namespace { *M, *TD, Diags)); } + virtual void MarkVarRequired(VarDecl *VD) { + Builder->MarkVarRequired(VD); + } + virtual bool HandleTopLevelDecl(DeclGroupRef DG) { // Make sure to emit all elements of a Decl. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) diff --git a/clang/lib/Frontend/MultiplexConsumer.cpp b/clang/lib/Frontend/MultiplexConsumer.cpp index cef4590955a..a1d1156529f 100644 --- a/clang/lib/Frontend/MultiplexConsumer.cpp +++ b/clang/lib/Frontend/MultiplexConsumer.cpp @@ -209,6 +209,11 @@ bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) { return Continue; } +void MultiplexConsumer::MarkVarRequired(VarDecl *VD) { + for (size_t i = 0, e = Consumers.size(); i != e; ++i) + Consumers[i]->MarkVarRequired(VD); +} + void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) { for (size_t i = 0, e = Consumers.size(); i != e; ++i) Consumers[i]->HandleInterestingDecl(D); diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index d491dfc51ed..4633db737ac 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2596,21 +2596,25 @@ void Sema::InstantiateStaticDataMemberDefinition( return; } + TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); + // Never instantiate an explicit specialization. - if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) + if (TSK == TSK_ExplicitSpecialization) return; // C++0x [temp.explicit]p9: // Except for inline functions, other explicit instantiation declarations // have the effect of suppressing the implicit instantiation of the entity // to which they refer. - if (Var->getTemplateSpecializationKind() - == TSK_ExplicitInstantiationDeclaration) + if (TSK == TSK_ExplicitInstantiationDeclaration) return; // If we already have a definition, we're done. - if (Var->getDefinition()) + if (Var->getDefinition()) { + if (TSK == TSK_ExplicitInstantiationDefinition) + Consumer.MarkVarRequired(Var); return; + } InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); if (Inst) diff --git a/clang/test/CodeGenCXX/pr12104.cpp b/clang/test/CodeGenCXX/pr12104.cpp new file mode 100644 index 00000000000..d126108073f --- /dev/null +++ b/clang/test/CodeGenCXX/pr12104.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s + +template <int dimm> struct Patch { + static const unsigned int no_neighbor = 1; +}; +template <int dim> +const unsigned int Patch<dim>::no_neighbor; +void f(const unsigned int); +void g() { + f(Patch<1>::no_neighbor); +} +template struct Patch<1>; + +// CHECK: _ZN5PatchILi1EE11no_neighborE |