[1/7] Use .def file to stringify type codes

Message ID 20230921-field-bits-v1-1-201285360900@adacore.com
State New
Headers
Series Remove char-based bitfield macros |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Tom Tromey Sept. 21, 2023, 6:01 p.m. UTC
  This changes recursive_dump_type to reuse the type-codes.def file when
stringifying type codes.
---
 gdb/gdbtypes.c | 99 +++++++++++-----------------------------------------------
 1 file changed, 18 insertions(+), 81 deletions(-)
  

Comments

Lancelot SIX Sept. 25, 2023, 9:58 p.m. UTC | #1
Hi Tom,

On Thu, Sep 21, 2023 at 12:01:28PM -0600, Tom Tromey via Gdb-patches wrote:
> This changes recursive_dump_type to reuse the type-codes.def file when
> stringifying type codes.
> ---
>  gdb/gdbtypes.c | 99 +++++++++++-----------------------------------------------
>  1 file changed, 18 insertions(+), 81 deletions(-)
> 
> diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
> index 7011fddd695..18aa8e5c29e 100644
> --- a/gdb/gdbtypes.c
> +++ b/gdb/gdbtypes.c
> @@ -5099,6 +5099,23 @@ dump_dynamic_prop (dynamic_prop const& prop)
>      }
>  }
>  
> +/* Return a string that represents a type code.  */
> +static const char *
> +type_code_name (type_code code)
> +{
> +  switch (code)
> +    {
> +#define OP(X) case X: return # X;
> +#include "type-codes.def"
> +#undef OP
> +
> +    case TYPE_CODE_UNDEF:
> +      return "TYPE_CODE_UNDEF";
> +    default:
> +      return "UNKNOWN TYPE CODE";

Is this default "just" to make the compiler happy?  I don't expect it
should ever executed.  If so, I usually prefer the following construct:

    static const char *
    foo (some_enume e)
    {
      switch (e)
        {
        case A:
          return "A";
        case B:
          return "B";
        [...]
        /* No "default:".  */
        }
      return "unknown";
    }

The return after the switch block avoids the "control reaches end of
non-void function" error, but since there is no "default:" the compiler
can warn if some enumerator is not covered by the switch.

That being said, given how type_code is constructed, the
"#include type-codes.def" make it really unlikely  that type_code_name
can evolve without having this function evolve as well.

Best,
Lancelot.

> +    }
> +}
> +
>  void
>  recursive_dump_type (struct type *type, int spaces)
>  {
> @@ -5136,87 +5153,7 @@ recursive_dump_type (struct type *type, int spaces)
>  	      type->name () ? type->name () : "<NULL>",
>  	      host_address_to_string (type->name ()));
>    gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
> -  switch (type->code ())
> -    {
> -    case TYPE_CODE_UNDEF:
> -      gdb_printf ("(TYPE_CODE_UNDEF)");
> -      break;
> -    case TYPE_CODE_PTR:
> -      gdb_printf ("(TYPE_CODE_PTR)");
> -      break;
> -    case TYPE_CODE_ARRAY:
> -      gdb_printf ("(TYPE_CODE_ARRAY)");
> -      break;
> -    case TYPE_CODE_STRUCT:
> -      gdb_printf ("(TYPE_CODE_STRUCT)");
> -      break;
> -    case TYPE_CODE_UNION:
> -      gdb_printf ("(TYPE_CODE_UNION)");
> -      break;
> -    case TYPE_CODE_ENUM:
> -      gdb_printf ("(TYPE_CODE_ENUM)");
> -      break;
> -    case TYPE_CODE_FLAGS:
> -      gdb_printf ("(TYPE_CODE_FLAGS)");
> -      break;
> -    case TYPE_CODE_FUNC:
> -      gdb_printf ("(TYPE_CODE_FUNC)");
> -      break;
> -    case TYPE_CODE_INT:
> -      gdb_printf ("(TYPE_CODE_INT)");
> -      break;
> -    case TYPE_CODE_FLT:
> -      gdb_printf ("(TYPE_CODE_FLT)");
> -      break;
> -    case TYPE_CODE_VOID:
> -      gdb_printf ("(TYPE_CODE_VOID)");
> -      break;
> -    case TYPE_CODE_SET:
> -      gdb_printf ("(TYPE_CODE_SET)");
> -      break;
> -    case TYPE_CODE_RANGE:
> -      gdb_printf ("(TYPE_CODE_RANGE)");
> -      break;
> -    case TYPE_CODE_STRING:
> -      gdb_printf ("(TYPE_CODE_STRING)");
> -      break;
> -    case TYPE_CODE_ERROR:
> -      gdb_printf ("(TYPE_CODE_ERROR)");
> -      break;
> -    case TYPE_CODE_MEMBERPTR:
> -      gdb_printf ("(TYPE_CODE_MEMBERPTR)");
> -      break;
> -    case TYPE_CODE_METHODPTR:
> -      gdb_printf ("(TYPE_CODE_METHODPTR)");
> -      break;
> -    case TYPE_CODE_METHOD:
> -      gdb_printf ("(TYPE_CODE_METHOD)");
> -      break;
> -    case TYPE_CODE_REF:
> -      gdb_printf ("(TYPE_CODE_REF)");
> -      break;
> -    case TYPE_CODE_CHAR:
> -      gdb_printf ("(TYPE_CODE_CHAR)");
> -      break;
> -    case TYPE_CODE_BOOL:
> -      gdb_printf ("(TYPE_CODE_BOOL)");
> -      break;
> -    case TYPE_CODE_COMPLEX:
> -      gdb_printf ("(TYPE_CODE_COMPLEX)");
> -      break;
> -    case TYPE_CODE_TYPEDEF:
> -      gdb_printf ("(TYPE_CODE_TYPEDEF)");
> -      break;
> -    case TYPE_CODE_NAMESPACE:
> -      gdb_printf ("(TYPE_CODE_NAMESPACE)");
> -      break;
> -    case TYPE_CODE_FIXED_POINT:
> -      gdb_printf ("(TYPE_CODE_FIXED_POINT)");
> -      break;
> -    default:
> -      gdb_printf ("(UNKNOWN TYPE CODE)");
> -      break;
> -    }
> +  gdb_printf ("(%s)", type_code_name (type->code ()));
>    gdb_puts ("\n");
>    gdb_printf ("%*slength %s\n", spaces, "",
>  	      pulongest (type->length ()));
> 
> -- 
> 2.40.1
>
  
Tom Tromey Sept. 26, 2023, 1:10 p.m. UTC | #2
>>>>> "Lancelot" == Lancelot SIX <lsix@lancelotsix.com> writes:

>> +    default:
>> +      return "UNKNOWN TYPE CODE";

Lancelot> Is this default "just" to make the compiler happy?  I don't expect it
Lancelot> should ever executed.  If so, I usually prefer the following construct:

It's just to avoid any semantic change.
I doubt it can happen -- it certainly isn't supposed to happen.

Lancelot> The return after the switch block avoids the "control reaches end of
Lancelot> non-void function" error, but since there is no "default:" the compiler
Lancelot> can warn if some enumerator is not covered by the switch.

I'll do it.

Tom
  

Patch

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 7011fddd695..18aa8e5c29e 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5099,6 +5099,23 @@  dump_dynamic_prop (dynamic_prop const& prop)
     }
 }
 
+/* Return a string that represents a type code.  */
+static const char *
+type_code_name (type_code code)
+{
+  switch (code)
+    {
+#define OP(X) case X: return # X;
+#include "type-codes.def"
+#undef OP
+
+    case TYPE_CODE_UNDEF:
+      return "TYPE_CODE_UNDEF";
+    default:
+      return "UNKNOWN TYPE CODE";
+    }
+}
+
 void
 recursive_dump_type (struct type *type, int spaces)
 {
@@ -5136,87 +5153,7 @@  recursive_dump_type (struct type *type, int spaces)
 	      type->name () ? type->name () : "<NULL>",
 	      host_address_to_string (type->name ()));
   gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
-  switch (type->code ())
-    {
-    case TYPE_CODE_UNDEF:
-      gdb_printf ("(TYPE_CODE_UNDEF)");
-      break;
-    case TYPE_CODE_PTR:
-      gdb_printf ("(TYPE_CODE_PTR)");
-      break;
-    case TYPE_CODE_ARRAY:
-      gdb_printf ("(TYPE_CODE_ARRAY)");
-      break;
-    case TYPE_CODE_STRUCT:
-      gdb_printf ("(TYPE_CODE_STRUCT)");
-      break;
-    case TYPE_CODE_UNION:
-      gdb_printf ("(TYPE_CODE_UNION)");
-      break;
-    case TYPE_CODE_ENUM:
-      gdb_printf ("(TYPE_CODE_ENUM)");
-      break;
-    case TYPE_CODE_FLAGS:
-      gdb_printf ("(TYPE_CODE_FLAGS)");
-      break;
-    case TYPE_CODE_FUNC:
-      gdb_printf ("(TYPE_CODE_FUNC)");
-      break;
-    case TYPE_CODE_INT:
-      gdb_printf ("(TYPE_CODE_INT)");
-      break;
-    case TYPE_CODE_FLT:
-      gdb_printf ("(TYPE_CODE_FLT)");
-      break;
-    case TYPE_CODE_VOID:
-      gdb_printf ("(TYPE_CODE_VOID)");
-      break;
-    case TYPE_CODE_SET:
-      gdb_printf ("(TYPE_CODE_SET)");
-      break;
-    case TYPE_CODE_RANGE:
-      gdb_printf ("(TYPE_CODE_RANGE)");
-      break;
-    case TYPE_CODE_STRING:
-      gdb_printf ("(TYPE_CODE_STRING)");
-      break;
-    case TYPE_CODE_ERROR:
-      gdb_printf ("(TYPE_CODE_ERROR)");
-      break;
-    case TYPE_CODE_MEMBERPTR:
-      gdb_printf ("(TYPE_CODE_MEMBERPTR)");
-      break;
-    case TYPE_CODE_METHODPTR:
-      gdb_printf ("(TYPE_CODE_METHODPTR)");
-      break;
-    case TYPE_CODE_METHOD:
-      gdb_printf ("(TYPE_CODE_METHOD)");
-      break;
-    case TYPE_CODE_REF:
-      gdb_printf ("(TYPE_CODE_REF)");
-      break;
-    case TYPE_CODE_CHAR:
-      gdb_printf ("(TYPE_CODE_CHAR)");
-      break;
-    case TYPE_CODE_BOOL:
-      gdb_printf ("(TYPE_CODE_BOOL)");
-      break;
-    case TYPE_CODE_COMPLEX:
-      gdb_printf ("(TYPE_CODE_COMPLEX)");
-      break;
-    case TYPE_CODE_TYPEDEF:
-      gdb_printf ("(TYPE_CODE_TYPEDEF)");
-      break;
-    case TYPE_CODE_NAMESPACE:
-      gdb_printf ("(TYPE_CODE_NAMESPACE)");
-      break;
-    case TYPE_CODE_FIXED_POINT:
-      gdb_printf ("(TYPE_CODE_FIXED_POINT)");
-      break;
-    default:
-      gdb_printf ("(UNKNOWN TYPE CODE)");
-      break;
-    }
+  gdb_printf ("(%s)", type_code_name (type->code ()));
   gdb_puts ("\n");
   gdb_printf ("%*slength %s\n", spaces, "",
 	      pulongest (type->length ()));