]> git.ozlabs.org Git - ccan/blob - ccan/build_assert/_info
cf939d794ce95ddd7829fd00b0ba9db48d2653c8
[ccan] / ccan / build_assert / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * build_assert - routines for build-time assertions
7  *
8  * This code provides routines which will cause compilation to fail should some
9  * assertion be untrue: such failures are preferable to run-time assertions,
10  * but much more limited since they can only depends on compile-time constants.
11  *
12  * These assertions are most useful when two parts of the code must be kept in
13  * sync: it is better to avoid such cases if possible, but seconds best is to
14  * detect invalid changes at build time.
15  *
16  * For example, a tricky piece of code might rely on a certain element being at
17  * the start of the structure.  To ensure that future changes don't break it,
18  * you would catch such changes in your code like so:
19  *
20  * Example:
21  *      #include <stddef.h>
22  *      #include <ccan/build_assert/build_assert.h>
23  *
24  *      struct foo {
25  *              char string[5];
26  *              int x;
27  *      };
28  *
29  *      char *foo_string(struct foo *foo)
30  *      {
31  *              // This trick requires that the string be first in the structure
32  *              BUILD_ASSERT(offsetof(struct foo, string) == 0);
33  *              return (char *)foo;
34  *      }
35  *
36  * Licence: LGPL (2 or any later version)
37  * Author: Rusty Russell <rusty@rustcorp.com.au>
38  */
39 int main(int argc, char *argv[])
40 {
41         if (argc != 2)
42                 return 1;
43
44         if (strcmp(argv[1], "depends") == 0)
45                 /* Nothing. */
46                 return 0;
47
48         return 1;
49 }