All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alan Jenkins <sourcejedi.lkml@googlemail.com>
To: "Américo Wang" <xiyou.wangcong@gmail.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>,
	Hollis Blanchard <hollisb@us.ibm.com>,
	Jan Beulich <JBeulich@novell.com>,
	sfr@canb.auug.org.au, akpm@linux-foundation.org,
	linuxppc-dev@lists.ozlabs.org, kvm-ppc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-next@vger.kernel.org
Subject: Re: [PATCH] BUILD_BUG_ON: make it handle more cases
Date: Tue, 20 Oct 2009 15:43:13 +0100	[thread overview]
Message-ID: <9b2b86520910200743h4e134cf8jd2860d42b3936597@mail.gmail.com> (raw)
In-Reply-To: <20091020135835.GB2462@hack>

On 10/20/09, Américo Wang <xiyou.wangcong@gmail.com> wrote:
> On Tue, Oct 20, 2009 at 02:15:33PM +1030, Rusty Russell wrote:
>>BUILD_BUG_ON used to use the optimizer to do code elimination or fail
>>at link time; it was changed to first the size of a negative array (a
>>nicer compile time error), then (in
>>8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.
>>
>>bitfields: needs a literal constant at parse time, and can't be put under
>>	"if (__builtin_constant_p(x))" for example.
>>negative array: can handle anything, but if the compiler can't tell it's
>>	a constant, silently has no effect.
>>link time: breaks link if the compiler can't determine the value, but the
>>	linker output is not usually as informative as a compiler error.
>>
>>If we use the negative-array-size method *and* the link time trick,
>>we get the ability to use BUILD_BUG_ON() under __builtin_constant_p()
>>branches, and maximal ability for the compiler to detect errors at
>>build time.
>>
>>Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>>
>>diff --git a/include/linux/kernel.h b/include/linux/kernel.h
>>--- a/include/linux/kernel.h
>>+++ b/include/linux/kernel.h
>>@@ -683,12 +683,6 @@ struct sysinfo {
>> 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
>> };
>>
>>-/* Force a compilation error if condition is true */
>>-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
>>-
>>-/* Force a compilation error if condition is constant and true */
>>-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
>>-
>> /* Force a compilation error if condition is true, but also produce a
>>    result (of value 0 and type size_t), so the expression can be used
>>    e.g. in a structure initializer (or where-ever else comma expressions
>>@@ -696,6 +690,33 @@ struct sysinfo {
>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
>> #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
>>
>>+/**
>>+ * BUILD_BUG_ON - break compile if a condition is true.
>>+ * @cond: the condition which the compiler should know is false.
>>+ *
>>+ * If you have some code which relies on certain constants being equal, or
>>+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
>>+ * detect if someone changes it.
>>+ *
>>+ * The implementation uses gcc's reluctance to create a negative array,
>> but
>>+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not
>> arguments
>>+ * to inline functions).  So as a fallback we use the optimizer; if it
>> can't
>>+ * prove the condition is false, it will cause a link error on the
>> undefined
>>+ * "__build_bug_on_failed".  This error message can be harder to track
>> down
>>+ * though, hence the two different methods.
>>+ */
>>+#ifndef __OPTIMIZE__
>>+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
>>+#else
>>+extern int __build_bug_on_failed;
>
> Hmm, what exactly is __build_bug_on_failed?

Well, we haven't added a definition for it in this patch.  I'm sure
grep will tell you it wasn't defined before hand either.  So any
reference to it is an error - which will be reported at link time.

>>+#define BUILD_BUG_ON(condition)					\
>>+	do {							\
>>+		((void)sizeof(char[1 - 2*!!(condition)]));	\
>>+		if (condition) __build_bug_on_failed = 1;	\

If "condition" is known false at compile time, gcc -O will eliminate
the code which refers to __build_bug_on_failed.  If it's not proved to
be false - it will break the build, which is exactly what we want
BUILD_BUG_ON to do.

>>+	} while(0)
>>+#endif
>>+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
>>+
>> /* Trap pasters of __FUNCTION__ at compile-time */
>> #define __FUNCTION__ (__func__)
>>

WARNING: multiple messages have this Message-ID (diff)
From: Alan Jenkins <sourcejedi.lkml@googlemail.com>
To: "Américo Wang" <xiyou.wangcong@gmail.com>
Cc: sfr@canb.auug.org.au, Hollis Blanchard <hollisb@us.ibm.com>,
	Rusty Russell <rusty@rustcorp.com.au>,
	linux-kernel@vger.kernel.org, Jan Beulich <JBeulich@novell.com>,
	linux-next@vger.kernel.org, kvm-ppc@vger.kernel.org,
	akpm@linux-foundation.org, linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH] BUILD_BUG_ON: make it handle more cases
Date: Tue, 20 Oct 2009 15:43:13 +0100	[thread overview]
Message-ID: <9b2b86520910200743h4e134cf8jd2860d42b3936597@mail.gmail.com> (raw)
In-Reply-To: <20091020135835.GB2462@hack>

On 10/20/09, Am=E9rico Wang <xiyou.wangcong@gmail.com> wrote:
> On Tue, Oct 20, 2009 at 02:15:33PM +1030, Rusty Russell wrote:
>>BUILD_BUG_ON used to use the optimizer to do code elimination or fail
>>at link time; it was changed to first the size of a negative array (a
>>nicer compile time error), then (in
>>8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.
>>
>>bitfields: needs a literal constant at parse time, and can't be put under
>>	"if (__builtin_constant_p(x))" for example.
>>negative array: can handle anything, but if the compiler can't tell it's
>>	a constant, silently has no effect.
>>link time: breaks link if the compiler can't determine the value, but the
>>	linker output is not usually as informative as a compiler error.
>>
>>If we use the negative-array-size method *and* the link time trick,
>>we get the ability to use BUILD_BUG_ON() under __builtin_constant_p()
>>branches, and maximal ability for the compiler to detect errors at
>>build time.
>>
>>Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>>
>>diff --git a/include/linux/kernel.h b/include/linux/kernel.h
>>--- a/include/linux/kernel.h
>>+++ b/include/linux/kernel.h
>>@@ -683,12 +683,6 @@ struct sysinfo {
>> 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. *=
/
>> };
>>
>>-/* Force a compilation error if condition is true */
>>-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
>>-
>>-/* Force a compilation error if condition is constant and true */
>>-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
>>-
>> /* Force a compilation error if condition is true, but also produce a
>>    result (of value 0 and type size_t), so the expression can be used
>>    e.g. in a structure initializer (or where-ever else comma expressions
>>@@ -696,6 +690,33 @@ struct sysinfo {
>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
>> #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
>>
>>+/**
>>+ * BUILD_BUG_ON - break compile if a condition is true.
>>+ * @cond: the condition which the compiler should know is false.
>>+ *
>>+ * If you have some code which relies on certain constants being equal, =
or
>>+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON t=
o
>>+ * detect if someone changes it.
>>+ *
>>+ * The implementation uses gcc's reluctance to create a negative array,
>> but
>>+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not
>> arguments
>>+ * to inline functions).  So as a fallback we use the optimizer; if it
>> can't
>>+ * prove the condition is false, it will cause a link error on the
>> undefined
>>+ * "__build_bug_on_failed".  This error message can be harder to track
>> down
>>+ * though, hence the two different methods.
>>+ */
>>+#ifndef __OPTIMIZE__
>>+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])=
)
>>+#else
>>+extern int __build_bug_on_failed;
>
> Hmm, what exactly is __build_bug_on_failed?

Well, we haven't added a definition for it in this patch.  I'm sure
grep will tell you it wasn't defined before hand either.  So any
reference to it is an error - which will be reported at link time.

>>+#define BUILD_BUG_ON(condition)					\
>>+	do {							\
>>+		((void)sizeof(char[1 - 2*!!(condition)]));	\
>>+		if (condition) __build_bug_on_failed =3D 1;	\

If "condition" is known false at compile time, gcc -O will eliminate
the code which refers to __build_bug_on_failed.  If it's not proved to
be false - it will break the build, which is exactly what we want
BUILD_BUG_ON to do.

>>+	} while(0)
>>+#endif
>>+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
>>+
>> /* Trap pasters of __FUNCTION__ at compile-time */
>> #define __FUNCTION__ (__func__)
>>

WARNING: multiple messages have this Message-ID (diff)
From: Alan Jenkins <sourcejedi.lkml@googlemail.com>
To: "Américo Wang" <xiyou.wangcong@gmail.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>,
	Hollis Blanchard <hollisb@us.ibm.com>,
	Jan Beulich <JBeulich@novell.com>,
	sfr@canb.auug.org.au, akpm@linux-foundation.org,
	linuxppc-dev@lists.ozlabs.org, kvm-ppc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-next@vger.kernel.org
Subject: Re: [PATCH] BUILD_BUG_ON: make it handle more cases
Date: Tue, 20 Oct 2009 14:43:13 +0000	[thread overview]
Message-ID: <9b2b86520910200743h4e134cf8jd2860d42b3936597@mail.gmail.com> (raw)
In-Reply-To: <20091020135835.GB2462@hack>

On 10/20/09, Américo Wang <xiyou.wangcong@gmail.com> wrote:
> On Tue, Oct 20, 2009 at 02:15:33PM +1030, Rusty Russell wrote:
>>BUILD_BUG_ON used to use the optimizer to do code elimination or fail
>>at link time; it was changed to first the size of a negative array (a
>>nicer compile time error), then (in
>>8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.
>>
>>bitfields: needs a literal constant at parse time, and can't be put under
>>	"if (__builtin_constant_p(x))" for example.
>>negative array: can handle anything, but if the compiler can't tell it's
>>	a constant, silently has no effect.
>>link time: breaks link if the compiler can't determine the value, but the
>>	linker output is not usually as informative as a compiler error.
>>
>>If we use the negative-array-size method *and* the link time trick,
>>we get the ability to use BUILD_BUG_ON() under __builtin_constant_p()
>>branches, and maximal ability for the compiler to detect errors at
>>build time.
>>
>>Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
>>
>>diff --git a/include/linux/kernel.h b/include/linux/kernel.h
>>--- a/include/linux/kernel.h
>>+++ b/include/linux/kernel.h
>>@@ -683,12 +683,6 @@ struct sysinfo {
>> 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
>> };
>>
>>-/* Force a compilation error if condition is true */
>>-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
>>-
>>-/* Force a compilation error if condition is constant and true */
>>-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
>>-
>> /* Force a compilation error if condition is true, but also produce a
>>    result (of value 0 and type size_t), so the expression can be used
>>    e.g. in a structure initializer (or where-ever else comma expressions
>>@@ -696,6 +690,33 @@ struct sysinfo {
>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
>> #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
>>
>>+/**
>>+ * BUILD_BUG_ON - break compile if a condition is true.
>>+ * @cond: the condition which the compiler should know is false.
>>+ *
>>+ * If you have some code which relies on certain constants being equal, or
>>+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
>>+ * detect if someone changes it.
>>+ *
>>+ * The implementation uses gcc's reluctance to create a negative array,
>> but
>>+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not
>> arguments
>>+ * to inline functions).  So as a fallback we use the optimizer; if it
>> can't
>>+ * prove the condition is false, it will cause a link error on the
>> undefined
>>+ * "__build_bug_on_failed".  This error message can be harder to track
>> down
>>+ * though, hence the two different methods.
>>+ */
>>+#ifndef __OPTIMIZE__
>>+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
>>+#else
>>+extern int __build_bug_on_failed;
>
> Hmm, what exactly is __build_bug_on_failed?

Well, we haven't added a definition for it in this patch.  I'm sure
grep will tell you it wasn't defined before hand either.  So any
reference to it is an error - which will be reported at link time.

>>+#define BUILD_BUG_ON(condition)					\
>>+	do {							\
>>+		((void)sizeof(char[1 - 2*!!(condition)]));	\
>>+		if (condition) __build_bug_on_failed = 1;	\

If "condition" is known false at compile time, gcc -O will eliminate
the code which refers to __build_bug_on_failed.  If it's not proved to
be false - it will break the build, which is exactly what we want
BUILD_BUG_ON to do.

>>+	} while(0)
>>+#endif
>>+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
>>+
>> /* Trap pasters of __FUNCTION__ at compile-time */
>> #define __FUNCTION__ (__func__)
>>

  reply	other threads:[~2009-10-20 14:43 UTC|newest]

Thread overview: 97+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-29  9:28 linux-next: tree build failure Jan Beulich
2009-09-29  9:28 ` Jan Beulich
2009-09-29  9:28 ` Jan Beulich
2009-09-29  9:28 ` Jan Beulich
2009-09-29  9:51 ` roel kluin
2009-09-29  9:51   ` roel kluin
2009-09-29  9:51   ` roel kluin
2009-09-29  9:51   ` roel kluin
2009-09-30  6:29   ` Jan Beulich
2009-09-30  6:29     ` Jan Beulich
2009-09-30  6:29     ` Jan Beulich
2009-09-30  6:29     ` Jan Beulich
2009-09-29 23:39 ` Hollis Blanchard
2009-09-29 23:39   ` Hollis Blanchard
2009-09-29 23:39   ` Hollis Blanchard
2009-09-29 23:39   ` Hollis Blanchard
2009-09-30  6:35   ` Jan Beulich
2009-09-30  6:35     ` Jan Beulich
2009-09-30  6:35     ` Jan Beulich
2009-09-30  6:35     ` Jan Beulich
2009-10-02 15:48     ` Hollis Blanchard
2009-10-02 15:48       ` Hollis Blanchard
2009-10-02 15:48       ` Hollis Blanchard
2009-10-02 15:48       ` Hollis Blanchard
2009-10-05  6:58       ` Jan Beulich
2009-10-05  6:58         ` Jan Beulich
2009-10-05  6:58         ` Jan Beulich
2009-10-05  6:58         ` Jan Beulich
2009-10-09 19:14         ` Hollis Blanchard
2009-10-09 19:14           ` Hollis Blanchard
2009-10-09 19:14           ` Hollis Blanchard
2009-10-14 22:57           ` Hollis Blanchard
2009-10-14 22:57             ` Hollis Blanchard
2009-10-14 22:57             ` Hollis Blanchard
2009-10-15  7:27             ` Jan Beulich
2009-10-15  7:27               ` Jan Beulich
2009-10-15  7:27               ` Jan Beulich
2009-10-15  7:27               ` Jan Beulich
2009-10-19 18:19               ` Hollis Blanchard
2009-10-19 18:19                 ` Hollis Blanchard
2009-10-19 18:19                 ` Hollis Blanchard
2009-10-20  1:12                 ` Rusty Russell
2009-10-20  1:24                   ` Rusty Russell
2009-10-20  1:12                   ` Rusty Russell
2009-10-20  1:29                   ` Hollis Blanchard
2009-10-20  1:29                     ` Hollis Blanchard
2009-10-20  1:29                     ` Hollis Blanchard
2009-10-20  1:29                     ` Hollis Blanchard
2009-10-20  3:45                     ` [PATCH] BUILD_BUG_ON: make it handle more cases Rusty Russell
2009-10-20  3:57                       ` Rusty Russell
2009-10-20  3:45                       ` Rusty Russell
2009-10-20  3:45                       ` Rusty Russell
2009-10-20 13:58                       ` Américo Wang
2009-10-20 13:58                         ` Américo Wang
2009-10-20 13:58                         ` Américo Wang
2009-10-20 14:43                         ` Alan Jenkins [this message]
2009-10-20 14:43                           ` Alan Jenkins
2009-10-20 14:43                           ` Alan Jenkins
2009-10-23  1:50                           ` Américo Wang
2009-10-23  1:50                             ` Américo Wang
2009-10-23  1:50                             ` Américo Wang
2009-10-23  1:50                             ` Américo Wang
2009-10-22 21:04                       ` Hollis Blanchard
2009-10-22 21:04                         ` Hollis Blanchard
2009-10-22 21:04                         ` Hollis Blanchard
2009-10-22 21:04                         ` Hollis Blanchard
2009-10-29 21:30                       ` Hollis Blanchard
2009-10-29 21:30                         ` Hollis Blanchard
2009-10-29 21:30                         ` Hollis Blanchard
2009-11-05  0:20                       ` Stephen Rothwell
2009-11-05  0:20                         ` Stephen Rothwell
2009-11-05  0:20                         ` Stephen Rothwell
2009-11-05  0:20                         ` Stephen Rothwell
2009-11-05  6:28                         ` Rusty Russell
2009-11-05  6:40                           ` Rusty Russell
2009-11-05  6:28                           ` Rusty Russell
2009-11-05  6:37                           ` Rusty Russell
2009-11-05  6:49                             ` Rusty Russell
2009-11-05  6:37                             ` Rusty Russell
2009-11-05  6:37                             ` Rusty Russell
2009-11-05  6:38                           ` Stephen Rothwell
2009-11-05  6:38                             ` Stephen Rothwell
2009-11-05  6:38                             ` Stephen Rothwell
2009-11-06  6:30                             ` Rusty Russell
2009-11-06  6:42                               ` Rusty Russell
2009-11-06  6:30                               ` Rusty Russell
2009-11-06  6:30                               ` Rusty Russell
2009-11-05  6:01                       ` Benjamin Herrenschmidt
2009-11-05  6:01                         ` Benjamin Herrenschmidt
2009-11-05  6:01                         ` Benjamin Herrenschmidt
  -- strict thread matches above, loose matches on Subject: below --
2009-09-24  5:21 linux-next: tree build failure Stephen Rothwell
2009-09-24  5:21 ` Stephen Rothwell
2009-09-24  5:21 ` Stephen Rothwell
2009-09-29  0:00 ` Hollis Blanchard
2009-09-29  0:00   ` Hollis Blanchard
2009-09-29  0:00   ` Hollis Blanchard
2009-09-29  0:00   ` Hollis Blanchard

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=9b2b86520910200743h4e134cf8jd2860d42b3936597@mail.gmail.com \
    --to=sourcejedi.lkml@googlemail.com \
    --cc=JBeulich@novell.com \
    --cc=akpm@linux-foundation.org \
    --cc=hollisb@us.ibm.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-next@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=rusty@rustcorp.com.au \
    --cc=sfr@canb.auug.org.au \
    --cc=xiyou.wangcong@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.