From: Jim Blandy <jimb@redhat.com>
To: Jim Ingham <jingham@apple.com>
Cc: gdb-patches@sources.redhat.com
Subject: Re: Trivial fix in value_sub
Date: Thu, 04 Apr 2002 15:15:00 -0000 [thread overview]
Message-ID: <npy9g39htb.fsf@zwingli.cygnus.com> (raw)
In-Reply-To: <C207B8FE-4820-11D6-86BE-000393540DDC@apple.com>
Great. Please commit that.
Jim Ingham <jingham@apple.com> writes:
> Okay, how 'bout:
>
> 2002-04-04 Jim Ingham <jingham@apple.com>
>
> * valarith.c (find_size_for_pointer_math): New function,
> either returns
> the size for a pointer's target, returns 1 for void *, or errors for
> incomplete types.
> (value_add, value_sub): use find_size_for_pointer_math.
>
> Index: valarith.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/valarith.c,v
> retrieving revision 1.14
> diff -c -w -p -r1.14 valarith.c
> *** valarith.c 2002/03/27 21:35:35 1.14
> --- valarith.c 2002/04/04 23:05:12
> *************** static struct value *value_subscripted_r
> *** 43,54 ****
> void _initialize_valarith (void);
>
>
>
> struct value *
> value_add (struct value *arg1, struct value *arg2)
> {
> struct value *valint;
> struct value *valptr;
> ! register int len;
> struct type *type1, *type2, *valptrtype;
>
> COERCE_NUMBER (arg1);
> --- 43,92 ----
> void _initialize_valarith (void);
>
>
>
> + /* Given a pointer, return the size of its target.
> + If the pointer type is void *, then return 1.
> + If the target type is incomplete, then error out.
> + This isn't a general purpose function, but just a
> + helper for value_sub & value_add.
> + */
> +
> + static LONGEST
> + find_size_for_pointer_math (struct type *ptr_type)
> + {
> + LONGEST sz = -1;
> + struct type *ptr_target;
> +
> + ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
> +
> + sz = TYPE_LENGTH (ptr_target);
> + if (sz == 0)
> + {
> + if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
> + sz = 1;
> + else
> + {
> + char *name;
> +
> + name = TYPE_NAME (ptr_target);
> + if (name == NULL)
> + name = TYPE_TAG_NAME (ptr_target);
> + if (name == NULL)
> + error ("Cannot perform pointer math on incomplete types, "
> + "try casting to a known type, or void *.");
> + else
> + error ("Cannot perform pointer math on incomplete type
> \"%s\", "
> + "try casting to a known type, or void *.", name);
> + }
> + }
> + return sz;
> + }
> +
> struct value *
> value_add (struct value *arg1, struct value *arg2)
> {
> struct value *valint;
> struct value *valptr;
> ! LONGEST sz;
> struct type *type1, *type2, *valptrtype;
>
> COERCE_NUMBER (arg1);
> *************** value_add (struct value *arg1, struct va
> *** 77,88 ****
> valint = arg1;
> valptrtype = type2;
> }
> ! len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE
> (valptrtype)));
> ! if (len == 0)
> ! len = 1; /* For (void *) */
> retval = value_from_pointer (valptrtype,
> value_as_address (valptr)
> ! + (len * value_as_long (valint)));
> VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
> return retval;
> }
> --- 115,126 ----
> valint = arg1;
> valptrtype = type2;
> }
> !
> ! sz = find_size_for_pointer_math (valptrtype);
> !
> retval = value_from_pointer (valptrtype,
> value_as_address (valptr)
> ! + (sz * value_as_long (valint)));
> VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
> return retval;
> }
> *************** value_sub (struct value *arg1, struct va
> *** 104,110 ****
> if (TYPE_CODE (type2) == TYPE_CODE_INT)
> {
> /* pointer - integer. */
> ! LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE
> (type1)));
> return value_from_pointer (type1,
> (value_as_address (arg1)
> - (sz * value_as_long (arg2))));
> --- 142,149 ----
> if (TYPE_CODE (type2) == TYPE_CODE_INT)
> {
> /* pointer - integer. */
> ! LONGEST sz = find_size_for_pointer_math (type1);
> !
> return value_from_pointer (type1,
> (value_as_address (arg1)
> - (sz * value_as_long (arg2))));
>
> Jim
>
> >
> > Jim Ingham <jingham@apple.com> writes:
> >> Okay, so to be consistent, I throw errors in both value_add &
> >> value_sub. How about this:
> >
> > Superb! Could you move the "fix up my size or report an error" code
> > into a function instead of repeating it?
> >
> --
> Jim Ingham jingham@apple.com
> Developer Tools - gdb
> Apple Computer
next prev parent reply other threads:[~2002-04-04 23:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-03-29 15:27 Jim Ingham
2002-04-03 14:52 ` Jim Blandy
2002-04-03 15:55 ` Michael Snyder
2002-04-03 16:11 ` Jim Ingham
2002-04-03 19:55 ` Jim Blandy
2002-04-03 20:11 ` Daniel Jacobowitz
2002-04-04 13:46 ` Jim Ingham
2002-04-04 14:24 ` Jim Blandy
2002-04-04 15:07 ` Jim Ingham
2002-04-04 15:15 ` Jim Blandy [this message]
2002-04-09 17:12 ` Jim Ingham
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=npy9g39htb.fsf@zwingli.cygnus.com \
--to=jimb@redhat.com \
--cc=gdb-patches@sources.redhat.com \
--cc=jingham@apple.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