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-10 06:27:44 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-28 02:38:38 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.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,
|
2013-06-11 08:36:55 +08:00
|
|
|
raw_ostream *OS, bool AllowASTWithErrors)
|
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),
|
2013-06-11 08:36:55 +08:00
|
|
|
SemaPtr(0), Stream(Buffer), Writer(Stream),
|
|
|
|
AllowASTWithErrors(AllowASTWithErrors),
|
|
|
|
HasEmittedPCH(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) {
|
2013-06-11 08:36:55 +08:00
|
|
|
// Don't create a PCH if there were fatal failures during module loading.
|
|
|
|
if (PP.getModuleLoader().HadFatalFailure)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
|
|
|
|
if (hasErrors && !AllowASTWithErrors)
|
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?");
|
2013-06-11 08:36:55 +08:00
|
|
|
Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
|
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();
|
2013-06-11 08:36:55 +08:00
|
|
|
|
|
|
|
HasEmittedPCH = true;
|
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;
|
|
|
|
}
|