Author: Chuanqi Xu Date: 2023-09-28T14:06:02+08:00 New Revision: 9744909a126ead515c433097c0b5f76c98e9a5b4
URL: https://github.com/llvm/llvm-project/commit/9744909a126ead515c433097c0b5f76c98e9a5b4 DIFF: https://github.com/llvm/llvm-project/commit/9744909a126ead515c433097c0b5f76c98e9a5b4.diff LOG: [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. Added: Modified: clang/include/clang/Basic/Module.h clang/lib/Basic/Module.cpp clang/lib/CodeGen/CodeGenModule.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h index 7f1eb6faecf4e75..edbd2dfc2973e92 100644 --- a/clang/include/clang/Basic/Module.h +++ b/clang/include/clang/Basic/Module.h @@ -587,6 +587,11 @@ class alignas(8) Module { return Kind == ModuleInterfaceUnit || isModulePartition(); } + /// Is this a C++20 named module unit. + bool isNamedModuleUnit() const { + return isInterfaceOrPartition() || isModuleImplementation(); + } + bool isModuleInterfaceUnit() const { return Kind == ModuleInterfaceUnit || Kind == ModulePartitionInterface; } @@ -720,13 +725,13 @@ class alignas(8) Module { /// one. /// /// \returns The GMF sub-module if found, or NULL otherwise. - Module *getGlobalModuleFragment() { return findSubmodule("<global>"); } + Module *getGlobalModuleFragment() const; /// Get the Private Module Fragment (sub-module) for this module, it there is /// one. /// /// \returns The PMF sub-module if found, or NULL otherwise. - Module *getPrivateModuleFragment() { return findSubmodule("<private>"); } + Module *getPrivateModuleFragment() const; /// Determine whether the specified module would be visible to /// a lookup at the end of this module. diff --git a/clang/lib/Basic/Module.cpp b/clang/lib/Basic/Module.cpp index 4ffdb6a5f717cf3..4e6fbfc754b9173 100644 --- a/clang/lib/Basic/Module.cpp +++ b/clang/lib/Basic/Module.cpp @@ -370,6 +370,28 @@ Module *Module::findOrInferSubmodule(StringRef Name) { return Result; } +Module *Module::getGlobalModuleFragment() const { + assert(isNamedModuleUnit() && "We should only query the global module " + "fragment from the C++ 20 Named modules"); + + for (auto *SubModule : SubModules) + if (SubModule->isExplicitGlobalModule()) + return SubModule; + + return nullptr; +} + +Module *Module::getPrivateModuleFragment() const { + assert(isNamedModuleUnit() && "We should only query the private module " + "fragment from the C++ 20 Named modules"); + + for (auto *SubModule : SubModules) + if (SubModule->isPrivateModule()) + return SubModule; + + return nullptr; +} + void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { // All non-explicit submodules are exported. for (std::vector<Module *>::const_iterator I = SubModules.begin(), diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 25945759e23f2c5..6073a2749449a6d 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2915,6 +2915,9 @@ static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, } void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) { + assert(Primary->isNamedModuleUnit() && + "We should only emit module initializers for named modules."); + // Emit the initializers in the order that sub-modules appear in the // source, first Global Module Fragments, if present. if (auto GMF = Primary->getGlobalModuleFragment()) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits