clang  3.9.0
GeneratePCH.cpp
Go to the documentation of this file.
1 //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PCHGenerator, which as a SemaConsumer that generates
11 // a PCH file.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
19 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/Bitcode/BitstreamWriter.h"
22 #include <string>
23 
24 using namespace clang;
25 
27  const Preprocessor &PP, StringRef OutputFile,
28  clang::Module *Module, StringRef isysroot,
29  std::shared_ptr<PCHBuffer> Buffer,
31  bool AllowASTWithErrors, bool IncludeTimestamps)
32  : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
33  SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
34  Writer(Stream, Extensions, IncludeTimestamps),
35  AllowASTWithErrors(AllowASTWithErrors) {
36  Buffer->IsComplete = false;
37 }
38 
40 }
41 
43  // Don't create a PCH if there were fatal failures during module loading.
45  return;
46 
47  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
48  if (hasErrors && !AllowASTWithErrors)
49  return;
50 
51  // Emit the PCH file to the Buffer.
52  assert(SemaPtr && "No Sema?");
53  Buffer->Signature =
54  Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
55  // For serialization we are lenient if the errors were
56  // only warn-as-error kind.
58 
59  Buffer->IsComplete = true;
60 }
61 
63  return &Writer;
64 }
65 
67  return &Writer;
68 }
Defines the clang::ASTContext interface.
Defines the clang::FileManager interface and associated types.
bool hasErrorOccurred() const
Definition: Diagnostic.h:580
std::unique_ptr< llvm::MemoryBuffer > Buffer
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:42
~PCHGenerator() override
Definition: GeneratePCH.cpp:39
Describes a module or submodule.
Definition: Basic/Module.h:47
PCHGenerator(const Preprocessor &PP, StringRef OutputFile, clang::Module *Module, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, ArrayRef< llvm::IntrusiveRefCntPtr< ModuleFileExtension >> Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true)
Definition: GeneratePCH.cpp:26
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:66
Defines the clang::Preprocessor interface.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
Definition: Preprocessor.h:716
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:687
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation, it should return a pointer to an ASTMutationListener here.
Definition: GeneratePCH.cpp:62
uint64_t WriteAST(Sema &SemaRef, const std::string &OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4113
bool hasUncompilableErrorOccurred() const
Errors that actually prevent compilation, not those that are upgraded from a warning by -Werror...
Definition: Diagnostic.h:584
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:97