[clang] [clang][modules] Include `-fmodule-map-file=` in context hash (PR #227776)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 13 12:55:03 PDT 2026
https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/227776
>From 44795e262e13419149af4042252432d3947ea718 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 30 Sep 2026 09:35:59 -0700
Subject: [PATCH 1/2] [clang][modules] Include -fmodule-map-file= in context
hash
---
clang/lib/Frontend/CompilerInvocation.cpp | 2 +
.../Modules/context-hash-module-map-file.c | 41 +++++++++++++++++++
2 files changed, 43 insertions(+)
create mode 100644 clang/test/Modules/context-hash-module-map-file.c
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 649a1a18b4ebe8..3122fec7683586 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -5337,6 +5337,8 @@ std::string CompilerInvocation::computeContextHash() const {
HBuilder.add(UserEntry);
}
+ HBuilder.addRange(getFrontendOpts().ModuleMapFiles);
+
HBuilder.addRange(hsOpts.VFSOverlayFiles);
const DiagnosticOptions &diagOpts = getDiagnosticOpts();
diff --git a/clang/test/Modules/context-hash-module-map-file.c b/clang/test/Modules/context-hash-module-map-file.c
new file mode 100644
index 00000000000000..07245b11415998
--- /dev/null
+++ b/clang/test/Modules/context-hash-module-map-file.c
@@ -0,0 +1,41 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// Build A without knowing about the module map for B.
+//
+// This means "b.h" is included textually and embedded into the PCM for module A
+// with B_VALUE set to 1.
+//
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
+// RUN: -fmodules-strict-context-hash -I %t/a -I %t/b %t/tu.c -E -o -
+
+// Load A with module map for B being known.
+//
+// This should make it so that module A turns the header inclusion into a module
+// import of B, which gets built with clean preprocessor state, resulting in the
+// macro B_VALUE set to 2.
+//
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
+// RUN: -fmodules-strict-context-hash -I %t/a -I %t/b %t/tu.c -E -o - \
+// RUN: -fmodule-map-file=%t/b/b.modulemap | FileCheck %s
+//
+// CHECK: int value = 2;
+
+//--- tu.c
+#include "a.h"
+int value = B_VALUE;
+
+//--- a/module.modulemap
+module A { header "a.h" export * }
+//--- a/a.h
+#define WITHIN_A
+#include "b.h"
+
+//--- b/b.modulemap
+module B { header "b.h" }
+//--- b/b.h
+#ifdef WITHIN_A
+#define B_VALUE 1
+#else
+#define B_VALUE 2
+#endif
>From bddf15285598a63f5ee353c11695c4f6931c45dc Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Tue, 13 Oct 2026 11:24:22 -0700
Subject: [PATCH 2/2] Check A gets recompiled
---
clang/test/Modules/context-hash-module-map-file.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/test/Modules/context-hash-module-map-file.c b/clang/test/Modules/context-hash-module-map-file.c
index 07245b11415998..28ac002811f7d2 100644
--- a/clang/test/Modules/context-hash-module-map-file.c
+++ b/clang/test/Modules/context-hash-module-map-file.c
@@ -17,10 +17,13 @@
//
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \
// RUN: -fmodules-strict-context-hash -I %t/a -I %t/b %t/tu.c -E -o - \
-// RUN: -fmodule-map-file=%t/b/b.modulemap | FileCheck %s
+// RUN: -fmodule-map-file=%t/b/b.modulemap -Rmodule-build 2>&1 | FileCheck %s
//
+// CHECK: remark: building module 'A'
// CHECK: int value = 2;
+// RUN: ls %t/cache/*/A-*.pcm | count 2
+
//--- tu.c
#include "a.h"
int value = B_VALUE;
More information about the cfe-commits
mailing list