[PATCH] D117321: [clang] Remap file path in __PRETTY_FUNCTION__

Jyun-Yan You via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 07:51:51 PST 2022


jyyou.tw created this revision.
jyyou.tw requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

__PRETTY_FUNCTION__ may have file path in template argument
but -fmacro-prefix-map can't remap it. This patch tries to fix the problem.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117321

Files:
  clang/lib/AST/Expr.cpp
  clang/test/CodeGenCXX/macro-prefix-map.cpp


Index: clang/test/CodeGenCXX/macro-prefix-map.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/macro-prefix-map.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fmacro-prefix-map=%p=/UNLIKELY/PATH -emit-llvm -o - %s | FileCheck %s
+//
+// CHECK: /UNLIKELY/PATH{{/|\\\\}}macro-prefix-map.cpp
+template <class T>
+const char *pretty(T) {
+  return __PRETTY_FUNCTION__;
+}
+
+const char *testEnum() {
+  enum {
+    A
+  };
+  return pretty(A);
+}
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -635,6 +635,21 @@
   llvm_unreachable("Unknown ident kind for PredefinedExpr");
 }
 
+namespace {
+class PPCallbacks final : public PrintingCallbacks {
+  const LangOptions &LO;
+
+public:
+  PPCallbacks(const LangOptions &LO) : LO(LO) {}
+
+  std::string remapPath(StringRef Path) const override {
+    SmallString<256> Buffer(Path);
+    LO.remapPathPrefix(Buffer);
+    return static_cast<std::string>(Buffer);
+  }
+};
+} // namespace
+
 // FIXME: Maybe this should use DeclPrinter with a special "print predefined
 // expr" policy instead.
 std::string PredefinedExpr::ComputeName(IdentKind IK, const Decl *CurrentDecl) {
@@ -700,6 +715,9 @@
     }
 
     PrintingPolicy Policy(Context.getLangOpts());
+    PPCallbacks Callbacks(Context.getLangOpts());
+    Policy.Callbacks = &Callbacks;
+
     std::string Proto;
     llvm::raw_string_ostream POut(Proto);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117321.400006.patch
Type: text/x-patch
Size: 1521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220114/ce93f8f0/attachment-0001.bin>


More information about the cfe-commits mailing list