From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sonic307-7.consmr.mail.ir2.yahoo.com (sonic307-7.consmr.mail.ir2.yahoo.com [87.248.110.32]) by sourceware.org (Postfix) with ESMTPS id EB9DF385BF83 for ; Tue, 31 Mar 2020 22:07:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EB9DF385BF83 X-YMail-OSG: Tk5KFvUVM1mSxtLtwJzfJyJpzdMEU4LQPOitMptN9vptmeYaeML6jCUFec6HsR_ 6VhfRdFzFv_1PURNKnaZa._qlGIVGnm5CATxZZLLnRMBDFA03qvjnOUGvlPgjPEmBfzGf8NBvXMS i8X8F3PNfNRpVboKpbDuVpvWYaiInE9J_kNS7JTjuSnly2LEJdaHTMPOZRWIemmjO6Wobn5fenfw FDSKuP.Gz5kQiZQgGwgIyTS9u_5Q3lB6AeCGlu9O5iBpUnUkr29xmCdn0r.WIG8fcuGeDPz5emPy vZBb64vQKIoS8Id.TLy05SCkaqkPHQC7BY5QLfvDdYDb.Yk2I5_rTiVXDr1XS7isJ3WrepLU60tc BLhnaetpJFQZ8Ct9C4r0RkAj7Ct515HSGZk8Vw39mIF4tGwGc2VyYj0hIK7ytD2VXQN4XG0Xu7gc i7rqD8X6w3MW0DPKpVD9p8t50j4xKzKayXFdAp6JxWIWo3rwvHEN2MePeyYjhfCzmJ5tNpW180R4 iSTCDyzjjhNM1aXr9SOhhDOmv5A7Q1ses.vdyVH9c0zO_nc5Xhx1bzCrEX583LD1XCbmq336fyZC qKRYcPWCHIgasPTXuqQzWnUczeG4kMVSS6KKZhn5Tia1ysqqybABhqNaIIYKCHjNYV2F5kKFrCNz 3yTzunRSBIZEaP8RvAf_Bs79py9tb7kpblJ9bTs_ExWvSyPobWo6qi6x1hYp5NXWUMT5FSY5fIqH Oy69Qngd1BIi3dGqJEqN2_wtHvcdk0.ggMrGTHCDQPv2UM1PzZ5JXVoR_Qk1TpDPdW2s.UdAOaBP l6IAUH0.M8TnvwvcOTeAEqj8A1SvSDTQdw.168VdiifxBlXcNEmwXgOhFfC2qkyEulDgn.jQcOjH t_khivG.6KW3rVt2mGxkk1mqHiADAfl9ddIffZooPNZ6lKmiPb7XxWY65oOVeMJeUOeoMNColxCi ZRqrE22by3oTxuehYLoz7kisVctEAHA8WudBx3L.o2tMXZWdNxctgmznFUNPiaaEKzxKe8CH9Fa7 fZLxCrTZS4rt.p60JYyR1c0BAa0r_ttmiusQtGXmsxFhxkWpGf0uMdSII5giGAT7QL8aEEMgcnex 1K0oilIA4HxRsszWeDEdNMCV3SMdHXvViuWlEwhFtNdr2cLqCb5gw_GFiEg584S8cFouiIy3fnf0 SAGWYFbU35AzO5tpdf9MEHvUm17p2_xQ4OCLSGWzVlYAEC4r.qU5LrlBmd71xwE.1ALd2Ulh.KUF F39BQLbxQqJfYt0NRgeK_ba5B1sDZePXvZJdObn4n4MOZw_8tMHkZuSjUbjHuajdZ3pfdKQwFWd1 CK3IdjJViLEIGTk9By.Jl3fHaRJj5n_CfAPnrTnhhUiGgGuLrU8egQK4_0otOie5N7g5dlOCXAye Q81iXlXIQYncL3Di2.Pf6cqKv1RZ94A-- Received: from sonic.gate.mail.ne1.yahoo.com by sonic307.consmr.mail.ir2.yahoo.com with HTTP; Tue, 31 Mar 2020 22:07:14 +0000 Received: by smtp402.mail.ir2.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 784d643f6750ea8e4e3ac153b7083822; Tue, 31 Mar 2020 22:07:13 +0000 (UTC) From: Hannes Domani 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 Message-Id: <20200331220655.4436-1-ssbssa@yahoo.de> X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Antivirus: Avast (VPS 200330-0, 03/30/2020), Outbound message X-Antivirus-Status: Clean References: <20200331220655.4436-1-ssbssa.ref@yahoo.de> X-Spam-Status: No, score=-21.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2020 22:07:17 -0000 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 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 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