https://github.com/naveen-seth created https://github.com/llvm/llvm-project/pull/159771
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. >From b2b6053fefca001ccd626db3917ef1ee388369e8 Mon Sep 17 00:00:00 2001 From: Naveen Seth Hanig <naveen.ha...@outlook.com> Date: Fri, 19 Sep 2025 14:37:22 +0200 Subject: [PATCH] [modules] Fix assert on Clang module import from the global module fragment. 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. --- clang/lib/Sema/SemaModule.cpp | 7 +++++- .../Modules/named-module-with-fmodules.cppm | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 clang/test/Modules/named-module-with-fmodules.cppm 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; } + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits