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.129.124]) by sourceware.org (Postfix) with ESMTPS id BD268385841D for ; Sat, 20 Apr 2024 09:10:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD268385841D 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 BD268385841D 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=1713604233; cv=none; b=J5JkORHotM0OZ4ruFKRd7bFPvQdo5Jl4V+gQ86/2PwDenKRR+sc3BzWgEAncYGh/1lSojSVa579u1ox81Rahil+95WF0LBRzOLrUSpFd68YBr8kp9sKFNBP/q8MUHFkWbpcQs8l0ukY2eJqrR2vNg1TdHG26lRAINW6nRcLVLVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713604233; c=relaxed/simple; bh=u/GsuHSHmFpwclaaFQBIp/xsbQVRs4W+jnZydjsbYF0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YHXJA7RPktJyAUpdiUntTDML9YhpnVaqJZt3U5hJTo//i6M+o1rH5blQZzWyXMnK1OUOUspgNT6hQceEx8rfRDgdIwXk9ixsdl6MFzIUc70TdC4OIWy7CbGEW1t7TGyG0JJt3FJrOqGwI+ySShI5sHzTd6D100BgR6vjfuBX10k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713604231; 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=+M/INGSSPHOhi2q/VLRmssFWbGmPWxCAazUYn4JXiYk=; b=bQhTSKulyWDOctUt3UloUxknwftIp3j3CcbKQoyfdTKXTFkJ9mpAhQN51t+FaNfwIfdQgk hT5gWx89hR1g9raHsnz0fy6U+fFwOlk4c+p1RDhPgJ9RWyWiOIW3IHVGVpmkdDyEOhI5y3 SXjIXnrbUPpg3f4LFMRagBugIcGbSVU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-Z4Wk07rLPT2dwXE2PitM9Q-1; Sat, 20 Apr 2024 05:10:30 -0400 X-MC-Unique: Z4Wk07rLPT2dwXE2PitM9Q-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3455cbdea2cso1626749f8f.0 for ; Sat, 20 Apr 2024 02:10:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713604228; x=1714209028; 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=+M/INGSSPHOhi2q/VLRmssFWbGmPWxCAazUYn4JXiYk=; b=MMYSp+802WLvOVuo5i8N6SLNS19sSqQVToaumYqXL0s49ncNqT45b9rsHmhwOd1haV cXuxFZSuioorRSGwAafAOAjifK3zIE65CG/AqZ8/SUOYFDxzmIsUgxATHjEb9IDiD/Fx RlqusozUoCcgein0mAiPlBzfkfBnXvU/BtU1hfBTDXpNWKx+dZ3k8ZYI+8FX6NSAUm3H Z4zZbcj+DAvM1IO581JUVNSmexWsDRhzTSxMOVy6FZohDRB33kRw679G7HlExEyWGT7r ZU4EDH4J4r8RZkn12aQDwF1OhFln0b/C72DgguvWeelyg7RCifJdV7HQa1xtppv5IUQw Xv9w== X-Gm-Message-State: AOJu0YzHmTYPswSdo96cbQlEcrbeqVwfCJ+XZstD+qdECG9FF4ByqBlL gUEtTEwFtoE4sbZ7UXI5Kbztn0gaTox5/tG9NA7aBkXqz2o6fBejt0vV8C34lKVaCpQ3A9EhUp/ /3rTimOay6RpAT3WaClbfBMUDfrrCWNJOFlxBFxlMdNHq7RCBQeuzt7PwaODA6GIL7Ks1bGxcYq 0rk1FKcMNYXnQGolqjf0YfldPIkcLQ2deocTeVlvvwOzs= X-Received: by 2002:adf:ea82:0:b0:349:7f89:2200 with SMTP id s2-20020adfea82000000b003497f892200mr5691526wrm.5.1713604227526; Sat, 20 Apr 2024 02:10:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+BOfP3A4C4PIlpgpA15P6U3DED0BD8TJ8PufJB8d3i6Otwb6JUSZdukoT1JffAmEnPT66sQ== X-Received: by 2002:adf:ea82:0:b0:349:7f89:2200 with SMTP id s2-20020adfea82000000b003497f892200mr5691511wrm.5.1713604227080; Sat, 20 Apr 2024 02:10:27 -0700 (PDT) Received: from localhost (92.40.185.25.threembb.co.uk. [92.40.185.25]) by smtp.gmail.com with ESMTPSA id q4-20020adffec4000000b00347f6b5bb6dsm6374498wrs.30.2024.04.20.02.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Apr 2024 02:10:26 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Lancelot SIX , Eli Zaretskii Subject: [PATCHv2 5/8] gdb: simplify completion_result::print_matches Date: Sat, 20 Apr 2024 10:10:05 +0100 Message-Id: <7a132f2949bb05f00d0ffb6be8e0863417a7e0fc.1713603416.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=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_ABUSEAT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,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 af1c09b45b1..4ba45a35f8f 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -2327,23 +2327,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); } @@ -2465,21 +2472,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 275140ffd9d..3e3b9b29ba4 100644 --- a/gdb/testsuite/gdb.base/filename-completion.exp +++ b/gdb/testsuite/gdb.base/filename-completion.exp @@ -82,8 +82,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 "file ${qc}${root}/a" \ + "file ${qc}${root}/aaa/" \ + "expand a unique directory name" + + if { [readline_is_used] } { + test_gdb_complete_tab_unique "file ${qc}${root}/a" \ + "file ${qc}${root}/aaa/" "" \ + "expand a unique directory name" + } + + test_gdb_complete_unique "file ${qc}${root}/cc2" \ + "file ${qc}${root}/cc2${qc}" " " false \ "expand a unique filename" test_gdb_complete_multiple "$cmd ${qc}${root}/" \ -- 2.25.4