public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-5047] pretty-print: gracefully handle null URLs
@ 2023-10-31 21:12 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2023-10-31 21:12 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:b9e2088d2977441818e8e24f3b497b167992dba0

commit r14-5047-gb9e2088d2977441818e8e24f3b497b167992dba0
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Tue Oct 31 17:05:40 2023 -0400

    pretty-print: gracefully handle null URLs
    
    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.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 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 75446cc73a13..80780cfd7b8b 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 02658c8afad9..8759f0def386 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 *

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-31 21:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 21:12 [gcc r14-5047] pretty-print: gracefully handle null URLs David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).