ic-macros  0.1.5
build_assert.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2014, Wind River Systems, Inc.
3  * Copyright (c) 2023, Intercreate, Inc.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 
15 /* concatenate the values of the arguments into one */
16 #define _IC_Z_DO_CONCAT(x, y) x##y
17 #define _IC_Z_CONCAT(x, y) _IC_Z_DO_CONCAT(x, y)
18 
19 #define _IC_Z_DO_STR(x) #x
20 #define _IC_Z_STR(x) _IC_Z_DO_STR(x)
21 
27 #define IC_BUILD_ASSERT(expr, msg...) \
28  enum _IC_Z_CONCAT(__build_assert_enum, __COUNTER__) { \
29  _IC_Z_CONCAT(__build_assert, __COUNTER__) = 1 / !!(expr) \
30  }
31 /* C++11 has static_assert built in */
32 #if defined(__cplusplus) && (__cplusplus >= 201103L)
33 # undef IC_BUILD_ASSERT
39 # define IC_BUILD_ASSERT(expr, msg...) static_assert(expr, "" msg)
40 /*
41  * GCC 4.6 and higher have the C11 _Static_assert built in and its
42  * output is easier to understand than the common IC_BUILD_ASSERT macros.
43  * Don't use this in C++98 mode though (which we can hit, as
44  * static_assert() is not available)
45  */
46 #elif !defined(__cplusplus) \
47  && ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (__STDC_VERSION__) >= 201100)
48 # undef IC_BUILD_ASSERT
54 # define IC_BUILD_ASSERT(expr, msg...) \
55  _Static_assert( \
56  expr, "" msg " See " __FILE__ ":" _IC_Z_STR(__LINE__) ", expr: " _IC_Z_STR(expr) \
57  )
58 #endif /* defined(__cplusplus) && (__cplusplus >= 201103L) */
59 
60 #ifdef __cplusplus
61 }
62 #endif