2009-04-10 06:27:44 +08: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
|
|
|
|
// ASTConsume that generates a PCH file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-04-11 01:15:23 +08:00
|
|
|
#include "ASTConsumers.h"
|
2009-04-10 06:27:44 +08:00
|
|
|
#include "clang/Frontend/PCHWriter.h"
|
2009-04-20 23:53:59 +08:00
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2009-04-10 06:27:44 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-04-11 01:15:23 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-04-28 02:38:38 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-04-10 06:27:44 +08:00
|
|
|
#include "llvm/Bitcode/BitstreamWriter.h"
|
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-04-20 23:53:59 +08:00
|
|
|
class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
|
2009-04-11 02:08:30 +08:00
|
|
|
const Preprocessor &PP;
|
2009-04-10 06:27:44 +08:00
|
|
|
std::string OutFile;
|
2009-04-20 23:53:59 +08:00
|
|
|
Sema *SemaPtr;
|
2009-04-28 02:38:38 +08:00
|
|
|
MemorizeStatCalls *StatCalls; // owned by the FileManager
|
2009-04-10 06:27:44 +08:00
|
|
|
|
|
|
|
public:
|
2009-04-28 02:38:38 +08:00
|
|
|
explicit PCHGenerator(const Preprocessor &PP, const std::string &OutFile);
|
2009-04-20 23:53:59 +08:00
|
|
|
virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
|
2009-04-10 06:27:44 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-04-28 02:38:38 +08:00
|
|
|
PCHGenerator::PCHGenerator(const Preprocessor &PP, const std::string &OutFile)
|
|
|
|
: PP(PP), OutFile(OutFile), SemaPtr(0), StatCalls(0) {
|
|
|
|
|
|
|
|
// Install a stat() listener to keep track of all of the stat()
|
|
|
|
// calls.
|
|
|
|
StatCalls = new MemorizeStatCalls;
|
|
|
|
PP.getFileManager().setStatCache(StatCalls);
|
|
|
|
}
|
|
|
|
|
2009-04-10 06:27:44 +08:00
|
|
|
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
|
2009-04-11 01:15:23 +08:00
|
|
|
if (PP.getDiagnostics().hasErrorOccurred())
|
2009-04-10 06:27:44 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Write the PCH contents into a buffer
|
|
|
|
std::vector<unsigned char> Buffer;
|
|
|
|
BitstreamWriter Stream(Buffer);
|
|
|
|
PCHWriter Writer(Stream);
|
|
|
|
|
|
|
|
// Emit the PCH file
|
2009-04-20 23:53:59 +08:00
|
|
|
assert(SemaPtr && "No Sema?");
|
2009-04-28 02:38:38 +08:00
|
|
|
Writer.WritePCH(*SemaPtr, StatCalls);
|
2009-04-10 06:27:44 +08:00
|
|
|
|
|
|
|
// Open up the PCH file.
|
|
|
|
std::string ErrMsg;
|
|
|
|
llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
|
|
|
|
|
|
|
|
if (!ErrMsg.empty()) {
|
|
|
|
llvm::errs() << "PCH error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the generated bitstream to "Out".
|
|
|
|
Out.write((char *)&Buffer.front(), Buffer.size());
|
|
|
|
|
|
|
|
// Make sure it hits disk now.
|
|
|
|
Out.flush();
|
|
|
|
}
|
|
|
|
|
2009-04-11 02:08:30 +08:00
|
|
|
ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
|
2009-04-11 01:15:23 +08:00
|
|
|
const std::string &OutFile) {
|
|
|
|
return new PCHGenerator(PP, OutFile);
|
2009-04-10 06:27:44 +08:00
|
|
|
}
|