Skip to content

Commit

Permalink
clang: Apply upstream D152570
Browse files Browse the repository at this point in the history
This fixes real file names appearing in lambda names that are
used as arguments to function templates.

Signed-off-by: Dan McGregor <[email protected]>
  • Loading branch information
dankm committed Jun 16, 2023
1 parent 190da5c commit c0f6078
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
From 98a5709ff5a34a2906536e110d227f83898f550b Mon Sep 17 00:00:00 2001
From: Dan McGregor <[email protected]>
Date: Fri, 16 Jun 2023 08:47:00 -0700
Subject: [PATCH] [clang] Apply -fmacro-prefix-map to anonymous tags in
template arguments

When expanding template arguments for pretty function printing,
such as for __PRETTY_FUNCTION__, make TypePrinter apply
macro-prefix-map remapping to anonymous tags such as lambdas.

Fixes https://github.com/llvm/llvm-project/issues/63219

Reviewed By: MaskRay, aaron.ballman

Differential Revision: https://reviews.llvm.org/D152570

Upstream-Status: Backport
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/AST/Expr.cpp | 14 ++++++++++++++
clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp | 14 ++++++++++++++
3 files changed, 30 insertions(+)
create mode 100644 clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8d67ff904469..bf3ea4a54d01 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -724,6 +724,8 @@ Bug Fixes in This Version
- Fix crash when passing a braced initializer list to a parentehsized aggregate
initialization expression.
(`#63008 <https://github.com/llvm/llvm-project/issues/63008>`_).
+- Apply ``-fmacro-prefix-map`` to anonymous tags in template arguments
+ (`#63219 <https://github.com/llvm/llvm-project/issues/63219>`_).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index e45ae68cd5fe..a65678f5998c 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -783,7 +783,21 @@ std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
Out << "static ";
}

+ class PrettyCallbacks final : public PrintingCallbacks {
+ public:
+ PrettyCallbacks(const LangOptions &LO) : LO(LO) {}
+ std::string remapPath(StringRef Path) const override {
+ SmallString<128> p(Path);
+ LO.remapPathPrefix(p);
+ return std::string(p);
+ }
+
+ private:
+ const LangOptions &LO;
+ };
PrintingPolicy Policy(Context.getLangOpts());
+ PrettyCallbacks PrettyCB(Context.getLangOpts());
+ Policy.Callbacks = &PrettyCB;
std::string Proto;
llvm::raw_string_ostream POut(Proto);

diff --git a/clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp b/clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp
new file mode 100644
index 000000000000..e87f0ab484dc
--- /dev/null
+++ b/clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -fmacro-prefix-map=%p=./UNLIKELY_PATH/empty %s -emit-llvm -o - | FileCheck %s
+
+template<typename f>
+auto lambdatest(f&& cb) {
+ const char *s = __PRETTY_FUNCTION__;
+ return s;
+}
+
+int main() {
+ auto *s = lambdatest([](){});
+// CHECK: @"__PRETTY_FUNCTION__._Z10lambdatestIZ4mainE3$_0EDaOT_" = private unnamed_addr constant [{{[0-9]+}} x i8] c"auto lambdatest(f &&) [f = (lambda at ./UNLIKELY_PATH/empty{{/|\\\\}}{{.*}}.cpp:[[#@LINE-1]]:24)]\00", align 1
+
+ return 0;
+}
--
2.40.1

1 change: 1 addition & 0 deletions recipes-devtools/clang/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SRC_URI = "\
file://0035-compiler-rt-Fix-stat-struct-s-size-for-O32-ABI.patch \
file://0036-compiler-rt-Undef-_TIME_BITS-along-with-_FILE_OFFSET.patch \
file://0037-clang-Call-printName-to-get-name-of-Decl.patch \
file://0038-clang-Apply-fmacro-prefix-map-to-anonymous-tags-in-t.patch \
"
# Fallback to no-PIE if not set
GCCPIE ??= ""
Expand Down

0 comments on commit c0f6078

Please sign in to comment.