Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v2][PR gdb/24789] Allow pointer arithmetic with integer references
Date: Wed,  1 Apr 2020 00:06:55 +0200	[thread overview]
Message-ID: <20200331220655.4436-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20200331220655.4436-1-ssbssa.ref@yahoo.de>

Considering these variables:
int i = 3;
int &iref = i;

It's not possible to do any pointer arithmetic with iref:
(gdb) p &i+iref
Argument to arithmetic operation not a number or boolean.

So this adds checks for references to integers in pointer arithmetic.

gdb/ChangeLog:

2020-04-01  Hannes Domani  <ssbssa@yahoo.de>

	PR gdb/24789
	* eval.c (is_integral_or_integral_reference): New function.
	(evaluate_subexp_standard): Allow integer references in
	pointer arithmetic.

gdb/testsuite/ChangeLog:

2020-04-01  Hannes Domani  <ssbssa@yahoo.de>

	PR gdb/24789
	* gdb.cp/misc.cc: Add integer reference variable.
	* gdb.cp/misc.exp: Add test.
---
v2:
- no assignment in expression, new helper function instead
- add test cases
---
 gdb/eval.c                    | 20 +++++++++++++++++---
 gdb/testsuite/gdb.cp/misc.cc  |  3 +++
 gdb/testsuite/gdb.cp/misc.exp |  8 ++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index 17af1b51df..0425c59f8d 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1248,6 +1248,20 @@ skip_undetermined_arglist (int nargs, struct expression *exp, int *pos,
     evaluate_subexp (NULL_TYPE, exp, pos, noside);
 }
 
+/* Return true if type is integral or reference to integral */
+
+static bool
+is_integral_or_integral_reference (struct type *type)
+{
+  if (is_integral_type (type))
+    return true;
+
+  type = check_typedef (type);
+  return type != nullptr
+    && TYPE_IS_REFERENCE (type)
+    && is_integral_type (TYPE_TARGET_TYPE (type));
+}
+
 struct value *
 evaluate_subexp_standard (struct type *expect_type,
 			  struct expression *exp, int *pos,
@@ -2208,10 +2222,10 @@ evaluate_subexp_standard (struct type *expect_type,
       if (binop_user_defined_p (op, arg1, arg2))
 	return value_x_binop (arg1, arg2, op, OP_NULL, noside);
       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
-	       && is_integral_type (value_type (arg2)))
+	       && is_integral_or_integral_reference (value_type (arg2)))
 	return value_ptradd (arg1, value_as_long (arg2));
       else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
-	       && is_integral_type (value_type (arg1)))
+	       && is_integral_or_integral_reference (value_type (arg1)))
 	return value_ptradd (arg2, value_as_long (arg1));
       else
 	{
@@ -2234,7 +2248,7 @@ evaluate_subexp_standard (struct type *expect_type,
 	  return value_from_longest (type, value_ptrdiff (arg1, arg2));
 	}
       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
-	       && is_integral_type (value_type (arg2)))
+	       && is_integral_or_integral_reference (value_type (arg2)))
 	return value_ptradd (arg1, - value_as_long (arg2));
       else
 	{
diff --git a/gdb/testsuite/gdb.cp/misc.cc b/gdb/testsuite/gdb.cp/misc.cc
index d461d6de61..41fb9d2f2d 100644
--- a/gdb/testsuite/gdb.cp/misc.cc
+++ b/gdb/testsuite/gdb.cp/misc.cc
@@ -24,6 +24,9 @@ bool            v_bool_array[2];
 typedef struct fleep fleep;
 struct fleep { int a; } s;
 
+int number;
+int &number_ref = number;
+
 // ====================== simple class structures  =======================
 
 struct default_public_struct {
diff --git a/gdb/testsuite/gdb.cp/misc.exp b/gdb/testsuite/gdb.cp/misc.exp
index bceb73ef87..cd6f0f7070 100644
--- a/gdb/testsuite/gdb.cp/misc.exp
+++ b/gdb/testsuite/gdb.cp/misc.exp
@@ -110,3 +110,11 @@ gdb_test "print (int)false" "\\$\[0-9\]* = 0" "(int)false"
 
 gdb_test "print 'misc.cc'::v_bool" " = true" \
     "expression using block qualifier"
+
+# pointer arithmetic
+gdb_test "print *(v_bool_array + number_ref)" "\\$\[0-9\]* = false" \
+    "pointer addition with integer reference"
+gdb_test "print *(number_ref + v_bool_array)" "\\$\[0-9\]* = false" \
+    "pointer addition with integer reference"
+gdb_test "print *(v_bool_array - number_ref)" "\\$\[0-9\]* = false" \
+    "pointer subtraction with integer reference"
-- 
2.26.0



       reply	other threads:[~2020-03-31 22:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200331220655.4436-1-ssbssa.ref@yahoo.de>
2020-03-31 22:06 ` Hannes Domani [this message]
2020-04-01 16:36   ` Tom Tromey
2020-04-01 17:17     ` Hannes Domani
2020-04-01 17:36       ` Hannes Domani
2020-04-01 18:06         ` Tom Tromey
     [not found]         ` <87sghn2i0g.fsf@tromey.com>
2020-04-01 18:14           ` Hannes Domani
2020-04-01 18:55             ` Tom Tromey

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=20200331220655.4436-1-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --cc=gdb-patches@sourceware.org \
    /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