blob: 47dce37ede15ed3d10621e89b856a82a29c1ad4b [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
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Lex/Preprocessor.h"
17#include "clang/Sema/SemaConsumer.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000018#include "clang/Serialization/ASTWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020
21using namespace clang;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022
Douglas Gregor6623e1f2015-11-03 18:33:07 +000023PCHGenerator::PCHGenerator(
24 const Preprocessor &PP, StringRef OutputFile,
25 clang::Module *Module, StringRef isysroot,
26 std::shared_ptr<PCHBuffer> Buffer,
27 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
28 bool AllowASTWithErrors, bool IncludeTimestamps)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000029 : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
Richard Smithe75ee0f2015-08-17 07:13:32 +000030 SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
Douglas Gregor6623e1f2015-11-03 18:33:07 +000031 Writer(Stream, Extensions, IncludeTimestamps),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000032 AllowASTWithErrors(AllowASTWithErrors) {
33 Buffer->IsComplete = false;
Douglas Gregor77d993d2011-07-22 06:03:18 +000034}
35
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000036PCHGenerator::~PCHGenerator() {
37}
Douglas Gregorc5046832009-04-27 18:38:38 +000038
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000040 // Don't create a PCH if there were fatal failures during module loading.
41 if (PP.getModuleLoader().HadFatalFailure)
42 return;
43
44 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
45 if (hasErrors && !AllowASTWithErrors)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046 return;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000047
48 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000049 assert(SemaPtr && "No Sema?");
Adrian Prantla5206ce2015-09-22 23:26:43 +000050 Buffer->Signature =
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +000051 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
52 // For serialization we are lenient if the errors were
53 // only warn-as-error kind.
54 PP.getDiagnostics().hasUncompilableErrorOccurred());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
Adrian Prantlbb165fb2015-06-20 18:53:08 +000056 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057}
58
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000059ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000060 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000061}
62
Sebastian Redl3e31c722010-08-18 23:56:56 +000063ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000064 return &Writer;
65}