1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_ASSERT_H
8 #define _PICO_ASSERT_H
9 
10 #include <stdbool.h>
11 
12 #ifdef __cplusplus
13 
14 #include <cassert>
15 
16 extern "C" {
17 #else
18 #include <assert.h>
19 #endif
20 
21 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLE_ALL, Global assert enable, type=bool, default=0, group=pico_base
22 // PICO_CONFIG: PARAM_ASSERTIONS_DISABLE_ALL, Global assert disable, type=bool, default=0, group=pico_base
23 
24 #ifndef PARAM_ASSERTIONS_ENABLE_ALL
25 #define PARAM_ASSERTIONS_ENABLE_ALL 0
26 #endif
27 
28 #ifndef PARAM_ASSERTIONS_DISABLE_ALL
29 #define PARAM_ASSERTIONS_DISABLE_ALL 0
30 #endif
31 
32 #define PARAM_ASSERTIONS_ENABLED(x) ((PARAM_ASSERTIONS_ENABLED_ ## x || PARAM_ASSERTIONS_ENABLE_ALL) && !PARAM_ASSERTIONS_DISABLE_ALL)
33 
34 #define invalid_params_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) assert(!(test));})
35 #define valid_params_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) assert(test);})
36 #define hard_assert_if(x, test) ({if (PARAM_ASSERTIONS_ENABLED(x)) hard_assert(!(test));})
37 #define invalid_params_if_and_return(x, test, rc) ({/*if (PARAM_ASSERTIONS_ENABLED(x)) assert(!(test)); */ if (test) return rc; })
38 
39 #ifdef NDEBUG
40 extern void hard_assertion_failure(void);
hard_assert(bool condition,...)41 static inline void hard_assert(bool condition, ...) {
42     if (!condition)
43         hard_assertion_failure();
44 }
45 #else
46 #define hard_assert assert
47 #endif
48 
49 #ifdef __cplusplus
50 }
51 #endif
52 #endif
53