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

            Bug ID: 124340
           Summary: Review of missing virtual destructors in multiple base
                    classes
           Product: gcc
           Version: 14.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jiangxuezhi2 at huawei dot com
  Target Milestone: ---

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.

For example:

1. operand_compare (gcc/fold-const.h)
This class contains virtual functions (operand_equal_p and hash_operand) but
lacks a virtual destructor:

~~~c
class operand_compare
{
public:
  /* Return true if two operands are equal.  The flags fields can be used
     to specify OEP flags described in tree-core.h.  */
  virtual bool operand_equal_p (const_tree, const_tree, unsigned int flags);

  /* Generate a hash value for an expression.  This can be used iteratively
     by passing a previous result as the HSTATE argument.  */
  virtual void hash_operand (const_tree, inchash::hash &, unsigned flags);

protected:
  /* Verify that when arguments (ARG0 and ARG1) are equal, then they have
     an equal hash value.  When the function knowns comparison return,
     true is returned.  Then RET is set to corresponding comparsion result.  */
  bool verify_hash_value (const_tree arg0, const_tree arg1, unsigned int flags,
                          bool *ret);
};

~~~
gcc/emit-rtl.h

~~~c
struct address_reload_context
{
  /* Can be overriden by derived classes.  */
  virtual rtx get_reload_reg () const { return gen_reg_rtx (Pmode); }

  /* Emit insns to reload VALUE into a new register.  VALUE is an
     auto-increment or auto-decrement RTX whose operand is a register or
     memory location; so reloading involves incrementing that location.

     AMOUNT is the number to increment or decrement by (always
     positive and ignored for POST_MODIFY/PRE_MODIFY).

     Return a pseudo containing the result.  */
  rtx emit_autoinc (rtx value, poly_int64 amount);
};

~~~
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. However, I recognize there may be valid
reasons for the current design.

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

Thanks for your reply!

Reply via email to