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 <string>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
PCHGenerator::PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
|
|
|
|
clang::Module *Module, StringRef isysroot,
|
|
|
|
std::shared_ptr<PCHBuffer> Buffer,
|
2015-08-17 15:13:32 +08:00
|
|
|
bool AllowASTWithErrors, bool IncludeTimestamps)
|
2015-06-21 02:53:08 +08:00
|
|
|
: PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
|
2015-08-17 15:13:32 +08:00
|
|
|
SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
|
|
|
|
Writer(Stream, IncludeTimestamps),
|
2015-06-21 02:53:08 +08:00
|
|
|
AllowASTWithErrors(AllowASTWithErrors) {
|
|
|
|
Buffer->IsComplete = 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;
|
2015-06-21 02:53:08 +08:00
|
|
|
|
|
|
|
// Emit the PCH file to the Buffer.
|
2009-04-20 23:53:59 +08:00
|
|
|
assert(SemaPtr && "No Sema?");
|
2015-09-23 07:26:43 +08:00
|
|
|
Buffer->Signature =
|
|
|
|
Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
|
2009-04-10 06:27:44 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
Buffer->IsComplete = 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;
|
|
|
|
}
|