From: Ingo Molnar <mingo@elte.hu>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>,
Alexey Dobriyan <adobriyan@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Sam Ravnborg <sam@ravnborg.org>
Subject: Re: [PATCH] debug: fix BUILD_BUG_ON() for non-constant expressions
Date: Sun, 17 Aug 2008 19:33:19 +0200 [thread overview]
Message-ID: <20080817173319.GA2450@elte.hu> (raw)
In-Reply-To: <alpine.LFD.1.10.0808170936260.3324@nehalem.linux-foundation.org>
* Linus Torvalds <torvalds@linux-foundation.org> wrote:
>
>
> On Sun, 17 Aug 2008, Ingo Molnar wrote:
> > +/*
> > + * Force a compilation error if condition is true [array index becomes
> > + * negative], and a linker error if condition is not constant [non-defined
> > + * variable is used as an array index]:
> > + *
> > + * ( The linker trick relies on gcc optimizing out a multiplication with
> > + * constant zero - which should be reasonable enough. )
> > + */
> > +extern unsigned int __BUILD_BUG_ON_non_constant;
> > +
> > +#define BUILD_BUG_ON(condition) \
> > +do { \
> > + (void)sizeof(char[1 - 2*!!(condition)]); \
> > + if (!__builtin_constant_p(condition)) \
> > + __BUILD_BUG_ON_non_constant++; \
> > +} while (0)
>
> Gag me now.
>
> Why not just do
>
> #define __BBO(c) sizeof(const char[1 - 2*!!(c)])
> #define __BBONC(c) __BBO(!__builtin_constant_p(c))
> #define BUILD_BUG_ON_ZERO(c) (__BBO(c) - __BBONC(c))
> #define BUILD_BUG_ON(c) (void)BUILD_BUG_ON_ZERO(c)
>
> and be done with it?
yeah, i first tried a few variants of that (compile-time warnings are
much better than link time warnings), but none worked when i tested
various failure modes.
try the patch below - it only gives this error during build:
kernel/sched.c: In function ‘test':
kernel/sched.c:9193: error: size of array ‘type name' is negative
make[1]: *** [kernel/sched.o] Error 1
it doesnt notice the error on the next line. I suspect this is because
__builtin_constant_p() is special (or broken). I tried it with gcc 4.2.3
and 4.3.0.
Ingo
Index: linux/kernel/sched.c
===================================================================
--- linux.orig/kernel/sched.c
+++ linux/kernel/sched.c
@@ -9181,3 +9181,16 @@ struct cgroup_subsys cpuacct_subsys = {
.subsys_id = cpuacct_subsys_id,
};
#endif /* CONFIG_CGROUP_CPUACCT */
+
+#define __BBO(c) sizeof(const char[1 - 2*!!(c)])
+#define __BBONC(c) __BBO(!__builtin_constant_p(c))
+#define BUILD_BUG_ON_ZERO2(c) (__BBO(c) - __BBONC(c))
+#define BUILD_BUG_ON2(c) (void)BUILD_BUG_ON_ZERO(c)
+
+void test(void)
+{
+ BUILD_BUG_ON2(0);
+ BUILD_BUG_ON2(1);
+ BUILD_BUG_ON2(panic_timeout);
+}
+
next prev parent reply other threads:[~2008-08-17 17:34 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-16 10:09 [PATCH] BUILD_BUG_ON sucks Alexey Dobriyan
2008-08-16 10:55 ` Rusty Russell
2008-08-16 20:07 ` Linus Torvalds
2008-08-17 10:32 ` [PATCH] debug: fix BUILD_BUG_ON() for non-constant expressions Ingo Molnar
2008-08-17 16:56 ` Linus Torvalds
2008-08-17 17:33 ` Ingo Molnar [this message]
2008-08-17 17:53 ` Ingo Molnar
2008-08-17 18:39 ` Linus Torvalds
2008-08-17 18:45 ` Ingo Molnar
2008-08-18 1:09 ` Rusty Russell
2008-08-18 7:54 ` Ingo Molnar
2008-08-18 9:55 ` Boaz Harrosh
2008-08-18 12:32 ` Boaz Harrosh
2008-08-19 13:34 ` Ingo Molnar
2008-08-19 16:33 ` Boaz Harrosh
2008-08-20 10:59 ` Ingo Molnar
2008-08-20 12:31 ` Boaz Harrosh
2008-08-20 12:39 ` adobriyan
2008-08-20 13:07 ` Boaz Harrosh
2008-08-21 12:17 ` Ingo Molnar
2008-08-25 1:19 ` Rusty Russell
2008-08-20 13:21 ` Boaz Harrosh
2008-08-16 17:46 ` [PATCH] BUILD_BUG_ON sucks Andrew Morton
2008-08-17 12:19 ` Theodore Tso
2008-08-17 16:33 ` Andrew Morton
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=20080817173319.GA2450@elte.hu \
--to=mingo@elte.hu \
--cc=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rusty@rustcorp.com.au \
--cc=sam@ravnborg.org \
--cc=torvalds@linux-foundation.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).