The attached patch fixes a small difference between gcc and clang when
building .d files. When GCC is passed a "-include foo/bar.h" flag, it will
emit "foo/bar.h" verbatim to the .d file. Clang takes the absolute path,
which isn't helpful when the .d file is supposed to be relocatable.

This is PR8974. Please review!

Nick
Index: include/clang/Frontend/Utils.h
===================================================================
--- include/clang/Frontend/Utils.h	(revision 123487)
+++ include/clang/Frontend/Utils.h	(working copy)
@@ -28,6 +28,7 @@
 class DependencyOutputOptions;
 class Diagnostic;
 class DiagnosticOptions;
+class FileManager;
 class HeaderSearch;
 class HeaderSearchOptions;
 class IdentifierTable;
@@ -42,7 +43,8 @@
 
 /// Normalize \arg File for use in a user defined #include directive (in the
 /// predefines buffer).
-std::string NormalizeDashIncludePath(llvm::StringRef File);
+std::string NormalizeDashIncludePath(llvm::StringRef File,
+                                     FileManager &FileMgr);
 
 /// Apply the header search options to get given HeaderSearch object.
 void ApplyHeaderSearchOptions(HeaderSearch &HS,
Index: include/clang/Serialization/ASTReader.h
===================================================================
--- include/clang/Serialization/ASTReader.h	(revision 123487)
+++ include/clang/Serialization/ASTReader.h	(working copy)
@@ -115,7 +115,8 @@
   /// \returns true to indicate the predefines are invalid or false otherwise.
   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
                                     llvm::StringRef OriginalFileName,
-                                    std::string &SuggestedPredefines) {
+                                    std::string &SuggestedPredefines,
+                                    FileManager &FileMgr) {
     return false;
   }
 
@@ -142,7 +143,8 @@
   virtual bool ReadTargetTriple(llvm::StringRef Triple);
   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
                                     llvm::StringRef OriginalFileName,
-                                    std::string &SuggestedPredefines);
+                                    std::string &SuggestedPredefines,
+                                    FileManager &FileMgr);
   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID);
   virtual void ReadCounter(unsigned Value);
 
Index: lib/Frontend/InitPreprocessor.cpp
===================================================================
--- lib/Frontend/InitPreprocessor.cpp	(revision 123487)
+++ lib/Frontend/InitPreprocessor.cpp	(working copy)
@@ -48,12 +48,13 @@
   }
 }
 
-std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
+std::string clang::NormalizeDashIncludePath(llvm::StringRef File,
+                                            FileManager &FileMgr) {
   // Implicit include paths should be resolved relative to the current
   // working directory first, and then use the regular header search
   // mechanism. The proper way to handle this is to have the
   // predefines buffer located at the current working directory, but
-  // it has not file entry. For now, workaround this by using an
+  // it has no file entry. For now, workaround this by using an
   // absolute path if we find the file here, and otherwise letting
   // header search handle it.
   llvm::SmallString<128> Path(File);
@@ -61,21 +62,25 @@
   bool exists;
   if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
     Path = File;
+  else if (exists)
+    FileMgr.getFile(File);
 
   return Lexer::Stringify(Path.str());
 }
 
 /// AddImplicitInclude - Add an implicit #include of the specified file to the
 /// predefines buffer.
-static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
+static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File,
+                               FileManager &FileMgr) {
   Builder.append("#include \"" +
-                 llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
+                 llvm::Twine(NormalizeDashIncludePath(File, FileMgr)) + "\"");
 }
 
 static void AddImplicitIncludeMacros(MacroBuilder &Builder,
-                                     llvm::StringRef File) {
+                                     llvm::StringRef File,
+                                     FileManager &FileMgr) {
   Builder.append("#__include_macros \"" +
-                 llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
+                 llvm::Twine(NormalizeDashIncludePath(File, FileMgr)) + "\"");
   // Marker token to stop the __include_macros fetch loop.
   Builder.append("##"); // ##?
 }
@@ -94,7 +99,7 @@
     return;
   }
 
-  AddImplicitInclude(Builder, OriginalFile);
+  AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
 }
 
 /// PickFP - This is used to pick a value based on the FP semantics of the
@@ -590,7 +595,8 @@
   // If -imacros are specified, include them now.  These are processed before
   // any -include directives.
   for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
-    AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
+    AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
+                             PP.getFileManager());
 
   // Process -include directives.
   for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
@@ -598,7 +604,7 @@
     if (Path == InitOpts.ImplicitPTHInclude)
       AddImplicitIncludePTH(Builder, PP, Path);
     else
-      AddImplicitInclude(Builder, Path);
+      AddImplicitInclude(Builder, Path, PP.getFileManager());
   }
 
   // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
Index: lib/Frontend/ASTUnit.cpp
===================================================================
--- lib/Frontend/ASTUnit.cpp	(revision 123487)
+++ lib/Frontend/ASTUnit.cpp	(working copy)
@@ -383,7 +383,8 @@
 
   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
                                     llvm::StringRef OriginalFileName,
-                                    std::string &SuggestedPredefines) {
+                                    std::string &SuggestedPredefines,
+                                    FileManager &FileMgr) {
     Predefines = Buffers[0].Data;
     for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
       Predefines += Buffers[I].Data;
Index: lib/Serialization/ASTReader.cpp
===================================================================
--- lib/Serialization/ASTReader.cpp	(revision 123487)
+++ lib/Serialization/ASTReader.cpp	(working copy)
@@ -238,14 +238,15 @@
 
 bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
                                         llvm::StringRef OriginalFileName,
-                                        std::string &SuggestedPredefines) {
+                                        std::string &SuggestedPredefines,
+                                        FileManager &FileMgr) {
   // We are in the context of an implicit include, so the predefines buffer will
   // have a #include entry for the PCH file itself (as normalized by the
   // preprocessor initialization). Find it and skip over it in the checking
   // below.
   llvm::SmallString<256> PCHInclude;
   PCHInclude += "#include \"";
-  PCHInclude += NormalizeDashIncludePath(OriginalFileName);
+  PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
   PCHInclude += "\"\n";
   std::pair<llvm::StringRef,llvm::StringRef> Split =
     llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
@@ -955,7 +956,8 @@
   if (Listener)
     return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
                                           ActualOriginalFileName,
-                                          SuggestedPredefines);
+                                          SuggestedPredefines,
+                                          FileMgr);
   return false;
 }
 
@@ -4711,4 +4713,3 @@
   delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
   delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
 }
-
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to