clang API Documentation

GeneratePCH.cpp
Go to the documentation of this file.
00001 //===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the PCHGenerator, which as a SemaConsumer that generates
00011 //  a PCH file.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Serialization/ASTWriter.h"
00016 #include "clang/Sema/SemaConsumer.h"
00017 #include "clang/AST/ASTContext.h"
00018 #include "clang/AST/ASTConsumer.h"
00019 #include "clang/Lex/Preprocessor.h"
00020 #include "clang/Basic/FileManager.h"
00021 #include "clang/Basic/FileSystemStatCache.h"
00022 #include "llvm/Bitcode/BitstreamWriter.h"
00023 #include "llvm/Support/raw_ostream.h"
00024 #include <string>
00025 
00026 using namespace clang;
00027 
00028 PCHGenerator::PCHGenerator(const Preprocessor &PP,
00029                            StringRef OutputFile,
00030                            clang::Module *Module,
00031                            StringRef isysroot,
00032                            raw_ostream *OS)
00033   : PP(PP), OutputFile(OutputFile), Module(Module), 
00034     isysroot(isysroot.str()), Out(OS), 
00035     SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) {
00036   // Install a stat() listener to keep track of all of the stat()
00037   // calls.
00038   StatCalls = new MemorizeStatCalls();
00039   PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
00040 }
00041 
00042 PCHGenerator::~PCHGenerator() {
00043 }
00044 
00045 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
00046   if (PP.getDiagnostics().hasErrorOccurred())
00047     return;
00048   
00049   // Emit the PCH file
00050   assert(SemaPtr && "No Sema?");
00051   Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, Module, isysroot);
00052 
00053   // Write the generated bitstream to "Out".
00054   Out->write((char *)&Buffer.front(), Buffer.size());
00055 
00056   // Make sure it hits disk now.
00057   Out->flush();
00058 
00059   // Free up some memory, in case the process is kept alive.
00060   Buffer.clear();
00061 }
00062 
00063 ASTMutationListener *PCHGenerator::GetASTMutationListener() {
00064   return &Writer;
00065 }
00066 
00067 ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
00068   return &Writer;
00069 }