[clang] [Clang] Produce deterministic hash for anonymous namespaces. (PR #194542)
Vladimir Vereschaka via cfe-commits
cfe-commits at lists.llvm.org
Wed May 6 09:29:10 PDT 2026
https://github.com/vvereschaka updated https://github.com/llvm/llvm-project/pull/194542
>From ce18d063f2c51e94a30cc48300a549d7f48ba654 Mon Sep 17 00:00:00 2001
From: Vladimir Vereschaka <vvereschaka at accesssoftek.com>
Date: Mon, 27 Apr 2026 21:25:36 -0700
Subject: [PATCH 1/4] [Clang] Produce determenistic hash for anonymous
namespaces.
This change adds a path substitution for the main module file during anonymous
namespace hash generation using the prefix map specified by -fmacro-prefix-map
option. That ensures deterministic symbol mangling for reproducible builds.
---
clang/lib/AST/MicrosoftMangle.cpp | 8 +++++++-
clang/test/AST/anon-ns-determ-hash.cpp | 9 +++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
create mode 100644 clang/test/AST/anon-ns-determ-hash.cpp
diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp
index 20c52969d7024..7d0c60d57253c 100644
--- a/clang/lib/AST/MicrosoftMangle.cpp
+++ b/clang/lib/AST/MicrosoftMangle.cpp
@@ -29,6 +29,7 @@
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
+#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CRC.h"
@@ -503,8 +504,13 @@ MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context,
// which are something like "?A0x01234567@".
SourceManager &SM = Context.getSourceManager();
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID())) {
+ SmallString<256> Path(FE->getName());
+ // Do a path substitution from the MacroPrefixMap if needed.
+ clang::Preprocessor::processPathForFileMacro(Path, Context.getLangOpts(),
+ Context.getTargetInfo());
+
// Truncate the hash so we get 8 characters of hexadecimal.
- uint32_t TruncatedHash = uint32_t(xxh3_64bits(FE->getName()));
+ uint32_t TruncatedHash = uint32_t(xxh3_64bits(Path));
AnonymousNamespaceHash = llvm::utohexstr(TruncatedHash);
} else {
// If we don't have a path to the main file, we'll just use 0.
diff --git a/clang/test/AST/anon-ns-determ-hash.cpp b/clang/test/AST/anon-ns-determ-hash.cpp
new file mode 100644
index 0000000000000..aa16455d80154
--- /dev/null
+++ b/clang/test/AST/anon-ns-determ-hash.cpp
@@ -0,0 +1,9 @@
+// Check anonymous namespace deterministic hash generation.
+// RUN: %clang_cc1 -std=c++2a -ast-dump=json -fmacro-prefix-map=%p=/static-path %s | FileCheck %s
+
+namespace {
+ int internal_ns_var = 0;
+}
+
+// The 0xA110234F hash value must be identical on any system with proper path substitution.
+// CHECK: "mangledName": "?internal_ns_var@?A0xA110234F@@3HA",
>From acf285bf0dfd8ccfd10a1cc3a279a0b84a93325d Mon Sep 17 00:00:00 2001
From: Vladimir Vereschaka <vvereschaka at accesssoftek.com>
Date: Tue, 28 Apr 2026 15:39:05 -0700
Subject: [PATCH 2/4] Apply proper *-msvc triple to enable namespace hash
within the symbols.
---
clang/test/AST/anon-ns-determ-hash.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/test/AST/anon-ns-determ-hash.cpp b/clang/test/AST/anon-ns-determ-hash.cpp
index aa16455d80154..4cfca7438ba4a 100644
--- a/clang/test/AST/anon-ns-determ-hash.cpp
+++ b/clang/test/AST/anon-ns-determ-hash.cpp
@@ -1,5 +1,8 @@
// Check anonymous namespace deterministic hash generation.
-// RUN: %clang_cc1 -std=c++2a -ast-dump=json -fmacro-prefix-map=%p=/static-path %s | FileCheck %s
+// NOTE: applies to *-msvc targets only.
+
+// RUN: %clang_cc1 -triple=x86_64-pc-windows-msvc -std=c++2a -ast-dump=json -fmacro-prefix-map=%p=/static-path %s | FileCheck %s
+// REQUIRES: x86-registered-target
namespace {
int internal_ns_var = 0;
>From ba57d7e0a8cfba8c9a16d548a44b703d933ad423 Mon Sep 17 00:00:00 2001
From: Vladimir Vereschaka <vvereschaka at accesssoftek.com>
Date: Tue, 5 May 2026 22:00:48 -0700
Subject: [PATCH 3/4] Update clang/docs/ReleaseNotes.rst accordingly.
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ec64c2008d89b..84f4e7abfd747 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -636,6 +636,8 @@ Windows Support
- Clang now defines the ``_MSVC_TRADITIONAL`` macro as ``1`` when emulating MSVC
19.15 (Visual Studio 2017 version 15.8) and later. (#GH47114)
+- ``-fmacro-prefix-map=`` (``-ffile-prefix-map=``) now affects an anonymous namespace hash generation
+ for the MSVC targets and allows deterministic symbol mangling for reproducible builds. (#GH194542)
LoongArch Support
^^^^^^^^^^^^^^^^^
>From 9c95f9432718099facb17e229af02a56975c4374 Mon Sep 17 00:00:00 2001
From: Vladimir Vereschaka <vvereschaka at accesssoftek.com>
Date: Wed, 6 May 2026 09:28:59 -0700
Subject: [PATCH 4/4] Update clang/docs/ReleaseNotes.rst with suggested
changes.
Co-authored-by: Corentin Jabot <corentinjabot at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 84f4e7abfd747..8c856f27852c6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -637,7 +637,7 @@ Windows Support
- Clang now defines the ``_MSVC_TRADITIONAL`` macro as ``1`` when emulating MSVC
19.15 (Visual Studio 2017 version 15.8) and later. (#GH47114)
- ``-fmacro-prefix-map=`` (``-ffile-prefix-map=``) now affects an anonymous namespace hash generation
- for the MSVC targets and allows deterministic symbol mangling for reproducible builds. (#GH194542)
+ for the MSVC targets and allows deterministic symbol mangling for reproducible builds.
LoongArch Support
^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list