clang 19.0.0git
GeneratePCH.cpp
Go to the documentation of this file.
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
21#include "llvm/Bitstream/BitstreamWriter.h"
22
23using namespace clang;
24
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
42}
43
45 Module *M = nullptr;
46
47 if (PP.getLangOpts().isCompilingModule()) {
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
60 // Don't create a PCH if there were fatal failures during module loading.
62 return;
63
64 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
65 if (hasErrors && !AllowASTWithErrors)
66 return;
67
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)
74
75 // Emit the PCH file to the Buffer.
76 assert(SemaPtr && "No Sema?");
77 Buffer->Signature = Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
78 ShouldCacheASTInMemory);
79
80 Buffer->IsComplete = true;
81}
82
84 return &Writer;
85}
86
88 return &Writer;
89}
90
92 InMemoryModuleCache &ModuleCache,
93 StringRef OutputFile)
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
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
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 =
118 HSOpts.ModulesSkipDiagnosticOptions = true;
119 HSOpts.ModulesSkipHeaderSearchPaths = true;
121
123
124 if (!isComplete())
125 return;
126
127 std::error_code EC;
128 auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), 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}
Defines the clang::ASTContext interface.
Defines the clang::Preprocessor interface.
const char * Data
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
Definition: ASTContext.h:1071
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile, Module *WritingModule, StringRef isysroot, bool ShouldCacheASTInMemory=false)
Write a precompiled header for the given semantic analysis.
Definition: ASTWriter.cpp:4714
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1547
bool hasErrorOccurred() const
Definition: Diagnostic.h:843
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:572
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
unsigned ModulesSkipHeaderSearchPaths
Whether to entirely skip writing header search paths.
unsigned ModulesSkipDiagnosticOptions
Whether to entirely skip writing diagnostic options.
unsigned ModulesSkipPragmaDiagnosticMappings
Whether to entirely skip writing pragma diagnostic mappings.
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized.
Definition: HeaderSearch.h:386
In-memory cache for modules.
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:602
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:516
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Describes a module or submodule.
Definition: Module.h:105
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:619
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
Definition: ASTWriter.h:887
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:83
PCHBuffer * getBufferPtr()
Definition: ASTWriter.h:904
Preprocessor & getPreprocessor()
Definition: ASTWriter.h:909
virtual Module * getEmittingModule(ASTContext &Ctx)
Definition: GeneratePCH.cpp:44
StringRef getOutputFile() const
Definition: ASTWriter.h:905
~PCHGenerator() override
Definition: GeneratePCH.cpp:41
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:87
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:59
PCHGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, ArrayRef< std::shared_ptr< ModuleFileExtension > > Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true, bool BuildingImplicitModule=false, bool ShouldCacheASTInMemory=false, bool GeneratingReducedBMI=false)
Definition: GeneratePCH.cpp:25
bool isComplete() const
Definition: ASTWriter.h:903
DiagnosticsEngine & getDiagnostics() const
Definition: ASTWriter.h:906
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
HeaderSearch & getHeaderSearchInfo() const
const LangOptions & getLangOpts() const
DiagnosticsEngine & getDiagnostics() const
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile)
Definition: GeneratePCH.cpp:91
virtual Module * getEmittingModule(ASTContext &Ctx) override
Encodes a location in the source.
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Definition: Format.h:5394
#define true
Definition: stdbool.h:21
#define false
Definition: stdbool.h:22
llvm::SmallVector< char, 0 > Data