From: Marek Polacek <polacek@redhat.com>
To: Martin Sebor <msebor@gmail.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>,
Jason Merrill <jason@redhat.com>,
Joseph Myers <joseph@codesourcery.com>
Subject: Re: [C/C++ PATCH] RFC: Implement -Wduplicated-cond (PR c/64249)
Date: Fri, 18 Sep 2015 10:24:00 -0000 [thread overview]
Message-ID: <20150918100606.GF27588@redhat.com> (raw)
In-Reply-To: <55FAEC54.1070508@gmail.com>
On Thu, Sep 17, 2015 at 10:37:40AM -0600, Martin Sebor wrote:
> >>The patch currently issues a false positive for the test case
> >>below. I suspect the chain might need to be cleared after each
> >>condition that involves a side-effect.
> >>
> >> int foo (int a)
> >> {
> >> if (a) return 1; else if (++a) return 2; else if (a) return 3;
> >> return 0;
> >> }
> >
> >But the last branch here can never be reached, right? If a == 0, foo
> >returns 2, otherwise it just returns 1. So I think we should diagnose
> >this.
>
> It probably wasn't the best example. The general issue here is
> that the second condition has a side-effect that can change (in
> this case clearly does) the value of the expression.
>
> Here's a better example:
>
> int a;
>
> int bar (void) { a = 1; return 0; }
>
> int foo (void) {
> if (a) return 1;
> else if (foo ()) return 2;
> else if (a) return 3;
> return 0;
> }
>
> Since we don't know bar's side-effects we must assume they change
> the value of a and so we must avoid diagnosing the third if.
Ok, I'm convinced now. We have something similar in the codebase:
libsupc++/eh_catch.cc has
int count = header->handlerCount;
if (count < 0)
{
// This exception was rethrown. Decrement the (inverted) catch
// count and remove it from the chain when it reaches zero.
if (++count == 0)
globals->caughtExceptions = header->nextException;
}
else if (--count == 0)
{
// Handling for this exception is complete. Destroy the object.
globals->caughtExceptions = header->nextException;
_Unwind_DeleteException (&header->unwindHeader);
return;
}
else if (count < 0)
// A bug in the exception handling library or compiler.
std::terminate ();
Here all arms are reachable. I guess I need to kill the chain of conditions
when we find something with side-effects, exactly as you suggested.
Again, thanks for your comments.
Marek
prev parent reply other threads:[~2015-09-18 10:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-16 16:02 Marek Polacek
2015-09-16 21:21 ` Martin Sebor
2015-09-17 16:12 ` Marek Polacek
2015-09-17 16:37 ` Jeff Law
2015-09-17 16:38 ` Martin Sebor
2015-09-18 10:24 ` Marek Polacek [this message]
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=20150918100606.GF27588@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
--cc=joseph@codesourcery.com \
--cc=msebor@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 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 read-only IMAP folder(s) and NNTP newsgroup(s).