]> granicus.if.org Git - clang/commitdiff
Chained PCH: Remember when additional specializations are added to a function templat...
authorSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 14 Apr 2011 14:07:59 +0000 (14:07 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 14 Apr 2011 14:07:59 +0000 (14:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129516 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTMutationListener.h
include/clang/AST/DeclTemplate.h
include/clang/Serialization/ASTWriter.h
lib/AST/Decl.cpp
lib/AST/DeclTemplate.cpp
lib/Frontend/MultiplexConsumer.cpp
lib/Serialization/ASTWriter.cpp
test/PCH/cxx-chain-function-template.cpp [new file with mode: 0644]

index 01e61802491370a3b3cec6410f7f79b3c2428539..33e9ad485968da6997736cbfafc54373656e8812 100644 (file)
@@ -20,6 +20,8 @@ namespace clang {
   class CXXRecordDecl;
   class ClassTemplateDecl;
   class ClassTemplateSpecializationDecl;
+  class FunctionDecl;
+  class FunctionTemplateDecl;
 
 /// \brief An abstract interface that should be implemented by listeners
 /// that want to be notified when an AST entity gets modified after its
@@ -41,6 +43,11 @@ public:
   /// template declaration.
   virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
                                     const ClassTemplateSpecializationDecl *D) {}
+
+  /// \brief A template specialization (or partial one) was added to the
+  /// template declaration.
+  virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
+                                              const FunctionDecl *D) {}
 };
 
 } // end namespace clang
index 0fba9f62070a89cea97cb6d3f330ae3abe0d22b6..823bd84b38b0837c313e92cdb72b2b2f3efda273 100644 (file)
@@ -804,6 +804,13 @@ protected:
   llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
     return getCommonPtr()->Specializations;
   }
+
+  /// \brief Add a specialization of this function template.
+  ///
+  /// \param InsertPos Insert position in the FoldingSet, must have been
+  ///        retrieved by an earlier call to findSpecialization().
+  void addSpecialization(FunctionTemplateSpecializationInfo* Info,
+                         void *InsertPos);
   
 public:
   /// Get the underlying function declaration of the template.
index 474631fce04b83a2bc0b6c48268e14d104fdb500..f33381ed058bbb71a434af81f881e178dd3db14d 100644 (file)
@@ -586,6 +586,8 @@ public:
   virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
   virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
                                     const ClassTemplateSpecializationDecl *D);
+  virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
+                                              const FunctionDecl *D);
 };
 
 /// \brief AST and semantic-analysis consumer that generates a
index dfa53881f1ac6d3c65b700e62a7372e0e30aa55f..99a180662ddac4830d96065696a9a0dc4c775621 100644 (file)
@@ -1893,7 +1893,7 @@ FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
   // Insert this function template specialization into the set of known
   // function template specializations.
   if (InsertPos)
-    Template->getSpecializations().InsertNode(Info, InsertPos);
+    Template->addSpecialization(Info, InsertPos);
   else {
     // Try to insert the new node. If there is an existing node, leave it, the
     // set will contain the canonical decls while
index ae17dd15b1289551a615fda6d1572e655ace8877..73c91dbd72b82c0a0b56c8af40610e8fb6894236 100644 (file)
@@ -242,6 +242,13 @@ FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
   return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
 }
 
+void FunctionTemplateDecl::addSpecialization(
+      FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
+  getSpecializations().InsertNode(Info, InsertPos);
+  if (ASTMutationListener *L = getASTMutationListener())
+    L->AddedCXXTemplateSpecialization(this, Info->Function);
+}
+
 std::pair<const TemplateArgument *, unsigned> 
 FunctionTemplateDecl::getInjectedTemplateArgs() {
   TemplateParameterList *Params = getTemplateParameters();
index 3649c3c997287f2c69ceb27539fdcb9c80b27de5..ecad91f1f04ee6427bf1f2380ae6837dda1d9818 100644 (file)
@@ -95,6 +95,8 @@ public:
   virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
   virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
                                     const ClassTemplateSpecializationDecl *D);
+  virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
+                                              const FunctionDecl *D);
 private:
   std::vector<ASTMutationListener*> Listeners;
 };
@@ -125,6 +127,11 @@ void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
   for (size_t i = 0, e = Listeners.size(); i != e; ++i)
     Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
 }
+void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
+    const FunctionTemplateDecl *TD, const FunctionDecl *D) {
+  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
+    Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
+}
 
 }  // end namespace clang
 
index f7dfc38224d528c9490736db050c96574671a2eb..0e925a2691b876ddea3a0da059ec5e6afc248404 100644 (file)
@@ -3936,4 +3936,16 @@ void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
   AddDeclRef(D, Record);
 }
 
+void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
+                                               const FunctionDecl *D) {
+  // The specializations set is kept in the canonical template.
+  TD = TD->getCanonicalDecl();
+  if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
+    return; // Not a source specialization added to a template from PCH.
+
+  UpdateRecord &Record = DeclUpdates[TD];
+  Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
+  AddDeclRef(D, Record);
+}
+
 ASTSerializationListener::~ASTSerializationListener() { }
diff --git a/test/PCH/cxx-chain-function-template.cpp b/test/PCH/cxx-chain-function-template.cpp
new file mode 100644 (file)
index 0000000..494e190
--- /dev/null
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -chain-include %s -chain-include %s -fsyntax-only %s
+// Just don't crash.
+#if !defined(RUN1)
+#define RUN1
+
+struct CXXRecordDecl { CXXRecordDecl(int); };
+
+template <typename T, typename U>
+T cast(U u) {
+  return reinterpret_cast<T&>(u);
+}
+
+void test1() {
+  cast<float>(1);
+}
+
+#elif !defined(RUN2)
+#define RUN2
+
+template <typename T>
+void test2(T) {
+  cast<CXXRecordDecl>(1.0f);
+}
+
+#else
+
+void test3() {
+  cast<CXXRecordDecl>(1.0f);
+  test2(1);
+}
+
+#endif