[clang] [modules] Fix assert on Clang module import from the global module fragment. (PR #159771)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 19 05:57:29 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-modules
Author: Naveen Seth Hanig (naveen-seth)
<details>
<summary>Changes</summary>
Fixes #<!-- -->159768.
When building a named module interface with `-fmodules` enabled, importing a Clang module from inside the global module fragment causes Clang to crash only on assertion builds.
This fixes the assert and adds a test.
---
Full diff: https://github.com/llvm/llvm-project/pull/159771.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaModule.cpp (+6-1)
- (added) clang/test/Modules/named-module-with-fmodules.cppm (+24)
``````````diff
diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp
index 773bcb225c188..06c6f309ea6d6 100644
--- a/clang/lib/Sema/SemaModule.cpp
+++ b/clang/lib/Sema/SemaModule.cpp
@@ -772,7 +772,12 @@ void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
getLangOpts().CurrentModule, DirectiveLoc, false, false);
(void)ThisModule;
- assert(ThisModule && "was expecting a module if building one");
+ // For named modules, the current module name is not known while parsing the
+ // global module fragment and lookupModule may return null.
+ assert((getLangOpts().getCompilingModule() ==
+ LangOptionsBase::CMK_ModuleInterface ||
+ ThisModule) &&
+ "was expecting a module if building a Clang module");
}
}
diff --git a/clang/test/Modules/named-module-with-fmodules.cppm b/clang/test/Modules/named-module-with-fmodules.cppm
new file mode 100644
index 0000000000000..a4f7fbec3b176
--- /dev/null
+++ b/clang/test/Modules/named-module-with-fmodules.cppm
@@ -0,0 +1,24 @@
+// Checks that Clang modules can be imported from within the global module
+// fragment of a named module interface unit.
+// Fixes #159768.
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// RUN: %clang -std=c++23 -fmodules -fmodule-map-file=%t/module.modulemap \
+// RUN: --precompile %t/A.cppm -o %t/A.pcm
+
+//--- module.modulemap
+module foo { header "foo.h" }
+
+//--- foo.h
+// empty
+
+//--- A.cppm
+module;
+#include "foo.h"
+export module A;
+
+export auto A() -> int { return 42; }
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/159771
More information about the cfe-commits
mailing list