clang 20.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), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33 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 return PP.getDiagnostics();
61}
62
64 if (!PP.getHeaderSearchInfo()
67 Subject = &S;
68}
69
71 // Don't create a PCH if there were fatal failures during module loading.
73 return;
74
75 bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
76 if (hasErrors && !AllowASTWithErrors)
77 return;
78
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)
85
86 Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot,
87 ShouldCacheASTInMemory);
88
89 Buffer->IsComplete = true;
90}
91
93 return &Writer;
94}
95
97 return &Writer;
98}
99
100void PCHGenerator::anchor() {}
101
103 InMemoryModuleCache &ModuleCache,
104 StringRef OutputFile,
105 bool GeneratingReducedBMI)
106 : PCHGenerator(
107 PP, ModuleCache, OutputFile, llvm::StringRef(),
108 std::make_shared<PCHBuffer>(),
109 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
110 /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
111 /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
112 GeneratingReducedBMI) {}
113
115 Module *M = Ctx.getCurrentNamedModule();
116 assert(M && M->isNamedModuleUnit() &&
117 "CXX20ModulesGenerator should only be used with C++20 Named modules.");
118 return M;
119}
120
122 // FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
123 // since this is not about searching header really.
124 HeaderSearchOptions &HSOpts =
126 HSOpts.ModulesSkipDiagnosticOptions = true;
127 HSOpts.ModulesSkipHeaderSearchPaths = true;
128
130
131 if (!isComplete())
132 return;
133
134 std::error_code EC;
135 auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), EC);
136 if (EC) {
137 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
138 << getOutputFile() << EC.message() << "\n";
139 return;
140 }
141
142 *OS << getBufferPtr()->Data;
143 OS->flush();
144}
145
146void CXX20ModulesGenerator::anchor() {}
147
148void 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:1133
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:5106
CXX20ModulesGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile, bool GeneratingReducedBMI)
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
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:1493
bool hasErrorOccurred() const
Definition: Diagnostic.h:866
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:595
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 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.
HeaderSearchOptions & getHeaderSearchOpts() const
Retrieve the header-search options with which this header search was initialized.
Definition: HeaderSearch.h:370
In-memory cache for modules.
bool isCompilingModule() const
Are we compiling a module?
Definition: LangOptions.h:649
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:554
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Describes a module or submodule.
Definition: Module.h:115
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Definition: Module.h:647
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
Definition: ASTWriter.h:948
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
Definition: GeneratePCH.cpp:92
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
Definition: GeneratePCH.cpp:63
PCHBuffer * getBufferPtr()
Definition: ASTWriter.h:967
Preprocessor & getPreprocessor()
Definition: ASTWriter.h:970
virtual Module * getEmittingModule(ASTContext &Ctx)
Definition: GeneratePCH.cpp:44
StringRef getOutputFile() const
Definition: ASTWriter.h:968
~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:96
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: GeneratePCH.cpp:70
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:966
DiagnosticsEngine & getDiagnostics() const
Definition: GeneratePCH.cpp:59
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:138
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:463
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