1 //===-- Definition of a libc internal assert macro --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H
10 #define LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H
12 #include "src/__support/OSUtil/io.h"
13 #include "src/__support/OSUtil/quick_exit.h"
14 #include "src/__support/integer_to_string.h"
15 #include "src/__support/macros/attributes.h" // For LIBC_INLINE
17 namespace __llvm_libc
{
19 LIBC_INLINE
void report_assertion_failure(const char *assertion
,
20 const char *filename
, unsigned line
,
21 const char *funcname
) {
22 char line_str
[IntegerToString::dec_bufsize
<unsigned>()];
23 IntegerToString::dec(line
, line_str
);
24 __llvm_libc::write_to_stderr(filename
);
25 __llvm_libc::write_to_stderr(":");
26 __llvm_libc::write_to_stderr(line_str
);
27 __llvm_libc::write_to_stderr(": Assertion failed: '");
28 __llvm_libc::write_to_stderr(assertion
);
29 __llvm_libc::write_to_stderr("' in function: '");
30 __llvm_libc::write_to_stderr(funcname
);
31 __llvm_libc::write_to_stderr("'\n");
34 } // namespace __llvm_libc
37 #error "Unexpected: LIBC_ASSERT macro already defined"
40 // The public "assert" macro calls abort on failure. Should it be same here?
41 // The libc intenral assert can fire from anywhere inside the libc. So, to
42 // avoid potential chicken-and-egg problems, it is simple to do a quick_exit
43 // on assertion failure instead of calling abort. We also don't want to use
44 // __builtin_trap as it could potentially be implemented using illegal
45 // instructions which can be very misleading when debugging.
47 #define LIBC_ASSERT(COND) \
51 #define LIBC_ASSERT(COND) \
54 __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__, \
55 __PRETTY_FUNCTION__); \
56 __llvm_libc::quick_exit(0xFF); \
61 #endif // LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H