/* Return a malloc'd string describing a location and the severity of the diagnostic, e.g. "foo.c:42:10: error: ". The caller is responsible for freeing the memory. */ char * diagnostic_build_prefix (diagnostic_context *context, const diagnostic_info *diagnostic) { static const char *const diagnostic_kind_text[] = { #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), #include "diagnostic.def" #undef DEFINE_DIAGNOSTIC_KIND "must-not-happen" }; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); const char *text = _(diagnostic_kind_text[diagnostic->kind]); const char *text_cs = "", *text_ce = ""; pretty_printer *pp = context->printer; if (diagnostic_kind_color[diagnostic->kind]) { text_cs = colorize_start (pp_show_color (pp), diagnostic_kind_color[diagnostic->kind]); text_ce = colorize_stop (pp_show_color (pp)); } expanded_location s = diagnostic_expand_location (diagnostic); char *location_text = diagnostic_get_location_text (context, s); char *result = build_message_string ("%s %s%s%s", location_text, text_cs, text, text_ce); free (location_text); return result; }
/* Same as diagnostic_build_prefix, but only the source FILE is given. */ char * file_name_as_prefix (diagnostic_context *context, const char *f) { const char *locus_cs = colorize_start (pp_show_color (context->printer), "locus"); const char *locus_ce = colorize_stop (pp_show_color (context->printer)); return build_message_string ("%s%s:%s ", locus_cs, f, locus_ce); }
static void custom_diagnostic_finalizer (diagnostic_context *context, diagnostic_info *diagnostic) { bool old_show_color = pp_show_color (context->printer); if (force_show_locus_color) pp_show_color (context->printer) = true; diagnostic_show_locus (context, diagnostic); pp_show_color (context->printer) = old_show_color; pp_destroy_prefix (context->printer); pp_flush (context->printer); }
/* 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); }
/* Return a malloc'd string describing a location. The caller is responsible for freeing the memory. */ char * diagnostic_build_prefix (diagnostic_context *context, const diagnostic_info *diagnostic) { static const char *const diagnostic_kind_text[] = { #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (T), #include "diagnostic.def" #undef DEFINE_DIAGNOSTIC_KIND "must-not-happen" }; static const char *const diagnostic_kind_color[] = { #define DEFINE_DIAGNOSTIC_KIND(K, T, C) (C), #include "diagnostic.def" #undef DEFINE_DIAGNOSTIC_KIND NULL }; const char *text = _(diagnostic_kind_text[diagnostic->kind]); const char *text_cs = "", *text_ce = ""; const char *locus_cs, *locus_ce; pretty_printer *pp = context->printer; if (diagnostic_kind_color[diagnostic->kind]) { text_cs = colorize_start (pp_show_color (pp), diagnostic_kind_color[diagnostic->kind]); text_ce = colorize_stop (pp_show_color (pp)); } locus_cs = colorize_start (pp_show_color (pp), "locus"); locus_ce = colorize_stop (pp_show_color (pp)); expanded_location s = expand_location_to_spelling_point (diagnostic->location); if (diagnostic->override_column) s.column = diagnostic->override_column; gcc_assert (diagnostic->kind < DK_LAST_DIAGNOSTIC_KIND); return (s.file == NULL ? build_message_string ("%s%s:%s %s%s%s", locus_cs, progname, locus_ce, text_cs, text, text_ce) : context->show_column ? build_message_string ("%s%s:%d:%d:%s %s%s%s", locus_cs, s.file, s.line, s.column, locus_ce, text_cs, text, text_ce) : build_message_string ("%s%s:%d:%s %s%s%s", locus_cs, s.file, s.line, locus_ce, text_cs, text, text_ce)); }
static char * diagnostic_get_location_text (diagnostic_context *context, expanded_location s) { pretty_printer *pp = context->printer; const char *locus_cs = colorize_start (pp_show_color (pp), "locus"); const char *locus_ce = colorize_stop (pp_show_color (pp)); if (s.file == NULL) return build_message_string ("%s%s:%s", locus_cs, progname, locus_ce); if (!strcmp (s.file, N_("<built-in>"))) return build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce); if (context->show_column) return build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line, s.column, locus_ce); else return build_message_string ("%s%s:%d:%s", locus_cs, s.file, s.line, locus_ce); }
void diagnostic_color_init (diagnostic_context *context, int value /*= -1 */) { /* value == -1 is the default value. */ if (value < 0) { /* If DIAGNOSTICS_COLOR_DEFAULT is -1, default to -fdiagnostics-color=auto if GCC_COLORS is in the environment, otherwise default to -fdiagnostics-color=never, for other values default to that -fdiagnostics-color={never,auto,always}. */ if (DIAGNOSTICS_COLOR_DEFAULT == -1) { if (!getenv ("GCC_COLORS")) return; value = DIAGNOSTICS_COLOR_AUTO; } else value = DIAGNOSTICS_COLOR_DEFAULT; } pp_show_color (context->printer) = colorize_init ((diagnostic_color_rule_t) value); }