aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/HeaderSearch.cpp
diff options
context:
space:
mode:
authorZezhengLi <Zezheng.Li@linux.alibaba.com>2022-02-11 11:22:04 +0800
committerChuanqi Xu <yedeng.yd@linux.alibaba.com>2022-02-11 11:22:50 +0800
commitd7969012e40a0906512b5bb304fc8b245386bb15 (patch)
treeaeafe5668aba5ec727dc8574491d2feeb955cdb8 /clang/lib/Lex/HeaderSearch.cpp
parentce420820c815e806bab9c5f17cb3b829a616548a (diff)
downloadllvm-d7969012e40a0906512b5bb304fc8b245386bb15.zip
llvm-d7969012e40a0906512b5bb304fc8b245386bb15.tar.gz
llvm-d7969012e40a0906512b5bb304fc8b245386bb15.tar.bz2
[C++20] [Modules] Check if modulemap exists to avoid crash in implicit used C++ module
An impilt used of C++ module without prebuild path may cause crash. For example: ``` // ./dir1/C.cppm export module C; // ./dir2/B.cppm export module B; import C; // ./A.cpp import B; import C; ``` When we compile A.cpp without the prebuild path of C.pcm, the compiler will crash. ``` clang++ -std=c++20 --precompile -c ./dir1/C.cppm -o ./dir1/C.pcm clang++ -std=c++20 --precompile -fprebuilt-module-path=./dir2 -c ./dir2/B.cppm -o ./dir2/B.pcm clang++ -std=c++20 -fprebuilt-module-path=./dir2 A.cpp ``` The prebuilt path of module C is cached when import module B, and in the function HeaderSearch::getCachedModuleFileName, the compiler try to get the filename by modulemap without check if modulemap exists, and there is no modulemap in C++ module. Reviewed By: ChuanqiXu Differential review: https://reviews.llvm.org/D119426
Diffstat (limited to 'clang/lib/Lex/HeaderSearch.cpp')
-rw-r--r--clang/lib/Lex/HeaderSearch.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 19e284f..500f569 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());
}