Macros
assert.h File Reference
#include "auto/config.h"

Go to the source code of this file.

Macros

#define __has_feature(x)   0
 
#define __has_extension   __has_feature
 
#define STATIC_ASSERT_PRAGMA_START
 
#define STATIC_ASSERT_PRAGMA_END
 
#define STATIC_ASSERT(cond, msg)
 Assert at compile time if condition is not satisfied. More...
 
#define STATIC_ASSERT_STATEMENT   STATIC_ASSERT_EXPR
 
#define ASSERT_CONCAT_(a, b)   a##b
 
#define ASSERT_CONCAT(a, b)   ASSERT_CONCAT_(a, b)
 
#define STATIC_ASSERT_EXPR(e, m)   ((enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }) 0)
 Like STATIC_ASSERT, but can be used where expressions are used. More...
 
#define STRICT_ADD(a, b, c, t)   do { *(c) = (t)((a) + (b)); } while (0)
 Adds (a + b) and stores result in c. Aborts on overflow. More...
 
#define STRICT_SUB(a, b, c, t)   do { *(c) = (t)((a) - (b)); } while (0)
 Subtracts (a - b) and stores result in c. Aborts on overflow. More...
 

Macro Definition Documentation

#define __has_extension   __has_feature
#define __has_feature (   x)    0
#define ASSERT_CONCAT (   a,
 
)    ASSERT_CONCAT_(a, b)
#define ASSERT_CONCAT_ (   a,
 
)    a##b
#define STATIC_ASSERT (   cond,
  msg 
)
Value:
do { \
} while (0)
#define STATIC_ASSERT_PRAGMA_END
Definition: assert.h:50
#define STATIC_ASSERT_STATEMENT
Definition: assert.h:108
err msg
Definition: helpers.c:1464

Assert at compile time if condition is not satisfied.

Should be put on its own line, followed by a semicolon.

Example:

STATIC_ASSERT(sizeof(void *) == 8, "Expected 64-bit mode");
Parameters
[in]conditionCondition to check, should be an integer constant expression.
[in]messageMessage which will be given if check fails.
#define STATIC_ASSERT_EXPR (   e,
 
)    ((enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }) 0)

Like STATIC_ASSERT, but can be used where expressions are used.

STATIC_ASSERT_EXPR may be put in brace initializer lists. Error message given in this case is not very nice with the current implementation though and message argument is ignored.

#define STATIC_ASSERT_PRAGMA_END
#define STATIC_ASSERT_PRAGMA_START
#define STATIC_ASSERT_STATEMENT   STATIC_ASSERT_EXPR
#define STRICT_ADD (   a,
  b,
  c,
 
)    do { *(c) = (t)((a) + (b)); } while (0)

Adds (a + b) and stores result in c. Aborts on overflow.

Requires GCC 5+ and Clang 3.8+ https://clang.llvm.org/docs/LanguageExtensions.html https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html

Alternative for compilers without __builtin_xx_overflow ? https://stackoverflow.com/a/44830670/152142

Parameters
aOperand 1.
bOperand 2.
cWhere to store the result.
tResult type. Not used if compiler supports __builtin_add_overflow.
#define STRICT_SUB (   a,
  b,
  c,
 
)    do { *(c) = (t)((a) - (b)); } while (0)

Subtracts (a - b) and stores result in c. Aborts on overflow.

See also
STRICT_ADD