opnsense-src/lib/Frontend/GeneratePCH.cpp

81 lines
2.5 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04: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.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/PCHWriter.h"
#include "clang/Sema/SemaConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/FileManager.h"
#include "llvm/Bitcode/BitstreamWriter.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
using namespace clang;
namespace {
2009-12-01 06:08:04 -05:00
class PCHGenerator : public SemaConsumer {
2009-06-02 13:58:47 -04:00
const Preprocessor &PP;
2009-10-14 14:03:49 -04:00
const char *isysroot;
2009-06-02 13:58:47 -04:00
llvm::raw_ostream *Out;
Sema *SemaPtr;
MemorizeStatCalls *StatCalls; // owned by the FileManager
public:
2009-10-14 14:03:49 -04:00
explicit PCHGenerator(const Preprocessor &PP,
const char *isysroot,
llvm::raw_ostream *Out);
2009-06-02 13:58:47 -04:00
virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
virtual void HandleTranslationUnit(ASTContext &Ctx);
};
}
2009-10-14 14:03:49 -04:00
PCHGenerator::PCHGenerator(const Preprocessor &PP,
const char *isysroot,
llvm::raw_ostream *OS)
: PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) {
2009-06-02 13:58:47 -04:00
// Install a stat() listener to keep track of all of the stat()
// calls.
StatCalls = new MemorizeStatCalls;
2009-10-23 10:22:18 -04:00
PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/true);
2009-06-02 13:58:47 -04:00
}
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
if (PP.getDiagnostics().hasErrorOccurred())
return;
2009-10-14 14:03:49 -04:00
// Write the PCH contents into a buffer
2009-06-02 13:58:47 -04:00
std::vector<unsigned char> Buffer;
2009-12-01 06:08:04 -05:00
llvm::BitstreamWriter Stream(Buffer);
2009-06-02 13:58:47 -04:00
PCHWriter Writer(Stream);
// Emit the PCH file
assert(SemaPtr && "No Sema?");
2009-10-14 14:03:49 -04:00
Writer.WritePCH(*SemaPtr, StatCalls, isysroot);
2009-06-02 13:58:47 -04:00
// Write the generated bitstream to "Out".
Out->write((char *)&Buffer.front(), Buffer.size());
// Make sure it hits disk now.
Out->flush();
}
ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
2009-10-14 14:03:49 -04:00
llvm::raw_ostream *OS,
const char *isysroot) {
return new PCHGenerator(PP, isysroot, OS);
2009-06-02 13:58:47 -04:00
}