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 "llvm/Support/raw_ostream.h" 23 #include <string> 24 25 using namespace clang; 26 27 PCHGenerator::PCHGenerator(const Preprocessor &PP, 28 StringRef OutputFile, 29 clang::Module *Module, 30 StringRef isysroot, 31 raw_ostream *OS, bool AllowASTWithErrors) 32 : PP(PP), OutputFile(OutputFile), Module(Module), 33 isysroot(isysroot.str()), Out(OS), 34 SemaPtr(nullptr), Stream(Buffer), 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 // Write the generated bitstream to "Out". 56 Out->write((char *)&Buffer.front(), Buffer.size()); 57 58 // Make sure it hits disk now. 59 Out->flush(); 60 61 // Free up some memory, in case the process is kept alive. 62 Buffer.clear(); 63 64 HasEmittedPCH = true; 65 } 66 67 ASTMutationListener *PCHGenerator::GetASTMutationListener() { 68 return &Writer; 69 } 70 71 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { 72 return &Writer; 73 } 74