]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix array pretty formatter
authorHannes Domani <ssbssa@yahoo.de>
Sun, 26 Apr 2020 13:28:46 +0000 (15:28 +0200)
committerHannes Domani <ssbssa@yahoo.de>
Wed, 29 Apr 2020 10:57:25 +0000 (12:57 +0200)
Currently, printing with array pretty formatting makes the output actually
less readable than without:

(gdb) p -array on -- {{1,2,3},{4,5,6}}
$1 =   {    {1,
    2,
    3},
      {4,
    5,
    6}}
(gdb) p -array on -array-indexes on -- {{1,2,3},{4,5,6}}
$2 =   {[0] =     {[0] = 1,
    [1] = 2,
    [2] = 3},
  [1] =     {[0] = 4,
    [1] = 5,
    [2] = 6}}

These changes now also put the first element and the array end bracket on a new
line, similar to the structure pretty formatter:

(gdb) p -array on -- {{1,2,3},{4,5,6}}
$1 = {
  {
    1,
    2,
    3
  },
  {
    4,
    5,
    6
  }
}
(gdb) p -array on -array-indexes on -- {{1,2,3},{4,5,6}}
$2 = {
  [0] = {
    [0] = 1,
    [1] = 2,
    [2] = 3
  },
  [1] = {
    [0] = 4,
    [1] = 5,
    [2] = 6
  }
}

gdb/ChangeLog:

2020-04-29  Hannes Domani  <ssbssa@yahoo.de>

PR gdb/17320
* ada-valprint.c (val_print_packed_array_elements): Move array
end bracket to new line.
(ada_val_print_string): Remove extra spaces before first array
element.
* c-valprint.c (c_value_print_array): Likewise.
* m2-valprint.c (m2_print_array_contents): Likewise.
(m2_value_print_inner): Likewise.
* p-valprint.c (pascal_value_print_inner): Likewise.
* valprint.c (generic_val_print_array): Likewise.
(value_print_array_elements): Move first array element and array
end bracket to new line.

gdb/testsuite/ChangeLog:

2020-04-29  Hannes Domani  <ssbssa@yahoo.de>

PR gdb/17320
* gdb.base/pretty-array.c: New test.
* gdb.base/pretty-array.exp: New file.

gdb/ChangeLog
gdb/ada-valprint.c
gdb/c-valprint.c
gdb/m2-valprint.c
gdb/p-valprint.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/pretty-array.c [new file with mode: 0644]
gdb/testsuite/gdb.base/pretty-array.exp [new file with mode: 0644]
gdb/valprint.c

index ef605655999aa59382486a58eb2482086c2b66d5..9e1ce39c9e2492ea38dabf5b583dae85c7905eee 100644 (file)
@@ -1,3 +1,18 @@
+2020-04-29  Hannes Domani  <ssbssa@yahoo.de>
+
+       PR gdb/17320
+       * ada-valprint.c (val_print_packed_array_elements): Move array
+       end bracket to new line.
+       (ada_val_print_string): Remove extra spaces before first array
+       element.
+       * c-valprint.c (c_value_print_array): Likewise.
+       * m2-valprint.c (m2_print_array_contents): Likewise.
+       (m2_value_print_inner): Likewise.
+       * p-valprint.c (pascal_value_print_inner): Likewise.
+       * valprint.c (generic_val_print_array): Likewise.
+       (value_print_array_elements): Move first array element and array
+       end bracket to new line.
+
 2020-04-29  Tom de Vries  <tdevries@suse.de>
 
        PR symtab/25889
index 474b07999104e5e55adb84e1478c954bce002c10..31f3a50b340aa36f510de9adbfbf524942647444 100644 (file)
@@ -195,6 +195,11 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
              fprintf_filtered (stream, ", ");
            }
        }
+      else if (options->prettyformat_arrays)
+       {
+         fprintf_filtered (stream, "\n");
+         print_spaces_filtered (2 + 2 * recurse, stream);
+       }
       wrap_here (n_spaces (2 + 2 * recurse));
       maybe_print_array_index (index_type, i + low, stream, options);
 
@@ -707,9 +712,6 @@ ada_val_print_string (struct type *type, const gdb_byte *valaddr,
   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)
index bde9c6cc88330dbf7a04d19e5c8301d02e020939..52ea5eda0c1de445004233bfa0d1faac12158a59 100644 (file)
@@ -252,10 +252,6 @@ c_value_print_array (struct value *val,
 
       eltlen = TYPE_LENGTH (elttype);
       len = high_bound - low_bound + 1;
-      if (options->prettyformat_arrays)
-       {
-         print_spaces_filtered (2 + 2 * recurse, stream);
-       }
 
       /* Print arrays of textual chars with a string syntax, as
         long as the entire array is valid.  */
index 844a63f3bd69a40aa8e015ca4d4e43f0efa672a2..e210b5ec2f1821bebbb65efc668aa4a2c398f614 100644 (file)
@@ -265,8 +265,6 @@ m2_print_array_contents (struct value *val,
 
   if (TYPE_LENGTH (type) > 0)
     {
-      if (options->prettyformat_arrays)
-       print_spaces_filtered (2 + 2 * recurse, stream);
       /* For an array of chars, print with string syntax.  */
       if (TYPE_LENGTH (type) == 1 &&
          ((TYPE_CODE (type) == TYPE_CODE_INT)
@@ -318,8 +316,6 @@ m2_value_print_inner (struct value *val, struct ui_file *stream, int recurse,
        {
          elttype = check_typedef (TYPE_TARGET_TYPE (type));
          len = TYPE_LENGTH (type) / TYPE_LENGTH (elttype);
-         if (options->prettyformat_arrays)
-           print_spaces_filtered (2 + 2 * recurse, stream);
          /* For an array of chars, print with string syntax.  */
          if (TYPE_LENGTH (elttype) == 1 &&
              ((TYPE_CODE (elttype) == TYPE_CODE_INT)
index cbd7fb75e242c237aefc4919fa71751417d39d3d..fbf5c5cf1412e94661e4d76cae8b173d6476df0b 100644 (file)
@@ -93,10 +93,6 @@ pascal_value_print_inner (struct value *val, struct ui_file *stream,
            len = high_bound - low_bound + 1;
            elttype = check_typedef (TYPE_TARGET_TYPE (type));
            eltlen = TYPE_LENGTH (elttype);
-           if (options->prettyformat_arrays)
-             {
-               print_spaces_filtered (2 + 2 * recurse, stream);
-             }
            /* If 's' format is used, try to print out as string.
               If no format is given, print as string if element type
               is of TYPE_CODE_CHAR and element size is 1,2 or 4.  */
index 761fe30535eaffcc7e6f63d1ca47ba0448e156d0..86b71913b0111ae094b100c5f2d63864c3816e64 100644 (file)
@@ -1,3 +1,9 @@
+2020-04-29  Hannes Domani  <ssbssa@yahoo.de>
+
+       PR gdb/17320
+       * gdb.base/pretty-array.c: New test.
+       * gdb.base/pretty-array.exp: New file.
+
 2020-04-29  Tom de Vries  <tdevries@suse.de>
 
        PR symtab/25889
diff --git a/gdb/testsuite/gdb.base/pretty-array.c b/gdb/testsuite/gdb.base/pretty-array.c
new file mode 100644 (file)
index 0000000..2adebcc
--- /dev/null
@@ -0,0 +1,24 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2020 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int nums[2][3] = {{11, 12, 13}, {21, 22, 23}};
+
+int
+main ()
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/pretty-array.exp b/gdb/testsuite/gdb.base/pretty-array.exp
new file mode 100644 (file)
index 0000000..e17ce18
--- /dev/null
@@ -0,0 +1,65 @@
+# Copyright 2020 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test pretty printing of arrays.
+
+standard_testfile
+
+if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} {
+    untested $testfile.exp
+    return -1
+}
+
+if ![runto_main] {
+    untested $testfile.exp
+    return -1
+}
+
+gdb_test "print nums" \
+    "= \\{\\{11, 12, 13\\}, \\{21, 22, 23\\}\\}"
+
+gdb_test_no_output "set print array on"
+
+gdb_test "print nums" \
+    [multi_line \
+        " = {" \
+        "  {" \
+        "    11," \
+        "    12," \
+        "    13" \
+        "  }," \
+        "  {" \
+        "    21," \
+        "    22," \
+        "    23" \
+        "  }" \
+        "}" ]
+
+gdb_test_no_output "set print array-indexes on"
+
+gdb_test "print nums" \
+    [multi_line \
+        " = {" \
+        "  \\\[0\\\] = {" \
+        "    \\\[0\\\] = 11," \
+        "    \\\[1\\\] = 12," \
+        "    \\\[2\\\] = 13" \
+        "  }," \
+        "  \\\[1\\\] = {" \
+        "    \\\[0\\\] = 21," \
+        "    \\\[1\\\] = 22," \
+        "    \\\[2\\\] = 23" \
+        "  }" \
+        "}" ]
index 0be7c6071b7efad74600f93ffc7f6e99ec29dab8..2f910242fcc0cb4e44ab93f25201ac307d756986 100644 (file)
@@ -441,11 +441,6 @@ generic_val_print_array (struct value *val,
       if (!get_array_bounds (type, &low_bound, &high_bound))
        error (_("Could not determine the array high bound"));
 
-      if (options->prettyformat_arrays)
-       {
-         print_spaces_filtered (2 + 2 * recurse, stream);
-       }
-
       fputs_filtered (decorations->array_start, stream);
       value_print_array_elements (val, stream, recurse, options, 0);
       fputs_filtered (decorations->array_end, stream);
@@ -1945,6 +1940,11 @@ value_print_array_elements (struct value *val, struct ui_file *stream,
          else
            fprintf_filtered (stream, ", ");
        }
+      else if (options->prettyformat_arrays)
+       {
+         fprintf_filtered (stream, "\n");
+         print_spaces_filtered (2 + 2 * recurse, stream);
+       }
       wrap_here (n_spaces (2 + 2 * recurse));
       maybe_print_array_index (index_type, i + low_bound,
                                stream, options);
@@ -1988,6 +1988,11 @@ value_print_array_elements (struct value *val, struct ui_file *stream,
   annotate_array_section_end ();
   if (i < len)
     fprintf_filtered (stream, "...");
+  if (options->prettyformat_arrays)
+    {
+      fprintf_filtered (stream, "\n");
+      print_spaces_filtered (2 * recurse, stream);
+    }
 }
 
 /* Read LEN bytes of target memory at address MEMADDR, placing the