[cfe-commits] r129516 - in /cfe/trunk: 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

Sebastian Redl sebastian.redl at getdesigned.at
Thu Apr 14 07:08:00 PDT 2011


Author: cornedbee
Date: Thu Apr 14 09:07:59 2011
New Revision: 129516

URL: http://llvm.org/viewvc/llvm-project?rev=129516&view=rev
Log:
Chained PCH: Remember when additional specializations are added to a function template from a previous PCH. Fixes the only crasher when using massive chains on Clang's Sema component. We still have some incomplete codegen there.

Added:
    cfe/trunk/test/PCH/cxx-chain-function-template.cpp
Modified:
    cfe/trunk/include/clang/AST/ASTMutationListener.h
    cfe/trunk/include/clang/AST/DeclTemplate.h
    cfe/trunk/include/clang/Serialization/ASTWriter.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/DeclTemplate.cpp
    cfe/trunk/lib/Frontend/MultiplexConsumer.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/AST/ASTMutationListener.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTMutationListener.h?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTMutationListener.h (original)
+++ cfe/trunk/include/clang/AST/ASTMutationListener.h Thu Apr 14 09:07:59 2011
@@ -20,6 +20,8 @@
   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 @@
   /// 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

Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Thu Apr 14 09:07:59 2011
@@ -804,6 +804,13 @@
   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.

Modified: cfe/trunk/include/clang/Serialization/ASTWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTWriter.h?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTWriter.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTWriter.h Thu Apr 14 09:07:59 2011
@@ -586,6 +586,8 @@
   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

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Thu Apr 14 09:07:59 2011
@@ -1893,7 +1893,7 @@
   // 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

Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Thu Apr 14 09:07:59 2011
@@ -242,6 +242,13 @@
   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();

Modified: cfe/trunk/lib/Frontend/MultiplexConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/MultiplexConsumer.cpp?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/MultiplexConsumer.cpp (original)
+++ cfe/trunk/lib/Frontend/MultiplexConsumer.cpp Thu Apr 14 09:07:59 2011
@@ -95,6 +95,8 @@
   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 @@
   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
 

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=129516&r1=129515&r2=129516&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Thu Apr 14 09:07:59 2011
@@ -3936,4 +3936,16 @@
   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() { }

Added: cfe/trunk/test/PCH/cxx-chain-function-template.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/cxx-chain-function-template.cpp?rev=129516&view=auto
==============================================================================
--- cfe/trunk/test/PCH/cxx-chain-function-template.cpp (added)
+++ cfe/trunk/test/PCH/cxx-chain-function-template.cpp Thu Apr 14 09:07:59 2011
@@ -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





More information about the cfe-commits mailing list