diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 19e284f04b38c..500f5693911b4 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -172,6 +172,10 @@ void HeaderSearch::getHeaderMapFileNames( std::string HeaderSearch::getCachedModuleFileName(Module *Module) { const FileEntry *ModuleMap = getModuleMap().getModuleMapFileForUniquing(Module); + // The ModuleMap maybe a nullptr, when we load a cached C++ module without + // *.modulemap file. In this case, just return an empty string. + if (ModuleMap == nullptr) + return {}; return getCachedModuleFileName(Module->Name, ModuleMap->getName()); } diff --git a/clang/test/Modules/implicit-module-with-missing-path.cpp b/clang/test/Modules/implicit-module-with-missing-path.cpp new file mode 100644 index 0000000000000..2ce67d58d3d67 --- /dev/null +++ b/clang/test/Modules/implicit-module-with-missing-path.cpp @@ -0,0 +1,12 @@ +// This tests that the compiler wouldn't crash if the module path misses + +// RUN: rm -rf %t +// RUN: mkdir -p %t/subdir +// RUN: echo "export module C;" >> %t/subdir/C.cppm +// RUN: echo -e "export module B;\nimport C;" >> %t/B.cppm +// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/subdir/C.cppm -o %t/subdir/C.pcm +// RUN: %clang_cc1 -std=c++20 -emit-module-interface -fprebuilt-module-path=%t/subdir %t/B.cppm -o %t/B.pcm +// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %s -fsyntax-only -verify + +import B; +import C; // expected-error {{module 'C' is needed but has not been provided, and implicit use of module files is disabled}}