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/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"
25 using namespace clang;
27 PCHGenerator::PCHGenerator(const Preprocessor &PP,
29 clang::Module *Module,
32 : PP(PP), OutputFile(OutputFile), Module(Module),
33 isysroot(isysroot.str()), Out(OS),
34 SemaPtr(0), Stream(Buffer), Writer(Stream) {
37 PCHGenerator::~PCHGenerator() {
40 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
41 if (PP.getDiagnostics().hasErrorOccurred())
45 assert(SemaPtr && "No Sema?");
46 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot);
48 // Write the generated bitstream to "Out".
49 Out->write((char *)&Buffer.front(), Buffer.size());
51 // Make sure it hits disk now.
54 // Free up some memory, in case the process is kept alive.
58 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
62 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {