[cfe-commits] r109856 - in /cfe/trunk: include/clang/AST/ExternalASTSource.h include/clang/Frontend/PCHReader.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHReaderDecl.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Jul 30 03:03:16 PDT 2010


Author: akirtzidis
Date: Fri Jul 30 05:03:16 2010
New Revision: 109856

URL: http://llvm.org/viewvc/llvm-project?rev=109856&view=rev
Log:
Refactor the way PCHReader tracks whether we are in recursive loading.

-Replace CurrentlyLoadingTypeOrDecl with a counting scheme (NumCurrentElementsDeserializing)
-Provide outside access to the mechanism by adding methods StartedDeserializing/FinishedDeserializing
  to ExternalASTSource.

These are preparation for the next commit.

Modified:
    cfe/trunk/include/clang/AST/ExternalASTSource.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHReaderDecl.cpp

Modified: cfe/trunk/include/clang/AST/ExternalASTSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExternalASTSource.h?rev=109856&r1=109855&r2=109856&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExternalASTSource.h (original)
+++ cfe/trunk/include/clang/AST/ExternalASTSource.h Fri Jul 30 05:03:16 2010
@@ -58,6 +58,20 @@
 
   virtual ~ExternalASTSource();
 
+  /// \brief RAII class for safely pairing a StartedDeserializing call
+  /// with FinishedDeserializing.
+  class Deserializing {
+    ExternalASTSource *Source;
+  public:
+    explicit Deserializing(ExternalASTSource *source) : Source(source) {
+      assert(Source);
+      Source->StartedDeserializing();
+    }
+    ~Deserializing() {
+      Source->FinishedDeserializing();
+    }
+  };
+
   /// \brief Resolve a declaration ID into a declaration, potentially
   /// building a new declaration.
   ///
@@ -100,6 +114,19 @@
   virtual bool FindExternalLexicalDecls(const DeclContext *DC,
                                 llvm::SmallVectorImpl<Decl*> &Result) = 0;
 
+  /// \brief Notify ExternalASTSource that we started deserialization of
+  /// a decl or type so until FinishedDeserializing is called there may be
+  /// decls that are initializing. Must be paired with FinishedDeserializing.
+  ///
+  /// The default implementation of this method is a no-op.
+  virtual void StartedDeserializing() { }
+
+  /// \brief Notify ExternalASTSource that we finished the deserialization of
+  /// a decl or type. Must be paired with StartedDeserializing.
+  ///
+  /// The default implementation of this method is a no-op.
+  virtual void FinishedDeserializing() { }
+
   /// \brief Function that will be invoked when we begin parsing a new
   /// translation unit involving this external AST source.
   ///

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=109856&r1=109855&r2=109856&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Fri Jul 30 05:03:16 2010
@@ -468,26 +468,9 @@
 
   /// Number of visible decl contexts read/total.
   unsigned NumVisibleDeclContextsRead, TotalVisibleDeclContexts;
-
-  /// \brief When a type or declaration is being loaded from the PCH file, an
-  /// instantance of this RAII object will be available on the stack to
-  /// indicate when we are in a recursive-loading situation.
-  class LoadingTypeOrDecl {
-    PCHReader &Reader;
-    LoadingTypeOrDecl *Parent;
-
-    LoadingTypeOrDecl(const LoadingTypeOrDecl&); // do not implement
-    LoadingTypeOrDecl &operator=(const LoadingTypeOrDecl&); // do not implement
-
-  public:
-    explicit LoadingTypeOrDecl(PCHReader &Reader);
-    ~LoadingTypeOrDecl();
-  };
-  friend class LoadingTypeOrDecl;
-
-  /// \brief If we are currently loading a type or declaration, points to the
-  /// most recent LoadingTypeOrDecl object on the stack.
-  LoadingTypeOrDecl *CurrentlyLoadingTypeOrDecl;
+  
+  /// \brief Number of Decl/types that are currently deserializing.
+  unsigned NumCurrentElementsDeserializing;
 
   /// \brief An IdentifierInfo that has been loaded but whose top-level
   /// declarations of the same name have not (yet) been loaded.
@@ -757,6 +740,15 @@
   virtual bool FindExternalLexicalDecls(const DeclContext *DC,
                                         llvm::SmallVectorImpl<Decl*> &Decls);
 
+  /// \brief Notify PCHReader that we started deserialization of
+  /// a decl or type so until FinishedDeserializing is called there may be
+  /// decls that are initializing. Must be paired with FinishedDeserializing.
+  virtual void StartedDeserializing() { ++NumCurrentElementsDeserializing; }
+
+  /// \brief Notify PCHReader that we finished the deserialization of
+  /// a decl or type. Must be paired with StartedDeserializing.
+  virtual void FinishedDeserializing();
+
   /// \brief Function that will be invoked when we begin parsing a new
   /// translation unit involving this external AST source.
   ///

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=109856&r1=109855&r2=109856&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Jul 30 05:03:16 2010
@@ -426,7 +426,7 @@
     TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0),
     NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0),
     TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
-    TotalVisibleDeclContexts(0), CurrentlyLoadingTypeOrDecl(0) {
+    TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
   RelocatablePCH = false;
 }
 
@@ -443,7 +443,7 @@
     TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0),
     NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0),
     TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
-    TotalVisibleDeclContexts(0), CurrentlyLoadingTypeOrDecl(0) {
+    TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
   RelocatablePCH = false;
 }
 
@@ -2242,7 +2242,7 @@
   ReadingKindTracker ReadingKind(Read_Type, *this);
   
   // Note that we are loading a type record.
-  LoadingTypeOrDecl Loading(*this);
+  Deserializing AType(this);
 
   DeclsCursor.JumpToBit(Loc.second);
   RecordData Record;
@@ -3235,7 +3235,7 @@
 PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II,
                               const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
                                    bool Nonrecursive) {
-  if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) {
+  if (NumCurrentElementsDeserializing && !Nonrecursive) {
     PendingIdentifierInfos.push_back(PendingIdentifierInfo());
     PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
     PII.II = II;
@@ -3677,28 +3677,22 @@
   }
 }
 
-
-PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader)
-  : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) {
-  Reader.CurrentlyLoadingTypeOrDecl = this;
-}
-
-PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() {
-  if (!Parent) {
+void PCHReader::FinishedDeserializing() {
+  assert(NumCurrentElementsDeserializing &&
+         "FinishedDeserializing not paired with StartedDeserializing");
+  if (NumCurrentElementsDeserializing == 1) {
     // If any identifiers with corresponding top-level declarations have
     // been loaded, load those declarations now.
-    while (!Reader.PendingIdentifierInfos.empty()) {
-      Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II,
-                                 Reader.PendingIdentifierInfos.front().DeclIDs,
-                                     true);
-      Reader.PendingIdentifierInfos.pop_front();
+    while (!PendingIdentifierInfos.empty()) {
+      SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
+                              PendingIdentifierInfos.front().DeclIDs, true);
+      PendingIdentifierInfos.pop_front();
     }
 
     // We are not in recursive loading, so it's safe to pass the "interesting"
     // decls to the consumer.
-    if (Reader.Consumer)
-      Reader.PassInterestingDeclsToConsumer();
+    if (Consumer)
+      PassInterestingDeclsToConsumer();
   }
-
-  Reader.CurrentlyLoadingTypeOrDecl = Parent;
+  --NumCurrentElementsDeserializing;
 }

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=109856&r1=109855&r2=109856&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Fri Jul 30 05:03:16 2010
@@ -1314,7 +1314,7 @@
   ReadingKindTracker ReadingKind(Read_Decl, *this);
 
   // Note that we are loading a declaration record.
-  LoadingTypeOrDecl Loading(*this);
+  Deserializing ADecl(this);
 
   DeclsCursor.JumpToBit(Loc.second);
   RecordData Record;





More information about the cfe-commits mailing list