Hi Jason, On 16 Jan 2025, at 22:49, Jason Merrill wrote:
> On 10/16/24 11:43 AM, Simon Martin wrote: >> As you know the patch had to be reverted due to PR117114, that >> highlighted a bunch of issues with comparing DECL_VINDEXes: it might >> give false positives in case of multiple inheritance (the case in >> PR117114), but also if there’s single inheritance by the hierarchy >> has >> more than two levels (another issue I found while bootstrapping with >> rust enabled). > > Yes, relying on DECL_VINDEX equality was wrong, sorry to mislead you. > >> The attached updated patch introduces an overrides_p function, based >> on >> the existing check_final_overrider, and uses it when the signatures >> match. > > That seems unnecessary. It seems like removing that only breaks > Woverloaded-virt11.C, and making that work again only requires > bringing back the check that DECL_VINDEX (fndecl) is set (to any > value). Or remembering that fndecl was a template, so it can't really > have the same signature as a non-template, whatever same_signature_p > says. That’s right, only Woverloaded-virt11.C fails without the check_final_overrider call. Thanks for the suggestion to check whether fndecl is a template. This is what the updated attached patch does, successfully tested on x86_64-pc-linux-gnu. OK for GCC 15? And if so, thoughts on backporting to release branches (technically it’s a regression but it’s “just” an incorrect warning fix, so probably not worth the risk)? Thanks, Simon
From 1d94d90e979720049f27025233a4ea6e036ae712 Mon Sep 17 00:00:00 2001 From: Simon Martin <si...@nasilyan.com> Date: Fri, 17 Jan 2025 10:37:33 +0100 Subject: [PATCH] c++: Fix overeager Woverloaded-virtual with conversion operators [PR109918] We currently emit an incorrect -Woverloaded-virtual warning upon the following test case === cut here === struct A { virtual operator int() { return 42; } virtual operator char() = 0; }; struct B : public A { operator char() { return 'A'; } }; === cut here === The problem is that when iterating over ovl_range (fns), warn_hidden gets confused by the conversion operator marker, concludes that seen_non_override is true and therefore emits a warning for all conversion operators in A that do not convert to char, even if -Woverloaded-virtual is 1 (e.g. with -Wall, the case reported). A second set of problems is highlighted when -Woverloaded-virtual is 2. First, with the same test case, since base_fndecls contains all conversion operators in A (except the one to char, that's been removed when iterating over ovl_range (fns)), we emit a spurious warning for the conversion operator to int, even though it's unrelated. Second, in case there are several conversion operators with different cv-qualifiers to the same type in A, we rightfully emit a warning, however the note uses the location of the conversion operator marker instead of the right one; location_of should go over conv_op_marker. This patch fixes all these by explicitly keeping track of (1) base methods that are overriden, as well as (2) base methods that are hidden but not overriden (and by what), and warning about methods that are in (2) but not (1). It also ignores non virtual base methods, per "definition" of -Woverloaded-virtual. Successfully tested on x86_64-pc-linux-gnu. Co-authored-by: Jason Merrill <ja...@redhat.com> PR c++/117114 PR c++/109918 gcc/cp/ChangeLog: * class.cc (warn_hidden): Keep track of overloaded and of hidden base methods. Mention the actual hiding function in the warning, not the first overload. * error.cc (location_of): Skip over conv_op_marker. gcc/testsuite/ChangeLog: * g++.dg/warn/Woverloaded-virt1.C: Check that no warning is emitted for non virtual base methods. * g++.dg/warn/Woverloaded-virt10.C: New test. * g++.dg/warn/Woverloaded-virt11.C: New test. * g++.dg/warn/Woverloaded-virt12.C: New test. * g++.dg/warn/Woverloaded-virt13.C: New test. * g++.dg/warn/Woverloaded-virt5.C: New test. * g++.dg/warn/Woverloaded-virt6.C: New test. * g++.dg/warn/Woverloaded-virt7.C: New test. * g++.dg/warn/Woverloaded-virt8.C: New test. * g++.dg/warn/Woverloaded-virt9.C: New test. --- gcc/cp/class.cc | 90 ++++++++++++------- gcc/cp/error.cc | 3 +- gcc/testsuite/g++.dg/warn/Woverloaded-virt1.C | 2 + .../g++.dg/warn/Woverloaded-virt10.C | 11 +++ .../g++.dg/warn/Woverloaded-virt11.C | 25 ++++++ .../g++.dg/warn/Woverloaded-virt12.C | 23 +++++ .../g++.dg/warn/Woverloaded-virt13.C | 28 ++++++ gcc/testsuite/g++.dg/warn/Woverloaded-virt5.C | 12 +++ gcc/testsuite/g++.dg/warn/Woverloaded-virt6.C | 12 +++ gcc/testsuite/g++.dg/warn/Woverloaded-virt7.C | 37 ++++++++ gcc/testsuite/g++.dg/warn/Woverloaded-virt8.C | 15 ++++ gcc/testsuite/g++.dg/warn/Woverloaded-virt9.C | 14 +++ 12 files changed, 237 insertions(+), 35 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt10.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt11.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt12.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt13.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt5.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt6.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt7.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt8.C create mode 100644 gcc/testsuite/g++.dg/warn/Woverloaded-virt9.C diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index f2f81a44718..73e2b798ad8 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -3243,10 +3243,15 @@ warn_hidden (tree t) continue; tree name = OVL_NAME (fns); + size_t num_fns = 0; /* The number of fndecls in fns. */ auto_vec<tree, 20> base_fndecls; tree base_binfo; tree binfo; unsigned j; + hash_set<tree> overriden_base_fndecls, overrider_fndecls; + /* base_fndecls that are hidden but not overriden. The "value" + contains a vector of fndecls that hide the "key". */ + hash_map<tree, auto_vec<tree> > hidden_base_fndecls; if (IDENTIFIER_CDTOR_P (name)) continue; @@ -3264,47 +3269,64 @@ warn_hidden (tree t) if (base_fndecls.is_empty ()) continue; - /* Remove any overridden functions. */ - bool seen_non_override = false; + /* Find all the base_fndecls that are overridden, as well as those + that are hidden, in T. */ for (tree fndecl : ovl_range (fns)) { - bool any_override = false; - if (TREE_CODE (fndecl) == FUNCTION_DECL - && DECL_VINDEX (fndecl)) + bool template_p = TREE_CODE (fndecl) == TEMPLATE_DECL; + fndecl = STRIP_TEMPLATE (fndecl); + if (TREE_CODE (fndecl) != FUNCTION_DECL + || fndecl == conv_op_marker) + continue; + num_fns++; + for (size_t k = 0; k < base_fndecls.length (); k++) { - /* If the method from the base class has the same - signature as the method from the derived class, it - has been overridden. Note that we can't move on - after finding one match: fndecl might override - multiple base fns. */ - for (size_t k = 0; k < base_fndecls.length (); k++) - if (base_fndecls[k] - && same_signature_p (fndecl, base_fndecls[k])) - { - base_fndecls[k] = NULL_TREE; - any_override = true; - } + if (!base_fndecls[k] || !DECL_VINDEX (base_fndecls[k])) + continue; + if (IDENTIFIER_CONV_OP_P (name) + && !same_type_p (DECL_CONV_FN_TYPE (fndecl), + DECL_CONV_FN_TYPE (base_fndecls[k]))) + /* If base_fndecl[k] and fndecl are conversion operators + to different types, they're unrelated. */ + ; + else if (!template_p /* Template methods don't override. */ + && same_signature_p (fndecl, base_fndecls[k])) + { + overriden_base_fndecls.add (base_fndecls[k]); + overrider_fndecls.add (fndecl); + } + else + { + /* fndecls hides base_fndecls[k]. */ + auto_vec<tree> &hiders = + hidden_base_fndecls.get_or_insert (base_fndecls[k]); + if (!hiders.contains (fndecl)) + hiders.safe_push (fndecl); + } } - if (!any_override) - seen_non_override = true; } - if (!seen_non_override && warn_overloaded_virtual == 1) - /* All the derived fns override base virtuals. */ - return; + if (warn_overloaded_virtual == 1 + && overrider_fndecls.elements () == num_fns) + /* All the fns override a base virtual. */ + continue; - /* Now give a warning for all base functions without overriders, - as they are hidden. */ - for (tree base_fndecl : base_fndecls) - if (base_fndecl) - { - auto_diagnostic_group d; - /* Here we know it is a hider, and no overrider exists. */ - if (warning_at (location_of (base_fndecl), - OPT_Woverloaded_virtual_, - "%qD was hidden", base_fndecl)) - inform (location_of (fns), " by %qD", fns); - } + /* Now give a warning for all hidden methods. Note that a method that + is both in hidden_base_fndecls and overriden_base_fndecls is not + hidden. */ + for (auto hidden_base_fndecl : hidden_base_fndecls) + { + tree hidden_fndecl = hidden_base_fndecl.first; + if (!hidden_fndecl + || overriden_base_fndecls.contains (hidden_fndecl)) + continue; + auto_diagnostic_group d; + if (warning_at (location_of (hidden_fndecl), + OPT_Woverloaded_virtual_, + "%qD was hidden", hidden_fndecl)) + for (auto h : hidden_base_fndecl.second) + inform (location_of (h), " by %qD", h); + } } } diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index 615ae0d1b65..c0665fe9dd1 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -3383,7 +3383,8 @@ location_of (tree t) return input_location; } else if (TREE_CODE (t) == OVERLOAD) - t = OVL_FIRST (t); + t = OVL_FIRST (t) != conv_op_marker ? OVL_FIRST (t) + : OVL_FIRST (OVL_CHAIN (t)); if (DECL_P (t)) return DECL_SOURCE_LOCATION (t); diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt1.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt1.C index 92f8327b9d0..9091bfabc96 100644 --- a/gcc/testsuite/g++.dg/warn/Woverloaded-virt1.C +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt1.C @@ -5,10 +5,12 @@ class Foo { public: virtual void f(int); // { dg-warning "hidden" } + void g(int); // Not virtual, so no warning }; class Bar : public Foo { public: virtual void f(short); // { dg-message "by" } + virtual void g(short); }; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt10.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt10.C new file mode 100644 index 00000000000..42b8b0f9788 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt10.C @@ -0,0 +1,11 @@ +// PR c++/117114 - Test case from PR c++/117114 +// { dg-do compile { target c++11 } } +// { dg-additional-options -Woverloaded-virtual } + +struct Troops { virtual ~Troops(); }; +struct Control { virtual int GetControl() const = 0; }; +struct Army : Troops, Control { int GetControl() const override; }; + +struct VirtualControl : virtual Control { + int GetControl() const override; +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt11.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt11.C new file mode 100644 index 00000000000..bd82ce90ea8 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt11.C @@ -0,0 +1,25 @@ +// PR c++/109918 - More tests with multiple inheritance +// { dg-do compile { target c++11 } } +// { dg-additional-options -Woverloaded-virtual } + +struct Troops { virtual ~Troops(); }; +struct Control { + virtual int GetControl() const { return 42; } // { dg-warning "was hidden" } +}; +struct Army : Troops, Control { + template<class T> void GetControl() const; // { dg-message "by" } +}; + + +struct A { + virtual void get() const; +}; +struct B { + virtual void get() const; + virtual void get(char) const; // { dg-warning "was hidden" } +}; + +struct C : A, B { + virtual void get() const; // { dg-message "by" } + virtual void get(int) const; // { dg-message "by" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt12.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt12.C new file mode 100644 index 00000000000..3cfe22c03ec --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt12.C @@ -0,0 +1,23 @@ +// PR c++/109918 - Test covariant return types +// { dg-do compile { target c++11 } } +// { dg-additional-options -Woverloaded-virtual } + +struct Big { virtual ~Big () {} }; +struct Smaller : Big {}; + +// Single inheritance case +struct Foo { + virtual Big* getMe() const; +}; +struct Bar : Foo { + virtual Smaller* getMe() const; +}; + +// Multiple inheritance case +struct Troops { virtual ~Troops(); }; +struct Control { + virtual Big* GetControl() const; +}; +struct Army : Troops, Control { + virtual Smaller* GetControl() const; +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt13.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt13.C new file mode 100644 index 00000000000..fb421e17dc3 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt13.C @@ -0,0 +1,28 @@ +// PR c++/117114 - Reduced version of another bootstrap error +// Unrelated methods can have the same DECL_VINDEX when the class hierarchy +// depth is 2 or more. +// { dg-do compile { target c++11 } } +// { dg-additional-options -Woverloaded-virtual } + +class HIRFullVisitor; +class HIRTypeVisitor; + +struct FullVisitable { + virtual void accept_vis (HIRFullVisitor &vis) = 0; +}; + +struct Node { + virtual ~Node() {} +}; + +struct Type : Node, FullVisitable +{ + using FullVisitable::accept_vis; + virtual void accept_vis (HIRTypeVisitor &vis) = 0; +}; + +struct TypePath : Type +{ + void accept_vis (HIRFullVisitor &vis) override; + void accept_vis (HIRTypeVisitor &vis) override; +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt5.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt5.C new file mode 100644 index 00000000000..01cd562609a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt5.C @@ -0,0 +1,12 @@ +// PR c++/109918 - Exact PR testcase +// { dg-do compile } +// { dg-additional-options -Wall } + +struct A { + virtual operator int() { return 42; } + virtual operator char() = 0; +}; + +struct B : public A { + operator char() { return 'A'; } +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt6.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt6.C new file mode 100644 index 00000000000..c18049e3a92 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt6.C @@ -0,0 +1,12 @@ +// PR c++/109918 - PR testcase with -Woverloaded-virtual=2 +// { dg-do compile } +// { dg-additional-options -Woverloaded-virtual=2 } + +struct A { + virtual operator int() { return 42; } + virtual operator char() = 0; +}; + +struct B : public A { + operator char() { return 'A'; } +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt7.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt7.C new file mode 100644 index 00000000000..4b56bb50f78 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt7.C @@ -0,0 +1,37 @@ +// PR c++/109918 - Test different CV-quals, usage of typedefs, and templates +// { dg-do compile } +// { dg-additional-options -Woverloaded-virtual } + +struct A { + virtual operator char() { return 'a'; } + virtual operator char() const { return 'b'; } // { dg-warning "was hidden" } + virtual operator int() { return 42; } +}; + +struct B : public A { + operator char() { return 'A'; } // { dg-note "by" } + operator int() { return 43; } +}; + +typedef char MyChar; +struct C : public A { + operator MyChar() { return 'A'; } // { dg-note "by" } + operator int() { return 43; } +}; + +struct D : public A { + template<class T> + operator T() { return T(); } +}; +int d = D(); + +struct AA { + virtual char func(char) { return 'a'; } + virtual char func(char) const { return 'b'; } // { dg-warning "was hidden" } + virtual int func(int) { return 42; } +}; + +struct BB : public AA { + char func(char) { return 'A'; } // { dg-note "by" } + int func(int) { return 43; } // { dg-note "by" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt8.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt8.C new file mode 100644 index 00000000000..51af2dae77c --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt8.C @@ -0,0 +1,15 @@ +// Identified when investigating PR c++/109918: no warning was emitted due to +// an incorrect early return in warn_hidden. +// { dg-additional-options -Wall } + +struct Foo +{ + virtual void f(int); // { dg-warning "hidden" } + virtual void g() {} +}; + +struct Bar : Foo +{ + virtual void f(short); // { dg-message "by" } + virtual void g() {} +}; diff --git a/gcc/testsuite/g++.dg/warn/Woverloaded-virt9.C b/gcc/testsuite/g++.dg/warn/Woverloaded-virt9.C new file mode 100644 index 00000000000..6d315c63a08 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Woverloaded-virt9.C @@ -0,0 +1,14 @@ +// PR c++/109918: Non virtual overloads in derived classes that don't override +// anything shouldn't cause warnings, even at -Woverloaded-virtual=2 +// { dg-additional-options -Woverloaded-virtual=2 } + +struct Foo +{ + virtual void g() {} +}; + +struct Bar : Foo +{ + virtual void g() {} + void g(int) {} +}; -- 2.44.0