Re: [PATCH 02/10] Add JSON implementation

2018-05-30 Thread Eric Gallager
On 5/30/18, David Malcolm  wrote:
> On Wed, 2018-05-30 at 13:25 -0400, Eric Gallager wrote:
>> On 5/29/18, David Malcolm  wrote:
>> > This patch is the JSON patch I posted last year;
>> > it adds support to gcc for reading and writing JSON,
>> > based on DOM-like trees of json::value instances.
>> >
>> > This is overkill for what's needed by the rest of the
>> > patch kit (which just needs to be able to write JSON),
>> > but this code already existed, so I'm using it for now.
>> >
>>
>> I think I remember you posting this last year, but I forget where in
>> the archives it is. Could you post a link to the thread from last
>> year
>> just for reference? Thanks.
>
> It was "[PATCH 03/22] Add JSON implementation"
>   https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00417.html
> as part of:
>   "[PATCH 00/22] RFC: integrated 3rd-party static analysis support"
>https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00416.html
>
> [...snip...]
>
> Dave
>

Right, thanks! Considering that the static analysis support is a
different thing, I almost wonder if it's worth adding the JSON code
separately from either that or this, so it can be ready whenever
something else needs it...

Eric


Re: [PATCH 02/10] Add JSON implementation

2018-05-30 Thread David Malcolm
On Wed, 2018-05-30 at 13:25 -0400, Eric Gallager wrote:
> On 5/29/18, David Malcolm  wrote:
> > This patch is the JSON patch I posted last year;
> > it adds support to gcc for reading and writing JSON,
> > based on DOM-like trees of json::value instances.
> > 
> > This is overkill for what's needed by the rest of the
> > patch kit (which just needs to be able to write JSON),
> > but this code already existed, so I'm using it for now.
> > 
> 
> I think I remember you posting this last year, but I forget where in
> the archives it is. Could you post a link to the thread from last
> year
> just for reference? Thanks.

It was "[PATCH 03/22] Add JSON implementation"
  https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00417.html
as part of:
  "[PATCH 00/22] RFC: integrated 3rd-party static analysis support"
   https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00416.html

[...snip...]

Dave


Re: [PATCH 02/10] Add JSON implementation

2018-05-30 Thread Eric Gallager
On 5/29/18, David Malcolm  wrote:
> This patch is the JSON patch I posted last year;
> it adds support to gcc for reading and writing JSON,
> based on DOM-like trees of json::value instances.
>
> This is overkill for what's needed by the rest of the
> patch kit (which just needs to be able to write JSON),
> but this code already existed, so I'm using it for now.
>

I think I remember you posting this last year, but I forget where in
the archives it is. Could you post a link to the thread from last year
just for reference? Thanks.

> gcc/ChangeLog:
>   * Makefile.in (OBJS): Add json.o.
>   * json.cc: New file.
>   * json.h: New file.
>   * selftest-run-tests.c (selftest::run_tests): Call json_cc_tests.
>   * selftest.h (selftest::json_cc_tests): New decl.
> ---
>  gcc/Makefile.in  |1 +
>  gcc/json.cc  | 1914
> ++
>  gcc/json.h   |  214 ++
>  gcc/selftest-run-tests.c |1 +
>  gcc/selftest.h   |1 +
>  5 files changed, 2131 insertions(+)
>  create mode 100644 gcc/json.cc
>  create mode 100644 gcc/json.h
>
> diff --git a/gcc/Makefile.in b/gcc/Makefile.in
> index 20bee04..b3c7d5d 100644
> --- a/gcc/Makefile.in
> +++ b/gcc/Makefile.in
> @@ -1385,6 +1385,7 @@ OBJS = \
>   ira-color.o \
>   ira-emit.o \
>   ira-lives.o \
> + json.o \
>   jump.o \
>   langhooks.o \
>   lcm.o \
> diff --git a/gcc/json.cc b/gcc/json.cc
> new file mode 100644
> index 000..e0d5a76
> --- /dev/null
> +++ b/gcc/json.cc
> @@ -0,0 +1,1914 @@
> +/* JSON parsing
> +   Copyright (C) 2017 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify it under
> +the terms of the GNU General Public License as published by the Free
> +Software Foundation; either version 3, or (at your option) any later
> +version.
> +
> +GCC is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or
> +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
> +for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +.  */
> +
> +#include "config.h"
> +#include "system.h"
> +#include "coretypes.h"
> +#include "json.h"
> +#include "pretty-print.h"
> +#include "math.h"
> +#include "selftest.h"
> +
> +using namespace json;
> +
> +/* class json::value.  */
> +
> +/* Generate a char * for this json::value tree.
> +   The returned value must be freed by the caller.  */
> +
> +char *
> +value::to_str () const
> +{
> +  pretty_printer pp;
> +  print (&pp);
> +  return xstrdup (pp_formatted_text (&pp));
> +}
> +
> +/* Dump this json::value tree to OUTF.
> +   No formatting is done.  There are no guarantees about the order
> +   in which the key/value pairs of json::objects are printed.  */
> +
> +void
> +value::dump (FILE *outf) const
> +{
> +  pretty_printer pp;
> +  pp_buffer (&pp)->stream = outf;
> +  print (&pp);
> +  pp_flush (&pp);
> +}
> +
> +/* If this json::value is a json::object, return it,
> +   otherwise return NULL.  */
> +
> +const object *
> +value::as_object () const
> +{
> +  if (get_kind () != JSON_OBJECT)
> +return NULL;
> +  return static_cast  (this);
> +}
> +
> +/* If this json::value is a json::array, return it,
> +   otherwise return NULL.  */
> +
> +const array *
> +value::as_array () const
> +{
> +  if (get_kind () != JSON_ARRAY)
> +return NULL;
> +  return static_cast  (this);
> +}
> +
> +/* If this json::value is a json::number, return it,
> +   otherwise return NULL.  */
> +
> +const number *
> +value::as_number () const
> +{
> +  if (get_kind () != JSON_NUMBER)
> +return NULL;
> +  return static_cast  (this);
> +}
> +
> +/* If this json::value is a json::string, return it,
> +   otherwise return NULL.  */
> +
> +const string *
> +value::as_string () const
> +{
> +  if (get_kind () != JSON_STRING)
> +return NULL;
> +  return static_cast  (this);
> +}
> +
> +/* Attempt to get the value of a key/value pair from this value
> +   as if THIS value were an object.
> +
> +   If THIS is not a json::object, return write an error message to OUT_ERR
> +   (which must be freed by the caller) and return false.
> +
> +   Otherwise write the value ptr (possibly NULL) to OUT_VALUE and
> +   return true.  */
> +
> +bool
> +value::get_optional_value_by_key (const char *name, const value
> *&out_value,
> +   char *&out_err) const
> +{
> +  const json::object *obj = as_object ();
> +  if (!obj)
> +{
> +  out_err = xstrdup ("not an object");
> +  return false;
> +}
> +  out_value = obj->get (name);
> +  return true;
> +}
> +
> +/* Attempt to get a string value of a key/value pair from this value
> +   as if THIS value were an object.
> +
> +   If THIS is a json::object, and KEY is ei

[PATCH 02/10] Add JSON implementation

2018-05-29 Thread David Malcolm
This patch is the JSON patch I posted last year;
it adds support to gcc for reading and writing JSON,
based on DOM-like trees of json::value instances.

This is overkill for what's needed by the rest of the
patch kit (which just needs to be able to write JSON),
but this code already existed, so I'm using it for now.

gcc/ChangeLog:
* Makefile.in (OBJS): Add json.o.
* json.cc: New file.
* json.h: New file.
* selftest-run-tests.c (selftest::run_tests): Call json_cc_tests.
* selftest.h (selftest::json_cc_tests): New decl.
---
 gcc/Makefile.in  |1 +
 gcc/json.cc  | 1914 ++
 gcc/json.h   |  214 ++
 gcc/selftest-run-tests.c |1 +
 gcc/selftest.h   |1 +
 5 files changed, 2131 insertions(+)
 create mode 100644 gcc/json.cc
 create mode 100644 gcc/json.h

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 20bee04..b3c7d5d 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1385,6 +1385,7 @@ OBJS = \
ira-color.o \
ira-emit.o \
ira-lives.o \
+   json.o \
jump.o \
langhooks.o \
lcm.o \
diff --git a/gcc/json.cc b/gcc/json.cc
new file mode 100644
index 000..e0d5a76
--- /dev/null
+++ b/gcc/json.cc
@@ -0,0 +1,1914 @@
+/* JSON parsing
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "json.h"
+#include "pretty-print.h"
+#include "math.h"
+#include "selftest.h"
+
+using namespace json;
+
+/* class json::value.  */
+
+/* Generate a char * for this json::value tree.
+   The returned value must be freed by the caller.  */
+
+char *
+value::to_str () const
+{
+  pretty_printer pp;
+  print (&pp);
+  return xstrdup (pp_formatted_text (&pp));
+}
+
+/* Dump this json::value tree to OUTF.
+   No formatting is done.  There are no guarantees about the order
+   in which the key/value pairs of json::objects are printed.  */
+
+void
+value::dump (FILE *outf) const
+{
+  pretty_printer pp;
+  pp_buffer (&pp)->stream = outf;
+  print (&pp);
+  pp_flush (&pp);
+}
+
+/* If this json::value is a json::object, return it,
+   otherwise return NULL.  */
+
+const object *
+value::as_object () const
+{
+  if (get_kind () != JSON_OBJECT)
+return NULL;
+  return static_cast  (this);
+}
+
+/* If this json::value is a json::array, return it,
+   otherwise return NULL.  */
+
+const array *
+value::as_array () const
+{
+  if (get_kind () != JSON_ARRAY)
+return NULL;
+  return static_cast  (this);
+}
+
+/* If this json::value is a json::number, return it,
+   otherwise return NULL.  */
+
+const number *
+value::as_number () const
+{
+  if (get_kind () != JSON_NUMBER)
+return NULL;
+  return static_cast  (this);
+}
+
+/* If this json::value is a json::string, return it,
+   otherwise return NULL.  */
+
+const string *
+value::as_string () const
+{
+  if (get_kind () != JSON_STRING)
+return NULL;
+  return static_cast  (this);
+}
+
+/* Attempt to get the value of a key/value pair from this value
+   as if THIS value were an object.
+
+   If THIS is not a json::object, return write an error message to OUT_ERR
+   (which must be freed by the caller) and return false.
+
+   Otherwise write the value ptr (possibly NULL) to OUT_VALUE and
+   return true.  */
+
+bool
+value::get_optional_value_by_key (const char *name, const value *&out_value,
+ char *&out_err) const
+{
+  const json::object *obj = as_object ();
+  if (!obj)
+{
+  out_err = xstrdup ("not an object");
+  return false;
+}
+  out_value = obj->get (name);
+  return true;
+}
+
+/* Attempt to get a string value of a key/value pair from this value
+   as if THIS value were an object.
+
+   If THIS is a json::object, and KEY is either not present, is a string,
+   or is the "null" JSON literal, then return true, and write to OUT_VALUE.
+   If a string, then the ptr is written to OUT_VALUE, otherwise NULL
+   is written to OUT_VALUE.
+
+   If THIS is not a json::object, or KEY is not a string/"null",
+   return false and write an error message to OUT_ERR
+   (which must be freed by the caller).  */
+
+bool
+value::get_optional_string_by_key (const char *name, const char *&out_value,
+  char *&out_err) con