[llvm] Strip the full path from __FILE__ in the LDBG macro and keep only the filename (PR #150677)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 25 11:28:14 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Mehdi Amini (joker-eph)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/150677.diff


1 Files Affected:

- (modified) llvm/include/llvm/Support/DebugLog.h (+19-1) 


``````````diff
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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/150677


More information about the llvm-commits mailing list