From: Joe Perches <joe@perches.com>
To: Vegard Nossum <vegard.nossum@oracle.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org,
Daniel Santos <daniel.santos@pobox.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Masahiro Yamada <yamada.masahiro@socionext.com>,
Ian Abbott <abbotti@mev.co.uk>
Subject: Re: [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting
Date: Tue, 31 Mar 2020 11:20:07 -0700 [thread overview]
Message-ID: <dc53b8704ec674cba636b41d7f55bf507a7bd7aa.camel@perches.com> (raw)
In-Reply-To: <20200331112637.25047-1-vegard.nossum@oracle.com>
On Tue, 2020-03-31 at 13:26 +0200, Vegard Nossum wrote:
> compiletime_assert() uses __LINE__ to create a unique function name.
> This means that if you have more than one BUILD_BUG_ON() in the same
> source line (which can happen if they appear e.g. in a macro), then
> the error message from the compiler might output the wrong condition.
>
> For this source file:
>
> #include <linux/build_bug.h>
>
> #define macro() \
> BUILD_BUG_ON(1); \
> BUILD_BUG_ON(0);
>
> void foo()
> {
> macro();
> }
>
> gcc would output:
>
> ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_9’ declared with attribute error: BUILD_BUG_ON failed: 0
> _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
>
> However, it was not the BUILD_BUG_ON(0) that failed, so it should say 1
> instead of 0. With this patch, we use __COUNTER__ instead of __LINE__, so
> each BUILD_BUG_ON() gets a different function name and the correct
> condition is printed:
>
> ./include/linux/compiler.h:350:38: error: call to ‘__compiletime_assert_0’ declared with attribute error: BUILD_BUG_ON failed: 1
> _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
[]
> diff --git a/include/linux/compiler.h b/include/linux/compiler.h
[]
> @@ -347,7 +347,7 @@ static inline void *offset_to_ptr(const int *off)
> * compiler has support to do so.
> */
> #define compiletime_assert(condition, msg) \
> - _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
> + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
This might be better using something like __LINE__ ## _ ## __COUNTER__
as line # is somewhat useful to identify the specific assert in a file.
next prev parent reply other threads:[~2020-03-31 18:22 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-31 11:26 Vegard Nossum
2020-03-31 17:31 ` Masahiro Yamada
2020-03-31 18:20 ` Joe Perches [this message]
2020-03-31 18:56 ` Rasmus Villemoes
2020-03-31 19:00 ` Joe Perches
2020-03-31 19:05 ` Rasmus Villemoes
2020-03-31 19:08 ` Vegard Nossum
2020-04-01 1:12 ` Daniel Santos
2020-03-31 22:30 ` Daniel Santos
2020-03-31 22:25 ` Daniel Santos
2020-03-31 22:36 ` Nathan Chancellor
2020-04-01 0:53 ` Daniel Santos
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=dc53b8704ec674cba636b41d7f55bf507a7bd7aa.camel@perches.com \
--to=joe@perches.com \
--cc=abbotti@mev.co.uk \
--cc=akpm@linux-foundation.org \
--cc=daniel.santos@pobox.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=vegard.nossum@oracle.com \
--cc=yamada.masahiro@socionext.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®