From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 056BB384A45F for ; Thu, 4 Jul 2024 14:21:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 056BB384A45F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 056BB384A45F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720102888; cv=none; b=huaDlPcY6wk6wgl6c9n+EN3F/KDgsSAjupM4IQpO9KQD/m7Q/oUZwWGskaAzCQstYHBQX2fME735OSRfkcuYQEp4U0RUgRQ1cuAbBl4/UaXX4cfdWv7qGpHY/0iJ6QKLy6XixXBzXpAOXqlIIRCF7AEQbOxgPa98hFSrwAoHmkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720102888; c=relaxed/simple; bh=fxnBwevF27AhRqv7f06I57/57ZR5jZTPrVFBflIt8HE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=DwhlUhHYxJdivCD2gatvCEwIlLvt62CRRc+TQG2cC2zs0NTi8B4zc7Io5M4M8HozbJKl/ey/6GN9KVDkNjT3MRwwIHylJfA3/cLKHYVK4iHJxDQTS6dcPQiSXSq6eOjSvoi/l2bUEaKFtVPn8wTyQbS+CcmxegG1+rR/XUnLhYM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720102885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JaTb9rSUuwUosKijFLIrofe4FvM80gXNrMdlHMQzeWQ=; b=FOE2KQ7ONfT+gV9OMqAYIfxdMYrNRTxpROsCC/jUUe7sml8GxV6QsSuQUqNCEOvLTDWiKP Sug8Od6s383/NaClR7hyjus69C0iuwCUffdf+LgMK6DeyKjb3zzXoGp5pIYRPGHIkgNbaA xgB8i3w61F28FJc7J7lC7Hl//RhnqCw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-134-tBdxZWkaNXmKgzFX4vbRrg-1; Thu, 04 Jul 2024 10:21:24 -0400 X-MC-Unique: tBdxZWkaNXmKgzFX4vbRrg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-424fb2df2bdso6908525e9.3 for ; Thu, 04 Jul 2024 07:21:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720102882; x=1720707682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JaTb9rSUuwUosKijFLIrofe4FvM80gXNrMdlHMQzeWQ=; b=lSWiF7onZElojN+KoxOrw8wPeiitugBUgncupbExweqX7Rhl4wDHK8XRxuDh8/hLEH TXhQvu6/3Pz9bwAvwDzgBsg8BjpEPXUekEuXWILlamVt2/RbEZz4B0rHiFDsCT4VTeea 5YKwG95EUAI44eUhNeJ0s6MDP34FDiAdMtA6i5e8A8oJNUXfCso6J0ami/b2Yqr4rxFr /3daA+k3UPm7hM1VyDkFGZUU/WLwyHHJDQgc47Wf0H/PkL29q7Vz03yjCfs1mPTL2TP+ JKkF4dhjGIg0oHLqt/YtgDn+bDYEenM2Yh/Zdnyb+oKmqk8PrKz5HrltBqtgFzphd3gS 8UzA== X-Gm-Message-State: AOJu0YzSx9rZTYgMnSZ2Hi7/Ki8m33EGHzWxHJMaoS0t27ytZQYeQYRP 9Ut8Jr+8s8r43ujUAr/CST72Oo4WS9+wzzI3MeWHBjtL+chEUQR5SmAgB/1mIoKxB90idAaypyD /C5g6KpT0qujxgRYL5VxhFw4Tih8ZiNwtsRURFAtY32WnHyt5sYFbT6X9tbji/eIBgm8TRq6mGA 0UkmrcIIBohAIMOZrGvxtzVXVTpFBZwJSzh0z3GAApKlQ= X-Received: by 2002:a05:600c:331a:b0:426:47df:7d5f with SMTP id 5b1f17b1804b1-4264a41f864mr15184355e9.40.1720102882000; Thu, 04 Jul 2024 07:21:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEAdeKssFEWVnybZ0ySu9A3zcMFW/bSM/h3onS+te1dJqPcjcJTljcCTRLijXoE5ZBa0hAZA== X-Received: by 2002:a05:600c:331a:b0:426:47df:7d5f with SMTP id 5b1f17b1804b1-4264a41f864mr15184175e9.40.1720102881527; Thu, 04 Jul 2024 07:21:21 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4264a1dda61sm26538105e9.19.2024.07.04.07.21.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 07:21:20 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv4 05/14] gdb: simplify completion_result::print_matches Date: Thu, 4 Jul 2024 15:21:00 +0100 Message-Id: <77c38da2452bfa3fc1206b5ab592b11073a01450.1720101827.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Simplify completion_result::print_matches by removing one of the code paths. Now, every time we call ::print_matches we always add the trailing quote. Previously, when using the 'complete' command, if there was only one result then trailing quote was added in ::build_completion_result, but when we had multiple results the trailing quote was added in ::print_matches. As a consequence, ::print_matches had to understand not to add the trailing quote for the single result case. After this commit we don't add the trailing quote in ::build_completion_result, instead ::print_matches always adds the trailing quote, which makes ::print_matches simpler. However, there is a slight problem. When completion is being driven by readline, and not by the 'complete' command, we still need to manually add the trailing quote in the single result case, and as the printing is done by readline we can't add the quote at the time of printing, and so, in ::build_completion_result, we still add the trailing quote, but only when completion is being done for readline. And this does cause a small problem. When completing a filename, if the completion results in a directory name then, when using the 'complete' command, GDB should not be adding a trailing quote. For example, if we have the file /tmp/xxx/foo.c, then what we should see is this: (gdb) complete file '/tmp/xx file 'tmp/xxx/ But what we actually see after this commit is this: (gdb) complete file '/tmp/xx file 'tmp/xxx/' Previously we didn't get the trailing quote in this case, as when there is only a single result, the quote was added in ::build_completion_result, and for filename completion, GDB didn't know what the quote character was in ::build_completion_result, so no quote was added. Now that the trailing quote is always added in ::print_matches, and GDB does know the quote character at this point, so we are now getting the trailing quote, which is not correct. This is a regression, but really, GDB is now broken in a consistent way, if we create the file /tmp/xxa/bar.c, then previously if we did this: (gdb) complete file '/tmp/xx file '/tmp/xxa/' file '/tmp/xxx/' Notice how we get the trailing quote in this case, this is the before patch behaviour, and is also wrong. A later commit will fix things so that the trailing quote is not added in this filename completion case, but for now I'm going to accept this small regression. This change in behaviour caused some failures in one of the completion tests, I've tweaked the test case to expect the trailing quote as part of this commit, but will revert this in a later commit in this series. I've also added an extra test for when the 'complete' command does complete to a single complete filename, in which case the trailing quote is expected. --- gdb/completer.c | 62 +++++++++---------- .../gdb.base/filename-completion.exp | 21 ++++++- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index 370a57e6d7f..2793ce600b9 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -2372,23 +2372,30 @@ completion_tracker::build_completion_result (const char *text, if (m_lowest_common_denominator_unique) { - /* We don't rely on readline appending the quote char as - delimiter as then readline wouldn't append the ' ' after the - completion. */ - char buf[2] = { (char) quote_char () }; - - match_list[0] = reconcat (match_list[0], match_list[0], - buf, (char *) NULL); - match_list[1] = NULL; - - /* If the tracker wants to, or we already have a space at the - end of the match, tell readline to skip appending - another. */ - char *match = match_list[0]; - bool completion_suppress_append - = (suppress_append_ws () - || (match[0] != '\0' - && match[strlen (match) - 1] == ' ')); + bool completion_suppress_append; + + if (from_readline ()) + { + /* We don't rely on readline appending the quote char as + delimiter as then readline wouldn't append the ' ' after the + completion. */ + char buf[2] = { (char) quote_char (), '\0' }; + + match_list[0] = reconcat (match_list[0], match_list[0], buf, + (char *) nullptr); + + /* If the tracker wants to, or we already have a space at the end + of the match, tell readline to skip appending another. */ + char *match = match_list[0]; + completion_suppress_append + = (suppress_append_ws () + || (match[0] != '\0' + && match[strlen (match) - 1] == ' ')); + } + else + completion_suppress_append = false; + + match_list[1] = nullptr; return completion_result (match_list, 1, completion_suppress_append); } @@ -2510,21 +2517,14 @@ void completion_result::print_matches (const std::string &prefix, const char *word, int quote_char) { - if (this->number_matches == 1) - printf_unfiltered ("%s%s\n", prefix.c_str (), this->match_list[0]); - else - { - this->sort_match_list (); + this->sort_match_list (); - for (size_t i = 0; i < this->number_matches; i++) - { - printf_unfiltered ("%s%s", prefix.c_str (), - this->match_list[i + 1]); - if (quote_char) - printf_unfiltered ("%c", quote_char); - printf_unfiltered ("\n"); - } - } + char buf[2] = { (char) quote_char, '\0' }; + size_t off = this->number_matches == 1 ? 0 : 1; + + for (size_t i = 0; i < this->number_matches; i++) + printf_unfiltered ("%s%s%s\n", prefix.c_str (), + this->match_list[i + off], buf); if (this->number_matches == max_completions) { diff --git a/gdb/testsuite/gdb.base/filename-completion.exp b/gdb/testsuite/gdb.base/filename-completion.exp index c670637ad61..e8acd2f85cf 100644 --- a/gdb/testsuite/gdb.base/filename-completion.exp +++ b/gdb/testsuite/gdb.base/filename-completion.exp @@ -81,8 +81,25 @@ proc run_quoting_and_escaping_tests { root } { test_gdb_complete_none "$cmd ${qc}${root}/xx" \ "expand a non-existent filename" - test_gdb_complete_unique "$cmd ${qc}${root}/a" \ - "$cmd ${qc}${root}/aaa/" "" false \ + # The following test is split into separate cmd and tab calls + # so we can xfail the cmd version. The cmd version will add a + # closing quote, it shouldn't be doing this. This will be + # fixed in a later commit. + if { $qc ne "" } { + setup_xfail "*-*-*" + } + test_gdb_complete_cmd_unique "$cmd ${qc}${root}/a" \ + "$cmd ${qc}${root}/aaa/" \ + "expand a unique directory name" + + if { [readline_is_used] } { + test_gdb_complete_tab_unique "$cmd ${qc}${root}/a" \ + "$cmd ${qc}${root}/aaa/" "" \ + "expand a unique directory name" + } + + test_gdb_complete_unique "$cmd ${qc}${root}/cc2" \ + "$cmd ${qc}${root}/cc2${qc}" " " false \ "expand a unique filename" test_gdb_complete_multiple "$cmd ${qc}${root}/" \ -- 2.25.4