]> git.ipfire.org Git - thirdparty/gcc.git/commit
json: support std::unique_ptr in array::append and object::set
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 24 Jul 2024 22:07:53 +0000 (18:07 -0400)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Sun, 28 Jul 2024 17:05:54 +0000 (19:05 +0200)
commitf43be6cc9d84eb8e80c9b01d96a36e5937887253
treed2b4c592f996f0a7f8189d62b72b3cec3af9c7f4
parent336dceb5dd2d0d8bf235100af4b3c493d8ef591c
json: support std::unique_ptr in array::append and object::set

This patch uses templates to add overloads of json::array::append and
json::object::set taking std::unique_ptr<T> where T is a subclass of
json::value.

Doing so makes it much easier to track memory ownership and enforce
schema validity when constructing non-trivial JSON; using the wrong
kind of JSON value leads to compile-time errors like the following:

error: cannot convert ‘unique_ptr<sarif_message>’ to ‘unique_ptr<sarif_log>’
  629 |   location_obj->set<sarif_log> ("message", std::move (message_obj));
      |                                            ~~~~~~~~~~^~~~~~~~~~~~~
      |                                                      |
      |                                                      unique_ptr<sarif_message>

No functional change intended.

gcc/ChangeLog:
* diagnostic-format-json.cc: Define INCLUDE_MEMORY.
* diagnostic-format-sarif.cc: Likewise.
* dumpfile.cc: Likewise.
* gcov.cc: Likewise.
* json.cc: Likewise.  Include "make-unique.h".
(selftest::test_formatting): Exercise overloads of
array::append and object::set that use unique_ptr.
* json.h: Require INCLUDE_MEMORY to have been defined.
(json::object::set): Add a template to add a family of overloads
taking a std::unique_ptr<JsonType>
(json::array::append): Likewise.
* optinfo-emit-json.cc: Define INCLUDE_MEMORY.
* optinfo.cc: Likewise.
* timevar.cc: Likewise.
* toplev.cc: Likewise.
* tree-diagnostic-client-data-hooks.cc: Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostic-format-json.cc
gcc/diagnostic-format-sarif.cc
gcc/dumpfile.cc
gcc/gcov.cc
gcc/json.cc
gcc/json.h
gcc/optinfo-emit-json.cc
gcc/optinfo.cc
gcc/timevar.cc
gcc/toplev.cc
gcc/tree-diagnostic-client-data-hooks.cc