From 50b2b1bcdc910f1fc6a61067f18ff4e7c5322853 Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Tue, 25 Jun 2024 15:18:39 -0700 Subject: [PATCH] Only print the last component of the filename In particular, on Windows MSVC, the ASSERT macros use `__FILE__` (with the full pathname) instead of `__FILE_NAME__` (with only the last component). --- lib/Basic/Assertions.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Basic/Assertions.cpp b/lib/Basic/Assertions.cpp index 54bce29057f74..07ab7ff6de2b0 100644 --- a/lib/Basic/Assertions.cpp +++ b/lib/Basic/Assertions.cpp @@ -33,7 +33,7 @@ int CONDITIONAL_ASSERT_Global_enable_flag = #ifdef NDEBUG 0; // Default to `off` in release builds #else - 0; // TODO: Default to `on` in debug builds + 1; // Default to `on` in debug builds #endif void ASSERT_failure(const char *expr, const char *filename, int line, const char *func) { @@ -43,8 +43,16 @@ void ASSERT_failure(const char *expr, const char *filename, int line, const char llvm::errs() << "Assertion help: -Xllvm -assert-help\n"; } + // Find the last component of `filename` + // Needed on Windows MSVC, which lacks __FILE_NAME__ + // so we have to use __FILE__ instead: + for (const char *p = filename; *p != '\0'; p++) { + if ((p[0] == '/' || p[0] == '\\') + && p[1] != '/' && p[1] != '\\' && p[1] != '\0') { + filename = p + 1; + } + } - // Format here matches that used by `assert` on macOS: llvm::errs() << "Assertion failed: " << "(" << expr << "), "