Esempio n. 1
0
/* Print the physical source line corresponding to the location of
   this diagnostics, and a caret indicating the precise column.  */
void
diagnostic_show_locus (diagnostic_context * context,
		       const diagnostic_info *diagnostic)
{
  const char *line;
  char *buffer;
  expanded_location s;
  int max_width;
  const char *saved_prefix;
  const char *caret_cs, *caret_ce;

  if (!context->show_caret
      || diagnostic->location <= BUILTINS_LOCATION
      || diagnostic->location == context->last_location)
    return;

  context->last_location = diagnostic->location;
  s = expand_location_to_spelling_point (diagnostic->location);
  line = location_get_source_line (s);
  if (line == NULL)
    return;

  max_width = context->caret_max_width;
  line = adjust_line (line, max_width, &(s.column));

  pp_newline (context->printer);
  saved_prefix = pp_get_prefix (context->printer);
  pp_set_prefix (context->printer, NULL);
  pp_character (context->printer, ' ');
  while (max_width > 0 && *line != '\0')
    {
      char c = *line == '\t' ? ' ' : *line;
      pp_character (context->printer, c);
      max_width--;
      line++;
    }
  pp_newline (context->printer);
  caret_cs = colorize_start (pp_show_color (context->printer), "caret");
  caret_ce = colorize_stop (pp_show_color (context->printer));

  /* pp_printf does not implement %*c.  */
  size_t len = s.column + 3 + strlen (caret_cs) + strlen (caret_ce);
  buffer = XALLOCAVEC (char, len);
  snprintf (buffer, len, "%s %*c%s", caret_cs, s.column, '^', caret_ce);
  pp_string (context->printer, buffer);
  pp_set_prefix (context->printer, saved_prefix);
}
Esempio n. 2
0
void
diagnostic_show_locus (diagnostic_context * context,
		       const diagnostic_info *diagnostic)
{
  pp_newline (context->printer);

  if (!context->show_caret
      || diagnostic_location (diagnostic, 0) <= BUILTINS_LOCATION
      || diagnostic_location (diagnostic, 0) == context->last_location)
    return;

  context->last_location = diagnostic_location (diagnostic, 0);

  const char *saved_prefix = pp_get_prefix (context->printer);
  pp_set_prefix (context->printer, NULL);

  layout layout (context, diagnostic);
  for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans ();
       line_span_idx++)
    {
      const line_span *line_span = layout.get_line_span (line_span_idx);
      if (layout.print_heading_for_line_span_index_p (line_span_idx))
	{
	  expanded_location exploc = layout.get_expanded_location (line_span);
	  context->start_span (context, exploc);
	}
      int last_line = line_span->get_last_line ();
      for (int row = line_span->get_first_line (); row <= last_line; row++)
	{
	  /* Print the source line, followed by an annotation line
	     consisting of any caret/underlines, then any fixits.
	     If the source line can't be read, print nothing.  */
	  line_bounds lbounds;
	  if (layout.print_source_line (row, &lbounds))
	    {
	      layout.print_annotation_line (row, lbounds);
	      layout.print_any_fixits (row, diagnostic->richloc);
	    }
	}
    }

  pp_set_prefix (context->printer, saved_prefix);
}