Skip to content

Strip the full path from __FILE__ in the LDBG macro and keep only the filename #150677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/cmake/modules/LLVMProcessSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
20 changes: 20 additions & 0 deletions llvm/include/llvm/Support/DebugLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@ 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 {
public:
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 << " ";
Expand All @@ -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;
Expand Down