blob: 308fde8b1dd7aa99ec60f96e875ae23dbbf07f23 [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
Douglas Gregor6623e1f2015-11-03 18:33:07 +000026PCHGenerator::PCHGenerator(
27 const Preprocessor &PP, StringRef OutputFile,
28 clang::Module *Module, StringRef isysroot,
29 std::shared_ptr<PCHBuffer> Buffer,
30 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
31 bool AllowASTWithErrors, bool IncludeTimestamps)
Adrian Prantlbb165fb2015-06-20 18:53:08 +000032 : PP(PP), OutputFile(OutputFile), Module(Module), isysroot(isysroot.str()),
Richard Smithe75ee0f2015-08-17 07:13:32 +000033 SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
Douglas Gregor6623e1f2015-11-03 18:33:07 +000034 Writer(Stream, Extensions, IncludeTimestamps),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000035 AllowASTWithErrors(AllowASTWithErrors) {
36 Buffer->IsComplete = false;
Douglas Gregor77d993d2011-07-22 06:03:18 +000037}
38
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000039PCHGenerator::~PCHGenerator() {
40}
Douglas Gregorc5046832009-04-27 18:38:38 +000041
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000043 // Don't create a PCH if there were fatal failures during module loading.
44 if (PP.getModuleLoader().HadFatalFailure)
45 return;
46
47 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
48 if (hasErrors && !AllowASTWithErrors)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049 return;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000050
51 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000052 assert(SemaPtr && "No Sema?");
Adrian Prantla5206ce2015-09-22 23:26:43 +000053 Buffer->Signature =
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +000054 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
55 // For serialization we are lenient if the errors were
56 // only warn-as-error kind.
57 PP.getDiagnostics().hasUncompilableErrorOccurred());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058
Adrian Prantlbb165fb2015-06-20 18:53:08 +000059 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060}
61
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000062ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000063 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000064}
65
Sebastian Redl3e31c722010-08-18 23:56:56 +000066ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000067 return &Writer;
68}