GeneratePCH.cpp revision 212795
1132718Skan//===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- C++ -*-===// 2132718Skan// 3132718Skan// The LLVM Compiler Infrastructure 4132718Skan// 5132718Skan// This file is distributed under the University of Illinois Open Source 6132718Skan// License. See LICENSE.TXT for details. 7132718Skan// 8132718Skan//===----------------------------------------------------------------------===// 9132718Skan// 10132718Skan// This file defines the CreatePCHGenerate function, which creates an 11132718Skan// ASTConsumer that generates a PCH file. 12132718Skan// 13132718Skan//===----------------------------------------------------------------------===// 14132718Skan 15132718Skan#include "clang/Frontend/ASTConsumers.h" 16132718Skan#include "clang/Serialization/ASTWriter.h" 17132718Skan#include "clang/Sema/SemaConsumer.h" 18132718Skan#include "clang/AST/ASTContext.h" 19132718Skan#include "clang/AST/ASTConsumer.h" 20132718Skan#include "clang/Lex/Preprocessor.h" 21132718Skan#include "clang/Basic/FileManager.h" 22132718Skan#include "llvm/Bitcode/BitstreamWriter.h" 23132718Skan#include "llvm/Support/raw_ostream.h" 24132718Skan#include <string> 25132718Skan 26132718Skanusing namespace clang; 27132718Skan 28132718SkanPCHGenerator::PCHGenerator(const Preprocessor &PP, 29132718Skan bool Chaining, 30132718Skan const char *isysroot, 31132718Skan llvm::raw_ostream *OS) 32132718Skan : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), 33132718Skan StatCalls(0), Stream(Buffer), Writer(Stream) { 34132718Skan 35132718Skan // Install a stat() listener to keep track of all of the stat() 36132718Skan // calls. 37132718Skan StatCalls = new MemorizeStatCalls; 38 // If we have a chain, we want new stat calls only, so install the memorizer 39 // *after* the already installed ASTReader's stat cache. 40 PP.getFileManager().addStatCache(StatCalls, 41 /*AtBeginning=*/!Chaining); 42} 43 44void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { 45 if (PP.getDiagnostics().hasErrorOccurred()) 46 return; 47 48 // Emit the PCH file 49 assert(SemaPtr && "No Sema?"); 50 Writer.WriteAST(*SemaPtr, StatCalls, isysroot); 51 52 // Write the generated bitstream to "Out". 53 Out->write((char *)&Buffer.front(), Buffer.size()); 54 55 // Make sure it hits disk now. 56 Out->flush(); 57 58 // Free up some memory, in case the process is kept alive. 59 Buffer.clear(); 60} 61 62ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() { 63 return &Writer; 64} 65