From: jim.cromie@gmail.com (Jim Cromie)
To: kernelnewbies@lists.kernelnewbies.org
Subject: [PATCH 1/3] bug.h: add BUILD_BUG_DECL, usable at file scope
Date: Mon, 9 Apr 2012 23:09:44 -0600 [thread overview]
Message-ID: <1334034585-16651-2-git-send-email-jim.cromie@gmail.com> (raw)
In-Reply-To: <1334034585-16651-1-git-send-email-jim.cromie@gmail.com>
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@gmail.com>
---
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.8.1
next prev parent reply other threads:[~2012-04-10 5:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-10 5:09 __initdata access at runtime Jim Cromie
2012-04-10 5:09 ` Jim Cromie [this message]
2012-04-10 5:09 ` [PATCH 2/3] bug.h: add test/demo module Jim Cromie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1334034585-16651-2-git-send-email-jim.cromie@gmail.com \
--to=jim.cromie@gmail.com \
--cc=kernelnewbies@lists.kernelnewbies.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).