blob: 20c2dc7f4bd629309e55098910ad112b5064e70c [file] [log] [blame]
Masahiro Yamada059a4802017-09-16 14:10:44 +09001#ifndef _LINUX_BUILD_BUG_H
2#define _LINUX_BUILD_BUG_H
3
4#include <linux/compiler.h>
5
6#ifdef __CHECKER__
Masahiro Yamada059a4802017-09-16 14:10:44 +09007#define BUILD_BUG_ON_ZERO(e) (0)
Masahiro Yamada059a4802017-09-16 14:10:44 +09008#else /* __CHECKER__ */
Christophe Leroy6ab7c3d2023-05-05 10:39:39 +02009/*
10 * Force a compilation error if condition is true, but also produce a
11 * result (of value 0 and type int), so the expression can be used
12 * e.g. in a structure initializer (or where-ever else comma expressions
13 * aren't permitted).
14 */
15#define BUILD_BUG_ON_ZERO(e) ((int)sizeof(struct { int:(-!!(e)); }))
16#endif /* __CHECKER__ */
Masahiro Yamada059a4802017-09-16 14:10:44 +090017
18/* Force a compilation error if a constant expression is not a power of 2 */
19#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
20 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
21#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
22 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
23
24/*
Masahiro Yamada059a4802017-09-16 14:10:44 +090025 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
26 * expression but avoids the generation of any code, even if that expression
27 * has side-effects.
28 */
29#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
30
31/**
32 * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
33 * error message.
34 * @condition: the condition which the compiler should know is false.
35 *
36 * See BUILD_BUG_ON for description.
37 */
38#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
39
40/**
41 * BUILD_BUG_ON - break compile if a condition is true.
42 * @condition: the condition which the compiler should know is false.
43 *
44 * If you have some code which relies on certain constants being equal, or
45 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
46 * detect if someone changes it.
Masahiro Yamada059a4802017-09-16 14:10:44 +090047 */
Masahiro Yamada059a4802017-09-16 14:10:44 +090048#define BUILD_BUG_ON(condition) \
49 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
Masahiro Yamada059a4802017-09-16 14:10:44 +090050
51/**
52 * BUILD_BUG - break compile if used.
53 *
54 * If you have some code that you expect the compiler to eliminate at
55 * build time, you should use BUILD_BUG to detect if it is
56 * unexpectedly used.
57 */
58#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
59
Rasmus Villemoesef0f4e82021-05-18 11:19:46 +020060/**
61 * static_assert - check integer constant expression at build time
62 *
63 * static_assert() is a wrapper for the C11 _Static_assert, with a
64 * little macro magic to make the message optional (defaulting to the
65 * stringification of the tested expression).
66 *
67 * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
68 * scope, but requires the expression to be an integer constant
69 * expression (i.e., it is not enough that __builtin_constant_p() is
70 * true for expr).
71 *
72 * Also note that BUILD_BUG_ON() fails the build if the condition is
73 * true, while static_assert() fails the build if the expression is
74 * false.
75 */
76#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
77#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
78
Masahiro Yamada059a4802017-09-16 14:10:44 +090079#endif /* _LINUX_BUILD_BUG_H */