diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt --- a/libc/src/__support/CMakeLists.txt +++ b/libc/src/__support/CMakeLists.txt @@ -212,12 +212,20 @@ .uint ) +add_header_library( + display_assert + HDRS + display_assert.h + DEPENDS + libc.src.__support.OSUtil.osutil +) + add_header_library( libc_assert HDRS libc_assert.h DEPENDS - .integer_to_string + .display_assert libc.src.__support.OSUtil.osutil ) diff --git a/libc/src/__support/display_assert.h b/libc/src/__support/display_assert.h new file mode 100644 --- /dev/null +++ b/libc/src/__support/display_assert.h @@ -0,0 +1,30 @@ +//===-- Display macro for assertion fail ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_SUPPORT_DISPLAY_ASSERT_H +#define LLVM_LIBC_SRC_SUPPORT_DISPLAY_ASSERT_H + +#include "src/__support/OSUtil/io.h" + +// 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 __LIBC_MACRO_TO_STR(x) #x +#define __LIBC_MACRO_TO_STR_INDIR(y) __LIBC_MACRO_TO_STR(y) +#define __LIBC_LINE_STR__ __LIBC_MACRO_TO_STR_INDIR(__LINE__) + +// This is in parentheses and commas so that the ternary operator in the assert +// macro works. +#define LIBC_DISPLAY_ASSERT(COND) \ + (__llvm_libc::write_to_stderr(__FILE__ ":" __LIBC_LINE_STR__ \ + ": Assertion failed: '" #COND \ + "' in function: '"), \ + __llvm_libc::write_to_stderr(__PRETTY_FUNCTION__), \ + __llvm_libc::write_to_stderr("'\n")) + +#endif // LLVM_LIBC_SRC_SUPPORT_DISPLAY_ASSERT_H 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 @@ -20,9 +20,8 @@ #else // Not LIBC_COPT_USE_C_ASSERT -#include "src/__support/OSUtil/io.h" #include "src/__support/OSUtil/quick_exit.h" -#include "src/__support/integer_to_string.h" +#include "src/__support/display_assert.h" #include "src/__support/macros/attributes.h" // For LIBC_INLINE #ifdef LIBC_ASSERT @@ -41,21 +40,10 @@ } 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 __LIBC_MACRO_TO_STR(x) #x -#define __LIBC_MACRO_TO_STR_INDIR(y) __LIBC_MACRO_TO_STR(y) -#define __LIBC_LINE_STR__ __LIBC_MACRO_TO_STR_INDIR(__LINE__) - #define LIBC_ASSERT(COND) \ do { \ if (!(COND)) { \ - __llvm_libc::write_to_stderr(__FILE__ ":" __LIBC_LINE_STR__ \ - ": Assertion failed: '" #COND \ - "' in function: '"); \ - __llvm_libc::write_to_stderr(__PRETTY_FUNCTION__); \ - __llvm_libc::write_to_stderr("'\n"); \ + LIBC_DISPLAY_ASSERT(COND); \ __llvm_libc::quick_exit(0xFF); \ } \ } while (false) diff --git a/libc/src/assert/CMakeLists.txt b/libc/src/assert/CMakeLists.txt --- a/libc/src/assert/CMakeLists.txt +++ b/libc/src/assert/CMakeLists.txt @@ -8,5 +8,6 @@ DEPENDS libc.include.assert libc.src.__support.OSUtil.osutil + libc.src.__support.display_assert libc.src.stdlib.abort ) diff --git a/libc/src/assert/__assert_fail.h b/libc/src/assert/__assert_fail.h --- a/libc/src/assert/__assert_fail.h +++ b/libc/src/assert/__assert_fail.h @@ -13,8 +13,7 @@ namespace __llvm_libc { -[[noreturn]] void __assert_fail(const char *assertion, const char *file, - unsigned line, const char *function); +[[noreturn]] void __assert_fail(); } // namespace __llvm_libc diff --git a/libc/src/assert/__assert_fail.cpp b/libc/src/assert/__assert_fail.cpp --- a/libc/src/assert/__assert_fail.cpp +++ b/libc/src/assert/__assert_fail.cpp @@ -8,16 +8,10 @@ #include "src/assert/__assert_fail.h" #include "src/__support/OSUtil/io.h" -#include "src/__support/libc_assert.h" #include "src/stdlib/abort.h" namespace __llvm_libc { -LLVM_LIBC_FUNCTION(void, __assert_fail, - (const char *assertion, const char *file, unsigned line, - const char *function)) { - __llvm_libc::report_assertion_failure(assertion, file, line, function); - __llvm_libc::abort(); -} +LLVM_LIBC_FUNCTION(void, __assert_fail, ()) { __llvm_libc::abort(); } } // namespace __llvm_libc diff --git a/libc/src/assert/assert.h b/libc/src/assert/assert.h --- a/libc/src/assert/assert.h +++ b/libc/src/assert/assert.h @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "src/__support/display_assert.h" #include "src/assert/__assert_fail.h" // There is no header guard here since assert is intended to be capable of being @@ -18,7 +19,5 @@ #define assert(e) (void)0 #else #define assert(e) \ - ((e) ? (void)0 \ - : __llvm_libc::__assert_fail(#e, __FILE__, __LINE__, \ - __PRETTY_FUNCTION__)) + ((e) ? (void)0 : LIBC_DISPLAY_ASSERT(e), __llvm_libc::__assert_fail()) #endif diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -413,14 +413,24 @@ ], ) + +libc_support_library( + name = "__support_display_assert", + hdrs = ["src/__support/display_assert.h"], + deps = [ + ":__support_osutil_io", + ":libc_root", + ], +) + libc_support_library( name = "__support_libc_assert", hdrs = ["src/__support/libc_assert.h"], deps = [ - ":__support_integer_to_string", ":__support_macros_attributes", ":__support_osutil_io", ":__support_osutil_quick_exit", + ":__support_display_assert", ":libc_root", ], )