From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 337513858D38; Fri, 30 Sep 2022 03:47:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 337513858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664509678; bh=xAiRqy7PLItqs3G62cnjpbCexNQsLbWqxHJP3FJKPbA=; h=From:To:Subject:Date:From; b=puH929rNjzZkkM/aeKX5meJPRGTvaT+JD6uffKawUheY5SD8dq65NcWxwGYLLtJw4 XRI9FrOjgebNIEf1wWnkGTvewSzMlp0XiPQ4klscdImjYcJB8rbbiCOp62tJcrECts R3RRl0aVpjiBnWSWpSvhUa9/37ZfacVTac26B1JM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/c++] Print destructor the same for gcc and clang X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 4eeb0013059856b8660b4a0351589b096167b4d1 X-Git-Newrev: 137c886e9a624f68cba212ebdb156298957c25c4 Message-Id: <20220930034758.337513858D38@sourceware.org> Date: Fri, 30 Sep 2022 03:47:58 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D137c886e9a62= 4f68cba212ebdb156298957c25c4 commit 137c886e9a624f68cba212ebdb156298957c25c4 Author: Tom de Vries Date: Fri Sep 30 05:47:54 2022 +0200 [gdb/c++] Print destructor the same for gcc and clang =20 Consider the test-case contained in this patch. =20 With g++ (7.5.0) we have for "ptype A": ... type =3D class A { public: int a; =20 A(void); ~A(); } ... and with clang++ (13.0.1): ... type =3D class A { public: int a; =20 A(void); ~A(void); } ... and we observe that the destructor is printed differently. =20 There's a difference in debug info between the two cases: in the clang = case, there's one artificial parameter, but in the g++ case, there are two, a= nd these similar cases are handled differently in cp_type_print_method_arg= s. =20 This is due to this slightly convoluted bit of code: ... i =3D staticp ? 0 : 1; if (nargs > i) { while (i < nargs) ... } else if (varargs) gdb_printf (stream, "..."); else if (language =3D=3D language_cplus) gdb_printf (stream, "void"); ... =20 The purpose of "i =3D staticp ? 0 : 1" is to skip the printing of the i= mplicit this parameter. =20 In commit 5f4d1085085 ("c++/8218: Destructors w/arguments"), skipping o= f other artificial parameters was added, but using a different method: rather t= han adjusting the potential loop start, it skips the parameter in the loop. =20 The observed difference in printing is explained by whether we enter th= e loop: - in the clang case, the loop is not entered and we print "void". - in the gcc case, the loop is entered, and nothing is printed. =20 Fix this by rewriting the code to: - always enter the loop - handle whether arguments need printing in the loop - keep track of how many arguments are printed, and use that after the loop to print void etc. such that we have the same for both gcc and clang: ... A(void); ~A(void); ... =20 Note that I consider the discussion of whether we want to print: - A(void) / ~A(void), or - A() / ~A() out-of-scope for this patch. =20 Tested on x86_64-linux. Diff: --- gdb/c-typeprint.c | 55 +++++++++++++++++---------= ---- gdb/testsuite/gdb.base/ptype-offsets.exp | 2 +- gdb/testsuite/gdb.cp/print-method-args.cc | 38 +++++++++++++++++++++ gdb/testsuite/gdb.cp/print-method-args.exp | 38 +++++++++++++++++++++ gdb/testsuite/gdb.cp/templates.exp | 4 +-- 5 files changed, 111 insertions(+), 26 deletions(-) diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c index 3a611cdac5d..23e8a5a4d6f 100644 --- a/gdb/c-typeprint.c +++ b/gdb/c-typeprint.c @@ -273,35 +273,44 @@ cp_type_print_method_args (struct type *mtype, const = char *prefix, language_cplus, DMGL_ANSI); gdb_puts ("(", stream); =20 - /* Skip the class variable. We keep this here to accommodate older - compilers and debug formats which may not support artificial - parameters. */ - i =3D staticp ? 0 : 1; - if (nargs > i) + int printed_args =3D 0; + for (i =3D 0; i < nargs; ++i) { - while (i < nargs) + if (i =3D=3D 0 && !staticp) { - struct field arg =3D args[i++]; - - /* Skip any artificial arguments. */ - if (FIELD_ARTIFICIAL (arg)) - continue; + /* Skip the class variable. We keep this here to accommodate older + compilers and debug formats which may not support artificial + parameters. */ + continue; + } =20 - c_print_type (arg.type (), "", stream, 0, 0, language, flags); + struct field arg =3D args[i]; + /* Skip any artificial arguments. */ + if (FIELD_ARTIFICIAL (arg)) + continue; =20 - if (i =3D=3D nargs && varargs) - gdb_printf (stream, ", ..."); - else if (i < nargs) - { - gdb_printf (stream, ", "); - stream->wrap_here (8); - } + if (printed_args > 0) + { + gdb_printf (stream, ", "); + stream->wrap_here (8); } + + c_print_type (arg.type (), "", stream, 0, 0, language, flags); + printed_args++; + } + + if (varargs) + { + if (printed_args =3D=3D 0) + gdb_printf (stream, "..."); + else + gdb_printf (stream, ", ..."); + } + else if (printed_args =3D=3D 0) + { + if (language =3D=3D language_cplus) + gdb_printf (stream, "void"); } - else if (varargs) - gdb_printf (stream, "..."); - else if (language =3D=3D language_cplus) - gdb_printf (stream, "void"); =20 gdb_printf (stream, ")"); =20 diff --git a/gdb/testsuite/gdb.base/ptype-offsets.exp b/gdb/testsuite/gdb.b= ase/ptype-offsets.exp index e80d876fa32..eb41bafb3e6 100644 --- a/gdb/testsuite/gdb.base/ptype-offsets.exp +++ b/gdb/testsuite/gdb.base/ptype-offsets.exp @@ -104,7 +104,7 @@ gdb_test "ptype /oTM struct abc" \ "/* 48 | 2 */ my_int_type field9;" \ "" \ " abc(void);" \ -" ~abc();" \ +" ~abc(void);" \ "" \ " typedef short my_int_type;" \ "/* XXX 6-byte padding */" \ diff --git a/gdb/testsuite/gdb.cp/print-method-args.cc b/gdb/testsuite/gdb.= cp/print-method-args.cc new file mode 100644 index 00000000000..98be8286aeb --- /dev/null +++ b/gdb/testsuite/gdb.cp/print-method-args.cc @@ -0,0 +1,38 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . = */ + +class A { +public: + int a; + + A() { + this->a =3D 3; + } + ~A() { + a =3D 0; + } +}; + +int +main (void) +{ + A a; + + return a.a; +} diff --git a/gdb/testsuite/gdb.cp/print-method-args.exp b/gdb/testsuite/gdb= .cp/print-method-args.exp new file mode 100644 index 00000000000..7773e041e21 --- /dev/null +++ b/gdb/testsuite/gdb.cp/print-method-args.exp @@ -0,0 +1,38 @@ +# Copyright (C) 2022 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 . + +# This file is part of the gdb testsuite. + +# This test checks that a constructor and destructor are printed the same. + +if { [skip_cplus_tests] } { continue } + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++= }]} { + return -1 +} + +set re \ + [multi_line \ + "type =3D class A {" \ + " public:" \ + " int a;" \ + "" \ + " A\\(void\\);" \ + " ~A\\(void\\);" \ + "}"] + +gdb_test "ptype A" $re diff --git a/gdb/testsuite/gdb.cp/templates.exp b/gdb/testsuite/gdb.cp/temp= lates.exp index 27c9bbc1c7e..d3ede5a5fb6 100644 --- a/gdb/testsuite/gdb.cp/templates.exp +++ b/gdb/testsuite/gdb.cp/templates.exp @@ -68,7 +68,7 @@ proc test_ptype_of_templates {} { # The destructor has an argument type. kfail "gdb/8218" "ptype T5" } - -re "type =3D class T5 \{${ws}public:${ws}static int X;${ws}int x;${= ws}int val;${ws}T5\\(int\\);${ws}T5\\((T5 const|const T5) ?&\\);$= {ws}~T5\\(\\);${ws}static void \\* operator new\\((size_t|unsigned( int| lo= ng|))\\);${ws}static void operator delete\\(void ?\\*\\);${ws}int value\\((= void|)\\);${ws}\}\r\n$gdb_prompt $" { + -re "type =3D class T5 \{${ws}public:${ws}static int X;${ws}int x;${= ws}int val;${ws}T5\\(int\\);${ws}T5\\((T5 const|const T5) ?&\\);$= {ws}~T5\\(void\\);${ws}static void \\* operator new\\((size_t|unsigned( int= | long|))\\);${ws}static void operator delete\\(void ?\\*\\);${ws}int value= \\((void|)\\);${ws}\}\r\n$gdb_prompt $" { pass "ptype T5" } } @@ -108,7 +108,7 @@ proc test_ptype_of_templates {} { # The destructor has an argument type. kfail "gdb/8218" "ptype t5i" } - -re "type =3D class T5 \{${ws}public:${ws}static int X;${ws}int x;${= ws}int val;${ws}T5\\(int\\);${ws}T5\\((T5 const|const T5) ?&\\);$= {ws}~T5\\(\\);${ws}static void \\* operator new\\((size_t|unsigned( int| lo= ng|))\\);${ws}static void operator delete\\(void ?\\*\\);${ws}int value\\((= void|)\\);${ws}\}\r\n$gdb_prompt $" { + -re "type =3D class T5 \{${ws}public:${ws}static int X;${ws}int x;${= ws}int val;${ws}T5\\(int\\);${ws}T5\\((T5 const|const T5) ?&\\);$= {ws}~T5\\(void\\);${ws}static void \\* operator new\\((size_t|unsigned( int= | long|))\\);${ws}static void operator delete\\(void ?\\*\\);${ws}int value= \\((void|)\\);${ws}\}\r\n$gdb_prompt $" { pass "ptype t5i" } }