Author: Vladimir Vereschaka Date: 2026-05-06T13:57:01-07:00 New Revision: 625cf4026ffb45db3a53406cd1b1ec5172ff2bf3
URL: https://github.com/llvm/llvm-project/commit/625cf4026ffb45db3a53406cd1b1ec5172ff2bf3 DIFF: https://github.com/llvm/llvm-project/commit/625cf4026ffb45db3a53406cd1b1ec5172ff2bf3.diff LOG: [Clang] Produce deterministic hash for anonymous namespaces. (#194542) 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. --------- Co-authored-by: Corentin Jabot <[email protected]> Added: clang/test/AST/anon-ns-determ-hash.cpp Modified: clang/docs/ReleaseNotes.rst clang/lib/AST/MicrosoftMangle.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4de2b90cbabe5..cb19b80b7e994 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -653,6 +653,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. LoongArch Support ^^^^^^^^^^^^^^^^^ 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..4cfca7438ba4a --- /dev/null +++ b/clang/test/AST/anon-ns-determ-hash.cpp @@ -0,0 +1,12 @@ +// Check anonymous namespace deterministic hash generation. +// 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; +} + +// The 0xA110234F hash value must be identical on any system with proper path substitution. +// CHECK: "mangledName": "?internal_ns_var@?A0xA110234F@@3HA", _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
