From c0492bea7ca33fafc280f08f4ed9f505d29e7130 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 23 Sep 2021 09:56:51 -0400 Subject: [PATCH] gdb: add selftest name completion After the previous commit, it is easy to add completion for selftest names. Again, this is not particularly high value, but I rarely touched completion, so it served as a simple example to get some practice. Change the for_each_selftest_ftype parameter to gdb::function_view, so that we can pass a lambda that captures things. Change-Id: I87cac299ddca9ca7eb0ffab78342e850a98d954c --- gdb/maint.c | 11 +++++++++-- gdb/testsuite/gdb.gdb/unittest.exp | 15 +++++++++++++++ gdbsupport/selftest.h | 4 +++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/gdb/maint.c b/gdb/maint.c index 4e75e47c6a7..85fa18c34a0 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -1176,8 +1176,15 @@ maintenance_selftest_completer (cmd_list_element *cmd, { auto grp = make_maintenance_selftest_option_group (nullptr); - gdb::option::complete_options - (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp); + if (gdb::option::complete_options + (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp)) + return; + + selftests::for_each_selftest ([&tracker, text] (const std::string &name) + { + if (startswith (name.c_str (), text)) + tracker.add_completion (make_unique_xstrdup (name.c_str ())); + }); } static void diff --git a/gdb/testsuite/gdb.gdb/unittest.exp b/gdb/testsuite/gdb.gdb/unittest.exp index 670205fff76..0ddd2068377 100644 --- a/gdb/testsuite/gdb.gdb/unittest.exp +++ b/gdb/testsuite/gdb.gdb/unittest.exp @@ -20,6 +20,8 @@ if [gdb_debug_enabled] { return 0 } +load_lib completion-support.exp + set do_xml_test [expr ![gdb_skip_xml_test]] standard_testfile @@ -59,6 +61,17 @@ proc run_selftests { binfile } { } } +# Test completion of command "maintenance selftest". + +proc_with_prefix test_completion {} { + clean_restart + test_gdb_complete_tab_multiple "maintenance selftest string_v" "" \ + {string_vappendf string_view string_vprintf} + test_gdb_complete_tab_unique "maintenance selftest string_vie" "maintenance selftest string_view" " " + test_gdb_complete_tab_unique "maintenance selftest -ver" "maintenance selftest -verbose" " " + test_gdb_complete_tab_none "maintenance selftest name_that_does_not_exist" +} + with_test_prefix "no executable loaded" { run_selftests "" } @@ -67,6 +80,8 @@ with_test_prefix "executable loaded" { run_selftests ${binfile} } +test_completion + if { ![is_remote host] && $do_xml_test } { gdb_test "maintenance check xml-descriptions ${srcdir}/../features" \ "Tested $decimal XML files, 0 failed" \ diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h index 2326ebad495..7275126d2a9 100644 --- a/gdbsupport/selftest.h +++ b/gdbsupport/selftest.h @@ -20,6 +20,7 @@ #define COMMON_SELFTEST_H #include "gdbsupport/array-view.h" +#include "gdbsupport/function-view.h" /* A test is just a function that does some checks and throws an exception if something has gone wrong. */ @@ -48,7 +49,8 @@ extern void run_tests (gdb::array_view filters, /* Reset GDB or GDBserver's internal state. */ extern void reset (); -typedef void for_each_selftest_ftype (const std::string &name); +using for_each_selftest_ftype + = gdb::function_view; /* Call FUNC for each registered selftest. */ -- 2.30.2