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