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 CF687385840D for ; Fri, 29 Mar 2024 11:42:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CF687385840D 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 CF687385840D 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=1711712567; cv=none; b=xa8CyMuA3cpbQ32tdMSYrgwsFsNsSN7l/MneoOIWeDbu7UeC7et29hwU3KBfOfzHOJkTVHSzy0iGlKGpDSqKSSDIopmJJHqvx/6uIyaq/DQdtqC7vz0RTIW0h7lIft1eRwG5+8DGofIMEhT5VPF9HALNMC/e4ayJOBlLbV7pWCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711712567; c=relaxed/simple; bh=sU0Mk4dOxYF6gBBomXwAYcsCLgaMs6rWE1+m5HCk74I=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SrcFm5OBuVNcXwBDakhllyfG7LdpbJNF9KkCrxILIENixZHRZrYEJYoWDvAFx5Yc2pi/zKSC8yLeY3IsSmVXMnz6OPJLZxRvcoeIL0BBco02OK5noaMsGP/TOEwqaz62PVmF+9RAa4aNykjp4pMS5FbsRIOQZik8LGE7u762p5A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711712563; 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=b9dnuwC4RR7jwlKBShO3OMAYIDAxPmmgBy+UZLxHkrM=; b=eXUewhHzBRH1A6caAS325uj6RoyR+raMhDe1R68SSyquDoJacJYMxwQjS5vHKj5z+2S1mS YvQLzzB7Uxa84IpUR9ymRoJehLz2i3L0pVKTGgUEzKmIqNwXTn6mwHsOdyToEmzKdIOIbu QCEoyqszV7CIslmP3mN/xxzqu9NvEYU= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-263-Xs4kjU9bNpuBohx1VnZpZQ-1; Fri, 29 Mar 2024 07:42:40 -0400 X-MC-Unique: Xs4kjU9bNpuBohx1VnZpZQ-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a46852c2239so125694766b.1 for ; Fri, 29 Mar 2024 04:42:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711712558; x=1712317358; 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=b9dnuwC4RR7jwlKBShO3OMAYIDAxPmmgBy+UZLxHkrM=; b=tVeBZhqNWtXSfMc79Zt0foNvghYNzEi+e7JUzHL2CUfKNbAEouH2Z4maGNvBZH7jPV lThfRTAMs0cfeMZm3OknCsV4T7v3jcwqyfhynKUUPILakyeRMUodgChq+sPFTmDFnSQO 8yJ6tct2ZjH4TXICENV9ejjqNLgMjkDJfuTv78R1zc01bG2tdMS3gq+0bAGSq8Qr7EM4 WV2ohQVke6c3PlVx111I+5vzlJtrgmeKVwP9umJ58q/cx2YW07TzJJbsasYimuS8ufei o00w7gOEnsMw5SOWj2pQC2M4wCgX9/yJ2iJX2l473jz9UiXgpDqvnAFIHBw4BJNye43E GIow== X-Gm-Message-State: AOJu0YxubNGx8iuzxrIzboaKq8M34mhmYf+FNqIsy1AKXAv4E5Q85iyj 3MJ5s04fgNtTV0Z7Wl2+avVK1BFq0TVuxNNEvp0pFndj+ZZxR3lBgjX5UQ7NryraHm6YO1T40ND V47I0kOWMtvwbMqEffyUmH6gt+kZ/k/o/lMsqx1H2vRXqR/NhpvcVYpE7JstCrpwD6kArP49QEh VKg2Dunjwu4IwLEcXNbZ3/GEpG3YTCVp0ORVC0514/OpU= X-Received: by 2002:a17:907:10d8:b0:a46:92a1:6459 with SMTP id rv24-20020a17090710d800b00a4692a16459mr1344786ejb.17.1711712557976; Fri, 29 Mar 2024 04:42:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE85G0ELc+Ajh95BsSBndUaAUSsB7bn/9ciY1b9ur+jfgs4QPxAMnZxfs7KpowEKPgzK9DiXw== X-Received: by 2002:a17:907:10d8:b0:a46:92a1:6459 with SMTP id rv24-20020a17090710d800b00a4692a16459mr1344766ejb.17.1711712557462; Fri, 29 Mar 2024 04:42:37 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id xg6-20020a170907320600b00a4e38a06c86sm468400ejb.66.2024.03.29.04.42.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 04:42:37 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/6] gdb: simplify completion_result::print_matches Date: Fri, 29 Mar 2024 11:42:29 +0000 Message-Id: <8f677c4387cb1b14d736112bd33e799e01df9167.1711712401.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=-12.0 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 | 17 ++++- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/gdb/completer.c b/gdb/completer.c index 9b4041da01a..2b3972213d8 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -2311,23 +2311,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 () }; + + 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); } @@ -2449,21 +2456,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 f23e8671f40..66e5f411795 100644 --- a/gdb/testsuite/gdb.base/filename-completion.exp +++ b/gdb/testsuite/gdb.base/filename-completion.exp @@ -63,8 +63,21 @@ proc run_tests { root } { test_gdb_complete_none "file ${qc}${root}/xx" \ "expand a non-existent filename" - test_gdb_complete_unique "file ${qc}${root}/a" \ - "file ${qc}${root}/aaa/" "" false \ + # The following test is split into separate cmd and tab calls as the + # cmd versions will add a closing quote. It shouldn't be doing + # this; this will be fixed in a later commit. + test_gdb_complete_cmd_unique "file ${qc}${root}/a" \ + "file ${qc}${root}/aaa/${qc}" \ + "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 "file ${qc}${root}/" \ -- 2.25.4