2011-12-09 01:45:42 +00:00
|
|
|
//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
|
2009-04-09 22:27:44 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-12-09 01:45:42 +00:00
|
|
|
// This file defines the PCHGenerator, which as a SemaConsumer that generates
|
|
|
|
// a PCH file.
|
2009-04-09 22:27:44 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-18 23:56:37 +00:00
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2009-04-20 15:53:59 +00:00
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2009-04-09 22:27:44 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-04-10 17:15:23 +00:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-04-27 18:38:38 +00:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2010-11-23 19:19:34 +00:00
|
|
|
#include "clang/Basic/FileSystemStatCache.h"
|
2009-04-09 22:27:44 +00:00
|
|
|
#include "llvm/Bitcode/BitstreamWriter.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-09 15:08:12 +00:00
|
|
|
PCHGenerator::PCHGenerator(const Preprocessor &PP,
|
2011-08-25 22:30:56 +00:00
|
|
|
StringRef OutputFile,
|
2011-11-30 23:21:26 +00:00
|
|
|
clang::Module *Module,
|
2011-07-22 16:35:34 +00:00
|
|
|
StringRef isysroot,
|
2011-07-23 10:55:15 +00:00
|
|
|
raw_ostream *OS)
|
2011-11-30 04:39:39 +00:00
|
|
|
: PP(PP), OutputFile(OutputFile), Module(Module),
|
2011-09-01 17:04:32 +00:00
|
|
|
isysroot(isysroot.str()), Out(OS),
|
2011-08-25 22:35:51 +00:00
|
|
|
SemaPtr(0), StatCalls(0), Stream(Buffer), Writer(Stream) {
|
2009-04-27 18:38:38 +00:00
|
|
|
// Install a stat() listener to keep track of all of the stat()
|
|
|
|
// calls.
|
2010-11-23 19:19:34 +00:00
|
|
|
StatCalls = new MemorizeStatCalls();
|
2011-08-25 22:35:51 +00:00
|
|
|
PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/false);
|
2011-07-22 06:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PCHGenerator::~PCHGenerator() {
|
2009-04-27 18:38:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 22:27:44 +00:00
|
|
|
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
|
2009-04-10 17:15:23 +00:00
|
|
|
if (PP.getDiagnostics().hasErrorOccurred())
|
2009-04-09 22:27:44 +00:00
|
|
|
return;
|
2010-11-30 06:16:57 +00:00
|
|
|
|
2009-04-09 22:27:44 +00:00
|
|
|
// Emit the PCH file
|
2009-04-20 15:53:59 +00:00
|
|
|
assert(SemaPtr && "No Sema?");
|
2011-11-30 04:39:39 +00:00
|
|
|
Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, Module, isysroot);
|
2009-04-09 22:27:44 +00:00
|
|
|
|
|
|
|
// Write the generated bitstream to "Out".
|
2009-05-18 22:20:00 +00:00
|
|
|
Out->write((char *)&Buffer.front(), Buffer.size());
|
2009-04-09 22:27:44 +00:00
|
|
|
|
|
|
|
// Make sure it hits disk now.
|
2009-05-18 22:20:00 +00:00
|
|
|
Out->flush();
|
2010-07-14 23:45:08 +00:00
|
|
|
|
|
|
|
// Free up some memory, in case the process is kept alive.
|
|
|
|
Buffer.clear();
|
2009-04-09 22:27:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 17:26:36 +00:00
|
|
|
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
|
2011-08-25 22:35:51 +00:00
|
|
|
return &Writer;
|
2010-10-24 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 23:56:56 +00:00
|
|
|
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
|
2010-07-30 00:29:29 +00:00
|
|
|
return &Writer;
|
|
|
|
}
|