From: Matthew Wilcox <willy@infradead.org>
To: linux-sparse@vger.kernel.org
Subject: Unreachable code diagnostic
Date: Fri, 24 Feb 2017 10:07:59 -0800 [thread overview]
Message-ID: <20170224180759.GB16328@bombadil.infradead.org> (raw)
I was recently sent some code that looked like this:
int foo()
{
lock();
return bar();
unlock();
}
When you're restructuring code that contains locks, this is a
*really* easy mistake to make. I've done it myself. But there's no
compiler warning for it! gcc doesn't have it, sparse doesn't have it.
I've mentioned it to the gcc developers and they don't seem terribly
enthusiastic (they had a -Wunreachable-code at one point, but it
got disabled, probably due to too many false positives like their
-Wmaybe-uninitialized).
Maybe sparse could warn about code after an unconditional return
statement? I wouldn't like to see it warn about code after a conditional
return statement where the condition is always true; I think that would
have a lot of false positives due to macros. For example, something
like this:
int foo()
{
int i = 0;
for (;;) {
if (i == FOO_MAX)
return i;
bar(i++);
}
}
if FOO_MAX happens to be 0 should silently optimise to 'return 0' and
not emit a warning.
I would like to see it warn in this case:
int foo()
{
return bar();
do { } while (0);
}
As that can be generated by the preprocessor in the case of optimising
away locks for the !SMP case, for example.
Any takers for this idea? :-)
next reply other threads:[~2017-02-24 18:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-24 18:07 Matthew Wilcox [this message]
2017-02-24 18:33 ` Unreachable code diagnostic Linus Torvalds
2017-02-24 20:11 ` Luc Van Oostenryck
2017-02-24 20:43 ` Josh Triplett
2017-02-24 20:47 ` Luc Van Oostenryck
2017-02-24 18:57 ` Josh Triplett
2017-02-24 19:56 ` Luc Van Oostenryck
2017-02-24 21:05 ` Matthew Wilcox
2017-02-24 19:26 ` Dan Carpenter
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=20170224180759.GB16328@bombadil.infradead.org \
--to=willy@infradead.org \
--cc=linux-sparse@vger.kernel.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).