]> git.ozlabs.org Git - ccan/blob - ccan/build_assert/build_assert.h
Rename _info.c to _info: this means we can simple compile *.c.
[ccan] / ccan / build_assert / build_assert.h
1 #ifndef CCAN_BUILD_ASSERT_H
2 #define CCAN_BUILD_ASSERT_H
3
4 /**
5  * BUILD_ASSERT - assert a build-time dependency.
6  * @cond: the compile-time condition which must be true.
7  *
8  * Your compile will fail if the condition isn't true, or can't be evaluated
9  * by the compiler.  This can only be used within a function.
10  *
11  * Example:
12  *      char *foo_to_char(struct foo *foo)
13  *      {
14  *              // This code needs string to be at start of foo.
15  *              BUILD_ASSERT(offsetof(struct foo, string) == 0);
16  *              return (char *)foo;
17  *      }
18  */
19 #define BUILD_ASSERT(cond) \
20         do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
21
22 /**
23  * EXPR_BUILD_ASSERT - assert a build-time dependency, as an expression.
24  * @cond: the compile-time condition which must be true.
25  *
26  * Your compile will fail if the condition isn't true, or can't be evaluated
27  * by the compiler.  This can be used in an expression: its value is "0".
28  *
29  * Example:
30  *      #define foo_to_char(foo)                                        \
31  *               ((char *)(foo)                                         \
32  *                + EXPR_BUILD_ASSERT(offsetof(struct foo, string) == 0))
33  */
34 #define EXPR_BUILD_ASSERT(cond) \
35         (sizeof(char [1 - 2*!(cond)]) - 1)
36
37 #endif /* CCAN_BUILD_ASSERT_H */