diff --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h --- a/libc/src/__support/libc_assert.h +++ b/libc/src/__support/libc_assert.h @@ -27,18 +27,9 @@ namespace __llvm_libc { -LIBC_INLINE void report_assertion_failure(const char *assertion, - const char *filename, unsigned line, +LIBC_INLINE void report_assertion_failure(const char *error_str, const char *funcname) { - char line_str[IntegerToString::dec_bufsize()]; - // dec returns an optional, will always be valid for this size buffer - auto line_number = IntegerToString::dec(line, line_str); - __llvm_libc::write_to_stderr(filename); - __llvm_libc::write_to_stderr(":"); - __llvm_libc::write_to_stderr(*line_number); - __llvm_libc::write_to_stderr(": Assertion failed: '"); - __llvm_libc::write_to_stderr(assertion); - __llvm_libc::write_to_stderr("' in function: '"); + __llvm_libc::write_to_stderr(error_str); __llvm_libc::write_to_stderr(funcname); __llvm_libc::write_to_stderr("'\n"); } @@ -60,11 +51,21 @@ do { \ } while (false) #else + +// Convert __LINE__ to a string using macros. The indirection is necessary +// because otherwise it will turn "__LINE__" into a string, not its value. The +// value is evaluated in the indirection step. +#define __MACRO_TO_STR(x) #x +#define __MACRO_TO_STR_INDIR(y) __MACRO_TO_STR(y) +#define __LINE_STR__ __MACRO_TO_STR_INDIR(__LINE__) + #define LIBC_ASSERT(COND) \ do { \ if (!(COND)) { \ - __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__, \ - __PRETTY_FUNCTION__); \ + __llvm_libc::report_assertion_failure( \ + __FILE__ ":" __LINE_STR__ ": Assertion failed: '" #COND \ + "' in function: '", \ + __PRETTY_FUNCTION__); \ __llvm_libc::quick_exit(0xFF); \ } \ } while (false)