2011-12-09 01:45:42 +00:00
|
|
|
//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
|
2009-04-09 22:27:44 +00: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 01:45:42 +00:00
|
|
|
// This file defines the PCHGenerator, which as a SemaConsumer that generates
|
|
|
|
// a PCH file.
|
2009-04-09 22:27:44 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-18 23:56:37 +00:00
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2009-04-09 22:27:44 +00:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-27 18:38:38 +00:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
2009-04-09 22:27:44 +00:00
|
|
|
#include "llvm/Bitcode/BitstreamWriter.h"
|
2015-02-24 05:14:17 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-04-09 22:27:44 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-09 15:08:12 +00:00
|
|
|
PCHGenerator::PCHGenerator(const Preprocessor &PP,
|
2011-08-25 22:30:56 +00:00
|
|
|
StringRef OutputFile,
|
2011-11-30 23:21:26 +00:00
|
|
|
clang::Module *Module,
|
2011-07-22 16:35:34 +00:00
|
|
|
StringRef isysroot,
|
2015-02-24 05:14:17 +00:00
|
|
|
raw_ostream *OS, bool AllowASTWithErrors)
|
2011-11-30 04:39:39 +00:00
|
|
|
: PP(PP), OutputFile(OutputFile), Module(Module),
|
2015-02-24 05:14:17 +00:00
|
|
|
isysroot(isysroot.str()), Out(OS),
|
|
|
|
SemaPtr(nullptr), Stream(Buffer), Writer(Stream),
|
2013-06-11 00:36:55 +00:00
|
|
|
AllowASTWithErrors(AllowASTWithErrors),
|
|
|
|
HasEmittedPCH(false) {
|
2011-07-22 06:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PCHGenerator::~PCHGenerator() {
|
2009-04-27 18:38:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 22:27:44 +00:00
|
|
|
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
|
2013-06-11 00:36:55 +00: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-09 22:27:44 +00:00
|
|
|
return;
|
2010-11-30 06:16:57 +00:00
|
|
|
|
2009-04-09 22:27:44 +00:00
|
|
|
// Emit the PCH file
|
2009-04-20 15:53:59 +00:00
|
|
|
assert(SemaPtr && "No Sema?");
|
2013-06-11 00:36:55 +00:00
|
|
|
Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
|
2009-04-09 22:27:44 +00:00
|
|
|
|
2015-02-24 05:14:17 +00:00
|
|
|
// Write the generated bitstream to "Out".
|
|
|
|
Out->write((char *)&Buffer.front(), Buffer.size());
|
|
|
|
|
|
|
|
// Make sure it hits disk now.
|
|
|
|
Out->flush();
|
|
|
|
|
|
|
|
// Free up some memory, in case the process is kept alive.
|
|
|
|
Buffer.clear();
|
2013-06-11 00:36:55 +00:00
|
|
|
|
|
|
|
HasEmittedPCH = true;
|
2009-04-09 22:27:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 17:26:36 +00:00
|
|
|
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
|
2011-08-25 22:35:51 +00:00
|
|
|
return &Writer;
|
2010-10-24 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 23:56:56 +00:00
|
|
|
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
|
2010-07-30 00:29:29 +00:00
|
|
|
return &Writer;
|
|
|
|
}
|