2005-10-03 Joel Brobecker <brobecker@adacore.com>
authorJoel Brobecker <brobecker@gnat.com>
Mon, 3 Oct 2005 21:21:20 +0000 (21:21 +0000)
committerJoel Brobecker <brobecker@gnat.com>
Mon, 3 Oct 2005 21:21:20 +0000 (21:21 +0000)
        * language.h (language_defn): New field la_print_array_index.
        (LA_PRINT_ARRAY_INDEX): New macro.
        (default_print_array_index): Add declaration.
        * language.c (default_print_array_index): new function.
        (unknown_language): Add value for new field.
        (auto_language): Likewise.
        (local_language): Likewise.
        * ada-lang.c (ada_print_array_index): New function.
        (ada_language_defn): Add value for new field.
        * c-lang.c (c_language_defn): Likewise.
        (cpluc_language_defn): Likewise.
        (asm_language_defn): Likewise.
        (minimal_language_defn): Likewise.
        * f-lang.c (f_language_defn): Likewise.
        * jv-lang.c (java_language_defn): Likewise.
        * m2-lang.c (m2_language_defn): Likewise.
        * objc-lang.c (objc_language_defn): Likewise.
        * p-lang.c (pascal_language_defn): Likewise.
        * scm-lang.c (scm_language_defn): Likewise.
        * valprint.h (print_array_indexes_p): Add declaration.
        (get_array_low_bound): Add declaration.
        (maybe_print_array_index): Add declaration.
        * valprint.c (print_array_indexes): New static variable.
        (show_print_array_indexes): New function.
        (print_array_indexes_p): New function.
        (get_array_low_bound): New function.
        (maybe_print_array_index): New function.
        (val_print_array_elements): Print the index of each element if
        requested by the user.
        (_initialize_valprint): Add new array-indexes "set/show print" command.
        * ada-valprint.c (print_optional_low_bound): Replace extracted code
        by call to ada_get_array_low_bound_and_type(). Stop printing the low
        bound if indexes will be printed for all elements of the array.
        (val_print_packed_array_elements): Print the index of each element
        of the array if necessary.

14 files changed:
gdb/ChangeLog
gdb/ada-lang.c
gdb/ada-valprint.c
gdb/c-lang.c
gdb/f-lang.c
gdb/jv-lang.c
gdb/language.c
gdb/language.h
gdb/m2-lang.c
gdb/objc-lang.c
gdb/p-lang.c
gdb/scm-lang.c
gdb/valprint.c
gdb/valprint.h

index 9ea389c3eb2cfc0db78cbb7581c24c165bebb48b..f00e8603a2fedfbd664120888c646b5c7e896deb 100644 (file)
@@ -1,4 +1,42 @@
-2005-10-02  Joel Brobecker  <brobecker@adacore.com>
+2005-10-03  Joel Brobecker  <brobecker@adacore.com>
+
+       * language.h (language_defn): New field la_print_array_index.
+       (LA_PRINT_ARRAY_INDEX): New macro.
+       (default_print_array_index): Add declaration.
+       * language.c (default_print_array_index): new function.
+       (unknown_language): Add value for new field.
+       (auto_language): Likewise.
+       (local_language): Likewise.
+       * ada-lang.c (ada_print_array_index): New function.
+       (ada_language_defn): Add value for new field.
+       * c-lang.c (c_language_defn): Likewise.
+       (cpluc_language_defn): Likewise.
+       (asm_language_defn): Likewise.
+       (minimal_language_defn): Likewise.
+       * f-lang.c (f_language_defn): Likewise.
+       * jv-lang.c (java_language_defn): Likewise.
+       * m2-lang.c (m2_language_defn): Likewise.
+       * objc-lang.c (objc_language_defn): Likewise.
+       * p-lang.c (pascal_language_defn): Likewise.
+       * scm-lang.c (scm_language_defn): Likewise.
+       * valprint.h (print_array_indexes_p): Add declaration.
+       (get_array_low_bound): Add declaration.
+       (maybe_print_array_index): Add declaration.
+       * valprint.c (print_array_indexes): New static variable.
+       (show_print_array_indexes): New function.
+       (print_array_indexes_p): New function.
+       (get_array_low_bound): New function.
+       (maybe_print_array_index): New function.
+       (val_print_array_elements): Print the index of each element if
+       requested by the user.
+       (_initialize_valprint): Add new array-indexes "set/show print" command.
+       * ada-valprint.c (print_optional_low_bound): Replace extracted code
+       by call to ada_get_array_low_bound_and_type(). Stop printing the low
+       bound if indexes will be printed for all elements of the array.
+       (val_print_packed_array_elements): Print the index of each element
+       of the array if necessary.
+
+2005-10-03  Joel Brobecker  <brobecker@adacore.com>
 
        * hppa-tdep.c (read_unwind_info): Fix typo in comment.
 
index 0760beca5e49a16daf01d41e8f8696c237f19acf..31063f76165d8cb495b46ba1ced6b025419e808a 100644 (file)
@@ -303,6 +303,16 @@ ada_get_gdb_completer_word_break_characters (void)
   return ada_completer_word_break_characters;
 }
 
+/* Print an array element index using the Ada syntax.  */
+
+static void
+ada_print_array_index (struct value *index_value, struct ui_file *stream,
+                       int format, enum val_prettyprint pretty)
+{
+  LA_VALUE_PRINT (index_value, stream, format, pretty);
+  fprintf_filtered (stream, " => ");
+}
+
 /* Read the string located at ADDR from the inferior and store the
    result into BUF.  */
 
@@ -8766,6 +8776,7 @@ const struct language_defn ada_language_defn = {
   NULL,
   ada_get_gdb_completer_word_break_characters,
   ada_language_arch_info,
+  ada_print_array_index,
   LANG_MAGIC
 };
 
index 0fbb9c9b862dd8b7e563fcd8b0e8c65c786daa04..a02bf6e39928f331fe843976a51c76f7fbb63b23 100644 (file)
@@ -86,21 +86,14 @@ print_optional_low_bound (struct ui_file *stream, struct type *type)
   struct type *index_type;
   long low_bound;
 
-  index_type = TYPE_INDEX_TYPE (type);
-  low_bound = 0;
-
-  if (index_type == NULL)
+  if (print_array_indexes_p ())
     return 0;
-  if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
-    {
-      low_bound = TYPE_LOW_BOUND (index_type);
-      if (low_bound > TYPE_HIGH_BOUND (index_type))
-       return 0;
-      index_type = TYPE_TARGET_TYPE (index_type);
-    }
-  else
+
+  if (!get_array_low_bound (type, &low_bound))
     return 0;
 
+  index_type = TYPE_INDEX_TYPE (type);
+
   switch (TYPE_CODE (index_type))
     {
     case TYPE_CODE_ENUM:
@@ -137,16 +130,18 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
   unsigned int i;
   unsigned int things_printed = 0;
   unsigned len;
-  struct type *elttype;
+  struct type *elttype, *index_type;
   unsigned eltlen;
   unsigned long bitsize = TYPE_FIELD_BITSIZE (type, 0);
   struct value *mark = value_mark ();
+  LONGEST low = 0;
 
   elttype = TYPE_TARGET_TYPE (type);
   eltlen = TYPE_LENGTH (check_typedef (elttype));
+  index_type = TYPE_INDEX_TYPE (type);
 
   {
-    LONGEST low, high;
+    LONGEST high;
     if (get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), &low, &high) < 0)
       len = 1;
     else
@@ -174,6 +169,7 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
            }
        }
       wrap_here (n_spaces (2 + 2 * recurse));
+      maybe_print_array_index (index_type, i + low, stream, format, pretty);
 
       i0 = i;
       v0 = ada_value_primitive_packed_val (NULL, valaddr,
@@ -219,6 +215,8 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
                      fprintf_filtered (stream, ", ");
                    }
                  wrap_here (n_spaces (2 + 2 * recurse));
+                 maybe_print_array_index (index_type, j + low,
+                                          stream, format, pretty);
                }
              val_print (elttype, value_contents (v0), 0, 0, stream, format,
                         0, recurse + 1, pretty);
index 6f1d8fe7fba8e6734f3fa8f3c76bed2541fd792b..882eaaa0b6c66ab88ef2c994afb32af5a834c8dc 100644 (file)
@@ -595,6 +595,7 @@ const struct language_defn c_language_defn =
   NULL,
   default_word_break_characters,
   c_language_arch_info,
+  default_print_array_index,
   LANG_MAGIC
 };
 
@@ -653,6 +654,7 @@ const struct language_defn cplus_language_defn =
   &builtin_type_char,          /* Type of string elements */
   default_word_break_characters,
   NULL, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
@@ -688,6 +690,7 @@ const struct language_defn asm_language_defn =
   NULL,
   default_word_break_characters,
   c_language_arch_info, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
@@ -728,6 +731,7 @@ const struct language_defn minimal_language_defn =
   NULL,
   default_word_break_characters,
   c_language_arch_info,
+  default_print_array_index,
   LANG_MAGIC
 };
 
index 9308dbd6bca55c9241b0b6e630d1eae9e60b1e7d..4ae0d508c666fd398f4cd6948be06c9ff1424ec5 100644 (file)
@@ -485,6 +485,7 @@ const struct language_defn f_language_defn =
   &builtin_type_f_character,   /* Type of string elements */
   default_word_break_characters,
   NULL, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
index c6700c9241568e6e1167d39f1e0b0e5edb9db930..02b8100a78c5d92927904dc895072758093e89a9 100644 (file)
@@ -1114,6 +1114,7 @@ const struct language_defn java_language_defn =
   NULL,
   default_word_break_characters,
   c_language_arch_info,
+  default_print_array_index,
   LANG_MAGIC
 };
 
index 78a72f0dbfa6aa9fc28989f9881f670703477baf..493fe5dca0154526fba9f4f50eec3807529b8d50 100644 (file)
@@ -1057,6 +1057,17 @@ default_word_break_characters (void)
   return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
 }
 
+/* Print the index of array elements using the C99 syntax.  */
+
+void
+default_print_array_index (struct value *index_value, struct ui_file *stream,
+                           int format, enum val_prettyprint pretty)
+{
+  fprintf_filtered (stream, "[");
+  LA_VALUE_PRINT (index_value, stream, format, pretty);
+  fprintf_filtered (stream, "] = ");
+}
+
 /* Define the language that is no language.  */
 
 static int
@@ -1181,6 +1192,7 @@ const struct language_defn unknown_language_defn =
   NULL,
   default_word_break_characters,
   unknown_language_arch_info,  /* la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
@@ -1217,6 +1229,7 @@ const struct language_defn auto_language_defn =
   NULL,
   default_word_break_characters,
   unknown_language_arch_info,  /* la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
@@ -1252,6 +1265,7 @@ const struct language_defn local_language_defn =
   NULL,
   default_word_break_characters,
   unknown_language_arch_info,  /* la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 \f
index d581bd2ddec64baff15edfd9f53e5addd8d8005a..e25aec4036d8d09fcbeae3dbf6d0cf648481db66 100644 (file)
@@ -275,6 +275,12 @@ struct language_defn
     void (*la_language_arch_info) (struct gdbarch *,
                                   struct language_arch_info *);
 
+    /* Print the index of an element of an array.  */
+    void (*la_print_array_index) (struct value *index_value,
+                                  struct ui_file *stream,
+                                  int format,
+                                  enum val_prettyprint pretty);
+
     /* Add fields above this point, so the magic number is always last. */
     /* Magic number for compat checking */
 
@@ -362,6 +368,9 @@ extern enum language set_language (enum language);
 #define LA_EMIT_CHAR(ch, stream, quoter) \
   (current_language->la_emitchar(ch, stream, quoter))
 
+#define LA_PRINT_ARRAY_INDEX(index_value, stream, format, pretty) \
+  (current_language->la_print_array_index(index_value, stream, format, pretty))
+
 /* Test a character to decide whether it can be printed in literal form
    or needs to be printed in another representation.  For example,
    in C the literal form of the character with octal value 141 is 'a'
@@ -457,4 +466,10 @@ extern char *language_class_name_from_physname (const struct language_defn *,
 /* Splitting strings into words.  */
 extern char *default_word_break_characters (void);
 
+/* Print the index of an array element using the C99 syntax.  */
+extern void default_print_array_index (struct value *index_value,
+                                       struct ui_file *stream,
+                                       int format,
+                                       enum val_prettyprint pretty);
+
 #endif /* defined (LANGUAGE_H) */
index 6ec2364b352de70288eda640b41af14fe74b825f..29f018095b70142c6738ef168f6dae8415e184fc 100644 (file)
@@ -437,6 +437,7 @@ const struct language_defn m2_language_defn =
   &builtin_type_m2_char,       /* Type of string elements */
   default_word_break_characters,
   NULL, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
index a27ff68bd19877f6f872ddae2d7e94e81388c156..6c4587ec844e645bb127c6c0f82ff4659baa6d26 100644 (file)
@@ -684,6 +684,7 @@ const struct language_defn objc_language_defn = {
   &builtin_type_char,          /* Type of string elements */
   default_word_break_characters,
   NULL, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
index cd05a2484ea40641bbe295a30240d74a9a946214..36092c6d33a286542d580f32035d414c8ac23b53 100644 (file)
@@ -477,6 +477,7 @@ const struct language_defn pascal_language_defn =
   &builtin_type_char,          /* Type of string elements */
   default_word_break_characters,
   NULL, /* FIXME: la_language_arch_info.  */
+  default_print_array_index,
   LANG_MAGIC
 };
 
index 5ffa233603aecdfd234aeb5c349b5280a928f024..f86a1398f64769c7cc13081aa5a04f35f641f1f8 100644 (file)
@@ -269,6 +269,7 @@ const struct language_defn scm_language_defn =
   NULL,
   default_word_break_characters,
   c_language_arch_info,
+  default_print_array_index,
   LANG_MAGIC
 };
 
index b65f8e553b81031104866733d194fa748a7871ce..60149acaedfbe151871cf5639c83f824f8744b0c 100644 (file)
@@ -100,6 +100,17 @@ Default output radix for printing of values is %s.\n"),
 }
 int output_format = 0;
 
+/* By default we print arrays without printing the index of each element in
+   the array.  This behavior can be changed by setting PRINT_ARRAY_INDEXES.  */
+
+static int print_array_indexes = 0;
+static void
+show_print_array_indexes (struct ui_file *file, int from_tty,
+                         struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
+}
+
 /* Print repeat counts if there are more than this many repetitions of an
    element in an array.  Referenced by the low level language dependent
    print routines. */
@@ -859,6 +870,70 @@ print_char_chars (struct ui_file *stream, const gdb_byte *valaddr,
     }
 }
 
+/* Return non-zero if the debugger should print the index of each element
+   when printing array values.  */
+
+int
+print_array_indexes_p (void)
+{              
+  return print_array_indexes;
+} 
+
+/* Assuming TYPE is a simple, non-empty array type, compute its lower bound.
+   Save it into LOW_BOUND if not NULL.
+
+   Return 1 if the operation was successful. Return zero otherwise,
+   in which case the value of LOW_BOUND is unmodified.
+   
+   Computing the array lower bound is pretty easy, but this function
+   does some additional verifications before returning the low bound.
+   If something incorrect is detected, it is better to return a status
+   rather than throwing an error, making it easier for the caller to
+   implement an error-recovery plan.  For instance, it may decide to
+   warn the user that the bound was not found and then use a default
+   value instead.  */
+
+int
+get_array_low_bound (struct type *type, long *low_bound)
+{
+  struct type *index = TYPE_INDEX_TYPE (type);
+  long low = 0;
+                                  
+  if (index == NULL)
+    return 0;
+
+  if (TYPE_CODE (index) != TYPE_CODE_RANGE
+      && TYPE_CODE (index) != TYPE_CODE_ENUM)
+    return 0;
+
+  low = TYPE_LOW_BOUND (index);
+  if (low > TYPE_HIGH_BOUND (index))
+    return 0;
+
+  if (low_bound)
+    *low_bound = low;
+
+  return 1;
+}
+                                       
+/* Print on STREAM using the given FORMAT the index for the element
+   at INDEX of an array whose index type is INDEX_TYPE.  */
+    
+void  
+maybe_print_array_index (struct type *index_type, LONGEST index,
+                         struct ui_file *stream, int format,
+                         enum val_prettyprint pretty)
+{
+  struct value *index_value;
+
+  if (!print_array_indexes)
+    return; 
+    
+  index_value = value_from_longest (index_type, index);
+
+  LA_PRINT_ARRAY_INDEX (index_value, stream, format, pretty);
+}   
+
 /*  Called by various <lang>_val_print routines to print elements of an
    array in the form "<elem1>, <elem2>, <elem3>, ...".
 
@@ -877,17 +952,25 @@ val_print_array_elements (struct type *type, const gdb_byte *valaddr,
 {
   unsigned int things_printed = 0;
   unsigned len;
-  struct type *elttype;
+  struct type *elttype, *index_type;
   unsigned eltlen;
   /* Position of the array element we are examining to see
      whether it is repeated.  */
   unsigned int rep1;
   /* Number of repetitions we have detected so far.  */
   unsigned int reps;
+  long low_bound_index;
+
+  if (!get_array_low_bound (type, &low_bound_index))
+    {
+      warning ("unable to get low bound of array, using zero as default");
+      low_bound_index = 0;
+    }
 
   elttype = TYPE_TARGET_TYPE (type);
   eltlen = TYPE_LENGTH (check_typedef (elttype));
   len = TYPE_LENGTH (type) / eltlen;
+  index_type = TYPE_INDEX_TYPE (type);
 
   annotate_array_section_begin (i, elttype);
 
@@ -906,6 +989,8 @@ val_print_array_elements (struct type *type, const gdb_byte *valaddr,
            }
        }
       wrap_here (n_spaces (2 + 2 * recurse));
+      maybe_print_array_index (index_type, i + low_bound_index,
+                               stream, format, pretty);
 
       rep1 = i + 1;
       reps = 1;
@@ -1396,6 +1481,12 @@ Show the default input and output number radices.\n\
 Use 'show input-radix' or 'show output-radix' to independently show each."),
           &showlist);
 
+  add_setshow_boolean_cmd ("array-indexes", class_support,
+                           &print_array_indexes, _("\
+Set printing of array indexes."), _("\
+Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
+                           &setprintlist, &showprintlist);
+
   /* Give people the defaults which they are used to.  */
   prettyprint_structs = 0;
   prettyprint_arrays = 0;
index 4b776189f6c4751c81b8758ace2eada680f9c8e9..1c2288e33d7dc8edd5b023012880295eee32165e 100644 (file)
@@ -50,6 +50,14 @@ extern int output_format;
 
 extern int stop_print_at_null; /* Stop printing at null char? */
 
+extern int print_array_indexes_p (void);
+extern int get_array_low_bound (struct type *type, long *low_bound);
+
+extern void maybe_print_array_index (struct type *index_type, LONGEST index,
+                                     struct ui_file *stream, int format,
+                                     enum val_prettyprint pretty);
+
 extern void val_print_array_elements (struct type *, const gdb_byte *,
                                      CORE_ADDR, struct ui_file *, int,
                                      int, int, enum val_prettyprint,
This page took 0.042052 seconds and 4 git commands to generate.