From patchwork Tue Jan 28 00:36:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 37583 Received: (qmail 31818 invoked by alias); 28 Jan 2020 00:37:15 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 31660 invoked by uid 89); 28 Jan 2020 00:37:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=tracker, HX-Received:f003, maxcompletions, max-completions X-HELO: mail-wm1-f42.google.com Received: from mail-wm1-f42.google.com (HELO mail-wm1-f42.google.com) (209.85.128.42) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 28 Jan 2020 00:37:11 +0000 Received: by mail-wm1-f42.google.com with SMTP id p9so637028wmc.2 for ; Mon, 27 Jan 2020 16:37:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=JmzG5YzVG0JxcXr71+yi4Tq6laSES0XQK917axDKyj4=; b=IOsilqEMWjJQ4Znx57orBOlagrxCFIlCjFBXQVQ2cG1q6m+pfyNUGhBd9FxbMmtkag fhcrdJs8lXghBsWisGmmcOXw+TnHhlzV9JwS0AvY0N3j6MN+yhhXlFXND9DlKElU3lPv Kn7k0TvTWPJfr06doO1jdzlqecCI7Y51b0135v84o4CHXhreG5GPyjNBX78WbJr+zaMl 2/SCVLMhBZOVRdorWeeV8OmWJe8ShbjaiOPGObu0MbcBG0jbd3TJ56Io1XsVdP22vght z7BMpo7S5Wx8FVy3WHdYO3qH6EpKD+WmUZg8kHKVBvTIxUlMg2s/yq+L5qfPTIUKWJBn pjIQ== Return-Path: Received: from localhost (host86-191-239-73.range86-191.btcentralplus.com. [86.191.239.73]) by smtp.gmail.com with ESMTPSA id b18sm21851693wru.50.2020.01.27.16.37.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 27 Jan 2020 16:37:07 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 3/3] gdb: Remove C++ symbol aliases from completion list Date: Tue, 28 Jan 2020 00:36:59 +0000 Message-Id: <4acf4e31571f81bdb0199d78ed7e1e93b41b5a31.1580171514.git.andrew.burgess@embecosm.com> In-Reply-To: References: In-Reply-To: References: X-IsSubscribed: yes Consider debugging the following C++ program: struct object { int a; }; typedef object *object_p; static int get_value (object_p obj) { return obj->a; } int main () { object obj; obj.a = 0; return get_value (&obj); } Now in a GDB session: (gdb) complete break get_value break get_value(object*) break get_value(object_p) Or: (gdb) break get_va (gdb) break get_value(object Function "get_value(object" not defined. Make breakpoint pending on future shared library load? (y or [n]) n The reason this happens is that we add completions based on the msymbol names and on the symbol names. For C++ both of these names include the parameter list, however, the msymbol names have some differences from the symbol names, for example: + typedefs are resolved, + whitespace rules are different around pointers, + the 'const' keyword is placed differently. What this means is that the msymbol names and symbol names appear to be completely different to GDB's completion tracker, and therefore to readline when it offers the completions. This commit builds on the previous commit which reworked the completion_tracker class. It is now trivial to add a remove_completion member function, this is then used along with cp_canonicalize_string_no_typedefs to remove the msymbol aliases from the completion tracker as we add the symbol names. Now, for the above program GDB only presents a single completion for 'get_value', which is 'get_value(object_p)'. It is still possible to reference the symbol using the msymbol name, so a user can manually type out 'break get_value (object *)' if they wish and will get the expected behaviour. I did consider adding an option to make this alias exclusion optional, in the end I didn't bother as I didn't think it would be very useful, but I can easily add such an option if people think it would be useful. gdb/ChangeLog: * completer.c (completion_tracker::remove_completion): Define new function. * completer.h (completion_tracker::remove_completion): Declare new function. * symtab.c (completion_list_add_symbol): Remove aliasing msymbols when adding a C++ function symbol. gdb/testsuite/ChangeLog: * gdb.linespec/cp-completion-aliases.cc: New file. * gdb.linespec/cp-completion-aliases.exp: New file. Change-Id: Ie5c7c9fc8ecf973072cfb4a9650867104bf7f50c --- gdb/ChangeLog | 9 +++ gdb/completer.c | 14 +++++ gdb/completer.h | 4 ++ gdb/symtab.c | 21 +++++++ gdb/testsuite/ChangeLog | 5 ++ .../gdb.linespec/cp-completion-aliases.cc | 73 ++++++++++++++++++++++ .../gdb.linespec/cp-completion-aliases.exp | 57 +++++++++++++++++ 7 files changed, 183 insertions(+) create mode 100644 gdb/testsuite/gdb.linespec/cp-completion-aliases.cc create mode 100644 gdb/testsuite/gdb.linespec/cp-completion-aliases.exp diff --git a/gdb/completer.c b/gdb/completer.c index 14c7a579970..67dce30fbe3 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -1678,6 +1678,20 @@ completion_tracker::add_completions (completion_list &&list) add_completion (std::move (candidate)); } +/* See completer.h. */ + +void +completion_tracker::remove_completion (const char *name) +{ + hashval_t hash = htab_hash_string (name); + if (htab_find_slot_with_hash (m_entries_hash, name, hash, NO_INSERT) + != NULL) + { + htab_remove_elt_with_hash (m_entries_hash, name, hash); + m_lowest_common_denominator_valid = false; + } +} + /* Helper for the make_completion_match_str overloads. Returns NULL as an indication that we want MATCH_NAME exactly. It is up to the caller to xstrdup that string if desired. */ diff --git a/gdb/completer.h b/gdb/completer.h index 7bfe0d58142..fd0d47b206b 100644 --- a/gdb/completer.h +++ b/gdb/completer.h @@ -326,6 +326,10 @@ public: LIST. */ void add_completions (completion_list &&list); + /* Remove completion matching NAME from the completion list, does nothing + if NAME is not already in the completion list. */ + void remove_completion (const char *name); + /* Set the quote char to be appended after a unique completion is added to the input line. Set to '\0' to clear. See m_quote_char's description. */ diff --git a/gdb/symtab.c b/gdb/symtab.c index f456f4d852d..d23552716f8 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -5269,6 +5269,27 @@ completion_list_add_symbol (completion_tracker &tracker, completion_list_add_name (tracker, sym->language (), sym->natural_name (), lookup_name, text, word); + + /* C++ function symbols include the parameters within both the msymbol + name and the symbol name. The problem is that the msymbol name will + describe the parameters in the most basic way, with typedefs stripped + out, while the symbol name will represent the types as they appear in + the program. This means we will see duplicate entries in the + completion tracker. The following converts the symbol name back to + the msymbol name and removes the msymbol name from the completion + tracker. */ + if (sym->language () == language_cplus + && SYMBOL_DOMAIN (sym) == VAR_DOMAIN + && SYMBOL_CLASS (sym) == LOC_BLOCK) + { + /* The call to canonicalize returns the empty string if the input + string is already in canonical form, thanks to this we don't + remove the symbol we just added above. */ + std::string str + = cp_canonicalize_string_no_typedefs (sym->natural_name ()); + if (!str.empty ()) + tracker.remove_completion (str.c_str ()); + } } /* completion_list_add_name wrapper for struct minimal_symbol. */ diff --git a/gdb/testsuite/gdb.linespec/cp-completion-aliases.cc b/gdb/testsuite/gdb.linespec/cp-completion-aliases.cc new file mode 100644 index 00000000000..5f2fb5c663a --- /dev/null +++ b/gdb/testsuite/gdb.linespec/cp-completion-aliases.cc @@ -0,0 +1,73 @@ +/* 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 . */ + +#include + +template +struct magic +{ + T x; +}; + +struct object +{ + int a; +}; + +typedef magic int_magic_t; + +typedef object *object_p; + +typedef const char *my_string_t; + +static int +get_value (object_p obj) +{ + return obj->a; +} + +static int +get_something (object_p obj) +{ + return obj->a; +} + +static int +get_something (my_string_t msg) +{ + return strlen (msg); +} + +static int +grab_it (int_magic_t *var) +{ + return var->x; +} + +int +main () +{ + magic m; + m.x = 4; + + object obj; + obj.a = 0; + + int val = (get_value (&obj) + get_something (&obj) + + get_something ("abc") + grab_it (&m)); + return val; +} diff --git a/gdb/testsuite/gdb.linespec/cp-completion-aliases.exp b/gdb/testsuite/gdb.linespec/cp-completion-aliases.exp new file mode 100644 index 00000000000..e8fe0bc6af2 --- /dev/null +++ b/gdb/testsuite/gdb.linespec/cp-completion-aliases.exp @@ -0,0 +1,57 @@ +# 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 . + +# This file tests GDB's ability to remove symbol aliases from the +# completion list in C++. + +load_lib completion-support.exp + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}]} { + return -1 +} + +# Tests below are about tab-completion, which doesn't work if readline +# library isn't used. Check it first. + +if { ![readline_is_used] } { + untested "no tab completion support without readline" + return -1 +} + +# Disable the completion limit for the whole testcase. +gdb_test_no_output "set max-completions unlimited" + +test_gdb_complete_tab_unique "break get_v" \ + "break get_value\\(object_p\\)" " " + +test_gdb_complete_cmd_unique "break get_v" \ + "break get_value\\(object_p\\)" + +test_gdb_complete_tab_unique "break gr" \ + "break grab_it\\(int_magic_t\\*\\)" " " + +test_gdb_complete_cmd_unique "break gr" \ + "break grab_it\\(int_magic_t\\*\\)" + +test_gdb_complete_tab_multiple "break get_som" "ething(" \ + { "get_something(my_string_t)" "get_something(object_p)" } + +test_gdb_complete_cmd_multiple "break " "get_som" \ + { "get_something(my_string_t)" "get_something(object_p)" } + + +