summaryrefslogtreecommitdiff
path: root/ccan/build_assert/build_assert.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2016-03-11 21:18:42 -0900
committerKent Overstreet <kent.overstreet@gmail.com>2016-03-11 21:18:42 -0900
commit009d6db7b01462a264a8fb15477611bc2d8674eb (patch)
tree48bc88526b34c352797711dd405616bd2ca60051 /ccan/build_assert/build_assert.h
parent074caf716a816d3c4c62bce28503e512d0ef4169 (diff)
Redo lots of stuff
Diffstat (limited to 'ccan/build_assert/build_assert.h')
-rw-r--r--ccan/build_assert/build_assert.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/ccan/build_assert/build_assert.h b/ccan/build_assert/build_assert.h
new file mode 100644
index 00000000..b9ecd840
--- /dev/null
+++ b/ccan/build_assert/build_assert.h
@@ -0,0 +1,40 @@
+/* CC0 (Public domain) - see LICENSE file for details */
+#ifndef CCAN_BUILD_ASSERT_H
+#define CCAN_BUILD_ASSERT_H
+
+/**
+ * BUILD_ASSERT - assert a build-time dependency.
+ * @cond: the compile-time condition which must be true.
+ *
+ * Your compile will fail if the condition isn't true, or can't be evaluated
+ * by the compiler. This can only be used within a function.
+ *
+ * Example:
+ * #include <stddef.h>
+ * ...
+ * static char *foo_to_char(struct foo *foo)
+ * {
+ * // This code needs string to be at start of foo.
+ * BUILD_ASSERT(offsetof(struct foo, string) == 0);
+ * return (char *)foo;
+ * }
+ */
+#define BUILD_ASSERT(cond) \
+ do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
+
+/**
+ * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
+ * @cond: the compile-time condition which must be true.
+ *
+ * Your compile will fail if the condition isn't true, or can't be evaluated
+ * by the compiler. This can be used in an expression: its value is "0".
+ *
+ * Example:
+ * #define foo_to_char(foo) \
+ * ((char *)(foo) \
+ * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
+ */
+#define BUILD_ASSERT_OR_ZERO(cond) \
+ (sizeof(char [1 - 2*!(cond)]) - 1)
+
+#endif /* CCAN_BUILD_ASSERT_H */