old-cross-binutils/gdb/testsuite/gdb.ada
Joel Brobecker 45e44d277a Handling of empty Ada ranges with a negative upper bound.
Consider the following variable declaration:

    type Array_Type is array (Integer range <>) of Integer;
    Var: Array_Type (0 .. -1);

"ptype var" prints the wrong upper bound for that array:

    (gdb) ptype var
    type = array (0 .. 4294967295) of integer

The debugging info for the type of variable "Var" is as follow:

  <2><cf>: Abbrev Number: 13 (DW_TAG_structure_type)
     <d0>   DW_AT_name        : foo__var___PAD
  <3><db>: Abbrev Number: 14 (DW_TAG_member)
     <dc>   DW_AT_name        : F
     <e0>   DW_AT_type        : <0xa5>

This is just an artifact from code generation, which is just
a wrapper that we should ignore. The real type is the type of
field "F" in that PAD type, which is described as:

  <2><a5>: Abbrev Number: 10 (DW_TAG_array_type)
     <a6>   DW_AT_name        : foo__TvarS
  <3><b6>: Abbrev Number: 11 (DW_TAG_subrange_type)
     <b7>   DW_AT_type        : <0xc1>
     <bb>   DW_AT_lower_bound : 0
     <bc>   DW_AT_upper_bound : 0xffffffff

Trouble occurs because DW_AT_upper_bound is encoded using
a DW_FORM_data4, which is ambiguous regarding signedness.
In that case, dwarf2read.c::dwarf2_get_attr_constant_value
reads the value as unsigned, which is not what we want
in this case.

As it happens, we already have code dealing with this situation
in dwarf2read.c::read_subrange_type which checks whether
the subrange's type is signed or not, and if it is, fixes
the bound's value by sign-extending it:

  if (high.kind == PROP_CONST
      && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
    high.data.const_val |= negative_mask;

Unfortunately, what happens in our case is that the base type
of the array's subrange type is marked as being unsigned, and
so we never get to apply the sign extension. Following the DWARF
trail, the range's base type is described as another subrange type...

  <2><c1>: Abbrev Number: 12 (DW_TAG_subrange_type)
     <c7>   DW_AT_name        : foo__TTvarSP1___XDLU_0__1m
     <cb>   DW_AT_type        : <0x2d>

... whose base type is, (finally), a basic type (signed):

  <1><2d>: Abbrev Number: 2 (DW_TAG_base_type)
     <2e>   DW_AT_byte_size   : 4
     <2f>   DW_AT_encoding    : 5        (signed)
     <30>   DW_AT_name        : integer

The reason why GDB thinks that foo__TTvarSP1___XDLU_0__1m
(the base type of the array's range type) is an unsigned type
is found in gdbtypes.c::create_range_type.  We consider that
a range type is unsigned iff its lower bound is >= 0:

  if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0)
    TYPE_UNSIGNED (result_type) = 1;

That is normally sufficient, as one would expect the upper bound to
always be greater or equal to the lower bound. But Ada actually
allows the declaration of empty range types where the upper bound
is less than the lower bound. In this case, the upper bound is
negative, so we should not be marking the type as unsigned.

This patch fixes the issue by simply checking the upper bound as well
as the lower bound, and clears the range type's unsigned flag when
it is found to be constant and negative.

gdb/ChangeLog:

        * gdbtypes.c (create_range_type): Unset RESULT_TYPE's
        flag_unsigned if HIGH_BOUND is constant and negative.

gdb/testsuite/ChangeLog:

        * gdb.ada/n_arr_bound: New testcase.

Tested on x86_64-linux.
2014-11-21 07:07:07 +04:00
..
addr_arith
aliased_array
arr_arr [Ada] Ignore __XA types when redundant. 2014-11-19 12:48:07 +04:00
array_bounds
array_char_idx
array_return
array_subscript_addr
arraydim
arrayidx
arrayparam
arrayptr
atomic_enum
bad-task-bp-keyword
bp_enum_homonym
bp_on_var
bp_range_type
bp_reset
call_pn
catch_ex
char_enum
char_param
complete
cond_lang GDB testsuite: Fix warnings with -std=gnu11 2014-11-13 10:20:44 +01:00
dot_all
dyn_arrayidx
dyn_loc
enum_idx_packed
exec_changed
expr_delims
exprs
fixed_cmp
fixed_points
float_param
formatted_ref
frame_args
fullname_bp
fun_addr
fun_in_declare
funcall_param
homonym
info_exc
info_locals_renaming
int_deref
interface
iwide
lang_switch
mi_catch_ex
mi_dyn_arr
mi_ex_cond
mi_exc_info
mi_interface
mi_task_arg
mi_task_info
mod_from_name
n_arr_bound Handling of empty Ada ranges with a negative upper bound. 2014-11-21 07:07:07 +04:00
nested
null_array
null_record
O2_float_param
operator_bp
optim_drec
packed_array
packed_tagged
pckd_arr_ren
pkd_arr_elem varsize-limit error printing element of packed array... 2014-11-19 12:06:19 +04:00
pp-rec-component
print_chars
ptr_typedef
ptype_field
ptype_tagged_param
py_range
rdv_wait
rec_return
ref_param
ref_tick_size
same_enum
set_pckd_arr_elt
set_wstr
small_reg_param
start
str_ref_cmp
sym_print_name
taft_type
tagged
tagged_access
tagged_not_init
task_bp
tasks
tick_last_segv
tick_length_array_enum_idx
type_coercion
unc_arr_ptr_in_var_rec
uninitialized_vars
variant_record_packed_array
watch_arg
whatis_array_val
widewide
win_fu_syms
addr_arith.exp
aliased_array.exp
arr_arr.exp [Ada] Ignore __XA types when redundant. 2014-11-19 12:48:07 +04:00
array_bounds.exp
array_char_idx.exp
array_return.exp
array_subscript_addr.exp
arraydim.exp
arrayidx.exp
arrayparam.exp
arrayptr.exp
assign_1.exp
atomic_enum.exp
bad-task-bp-keyword.exp
boolean_expr.exp
bp_enum_homonym.exp
bp_on_var.exp
bp_range_type.exp
bp_reset.exp
call_pn.exp
catch_ex.exp
char_enum.exp
char_param.exp
complete.exp
cond_lang.exp
dot_all.exp
dyn_arrayidx.exp
dyn_loc.exp
enum_idx_packed.exp
exec_changed.exp
expr_delims.exp
exprs.exp
fixed_cmp.exp
fixed_points.exp
float_param.exp
formatted_ref.exp
frame_args.exp
fullname_bp.exp
fun_addr.exp
fun_in_declare.exp
funcall_param.exp
gnat_ada.gpr
homonym.exp
info_exc.exp
info_locals_renaming.exp
info_types.c
info_types.exp
int_deref.exp
interface.exp
iwide.exp
lang_switch.exp
Makefile.in
mi_catch_ex.exp
mi_dyn_arr.exp
mi_ex_cond.exp
mi_exc_info.exp
mi_interface.exp
mi_task_arg.exp
mi_task_info.exp
mod_from_name.exp
n_arr_bound.exp Handling of empty Ada ranges with a negative upper bound. 2014-11-21 07:07:07 +04:00
nested.exp
null_array.exp
null_record.exp
O2_float_param.exp
operator_bp.exp
optim_drec.exp
packed_array.exp
packed_tagged.exp
pckd_arr_ren.exp
pkd_arr_elem.exp varsize-limit error printing element of packed array... 2014-11-19 12:06:19 +04:00
pp-rec-component.exp
pp-rec-component.py
print_chars.exp
print_pc.exp
ptr_typedef.exp
ptype_arith_binop.exp
ptype_field.exp
ptype_tagged_param.exp
py_range.exp
rdv_wait.exp
rec_return.exp
ref_param.exp
ref_tick_size.exp
same_enum.exp
set_pckd_arr_elt.exp
set_wstr.exp
small_reg_param.exp
start.exp
str_ref_cmp.exp
sym_print_name.exp
taft_type.exp
tagged.exp
tagged_access.exp
tagged_not_init.exp
task_bp.exp
tasks.exp
tick_last_segv.exp
tick_length_array_enum_idx.exp
type_coercion.exp
unc_arr_ptr_in_var_rec.exp
uninitialized_vars.exp
variant_record_packed_array.exp
watch_arg.exp
whatis_array_val.exp
widewide.exp
win_fu_syms.exp