From: Daniel Jacobowitz Date: Mon, 8 Sep 2008 22:16:51 +0000 (+0000) Subject: * valops.c (value_cast_structs): Return NULL for failure. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=694182d2ba45b3a4e97846f59cf04b75ad66901a;p=binutils-gdb.git * valops.c (value_cast_structs): Return NULL for failure. (value_cast): Handle NULL from value_cast_structs. (value_fetch_lazy): Call check_typedef. Remove unused variable. testsuite/ * gdb.base/structs3.c, gdb.base/structs3.exp: New files. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index c679592e841..8edf7dd0ab2 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2008-09-08 Daniel Jacobowitz + + * valops.c (value_cast_structs): Return NULL for failure. + (value_cast): Handle NULL from value_cast_structs. + (value_fetch_lazy): Call check_typedef. Remove unused variable. + 2008-09-08 Pedro Alves * inferior.h (context_switch_to): Delete. diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 4044b60ed13..59b26a4dd61 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2008-09-08 Daniel Jacobowitz + + * gdb.base/structs3.c, gdb.base/structs3.exp: New files. + 2008-09-08 Jerome Guitton * gdb.arch/powerpc-aix-prologue.c (stack_check_probe_1) diff --git a/gdb/testsuite/gdb.base/structs3.c b/gdb/testsuite/gdb.base/structs3.c new file mode 100644 index 00000000000..0c20fd708a8 --- /dev/null +++ b/gdb/testsuite/gdb.base/structs3.c @@ -0,0 +1,42 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2008 + 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 . */ + +struct One +{ + int x; +}; + +struct Two +{ + struct One one; + int x, y; +}; + +struct Two two = { { 1 }, 2, 3 }; + +typedef struct One tOne; +typedef struct Two tTwo; + +tOne *onep = &two.one; +tTwo *twop = &two; + +int main() +{ + onep->x = twop->y; + return 0; +} diff --git a/gdb/testsuite/gdb.base/structs3.exp b/gdb/testsuite/gdb.base/structs3.exp new file mode 100644 index 00000000000..bec93b60027 --- /dev/null +++ b/gdb/testsuite/gdb.base/structs3.exp @@ -0,0 +1,37 @@ +# This testcase is part of GDB, the GNU debugger. + +# Copyright 2008 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 . + +set testfile "structs3" +set srcfile ${testfile}.c +set binfile ${objdir}/${subdir}/${testfile} + +if { [prepare_for_testing structs3.exp "structs3" "" {debug}] } { + return -1 +} + +set vhn "\\$\[0-9\]+" + +# Check the real contents. +gdb_test "print two" "$vhn = {one = {x = 1}, x = 2, y = 3}" + +# Check through the pointer. +gdb_test "print *twop" "$vhn = {one = {x = 1}, x = 2, y = 3}" + +# Check through a pointer to a smaller type, casted up. +gdb_test "print *(struct Two *)onep" "$vhn = {one = {x = 1}, x = 2, y = 3}" + +gdb_test "print *(tTwo *)onep" "$vhn = {one = {x = 1}, x = 2, y = 3}" diff --git a/gdb/valops.c b/gdb/valops.c index 49106495689..0d1ffdf3e6a 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -193,7 +193,8 @@ allocate_space_in_inferior (int len) /* Cast struct value VAL to type TYPE and return as a value. Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION - for this to work. Typedef to one of the codes is permitted. */ + for this to work. Typedef to one of the codes is permitted. + Returns NULL if the cast is neither an upcast nor a downcast. */ static struct value * value_cast_structs (struct type *type, struct value *v2) @@ -244,7 +245,8 @@ value_cast_structs (struct type *type, struct value *v2) return value_at (type, addr2); } } - return v2; + + return NULL; } /* Cast one pointer or reference type to another. Both TYPE and @@ -397,7 +399,12 @@ value_cast (struct type *type, struct value *arg2) if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION) && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION) && TYPE_NAME (type) != 0) - return value_cast_structs (type, arg2); + { + struct value *v = value_cast_structs (type, arg2); + if (v) + return v; + } + if (code1 == TYPE_CODE_FLT && scalar) return value_from_double (type, value_as_double (arg2)); else if (code1 == TYPE_CODE_DECFLOAT && scalar) @@ -615,9 +622,8 @@ value_fetch_lazy (struct value *val) if (VALUE_LVAL (val) == lval_memory) { CORE_ADDR addr = VALUE_ADDRESS (val) + value_offset (val); - int length = TYPE_LENGTH (value_enclosing_type (val)); + int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val))); - struct type *type = value_type (val); if (length) read_memory (addr, value_contents_all_raw (val), length); }