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/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" 22 #include <string> 23 24 using namespace clang; 25 26 PCHGenerator::PCHGenerator(const Preprocessor &PP, 27 StringRef OutputFile, 28 clang::Module *Module, 29 StringRef isysroot, 30 bool AllowASTWithErrors) 31 : PP(PP), OutputFile(OutputFile), Module(Module), 32 isysroot(isysroot.str()), 33 SemaPtr(nullptr), Stream(Buffer), 34 Writer(Stream), 35 AllowASTWithErrors(AllowASTWithErrors), 36 HasEmittedPCH(false) { 37 } 38 39 PCHGenerator::~PCHGenerator() { 40 } 41 42 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { 43 // Don't create a PCH if there were fatal failures during module loading. 44 if (PP.getModuleLoader().HadFatalFailure) 45 return; 46 47 bool hasErrors = PP.getDiagnostics().hasErrorOccurred(); 48 if (hasErrors && !AllowASTWithErrors) 49 return; 50 51 // Emit the PCH file 52 assert(SemaPtr && "No Sema?"); 53 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors); 54 55 if (SerializationFinishedCallback) 56 SerializationFinishedCallback(&Buffer); 57 58 HasEmittedPCH = true; 59 } 60 61 ASTMutationListener *PCHGenerator::GetASTMutationListener() { 62 return &Writer; 63 } 64 65 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { 66 return &Writer; 67 } 68