[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 12:24:25 PDT 2025
https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/150677
>From 0bb522f35aa9ce1881fba9d57435bd612f336813 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 1/2] 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 3e53944edc905..da915a31367de 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;
>From e81bdd6f4a9f2267c36481759748b5cbae6e5ee8 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Fri, 25 Jul 2025 12:22:39 -0700
Subject: [PATCH 2/2] Use a CMake macro for speedup
---
llvm/cmake/modules/LLVMProcessSources.cmake | 9 +++++++++
llvm/include/llvm/Support/DebugLog.h | 22 +++++++++++----------
2 files changed, 21 insertions(+), 10 deletions(-)
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 da915a31367de..b1b17e3ede950 100644
--- a/llvm/include/llvm/Support/DebugLog.h
+++ b/llvm/include/llvm/Support/DebugLog.h
@@ -26,18 +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, \
- [] { \
- /* Force constexpr eval */ \
- constexpr const char *filename = \
- ::llvm::impl::LogWithNewline::getFileName(__FILE__); \
- return filename; \
- }(), \
- __LINE__, (STREAM))
+ ::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 {
@@ -45,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 << " ";
@@ -59,7 +61,7 @@ class LogWithNewline {
LogWithNewline(const LogWithNewline &) = delete;
LogWithNewline &operator=(const LogWithNewline &) = delete;
LogWithNewline &operator=(LogWithNewline &&) = delete;
- static constexpr const char *getFileName(const char *path) {
+ 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) {
More information about the llvm-commits
mailing list