public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 1/2] Fix bug in Ada packed array handling
Date: Thu,  6 Oct 2022 14:06:09 -0600	[thread overview]
Message-ID: <20221006200610.3678399-2-tromey@adacore.com> (raw)
In-Reply-To: <20221006200610.3678399-1-tromey@adacore.com>

A user found a bug where an array of packed arrays was printed
incorrectly.  The bug here is that the packed array has a bit stride,
but the outer array does not -- and should not.  However,
update_static_array_size does not distinguish between an array of
packed arrays and a multi-dimensional packed array, and for the
latter, only the innermost array will end up with a stride.

This patch fixes the problem by adding a flag to indicate whether a
given array type is a constituent of a multi-dimensional array.
---
 gdb/dwarf2/read.c                         |  4 ++++
 gdb/gdbtypes.c                            |  1 +
 gdb/gdbtypes.h                            | 21 +++++++++++++++++++++
 gdb/testsuite/gdb.ada/packed_array.exp    |  6 ++++++
 gdb/testsuite/gdb.ada/packed_array/pa.adb | 14 ++++++++++++++
 5 files changed, 46 insertions(+)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 78f4cc1f60d..116fc59821a 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -15695,6 +15695,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 	{
 	  type = create_array_type_with_stride (NULL, type, range_types[i++],
 						byte_stride_prop, bit_stride);
+	  type->set_is_multi_dimensional (true);
 	  bit_stride = 0;
 	  byte_stride_prop = nullptr;
 	}
@@ -15706,11 +15707,14 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 	{
 	  type = create_array_type_with_stride (NULL, type, range_types[ndim],
 						byte_stride_prop, bit_stride);
+	  type->set_is_multi_dimensional (true);
 	  bit_stride = 0;
 	  byte_stride_prop = nullptr;
 	}
     }
 
+  /* Clear the flag on the outermost array type.  */
+  type->set_is_multi_dimensional (false);
   gdb_assert (type != element_type);
 
   /* Understand Dwarf2 support for vector types (like they occur on
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 643bb0a14a3..9ddb1f81cc1 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1319,6 +1319,7 @@ update_static_array_size (struct type *type)
 	 wrong size when trying to find elements of the outer
 	 array.  */
       if (element_type->code () == TYPE_CODE_ARRAY
+	  && (stride != 0 || element_type->is_multi_dimensional ())
 	  && element_type->length () != 0
 	  && TYPE_FIELD_BITSIZE (element_type, 0) != 0
 	  && get_array_bounds (element_type, &low_bound, &high_bound)
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 94d4b6684fb..f92b3894a1b 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -926,6 +926,15 @@ struct main_type
 
   unsigned int m_flag_flag_enum : 1;
 
+  /* * For TYPE_CODE_ARRAY, this is true if this type is part of a
+     multi-dimensional array.  Multi-dimensional arrays are
+     represented internally as arrays of arrays, and this flag lets
+     gdb distinguish between multiple dimensions and an ordinary array
+     of arrays.  The flag is set on each inner dimension, but not the
+     outermost dimension.  */
+
+  unsigned int m_multi_dimensional : 1;
+
   /* * A discriminant telling us which field of the type_specific
      union is being used for this type, if any.  */
 
@@ -1349,6 +1358,18 @@ struct type
     this->main_type->m_flag_flag_enum = is_flag_enum;
   }
 
+  /* True if this array type is part of a multi-dimensional array.  */
+
+  bool is_multi_dimensional () const
+  {
+    return this->main_type->m_multi_dimensional;
+  }
+
+  void set_is_multi_dimensional (bool value)
+  {
+    this->main_type->m_multi_dimensional = value;
+  }
+
   /* * Assuming that THIS is a TYPE_CODE_FIXED_POINT, return a reference
      to this type's fixed_point_info.  */
 
diff --git a/gdb/testsuite/gdb.ada/packed_array.exp b/gdb/testsuite/gdb.ada/packed_array.exp
index eb857f7e564..df34f31348a 100644
--- a/gdb/testsuite/gdb.ada/packed_array.exp
+++ b/gdb/testsuite/gdb.ada/packed_array.exp
@@ -57,4 +57,10 @@ foreach_with_prefix scenario {all minimal} {
 	    xfail $test
 	}
     }
+
+    set line "(4 => true, false, true, false, true)"
+    gdb_test "print o_var" \
+	[string_to_regexp " = ($line, $line, $line, $line)"]
+    gdb_test "print o2_var" \
+	[string_to_regexp " = ($line, $line, $line, $line)"]
 }
diff --git a/gdb/testsuite/gdb.ada/packed_array/pa.adb b/gdb/testsuite/gdb.ada/packed_array/pa.adb
index 2955f986b00..bb6a2acd388 100644
--- a/gdb/testsuite/gdb.ada/packed_array/pa.adb
+++ b/gdb/testsuite/gdb.ada/packed_array/pa.adb
@@ -27,6 +27,20 @@ procedure PA is
 
    U_Var : Unconstrained_Packed_Array (1 .. Ident (6));
 
+   -- Note that this array is not packed.
+   type Outer_Array is array (1 .. 4) of Packed_Array;
+   O_Var : Outer_Array := ((true, false, true, false, true),
+                           (true, false, true, false, true),
+                           (true, false, true, false, true),
+                           (true, false, true, false, true));
+
+   type Outer_Array2 is array (1 .. 4) of Packed_Array;
+   pragma pack (Outer_Array2);
+   O2_Var : Outer_Array2 := ((true, false, true, false, true),
+                           (true, false, true, false, true),
+                           (true, false, true, false, true),
+                           (true, false, true, false, true));
+
 begin
 
    Var := (True, False, True, False, True);
-- 
2.34.3


  reply	other threads:[~2022-10-06 20:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-06 20:06 [PATCH 0/2] Fix two bugs with packed array printing Tom Tromey
2022-10-06 20:06 ` Tom Tromey [this message]
2022-10-06 20:06 ` [PATCH 2/2] Fix crash in value_print_array_elements Tom Tromey
2022-10-21 15:40 ` [PATCH 0/2] Fix two bugs with packed array printing 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=20221006200610.3678399-2-tromey@adacore.com \
    --to=tromey@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).