From patchwork Tue Oct 31 21:15:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 160348 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp27099vqx; Tue, 31 Oct 2023 14:16:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQKJ8d4cVy/QJ+MiNf9WligvKB8amL15lWvJ78Ix8XSWo1ovzY3UDbCr38U5sC+DwS0aUu X-Received: by 2002:a05:620a:4088:b0:775:7703:ace5 with SMTP id f8-20020a05620a408800b007757703ace5mr16794001qko.56.1698786985137; Tue, 31 Oct 2023 14:16:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698786985; cv=pass; d=google.com; s=arc-20160816; b=ziua36B4uY9jV+wENvAZCow5uND/R9+svTOQdt79429+8Q42pzYErhHefY7iXzHr9l XcbFA0bdkDbbDpRz2oUwBs2QueNTNIvBv/FLZebLPvGa3aILEOiBaruaI6rx427XCp7w fvb3r98wu//n9YM9K9SoHp8zRpMhmOEKgx0199x3sMppgbIj5hdYR8RHJZnQLc35DYYU eZ+OtnNbAwI1uT4+783TOyB/pKsYKtsH1dUd/ZIfaNMS+VPLllXqZL6LJ2LoBhhIHdGm j3KmYk+XqgM/fohQSrp/5Z1q3DaIXXX1bN4S0s8ssj3xeUwNDIEL8XWta0dj4nBI1tlQ ig6A== 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=68F2jh/gixXiTd5EaDAwhB/PB+hMruDgrX4F+ICcUgI=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=Cm+yLwLp0QksUI3v0pBT6Kd+s3/IFf5nPxk2ugYvF9LVJDxd5ApWIelfHO6IBrV5OK EXIkJUzO8QiVwLg6rKx3qihHFw0eT4+25RfbPIGZZ2r6AoM7ZqLnMnDstWA4s2Rao/Zp HdSuW+KxU/4ljq5dN6+SGV1MwEknvnxAHq2P9G59pntIFZzc0hfbbBmVOiFyw1ujROQU SFuxgOYstAnCDMseIL1IxTkI+Su+QS4OxiaXl1tMnrKBFemDCEsvaZ2XFSd7dNcj7Wxu N4HYN5n0Isxs437V0MYFd6/VvQVZMmgTREMWGKOCvROIQACrdVbyMg1LjBJZk7Ee4inV YgkA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="bNy/upMm"; 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 rb14-20020a05620a8d0e00b00778a0d33554si1717203qkn.103.2023.10.31.14.16.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 14:16:25 -0700 (PDT) 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="bNy/upMm"; 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 60B4C3857715 for ; Tue, 31 Oct 2023 21:16:22 +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 7200A385840E for ; Tue, 31 Oct 2023 21:15:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7200A385840E 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 7200A385840E 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=1698786958; cv=none; b=KTsHLvJXct8uyVlBnloNOBFJ++lx3R5m8HDsDtB1oT+O/Vm/mxD5AeFpVfu+5pf3EP51Q31eSkIvt/6DmO9MimCSSfLFt39DyXdFQ1u9DqoyEIqmU8919UWVMrmDYiRNEkZnlQ7dX7QQWjLMYpuJz7RmXJH+//W0TcaXCWiOY5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698786958; c=relaxed/simple; bh=x5YwmGbgi+tG7gNEi97fxQt2BrpxjuPEtFQFGi3y+7w=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=K45n8kIhws2bjDap9LyxVRkxMQBETnJc3jkWX9SO8EnwCTWeHGT7VczRn64fjLS4e2tZpT3tyLpuMjZijE47qEiSG9eS61OL3+kFoTx9T8BKhSKtHEl/9RJ4Lgsq+fUyBrsj/GemWgNer+fEHFqwfMS1XpxdBjDASn/W9sB2xlo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698786956; 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=68F2jh/gixXiTd5EaDAwhB/PB+hMruDgrX4F+ICcUgI=; b=bNy/upMmxP+AL0y+I2llp7VqijdoeJpRQzfTItEI2lCiyid7musmHq1tMZo5KFeWt4e+Y4 XOUHRV+kqWNGo5Ihf9c2wTthcR/vVFqgDzHeseyVgArt7eYiOTwuZ+G7/lqpYx52Kipj6k XY6REMnTuZxC7rjUVHXsP7aQcgxH94c= 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-323-mlV_rJmAOZ2gU0AAMJmBMw-1; Tue, 31 Oct 2023 17:15:54 -0400 X-MC-Unique: mlV_rJmAOZ2gU0AAMJmBMw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 4917938117F2 for ; Tue, 31 Oct 2023 21:15:54 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.8.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1E82A2166B26; Tue, 31 Oct 2023 21:15:54 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] pretty-print: gracefully handle null URLs Date: Tue, 31 Oct 2023 17:15:52 -0400 Message-Id: <20231031211552.1907869-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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: 1781307261778839475 X-GMAIL-MSGID: 1781307261778839475 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-5047-gb9e2088d297744. gcc/ChangeLog: * pretty-print.cc (pretty_printer::pretty_printer): Initialize m_skipping_null_url. (pp_begin_url): Handle URL being null. (pp_end_url): Likewise. (selftest::test_null_urls): New. (selftest::pretty_print_cc_tests): Call it. * pretty-print.h (pretty_printer::m_skipping_null_url): New. --- gcc/pretty-print.cc | 57 +++++++++++++++++++++++++++++++++++++++++++-- gcc/pretty-print.h | 4 ++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc index 75446cc73a1..80780cfd7b8 100644 --- a/gcc/pretty-print.cc +++ b/gcc/pretty-print.cc @@ -1664,7 +1664,8 @@ pretty_printer::pretty_printer (int maximum_length) need_newline (), translate_identifiers (true), show_color (), - url_format (URL_FORMAT_NONE) + url_format (URL_FORMAT_NONE), + m_skipping_null_url (false) { pp_line_cutoff (this) = maximum_length; /* By default, we emit prefixes once per message. */ @@ -1687,7 +1688,8 @@ pretty_printer::pretty_printer (const pretty_printer &other) need_newline (other.need_newline), translate_identifiers (other.translate_identifiers), show_color (other.show_color), - url_format (other.url_format) + url_format (other.url_format), + m_skipping_null_url (false) { pp_line_cutoff (this) = maximum_length; /* By default, we emit prefixes once per message. */ @@ -2211,6 +2213,13 @@ identifier_to_locale (const char *ident) void pp_begin_url (pretty_printer *pp, const char *url) { + if (!url) + { + /* Handle null URL by skipping all output here, + and in the next pp_end_url. */ + pp->m_skipping_null_url = true; + return; + } switch (pp->url_format) { case URL_FORMAT_NONE: @@ -2254,6 +2263,13 @@ get_end_url_string (pretty_printer *pp) void pp_end_url (pretty_printer *pp) { + if (pp->m_skipping_null_url) + { + /* We gracefully handle pp_begin_url (NULL) by omitting output for + both begin and end. Here we handle the latter. */ + pp->m_skipping_null_url = false; + return; + } if (pp->url_format != URL_FORMAT_NONE) pp_string (pp, get_end_url_string (pp)); } @@ -2588,6 +2604,42 @@ test_urls () } } +/* Verify that we gracefully reject null URLs. */ + +void +test_null_urls () +{ + { + pretty_printer pp; + pp.url_format = URL_FORMAT_NONE; + pp_begin_url (&pp, nullptr); + pp_string (&pp, "This isn't a link"); + pp_end_url (&pp); + ASSERT_STREQ ("This isn't a link", + pp_formatted_text (&pp)); + } + + { + pretty_printer pp; + pp.url_format = URL_FORMAT_ST; + pp_begin_url (&pp, nullptr); + pp_string (&pp, "This isn't a link"); + pp_end_url (&pp); + ASSERT_STREQ ("This isn't a link", + pp_formatted_text (&pp)); + } + + { + pretty_printer pp; + pp.url_format = URL_FORMAT_BEL; + pp_begin_url (&pp, nullptr); + pp_string (&pp, "This isn't a link"); + pp_end_url (&pp); + ASSERT_STREQ ("This isn't a link", + pp_formatted_text (&pp)); + } +} + /* Test multibyte awareness. */ static void test_utf8 () { @@ -2637,6 +2689,7 @@ pretty_print_cc_tests () test_pp_format (); test_prefixes_and_wrapping (); test_urls (); + test_null_urls (); test_utf8 (); } diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h index 02658c8afad..8759f0def38 100644 --- a/gcc/pretty-print.h +++ b/gcc/pretty-print.h @@ -295,6 +295,10 @@ public: /* Whether URLs should be emitted, and which terminator to use. */ diagnostic_url_format url_format; + + /* If true, then we've had a pp_begin_url (nullptr), and so the + next pp_end_url should be a no-op. */ + bool m_skipping_null_url; }; inline const char *