1//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the PCHGenerator, which as a SemaConsumer that generates
10// a PCH file.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/Basic/DiagnosticFrontend.h"
16#include "clang/Lex/HeaderSearch.h"
17#include "clang/Lex/HeaderSearchOptions.h"
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Sema/SemaConsumer.h"
20#include "clang/Serialization/ASTWriter.h"
21#include "llvm/Bitstream/BitstreamWriter.h"
22
23using namespace clang;
24
25PCHGenerator::PCHGenerator(
26 Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile,
27 StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
28 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
29 bool AllowASTWithErrors, bool IncludeTimestamps,
30 bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
31 bool GeneratingReducedBMI)
32 : PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33 Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
34 Writer(Stream, this->Buffer->Data, ModCache, Extensions,
35 IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
36 AllowASTWithErrors(AllowASTWithErrors),
37 ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
38 this->Buffer->IsComplete = false;
39}
40
41PCHGenerator::~PCHGenerator() {
42}
43
44Module *PCHGenerator::getEmittingModule(ASTContext &) {
45 Module *M = nullptr;
46
47 if (PP.getLangOpts().isCompilingModule()) {
48 M = PP.getHeaderSearchInfo().lookupModule(ModuleName: PP.getLangOpts().CurrentModule,
49 ImportLoc: SourceLocation(),
50 /*AllowSearch*/ false);
51 if (!M)
52 assert(PP.getDiagnostics().hasErrorOccurred() &&
53 "emitting module but current module doesn't exist");
54 }
55
56 return M;
57}
58
59DiagnosticsEngine &PCHGenerator::getDiagnostics() const {
60 return PP.getDiagnostics();
61}
62
63void PCHGenerator::InitializeSema(Sema &S) {
64 if (!PP.getHeaderSearchInfo()
65 .getHeaderSearchOpts()
66 .ModulesSerializeOnlyPreprocessor)
67 Subject = &S;
68}
69
70void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
71 // Don't create a PCH if there were fatal failures during module loading.
72 if (PP.getModuleLoader().HadFatalFailure)
73 return;
74
75 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
76 if (hasErrors && !AllowASTWithErrors)
77 return;
78
79 Module *Module = getEmittingModule(Ctx);
80
81 // Errors that do not prevent the PCH from being written should not cause the
82 // overall compilation to fail either.
83 if (AllowASTWithErrors)
84 PP.getDiagnostics().getClient()->clear();
85
86 Buffer->Signature = Writer.WriteAST(Subject, OutputFile, WritingModule: Module, isysroot,
87 ShouldCacheASTInMemory);
88
89 Buffer->IsComplete = true;
90}
91
92ASTMutationListener *PCHGenerator::GetASTMutationListener() {
93 return &Writer;
94}
95
96ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
97 return &Writer;
98}
99
100void PCHGenerator::anchor() {}
101
102CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP,
103 ModuleCache &ModCache,
104 StringRef OutputFile,
105 bool GeneratingReducedBMI,
106 bool AllowASTWithErrors)
107 : PCHGenerator(
108 PP, ModCache, OutputFile, llvm::StringRef(),
109 std::make_shared<PCHBuffer>(),
110 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
111 AllowASTWithErrors, /*IncludeTimestamps=*/false,
112 /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
113 GeneratingReducedBMI) {}
114
115Module *CXX20ModulesGenerator::getEmittingModule(ASTContext &Ctx) {
116 Module *M = Ctx.getCurrentNamedModule();
117 assert(M && M->isNamedModuleUnit() &&
118 "CXX20ModulesGenerator should only be used with C++20 Named modules.");
119 return M;
120}
121
122void CXX20ModulesGenerator::HandleTranslationUnit(ASTContext &Ctx) {
123 PCHGenerator::HandleTranslationUnit(Ctx);
124
125 if (!isComplete())
126 return;
127
128 std::error_code EC;
129 auto OS = std::make_unique<llvm::raw_fd_ostream>(args: getOutputFile(), args&: EC);
130 if (EC) {
131 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_open_output)
132 << getOutputFile() << EC.message() << "\n";
133 return;
134 }
135
136 *OS << getBufferPtr()->Data;
137 OS->flush();
138}
139
140void CXX20ModulesGenerator::anchor() {}
141
142void ReducedBMIGenerator::anchor() {}
143