clang 22.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, 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 ShouldCacheASTInMemory,
32 bool GeneratingReducedBMI)
33 : PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
34 Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
35 Writer(Stream, this->Buffer->Data, ModCache, CodeGenOpts, Extensions,
36 IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
37 AllowASTWithErrors(AllowASTWithErrors),
38 ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
39 this->Buffer->IsComplete = false;
40}
41
43}
44
46 Module *M = nullptr;
47
48 if (PP.getLangOpts().isCompilingModule()) {
51 /*AllowSearch*/ false);
52 if (!M)
53 assert(PP.getDiagnostics().hasErrorOccurred() &&
54 "emitting module but current module doesn't exist");
55 }
56
57 return M;
58}
59
61 return PP.getDiagnostics();
62}
63
65 if (!PP.getHeaderSearchInfo()
68 Subject = &S;
69}
70
72 // Don't create a PCH if there were fatal failures during module loading.
74 return;
75
76 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
77 if (hasErrors && !AllowASTWithErrors)
78 return;
79
81
82 // Errors that do not prevent the PCH from being written should not cause the
83 // overall compilation to fail either.
84 if (AllowASTWithErrors)
86
87 Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot,
88 ShouldCacheASTInMemory);
89
90 Buffer->IsComplete = true;
91}
92
94 return &Writer;
95}
96
98 return &Writer;
99}
100
101void PCHGenerator::anchor() {}
102
104 ModuleCache &ModCache,
105 StringRef OutputFile,
106 const CodeGenOptions &CodeGenOpts,
107 bool GeneratingReducedBMI,
108 bool AllowASTWithErrors)
109 : PCHGenerator(
110 PP, ModCache, OutputFile, llvm::StringRef(),
111 std::make_shared<PCHBuffer>(), CodeGenOpts,
112 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
113 AllowASTWithErrors, /*IncludeTimestamps=*/false,
114 /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
115 GeneratingReducedBMI) {}
116
118 Module *M = Ctx.getCurrentNamedModule();
119 assert(M && M->isNamedModuleUnit() &&
120 "CXX20ModulesGenerator should only be used with C++20 Named modules.");
121 return M;
122}
123
126
127 if (!isComplete())
128 return;
129
130 std::error_code EC;
131 auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), EC);
132 if (EC) {
133 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
134 << getOutputFile() << EC.message() << "\n";
135 return;
136 }
137
138 *OS << getBufferPtr()->Data;
139 OS->flush();
140}
141
142void CXX20ModulesGenerator::anchor() {}
143
144void ReducedBMIGenerator::anchor() {}
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:188
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
Definition: ASTContext.h:1193
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
ASTFileSignature WriteAST(llvm::PointerUnion< Sema *, Preprocessor * > Subject, StringRef OutputFile, Module *WritingModule, StringRef isysroot, bool ShouldCacheASTInMemory=false)
Write a precompiled header or a module with the AST produced by the Sema object, or a dependency scan...
Definition: ASTWriter.cpp:5448
CXX20ModulesGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, const CodeGenOptions &CodeGenOpts, bool GeneratingReducedBMI, bool AllowASTWithErrors)
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
virtual Module * getEmittingModule(ASTContext &Ctx) override
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1529
bool hasErrorOccurred() const
Definition: Diagnostic.h:871
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:606
unsigned ModulesSerializeOnlyPreprocessor
Whether AST files should only contain the preprocessor information.
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
const HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized.
Definition: HeaderSearch.h:384
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:596
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:489
The module cache used for compiling modules implicitly.
Definition: ModuleCache.h:26
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Describes a module or submodule.
Definition: Module.h:144
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:676
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
Definition: ASTWriter.h:977
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:93
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
Definition: GeneratePCH.cpp:64
PCHBuffer * getBufferPtr()
Definition: ASTWriter.h:996
virtual Module * getEmittingModule(ASTContext &Ctx)
Definition: GeneratePCH.cpp:45
StringRef getOutputFile() const
Definition: ASTWriter.h:997
~PCHGenerator() override
Definition: GeneratePCH.cpp:42
PCHGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, const CodeGenOptions &CodeGenOpts, 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
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
Definition: GeneratePCH.cpp:97
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:71
bool isComplete() const
Definition: ASTWriter.h:995
DiagnosticsEngine & getDiagnostics() const
Definition: GeneratePCH.cpp:60
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:145
ModuleLoader & getModuleLoader() const
Retrieve the module loader associated with this preprocessor.
HeaderSearch & getHeaderSearchInfo() const
const LangOptions & getLangOpts() const
DiagnosticsEngine & getDiagnostics() const
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:850
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
#define false
Definition: stdbool.h:26
llvm::SmallVector< char, 0 > Data