blob: bd0725401a79e914a3a7963f1d173b2c462c71f5 [file] [log] [blame]
Norman James6a58a272015-10-07 14:34:16 -05001/* CC0 (Public domain) - see LICENSE file for details */
2#ifndef CCAN_BUILD_ASSERT_H
3#define CCAN_BUILD_ASSERT_H
4
5/**
6 * BUILD_ASSERT - assert a build-time dependency.
7 * @cond: the compile-time condition which must be true.
8 *
9 * Your compile will fail if the condition isn't true, or can't be evaluated
10 * by the compiler. This can only be used within a function.
11 *
12 * Example:
13 * #include <stddef.h>
14 * ...
15 * static char *foo_to_char(struct foo *foo)
16 * {
17 * // This code needs string to be at start of foo.
18 * BUILD_ASSERT(offsetof(struct foo, string) == 0);
19 * return (char *)foo;
20 * }
21 */
22#define BUILD_ASSERT(cond) \
23 do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
24
25/**
26 * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
27 * @cond: the compile-time condition which must be true.
28 *
29 * Your compile will fail if the condition isn't true, or can't be evaluated
30 * by the compiler. This can be used in an expression: its value is "0".
31 *
32 * Example:
33 * #define foo_to_char(foo) \
34 * ((char *)(foo) \
35 * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
36 */
37#define BUILD_ASSERT_OR_ZERO(cond) \
38 (sizeof(char [1 - 2*!(cond)]) - 1)
39
40#endif /* CCAN_BUILD_ASSERT_H */