1 //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PCHGenerator, which as a SemaConsumer that generates
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ASTContext.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Sema/SemaConsumer.h"
19 #include "clang/Serialization/ASTWriter.h"
20 #include "llvm/Bitcode/BitstreamWriter.h"
22 using namespace clang;
24 PCHGenerator::PCHGenerator(
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(Buffer), Stream(Buffer->Data),
31 Writer(Stream, Extensions, IncludeTimestamps),
32 AllowASTWithErrors(AllowASTWithErrors) {
33 Buffer->IsComplete = false;
36 PCHGenerator::~PCHGenerator() {
39 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
40 // Don't create a PCH if there were fatal failures during module loading.
41 if (PP.getModuleLoader().HadFatalFailure)
44 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
45 if (hasErrors && !AllowASTWithErrors)
48 Module *Module = nullptr;
49 if (PP.getLangOpts().isCompilingModule()) {
50 Module = PP.getHeaderSearchInfo().lookupModule(
51 PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
53 assert(hasErrors && "emitting module but current module doesn't exist");
58 // Emit the PCH file to the Buffer.
59 assert(SemaPtr && "No Sema?");
61 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
62 // For serialization we are lenient if the errors were
63 // only warn-as-error kind.
64 PP.getDiagnostics().hasUncompilableErrorOccurred());
66 Buffer->IsComplete = true;
69 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
73 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {