diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 86faab1a56..2ea60bb476 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2020-05-30 Pedro Alves + + * cp-support.c (replace_typedefs_template): New. + (replace_typedefs_qualified_name): Handle + DEMANGLE_COMPONENT_TEMPLATE. + 2020-05-29 Simon Marchi * dwarf2/comp-unit.c, dwarf2/comp-unit.h, dwarf2/index-cache.c, diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 1e54aaea3b..11e54c272c 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -294,6 +294,42 @@ inspect_type (struct demangle_parse_info *info, return 0; } +/* Helper for replace_typedefs_qualified_name to handle + DEMANGLE_COMPONENT_TEMPLATE. TMPL is the template node. BUF is + the buffer that holds the qualified name being built by + replace_typedefs_qualified_name. REPL is the node that will be + rewritten as a DEMANGLE_COMPONENT_NAME node holding the 'template + plus template arguments' name with typedefs replaced. */ + +static bool +replace_typedefs_template (struct demangle_parse_info *info, + string_file &buf, + struct demangle_component *tmpl, + struct demangle_component *repl, + canonicalization_ftype *finder, + void *data) +{ + demangle_component *tmpl_arglist = d_right (tmpl); + + /* Replace typedefs in the template argument list. */ + replace_typedefs (info, tmpl_arglist, finder, data); + + /* Convert 'template + replaced template argument list' to a string + and replace the REPL node. */ + gdb::unique_xmalloc_ptr tmpl_str = cp_comp_to_string (tmpl, 100); + if (tmpl_str == nullptr) + { + /* If something went astray, abort typedef substitutions. */ + return false; + } + buf.puts (tmpl_str.get ()); + + repl->type = DEMANGLE_COMPONENT_NAME; + repl->u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); + repl->u.s_name.len = buf.size (); + return true; +} + /* Replace any typedefs appearing in the qualified name (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse given in INFO. */ @@ -314,6 +350,29 @@ replace_typedefs_qualified_name (struct demangle_parse_info *info, substituted name. */ while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME) { + if (d_left (comp)->type == DEMANGLE_COMPONENT_TEMPLATE) + { + /* Convert 'template + replaced template argument list' to a + string and replace the top DEMANGLE_COMPONENT_QUAL_NAME + node. */ + if (!replace_typedefs_template (info, buf, + d_left (comp), d_left (ret_comp), + finder, data)) + return; + + buf.clear (); + d_right (ret_comp) = d_right (comp); + comp = ret_comp; + + /* Fallback to DEMANGLE_COMPONENT_NAME processing. We want + to call inspect_type for this template, in case we have a + template alias, like: + template using alias = base; + in which case we want inspect_type to do a replacement like: + alias -> base + */ + } + if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME) { struct demangle_component newobj; @@ -370,11 +429,20 @@ replace_typedefs_qualified_name (struct demangle_parse_info *info, comp = d_right (comp); } - /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified - name assembled above and append the name given by COMP. Then use this - reassembled name to check for a typedef. */ + /* If the next component is DEMANGLE_COMPONENT_TEMPLATE or + DEMANGLE_COMPONENT_NAME, save the qualified name assembled above + and append the name given by COMP. Then use this reassembled + name to check for a typedef. */ - if (comp->type == DEMANGLE_COMPONENT_NAME) + if (comp->type == DEMANGLE_COMPONENT_TEMPLATE) + { + /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node with a + DEMANGLE_COMPONENT_NAME node containing the whole name. */ + if (!replace_typedefs_template (info, buf, comp, ret_comp, finder, data)) + return; + inspect_type (info, ret_comp, finder, data); + } + else if (comp->type == DEMANGLE_COMPONENT_NAME) { buf.write (comp->u.s_name.s, comp->u.s_name.len); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index d4e7220b32..8c5553ee14 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-05-30 Pedro Alves + + * gdb.linespec/cp-replace-typedefs-ns-template.cc: New. + * gdb.linespec/cp-replace-typedefs-ns-template.exp: New. + 2020-05-29 Gary Benson * gdb.compile/compile-cplus.exp (additional_flags): Also diff --git a/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.cc b/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.cc new file mode 100644 index 0000000000..fb30685f2a --- /dev/null +++ b/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.cc @@ -0,0 +1,101 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2019-2020 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 . */ + +namespace NS1 { + +namespace NS2 { + +struct object +{ + object () + { + } +}; + +typedef object *object_p; + +template +struct Templ1 +{ + explicit Templ1 (object_p) + { + } + + template + static void + static_method (object_p) + { + } +}; + +template +struct Templ2 +{ + explicit Templ2 (object_p) + { + } + + template + static void + static_method (object_p) + { + } +}; + +template using AliasTempl = Templ2; + +typedef Templ1 int_Templ1_t; + +void +object_p_func (object_p) +{ +} + +void +int_Templ1_t_func (int_Templ1_t *) +{ +} + +} // namespace NS2 + +} // namespace NS1 + +/* These typedefs have the same name as some of the components within + NS1 that they alias to, on purpose, to try to confuse GDB and cause + recursion. */ +using NS2 = int; +using object = NS1::NS2::object; +using Templ1 = NS1::NS2::Templ1; +using Templ2 = NS1::NS2::Templ2; +using AliasTempl = NS1::NS2::AliasTempl; + +NS2 ns2_int = 0; +object obj; +Templ1 templ1 (0); +NS1::NS2::int_Templ1_t int_templ1 (0); +AliasTempl alias (0); + +int +main () +{ + NS1::NS2::Templ1::static_method (0); + NS1::NS2::AliasTempl::static_method (0); + NS1::NS2::object_p_func (0); + NS1::NS2::int_Templ1_t_func (0); + + return 0; +} diff --git a/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.exp b/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.exp new file mode 100644 index 0000000000..590b06d34f --- /dev/null +++ b/gdb/testsuite/gdb.linespec/cp-replace-typedefs-ns-template.exp @@ -0,0 +1,121 @@ +# Copyright 2020 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 tests GDB's ability to replace typedefs in C++ symbols +# when setting breakpoints, particularly around templates in +# namespaces. + +load_lib completion-support.exp + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile \ + {debug c++ additional_flags=-std=c++11}]} { + return -1 +} + +# Disable the completion limit for the whole testcase. +gdb_test_no_output "set max-completions unlimited" + +# Confirm that the important global namespace typedefs were indeed +# emited in the debug info. +gdb_test "ptype NS2" "type = int" +gdb_test "ptype object" "type = struct NS1::NS2::object {.*" +gdb_test "ptype Templ1" "type = struct NS1::NS2::Templ1 .*" +gdb_test "ptype AliasTempl" "type = struct NS1::NS2::Templ2 .*" + +# Wrapper around check_bp_locations_match_list that expect a single +# location in the set breakpoint, instead of a list of locations. If +# the set location isn't specified, then it is assumed to be the exact +# same as the input location. +proc check_bp {location_in {location_out ""}} { + if {$location_out == ""} { + set location_out $location_in + } + check_bp_locations_match_list "b $location_in" [list $location_out] +} + +# These used to crash GDB with infinite recursion because GDB would +# confuse the "Templ1" typedef in the global namespace with the "Templ1" +# template in within NS1::NS2. +test_gdb_complete_unique \ + "break NS1::NS2::Templ1::Tem" \ + "break NS1::NS2::Templ1::Templ1(NS1::NS2::object*)" +check_bp "NS1::NS2::Templ1::Templ1(NS1::NS2::object*)" + +# Similar test, but without a template. This would not crash. +test_gdb_complete_unique \ + "break NS1::NS2::object::obj" \ + "break NS1::NS2::object::object()" +check_bp "NS1::NS2::object::object()" + +# Test some non-template typedef replacing within a namespace. +test_gdb_complete_unique \ + "break NS1::NS2::object_p_f" \ + "break NS1::NS2::object_p_func(NS1::NS2::object*)" +check_bp \ + "NS1::NS2::object_p_func(NS1::NS2::object_p)" \ + "NS1::NS2::object_p_func(NS1::NS2::object*)" + +# Make sure the "NS2" in the template argument list is resolved as +# being a global typedef for int. +foreach loc { + "NS1::NS2::Templ1::static_method(NS1::NS2::object*)" + "NS1::NS2::Templ1::static_method(NS1::NS2::object*)" + "NS1::NS2::Templ1::static_method(NS1::NS2::object*)" + "NS1::NS2::Templ1::static_method(NS1::NS2::object*)" +} { + check_bp $loc "NS1::NS2::Templ1::static_method(NS1::NS2::object*)" +} + +foreach loc { + "NS1::NS2::Templ2::static_method(NS1::NS2::object*)" + "NS1::NS2::Templ2::static_method(NS1::NS2::object_p)" +} { + check_bp $loc "NS1::NS2::Templ2::static_method(NS1::NS2::object*)" +} + +# Check that GDB expands the "NS1::NS2::AliasTempl" as +# "NS1::NS2::Templ2". +foreach loc { + "NS1::NS2::AliasTempl::static_method(NS1::NS2::object*)" + "NS1::NS2::AliasTempl::static_method(NS1::NS2::object_p)" +} { + if [test_compiler_info gcc*] { + # While Clang emits "AliasTempl" (etc.) typedefs, GCC + # emits "AliasTempl" typedefs with no template parameter info. + setup_xfail gcc/95437 *-*-* + } + check_bp $loc "NS1::NS2::Templ2::static_method(NS1::NS2::object*)" + + # Check that setting the breakpoint with GCC really failed, + # instead of succeeding with e.g., "AliasTempl" preserved in + # the location text. If that ever happens, we'll need to update + # these tests. + if [test_compiler_info gcc*] { + check_setting_bp_fails "b $loc" + } +} + +# Check typedef substitution in a template in a qualified name in a +# function parameter list. These used to crash GDB with recursion +# around "Templ1", because there's a "Templ1" typedef in the global +# namespace. +foreach loc { + "NS1::NS2::int_Templ1_t_func(NS1::NS2::int_Templ1_t*)" + "NS1::NS2::int_Templ1_t_func(NS1::NS2::Templ1*)" +} { + check_bp $loc "NS1::NS2::int_Templ1_t_func(NS1::NS2::Templ1*)" +}