blob: 08660bc9675282737afdd8a652e0728de3b9ceb6 [file] [log] [blame]
Chandler Carruthae899872011-12-09 01:45:42 +00001//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruthae899872011-12-09 01:45:42 +000010// This file defines the PCHGenerator, which as a SemaConsumer that generates
11// a PCH file.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Sebastian Redl1914c6f2010-08-18 23:56:37 +000015#include "clang/Serialization/ASTWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTConsumer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Douglas Gregorc5046832009-04-27 18:38:38 +000018#include "clang/Basic/FileManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Sema/SemaConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include <string>
23
24using namespace clang;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025
Adrian Prantlbb165fb2015-06-20 18:53:08 +000026PCHGenerator::PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
27 clang::Module *Module, StringRef isysroot,
28 std::shared_ptr<PCHBuffer> Buffer,
Richard Smithe75ee0f2015-08-17 07:13:32 +000029 bool AllowASTWithErrors, bool IncludeTimestamps)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000030 : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
Richard Smithe75ee0f2015-08-17 07:13:32 +000031 SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
32 Writer(Stream, IncludeTimestamps),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000033 AllowASTWithErrors(AllowASTWithErrors) {
34 Buffer->IsComplete = false;
Douglas Gregor77d993d2011-07-22 06:03:18 +000035}
36
37PCHGenerator::~PCHGenerator() {
Douglas Gregorc5046832009-04-27 18:38:38 +000038}
39
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000041 // Don't create a PCH if there were fatal failures during module loading.
42 if (PP.getModuleLoader().HadFatalFailure)
43 return;
44
45 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
46 if (hasErrors && !AllowASTWithErrors)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047 return;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000048
49 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000050 assert(SemaPtr && "No Sema?");
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000051 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Adrian Prantlbb165fb2015-06-20 18:53:08 +000053 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054}
55
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000056ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000057 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000058}
59
Sebastian Redl3e31c722010-08-18 23:56:56 +000060ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000061 return &Writer;
62}