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

            Bug ID: 81565
           Summary: confusion between function and type attributes
           Product: gcc
           Version: 8.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: ---

Attributes noreturn and warn_unused_result are both documented to apply to
functions and are not documented to apply to types.  However, as the following
test case shows, while attribute noreturn is, in fact, a part of a function
declaration only and not its type, attribute warn_unused_result is also part of
the function's type and transfers to function pointers along with it.  This
subtlety leads to confusion, surprising results, and bug reports about missing
or bogus warnings.

$ cat x.c && gcc -O2 -S -Wall -Wextra -Wpedantic x.c
int __attribute__ ((noreturn)) f0 (void);

int g0 (void)
{
  __typeof__ (f0) *pf = f0;

  pf ();

}  // -Wreturn-type implies "noreturn" isn't part of f0's type.

int __attribute__ ((warn_unused_result)) f1 (void);

int f2 (void);

void g1 (void)
{
  __typeof__ (f1) *pf = f1;

  pf ();   // -Wunused-result implies "warn_unused_result" is part of f1's type

  pf = f2;

  pf ();   // bogus -Wunused-result
}

x.c: In function ‘g0’:
x.c:9:1: warning: control reaches end of non-void function [-Wreturn-type]
 }  // -Wreturn-type implies "noreturn" isn't part of f0's type.
 ^
x.c: In function ‘g1’:
x.c:19:3: warning: ignoring return value of function declared with attribute
warn_unused_result [-Wunused-result]
   pf ();   // -Wunused-result implies "warn_unused_result" is part of f1's
type
   ^~~~~
x.c:23:3: warning: ignoring return value of function declared with attribute
warn_unused_result [-Wunused-result]
   pf ();   // bogus -Wunused-result
   ^~~~~

Reply via email to