/ components / esp_common / include / esp_assert.h
esp_assert.h
 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  /* Assert at compile time if possible, runtime otherwise */
20  #ifndef __cplusplus
21  /* __builtin_choose_expr() is only in C, makes this a lot cleaner */
22  #define TRY_STATIC_ASSERT(CONDITION, MSG) do {                                                              \
23              _Static_assert(__builtin_choose_expr(__builtin_constant_p(CONDITION), (CONDITION), 1), #MSG);   \
24              assert(#MSG && (CONDITION));                                                                    \
25          } while(0)
26  #else
27  /* for C++, use __attribute__((error)) - works almost as well as _Static_assert */
28  #define TRY_STATIC_ASSERT(CONDITION, MSG) do {                                                              \
29              if (__builtin_constant_p(CONDITION) && !(CONDITION)) {          \
30                  extern __attribute__((error(#MSG))) void failed_compile_time_assert(void);      \
31                  failed_compile_time_assert();                                                   \
32              }                                                                                   \
33              assert(#MSG && (CONDITION));                                                                    \
34          } while(0)
35  #endif /* __cplusplus */
36  
37  #endif /* __ESP_ASSERT_H__ */