blob: 002233e49bb06c14712ec2a2217b2cef355c1298 [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"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000019#include "llvm/Bitstream/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(
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000024 const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
25 StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
David Blaikie61137e12017-01-05 18:23:18 +000026 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +000027 bool AllowASTWithErrors, bool IncludeTimestamps,
28 bool ShouldCacheASTInMemory)
Richard Smithbd97f352016-08-25 18:26:30 +000029 : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000030 SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000031 Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000032 IncludeTimestamps),
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +000033 AllowASTWithErrors(AllowASTWithErrors),
34 ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000035 this->Buffer->IsComplete = false;
Douglas Gregor77d993d2011-07-22 06:03:18 +000036}
37
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000038PCHGenerator::~PCHGenerator() {
39}
Douglas Gregorc5046832009-04-27 18:38:38 +000040
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +000042 // Don't create a PCH if there were fatal failures during module loading.
43 if (PP.getModuleLoader().HadFatalFailure)
44 return;
45
46 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
47 if (hasErrors && !AllowASTWithErrors)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048 return;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000049
Richard Smithbd97f352016-08-25 18:26:30 +000050 Module *Module = nullptr;
Richard Smithbbcc9f02016-08-26 00:14:38 +000051 if (PP.getLangOpts().isCompilingModule()) {
Richard Smithbd97f352016-08-25 18:26:30 +000052 Module = PP.getHeaderSearchInfo().lookupModule(
53 PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
Richard Smithbbcc9f02016-08-26 00:14:38 +000054 if (!Module) {
55 assert(hasErrors && "emitting module but current module doesn't exist");
56 return;
57 }
Richard Smithbd97f352016-08-25 18:26:30 +000058 }
59
Adrian Prantlbb165fb2015-06-20 18:53:08 +000060 // Emit the PCH file to the Buffer.
Douglas Gregor162dd022009-04-20 15:53:59 +000061 assert(SemaPtr && "No Sema?");
Adrian Prantla5206ce2015-09-22 23:26:43 +000062 Buffer->Signature =
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +000063 Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
64 // For serialization we are lenient if the errors were
65 // only warn-as-error kind.
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04 +000066 PP.getDiagnostics().hasUncompilableErrorOccurred(),
67 ShouldCacheASTInMemory);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
Adrian Prantlbb165fb2015-06-20 18:53:08 +000069 Buffer->IsComplete = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070}
71
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000072ASTMutationListener *PCHGenerator::GetASTMutationListener() {
Douglas Gregor36db4f92011-08-25 22:35:51 +000073 return &Writer;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000074}
75
Sebastian Redl3e31c722010-08-18 23:56:56 +000076ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redl07a89a82010-07-30 00:29:29 +000077 return &Writer;
78}