hc
2024-09-20 a36159eec6ca17402b0e146b86efaf76568dc353
kernel/include/linux/build_bug.h
....@@ -5,28 +5,22 @@
55 #include <linux/compiler.h>
66
77 #ifdef __CHECKER__
8
-#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
9
-#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
108 #define BUILD_BUG_ON_ZERO(e) (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)
159 #else /* __CHECKER__ */
10
+/*
11
+ * Force a compilation error if condition is true, but also produce a
12
+ * result (of value 0 and type int), so the expression can be used
13
+ * e.g. in a structure initializer (or where-ever else comma expressions
14
+ * aren't permitted).
15
+ */
16
+#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
17
+#endif /* __CHECKER__ */
1618
1719 /* Force a compilation error if a constant expression is not a power of 2 */
1820 #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \
1921 BUILD_BUG_ON(((n) & ((n) - 1)) != 0)
2022 #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
2123 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)); }))
3024
3125 /*
3226 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
....@@ -51,23 +45,9 @@
5145 * If you have some code which relies on certain constants being equal, or
5246 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
5347 * detect if someone changes it.
54
- *
55
- * The implementation uses gcc's reluctance to create a negative array, but gcc
56
- * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
57
- * inline functions). Luckily, in 4.3 they added the "error" function
58
- * attribute just for this type of case. Thus, we use a negative sized array
59
- * (should always create an error on gcc versions older than 4.4) and then call
60
- * an undefined function with the error attribute (should always create an
61
- * error on gcc 4.3 and later). If for some reason, neither creates a
62
- * compile-time error, we'll still have a link-time error, which is harder to
63
- * track down.
6448 */
65
-#ifndef __OPTIMIZE__
66
-#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
67
-#else
6849 #define BUILD_BUG_ON(condition) \
6950 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
70
-#endif
7151
7252 /**
7353 * BUILD_BUG - break compile if used.
....@@ -78,7 +58,24 @@
7858 */
7959 #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
8060
81
-#endif /* __CHECKER__ */
61
+/**
62
+ * static_assert - check integer constant expression at build time
63
+ *
64
+ * static_assert() is a wrapper for the C11 _Static_assert, with a
65
+ * little macro magic to make the message optional (defaulting to the
66
+ * stringification of the tested expression).
67
+ *
68
+ * Contrary to BUILD_BUG_ON(), static_assert() can be used at global
69
+ * scope, but requires the expression to be an integer constant
70
+ * expression (i.e., it is not enough that __builtin_constant_p() is
71
+ * true for expr).
72
+ *
73
+ * Also note that BUILD_BUG_ON() fails the build if the condition is
74
+ * true, while static_assert() fails the build if the expression is
75
+ * false.
76
+ */
77
+#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
78
+#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
8279
8380 #ifdef __GENKSYMS__
8481 /* genksyms gets confused by _Static_assert */