Bug 64431 - "-Wignored-qualifiers" warning gives misleading position in code
Summary: "-Wignored-qualifiers" warning gives misleading position in code
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, easyhack
Depends on:
Blocks:
 
Reported: 2014-12-28 22:02 UTC by Vladimir Rutsky
Modified: 2017-06-27 08:39 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-12-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Rutsky 2014-12-28 22:02:40 UTC
"type qualifiers ignored on function return type" warning doesn't point in "const" that is being ignored, but instead it points to function arguments or function qualifiers.

Consider following example:

$ cat ignored_qualifiers.cpp
struct C
{
    const int f1();
    const int f2(float);
    const int f3(char *, int);
    const int f4() volatile const;
    const int f5() const volatile;
};

const int f6();
const int f7(float);
const int f8(char *, int);


$ g++-4.9 -c -Wignored-qualifiers ignored_qualifiers.cpp
ignored_qualifiers.cpp:3:18: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f1();
                  ^
ignored_qualifiers.cpp:4:23: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f2(float);
                       ^
ignored_qualifiers.cpp:5:29: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f3(char *, int);
                             ^
ignored_qualifiers.cpp:6:29: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f4() volatile const;
                             ^
ignored_qualifiers.cpp:7:26: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f5() const volatile;
                          ^
ignored_qualifiers.cpp:10:14: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 const int f6();
              ^
ignored_qualifiers.cpp:11:19: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 const int f7(float);
                   ^
ignored_qualifiers.cpp:12:25: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
 const int f8(char *, int);
                         ^


Expected behavior: the warning should point on "const" in the return value of a funtion.
For example:


ignored_qualifiers.cpp:7:26: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     const int f5() const volatile;
     ^



$ g++-4.9 --version
g++-4.9 (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
Copyright (C) 2014 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.
Comment 1 Vladimir Rutsky 2014-12-28 22:04:47 UTC
$ g++-4.9 -v
Using built-in specs.
COLLECT_GCC=g++-4.9
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-0ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Ubuntu 4.9.2-0ubuntu1~14.04)
Comment 2 Manuel López-Ibáñez 2014-12-29 01:50:20 UTC
This is a problem also in C, however, in C++ it is trivial to fix:

Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c       (revision 218749)
+++ gcc/cp/decl.c       (working copy)
@@ -9686,11 +9686,11 @@ grokdeclarator (const cp_declarator *dec
               Make sure we have a valid type for the function to return.  */
 
            if (type_quals != TYPE_UNQUALIFIED)
              {
                if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
-                 warning (OPT_Wignored_qualifiers,
+                 warning_at (declspecs->locations[ds_const], OPT_Wignored_qualifiers,
                           "type qualifiers ignored on function return type");
                /* We now know that the TYPE_QUALS don't apply to the
                   decl, but to its return type.  */
                type_quals = TYPE_UNQUALIFIED;
              }

I would suggest to add a testcase with explicit column numbers.

I added this to the list of EasyHacks and I'll leave it to a new dev to fix it (see https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps if you want to be the one)
Comment 3 Eugene Zelenko 2015-01-31 01:10:38 UTC
Same problem exists in 4.8.2 and 4.8.3 and probably earlier versions.

Will be good idea to fix problem there too.

Eugene.
Comment 4 Paolo Carlini 2015-03-05 09:40:22 UTC
Mine.
Comment 5 Paolo Carlini 2015-03-05 17:51:19 UTC
Note that, besides the cases of volatile and restrict which should be also included, already have a patch including those, we need further tweaks to recover the location of 'const' in:

   int* const f9();
Comment 6 Paul Smith 2016-10-24 22:03:41 UTC
Still see this in GCC 6.2.0.  It was even more confusing for me because in my case the const in the return type was hidden in a typedef:

  class Foo
  {
      typedef const char* const MyType;
    ...

      MyType myFunction() const { ... }
  };

Shows this error:

  Foo.h:202:26: error: type qualifiers ignored on function return type
       MyType myFunction() const
                           ^~~~~

I was _really_ confused :).

I'm not even sure this should trigger this error... I mean, it's inside a type.  In my case I can take it out without a big problem but what if I wanted to have the const in the type?  Then I can't return that type anymore without a warning?  Maybe that's a separate bug.
Comment 7 Paolo Carlini 2016-10-25 08:35:01 UTC
Unfortunately, I'm not actively working on this.
Comment 8 Paolo Carlini 2017-06-27 08:39:59 UTC
The main issue is fixed for 7.1.0 in the straightforward way exploiting my smallest_type_quals_location. The location for Comment #5 is wrong, however.