From f1d022dcbaa226d80035b686e67d4648740fa0bb Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 7 Apr 2020 07:17:29 -0400 Subject: [PATCH 012/179] FIXME: try colorizing JSON output --- gcc/diagnostic-color.c | 3 +++ gcc/json.cc | 27 ++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/gcc/diagnostic-color.c b/gcc/diagnostic-color.c index b1baded2c9e..25a1bffb4d6 100644 --- a/gcc/diagnostic-color.c +++ b/gcc/diagnostic-color.c @@ -98,6 +98,9 @@ static struct color_cap color_dict[] = { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false }, { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false }, { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false }, + { "json-punct", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 10, false }, + { "json-key", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 8, false }, + { "json-value", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 10, false }, { NULL, NULL, 0, false } }; diff --git a/gcc/json.cc b/gcc/json.cc index 627741db7f0..2f01e03007d 100644 --- a/gcc/json.cc +++ b/gcc/json.cc @@ -63,17 +63,18 @@ void object::print (pretty_printer *pp) const { /* Note that the order is not guaranteed. */ - pp_character (pp, '{'); + pp_printf (pp, "%r{%R", "json-punct"); for (map_t::iterator it = m_map.begin (); it != m_map.end (); ++it) { if (it != m_map.begin ()) - pp_string (pp, ", "); + pp_printf (pp, "%r,%R ", "json-punct"); const char *key = const_cast ((*it).first); value *value = (*it).second; - pp_printf (pp, "\"%s\": ", key); // FIXME: escaping? + pp_printf (pp, "%r\"%R\%r%s%R%r\":%R ", + "json-punct", "json-key", key, "json-punct"); value->print (pp); } - pp_character (pp, '}'); + pp_printf (pp, "%r}%R", "json-punct"); } /* Set the json::value * for KEY, taking ownership of V @@ -133,16 +134,16 @@ array::~array () void array::print (pretty_printer *pp) const { - pp_character (pp, '['); + pp_printf (pp, "%r[%R", "json-punct"); unsigned i; value *v; FOR_EACH_VEC_ELT (m_elements, i, v) { if (i) - pp_string (pp, ", "); + pp_printf (pp, "%r, %R", "json-punct"); v->print (pp); } - pp_character (pp, ']'); + pp_printf (pp, "%r]%R", "json-punct"); } /* Append non-NULL value V to a json::array, taking ownership of V. */ @@ -163,7 +164,7 @@ float_number::print (pretty_printer *pp) const { char tmp[1024]; snprintf (tmp, sizeof (tmp), "%g", m_value); - pp_string (pp, tmp); + pp_printf (pp, "%r%s%R", "json-value", tmp); } /* class json::integer_number, a subclass of json::value, wrapping a long. */ @@ -175,7 +176,7 @@ integer_number::print (pretty_printer *pp) const { char tmp[1024]; snprintf (tmp, sizeof (tmp), "%ld", m_value); - pp_string (pp, tmp); + pp_printf (pp, "%r%s%R", "json-value", tmp); } @@ -194,7 +195,8 @@ string::string (const char *utf8) void string::print (pretty_printer *pp) const { - pp_character (pp, '"'); + pp_printf (pp, "%r\"%R", "json-punct"); + pp_printf (pp, "%r", "json-value"); for (const char *ptr = m_utf8; *ptr; ptr++) { char ch = *ptr; @@ -226,7 +228,8 @@ string::print (pretty_printer *pp) const pp_character (pp, ch); } } - pp_character (pp, '"'); + pp_printf (pp, "%R"); + pp_printf (pp, "%r\"%R", "json-punct"); } /* class json::literal, a subclass of json::value. */ @@ -236,6 +239,7 @@ string::print (pretty_printer *pp) const void literal::print (pretty_printer *pp) const { + pp_printf (pp, "%r", "json-value"); switch (m_kind) { case JSON_TRUE: @@ -250,6 +254,7 @@ literal::print (pretty_printer *pp) const default: gcc_unreachable (); } + pp_printf (pp, "%R"); } -- 2.21.0