public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/31878]  New: Spurious warnings generated due to not optimizing first
@ 2007-05-09 13:12 lloyd at randombit dot net
  2007-05-09 21:07 ` [Bug c/31878] " pinskia at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: lloyd at randombit dot net @ 2007-05-09 13:12 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1735 bytes --]

$ cat return.c
#include <assert.h>

int f(int x) {
    if(x)
        return x;
    assert(x);
}

$ gcc -O2 -c -Wall -W return.c 
return.c: In function ‘int f(int)’:
return.c:9: warning: control reaches end of non-void function

The call to assert expands to (glibc 2.4):

    (static_cast<void> ((x) ? 0 : (__assert_fail ("x", "return.c", 8,
__PRETTY_FUNCTION__), 0)));

So unless I'm missing something f() is equivalent to:

if(x)
  return x;
else
  __assert_fail(/* etc */);

However when explicitly written in this way, we don't get a warning:

$ cat return2.c
#include <assert.h>

int f(int x)
{
    if(x)
        return x;
    else
        __assert_fail ("x", __FILE__, __LINE__, __PRETTY_FUNCTION__);
}

$ gcc -O2 -Wall -c return2.c
$

Looking at the produced assembly on an x86-64 system, GCC is only performing
the comparison once (when optimizing), so it should see that there is no
codepath that doesn't either return or call a noreturn function. However I'm
guessing warnings are generated prior to the optimizer running and thus don't
see this?

Marked as Severity: trivial because one can always insert a dummy return after
the assert. Which is good because AFAICT fixing this would require rewriting
GCC. :)


-- 
           Summary: Spurious warnings generated due to not optimizing first
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: trivial
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lloyd at randombit dot net
 GCC build triplet: x86_64-redhat-linux
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31878


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-12-30  1:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-09 13:12 [Bug c/31878] New: Spurious warnings generated due to not optimizing first lloyd at randombit dot net
2007-05-09 21:07 ` [Bug c/31878] " pinskia at gcc dot gnu dot org
2007-05-09 21:16 ` lloyd at randombit dot net
2007-05-10 14:50 ` manu at gcc dot gnu dot org
2007-05-10 16:51 ` lloyd at randombit dot net
2007-05-11 10:38 ` manu at gcc dot gnu dot org
2008-11-14 23:39 ` [Bug middle-end/31878] " pinskia at gcc dot gnu dot org
2009-02-12 14:59 ` [Bug middle-end/31878] Spurious warnings with -Wreturn-type due to not performing CCP/VRP in the front-end manu at gcc dot gnu dot org
2009-12-29  6:07 ` sabre at nondot dot org
2009-12-30  1:43 ` manu at gcc dot gnu dot org
2009-12-30  1:46 ` sabre at nondot dot org

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).