Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@adacore.com>
To: gdb-patches@sources.redhat.com
Subject: [RFA/ada] fix bug in ada-valprint.c:print_optional_low_bound()
Date: Wed, 05 Oct 2005 23:28:00 -0000	[thread overview]
Message-ID: <20051005232815.GG1590@adacore.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2336 bytes --]

Hello,

This is a regression I noticed after introducing "set print array-indexes".
Consider the code submitted in the gdb.ada/arrayidx testcase:

        http://sources.redhat.com/ml/gdb-patches/2005-10/msg00046.html

You have an enumerated type declared, and then an array type declared
using that enumerated type as its index:

        type Index is (One, Two, Three);
        type RTable is array (Index range Two .. Three) of Integer;

As you see, the array type does not use the entire enumerated type
range as its index, but only a subrange of it. So when printing an
array of this type, such as:

        R_Two_Three : RTable := (2, 3);

The debugger should notice that the lower bound of the array is
not the first element of the enumerated type, and therefore should
explicitly print the index of the first array element, like this:

        (gdb) print r_two_three
        $1 = (two => 2, 3)

However, we currently get this:

        (gdb) print r_two_three
        $1 = (two => 2, 3)

What happens is that GDB sees in print_optional_low_bound() that
the index type is a TYPE_CODE_RANGE, failing to see that underneath
there is an enumerated type. So it therefore compares the value of
the low bound against 1, which it should compare it against the value
of the first enumeration in the enumerated type.

The underlying value of an enumerated type is typically zero, so
"one" is zero, and "two" is 1.

Just to make it easier to see what's after the patch, here is a copy
of the switch seen in the patch:

        switch (TYPE_CODE (index_type))
          {
          case TYPE_CODE_ENUM:
            if (low_bound == TYPE_FIELD_BITPOS (index_type, 0))
              return 0;
            break;
          case TYPE_CODE_UNDEF:
            index_type = builtin_type_long;
            /* FALL THROUGH */
          default:
            if (low_bound == 1)
              return 0;
            break;
          }

The patch fixes this problem. It's not sufficient to fix the problem,
due to another problem I eluded to when submitting gdb.ada/arrayidx.exp.
But this patch will be needed.

2005-10-05  Joel Brobecker  <brobecker@adacore.com>

        * ada-valprint.c (print_optional_low_bound): Handle properly
        cases where the array index type is a TYPE_CODE_RANGE.

Tested on x86-linux. No regression.

Thanks,
-- 
Joel

[-- Attachment #2: ada-valprint.c.diff --]
[-- Type: text/plain, Size: 887 bytes --]

Index: ada-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/ada-valprint.c,v
retrieving revision 1.24
diff -u -p -r1.24 ada-valprint.c
--- ada-valprint.c	3 Oct 2005 21:21:20 -0000	1.24
+++ ada-valprint.c	5 Oct 2005 23:26:09 -0000
@@ -94,6 +94,16 @@ print_optional_low_bound (struct ui_file
 
   index_type = TYPE_INDEX_TYPE (type);
 
+  if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
+    {
+      /* We need to know what the base type is, in order to do the
+         appropriate check below.  Otherwise, if this is a subrange
+         of an enumerated type, where the underlying value of the
+         first element is typically 0, we might test the low bound
+         against the wrong value.  */
+      index_type = TYPE_TARGET_TYPE (index_type);
+    }
+
   switch (TYPE_CODE (index_type))
     {
     case TYPE_CODE_ENUM:

             reply	other threads:[~2005-10-05 23:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-05 23:28 Joel Brobecker [this message]
2005-10-05 23:30 ` Joel Brobecker
2005-10-09 20:32 ` Daniel Jacobowitz
2005-10-10  1:15   ` Joel Brobecker

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=20051005232815.GG1590@adacore.com \
    --to=brobecker@adacore.com \
    --cc=gdb-patches@sources.redhat.com \
    /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