1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef __ESP_ASSERT_H__
15 #define __ESP_ASSERT_H__
16 
17 #include "assert.h"
18 
19 #ifndef __cplusplus
20     #define ESP_STATIC_ASSERT _Static_assert
21 #else // __cplusplus
22     #define ESP_STATIC_ASSERT static_assert
23 #endif // __cplusplus
24 
25 /* Assert at compile time if possible, runtime otherwise */
26 #ifndef __cplusplus
27 /* __builtin_choose_expr() is only in C, makes this a lot cleaner */
28 #define TRY_STATIC_ASSERT(CONDITION, MSG) do {                                                              \
29             ESP_STATIC_ASSERT(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG);   \
30             assert(#MSG && (CONDITION));                                                                    \
31         } while(0)
32 #else
33 /* for C++, use __attribute__((error)) - works almost as well as ESP_STATIC_ASSERT */
34 #define TRY_STATIC_ASSERT(CONDITION, MSG) do {                                                              \
35             if (__builtin_constant_p(CONDITION) && !(CONDITION)) {          \
36                 extern __attribute__((error(#MSG))) void failed_compile_time_assert(void);      \
37                 failed_compile_time_assert();                                                   \
38             }                                                                                   \
39             assert(#MSG && (CONDITION));                                                                    \
40         } while(0)
41 #endif /* __cplusplus */
42 
43 #endif /* __ESP_ASSERT_H__ */
44