1 // © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 // Local version of the standard-defined assert.h
6 
7 #if !defined(HYP_STANDALONE_TEST)
8 _Static_assert(__STDC_HOSTED__ == 0,
9 	       "This file deviates from MISRA rule 21.2 in hosted mode");
10 #endif
11 
12 #define static_assert _Static_assert
13 
14 // Use a Clang extension to assert that an expression is true if its value
15 // can be statically determined.
16 static inline _Bool
assert_if_const(_Bool x)17 assert_if_const(_Bool x)
18 	__attribute__((diagnose_if(!(x), "Static assert failure", "error"),
19 		       always_inline))
20 {
21 	return x;
22 }
23 
24 #if defined(__KLOCWORK__)
25 _Noreturn void
26 panic(const char *str);
27 #define assert(x) ((x) ? (void)0 : panic("assertion"))
28 #elif defined(NDEBUG)
29 // Strictly this should be defined to ((void)0), but since assert_if_const()
30 // has no runtime overhead we may as well use it. Also the fact that the
31 // expression is evaluated means we don't need to put maybe-unused annotations
32 // on variables that are only used in assert expressions.
33 #define assert(x) (void)assert_if_const(x)
34 #else
35 _Noreturn void
36 assert_failed(const char *file, int line, const char *func, const char *err);
37 #define assert(x)                                                              \
38 	(assert_if_const(x) ? (void)0                                          \
39 			    : assert_failed(__FILE__, __LINE__, __func__, #x))
40 #endif
41 
42 #if defined(VERBOSE) && VERBOSE
43 #define assert_debug assert
44 #else
45 #define assert_debug(x) (void)assert_if_const(x)
46 #endif
47