[clang] [C++20] [Modules] Don't generate call to an imported module that dont init anything (PR #67638)

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 28 08:37:02 PDT 2023


================
@@ -1245,6 +1245,27 @@ void Sema::ActOnEndOfTranslationUnit() {
       }
     }
 
+    // Now we can decide whether the modules we're building need an initializer.
+    if (Module *CurrentModule = getCurrentModule();
+        CurrentModule && CurrentModule->isInterfaceOrPartition()) {
+      auto DoesModNeedInit = [this](Module *M) {
+        if (!getASTContext().getModuleInitializers(M).empty())
+          return false;
+        for (auto [Exported, _] : M->Exports)
+          if (!Exported->isNamedModuleInterfaceHasNoInit())
+            return false;
+        for (Module *I : M->Imports)
+          if (!I->isNamedModuleInterfaceHasNoInit())
+            return false;
+
+        return true;
+      };
+
+      CurrentModule->NamedModuleHasNoInit = DoesModNeedInit(CurrentModule);
+      for (Module *SubModules : CurrentModule->submodules())
+        CurrentModule->NamedModuleHasNoInit &= DoesModNeedInit(SubModules);
----------------
dwblaikie wrote:

This could be, maybe:
```
CurrentModule->NamedModuleHasNoInit = DoesModNeedInit(CurrentModule) && llvm::all_of(CurrentModule->submodules(), DoesModNeedInit);
```
(be slightly more efficient, since it'd bail out and return false as soon as one module needed init, rather than checking later modules)
Though the `DoesModNeedInit` might need a better name, since you'd expect that to return `true` if the module needs init, but the "no" in "noinit" is confusing/inverting - maybe it'd be better to name that flag "NamedModuleHasInit" - to avoid double negatives, etc?

https://github.com/llvm/llvm-project/pull/67638


More information about the cfe-commits mailing list