2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "pretty-print.h"
31 /* Print a JSON string to PP, escaping '"', control characters,
32 and embedded null bytes.
33 The string is required to be UTF-8 encoded. */
36 print_escaped_json_string (pretty_printer
*pp
,
40 pp_character (pp
, '"');
41 for (size_t i
= 0; i
!= len
; ++i
)
43 char ch
= utf8_str
[i
];
47 pp_string (pp
, "\\\"");
50 pp_string (pp
, "\\\\");
53 pp_string (pp
, "\\b");
56 pp_string (pp
, "\\f");
59 pp_string (pp
, "\\n");
62 pp_string (pp
, "\\r");
65 pp_string (pp
, "\\t");
68 pp_string (pp
, "\\0");
71 pp_character (pp
, ch
);
74 pp_character (pp
, '"');
77 /* class json::value. */
79 /* Dump this json::value tree to OUTF.
81 The key/value pairs of json::objects are printed in the order
82 in which the keys were originally inserted. */
85 value::dump (FILE *outf
, bool formatted
) const
88 pp_buffer (&pp
)->stream
= outf
;
89 print (&pp
, formatted
);
93 /* class json::object, a subclass of json::value, representing
94 an ordered collection of key/value pairs. */
96 /* json:object's dtor. */
100 for (map_t::iterator it
= m_map
.begin (); it
!= m_map
.end (); ++it
)
102 free (const_cast <char *>((*it
).first
));
103 delete ((*it
).second
);
107 /* Implementation of json::value::print for json::object. */
110 object::print (pretty_printer
*pp
, bool formatted
) const
112 pp_character (pp
, '{');
114 pp_indentation (pp
) += 1;
116 /* Iterate in the order that the keys were inserted. */
119 FOR_EACH_VEC_ELT (m_keys
, i
, key
)
132 map_t
&mut_map
= const_cast<map_t
&> (m_map
);
133 value
*value
= *mut_map
.get (key
);
134 print_escaped_json_string (pp
, key
, strlen (key
));
135 pp_string (pp
, ": ");
136 const int indent
= strlen (key
) + 4;
138 pp_indentation (pp
) += indent
;
139 value
->print (pp
, formatted
);
141 pp_indentation (pp
) -= indent
;
144 pp_indentation (pp
) -= 1;
145 pp_character (pp
, '}');
148 /* Set the json::value * for KEY, taking ownership of V
149 (and taking a copy of KEY if necessary). */
152 object::set (const char *key
, value
*v
)
157 value
**ptr
= m_map
.get (key
);
160 /* If the key is already present, delete the existing value
167 /* If the key wasn't already present, take a copy of the key,
168 and store the value. */
169 char *owned_key
= xstrdup (key
);
170 m_map
.put (owned_key
, v
);
171 m_keys
.safe_push (owned_key
);
175 /* Get the json::value * for KEY.
177 The object retains ownership of the value. */
180 object::get (const char *key
) const
184 value
**ptr
= const_cast <map_t
&> (m_map
).get (key
);
191 /* Set value of KEY within this object to a JSON
192 string value based on UTF8_VALUE. */
195 object::set_string (const char *key
, const char *utf8_value
)
197 set (key
, new json::string (utf8_value
));
200 /* Set value of KEY within this object to a JSON
201 integer value based on V. */
204 object::set_integer (const char *key
, long v
)
206 set (key
, new json::integer_number (v
));
209 /* Set value of KEY within this object to a JSON
210 floating point value based on V. */
213 object::set_float (const char *key
, double v
)
215 set (key
, new json::float_number (v
));
218 /* Set value of KEY within this object to the JSON
219 literal true or false, based on V. */
222 object::set_bool (const char *key
, bool v
)
224 set (key
, new json::literal (v
));
227 /* class json::array, a subclass of json::value, representing
228 an ordered collection of values. */
230 /* json::array's dtor. */
236 FOR_EACH_VEC_ELT (m_elements
, i
, v
)
240 /* Implementation of json::value::print for json::array. */
243 array::print (pretty_printer
*pp
, bool formatted
) const
245 pp_character (pp
, '[');
247 pp_indentation (pp
) += 1;
250 FOR_EACH_VEC_ELT (m_elements
, i
, v
)
263 v
->print (pp
, formatted
);
266 pp_indentation (pp
) -= 1;
267 pp_character (pp
, ']');
270 /* Append non-NULL value V to a json::array, taking ownership of V. */
273 array::append (value
*v
)
276 m_elements
.safe_push (v
);
279 /* class json::float_number, a subclass of json::value, wrapping a double. */
281 /* Implementation of json::value::print for json::float_number. */
284 float_number::print (pretty_printer
*pp
,
285 bool formatted ATTRIBUTE_UNUSED
) const
288 snprintf (tmp
, sizeof (tmp
), "%g", m_value
);
292 /* class json::integer_number, a subclass of json::value, wrapping a long. */
294 /* Implementation of json::value::print for json::integer_number. */
297 integer_number::print (pretty_printer
*pp
,
298 bool formatted ATTRIBUTE_UNUSED
) const
301 snprintf (tmp
, sizeof (tmp
), "%ld", m_value
);
306 /* class json::string, a subclass of json::value. */
308 /* json::string's ctor. */
310 string::string (const char *utf8
)
313 m_utf8
= xstrdup (utf8
);
314 m_len
= strlen (utf8
);
317 string::string (const char *utf8
, size_t len
)
320 m_utf8
= XNEWVEC (char, len
);
322 memcpy (m_utf8
, utf8
, len
);
325 /* Implementation of json::value::print for json::string. */
328 string::print (pretty_printer
*pp
,
329 bool formatted ATTRIBUTE_UNUSED
) const
331 print_escaped_json_string (pp
, m_utf8
, m_len
);
334 /* class json::literal, a subclass of json::value. */
336 /* Implementation of json::value::print for json::literal. */
339 literal::print (pretty_printer
*pp
,
340 bool formatted ATTRIBUTE_UNUSED
) const
345 pp_string (pp
, "true");
348 pp_string (pp
, "false");
351 pp_string (pp
, "null");
365 /* Verify that JV->print () prints EXPECTED_JSON. */
368 assert_print_eq (const location
&loc
,
369 const json::value
&jv
,
371 const char *expected_json
)
374 jv
.print (&pp
, formatted
);
375 ASSERT_STREQ_AT (loc
, expected_json
, pp_formatted_text (&pp
));
378 #define ASSERT_PRINT_EQ(JV, FORMATTED, EXPECTED_JSON) \
379 assert_print_eq (SELFTEST_LOCATION, JV, FORMATTED, EXPECTED_JSON)
381 /* Verify that object::get works as expected. */
387 value
*val
= new json::string ("value");
388 obj
.set ("foo", val
);
389 ASSERT_EQ (obj
.get ("foo"), val
);
390 ASSERT_EQ (obj
.get ("not-present"), NULL
);
393 /* Verify that JSON objects are written correctly. */
396 test_writing_objects ()
399 obj
.set_string ("foo", "bar");
400 obj
.set_string ("baz", "quux");
401 obj
.set_string ("\"\\\b\f\n\r\t", "value for awkward key");
403 /* This test relies on json::object writing out key/value pairs
404 in key-insertion order. */
405 ASSERT_PRINT_EQ (obj
, true,
406 "{\"foo\": \"bar\",\n"
407 " \"baz\": \"quux\",\n"
408 " \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}");
409 ASSERT_PRINT_EQ (obj
, false,
410 "{\"foo\": \"bar\", \"baz\": \"quux\""
411 ", \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}");
414 /* Verify that JSON arrays are written correctly. */
417 test_writing_arrays ()
420 ASSERT_PRINT_EQ (arr
, true, "[]");
422 arr
.append (new json::string ("foo"));
423 ASSERT_PRINT_EQ (arr
, true, "[\"foo\"]");
425 arr
.append (new json::string ("bar"));
426 ASSERT_PRINT_EQ (arr
, true,
429 ASSERT_PRINT_EQ (arr
, false,
430 "[\"foo\", \"bar\"]");
433 /* Verify that JSON numbers are written correctly. */
436 test_writing_float_numbers ()
438 ASSERT_PRINT_EQ (float_number (0), true, "0");
439 ASSERT_PRINT_EQ (float_number (42), true, "42");
440 ASSERT_PRINT_EQ (float_number (-100), true, "-100");
441 ASSERT_PRINT_EQ (float_number (123456789), true, "1.23457e+08");
445 test_writing_integer_numbers ()
447 ASSERT_PRINT_EQ (integer_number (0), true, "0");
448 ASSERT_PRINT_EQ (integer_number (42), true, "42");
449 ASSERT_PRINT_EQ (integer_number (-100), true, "-100");
450 ASSERT_PRINT_EQ (integer_number (123456789), true, "123456789");
451 ASSERT_PRINT_EQ (integer_number (-123456789), true, "-123456789");
454 /* Verify that JSON strings are written correctly. */
457 test_writing_strings ()
460 ASSERT_PRINT_EQ (foo
, true, "\"foo\"");
462 string
contains_quotes ("before \"quoted\" after");
463 ASSERT_PRINT_EQ (contains_quotes
, true, "\"before \\\"quoted\\\" after\"");
465 const char data
[] = {'a', 'b', 'c', 'd', '\0', 'e', 'f'};
466 string
not_terminated (data
, 3);
467 ASSERT_PRINT_EQ (not_terminated
, true, "\"abc\"");
468 string
embedded_null (data
, sizeof data
);
469 ASSERT_PRINT_EQ (embedded_null
, true, "\"abcd\\0ef\"");
472 /* Verify that JSON literals are written correctly. */
475 test_writing_literals ()
477 ASSERT_PRINT_EQ (literal (JSON_TRUE
), true, "true");
478 ASSERT_PRINT_EQ (literal (JSON_FALSE
), true, "false");
479 ASSERT_PRINT_EQ (literal (JSON_NULL
), true, "null");
481 ASSERT_PRINT_EQ (literal (true), true, "true");
482 ASSERT_PRINT_EQ (literal (false), true, "false");
485 /* Verify that nested values are formatted correctly when written. */
491 object
*child
= new object
;
492 object
*grandchild
= new object
;
494 obj
.set_string ("str", "bar");
495 obj
.set ("child", child
);
496 obj
.set_integer ("int", 42);
498 child
->set ("grandchild", grandchild
);
499 child
->set_integer ("int", 1776);
501 array
*arr
= new array
;
502 for (int i
= 0; i
< 3; i
++)
503 arr
->append (new integer_number (i
));
504 grandchild
->set ("arr", arr
);
505 grandchild
->set_integer ("int", 1066);
507 /* This test relies on json::object writing out key/value pairs
508 in key-insertion order. */
509 ASSERT_PRINT_EQ (obj
, true,
510 ("{\"str\": \"bar\",\n"
511 " \"child\": {\"grandchild\": {\"arr\": [0,\n"
517 ASSERT_PRINT_EQ (obj
, false,
518 ("{\"str\": \"bar\", \"child\": {\"grandchild\":"
519 " {\"arr\": [0, 1, 2], \"int\": 1066},"
520 " \"int\": 1776}, \"int\": 42}"));
523 /* Run all of the selftests within this file. */
529 test_writing_objects ();
530 test_writing_arrays ();
531 test_writing_float_numbers ();
532 test_writing_integer_numbers ();
533 test_writing_strings ();
534 test_writing_literals ();
538 } // namespace selftest
540 #endif /* #if CHECKING_P */