Skip to content

Commit 5d7796e

Browse files
committed
[NFC] [C++20] [Modules] Refactor ReducedBMIGenerator
Changes: - Don't lookup the emitting module from HeaderSearch. We will use the module from the ASTContext directly. - Remove some useless arguments. Let's addback in the future if required.
1 parent a62222f commit 5d7796e

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,8 @@ class PCHGenerator : public SemaConsumer {
868868
return SemaPtr->getDiagnostics();
869869
}
870870

871+
virtual Module *getEmittingModule(ASTContext &Ctx);
872+
871873
public:
872874
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
873875
StringRef OutputFile, StringRef isysroot,
@@ -887,10 +889,12 @@ class PCHGenerator : public SemaConsumer {
887889
};
888890

889891
class ReducedBMIGenerator : public PCHGenerator {
892+
protected:
893+
virtual Module *getEmittingModule(ASTContext &Ctx) override;
894+
890895
public:
891896
ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
892-
StringRef OutputFile, std::shared_ptr<PCHBuffer> Buffer,
893-
bool IncludeTimestamps);
897+
StringRef OutputFile);
894898

895899
void HandleTranslationUnit(ASTContext &Ctx) override;
896900
};

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,9 @@ GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
293293
std::unique_ptr<ASTConsumer>
294294
GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
295295
StringRef InFile) {
296-
auto Buffer = std::make_shared<PCHBuffer>();
297-
return std::make_unique<ReducedBMIGenerator>(
298-
CI.getPreprocessor(), CI.getModuleCache(),
299-
CI.getFrontendOpts().OutputFile, Buffer,
300-
/*IncludeTimestamps=*/+CI.getFrontendOpts().IncludeTimestamps);
296+
return std::make_unique<ReducedBMIGenerator>(CI.getPreprocessor(),
297+
CI.getModuleCache(),
298+
CI.getFrontendOpts().OutputFile);
301299
}
302300

303301
bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) {

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ PCHGenerator::PCHGenerator(
4141
PCHGenerator::~PCHGenerator() {
4242
}
4343

44+
Module *PCHGenerator::getEmittingModule(ASTContext &) {
45+
Module *M = nullptr;
46+
47+
if (PP.getLangOpts().isCompilingModule()) {
48+
M = PP.getHeaderSearchInfo().lookupModule(PP.getLangOpts().CurrentModule,
49+
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+
4459
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
4560
// Don't create a PCH if there were fatal failures during module loading.
4661
if (PP.getModuleLoader().HadFatalFailure)
@@ -50,16 +65,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
5065
if (hasErrors && !AllowASTWithErrors)
5166
return;
5267

53-
Module *Module = nullptr;
54-
if (PP.getLangOpts().isCompilingModule()) {
55-
Module = PP.getHeaderSearchInfo().lookupModule(
56-
PP.getLangOpts().CurrentModule, SourceLocation(),
57-
/*AllowSearch*/ false);
58-
if (!Module) {
59-
assert(hasErrors && "emitting module but current module doesn't exist");
60-
return;
61-
}
62-
}
68+
Module *Module = getEmittingModule(Ctx);
6369

6470
// Errors that do not prevent the PCH from being written should not cause the
6571
// overall compilation to fail either.
@@ -84,16 +90,22 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
8490

8591
ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP,
8692
InMemoryModuleCache &ModuleCache,
87-
StringRef OutputFile,
88-
std::shared_ptr<PCHBuffer> Buffer,
89-
bool IncludeTimestamps)
93+
StringRef OutputFile)
9094
: PCHGenerator(
91-
PP, ModuleCache, OutputFile, llvm::StringRef(), Buffer,
95+
PP, ModuleCache, OutputFile, llvm::StringRef(),
96+
std::make_shared<PCHBuffer>(),
9297
/*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
93-
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/IncludeTimestamps,
98+
/*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
9499
/*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
95100
/*GeneratingReducedBMI=*/true) {}
96101

102+
Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
103+
Module *M = Ctx.getCurrentNamedModule();
104+
assert(M->isNamedModuleUnit() &&
105+
"ReducedBMIGenerator should only be used with C++20 Named modules.");
106+
return M;
107+
}
108+
97109
void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
98110
PCHGenerator::HandleTranslationUnit(Ctx);
99111

0 commit comments

Comments
 (0)