Bug 84695 - Missed opportunity to issue warning about override [-Winconsistent-missing-override]
Summary: Missed opportunity to issue warning about override [-Winconsistent-missing-ov...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2018-03-04 06:15 UTC by Ulrich Drepper
Modified: 2022-12-19 01:50 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-03-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ulrich Drepper 2018-03-04 06:15:58 UTC
Stroustrup in [1] ยง20.3.4 writes the compilers should issue warnings about inconsistent use of explicit override controls.

struct foo {
  virtual int one(int) { return 1; }
  virtual int two(int) { return 2; }
};
struct bar : foo {
  int one(int) override { return 101; }
  int two(int) { return 102; }
};
bar b;

In this case the compiler should issue a warning about bar::two.

This is different from -Wsuggest-override.  That command line option causes a warning to be issued but the fact that bar::one uses override should automatically turn on this type of warnings for the rest of the class.  I would even say there is no need for a further command line option, it should just been done.

[1] http://www.stroustrup.com/4th.html
Comment 1 Andrew Pinski 2022-01-07 03:56:55 UTC
Clang has -Winconsistent-missing-override which is turned on by default and provides the warning:

<source>:8:7: warning: 'two' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
  int two(int) { return 102; }
      ^
<source>:4:15: note: overridden virtual function is here
  virtual int two(int) { return 2; }
              ^
Comment 2 Eric Gallager 2022-02-04 08:03:08 UTC
adding suggested name for the warning to the title
Comment 3 David Binderman 2022-07-15 06:38:52 UTC
Interestingly, gcc trunk source code produces this warning when
compiled by clang:

$ fgrep Winco /tmp/0 | sort | uniq -c
      1 ../../trunk.git/gcc/analyzer/region-model.cc:3007:8: warning: 'visit_unaryop_svalue' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
      1 ../../trunk.git/gcc/analyzer/region-model.cc:3031:8: warning: 'visit_repeated_svalue' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
      2 ../../trunk.git/gcc/config/i386/i386-features.h:184:8: warning: 'convert_op' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
      2 ../../trunk.git/gcc/config/i386/i386-features.h:199:8: warning: 'convert_op' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
     11 ../../trunk.git/gcc/value-range-equiv.h:57:8: warning: 'set_undefined' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
     11 ../../trunk.git/gcc/value-range-equiv.h:58:8: warning: 'set_varying' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
     11 ../../trunk.git/gcc/value-range-equiv.h:68:8: warning: 'dump' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
$ 

A couple in the analyzer, a couple in the i386 machine specific code and 3
in value-range-equiv.h. 

Might be worth fixing ?