blob: 9c7088bafa49ddea0b92f5e592fb2b9d817e933c [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__
7#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
8#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
9#define BUILD_BUG_ON_ZERO(e) (0)
10#define BUILD_BUG_ON_NULL(e) ((void *)0)
11#define BUILD_BUG_ON_INVALID(e) (0)
12#define BUILD_BUG_ON_MSG(cond, msg) (0)
13#define BUILD_BUG_ON(condition) (0)
14#define BUILD_BUG() (0)
15#else /* __CHECKER__ */
16
17/* Force a compilation error if a constant expression is not a power of 2 */
18#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
19 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
20#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
21 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
22
23/*
24 * Force a compilation error if condition is true, but also produce a
25 * result (of value 0 and type size_t), so the expression can be used
26 * e.g. in a structure initializer (or where-ever else comma expressions
27 * aren't permitted).
28 */
29#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); }))
30#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); }))
31
32/*
33 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
34 * expression but avoids the generation of any code, even if that expression
35 * has side-effects.
36 */
37#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
38
39/**
40 * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
41 * error message.
42 * @condition: the condition which the compiler should know is false.
43 *
44 * See BUILD_BUG_ON for description.
45 */
46#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
47
48/**
49 * BUILD_BUG_ON - break compile if a condition is true.
50 * @condition: the condition which the compiler should know is false.
51 *
52 * If you have some code which relies on certain constants being equal, or
53 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
54 * detect if someone changes it.
55 *
56 * The implementation uses gcc's reluctance to create a negative array, but gcc
57 * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
58 * inline functions). Luckily, in 4.3 they added the "error" function
59 * attribute just for this type of case. Thus, we use a negative sized array
60 * (should always create an error on gcc versions older than 4.4) and then call
61 * an undefined function with the error attribute (should always create an
62 * error on gcc 4.3 and later). If for some reason, neither creates a
63 * compile-time error, we'll still have a link-time error, which is harder to
64 * track down.
65 */
66#ifndef __OPTIMIZE__
67#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
68#else
69#define BUILD_BUG_ON(condition) \
70 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
71#endif
72
73/**
74 * BUILD_BUG - break compile if used.
75 *
76 * If you have some code that you expect the compiler to eliminate at
77 * build time, you should use BUILD_BUG to detect if it is
78 * unexpectedly used.
79 */
80#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
81
Rasmus Villemoesef0f4e82021-05-18 11:19:46 +020082/**
83 * static_assert - check integer constant expression at build time
84 *
85 * static_assert() is a wrapper for the C11 _Static_assert, with a
86 * little macro magic to make the message optional (defaulting to the
87 * stringification of the tested expression).
88 *
89 * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
90 * scope, but requires the expression to be an integer constant
91 * expression (i.e., it is not enough that __builtin_constant_p() is
92 * true for expr).
93 *
94 * Also note that BUILD_BUG_ON() fails the build if the condition is
95 * true, while static_assert() fails the build if the expression is
96 * false.
97 */
98#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
99#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
100
Masahiro Yamada059a4802017-09-16 14:10:44 +0900101#endif /* __CHECKER__ */
102
103#endif /* _LINUX_BUILD_BUG_H */