This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: if() and trailing ;
- From: "Denis Vlasenko" <vda dot linux at googlemail dot com>
- To: "Joe Buck" <Joe dot Buck at synopsys dot com>
- Cc: "Simon Boulet" <simon at nostalgeek dot com>, gcc at gcc dot gnu dot org
- Date: Tue, 1 Aug 2006 11:52:13 +0100
- Subject: Re: if() and trailing ;
- References: <5A1EA79B-D8B0-4076-8E22-7F81BAA5685C@nostalgeek.com> <20060730043255.GA10835@synopsys.com>
On 7/30/06, Joe Buck <Joe.Buck@synopsys.com> wrote:
On Sat, Jul 29, 2006 at 07:33:03PM -0400, Simon Boulet wrote:
> After a couple hours debugging code, I figured our an if() somewhere
> had a trailing ; like this:
>
> if (memcmp(p, COMMUNITY, strlen(COMMUNITY)) != 0);
> continue; /* failed */
>
> The code above will always reach "continue" even when memcmp() == 0.
>
> I was surprised to see gcc doesn't report anything, I would expect a
> "statement has no effect" warning.
But it is common to have an empty action on a condition. You'll often
see code like
if (condition)
/* nothing */;
or more usually
if (condition) {
/* nothing */;
if()
(void)0; /* do nothing */
will make you happy.
}
or even more usually
if (condition) {
INVOKE_MACRO(...);
}
where the macro might be null on some platforms.
Empty macros have lots of other obscure failure cases.
if(...)
SOME_MACRO(with complex arguments)
do_something();
If SOME_MACRO is usually #defined to /* nothing */,
say, on your architecture, you've got a bad bug there.
Make it ((void)0) or, better yet, do {} while(0).
--
vda