From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19928 invoked by alias); 4 Apr 2002 23:07:44 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 19919 invoked from network); 4 Apr 2002 23:07:43 -0000 Received: from unknown (HELO mail-out2.apple.com) (17.254.0.51) by sources.redhat.com with SMTP; 4 Apr 2002 23:07:43 -0000 Received: from mailgate2.apple.com (A17-129-100-225.apple.com [17.129.100.225]) by mail-out2.apple.com (8.11.3/8.11.3) with ESMTP id g34N7hs10580 for ; Thu, 4 Apr 2002 15:07:43 -0800 (PST) Received: from scv3.apple.com (scv3.apple.com) by mailgate2.apple.com (Content Technologies SMTPRS 4.2.1) with ESMTP id ; Thu, 4 Apr 2002 15:07:42 -0800 Received: from inghji (inghji.apple.com [17.202.40.220]) by scv3.apple.com (8.11.3/8.11.3) with ESMTP id g34N7dv04279; Thu, 4 Apr 2002 15:07:42 -0800 (PST) Date: Thu, 04 Apr 2002 15:07:00 -0000 Subject: Re: Trivial fix in value_sub Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v481) Cc: gdb-patches@sources.redhat.com To: Jim Blandy From: Jim Ingham In-Reply-To: Message-Id: Content-Transfer-Encoding: 7bit X-SW-Source: 2002-04/txt/msg00141.txt.bz2 Okay, how 'bout: 2002-04-04 Jim Ingham * 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 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