[cfe-commits] r110108 - in /cfe/trunk: include/clang/Frontend/ASTConsumers.h include/clang/Frontend/FrontendActions.h include/clang/Frontend/PCHWriter.h lib/Frontend/ASTUnit.cpp lib/Frontend/FrontendActions.cpp lib/Frontend/GeneratePCH.cpp

Douglas Gregor dgregor at apple.com
Tue Aug 3 01:14:03 PDT 2010


Author: dgregor
Date: Tue Aug  3 03:14:03 2010
New Revision: 110108

URL: http://llvm.org/viewvc/llvm-project?rev=110108&view=rev
Log:
Reshuffle the PCH generator action and consumer, so that we can re-use
it while generating precompiled preambles. No functionality change.

Modified:
    cfe/trunk/include/clang/Frontend/ASTConsumers.h
    cfe/trunk/include/clang/Frontend/FrontendActions.h
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/FrontendActions.cpp
    cfe/trunk/lib/Frontend/GeneratePCH.cpp

Modified: cfe/trunk/include/clang/Frontend/ASTConsumers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTConsumers.h?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTConsumers.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTConsumers.h Tue Aug  3 03:14:03 2010
@@ -57,14 +57,6 @@
 // to stderr; this is intended for debugging.
 ASTConsumer *CreateDeclContextPrinter();
 
-// PCH generator: generates a precompiled header file; this file can be used
-// later with the PCHReader (clang -cc1 option -include-pch) to speed up compile
-// times.
-ASTConsumer *CreatePCHGenerator(const Preprocessor &PP,
-                                llvm::raw_ostream *OS,
-                                bool Chaining,
-                                const char *isysroot = 0);
-
 // Inheritance viewer: for C++ code, creates a graph of the inheritance
 // tree for the given class and displays it with "dotty".
 ASTConsumer *CreateInheritanceViewer(const std::string& clsname);

Modified: cfe/trunk/include/clang/Frontend/FrontendActions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/FrontendActions.h?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/FrontendActions.h (original)
+++ cfe/trunk/include/clang/Frontend/FrontendActions.h Tue Aug  3 03:14:03 2010
@@ -74,6 +74,17 @@
   virtual bool usesCompleteTranslationUnit() { return false; }
 
   virtual bool hasASTFileSupport() const { return false; }
+
+public:
+  /// \brief Compute the AST consumer arguments that will be used to
+  /// create the PCHGenerator instance returned by CreateASTConsumer.
+  ///
+  /// \returns true if an error occurred, false otherwise.
+  static bool ComputeASTConsumerArguments(CompilerInstance &CI,
+                                          llvm::StringRef InFile,
+                                          std::string &Sysroot,
+                                          llvm::raw_ostream *&OS,
+                                          bool &Chaining);
 };
 
 class InheritanceViewAction : public ASTFrontendAction {

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Tue Aug  3 03:14:03 2010
@@ -20,10 +20,13 @@
 #include "clang/AST/TemplateBase.h"
 #include "clang/Frontend/PCHBitCodes.h"
 #include "clang/Frontend/PCHDeserializationListener.h"
+#include "clang/Sema/SemaConsumer.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Bitcode/BitstreamWriter.h"
 #include <map>
 #include <queue>
+#include <vector>
 
 namespace llvm {
   class APFloat;
@@ -427,6 +430,26 @@
   void DeclRead(pch::DeclID ID, const Decl *D);
 };
 
+/// \brief AST and semantic-analysis consumer that generates a
+/// precompiled header from the parsed source code.
+class PCHGenerator : public SemaConsumer {
+  const Preprocessor &PP;
+  const char *isysroot;
+  llvm::raw_ostream *Out;
+  Sema *SemaPtr;
+  MemorizeStatCalls *StatCalls; // owned by the FileManager
+  std::vector<unsigned char> Buffer;
+  llvm::BitstreamWriter Stream;
+  PCHWriter Writer;
+
+public:
+  PCHGenerator(const Preprocessor &PP, bool Chaining,
+               const char *isysroot, llvm::raw_ostream *Out);
+  virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
+  virtual void HandleTranslationUnit(ASTContext &Ctx);
+  virtual PCHDeserializationListener *GetPCHDeserializationListener();
+};
+
 } // end namespace clang
 
 #endif

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Tue Aug  3 03:14:03 2010
@@ -12,7 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Frontend/ASTUnit.h"
-#include "clang/Frontend/PCHReader.h"
+#include "clang/Frontend/PCHWriter.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/DeclVisitor.h"
@@ -25,6 +25,7 @@
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Frontend/FrontendOptions.h"
+#include "clang/Frontend/PCHReader.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Basic/TargetOptions.h"
@@ -328,6 +329,54 @@
   virtual bool hasCodeCompletionSupport() const { return false; }
 };
 
+class PrecompilePreambleConsumer : public PCHGenerator {
+  ASTUnit &Unit;
+
+public:
+  PrecompilePreambleConsumer(ASTUnit &Unit,
+                             const Preprocessor &PP, bool Chaining,
+                             const char *isysroot, llvm::raw_ostream *Out)
+    : PCHGenerator(PP, Chaining, isysroot, Out), Unit(Unit) { }
+
+  void HandleTopLevelDecl(DeclGroupRef D) {
+    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
+      Decl *D = *it;
+      // FIXME: Currently ObjC method declarations are incorrectly being
+      // reported as top-level declarations, even though their DeclContext
+      // is the containing ObjC @interface/@implementation.  This is a
+      // fundamental problem in the parser right now.
+      if (isa<ObjCMethodDecl>(D))
+        continue;
+      Unit.getTopLevelDecls().push_back(D);
+    }
+  }
+};
+
+class PrecompilePreambleAction : public ASTFrontendAction {
+  ASTUnit &Unit;
+
+public:
+  explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
+
+  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
+                                         llvm::StringRef InFile) {
+    std::string Sysroot;
+    llvm::raw_ostream *OS = 0;
+    bool Chaining;
+    if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, 
+                                                       OS, Chaining))
+      return 0;
+    
+    const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
+                             Sysroot.c_str() : 0;  
+    return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
+                                          isysroot, OS);
+  }
+
+  virtual bool hasCodeCompletionSupport() const { return false; }
+  virtual bool hasASTFileSupport() const { return false; }
+};
+
 }
 
 /// Parse the source file into a translation unit using the given compiler
@@ -819,8 +868,8 @@
   Clang.setSourceManager(new SourceManager(getDiagnostics()));
   
   // FIXME: Eventually, we'll have to track top-level declarations here, too.
-  llvm::OwningPtr<GeneratePCHAction> Act;
-  Act.reset(new GeneratePCHAction);
+  llvm::OwningPtr<PrecompilePreambleAction> Act;
+  Act.reset(new PrecompilePreambleAction(*this));
   if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
                             Clang.getFrontendOpts().Inputs[0].first)) {
     Clang.takeDiagnosticClient();

Modified: cfe/trunk/lib/Frontend/FrontendActions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/FrontendActions.cpp?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/FrontendActions.cpp (original)
+++ cfe/trunk/lib/Frontend/FrontendActions.cpp Tue Aug  3 03:14:03 2010
@@ -17,6 +17,7 @@
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Frontend/PCHWriter.h"
 #include "clang/Frontend/Utils.h"
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -70,22 +71,35 @@
 
 ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
                                                   llvm::StringRef InFile) {
-  const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
-  if (CI.getFrontendOpts().RelocatablePCH &&
-      Sysroot.empty()) {
-    CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
+  std::string Sysroot;
+  llvm::raw_ostream *OS = 0;
+  bool Chaining;
+  if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OS, Chaining))
     return 0;
+
+  const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
+                             Sysroot.c_str() : 0;  
+  return new PCHGenerator(CI.getPreprocessor(), Chaining, isysroot, OS);
+}
+
+bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
+                                                    llvm::StringRef InFile,
+                                                    std::string &Sysroot,
+                                                    llvm::raw_ostream *&OS,
+                                                    bool &Chaining) {
+  Sysroot = CI.getHeaderSearchOpts().Sysroot;
+  if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
+    CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
+    return true;
   }
 
-  llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
+  OS = CI.createDefaultOutputFile(true, InFile);
   if (!OS)
-    return 0;
+    return true;
 
-  bool Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
-                  !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
-  const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
-                             Sysroot.c_str() : 0;
-  return CreatePCHGenerator(CI.getPreprocessor(), OS, Chaining, isysroot);
+  Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
+             !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
+  return false;
 }
 
 ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,

Modified: cfe/trunk/lib/Frontend/GeneratePCH.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/GeneratePCH.cpp?rev=110108&r1=110107&r2=110108&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/GeneratePCH.cpp (original)
+++ cfe/trunk/lib/Frontend/GeneratePCH.cpp Tue Aug  3 03:14:03 2010
@@ -25,26 +25,6 @@
 
 using namespace clang;
 
-namespace {
-  class PCHGenerator : public SemaConsumer {
-    const Preprocessor &PP;
-    const char *isysroot;
-    llvm::raw_ostream *Out;
-    Sema *SemaPtr;
-    MemorizeStatCalls *StatCalls; // owned by the FileManager
-    std::vector<unsigned char> Buffer;
-    llvm::BitstreamWriter Stream;
-    PCHWriter Writer;
-
-  public:
-    PCHGenerator(const Preprocessor &PP, bool Chaining,
-                 const char *isysroot, llvm::raw_ostream *Out);
-    virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
-    virtual void HandleTranslationUnit(ASTContext &Ctx);
-    virtual PCHDeserializationListener *GetPCHDeserializationListener();
-  };
-}
-
 PCHGenerator::PCHGenerator(const Preprocessor &PP,
                            bool Chaining,
                            const char *isysroot,
@@ -82,10 +62,3 @@
 PCHDeserializationListener *PCHGenerator::GetPCHDeserializationListener() {
   return &Writer;
 }
-
-ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
-                                       llvm::raw_ostream *OS,
-                                       bool Chaining,
-                                       const char *isysroot) {
-  return new PCHGenerator(PP, Chaining, isysroot, OS);
-}





More information about the cfe-commits mailing list