[llvm] Strip the full path from __FILE__ in the LDBG macro and keep only the filename (PR #150677)
Mehdi Amini via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 25 11:27:36 PDT 2025
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/150677
None
>From 90360ed7d0d3101c6944ca94c1cabf5c8500e7c9 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Fri, 25 Jul 2025 11:25:55 -0700
Subject: [PATCH] Strip the full path from __FILE__ in the LDBG macro and keep
only the filename
---
llvm/include/llvm/Support/DebugLog.h | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/DebugLog.h b/llvm/include/llvm/Support/DebugLog.h
index 9556bf2d6242d..a93aa54a54bde 100644
--- a/llvm/include/llvm/Support/DebugLog.h
+++ b/llvm/include/llvm/Support/DebugLog.h
@@ -29,7 +29,15 @@ namespace llvm {
#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE) \
for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \
_c = false) \
- ::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM))
+ ::llvm::impl::LogWithNewline( \
+ TYPE, \
+ [] { \
+ /* Force constexpr eval */ \
+ constexpr const char *filename = \
+ ::llvm::impl::LogWithNewline::getFileName(__FILE__); \
+ return filename; \
+ }(), \
+ __LINE__, (STREAM))
namespace impl {
class LogWithNewline {
@@ -51,6 +59,16 @@ class LogWithNewline {
LogWithNewline(const LogWithNewline &) = delete;
LogWithNewline &operator=(const LogWithNewline &) = delete;
LogWithNewline &operator=(LogWithNewline &&) = delete;
+ static constexpr const char *getFileName(const char *path) {
+ // Remove the path prefix from the file name.
+ const char *filename = path;
+ for (const char *p = path; *p != '\0'; ++p) {
+ if (*p == '/' || *p == '\\') {
+ filename = p + 1;
+ }
+ }
+ return filename;
+ }
private:
raw_ostream &os;
More information about the llvm-commits
mailing list