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