[cfe-commits] r109292 - in /cfe/trunk: include/clang/Frontend/PCHDeserializationListener.h include/clang/Frontend/PCHReader.h include/clang/Frontend/PCHWriter.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp lib/Frontend/PCHWriterDecl.cpp test/PCH/Inputs/chain-function1.h test/PCH/Inputs/chain-function2.h test/PCH/chain-function.c

Sebastian Redl sebastian.redl at getdesigned.at
Fri Jul 23 16:49:55 PDT 2010


Author: cornedbee
Date: Fri Jul 23 18:49:55 2010
New Revision: 109292

URL: http://llvm.org/viewvc/llvm-project?rev=109292&view=rev
Log:
Make declarations in the dependent PCH visible, for C at least.

Added:
    cfe/trunk/test/PCH/Inputs/chain-function1.h
    cfe/trunk/test/PCH/Inputs/chain-function2.h
    cfe/trunk/test/PCH/chain-function.c
Modified:
    cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Frontend/PCHWriterDecl.cpp

Modified: cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHDeserializationListener.h Fri Jul 23 18:49:55 2010
@@ -27,6 +27,8 @@
   virtual ~PCHDeserializationListener() {}
 
 public:
+  /// \brief An identifier was deserialized from the PCH.
+  virtual void IdentifierRead(pch::IdentID ID, IdentifierInfo *II) = 0;
   /// \brief A type was deserialized from the PCH. The ID here has the qualifier
   ///        bits already removed, and T is guaranteed to be locally unqualified
   virtual void TypeRead(pch::TypeID ID, QualType T) = 0;

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Fri Jul 23 18:49:55 2010
@@ -655,11 +655,16 @@
   /// \brief Read preprocessed entities into the 
   virtual void ReadPreprocessedEntities();
 
-  /// \brief Returns the number of source locations found in this file.
+  /// \brief Returns the number of source locations found in the chain.
   unsigned getTotalNumSLocs() const {
     return TotalNumSLocEntries;
   }
 
+  /// \brief Returns the number of identifiers found in the chain.
+  unsigned getTotalNumIdentifiers() const {
+    return static_cast<unsigned>(IdentifiersLoaded.size());
+  }
+
   /// \brief Returns the number of types found in this file.
   unsigned getTotalNumTypes() const {
     return static_cast<unsigned>(TypesLoaded.size());

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Fri Jul 23 18:49:55 2010
@@ -113,6 +113,9 @@
   /// \brief The first ID number we can use for our own declarations.
   pch::DeclID FirstDeclID;
 
+  /// \brief The decl ID that will be assigned to the next new decl.
+  pch::DeclID NextDeclID;
+
   /// \brief Map that provides the ID numbers of each declaration within
   /// the output stream, as well as those deserialized from a chained PCH.
   ///
@@ -128,6 +131,9 @@
   /// \brief The first ID number we can use for our own types.
   pch::TypeID FirstTypeID;
 
+  /// \brief The type ID that will be assigned to the next new type.
+  pch::TypeID NextTypeID;
+
   /// \brief Map that provides the ID numbers of each type within the
   /// output stream, plus those deserialized from a chained PCH.
   ///
@@ -143,8 +149,11 @@
   /// the type's ID.
   std::vector<uint32_t> TypeOffsets;
 
-  /// \brief The type ID that will be assigned to the next new type.
-  pch::TypeID NextTypeID;
+  /// \brief The first ID number we can use for our own identifiers.
+  pch::IdentID FirstIdentID;
+
+  /// \brief The identifier ID that will be assigned to the next new identifier.
+  pch::IdentID NextIdentID;
 
   /// \brief Map that provides the ID numbers of each identifier in
   /// the output stream.
@@ -405,7 +414,10 @@
 
   unsigned getParmVarDeclAbbrev() const { return ParmVarDeclAbbrev; }
 
+  bool hasChain() const { return Chain; }
+
   // PCHDeserializationListener implementation
+  void IdentifierRead(pch::IdentID ID, IdentifierInfo *II);
   void TypeRead(pch::TypeID ID, QualType T);
   void DeclRead(pch::DeclID ID, const Decl *D);
 };

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Jul 23 18:49:55 2010
@@ -3193,6 +3193,8 @@
   assert(ID && "Non-zero identifier ID required");
   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
   IdentifiersLoaded[ID - 1] = II;
+  if (DeserializationListener)
+    DeserializationListener->IdentifierRead(ID, II);
 }
 
 /// \brief Set the globally-visible declarations associated with the given
@@ -3277,6 +3279,8 @@
                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
     IdentifiersLoaded[ID]
       = &PP->getIdentifierTable().get(Str, StrLen);
+    if (DeserializationListener)
+      DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
   }
 
   return IdentifiersLoaded[ID];

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Fri Jul 23 18:49:55 2010
@@ -1851,12 +1851,14 @@
     // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
     // adds declarations to the end of the list (so we need to see the
     // struct "status" before the function "status").
+    // Only emit declarations that aren't from a chained PCH, though.
     llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
                                         IdentifierResolver::end());
     for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
                                                       DEnd = Decls.rend();
          D != DEnd; ++D)
-      clang::io::Emit32(Out, Writer.getDeclID(*D));
+      if (!Writer.hasChain() || (*D)->getPCHLevel() == 0)
+        clang::io::Emit32(Out, Writer.getDeclID(*D));
   }
 };
 } // end anonymous namespace
@@ -1884,13 +1886,17 @@
          ID != IDEnd; ++ID)
       getIdentifierRef(ID->second);
 
-    // Create the on-disk hash table representation.
-    IdentifierOffsets.resize(IdentifierIDs.size());
+    // Create the on-disk hash table representation. We only store offsets
+    // for identifiers that appear here for the first time.
+    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
     for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
            ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
          ID != IDEnd; ++ID) {
       assert(ID->first && "NULL identifier in identifier table");
-      Generator.insert(ID->first, ID->second);
+      // FIXME: Right now, we only write identifiers that are new to this file.
+      // We need to write older identifiers that changed too, though.
+      if (ID->second >= FirstIdentID)
+        Generator.insert(ID->first, ID->second);
     }
 
     // Create the on-disk hash table in a buffer.
@@ -2116,7 +2122,11 @@
 /// \brief Note that the identifier II occurs at the given offset
 /// within the identifier table.
 void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
-  IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
+  pch::IdentID ID = IdentifierIDs[II];
+  // Only store offsets new to this PCH file. Other identifier names are looked
+  // up earlier in the chain and thus don't need an offset.
+  if (ID >= FirstIdentID)
+    IdentifierOffsets[ID - FirstIdentID] = Offset;
 }
 
 /// \brief Note that the selector Sel occurs at the given offset
@@ -2129,15 +2139,18 @@
 
 PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain)
   : Stream(Stream), Chain(Chain), FirstDeclID(1),
-    FirstTypeID(pch::NUM_PREDEF_TYPE_IDS),
+    FirstTypeID(pch::NUM_PREDEF_TYPE_IDS), FirstIdentID(1),
     CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
     NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) {
   if (Chain) {
     Chain->setDeserializationListener(this);
     FirstDeclID += Chain->getTotalNumDecls();
     FirstTypeID += Chain->getTotalNumTypes();
+    FirstIdentID += Chain->getTotalNumIdentifiers();
   }
+  NextDeclID = FirstDeclID;
   NextTypeID = FirstTypeID;
+  NextIdentID = FirstIdentID;
 }
 
 void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
@@ -2165,6 +2178,7 @@
 
   // The translation unit is the first declaration we'll emit.
   DeclIDs[Context.getTranslationUnitDecl()] = 1;
+  ++NextDeclID;
   DeclTypesToEmit.push(Context.getTranslationUnitDecl());
 
   // Make sure that we emit IdentifierInfos (and any attached
@@ -2334,6 +2348,9 @@
   // We don't start with the translation unit, but with its decls that
   // don't come from the other PCH.
   const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
+  // The TU was loaded before we managed to register ourselves as a listener.
+  // Thus we need to add it manually.
+  DeclIDs[TU] = 1;
   // FIXME: We don't want to iterate over everything here, because it needlessly
   // deserializes the entire original PCH. Instead we only want to iterate over
   // the stuff that's already there.
@@ -2359,7 +2376,7 @@
 
   // FIXME: Preprocessor
   // FIXME: Method pool
-  // FIXME: Identifier table
+  WriteIdentifierTable(PP);
   WriteTypeDeclOffsets();
   // FIXME: External unnamed definitions
   // FIXME: Tentative definitions
@@ -2407,7 +2424,7 @@
 
   pch::IdentID &ID = IdentifierIDs[II];
   if (ID == 0)
-    ID = IdentifierIDs.size();
+    ID = NextIdentID++;
   return ID;
 }
 
@@ -2576,7 +2593,7 @@
   if (ID == 0) {
     // We haven't seen this declaration before. Give it a new ID and
     // enqueue it in the list of declarations to emit.
-    ID = DeclIDs.size();
+    ID = NextDeclID++;
     DeclTypesToEmit.push(const_cast<Decl *>(D));
   }
 
@@ -2777,6 +2794,10 @@
   AddSourceRange(Base.getSourceRange(), Record);
 }
 
+void PCHWriter::IdentifierRead(pch::IdentID ID, IdentifierInfo *II) {
+  IdentifierIDs[II] = ID;
+}
+
 void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
   TypeIDs[T] = ID;
 }

Modified: cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterDecl.cpp?rev=109292&r1=109291&r2=109292&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterDecl.cpp Fri Jul 23 18:49:55 2010
@@ -1144,9 +1144,9 @@
   // Determine the ID for this declaration
   pch::DeclID &ID = DeclIDs[D];
   if (ID == 0)
-    ID = DeclIDs.size();
+    ID = NextDeclID++;
 
-  unsigned Index = ID - 1;
+  unsigned Index = ID - FirstDeclID;
 
   // Record the offset for this declaration
   if (DeclOffsets.size() == Index)

Added: cfe/trunk/test/PCH/Inputs/chain-function1.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/Inputs/chain-function1.h?rev=109292&view=auto
==============================================================================
--- cfe/trunk/test/PCH/Inputs/chain-function1.h (added)
+++ cfe/trunk/test/PCH/Inputs/chain-function1.h Fri Jul 23 18:49:55 2010
@@ -0,0 +1 @@
+void f();

Added: cfe/trunk/test/PCH/Inputs/chain-function2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/Inputs/chain-function2.h?rev=109292&view=auto
==============================================================================
--- cfe/trunk/test/PCH/Inputs/chain-function2.h (added)
+++ cfe/trunk/test/PCH/Inputs/chain-function2.h Fri Jul 23 18:49:55 2010
@@ -0,0 +1 @@
+void g();

Added: cfe/trunk/test/PCH/chain-function.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/chain-function.c?rev=109292&view=auto
==============================================================================
--- cfe/trunk/test/PCH/chain-function.c (added)
+++ cfe/trunk/test/PCH/chain-function.c Fri Jul 23 18:49:55 2010
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -emit-pch -o %t1 %S/Inputs/chain-function1.h
+// RUN: %clang_cc1 -emit-pch -o %t2 %S/Inputs/chain-function2.h -include-pch %t1 -chained-pch
+// RUN: %clang_cc1 -fsyntax-only -verify -include-pch %t2 %s
+
+void h() {
+  f();
+  g();
+}





More information about the cfe-commits mailing list