GeneratePCH.cpp revision a8235d6c4093cd38dcf742909651f867de62e55b
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/Sema/SemaConsumer.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/ASTConsumer.h" 19#include "clang/Lex/Preprocessor.h" 20#include "clang/Basic/FileManager.h" 21#include "clang/Basic/FileSystemStatCache.h" 22#include "llvm/Bitcode/BitstreamWriter.h" 23#include "llvm/Support/raw_ostream.h" 24#include <string> 25 26using namespace clang; 27 28PCHGenerator::PCHGenerator(const Preprocessor &PP, 29 StringRef OutputFile, 30 clang::Module *Module, 31 StringRef isysroot, 32 raw_ostream *OS) 33 : PP(PP), OutputFile(OutputFile), Module(Module), 34 isysroot(isysroot.str()), Out(OS), 35 SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) { 36 // Install a stat() listener to keep track of all of the stat() 37 // calls. 38 StatCalls = new MemorizeStatCalls(); 39 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false); 40} 41 42PCHGenerator::~PCHGenerator() { 43} 44 45void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { 46 if (PP.getDiagnostics().hasErrorOccurred()) 47 return; 48 49 // Emit the PCH file 50 assert(SemaPtr && "No Sema?"); 51 Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, Module, isysroot); 52 53 // Write the generated bitstream to "Out". 54 Out->write((char *)&Buffer.front(), Buffer.size()); 55 56 // Make sure it hits disk now. 57 Out->flush(); 58 59 // Free up some memory, in case the process is kept alive. 60 Buffer.clear(); 61} 62 63PPMutationListener *PCHGenerator::GetPPMutationListener() { 64 return &Writer; 65} 66 67ASTMutationListener *PCHGenerator::GetASTMutationListener() { 68 return &Writer; 69} 70 71ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { 72 return &Writer; 73} 74