From patchwork Wed Jan 10 13:41:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 186882 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp796502dyi; Wed, 10 Jan 2024 05:42:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGApgZayXeITVAI7ed5rAan2iMHqAiRMVABSirjYmC49PxSK5798GTP07R1zw8jTucRFFbD X-Received: by 2002:a05:6808:14d3:b0:3bc:e27:3cdf with SMTP id f19-20020a05680814d300b003bc0e273cdfmr1011182oiw.75.1704894131844; Wed, 10 Jan 2024 05:42:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704894131; cv=pass; d=google.com; s=arc-20160816; b=mmHuVCLRjaKHlbRTTdstyh5JmKNDr9EdMorNipPCmAm9tLN+aAw9mH32k/B4A6OsY6 EMGO8+zrs5wo3+dxB+OOf6/skb3YqETHmy+W3oMCzujKtklR4pKFw13ohKbeHni61JjE iNl3P+U0Lu/gnu68mm9S4np1ADF5Itd6dEuJcqxO2rg2OkkkNG+FgOL86SLZbiTti7cm umQ1kBEOXRptuIbh7qt0RnssKtwqNMrR1wLaE7R7stPbBsuxx7nMJ2pIXP/rJ88QMdw6 SLWqdgp8eS9c7mgeIYxajg0TtOTbWR7MiAQyDTVzykg1EcABuZq7pP0IMPKn/uA/i2nk 4Ekw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=vhED0WbtG8f2sjyHOGeICeiRvcByn6CV0NVArWOmdfo=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=VEtGaUnwwsCMhOjvpw8WGmGywoTR/b/41pC6lGKpbZtQMWdgcvzi9q0IAZSEwXMcoD d0P49UGiqLqBUC26Nq32namxOBXKt/uR0z1k5WkdWI76FtMJoYdK0sJRRL66ZGj6HyUK xoO2DsUpyvOTpsOZpm1M7jZfuKDPJOpCfUEwjP125/8SCo+i59MJ0VJ4yG9H3Se2aafB /S9s+3N6oPGtIbiZsxub5DWgRjPS7jPfz2ImwGF79O7ZLWq8it1D0tbgWMWud3Mvsx2l GTchBliz+GfSz/S+bD97lmnAsEPdHjT7Q8LoWVN58SSQ+udwD87VO+7TEjAWMP3DXHVc 0dsw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JyDmegVc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id f2-20020a05620a280200b00781cac14dfdsi4294211qkp.705.2024.01.10.05.42.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 05:42:11 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JyDmegVc; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 864093857831 for ; Wed, 10 Jan 2024 13:42:11 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 C757F3858410 for ; Wed, 10 Jan 2024 13:41:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C757F3858410 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 C757F3858410 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=1704894071; cv=none; b=J0IYOEFUS6CwUpeMfoJdBUhcGOAlAlfDXb1+SzVxb0kbD4bGUa9ia++LetfzPguivNQQ4/c7srxNslHbpyNTtBDZLPRm0reUEi2lXUS5owXgfi7laxO1+zlL85vxEWx1EjRAz3Nm0iUcYJ0Nx4ko0iTVFxz+8XeFVw3mRSzokc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704894071; c=relaxed/simple; bh=J5nbeaepCYa8GKgRzbFPt01vScqkIaemCnGImYLQDK0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=iVUZ89Ig5FkOuHvRDzjnYatmHNbLfpSUAUVRnx88x5eokiqJfLHq+wyQxMVOCaND8wOkmhl/Pmz9+DDYodFvH8iJmf+Jd6qxaE2A4V+o/PdNAVkMrdh/yBBH87uECyZCdGBP4OtWuRJ1imVjfmGMuEPHiFvKqCh1GQJJrkcfQ20= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704894069; 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; bh=vhED0WbtG8f2sjyHOGeICeiRvcByn6CV0NVArWOmdfo=; b=JyDmegVckNgqSQWYku4sDE3UXvT6Fm4xkXCWmm06FKf98Ap7KoIuq3u+CTj/cHbw/kVky6 0X3jtIp297jKUEpLiw8F95gwPW6P4TR5XXwA32vGP0/MgV2/SdfdPc9beH2k0AKeiZ/K3r 9fkPiwB41V+A/UM5mcqA+XC5z2Ce/1o= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-384-eidlHue7MfCpcjfEeA0jnQ-1; Wed, 10 Jan 2024 08:41:08 -0500 X-MC-Unique: eidlHue7MfCpcjfEeA0jnQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B82BD3C0ED66 for ; Wed, 10 Jan 2024 13:41:07 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 924ED1121306; Wed, 10 Jan 2024 13:41:07 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 1/3] pretty-print: add selftest coverage for numbered args Date: Wed, 10 Jan 2024 08:41:03 -0500 Message-Id: <20240110134105.749310-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787711069196279177 X-GMAIL-MSGID: 1787711069196279177 No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-7104-g7daa935c7997f3. gcc/ChangeLog: * pretty-print.cc (selftest::test_pp_format): Add selftest coverage for numbered args. Signed-off-by: David Malcolm --- gcc/pretty-print.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc index fd4c38ed3eb4..859ae2a273db 100644 --- a/gcc/pretty-print.cc +++ b/gcc/pretty-print.cc @@ -2605,6 +2605,20 @@ test_pp_format () assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7); assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10", "problem with %qs at line %i", "bar", 10); + + /* Verified numbered args. */ + assert_pp_format (SELFTEST_LOCATION, + "foo: second bar: first", + "foo: %2$s bar: %1$s", + "first", "second"); + assert_pp_format (SELFTEST_LOCATION, + "foo: 1066 bar: 1776", + "foo: %2$i bar: %1$i", + 1776, 1066); + assert_pp_format (SELFTEST_LOCATION, + "foo: second bar: 1776", + "foo: %2$s bar: %1$i", + 1776, "second"); } /* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */ From patchwork Wed Jan 10 13:41:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 186884 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp797112dyi; Wed, 10 Jan 2024 05:43:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNa1Ft9KtXWJmeWFM+sWWFtdYN8It23sKfebZfvar52kZz7DZwQwCLHuMkR+e+dPlqFGxv X-Received: by 2002:a05:6214:f2b:b0:67f:f29f:93d6 with SMTP id iw11-20020a0562140f2b00b0067ff29f93d6mr1192432qvb.129.1704894200863; Wed, 10 Jan 2024 05:43:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704894200; cv=pass; d=google.com; s=arc-20160816; b=UZZf0kx9Dk8qllqFlqVk4sKVLpvuuStX3XemUhXYWDn44yVJiMAEYKtF+Vo4Ofo5Gh irB3lnFfFSzE+UaJaJUr1UroSnMPdP9jpatW9Ll952HRViNwL3DUJSe2n4cIuKTIIOkH wQdgXd6ioaRy8INGmrfP+hUmDbEzo1AuE7EH0lxXLLAdvrO3JDcNb1kP9+TAWHQ8Iex8 DvbFR86LqSheOHdQiBtG2JpGmBC55jD4V5beujAol3LEBdDrXYUMI1t/vFR2i+S0lGUv 9ASu6/3pJsOjXQngw+GAYkMI6Cw4WpbhDJE/F/fOh2a92Lxl62qkufUD8qBjrvUZJ7ez 0IPQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=Noa6CjaoRQqTXpN9hdV17f561DvyzBhwBsdilHsk2nk=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=Ws5+FXTX0Ug5b18yFqtR99xtyYOM3A2ScTV6FYDEDPyUHvWs1aJGx4HXC9QzuMeGYz 0/KVG4CussXOKVmjpszPex/cFpu5tE3B/FRXMHu+ohFfQ3oiifXMPWQEqrKhZg8KPRNn AU/rRaEleDZ0savuAFEkNh74T7biJGv+zGBA/yfYpvMheP7037b+Jk/kPtUC1hlCBiJ+ WJu0DQb3VkPfPGaW44fqcCOJSj9UreXqh1mfCZB96MH1DVhMOIQgy0VCvzK2VgX/OuSq TiuFZX0CYeWGF3sLNdkAxaG0kX1MFve+s37WFnNlqFSSR4lHfrhrD2h1dJCjriPXiXgb 8NOQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Um5MetWn; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bl35-20020a05620a1aa300b007833644461fsi1125903qkb.238.2024.01.10.05.43.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 05:43:20 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Um5MetWn; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7EF613857BAB for ; Wed, 10 Jan 2024 13:43:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 02B903857C4F for ; Wed, 10 Jan 2024 13:41:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 02B903857C4F 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 02B903857C4F 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=1704894079; cv=none; b=rICXvytOTKkZ5TWu549kZo5Z6gIx5n4L5+vSuCok1cwNoeYiI9YAA6+saLVAbAYvx27/JOfsBuwG9UhDM5sBfWsUHxjzHlq6BBw06UXy7aBtOetCQhlN0PLcrfYzLgzJWgY+KgSb630YNkN04i5yRc/sTfLd7ITAvLK8SbSQ7xc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704894079; c=relaxed/simple; bh=MfwqIcEjp7w9A1YumgS3eI9in+RMNAJcvQqX6g9Ed1g=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dXApoApwKkVbUmWcaJIKZu2iIyW6UYI/P3hhRB+WocH2z6hH13NHrCV1fQ5r6/ri710UEs1zl7vtNjojl/yhKC1zbxqnATu/2cpsqQV4mxPTwO6BrS4gbhp0P4cPryQjLKkvvMSH0g9WtKQAcKlI3WV2trDUQK9v3XlTrTYqdmk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704894074; 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=Noa6CjaoRQqTXpN9hdV17f561DvyzBhwBsdilHsk2nk=; b=Um5MetWnvYZLmG3aNZMI65ow90tZ0G6nZizR1MBsNBDcKFvm8N/XEf9t8HLxl1yWSr57E0 KIQgQ33Cq1Qb/5g8aAcMEMND8CfSm778HaNpylfV86bHREJpqj+TfptwaD/mVb7JLRU8By 4rOZjAaAFnoNMIh4uQETZIyAhtTU0vI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-92-0sF6lsEWM4WyG5M9x0D9yw-1; Wed, 10 Jan 2024 08:41:08 -0500 X-MC-Unique: 0sF6lsEWM4WyG5M9x0D9yw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1A121831523 for ; Wed, 10 Jan 2024 13:41:08 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA6011121306; Wed, 10 Jan 2024 13:41:07 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 2/3] pretty-print: support urlification in phase 3 Date: Wed, 10 Jan 2024 08:41:04 -0500 Message-Id: <20240110134105.749310-2-dmalcolm@redhat.com> In-Reply-To: <20240110134105.749310-1-dmalcolm@redhat.com> References: <20240110134105.749310-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787711141975697108 X-GMAIL-MSGID: 1787711141975697108 TL;DR: for the case when the user misspells a command-line option and we suggest one, with this patch we now provide a documentation URL for the suggestion. In r14-5118-gc5db4d8ba5f3de I added a mechanism to automatically add URLs to quoted strings in diagnostics, and in r14-6920-g9e49746da303b8 through r14-6923-g4ded42c2c5a5c9 wired this up so that any time we mention a command-line option in a diagnostic message in quotes, the user gets a URL to the HTML documentation for that option. However this only worked for quoted strings that were fully within a single "chunk" within the pretty-printer implementation, such as: * "%<-foption%>" (handled in phase 1) * "%qs", "-foption" (handled in phase 2) but not where the the quoted string straddled multiple chunks, in particular for this important case in the gcc.cc: error ("unrecognized command-line option %<-%s%>;" " did you mean %<-%s%>?", switches[i].part1, hint); e.g. for: $ LANG=C ./xgcc -B. -finling-small-functions xgcc: error: unrecognized command-line option '-finling-small-functions'; did you mean '-finline-small-functions'? which within pp_format becomes these chunks: * chunk 0: "unrecognized command-line option `-" * chunk 1: switches[i].part1 (e.g. "finling-small-functions") * chunk 2: "'; did you mean `-" * chunk 3: hint (e.g. "finline-small-functions") * chunk 4: "'?" where the first quoted run is in chunks 1-3 and the second in chunks 2-4. Hence we were not attempting to provide a URL for the two quoted runs, and, in particular not for the hint. This patch refactors the urlification mechanism in pretty-print.cc so that it checks for quoted runs that appear in phase 3 (as well as in phases 1 and 2, as before). With this, the quoted text runs "-finling-small-functions" and "-finline-small-functions" are passed to the urlifier, which successfully finds a documentation URL for the latter. As before, the urlification code is only run if the URL escapes are enabled, and only for messages from diagnostic.cc (error, warn, inform, etc), not for all pretty_printer usage. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-7105-g5daf9104ed5d4e. gcc/ChangeLog: * diagnostic.cc (diagnostic_context::report_diagnostic): Pass m_urlifier to pp_output_formatted_text. * pretty-print.cc: Add #define of INCLUDE_VECTOR. (obstack_append_string): New overload, taking a length. (urlify_quoted_string): Pass in an obstack ptr, rather than using that of the pp's buffer. Generalize to handle trailing text in the buffer beyond the run of quoted text. (class quoting_info): New. (on_begin_quote): New. (on_end_quote): New. (pp_format): Refactor phase 1 and phase 2 quoting support, moving it to calls to on_begin_quote and on_end_quote. (struct auto_obstack): New. (quoting_info::handle_phase_3): New. (pp_output_formatted_text): Add urlifier param. Use it if there is deferred urlification. Delete m_quotes. (selftest::pp_printf_with_urlifier): Pass urlifier to pp_output_formatted_text. (selftest::test_urlification): Update results for the existing case of quoted text stradding chunks; add more such test cases. * pretty-print.h (class quoting_info): New forward decl. (chunk_info::m_quotes): New field. (pp_output_formatted_text): Add optional urlifier param. Signed-off-by: David Malcolm --- gcc/diagnostic.cc | 2 +- gcc/pretty-print.cc | 444 ++++++++++++++++++++++++++++++++++++-------- gcc/pretty-print.h | 9 +- 3 files changed, 379 insertions(+), 76 deletions(-) diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index cc2b1840c661..f5411b1ede0d 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -1603,7 +1603,7 @@ diagnostic_context::report_diagnostic (diagnostic_info *diagnostic) pp_format (this->printer, &diagnostic->message, m_urlifier); m_output_format->on_begin_diagnostic (*diagnostic); - pp_output_formatted_text (this->printer); + pp_output_formatted_text (this->printer, m_urlifier); if (m_show_cwe) print_any_cwe (*diagnostic); if (m_show_rules) diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc index 859ae2a273db..de454ab7a401 100644 --- a/gcc/pretty-print.cc +++ b/gcc/pretty-print.cc @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3. If not see . */ #include "config.h" +#define INCLUDE_VECTOR #include "system.h" #include "coretypes.h" #include "intl.h" @@ -1031,7 +1032,16 @@ obstack_append_string (obstack *ostack, const char *str) obstack_grow (ostack, str, strlen (str)); } -/* Given quoted text starting at QUOTED_TEXT_START_IDX within PP's buffer, +/* Append STR to OSTACK, without a null-terminator. */ + +static void +obstack_append_string (obstack *ostack, const char *str, size_t len) +{ + obstack_grow (ostack, str, len); +} + +/* Given quoted text within the buffer OBSTACK + at the half-open interval [QUOTED_TEXT_START_IDX, QUOTED_TEXT_END_IDX), potentially use URLIFIER (if non-null) to see if there's a URL for the quoted text. @@ -1039,47 +1049,45 @@ obstack_append_string (obstack *ostack, const char *str) version of the text, using PP's settings. For example, given this is the buffer: - "this is a test `hello world" + "this is a test `hello worldTRAILING-CONTENT" .................^~~~~~~~~~~ with the quoted text starting at the 'h' of "hello world", the buffer becomes: - "this is a test `BEGIN_URL(URL)hello worldEND(URL)" + "this is a test `BEGIN_URL(URL)hello worldEND(URL)TRAILING-CONTENT" .................^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .................-----------replacement----------- -*/ -static void + Return the new offset into the buffer of the quoted text endpoint i.e. + the offset of "TRAILING-CONTENT" in the above. */ + +static size_t urlify_quoted_string (pretty_printer *pp, + obstack *obstack, const urlifier *urlifier, - size_t quoted_text_start_idx) + size_t quoted_text_start_idx, + size_t quoted_text_end_idx) { if (pp->url_format == URL_FORMAT_NONE) - return; + return quoted_text_end_idx; if (!urlifier) - return; + return quoted_text_end_idx; - output_buffer * const buffer = pp_buffer (pp); - - /* Get end of quoted string. */ - const size_t close_quote_idx - = obstack_object_size (&buffer->chunk_obstack); - gcc_assert (close_quote_idx >= quoted_text_start_idx); - if (close_quote_idx == quoted_text_start_idx) + const size_t quoted_len = quoted_text_end_idx - quoted_text_start_idx; + if (quoted_len == 0) /* Empty quoted string; do nothing. */ - return; - const size_t len = close_quote_idx - quoted_text_start_idx; - const char *start = (buffer->chunk_obstack.object_base - + quoted_text_start_idx); - char *url = urlifier->get_url_for_quoted_text (start, len); + return quoted_text_end_idx; + const char *start = (obstack->object_base + quoted_text_start_idx); + char *url = urlifier->get_url_for_quoted_text (start, quoted_len); if (!url) /* No URL for this quoted text; do nothing. */ - return; + return quoted_text_end_idx; - /* Stash a copy of the quoted text. */ - char *text = xstrndup (start, len); + /* Stash a copy of the remainder of the chunk. */ + char *text = xstrndup (start, + obstack_object_size (obstack) - quoted_text_start_idx); /* Replace quoted text... */ - buffer->chunk_obstack.next_free -= len; + obstack->next_free = obstack->object_base + quoted_text_start_idx; /* ...with URLified version of the text. */ /* Begin URL. */ @@ -1089,27 +1097,136 @@ urlify_quoted_string (pretty_printer *pp, case URL_FORMAT_NONE: gcc_unreachable (); case URL_FORMAT_ST: - obstack_append_string (&buffer->chunk_obstack, - "\33]8;;"); - obstack_append_string (&buffer->chunk_obstack, url); - obstack_append_string (&buffer->chunk_obstack, - "\33\\"); + obstack_append_string (obstack, "\33]8;;"); + obstack_append_string (obstack, url); + obstack_append_string (obstack, "\33\\"); break; case URL_FORMAT_BEL: - obstack_append_string (&buffer->chunk_obstack, - "\33]8;;"); - obstack_append_string (&buffer->chunk_obstack, url); - obstack_append_string (&buffer->chunk_obstack, - "\a"); + obstack_append_string (obstack, "\33]8;;"); + obstack_append_string (obstack, url); + obstack_append_string (obstack, "\a"); break; } - /* Add the text back. */ - obstack_append_string (&buffer->chunk_obstack, text); + /* Add back the quoted part of the text. */ + obstack_append_string (obstack, text, quoted_len); /* End URL. */ - obstack_append_string (&buffer->chunk_obstack, + obstack_append_string (obstack, get_end_url_string (pp)); + + size_t new_end_idx = obstack_object_size (obstack); + + /* Add back the remainder of the text after the quoted part. */ + obstack_append_string (obstack, text + quoted_len); free (text); free (url); + return new_end_idx; +} + +/* A class for tracking quoted text within a buffer for + use by a urlifier. */ + +class quoting_info +{ +public: + /* Called when quoted text is begun in phase 1 or 2. */ + void on_begin_quote (const output_buffer &buf, + unsigned chunk_idx) + { + /* Stash location of start of quoted string. */ + size_t byte_offset = obstack_object_size (&buf.chunk_obstack); + m_loc_last_open_quote = location (chunk_idx, byte_offset); + } + + /* Called when quoted text is ended in phase 1 or 2. */ + void on_end_quote (pretty_printer *pp, + output_buffer &buf, + unsigned chunk_idx, + const urlifier &urlifier) + { + /* If possible, do urlification now. */ + if (chunk_idx == m_loc_last_open_quote.m_chunk_idx) + { + urlify_quoted_string (pp, + &buf.chunk_obstack, + &urlifier, + m_loc_last_open_quote.m_byte_offset, + obstack_object_size (&buf.chunk_obstack)); + m_loc_last_open_quote = location (); + return; + } + /* Otherwise the quoted text straddles multiple chunks. + Stash the location of end of quoted string for use in phase 3. */ + size_t byte_offset = obstack_object_size (&buf.chunk_obstack); + m_phase_3_quotes.push_back (run (m_loc_last_open_quote, + location (chunk_idx, byte_offset))); + m_loc_last_open_quote = location (); + } + + bool has_phase_3_quotes_p () const + { + return m_phase_3_quotes.size () > 0; + } + void handle_phase_3 (pretty_printer *pp, + const urlifier &urlifier); + +private: + struct location + { + location () + : m_chunk_idx (UINT_MAX), + m_byte_offset (SIZE_MAX) + { + } + + location (unsigned chunk_idx, + size_t byte_offset) + : m_chunk_idx (chunk_idx), + m_byte_offset (byte_offset) + { + } + + unsigned m_chunk_idx; + size_t m_byte_offset; + }; + + struct run + { + run (location start, location end) + : m_start (start), m_end (end) + { + } + + location m_start; + location m_end; + }; + + location m_loc_last_open_quote; + std::vector m_phase_3_quotes; +}; + +static void +on_begin_quote (const output_buffer &buf, + unsigned chunk_idx, + const urlifier *urlifier) +{ + if (!urlifier) + return; + if (!buf.cur_chunk_array->m_quotes) + buf.cur_chunk_array->m_quotes = new quoting_info (); + buf.cur_chunk_array->m_quotes->on_begin_quote (buf, chunk_idx); +} + +static void +on_end_quote (pretty_printer *pp, + output_buffer &buf, + unsigned chunk_idx, + const urlifier *urlifier) +{ + if (!urlifier) + return; + if (!buf.cur_chunk_array->m_quotes) + buf.cur_chunk_array->m_quotes = new quoting_info (); + buf.cur_chunk_array->m_quotes->on_end_quote (pp, buf, chunk_idx, *urlifier); } /* The following format specifiers are recognized as being client independent: @@ -1161,11 +1278,12 @@ urlify_quoted_string (pretty_printer *pp, "before % after" with a URLIFIER that has a URL for "quoted" might be emitted as: "before `BEGIN_URL(http://example.com)quotedEND_URL' after" - This only works for message fragments that are: + This is handled here for message fragments that are: - quoted entirely in phase 1 (e.g. "%"), or - quoted entirely in phase 2 (e.g. "%qs"), - but *not* in strings that use a mixture of both phases - (e.g. "%"). */ + Quoted fragments that use a mixture of both phases + (e.g. "%") + are stashed into the output_buffer's m_quotes for use in phase 3. */ void pp_format (pretty_printer *pp, @@ -1182,12 +1300,11 @@ pp_format (pretty_printer *pp, bool any_unnumbered = false, any_numbered = false; const char **formatters[PP_NL_ARGMAX]; - /* Keep track of location of last "%", if any. */ - size_t quoted_text_start_idx = 0; - /* Allocate a new chunk structure. */ new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info); + new_chunk_array->prev = buffer->cur_chunk_array; + new_chunk_array->m_quotes = nullptr; buffer->cur_chunk_array = new_chunk_array; args = new_chunk_array->args; @@ -1229,20 +1346,14 @@ pp_format (pretty_printer *pp, obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr)); p++; - /* Stash offset of start of quoted string. */ - quoted_text_start_idx - = obstack_object_size (&buffer->chunk_obstack); - + on_begin_quote (*buffer, chunk, urlifier); continue; } case '>': { - if (quoted_text_start_idx) - { - urlify_quoted_string (pp, urlifier, quoted_text_start_idx); - quoted_text_start_idx = 0; - } + on_end_quote (pp, *buffer, chunk, urlifier); + const char *colorstr = colorize_stop (pp_show_color (pp)); obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr)); } @@ -1282,14 +1393,7 @@ pp_format (pretty_printer *pp, default: /* Handled in phase 2. Terminate the plain chunk here. */ obstack_1grow (&buffer->chunk_obstack, '\0'); - gcc_assert (chunk < PP_NL_ARGMAX * 2); args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *); - /* We can't yet handle urlifying quoted strings that use - a combination of phase 1 and phase 2 e.g. - "did you mean %<-%s%>". - Stop any phase 1 quoted text if there are going to be any - phase 2 quoted chunks. */ - quoted_text_start_idx = 0; break; } @@ -1392,7 +1496,6 @@ pp_format (pretty_printer *pp, bool plus = false; bool hash = false; bool quote = false; - quoted_text_start_idx = 0; /* We do not attempt to enforce any ordering on the modifier characters. */ @@ -1435,8 +1538,7 @@ pp_format (pretty_printer *pp, if (quote) { pp_begin_quote (pp, pp_show_color (pp)); - quoted_text_start_idx - = obstack_object_size (&buffer->chunk_obstack); + on_begin_quote (*buffer, chunk, urlifier); } switch (*p) @@ -1608,11 +1710,7 @@ pp_format (pretty_printer *pp, if (quote) { - if (quoted_text_start_idx) - { - urlify_quoted_string (pp, urlifier, quoted_text_start_idx); - quoted_text_start_idx = 0; - } + on_end_quote (pp, *buffer, chunk, urlifier); pp_end_quote (pp, pp_show_color (pp)); } @@ -1636,9 +1734,136 @@ pp_format (pretty_printer *pp, pp_clear_state (pp); } -/* Format of a message pointed to by TEXT. */ +struct auto_obstack +{ + auto_obstack () + { + obstack_init (&m_obstack); + } + + ~auto_obstack () + { + obstack_free (&m_obstack, NULL); + } + + void grow (const void *src, size_t length) + { + obstack_grow (&m_obstack, src, length); + } + + void *object_base () const + { + return m_obstack.object_base; + } + + size_t object_size () const + { + return obstack_object_size (&m_obstack); + } + + obstack m_obstack; +}; + +/* Subroutine of pp_output_formatted_text for the awkward case where + quoted text straddles multiple chunks. + + Flush PP's buffer's chunks to PP's output buffer, whilst inserting + URLs for any quoted text that should be URLified. + + For example, given: + | pp_format (pp, + | "unrecognized option %qs; did you mean %<-%s%>", + | "foo", "foption"); + we would have these chunks: + | chunk 0: "unrecognized option " + | chunk 1: "`foo'" (already checked for urlification) + | chunk 2: "; did you mean `-" + | ^* + | chunk 3: "foption" + | ******* + | chunk 4: "'" + | ^ + and this quoting_info would have recorded the open quote near the end + of chunk 2 and close quote at the start of chunk 4; this function would + check the combination of the end of chunk 2 and all of chunk 3 ("-foption") + for urlification. */ + +void +quoting_info::handle_phase_3 (pretty_printer *pp, + const urlifier &urlifier) +{ + unsigned int chunk; + output_buffer * const buffer = pp_buffer (pp); + struct chunk_info *chunk_array = buffer->cur_chunk_array; + const char **args = chunk_array->args; + + /* We need to construct the string into an intermediate buffer + for this case, since using pp_string can introduce prefixes + and line-wrapping, and omit whitespace at the start of lines. */ + auto_obstack combined_buf; + + /* Iterate simultaneously through both + - the chunks and + - the runs of quoted characters + Accumulate text from the chunks into combined_buf, and handle + runs of quoted characters when handling the chunks they + correspond to. */ + size_t start_of_run_byte_offset = 0; + std::vector::const_iterator iter_run + = buffer->cur_chunk_array->m_quotes->m_phase_3_quotes.begin (); + std::vector::const_iterator end_runs + = buffer->cur_chunk_array->m_quotes->m_phase_3_quotes.end (); + for (chunk = 0; args[chunk]; chunk++) + { + size_t start_of_chunk_idx = combined_buf.object_size (); + + combined_buf.grow (args[chunk], strlen (args[chunk])); + + if (iter_run != end_runs + && chunk == iter_run->m_end.m_chunk_idx) + { + /* A run is ending; consider for it urlification. */ + const size_t end_of_run_byte_offset + = start_of_chunk_idx + iter_run->m_end.m_byte_offset; + const size_t end_offset + = urlify_quoted_string (pp, + &combined_buf.m_obstack, + &urlifier, + start_of_run_byte_offset, + end_of_run_byte_offset); + + /* If URLification occurred it will have grown the buffer. + We need to update start_of_chunk_idx so that offsets + relative to it are still correct, for the case where + we have a chunk that both ends a quoted run and starts + another quoted run. */ + gcc_assert (end_offset >= end_of_run_byte_offset); + start_of_chunk_idx += end_offset - end_of_run_byte_offset; + + iter_run++; + } + if (iter_run != end_runs + && chunk == iter_run->m_start.m_chunk_idx) + { + /* Note where the run starts w.r.t. the composed buffer. */ + start_of_run_byte_offset + = start_of_chunk_idx + iter_run->m_start.m_byte_offset; + } + } + + /* Now print to PP. */ + const char *start + = static_cast (combined_buf.object_base ()); + pp_maybe_wrap_text (pp, start, start + combined_buf.object_size ()); +} + +/* Format of a message pointed to by TEXT. + If URLIFIER is non-null then use it on any quoted text that was not + handled in phases 1 or 2 to potentially add URLs. */ + void -pp_output_formatted_text (pretty_printer *pp) +pp_output_formatted_text (pretty_printer *pp, + const urlifier *urlifier) { unsigned int chunk; output_buffer * const buffer = pp_buffer (pp); @@ -1649,11 +1874,20 @@ pp_output_formatted_text (pretty_printer *pp) /* This is a third phase, first 2 phases done in pp_format_args. Now we actually print it. */ - for (chunk = 0; args[chunk]; chunk++) - pp_string (pp, args[chunk]); + + /* If we have any deferred urlification, handle it now. */ + if (urlifier + && pp->url_format != URL_FORMAT_NONE + && buffer->cur_chunk_array->m_quotes + && buffer->cur_chunk_array->m_quotes->has_phase_3_quotes_p ()) + buffer->cur_chunk_array->m_quotes->handle_phase_3 (pp, *urlifier); + else + for (chunk = 0; args[chunk]; chunk++) + pp_string (pp, args[chunk]); /* Deallocate the chunk structure and everything after it (i.e. the associated series of formatted strings). */ + delete buffer->cur_chunk_array->m_quotes; buffer->cur_chunk_array = chunk_array->prev; obstack_free (&buffer->chunk_obstack, chunk_array); } @@ -2800,7 +3034,7 @@ pp_printf_with_urlifier (pretty_printer *pp, va_start (ap, msg); text_info text (msg, &ap, errno); pp_format (pp, &text, urlifier); - pp_output_formatted_text (pp); + pp_output_formatted_text (pp, urlifier); va_end (ap); } @@ -2876,9 +3110,71 @@ test_urlification () pp_printf_with_urlifier (&pp, &urlifier, "foo %<-f%s%> bar", "option"); - /* We don't support this, but make sure we don't crash. */ ASSERT_STREQ - ("foo `-foption' bar", + ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar", + pp_formatted_text (&pp)); + } + + /* Likewise, where there is trailing phase 1 content within the + quoted region. */ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_printf_with_urlifier (&pp, &urlifier, + "foo %<-f%sion%> bar %<-f%sion%> baz", + "opt", "opt"); + ASSERT_STREQ + ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' baz", + pp_formatted_text (&pp)); + } + + /* Likewise. */ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_printf_with_urlifier (&pp, &urlifier, + "foo %<%sption%> bar %<-f%sion%> baz", + "-fo", "opt"); + ASSERT_STREQ + ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' baz", + pp_formatted_text (&pp)); + } + + /* Another mixed usage of %< and %s, where the quoted string is built + between a mixture of phase 1 and multiple phase 2. */ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_printf_with_urlifier (&pp, &urlifier, + "foo %<-f%s%s%> bar", + "opt", "ion"); + ASSERT_STREQ + ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar", + pp_formatted_text (&pp)); + } + + /* Mixed usage of %< and %s with a prefix. */ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_set_prefix (&pp, xstrdup ("PREFIX")); + pp_printf_with_urlifier (&pp, &urlifier, + "foo %<-f%s%> bar", + "option"); + ASSERT_STREQ + ("PREFIXfoo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar", + pp_formatted_text (&pp)); + } + + /* Example of mixed %< and %s with numbered args. */ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_printf_with_urlifier (&pp, &urlifier, + "foo %<-f%2$st%1$sn%> bar", + "io", "op"); + ASSERT_STREQ + ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar", pp_formatted_text (&pp)); } } diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h index 4548c25a42de..14a225eefe06 100644 --- a/gcc/pretty-print.h +++ b/gcc/pretty-print.h @@ -69,6 +69,8 @@ enum diagnostic_prefixing_rule_t DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2 }; +class quoting_info; + /* The chunk_info data structure forms a stack of the results from the first phase of formatting (pp_format) which have not yet been output (pp_output_formatted_text). A stack is necessary because @@ -86,6 +88,10 @@ struct chunk_info text, and the third phase simply emits all the chunks in sequence with appropriate line-wrapping. */ const char *args[PP_NL_ARGMAX * 2]; + + /* If non-null, information on quoted text runs within the chunks + for use by a urlifier. */ + quoting_info *m_quotes; }; /* The output buffer datatype. This is best seen as an abstract datatype @@ -409,7 +415,8 @@ extern void pp_flush (pretty_printer *); extern void pp_really_flush (pretty_printer *); extern void pp_format (pretty_printer *, text_info *, const urlifier * = nullptr); -extern void pp_output_formatted_text (pretty_printer *); +extern void pp_output_formatted_text (pretty_printer *, + const urlifier * = nullptr); extern void pp_format_verbatim (pretty_printer *, text_info *); extern void pp_indent (pretty_printer *); From patchwork Wed Jan 10 13:41:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 186883 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp796547dyi; Wed, 10 Jan 2024 05:42:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IHlqTKd4ZrdjyjB6MUlODEylCP5P1lQxjDohGzLMn/kfJMJRl9Zy6zD0Hg/XyNYipLc9W7T X-Received: by 2002:a05:620a:672:b0:781:4d22:da3 with SMTP id a18-20020a05620a067200b007814d220da3mr995595qkh.138.1704894136947; Wed, 10 Jan 2024 05:42:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704894136; cv=pass; d=google.com; s=arc-20160816; b=aGliz/3dIWv2abvSxYtCfYmfRf2HZ8lSow22OvFegn+Exj61dFGolqvoF7OlBt3v0u G4p8BzSYJMELKDUA3mIVTZHu0yh4D4hRKBy5/X9afNDv5KZlB2WbQLh6wy5mppgjUQjd aTq/QknTOLp8vx0UBl91EwM6GkYvP2nR5tOeHNO0VFMfYWaRa3c+VC4hXGTXay25gwJV F+JrnEJV6kWGfPKWjRQwwK4abi6ys/BClBdNMZgXfhUsO5Jq1GSXIhsrsZbBWYmCnqan pZkIGClm5By4Z+UlfEreByJgCdBjCOSBKT+WCxbTcjXjb2T4r2FWY2qa8e8usfna8cgw Jw5A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=R5gZQVaXk2bnUTOVNqPW/n/4l3VZ7uwJ0ShvMVKjEys=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=hNhNZ7TjkLBbEcZWHOR1ubqCDHXMEfoLct6OILGMJw+tpe5qSXxKD2zNjPBudckDdR ISeTeGQ0mmwyRbPHLoxFKtV3FjWazOz6vavQ1NNwUR+/tAO5eWQ+fl9Fx2yapY9bQzGr mK+YJiW4E909JpG4jw8QI4/VoqVtKsZuyfYRSn5zXgG3LpGDGLAqxhoe8UZmX1mxFBYh th4/U/0+EhZvLmgcAd+PK5CSff3/flU3slMV/MEkiq81rbsFMHunPlPfKgmBY4D5YSQO 7ZkcEr4gEjEULxPw5MKuFhJjB+BUW8LBY+m2sIhTqoRexvgNKUJ4ZnjYQ+6G4cNKYitp G0KQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LB7MIJej; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id vz27-20020a05620a495b00b007811f296177si3956642qkn.192.2024.01.10.05.42.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 05:42:16 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LB7MIJej; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A23F4385802E for ; Wed, 10 Jan 2024 13:42:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 1A43E38582B0 for ; Wed, 10 Jan 2024 13:41:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A43E38582B0 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 1A43E38582B0 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=1704894078; cv=none; b=u+7+LgqTYJuJMULNLgc5/fMckh2xnnO0diZdM/ldzz7AKy07iyWsiElAEeEvPZH71BsuDluk0ZIsXlj0RmgOVP5UUi+CZzMoS7Zr9X0v/PcmZAmn62VXN4xr/U8/bWmo/dFEFU3EDLpTrUzE+Ci+PY9ZFJwQGd0mI2w9Y7sAXME= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704894078; c=relaxed/simple; bh=/EQ4/LPGL4qwfvc6iOfsF81Uo7YGy+Y9bEDnyO+gBfU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=iJzI2FAB9bGnA2ahIEtFLCJBmQO2PHTZcRY9Vuya/pjjG96qXgRQYS4h9rfBGxEb6nJrxvpks6OUj3U40S2SDxG/jFUSsfN8huJqh/1BtFk4Xmd3XeefR5hXhehoxCTbDQp/kxB+xpJbwWOjgZ1RHnpn5Trf3uyLc0PHthCV36s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704894075; 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=R5gZQVaXk2bnUTOVNqPW/n/4l3VZ7uwJ0ShvMVKjEys=; b=LB7MIJejDe11GKRjWuFK4+Grxsry3TweBEmuJ6jE+F6RM/XbgG8d3C+oaske/jDGFYSn7u whKFLGputqyNuH8CTtbtu/XfsE6X4r9CaRynojdrC6PfQs9gZurhv8CQ9DVwjpHgACBSmB kyKY8PpioVxr1xvU352F7SZY3I4GBeY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-23ebBNLuMDWsKDHO_Yq-PQ-1; Wed, 10 Jan 2024 08:41:08 -0500 X-MC-Unique: 23ebBNLuMDWsKDHO_Yq-PQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5622C831525 for ; Wed, 10 Jan 2024 13:41:08 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.251]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E1401121306; Wed, 10 Jan 2024 13:41:08 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed 3/3] gcc-urlifier: handle option prefixes such as '-fno-' Date: Wed, 10 Jan 2024 08:41:05 -0500 Message-Id: <20240110134105.749310-3-dmalcolm@redhat.com> In-Reply-To: <20240110134105.749310-1-dmalcolm@redhat.com> References: <20240110134105.749310-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787711074854580992 X-GMAIL-MSGID: 1787711074854580992 Given e.g. this missppelled option (omitting the trailing 's'): $ LANG=C ./xgcc -B. -fno-inline-small-function xgcc: error: unrecognized command-line option '-fno-inline-small-function'; did you mean '-fno-inline-small-functions'? we weren't providing a documentation URL for the suggestion. The issue is the URLification code uses find_opt, which doesn't consider the various '-fno-' prefixes. This patch adds a way to find the pertinent prefix remapping and uses it when determining URLs. With this patch, the suggestion '-fno-inline-small-functions' now gets a documentation link (to that of '-finline-small-functions'). Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-7106-gbe2bf5dc93ca1e. gcc/ChangeLog: * gcc-urlifier.cc (gcc_urlifier::get_url_suffix_for_option): Handle prefix mappings before calling find_opt. (selftest::gcc_urlifier_cc_tests): Add example of urlifying a "-fno-"-prefixed command-line option. * opts-common.cc (get_option_prefix_remapping): New. * opts.h (get_option_prefix_remapping): New decl. Signed-off-by: David Malcolm --- gcc/gcc-urlifier.cc | 49 ++++++++++++++++++++++++++++++++++++++++----- gcc/opts-common.cc | 22 ++++++++++++++++++++ gcc/opts.h | 3 +++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/gcc/gcc-urlifier.cc b/gcc/gcc-urlifier.cc index 6bd176fc2483..be6459e8d7c1 100644 --- a/gcc/gcc-urlifier.cc +++ b/gcc/gcc-urlifier.cc @@ -154,11 +154,46 @@ gcc_urlifier::get_url_suffix_for_option (const char *p, size_t sz) const and skipping the leading '-'. We have a (pointer,size) pair that doesn't necessarily have a - terminator, so create a 0-terminated clone of the string. */ - gcc_assert (sz > 0); - char *tmp = xstrndup (p + 1, sz - 1); // skip the leading '-' - size_t opt = find_opt (tmp, m_lang_mask); - free (tmp); + terminator. + Additionally, we could have one of the e.g. "-Wno-" variants of + the option, which find_opt doesn't handle. + + Hence we need to create input for find_opt in a temporary buffer. */ + char *option_buffer; + + const char *new_prefix; + if (const char *old_prefix = get_option_prefix_remapping (p, sz, &new_prefix)) + { + /* We have one of the variants; generate a buffer containing a copy + that maps from the old prefix to the new prefix + e.g. given "-Wno-suffix", generate "-Wsuffix". */ + gcc_assert (old_prefix[0] == '-'); + gcc_assert (new_prefix); + gcc_assert (new_prefix[0] == '-'); + + const size_t old_prefix_len = strlen (old_prefix); + gcc_assert (old_prefix_len <= sz); + const size_t suffix_len = sz - old_prefix_len; + const size_t new_prefix_len = strlen (new_prefix); + const size_t new_sz = new_prefix_len + suffix_len + 1; + + option_buffer = (char *)xmalloc (new_sz); + memcpy (option_buffer, new_prefix, new_prefix_len); + /* Copy suffix. */ + memcpy (option_buffer + new_prefix_len, p + old_prefix_len, suffix_len); + /* Terminate. */ + option_buffer[new_prefix_len + suffix_len] = '\0'; + } + else + { + /* Otherwise we can simply create a 0-terminated clone of the string. */ + gcc_assert (sz > 0); + gcc_assert (p[0] == '-'); + option_buffer = xstrndup (p, sz); + } + + size_t opt = find_opt (option_buffer + 1, m_lang_mask); + free (option_buffer); if (opt >= N_OPTS) /* Option not recognized. */ @@ -221,6 +256,10 @@ gcc_urlifier_cc_tests () /* Check an option. */ ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fpack-struct").get (), "gcc/Code-Gen-Options.html#index-fpack-struct"); + + /* Check a "-fno-" variant of an option. */ + ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fno-inline").get (), + "gcc/Optimize-Options.html#index-finline"); } } // namespace selftest diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc index 73126cb74e0e..4a2dff243b0c 100644 --- a/gcc/opts-common.cc +++ b/gcc/opts-common.cc @@ -468,6 +468,28 @@ static const struct option_map option_map[] = { "--no-", NULL, "-f", false, true } }; +/* Given buffer P of size SZ, look for a prefix within OPTION_MAP; + if found, return the prefix and write the new prefix to *OUT_NEW_PREFIX. + Otherwise return nullptr. */ + +const char * +get_option_prefix_remapping (const char *p, size_t sz, + const char **out_new_prefix) +{ + for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++) + { + const char * const old_prefix = option_map[i].opt0; + const size_t old_prefix_len = strlen (old_prefix); + if (old_prefix_len <= sz + && !memcmp (p, old_prefix, old_prefix_len)) + { + *out_new_prefix = option_map[i].new_prefix; + return old_prefix; + } + } + return nullptr; +} + /* Helper function for gcc.cc's driver::suggest_option, for populating the vec of suggestions for misspelled options. diff --git a/gcc/opts.h b/gcc/opts.h index 64f94f63d206..82800b8f87cd 100644 --- a/gcc/opts.h +++ b/gcc/opts.h @@ -491,6 +491,9 @@ extern const struct zero_call_used_regs_opts_s extern vec help_option_arguments; +extern const char *get_option_prefix_remapping (const char *p, size_t sz, + const char **out_new_prefix); + extern void add_misspelling_candidates (auto_vec *candidates, const struct cl_option *option, const char *base_option);