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..0316a7fcc5 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -1888,36 +1888,73 @@ public:
 
   void visit (const tdesc_type_with_fields *type) override
   {
+    bool need_field_type;
+
     if (!m_printed_type_with_fields)
       {
 	printf_unfiltered ("  tdesc_type_with_fields *type_with_fields;\n");
 	m_printed_type_with_fields = true;
       }
 
+    std::string text = visit_1 (type, &need_field_type);
+
     if (!type->fields.empty ()
-	&& !m_printed_field_type)
+	&& !m_printed_field_type
+	&& need_field_type)
       {
 	printf_unfiltered ("  tdesc_type *field_type;\n");
 	m_printed_field_type = true;
       }
 
+    printf_unfiltered ("%s", text.c_str ());
+
+    printf_unfiltered ("\n");
+  }
+
+  void visit (const tdesc_reg *reg) override
+  {
+    printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
+		       reg->name.c_str (), reg->target_regnum,
+		       reg->save_restore);
+    if (!reg->group.empty ())
+      printf_unfiltered ("\"%s\", ", reg->group.c_str ());
+    else
+      printf_unfiltered ("NULL, ");
+    printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
+  }
+
+protected:
+  std::string m_filename_after_features;
+
+private:
+  std::string visit_1 (const tdesc_type_with_fields *type,
+		       bool *need_field_type)
+  {
+    std::string ret;
+
+    *need_field_type = false;
+
     switch (type->kind)
       {
       case TDESC_TYPE_STRUCT:
       case TDESC_TYPE_FLAGS:
 	if (type->kind == TDESC_TYPE_STRUCT)
 	  {
-	    printf_unfiltered
-	      ("  type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
+	    string_appendf
+	      (ret,
+	       "  type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
 	       type->name.c_str ());
 	    if (type->size != 0)
-	      printf_unfiltered
-		("  tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
+	      string_appendf
+		(ret,
+		 "  tdesc_set_struct_size (type_with_fields, %d);\n",
+		 type->size);
 	  }
 	else
 	  {
-	    printf_unfiltered
-	      ("  type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
+	    string_appendf
+	      (ret,
+	       "  type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
 	       type->name.c_str (), type->size);
 	  }
 	for (const tdesc_type_field &f : type->fields)
@@ -1935,25 +1972,30 @@ public:
 		if (f.type->kind == TDESC_TYPE_BOOL)
 		  {
 		    gdb_assert (f.start == f.end);
-		    printf_unfiltered
-		      ("  tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
+		    string_appendf
+		      (ret,
+		       "  tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
 		       f.start, f.name.c_str ());
 		  }
 		else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
 			 || (type->size == 8
 			     && f.type->kind == TDESC_TYPE_UINT64))
 		  {
-		    printf_unfiltered
-		      ("  tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
+		    string_appendf
+		      (ret,
+		       "  tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
 		       f.name.c_str (), f.start, f.end);
 		  }
 		else
 		  {
-		    printf_unfiltered
-		      ("  field_type = tdesc_named_type (feature, \"%s\");\n",
+		    *need_field_type = true;
+		    string_appendf
+		      (ret,
+		       "  field_type = tdesc_named_type (feature, \"%s\");\n",
 		       type_name);
-		    printf_unfiltered
-		      ("  tdesc_add_typed_bitfield (type_with_fields, \"%s\","
+		    string_appendf
+		      (ret,
+		       "  tdesc_add_typed_bitfield (type_with_fields, \"%s\","
 		       " %d, %d, field_type);\n",
 		       f.name.c_str (), f.start, f.end);
 		  }
@@ -1962,62 +2004,53 @@ public:
 	      {
 		gdb_assert (f.end == -1);
 		gdb_assert (type->kind == TDESC_TYPE_STRUCT);
-		printf_unfiltered
-		  ("  field_type = tdesc_named_type (feature,"
+		*need_field_type = true;
+		string_appendf
+		  (ret, "  field_type = tdesc_named_type (feature,"
 		   " \"%s\");\n",
 		   type_name);
-		printf_unfiltered
-		  ("  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
+		string_appendf
+		  (ret,
+		   "  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
 		   f.name.c_str ());
 	      }
 	  }
 	break;
       case TDESC_TYPE_UNION:
-	printf_unfiltered
-	  ("  type_with_fields = tdesc_create_union (feature, \"%s\");\n",
+	string_appendf
+	  (ret,
+	   "  type_with_fields = tdesc_create_union (feature, \"%s\");\n",
 	   type->name.c_str ());
+	*need_field_type = true;
 	for (const tdesc_type_field &f : type->fields)
 	  {
-	    printf_unfiltered
-	      ("  field_type = tdesc_named_type (feature, \"%s\");\n",
+	    string_appendf
+	      (ret,
+	       "  field_type = tdesc_named_type (feature, \"%s\");\n",
 	       f.type->name.c_str ());
-	    printf_unfiltered
-	      ("  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
+	    string_appendf
+	      (ret,
+	       "  tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
 	       f.name.c_str ());
 	  }
 	break;
       case TDESC_TYPE_ENUM:
-	printf_unfiltered
-	  ("  type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
+	string_appendf
+	  (ret,
+	   "  type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
 	   type->name.c_str (), type->size);
 	for (const tdesc_type_field &f : type->fields)
-	  printf_unfiltered
-	    ("  tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
+	  string_appendf
+	    (ret,
+	     "  tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
 	     f.start, f.name.c_str ());
 	break;
       default:
 	error (_("C output is not supported type \"%s\"."), type->name.c_str ());
       }
-
-    printf_unfiltered ("\n");
-  }
-
-  void visit (const tdesc_reg *reg) override
-  {
-    printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
-		       reg->name.c_str (), reg->target_regnum,
-		       reg->save_restore);
-    if (!reg->group.empty ())
-      printf_unfiltered ("\"%s\", ", reg->group.c_str ());
-    else
-      printf_unfiltered ("NULL, ");
-    printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
+    return ret;
   }
 
-protected:
-  std::string m_filename_after_features;
-
-private:
   char *m_function;
 
   /* Did we print "struct tdesc_type *element_type;" yet?  */
