[PATCH 1/2] bug.h: add BUILD_BUG_DECL, usable at file scope

From: Jim Cromie
Date: Sun Apr 08 2012 - 18:39:22 EST


This adds BUILD_BUG_DECL, primarily to check sizes of file-scoped
arrays etc:

const char const *names[] = { "bart", "lisa", "homer", "marge" };
int a[] = {1,2,3,4}, b[] = {1,2,3,5}, c[] = {1,2}
long d[] = {1,2};
BUILD_BUG_DECL(Luke, sizeof(a) != sizeof(d)); // ok, but iffy usage
BUILD_BUG_DECL(Han, sizeof(a) != sizeof(b)); // good
BUILD_BUG_DECL(Obi, ARRAY_SIZE(a) != ARRAY_SIZE(b)); // better
BUILD_BUG_DECL(Yoda, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // good, on different types
BUILD_BUG_DECL(Darth, sizeof(a) != sizeof(c)); // compile err

example 1 expands as:
static __attribute__ ((__section__(".init.data"))) struct {
int BUILD_BUG_DECL_Luke[1 - 2*!!(sizeof(a) != sizeof(b))];
} BUILD_BUG_DECL_Luke[0] __attribute__((unused));

The name parameter distinguishes multiple uses in the same scope, but
is otherwise arbitrary. You can reuse the name of one of the checked
vars, or pick something easy to find on the rare occaision when the
assertion breaks the build.

example 5 yields:
error: size of array âBUILD_BUG_DECL_Darthâ is negative

Signed-off-by: Jim Cromie <jim.cromie@xxxxxxxxx>
---
include/linux/bug.h | 15 +++++++++++++++
1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/include/linux/bug.h b/include/linux/bug.h
index 72961c3..c76e6f6 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -17,6 +17,7 @@ struct pt_regs;
#define BUILD_BUG_ON_NULL(e) ((void*)0)
#define BUILD_BUG_ON(condition)
#define BUILD_BUG() (0)
+#define BUILD_BUG_DECL(name, condition)
#else /* __CHECKER__ */

/* Force a compilation error if a constant expression is not a power of 2 */
@@ -70,6 +71,20 @@ extern int __build_bug_on_failed;
__build_bug_failed(); \
} while (0)

+/**
+ * BUILD_BUG_DECL - check declared objects
+ * @name: distinguishes multiple uses at same scope.
+ * @cond: false expr, typically like sizeof(a) != sizeof(b)
+ *
+ * This works at file-scope too, and supports checks like:
+ * BUILD_BUG_DECL(foo, sizeof(a) != sizeof(b));
+ * BUILD_BUG_DECL(id_strings, ARRAY_SIZE(id_strings) != ARRAY_SIZE(id_vals));
+ */
+#define BUILD_BUG_DECL(name, cond) \
+ static __initdata struct { \
+ int BUILD_BUG_DECL_ ## name[1 - 2*!!(cond)]; \
+ } BUILD_BUG_DECL_ ##name[0] __attribute__((unused))
+
#endif /* __CHECKER__ */

#ifdef CONFIG_GENERIC_BUG
--
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/