2020-06-15 14:18:57 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_ASSERT_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_ASSERT_H_
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
2022-06-26 01:17:31 +00:00
|
|
|
extern bool __assert_disable;
|
|
|
|
void __assert_fail(const char *, const char *, int) hidden relegated;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
2022-09-15 04:29:50 +00:00
|
|
|
#define assert(x) ((void)0)
|
2020-06-15 14:18:57 +00:00
|
|
|
#else
|
2022-09-15 04:29:50 +00:00
|
|
|
#define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__), 0)))
|
2020-06-15 14:18:57 +00:00
|
|
|
#endif
|
|
|
|
|
Import C++ Standard Template Library
You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.
- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()
Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
2022-03-22 12:51:41 +00:00
|
|
|
#ifndef __cplusplus
|
2021-03-06 04:32:25 +00:00
|
|
|
#define static_assert _Static_assert
|
Import C++ Standard Template Library
You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.
- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()
Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
2022-03-22 12:51:41 +00:00
|
|
|
#endif
|
2021-03-06 04:32:25 +00:00
|
|
|
|
2022-10-10 05:38:28 +00:00
|
|
|
#ifdef __GNUC__
|
2022-10-16 19:05:08 +00:00
|
|
|
#if !defined(TINY) && !defined(MODE_DBG)
|
2022-10-10 05:38:28 +00:00
|
|
|
/**
|
|
|
|
* Specifies expression can't possibly be false.
|
|
|
|
*
|
|
|
|
* This macro uses the `notpossible` keyword and is intended for things
|
|
|
|
* like systems integration, where we know for a fact that something is
|
|
|
|
* never going to happen, but there's no proof. So we don't want to add
|
|
|
|
* extra bloat for filenames and line numbers, since `ShowCrashReports`
|
|
|
|
* can print a backtrace if we just embed the UD2 instruction to crash.
|
|
|
|
* That's useful for systems code, for the following reason. Invariants
|
|
|
|
* make sense with _unassert() since they usually get placed at the top
|
|
|
|
* of functions. But if you used _unassert() to test a system call does
|
|
|
|
* not fail, then check happens at the end of your function usually and
|
|
|
|
* is therefore likely to result in a failure condition where execution
|
|
|
|
* falls through into a different function, which is shocking to debug.
|
|
|
|
*
|
|
|
|
* In `MODE=tiny` these assertions are redefined as undefined behavior.
|
|
|
|
*/
|
2022-10-09 20:00:46 +00:00
|
|
|
#define _npassert(x) \
|
2022-10-16 19:05:08 +00:00
|
|
|
__extension__({ \
|
2022-10-09 20:00:46 +00:00
|
|
|
if (__builtin_expect(!(x), 0)) { \
|
|
|
|
notpossible; \
|
|
|
|
} \
|
2022-10-10 05:38:28 +00:00
|
|
|
(void)0; \
|
|
|
|
})
|
2022-10-09 20:00:46 +00:00
|
|
|
#else
|
|
|
|
#define _npassert(x) _unassert(x)
|
|
|
|
#endif
|
2022-10-10 05:38:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
/**
|
|
|
|
* Specifies expression being false is undefined behavior.
|
|
|
|
*
|
|
|
|
* This is a good way to tell the compiler about your invariants, which
|
|
|
|
* leads to smaller faster programs. The expression is never removed by
|
|
|
|
* the preprocessor so it's recommended that it be free of side-effects
|
|
|
|
* if you intend for it to be removed. At the same time, this guarantee
|
|
|
|
* makes this assertion useful for things like system calls, since they
|
|
|
|
* won't be removed by `NDEBUG` mode.
|
|
|
|
*
|
|
|
|
* If this assertion fails, the worst possible things can happen unless
|
|
|
|
* you've built your binary in `MODE=dbg` since UBSAN is the only thing
|
|
|
|
* that's capable of debugging this macro.
|
|
|
|
*/
|
|
|
|
#define _unassert(x) \
|
2022-10-16 19:05:08 +00:00
|
|
|
__extension__({ \
|
2022-10-10 05:38:28 +00:00
|
|
|
if (__builtin_expect(!(x), 0)) { \
|
|
|
|
unreachable; \
|
|
|
|
} \
|
|
|
|
(void)0; \
|
|
|
|
})
|
|
|
|
#endif
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_ASSERT_H_ */
|