[llvm-branch-commits] [clang] release/23.x: [clang][modulemap] Fix crashes loading pcms (#218011) (PR #224159)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Sep 16 15:50:45 PDT 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/224159
Backport cd1abefa98daa37334eb4f8e48f1956fde859b1a
Requested by: @Bigcheese
>From 47d985f578dd621434cfb4156f5ca547cd7b7b03 Mon Sep 17 00:00:00 2001
From: Keith Smiley <keithbsmiley at gmail.com>
Date: Thu, 27 Aug 2026 16:42:12 -0700
Subject: [PATCH] [clang][modulemap] Fix crashes loading pcms (#218011)
Previously in the case that the directory was missing, loading a pcm
would crash. Now it attempts to load absolute paths first, and
gracefully ignores missing directories.
Follow up to https://github.com/llvm/llvm-project/pull/181916
Fixes: https://github.com/llvm/llvm-project/issues/215931
Assisted By: codex
(cherry picked from commit cd1abefa98daa37334eb4f8e48f1956fde859b1a)
---
clang/lib/Lex/ModuleMap.cpp | 22 +++++++++++---------
clang/test/Modules/pr215931.m | 38 +++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 10 deletions(-)
create mode 100644 clang/test/Modules/pr215931.m
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 6c07386f89010..9e0202409dfb2 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -175,10 +175,6 @@ static void appendSubframeworkPaths(Module *Mod,
OptionalFileEntryRef ModuleMap::findHeader(
Module *M, const Module::UnresolvedHeaderDirective &Header,
SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) {
- // Search for the header file within the module's home directory.
- auto Directory = M->Directory;
- SmallString<128> FullPathName(Directory->getName());
-
auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef {
auto File = SourceMgr.getFileManager().getOptionalFileRef(Filename);
if (!File || (Header.Size && File->getSize() != *Header.Size) ||
@@ -187,6 +183,18 @@ OptionalFileEntryRef ModuleMap::findHeader(
return *File;
};
+ if (llvm::sys::path::is_absolute(Header.FileName)) {
+ RelativePathName.clear();
+ RelativePathName.append(Header.FileName.begin(), Header.FileName.end());
+ return GetFile(Header.FileName);
+ }
+
+ // Search for the header file within the module's home directory.
+ auto Directory = M->Directory;
+ if (!Directory)
+ return std::nullopt;
+ SmallString<128> FullPathName(Directory->getName());
+
auto GetFrameworkFile = [&]() -> OptionalFileEntryRef {
unsigned FullPathLength = FullPathName.size();
appendSubframeworkPaths(M, RelativePathName);
@@ -215,12 +223,6 @@ OptionalFileEntryRef ModuleMap::findHeader(
return GetFile(FullPathName);
};
- if (llvm::sys::path::is_absolute(Header.FileName)) {
- RelativePathName.clear();
- RelativePathName.append(Header.FileName.begin(), Header.FileName.end());
- return GetFile(Header.FileName);
- }
-
if (M->isPartOfFramework())
return GetFrameworkFile();
diff --git a/clang/test/Modules/pr215931.m b/clang/test/Modules/pr215931.m
new file mode 100644
index 0000000000000..7a8a5476e4a9a
--- /dev/null
+++ b/clang/test/Modules/pr215931.m
@@ -0,0 +1,38 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: cd %t
+
+// A PCM whose paths are relative to the consumer's working directory should
+// load successfully even though it does not contain a MODULE_DIRECTORY record.
+// RUN: %clang_cc1 -emit-module -x objective-c -fmodules \
+// RUN: -fno-implicit-modules -fmodule-file-home-is-cwd \
+// RUN: -fmodule-name=Repro mod/module.modulemap -o Repro-relative.pcm
+// RUN: %clang_cc1 -fsyntax-only -x objective-c -fmodules \
+// RUN: -fno-implicit-modules -fmodule-file-home-is-cwd \
+// RUN: -fmodule-file=Repro=Repro-relative.pcm use.m
+
+// An explicitly loaded PCM should also not crash if its absolute module home
+// directory has been removed.
+// RUN: %clang_cc1 -emit-module -x objective-c -fmodules \
+// RUN: -fno-implicit-modules -fmodule-name=Repro \
+// RUN: mod/module.modulemap -o Repro-absolute.pcm
+// RUN: rm -rf mod
+// RUN: %clang_cc1 -fsyntax-only -x objective-c -fmodules \
+// RUN: -fno-implicit-modules -fmodule-file=Repro=Repro-absolute.pcm use.m
+
+//--- mod/module.modulemap
+module Repro {
+ umbrella header "Repro.h"
+ export *
+ module * { export * }
+}
+
+//--- mod/Repro.h
+#include "sub.h"
+
+//--- mod/sub.h
+static inline int repro_answer(void) { return 42; }
+
+//--- use.m
+ at import Repro;
+int main(void) { return repro_answer(); }
More information about the llvm-branch-commits
mailing list