30d3ba5142
PR c++/90462 reports an ICE with -fdiagnostics-format=json when attempting to serialize a malformed location to JSON. The compound location_t in question has meaningful "caret" and "start" locations, but has UNKNOWN_LOCATION for its "finish" location, leading to a NULL pointer dereference when attempting to build a JSON string for the filename. This patch bulletproofs the JSON output so that attempts to write a JSON object for a location with a NULL file will lead to an object with no "file" key, and attempts to write a compound location with UNKNOWN_LOCATION for its start or finish will lead to the corresponding JSON child object being omitted. This patch also adds a json::object::get member function, for self-testing the above. gcc/ChangeLog: PR c++/90462 * diagnostic-format-json.cc: Include "selftest.h". (json_from_expanded_location): Only add "file" key for non-NULL file strings. (json_from_location_range): Don't add "start" and "finish" children if they are UNKNOWN_LOCATION. (selftest::test_unknown_location): New selftest. (selftest::test_bad_endpoints): New selftest. (selftest::diagnostic_format_json_cc_tests): New function. * json.cc (json::object::get): New function. (selftest::test_object_get): New selftest. (selftest::json_cc_tests): Call it. * json.h (json::object::get): New decl. * selftest-run-tests.c (selftest::run_tests): Call selftest::diagnostic_format_json_cc_tests. * selftest.h (selftest::diagnostic_format_json_cc_tests): New decl. gcc/testsuite/ChangeLog: PR c++/90462 * g++.dg/pr90462.C: New test. From-SVN: r271535
171 lines
3.8 KiB
C++
171 lines
3.8 KiB
C++
/* JSON trees
|
|
Copyright (C) 2017-2019 Free Software Foundation, Inc.
|
|
Contributed by David Malcolm <dmalcolm@redhat.com>.
|
|
|
|
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
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef GCC_JSON_H
|
|
#define GCC_JSON_H
|
|
|
|
/* Implementation of JSON, a lightweight data-interchange format.
|
|
|
|
See http://www.json.org/
|
|
and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
|
|
and https://tools.ietf.org/html/rfc7159
|
|
|
|
Supports creating a DOM-like tree of json::value *, and then dumping
|
|
json::value * to text. */
|
|
|
|
namespace json
|
|
{
|
|
|
|
/* Forward decls of json::value and its subclasses (using indentation
|
|
to denote inheritance. */
|
|
|
|
class value;
|
|
class object;
|
|
class array;
|
|
class number;
|
|
class string;
|
|
class literal;
|
|
|
|
/* An enum for discriminating the subclasses of json::value. */
|
|
|
|
enum kind
|
|
{
|
|
/* class json::object. */
|
|
JSON_OBJECT,
|
|
|
|
/* class json::array. */
|
|
JSON_ARRAY,
|
|
|
|
/* class json::number. */
|
|
JSON_NUMBER,
|
|
|
|
/* class json::string. */
|
|
JSON_STRING,
|
|
|
|
/* class json::literal uses these three values to identify the
|
|
particular literal. */
|
|
JSON_TRUE,
|
|
JSON_FALSE,
|
|
JSON_NULL
|
|
};
|
|
|
|
/* Base class of JSON value. */
|
|
|
|
class value
|
|
{
|
|
public:
|
|
virtual ~value () {}
|
|
virtual enum kind get_kind () const = 0;
|
|
virtual void print (pretty_printer *pp) const = 0;
|
|
|
|
void dump (FILE *) const;
|
|
};
|
|
|
|
/* Subclass of value for objects: an unordered collection of
|
|
key/value pairs. */
|
|
|
|
class object : public value
|
|
{
|
|
public:
|
|
~object ();
|
|
|
|
enum kind get_kind () const FINAL OVERRIDE { return JSON_OBJECT; }
|
|
void print (pretty_printer *pp) const FINAL OVERRIDE;
|
|
|
|
void set (const char *key, value *v);
|
|
value *get (const char *key) const;
|
|
|
|
private:
|
|
typedef hash_map <char *, value *,
|
|
simple_hashmap_traits<nofree_string_hash, value *> > map_t;
|
|
map_t m_map;
|
|
};
|
|
|
|
/* Subclass of value for arrays. */
|
|
|
|
class array : public value
|
|
{
|
|
public:
|
|
~array ();
|
|
|
|
enum kind get_kind () const FINAL OVERRIDE { return JSON_ARRAY; }
|
|
void print (pretty_printer *pp) const FINAL OVERRIDE;
|
|
|
|
void append (value *v);
|
|
|
|
private:
|
|
auto_vec<value *> m_elements;
|
|
};
|
|
|
|
/* Subclass of value for numbers. */
|
|
|
|
class number : public value
|
|
{
|
|
public:
|
|
number (double value) : m_value (value) {}
|
|
|
|
enum kind get_kind () const FINAL OVERRIDE { return JSON_NUMBER; }
|
|
void print (pretty_printer *pp) const FINAL OVERRIDE;
|
|
|
|
double get () const { return m_value; }
|
|
|
|
private:
|
|
double m_value;
|
|
};
|
|
|
|
/* Subclass of value for strings. */
|
|
|
|
class string : public value
|
|
{
|
|
public:
|
|
string (const char *utf8);
|
|
~string () { free (m_utf8); }
|
|
|
|
enum kind get_kind () const FINAL OVERRIDE { return JSON_STRING; }
|
|
void print (pretty_printer *pp) const FINAL OVERRIDE;
|
|
|
|
const char *get_string () const { return m_utf8; }
|
|
|
|
private:
|
|
char *m_utf8;
|
|
};
|
|
|
|
/* Subclass of value for the three JSON literals "true", "false",
|
|
and "null". */
|
|
|
|
class literal : public value
|
|
{
|
|
public:
|
|
literal (enum kind kind) : m_kind (kind) {}
|
|
|
|
/* Construct literal for a boolean value. */
|
|
literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
|
|
|
|
enum kind get_kind () const FINAL OVERRIDE { return m_kind; }
|
|
void print (pretty_printer *pp) const FINAL OVERRIDE;
|
|
|
|
private:
|
|
enum kind m_kind;
|
|
};
|
|
|
|
} // namespace json
|
|
|
|
#endif /* GCC_JSON_H */
|