From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 19BDE3858C3A; Tue, 17 Jan 2023 14:05:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 19BDE3858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673964359; bh=ZETnBsxMapyY0C/sPsKszeS1iVV1PG6mkOhcH8tg1oc=; h=From:To:Subject:Date:From; b=cwFtOMZ87qj/x0LK7UWIBT0vLSXG5QyXv1oisD1i/IYpKpF+Ty4uC/HAZLHjHp95i 65gw7OvYszrQjD+GvzlV9nNjjAUhxS8BRp+M5sIffzv0NR0gxo6iyCMMtmV3j1cTY5 YSkRijjabUIZ3+EVpyaPlQOtjT6IU8jCZKUs79CY= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Fix parameter-less template regression in new DWARF reader X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 5a89072f36ddd3be71103e3806d42ff5e49ff616 X-Git-Newrev: ac37b79cc440e37fc704d425a6e450afb3c7ee89 Message-Id: <20230117140559.19BDE3858C3A@sourceware.org> Date: Tue, 17 Jan 2023 14:05:59 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dac37b79cc440= e37fc704d425a6e450afb3c7ee89 commit ac37b79cc440e37fc704d425a6e450afb3c7ee89 Author: Tom Tromey Date: Wed Dec 14 14:37:41 2022 -0700 Fix parameter-less template regression in new DWARF reader =20 PR c++/29896 points out a regression in the new DWARF reader. It does not properly handle a case like "break fn", where "fn" is a template function. =20 This happens because the new index uses strncasecmp to compare. However, to make this work correctly, we need a custom function that ignores template parameters. =20 This patch adds a custom comparison function and fixes the bug. A new test case is included. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29896 Diff: --- gdb/dwarf2/cooked-index.c | 143 ++++++++++++++++++++++++++++++++-= ---- gdb/dwarf2/cooked-index.h | 15 ++-- gdb/dwarf2/read.c | 3 +- gdb/testsuite/gdb.cp/paramless.cc | 46 ++++++++++++ gdb/testsuite/gdb.cp/paramless.exp | 41 +++++++++++ 5 files changed, 226 insertions(+), 22 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index c711e3b9b2a..09b3fd70b26 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -25,6 +25,114 @@ #include "ada-lang.h" #include "split-name.h" #include +#include "safe-ctype.h" +#include "gdbsupport/selftest.h" + +/* See cooked-index.h. */ + +bool +cooked_index_entry::compare (const char *stra, const char *strb, + bool completing) +{ + /* If we've ever matched "<" in both strings, then we disable the + special template parameter handling. */ + bool seen_lt =3D false; + + while (*stra !=3D '\0' + && *strb !=3D '\0' + && (TOLOWER ((unsigned char) *stra) + =3D=3D TOLOWER ((unsigned char ) *strb))) + { + if (*stra =3D=3D '<') + seen_lt =3D true; + ++stra; + ++strb; + } + + unsigned c1 =3D TOLOWER ((unsigned char) *stra); + unsigned c2 =3D TOLOWER ((unsigned char) *strb); + + if (completing) + { + /* When completing, if one string ends earlier than the other, + consider them as equal. Also, completion mode ignores the + special '<' handling. */ + if (c1 =3D=3D '\0' || c2 =3D=3D '\0') + return false; + /* Fall through to the generic case. */ + } + else if (seen_lt) + { + /* Fall through to the generic case. */ + } + else if (c1 =3D=3D '\0' || c1 =3D=3D '<') + { + /* Maybe they both end at the same spot. */ + if (c2 =3D=3D '\0' || c2 =3D=3D '<') + return false; + /* First string ended earlier. */ + return true; + } + else if (c2 =3D=3D '\0' || c2 =3D=3D '<') + { + /* Second string ended earlier. */ + return false; + } + + return c1 < c2; +} + +#if GDB_SELF_TEST + +namespace { + +void +test_compare () +{ + SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", false)); + SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", false)); + SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", true)); + SELF_CHECK (!cooked_index_entry::compare ("abcd", "abcd", true)); + + SELF_CHECK (cooked_index_entry::compare ("abcd", "ABCDE", false)); + SELF_CHECK (!cooked_index_entry::compare ("ABCDE", "abcd", false)); + SELF_CHECK (!cooked_index_entry::compare ("abcd", "ABCDE", true)); + SELF_CHECK (!cooked_index_entry::compare ("ABCDE", "abcd", true)); + + SELF_CHECK (!cooked_index_entry::compare ("name", "name<>", false)); + SELF_CHECK (!cooked_index_entry::compare ("name<>", "name", false)); + SELF_CHECK (!cooked_index_entry::compare ("name", "name<>", true)); + SELF_CHECK (!cooked_index_entry::compare ("name<>", "name", true)); + + SELF_CHECK (!cooked_index_entry::compare ("name", "name", fals= e)); + SELF_CHECK (!cooked_index_entry::compare ("name", "name", fals= e)); + SELF_CHECK (!cooked_index_entry::compare ("name", "name", true= )); + SELF_CHECK (!cooked_index_entry::compare ("name", "name", true)= ); + + SELF_CHECK (!cooked_index_entry::compare ("name>", + "name>", false)); + + SELF_CHECK (!cooked_index_entry::compare ("name", "name>", fal= se)); + SELF_CHECK (!cooked_index_entry::compare ("name>", "name", fal= se)); + SELF_CHECK (cooked_index_entry::compare ("name>", + false)); + SELF_CHECK (!cooked_index_entry::compare ("name>", + true)); + SELF_CHECK (!cooked_index_entry::compare ("name>", "name>", "namecanonical, n.data (), n.length ()); - if (cmp !=3D 0 || completing) - return cmp < 0; - return strlen (entry->canonical) < n.length (); + return cooked_index_entry::compare (entry->canonical, n.c_str (), + completing); }); =20 - auto upper =3D std::upper_bound (m_entries.begin (), m_entries.end (), - name, - [=3D] (const gdb::string_view &n, + auto upper =3D std::upper_bound (m_entries.begin (), m_entries.end (), n= ame, + [=3D] (const std::string &n, const cooked_index_entry *entry) { - int cmp =3D strncasecmp (n.data (), entry->canonical, n.length ()); - if (cmp !=3D 0 || completing) - return cmp < 0; - return n.length () < strlen (entry->canonical); + return cooked_index_entry::compare (n.c_str (), entry->canonical, + completing); }); =20 return range (lower, upper); @@ -311,7 +413,7 @@ cooked_index_vector::get_addrmaps () /* See cooked-index.h. */ =20 cooked_index_vector::range -cooked_index_vector::find (gdb::string_view name, bool completing) +cooked_index_vector::find (const std::string &name, bool completing) { std::vector result_range; result_range.reserve (m_vector.size ()); @@ -339,3 +441,12 @@ cooked_index_vector::get_main () const =20 return result; } + +void _initialize_cooked_index (); +void +_initialize_cooked_index () +{ +#if GDB_SELF_TEST + selftests::register_test ("cooked_index_entry::compare", test_compare); +#endif +} diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index 607e07745f9..2b34a6534e8 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -143,11 +143,16 @@ struct cooked_index_entry : public allocate_on_obstack STORAGE. */ const char *full_name (struct obstack *storage) const; =20 - /* Entries must be sorted case-insensitively; this compares two - entries. */ + /* Compare two strings, case-insensitively. Return true if STRA is + less than STRB. If one string has template parameters, but the + other does not, then they are considered to be equal; so for + example "t" =3D=3D "t", "t" < "t", but "t" =3D=3D "t".= */ + static bool compare (const char *stra, const char *strb, bool completing= ); + + /* Compare two entries by canonical name. */ bool operator< (const cooked_index_entry &other) const { - return strcasecmp (canonical, other.canonical) < 0; + return compare (canonical, other.canonical, false); } =20 /* The name as it appears in DWARF. This always points into one of @@ -232,7 +237,7 @@ public: /* Look up an entry by name. Returns a range of all matching results. If COMPLETING is true, then a larger range, suitable for completion, will be returned. */ - range find (gdb::string_view name, bool completing); + range find (const std::string &name, bool completing); =20 private: =20 @@ -335,7 +340,7 @@ public: /* Look up an entry by name. Returns a range of all matching results. If COMPLETING is true, then a larger range, suitable for completion, will be returned. */ - range find (gdb::string_view name, bool completing); + range find (const std::string &name, bool completing); =20 /* Return a range of all the entries. */ range all_entries () diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index c3f246fedf7..44b54f77de9 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -18741,8 +18741,9 @@ cooked_index_functions::expand_symtabs_matching { std::vector name_vec =3D lookup_name_without_params.split_name (lang); + std::string last_name =3D gdb::to_string (name_vec.back ()); =20 - for (const cooked_index_entry *entry : table->find (name_vec.back (), + for (const cooked_index_entry *entry : table->find (last_name, completing)) { QUIT; diff --git a/gdb/testsuite/gdb.cp/paramless.cc b/gdb/testsuite/gdb.cp/param= less.cc new file mode 100644 index 00000000000..82924391c24 --- /dev/null +++ b/gdb/testsuite/gdb.cp/paramless.cc @@ -0,0 +1,46 @@ +/* Test case for template breakpoint test. + + Copyright 2022-2023 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 . = */ + +template +struct outer +{ + template + int fn (int x) + { + return x + 23; + } +}; + +template +int toplev (int y) +{ + return y; +} + +outer outer1; +outer outer2; + +int main () +{ + int x1 =3D outer1.fn (0); + int x2 =3D outer2.fn (-46); + int x3 =3D toplev (0); + int x4 =3D toplev (0); + return x1 + x2; +} diff --git a/gdb/testsuite/gdb.cp/paramless.exp b/gdb/testsuite/gdb.cp/para= mless.exp new file mode 100644 index 00000000000..79529de49f5 --- /dev/null +++ b/gdb/testsuite/gdb.cp/paramless.exp @@ -0,0 +1,41 @@ +# Copyright 2022-2023 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. + +# Test template breakpoints without parameters. + +if { [skip_cplus_tests] } { continue } + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++= }]} { + return -1 +} + +gdb_breakpoint "outer::fn" message +delete_breakpoints + +gdb_breakpoint "outer::fn" message +delete_breakpoints + +gdb_test "break outer::fn" "Breakpoint $decimal at .*2 locations." +delete_breakpoints + +gdb_test "break toplev" "Breakpoint $decimal at .*2 locations." +delete_breakpoints + +gdb_breakpoint "toplev" message +delete_breakpoints