https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124340
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jiang XueZhi from comment #0)
> I would like to respectfully bring to your attention a pattern I’ve observed
> while studying the GCC codebase. Several classes designed as base classes
> appear to be missing virtual destructors, which may trigger
> -Wnon-virtual-dtor warnings and could potentially lead to incomplete
> destruction when these classes are used polymorphically.
That warning is broken by design, don't use it.
As documented, you should prefer -Wdelete-non-virtual-dtor which is included in
-Wall.
> class operand_compare
This is used as a base class of ao_compare which is a base class of
func_checker, which gets dynamically allocated and *does* have a virtual
destructor.
There is a global object of the base class type:
static operand_compare default_compare_instance;
No objects of type operand_compare or ao_compare get deleted.
> struct address_reload_context
This is only ever created in two places, both times on the stack:
In gcc/emit-rtl.cc:
addr = address_reload_context ().emit_autoinc (addr, size);
In gcc/lra-constraints.cc:
lra_autoinc_reload_context context (GET_MODE (value), new_rclass);
> ~~~
> I’ve noticed this pattern appears in multiple places throughout the
> codebase. Based on C++ best practices, classes with virtual functions that
> are intended as base classes should typically have virtual destructors to
> ensure proper cleanup of derived class resources.
It's only necessary if you destroy them with delete, and that's what
-Wdelete-non-virtual-dtor will warn you about.
> I would appreciate guidance on the preferred approach for handling these
> cases:
>
> 1.Adding virtual destructors where appropriate
>
> 2.Using pragma directives to suppress warnings
Or
3. Add protected destructors, so the base class cannot be destroyed with
delete.
4. Don't use -Wnon-virtual-dtor in a codebase you don't control.
It's OK to enable that warning if you really want to find all classes in your
own codebase and decide whether to add a virtual dtor, but it's not appropriate
to just turn that warning on routinely. It gives false positives.
-Wdelete-non-virtual-dtor is the option that should be used routinely, that's
why I added it, and that's why it's in -Wall.
I don't think any change to address_reload_context is needed.