GeneratePCH.cpp revision 226633
1141104Sharti//===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- C++ -*-===//
2141290Sharti//
394589Sobrien//                     The LLVM Compiler Infrastructure
494589Sobrien//
55814Sjkh// This file is distributed under the University of Illinois Open Source
61590Srgrimes// License. See LICENSE.TXT for details.
71590Srgrimes//
81590Srgrimes//===----------------------------------------------------------------------===//
91590Srgrimes//
101590Srgrimes//  This file defines the CreatePCHGenerate function, which creates an
111590Srgrimes//  ASTConsumer that generates a PCH file.
121590Srgrimes//
131590Srgrimes//===----------------------------------------------------------------------===//
141590Srgrimes
151590Srgrimes#include "clang/Frontend/ASTConsumers.h"
161590Srgrimes#include "clang/Serialization/ASTWriter.h"
171590Srgrimes#include "clang/Sema/SemaConsumer.h"
181590Srgrimes#include "clang/AST/ASTContext.h"
191590Srgrimes#include "clang/AST/ASTConsumer.h"
201590Srgrimes#include "clang/Lex/Preprocessor.h"
211590Srgrimes#include "clang/Basic/FileManager.h"
221590Srgrimes#include "clang/Basic/FileSystemStatCache.h"
231590Srgrimes#include "llvm/Bitcode/BitstreamWriter.h"
241590Srgrimes#include "llvm/Support/raw_ostream.h"
251590Srgrimes#include <string>
261590Srgrimes
271590Srgrimesusing namespace clang;
281590Srgrimes
291590SrgrimesPCHGenerator::PCHGenerator(const Preprocessor &PP,
301590Srgrimes                           StringRef OutputFile,
311590Srgrimes                           bool IsModule,
321590Srgrimes                           StringRef isysroot,
331590Srgrimes                           raw_ostream *OS)
341590Srgrimes  : PP(PP), OutputFile(OutputFile), IsModule(IsModule),
351590Srgrimes    isysroot(isysroot.str()), Out(OS),
361590Srgrimes    SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) {
371590Srgrimes  // Install a stat() listener to keep track of all of the stat()
381590Srgrimes  // calls.
3962833Swsanchez  StatCalls = new MemorizeStatCalls();
4062833Swsanchez  PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
411590Srgrimes}
421590Srgrimes
4362833SwsanchezPCHGenerator::~PCHGenerator() {
4494587Sobrien}
451590Srgrimes
46141290Shartivoid PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
47141290Sharti  if (PP.getDiagnostics().hasErrorOccurred())
481590Srgrimes    return;
491590Srgrimes
501590Srgrimes  // Emit the PCH file
51141104Sharti  assert(SemaPtr && "No Sema?");
52141104Sharti  Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, IsModule, isysroot);
53141104Sharti
54141104Sharti  // Write the generated bitstream to "Out".
55141104Sharti  Out->write((char *)&Buffer.front(), Buffer.size());
561590Srgrimes
57141290Sharti  // Make sure it hits disk now.
58141290Sharti  Out->flush();
59141290Sharti
601590Srgrimes  // Free up some memory, in case the process is kept alive.
61187475Srdivacky  Buffer.clear();
62141290Sharti}
63141290Sharti
641590SrgrimesASTMutationListener *PCHGenerator::GetASTMutationListener() {
65141290Sharti  return &Writer;
66141290Sharti}
671590Srgrimes
68141290ShartiASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
69143957Sharti  return &Writer;
70143957Sharti}
71143957Sharti