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

            Bug ID: 70951
           Summary: misleading -Wignored-qualifiers text, incorrect
                    documentation
           Product: gcc
           Version: 7.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 following C/C++ program elicits warnings in either language mode.  The
warning is expected, but the phrasing of the text isn't entirely correct
because, as the program shows, qualifiers on function return types can be
deduced in both languages and so are not ignored.  I think this can be easily
corrected by tweaking the text of the warning to avoid giving the impression
that the qualifiers are not part of the type.

In addition, the documentation for the option states:

Warn if the return type of a function has a type qualifier such as const.  For
ISO C such a type qualifier has no effect, since the value returned by a
function is not an lvalue.  For C++, the warning is only emitted for scalar
types or void.

I noticed two problems with the documentation:

First, as the C example program shows, a type qualifier on a function return
type does have an effect even in C.  The description should be expanded to make
it clear that the qualifier has no effect on the returned value but that it
does affect the type of the function.

Second, as the C++ example shows, G++ emits the warning also for enumerations
in addition to scalar types.  Either the C++ description should be corrected to
mention that the warning is issued for enumerations as well, or the
implementation fixed to match the documentation.

$ (set -x; cat xx.c && gcc -Wall -Wextra -xc xx.c && gcc -Wall -Wextra -xc++
xx.c)
+ cat xx.c

enum E { e };
typedef const enum E T;

T foo (void) { return (T)0; }

#if __cplusplus

template <class T>
void f (T);

int main () { f (foo); }

#else

typedef T (*tf)(void);
typedef enum E (*ef)(void);

_Static_assert (1 ==  _Generic (foo, tf: 1, ef: 0), "");

#endif
+ gcc -S -Wall -Wextra -xc xx.c
xx.c:4:3: warning: type qualifiers ignored on function return type
[-Wignored-qualifiers]
 T foo (void) { return (T)0; }
   ^~~
xx.c:15:13: warning: type qualifiers ignored on function return type
[-Wignored-qualifiers]
 typedef T (*tf)(void);
             ^~
+ gcc -Wall -Wextra -xc++ xx.c
xx.c:4:12: warning: type qualifiers ignored on function return type
[-Wignored-qualifiers]
 T foo (void) { return (T)0; }
            ^
/tmp/cccrycoh.o: In function `main':
xx.c:(.text+0x15): undefined reference to `void f<E const (*)()>(E const
(*)())'
collect2: error: ld returned 1 exit status

Reply via email to