]> granicus.if.org Git - clang/commitdiff
Add CompilerInstance::createPCHExternalASTSource.
authorDaniel Dunbar <daniel@zuster.org>
Fri, 13 Nov 2009 08:21:10 +0000 (08:21 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 13 Nov 2009 08:21:10 +0000 (08:21 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@87097 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/CompilerInstance.h
lib/Frontend/CompilerInstance.cpp
tools/clang-cc/clang-cc.cpp

index 8c638206b6dad7d44314cdf7343eb5bf3416d159..2e19822fa0e7682ea40c3f7afbb1ba10de11c12a 100644 (file)
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_FRONTEND_COMPILERINSTANCE_H_
 
 #include "clang/Frontend/CompilerInvocation.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/OwningPtr.h"
 #include <cassert>
 
@@ -22,8 +23,10 @@ namespace clang {
 class ASTContext;
 class Diagnostic;
 class DiagnosticClient;
-class Preprocessor;
+class ExternalASTSource;
 class FileManager;
+class Preprocessor;
+class Source;
 class SourceManager;
 class TargetInfo;
 
@@ -349,6 +352,17 @@ public:
   /// Create the AST context.
   void createASTContext();
 
+  /// Create an external AST source to read a PCH file and attach it to the AST
+  /// context.
+  void createPCHExternalASTSource(llvm::StringRef Path);
+
+  /// Create an external AST source to read a PCH file.
+  ///
+  /// \return - The new object on success, or null on failure.
+  static ExternalASTSource *
+  createPCHExternalASTSource(llvm::StringRef Path, const std::string &Sysroot,
+                             Preprocessor &PP, ASTContext &Context);
+
   /// }
 };
 
index 0829ce32cc3c8e8aabfda976fd9ed6f3bda9423b..e7c3611f0fd1118d9f76670ea418c6699d0090ba 100644 (file)
@@ -17,6 +17,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/PTHManager.h"
 #include "clang/Frontend/ChainedDiagnosticClient.h"
+#include "clang/Frontend/PCHReader.h"
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/Utils.h"
@@ -164,3 +165,40 @@ void CompilerInstance::createASTContext() {
                                /*FreeMemory=*/ !getFrontendOpts().DisableFree,
                                /*size_reserve=*/ 0));
 }
+
+// ExternalASTSource
+
+void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) {
+  llvm::OwningPtr<ExternalASTSource> Source;
+  Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
+                                          getPreprocessor(), getASTContext()));
+  getASTContext().setExternalSource(Source);
+}
+
+ExternalASTSource *
+CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path,
+                                             const std::string &Sysroot,
+                                             Preprocessor &PP,
+                                             ASTContext &Context) {
+  llvm::OwningPtr<PCHReader> Reader;
+  Reader.reset(new PCHReader(PP, &Context,
+                             Sysroot.empty() ? 0 : Sysroot.c_str()));
+
+  switch (Reader->ReadPCH(Path)) {
+  case PCHReader::Success:
+    // Set the predefines buffer as suggested by the PCH reader. Typically, the
+    // predefines buffer will be empty.
+    PP.setPredefines(Reader->getSuggestedPredefines());
+    return Reader.take();
+
+  case PCHReader::Failure:
+    // Unrecoverable failure: don't even try to process the input file.
+    break;
+
+  case PCHReader::IgnorePCH:
+    // No suitable PCH file could be found. Return an error.
+    break;
+  }
+
+  return 0;
+}
index 764db6f503d5cb4f496c4b1987bb95c611c1af0d..b186f20ed53c8d5296ff592075dc140edb9f93a8 100644 (file)
@@ -33,7 +33,6 @@
 #include "clang/Frontend/DependencyOutputOptions.h"
 #include "clang/Frontend/FixItRewriter.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
-#include "clang/Frontend/PCHReader.h"
 #include "clang/Frontend/PathDiagnosticClients.h"
 #include "clang/Frontend/PreprocessorOptions.h"
 #include "clang/Frontend/PreprocessorOutputOptions.h"
@@ -388,41 +387,6 @@ static ASTConsumer *CreateConsumerAction(CompilerInstance &CI,
   }
 }
 
-/// ReadPCHFile - Load a PCH file from disk, and initialize the preprocessor for
-/// reading from the PCH file.
-///
-/// \return The AST source, or null on failure.
-static ExternalASTSource *ReadPCHFile(llvm::StringRef Path,
-                                      const std::string Sysroot,
-                                      Preprocessor &PP,
-                                      ASTContext &Context) {
-  // If the user specified -isysroot, it will be used for relocatable PCH files.
-  const char *isysrootPCH = Sysroot.c_str();
-  if (isysrootPCH[0] == '\0')
-    isysrootPCH = 0;
-
-  llvm::OwningPtr<PCHReader> Reader;
-  Reader.reset(new PCHReader(PP, &Context, isysrootPCH));
-
-  switch (Reader->ReadPCH(Path)) {
-  case PCHReader::Success:
-    // Set the predefines buffer as suggested by the PCH reader. Typically, the
-    // predefines buffer will be empty.
-    PP.setPredefines(Reader->getSuggestedPredefines());
-    return Reader.take();
-
-  case PCHReader::Failure:
-    // Unrecoverable failure: don't even try to process the input file.
-    break;
-
-  case PCHReader::IgnorePCH:
-    // No suitable PCH file could be found. Return an error.
-    break;
-  }
-
-  return 0;
-}
-
 /// ProcessInputFile - Process a single input file with the specified state.
 static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile,
                              ProgActions PA) {
@@ -509,7 +473,6 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile,
     }
   }
 
-  llvm::OwningPtr<ExternalASTSource> Source;
   const std::string &ImplicitPCHInclude =
     CI.getPreprocessorOpts().getImplicitPCHInclude();
   if (Consumer) {
@@ -517,15 +480,9 @@ static void ProcessInputFile(CompilerInstance &CI, const std::string &InFile,
     CI.createASTContext();
 
     if (!ImplicitPCHInclude.empty()) {
-      Source.reset(ReadPCHFile(ImplicitPCHInclude,
-                               CI.getHeaderSearchOpts().Sysroot, PP,
-                               CI.getASTContext()));
-      if (!Source)
+      CI.createPCHExternalASTSource(ImplicitPCHInclude);
+      if (!CI.getASTContext().getExternalSource())
         return;
-
-      // Attach the PCH reader to the AST context as an external AST source, so
-      // that declarations will be deserialized from the PCH file as needed.
-      CI.getASTContext().setExternalSource(Source);
     } else {
       // Initialize builtin info when not using PCH.
       PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),