Skip to content

Commit 9744909

Browse files
committed
[NFC] [C++20] [Modules] Refactor Module::getGlobalModuleFragment and Module::getPrivateModuleFragment
The original implementation of `Module::getGlobalModuleFragment` and `Module::getPrivateModuleFragment` tried to find the global module fragment and the private module fragment by comparing strings, which smells bad. This patch tries to improve this.
1 parent 34ee53c commit 9744909

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ class alignas(8) Module {
587587
return Kind == ModuleInterfaceUnit || isModulePartition();
588588
}
589589

590+
/// Is this a C++20 named module unit.
591+
bool isNamedModuleUnit() const {
592+
return isInterfaceOrPartition() || isModuleImplementation();
593+
}
594+
590595
bool isModuleInterfaceUnit() const {
591596
return Kind == ModuleInterfaceUnit || Kind == ModulePartitionInterface;
592597
}
@@ -720,13 +725,13 @@ class alignas(8) Module {
720725
/// one.
721726
///
722727
/// \returns The GMF sub-module if found, or NULL otherwise.
723-
Module *getGlobalModuleFragment() { return findSubmodule("<global>"); }
728+
Module *getGlobalModuleFragment() const;
724729

725730
/// Get the Private Module Fragment (sub-module) for this module, it there is
726731
/// one.
727732
///
728733
/// \returns The PMF sub-module if found, or NULL otherwise.
729-
Module *getPrivateModuleFragment() { return findSubmodule("<private>"); }
734+
Module *getPrivateModuleFragment() const;
730735

731736
/// Determine whether the specified module would be visible to
732737
/// a lookup at the end of this module.

clang/lib/Basic/Module.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ Module *Module::findOrInferSubmodule(StringRef Name) {
370370
return Result;
371371
}
372372

373+
Module *Module::getGlobalModuleFragment() const {
374+
assert(isNamedModuleUnit() && "We should only query the global module "
375+
"fragment from the C++ 20 Named modules");
376+
377+
for (auto *SubModule : SubModules)
378+
if (SubModule->isExplicitGlobalModule())
379+
return SubModule;
380+
381+
return nullptr;
382+
}
383+
384+
Module *Module::getPrivateModuleFragment() const {
385+
assert(isNamedModuleUnit() && "We should only query the private module "
386+
"fragment from the C++ 20 Named modules");
387+
388+
for (auto *SubModule : SubModules)
389+
if (SubModule->isPrivateModule())
390+
return SubModule;
391+
392+
return nullptr;
393+
}
394+
373395
void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
374396
// All non-explicit submodules are exported.
375397
for (std::vector<Module *>::const_iterator I = SubModules.begin(),

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,6 +2915,9 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
29152915
}
29162916

29172917
void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
2918+
assert(Primary->isNamedModuleUnit() &&
2919+
"We should only emit module initializers for named modules.");
2920+
29182921
// Emit the initializers in the order that sub-modules appear in the
29192922
// source, first Global Module Fragments, if present.
29202923
if (auto GMF = Primary->getGlobalModuleFragment()) {

0 commit comments

Comments
 (0)