Make precompiled headers work with -E. When we're only preprocessing
(with -E), we turn the PCH include into an implicit include of the
file from which the PCH file was generated.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71534 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index ca8d6e5..a948f84 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -1267,7 +1267,8 @@
   for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
     InitOpts.addMacroInclude(ImplicitMacroIncludes[i]);
 
-  if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty()) {
+  if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty() ||
+      (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput)) {
     // 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;
@@ -1275,6 +1276,9 @@
     if (!ImplicitIncludePTH.empty())
       OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
                                             &ImplicitIncludePTH));
+    if (!ImplicitIncludePCH.empty() && ProgAction == PrintPreprocessedInput)
+      OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
+                                            &ImplicitIncludePCH));
     for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
       OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
                                             &ImplicitIncludes[i]));
@@ -1288,9 +1292,18 @@
           Ptr >= &ImplicitIncludes[0] &&
           Ptr <= &ImplicitIncludes[ImplicitIncludes.size()-1]) {
         InitOpts.addInclude(*Ptr, false);
-      } else {
-        assert(Ptr == &ImplicitIncludePTH);
+      } else if (Ptr == &ImplicitIncludePTH) {
         InitOpts.addInclude(*Ptr, true);
+      } else {
+        // We end up here when we're producing preprocessed output and
+        // we loaded a PCH file. In this case, just include the header
+        // file that was used to build the precompiled header.
+        assert(Ptr == &ImplicitIncludePCH);
+        std::string OriginalFile = PCHReader::getOriginalSourceFile(*Ptr);
+        if (!OriginalFile.empty()) {
+          InitOpts.addInclude(OriginalFile, false);
+          ImplicitIncludePCH.clear();
+        }
       }
     }
   }