XenevaOS
Loading...
Searching...
No Matches
build_bug.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_BUILD_BUG_H
3#define _LINUX_BUILD_BUG_H
4
5#include <linux/compiler.h>
6
7/*
8 * Force a compilation error if condition is true, but also produce a
9 * result (of value 0 and type int), so the expression can be used
10 * e.g. in a structure initializer (or where-ever else comma expressions
11 * aren't permitted).
12 *
13 * Take an error message as an optional second argument. If omitted,
14 * default to the stringification of the tested expression.
15 */
16#define BUILD_BUG_ON_ZERO(e, ...) \
17 __BUILD_BUG_ON_ZERO_MSG(e, ##__VA_ARGS__, #e " is true")
18
19/* Force a compilation error if a constant expression is not a power of 2 */
20#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
21 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
22#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
23 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
24
25/*
26 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
27 * expression but avoids the generation of any code, even if that expression
28 * has side-effects.
29 */
30#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
31
40#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
41
50#define BUILD_BUG_ON(condition) \
51 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
52
60#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
61
79#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
80#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
81
82
83/*
84 * Compile time check that field has an expected offset
85 */
86#define ASSERT_STRUCT_OFFSET(type, field, expected_offset) \
87 BUILD_BUG_ON_MSG(offsetof(type, field) != (expected_offset), \
88 "Offset of " #field " in " #type " has changed.")
89
90
91#endif /* _LINUX_BUILD_BUG_H */