1 //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PCHGenerator, which as a SemaConsumer that generates
13 //===----------------------------------------------------------------------===//
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"
26 using namespace clang;
28 PCHGenerator::PCHGenerator(const Preprocessor &PP,
30 clang::Module *Module,
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()
38 StatCalls = new MemorizeStatCalls();
39 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
42 PCHGenerator::~PCHGenerator() {
45 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
46 if (PP.getDiagnostics().hasErrorOccurred())
50 assert(SemaPtr && "No Sema?");
51 Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, Module, isysroot);
53 // Write the generated bitstream to "Out".
54 Out->write((char *)&Buffer.front(), Buffer.size());
56 // Make sure it hits disk now.
59 // Free up some memory, in case the process is kept alive.
63 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
67 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {