From: Konstantin Khlebnikov Date: Tue, 29 May 2012 22:06:27 +0000 (-0700) Subject: bug: introduce BUILD_BUG_ON_INVALID() macro X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=baf05aa9271b;p=GitHub%2FLineageOS%2Fandroid_kernel_motorola_exynos9610.git bug: introduce BUILD_BUG_ON_INVALID() macro Sometimes we want to check some expressions correctness at compile time. "(void)(e);" or "if (e);" can be dangerous if the expression has side-effects, and gcc sometimes generates a lot of code, even if the expression has no effect. This patch introduces macro BUILD_BUG_ON_INVALID() for such checks, it forces a compilation error if expression is invalid without any extra code. [Cast to "long" required because sizeof does not work for bit-fields.] Signed-off-by: Konstantin Khlebnikov Cc: Linus Torvalds Cc: Geert Uytterhoeven Cc: "H. Peter Anvin" Cc: Cong Wang Cc: Hugh Dickins Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/include/linux/bug.h b/include/linux/bug.h index 72961c39576a..aaac4bba6f5c 100644 --- a/include/linux/bug.h +++ b/include/linux/bug.h @@ -30,6 +30,13 @@ struct pt_regs; #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) +/* + * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the + * expression but avoids the generation of any code, even if that expression + * has side-effects. + */ +#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) + /** * BUILD_BUG_ON - break compile if a condition is true. * @condition: the condition which the compiler should know is false.