[clang] 30ffb3e - [clang] Apply -fmacro-prefix-map to anonymous tags in template arguments

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 16 08:47:04 PDT 2023


Author: Dan McGregor
Date: 2023-06-16T08:47:00-07:00
New Revision: 30ffb3e4fbce3132fbf63c62a8ecbb1edbec4fc9

URL: https://github.com/llvm/llvm-project/commit/30ffb3e4fbce3132fbf63c62a8ecbb1edbec4fc9
DIFF: https://github.com/llvm/llvm-project/commit/30ffb3e4fbce3132fbf63c62a8ecbb1edbec4fc9.diff

LOG: [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

Added: 
    clang/test/CodeGenCXX/macro-prefix-map-lambda.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/Expr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 336bd5b6aa2e1..82e3b9d5de38b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -511,6 +511,8 @@ Bug Fixes in This Version
 - CallExpr built for C error-recovery now is always type-dependent. Fixes a
   crash when we encounter a unresolved TypoExpr during diagnostic emission.
   (`#50244 <https://github.com/llvm/llvm-project/issues/50244>_`).
+- 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 1fbb76a19acb2..0d927d083ed20 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -786,7 +786,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 0000000000000..e87f0ab484dc4
--- /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;
+}


        


More information about the cfe-commits mailing list