[4/5] diagnostics: add diagnostic_context::get_location_text
Checks
Commit Message
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(-)
Comments
On Tue, 2023-11-21 at 17:20 -0500, David Malcolm wrote:
> 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.
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Not a bugfix, but low-risk and useful for libdiagnostics, so I've taken
the liberty of pushing this to gcc trunk as r14-5895-g93096d3ce14a89.
Dave
@@ -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. */
@@ -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);