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/Frontend/FrontendDiagnostic.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, InMemoryModuleCache &ModuleCache, 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), OutputFile(OutputFile), isysroot(isysroot.str()),
33 SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
34 Writer(Stream, this->Buffer->Data, ModuleCache, 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
59void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
60 // Don't create a PCH if there were fatal failures during module loading.
61 if (PP.getModuleLoader().HadFatalFailure)
62 return;
63
64 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
65 if (hasErrors && !AllowASTWithErrors)
66 return;
67
68 Module *Module = getEmittingModule(Ctx);
69
70 // Errors that do not prevent the PCH from being written should not cause the
71 // overall compilation to fail either.
72 if (AllowASTWithErrors)
73 PP.getDiagnostics().getClient()->clear();
74
75 // Emit the PCH file to the Buffer.
76 assert(SemaPtr && "No Sema?");
77 Buffer->Signature = Writer.WriteAST(SemaRef&: *SemaPtr, OutputFile, WritingModule: Module, isysroot,
78 ShouldCacheASTInMemory);
79
80 Buffer->IsComplete = true;
81}
82
83ASTMutationListener *PCHGenerator::GetASTMutationListener() {
84 return &Writer;
85}
86
87ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
88 return &Writer;
89}
90
91ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
92 InMemoryModuleCache &ModuleCache,
93 StringRef OutputFile)
94 : PCHGenerator(
95 PP, ModuleCache, OutputFile, llvm::StringRef(),
96 std::make_shared<PCHBuffer>(),
97 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
98 /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
99 /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
100 /*GeneratingReducedBMI=*/true) {}
101
102Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
103 Module *M = Ctx.getCurrentNamedModule();
104 assert(M && M->isNamedModuleUnit() &&
105 "ReducedBMIGenerator should only be used with C++20 Named modules.");
106 return M;
107}
108
109void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
110 // We need to do this to make sure the size of reduced BMI not to be larger
111 // than full BMI.
112 //
113 // FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
114 // since this is not about searching header really.
115 // FIXME2: We'd better to move the class writing full BMI with reduced BMI.
116 HeaderSearchOptions &HSOpts =
117 getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
118 HSOpts.ModulesSkipDiagnosticOptions = true;
119 HSOpts.ModulesSkipHeaderSearchPaths = true;
120 HSOpts.ModulesSkipPragmaDiagnosticMappings = true;
121
122 PCHGenerator::HandleTranslationUnit(Ctx);
123
124 if (!isComplete())
125 return;
126
127 std::error_code EC;
128 auto OS = std::make_unique<llvm::raw_fd_ostream>(args: getOutputFile(), args&: EC);
129 if (EC) {
130 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
131 << getOutputFile() << EC.message() << "\n";
132 return;
133 }
134
135 *OS << getBufferPtr()->Data;
136 OS->flush();
137}
138

source code of clang/lib/Serialization/GeneratePCH.cpp