public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function
@ 2021-04-08 15:50 msebor at gcc dot gnu.org
  2021-04-08 15:53 ` [Bug c/99972] " msebor at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-08 15:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

            Bug ID: 99972
           Summary: missing -Wunused-result on a call to a locally
                    redeclared warn_unused_result function
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The test case below (related to pr99420) shows that GCC doesn't consistently
propagate function attributes across redeclarations of extern functions at
function scope.

The noreturn attribute on fnr() is copied from the declaration in gnr() to the
one in hnr() and successfully suppresses -Wreturn-type in both callers.  That's
how it's supposed to work.

But the warn_unused_result on fwur() is clearly not copied from the declaration
inb gwur() to the one in hwur() because only the call to the function in gwur()
triggers -Wunused-result but the call in hwur() doesn't.  That's a bug with the
same root cause as pr99420.

$ cat z.c && gcc -S -Wall -xc++ z.c
int gnr (void)
{ 
  __attribute__ ((noreturn)) int fnr (void);
  fnr ();
  // no return, no warning (good)
}

int hnr (void)
{
  int fnr (void);

  fnr ();
  // no return, no warning (good)
}


void gwur (void)
{
  __attribute__ ((warn_unused_result)) int fwur (void);
  fwur ();   // -Wunused-result (good)
}

void hwur (void)
{
  int fwur (void); 

  fwur ();   // missing -Wunused-result (bug)
}
z.c: In function ‘int hnr()’:
z.c:14:1: warning: no return statement in function returning non-void
[-Wreturn-type]
   14 | }
      | ^
z.c: In function ‘void gwur()’:
z.c:20:8: warning: ignoring return value of ‘int fwur()’ declared with
attribute ‘warn_unused_result’ [-Wunused-result]
   20 |   fwur ();   // -Wunused-result (good)
      |   ~~~~~^~

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

* [Bug c/99972] missing -Wunused-result on a call to a locally redeclared warn_unused_result function
  2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
@ 2021-04-08 15:53 ` msebor at gcc dot gnu.org
  2021-04-08 16:54 ` msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-08 15:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.2.0, 11.0, 4.7.0, 4.8.4,
                   |                            |4.9.4, 5.5.0, 6.4.0, 7.2.0,
                   |                            |8.3.0, 9.1.0
           Keywords|                            |diagnostic

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
This never worked correctly so it's not a regression.

Clang does the right thing and warns where expected:

z.c:20:3: warning: ignoring return value of function declared with
      'warn_unused_result' attribute [-Wunused-result]
  fwur ();   // -Wunused-result (good)
  ^~~~
z.c:27:3: warning: ignoring return value of function declared with
      'warn_unused_result' attribute [-Wunused-result]
  fwur ();   // missing -Wunused-result (bug)
  ^~~~
2 warnings generated.

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

* [Bug c/99972] missing -Wunused-result on a call to a locally redeclared warn_unused_result function
  2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
  2021-04-08 15:53 ` [Bug c/99972] " msebor at gcc dot gnu.org
@ 2021-04-08 16:54 ` msebor at gcc dot gnu.org
  2021-04-08 22:17 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-08 16:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
The code (obviously) needs to be compiled as C to show the C bug (the C++ front
end is also buggy but differently; pr99974 tracks that):

$ gcc -S -Wall pr99972.c
pr99972.c: In function ‘gwur’:
pr99972.c:20:3: warning: ignoring return value of ‘fwur’ declared with
attribute ‘warn_unused_result’ [-Wunused-result]
   20 |   fwur ();   // -Wunused-result (good)
      |   ^~~~~~~

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

* [Bug c/99972] missing -Wunused-result on a call to a locally redeclared warn_unused_result function
  2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
  2021-04-08 15:53 ` [Bug c/99972] " msebor at gcc dot gnu.org
  2021-04-08 16:54 ` msebor at gcc dot gnu.org
@ 2021-04-08 22:17 ` msebor at gcc dot gnu.org
  2021-04-15 21:51 ` cvs-commit at gcc dot gnu.org
  2021-04-15 21:53 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-08 22:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |msebor at gcc dot gnu.org
   Last reconfirmed|                            |2021-04-08
           Keywords|                            |patch
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-April/567800.html

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

* [Bug c/99972] missing -Wunused-result on a call to a locally redeclared warn_unused_result function
  2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-04-08 22:17 ` msebor at gcc dot gnu.org
@ 2021-04-15 21:51 ` cvs-commit at gcc dot gnu.org
  2021-04-15 21:53 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-15 21:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:da879e01ecd35737c18be1da3324f4560aba1961

commit r11-8205-gda879e01ecd35737c18be1da3324f4560aba1961
Author: Martin Sebor <msebor@redhat.com>
Date:   Thu Apr 15 15:49:30 2021 -0600

    Propagate type attribute when merging extern declarations at local scope.

    Resolves:
    PR c/99420 - bogus -Warray-parameter on a function redeclaration in
function scope
    PR c/99972 - missing -Wunused-result on a call to a locally redeclared
warn_unused_result function

    gcc/c/ChangeLog:

            PR c/99420
            PR c/99972
            * c-decl.c (pushdecl): Always propagate type attribute.

    gcc/testsuite/ChangeLog:

            PR c/99420
            PR c/99972
            * gcc.dg/Warray-parameter-9.c: New test.
            * gcc.dg/Wnonnull-6.c: New test.
            * gcc.dg/Wreturn-type3.c: New test.
            * gcc.dg/Wunused-result.c: New test.
            * gcc.dg/attr-noreturn.c: New test.
            * gcc.dg/attr-returns-nonnull.c: New test.

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

* [Bug c/99972] missing -Wunused-result on a call to a locally redeclared warn_unused_result function
  2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-04-15 21:51 ` cvs-commit at gcc dot gnu.org
@ 2021-04-15 21:53 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-15 21:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99972

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.0
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Fix committed in r11-8205.

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

end of thread, other threads:[~2021-04-15 21:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-08 15:50 [Bug c/99972] New: missing -Wunused-result on a call to a locally redeclared warn_unused_result function msebor at gcc dot gnu.org
2021-04-08 15:53 ` [Bug c/99972] " msebor at gcc dot gnu.org
2021-04-08 16:54 ` msebor at gcc dot gnu.org
2021-04-08 22:17 ` msebor at gcc dot gnu.org
2021-04-15 21:51 ` cvs-commit at gcc dot gnu.org
2021-04-15 21:53 ` msebor at gcc dot gnu.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).