Skip to content

Commit 5d26e3c

Browse files
authored
Strip the full path from __FILE__ in the LDBG macro and keep only the filename (#150677)
1 parent 5ec6ac8 commit 5d26e3c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

llvm/cmake/modules/LLVMProcessSources.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ function(llvm_process_sources OUT_VAR)
5858
set(sources ${ARG_UNPARSED_ARGUMENTS})
5959
llvm_check_source_file_list(${sources})
6060

61+
foreach(fn ${sources})
62+
get_filename_component(suf ${fn} EXT)
63+
if("${suf}" STREQUAL ".cpp" OR "${suf}" STREQUAL ".c")
64+
get_filename_component(short_name ${fn} NAME)
65+
set_source_files_properties(${fn} PROPERTIES COMPILE_DEFINITIONS "__SHORT_FILE__=\"${short_name}\"")
66+
endif()
67+
endforeach()
68+
69+
6170
# This adds .td and .h files to the Visual Studio solution:
6271
add_td_sources(sources)
6372
find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")

llvm/include/llvm/Support/DebugLog.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,27 @@ namespace llvm {
2626
// << "] " << "Bitset contains: " << Bitset << "\n");
2727
#define LDBG() DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE)
2828

29+
#if defined(__SHORT_FILE__)
30+
#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE) \
31+
for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \
32+
_c = false) \
33+
::llvm::impl::LogWithNewline(TYPE, __SHORT_FILE__, __LINE__, (STREAM))
34+
#else
2935
#define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE) \
3036
for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \
3137
_c = false) \
3238
::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM))
39+
#endif
3340

3441
namespace impl {
3542
class LogWithNewline {
3643
public:
3744
LogWithNewline(const char *debug_type, const char *file, int line,
3845
raw_ostream &os)
3946
: os(os) {
47+
#if !defined(__SHORT_FILE__)
48+
file = ::llvm::impl::LogWithNewline::getShortFileName(file);
49+
#endif
4050
if (debug_type)
4151
os << "[" << debug_type << "] ";
4252
os << file << ":" << line << " ";
@@ -51,6 +61,16 @@ class LogWithNewline {
5161
LogWithNewline(const LogWithNewline &) = delete;
5262
LogWithNewline &operator=(const LogWithNewline &) = delete;
5363
LogWithNewline &operator=(LogWithNewline &&) = delete;
64+
static constexpr const char *getShortFileName(const char *path) {
65+
// Remove the path prefix from the file name.
66+
const char *filename = path;
67+
for (const char *p = path; *p != '\0'; ++p) {
68+
if (*p == '/' || *p == '\\') {
69+
filename = p + 1;
70+
}
71+
}
72+
return filename;
73+
}
5474

5575
private:
5676
raw_ostream &os;

0 commit comments

Comments
 (0)