[cfe-commits] r108383 - in /cfe/trunk: include/clang/Frontend/ASTConsumers.h include/clang/Frontend/PCHDeserializationListener.h include/clang/Frontend/PCHReader.h include/clang/Frontend/PCHWriter.h lib/Frontend/FrontendActions.cpp lib/Frontend/GeneratePCH.cpp lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Wed Jul 14 16:45:09 PDT 2010


Author: cornedbee
Date: Wed Jul 14 18:45:08 2010
New Revision: 108383

URL: http://llvm.org/viewvc/llvm-project?rev=108383&view=rev
Log:
Add a callback interface that allows interested parties to get notified whenever PCHReader deserializes a type or decl (and possibly other things in the future). Have PCHWriter implement these callbacks as noops and register to receive them if we're chaining PCHs. This will allow PCHWriter to track the IDs of these things, which it needs to write the dependent files. WIP

Added:
    cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h
Modified:
    cfe/trunk/include/clang/Frontend/ASTConsumers.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/FrontendActions.cpp
    cfe/trunk/lib/Frontend/GeneratePCH.cpp
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTConsumers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTConsumers.h?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTConsumers.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTConsumers.h Wed Jul 14 18:45:08 2010
@@ -63,7 +63,7 @@
 // times.
 ASTConsumer *CreatePCHGenerator(const Preprocessor &PP,
                                 llvm::raw_ostream *OS,
-                                const PCHReader *Chain,
+                                PCHReader *Chain,
                                 const char *isysroot = 0);
 
 // Inheritance viewer: for C++ code, creates a graph of the inheritance

Added: cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h?rev=108383&view=auto
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h (added)
+++ cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h Wed Jul 14 18:45:08 2010
@@ -0,0 +1,36 @@
+//===- PCHDeserializationListener.h - Decl/Type PCH Read Events -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the PCHDeserializationListener class, which is notified
+//  by the PCHReader whenever a type or declaration is deserialized.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_FRONTEND_PCH_DESERIALIZATION_LISTENER_H
+#define LLVM_CLANG_FRONTEND_PCH_DESERIALIZATION_LISTENER_H
+
+#include "clang/Frontend/PCHBitCodes.h"
+
+namespace clang {
+
+class Decl;
+class QualType;
+
+class PCHDeserializationListener {
+protected:
+  ~PCHDeserializationListener() {}
+
+public:
+  virtual void TypeRead(pch::TypeID ID, QualType T) = 0;
+  virtual void DeclRead(pch::DeclID ID, const Decl *D) = 0;
+};
+
+}
+
+#endif

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Wed Jul 14 18:45:08 2010
@@ -59,6 +59,7 @@
 class LabelStmt;
 class MacroDefinition;
 class NamedDecl;
+class PCHDeserializationListener;
 class Preprocessor;
 class Sema;
 class SwitchCase;
@@ -100,10 +101,7 @@
 
   /// \brief Receives the contents of the predefines buffer.
   ///
-  /// \param PCHPredef The start of the predefines buffer in the PCH
-  /// file.
-  ///
-  /// \param PCHBufferID The FileID for the PCH predefines buffer.
+  /// \param Buffers Information about the predefines buffers.
   ///
   /// \param OriginalFileName The original file name for the PCH, which will
   /// appear as an entry in the predefines buffer.
@@ -172,9 +170,12 @@
   enum PCHReadResult { Success, Failure, IgnorePCH };
   friend class PCHValidator;
 private:
-  /// \ brief The receiver of some callbacks invoked by PCHReader.
+  /// \brief The receiver of some callbacks invoked by PCHReader.
   llvm::OwningPtr<PCHReaderListener> Listener;
 
+  /// \brief The receiver of deserialization events.
+  PCHDeserializationListener *DeserializationListener;
+
   SourceManager &SourceMgr;
   FileManager &FileMgr;
   Diagnostic &Diags;
@@ -577,6 +578,10 @@
     Listener.reset(listener);
   }
 
+  void setDeserializationListener(PCHDeserializationListener *Listener) {
+    DeserializationListener = Listener;
+  }
+
   /// \brief Set the Preprocessor to use.
   void setPreprocessor(Preprocessor &pp);
 

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Wed Jul 14 18:45:08 2010
@@ -19,6 +19,7 @@
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/TemplateBase.h"
 #include "clang/Frontend/PCHBitCodes.h"
+#include "clang/Frontend/PCHDeserializationListener.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include <map>
@@ -71,7 +72,7 @@
 /// representation of a given abstract syntax tree and its supporting
 /// data structures. This bitstream can be de-serialized via an
 /// instance of the PCHReader class.
-class PCHWriter {
+class PCHWriter : public PCHDeserializationListener {
 public:
   typedef llvm::SmallVector<uint64_t, 64> RecordData;
 
@@ -79,6 +80,9 @@
   /// \brief The bitstream writer used to emit this precompiled header.
   llvm::BitstreamWriter &Stream;
 
+  /// \brief The reader of existing PCH files, if we're chaining.
+  PCHReader *Chain;
+
   /// \brief Stores a declaration or a type to be written to the PCH file.
   class DeclOrType {
   public:
@@ -220,7 +224,7 @@
   void WriteSubStmt(Stmt *S);
 
   void WriteBlockInfoBlock();
-  void WriteMetadata(ASTContext &Context, const PCHReader *Chain, const char *isysroot);
+  void WriteMetadata(ASTContext &Context, const char *isysroot);
   void WriteLanguageOptions(const LangOptions &LangOpts);
   void WriteStatCache(MemorizeStatCalls &StatCalls);
   void WriteSourceManagerBlock(SourceManager &SourceMgr,
@@ -242,12 +246,12 @@
   void WritePCHCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
                     const char* isysroot);
   void WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
-                     const PCHReader *Chain, const char* isysroot);
+                     const char* isysroot);
   
 public:
   /// \brief Create a new precompiled header writer that outputs to
   /// the given bitstream.
-  PCHWriter(llvm::BitstreamWriter &Stream);
+  PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain);
 
   /// \brief Write a precompiled header for the given semantic analysis.
   ///
@@ -263,7 +267,7 @@
   /// \param PPRec Record of the preprocessing actions that occurred while
   /// preprocessing this file, e.g., macro instantiations
   void WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
-                const PCHReader *Chain, const char* isysroot);
+                const char* isysroot);
 
   /// \brief Emit a source location.
   void AddSourceLocation(SourceLocation Loc, RecordData &Record);
@@ -393,6 +397,10 @@
   unsigned GetLabelID(LabelStmt *S);
 
   unsigned getParmVarDeclAbbrev() const { return ParmVarDeclAbbrev; }
+
+  // PCHDeserializationListener implementation
+  void TypeRead(pch::TypeID ID, QualType T);
+  void DeclRead(pch::DeclID ID, const Decl *D);
 };
 
 } // end namespace clang

Modified: cfe/trunk/lib/Frontend/FrontendActions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/FrontendActions.cpp?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/FrontendActions.cpp (original)
+++ cfe/trunk/lib/Frontend/FrontendActions.cpp Wed Jul 14 18:45:08 2010
@@ -80,7 +80,7 @@
   if (!OS)
     return 0;
 
-  const PCHReader *Chain = CI.getInvocation().getFrontendOpts().ChainedPCH ?
+  PCHReader *Chain = CI.getInvocation().getFrontendOpts().ChainedPCH ?
                                CI.getPCHReader() : 0;
   const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
                              Sysroot.c_str() : 0;

Modified: cfe/trunk/lib/Frontend/GeneratePCH.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/GeneratePCH.cpp?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/GeneratePCH.cpp (original)
+++ cfe/trunk/lib/Frontend/GeneratePCH.cpp Wed Jul 14 18:45:08 2010
@@ -28,28 +28,28 @@
 namespace {
   class PCHGenerator : public SemaConsumer {
     const Preprocessor &PP;
-    const PCHReader *Chain;
     const char *isysroot;
     llvm::raw_ostream *Out;
     Sema *SemaPtr;
     MemorizeStatCalls *StatCalls; // owned by the FileManager
+    std::vector<unsigned char> Buffer;
+    llvm::BitstreamWriter Stream;
+    PCHWriter Writer;
 
   public:
-    explicit PCHGenerator(const Preprocessor &PP,
-                          const PCHReader *Chain,
-                          const char *isysroot,
-                          llvm::raw_ostream *Out);
+    PCHGenerator(const Preprocessor &PP, PCHReader *Chain,
+                 const char *isysroot, llvm::raw_ostream *Out);
     virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
     virtual void HandleTranslationUnit(ASTContext &Ctx);
   };
 }
 
 PCHGenerator::PCHGenerator(const Preprocessor &PP,
-                           const PCHReader *Chain,
+                           PCHReader *Chain,
                            const char *isysroot,
                            llvm::raw_ostream *OS)
-  : PP(PP), Chain(Chain), isysroot(isysroot), Out(OS), SemaPtr(0),
-    StatCalls(0) {
+  : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0),
+    Stream(Buffer), Writer(Stream, Chain) {
 
   // Install a stat() listener to keep track of all of the stat()
   // calls.
@@ -61,25 +61,23 @@
   if (PP.getDiagnostics().hasErrorOccurred())
     return;
 
-  // Write the PCH contents into a buffer
-  std::vector<unsigned char> Buffer;
-  llvm::BitstreamWriter Stream(Buffer);
-  PCHWriter Writer(Stream);
-
   // Emit the PCH file
   assert(SemaPtr && "No Sema?");
-  Writer.WritePCH(*SemaPtr, StatCalls, Chain, isysroot);
+  Writer.WritePCH(*SemaPtr, StatCalls, isysroot);
 
   // Write the generated bitstream to "Out".
   Out->write((char *)&Buffer.front(), Buffer.size());
 
   // Make sure it hits disk now.
   Out->flush();
+
+  // Free up some memory, in case the process is kept alive.
+  Buffer.clear();
 }
 
 ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
                                        llvm::raw_ostream *OS,
-                                       const PCHReader *Chain,
+                                       PCHReader *Chain,
                                        const char *isysroot) {
   return new PCHGenerator(PP, Chain, isysroot, OS);
 }

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Wed Jul 14 18:45:08 2010
@@ -13,6 +13,7 @@
 
 #include "clang/Frontend/PCHReader.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Frontend/PCHDeserializationListener.h"
 #include "clang/Frontend/Utils.h"
 #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
 #include "clang/AST/ASTConsumer.h"
@@ -413,10 +414,10 @@
 
 PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context,
                      const char *isysroot)
-  : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()),
-    FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
-    SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0),
-    IdentifierTableData(0), IdentifierLookupTable(0),
+  : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
+    SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
+    Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
+    StatCache(0), Consumer(0), IdentifierTableData(0), IdentifierLookupTable(0),
     IdentifierOffsets(0),
     MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
     TotalSelectorsInMethodPool(0), SelectorOffsets(0),
@@ -432,8 +433,8 @@
 
 PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr,
                      Diagnostic &Diags, const char *isysroot)
-  : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags),
-    SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
+  : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
+    Diags(Diags), SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0),
     IdentifierTableData(0), IdentifierLookupTable(0),
     IdentifierOffsets(0),
     MethodPoolLookupTable(0), MethodPoolLookupTableData(0),
@@ -2629,6 +2630,8 @@
   if (TypesLoaded[Index].isNull()) {
     TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]);
     TypesLoaded[Index]->setFromPCH();
+    if (DeserializationListener)
+      DeserializationListener->TypeRead(ID, TypesLoaded[Index]);
   }
 
   return TypesLoaded[Index].withFastQualifiers(FastQuals);
@@ -2675,8 +2678,11 @@
 }
 
 TranslationUnitDecl *PCHReader::GetTranslationUnitDecl() {
-  if (!DeclsLoaded[0])
+  if (!DeclsLoaded[0]) {
     ReadDeclRecord(DeclOffsets[0], 0);
+    if (DeserializationListener)
+      DeserializationListener->DeclRead(0, DeclsLoaded[0]);
+  }
 
   return cast<TranslationUnitDecl>(DeclsLoaded[0]);
 }
@@ -2691,8 +2697,11 @@
   }
 
   unsigned Index = ID - 1;
-  if (!DeclsLoaded[Index])
+  if (!DeclsLoaded[Index]) {
     ReadDeclRecord(DeclOffsets[Index], Index);
+    if (DeserializationListener)
+      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
+  }
 
   return DeclsLoaded[Index];
 }

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=108383&r1=108382&r2=108383&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Wed Jul 14 18:45:08 2010
@@ -743,8 +743,7 @@
 }
 
 /// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
-void PCHWriter::WriteMetadata(ASTContext &Context, const PCHReader *Chain,
-                              const char *isysroot) {
+void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
   using namespace llvm;
 
   // Metadata
@@ -2074,13 +2073,16 @@
   SelectorOffsets[ID - 1] = Offset;
 }
 
-PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
-  : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
+PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain)
+  : Stream(Stream), Chain(Chain), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
     CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
-    NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { }
+    NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) {
+  if (Chain)
+    Chain->setDeserializationListener(this);
+}
 
 void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
-                         const PCHReader *Chain, const char *isysroot) {
+                         const char *isysroot) {
   // Emit the file header.
   Stream.Emit((unsigned)'C', 8);
   Stream.Emit((unsigned)'P', 8);
@@ -2090,7 +2092,7 @@
   WriteBlockInfoBlock();
 
   if (Chain)
-    WritePCHChain(SemaRef, StatCalls, Chain, isysroot);
+    WritePCHChain(SemaRef, StatCalls, isysroot);
   else
     WritePCHCore(SemaRef, StatCalls, isysroot);
 }
@@ -2164,7 +2166,7 @@
   // Write the remaining PCH contents.
   RecordData Record;
   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
-  WriteMetadata(Context, 0, isysroot);
+  WriteMetadata(Context, isysroot);
   WriteLanguageOptions(Context.getLangOptions());
   if (StatCalls && !isysroot)
     WriteStatCache(*StatCalls);
@@ -2275,7 +2277,7 @@
 }
 
 void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
-                              const PCHReader *Chain, const char *isysroot) {
+                              const char *isysroot) {
   using namespace llvm;
 
   ASTContext &Context = SemaRef.Context;
@@ -2284,7 +2286,7 @@
   
   RecordData Record;
   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5);
-  WriteMetadata(Context, Chain, isysroot);
+  WriteMetadata(Context, isysroot);
   // FIXME: StatCache
   // FIXME: Source manager block
 
@@ -2737,3 +2739,10 @@
   AddTypeRef(Base.getType(), Record);
   AddSourceRange(Base.getSourceRange(), Record);
 }
+
+void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
+}
+
+void PCHWriter::DeclRead(pch::DeclID ID, const Decl *D) {
+}
+





More information about the cfe-commits mailing list