[PATCH] Increase size of main_type::nfields

Tom Tromey tromey@adacore.com
Wed Jan 11 20:44:18 GMT 2023


main_type::nfields is a 'short', and has been for many years.  PR
c++/29985 points out that 'short' is too narrow for an enum that
contains more than 2^15 constants.

This patch bumps the size of 'nfields'.  To verify that the field
isn't directly used, it is also renamed.  Note that this does not
affect the size of main_type on x86-64 Fedora 36.  And, if it does
have a negative effect somewhere, it's worth considering that types
could be shrunk more drastically by using subclasses for the different
codes.

I wasn't sure whether a test case for this would be desirable.  It
would be a bit large.
---
 gdb/gdb-gdb.py.in | 4 ++--
 gdb/gdbtypes.h    | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gdb/gdb-gdb.py.in b/gdb/gdb-gdb.py.in
index dbc4d773e0b..95b7d84966f 100644
--- a/gdb/gdb-gdb.py.in
+++ b/gdb/gdb-gdb.py.in
@@ -261,8 +261,8 @@ class StructMainTypePrettyPrinter:
         fields.append("flags = [%s]" % self.flags_to_string())
         fields.append("owner = %s" % self.owner_to_string())
         fields.append("target_type = %s" % self.val["m_target_type"])
-        if self.val["nfields"] > 0:
-            for fieldno in range(self.val["nfields"]):
+        if self.val["m_nfields"] > 0:
+            for fieldno in range(self.val["m_nfields"]):
                 fields.append(self.struct_field_img(fieldno))
         if self.val["code"] == gdb.TYPE_CODE_RANGE:
             fields.append(self.bounds_img())
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index a9abb0d8071..da1d0f79d1f 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -836,7 +836,7 @@ struct main_type
   /* * Number of fields described for this type.  This field appears
      at this location because it packs nicely here.  */
 
-  short nfields;
+  int m_nfields;
 
   /* * Name of this type, or NULL if none.
 
@@ -964,13 +964,13 @@ struct type
   /* Get the number of fields of this type.  */
   int num_fields () const
   {
-    return this->main_type->nfields;
+    return this->main_type->m_nfields;
   }
 
   /* Set the number of fields of this type.  */
   void set_num_fields (int num_fields)
   {
-    this->main_type->nfields = num_fields;
+    this->main_type->m_nfields = num_fields;
   }
 
   /* Get the fields array of this type.  */
-- 
2.38.1



More information about the Gdb-patches mailing list