From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OFV+A33OxGap9w0AWB0awg (envelope-from ) for ; Tue, 20 Aug 2024 13:12:29 -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=ZVE3A79C; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 089C91E0D0; Tue, 20 Aug 2024 13:12:29 -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 E907F1E08C for ; Tue, 20 Aug 2024 13:12:26 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE3193842FEC for ; Tue, 20 Aug 2024 17:12:26 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id C65523842582 for ; Tue, 20 Aug 2024 17:10:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C65523842582 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 C65523842582 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=1724173862; cv=none; b=JIy0m+zNWOt4p3JqVNqe/PKQjIF7MQVJ3JuPpSJ+uiMjdyu3TbxNuYt+6tbg6akQ240mgiGhZS4uJAO3AZeKc//g7gwXfF4D63AodbOtDV6Bi+/i2EjW6lJd36dSKkvSwyiZRmQfyysGWfjmbU0zrRAalq5n3jLPCWz+3JHUI1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724173862; c=relaxed/simple; bh=fxnBwevF27AhRqv7f06I57/57ZR5jZTPrVFBflIt8HE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=HfX5WLOcnbqni1F2G7uQ4MSRZwXMNJa/ndTAmfjISRQiC8hodgYvF9n/hgrKexUIqC1gxUJLNtdQ20UTkyTIUBVd3VyNWPB9vyJcuCrcAukTDJMpRw0FR4S6oN/sXoKj5y05W25unUlNN8v1hXRL9fzNZYMG+guNtNT5ntz6S1s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724173858; 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=ZVE3A79ClrVGbI7j8rMNY1BVC/WWGvV4av6xzO3ljZ0t5ceLHoRMU9Z5d+/gPyb+zMVqQv A0KIrj2jkUZsJ2mjUohuEcnShYDXiNMk7zoyDrvm1E7i/KP7nwjP/wfGnkaOYmltz7zZ3F PX6WbTJ5eP7jw3cCQkklZpEqxS7j/ho= 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-286-ZZ587Cj2OpKJtIYTA0gBNA-1; Tue, 20 Aug 2024 13:10:57 -0400 X-MC-Unique: ZZ587Cj2OpKJtIYTA0gBNA-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-428fc34f41bso50050335e9.3 for ; Tue, 20 Aug 2024 10:10:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724173855; x=1724778655; 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=af9Mpzug7vKfjGHYxJSafEOv/j/vUsbo7g6pM70GfYr8EQGmyZaJ+wOLqa030GaQpj y7fGKy0kOK/AqAGA6T6TWvO8nvXtunjvLG/3aIc81F+j7Ta9n4lj76AO4sSNkx3Q8JEq L1oUR/fA9gqB2OewFxNKLKh9eUlgMRGNSRTq9rW6mszET2Cpw1QpA+TI+aV8M6wXpdEl 6SBdsfaUpnL9bsndNsR4YCfO88fUbGMmmFIszgPJM+IOfVLyB6p1V53NE0n4TXrTpimd 5gtZwcd78F+3OcYkusTCKNw9ykp7glu+2qeUQcotckiV/R/Il1mrloqxMAQa5FaNnV49 ym+A== X-Gm-Message-State: AOJu0YwuQ5DwSpt5BeKGCYQpzq4VueY6/y0zxzCWPQbn/RyUfBejY3G3 sPrPJS9mOR8WizIvxmf1i71wRRBHNjT+0uAyvnltf/p8EMujlthwH3571/v7t0cDNwY1g21LsGd 9tXlc1JfH4Un85mSdzliCX0YSYCjlc9GOrZcl4T3p4TUz2BUyp7j030mNd4MYe6rjJHb7zeU94Y a/fpik1yjiPUdyskCs05wwBp1IR9JYur3a2BJyWe10+hg= X-Received: by 2002:a05:600c:1547:b0:425:7c95:75d0 with SMTP id 5b1f17b1804b1-42abd21f678mr380285e9.18.1724173854530; Tue, 20 Aug 2024 10:10:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEOz77mRrmPW4vzZWwoaKfNCGILCAhDj2JGUcbFEdJRsWx0bU6dpdMs7ElXAGbwhflzDKuOhA== X-Received: by 2002:a05:600c:1547:b0:425:7c95:75d0 with SMTP id 5b1f17b1804b1-42abd21f678mr379985e9.18.1724173853647; Tue, 20 Aug 2024 10:10:53 -0700 (PDT) Received: from localhost (178.126.90.146.dyn.plus.net. [146.90.126.178]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3718985a35fsm13595581f8f.59.2024.08.20.10.10.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 10:10:53 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv5 05/14] gdb: simplify completion_result::print_matches Date: Tue, 20 Aug 2024 18:10:35 +0100 Message-Id: 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_H4, 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