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 #ifdef LIBC_COPT_USE_C_ASSERT
14 // The build is configured to just use the public <assert.h> API
15 // for libc's internal assertions.
19 #define LIBC_ASSERT(COND) assert(COND)
21 #else // Not LIBC_COPT_USE_C_ASSERT
23 #include "src/__support/OSUtil/io.h"
24 #include "src/__support/OSUtil/quick_exit.h"
25 #include "src/__support/integer_to_string.h"
26 #include "src/__support/macros/attributes.h" // For LIBC_INLINE
28 namespace __llvm_libc
{
30 LIBC_INLINE
void report_assertion_failure(const char *assertion
,
31 const char *filename
, unsigned line
,
32 const char *funcname
) {
33 char line_str
[IntegerToString::dec_bufsize
<unsigned>()];
34 IntegerToString::dec(line
, line_str
);
35 __llvm_libc::write_to_stderr(filename
);
36 __llvm_libc::write_to_stderr(":");
37 __llvm_libc::write_to_stderr(line_str
);
38 __llvm_libc::write_to_stderr(": Assertion failed: '");
39 __llvm_libc::write_to_stderr(assertion
);
40 __llvm_libc::write_to_stderr("' in function: '");
41 __llvm_libc::write_to_stderr(funcname
);
42 __llvm_libc::write_to_stderr("'\n");
45 } // namespace __llvm_libc
48 #error "Unexpected: LIBC_ASSERT macro already defined"
51 // The public "assert" macro calls abort on failure. Should it be same here?
52 // The libc internal assert can fire from anywhere inside the libc. So, to
53 // avoid potential chicken-and-egg problems, it is simple to do a quick_exit
54 // on assertion failure instead of calling abort. We also don't want to use
55 // __builtin_trap as it could potentially be implemented using illegal
56 // instructions which can be very misleading when debugging.
58 #define LIBC_ASSERT(COND) \
62 #define LIBC_ASSERT(COND) \
65 __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__, \
66 __PRETTY_FUNCTION__); \
67 __llvm_libc::quick_exit(0xFF); \
72 #endif // LIBC_COPT_USE_C_ASSERT
74 #endif // LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H