public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@adacore.com>
To: gdb-patches@sourceware.org
Subject: [commit/Ada 09/11] Extract string-printing out of ada_val_print_array
Date: Tue, 07 Jan 2014 04:22:00 -0000	[thread overview]
Message-ID: <1389068515-10129-10-git-send-email-brobecker@adacore.com> (raw)
In-Reply-To: <1389068515-10129-1-git-send-email-brobecker@adacore.com>

This patch creates a new function called "ada_val_print_string"
whose code is directly extracted out of ada_val_print_array.
The extracted code is then replaced by a call to this new function,
followed by a "return". The return avoids the need for an "else"
branch, with the associated block nesting. The latter is not really
terrible in this case, but it seems more readable this way.

gdb/ChangeLog:

        * ada-valprint.c (ada_val_print_string): New function,
        extracted from ada_val_print_array.
        (ada_val_print_array): Replace extracted code by call
        to ada_val_print_string followed by a return.  Move
        "else" branch to the function's top block.
---
 gdb/ChangeLog      |   8 ++++
 gdb/ada-valprint.c | 113 ++++++++++++++++++++++++++++++-----------------------
 2 files changed, 72 insertions(+), 49 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 004709b..e668617 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
 2014-01-07  Joel Brobecker  <brobecker@adacore.com>
 
+	* ada-valprint.c (ada_val_print_string): New function,
+	extracted from ada_val_print_array.
+	(ada_val_print_array): Replace extracted code by call
+	to ada_val_print_string followed by a return.  Move
+	"else" branch to the function's top block.
+
+2014-01-07  Joel Brobecker  <brobecker@adacore.com>
+
 	* ada-valprint.c (ada_val_print_array): Move implementation
 	down.  Rename parameter "offset" and "val" into "offset_aligned"
 	and "original_value" respectively.  Add parameter "offset".
diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index 43e1490..d1c8553 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -685,6 +685,54 @@ print_field_values (struct type *type, const gdb_byte *valaddr,
   return comma_needed;
 }
 
+/* Implement Ada val_print'ing for the case where TYPE is
+   a TYPE_CODE_ARRAY of characters.  */
+
+static void
+ada_val_print_string (struct type *type, const gdb_byte *valaddr,
+		      int offset, int offset_aligned, CORE_ADDR address,
+		      struct ui_file *stream, int recurse,
+		      const struct value *original_value,
+		      const struct value_print_options *options)
+{
+  enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
+  struct type *elttype = TYPE_TARGET_TYPE (type);
+  unsigned int eltlen;
+  unsigned int len;
+
+  /* We know that ELTTYPE cannot possibly be null, because we assume
+     that we're called only when TYPE is a string-like type.
+     Similarly, the size of ELTTYPE should also be non-null, since
+     it's a character-like type.  */
+  gdb_assert (elttype != NULL);
+  gdb_assert (TYPE_LENGTH (elttype) != 0);
+
+  eltlen = TYPE_LENGTH (elttype);
+  len = TYPE_LENGTH (type) / eltlen;
+
+  if (options->prettyformat_arrays)
+    print_spaces_filtered (2 + 2 * recurse, stream);
+
+  /* If requested, look for the first null char and only print
+     elements up to it.  */
+  if (options->stop_print_at_null)
+    {
+      int temp_len;
+
+      /* Look for a NULL char.  */
+      for (temp_len = 0;
+	   (temp_len < len
+	    && temp_len < options->print_max
+	    && char_at (valaddr + offset_aligned,
+			temp_len, eltlen, byte_order) != 0);
+	   temp_len += 1);
+      len = temp_len;
+    }
+
+  printstr (stream, elttype, valaddr + offset_aligned, len, 0,
+	    eltlen, options);
+}
+
 /* Implement Ada val_print-ing for GNAT arrays (Eg. fat pointers,
    thin pointers, etc).  */
 
@@ -943,60 +991,27 @@ ada_val_print_array (struct type *type, const gdb_byte *valaddr,
 		     const struct value *original_value,
 		     const struct value_print_options *options)
 {
-  /* For an array of chars, print with string syntax.  */
+  /* For an array of characters, print with string syntax.  */
   if (ada_is_string_type (type)
       && (options->format == 0 || options->format == 's'))
     {
-      enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
-      struct type *elttype = TYPE_TARGET_TYPE (type);
-      unsigned int eltlen;
-      unsigned int len;
-
-      /* We know that ELTTYPE cannot possibly be null, because we found
-	 that TYPE is a string-like type.  Similarly, the size of ELTTYPE
-	 should also be non-null, since it's a character-like type.  */
-      gdb_assert (elttype != NULL);
-      gdb_assert (TYPE_LENGTH (elttype) != 0);
-
-      eltlen = TYPE_LENGTH (elttype);
-      len = TYPE_LENGTH (type) / eltlen;
-
-      if (options->prettyformat_arrays)
-        print_spaces_filtered (2 + 2 * recurse, stream);
-
-      /* If requested, look for the first null char and only print
-         elements up to it.  */
-      if (options->stop_print_at_null)
-        {
-          int temp_len;
-
-          /* Look for a NULL char.  */
-          for (temp_len = 0;
-               (temp_len < len
-                && temp_len < options->print_max
-                && char_at (valaddr + offset_aligned,
-			    temp_len, eltlen, byte_order) != 0);
-               temp_len += 1);
-          len = temp_len;
-        }
-
-      printstr (stream, elttype, valaddr + offset_aligned, len, 0,
-		eltlen, options);
+      ada_val_print_string (type, valaddr, offset, offset_aligned,
+			    address, stream, recurse, original_value,
+			    options);
+      return;
     }
+
+  fprintf_filtered (stream, "(");
+  print_optional_low_bound (stream, type, options);
+  if (TYPE_FIELD_BITSIZE (type, 0) > 0)
+    val_print_packed_array_elements (type, valaddr, offset_aligned,
+				     0, stream, recurse,
+				     original_value, options);
   else
-    {
-      fprintf_filtered (stream, "(");
-      print_optional_low_bound (stream, type, options);
-      if (TYPE_FIELD_BITSIZE (type, 0) > 0)
-        val_print_packed_array_elements (type, valaddr, offset_aligned,
-					 0, stream, recurse,
-					 original_value, options);
-      else
-        val_print_array_elements (type, valaddr, offset_aligned, address,
-				  stream, recurse, original_value,
-				  options, 0);
-      fprintf_filtered (stream, ")");
-    }
+    val_print_array_elements (type, valaddr, offset_aligned, address,
+			      stream, recurse, original_value,
+			      options, 0);
+  fprintf_filtered (stream, ")");
 }
 
 /* Implement Ada val_print'ing for the case where TYPE is
-- 
1.8.3.2

  parent reply	other threads:[~2014-01-07  4:22 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-07  4:22 [commit/Ada] General ada-valprint improvements Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 05/11] Split ada_val_print_1 into smaller functions Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 06/11] ada-valprint.c: Inline print_record inside ada_val_print_struct_union Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 04/11] Remove call to gdb_flush at end of ada_val_print_1 Joel Brobecker
2014-01-07  4:22 ` Joel Brobecker [this message]
2014-01-07  4:22 ` [commit/Ada 01/11] ada-valprint.c: Reorder functions to reduce advance declarations Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 02/11] ada_val_print_1: Add language parameter Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 08/11] move ada_val_print_array down within other ada_val_print* functions Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 03/11] ada_val_print_1: Go through val_print instead of recursive call to self Joel Brobecker
2014-01-07  4:22 ` [commit/Ada 07/11] rewrite ada_val_print_ref to reduce if/else block nesting depth Joel Brobecker
2014-01-07  4:23 ` [commit/Ada 11/11] Ada: Fix missing call to pretty-printer for fields of records Joel Brobecker
2014-01-07  4:23 ` [commit/Ada 10/11] ada_print_floating: Remove use of statically sized buffer Joel Brobecker
2014-01-07 17:29 ` [commit/Ada] General ada-valprint improvements Tom Tromey
2014-01-08 11:35   ` Joel Brobecker
2014-01-08 15:41     ` Tom Tromey
2014-01-09  3:06       ` Joel Brobecker
2014-01-09 16:14         ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1389068515-10129-10-git-send-email-brobecker@adacore.com \
    --to=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).