public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org, binutils@sourceware.org
Cc: Nick Clifton <nickc@redhat.com>,
	Simon Sobisch <simonsobisch@gnu.org>,
	David Malcolm <dmalcolm@redhat.com>
Subject: [PATCH 4/5] diagnostics: add diagnostic_context::get_location_text
Date: Tue, 21 Nov 2023 17:20:17 -0500	[thread overview]
Message-ID: <20231121222019.646253-5-dmalcolm@redhat.com> (raw)
In-Reply-To: <20231121222019.646253-1-dmalcolm@redhat.com>

No functional change intended.

gcc/ChangeLog:
	* diagnostic.cc (diagnostic_get_location_text): Convert to...
	(diagnostic_context::get_location_text): ...this, and convert
	return type from char * to label_text.
	(diagnostic_build_prefix): Update for above change.
	(default_diagnostic_start_span_fn): Likewise.
	(selftest::assert_location_text): Likewise.
	* diagnostic.h (diagnostic_context::get_location_text): New decl.
---
 gcc/diagnostic.cc | 35 +++++++++++++++--------------------
 gcc/diagnostic.h  |  2 ++
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc
index b4ebcd29457c..4f66fa6acaa8 100644
--- a/gcc/diagnostic.cc
+++ b/gcc/diagnostic.cc
@@ -558,14 +558,12 @@ maybe_line_and_column (int line, int col)
   return result;
 }
 
-/* Return a malloc'd string describing a location e.g. "foo.c:42:10".
-   The caller is responsible for freeing the memory.  */
+/* Return a string describing a location e.g. "foo.c:42:10".  */
 
-static char *
-diagnostic_get_location_text (diagnostic_context *context,
-			      expanded_location s)
+label_text
+diagnostic_context::get_location_text (const expanded_location &s) const
 {
-  pretty_printer *pp = context->printer;
+  pretty_printer *pp = this->printer;
   const char *locus_cs = colorize_start (pp_show_color (pp), "locus");
   const char *locus_ce = colorize_stop (pp_show_color (pp));
   const char *file = s.file ? s.file : progname;
@@ -574,13 +572,13 @@ diagnostic_get_location_text (diagnostic_context *context,
   if (strcmp (file, special_fname_builtin ()))
     {
       line = s.line;
-      if (context->m_show_column)
-	col = context->converted_column (s);
+      if (m_show_column)
+	col = this->converted_column (s);
     }
 
   const char *line_col = maybe_line_and_column (line, col);
-  return build_message_string ("%s%s%s:%s", locus_cs, file,
-			       line_col, locus_ce);
+  return label_text::take (build_message_string ("%s%s%s:%s", locus_cs, file,
+						 line_col, locus_ce));
 }
 
 static const char *const diagnostic_kind_text[] = {
@@ -610,12 +608,11 @@ diagnostic_build_prefix (diagnostic_context *context,
       text_ce = colorize_stop (pp_show_color (pp));
     }
 
-  expanded_location s = diagnostic_expand_location (diagnostic);
-  char *location_text = diagnostic_get_location_text (context, s);
+  const expanded_location s = diagnostic_expand_location (diagnostic);
+  label_text location_text = context->get_location_text (s);
 
-  char *result = build_message_string ("%s %s%s%s", location_text,
+  char *result = build_message_string ("%s %s%s%s", location_text.get (),
 				       text_cs, text, text_ce);
-  free (location_text);
   return result;
 }
 
@@ -1091,9 +1088,8 @@ void
 default_diagnostic_start_span_fn (diagnostic_context *context,
 				  expanded_location exploc)
 {
-  char *text = diagnostic_get_location_text (context, exploc);
-  pp_string (context->printer, text);
-  free (text);
+  label_text text = context->get_location_text (exploc);
+  pp_string (context->printer, text.get ());
   pp_newline (context->printer);
 }
 
@@ -2852,9 +2848,8 @@ assert_location_text (const char *expected_loc_text,
   xloc.data = NULL;
   xloc.sysp = false;
 
-  char *actual_loc_text = diagnostic_get_location_text (&dc, xloc);
-  ASSERT_STREQ (expected_loc_text, actual_loc_text);
-  free (actual_loc_text);
+  label_text actual_loc_text = dc.get_location_text (xloc);
+  ASSERT_STREQ (expected_loc_text, actual_loc_text.get ());
 }
 
 /* Verify that diagnostic_get_location_text works as expected.  */
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index 958068fea926..8b7f147c0d28 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -543,6 +543,8 @@ public:
     return m_option_callbacks.m_lang_mask;
   }
 
+  label_text get_location_text (const expanded_location &s) const;
+
 private:
   bool includes_seen_p (const line_map_ordinary *map);
 
-- 
2.26.3


  parent reply	other threads:[~2023-11-21 22:20 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-06 22:29 [PATCH/RFC] libdiagnostics: a shared library for emitting diagnostics David Malcolm
2023-11-06 22:29 ` [PATCH 1/2] libdiagnostics: header and examples David Malcolm
2023-11-06 22:29 ` [PATCH 2/2] libdiagnostics: work-in-progress implementation David Malcolm
2023-11-07  7:54   ` Simon Sobisch
2023-11-07 14:59     ` David Malcolm
2023-11-07 15:35       ` Simon Sobisch
2023-11-06 22:29 ` [PATCH] binutils: experimental use of libdiagnostics in gas David Malcolm
2023-11-07  7:04   ` Simon Sobisch
2023-11-07 14:51     ` David Malcolm
2023-11-07  9:21   ` Clément Chigot
2023-11-07 14:09     ` David Malcolm
2023-11-07 15:57       ` Clément Chigot
2023-11-07 16:18         ` David Malcolm
2023-11-07 10:03   ` Jan Beulich
2023-11-07 14:32     ` David Malcolm
2023-11-07 14:59       ` Jan Beulich
2023-11-21 22:20 ` [PATCH 0/6] v2 of libdiagnostics David Malcolm
2023-11-21 22:20   ` [PATCH 1/5] libdiagnostics v2: header and examples David Malcolm
2023-11-21 22:20   ` [PATCH 2/5] libdiagnostics v2: work-in-progress implementation David Malcolm
2023-11-21 22:20   ` [PATCH 3/5] libdiagnostics v2: add C++ wrapper API David Malcolm
2023-11-21 22:20   ` David Malcolm [this message]
2023-11-28  1:25     ` [PATCH 4/5] diagnostics: add diagnostic_context::get_location_text David Malcolm
2023-11-21 22:20   ` [PATCH 5/5] diagnostics: don't print annotation lines when there's no column info David Malcolm
2023-11-28  1:25     ` David Malcolm
2023-11-21 22:20   ` [PATCH] binutils: v2: experimental use of libdiagnostics in gas David Malcolm
2023-11-22  7:36     ` Jan Beulich
2023-11-21 22:35   ` [PATCH 0/6] v2 of libdiagnostics Simon Sobisch
2023-11-23 17:36   ` Pedro Alves
2024-01-27 23:28     ` Simon Sobisch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231121222019.646253-5-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=binutils@sourceware.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=nickc@redhat.com \
    --cc=simonsobisch@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).