[PATCH/RFA] Do not emit "field_type" var if not needed on "maint print c-tdesc"

Sergio Durigan Junior
2017-12-19T19:18:30Z

While fiddling a bit with -Wunused-variable, I noticed that "maint
print c-tdesc" was always generating code for the
"tdesc_type *field_type" variable, even when it wasn't used. This is
caught by GCC when using -Wunused-variable, of course. So I modified
the "print_c_tdesc" class to check whether "field_type" will be needed
or not.

In order to do the check, I basically copied the logic implemented on
"void visit (const tdesc_type_with_fields *type)", specifically the
"switch" part, and simplified it to determine which types need
"field_type". It's on a new simple function called
"need_field_type". Then, we can simply call this function when
deciding whether to print "field_type", and that's it.

I've also regenerated all the C files under gdb/features/, and as
expected only those that don't need "field_type" were modified.

yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>

* features/aarch64-core.c: Regenerate.
* features/arc-arcompact.c: Regenerate.
* features/arc-v2.c: Regenerate.
* features/i386/32bit-core.c: Regenerate.
* features/i386/64bit-core.c: Regenerate.
* features/i386/x32-core.c: Regenerate.
* features/or1k.c: Regenerate.
* target-descriptions.c (class print_c_tdesc)
<need_field_type>: New method.
---
gdb/features/aarch64-core.c | 1 -
gdb/features/arc-arcompact.c | 1 -
gdb/features/arc-v2.c | 1 -
gdb/features/i386/32bit-core.c | 1 -
gdb/features/i386/64bit-core.c | 1 -
gdb/features/i386/x32-core.c | 1 -
gdb/features/or1k.c | 1 -
gdb/target-descriptions.c | 41 ++++++++++++++++++++++++++++++++++++++++-
8 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/gdb/features/aarch64-core.c b/gdb/features/aarch64-core.c
index 618a7ef787..3707b7e055 100644
--- a/gdb/features/aarch64-core.c
+++ b/gdb/features/aarch64-core.c
@@ -10,7 +10,6 @@ create_feature_aarch64_core (struct target_desc *result, long regnum)

feature = tdesc_create_feature (result, "org.gnu.gdb.aarch64.core", "aarch64-core.xml");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "cpsr_flags", 4);
tdesc_add_flag (type_with_fields, 0, "SP");
tdesc_add_flag (type_with_fields, 1, "");
diff --git a/gdb/features/arc-arcompact.c b/gdb/features/arc-arcompact.c
index 79b6889172..f81f0a26ba 100644
--- a/gdb/features/arc-arcompact.c
+++ b/gdb/features/arc-arcompact.c
@@ -52,7 +52,6 @@ initialize_tdesc_arc_arcompact (void)

feature = tdesc_create_feature (result, "org.gnu.gdb.arc.aux-minimal");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "status32_type", 4);
tdesc_add_flag (type_with_fields, 0, "H");
tdesc_add_bitfield (type_with_fields, "E", 1, 2);
diff --git a/gdb/features/arc-v2.c b/gdb/features/arc-v2.c
index 9908b4c5ec..b2254b293c 100644
--- a/gdb/features/arc-v2.c
+++ b/gdb/features/arc-v2.c
@@ -52,7 +52,6 @@ initialize_tdesc_arc_v2 (void)

feature = tdesc_create_feature (result, "org.gnu.gdb.arc.aux-minimal");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "status32_type", 4);
tdesc_add_flag (type_with_fields, 0, "H");
tdesc_add_bitfield (type_with_fields, "E", 1, 4);
diff --git a/gdb/features/i386/32bit-core.c b/gdb/features/i386/32bit-core.c
index de2ce474d5..294e86d81e 100644
--- a/gdb/features/i386/32bit-core.c
+++ b/gdb/features/i386/32bit-core.c
@@ -10,7 +10,6 @@ create_feature_i386_32bit_core (struct target_desc *result, long regnum)

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "32bit-core.xml");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type_with_fields, 0, "CF");
tdesc_add_flag (type_with_fields, 1, "");
diff --git a/gdb/features/i386/64bit-core.c b/gdb/features/i386/64bit-core.c
index f4cad06e66..9e39ee42d9 100644
--- a/gdb/features/i386/64bit-core.c
+++ b/gdb/features/i386/64bit-core.c
@@ -10,7 +10,6 @@ create_feature_i386_64bit_core (struct target_desc *result, long regnum)

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "64bit-core.xml");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type_with_fields, 0, "CF");
tdesc_add_flag (type_with_fields, 1, "");
diff --git a/gdb/features/i386/x32-core.c b/gdb/features/i386/x32-core.c
index acafc7dace..c268e11bea 100644
--- a/gdb/features/i386/x32-core.c
+++ b/gdb/features/i386/x32-core.c
@@ -10,7 +10,6 @@ create_feature_i386_x32_core (struct target_desc *result, long regnum)

feature = tdesc_create_feature (result, "org.gnu.gdb.i386.core", "x32-core.xml");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "i386_eflags", 4);
tdesc_add_flag (type_with_fields, 0, "CF");
tdesc_add_flag (type_with_fields, 1, "");
diff --git a/gdb/features/or1k.c b/gdb/features/or1k.c
index 929a5f9208..9169cae940 100644
--- a/gdb/features/or1k.c
+++ b/gdb/features/or1k.c
@@ -16,7 +16,6 @@ initialize_tdesc_or1k (void)

feature = tdesc_create_feature (result, "org.gnu.gdb.or1k.group0");
tdesc_type_with_fields *type_with_fields;
- tdesc_type *field_type;
type_with_fields = tdesc_create_flags (feature, "sr_flags", 4);
tdesc_add_flag (type_with_fields, 0, "SM");
tdesc_add_flag (type_with_fields, 1, "TEE");
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 88ac55f404..fe4b5efbf5 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -1886,6 +1886,44 @@ public:
printf_unfiltered ("\n");
}

+ /* Some targets don't need the "tdesc_type *field_type" variable.
+ This function returns true if TYPE is going to need it, false
+ otherwise. */
+ bool need_field_type (const tdesc_type_with_fields *type)
+ {
+ switch (type->kind)
+ {
+ case TDESC_TYPE_UNION:
+ return true;
+
+ case TDESC_TYPE_ENUM:
+ return false;
+
+ case TDESC_TYPE_STRUCT:
+ case TDESC_TYPE_FLAGS:
+ for (const tdesc_type_field &f : type->fields)
+ {
+ if (f.start != -1)
+ {
+ if (f.type->kind == TDESC_TYPE_BOOL)
+ continue;
+ else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
+ || (type->size == 8
+ && f.type->kind == TDESC_TYPE_UINT64))
+ continue;
+ else
+ return true;
+ }
+ else /* Not a bitfield. */
+ return true;
+ }
+ break;
+ default:
+ error (_("C output is not supported type \"%s\"."), type->name.c_str ());
+ }
+ return false;
+ }
+
void visit (const tdesc_type_with_fields *type) override
{
if (!m_printed_type_with_fields)
@@ -1895,7 +1933,8 @@ public:
}

if (!type->fields.empty ()
- && !m_printed_field_type)
+ && !m_printed_field_type
+ && need_field_type (type))
{
printf_unfiltered (" tdesc_type *field_type;\n");
m_printed_field_type = true;
--
2.14.3