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/Serialization/ASTWriter.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Sema/SemaConsumer.h"
21 #include "llvm/Bitcode/BitstreamWriter.h"
24 using namespace clang;
26 PCHGenerator::PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
27 clang::Module *Module, StringRef isysroot,
28 std::shared_ptr<PCHBuffer> Buffer,
29 bool AllowASTWithErrors)
30 : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
31 SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data), Writer(Stream),
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 // Emit the PCH file to the Buffer.
49 assert(SemaPtr && "No Sema?");
50 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
52 Buffer->IsComplete = true;
55 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
59 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {