From 2486234b5ae0d71ca7bbdc6e216b4707cd5bef15 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 24 Jul 2024 18:07:53 -0400 Subject: json: support std::unique_ptr in array::append and object::set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch uses templates to add overloads of json::array::append and json::object::set taking std::unique_ptr 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’ to ‘unique_ptr’ 629 | location_obj->set ("message", std::move (message_obj)); | ~~~~~~~~~~^~~~~~~~~~~~~ | | | unique_ptr 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 (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 --- gcc/gcov.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'gcc/gcov.cc') diff --git a/gcc/gcov.cc b/gcc/gcov.cc index 85fdac4..aa016c6 100644 --- a/gcc/gcov.cc +++ b/gcc/gcov.cc @@ -32,6 +32,7 @@ along with Gcov; see the file COPYING3. If not see #include "config.h" #define INCLUDE_ALGORITHM +#define INCLUDE_MEMORY #define INCLUDE_VECTOR #define INCLUDE_STRING #define INCLUDE_MAP -- cgit v1.1