Skip to content

Commit 4554cdf

Browse files
authored
[modules] Fix assert on Clang module import from the global module fragment. (#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 extends the test coverage.
1 parent 9b3e2f5 commit 4554cdf

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

clang/lib/Sema/SemaModule.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,12 @@ void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
772772
Module *ThisModule = PP.getHeaderSearchInfo().lookupModule(
773773
getLangOpts().CurrentModule, DirectiveLoc, false, false);
774774
(void)ThisModule;
775-
assert(ThisModule && "was expecting a module if building one");
775+
// For named modules, the current module name is not known while parsing the
776+
// global module fragment and lookupModule may return null.
777+
assert((getLangOpts().getCompilingModule() ==
778+
LangOptionsBase::CMK_ModuleInterface ||
779+
ThisModule) &&
780+
"was expecting a module if building a Clang module");
776781
}
777782
}
778783

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Checks that Clang modules can be imported from within the global module
2+
// fragment of a named module interface unit.
3+
// Fixes #159768.
4+
5+
// RUN: rm -rf %t
6+
// RUN: mkdir -p %t
7+
// RUN: split-file %s %t
8+
9+
// RUN: %clang -std=c++23 -fmodules -fmodule-map-file=%t/module.modulemap \
10+
// RUN: --precompile %t/A.cppm -o %t/A.pcm
11+
12+
//--- module.modulemap
13+
module foo { header "foo.h" }
14+
15+
//--- foo.h
16+
// empty
17+
18+
//--- A.cppm
19+
module;
20+
#include "foo.h"
21+
export module A;
22+
23+
export auto A() -> int { return 42; }
24+

0 commit comments

Comments
 (0)