public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/44511]  New: Misdetects missing return with non-void return type, but only if the function is static
@ 2010-06-13  8:55 richard+gcc at sfere dot greenend dot org dot uk
  2010-06-13 10:33 ` [Bug c/44511] " rguenth at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: richard+gcc at sfere dot greenend dot org dot uk @ 2010-06-13  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

GCC 4.5 -Wall warns if your function has a non-void return type but
returns.  It can also detect an infinite loop which means that
"falling off the end" doesn't actually imply a return, and therefore
suppress the warning in that case.

However, if the function is static, this infinite loop detection does
not work:

  richard@deodand:~$ cat t.c
  void *a(void) { for(;;); }
  static void *b(void) { for(;;) { } }
  void foo(void) { b(); }

  richard@deodand:~$ /usr/local/gcc-4.5.0/bin/gcc -c -Wall t.c
  t.c: In function ‘b’:
  t.c:2:1: warning: no return statement in function returning non-void

This real case where I spotted it was a thread function which is
forced to have a return type of 'void *' by the signature of
pthread_create(), but never returns because (by design) the thread
never terminates.

(I assume that the infinite loop detection isn't perfect, but
obviously it shouldn't depend on the linkage of the containing
function.)


richard@deodand:~$ /usr/local/gcc-4.5.0/bin/gcc --version
gcc (GCC) 4.5.0
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Configured with:

richard@deodand:~$ ../configure -C --prefix=/usr/local/gcc-4.5.0
--enable-languages=c,c++

richard@deodand:~$ uname -a
Linux deodand 2.6.32-5-686-bigmem #1 SMP Tue Jun 1 05:38:08 UTC 2010 i686
GNU/Linux
richard@deodand:~$ dpkg -l libc6
ii  libc6          2.11.1-3       Embedded GNU C Library: Shared libraries


-- 
           Summary: Misdetects missing return with non-void return type, but
                    only if the function is static
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: richard+gcc at sfere dot greenend dot org dot uk


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


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

* [Bug c/44511] Misdetects missing return with non-void return type, but only if the function is static
  2010-06-13  8:55 [Bug c/44511] New: Misdetects missing return with non-void return type, but only if the function is static richard+gcc at sfere dot greenend dot org dot uk
@ 2010-06-13 10:33 ` rguenth at gcc dot gnu dot org
  2010-06-13 13:49 ` manu at gcc dot gnu dot org
  2010-06-13 14:18 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-06-13 10:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2010-06-13 10:33 -------
It's because

  /* Complain if there's just no return statement.  */
  if (warn_return_type
...
      /* Normally, with -Wreturn-type, flow will complain, but we might
         optimize out static functions.  */
      && !TREE_PUBLIC (fndecl))

so this is very simple detection inside the frontend which is only done
for functions with static linkage (to preserve warnings on unused
static functions).  Elsewhere you'd see

  "control reaches end of non-void function"

instead.  So the warning you see is really warning about no return statement,
not about control reaching the end of a non-void function.  And it does so
by design just for functions with static linkage.

So I'd say the warning is not false, it is just not useful for the case
where the function does not return at all.

Leaving the bug as enhancement request instead of closing as wontfix/invalid.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |diagnostic


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


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

* [Bug c/44511] Misdetects missing return with non-void return type, but only if the function is static
  2010-06-13  8:55 [Bug c/44511] New: Misdetects missing return with non-void return type, but only if the function is static richard+gcc at sfere dot greenend dot org dot uk
  2010-06-13 10:33 ` [Bug c/44511] " rguenth at gcc dot gnu dot org
@ 2010-06-13 13:49 ` manu at gcc dot gnu dot org
  2010-06-13 14:18 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-06-13 13:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from manu at gcc dot gnu dot org  2010-06-13 13:49 -------
(In reply to comment #1)
> 
> instead.  So the warning you see is really warning about no return statement,
> not about control reaching the end of a non-void function.  And it does so
> by design just for functions with static linkage.
> 
> So I'd say the warning is not false, it is just not useful for the case
> where the function does not return at all.
> 
> Leaving the bug as enhancement request instead of closing as wontfix/invalid.
> 

I don't see how one can detect that the function does not return from the FE,
so I don't think this is possible to implement without moving the warning to
the middle-end. Perhaps we should not care about unused static functions and
remove this warning altogether.

What happens if the static function actually returns and is used? Do you get 2
warnings?


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org


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


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

* [Bug c/44511] Misdetects missing return with non-void return type, but only if the function is static
  2010-06-13  8:55 [Bug c/44511] New: Misdetects missing return with non-void return type, but only if the function is static richard+gcc at sfere dot greenend dot org dot uk
  2010-06-13 10:33 ` [Bug c/44511] " rguenth at gcc dot gnu dot org
  2010-06-13 13:49 ` manu at gcc dot gnu dot org
@ 2010-06-13 14:18 ` rguenth at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-06-13 14:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from rguenth at gcc dot gnu dot org  2010-06-13 14:18 -------
(In reply to comment #2)
> (In reply to comment #1)
> > 
> > instead.  So the warning you see is really warning about no return statement,
> > not about control reaching the end of a non-void function.  And it does so
> > by design just for functions with static linkage.
> > 
> > So I'd say the warning is not false, it is just not useful for the case
> > where the function does not return at all.
> > 
> > Leaving the bug as enhancement request instead of closing as wontfix/invalid.
> > 
> 
> I don't see how one can detect that the function does not return from the FE,
> so I don't think this is possible to implement without moving the warning to
> the middle-end. Perhaps we should not care about unused static functions and
> remove this warning altogether.
> 
> What happens if the static function actually returns and is used? Do you get 2
> warnings?

No, the Frontend sets TREE_NO_WARNING after it issued the warning.


-- 


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


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

end of thread, other threads:[~2010-06-13 14:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-13  8:55 [Bug c/44511] New: Misdetects missing return with non-void return type, but only if the function is static richard+gcc at sfere dot greenend dot org dot uk
2010-06-13 10:33 ` [Bug c/44511] " rguenth at gcc dot gnu dot org
2010-06-13 13:49 ` manu at gcc dot gnu dot org
2010-06-13 14:18 ` rguenth at gcc dot gnu 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).