blob: 4860b54f83a9bc2256cc661803d8c6e99fd7df8e [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006//
7//===----------------------------------------------------------------------===//
8//
Chandler Carruthae899872011-12-09 01:45:42 +00009// This file defines the PCHGenerator, which as a SemaConsumer that generates
10// a PCH file.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/AST/ASTContext.h"
Richard Smithbd97f352016-08-25 18:26:30 +000015#include "clang/Lex/HeaderSearch.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(
Richard Smithbd97f352016-08-25 18:26:30 +000024 const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
25 std::shared_ptr<PCHBuffer> Buffer,
David Blaikie61137e12017-01-05 18:23:18 +000026 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
Richard Smithbd97f352016-08-25 18:26:30 +000027 bool AllowASTWithErrors, bool IncludeTimestamps)
28 : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000029 SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
30 Writer(Stream, this->Buffer->Data, PP.getPCMCache(), Extensions,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000031 IncludeTimestamps),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000032 AllowASTWithErrors(AllowASTWithErrors) {
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000033 this->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
Richard Smithbd97f352016-08-25 18:26:30 +000048 Module *Module = nullptr;
Richard Smithbbcc9f02016-08-26 00:14:38 +000049 if (PP.getLangOpts().isCompilingModule()) {
Richard Smithbd97f352016-08-25 18:26:30 +000050 Module = PP.getHeaderSearchInfo().lookupModule(
51 PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
Richard Smithbbcc9f02016-08-26 00:14:38 +000052 if (!Module) {
53 assert(hasErrors && "emitting module but current module doesn't exist");
54 return;
55 }
Richard Smithbd97f352016-08-25 18:26:30 +000056 }
57
Adrian Prantlbb165fb2015-06-20 18:53:08 +000058 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000059 assert(SemaPtr && "No Sema?");
Adrian Prantla5206ce2015-09-22 23:26:43 +000060 Buffer->Signature =
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +000061 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
62 // For serialization we are lenient if the errors were
63 // only warn-as-error kind.
64 PP.getDiagnostics().hasUncompilableErrorOccurred());
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065
Adrian Prantlbb165fb2015-06-20 18:53:08 +000066 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067}
68
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000069ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000070 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000071}
72
Sebastian Redl3e31c722010-08-18 23:56:56 +000073ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000074 return &Writer;
75}