[cfe-commits] r86806 - in /cfe/trunk: include/clang/Frontend/PCHReader.h lib/Frontend/ASTUnit.cpp lib/Frontend/PCHReader.cpp tools/clang-cc/clang-cc.cpp

Daniel Dunbar daniel at zuster.org
Tue Nov 10 21:29:05 PST 2009


Author: ddunbar
Date: Tue Nov 10 23:29:04 2009
New Revision: 86806

URL: http://llvm.org/viewvc/llvm-project?rev=86806&view=rev
Log:
Redo how PCH handles its implicit include. Instead of treating this specially in
the front-end (as far as the preprocessor goes), follow the usual logic of
inserting the (original include path) name into the predefines buffer. This
pushes the responsibility for handling this to PCH instead of the front-end.  In
PCH this requires being a little more clever when we diff the predefines
buffers.

Neither of these solutions are particularly great, I think what we eventually
should do is something like gcc where we insert a special marker to indicate the
PCH file, but then run the preprocessor as usual. This would be clearer and
would allow us to drop the overly clever predefines handling.

Modified:
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/tools/clang-cc/clang-cc.cpp

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=86806&r1=86805&r2=86806&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Tue Nov 10 23:29:04 2009
@@ -88,17 +88,18 @@
   /// \param PCHPredef The start of the predefines buffer in the PCH
   /// file.
   ///
-  /// \param PCHPredefLen The length of the predefines buffer in the PCH
-  /// file.
-  ///
   /// \param PCHBufferID The FileID for the PCH predefines buffer.
   ///
+  /// \param OriginalFileName The original file name for the PCH, which will
+  /// appear as an entry in the predefines buffer.
+  ///
   /// \param SuggestedPredefines If necessary, additional definitions are added
   /// here.
   ///
   /// \returns true to indicate the predefines are invalid or false otherwise.
   virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
                                     FileID PCHBufferID,
+                                    llvm::StringRef OriginalFileName,
                                     std::string &SuggestedPredefines) {
     return false;
   }
@@ -126,6 +127,7 @@
   virtual bool ReadTargetTriple(llvm::StringRef Triple);
   virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
                                     FileID PCHBufferID,
+                                    llvm::StringRef OriginalFileName,
                                     std::string &SuggestedPredefines);
   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI);
   virtual void ReadCounter(unsigned Value);
@@ -311,10 +313,13 @@
   /// the PCH file.
   llvm::SmallVector<uint64_t, 4> ObjCCategoryImpls;
 
-  /// \brief The original file name that was used to build the PCH
-  /// file.
+  /// \brief The original file name that was used to build the PCH file, which
+  /// may have been modified for relocatable-pch support.
   std::string OriginalFileName;
 
+  /// \brief The actual original file name that was used to build the PCH file.
+  std::string ActualOriginalFileName;
+
   /// \brief Whether this precompiled header is a relocatable PCH file.
   bool RelocatablePCH;
 

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=86806&r1=86805&r2=86806&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Tue Nov 10 23:29:04 2009
@@ -68,6 +68,7 @@
 
   virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
                                     FileID PCHBufferID,
+                                    llvm::StringRef OriginalFileName,
                                     std::string &SuggestedPredefines) {
     Predefines = PCHPredef;
     return false;

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=86806&r1=86805&r2=86806&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Tue Nov 10 23:29:04 2009
@@ -156,17 +156,34 @@
 
 bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef,
                                         FileID PCHBufferID,
+                                        llvm::StringRef OriginalFileName,
                                         std::string &SuggestedPredefines) {
-  // If the two predefines buffers compare equal, we're done!
-  if (PP.getPredefines() == PCHPredef)
+  // We are in the context of an implicit include, so the predefines buffer
+  // will have a #include entry for the PCH file itself. Find it and skip over
+  // it in the checking below.
+  llvm::SmallString<256> PCHInclude;
+  PCHInclude += "#include \"";
+  PCHInclude += OriginalFileName;
+  PCHInclude += "\"\n";
+  std::pair<llvm::StringRef,llvm::StringRef> Split =
+    llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
+  llvm::StringRef Left =  Split.first, Right = Split.second;
+  assert(Left != PP.getPredefines() && "Missing PCH include entry!");
+
+  // If the predefines is equal to the joined left and right halves, we're done!
+  if (Left.size() + Right.size() == PCHPredef.size() &&
+      PCHPredef.startswith(Left) && PCHPredef.endswith(Right))
     return false;
 
   SourceManager &SourceMgr = PP.getSourceManager();
 
   // The predefines buffers are different. Determine what the differences are,
   // and whether they require us to reject the PCH file.
-  std::vector<llvm::StringRef> CmdLineLines = splitLines(PP.getPredefines());
   std::vector<llvm::StringRef> PCHLines = splitLines(PCHPredef);
+  std::vector<llvm::StringRef> CmdLineLines = splitLines(Left);
+  std::vector<llvm::StringRef> CmdLineLinesRight = splitLines(Right);
+  CmdLineLines.insert(CmdLineLines.end(),
+                      CmdLineLinesRight.begin(), CmdLineLinesRight.end());
 
   // Sort both sets of predefined buffer lines, since we allow some extra
   // definitions and they may appear at any point in the output.
@@ -624,6 +641,7 @@
                                       FileID PCHBufferID) {
   if (Listener)
     return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID,
+                                          ActualOriginalFileName,
                                           SuggestedPredefines);
   return false;
 }
@@ -1333,7 +1351,8 @@
       break;
 
     case pch::ORIGINAL_FILE_NAME:
-      OriginalFileName.assign(BlobStart, BlobLen);
+      ActualOriginalFileName.assign(BlobStart, BlobLen);
+      OriginalFileName = ActualOriginalFileName;
       MaybeAddSystemRootToFilename(OriginalFileName);
       break;
 

Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=86806&r1=86805&r2=86806&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
+++ cfe/trunk/tools/clang-cc/clang-cc.cpp Tue Nov 10 23:29:04 2009
@@ -1110,7 +1110,7 @@
     InitOpts.addMacroInclude(ImplicitMacroIncludes[i]);
 
   if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty() ||
-      (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput)) {
+      !ImplicitIncludePCH.empty()) {
     // We want to add these paths to the predefines buffer in order, make a
     // temporary vector to sort by their occurrence.
     llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
@@ -1118,7 +1118,7 @@
     if (!ImplicitIncludePTH.empty())
       OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
                                             &ImplicitIncludePTH));
-    if (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput)
+    if (!ImplicitIncludePCH.empty())
       OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
                                             &ImplicitIncludePCH));
     for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
@@ -1126,7 +1126,6 @@
                                             &ImplicitIncludes[i]));
     llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
 
-
     // Now that they are ordered by position, add to the predefines buffer.
     for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i) {
       std::string *Ptr = OrderedPaths[i].second;
@@ -1142,10 +1141,10 @@
         // file that was used to build the precompiled header.
         assert(Ptr == &ImplicitIncludePCH);
         std::string OriginalFile = PCHReader::getOriginalSourceFile(*Ptr);
-        if (!OriginalFile.empty()) {
-          InitOpts.addInclude(OriginalFile);
-          InitOpts.setImplicitPCHInclude("");
-        }
+        // FIXME: Don't fail like this.
+        if (OriginalFile.empty())
+          exit(1);
+        InitOpts.addInclude(OriginalFile);
       }
     }
   }





More information about the cfe-commits mailing list