+2018-08-02 David Malcolm <dmalcolm@redhat.com>
+
+ * diagnostic-show-locus.c (diagnostic_show_locus): Use
+ pp_take_prefix when saving the existing prefix.
+ * diagnostic.c (diagnostic_append_note): Likewise.
+ * langhooks.c (lhd_print_error_function): Likewise.
+ * pretty-print.c (pp_set_prefix): Drop the "const" from "prefix"
+ param's type. Free the existing prefix.
+ (pp_take_prefix): New function.
+ (pretty_printer::pretty_printer): Drop the prefix parameter.
+ Rename the length parameter to match the comment.
+ (pretty_printer::~pretty_printer): Free the prefix.
+ * pretty-print.h (pretty_printer::pretty_printer): Drop the prefix
+ parameter.
+ (struct pretty_printer): Drop the "const" from "prefix" field's
+ type and clarify memory management.
+ (pp_set_prefix): Drop the "const" from the 2nd param.
+ (pp_take_prefix): New decl.
+
2018-08-02 Aaron Sawdey <acsawdey@linux.ibm.com>
* config/rs6000/rs6000-string.c (select_block_compare_mode): Move test
+2018-08-02 David Malcolm <dmalcolm@redhat.com>
+
+ * error.c (cxx_print_error_function): Duplicate "file" before
+ passing it to pp_set_prefix.
+ (cp_print_error_function): Use pp_take_prefix when saving the
+ existing prefix.
+
2018-08-01 Martin Sebor <msebor@redhat.com>
PR tree-optimization/86650
cxx_print_error_function (diagnostic_context *context, const char *file,
diagnostic_info *diagnostic)
{
+ char *prefix;
+ if (file)
+ prefix = xstrdup (file);
+ else
+ prefix = NULL;
lhd_print_error_function (context, file, diagnostic);
- pp_set_prefix (context->printer, file);
+ pp_set_prefix (context->printer, prefix);
maybe_print_instantiation_context (context);
}
return;
if (diagnostic_last_function_changed (context, diagnostic))
{
- const char *old_prefix = context->printer->prefix;
+ char *old_prefix = pp_take_prefix (context->printer);
const char *file = LOCATION_FILE (diagnostic_location (diagnostic));
tree abstract_origin = diagnostic_abstract_origin (diagnostic);
char *new_prefix = (file && abstract_origin == NULL)
context->last_location = loc;
- const char *saved_prefix = pp_get_prefix (context->printer);
+ char *saved_prefix = pp_take_prefix (context->printer);
pp_set_prefix (context->printer, NULL);
layout layout (context, richloc, diagnostic_kind);
{
diagnostic_info diagnostic;
va_list ap;
- const char *saved_prefix;
rich_location richloc (line_table, location);
va_start (ap, gmsgid);
va_end (ap);
return;
}
- saved_prefix = pp_get_prefix (context->printer);
+ char *saved_prefix = pp_take_prefix (context->printer);
pp_set_prefix (context->printer,
diagnostic_build_prefix (context, &diagnostic));
pp_format (context->printer, &diagnostic.message);
{
if (diagnostic_last_function_changed (context, diagnostic))
{
- const char *old_prefix = context->printer->prefix;
+ char *old_prefix = pp_take_prefix (context->printer);
tree abstract_origin = diagnostic_abstract_origin (diagnostic);
char *new_prefix = (file && abstract_origin == NULL)
? file_name_as_prefix (context, file) : NULL;
pp_buffer (pp)->line_length = 0;
}
-/* Set PREFIX for PRETTY-PRINTER. */
+/* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
+ will eventually be free-ed. */
+
void
-pp_set_prefix (pretty_printer *pp, const char *prefix)
+pp_set_prefix (pretty_printer *pp, char *prefix)
{
+ free (pp->prefix);
pp->prefix = prefix;
pp_set_real_maximum_length (pp);
pp->emitted_prefix = false;
pp_indentation (pp) = 0;
}
+/* Take ownership of PP's prefix, setting it to NULL.
+ This allows clients to save, overide, and then restore an existing
+ prefix, without it being free-ed. */
+
+char *
+pp_take_prefix (pretty_printer *pp)
+{
+ char *result = pp->prefix;
+ pp->prefix = NULL;
+ return result;
+}
+
/* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
void
pp_destroy_prefix (pretty_printer *pp)
{
if (pp->prefix != NULL)
{
- free (CONST_CAST (char *, pp->prefix));
+ free (pp->prefix);
pp->prefix = NULL;
}
}
}
}
-/* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
- characters per line. */
+/* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
-pretty_printer::pretty_printer (const char *p, int l)
+pretty_printer::pretty_printer (int maximum_length)
: buffer (new (XCNEW (output_buffer)) output_buffer ()),
prefix (),
padding (pp_none),
translate_identifiers (true),
show_color ()
{
- pp_line_cutoff (this) = l;
+ pp_line_cutoff (this) = maximum_length;
/* By default, we emit prefixes once per message. */
pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
- pp_set_prefix (this, p);
+ pp_set_prefix (this, NULL);
}
pretty_printer::~pretty_printer ()
delete m_format_postprocessor;
buffer->~output_buffer ();
XDELETE (buffer);
+ free (prefix);
}
/* Append a string delimited by START and END to the output area of
and add additional fields they need. */
struct pretty_printer
{
- // Default construct a pretty printer with specified prefix
- // and a maximum line length cut off limit.
- explicit pretty_printer (const char* = NULL, int = 0);
+ /* Default construct a pretty printer with specified
+ maximum line length cut off limit. */
+ explicit pretty_printer (int = 0);
virtual ~pretty_printer ();
/* Where we print external representation of ENTITY. */
output_buffer *buffer;
- /* The prefix for each new line. */
- const char *prefix;
+ /* The prefix for each new line. If non-NULL, this is "owned" by the
+ pretty_printer, and will eventually be free-ed. */
+ char *prefix;
/* Where to put whitespace around the entity being formatted. */
pp_padding padding;
#define pp_buffer(PP) (PP)->buffer
extern void pp_set_line_maximum_length (pretty_printer *, int);
-extern void pp_set_prefix (pretty_printer *, const char *);
+extern void pp_set_prefix (pretty_printer *, char *);
+extern char *pp_take_prefix (pretty_printer *);
extern void pp_destroy_prefix (pretty_printer *);
extern int pp_remaining_character_count_for_line (pretty_printer *);
extern void pp_clear_output_area (pretty_printer *);