>From c62cffde2be0debcbb35590a1ffc993161fcdb28 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 7 Jul 2022 20:21:39 +0100 Subject: [PATCH] common/include: Rename BUILD_BUG_ON_ZERO to something more meaningful Updates: commit 0fa23df5cd5dc97a55857416ea81d5de6d867c18 Thanks: Laszlo Ersek, Eric Blake --- common/include/array-size.h | 2 +- common/include/compiler-macros.h | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/common/include/array-size.h b/common/include/array-size.h index b6d33dde..3212d9dc 100644 --- a/common/include/array-size.h +++ b/common/include/array-size.h @@ -36,6 +36,6 @@ #include "compiler-macros.h" #define ARRAY_SIZE(a) \ - ((sizeof (a) / sizeof ((a)[0])) + BUILD_BUG_ON_ZERO (!TYPE_IS_ARRAY(a))) + ((sizeof (a) / sizeof ((a)[0])) + BUILD_BUG_UNLESS_TRUE (TYPE_IS_ARRAY(a))) #endif /* NBDKIT_ARRAY_SIZE_H */ diff --git a/common/include/compiler-macros.h b/common/include/compiler-macros.h index 504e0085..1a6cba2b 100644 --- a/common/include/compiler-macros.h +++ b/common/include/compiler-macros.h @@ -35,24 +35,26 @@ #ifndef __cplusplus -/* This expression fails at compile time if 'expr' is true. It does - * this by constructing a struct which has an impossible - * (negative-sized) array. +/* BUILD_BUG_UNLESS_TRUE(1) => 0 + * BUILD_BUG_UNLESS_TRUE(0) => build time failure * - * If 'expr' is false then we subtract the sizes of the two identical - * structures, returning zero. + * It works by constructing a struct which has an impossible + * (negative-sized) bit-field in the build failure case. + * + * The Linux kernel calls this BUILD_BUG_ON_ZERO which is a + * confusing name, but has the same semantics as above. */ -#define BUILD_BUG_ON_ZERO_SIZEOF(expr) \ - (sizeof (struct { int _array_size_failed[(expr) ? -1 : 1]; })) -#define BUILD_BUG_ON_ZERO(expr) \ - (BUILD_BUG_ON_ZERO_SIZEOF(expr) - BUILD_BUG_ON_ZERO_SIZEOF(expr)) +#define BUILD_BUG_STRUCT_SIZE(expr) \ + (sizeof (struct { int: (expr) ? 1 : -1; })) +#define BUILD_BUG_UNLESS_TRUE(expr) \ + (BUILD_BUG_STRUCT_SIZE(expr) - BUILD_BUG_STRUCT_SIZE(expr)) #define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) #else /* __cplusplus */ -#define BUILD_BUG_ON_ZERO(expr) 0 +#define BUILD_BUG_UNLESS_TRUE(expr) 0 #define TYPE_IS_ARRAY(a) 1 #endif /* __cplusplus */ -- 2.37.0.rc2