Skip to content

Commit 9a8ab1c

Browse files
daniel-santostorvalds
authored andcommitted
bug.h, compiler.h: introduce compiletime_assert & BUILD_BUG_ON_MSG
Introduce compiletime_assert to compiler.h, which moves the details of how to break a build and emit an error message for a specific compiler to the headers where these details should be. Following in the tradition of the POSIX assert macro, compiletime_assert creates a build-time error when the supplied condition is *false*. Next, we add BUILD_BUG_ON_MSG to bug.h which simply wraps compiletime_assert, inverting the logic, so that it fails when the condition is *true*, consistent with the language "build bug on." This macro allows you to specify the error message you want emitted when the supplied condition is true. Finally, we remove all other code from bug.h that mucks with these details (BUILD_BUG & BUILD_BUG_ON), and have them all call BUILD_BUG_ON_MSG. This not only reduces source code bloat, but also prevents the possibility of code being changed for one macro and not for the other (which was previously the case for BUILD_BUG and BUILD_BUG_ON). Since __compiletime_error_fallback is now only used in compiler.h, I'm considering it a private macro and removing the double negation that's now extraneous. [[email protected]: checkpatch fixes] Signed-off-by: Daniel Santos <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: David Rientjes <[email protected]> Cc: Joe Perches <[email protected]> Cc: Josh Triplett <[email protected]> Cc: Paul Gortmaker <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c361d3e commit 9a8ab1c

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

include/linux/bug.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct pt_regs;
1717
#define BUILD_BUG_ON_ZERO(e) (0)
1818
#define BUILD_BUG_ON_NULL(e) ((void*)0)
1919
#define BUILD_BUG_ON_INVALID(e) (0)
20+
#define BUILD_BUG_ON_MSG(cond, msg) (0)
2021
#define BUILD_BUG_ON(condition) (0)
2122
#define BUILD_BUG() (0)
2223
#else /* __CHECKER__ */
@@ -39,6 +40,15 @@ struct pt_regs;
3940
*/
4041
#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
4142

43+
/**
44+
* BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
45+
* error message.
46+
* @condition: the condition which the compiler should know is false.
47+
*
48+
* See BUILD_BUG_ON for description.
49+
*/
50+
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
51+
4252
/**
4353
* BUILD_BUG_ON - break compile if a condition is true.
4454
* @condition: the condition which the compiler should know is false.
@@ -60,15 +70,8 @@ struct pt_regs;
6070
#ifndef __OPTIMIZE__
6171
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
6272
#else
63-
#define BUILD_BUG_ON(condition) \
64-
do { \
65-
bool __cond = !!(condition); \
66-
extern void __build_bug_on_failed(void) \
67-
__compiletime_error("BUILD_BUG_ON failed"); \
68-
if (__cond) \
69-
__build_bug_on_failed(); \
70-
__compiletime_error_fallback(__cond); \
71-
} while (0)
73+
#define BUILD_BUG_ON(condition) \
74+
BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
7275
#endif
7376

7477
/**
@@ -78,12 +81,7 @@ struct pt_regs;
7881
* build time, you should use BUILD_BUG to detect if it is
7982
* unexpectedly used.
8083
*/
81-
#define BUILD_BUG() \
82-
do { \
83-
extern void __build_bug_failed(void) \
84-
__compiletime_error("BUILD_BUG failed");\
85-
__build_bug_failed(); \
86-
} while (0)
84+
#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
8785

8886
#endif /* __CHECKER__ */
8987

include/linux/compiler.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,35 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
308308
#ifndef __compiletime_error
309309
# define __compiletime_error(message)
310310
# define __compiletime_error_fallback(condition) \
311-
do { ((void)sizeof(char[1 - 2*!!(condition)])); } while (0)
311+
do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
312312
#else
313313
# define __compiletime_error_fallback(condition) do { } while (0)
314314
#endif
315315

316+
#define __compiletime_assert(condition, msg, prefix, suffix) \
317+
do { \
318+
bool __cond = !(condition); \
319+
extern void prefix ## suffix(void) __compiletime_error(msg); \
320+
if (__cond) \
321+
prefix ## suffix(); \
322+
__compiletime_error_fallback(__cond); \
323+
} while (0)
324+
325+
#define _compiletime_assert(condition, msg, prefix, suffix) \
326+
__compiletime_assert(condition, msg, prefix, suffix)
327+
328+
/**
329+
* compiletime_assert - break build and emit msg if condition is false
330+
* @condition: a compile-time constant condition to check
331+
* @msg: a message to emit if condition is false
332+
*
333+
* In tradition of POSIX assert, this macro will break the build if the
334+
* supplied condition is *false*, emitting the supplied error message if the
335+
* compiler has support to do so.
336+
*/
337+
#define compiletime_assert(condition, msg) \
338+
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
339+
316340
/*
317341
* Prevent the compiler from merging or refetching accesses. The compiler
318342
* is also forbidden from reordering successive instances of ACCESS_ONCE(),

0 commit comments

Comments
 (0)