aboutsummaryrefslogtreecommitdiff
path: root/gcc/json.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2023-11-14 11:01:39 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2023-11-14 11:01:39 -0500
commit070944fdd6394c992885f33f5bde8a231d37fa7f (patch)
tree64c7c7f0674a3bc0084122c1cdb674812993d75d /gcc/json.cc
parent4db820928065eccbeb725406450d826186582b9f (diff)
downloadgcc-070944fdd6394c992885f33f5bde8a231d37fa7f.zip
gcc-070944fdd6394c992885f33f5bde8a231d37fa7f.tar.gz
gcc-070944fdd6394c992885f33f5bde8a231d37fa7f.tar.bz2
json: reduce use of naked new in json-building code
No functional change intended. gcc/ChangeLog: * diagnostic-format-json.cc: Use type-specific "set_*" functions of json::object to avoid naked new of json value subclasses. * diagnostic-format-sarif.cc: Likewise. * gcov.cc: Likewise. * json.cc (object::set_string): New. (object::set_integer): New. (object::set_float): New. (object::set_bool): New. (selftest::test_writing_objects): Use object::set_string. * json.h (object::set_string): New decl. (object::set_integer): New decl. (object::set_float): New decl. (object::set_bool): New decl. * optinfo-emit-json.cc: Use type-specific "set_*" functions of json::object to avoid naked new of json value subclasses. * timevar.cc: Likewise. * tree-diagnostic-path.cc: Likewise. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/json.cc')
-rw-r--r--gcc/json.cc40
1 files changed, 38 insertions, 2 deletions
diff --git a/gcc/json.cc b/gcc/json.cc
index 741e97b..f5398ec 100644
--- a/gcc/json.cc
+++ b/gcc/json.cc
@@ -128,6 +128,42 @@ object::get (const char *key) const
return NULL;
}
+/* Set value of KEY within this object to a JSON
+ string value based on UTF8_VALUE. */
+
+void
+object::set_string (const char *key, const char *utf8_value)
+{
+ set (key, new json::string (utf8_value));
+}
+
+/* Set value of KEY within this object to a JSON
+ integer value based on V. */
+
+void
+object::set_integer (const char *key, long v)
+{
+ set (key, new json::integer_number (v));
+}
+
+/* Set value of KEY within this object to a JSON
+ floating point value based on V. */
+
+void
+object::set_float (const char *key, double v)
+{
+ set (key, new json::float_number (v));
+}
+
+/* Set value of KEY within this object to the JSON
+ literal true or false, based on V. */
+
+void
+object::set_bool (const char *key, bool v)
+{
+ set (key, new json::literal (v));
+}
+
/* class json::array, a subclass of json::value, representing
an ordered collection of values. */
@@ -311,8 +347,8 @@ static void
test_writing_objects ()
{
object obj;
- obj.set ("foo", new json::string ("bar"));
- obj.set ("baz", new json::string ("quux"));
+ obj.set_string ("foo", "bar");
+ obj.set_string ("baz", "quux");
/* This test relies on json::object writing out key/value pairs
in key-insertion order. */
assert_print_eq (obj, "{\"foo\": \"bar\", \"baz\": \"quux\"}");