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 "pico/types.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 
38 #ifdef NDEBUG
39 extern void hard_assertion_failure(void);
hard_assert(bool condition,...)40 static inline void hard_assert(bool condition, ...) {
41     if (!condition)
42         hard_assertion_failure();
43 }
44 #else
45 #define hard_assert assert
46 #endif
47 
48 #ifdef __cplusplus
49 }
50 #endif
51 #endif
52