2009-04-09 22:27:44 +00:00
|
|
|
//===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the CreatePCHGenerate function, which creates an
|
2010-08-18 23:57:06 +00:00
|
|
|
// ASTConsumer that generates a PCH file.
|
2009-04-09 22:27:44 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-18 22:50:54 +00:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
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"
|
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,
|
2010-07-30 00:29:29 +00:00
|
|
|
bool Chaining,
|
2009-07-07 00:12:59 +00:00
|
|
|
const char *isysroot,
|
|
|
|
llvm::raw_ostream *OS)
|
2010-07-30 00:29:29 +00:00
|
|
|
: PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0),
|
2010-10-24 17:26:36 +00:00
|
|
|
StatCalls(0), Stream(Buffer), Writer(Stream), Chaining(Chaining) {
|
2009-04-27 18:38:38 +00:00
|
|
|
|
|
|
|
// Install a stat() listener to keep track of all of the stat()
|
|
|
|
// calls.
|
|
|
|
StatCalls = new MemorizeStatCalls;
|
2010-07-16 16:36:56 +00:00
|
|
|
// If we have a chain, we want new stat calls only, so install the memorizer
|
2010-08-18 23:56:43 +00:00
|
|
|
// *after* the already installed ASTReader's stat cache.
|
2010-07-16 16:36:56 +00:00
|
|
|
PP.getFileManager().addStatCache(StatCalls,
|
2010-07-30 00:29:29 +00:00
|
|
|
/*AtBeginning=*/!Chaining);
|
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;
|
|
|
|
|
|
|
|
// Emit the PCH file
|
2009-04-20 15:53:59 +00:00
|
|
|
assert(SemaPtr && "No Sema?");
|
2010-08-18 23:56:21 +00:00
|
|
|
Writer.WriteAST(*SemaPtr, StatCalls, 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() {
|
|
|
|
if (Chaining)
|
|
|
|
return &Writer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-18 23:56:56 +00:00
|
|
|
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
|
2010-07-30 00:29:29 +00:00
|
|
|
return &Writer;
|
|
|
|
}
|