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

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 26 02:34:14 PDT 2025


Author: Mehdi Amini
Date: 2025-07-26T11:34:10+02:00
New Revision: 5d26e3c227f4b4a1761a8b0001b3165198def479

URL: https://github.com/llvm/llvm-project/commit/5d26e3c227f4b4a1761a8b0001b3165198def479
DIFF: https://github.com/llvm/llvm-project/commit/5d26e3c227f4b4a1761a8b0001b3165198def479.diff

LOG: Strip the full path from __FILE__ in the LDBG macro and keep only the filename (#150677)

Added: 
    

Modified: 
    llvm/cmake/modules/LLVMProcessSources.cmake
    llvm/include/llvm/Support/DebugLog.h

Removed: 
    


################################################################################
diff  --git a/llvm/cmake/modules/LLVMProcessSources.cmake b/llvm/cmake/modules/LLVMProcessSources.cmake
index 0670d60bf2afd..a7f9517ad767c 100644
--- a/llvm/cmake/modules/LLVMProcessSources.cmake
+++ b/llvm/cmake/modules/LLVMProcessSources.cmake
@@ -58,6 +58,15 @@ function(llvm_process_sources OUT_VAR)
   set(sources ${ARG_UNPARSED_ARGUMENTS})
   llvm_check_source_file_list(${sources})
 
+  foreach(fn ${sources})
+    get_filename_component(suf ${fn} EXT)
+    if("${suf}" STREQUAL ".cpp" OR "${suf}" STREQUAL ".c")
+      get_filename_component(short_name ${fn} NAME)
+      set_source_files_properties(${fn} PROPERTIES COMPILE_DEFINITIONS "__SHORT_FILE__=\"${short_name}\"")
+    endif()
+  endforeach()
+
+
   # This adds .td and .h files to the Visual Studio solution:
   add_td_sources(sources)
   find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")

diff  --git a/llvm/include/llvm/Support/DebugLog.h b/llvm/include/llvm/Support/DebugLog.h
index 3e53944edc905..b1b17e3ede950 100644
--- a/llvm/include/llvm/Support/DebugLog.h
+++ b/llvm/include/llvm/Support/DebugLog.h
@@ -26,10 +26,17 @@ namespace llvm {
 //              << "] " << "Bitset contains: " << Bitset << "\n");
 #define LDBG() DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE)
 
+#if defined(__SHORT_FILE__)
+#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE)                            \
+  for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c;  \
+       _c = false)                                                             \
+  ::llvm::impl::LogWithNewline(TYPE, __SHORT_FILE__, __LINE__, (STREAM))
+#else
 #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))
+#endif
 
 namespace impl {
 class LogWithNewline {
@@ -37,6 +44,9 @@ class LogWithNewline {
   LogWithNewline(const char *debug_type, const char *file, int line,
                  raw_ostream &os)
       : os(os) {
+#if !defined(__SHORT_FILE__)
+    file = ::llvm::impl::LogWithNewline::getShortFileName(file);
+#endif
     if (debug_type)
       os << "[" << debug_type << "] ";
     os << file << ":" << line << " ";
@@ -51,6 +61,16 @@ class LogWithNewline {
   LogWithNewline(const LogWithNewline &) = delete;
   LogWithNewline &operator=(const LogWithNewline &) = delete;
   LogWithNewline &operator=(LogWithNewline &&) = delete;
+  static constexpr const char *getShortFileName(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