clang  6.0.0svn
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 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/Preprocessor.h"
20 #include "llvm/Bitcode/BitstreamWriter.h"
21 
22 using namespace clang;
23 
25  const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
26  std::shared_ptr<PCHBuffer> Buffer,
27  ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
28  bool AllowASTWithErrors, bool IncludeTimestamps)
29  : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
30  SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
31  Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions,
32  IncludeTimestamps),
33  AllowASTWithErrors(AllowASTWithErrors) {
34  this->Buffer->IsComplete = false;
35 }
36 
38 }
39 
41  // Don't create a PCH if there were fatal failures during module loading.
43  return;
44 
45  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
46  if (hasErrors && !AllowASTWithErrors)
47  return;
48 
49  Module *Module = nullptr;
50  if (PP.getLangOpts().isCompilingModule()) {
51  Module = PP.getHeaderSearchInfo().lookupModule(
52  PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
53  if (!Module) {
54  assert(hasErrors && "emitting module but current module doesn't exist");
55  return;
56  }
57  }
58 
59  // Emit the PCH file to the Buffer.
60  assert(SemaPtr && "No Sema?");
61  Buffer->Signature =
62  Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
63  // For serialization we are lenient if the errors were
64  // only warn-as-error kind.
66 
67  Buffer->IsComplete = true;
68 }
69 
71  return &Writer;
72 }
73 
75  return &Writer;
76 }
Defines the clang::ASTContext interface.
bool hasErrorOccurred() const
Definition: Diagnostic.h:662
bool isCompilingModule() const
Are we compiling a module interface (.cppm or module map)?
Definition: LangOptions.h:167
PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, ArrayRef< std::shared_ptr< ModuleFileExtension >> Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true)
Definition: GeneratePCH.cpp:24
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:40
Definition: Format.h:1900
~PCHGenerator() override
Definition: GeneratePCH.cpp:37
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:811
Describes a module or submodule.
Definition: Module.h:65
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:817
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:131
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:74
bool hasUncompilableErrorOccurred() const
Errors that actually prevent compilation, not those that are upgraded from a warning by -Werror...
Definition: Diagnostic.h:666
Defines the clang::Preprocessor interface.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true)
Lookup a module Search for a module with the given name.
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:70
Dataflow Directional Tag Classes.
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
Definition: Preprocessor.h:838
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:808
ASTFileSignature 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:4551
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127