From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id mFy4EZuu0Wb19xkAWB0awg (envelope-from ) for ; Fri, 30 Aug 2024 07:35:55 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fGYsDFOw; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 41FC81E0D1; Fri, 30 Aug 2024 07:35:55 -0400 (EDT) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 26F821E08C for ; Fri, 30 Aug 2024 07:35:53 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B64E2385DDEF for ; Fri, 30 Aug 2024 11:35:52 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id ED520385E011 for ; Fri, 30 Aug 2024 11:34:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED520385E011 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 ED520385E011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725017653; cv=none; b=AZC5bEOQYRMU1SMqNcr0lXpvIXTzBnSSnRA0pwSaUxr5YJLYKT69O4f6xNBzQRcvnpW4GX7BvCTmahThOdqAJpW5G/lqGRf0G/Pr+xMNptM7GqvJvwlgQ4mFiKzXyH4B2OSvz8POLSCfenUPKOPhfx7BtQ/hm4JyIuPpjBWawAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725017653; c=relaxed/simple; bh=fxnBwevF27AhRqv7f06I57/57ZR5jZTPrVFBflIt8HE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LqrptytfJi6Hg2wDip02N71nL9kOrwI1lIwDS8pmmwzxlGUndARM2qs1h2xyVCg2jD90M1h/pss/wSJhY6TwOTNGncBh6wwTTNi2hOM01pPgePhIeUO2xq37d6qdw2+De2VvfagSEic0lLSyED5JOZcvbRVAP+iK9a8f1GrZ5To= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725017649; 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=fGYsDFOwMF62001yLBjAzhrB4KspKbnTiZ+szvupq4qf+uzj5FceRcJTxACNntA2q+bKcp fJARCqWKAVj1/UkQRkTscUuZtYH+f8Jeda7QI60WnXYBaHiTqSmUfDtWOh91zmzoM6JSuO 1EezLOROqmopbkRfvfk5o6L11Ieq30c= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-321-86fhQTTiNZ-rdibH4bZD5w-1; Fri, 30 Aug 2024 07:34:08 -0400 X-MC-Unique: 86fhQTTiNZ-rdibH4bZD5w-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a7d2d414949so163998966b.0 for ; Fri, 30 Aug 2024 04:34:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725017646; x=1725622446; 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=xQ+1bRORwx2K91slpzSu/SUzaxrBzyTJjJ1pVwOyyOWuMi7T0jNjz9gBDQWyvJr64M eRG8C+fxaH4ym66M/JKRKzhRPD4jWQp0zal+tls3Iqy8xqWQDQN0s2tY0cjijJjIds/6 yzo9vFZz4xjUwnquK0ZZvfqWtT7XOHDPEkgS7qygoo/alapVJLq5JOjPzQEFH57GrupA rTX7uB7e+yqtDDzOZNkUlAXaJYyXngKi1O7ir/zIB3nFvwNZSXc5hV5GMYIIkIPqW0e9 fwndJ3a5yueD7IaAtdZGJMpDkk85bw8aXdA9qDKGK2j3RiHz88I9/hiMV3h/hbalOi1e f6mA== X-Gm-Message-State: AOJu0YxuYNUIuia9Mr9jmLmHcHV5qwzG6Ka5dN1sBAWqTJkJgL4JGfo2 O0KkLJfaMzZnEdF5JQC2EAAnR4zkPBpdUgiAkz13iJSJuwRHASdia3SMCDRf95GuOMfmPg0zOy1 ++gw3Bz/Wgt+AdETadZMR9CMVVKPzOtHTd/1p2aVJFY1jd+cX6QSd0ShfZ59cJAvoWXLjw/owh0 2PTXl63FIS2w+jSrVatqNNgbe6rBTrqH6wq/YNzW2URzU= X-Received: by 2002:a05:6402:40ce:b0:57c:9d54:67db with SMTP id 4fb4d7f45d1cf-5c21ed4083amr4964434a12.9.1725017646085; Fri, 30 Aug 2024 04:34:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7oUQMUgtU6SdLhTuU/kGeiua4nEztKRDPmYxTUOYt0izigQQhrT8xb734tumTlGsJ7vFZYQ== X-Received: by 2002:a05:6402:40ce:b0:57c:9d54:67db with SMTP id 4fb4d7f45d1cf-5c21ed4083amr4964400a12.9.1725017644975; Fri, 30 Aug 2024 04:34:04 -0700 (PDT) Received: from localhost (178.126.90.146.dyn.plus.net. [146.90.126.178]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c226c7307bsm1929380a12.34.2024.08.30.04.34.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 04:34:04 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv6 05/14] gdb: simplify completion_result::print_matches Date: Fri, 30 Aug 2024 12:33:45 +0100 Message-Id: <227da3ef3c5d730ea82064e7ebca35f686e6c933.1725017349.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=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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