[clang] [Clang] Use the last macro prefix map for duplicate prefixes (PR #249124)
Dillon Amburgey via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 22:54:24 PST 2026
https://github.com/dillona updated https://github.com/llvm/llvm-project/pull/249124
>From 1792b962a8e427f85ce08820510f2e07e768c295 Mon Sep 17 00:00:00 2001
From: Dillon Amburgey <dillona at dillona.com>
Date: Fri, 4 Dec 2026 18:41:00 -0500
Subject: [PATCH] [Clang] Use the last macro prefix map for duplicate prefixes
Use the last destination when multiple macro prefix maps have the same source prefix. This makes repeated -ffile-prefix-map options consistent with their debug and coverage mappings.
Fixes #157721
Assisted-by: OpenAI Codex
---
clang/docs/ReleaseNotes.md | 1 +
clang/lib/Frontend/CompilerInvocation.cpp | 4 ++--
clang/test/Preprocessor/file_test.c | 2 ++
3 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 67f00e57a85d39..b4acef06540023 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1064,6 +1064,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed `#embed` of a missing file not being diagnosed if `-MD` is used. (#GH247359)
- Fixed `__has_embed` return value with `limit` greater than the file size. (#GH247344)
- Fixed a C2y rejects-valid where only a single `label:` per loop can be used with `break label;`. (#GH246505)
+- Fixed `-ffile-prefix-map` and `-fmacro-prefix-map` to use the last destination when the same source prefix is specified more than once, matching GCC. (#GH157721)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 194ce26e0b5bb4..cbc5b1acb14b43 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4629,8 +4629,8 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
for (const auto &A : Args.getAllArgValues(OPT_fmacro_prefix_map_EQ)) {
auto Split = StringRef(A).split('=');
- Opts.MacroPrefixMap.insert(
- {std::string(Split.first), std::string(Split.second)});
+ Opts.MacroPrefixMap.insert_or_assign(std::string(Split.first),
+ std::string(Split.second));
}
Opts.UseTargetPathSeparator =
diff --git a/clang/test/Preprocessor/file_test.c b/clang/test/Preprocessor/file_test.c
index 1e7e1dfe96dd94..cc4a2df8660683 100644
--- a/clang/test/Preprocessor/file_test.c
+++ b/clang/test/Preprocessor/file_test.c
@@ -1,6 +1,8 @@
// UNSUPPORTED: system-windows, system-cygwin
// RUN: %clang -E -ffile-prefix-map=%p=/UNLIKELY_PATH/empty -c -o - %s | FileCheck %s
// RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH/empty -c -o - %s | FileCheck %s
+// RUN: %clang -E -ffile-prefix-map=%p=/first -ffile-prefix-map=%p=/UNLIKELY_PATH/empty %s | FileCheck %s
+// RUN: %clang -E -ffile-prefix-map=%p=/first -fmacro-prefix-map=%p=/UNLIKELY_PATH/empty %s | FileCheck %s
// RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL
// RUN: %clang -E -fmacro-prefix-map=%p/= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE
// RUN: %clang -E -fno-file-reproducible -fmacro-prefix-map=%p/= -target x86_64-pc-win32 -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE
More information about the cfe-commits
mailing list