]> granicus.if.org Git - clang/commitdiff
Fix a small difference in sema and codegen views of what needs to be output.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 5 Mar 2012 10:54:55 +0000 (10:54 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 5 Mar 2012 10:54:55 +0000 (10:54 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152024 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTConsumer.h
include/clang/Frontend/MultiplexConsumer.h
lib/CodeGen/CodeGenAction.cpp
lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/CodeGenModule.h
lib/CodeGen/ModuleBuilder.cpp
lib/Frontend/MultiplexConsumer.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
test/CodeGenCXX/pr12104.cpp [new file with mode: 0644]

index 3c0e0fc9dc31a7d6cc37701fa993cc157f93a284..6e35aefbe97dbc35e77795422ab89536cf1a79e6 100644 (file)
@@ -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.
index 8814aa31bba2a89fa7ade13d0840ad73bfed5c7f..c2eb1ccbda68d67b2dcfebbb0a179e61951cc2be 100644 (file)
@@ -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);
index 02e08602922ad31ab89ebf9a7131afe19f2583f6..b669d994ad8ed73e4eae839356a87d5779878d92 100644 (file)
@@ -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;
 
index 922a5df344f45f5b2c6e6a49232f891e8a7e9a64..b2bfab055b6119c3868be144144fbf3e69d9f368 100644 (file)
@@ -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());
index ba4887d057e9f9f056f9ec18020a2817866f37e5..93eee444310d2c0ccb756047c59d983a472c5684 100644 (file)
@@ -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.
index ddbe27bc176181f080a9a64ac2c55f2050ffee7c..f81f6236d62b79f5e649785090d3d4e37ebab848 100644 (file)
@@ -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)
index cef4590955ab1425adf9f584c952b7889dc038a2..a1d1156529f23903a14cb98e70ced34f57c09bce 100644 (file)
@@ -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);
index d491dfc51ed4440fd4950301de74fecc0d5f8340..4633db737acd33948b2380c7bdb91cf347346e47 100644 (file)
@@ -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/test/CodeGenCXX/pr12104.cpp b/test/CodeGenCXX/pr12104.cpp
new file mode 100644 (file)
index 0000000..d126108
--- /dev/null
@@ -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