LCOV - code coverage report
Current view: top level - gcc/diagnostics - sarif-sink.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 87.9 % 2157 1895
Test Date: 2026-11-14 16:39:50 Functions: 85.7 % 189 162
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* SARIF output for diagnostics
       2              :    Copyright (C) 2018-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : 
      23              : #ifdef __MINGW32__
      24              : #include <winsock2.h>
      25              : #ifdef HAVE_AFUNIX_H
      26              : #include <afunix.h>
      27              : #else
      28              : struct sockaddr_un {
      29              :   ADDRESS_FAMILY sun_family;
      30              :   char sun_path[108];
      31              : };
      32              : #endif
      33              : #else
      34              : #include <sys/un.h>
      35              : #include <sys/socket.h>
      36              : #endif
      37              : 
      38              : #define INCLUDE_LIST
      39              : #define INCLUDE_MAP
      40              : #define INCLUDE_SET
      41              : #define INCLUDE_STRING
      42              : #define INCLUDE_VECTOR
      43              : #include "system.h"
      44              : #include "coretypes.h"
      45              : #include "diagnostics/metadata.h"
      46              : #include "diagnostics/digraphs.h"
      47              : #include "diagnostics/state-graphs.h"
      48              : #include "diagnostics/paths.h"
      49              : #include "diagnostics/sink.h"
      50              : #include "diagnostics/buffering.h"
      51              : #include "diagnostics/dumping.h"
      52              : #include "diagnostics/logging.h"
      53              : #include "json.h"
      54              : #include "cpplib.h"
      55              : #include "diagnostics/logical-locations.h"
      56              : #include "diagnostics/client-data-hooks.h"
      57              : #include "diagnostics/diagram.h"
      58              : #include "text-art/canvas.h"
      59              : #include "diagnostics/sarif-sink.h"
      60              : #include "diagnostics/text-sink.h"
      61              : #include "ordered-hash-map.h"
      62              : #include "sbitmap.h"
      63              : #include "selftest.h"
      64              : #include "diagnostics/selftest-context.h"
      65              : #include "diagnostics/selftest-source-printing.h"
      66              : #include "selftest-json.h"
      67              : #include "text-range-label.h"
      68              : #include "pretty-print-format-impl.h"
      69              : #include "pretty-print-urlifier.h"
      70              : #include "demangle.h"
      71              : #include "backtrace.h"
      72              : #include "xml.h"
      73              : #include "intl.h"
      74              : 
      75              : namespace diagnostics {
      76              : 
      77              : /* A json::array where the values are "unique" as per
      78              :    SARIF v2.1.0 section 3.7.3 ("Array properties with unique values").  */
      79              : 
      80              : template <typename JsonElementType>
      81              : class sarif_array_of_unique : public json::array
      82              : {
      83              :  public:
      84          621 :   size_t append_uniquely (std::unique_ptr<JsonElementType> val)
      85              :   {
      86              :     /* This should be O(log(n)) due to the std::map.  */
      87          621 :     auto search = m_index_by_value.find (val.get ());
      88          621 :     if (search != m_index_by_value.end())
      89          307 :       return (*search).second;
      90              : 
      91          314 :     const size_t insertion_idx = size ();
      92          314 :     m_index_by_value.insert ({val.get (), insertion_idx});
      93          314 :     append (std::move (val));
      94          314 :     return insertion_idx;
      95              :   }
      96              : 
      97              :   /* For ease of reading output, add "index": idx to all
      98              :      objects in the array.
      99              :      We don't do this until we've added everything, since
     100              :      the "index" property would otherwise confuse the
     101              :      comparison against new elements.  */
     102           68 :   void add_explicit_index_values ()
     103              :   {
     104          374 :     for (size_t idx = 0; idx < length (); ++idx)
     105          306 :       if (json::object *obj = get (idx)->dyn_cast_object ())
     106          306 :         obj->set_integer ("index", idx);
     107           68 :   }
     108              : 
     109              :   JsonElementType *
     110              :   get_element (size_t i) const
     111              :   {
     112              :     return static_cast<JsonElementType *> ((*this)[i]);
     113              :   }
     114              : 
     115              : private:
     116              :   struct comparator_t {
     117         5438 :     bool operator () (const json::value *a, const json::value *b) const
     118              :     {
     119         5438 :       gcc_assert (a);
     120         5438 :       gcc_assert (b);
     121         5438 :       return json::value::compare (*a, *b) < 0;
     122              :     }
     123              :   };
     124              : 
     125              :   // json::value * here is borrowed from m_elements
     126              :   std::map<json::value *, int, comparator_t> m_index_by_value;
     127              : };
     128              : 
     129              : /* Forward decls.  */
     130              : class sarif_builder;
     131              : class content_renderer;
     132              :   class escape_nonascii_renderer;
     133              : 
     134              : /* Subclasses of sarif_object.
     135              :    Keep these in order of their descriptions in the specification.  */
     136              : class sarif_artifact_content; // 3.3
     137              : class sarif_artifact_location; // 3.4
     138              : class sarif_message; // 3.11
     139              : class sarif_multiformat_message_string; // 3.12
     140              : class sarif_log; // 3.13
     141              : class sarif_run; // 3.14
     142              : class sarif_tool; // 3.18
     143              : class sarif_tool_component; // 3.19
     144              : class sarif_invocation; // 3.20
     145              : class sarif_artifact; // 3.24
     146              : class sarif_location_manager; // not in the spec
     147              : class sarif_result; // 3.27
     148              : class sarif_location; // 3.28
     149              : class sarif_physical_location; // 3.29
     150              : class sarif_region; // 3.30
     151              : class sarif_logical_location; // 3.33
     152              : class sarif_location_relationship; // 3.34
     153              : class sarif_code_flow; // 3.36
     154              : class sarif_thread_flow; // 3.37
     155              : class sarif_thread_flow_location; // 3.38
     156              : class sarif_reporting_descriptor; // 3.49
     157              : class sarif_reporting_descriptor_reference; // 3.53
     158              : class sarif_tool_component_reference; // 3.54
     159              : class sarif_fix; // 3.55
     160              : class sarif_artifact_change; // 3.56
     161              : class sarif_replacement; // 3.57
     162              : class sarif_ice_notification; // 3.58
     163              : 
     164              : // Valid values for locationRelationship's "kinds" property (3.34.3)
     165              : 
     166              : enum class location_relationship_kind
     167              : {
     168              :   includes,
     169              :   is_included_by,
     170              :   relevant,
     171              : 
     172              :   NUM_KINDS
     173              : };
     174              : 
     175              : /* Declarations of subclasses of sarif_object.
     176              :    Keep these in order of their descriptions in the specification.  */
     177              : 
     178              : /* Subclass of sarif_object for SARIF "artifactContent" objects
     179              :    (SARIF v2.1.0 section 3.3).  */
     180              : 
     181          943 : class sarif_artifact_content : public sarif_object {};
     182              : 
     183              : /* Subclass of sarif_object for SARIF "artifactLocation" objects
     184              :    (SARIF v2.1.0 section 3.4).  */
     185              : 
     186         1793 : class sarif_artifact_location : public sarif_object {};
     187              : 
     188              : /* Subclass of sarif_object for SARIF "message" objects
     189              :    (SARIF v2.1.0 section 3.11).  */
     190              : 
     191         2050 : class sarif_message : public sarif_object {};
     192              : 
     193              : /* Subclass of sarif_object for SARIF "multiformatMessageString" objects
     194              :    (SARIF v2.1.0 section 3.12).  */
     195              : 
     196          159 : class sarif_multiformat_message_string : public sarif_object {};
     197              : 
     198              : /* Subclass of sarif_object for SARIF "log" objects
     199              :    (SARIF v2.1.0 section 3.13).  */
     200              : 
     201          283 : class sarif_log : public sarif_object {};
     202              : 
     203              : /* Subclass of sarif_object for SARIF "run" objects
     204              :    (SARIF v2.1.0 section 3.14).  */
     205              : 
     206          283 : class sarif_run : public sarif_object {};
     207              : 
     208              : /* Subclass of sarif_object for SARIF "tool" objects
     209              :    (SARIF v2.1.0 section 3.18).  */
     210              : 
     211          283 : class sarif_tool : public sarif_object {};
     212              : 
     213              : /* Subclass of sarif_object for SARIF "toolComponent" objects
     214              :    (SARIF v2.1.0 section 3.19).  */
     215              : 
     216          317 : class sarif_tool_component : public sarif_object {};
     217              : 
     218              : /* Make a JSON string for the current date and time.
     219              :    See SARIF v2.1.0 section 3.9 "Date/time properties".
     220              :    Given that we don't run at the very beginning/end of the
     221              :    process, it doesn't make sense to be more accurate than
     222              :    the current second.  */
     223              : 
     224              : static std::unique_ptr<json::string>
     225          702 : make_date_time_string_for_current_time ()
     226              : {
     227          702 :   time_t t = time (nullptr);
     228          702 :   struct tm *tm = gmtime (&t);
     229          702 :   char buf[256];
     230          702 :   snprintf (buf, sizeof (buf) - 1,
     231              :             ("%04i-%02i-%02iT"
     232              :              "%02i:%02i:%02iZ"),
     233          702 :             tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
     234              :             tm->tm_hour, tm->tm_min, tm->tm_sec);
     235          702 :   return std::make_unique<json::string> (buf);
     236              : }
     237              : 
     238              : /* Subclass of sarif_object for SARIF "invocation" objects
     239              :    (SARIF v2.1.0 section 3.20).  */
     240              : 
     241              : class sarif_invocation : public sarif_object
     242              : {
     243              : public:
     244              :   sarif_invocation (sarif_builder &builder,
     245              :                     const char * const *original_argv);
     246              : 
     247              :   void add_notification_for_ice (const diagnostic_info &diagnostic,
     248              :                                  sarif_builder &builder,
     249              :                                  std::unique_ptr<json::object> backtrace);
     250              :   void prepare_to_flush (sarif_builder &builder);
     251              : 
     252              : private:
     253              :   std::unique_ptr<json::array> m_notifications_arr;
     254              :   bool m_success;
     255              : };
     256              : 
     257              : /* Corresponds to values for the SARIF artifact objects "roles" property.
     258              :    (SARIF v2.1.0 section 3.24.6).  */
     259              : 
     260              : enum class diagnostic_artifact_role
     261              : {
     262              :   analysis_target, /* "analysisTarget".  */
     263              :   debug_output_file, /* "debugOutputFile".  */
     264              :   result_file, /* "resultFile".  */
     265              : 
     266              :   /* "scannedFile" added in 2.2;
     267              :      see https://github.com/oasis-tcs/sarif-spec/issues/459 */
     268              :   scanned_file,
     269              : 
     270              :   traced_file, /* "tracedFile".  */
     271              : 
     272              :   NUM_ROLES
     273              : };
     274              : 
     275              : /* Subclass of sarif_object for SARIF artifact objects
     276              :    (SARIF v2.1.0 section 3.24).  */
     277              : 
     278              : class sarif_artifact : public sarif_object
     279              : {
     280              : public:
     281          437 :   sarif_artifact (const char *filename)
     282          437 :   : m_filename (filename),
     283          874 :     m_roles ((unsigned)diagnostic_artifact_role::NUM_ROLES),
     284          437 :     m_embed_contents (false)
     285              :   {
     286          437 :     bitmap_clear (m_roles);
     287          437 :   }
     288              : 
     289              :   void add_role (enum diagnostic_artifact_role role,
     290              :                  bool embed_contents);
     291              : 
     292          301 :   bool embed_contents_p () const { return m_embed_contents; }
     293              :   void populate_contents (sarif_builder &builder);
     294              :   void populate_roles ();
     295              : 
     296              : private:
     297              :   const char *m_filename;
     298              :   auto_sbitmap m_roles;
     299              : 
     300              :   /* Flag to track whether this artifact should have a "contents" property
     301              :      (SARIF v2.1.0 section 3.24.8).
     302              :      We only add the contents for those artifacts that have a location
     303              :      referencing them (so that a consumer might want to quote the source).   */
     304              :   bool m_embed_contents;
     305              : };
     306              : 
     307              : /* A class for sarif_objects that own a "namespace" of numeric IDs for
     308              :    managing location objects within them.  Currently (SARIF v2.1.0)
     309              :    this is just for sarif_result (section 3.28.2), but it will likely
     310              :    eventually also be for notification objects; see
     311              :    https://github.com/oasis-tcs/sarif-spec/issues/540
     312              : 
     313              :    Consider locations with chains of include information e.g.
     314              : 
     315              :    > include-chain-1.c:
     316              :    >   #include "include-chain-1.h"
     317              : 
     318              :    include-chain-1.h:
     319              :      | // First set of decls, which will be referenced in notes
     320              :      | #include "include-chain-1-1.h"
     321              :      |
     322              :      | // Second set of decls, which will trigger the errors
     323              :      | #include "include-chain-1-2.h"
     324              : 
     325              :    include-chain-1-1.h:
     326              :      | int p;
     327              :      | int q;
     328              : 
     329              :    include-chain-1-1.h:
     330              :      | char p;
     331              :      | char q;
     332              : 
     333              :    GCC's textual output emits:
     334              :      |   In file included from PATH/include-chain-1.h:5,
     335              :      |                    from PATH/include-chain-1.c:30:
     336              :      |   PATH/include-chain-1-2.h:1:6: error: conflicting types for 'p'; have 'char'
     337              :      |       1 | char p;
     338              :      |         |      ^
     339              :      |   In file included from PATH/include-chain-1.h:2:
     340              :      |   PATH/include-chain-1-1.h:1:5: note: previous declaration of 'p' with type 'int'
     341              :      |       1 | int p;
     342              :      |         |     ^
     343              :      |   PATH/include-chain-1-2.h:2:6: error: conflicting types for 'q'; have 'char'
     344              :      |       2 | char q;
     345              :      |         |      ^
     346              :      |   PATH/include-chain-1-1.h:2:5: note: previous declaration of 'q' with type 'int'
     347              :      |       2 | int q;
     348              :      |         |     ^
     349              : 
     350              :    Whenever a SARIF location is added for a location_t that
     351              :    was #included from somewhere, we queue up the creation of a SARIF
     352              :    location for the location of the #include.  The worklist of queued
     353              :    locations is flushed when the result is finished, which lazily creates
     354              :    any additional related locations for the include chain, and the
     355              :    relationships between the locations.  Doing so can lead to further
     356              :    include locations being processed.  The worklist approach allows us
     357              :    to lazily explore the relevant part of the directed graph of location_t
     358              :    values implicit in our line_maps structure, replicating it as a directed
     359              :    graph of SARIF locations within the SARIF result object, like this:
     360              : 
     361              :    [0]: error in include-chain-1-2.h ("conflicting types for 'p'; have 'char'")
     362              :    [1]: #include "include-chain-1-2.h" in include-chain-1.h
     363              :    [2]: note in include-chain-1-2.h ("previous declaration of 'p' with type 'int'")
     364              :    [3]: #include "include-chain-1-1.h" in include-chain-1.h
     365              :    [4]: #include "include-chain-1.h" in include-chain-1.c
     366              : 
     367              :    where we want to capture this "includes" graph in SARIF form:
     368              :    . +-----------------------------------+ +----------------------------------+
     369              :    . |"id": 0                            | |"id": 2                           |
     370              :    . | error: "conflicting types for 'p';| | note: previous declaration of 'p'|
     371              :    . |  have 'char'"|                    | | with type 'int'")                |
     372              :    . | in include-chain-1-2.h            | | in include-chain-1-1.h           |
     373              :    . +-----------------------------------+ +----------------------------------+
     374              :    .            ^         |                            ^         |
     375              :    .   includes |         | included-by       includes |         | included-by
     376              :    .            |         V                            |         V
     377              :    .  +--------------------------------+    +--------------------------------+
     378              :    .  |"id": 1                         |    |"id": 3                         |
     379              :    .  | #include "include-chain-1-2.h" |    | #include "include-chain-1-1.h" |
     380              :    .  | in include-chain-1.h           |    | in include-chain-1.h           |
     381              :    .  +--------------------------------+    +--------------------------------+
     382              :    .                   ^     |                       ^    |
     383              :    .          includes |     | included-by  includes |    | included-by
     384              :    .                   |     V                       |    V
     385              :    .                  +------------------------------------+
     386              :    .                  |"id": 4                             |
     387              :    .                  | The  #include "include-chain-1.h"  |
     388              :    .                  | in include-chain-1.c               |
     389              :    .                  +------------------------------------+
     390              :  */
     391              : 
     392              : class sarif_location_manager : public sarif_object
     393              : {
     394              : public:
     395              :   /* A worklist of pending actions needed to fully process this object.
     396              : 
     397              :      This lets us lazily walk our data structures to build the
     398              :      directed graph of locations, whilst keeping "notes" at the top
     399              :      of the "relatedLocations" array, and avoiding the need for
     400              :      recursion.  */
     401              :   struct worklist_item
     402              :   {
     403              :     enum class kind
     404              :     {
     405              :      /* Process a #include relationship where m_location_obj
     406              :         was #included-d at m_where.  */
     407              :      included_from,
     408              : 
     409              :      /* Process a location_t that was added as a secondary location
     410              :         to a rich_location without a label.  */
     411              :      unlabelled_secondary_location
     412              :     };
     413              : 
     414           27 :     worklist_item (sarif_location &location_obj,
     415              :                    enum kind kind,
     416              :                    location_t where)
     417           27 :       : m_location_obj (location_obj),
     418           27 :         m_kind (kind),
     419           27 :         m_where (where)
     420              :     {
     421              :     }
     422              : 
     423              :     sarif_location &m_location_obj;
     424              :     enum kind m_kind;
     425              :     location_t m_where;
     426              :   };
     427              : 
     428          614 :   sarif_location_manager ()
     429          614 :   : m_related_locations_arr (nullptr),
     430          614 :     m_next_location_id (0)
     431              :   {
     432          614 :   }
     433              : 
     434           39 :   unsigned allocate_location_id ()
     435              :   {
     436           39 :     return m_next_location_id++;
     437              :   }
     438              : 
     439              :   virtual void
     440              :   add_related_location (std::unique_ptr<sarif_location> location_obj,
     441              :                         sarif_builder &builder);
     442              : 
     443              :   void
     444              :   add_relationship_to_worklist (sarif_location &location_obj,
     445              :                                 enum worklist_item::kind kind,
     446              :                                 location_t where);
     447              : 
     448              :   void
     449              :   process_worklist (sarif_builder &builder);
     450              : 
     451              :   void
     452              :   process_worklist_item (sarif_builder &builder,
     453              :                          const worklist_item &item);
     454              : private:
     455              :   json::array *m_related_locations_arr; // borrowed
     456              :   unsigned m_next_location_id;
     457              : 
     458              :   std::list<worklist_item> m_worklist;
     459              :   std::map<location_t, sarif_location *> m_included_from_locations;
     460              :   std::map<location_t, sarif_location *> m_unlabelled_secondary_locations;
     461              : };
     462              : 
     463              : /* Subclass of sarif_object for SARIF "result" objects
     464              :    (SARIF v2.1.0 section 3.27).
     465              :    Each SARIF result object has its own "namespace" of numeric IDs for
     466              :    managing location objects (SARIF v2.1.0 section 3.28.2). */
     467              : 
     468          256 : class sarif_result : public sarif_location_manager
     469              : {
     470              : public:
     471          608 :   sarif_result (unsigned idx_within_parent)
     472          480 :   : m_idx_within_parent (idx_within_parent)
     473              :   {}
     474              : 
     475           18 :   unsigned get_index_within_parent () const { return m_idx_within_parent; }
     476              : 
     477              :   void
     478              :   on_nested_diagnostic (const diagnostic_info &diagnostic,
     479              :                         enum kind orig_diag_kind,
     480              :                         sarif_builder &builder);
     481              :   void on_diagram (const diagram &d,
     482              :                    sarif_builder &builder);
     483              : 
     484              : private:
     485              :   const unsigned m_idx_within_parent;
     486              : };
     487              : 
     488              : /* Subclass of sarif_object for SARIF "location" objects
     489              :    (SARIF v2.1.0 section 3.28).
     490              :    A location object can have an "id" which must be unique within
     491              :    the enclosing result, if any (see SARIF v2.1.0 section 3.28.2).  */
     492              : 
     493          996 : class sarif_location : public sarif_object
     494              : {
     495              : public:
     496              :   long lazily_add_id (sarif_location_manager &loc_mgr);
     497              :   long get_id () const;
     498              : 
     499              :   void lazily_add_relationship (sarif_location &target,
     500              :                                 enum location_relationship_kind kind,
     501              :                                 sarif_location_manager &loc_mgr);
     502              : 
     503              : private:
     504              :   sarif_location_relationship &
     505              :   lazily_add_relationship_object (sarif_location &target,
     506              :                                   sarif_location_manager &loc_mgr);
     507              : 
     508              :   json::array &lazily_add_relationships_array ();
     509              : 
     510              :   std::map<sarif_location *,
     511              :            sarif_location_relationship *> m_relationships_map;
     512              : };
     513              : 
     514              : /* Subclass of sarif_object for SARIF "physicalLocation" objects
     515              :    (SARIF v2.1.0 section 3.29).  */
     516              : 
     517          884 : class sarif_physical_location : public sarif_object {};
     518              : 
     519              : /* Subclass of sarif_object for SARIF "region" objects
     520              :    (SARIF v2.1.0 section 3.30).  */
     521              : 
     522         2179 : class sarif_region : public sarif_object {};
     523              : 
     524              : /* Subclass of sarif_object for SARIF "logicalLocation" objects
     525              :    (SARIF v2.1.0 section 3.33).  */
     526              : 
     527          943 : class sarif_logical_location : public sarif_object
     528              : {
     529              : };
     530              : 
     531              : /* Subclass of sarif_object for SARIF "locationRelationship" objects
     532              :    (SARIF v2.1.0 section 3.34).  */
     533              : 
     534              : class sarif_location_relationship : public sarif_object
     535              : {
     536              : public:
     537              :   sarif_location_relationship (sarif_location &target,
     538              :                                sarif_location_manager &loc_mgr);
     539              : 
     540              :   long get_target_id () const;
     541              : 
     542              :   void lazily_add_kind (enum location_relationship_kind kind);
     543              : 
     544              : private:
     545              :   auto_sbitmap m_kinds;
     546              : };
     547              : 
     548              : /* Subclass of sarif_object for SARIF "codeFlow" objects
     549              :    (SARIF v2.1.0 section 3.36).  */
     550              : 
     551              : class sarif_code_flow : public sarif_object
     552              : {
     553              : public:
     554              :   sarif_code_flow (sarif_result &parent,
     555              :                    unsigned idx_within_parent);
     556              : 
     557           18 :   sarif_result &get_parent () const { return m_parent; }
     558           18 :   unsigned get_index_within_parent () const { return m_idx_within_parent; }
     559              : 
     560              :   sarif_thread_flow &
     561              :   get_or_append_thread_flow (const paths::thread &thread,
     562              :                              paths::thread_id_t thread_id);
     563              : 
     564              :   sarif_thread_flow &
     565              :   get_thread_flow (paths::thread_id_t thread_id);
     566              : 
     567              :   void add_location (sarif_thread_flow_location &);
     568              : 
     569              :   sarif_thread_flow_location &
     570              :   get_thread_flow_loc_obj (paths::event_id_t event_id) const;
     571              : 
     572              : private:
     573              :   sarif_result &m_parent;
     574              :   const unsigned m_idx_within_parent;
     575              : 
     576              :   hash_map<int_hash<paths::thread_id_t, -1, -2>,
     577              :            sarif_thread_flow *> m_thread_id_map; // borrowed ptr
     578              :   json::array *m_thread_flows_arr; // borrowed
     579              : 
     580              :   /* Vec of borrowed ptr, allowing for going easily from
     581              :      an event_id to the corresponding threadFlowLocation object.  */
     582              :   std::vector<sarif_thread_flow_location *> m_all_tfl_objs;
     583              : };
     584              : 
     585              : /* Subclass of sarif_object for SARIF "threadFlow" objects
     586              :    (SARIF v2.1.0 section 3.37).  */
     587              : 
     588              : class sarif_thread_flow : public sarif_object
     589              : {
     590              : public:
     591              :   sarif_thread_flow (sarif_code_flow &parent,
     592              :                      const paths::thread &thread,
     593              :                      unsigned idx_within_parent);
     594              : 
     595           18 :   sarif_code_flow &get_parent () const { return m_parent; }
     596           18 :   unsigned get_index_within_parent () const { return m_idx_within_parent; }
     597              : 
     598              :   sarif_thread_flow_location &add_location ();
     599              : 
     600              : private:
     601              :   sarif_code_flow &m_parent;
     602              :   json::array *m_locations_arr; // borrowed
     603              :   const unsigned m_idx_within_parent;
     604              : };
     605              : 
     606              : /* Subclass of sarif_object for SARIF "threadFlowLocation" objects
     607              :    (SARIF v2.1.0 section 3.38).  */
     608              : 
     609              : class sarif_thread_flow_location : public sarif_object
     610              : {
     611              : public:
     612          168 :   sarif_thread_flow_location (sarif_thread_flow &parent,
     613              :                               unsigned idx_within_parent)
     614          168 :   : m_parent (parent),
     615          336 :     m_idx_within_parent (idx_within_parent)
     616              :   {
     617              :   }
     618              : 
     619           18 :   sarif_thread_flow &get_parent () const { return m_parent; }
     620           18 :   unsigned get_index_within_parent () const { return m_idx_within_parent; }
     621              : 
     622              : private:
     623              :   sarif_thread_flow &m_parent;
     624              :   const unsigned m_idx_within_parent;
     625              : };
     626              : 
     627              : /* Subclass of sarif_object for SARIF "reportingDescriptor" objects
     628              :    (SARIF v2.1.0 section 3.49).  */
     629              : 
     630           91 : class sarif_reporting_descriptor : public sarif_object {};
     631              : 
     632              : /* Subclass of sarif_object for SARIF "reportingDescriptorReference" objects
     633              :    (SARIF v2.1.0 section 3.53).  */
     634              : 
     635           26 : class sarif_reporting_descriptor_reference : public sarif_object {};
     636              : 
     637              : /* Subclass of sarif_object for SARIF "toolComponentReference" objects
     638              :    (SARIF v2.1.0 section 3.54).  */
     639              : 
     640           26 : class sarif_tool_component_reference : public sarif_object {};
     641              : 
     642              : /* Subclass of sarif_object for SARIF "fix" objects
     643              :    (SARIF v2.1.0 section 3.55).  */
     644              : 
     645            9 : class sarif_fix : public sarif_object {};
     646              : 
     647              : /* Subclass of sarif_object for SARIF "artifactChange" objects
     648              :    (SARIF v2.1.0 section 3.56).  */
     649              : 
     650            9 : class sarif_artifact_change : public sarif_object {};
     651              : 
     652              : /* Subclass of sarif_object for SARIF "replacement" objects
     653              :    (SARIF v2.1.0 section 3.57).  */
     654              : 
     655            9 : class sarif_replacement : public sarif_object {};
     656              : 
     657              : /* Subclass of sarif_object for SARIF "notification" objects
     658              :    (SARIF v2.1.0 section 3.58).
     659              : 
     660              :    This subclass is specifically for notifying when an
     661              :    internal compiler error occurs.  */
     662              : 
     663              : class sarif_ice_notification : public sarif_location_manager
     664              : {
     665              : public:
     666              :   sarif_ice_notification (const diagnostic_info &diagnostic,
     667              :                           sarif_builder &builder,
     668              :                           std::unique_ptr<json::object> backtrace);
     669              : 
     670              :   void
     671              :   add_related_location (std::unique_ptr<sarif_location> location_obj,
     672              :                         sarif_builder &builder) final override;
     673              : };
     674              : 
     675              : /* Abstract base class for use when making an  "artifactContent"
     676              :    object (SARIF v2.1.0 section 3.3): generate a value for the
     677              :    3.3.4 "rendered" property.
     678              :    Can return nullptr, for "no property".  */
     679              : 
     680          803 : class content_renderer
     681              : {
     682              : public:
     683          803 :   virtual ~content_renderer () {}
     684              : 
     685              :   virtual std::unique_ptr<sarif_multiformat_message_string>
     686              :   render (const sarif_builder &builder) const = 0;
     687              : };
     688              : 
     689              : /* Concrete buffering implementation subclass for SARIF output.  */
     690              : 
     691              : class sarif_sink_buffer : public per_sink_buffer
     692              : {
     693              : public:
     694              :   friend class sarif_sink;
     695              : 
     696           17 :   sarif_sink_buffer (sarif_builder &builder)
     697           17 :   : m_builder (builder)
     698              :   {}
     699              : 
     700              :   void dump (FILE *out, int indent) const final override;
     701              :   bool empty_p () const final override;
     702              :   void move_to (per_sink_buffer &dest) final override;
     703              :   void clear () final override;
     704              :   void flush () final override;
     705              : 
     706           21 :   void add_result (std::unique_ptr<sarif_result> result)
     707              :   {
     708           42 :     m_results.push_back (std::move (result));
     709              :   }
     710              : 
     711              :   size_t num_results () const { return m_results.size (); }
     712              :   sarif_result &get_result (size_t idx) { return *m_results[idx]; }
     713              : 
     714              : private:
     715              :   sarif_builder &m_builder;
     716              :   std::vector<std::unique_ptr<sarif_result>> m_results;
     717              : };
     718              : 
     719              : /* Classes for abstracting away JSON vs other serialization formats.  */
     720              : 
     721              : // class sarif_serialization_format_json : public sarif_serialization_format
     722              : 
     723              : void
     724          115 : sarif_serialization_format_json::write_to_file (FILE *outf,
     725              :                                                 const json::value &top)
     726              : {
     727          115 :   top.dump (outf, m_formatted);
     728          115 :   fprintf (outf, "\n");
     729          115 : }
     730              : 
     731              : void
     732            0 : sarif_serialization_format_json::dump (FILE *outfile, int indent) const
     733              : {
     734            0 :   dumping::emit_indent (outfile, indent);
     735            0 :   fprintf (outfile, "json\n");
     736            0 :   DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_formatted);
     737            0 : }
     738              : 
     739              : /* A class for managing SARIF output (for -fdiagnostics-format=sarif-stderr
     740              :    and -fdiagnostics-format=sarif-file).
     741              : 
     742              :    As diagnostics occur, we build "result" JSON objects, and
     743              :    accumulate state:
     744              :    - which source files are referenced
     745              :    - which warnings are emitted
     746              :    - which CWEs are used
     747              : 
     748              :    At the end of the compile, we use the above to build the full SARIF
     749              :    object tree, adding the result objects to the correct place, and
     750              :    creating objects for the various source files, warnings and CWEs
     751              :    referenced.
     752              : 
     753              :    Implemented:
     754              :    - fix-it hints
     755              :    - CWE metadata
     756              :    - diagnostic groups (see limitations below)
     757              :    - logical locations (e.g. cfun)
     758              :    - labelled ranges (as annotations)
     759              :    - secondary ranges without labels (as related locations)
     760              : 
     761              :    Known limitations:
     762              :    - GCC supports nesting of diagnostics (one-deep nesting via
     763              :      auto_diagnostic_group, and arbitrary nesting via
     764              :      auto_diagnostic_nesting_level).  These are captured in the SARIF
     765              :      as related locations, and so we only capture location and message
     766              :      information from such nested diagnostics (e.g. we ignore fix-it
     767              :      hints on them).  Diagnostics within an auto_diagnostic_nesting_level
     768              :      have their nesting level captured as a property.
     769              :    - although we capture command-line arguments (section 3.20.2), we don't
     770              :      yet capture response files.
     771              :    - doesn't capture "artifact.encoding" property
     772              :      (SARIF v2.1.0 section 3.24.9).
     773              :    - doesn't capture hashes of the source files
     774              :      ("artifact.hashes" property (SARIF v2.1.0 section 3.24.11).
     775              :    - doesn't capture the "analysisTarget" property
     776              :      (SARIF v2.1.0 section 3.27.13).
     777              :    - doesn't capture -Werror cleanly
     778              :    - doesn't capture inlining information (can SARIF handle this?)
     779              :    - doesn't capture macro expansion information (can SARIF handle this?).
     780              :    - doesn't capture any diagnostics::metadata::rules associated with
     781              :      a diagnostic.  */
     782              : 
     783              : class sarif_builder
     784              : {
     785              : public:
     786              :   friend class sarif_sink_buffer;
     787              : 
     788              :   sarif_builder (diagnostics::context &dc,
     789              :                  pretty_printer &printer,
     790              :                  const line_maps *line_maps,
     791              :                  std::unique_ptr<sarif_serialization_format> serialization_format,
     792              :                  const sarif_generation_options &sarif_gen_opts);
     793              :   ~sarif_builder ();
     794              : 
     795              :   void dump (FILE *out, int indent) const;
     796              : 
     797          291 :   void set_printer (pretty_printer &printer)
     798              :   {
     799          291 :     m_printer = &printer;
     800              :   }
     801              : 
     802              :   const logical_locations::manager *
     803         1252 :   get_logical_location_manager () const
     804              :   {
     805         1252 :     if (auto client_data_hooks = m_context.get_client_data_hooks ())
     806         1252 :       return client_data_hooks->get_logical_location_manager ();
     807              :     return nullptr;
     808              :   }
     809              : 
     810              :   void
     811              :   set_main_input_filename (const char *name);
     812              : 
     813              :   void on_report_diagnostic (const diagnostic_info &diagnostic,
     814              :                              enum kind orig_diag_kind,
     815              :                              sarif_sink_buffer *buffer);
     816              :   void emit_diagram (const diagram &d);
     817              :   void end_group ();
     818              : 
     819              :   void
     820              :   report_global_digraph (const lazily_created<digraphs::digraph> &);
     821              : 
     822              :   void
     823              :   report_digraph_for_logical_location (const lazily_created<digraphs::digraph> &ldg,
     824              :                                        logical_locations::key);
     825              : 
     826          291 :   std::unique_ptr<sarif_result> take_current_result ()
     827              :   {
     828          291 :     return std::move (m_cur_group_result);
     829              :   }
     830              : 
     831              :   std::unique_ptr<sarif_log> flush_to_object ();
     832              :   void flush_to_file (FILE *outf);
     833              : 
     834              :   std::unique_ptr<json::array>
     835              :   make_locations_arr (sarif_location_manager &loc_mgr,
     836              :                       const diagnostic_info &diagnostic,
     837              :                       enum diagnostic_artifact_role role);
     838              :   std::unique_ptr<sarif_location>
     839              :   make_location_object (sarif_location_manager *loc_mgr,
     840              :                         const rich_location &rich_loc,
     841              :                         logical_locations::key logical_loc,
     842              :                         enum diagnostic_artifact_role role);
     843              :   std::unique_ptr<sarif_location>
     844              :   make_location_object (sarif_location_manager &loc_mgr,
     845              :                         location_t where,
     846              :                         enum diagnostic_artifact_role role);
     847              :   std::unique_ptr<sarif_message>
     848              :   make_message_object (const char *msg) const;
     849              :   std::unique_ptr<sarif_message>
     850              :   make_message_object_for_diagram (const diagram &d);
     851              :   std::unique_ptr<sarif_artifact_content>
     852              :   maybe_make_artifact_content_object (const char *filename) const;
     853              : 
     854              :   std::unique_ptr<sarif_artifact_location>
     855              :   make_artifact_location_object (const char *filename);
     856              : 
     857              :   const sarif_code_flow *
     858          120 :   get_code_flow_for_event_ids () const
     859              :   {
     860          120 :     return m_current_code_flow;
     861              :   }
     862              : 
     863          409 :   diagnostics::context &get_context () const { return m_context; }
     864          422 :   pretty_printer *get_printer () const { return m_printer; }
     865          291 :   token_printer &get_token_printer () { return m_token_printer; }
     866          566 :   enum sarif_version get_version () const { return m_sarif_gen_opts.m_version; }
     867              : 
     868           96 :   size_t num_results () const { return m_results_array->size (); }
     869           16 :   sarif_result &get_result (size_t idx)
     870              :   {
     871           16 :     auto element = (*m_results_array)[idx];
     872           16 :     gcc_assert (element);
     873           16 :     return *static_cast<sarif_result *> (element);
     874              :   }
     875              : 
     876              :   const sarif_generation_options &get_opts () const { return m_sarif_gen_opts; }
     877              : 
     878              :   std::unique_ptr<sarif_logical_location>
     879              :   make_minimal_sarif_logical_location (logical_locations::key);
     880              : 
     881              : private:
     882          419 :   class sarif_token_printer : public token_printer
     883              :   {
     884              :   public:
     885          419 :     sarif_token_printer (sarif_builder &builder)
     886          419 :       : m_builder (builder)
     887              :     {
     888              :     }
     889              :     void print_tokens (pretty_printer *pp,
     890              :                        const pp_token_list &tokens) final override;
     891              :   private:
     892              :     sarif_builder &m_builder;
     893              :   };
     894              : 
     895              :   std::unique_ptr<sarif_result>
     896              :   make_result_object (const diagnostic_info &diagnostic,
     897              :                       enum kind orig_diag_kind,
     898              :                       unsigned idx_within_parent);
     899              :   void
     900              :   add_any_include_chain (sarif_location_manager &loc_mgr,
     901              :                          sarif_location &location_obj,
     902              :                          location_t where);
     903              :   void
     904              :   set_any_logical_locs_arr (sarif_location &location_obj,
     905              :                             logical_locations::key logical_loc);
     906              :   std::unique_ptr<sarif_location>
     907              :   make_location_object (sarif_location_manager &loc_mgr,
     908              :                         const paths::event &event,
     909              :                         enum diagnostic_artifact_role role);
     910              :   std::unique_ptr<sarif_code_flow>
     911              :   make_code_flow_object (sarif_result &result,
     912              :                          unsigned idx_within_parent,
     913              :                          const paths::path &path);
     914              :   void
     915              :   populate_thread_flow_location_object (sarif_result &result,
     916              :                                         sarif_thread_flow_location &thread_flow_loc_obj,
     917              :                                         const paths::event &event,
     918              :                                         int event_execution_idx);
     919              :   std::unique_ptr<json::array>
     920              :   maybe_make_kinds_array (paths::event::meaning m) const;
     921              :   std::unique_ptr<sarif_physical_location>
     922              :   maybe_make_physical_location_object (location_t loc,
     923              :                                        enum diagnostic_artifact_role role,
     924              :                                        int column_override,
     925              :                                        const content_renderer *snippet_renderer);
     926              :   std::unique_ptr<sarif_artifact_location>
     927              :   make_artifact_location_object (location_t loc);
     928              :   std::unique_ptr<sarif_artifact_location>
     929              :   make_artifact_location_object_for_pwd () const;
     930              :   std::unique_ptr<sarif_region>
     931              :   maybe_make_region_object (location_t loc,
     932              :                             int column_override) const;
     933              :   std::unique_ptr<sarif_region>
     934              :   maybe_make_region_object_for_context (location_t loc,
     935              :                                         const content_renderer *snippet_renderer) const;
     936              :   std::unique_ptr<sarif_region>
     937              :   make_region_object_for_hint (const fixit_hint &hint) const;
     938              : 
     939              :   int
     940              :   ensure_sarif_logical_location_for (logical_locations::key k);
     941              : 
     942              :   std::unique_ptr<sarif_multiformat_message_string>
     943              :   make_multiformat_message_string (const char *msg) const;
     944              :   std::unique_ptr<sarif_log>
     945              :   make_top_level_object (std::unique_ptr<sarif_invocation> invocation_obj,
     946              :                          std::unique_ptr<json::array> results);
     947              :   std::unique_ptr<sarif_run>
     948              :   make_run_object (std::unique_ptr<sarif_invocation> invocation_obj,
     949              :                    std::unique_ptr<json::array> results);
     950              :   std::unique_ptr<sarif_tool>
     951              :   make_tool_object ();
     952              :   std::unique_ptr<sarif_tool_component>
     953              :   make_driver_tool_component_object ();
     954              :   std::unique_ptr<json::array> maybe_make_taxonomies_array () const;
     955              :   std::unique_ptr<sarif_tool_component>
     956              :   maybe_make_cwe_taxonomy_object () const;
     957              :   std::unique_ptr<sarif_tool_component_reference>
     958              :   make_tool_component_reference_object_for_cwe () const;
     959              :   std::unique_ptr<sarif_reporting_descriptor>
     960              :   make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic,
     961              :                                                 enum kind orig_diag_kind,
     962              :                                                 const char *option_text);
     963              :   std::unique_ptr<sarif_reporting_descriptor>
     964              :   make_reporting_descriptor_object_for_cwe_id (int cwe_id) const;
     965              :   std::unique_ptr<sarif_reporting_descriptor_reference>
     966              :   make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id);
     967              :   sarif_artifact &
     968              :   get_or_create_artifact (const char *filename,
     969              :                           enum diagnostic_artifact_role role,
     970              :                           bool embed_contents);
     971              :   char *
     972              :   get_source_lines (const char *filename,
     973              :                     int start_line,
     974              :                     int end_line) const;
     975              :   std::unique_ptr<sarif_artifact_content>
     976              :   maybe_make_artifact_content_object (const char *filename,
     977              :                                       int start_line,
     978              :                                       int end_line,
     979              :                                       const content_renderer *r) const;
     980              :   std::unique_ptr<sarif_message>
     981              :   make_message_describing_fix_it_hint (const fixit_hint &hint) const;
     982              :   std::unique_ptr<sarif_fix>
     983              :   make_fix_object (const rich_location &rich_loc);
     984              :   std::unique_ptr<sarif_artifact_change>
     985              :   make_artifact_change_object (const rich_location &richloc);
     986              :   std::unique_ptr<sarif_replacement>
     987              :   make_replacement_object (const fixit_hint &hint) const;
     988              :   std::unique_ptr<sarif_artifact_content>
     989              :   make_artifact_content_object (const char *text) const;
     990              :   int get_sarif_column (expanded_location exploc) const;
     991              : 
     992              :   std::unique_ptr<json::object>
     993              :   make_stack_from_backtrace ();
     994              : 
     995              :   diagnostics::context &m_context;
     996              :   pretty_printer *m_printer;
     997              :   const line_maps *m_line_maps;
     998              :   sarif_token_printer m_token_printer;
     999              : 
    1000              :   /* The JSON object for the invocation object.  */
    1001              :   std::unique_ptr<sarif_invocation> m_invocation_obj;
    1002              : 
    1003              :   /* The JSON array of pending diagnostics.  */
    1004              :   std::unique_ptr<json::array> m_results_array;
    1005              : 
    1006              :   /* The JSON object for the result object (if any) in the current
    1007              :      diagnostic group.  */
    1008              :   std::unique_ptr<sarif_result> m_cur_group_result;
    1009              : 
    1010              :   /* Ideally we'd use std::unique_ptr<sarif_artifact> here, but I had
    1011              :      trouble getting this to work when building with GCC 4.8.  */
    1012              :   ordered_hash_map <nofree_string_hash,
    1013              :                     sarif_artifact *> m_filename_to_artifact_map;
    1014              : 
    1015              :   bool m_seen_any_relative_paths;
    1016              :   std::set<std::string> m_rule_id_set;
    1017              :   std::unique_ptr<json::array> m_rules_arr;
    1018              : 
    1019              :   /* The set of all CWE IDs we've seen, if any.  */
    1020              :   hash_set <int_hash <int, 0, 1> > m_cwe_id_set;
    1021              : 
    1022              :   std::unique_ptr<sarif_array_of_unique<sarif_logical_location>> m_cached_logical_locs;
    1023              : 
    1024              :   std::unique_ptr<sarif_array_of_unique<sarif_graph>> m_run_graphs;
    1025              : 
    1026              :   int m_tabstop;
    1027              : 
    1028              :   std::unique_ptr<sarif_serialization_format> m_serialization_format;
    1029              :   const sarif_generation_options m_sarif_gen_opts;
    1030              : 
    1031              :   unsigned m_next_result_idx;
    1032              :   sarif_code_flow *m_current_code_flow;
    1033              : };
    1034              : 
    1035              : /* class sarif_object : public json::object.  */
    1036              : 
    1037              : sarif_property_bag &
    1038         1802 : sarif_object::get_or_create_properties ()
    1039              : {
    1040         1802 :   json::value *properties_val = get ("properties");
    1041         1802 :   if (properties_val)
    1042              :     {
    1043          143 :       if (properties_val->get_kind () == json::JSON_OBJECT)
    1044              :         return *static_cast <sarif_property_bag *> (properties_val);
    1045              :     }
    1046              : 
    1047         1659 :   sarif_property_bag *bag = new sarif_property_bag ();
    1048         1659 :   set ("properties", bag);
    1049         1659 :   return *bag;
    1050              : }
    1051              : 
    1052              : /* class sarif_invocation : public sarif_object.  */
    1053              : 
    1054          419 : sarif_invocation::sarif_invocation (sarif_builder &builder,
    1055              :                                     const char * const *original_argv)
    1056          419 : : m_notifications_arr (std::make_unique<json::array> ()),
    1057          419 :   m_success (true)
    1058              : {
    1059              :   // "arguments" property (SARIF v2.1.0 section 3.20.2)
    1060          419 :   if (original_argv)
    1061              :     {
    1062          115 :       auto arguments_arr = std::make_unique<json::array> ();
    1063         4461 :       for (size_t i = 0; original_argv[i]; ++i)
    1064         4231 :         arguments_arr->append_string (original_argv[i]);
    1065          115 :       set<json::array> ("arguments", std::move (arguments_arr));
    1066          115 :     }
    1067              : 
    1068              :   // "workingDirectory" property (SARIF v2.1.0 section 3.20.19)
    1069          419 :   if (const char *pwd = getpwd ())
    1070          838 :     set<sarif_artifact_location> ("workingDirectory",
    1071          419 :                                   builder.make_artifact_location_object (pwd));
    1072              : 
    1073              :   // "startTimeUtc" property (SARIF v2.1.0 section 3.20.7)
    1074          838 :   set<json::string> ("startTimeUtc",
    1075          419 :                      make_date_time_string_for_current_time ());
    1076          419 : }
    1077              : 
    1078              : /* Handle an internal compiler error DIAGNOSTIC.
    1079              :    Add an object representing the ICE to the notifications array.  */
    1080              : 
    1081              : void
    1082            6 : sarif_invocation::add_notification_for_ice (const diagnostic_info &diagnostic,
    1083              :                                             sarif_builder &builder,
    1084              :                                             std::unique_ptr<json::object> backtrace)
    1085              : {
    1086            6 :   m_success = false;
    1087              : 
    1088            6 :   auto notification
    1089              :     = std::make_unique<sarif_ice_notification> (diagnostic,
    1090              :                                                 builder,
    1091            6 :                                                 std::move (backtrace));
    1092              : 
    1093              :   /* Support for related locations within a notification was added
    1094              :      in SARIF 2.2; see https://github.com/oasis-tcs/sarif-spec/issues/540  */
    1095            6 :   if (builder.get_version () >= sarif_version::v2_2_prerelease_csd01)
    1096            1 :     notification->process_worklist (builder);
    1097              : 
    1098            6 :   m_notifications_arr->append<sarif_ice_notification>
    1099            6 :     (std::move (notification));
    1100            6 : }
    1101              : 
    1102              : void
    1103          283 : sarif_invocation::prepare_to_flush (sarif_builder &builder)
    1104              : {
    1105          283 :   const context &dc = builder.get_context ();
    1106              : 
    1107              :   /* "executionSuccessful" property (SARIF v2.1.0 section 3.20.14).  */
    1108          283 :   if (dc.execution_failed_p ())
    1109          201 :     m_success = false;
    1110          283 :   set_bool ("executionSuccessful", m_success);
    1111              : 
    1112              :   /* "toolExecutionNotifications" property (SARIF v2.1.0 section 3.20.21).  */
    1113          283 :   set ("toolExecutionNotifications", std::move (m_notifications_arr));
    1114              : 
    1115              :   /* Call client hook, allowing it to create a custom property bag for
    1116              :      this object (SARIF v2.1.0 section 3.8) e.g. for recording time vars.  */
    1117          283 :   if (auto client_data_hooks = dc.get_client_data_hooks ())
    1118          115 :     client_data_hooks->add_sarif_invocation_properties (*this);
    1119              : 
    1120              :   // "endTimeUtc" property (SARIF v2.1.0 section 3.20.8);
    1121          566 :   set<json::string> ("endTimeUtc",
    1122          283 :                      make_date_time_string_for_current_time ());
    1123          283 : }
    1124              : 
    1125              : /* class sarif_artifact : public sarif_object.  */
    1126              : 
    1127              : /* Add ROLE to this artifact's roles.
    1128              :    If EMBED_CONTENTS is true, then flag that we will attempt to embed the
    1129              :    contents of this artifact when writing it out.  */
    1130              : 
    1131              : void
    1132         1175 : sarif_artifact::add_role (enum diagnostic_artifact_role role,
    1133              :                           bool embed_contents)
    1134              : {
    1135              :   /* TODO(SARIF 2.2): "scannedFile" is to be added as a role in SARIF 2.2;
    1136              :      see https://github.com/oasis-tcs/sarif-spec/issues/459
    1137              : 
    1138              :      For now, skip them.
    1139              :      Ultimately, we probably shouldn't bother embedding the contents
    1140              :      of such artifacts, just the snippets.  */
    1141         1175 :   if (role == diagnostic_artifact_role::scanned_file)
    1142              :     return;
    1143              : 
    1144         1130 :   if (embed_contents)
    1145          839 :     m_embed_contents = true;
    1146              : 
    1147              :   /* In SARIF v2.1.0 section 3.24.6 "roles" property:
    1148              :      "resultFile" is for an artifact
    1149              :      "which the analysis tool was not explicitly instructed to scan",
    1150              :      whereas "analysisTarget" is for one where the
    1151              :      "analysis tool was instructed to scan this artifact".
    1152              :      Hence the latter excludes the former.  */
    1153         1130 :   if (role == diagnostic_artifact_role::result_file)
    1154          543 :     if (bitmap_bit_p (m_roles, (int)diagnostic_artifact_role::analysis_target))
    1155              :         return;
    1156              : 
    1157          602 :   bitmap_set_bit (m_roles, (int)role);
    1158              : }
    1159              : 
    1160              : /* Populate the "contents" property (SARIF v2.1.0 section 3.24.8).
    1161              :    We do this after initialization to
    1162              :    (a) ensure that any charset options have been set
    1163              :    (b) only populate it for artifacts that are referenced by a location.  */
    1164              : 
    1165              : void
    1166          242 : sarif_artifact::populate_contents (sarif_builder &builder)
    1167              : {
    1168          242 :   if (auto artifact_content_obj
    1169          242 :         = builder.maybe_make_artifact_content_object (m_filename))
    1170          242 :     set<sarif_artifact_content> ("contents", std::move (artifact_content_obj));
    1171          242 : }
    1172              : 
    1173              : /* Get a string for ROLE corresponding to the
    1174              :    SARIF v2.1.0 section 3.24.6 "roles" property.  */
    1175              : 
    1176              : static const char *
    1177          332 : get_artifact_role_string (enum diagnostic_artifact_role role)
    1178              : {
    1179          332 :   switch (role)
    1180              :     {
    1181            0 :     default:
    1182            0 :       gcc_unreachable ();
    1183              :     case diagnostic_artifact_role::analysis_target:
    1184              :       return "analysisTarget";
    1185            0 :     case diagnostic_artifact_role::debug_output_file:
    1186            0 :       return "debugOutputFile";
    1187           13 :     case diagnostic_artifact_role::result_file:
    1188           13 :       return "resultFile";
    1189            0 :     case diagnostic_artifact_role::scanned_file:
    1190            0 :       return "scannedFile";
    1191           36 :     case diagnostic_artifact_role::traced_file:
    1192           36 :       return "tracedFile";
    1193              :     }
    1194              : }
    1195              : 
    1196              : /* Populate the "roles" property of this sarif_artifact with a new
    1197              :    json::array for the artifact.roles property (SARIF v2.1.0 section 3.24.6)
    1198              :    containing strings such as "analysisTarget", "resultFile"
    1199              :    and/or "tracedFile".  */
    1200              : 
    1201              : void
    1202          301 : sarif_artifact::populate_roles ()
    1203              : {
    1204          301 :   if (bitmap_empty_p (m_roles))
    1205            1 :     return;
    1206          300 :   auto roles_arr (std::make_unique<json::array> ());
    1207         2100 :   for (int i = 0; i < (int)diagnostic_artifact_role::NUM_ROLES; i++)
    1208         1500 :     if (bitmap_bit_p (m_roles, i))
    1209              :       {
    1210          332 :         enum diagnostic_artifact_role role = (enum diagnostic_artifact_role)i;
    1211          332 :         roles_arr->append_string (get_artifact_role_string (role));
    1212              :       }
    1213          300 :   set<json::array> ("roles", std::move (roles_arr));
    1214          300 : }
    1215              : 
    1216              : /* class sarif_location_manager : public sarif_object.  */
    1217              : 
    1218              : /* Base implementation of sarif_location_manager::add_related_location vfunc.
    1219              : 
    1220              :    Add LOCATION_OBJ to this object's "relatedLocations" array,
    1221              :    creating it if it doesn't yet exist.  */
    1222              : 
    1223              : void
    1224          146 : sarif_location_manager::
    1225              : add_related_location (std::unique_ptr<sarif_location> location_obj,
    1226              :                       sarif_builder &)
    1227              : {
    1228          146 :   if (!m_related_locations_arr)
    1229              :     {
    1230           42 :       m_related_locations_arr = new json::array ();
    1231              :       /* Give ownership of m_related_locations_arr to json::object;
    1232              :          keep a borrowed ptr.  */
    1233           42 :       set ("relatedLocations", m_related_locations_arr);
    1234              :     }
    1235          146 :   m_related_locations_arr->append (std::move (location_obj));
    1236          146 : }
    1237              : 
    1238              : void
    1239           27 : sarif_location_manager::
    1240              : add_relationship_to_worklist (sarif_location &location_obj,
    1241              :                               enum worklist_item::kind kind,
    1242              :                               location_t where)
    1243              : {
    1244           54 :   m_worklist.push_back (worklist_item (location_obj,
    1245              :                                        kind,
    1246           27 :                                        where));
    1247           27 : }
    1248              : 
    1249              : /* Process all items in this result's worklist.
    1250              :    Doing so may temporarily add new items to the end
    1251              :    of the worklist.
    1252              :    Handling any item should be "lazy", and thus we should
    1253              :    eventually drain the queue and terminate.  */
    1254              : 
    1255              : void
    1256          468 : sarif_location_manager::process_worklist (sarif_builder &builder)
    1257              : {
    1258          494 :   while (!m_worklist.empty ())
    1259              :     {
    1260           26 :       const worklist_item &item = m_worklist.front ();
    1261           26 :       process_worklist_item (builder, item);
    1262           26 :       m_worklist.pop_front ();
    1263              :     }
    1264          468 : }
    1265              : 
    1266              : /* Process one item in this result's worklist, potentially
    1267              :    adding new items to the end of the worklist.  */
    1268              : 
    1269              : void
    1270           26 : sarif_location_manager::process_worklist_item (sarif_builder &builder,
    1271              :                                                const worklist_item &item)
    1272              : {
    1273           26 :   switch (item.m_kind)
    1274              :     {
    1275            0 :     default:
    1276            0 :       gcc_unreachable ();
    1277           22 :     case worklist_item::kind::included_from:
    1278           22 :       {
    1279           22 :         sarif_location &included_loc_obj = item.m_location_obj;
    1280           22 :         sarif_location *includer_loc_obj = nullptr;
    1281           22 :         auto iter = m_included_from_locations.find (item.m_where);
    1282           22 :         if (iter != m_included_from_locations.end ())
    1283            5 :           includer_loc_obj = iter->second;
    1284              :         else
    1285              :           {
    1286           17 :             std::unique_ptr<sarif_location> new_loc_obj
    1287              :               = builder.make_location_object
    1288              :                   (*this,
    1289           17 :                    item.m_where,
    1290           17 :                    diagnostic_artifact_role::scanned_file);
    1291           17 :             includer_loc_obj = new_loc_obj.get ();
    1292           17 :             add_related_location (std::move (new_loc_obj), builder);
    1293           17 :             auto kv
    1294           17 :               = std::pair<location_t, sarif_location *> (item.m_where,
    1295           17 :                                                          includer_loc_obj);
    1296           17 :             m_included_from_locations.insert (kv);
    1297           17 :           }
    1298              : 
    1299           22 :         includer_loc_obj->lazily_add_relationship
    1300           22 :           (included_loc_obj,
    1301              :            location_relationship_kind::includes,
    1302              :            *this);
    1303           22 :         included_loc_obj.lazily_add_relationship
    1304           22 :           (*includer_loc_obj,
    1305              :            location_relationship_kind::is_included_by,
    1306              :            *this);
    1307              :       }
    1308           22 :       break;
    1309            4 :     case worklist_item::kind::unlabelled_secondary_location:
    1310            4 :       {
    1311            4 :         sarif_location &primary_loc_obj = item.m_location_obj;
    1312            4 :         sarif_location *secondary_loc_obj = nullptr;
    1313            4 :         auto iter = m_unlabelled_secondary_locations.find (item.m_where);
    1314            4 :         if (iter != m_unlabelled_secondary_locations.end ())
    1315            0 :           secondary_loc_obj = iter->second;
    1316              :         else
    1317              :           {
    1318            4 :             std::unique_ptr<sarif_location> new_loc_obj
    1319              :               = builder.make_location_object
    1320              :                   (*this,
    1321            4 :                    item.m_where,
    1322            4 :                    diagnostic_artifact_role::scanned_file);
    1323            4 :             secondary_loc_obj = new_loc_obj.get ();
    1324            4 :             add_related_location (std::move (new_loc_obj), builder);
    1325            4 :             auto kv
    1326            4 :               = std::pair<location_t, sarif_location *> (item.m_where,
    1327            4 :                                                          secondary_loc_obj);
    1328            4 :             m_unlabelled_secondary_locations.insert (kv);
    1329            4 :           }
    1330            4 :         gcc_assert (secondary_loc_obj);
    1331            4 :         primary_loc_obj.lazily_add_relationship
    1332            4 :           (*secondary_loc_obj,
    1333              :            location_relationship_kind::relevant,
    1334              :            *this);
    1335              :       }
    1336            4 :       break;
    1337              :     }
    1338           26 : }
    1339              : 
    1340              : /* class sarif_result : public sarif_location_manager.  */
    1341              : 
    1342              : /* Handle secondary diagnostics that occur within a diagnostic group.
    1343              :    The closest SARIF seems to have to nested diagnostics is the
    1344              :    "relatedLocations" property of result objects (SARIF v2.1.0 section 3.27.22),
    1345              :    so we lazily set this property and populate the array if and when
    1346              :    secondary diagnostics occur (such as notes to a warning).  */
    1347              : 
    1348              : void
    1349          121 : sarif_result::on_nested_diagnostic (const diagnostic_info &diagnostic,
    1350              :                                     enum kind /*orig_diag_kind*/,
    1351              :                                     sarif_builder &builder)
    1352              : {
    1353              :   /* We don't yet generate meaningful logical locations for notes;
    1354              :      sometimes these will related to current_function_decl, but
    1355              :      often they won't.  */
    1356          121 :   auto location_obj
    1357          121 :     = builder.make_location_object (this, *diagnostic.m_richloc,
    1358          121 :                                     logical_locations::key (),
    1359          121 :                                     diagnostic_artifact_role::result_file);
    1360          121 :   auto message_obj
    1361          121 :     = builder.make_message_object (pp_formatted_text (builder.get_printer ()));
    1362          121 :   pp_clear_output_area (builder.get_printer ());
    1363          121 :   location_obj->set<sarif_message> ("message", std::move (message_obj));
    1364              : 
    1365              :   /* Add nesting level, as per "P3358R0 SARIF for Structured Diagnostics"
    1366              :      https://wg21.link/P3358R0  */
    1367          121 :   sarif_property_bag &bag = location_obj->get_or_create_properties ();
    1368          121 :   bag.set_integer ("nestingLevel",
    1369          121 :                    builder.get_context ().get_diagnostic_nesting_level ());
    1370              : 
    1371          121 :   add_related_location (std::move (location_obj), builder);
    1372          121 : }
    1373              : 
    1374              : /* Handle diagrams that occur within a diagnostic group.
    1375              :    The closest thing in SARIF seems to be to add a location to the
    1376              :    "releatedLocations" property  (SARIF v2.1.0 section 3.27.22),
    1377              :    and to put the diagram into the "message" property of that location
    1378              :    (SARIF v2.1.0 section 3.28.5).  */
    1379              : 
    1380              : void
    1381            4 : sarif_result::on_diagram (const diagram &d,
    1382              :                           sarif_builder &builder)
    1383              : {
    1384            4 :   auto location_obj = std::make_unique<sarif_location> ();
    1385            4 :   auto message_obj = builder.make_message_object_for_diagram (d);
    1386            4 :   location_obj->set<sarif_message> ("message", std::move (message_obj));
    1387              : 
    1388            4 :   add_related_location (std::move (location_obj), builder);
    1389            4 : }
    1390              : 
    1391              : /* class sarif_location : public sarif_object.  */
    1392              : 
    1393              : /* Ensure this location has an "id" and return it.
    1394              :    Use LOC_MGR if an id needs to be allocated.
    1395              : 
    1396              :    See the "id" property (3.28.2).
    1397              : 
    1398              :    We use this to only assign ids to locations that are
    1399              :    referenced by another sarif object; others have no "id".   */
    1400              : 
    1401              : long
    1402           48 : sarif_location::lazily_add_id (sarif_location_manager &loc_mgr)
    1403              : {
    1404           48 :   long id = get_id ();
    1405           48 :   if (id != -1)
    1406              :     return id;
    1407           39 :   id = loc_mgr.allocate_location_id ();
    1408           39 :   set_integer ("id", id);
    1409           39 :   gcc_assert (id != -1);
    1410           39 :   return id;
    1411              : }
    1412              : 
    1413              : /* Get the id of this location, or -1 if it doesn't have one.  */
    1414              : 
    1415              : long
    1416           48 : sarif_location::get_id () const
    1417              : {
    1418           48 :   json::value *id = get ("id");
    1419           48 :   if (!id)
    1420              :     return -1;
    1421            9 :   gcc_assert (id->get_kind () == json::JSON_INTEGER);
    1422            9 :   return static_cast <json::integer_number *> (id)->get ();
    1423              : }
    1424              : 
    1425              : // 3.34.3 kinds property
    1426              : static const char *
    1427           48 : get_string_for_location_relationship_kind (enum location_relationship_kind kind)
    1428              : {
    1429           48 :   switch (kind)
    1430              :     {
    1431            0 :     default:
    1432            0 :       gcc_unreachable ();
    1433              :     case location_relationship_kind::includes:
    1434              :       return "includes";
    1435           22 :     case location_relationship_kind::is_included_by:
    1436           22 :       return "isIncludedBy";
    1437            4 :     case location_relationship_kind::relevant:
    1438            4 :       return "relevant";
    1439              :     }
    1440              : }
    1441              : 
    1442              : /* Lazily populate this location's "relationships" property (3.28.7)
    1443              :    with the relationship of KIND to TARGET, creating objects
    1444              :    as necessary.
    1445              :    Use LOC_MGR for any locations that need "id" values.  */
    1446              : 
    1447              : void
    1448           48 : sarif_location::lazily_add_relationship (sarif_location &target,
    1449              :                                          enum location_relationship_kind kind,
    1450              :                                          sarif_location_manager &loc_mgr)
    1451              : {
    1452           48 :   sarif_location_relationship &relationship_obj
    1453           48 :     = lazily_add_relationship_object (target, loc_mgr);
    1454              : 
    1455           48 :   relationship_obj.lazily_add_kind (kind);
    1456           48 : }
    1457              : 
    1458              : /* Lazily populate this location's "relationships" property (3.28.7)
    1459              :    with a location_relationship to TARGET, creating objects
    1460              :    as necessary.
    1461              :    Use LOC_MGR for any locations that need "id" values.  */
    1462              : 
    1463              : sarif_location_relationship &
    1464           48 : sarif_location::lazily_add_relationship_object (sarif_location &target,
    1465              :                                                 sarif_location_manager &loc_mgr)
    1466              : {
    1467              :   /* See if THIS already has a locationRelationship referencing TARGET.  */
    1468           48 :   auto iter = m_relationships_map.find (&target);
    1469           48 :   if (iter != m_relationships_map.end ())
    1470              :     {
    1471              :       /* We already have a locationRelationship from THIS to TARGET.  */
    1472            0 :       sarif_location_relationship *relationship = iter->second;
    1473            0 :       gcc_assert (relationship->get_target_id() == target.get_id ());
    1474              :       return *relationship;
    1475              :     }
    1476              : 
    1477              :   // Ensure that THIS has a "relationships" property (3.28.7).
    1478           48 :   json::array &relationships_arr = lazily_add_relationships_array ();
    1479              : 
    1480              :   /* No existing locationRelationship from THIS to TARGET; make one,
    1481              :      record it, and add it to the "relationships" array.  */
    1482           48 :   auto relationship_obj
    1483           48 :     = std::make_unique<sarif_location_relationship> (target, loc_mgr);
    1484           48 :   sarif_location_relationship *relationship = relationship_obj.get ();
    1485           48 :   auto kv
    1486              :     = std::pair<sarif_location *,
    1487           48 :                 sarif_location_relationship *> (&target, relationship);
    1488           48 :   m_relationships_map.insert (kv);
    1489              : 
    1490           48 :   relationships_arr.append (std::move (relationship_obj));
    1491              : 
    1492           48 :   return *relationship;
    1493           48 : }
    1494              : 
    1495              : /* Ensure this location has a "relationships" array (3.28.7).  */
    1496              : 
    1497              : json::array &
    1498           48 : sarif_location::lazily_add_relationships_array ()
    1499              : {
    1500           48 :   const char *const property_name = "relationships";
    1501           48 :   if (json::value *relationships = get (property_name))
    1502              :     {
    1503            9 :       gcc_assert (relationships->get_kind () == json::JSON_ARRAY);
    1504              :       return *static_cast <json::array *> (relationships);
    1505              :     }
    1506           39 :   json::array *relationships_arr = new json::array ();
    1507           39 :   set (property_name, relationships_arr);
    1508           39 :   return *relationships_arr;
    1509              : }
    1510              : 
    1511              : /* class sarif_ice_notification : public sarif_location_manager.  */
    1512              : 
    1513              : /* sarif_ice_notification's ctor.
    1514              :    DIAGNOSTIC is an internal compiler error.  */
    1515              : 
    1516            6 : sarif_ice_notification::
    1517              : sarif_ice_notification (const diagnostic_info &diagnostic,
    1518              :                         sarif_builder &builder,
    1519            6 :                         std::unique_ptr<json::object> backtrace)
    1520              : {
    1521              :   /* "locations" property (SARIF v2.1.0 section 3.58.4).  */
    1522            6 :   auto locations_arr
    1523              :     = builder.make_locations_arr (*this,
    1524              :                                   diagnostic,
    1525            6 :                                   diagnostic_artifact_role::result_file);
    1526            6 :   set<json::array> ("locations", std::move (locations_arr));
    1527              : 
    1528              :   /* "message" property (SARIF v2.1.0 section 3.85.5).  */
    1529            6 :   auto message_obj
    1530            6 :     = builder.make_message_object (pp_formatted_text (builder.get_printer ()));
    1531            6 :   pp_clear_output_area (builder.get_printer ());
    1532            6 :   set<sarif_message> ("message", std::move (message_obj));
    1533              : 
    1534              :   /* "level" property (SARIF v2.1.0 section 3.58.6).  */
    1535            6 :   set_string ("level", "error");
    1536              : 
    1537              :   /* If we have backtrace information, add it as part of a property bag.  */
    1538            6 :   if (backtrace)
    1539              :     {
    1540            6 :       sarif_property_bag &bag = get_or_create_properties ();
    1541            6 :       bag.set ("gcc/backtrace", std::move (backtrace));
    1542              :     }
    1543            6 : }
    1544              : 
    1545              : /* Implementation of sarif_location_manager::add_related_location vfunc
    1546              :    for notifications.  */
    1547              : 
    1548              : void
    1549            1 : sarif_ice_notification::
    1550              : add_related_location (std::unique_ptr<sarif_location> location_obj,
    1551              :                       sarif_builder &builder)
    1552              : {
    1553              :   /* Support for related locations within a notification was added
    1554              :      in SARIF 2.2; see https://github.com/oasis-tcs/sarif-spec/issues/540  */
    1555            1 :   if (builder.get_version () >= sarif_version::v2_2_prerelease_csd01)
    1556            1 :     sarif_location_manager::add_related_location (std::move (location_obj),
    1557              :                                                   builder);
    1558              :   /* Otherwise implicitly discard LOCATION_OBJ.  */
    1559            1 : }
    1560              : 
    1561              : /* class sarif_location_relationship : public sarif_object.  */
    1562              : 
    1563           48 : sarif_location_relationship::
    1564              : sarif_location_relationship (sarif_location &target,
    1565              :                              sarif_location_manager &loc_mgr)
    1566           48 : : m_kinds ((unsigned)location_relationship_kind::NUM_KINDS)
    1567              : {
    1568           48 :   bitmap_clear (m_kinds);
    1569           48 :   set_integer ("target", target.lazily_add_id (loc_mgr));
    1570           48 : }
    1571              : 
    1572              : long
    1573            0 : sarif_location_relationship::get_target_id () const
    1574              : {
    1575            0 :   json::value *id = get ("id");
    1576            0 :   gcc_assert (id);
    1577            0 :   return static_cast <json::integer_number *> (id)->get ();
    1578              : }
    1579              : 
    1580              : void
    1581           48 : sarif_location_relationship::
    1582              : lazily_add_kind (enum location_relationship_kind kind)
    1583              : {
    1584           48 :   if (bitmap_bit_p (m_kinds, (int)kind))
    1585              :     return; // already have this kind
    1586           48 :   bitmap_set_bit (m_kinds, (int)kind);
    1587              : 
    1588              :   // 3.34.3 kinds property
    1589           48 :   json::array *kinds_arr = nullptr;
    1590           48 :   if (json::value *kinds_val = get ("kinds"))
    1591              :     {
    1592            0 :       gcc_assert (kinds_val->get_kind () == json::JSON_ARRAY);
    1593              :     }
    1594              :   else
    1595              :     {
    1596           48 :       kinds_arr = new json::array ();
    1597           48 :       set ("kinds", kinds_arr);
    1598              :     }
    1599           48 :   const char *kind_str = get_string_for_location_relationship_kind (kind);
    1600           48 :   kinds_arr->append_string (kind_str);
    1601              : }
    1602              : 
    1603              : /* class sarif_code_flow : public sarif_object.  */
    1604              : 
    1605           35 : sarif_code_flow::sarif_code_flow (sarif_result &parent,
    1606              :                                   unsigned idx_within_parent)
    1607           35 : : m_parent (parent),
    1608           35 :   m_idx_within_parent (idx_within_parent)
    1609              : {
    1610              :   /* "threadFlows" property (SARIF v2.1.0 section 3.36.3).  */
    1611           35 :   auto thread_flows_arr = std::make_unique<json::array> ();
    1612           35 :   m_thread_flows_arr = thread_flows_arr.get (); // borrowed
    1613           35 :   set<json::array> ("threadFlows", std::move (thread_flows_arr));
    1614           35 : }
    1615              : 
    1616              : sarif_thread_flow &
    1617          168 : sarif_code_flow::get_or_append_thread_flow (const paths::thread &thread,
    1618              :                                             paths::thread_id_t thread_id)
    1619              : {
    1620          168 :   sarif_thread_flow **slot = m_thread_id_map.get (thread_id);
    1621          168 :   if (slot)
    1622          132 :     return **slot;
    1623              : 
    1624           36 :   unsigned next_thread_flow_idx = m_thread_flows_arr->size ();
    1625           36 :   auto thread_flow_obj
    1626           36 :     = std::make_unique<sarif_thread_flow> (*this, thread, next_thread_flow_idx);
    1627           36 :   m_thread_id_map.put (thread_id, thread_flow_obj.get ()); // borrowed
    1628           36 :   sarif_thread_flow *result = thread_flow_obj.get ();
    1629           36 :   m_thread_flows_arr->append<sarif_thread_flow> (std::move (thread_flow_obj));
    1630           36 :   return *result;
    1631           36 : }
    1632              : 
    1633              : sarif_thread_flow &
    1634            0 : sarif_code_flow::get_thread_flow (paths::thread_id_t thread_id)
    1635              : {
    1636            0 :   sarif_thread_flow **slot = m_thread_id_map.get (thread_id);
    1637            0 :   gcc_assert (slot); // it must already have one
    1638            0 :   return **slot;
    1639              : }
    1640              : 
    1641              : void
    1642          168 : sarif_code_flow::add_location (sarif_thread_flow_location &tfl_obj)
    1643              : {
    1644          168 :   m_all_tfl_objs.push_back (&tfl_obj);
    1645          168 : }
    1646              : 
    1647              : sarif_thread_flow_location &
    1648          186 : sarif_code_flow::get_thread_flow_loc_obj (paths::event_id_t event_id) const
    1649              : {
    1650          186 :   gcc_assert (event_id.known_p ());
    1651          186 :   gcc_assert ((size_t)event_id.zero_based () < m_all_tfl_objs.size ());
    1652          186 :   sarif_thread_flow_location *tfl_obj = m_all_tfl_objs[event_id.zero_based ()];
    1653          186 :   gcc_assert (tfl_obj);
    1654          186 :   return *tfl_obj;
    1655              : }
    1656              : 
    1657              : /* class sarif_thread_flow : public sarif_object.  */
    1658              : 
    1659           36 : sarif_thread_flow::sarif_thread_flow (sarif_code_flow &parent,
    1660              :                                       const paths::thread &thread,
    1661              :                                       unsigned idx_within_parent)
    1662           36 : : m_parent (parent),
    1663           36 :   m_idx_within_parent (idx_within_parent)
    1664              : {
    1665              :   /* "id" property (SARIF v2.1.0 section 3.37.2).  */
    1666           36 :   label_text name (thread.get_name (false));
    1667           36 :   set_string ("id", name.get ());
    1668              : 
    1669              :   /* "locations" property (SARIF v2.1.0 section 3.37.6).  */
    1670           36 :   m_locations_arr = new json::array ();
    1671              : 
    1672              :   /* Give ownership of m_locations_arr to json::object;
    1673              :      keep a borrowed ptr.  */
    1674           36 :   set ("locations", m_locations_arr);
    1675           36 : }
    1676              : 
    1677              : /* Add a sarif_thread_flow_location to this threadFlow object, but
    1678              :    don't populate it yet.  */
    1679              : 
    1680              : sarif_thread_flow_location &
    1681          168 : sarif_thread_flow::add_location ()
    1682              : {
    1683          168 :   const unsigned thread_flow_location_idx = m_locations_arr->size ();
    1684          168 :   sarif_thread_flow_location *thread_flow_loc_obj
    1685          168 :     = new sarif_thread_flow_location (*this, thread_flow_location_idx);
    1686          168 :   m_locations_arr->append (thread_flow_loc_obj);
    1687          168 :   m_parent.add_location (*thread_flow_loc_obj);
    1688          168 :   return *thread_flow_loc_obj;
    1689              : }
    1690              : 
    1691              : /* class sarif_builder.  */
    1692              : 
    1693              : /* sarif_builder's ctor.  */
    1694              : 
    1695          419 : sarif_builder::sarif_builder (diagnostics::context &dc,
    1696              :                               pretty_printer &printer,
    1697              :                               const line_maps *line_maps,
    1698              :                               std::unique_ptr<sarif_serialization_format> serialization_format,
    1699              :                               const sarif_generation_options &sarif_gen_opts)
    1700          419 : : m_context (dc),
    1701          419 :   m_printer (&printer),
    1702          419 :   m_line_maps (line_maps),
    1703          419 :   m_token_printer (*this),
    1704          419 :   m_invocation_obj
    1705              :     (std::make_unique<sarif_invocation> (*this,
    1706          419 :                                          dc.get_original_argv ())),
    1707          419 :   m_results_array (new json::array ()),
    1708          419 :   m_cur_group_result (nullptr),
    1709          419 :   m_seen_any_relative_paths (false),
    1710          419 :   m_rule_id_set (),
    1711          419 :   m_rules_arr (new json::array ()),
    1712          419 :   m_cached_logical_locs
    1713              :     (std::make_unique<sarif_array_of_unique<sarif_logical_location>> ()),
    1714          419 :   m_run_graphs
    1715              :     (std::make_unique<sarif_array_of_unique<sarif_graph>> ()),
    1716          419 :   m_tabstop (dc.get_column_options ().m_tabstop),
    1717          419 :   m_serialization_format (std::move (serialization_format)),
    1718          419 :   m_sarif_gen_opts (sarif_gen_opts),
    1719          419 :   m_next_result_idx (0),
    1720          838 :   m_current_code_flow (nullptr)
    1721              : {
    1722          419 :   gcc_assert (m_line_maps);
    1723          419 :   gcc_assert (m_serialization_format);
    1724          419 : }
    1725              : 
    1726          419 : sarif_builder::~sarif_builder ()
    1727              : {
    1728              :   /* Normally m_filename_to_artifact_map will have been emptied as part
    1729              :      of make_run_object, but this isn't run by all the selftests.
    1730              :      Ensure the artifact objects are cleaned up for such cases.  */
    1731          974 :   for (auto iter : m_filename_to_artifact_map)
    1732              :     {
    1733          136 :       sarif_artifact *artifact_obj = iter.second;
    1734          136 :       delete artifact_obj;
    1735              :     }
    1736          419 : }
    1737              : 
    1738              : void
    1739            0 : sarif_builder::dump (FILE *out, int indent) const
    1740              : {
    1741            0 :   dumping::emit_heading (out, indent, "serialization format");
    1742            0 :   m_serialization_format->dump (out, indent + 2);
    1743            0 :   dumping::emit_heading (out, indent, "SARIF generation options");
    1744            0 :   m_sarif_gen_opts.dump (out, indent + 2);
    1745            0 : }
    1746              : 
    1747              : /* Functions at which to stop the backtrace print.  It's not
    1748              :    particularly helpful to print the callers of these functions.  */
    1749              : 
    1750              : static const char * const bt_stop[] =
    1751              : {
    1752              :   "main",
    1753              :   "toplev::main",
    1754              :   "execute_one_pass",
    1755              :   "compile_file",
    1756              : };
    1757              : 
    1758              : struct bt_closure
    1759              : {
    1760            6 :   bt_closure (sarif_builder &builder,
    1761              :               json::array *frames_arr)
    1762            6 :   : m_builder (builder),
    1763            6 :     m_frames_arr (frames_arr)
    1764              :   {
    1765              :   }
    1766              : 
    1767              :   sarif_builder &m_builder;
    1768              :   json::array *m_frames_arr;
    1769              : };
    1770              : 
    1771              : /* A callback function passed to the backtrace_full function.  */
    1772              : 
    1773              : static int
    1774           22 : bt_callback (void *data, uintptr_t pc, const char *filename, int lineno,
    1775              :              const char *function)
    1776              : {
    1777           22 :   bt_closure *closure = (bt_closure *)data;
    1778              : 
    1779              :   /* If we don't have any useful information, don't print
    1780              :      anything.  */
    1781           22 :   if (filename == nullptr && function == nullptr)
    1782              :     return 0;
    1783              : 
    1784              :   /* Skip functions in context.cc or diagnostic-global-context.cc.  */
    1785           20 :   if (closure->m_frames_arr->size () == 0
    1786           12 :       && filename != nullptr
    1787           20 :       && (strcmp (lbasename (filename), "context.cc") == 0
    1788           12 :           || strcmp (lbasename (filename),
    1789              :                      "diagnostic-global-context.cc") == 0))
    1790              :     return 0;
    1791              : 
    1792              :   /* Print up to 20 functions.  We could make this a --param, but
    1793              :      since this is only for debugging just use a constant for now.  */
    1794           14 :   if (closure->m_frames_arr->size () >= 20)
    1795              :     {
    1796              :       /* Returning a non-zero value stops the backtrace.  */
    1797              :       return 1;
    1798              :     }
    1799              : 
    1800           14 :   char *alc = nullptr;
    1801           14 :   if (function != nullptr)
    1802              :     {
    1803           14 :       char *str = cplus_demangle_v3 (function,
    1804              :                                      (DMGL_VERBOSE | DMGL_ANSI
    1805              :                                       | DMGL_GNU_V3 | DMGL_PARAMS));
    1806           14 :       if (str != nullptr)
    1807              :         {
    1808           12 :           alc = str;
    1809           12 :           function = str;
    1810              :         }
    1811              : 
    1812           58 :       for (size_t i = 0; i < ARRAY_SIZE (bt_stop); ++i)
    1813              :         {
    1814           50 :           size_t len = strlen (bt_stop[i]);
    1815           50 :           if (strncmp (function, bt_stop[i], len) == 0
    1816            6 :               && (function[len] == '\0' || function[len] == '('))
    1817              :             {
    1818            6 :               if (alc != nullptr)
    1819            6 :                 free (alc);
    1820              :               /* Returning a non-zero value stops the backtrace.  */
    1821              :               return 1;
    1822              :             }
    1823              :         }
    1824              :     }
    1825              : 
    1826            8 :   auto frame_obj = std::make_unique<json::object> ();
    1827              : 
    1828              :   /* I tried using sarifStack and sarifStackFrame for this
    1829              :      but it's not a good fit e.g. PC information.  */
    1830            8 :   char buf[128];
    1831            8 :   snprintf (buf, sizeof (buf) - 1, "0x%lx", (unsigned long)pc);
    1832            8 :   frame_obj->set_string ("pc", buf);
    1833            8 :   if (function)
    1834            8 :     frame_obj->set_string ("function", function);
    1835            8 :   if (filename)
    1836            8 :     frame_obj->set_string ("filename", filename);
    1837            8 :   frame_obj->set_integer ("lineno", lineno);
    1838            8 :   closure->m_frames_arr->append (std::move (frame_obj));
    1839              : 
    1840            8 :   if (alc != nullptr)
    1841            6 :     free (alc);
    1842              : 
    1843            8 :   return 0;
    1844            8 : }
    1845              : 
    1846              : /* Attempt to generate a JSON object representing a backtrace,
    1847              :    for adding to ICE notifications.  */
    1848              : 
    1849              : std::unique_ptr<json::object>
    1850            6 : sarif_builder::make_stack_from_backtrace ()
    1851              : {
    1852            6 :   auto frames_arr = std::make_unique<json::array> ();
    1853              : 
    1854            6 :   backtrace_state *state = nullptr;
    1855            6 :   state = backtrace_create_state (nullptr, 0, nullptr, nullptr);
    1856            6 :   bt_closure closure (*this, frames_arr.get ());
    1857            6 :   const int frames_to_skip = 5;
    1858            6 :   if (state != nullptr)
    1859            6 :     backtrace_full (state, frames_to_skip, bt_callback, nullptr,
    1860              :                     (void *) &closure);
    1861              : 
    1862            6 :   if (frames_arr->size () == 0)
    1863            0 :     return nullptr;
    1864              : 
    1865            6 :   auto stack = std::make_unique<json::object> ();
    1866            6 :   stack->set ("frames", std::move (frames_arr));
    1867            6 :   return stack;
    1868            6 : }
    1869              : 
    1870              : void
    1871          291 : sarif_builder::set_main_input_filename (const char *name)
    1872              : {
    1873              :   /* Mark NAME as the artifact that the tool was instructed to scan.
    1874              :      Only quote the contents if it gets referenced by physical locations,
    1875              :      since otherwise the "no diagnostics" case would quote the main input
    1876              :      file, and doing so noticeably bloated the output seen in analyzer
    1877              :      integration testing (build directory went from 20G -> 21G).  */
    1878          291 :   if (name)
    1879          291 :     get_or_create_artifact (name,
    1880              :                             diagnostic_artifact_role::analysis_target,
    1881              :                             false);
    1882          291 : }
    1883              : 
    1884              : /* Implementation of "on_report_diagnostic" for SARIF output.  */
    1885              : 
    1886              : void
    1887          607 : sarif_builder::on_report_diagnostic (const diagnostic_info &diagnostic,
    1888              :                                      enum kind orig_diag_kind,
    1889              :                                      sarif_sink_buffer *buffer)
    1890              : {
    1891          607 :   pp_output_formatted_text (m_printer, m_context.get_urlifier ());
    1892              : 
    1893          607 :   if (diagnostic.m_kind == kind::ice || diagnostic.m_kind == kind::ice_nobt)
    1894              :     {
    1895            6 :       std::unique_ptr<json::object> stack = make_stack_from_backtrace ();
    1896            6 :       m_invocation_obj->add_notification_for_ice (diagnostic, *this,
    1897              :                                                   std::move (stack));
    1898              : 
    1899              :       /* Print a header for the remaining output to stderr, and
    1900              :          return, attempting to print the usual ICE messages to
    1901              :          stderr.  Hopefully this will be helpful to the user in
    1902              :          indicating what's gone wrong (also for DejaGnu, for pruning
    1903              :          those messages).   */
    1904            6 :       fnotice (stderr, "Internal compiler error:\n");
    1905              : 
    1906            6 :       return;
    1907            6 :     }
    1908              : 
    1909          601 :   if (buffer)
    1910              :     {
    1911              :       /* When buffering, we can only handle top-level results.  */
    1912           21 :       gcc_assert (!m_cur_group_result);
    1913           21 :       buffer->add_result (make_result_object (diagnostic, orig_diag_kind,
    1914           21 :                                               m_next_result_idx++));
    1915           21 :       return;
    1916              :     }
    1917              : 
    1918          580 :   if (m_cur_group_result)
    1919              :     /* Nested diagnostic.  */
    1920          121 :     m_cur_group_result->on_nested_diagnostic (diagnostic,
    1921              :                                               orig_diag_kind,
    1922              :                                               *this);
    1923              :   else
    1924              :     {
    1925              :       /* Top-level diagnostic.  */
    1926          459 :       m_cur_group_result = make_result_object (diagnostic, orig_diag_kind,
    1927          459 :                                                m_next_result_idx++);
    1928              :     }
    1929              : }
    1930              : 
    1931              : /* Implementation of diagnostics::context::m_diagrams.m_emission_cb
    1932              :    for SARIF output.  */
    1933              : 
    1934              : void
    1935            4 : sarif_builder::emit_diagram (const diagram &d)
    1936              : {
    1937              :   /* We must be within the emission of a top-level diagnostic.  */
    1938            4 :   gcc_assert (m_cur_group_result);
    1939            4 :   m_cur_group_result->on_diagram (d, *this);
    1940            4 : }
    1941              : 
    1942              : /* Implementation of "end_group_cb" for SARIF output.
    1943              :    Append the current sarifResult to results, and set it to nullptr.  */
    1944              : 
    1945              : void
    1946          484 : sarif_builder::end_group ()
    1947              : {
    1948          484 :   if (m_cur_group_result)
    1949              :     {
    1950          459 :       m_cur_group_result->process_worklist (*this);
    1951          459 :       m_results_array->append<sarif_result> (std::move (m_cur_group_result));
    1952              :     }
    1953          484 : }
    1954              : 
    1955              : void
    1956            3 : sarif_builder::
    1957              : report_global_digraph (const lazily_created<digraphs::digraph> &ldg)
    1958              : {
    1959            3 :   auto &dg = ldg.get_or_create ();
    1960              : 
    1961              :   /* Presumably the location manager must be nullptr; see
    1962              :      https://github.com/oasis-tcs/sarif-spec/issues/712  */
    1963            3 :   m_run_graphs->append (make_sarif_graph (dg, this, nullptr));
    1964            3 : }
    1965              : 
    1966              : void
    1967           45 : sarif_builder::
    1968              : report_digraph_for_logical_location (const lazily_created<digraphs::digraph> &ldg,
    1969              :                                      logical_locations::key logical_loc)
    1970              : {
    1971              :   /* Adding the graph to the logical location itself would break consolidation
    1972              :      of logical locations, as the objects would no longer be equal.
    1973              :      So we add the graph to the per-run graphs, but add a logicalLocation
    1974              :      property to it.  */
    1975              : 
    1976           45 :   auto &dg = ldg.get_or_create ();
    1977              : 
    1978              :   /* Presumably the location manager must be nullptr; see
    1979              :      https://github.com/oasis-tcs/sarif-spec/issues/712  */
    1980           45 :   auto graph_obj = make_sarif_graph (dg, this, nullptr);
    1981              : 
    1982           45 :   auto &bag = graph_obj->get_or_create_properties ();
    1983           90 :   bag.set ("logicalLocation",
    1984           45 :            make_minimal_sarif_logical_location (logical_loc));
    1985              : 
    1986           45 :   m_run_graphs->append (std::move (graph_obj));
    1987           45 : }
    1988              : 
    1989              : /* Create a top-level object, and add it to all the results
    1990              :    (and other entities) we've seen so far, moving ownership
    1991              :    to the object.  */
    1992              : 
    1993              : std::unique_ptr<sarif_log>
    1994          283 : sarif_builder::flush_to_object ()
    1995              : {
    1996          283 :   DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_context.get_logger (),
    1997          283 :                                  "diagnostics::sarif_builder::flush_to_object");
    1998          283 :   m_invocation_obj->prepare_to_flush (*this);
    1999          283 :   std::unique_ptr<sarif_log> top
    2000          283 :     = make_top_level_object (std::move (m_invocation_obj),
    2001          283 :                              std::move (m_results_array));
    2002          283 :   return top;
    2003          283 : }
    2004              : 
    2005              : /* Create a top-level object, and add it to all the results
    2006              :    (and other entities) we've seen so far.
    2007              : 
    2008              :    Flush it all to OUTF.  */
    2009              : 
    2010              : void
    2011          115 : sarif_builder::flush_to_file (FILE *outf)
    2012              : {
    2013          115 :   DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_context.get_logger (),
    2014          115 :                                  "diagnostics::sarif_builder::flush_to_file");
    2015          115 :   std::unique_ptr<sarif_log> top = flush_to_object ();
    2016          115 :   m_serialization_format->write_to_file (outf, *top);
    2017          115 : }
    2018              : 
    2019              : /* Attempt to convert DIAG_KIND to a suitable value for the "level"
    2020              :    property (SARIF v2.1.0 section 3.27.10).
    2021              : 
    2022              :    Return nullptr if there isn't one.  */
    2023              : 
    2024              : static const char *
    2025          480 : maybe_get_sarif_level (enum kind diag_kind)
    2026              : {
    2027          480 :   switch (diag_kind)
    2028              :     {
    2029              :     case kind::warning:
    2030              :       return "warning";
    2031          238 :     case kind::fatal:
    2032          238 :     case kind::error:
    2033          238 :       return "error";
    2034            8 :     case kind::note:
    2035            8 :     case kind::anachronism:
    2036            8 :       return "note";
    2037            0 :     default:
    2038            0 :       return nullptr;
    2039              :     }
    2040              : }
    2041              : 
    2042              : /* Make a string for DIAG_KIND suitable for use a ruleId
    2043              :    (SARIF v2.1.0 section 3.27.5) as a fallback for when we don't
    2044              :    have anything better to use.  */
    2045              : 
    2046              : static char *
    2047          238 : make_rule_id_for_diagnostic_kind (enum kind diag_kind)
    2048              : {
    2049              :   /* Lose the trailing ": ".  */
    2050          238 :   const char *kind_text = get_text_for_kind (diag_kind);
    2051          238 :   size_t len = strlen (kind_text);
    2052          238 :   gcc_assert (len > 2);
    2053          238 :   gcc_assert (kind_text[len - 2] == ':');
    2054          238 :   gcc_assert (kind_text[len - 1] == ' ');
    2055          238 :   char *rstrip = xstrdup (kind_text);
    2056          238 :   rstrip[len - 2] = '\0';
    2057          238 :   return rstrip;
    2058              : }
    2059              : 
    2060              : /* Make a "result" object (SARIF v2.1.0 section 3.27) for DIAGNOSTIC.  */
    2061              : 
    2062              : std::unique_ptr<sarif_result>
    2063          480 : sarif_builder::make_result_object (const diagnostic_info &diagnostic,
    2064              :                                    enum kind orig_diag_kind,
    2065              :                                    unsigned idx_within_parent)
    2066              : {
    2067          480 :   auto result_obj = std::make_unique<sarif_result> (idx_within_parent);
    2068              : 
    2069              :   /* "ruleId" property (SARIF v2.1.0 section 3.27.5).  */
    2070              :   /* Ideally we'd have an option_name for these.  */
    2071          480 :   label_text option_text
    2072          480 :     = m_context.get_option_name (diagnostic.m_option_id,
    2073          480 :                                  orig_diag_kind, diagnostic.m_kind);
    2074          480 :   if (option_text.get ())
    2075              :     {
    2076              :       /* Lazily create reportingDescriptor objects for and add to m_rules_arr.
    2077              :          Set ruleId referencing them.  */
    2078          242 :       result_obj->set_string ("ruleId", option_text.get ());
    2079          242 :       if (m_rule_id_set.find (option_text.get ()) == m_rule_id_set.end ())
    2080              :         {
    2081              :           /* This is the first time we've seen this ruleId.  */
    2082              :           /* Add to set, taking ownership.  */
    2083           68 :           m_rule_id_set.insert (option_text.get ());
    2084              : 
    2085           68 :           m_rules_arr->append<sarif_reporting_descriptor>
    2086           68 :             (make_reporting_descriptor_object_for_warning (diagnostic,
    2087              :                                                            orig_diag_kind,
    2088              :                                                            option_text.get ()));
    2089              :         }
    2090              :     }
    2091              :   else
    2092              :     {
    2093              :       /* Otherwise, we have an "error" or a stray "note"; use the
    2094              :          diagnostic kind as the ruleId, so that the result object at least
    2095              :          has a ruleId.
    2096              :          We don't bother creating reportingDescriptor objects for these.  */
    2097          238 :       char *rule_id = make_rule_id_for_diagnostic_kind (orig_diag_kind);
    2098          238 :       result_obj->set_string ("ruleId", rule_id);
    2099          238 :       free (rule_id);
    2100              :     }
    2101              : 
    2102          480 :   if (diagnostic.m_metadata)
    2103              :     {
    2104              :       /* "taxa" property (SARIF v2.1.0 section 3.27.8).  */
    2105           36 :       if (int cwe_id = diagnostic.m_metadata->get_cwe ())
    2106              :         {
    2107           26 :           auto taxa_arr = std::make_unique<json::array> ();
    2108           26 :           taxa_arr->append<sarif_reporting_descriptor_reference>
    2109           26 :             (make_reporting_descriptor_reference_object_for_cwe_id (cwe_id));
    2110           26 :           result_obj->set<json::array> ("taxa", std::move (taxa_arr));
    2111           26 :         }
    2112              : 
    2113           36 :       diagnostic.m_metadata->maybe_add_sarif_properties (*result_obj);
    2114              : 
    2115              :       /* We don't yet support diagnostics::metadata::rule.  */
    2116              :     }
    2117              : 
    2118              :   /* "level" property (SARIF v2.1.0 section 3.27.10).  */
    2119          480 :   if (const char *sarif_level = maybe_get_sarif_level (diagnostic.m_kind))
    2120          480 :     result_obj->set_string ("level", sarif_level);
    2121              : 
    2122              :   /* "message" property (SARIF v2.1.0 section 3.27.11).  */
    2123          480 :   auto message_obj
    2124          480 :     = make_message_object (pp_formatted_text (m_printer));
    2125          480 :   pp_clear_output_area (m_printer);
    2126          480 :   result_obj->set<sarif_message> ("message", std::move (message_obj));
    2127              : 
    2128              :   /* "locations" property (SARIF v2.1.0 section 3.27.12).  */
    2129          480 :   result_obj->set<json::array>
    2130          960 :     ("locations",
    2131          480 :      make_locations_arr (*result_obj.get (),
    2132              :                          diagnostic,
    2133              :                          diagnostic_artifact_role::result_file));
    2134              : 
    2135              :   /* "codeFlows" property (SARIF v2.1.0 section 3.27.18).  */
    2136          480 :   if (const paths::path *path = diagnostic.m_richloc->get_path ())
    2137              :     {
    2138           35 :       auto code_flows_arr = std::make_unique<json::array> ();
    2139           35 :       const unsigned code_flow_index = 0;
    2140           35 :       code_flows_arr->append<sarif_code_flow>
    2141           35 :         (make_code_flow_object (*result_obj.get (),
    2142              :                                 code_flow_index,
    2143              :                                 *path));
    2144           35 :       result_obj->set<json::array> ("codeFlows", std::move (code_flows_arr));
    2145           35 :     }
    2146              : 
    2147              :   // "graphs" property (SARIF v2.1.0 section 3.27.19).  */
    2148          480 :   if (diagnostic.m_metadata)
    2149           36 :     if (auto ldg = diagnostic.m_metadata->get_lazy_digraphs ())
    2150              :       {
    2151            1 :         auto &digraphs = ldg->get_or_create ();
    2152            1 :         auto graphs_arr = std::make_unique<json::array> ();
    2153            3 :         for (auto &iter : digraphs)
    2154            2 :           graphs_arr->append (make_sarif_graph (*iter, this,
    2155            2 :                                                 result_obj.get ()));
    2156            1 :         if (graphs_arr->size () > 0)
    2157            1 :           result_obj->set<json::array> ("graphs", std::move (graphs_arr));
    2158            1 :       }
    2159              : 
    2160              :   /* The "relatedLocations" property (SARIF v2.1.0 section 3.27.22) is
    2161              :      set up later, if any nested diagnostics occur within this diagnostic
    2162              :      group.  */
    2163              : 
    2164              :   /* "fixes" property (SARIF v2.1.0 section 3.27.30).  */
    2165          480 :   const rich_location *richloc = diagnostic.m_richloc;
    2166          480 :   if (richloc->get_num_fixit_hints ())
    2167              :     {
    2168            9 :       auto fix_arr = std::make_unique<json::array> ();
    2169            9 :       fix_arr->append<sarif_fix> (make_fix_object (*richloc));
    2170            9 :       result_obj->set<json::array> ("fixes", std::move (fix_arr));
    2171            9 :     }
    2172              : 
    2173          960 :   return result_obj;
    2174          480 : }
    2175              : 
    2176              : /* Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49)
    2177              :    for a GCC warning.  */
    2178              : 
    2179              : std::unique_ptr<sarif_reporting_descriptor>
    2180           68 : sarif_builder::
    2181              : make_reporting_descriptor_object_for_warning (const diagnostic_info &diagnostic,
    2182              :                                               enum kind /*orig_diag_kind*/,
    2183              :                                               const char *option_text)
    2184              : {
    2185           68 :   auto reporting_desc = std::make_unique<sarif_reporting_descriptor> ();
    2186              : 
    2187              :   /* "id" property (SARIF v2.1.0 section 3.49.3).  */
    2188           68 :   reporting_desc->set_string ("id", option_text);
    2189              : 
    2190              :   /* We don't implement "name" property (SARIF v2.1.0 section 3.49.7), since
    2191              :      it seems redundant compared to "id".  */
    2192              : 
    2193              :   /* "helpUri" property (SARIF v2.1.0 section 3.49.12).  */
    2194           68 :   label_text option_url = m_context.get_option_url (diagnostic.m_option_id);
    2195           68 :   if (option_url.get ())
    2196           68 :     reporting_desc->set_string ("helpUri", option_url.get ());
    2197              : 
    2198           68 :   return reporting_desc;
    2199           68 : }
    2200              : 
    2201              : /* Make a "reportingDescriptor" object (SARIF v2.1.0 section 3.49)
    2202              :    for CWE_ID, for use within the CWE taxa array.  */
    2203              : 
    2204              : std::unique_ptr<sarif_reporting_descriptor>
    2205           23 : sarif_builder::make_reporting_descriptor_object_for_cwe_id (int cwe_id) const
    2206              : {
    2207           23 :   auto reporting_desc = std::make_unique<sarif_reporting_descriptor> ();
    2208              : 
    2209              :   /* "id" property (SARIF v2.1.0 section 3.49.3).  */
    2210           23 :   {
    2211           23 :     pretty_printer pp;
    2212           23 :     pp_printf (&pp, "%i", cwe_id);
    2213           23 :     reporting_desc->set_string ("id", pp_formatted_text (&pp));
    2214           23 :   }
    2215              : 
    2216              :   /* "helpUri" property (SARIF v2.1.0 section 3.49.12).  */
    2217           23 :   {
    2218           23 :     char *url = get_cwe_url (cwe_id);
    2219           23 :     reporting_desc->set_string ("helpUri", url);
    2220           23 :     free (url);
    2221              :   }
    2222              : 
    2223           23 :   return reporting_desc;
    2224              : }
    2225              : 
    2226              : /* Make a "reportingDescriptorReference" object (SARIF v2.1.0 section 3.52)
    2227              :    referencing CWE_ID, for use within a result object.
    2228              :    Also, add CWE_ID to m_cwe_id_set.  */
    2229              : 
    2230              : std::unique_ptr<sarif_reporting_descriptor_reference>
    2231           26 : sarif_builder::
    2232              : make_reporting_descriptor_reference_object_for_cwe_id (int cwe_id)
    2233              : {
    2234           26 :   auto desc_ref_obj = std::make_unique<sarif_reporting_descriptor_reference> ();
    2235              : 
    2236              :   /* "id" property (SARIF v2.1.0 section 3.52.4).  */
    2237           26 :   {
    2238           26 :     pretty_printer pp;
    2239           26 :     pp_printf (&pp, "%i", cwe_id);
    2240           26 :     desc_ref_obj->set_string ("id", pp_formatted_text (&pp));
    2241           26 :   }
    2242              : 
    2243              :   /* "toolComponent" property (SARIF v2.1.0 section 3.52.7).  */
    2244           26 :   desc_ref_obj->set<sarif_tool_component_reference>
    2245           26 :     ("toolComponent", make_tool_component_reference_object_for_cwe ());
    2246              : 
    2247              :   /* Add CWE_ID to our set.  */
    2248           26 :   gcc_assert (cwe_id > 0);
    2249           26 :   m_cwe_id_set.add (cwe_id);
    2250              : 
    2251           26 :   return desc_ref_obj;
    2252              : }
    2253              : 
    2254              : /* Make a "toolComponentReference" object (SARIF v2.1.0 section 3.54) that
    2255              :    references the CWE taxonomy.  */
    2256              : 
    2257              : std::unique_ptr<sarif_tool_component_reference>
    2258           26 : sarif_builder::
    2259              : make_tool_component_reference_object_for_cwe () const
    2260              : {
    2261           26 :   auto comp_ref_obj = std::make_unique<sarif_tool_component_reference> ();
    2262              : 
    2263              :   /* "name" property  (SARIF v2.1.0 section 3.54.3).  */
    2264           26 :   comp_ref_obj->set_string ("name", "cwe");
    2265              : 
    2266           26 :   return comp_ref_obj;
    2267              : }
    2268              : 
    2269              : /* Make an array suitable for use as the "locations" property of:
    2270              :    - a "result" object (SARIF v2.1.0 section 3.27.12), or
    2271              :    - a "notification" object (SARIF v2.1.0 section 3.58.4).
    2272              :    Use LOC_MGR for any locations that need "id" values.  */
    2273              : 
    2274              : std::unique_ptr<json::array>
    2275          486 : sarif_builder::make_locations_arr (sarif_location_manager &loc_mgr,
    2276              :                                    const diagnostic_info &diagnostic,
    2277              :                                    enum diagnostic_artifact_role role)
    2278              : {
    2279          486 :   auto locations_arr = std::make_unique<json::array> ();
    2280          486 :   logical_locations::key logical_loc;
    2281          486 :   if (auto client_data_hooks = m_context.get_client_data_hooks ())
    2282          294 :     logical_loc = client_data_hooks->get_current_logical_location ();
    2283              : 
    2284          486 :   auto location_obj
    2285          486 :     = make_location_object (&loc_mgr, *diagnostic.m_richloc, logical_loc, role);
    2286              :   /* Don't add entirely empty location objects to the array.  */
    2287          486 :   if (!location_obj->is_empty ())
    2288          422 :     locations_arr->append<sarif_location> (std::move (location_obj));
    2289              : 
    2290          972 :   return locations_arr;
    2291          486 : }
    2292              : 
    2293              : /* If LOGICAL_LOC is non-null, use it to create a "logicalLocations" property
    2294              :    within LOCATION_OBJ (SARIF v2.1.0 section 3.28.4) with a minimal logical
    2295              :    location object referencing theRuns.logicalLocations (3.33.3).  */
    2296              : 
    2297              : void
    2298          971 : sarif_builder::
    2299              : set_any_logical_locs_arr (sarif_location &location_obj,
    2300              :                           logical_locations::key logical_loc)
    2301              : {
    2302          971 :   if (!logical_loc)
    2303          662 :     return;
    2304          309 :   auto logical_loc_mgr = get_logical_location_manager ();
    2305          309 :   gcc_assert (logical_loc_mgr);
    2306          309 :   auto location_locs_arr = std::make_unique<json::array> ();
    2307              : 
    2308          309 :   auto logical_loc_obj = make_minimal_sarif_logical_location (logical_loc);
    2309              : 
    2310          309 :   location_locs_arr->append<sarif_logical_location>
    2311          309 :     (std::move (logical_loc_obj));
    2312              : 
    2313          309 :   location_obj.set<json::array> ("logicalLocations",
    2314              :                                  std::move (location_locs_arr));
    2315          309 : }
    2316              : 
    2317              : /* Make a "location" object (SARIF v2.1.0 section 3.28) for RICH_LOC
    2318              :    and LOGICAL_LOC.
    2319              :    Use LOC_MGR for any locations that need "id" values, and for
    2320              :    any worklist items.
    2321              :    Note that we might not always have a LOC_MGR; see
    2322              :    https://github.com/oasis-tcs/sarif-spec/issues/712  */
    2323              : 
    2324              : std::unique_ptr<sarif_location>
    2325          803 : sarif_builder::make_location_object (sarif_location_manager *loc_mgr,
    2326              :                                      const rich_location &rich_loc,
    2327              :                                      logical_locations::key logical_loc,
    2328              :                                      enum diagnostic_artifact_role role)
    2329              : {
    2330         1606 :   class escape_nonascii_renderer : public content_renderer
    2331              :   {
    2332              :   public:
    2333          803 :     escape_nonascii_renderer (const rich_location &richloc,
    2334              :                               enum diagnostics_escape_format escape_format)
    2335          803 :     : m_richloc (richloc),
    2336          803 :       m_escape_format (escape_format)
    2337              :     {}
    2338              : 
    2339              :     std::unique_ptr<sarif_multiformat_message_string>
    2340          136 :     render (const sarif_builder &builder) const final override
    2341              :     {
    2342          136 :       diagnostics::context dc;
    2343          136 :       diagnostic_initialize (&dc, 0);
    2344          136 :       auto &source_printing_opts = dc.get_source_printing_options ();
    2345          136 :       source_printing_opts.enabled = true;
    2346          136 :       source_printing_opts.colorize_source_p = false;
    2347          136 :       source_printing_opts.show_labels_p = true;
    2348          136 :       source_printing_opts.show_line_numbers_p = true;
    2349              : 
    2350          136 :       rich_location my_rich_loc (m_richloc);
    2351          136 :       my_rich_loc.set_escape_on_output (true);
    2352              : 
    2353          136 :       source_print_policy source_policy (dc);
    2354          136 :       dc.set_escape_format (m_escape_format);
    2355          136 :       text_sink text_output (dc);
    2356          136 :       source_policy.print (*text_output.get_printer (),
    2357              :                            my_rich_loc, kind::error, nullptr);
    2358              : 
    2359          136 :       const char *buf = pp_formatted_text (text_output.get_printer ());
    2360          136 :       std::unique_ptr<sarif_multiformat_message_string> result
    2361          136 :         = builder.make_multiformat_message_string (buf);
    2362              : 
    2363          136 :       diagnostic_finish (&dc);
    2364              : 
    2365          272 :       return result;
    2366          136 :     }
    2367              :   private:
    2368              :     const rich_location &m_richloc;
    2369              :     enum diagnostics_escape_format m_escape_format;
    2370          803 :   } the_renderer (rich_loc,
    2371          803 :                   m_context.get_escape_format ());
    2372              : 
    2373          803 :   auto location_obj = std::make_unique<sarif_location> ();
    2374              : 
    2375              :   /* Get primary loc from RICH_LOC.  */
    2376          803 :   location_t loc = rich_loc.get_loc ();
    2377              : 
    2378              :   /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3).  */
    2379          803 :   const content_renderer *snippet_renderer
    2380          803 :     = rich_loc.escape_on_output_p () ? &the_renderer : nullptr;
    2381          803 :   if (auto phs_loc_obj
    2382              :         = maybe_make_physical_location_object (loc, role,
    2383              :                                                rich_loc.get_column_override (),
    2384          803 :                                                snippet_renderer))
    2385          695 :     location_obj->set<sarif_physical_location> ("physicalLocation",
    2386          803 :                                                 std::move (phs_loc_obj));
    2387              : 
    2388              :   /* "logicalLocations" property (SARIF v2.1.0 section 3.28.4).  */
    2389          803 :   set_any_logical_locs_arr (*location_obj, logical_loc);
    2390              : 
    2391              :   /* Handle labelled ranges and/or secondary locations.  */
    2392          803 :   {
    2393          803 :     std::unique_ptr<json::array> annotations_arr = nullptr;
    2394         1884 :     for (unsigned int i = 0; i < rich_loc.get_num_locations (); i++)
    2395              :       {
    2396         1081 :         const location_range *range = rich_loc.get_range (i);
    2397         1081 :         bool handled = false;
    2398         1081 :         if (const range_label *label = range->m_label)
    2399              :           {
    2400          410 :             label_text text = label->get_text (i);
    2401          410 :             if (text.get ())
    2402              :               {
    2403              :                 /* Create annotations for any labelled ranges.  */
    2404          410 :                 location_t range_loc = rich_loc.get_loc (i);
    2405          410 :                 auto region
    2406              :                   = maybe_make_region_object (range_loc,
    2407          410 :                                               rich_loc.get_column_override ());
    2408          410 :                 if (region)
    2409              :                   {
    2410          410 :                     if (!annotations_arr)
    2411          137 :                       annotations_arr = std::make_unique<json::array> ();
    2412          410 :                     region->set<sarif_message>
    2413          410 :                       ("message", make_message_object (text.get ()));
    2414          410 :                     annotations_arr->append<sarif_region> (std::move (region));
    2415          410 :                     handled = true;
    2416              :                   }
    2417          410 :               }
    2418          410 :           }
    2419              : 
    2420              :         /* Add related locations for any secondary locations in RICH_LOC
    2421              :            that don't have labels (and thus aren't added to "annotations"). */
    2422         1081 :         if (loc_mgr && i > 0 && !handled)
    2423            4 :           loc_mgr->add_relationship_to_worklist
    2424            4 :             (*location_obj.get (),
    2425              :              sarif_location_manager::worklist_item::kind::unlabelled_secondary_location,
    2426            4 :              range->m_loc);
    2427              :       }
    2428          803 :     if (annotations_arr)
    2429              :       /* "annotations" property (SARIF v2.1.0 section 3.28.6).  */
    2430          137 :       location_obj->set<json::array> ("annotations",
    2431              :                                       std::move (annotations_arr));
    2432          803 :   }
    2433              : 
    2434          803 :   if (loc_mgr)
    2435          773 :     add_any_include_chain (*loc_mgr, *location_obj.get (), loc);
    2436              : 
    2437              :   /* A flag for hinting that the diagnostic involves issues at the
    2438              :      level of character encodings (such as homoglyphs, or misleading
    2439              :      bidirectional control codes), and thus that it will be helpful
    2440              :      to the user if we show some representation of
    2441              :      how the characters in the pertinent source lines are encoded.  */
    2442          803 :   if (rich_loc.escape_on_output_p ())
    2443              :     {
    2444          136 :       sarif_property_bag &bag = location_obj->get_or_create_properties ();
    2445          136 :       bag.set_bool ("gcc/escapeNonAscii", rich_loc.escape_on_output_p ());
    2446              :     }
    2447              : 
    2448          803 :   return location_obj;
    2449          803 : }
    2450              : 
    2451              : /* If WHERE was #included from somewhere, add a worklist item
    2452              :    to LOC_MGR to lazily add a location for the #include location,
    2453              :    and relationships between it and the LOCATION_OBJ.
    2454              :    Compare with diagnostics::context::report_current_module, but rather
    2455              :    than iterating the current chain, we add the next edge and iterate
    2456              :    in the worklist, so that edges are only added once.  */
    2457              : 
    2458              : void
    2459          962 : sarif_builder::add_any_include_chain (sarif_location_manager &loc_mgr,
    2460              :                                       sarif_location &location_obj,
    2461              :                                       location_t where)
    2462              : {
    2463          962 :   if (where <= BUILTINS_LOCATION)
    2464          939 :     return;
    2465              : 
    2466          860 :   const line_map_ordinary *map = nullptr;
    2467          860 :   linemap_resolve_location (m_line_maps, where,
    2468              :                             LRK_MACRO_DEFINITION_LOCATION,
    2469              :                             &map);
    2470              : 
    2471          860 :   if (!map)
    2472              :     return;
    2473              : 
    2474          860 :   location_t include_loc = linemap_included_from (map);
    2475          860 :   map = linemap_included_from_linemap (m_line_maps, map);
    2476          860 :   if (!map)
    2477              :     return;
    2478           23 :   loc_mgr.add_relationship_to_worklist
    2479           23 :     (location_obj,
    2480              :      sarif_result::worklist_item::kind::included_from,
    2481              :      include_loc);
    2482              : }
    2483              : 
    2484              : /* Make a "location" object (SARIF v2.1.0 section 3.28) for WHERE
    2485              :    within an include chain.  */
    2486              : 
    2487              : std::unique_ptr<sarif_location>
    2488           21 : sarif_builder::make_location_object (sarif_location_manager &loc_mgr,
    2489              :                                      location_t loc,
    2490              :                                      enum diagnostic_artifact_role role)
    2491              : {
    2492           21 :   auto location_obj = std::make_unique<sarif_location> ();
    2493              : 
    2494              :   /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3).  */
    2495           21 :   if (auto phs_loc_obj
    2496           21 :       = maybe_make_physical_location_object (loc, role, 0, nullptr))
    2497           21 :     location_obj->set<sarif_physical_location> ("physicalLocation",
    2498           21 :                                                 std::move (phs_loc_obj));
    2499              : 
    2500           21 :   add_any_include_chain (loc_mgr, *location_obj.get (), loc);
    2501              : 
    2502           21 :   return location_obj;
    2503              : }
    2504              : 
    2505              : /* Make a "location" object (SARIF v2.1.0 section 3.28) for EVENT
    2506              :    within a paths::path.  */
    2507              : 
    2508              : std::unique_ptr<sarif_location>
    2509          168 : sarif_builder::make_location_object (sarif_location_manager &loc_mgr,
    2510              :                                      const paths::event &event,
    2511              :                                      enum diagnostic_artifact_role role)
    2512              : {
    2513          168 :   auto location_obj = std::make_unique<sarif_location> ();
    2514              : 
    2515              :   /* "physicalLocation" property (SARIF v2.1.0 section 3.28.3).  */
    2516          168 :   location_t loc = event.get_location ();
    2517          168 :   if (auto phs_loc_obj
    2518          168 :         = maybe_make_physical_location_object (loc, role, 0, nullptr))
    2519          168 :     location_obj->set<sarif_physical_location> ("physicalLocation",
    2520          168 :                                                 std::move (phs_loc_obj));
    2521              : 
    2522              :   /* "logicalLocations" property (SARIF v2.1.0 section 3.28.4).  */
    2523          168 :   logical_locations::key logical_loc = event.get_logical_location ();
    2524          168 :   set_any_logical_locs_arr (*location_obj, logical_loc);
    2525              : 
    2526              :   /* "message" property (SARIF v2.1.0 section 3.28.5).  */
    2527          168 :   std::unique_ptr<pretty_printer> pp = get_printer ()->clone ();
    2528          168 :   event.print_desc (*pp);
    2529          168 :   location_obj->set<sarif_message>
    2530          336 :     ("message",
    2531          168 :      make_message_object (pp_formatted_text (pp.get ())));
    2532              : 
    2533          168 :   add_any_include_chain (loc_mgr, *location_obj.get (), loc);
    2534              : 
    2535          336 :   return location_obj;
    2536          168 : }
    2537              : 
    2538              : /* Make a "physicalLocation" object (SARIF v2.1.0 section 3.29) for LOC.
    2539              : 
    2540              :    If COLUMN_OVERRIDE is non-zero, then use it as the column number
    2541              :    if LOC has no column information.
    2542              : 
    2543              :    Ensure that we have an artifact object for the file, adding ROLE to it,
    2544              :    and flagging that we will attempt to embed the contents of the artifact
    2545              :    when writing it out.  */
    2546              : 
    2547              : std::unique_ptr<sarif_physical_location>
    2548          992 : sarif_builder::
    2549              : maybe_make_physical_location_object (location_t loc,
    2550              :                                      enum diagnostic_artifact_role role,
    2551              :                                      int column_override,
    2552              :                                      const content_renderer *snippet_renderer)
    2553              : {
    2554          992 :   if (loc <= BUILTINS_LOCATION || LOCATION_FILE (loc) == nullptr)
    2555          108 :     return nullptr;
    2556              : 
    2557          884 :   auto phys_loc_obj = std::make_unique<sarif_physical_location> ();
    2558              : 
    2559              :   /* "artifactLocation" property (SARIF v2.1.0 section 3.29.3).  */
    2560          884 :   phys_loc_obj->set<sarif_artifact_location>
    2561          884 :     ("artifactLocation", make_artifact_location_object (loc));
    2562          884 :   get_or_create_artifact (LOCATION_FILE (loc), role, true);
    2563              : 
    2564              :   /* "region" property (SARIF v2.1.0 section 3.29.4).  */
    2565          884 :   if (auto region_obj = maybe_make_region_object (loc, column_override))
    2566          884 :     phys_loc_obj->set<sarif_region> ("region", std::move (region_obj));
    2567              : 
    2568              :   /* "contextRegion" property (SARIF v2.1.0 section 3.29.5).  */
    2569          884 :   if (auto context_region_obj
    2570          884 :         = maybe_make_region_object_for_context (loc, snippet_renderer))
    2571          880 :     phys_loc_obj->set<sarif_region> ("contextRegion",
    2572          884 :                                      std::move (context_region_obj));
    2573              : 
    2574              :   /* Instead, we add artifacts to the run as a whole,
    2575              :      with artifact.contents.
    2576              :      Could do both, though.  */
    2577              : 
    2578          884 :   return phys_loc_obj;
    2579          884 : }
    2580              : 
    2581              : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for LOC,
    2582              :    or return nullptr.  */
    2583              : 
    2584              : std::unique_ptr<sarif_artifact_location>
    2585          893 : sarif_builder::make_artifact_location_object (location_t loc)
    2586              : {
    2587          893 :   return make_artifact_location_object (LOCATION_FILE (loc));
    2588              : }
    2589              : 
    2590              : /* The ID value for use in "uriBaseId" properties (SARIF v2.1.0 section 3.4.4)
    2591              :    for when we need to express paths relative to PWD.  */
    2592              : 
    2593              : #define PWD_PROPERTY_NAME ("PWD")
    2594              : 
    2595              : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for FILENAME,
    2596              :    or return nullptr.  */
    2597              : 
    2598              : std::unique_ptr<sarif_artifact_location>
    2599         1749 : sarif_builder::make_artifact_location_object (const char *filename)
    2600              : {
    2601         1749 :   auto artifact_loc_obj = std::make_unique<sarif_artifact_location> ();
    2602              : 
    2603              :   /* "uri" property (SARIF v2.1.0 section 3.4.3).  */
    2604         1749 :   artifact_loc_obj->set_string ("uri", filename);
    2605              : 
    2606         1749 :   if (filename[0] != '/')
    2607              :     {
    2608              :       /* If we have a relative path, set the "uriBaseId" property
    2609              :          (SARIF v2.1.0 section 3.4.4).  */
    2610           56 :       artifact_loc_obj->set_string ("uriBaseId", PWD_PROPERTY_NAME);
    2611           56 :       m_seen_any_relative_paths = true;
    2612              :     }
    2613              : 
    2614         1749 :   return artifact_loc_obj;
    2615              : }
    2616              : 
    2617              : /* Get the PWD, or nullptr, as an absolute file-based URI,
    2618              :    adding a trailing forward slash (as required by SARIF v2.1.0
    2619              :    section 3.14.14).  */
    2620              : 
    2621              : static char *
    2622           44 : make_pwd_uri_str ()
    2623              : {
    2624              :   /* The prefix of a file-based URI, up to, but not including the path. */
    2625              : #define FILE_PREFIX ("file://")
    2626              : 
    2627           44 :   const char *pwd = getpwd ();
    2628           44 :   if (!pwd)
    2629              :     return nullptr;
    2630           44 :   size_t len = strlen (pwd);
    2631           44 :   if (len == 0 || pwd[len - 1] != '/')
    2632           44 :     return concat (FILE_PREFIX, pwd, "/", nullptr);
    2633              :   else
    2634              :     {
    2635            0 :       gcc_assert (pwd[len - 1] == '/');
    2636            0 :       return concat (FILE_PREFIX, pwd, nullptr);
    2637              :     }
    2638              : }
    2639              : 
    2640              : /* Make an "artifactLocation" object (SARIF v2.1.0 section 3.4) for the pwd,
    2641              :    for use in the "run.originalUriBaseIds" property (SARIF v2.1.0
    2642              :    section 3.14.14) when we have any relative paths.  */
    2643              : 
    2644              : std::unique_ptr<sarif_artifact_location>
    2645           44 : sarif_builder::make_artifact_location_object_for_pwd () const
    2646              : {
    2647           44 :   auto artifact_loc_obj = std::make_unique<sarif_artifact_location> ();
    2648              : 
    2649              :   /* "uri" property (SARIF v2.1.0 section 3.4.3).  */
    2650           44 :   if (char *pwd = make_pwd_uri_str ())
    2651              :     {
    2652           44 :       gcc_assert (strlen (pwd) > 0);
    2653           44 :       gcc_assert (pwd[strlen (pwd) - 1] == '/');
    2654           44 :       artifact_loc_obj->set_string ("uri", pwd);
    2655           44 :       free (pwd);
    2656              :     }
    2657              : 
    2658           44 :   return artifact_loc_obj;
    2659              : }
    2660              : 
    2661              : /* Get the column number within EXPLOC.  */
    2662              : 
    2663              : int
    2664         2396 : sarif_builder::get_sarif_column (expanded_location exploc) const
    2665              : {
    2666         2396 :   cpp_char_column_policy policy (m_tabstop, cpp_wcwidth);
    2667         2396 :   return location_compute_display_column (m_context.get_file_cache (),
    2668         2396 :                                           exploc, policy);
    2669              : }
    2670              : 
    2671              : /* Make a "region" object (SARIF v2.1.0 section 3.30) for LOC,
    2672              :    or return nullptr.
    2673              : 
    2674              :    If COLUMN_OVERRIDE is non-zero, then use it as the column number
    2675              :    if LOC has no column information.
    2676              : 
    2677              :    We only support text properties of regions ("text regions"),
    2678              :    not binary properties ("binary regions"); see 3.30.1.  */
    2679              : 
    2680              : std::unique_ptr<sarif_region>
    2681         1294 : sarif_builder::maybe_make_region_object (location_t loc,
    2682              :                                          int column_override) const
    2683              : {
    2684         1294 :   location_t caret_loc = get_pure_location (loc);
    2685              : 
    2686         1294 :   if (caret_loc <= BUILTINS_LOCATION)
    2687            0 :     return nullptr;
    2688              : 
    2689         1294 :   location_t start_loc = get_start (loc);
    2690         1294 :   location_t finish_loc = get_finish (loc);
    2691              : 
    2692         1294 :   expanded_location exploc_caret = expand_location (caret_loc);
    2693         1294 :   expanded_location exploc_start = expand_location (start_loc);
    2694         1294 :   expanded_location exploc_finish = expand_location (finish_loc);
    2695              : 
    2696         1294 :   if (exploc_start.file !=exploc_caret.file)
    2697            0 :     return nullptr;
    2698         1294 :   if (exploc_finish.file !=exploc_caret.file)
    2699            0 :     return nullptr;
    2700              : 
    2701              :   /* We can have line == 0 in the presence of "#" lines.
    2702              :      SARIF requires lines > 0, so if we hit this case we don't have a
    2703              :      way of validly representing the region as SARIF; bail out.  */
    2704         1294 :   if (exploc_start.line <= 0)
    2705            4 :     return nullptr;
    2706              : 
    2707         1290 :   auto region_obj = std::make_unique<sarif_region> ();
    2708              : 
    2709              :   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
    2710         1290 :   region_obj->set_integer ("startLine", exploc_start.line);
    2711              : 
    2712              :   /* "startColumn" property (SARIF v2.1.0 section 3.30.6).
    2713              : 
    2714              :      We use column == 0 to mean the whole line, so omit the column
    2715              :      information for this case, unless COLUMN_OVERRIDE is non-zero,
    2716              :      (for handling certain awkward lexer diagnostics)  */
    2717              : 
    2718         1290 :   if (exploc_start.column == 0 && column_override)
    2719              :     /* Use the provided column number.  */
    2720              :     exploc_start.column = column_override;
    2721              : 
    2722         1290 :   if (exploc_start.column > 0)
    2723              :     {
    2724         1273 :       int start_column = get_sarif_column (exploc_start);
    2725         1273 :       region_obj->set_integer ("startColumn", start_column);
    2726              :     }
    2727              : 
    2728              :   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
    2729         1290 :   if (exploc_finish.line != exploc_start.line
    2730            0 :       && exploc_finish.line > 0)
    2731            0 :     region_obj->set_integer ("endLine", exploc_finish.line);
    2732              : 
    2733              :   /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
    2734              :      This expresses the column immediately beyond the range.
    2735              : 
    2736              :      We use column == 0 to mean the whole line, so omit the column
    2737              :      information for this case.  */
    2738         1290 :   if (exploc_finish.column > 0)
    2739              :     {
    2740         1105 :       int next_column = get_sarif_column (exploc_finish) + 1;
    2741         1105 :       region_obj->set_integer ("endColumn", next_column);
    2742              :     }
    2743              : 
    2744         1290 :   return region_obj;
    2745         1290 : }
    2746              : 
    2747              : /* Make a "region" object (SARIF v2.1.0 section 3.30) for the "contextRegion"
    2748              :    property (SARIF v2.1.0 section 3.29.5) of a "physicalLocation".
    2749              : 
    2750              :    This is similar to maybe_make_region_object, but ignores column numbers,
    2751              :    covering the line(s) as a whole, and including a "snippet" property
    2752              :    embedding those source lines, making it easier for consumers to show
    2753              :    the pertinent source.  */
    2754              : 
    2755              : std::unique_ptr<sarif_region>
    2756          884 : sarif_builder::
    2757              : maybe_make_region_object_for_context (location_t loc,
    2758              :                                       const content_renderer *snippet_renderer)
    2759              :   const
    2760              : {
    2761          884 :   location_t caret_loc = get_pure_location (loc);
    2762              : 
    2763          884 :   if (caret_loc <= BUILTINS_LOCATION)
    2764            0 :     return nullptr;
    2765              : 
    2766          884 :   location_t start_loc = get_start (loc);
    2767          884 :   location_t finish_loc = get_finish (loc);
    2768              : 
    2769          884 :   expanded_location exploc_caret = expand_location (caret_loc);
    2770          884 :   expanded_location exploc_start = expand_location (start_loc);
    2771          884 :   expanded_location exploc_finish = expand_location (finish_loc);
    2772              : 
    2773          884 :   if (exploc_start.file !=exploc_caret.file)
    2774            0 :     return nullptr;
    2775          884 :   if (exploc_finish.file !=exploc_caret.file)
    2776            0 :     return nullptr;
    2777              : 
    2778              :   /* We can have line == 0 in the presence of "#" lines.
    2779              :      SARIF requires lines > 0, so if we hit this case we don't have a
    2780              :      way of validly representing the region as SARIF; bail out.  */
    2781          884 :   if (exploc_start.line <= 0)
    2782            4 :     return nullptr;
    2783              : 
    2784          880 :   auto region_obj = std::make_unique<sarif_region> ();
    2785              : 
    2786              :   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
    2787          880 :   region_obj->set_integer ("startLine", exploc_start.line);
    2788              : 
    2789              :   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
    2790          880 :   if (exploc_finish.line != exploc_start.line
    2791            0 :       && exploc_finish.line > 0)
    2792            0 :     region_obj->set_integer ("endLine", exploc_finish.line);
    2793              : 
    2794              :   /* "snippet" property (SARIF v2.1.0 section 3.30.13).  */
    2795          880 :   if (auto artifact_content_obj
    2796              :         = maybe_make_artifact_content_object (exploc_start.file,
    2797              :                                               exploc_start.line,
    2798              :                                               exploc_finish.line,
    2799          880 :                                               snippet_renderer))
    2800          708 :     region_obj->set<sarif_artifact_content> ("snippet",
    2801          880 :                                              std::move (artifact_content_obj));
    2802              : 
    2803          880 :   return region_obj;
    2804          880 : }
    2805              : 
    2806              : /* Make a "region" object (SARIF v2.1.0 section 3.30) for the deletion region
    2807              :    of HINT (as per SARIF v2.1.0 section 3.57.3).  */
    2808              : 
    2809              : std::unique_ptr<sarif_region>
    2810            9 : sarif_builder::make_region_object_for_hint (const fixit_hint &hint) const
    2811              : {
    2812            9 :   location_t start_loc = hint.get_start_loc ();
    2813            9 :   location_t next_loc = hint.get_next_loc ();
    2814              : 
    2815            9 :   expanded_location exploc_start = expand_location (start_loc);
    2816            9 :   expanded_location exploc_next = expand_location (next_loc);
    2817              : 
    2818            9 :   auto region_obj = std::make_unique<sarif_region> ();
    2819              : 
    2820              :   /* "startLine" property (SARIF v2.1.0 section 3.30.5) */
    2821            9 :   region_obj->set_integer ("startLine", exploc_start.line);
    2822              : 
    2823              :   /* "startColumn" property (SARIF v2.1.0 section 3.30.6) */
    2824            9 :   int start_col = get_sarif_column (exploc_start);
    2825            9 :   region_obj->set_integer ("startColumn", start_col);
    2826              : 
    2827              :   /* "endLine" property (SARIF v2.1.0 section 3.30.7) */
    2828            9 :   if (exploc_next.line != exploc_start.line)
    2829            0 :     region_obj->set_integer ("endLine", exploc_next.line);
    2830              : 
    2831              :   /* "endColumn" property (SARIF v2.1.0 section 3.30.8).
    2832              :      This expresses the column immediately beyond the range.  */
    2833            9 :   int next_col =  get_sarif_column (exploc_next);
    2834            9 :   region_obj->set_integer ("endColumn", next_col);
    2835              : 
    2836            9 :   return region_obj;
    2837              : }
    2838              : 
    2839              : /* Attempt to get a string for a logicalLocation's "kind" property
    2840              :    (SARIF v2.1.0 section 3.33.7).
    2841              :    Return nullptr if unknown.  */
    2842              : 
    2843              : static const char *
    2844          589 : maybe_get_sarif_kind (enum logical_locations::kind kind)
    2845              : {
    2846          589 :   using namespace logical_locations;
    2847              : 
    2848          589 :   switch (kind)
    2849              :     {
    2850            0 :     default:
    2851            0 :       gcc_unreachable ();
    2852              :     case logical_locations::kind::unknown:
    2853              :       return nullptr;
    2854              : 
    2855              :     /* Kinds within executable code.  */
    2856          354 :     case logical_locations::kind::function:
    2857          354 :       return "function";
    2858            0 :     case logical_locations::kind::member:
    2859            0 :       return "member";
    2860            0 :     case logical_locations::kind::module_:
    2861            0 :       return "module";
    2862          217 :     case logical_locations::kind::namespace_:
    2863          217 :       return "namespace";
    2864           18 :     case logical_locations::kind::type:
    2865           18 :       return "type";
    2866            0 :     case logical_locations::kind::return_type:
    2867            0 :       return "returnType";
    2868            0 :     case logical_locations::kind::parameter:
    2869            0 :       return "parameter";
    2870            0 :     case logical_locations::kind::variable:
    2871            0 :       return "variable";
    2872              : 
    2873              :     /* Kinds within XML or HTML documents.  */
    2874            0 :     case logical_locations::kind::element:
    2875            0 :       return "element";
    2876            0 :     case logical_locations::kind::attribute:
    2877            0 :       return "attribute";
    2878            0 :     case logical_locations::kind::text:
    2879            0 :       return "text";
    2880            0 :     case logical_locations::kind::comment:
    2881            0 :       return "comment";
    2882            0 :     case logical_locations::kind::processing_instruction:
    2883            0 :       return "processingInstruction";
    2884            0 :     case logical_locations::kind::dtd:
    2885            0 :       return "dtd";
    2886            0 :     case logical_locations::kind::declaration:
    2887            0 :       return "declaration";
    2888              : 
    2889              :     /* Kinds within JSON documents.  */
    2890            0 :     case logical_locations::kind::object:
    2891            0 :       return "object";
    2892            0 :     case logical_locations::kind::array:
    2893            0 :       return "array";
    2894            0 :     case logical_locations::kind::property:
    2895            0 :       return "property";
    2896            0 :     case logical_locations::kind::value:
    2897            0 :       return "value";
    2898              :     }
    2899              : }
    2900              : 
    2901              : /* Set PROPERTY_NAME within this bag to a "logicalLocation" object (SARIF v2.1.0
    2902              :    section 3.33) for LOGICAL_LOC.  The object has an "index" property to refer to
    2903              :    theRuns.logicalLocations (3.33.3).  */
    2904              : 
    2905              : void
    2906            0 : sarif_property_bag::set_logical_location (const char *property_name,
    2907              :                                           sarif_builder &builder,
    2908              :                                           logical_locations::key logical_loc)
    2909              : {
    2910            0 :   set<sarif_logical_location>
    2911            0 :     (property_name,
    2912            0 :      builder.make_minimal_sarif_logical_location (logical_loc));
    2913            0 : }
    2914              : 
    2915              : static void
    2916         2076 : copy_any_property_bag (const digraphs::object &input_obj,
    2917              :                        sarif_object &output_obj)
    2918              : {
    2919         2076 :   if (input_obj.get_property_bag ())
    2920              :     {
    2921         1192 :       const json::object &old_bag = *input_obj.get_property_bag ();
    2922         1192 :       sarif_property_bag &new_bag = output_obj.get_or_create_properties ();
    2923         5551 :       for (size_t i = 0; i < old_bag.get_num_keys (); ++i)
    2924              :         {
    2925         3167 :           const char *key = old_bag.get_key (i);
    2926         3167 :           json::value *val = old_bag.get (key);
    2927         3167 :           new_bag.set (key, val->clone ());
    2928              :         }
    2929              :     }
    2930         2076 : }
    2931              : 
    2932              : std::unique_ptr<sarif_graph>
    2933           85 : make_sarif_graph (const digraphs::digraph &g,
    2934              :                   sarif_builder *builder,
    2935              :                   sarif_location_manager *sarif_location_mgr)
    2936              : {
    2937           85 :   auto result = std::make_unique<sarif_graph> ();
    2938              : 
    2939              :   // 3.39.2 description property
    2940           85 :   if (const char *desc = g.get_description ())
    2941           54 :     if (builder)
    2942           50 :       result->set<sarif_message> ("description",
    2943           50 :                                   builder->make_message_object (desc));
    2944              : 
    2945           85 :   copy_any_property_bag (g, *result);
    2946              : 
    2947              :   // 3.39.3 nodes property
    2948           85 :   auto nodes_arr = std::make_unique<json::array> ();
    2949           85 :   const int num_nodes = g.get_num_nodes ();
    2950          232 :   for (int i = 0; i < num_nodes; ++i)
    2951          147 :     nodes_arr->append (make_sarif_node (g.get_node (i),
    2952              :                                         builder,
    2953              :                                         sarif_location_mgr));
    2954           85 :   result->set ("nodes", std::move (nodes_arr));
    2955              : 
    2956              :   // 3.39.4 edges property
    2957           85 :   auto edges_arr = std::make_unique<json::array> ();
    2958           85 :   const int num_edges = g.get_num_edges ();
    2959          822 :   for (int i = 0; i < num_edges; ++i)
    2960          737 :     edges_arr->append (make_sarif_edge (g.get_edge (i), builder));
    2961           85 :   result->set ("edges", std::move (edges_arr));
    2962              : 
    2963          170 :   return result;
    2964           85 : }
    2965              : 
    2966              : std::unique_ptr<sarif_node>
    2967         1254 : make_sarif_node (const digraphs::node &n,
    2968              :                  sarif_builder *builder,
    2969              :                  sarif_location_manager *sarif_location_mgr)
    2970              : {
    2971         1254 :   auto result = std::make_unique<sarif_node> ();
    2972              : 
    2973              :   // 3.40.2 id property
    2974         2508 :   result->set_string ("id", n.get_id ());
    2975              : 
    2976         1254 :   copy_any_property_bag (n, *result);
    2977              : 
    2978              :   // 3.40.3 label property
    2979         1254 :   if (const char *label = n.get_label ())
    2980          405 :     if (builder)
    2981          401 :       result->set<sarif_message> ("label",
    2982          401 :                                   builder->make_message_object (label));
    2983              : 
    2984              :   // 3.40.4 location property
    2985         1254 :   if (n.get_logical_loc ()
    2986         1254 :       || n.get_physical_loc () != UNKNOWN_LOCATION)
    2987           68 :     if (builder)
    2988              :       {
    2989           68 :         rich_location rich_loc
    2990           68 :           (line_table, n.get_physical_loc ());
    2991           68 :         auto loc_obj
    2992              :           = builder->make_location_object
    2993              :               (sarif_location_mgr,
    2994              :                rich_loc,
    2995              :                n.get_logical_loc (),
    2996           68 :                diagnostic_artifact_role::scanned_file);
    2997           68 :         result->set<sarif_location> ("location",
    2998              :                                      std::move (loc_obj));
    2999           68 :     }
    3000              : 
    3001              :   // 3.40.5 children property
    3002         1254 :   if (const int num_children = n.get_num_children ())
    3003              :     {
    3004          307 :       auto children_arr = std::make_unique<json::array> ();
    3005         1721 :       for (int i = 0; i < num_children; ++i)
    3006         1107 :         children_arr->append (make_sarif_node (n.get_child (i),
    3007              :                                                builder,
    3008              :                                                sarif_location_mgr));
    3009          307 :       result->set ("children", std::move (children_arr));
    3010          307 :     }
    3011              : 
    3012         1254 :   return result;
    3013              : }
    3014              : 
    3015              : std::unique_ptr<sarif_edge>
    3016          737 : make_sarif_edge (const digraphs::edge &e,
    3017              :                  sarif_builder *builder)
    3018              : {
    3019          737 :   auto result = std::make_unique<sarif_edge> ();
    3020              : 
    3021              :   // 3.41.2 id property
    3022         1474 :   result->set_string ("id", e.get_id ());
    3023              : 
    3024          737 :   copy_any_property_bag (e, *result);
    3025              : 
    3026              :   // 3.41.3 label property
    3027          737 :   if (const char *label = e.get_label ())
    3028          405 :     if (builder)
    3029          401 :       result->set<sarif_message> ("label",
    3030          401 :                                   builder->make_message_object (label));
    3031              : 
    3032              :   // 3.41.4 sourceNodeId property
    3033         1474 :   result->set_string ("sourceNodeId", e.get_src_node ().get_id ());
    3034              : 
    3035              :   // 3.41.5 targetNodeId property
    3036         1474 :   result->set_string ("targetNodeId", e.get_dst_node ().get_id ());
    3037              : 
    3038          737 :   return result;
    3039              : }
    3040              : 
    3041              : void
    3042           27 : sarif_property_bag::set_graph (const char *property_name,
    3043              :                                sarif_builder &builder,
    3044              :                                sarif_location_manager *sarif_location_mgr,
    3045              :                                const digraphs::digraph &g)
    3046              : {
    3047           54 :   set<sarif_graph> (property_name,
    3048           27 :                     make_sarif_graph (g, &builder, sarif_location_mgr));
    3049           27 : }
    3050              : 
    3051              : /* Ensure that m_cached_logical_locs has a "logicalLocation" object
    3052              :    (SARIF v2.1.0 section 3.33) for K, and return its index within the
    3053              :    array.  */
    3054              : 
    3055              : int
    3056          589 : sarif_builder::
    3057              : ensure_sarif_logical_location_for (logical_locations::key k)
    3058              : {
    3059          589 :   auto logical_loc_mgr = get_logical_location_manager ();
    3060          589 :   gcc_assert (logical_loc_mgr);
    3061              : 
    3062          589 :   auto sarif_logical_loc = std::make_unique<sarif_logical_location> ();
    3063              : 
    3064          589 :   label_text short_name = logical_loc_mgr->get_short_name (k);
    3065          589 :   if (short_name.get ())
    3066          589 :     sarif_logical_loc->set_string ("name", short_name.get ());
    3067              : 
    3068              :   /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5).  */
    3069          589 :   label_text name_with_scope = logical_loc_mgr->get_name_with_scope (k);
    3070          589 :   if (name_with_scope.get ())
    3071          571 :     sarif_logical_loc->set_string ("fullyQualifiedName",
    3072              :                                    name_with_scope.get ());
    3073              : 
    3074              :   /* "decoratedName" property (SARIF v2.1.0 section 3.33.6).  */
    3075          589 :   label_text internal_name = logical_loc_mgr->get_internal_name (k);
    3076          589 :   if (internal_name.get ())
    3077          354 :     sarif_logical_loc->set_string ("decoratedName", internal_name.get ());
    3078              : 
    3079              :   /* "kind" property (SARIF v2.1.0 section 3.33.7).  */
    3080          589 :   enum logical_locations::kind kind = logical_loc_mgr->get_kind (k);
    3081          589 :   if (const char *sarif_kind_str = maybe_get_sarif_kind (kind))
    3082          589 :     sarif_logical_loc->set_string ("kind", sarif_kind_str);
    3083              : 
    3084              :   /* "parentIndex" property (SARIF v2.1.0 section 3.33.8).  */
    3085          589 :   if (auto parent_key = logical_loc_mgr->get_parent (k))
    3086              :     {
    3087              :       /* Recurse upwards.  */
    3088          235 :       int parent_index = ensure_sarif_logical_location_for (parent_key);
    3089          235 :       sarif_logical_loc->set_integer ("parentIndex", parent_index);
    3090              :     }
    3091              : 
    3092              :   /* Consolidate if this logical location already exists.  */
    3093          589 :   int index
    3094          589 :     = m_cached_logical_locs->append_uniquely (std::move (sarif_logical_loc));
    3095              : 
    3096          589 :   return index;
    3097          589 : }
    3098              : 
    3099              : /* Ensure that theRuns.logicalLocations (3.14.17) has a "logicalLocation" object
    3100              :    (SARIF v2.1.0 section 3.33) for LOGICAL_LOC.
    3101              :    Create and return a minimal logicalLocation object referring to the
    3102              :    full object by index.  */
    3103              : 
    3104              : std::unique_ptr<sarif_logical_location>
    3105          354 : sarif_builder::
    3106              : make_minimal_sarif_logical_location (logical_locations::key logical_loc)
    3107              : {
    3108          354 :   auto logical_loc_mgr = get_logical_location_manager ();
    3109          354 :   gcc_assert (logical_loc_mgr);
    3110              : 
    3111              :   /* Ensure that m_cached_logical_locs has a "logicalLocation" object
    3112              :      (SARIF v2.1.0 section 3.33) for LOGICAL_LOC, and return its index within
    3113              :      the array.  */
    3114              : 
    3115          354 :   auto sarif_logical_loc = std::make_unique <sarif_logical_location> ();
    3116              : 
    3117          354 :   int index = ensure_sarif_logical_location_for (logical_loc);
    3118              : 
    3119              :   // 3.33.3 index property
    3120          354 :   sarif_logical_loc->set_integer ("index", index);
    3121              : 
    3122              :   /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5).  */
    3123          354 :   label_text name_with_scope
    3124          354 :     = logical_loc_mgr->get_name_with_scope (logical_loc);
    3125          354 :   if (name_with_scope.get ())
    3126          354 :     sarif_logical_loc->set_string ("fullyQualifiedName",
    3127              :                                    name_with_scope.get ());
    3128              : 
    3129          354 :   return sarif_logical_loc;
    3130          354 : }
    3131              : 
    3132              : label_text
    3133          120 : make_sarif_url_for_event (const sarif_code_flow *code_flow,
    3134              :                           paths::event_id_t event_id)
    3135              : {
    3136          120 :   gcc_assert (event_id.known_p ());
    3137              : 
    3138          120 :   if (!code_flow)
    3139          102 :     return label_text ();
    3140              : 
    3141           18 :   const sarif_thread_flow_location &tfl_obj
    3142           18 :     = code_flow->get_thread_flow_loc_obj (event_id);
    3143           18 :   const int location_idx = tfl_obj.get_index_within_parent ();
    3144              : 
    3145           18 :   const sarif_thread_flow &thread_flow_obj = tfl_obj.get_parent ();
    3146           18 :   const int thread_flow_idx = thread_flow_obj.get_index_within_parent ();
    3147              : 
    3148           18 :   const sarif_code_flow &code_flow_obj = thread_flow_obj.get_parent ();
    3149           18 :   const int code_flow_idx = code_flow_obj.get_index_within_parent ();
    3150              : 
    3151           18 :   const sarif_result &result_obj = code_flow_obj.get_parent ();
    3152           18 :   const int result_idx = result_obj.get_index_within_parent ();
    3153              : 
    3154              :   /* We only support a single run object in the log.  */
    3155           18 :   const int run_idx = 0;
    3156              : 
    3157           18 :   char *buf = xasprintf
    3158           18 :     ("sarif:/runs/%i/results/%i/codeFlows/%i/threadFlows/%i/locations/%i",
    3159              :      run_idx, result_idx, code_flow_idx, thread_flow_idx, location_idx);
    3160           18 :   return label_text::take (buf);
    3161              : }
    3162              : 
    3163              : /* Make a "codeFlow" object (SARIF v2.1.0 section 3.36) for PATH.  */
    3164              : 
    3165              : std::unique_ptr<sarif_code_flow>
    3166           35 : sarif_builder::make_code_flow_object (sarif_result &result,
    3167              :                                       unsigned idx_within_parent,
    3168              :                                       const paths::path &path)
    3169              : {
    3170           35 :   auto code_flow_obj
    3171           35 :     = std::make_unique <sarif_code_flow> (result, idx_within_parent);
    3172              : 
    3173              :   /* First pass:
    3174              :      Create threadFlows and threadFlowLocation objects within them,
    3175              :      effectively recording a mapping from event_id to threadFlowLocation
    3176              :      so that we can later go from an event_id to a URI within the
    3177              :      SARIF file.  */
    3178          238 :   for (unsigned i = 0; i < path.num_events (); i++)
    3179              :     {
    3180          168 :       const paths::event &event = path.get_event (i);
    3181          168 :       const paths::thread_id_t thread_id = event.get_thread_id ();
    3182              : 
    3183          168 :       sarif_thread_flow &thread_flow_obj
    3184          168 :         = code_flow_obj->get_or_append_thread_flow (path.get_thread (thread_id),
    3185              :                                                     thread_id);
    3186          168 :       thread_flow_obj.add_location ();
    3187              :     }
    3188              : 
    3189              :   /* Second pass: walk the events, populating the tfl objs.  */
    3190           35 :   m_current_code_flow = code_flow_obj.get ();
    3191          203 :   for (unsigned i = 0; i < path.num_events (); i++)
    3192              :     {
    3193          168 :       const paths::event &event = path.get_event (i);
    3194          168 :       sarif_thread_flow_location &thread_flow_loc_obj
    3195          168 :         = code_flow_obj->get_thread_flow_loc_obj (i);
    3196          168 :       populate_thread_flow_location_object (result,
    3197              :                                             thread_flow_loc_obj,
    3198              :                                             event,
    3199              :                                             i);
    3200              :     }
    3201           35 :   m_current_code_flow = nullptr;
    3202              : 
    3203           35 :   return code_flow_obj;
    3204              : }
    3205              : 
    3206              : /* Populate TFL_OBJ, a "threadFlowLocation" object (SARIF v2.1.0 section 3.38)
    3207              :    based on EVENT.  */
    3208              : 
    3209              : void
    3210          168 : sarif_builder::
    3211              : populate_thread_flow_location_object (sarif_result &result,
    3212              :                                       sarif_thread_flow_location &tfl_obj,
    3213              :                                       const paths::event &ev,
    3214              :                                       int event_execution_idx)
    3215              : {
    3216              :   /* Give paths::event subclasses a chance to add custom properties
    3217              :      via a property bag.  */
    3218          168 :   ev.maybe_add_sarif_properties (*this, tfl_obj);
    3219              : 
    3220          168 :   if (get_opts ().m_state_graph)
    3221           27 :     if (auto state_graph = ev.maybe_make_diagnostic_state_graph (true))
    3222              :       {
    3223           27 :         sarif_property_bag &props = tfl_obj.get_or_create_properties ();
    3224              : 
    3225              : #define PROPERTY_PREFIX "gcc/diagnostics/paths/event/"
    3226           27 :         props.set_graph (PROPERTY_PREFIX "state_graph",
    3227              :                          *this,
    3228              :                          /* Use RESULT for any related locations in the graph's
    3229              :                             nodes.
    3230              :                             It's not clear if this is correct; see:
    3231              :                             https://github.com/oasis-tcs/sarif-spec/issues/712
    3232              :                          */
    3233              :                          &result,
    3234           27 :                          *state_graph);
    3235              : #undef PROPERTY_PREFIX
    3236           27 :       }
    3237              : 
    3238              :   /* "location" property (SARIF v2.1.0 section 3.38.3).  */
    3239          168 :   tfl_obj.set<sarif_location>
    3240          336 :     ("location",
    3241          168 :      make_location_object (result, ev, diagnostic_artifact_role::traced_file));
    3242              : 
    3243              :   /* "kinds" property (SARIF v2.1.0 section 3.38.8).  */
    3244          168 :   paths::event::meaning m = ev.get_meaning ();
    3245          168 :   if (auto kinds_arr = maybe_make_kinds_array (m))
    3246          168 :     tfl_obj.set<json::array> ("kinds", std::move (kinds_arr));
    3247              : 
    3248              :   /* "nestingLevel" property (SARIF v2.1.0 section 3.38.10).  */
    3249          168 :   tfl_obj.set_integer ("nestingLevel", ev.get_stack_depth ());
    3250              : 
    3251              :   /* "executionOrder" property (SARIF v2.1.0 3.38.11).
    3252              :      Offset by 1 to match the human-readable values emitted by %@.  */
    3253          168 :   tfl_obj.set_integer ("executionOrder", event_execution_idx + 1);
    3254              : 
    3255              :   /* It might be nice to eventually implement the following for -fanalyzer:
    3256              :      - the "stack" property (SARIF v2.1.0 section 3.38.5)
    3257              :      - the "state" property (SARIF v2.1.0 section 3.38.9)
    3258              :      - the "importance" property (SARIF v2.1.0 section 3.38.13).  */
    3259          168 : }
    3260              : 
    3261              : /* If M has any known meaning, make a json array suitable for the "kinds"
    3262              :    property of a "threadFlowLocation" object (SARIF v2.1.0 section 3.38.8).
    3263              : 
    3264              :    Otherwise, return nullptr.  */
    3265              : 
    3266              : std::unique_ptr<json::array>
    3267          168 : sarif_builder::
    3268              : maybe_make_kinds_array (paths::event::meaning m) const
    3269              : {
    3270          168 :   using namespace paths;
    3271              : 
    3272          168 :   if (m.m_verb == event::verb::unknown
    3273           22 :       && m.m_noun == event::noun::unknown
    3274           22 :       && m.m_property == event::property::unknown)
    3275           22 :     return nullptr;
    3276              : 
    3277          146 :   auto kinds_arr = std::make_unique<json::array> ();
    3278          292 :   if (const char *verb_str
    3279          146 :         = event::meaning::maybe_get_verb_str (m.m_verb))
    3280          146 :     kinds_arr->append_string (verb_str);
    3281          292 :   if (const char *noun_str
    3282          146 :         = event::meaning::maybe_get_noun_str (m.m_noun))
    3283           63 :     kinds_arr->append_string (noun_str);
    3284          292 :   if (const char *property_str
    3285          146 :         = event::meaning::maybe_get_property_str (m.m_property))
    3286           32 :     kinds_arr->append_string (property_str);
    3287          146 :   return kinds_arr;
    3288          146 : }
    3289              : 
    3290              : /* In "3.11.5 Messages with placeholders":
    3291              :    "Within both plain text and formatted message strings, the characters
    3292              :    "{" and "}" SHALL be represented by the character sequences
    3293              :    "{{" and "}}" respectively."  */
    3294              : 
    3295              : static std::string
    3296         2213 : escape_braces (const char *text)
    3297              : {
    3298         2213 :   std::string result;
    3299        73195 :   while (char ch = *text++)
    3300        70982 :     switch (ch)
    3301              :       {
    3302           36 :       case '{':
    3303           36 :       case '}':
    3304           36 :         result += ch;
    3305              :         /* Fall through.  */
    3306        70982 :       default:
    3307        70982 :         result += ch;
    3308        70982 :         break;
    3309        70982 :       }
    3310         2213 :   return result;
    3311              : }
    3312              : 
    3313              : static void
    3314         2213 : set_string_property_escaping_braces (json::object &obj,
    3315              :                                      const char *property_name,
    3316              :                                      const char *value)
    3317              : {
    3318         2213 :   std::string escaped (escape_braces (value));
    3319         4426 :   obj.set_string (property_name, escaped);
    3320         2213 : }
    3321              : 
    3322              : /* Make a "message" object (SARIF v2.1.0 section 3.11) for MSG.  */
    3323              : 
    3324              : std::unique_ptr<sarif_message>
    3325         2046 : sarif_builder::make_message_object (const char *msg) const
    3326              : {
    3327         2046 :   auto message_obj = std::make_unique<sarif_message> ();
    3328              : 
    3329              :   /* "text" property (SARIF v2.1.0 section 3.11.8).  */
    3330         2046 :   set_string_property_escaping_braces (*message_obj,
    3331              :                                        "text", msg);
    3332              : 
    3333         2046 :   return message_obj;
    3334              : }
    3335              : 
    3336              : /* Make a "message" object (SARIF v2.1.0 section 3.11) for D.
    3337              :    We emit the diagram as a code block within the Markdown part
    3338              :    of the message.  */
    3339              : 
    3340              : std::unique_ptr<sarif_message>
    3341            4 : sarif_builder::make_message_object_for_diagram (const diagram &d)
    3342              : {
    3343            4 :   auto message_obj = std::make_unique<sarif_message> ();
    3344              : 
    3345              :   /* "text" property (SARIF v2.1.0 section 3.11.8).  */
    3346            4 :   set_string_property_escaping_braces (*message_obj,
    3347              :                                        "text", d.get_alt_text ());
    3348              : 
    3349            4 :   pretty_printer *const pp = m_printer;
    3350            4 :   char *saved_prefix = pp_take_prefix (pp);
    3351            4 :   pp_set_prefix (pp, nullptr);
    3352              : 
    3353              :   /* "To produce a code block in Markdown, simply indent every line of
    3354              :      the block by at least 4 spaces or 1 tab."
    3355              :      Here we use 4 spaces.  */
    3356            4 :   d.get_canvas ().print_to_pp (pp, "    ");
    3357            4 :   pp_set_prefix (pp, saved_prefix);
    3358              : 
    3359              :   /* "markdown" property (SARIF v2.1.0 section 3.11.9).  */
    3360            4 :   set_string_property_escaping_braces (*message_obj,
    3361              :                                        "markdown", pp_formatted_text (pp));
    3362              : 
    3363            4 :   pp_clear_output_area (pp);
    3364              : 
    3365            4 :   return message_obj;
    3366              : }
    3367              : 
    3368              : /* Make a "multiformatMessageString object" (SARIF v2.1.0 section 3.12)
    3369              :    for MSG.  */
    3370              : 
    3371              : std::unique_ptr<sarif_multiformat_message_string>
    3372          159 : sarif_builder::make_multiformat_message_string (const char *msg) const
    3373              : {
    3374          159 :   auto message_obj = std::make_unique<sarif_multiformat_message_string> ();
    3375              : 
    3376              :   /* "text" property (SARIF v2.1.0 section 3.12.3).  */
    3377          159 :   set_string_property_escaping_braces (*message_obj,
    3378              :                                        "text", msg);
    3379              : 
    3380          159 :   return message_obj;
    3381              : }
    3382              : 
    3383              : /* Convert VERSION to a value for the "$schema" property
    3384              :    of a "sarifLog" object (SARIF v2.1.0 section 3.13.3).  */
    3385              : 
    3386              : static const char *
    3387          291 : sarif_version_to_url (enum sarif_version version)
    3388              : {
    3389          291 :   switch (version)
    3390              :     {
    3391            0 :     default:
    3392            0 :       gcc_unreachable ();
    3393              :     case sarif_version::v2_1_0:
    3394              :       return "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json";
    3395           90 :     case sarif_version::v2_2_prerelease_csd01:
    3396           90 :       return "https://docs.oasis-open.org/sarif/sarif/v2.2/csd01/schema/sarif.json";
    3397              :     }
    3398              : }
    3399              : 
    3400              : /* Convert VERSION to a value for the "version" property
    3401              :    of a "sarifLog" object (SARIF v2.1.0 section 3.13.2).  */
    3402              : 
    3403              : static const char *
    3404          291 : sarif_version_to_property (enum sarif_version version)
    3405              : {
    3406          291 :   switch (version)
    3407              :     {
    3408            0 :     default:
    3409            0 :       gcc_unreachable ();
    3410              :     case sarif_version::v2_1_0:
    3411              :       return "2.1.0";
    3412           90 :     case sarif_version::v2_2_prerelease_csd01:
    3413              :       /* The schema only accepts "2.2".  */
    3414           90 :       return "2.2";
    3415              :     }
    3416              : }
    3417              : 
    3418              : /* Make a top-level "sarifLog" object (SARIF v2.1.0 section 3.13).  */
    3419              : 
    3420              : std::unique_ptr<sarif_log>
    3421          283 : sarif_builder::
    3422              : make_top_level_object (std::unique_ptr<sarif_invocation> invocation_obj,
    3423              :                        std::unique_ptr<json::array> results)
    3424              : {
    3425          283 :   auto log_obj = std::make_unique<sarif_log> ();
    3426              : 
    3427              :   /* "$schema" property (SARIF v2.1.0 section 3.13.3) .  */
    3428          283 :   log_obj->set_string ("$schema", sarif_version_to_url (get_version ()));
    3429              : 
    3430              :   /* "version" property (SARIF v2.1.0 section 3.13.2).  */
    3431          283 :   log_obj->set_string ("version", sarif_version_to_property (get_version ()));
    3432              : 
    3433              :   /* "runs" property (SARIF v2.1.0 section 3.13.4).  */
    3434          283 :   auto run_arr = std::make_unique<json::array> ();
    3435          283 :   auto run_obj = make_run_object (std::move (invocation_obj),
    3436          283 :                                   std::move (results));
    3437          283 :   run_arr->append<sarif_run> (std::move (run_obj));
    3438          283 :   log_obj->set<json::array> ("runs", std::move (run_arr));
    3439              : 
    3440          566 :   return log_obj;
    3441          283 : }
    3442              : 
    3443              : /* Make a "run" object (SARIF v2.1.0 section 3.14).  */
    3444              : 
    3445              : std::unique_ptr<sarif_run>
    3446          283 : sarif_builder::
    3447              : make_run_object (std::unique_ptr<sarif_invocation> invocation_obj,
    3448              :                  std::unique_ptr<json::array> results)
    3449              : {
    3450          283 :   auto run_obj = std::make_unique<sarif_run> ();
    3451              : 
    3452              :   /* "tool" property (SARIF v2.1.0 section 3.14.6).  */
    3453          283 :   run_obj->set<sarif_tool> ("tool", make_tool_object ());
    3454              : 
    3455              :   /* "taxonomies" property (SARIF v2.1.0 section 3.14.8).  */
    3456          283 :   if (auto taxonomies_arr = maybe_make_taxonomies_array ())
    3457          283 :     run_obj->set<json::array> ("taxonomies", std::move (taxonomies_arr));
    3458              : 
    3459              :   /* "invocations" property (SARIF v2.1.0 section 3.14.11).  */
    3460          283 :   {
    3461          283 :     auto invocations_arr = std::make_unique<json::array> ();
    3462          283 :     invocations_arr->append (std::move (invocation_obj));
    3463          283 :     run_obj->set<json::array> ("invocations", std::move (invocations_arr));
    3464          283 :   }
    3465              : 
    3466              :   /* "originalUriBaseIds (SARIF v2.1.0 section 3.14.14).  */
    3467          283 :   if (m_seen_any_relative_paths)
    3468              :     {
    3469           44 :       auto orig_uri_base_ids = std::make_unique<json::object> ();
    3470           44 :       orig_uri_base_ids->set<sarif_artifact_location>
    3471           44 :         (PWD_PROPERTY_NAME, make_artifact_location_object_for_pwd ());
    3472           44 :       run_obj->set<json::object> ("originalUriBaseIds",
    3473              :                                   std::move (orig_uri_base_ids));
    3474           44 :     }
    3475              : 
    3476              :   /* "artifacts" property (SARIF v2.1.0 section 3.14.15).  */
    3477          283 :   auto artifacts_arr = std::make_unique<json::array> ();
    3478          867 :   for (auto iter : m_filename_to_artifact_map)
    3479              :     {
    3480          301 :       sarif_artifact *artifact_obj = iter.second;
    3481          301 :       if (artifact_obj->embed_contents_p ())
    3482          242 :         artifact_obj->populate_contents (*this);
    3483          301 :       artifact_obj->populate_roles ();
    3484          301 :       artifacts_arr->append (artifact_obj);
    3485              :     }
    3486          283 :   run_obj->set<json::array> ("artifacts", std::move (artifacts_arr));
    3487          283 :   m_filename_to_artifact_map.empty ();
    3488              : 
    3489              :   /* "results" property (SARIF v2.1.0 section 3.14.23).  */
    3490          283 :   run_obj->set<json::array> ("results", std::move (results));
    3491              : 
    3492              :   /* "logicalLocations" property (SARIF v2.1.0 3.14.17). */
    3493          283 :   if (m_cached_logical_locs->size () > 0)
    3494              :     {
    3495           64 :       m_cached_logical_locs->add_explicit_index_values ();
    3496           64 :       run_obj->set<json::array> ("logicalLocations",
    3497           64 :                                  std::move (m_cached_logical_locs));
    3498              :     }
    3499              : 
    3500              :   // "graphs" property (SARIF v2.1.0 3.14.20)
    3501          283 :   if (m_run_graphs->size () > 0)
    3502            3 :     run_obj->set<json::array> ("graphs",
    3503            3 :                                std::move (m_run_graphs));
    3504              : 
    3505          566 :   return run_obj;
    3506          283 : }
    3507              : 
    3508              : /* Make a "tool" object (SARIF v2.1.0 section 3.18).  */
    3509              : 
    3510              : std::unique_ptr<sarif_tool>
    3511          283 : sarif_builder::make_tool_object ()
    3512              : {
    3513          283 :   auto tool_obj = std::make_unique<sarif_tool> ();
    3514              : 
    3515              :   /* "driver" property (SARIF v2.1.0 section 3.18.2).  */
    3516          283 :   tool_obj->set<sarif_tool_component> ("driver",
    3517          283 :                                        make_driver_tool_component_object ());
    3518              : 
    3519              :   /* Report plugins via the "extensions" property
    3520              :      (SARIF v2.1.0 section 3.18.3).  */
    3521          283 :   if (auto client_data_hooks = m_context.get_client_data_hooks ())
    3522          230 :     if (const client_version_info *vinfo
    3523          115 :           = client_data_hooks->get_any_version_info ())
    3524              :       {
    3525          115 :         class my_plugin_visitor : public client_version_info :: plugin_visitor
    3526              :         {
    3527              :         public:
    3528              :           void
    3529           11 :           on_plugin (const client_plugin_info &p) final override
    3530              :           {
    3531              :             /* Create a "toolComponent" object (SARIF v2.1.0 section 3.19)
    3532              :                for the plugin.  */
    3533           11 :             auto plugin_obj = std::make_unique<sarif_tool_component> ();
    3534              : 
    3535              :             /* "name" property (SARIF v2.1.0 section 3.19.8).  */
    3536           11 :             if (const char *short_name = p.get_short_name ())
    3537           11 :               plugin_obj->set_string ("name", short_name);
    3538              : 
    3539              :             /* "fullName" property (SARIF v2.1.0 section 3.19.9).  */
    3540           11 :             if (const char *full_name = p.get_full_name ())
    3541           11 :               plugin_obj->set_string ("fullName", full_name);
    3542              : 
    3543              :             /* "version" property (SARIF v2.1.0 section 3.19.13).  */
    3544           11 :             if (const char *version = p.get_version ())
    3545            0 :               plugin_obj->set_string ("version", version);
    3546              : 
    3547           11 :             m_plugin_objs.push_back (std::move (plugin_obj));
    3548           11 :           }
    3549              :           std::vector<std::unique_ptr<sarif_tool_component>> m_plugin_objs;
    3550              :         };
    3551          115 :         my_plugin_visitor v;
    3552          115 :         vinfo->for_each_plugin (v);
    3553          115 :         if (v.m_plugin_objs.size () > 0)
    3554              :           {
    3555           11 :             auto extensions_arr = std::make_unique<json::array> ();
    3556           22 :             for (auto &iter : v.m_plugin_objs)
    3557           11 :               extensions_arr->append<sarif_tool_component> (std::move (iter));
    3558           11 :             tool_obj->set<json::array> ("extensions",
    3559              :                                         std::move (extensions_arr));
    3560           11 :           }
    3561          115 :       }
    3562              : 
    3563              :   /* Perhaps we could also show GMP, MPFR, MPC, isl versions as other
    3564              :      "extensions" (see toplev.cc: print_version).  */
    3565              : 
    3566          283 :   return tool_obj;
    3567              : }
    3568              : 
    3569              : /* Make a "toolComponent" object (SARIF v2.1.0 section 3.19) for what SARIF
    3570              :    calls the "driver" (see SARIF v2.1.0 section 3.18.1).  */
    3571              : 
    3572              : std::unique_ptr<sarif_tool_component>
    3573          283 : sarif_builder::make_driver_tool_component_object ()
    3574              : {
    3575          283 :   auto driver_obj = std::make_unique<sarif_tool_component> ();
    3576              : 
    3577          283 :   if (auto client_data_hooks = m_context.get_client_data_hooks ())
    3578          230 :     if (const client_version_info *vinfo
    3579          115 :           = client_data_hooks->get_any_version_info ())
    3580              :       {
    3581              :         /* "name" property (SARIF v2.1.0 section 3.19.8).  */
    3582          115 :         if (const char *name = vinfo->get_tool_name ())
    3583          115 :           driver_obj->set_string ("name", name);
    3584              : 
    3585              :         /* "fullName" property (SARIF v2.1.0 section 3.19.9).  */
    3586          115 :         if (char *full_name = vinfo->maybe_make_full_name ())
    3587              :           {
    3588          115 :             driver_obj->set_string ("fullName", full_name);
    3589          115 :             free (full_name);
    3590              :           }
    3591              : 
    3592              :         /* "version" property (SARIF v2.1.0 section 3.19.13).  */
    3593          115 :         if (const char *version = vinfo->get_version_string ())
    3594          115 :           driver_obj->set_string ("version", version);
    3595              : 
    3596              :         /* "informationUri" property (SARIF v2.1.0 section 3.19.17).  */
    3597          115 :         if (char *version_url =  vinfo->maybe_make_version_url ())
    3598              :           {
    3599          115 :             driver_obj->set_string ("informationUri", version_url);
    3600          115 :             free (version_url);
    3601              :           }
    3602              :       }
    3603              : 
    3604              :   /* "rules" property (SARIF v2.1.0 section 3.19.23).  */
    3605          283 :   driver_obj->set<json::array> ("rules", std::move (m_rules_arr));
    3606              : 
    3607          283 :   return driver_obj;
    3608              : }
    3609              : 
    3610              : /* If we've seen any CWE IDs, make an array for the "taxonomies" property
    3611              :    (SARIF v2.1.0 section 3.14.8) of a run object, containing a single
    3612              :    "toolComponent" (3.19) as per 3.19.3, representing the CWE.
    3613              : 
    3614              :    Otherwise return nullptr.  */
    3615              : 
    3616              : std::unique_ptr<json::array>
    3617          283 : sarif_builder::maybe_make_taxonomies_array () const
    3618              : {
    3619          283 :   auto cwe_obj = maybe_make_cwe_taxonomy_object ();
    3620          283 :   if (!cwe_obj)
    3621          260 :     return nullptr;
    3622              : 
    3623              :   /* "taxonomies" property (SARIF v2.1.0 section 3.14.8).  */
    3624           23 :   auto taxonomies_arr = std::make_unique<json::array> ();
    3625           23 :   taxonomies_arr->append<sarif_tool_component> (std::move (cwe_obj));
    3626           23 :   return taxonomies_arr;
    3627          283 : }
    3628              : 
    3629              : /* If we've seen any CWE IDs, make a "toolComponent" object
    3630              :    (SARIF v2.1.0 section 3.19) representing the CWE taxonomy, as per 3.19.3.
    3631              :    Populate the "taxa" property with all of the CWE IDs in m_cwe_id_set.
    3632              : 
    3633              :    Otherwise return nullptr.  */
    3634              : 
    3635              : std::unique_ptr<sarif_tool_component>
    3636          283 : sarif_builder::maybe_make_cwe_taxonomy_object () const
    3637              : {
    3638          283 :   if (m_cwe_id_set.is_empty ())
    3639          260 :     return nullptr;
    3640              : 
    3641           23 :   auto taxonomy_obj = std::make_unique<sarif_tool_component> ();
    3642              : 
    3643              :   /* "name" property (SARIF v2.1.0 section 3.19.8).  */
    3644           23 :   taxonomy_obj->set_string ("name", "CWE");
    3645              : 
    3646              :   /* "version" property (SARIF v2.1.0 section 3.19.13).  */
    3647           23 :   taxonomy_obj->set_string ("version", "4.7");
    3648              : 
    3649              :   /* "organization" property (SARIF v2.1.0 section 3.19.18).  */
    3650           23 :   taxonomy_obj->set_string ("organization", "MITRE");
    3651              : 
    3652              :   /* "shortDescription" property (SARIF v2.1.0 section 3.19.19).  */
    3653           23 :   taxonomy_obj->set<sarif_multiformat_message_string>
    3654           23 :     ("shortDescription",
    3655           23 :      make_multiformat_message_string ("The MITRE"
    3656              :                                       " Common Weakness Enumeration"));
    3657              : 
    3658              :   /* "taxa" property (SARIF v2.1.0 3.section 3.19.25).  */
    3659           23 :   auto taxa_arr = std::make_unique<json::array> ();
    3660           46 :   for (auto cwe_id : m_cwe_id_set)
    3661           23 :     taxa_arr->append<sarif_reporting_descriptor>
    3662           23 :       (make_reporting_descriptor_object_for_cwe_id (cwe_id));
    3663           23 :   taxonomy_obj->set<json::array> ("taxa", std::move (taxa_arr));
    3664              : 
    3665           23 :   return taxonomy_obj;
    3666           23 : }
    3667              : 
    3668              : /* Ensure that we have an "artifact" object (SARIF v2.1.0 section 3.24)
    3669              :    for FILENAME, adding it to m_filename_to_artifact_map if not already
    3670              :    found, and adding ROLE to it.
    3671              :    If EMBED_CONTENTS is true, then flag that we will attempt to embed the
    3672              :    contents of this artifact when writing it out.  */
    3673              : 
    3674              : sarif_artifact &
    3675         1175 : sarif_builder::get_or_create_artifact (const char *filename,
    3676              :                                        enum diagnostic_artifact_role role,
    3677              :                                        bool embed_contents)
    3678              : {
    3679         1175 :   if (auto *slot = m_filename_to_artifact_map.get (filename))
    3680              :     {
    3681          738 :       (*slot)->add_role (role, embed_contents);
    3682          738 :       return **slot;
    3683              :     }
    3684              : 
    3685          437 :   sarif_artifact *artifact_obj = new sarif_artifact (filename);
    3686          437 :   artifact_obj->add_role (role, embed_contents);
    3687          437 :   m_filename_to_artifact_map.put (filename, artifact_obj);
    3688              : 
    3689              :   /* "location" property (SARIF v2.1.0 section 3.24.2).  */
    3690          437 :   artifact_obj->set<sarif_artifact_location>
    3691          437 :     ("location", make_artifact_location_object (filename));
    3692              : 
    3693              :   /* "sourceLanguage" property (SARIF v2.1.0 section 3.24.10).  */
    3694          437 :   switch (role)
    3695              :     {
    3696            0 :     default:
    3697            0 :       gcc_unreachable ();
    3698          437 :     case diagnostic_artifact_role::analysis_target:
    3699          437 :     case diagnostic_artifact_role::result_file:
    3700          437 :     case diagnostic_artifact_role::scanned_file:
    3701          437 :     case diagnostic_artifact_role::traced_file:
    3702              :       /* Assume that these are in the source language.  */
    3703          437 :       if (auto client_data_hooks = m_context.get_client_data_hooks ())
    3704          266 :         if (const char *source_lang
    3705          133 :             = client_data_hooks->maybe_get_sarif_source_language (filename))
    3706          133 :           artifact_obj->set_string ("sourceLanguage", source_lang);
    3707              :       break;
    3708              : 
    3709              :     case diagnostic_artifact_role::debug_output_file:
    3710              :       /* Assume that these are not in the source language.  */
    3711              :       break;
    3712              :     }
    3713              : 
    3714              :   return *artifact_obj;
    3715              : }
    3716              : 
    3717              : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the
    3718              :    full contents of FILENAME.  */
    3719              : 
    3720              : std::unique_ptr<sarif_artifact_content>
    3721          242 : sarif_builder::maybe_make_artifact_content_object (const char *filename) const
    3722              : {
    3723              :   /* Let input.cc handle any charset conversion.  */
    3724          242 :   char_span utf8_content
    3725          242 :     = m_context.get_file_cache ().get_source_file_content (filename);
    3726          242 :   if (!utf8_content)
    3727            4 :     return nullptr;
    3728              : 
    3729              :   /* Don't add it if it's not valid UTF-8.  */
    3730          238 :   if (!cpp_valid_utf8_p(utf8_content.get_buffer (), utf8_content.length ()))
    3731           12 :     return nullptr;
    3732              : 
    3733          226 :   auto artifact_content_obj = std::make_unique<sarif_artifact_content> ();
    3734          226 :   artifact_content_obj->set<json::string>
    3735          452 :     ("text",
    3736          226 :      std::make_unique <json::string> (utf8_content.get_buffer (),
    3737          226 :                                    utf8_content.length ()));
    3738          226 :   return artifact_content_obj;
    3739          226 : }
    3740              : 
    3741              : /* Attempt to read the given range of lines from FILENAME; return
    3742              :    a freshly-allocated 0-terminated buffer containing them, or nullptr.  */
    3743              : 
    3744              : char *
    3745          880 : sarif_builder::get_source_lines (const char *filename,
    3746              :                                  int start_line,
    3747              :                                  int end_line) const
    3748              : {
    3749          880 :   auto_vec<char> result;
    3750              : 
    3751         1760 :   for (int line = start_line; line <= end_line; line++)
    3752              :     {
    3753          880 :       char_span line_content
    3754          880 :         = m_context.get_file_cache ().get_source_line (filename, line);
    3755          880 :       if (!line_content.get_buffer ())
    3756            0 :         return nullptr;
    3757          880 :       result.reserve (line_content.length () + 1);
    3758        25901 :       for (size_t i = 0; i < line_content.length (); i++)
    3759        24141 :         result.quick_push (line_content[i]);
    3760          880 :       result.quick_push ('\n');
    3761              :     }
    3762          880 :   result.safe_push ('\0');
    3763              : 
    3764         1760 :   return xstrdup (result.address ());
    3765          880 : }
    3766              : 
    3767              : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for the given
    3768              :    run of lines within FILENAME (including the endpoints).
    3769              :    If R is non-NULL, use it to potentially set the "rendered"
    3770              :    property (3.3.4).  */
    3771              : 
    3772              : std::unique_ptr<sarif_artifact_content>
    3773          880 : sarif_builder::
    3774              : maybe_make_artifact_content_object (const char *filename,
    3775              :                                     int start_line,
    3776              :                                     int end_line,
    3777              :                                     const content_renderer *r) const
    3778              : {
    3779          880 :   char *text_utf8 = get_source_lines (filename, start_line, end_line);
    3780              : 
    3781          880 :   if (!text_utf8)
    3782            0 :     return nullptr;
    3783              : 
    3784              :   /* Don't add it if it's not valid UTF-8.  */
    3785          880 :   if (!cpp_valid_utf8_p(text_utf8, strlen(text_utf8)))
    3786              :     {
    3787          172 :       free (text_utf8);
    3788          172 :       return nullptr;
    3789              :     }
    3790              : 
    3791          708 :   auto artifact_content_obj = std::make_unique<sarif_artifact_content> ();
    3792          708 :   artifact_content_obj->set_string ("text", text_utf8);
    3793          708 :   free (text_utf8);
    3794              : 
    3795              :   /* 3.3.4 "rendered" property.  */
    3796          708 :   if (r)
    3797          136 :     if (std::unique_ptr<sarif_multiformat_message_string> rendered
    3798          136 :           = r->render (*this))
    3799          136 :       artifact_content_obj->set ("rendered", std::move (rendered));
    3800              : 
    3801          708 :   return artifact_content_obj;
    3802          708 : }
    3803              : 
    3804              : /* Attempt to generate a "message" object describing a fix-it hint,
    3805              :    or null if there was a problem.  */
    3806              : 
    3807              : std::unique_ptr<sarif_message>
    3808            9 : sarif_builder::
    3809              : make_message_describing_fix_it_hint (const fixit_hint &hint) const
    3810              : {
    3811            9 :   pretty_printer pp;
    3812            9 :   if (hint.insertion_p ())
    3813            4 :     pp_printf (&pp, G_("Insert %qs"), hint.get_string ());
    3814              :   else
    3815              :     {
    3816              :       /* Try to get prior content.  */
    3817            5 :       expanded_location start = expand_location (hint.get_start_loc ());
    3818            5 :       expanded_location next_loc = expand_location (hint.get_next_loc ());
    3819            5 :       if (start.file != next_loc.file)
    3820            0 :         return nullptr;
    3821            5 :       if (start.line != next_loc.line)
    3822            0 :         return nullptr;
    3823            5 :       if (start.column == 0)
    3824            0 :         return nullptr;
    3825            5 :       if (next_loc.column == 0)
    3826            0 :         return nullptr;
    3827              : 
    3828            5 :       const int start_offset = start.column - 1;
    3829            5 :       const int next_offset = next_loc.column - 1;
    3830            5 :       if (next_offset <= start_offset)
    3831            0 :         return nullptr;
    3832              : 
    3833            5 :       size_t victim_len = next_offset - start_offset;
    3834              : 
    3835            5 :       char_span existing_line = get_context ()
    3836            5 :         .get_file_cache ()
    3837            5 :         .get_source_line (start.file, start.line);
    3838            5 :       if (!existing_line)
    3839            0 :         return nullptr;
    3840              : 
    3841            5 :       label_text existing_text
    3842            5 :         = label_text::take (existing_line.subspan (start_offset,
    3843            5 :                                                    victim_len).xstrdup ());
    3844              : 
    3845            5 :       if (hint.deletion_p ())
    3846              :         {
    3847              :           // Removal
    3848            1 :           pp_printf (&pp, G_("Delete %qs"),
    3849              :                      existing_text.get ());
    3850              :         }
    3851              :       else
    3852              :         {
    3853              :           // Replacement
    3854            4 :           gcc_assert (hint.replacement_p ());
    3855            4 :           pp_printf (&pp, G_("Replace %qs with %qs"),
    3856              :                      existing_text.get (),
    3857              :                      hint.get_string ());
    3858              :         }
    3859            5 :     }
    3860            9 :   return make_message_object (pp_formatted_text (&pp));
    3861            9 : }
    3862              : 
    3863              : /* Make a "fix" object (SARIF v2.1.0 section 3.55) for RICHLOC.  */
    3864              : 
    3865              : std::unique_ptr<sarif_fix>
    3866            9 : sarif_builder::make_fix_object (const rich_location &richloc)
    3867              : {
    3868            9 :   auto fix_obj = std::make_unique<sarif_fix> ();
    3869              : 
    3870              :   /* "artifactChanges" property (SARIF v2.1.0 section 3.55.3).  */
    3871              :   /* We assume that all fix-it hints in RICHLOC affect the same file.  */
    3872            9 :   auto artifact_change_arr = std::make_unique<json::array> ();
    3873            9 :   artifact_change_arr->append<sarif_artifact_change>
    3874            9 :     (make_artifact_change_object (richloc));
    3875            9 :   fix_obj->set<json::array> ("artifactChanges",
    3876              :                              std::move (artifact_change_arr));
    3877              : 
    3878              :   // 3.55.2 "description" property
    3879              :   /* Attempt to generate a description.  We can only do this
    3880              :      if there was a single hint.  */
    3881            9 :   if (richloc.get_num_fixit_hints () == 1)
    3882              :     {
    3883            9 :       const fixit_hint *hint = richloc.get_fixit_hint (0);
    3884            9 :       gcc_assert (hint);
    3885            9 :       if (auto desc_msg = make_message_describing_fix_it_hint (*hint))
    3886            9 :         fix_obj->set<sarif_message> ("description",
    3887            9 :                                      std::move (desc_msg));
    3888              :     }
    3889              : 
    3890           18 :   return fix_obj;
    3891            9 : }
    3892              : 
    3893              : /* Make an "artifactChange" object (SARIF v2.1.0 section 3.56) for RICHLOC.  */
    3894              : 
    3895              : std::unique_ptr<sarif_artifact_change>
    3896            9 : sarif_builder::make_artifact_change_object (const rich_location &richloc)
    3897              : {
    3898            9 :   auto artifact_change_obj = std::make_unique<sarif_artifact_change> ();
    3899              : 
    3900              :   /* "artifactLocation" property (SARIF v2.1.0 section 3.56.2).  */
    3901            9 :   artifact_change_obj->set<sarif_artifact_location>
    3902            9 :     ("artifactLocation",
    3903            9 :      make_artifact_location_object (richloc.get_loc ()));
    3904              : 
    3905              :   /* "replacements" property (SARIF v2.1.0 section 3.56.3).  */
    3906            9 :   auto replacement_arr = std::make_unique<json::array> ();
    3907           27 :   for (unsigned int i = 0; i < richloc.get_num_fixit_hints (); i++)
    3908              :     {
    3909            9 :       const fixit_hint *hint = richloc.get_fixit_hint (i);
    3910            9 :       replacement_arr->append<sarif_replacement>
    3911            9 :         (make_replacement_object (*hint));
    3912              :     }
    3913            9 :   artifact_change_obj->set<json::array> ("replacements",
    3914              :                                          std::move (replacement_arr));
    3915              : 
    3916           18 :   return artifact_change_obj;
    3917            9 : }
    3918              : 
    3919              : /* Make a "replacement" object (SARIF v2.1.0 section 3.57) for HINT.  */
    3920              : 
    3921              : std::unique_ptr<sarif_replacement>
    3922            9 : sarif_builder::make_replacement_object (const fixit_hint &hint) const
    3923              : {
    3924            9 :   auto replacement_obj = std::make_unique<sarif_replacement> ();
    3925              : 
    3926              :   /* "deletedRegion" property (SARIF v2.1.0 section 3.57.3).  */
    3927            9 :   replacement_obj->set<sarif_region> ("deletedRegion",
    3928            9 :                                       make_region_object_for_hint (hint));
    3929              : 
    3930              :   /* "insertedContent" property (SARIF v2.1.0 section 3.57.4).  */
    3931            9 :   replacement_obj->set<sarif_artifact_content>
    3932            9 :     ("insertedContent",
    3933            9 :      make_artifact_content_object (hint.get_string ()));
    3934              : 
    3935            9 :   return replacement_obj;
    3936              : }
    3937              : 
    3938              : /* Make an "artifactContent" object (SARIF v2.1.0 section 3.3) for TEXT.  */
    3939              : 
    3940              : std::unique_ptr<sarif_artifact_content>
    3941            9 : sarif_builder::make_artifact_content_object (const char *text) const
    3942              : {
    3943            9 :   auto content_obj = std::make_unique<sarif_artifact_content> ();
    3944              : 
    3945              :   /* "text" property (SARIF v2.1.0 section 3.3.2).  */
    3946            9 :   content_obj->set_string ("text", text);
    3947              : 
    3948            9 :   return content_obj;
    3949              : }
    3950              : 
    3951              : /* class sarif_sink_buffer : public per_sink_buffer.  */
    3952              : 
    3953              : void
    3954            0 : sarif_sink_buffer::dump (FILE *out, int indent) const
    3955              : {
    3956            0 :   dumping::emit_heading (out, indent, "sarif_sink_buffer");
    3957            0 :   int idx = 0;
    3958            0 :   for (auto &result : m_results)
    3959              :     {
    3960            0 :       dumping::emit_indent (out, indent + 2);
    3961            0 :       fprintf (out, "result[%i]:\n", idx);
    3962            0 :       result->dump (out, true);
    3963            0 :       fprintf (out, "\n");
    3964            0 :       ++idx;
    3965              :     }
    3966            0 : }
    3967              : 
    3968              : bool
    3969           61 : sarif_sink_buffer::empty_p () const
    3970              : {
    3971           61 :   return m_results.empty ();
    3972              : }
    3973              : 
    3974              : void
    3975            0 : sarif_sink_buffer::move_to (per_sink_buffer &base)
    3976              : {
    3977            0 :   sarif_sink_buffer &dest
    3978              :     = static_cast<sarif_sink_buffer &> (base);
    3979            0 :   for (auto &&result : m_results)
    3980            0 :     dest.m_results.push_back (std::move (result));
    3981            0 :   m_results.clear ();
    3982            0 : }
    3983              : 
    3984              : void
    3985           22 : sarif_sink_buffer::clear ()
    3986              : {
    3987           22 :   m_results.clear ();
    3988           22 : }
    3989              : 
    3990              : void
    3991            8 : sarif_sink_buffer::flush ()
    3992              : {
    3993           16 :   for (auto &&result : m_results)
    3994              :     {
    3995            8 :       result->process_worklist (m_builder);
    3996            8 :       m_builder.m_results_array->append<sarif_result> (std::move (result));
    3997              :     }
    3998            8 :   m_results.clear ();
    3999            8 : }
    4000              : 
    4001              : class sarif_sink : public sink
    4002              : {
    4003              : public:
    4004          291 :   ~sarif_sink ()
    4005          291 :   {
    4006              :     /* Any sarifResult objects should have been handled by now.
    4007              :        If not, then something's gone wrong with diagnostic
    4008              :        groupings.  */
    4009          291 :     std::unique_ptr<sarif_result> pending_result
    4010          291 :       = m_builder.take_current_result ();
    4011          291 :     gcc_assert (!pending_result);
    4012          291 :   }
    4013              : 
    4014            0 :   void dump (FILE *out, int indent) const override
    4015              :   {
    4016            0 :     sink::dump (out, indent);
    4017            0 :     dumping::emit_heading (out, indent, "sarif_builder");
    4018            0 :     m_builder.dump (out, indent + 2);
    4019            0 :   }
    4020              : 
    4021              :   void
    4022          291 :   set_main_input_filename (const char *name) final override
    4023              :   {
    4024          115 :     m_builder.set_main_input_filename (name);
    4025          115 :   }
    4026              : 
    4027              :   std::unique_ptr<per_sink_buffer>
    4028           17 :   make_per_sink_buffer () final override
    4029              :   {
    4030           17 :     return std::make_unique<sarif_sink_buffer> (m_builder);
    4031              :   }
    4032           34 :   void set_buffer (per_sink_buffer *base_buffer) final override
    4033              :   {
    4034           34 :     sarif_sink_buffer *buffer
    4035              :       = static_cast<sarif_sink_buffer *> (base_buffer);
    4036           34 :     m_buffer = buffer;
    4037           34 :   }
    4038              : 
    4039            0 :   bool follows_reference_printer_p () const final override
    4040              :   {
    4041            0 :     return false;
    4042              :   }
    4043              : 
    4044          291 :   void update_printer () final override
    4045              :   {
    4046          291 :     m_printer = m_context.clone_printer ();
    4047              : 
    4048              :     /* Don't colorize the text.  */
    4049          291 :     pp_show_color (m_printer.get ()) = false;
    4050              : 
    4051              :     /* No textual URLs.  */
    4052          291 :     m_printer->set_url_format (URL_FORMAT_NONE);
    4053              : 
    4054              :     /* Use builder's token printer.  */
    4055          291 :     get_printer ()->set_token_printer (&m_builder.get_token_printer ());
    4056              : 
    4057              :     /* Update the builder to use the new printer.  */
    4058          291 :     m_builder.set_printer (*get_printer ());
    4059          291 :   }
    4060              : 
    4061          484 :   void on_begin_group () final override
    4062              :   {
    4063              :     /* No-op,  */
    4064          484 :   }
    4065          484 :   void on_end_group () override
    4066              :   {
    4067          484 :     m_builder.end_group ();
    4068          484 :   }
    4069              :   void
    4070          607 :   on_report_diagnostic (const diagnostic_info &diagnostic,
    4071              :                         enum kind orig_diag_kind) final override
    4072              :   {
    4073          607 :     DIAGNOSTICS_LOG_SCOPE_PRINTF0
    4074              :       (get_logger (),
    4075          607 :        "diagnostics::sarif_sink::on_report_diagnostic");
    4076          607 :     m_builder.on_report_diagnostic (diagnostic, orig_diag_kind, m_buffer);
    4077          607 :   }
    4078            4 :   void on_diagram (const diagram &d) final override
    4079              :   {
    4080            4 :     m_builder.emit_diagram (d);
    4081            4 :   }
    4082          579 :   void after_diagnostic (const diagnostic_info &) final override
    4083              :   {
    4084              :     /* No-op.  */
    4085          579 :   }
    4086              : 
    4087              :   void
    4088            3 :   report_global_digraph (const lazily_created<digraphs::digraph> &ldg)
    4089              :     final override
    4090              :   {
    4091            3 :     m_builder.report_global_digraph (ldg);
    4092            3 :   }
    4093              : 
    4094              :   void
    4095           45 :   report_digraph_for_logical_location (const lazily_created<digraphs::digraph> &ldg,
    4096              :                                        logical_locations::key key) final override
    4097              :   {
    4098           45 :     m_builder.report_digraph_for_logical_location (ldg, key);
    4099           45 :   }
    4100              : 
    4101              :   sarif_builder &get_builder () { return m_builder; }
    4102              : 
    4103           96 :   size_t num_results () const { return m_builder.num_results (); }
    4104           16 :   sarif_result &get_result (size_t idx) { return m_builder.get_result (idx); }
    4105              : 
    4106              : protected:
    4107          291 :   sarif_sink (context &dc,
    4108              :               const line_maps *line_maps,
    4109              :               std::unique_ptr<sarif_serialization_format> serialization_format,
    4110              :               const sarif_generation_options &sarif_gen_opts)
    4111              :   : sink (dc),
    4112          291 :     m_builder (dc, *get_printer (), line_maps,
    4113              :                std::move (serialization_format), sarif_gen_opts),
    4114          291 :     m_buffer (nullptr)
    4115          291 :   {}
    4116              : 
    4117              :   sarif_builder m_builder;
    4118              :   sarif_sink_buffer *m_buffer;
    4119              : };
    4120              : 
    4121              : class sarif_stream_sink : public sarif_sink
    4122              : {
    4123              : public:
    4124            0 :   sarif_stream_sink (context &dc,
    4125              :                      const line_maps *line_maps,
    4126              :                      std::unique_ptr<sarif_serialization_format> serialization_format,
    4127              :                      const sarif_generation_options &sarif_gen_opts,
    4128              :                      FILE *stream)
    4129              :   : sarif_sink (dc, line_maps,
    4130              :                 std::move (serialization_format), sarif_gen_opts),
    4131            0 :     m_stream (stream)
    4132              :   {
    4133            0 :   }
    4134            0 :   ~sarif_stream_sink ()
    4135            0 :   {
    4136            0 :     m_builder.flush_to_file (m_stream);
    4137            0 :   }
    4138            0 :   void dump_kind (FILE *out) const override
    4139              :   {
    4140            0 :     fprintf (out, "sarif_stream_sink");
    4141            0 :   }
    4142            0 :   bool machine_readable_stderr_p () const final override
    4143              :   {
    4144            0 :     return m_stream == stderr;
    4145              :   }
    4146              : private:
    4147              :   FILE *m_stream;
    4148              : };
    4149              : 
    4150              : class sarif_file_sink : public sarif_sink
    4151              : {
    4152              : public:
    4153          115 :   sarif_file_sink (context &dc,
    4154              :                    const line_maps *line_maps,
    4155              :                    std::unique_ptr<sarif_serialization_format> serialization_format,
    4156              :                    const sarif_generation_options &sarif_gen_opts,
    4157              :                    output_file output_file_)
    4158              :   : sarif_sink (dc, line_maps,
    4159              :                 std::move (serialization_format),
    4160              :                 sarif_gen_opts),
    4161          115 :     m_output_file (std::move (output_file_))
    4162              :   {
    4163          115 :     gcc_assert (m_output_file.get_open_file ());
    4164          115 :     gcc_assert (m_output_file.get_filename ());
    4165          115 :   }
    4166          230 :   ~sarif_file_sink ()
    4167          115 :   {
    4168          115 :     m_builder.flush_to_file (m_output_file.get_open_file ());
    4169          230 :   }
    4170            0 :   void dump_kind (FILE *out) const override
    4171              :   {
    4172            0 :     fprintf (out, "sarif_file_sink: %s",
    4173              :              m_output_file.get_filename ());
    4174            0 :   }
    4175           15 :   bool machine_readable_stderr_p () const final override
    4176              :   {
    4177           15 :     return false;
    4178              :   }
    4179              : 
    4180              : private:
    4181              :   output_file m_output_file;
    4182              : };
    4183              : 
    4184              : class unique_fd
    4185              : {
    4186              : public:
    4187              :   unique_fd () : m_ival (-1) {}
    4188            0 :   explicit unique_fd (int ival) : m_ival (ival) {}
    4189              :   unique_fd (const unique_fd &) = delete;
    4190            0 :   unique_fd (unique_fd &&other)
    4191            0 :   : m_ival (other.m_ival)
    4192              :   {
    4193            0 :     other.m_ival = -1;
    4194              :   }
    4195            0 :   ~unique_fd ()
    4196              :   {
    4197            0 :     if (m_ival != -1)
    4198            0 :       close (m_ival);
    4199            0 :   }
    4200              :   unique_fd &operator= (const unique_fd &other) = delete;
    4201              :   unique_fd &operator= (unique_fd &&other)
    4202              :   {
    4203              :     if (m_ival != -1)
    4204              :       close (m_ival);
    4205              :     m_ival = other.m_ival;
    4206              :     other.m_ival = -1;
    4207              :     return *this;
    4208              :   }
    4209              : 
    4210            0 :   operator int () const { return m_ival; }
    4211              : 
    4212              : private:
    4213              :   int m_ival;
    4214              : };
    4215              : 
    4216              : class sarif_socket_sink : public sarif_sink
    4217              : {
    4218              : public:
    4219            0 :   sarif_socket_sink (context &dc,
    4220              :                      const line_maps *line_maps,
    4221              :                      std::unique_ptr<sarif_serialization_format> serialization_format,
    4222              :                      const sarif_generation_options &sarif_gen_opts,
    4223              :                      unique_fd fd)
    4224              :   : sarif_sink (dc, line_maps,
    4225              :                 std::move (serialization_format),
    4226              :                 sarif_gen_opts),
    4227            0 :     m_fd (std::move (fd))
    4228              :   {
    4229            0 :   }
    4230            0 :   void dump_kind (FILE *out) const override
    4231              :   {
    4232            0 :     fprintf (out, "sarif_socket_sink: fd=%i", int (m_fd));
    4233            0 :   }
    4234            0 :   bool machine_readable_stderr_p () const final override
    4235              :   {
    4236            0 :     return false;
    4237              :   }
    4238              : 
    4239              :   /* Rather than appending it to the results array, instead
    4240              :      send it to the output socket as a JSON-RPC 2.0 notification.  */
    4241            0 :   void on_end_group () final override
    4242              :   {
    4243              :     // TODO: what about buffering?
    4244              : 
    4245            0 :     std::unique_ptr<sarif_result> result = m_builder.take_current_result ();
    4246            0 :     if (!result)
    4247            0 :       return;
    4248              : 
    4249            0 :     auto notification = std::make_unique<json::object> ();
    4250            0 :     notification->set_string ("jsonrpc", "2.0");
    4251            0 :     notification->set_string ("method", "OnSarifResult");
    4252            0 :     {
    4253            0 :       auto params = std::make_unique<json::object> ();
    4254            0 :       params->set ("result", std::move (result));
    4255            0 :       notification->set ("params", std::move (params));
    4256            0 :     }
    4257              : 
    4258            0 :     send_rpc_notification (*notification);
    4259            0 :   }
    4260              : 
    4261              : private:
    4262              :   void
    4263            0 :   send_rpc_notification (const json::object &notification)
    4264              :   {
    4265            0 :     DIAGNOSTICS_LOG_SCOPE_PRINTF0 (m_context.get_logger (),
    4266            0 :                                    "sarif_socket_sink::send_rpc_notification");
    4267            0 :     const bool formatted = false;
    4268            0 :     pretty_printer pp_content;
    4269            0 :     notification.print (&pp_content, formatted);
    4270            0 :     size_t content_length = strlen (pp_formatted_text (&pp_content));
    4271              : 
    4272            0 :     pretty_printer pp_header;
    4273              : #if __GNUC__ >= 10
    4274            0 : #  pragma GCC diagnostic push
    4275            0 : #  pragma GCC diagnostic ignored "-Wformat-diag"
    4276              : #endif
    4277            0 :     pp_printf (&pp_header, "Content-Length: %li\n\n", content_length);
    4278              : #if __GNUC__ >= 10
    4279            0 : #  pragma GCC diagnostic pop
    4280              : #endif
    4281            0 :     size_t header_length = strlen (pp_formatted_text (&pp_header));
    4282              : 
    4283            0 :     size_t output_size = header_length + content_length;
    4284            0 :     char *buf = (char *)xmalloc (output_size);
    4285            0 :     memcpy (buf, pp_formatted_text (&pp_header), header_length);
    4286            0 :     memcpy (buf + header_length,
    4287            0 :             pp_formatted_text (&pp_content),
    4288              :             content_length);
    4289              : 
    4290              :     /* TODO: should we attempt to handle partial writes here?  */
    4291            0 :     write (m_fd, buf, output_size);
    4292              : 
    4293            0 :     free (buf);
    4294            0 :   }
    4295              : 
    4296              :   unique_fd m_fd;
    4297              : };
    4298              : 
    4299              : /* Print the start of an embedded link to PP, as per 3.11.6.  */
    4300              : 
    4301              : static void
    4302           44 : sarif_begin_embedded_link (pretty_printer *pp)
    4303              : {
    4304            0 :   pp_character (pp, '[');
    4305            0 : }
    4306              : 
    4307              : /* Print the end of an embedded link to PP, as per 3.11.6.  */
    4308              : 
    4309              : static void
    4310           44 : sarif_end_embedded_link (pretty_printer *pp,
    4311              :                          const char *url)
    4312              : {
    4313           44 :   pp_string (pp, "](");
    4314              :   /* TODO: does the URI need escaping?
    4315              :      See https://github.com/oasis-tcs/sarif-spec/issues/657 */
    4316           44 :   pp_string (pp, url);
    4317           44 :   pp_character (pp, ')');
    4318           44 : }
    4319              : 
    4320              : /* class sarif_token_printer : public token_printer.  */
    4321              : 
    4322              : /* Implementation of pretty_printer::token_printer for SARIF output.
    4323              :    Emit URLs as per 3.11.6 ("Messages with embedded links").  */
    4324              : 
    4325              : void
    4326          803 : sarif_builder::sarif_token_printer::print_tokens (pretty_printer *pp,
    4327              :                                                   const pp_token_list &tokens)
    4328              : {
    4329              :   /* Convert to text, possibly with colorization, URLs, etc.  */
    4330          803 :   label_text current_url;
    4331         4285 :   for (auto iter = tokens.m_first; iter; iter = iter->m_next)
    4332         3482 :     switch (iter->m_kind)
    4333              :       {
    4334            0 :       default:
    4335            0 :         gcc_unreachable ();
    4336              : 
    4337         1926 :       case pp_token::kind::text:
    4338         1926 :         {
    4339         1926 :           const pp_token_text *sub = as_a <const pp_token_text *> (iter);
    4340         1926 :           const char * const str = sub->m_value.get ();
    4341         1926 :           if (current_url.get ())
    4342              :             {
    4343              :               /* Write iter->m_value, but escaping any
    4344              :                  escaped link characters as per 3.11.6.  */
    4345          296 :               for (const char *ptr = str; *ptr; ptr++)
    4346              :                 {
    4347          270 :                   const char ch = *ptr;
    4348          270 :                   switch (ch)
    4349              :                     {
    4350          230 :                     default:
    4351          230 :                       pp_character (pp, ch);
    4352          230 :                       break;
    4353           40 :                     case '\\':
    4354           40 :                     case '[':
    4355           40 :                     case ']':
    4356           40 :                       pp_character (pp, '\\');
    4357           40 :                       pp_character (pp, ch);
    4358           40 :                       break;
    4359              :                     }
    4360              :                 }
    4361              :             }
    4362              :           else
    4363              :             /* TODO: is other escaping needed? (e.g. of '[')
    4364              :                See https://github.com/oasis-tcs/sarif-spec/issues/658 */
    4365         1900 :             pp_string (pp, str);
    4366              :         }
    4367              :         break;
    4368              : 
    4369              :       case pp_token::kind::begin_color:
    4370              :       case pp_token::kind::end_color:
    4371              :         /* These are no-ops.  */
    4372              :         break;
    4373              : 
    4374          711 :       case pp_token::kind::begin_quote:
    4375          711 :         pp_begin_quote (pp, pp_show_color (pp));
    4376          711 :         break;
    4377          669 :       case pp_token::kind::end_quote:
    4378          669 :         pp_end_quote (pp, pp_show_color (pp));
    4379          669 :         break;
    4380              : 
    4381              :       /* Emit URLs as per 3.11.6 ("Messages with embedded links").  */
    4382           26 :       case pp_token::kind::begin_url:
    4383           26 :         {
    4384           26 :           pp_token_begin_url *sub = as_a <pp_token_begin_url *> (iter);
    4385           26 :           sarif_begin_embedded_link (pp);
    4386           26 :           current_url = std::move (sub->m_value);
    4387              :         }
    4388           26 :         break;
    4389           26 :       case pp_token::kind::end_url:
    4390           26 :         gcc_assert (current_url.get ());
    4391           26 :         sarif_end_embedded_link (pp, current_url.get ());
    4392           26 :         current_url = label_text::borrow (nullptr);
    4393           26 :         break;
    4394              : 
    4395          120 :       case pp_token::kind::event_id:
    4396          120 :         {
    4397          120 :           pp_token_event_id *sub = as_a <pp_token_event_id *> (iter);
    4398          120 :           gcc_assert (sub->m_event_id.known_p ());
    4399          120 :           const sarif_code_flow *code_flow
    4400          120 :             = m_builder.get_code_flow_for_event_ids ();
    4401          120 :           label_text url = make_sarif_url_for_event (code_flow,
    4402          120 :                                                      sub->m_event_id);
    4403          120 :           if (url.get ())
    4404           18 :             sarif_begin_embedded_link (pp);
    4405          120 :           pp_character (pp, '(');
    4406          120 :           pp_decimal_int (pp, sub->m_event_id.one_based ());
    4407          120 :           pp_character (pp, ')');
    4408          120 :           if (url.get ())
    4409           18 :             sarif_end_embedded_link (pp, url.get ());
    4410          120 :         }
    4411          120 :         break;
    4412              :       }
    4413          803 : }
    4414              : 
    4415              : /* Populate CONTEXT in preparation for SARIF output (either to stderr, or
    4416              :    to a file).
    4417              :    Return a reference to *FMT.  */
    4418              : 
    4419              : static sink &
    4420          266 : init_sarif_sink (context &dc,
    4421              :                  std::unique_ptr<sarif_sink> fmt)
    4422              : {
    4423          266 :   gcc_assert (fmt);
    4424          266 :   sink &out = *fmt;
    4425              : 
    4426          266 :   fmt->update_printer ();
    4427              : 
    4428          266 :   dc.set_sink (std::move (fmt));
    4429              : 
    4430          266 :   return out;
    4431              : }
    4432              : 
    4433              : /* Populate DC in preparation for SARIF output to stderr.
    4434              :    Return a reference to the new sink.  */
    4435              : 
    4436              : sink &
    4437            0 : init_sarif_stderr (context &dc,
    4438              :                    const line_maps *line_maps,
    4439              :                    bool formatted)
    4440              : {
    4441            0 :   gcc_assert (line_maps);
    4442            0 :   const sarif_generation_options sarif_gen_opts;
    4443            0 :   auto serialization
    4444            0 :     = std::make_unique<sarif_serialization_format_json> (formatted);
    4445            0 :   return init_sarif_sink
    4446            0 :     (dc,
    4447            0 :      std::make_unique<sarif_stream_sink> (dc,
    4448              :                                           line_maps,
    4449              :                                           std::move (serialization),
    4450              :                                           sarif_gen_opts,
    4451            0 :                                           stderr));
    4452            0 : }
    4453              : 
    4454              : /* Attempt to open "BASE_FILE_NAME""EXTENSION" for writing.
    4455              :    Return a non-null output_file,
    4456              :    or return a null output_file and complain to DC
    4457              :    using LINE_MAPS.  */
    4458              : 
    4459              : output_file
    4460          113 : output_file::try_to_open (context &dc,
    4461              :                           line_maps *line_maps,
    4462              :                           const char *base_file_name,
    4463              :                           const char *extension,
    4464              :                           bool is_binary)
    4465              : {
    4466          113 :   gcc_assert (extension);
    4467          113 :   gcc_assert (extension[0] == '.');
    4468              : 
    4469          113 :   if (!base_file_name)
    4470              :     {
    4471            0 :       rich_location richloc (line_maps, UNKNOWN_LOCATION);
    4472            0 :       dc.emit_diagnostic_with_group
    4473            0 :         (kind::error, richloc, nullptr, 0,
    4474              :          "unable to determine filename for SARIF output");
    4475            0 :       return output_file ();
    4476            0 :     }
    4477              : 
    4478          113 :   label_text filename = label_text::take (concat (base_file_name,
    4479              :                                                   extension,
    4480          113 :                                                   nullptr));
    4481          226 :   FILE *outf = fopen (filename.get (), is_binary ? "wb" : "w");
    4482          113 :   if (!outf)
    4483              :     {
    4484            0 :       rich_location richloc (line_maps, UNKNOWN_LOCATION);
    4485            0 :       dc.emit_diagnostic_with_group
    4486            0 :         (kind::error, richloc, nullptr, 0,
    4487              :          "unable to open %qs for diagnostic output: %m",
    4488              :          filename.get ());
    4489            0 :       return output_file ();
    4490            0 :     }
    4491          113 :   return output_file (outf, true, std::move (filename));
    4492          113 : }
    4493              : 
    4494              : /* Attempt to open BASE_FILE_NAME.sarif for writing JSON.
    4495              :    Return a non-null output_file,
    4496              :    or return a null output_file and complain to DC
    4497              :    using LINE_MAPS.  */
    4498              : 
    4499              : output_file
    4500          113 : open_sarif_output_file (context &dc,
    4501              :                         line_maps *line_maps,
    4502              :                         const char *base_file_name,
    4503              :                         enum sarif_serialization_kind serialization_kind)
    4504              : {
    4505          113 :   const char *suffix;
    4506          113 :   bool is_binary;
    4507          113 :   switch (serialization_kind)
    4508              :     {
    4509            0 :     default:
    4510            0 :       gcc_unreachable ();
    4511          113 :     case sarif_serialization_kind::json:
    4512          113 :       suffix = ".sarif";
    4513          113 :       is_binary = false;
    4514          113 :       break;
    4515              :     }
    4516              : 
    4517          113 :   return output_file::try_to_open (dc,
    4518              :                                    line_maps,
    4519              :                                    base_file_name,
    4520              :                                    suffix,
    4521          113 :                                    is_binary);
    4522              : }
    4523              : 
    4524              : /* Populate DC in preparation for SARIF output to a file named
    4525              :    BASE_FILE_NAME.sarif.
    4526              :    Return a reference to the new sink.  */
    4527              : 
    4528              : sink &
    4529           90 : init_sarif_file (context &dc,
    4530              :                  line_maps *line_maps,
    4531              :                  bool formatted,
    4532              :                  const char *base_file_name)
    4533              : {
    4534           90 :   gcc_assert (line_maps);
    4535              : 
    4536           90 :   output_file output_file_
    4537              :     = open_sarif_output_file (dc,
    4538              :                               line_maps,
    4539              :                               base_file_name,
    4540           90 :                               sarif_serialization_kind::json);
    4541           90 :   auto serialization
    4542           90 :     = std::make_unique<sarif_serialization_format_json> (formatted);
    4543              : 
    4544           90 :   const sarif_generation_options sarif_gen_opts;
    4545           90 :   return init_sarif_sink
    4546           90 :     (dc,
    4547           90 :      std::make_unique<sarif_file_sink> (dc,
    4548              :                                         line_maps,
    4549              :                                         std::move (serialization),
    4550              :                                         sarif_gen_opts,
    4551           90 :                                         std::move (output_file_)));
    4552           90 : }
    4553              : 
    4554              : /* Populate DC in preparation for SARIF output to STREAM.
    4555              :    Return a reference to the new sink.  */
    4556              : 
    4557              : sink &
    4558            0 : init_sarif_stream (context &dc,
    4559              :                    const line_maps *line_maps,
    4560              :                    bool formatted,
    4561              :                    FILE *stream)
    4562              : {
    4563            0 :   gcc_assert (line_maps);
    4564            0 :   const sarif_generation_options sarif_gen_opts;
    4565            0 :   auto serialization
    4566            0 :     = std::make_unique<sarif_serialization_format_json> (formatted);
    4567            0 :   return init_sarif_sink
    4568            0 :     (dc,
    4569            0 :      std::make_unique<sarif_stream_sink> (dc,
    4570              :                                           line_maps,
    4571              :                                           std::move (serialization),
    4572              :                                           sarif_gen_opts,
    4573            0 :                                           stream));
    4574            0 : }
    4575              : 
    4576              : std::unique_ptr<sink>
    4577           25 : make_sarif_sink (context &dc,
    4578              :                  const line_maps &line_maps,
    4579              :                  std::unique_ptr<sarif_serialization_format> serialization,
    4580              :                  const sarif_generation_options &sarif_gen_opts,
    4581              :                  output_file output_file_)
    4582              : {
    4583           25 :   auto sink
    4584              :     = std::make_unique<sarif_file_sink> (dc,
    4585           50 :                                          &line_maps,
    4586              :                                          std::move (serialization),
    4587              :                                          sarif_gen_opts,
    4588           25 :                                          std::move (output_file_));
    4589           25 :   sink->update_printer ();
    4590           25 :   return sink;
    4591           25 : }
    4592              : 
    4593              : // struct sarif_generation_options
    4594              : 
    4595          353 : sarif_generation_options::sarif_generation_options ()
    4596          353 : : m_version (sarif_version::v2_1_0),
    4597          353 :   m_state_graph (false)
    4598              : {
    4599          353 : }
    4600              : 
    4601              : static const char *
    4602            0 : get_dump_string_for_sarif_version (enum sarif_version version)
    4603              : {
    4604            0 :   switch (version)
    4605              :     {
    4606            0 :     default:
    4607            0 :       gcc_unreachable ();
    4608              :     case sarif_version::v2_1_0:
    4609              :       return "v2_1_0";
    4610            0 :     case sarif_version::v2_2_prerelease_csd01:
    4611            0 :       return "v2_2_prerelease_csd01";
    4612              :     }
    4613              : }
    4614              : 
    4615              : void
    4616            0 : sarif_generation_options::dump (FILE *outfile, int indent) const
    4617              : {
    4618            0 :   dumping::emit_string_field (outfile, indent,
    4619              :                               "m_version",
    4620            0 :                               get_dump_string_for_sarif_version (m_version));
    4621            0 :   DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_state_graph);
    4622            0 : }
    4623              : 
    4624              : void
    4625       297176 : maybe_open_sarif_sink_for_socket (context &dc)
    4626              : {
    4627       297176 :   gcc_assert (line_table);
    4628              : 
    4629       297176 :   const char * const env_var_name = "EXPERIMENTAL_SARIF_SOCKET";
    4630       297176 :   const char * const socket_name = getenv (env_var_name);
    4631       297176 :   if (!socket_name)
    4632       297176 :     return;
    4633              : 
    4634            0 :   unique_fd sfd (socket (AF_UNIX, SOCK_STREAM, 0));
    4635            0 :   if (sfd == -1)
    4636            0 :     fatal_error (UNKNOWN_LOCATION,
    4637              :                  "unable to create socket: %m");
    4638              : 
    4639            0 :   struct sockaddr_un addr;
    4640            0 :   memset (&addr, 0, sizeof (addr));
    4641            0 :   addr.sun_family = AF_UNIX;
    4642            0 :   strncpy (addr.sun_path, socket_name, sizeof (addr.sun_path) - 1);
    4643              : 
    4644            0 :   if (connect (sfd, (struct sockaddr *)&addr, sizeof (addr)) == -1)
    4645            0 :     fatal_error (UNKNOWN_LOCATION,
    4646              :                  "unable to connect to %qs: %m",
    4647              :                  socket_name);
    4648              : 
    4649              :   /* TODO: should there be a way to specify other key/value
    4650              :      pairs here?  (as per -fdiagnostics-add-output, but as an
    4651              :      environment variable, perhaps).  */
    4652            0 :   sarif_generation_options sarif_gen_opts;
    4653            0 :   sarif_gen_opts.m_version = sarif_version::v2_1_0;
    4654              : 
    4655            0 :   auto sink_ = std::make_unique<sarif_socket_sink>
    4656              :     (dc,
    4657              :      line_table,
    4658            0 :      std::make_unique <sarif_serialization_format_json> (true),
    4659              :      sarif_gen_opts,
    4660            0 :      std::move (sfd));
    4661            0 :   sink_->update_printer ();
    4662            0 :   dc.add_sink (std::move (sink_));
    4663            0 : }
    4664              : 
    4665              : #if CHECKING_P
    4666              : 
    4667              : namespace selftest {
    4668              : 
    4669              : using auto_fix_quotes = ::selftest::auto_fix_quotes;
    4670              : using line_table_case = ::selftest::line_table_case;
    4671              : 
    4672              : static void
    4673            4 : test_sarif_array_of_unique_1 ()
    4674              : {
    4675            4 :   sarif_array_of_unique<json::string> arr;
    4676              : 
    4677            4 :   ASSERT_EQ (arr.length (), 0);
    4678              : 
    4679            4 :   {
    4680            4 :     size_t idx = arr.append_uniquely (std::make_unique<json::string> ("foo"));
    4681            4 :     ASSERT_EQ (idx, 0);
    4682            4 :     ASSERT_EQ (arr.length (), 1);
    4683              :   }
    4684            4 :   {
    4685            4 :     size_t idx = arr.append_uniquely (std::make_unique<json::string> ("bar"));
    4686            4 :     ASSERT_EQ (idx, 1);
    4687            4 :     ASSERT_EQ (arr.length (), 2);
    4688              :   }
    4689              : 
    4690              :   /* Try adding them again, should be idempotent.  */
    4691            4 :   {
    4692            4 :     size_t idx = arr.append_uniquely (std::make_unique<json::string> ("foo"));
    4693            4 :     ASSERT_EQ (idx, 0);
    4694            4 :     ASSERT_EQ (arr.length (), 2);
    4695              :   }
    4696            4 :   {
    4697            4 :     size_t idx = arr.append_uniquely (std::make_unique<json::string> ("bar"));
    4698            4 :     ASSERT_EQ (idx, 1);
    4699            4 :     ASSERT_EQ (arr.length (), 2);
    4700              :   }
    4701            4 : }
    4702              : 
    4703              : static void
    4704            4 : test_sarif_array_of_unique_2 ()
    4705              : {
    4706            4 :   sarif_array_of_unique<json::object> arr;
    4707              : 
    4708            4 :   ASSERT_EQ (arr.length (), 0);
    4709              : 
    4710            4 :   {
    4711            4 :     auto obj0 = std::make_unique<json::object> ();
    4712            4 :     size_t idx = arr.append_uniquely (std::move (obj0));
    4713            4 :     ASSERT_EQ (idx, 0);
    4714            4 :     ASSERT_EQ (arr.length (), 1);
    4715              : 
    4716              :     // Attempting to add another empty objects should be idempotent.
    4717            4 :     idx = arr.append_uniquely (std::make_unique<json::object> ());
    4718            4 :     ASSERT_EQ (idx, 0);
    4719            4 :     ASSERT_EQ (arr.length (), 1);
    4720            4 :   }
    4721            4 :   {
    4722            4 :     auto obj1 = std::make_unique<json::object> ();
    4723            4 :     obj1->set_string ("foo", "bar");
    4724            4 :     size_t idx = arr.append_uniquely (std::move (obj1));
    4725            4 :     ASSERT_EQ (idx, 1);
    4726            4 :     ASSERT_EQ (arr.length (), 2);
    4727              : 
    4728              :     // Attempting to add an equivalent object should be idempotent.
    4729            4 :     auto other = std::make_unique<json::object> ();
    4730            4 :     other->set_string ("foo", "bar");
    4731            4 :     idx = arr.append_uniquely (std::move (other));
    4732            4 :     ASSERT_EQ (idx, 1);
    4733            4 :     ASSERT_EQ (arr.length (), 2);
    4734            4 :   }
    4735              : 
    4736              :   // Verify behavior of add_explicit_index_values.
    4737            4 :   arr.add_explicit_index_values ();
    4738            4 :   ASSERT_JSON_INT_PROPERTY_EQ (arr[0], "index", 0);
    4739            4 :   ASSERT_JSON_INT_PROPERTY_EQ (arr[1], "index", 1);
    4740            4 : }
    4741              : 
    4742              : /* A subclass of sarif_sink for writing selftests.
    4743              :    The JSON output is cached internally, rather than written
    4744              :    out to a file.  */
    4745              : 
    4746          352 : class test_sarif_diagnostic_context : public test_context
    4747              : {
    4748              : public:
    4749          176 :   test_sarif_diagnostic_context (const char *main_input_filename,
    4750              :                                  const sarif_generation_options &sarif_gen_opts)
    4751          176 :   {
    4752          176 :     auto sink_ = std::make_unique<sarif_buffered_sink> (*this,
    4753              :                                                         line_table,
    4754          352 :                                                         true,
    4755          176 :                                                         sarif_gen_opts);
    4756          176 :     m_sink = sink_.get (); // borrowed
    4757          176 :     init_sarif_sink (*this, std::move (sink_));
    4758          176 :     m_sink->set_main_input_filename (main_input_filename);
    4759          176 :   }
    4760              : 
    4761          168 :   std::unique_ptr<sarif_log> flush_to_object ()
    4762              :   {
    4763          336 :     return m_sink->flush_to_object ();
    4764              :   }
    4765              : 
    4766           96 :   size_t num_results () const { return m_sink->num_results (); }
    4767           32 :   sarif_result &get_result (size_t idx) { return m_sink->get_result (idx); }
    4768              : 
    4769              : private:
    4770              :   class sarif_buffered_sink : public sarif_sink
    4771              :   {
    4772              :   public:
    4773          176 :     sarif_buffered_sink (context &dc,
    4774              :                    const line_maps *line_maps,
    4775              :                    bool formatted,
    4776              :                    const sarif_generation_options &sarif_gen_opts)
    4777              :     : sarif_sink (dc, line_maps,
    4778          176 :                   std::make_unique<sarif_serialization_format_json> (formatted),
    4779          176 :                   sarif_gen_opts)
    4780              :     {
    4781          176 :     }
    4782            0 :     void dump_kind (FILE *out) const final override
    4783              :     {
    4784            0 :       fprintf (out, "sarif_buffered_sink");
    4785            0 :     }
    4786            0 :     bool machine_readable_stderr_p () const final override
    4787              :     {
    4788            0 :       return false;
    4789              :     }
    4790          168 :     std::unique_ptr<sarif_log> flush_to_object ()
    4791              :     {
    4792          168 :       return m_builder.flush_to_object ();
    4793              :     }
    4794              :   };
    4795              : 
    4796              :   sarif_buffered_sink *m_sink; // borrowed
    4797              : };
    4798              : 
    4799              : /* Test making a sarif_location for a complex rich_location
    4800              :    with labels and escape-on-output.  */
    4801              : 
    4802              : static void
    4803          192 : test_make_location_object (const sarif_generation_options &sarif_gen_opts,
    4804              :                            const ::selftest::line_table_case &case_)
    4805              : {
    4806          192 :   source_printing_fixture_one_liner_utf8 f (case_);
    4807          192 :   location_t line_end = linemap_position_for_column (line_table, 31);
    4808              : 
    4809              :   /* Don't attempt to run the tests if column data might be unavailable.  */
    4810          192 :   if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    4811           64 :     return;
    4812              : 
    4813          128 :   test_context dc;
    4814          128 :   pretty_printer pp;
    4815          128 :   sarif_builder builder
    4816              :     (dc, pp, line_table,
    4817          128 :      std::make_unique<sarif_serialization_format_json> (true),
    4818          128 :      sarif_gen_opts);
    4819              : 
    4820              :   /* These "columns" are byte offsets, whereas later on the columns
    4821              :      in the generated SARIF use sarif_builder::get_sarif_column and
    4822              :      thus respect tabs, encoding.  */
    4823          128 :   const location_t foo
    4824          128 :     = make_location (linemap_position_for_column (line_table, 1),
    4825              :                      linemap_position_for_column (line_table, 1),
    4826              :                      linemap_position_for_column (line_table, 8));
    4827          128 :   const location_t bar
    4828          128 :     = make_location (linemap_position_for_column (line_table, 12),
    4829              :                      linemap_position_for_column (line_table, 12),
    4830              :                      linemap_position_for_column (line_table, 17));
    4831          128 :   const location_t field
    4832          128 :     = make_location (linemap_position_for_column (line_table, 19),
    4833              :                      linemap_position_for_column (line_table, 19),
    4834              :                      linemap_position_for_column (line_table, 30));
    4835              : 
    4836          128 :   text_range_label label0 ("label0");
    4837          128 :   text_range_label label1 ("label1");
    4838          128 :   text_range_label label2 ("label2");
    4839              : 
    4840          128 :   rich_location richloc (line_table, foo, &label0, nullptr);
    4841          128 :   richloc.add_range (bar, SHOW_RANGE_WITHOUT_CARET, &label1);
    4842          128 :   richloc.add_range (field, SHOW_RANGE_WITHOUT_CARET, &label2);
    4843          128 :   richloc.set_escape_on_output (true);
    4844              : 
    4845          128 :   sarif_result result (0);
    4846              : 
    4847          128 :   std::unique_ptr<sarif_location> location_obj
    4848              :     = builder.make_location_object
    4849          128 :         (&result, richloc, logical_locations::key (),
    4850          128 :          diagnostic_artifact_role::analysis_target);
    4851          128 :   ASSERT_NE (location_obj, nullptr);
    4852              : 
    4853          128 :   auto physical_location
    4854          128 :     = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (location_obj.get (),
    4855              :                                                "physicalLocation");
    4856          128 :   {
    4857          128 :     auto region
    4858          128 :       = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location, "region");
    4859          128 :     ASSERT_JSON_INT_PROPERTY_EQ (region, "startLine", 1);
    4860          128 :     ASSERT_JSON_INT_PROPERTY_EQ (region, "startColumn", 1);
    4861          128 :     ASSERT_JSON_INT_PROPERTY_EQ (region, "endColumn", 7);
    4862              :   }
    4863          128 :   {
    4864          128 :     auto context_region
    4865          128 :       = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
    4866              :                                                  "contextRegion");
    4867          128 :     ASSERT_JSON_INT_PROPERTY_EQ (context_region, "startLine", 1);
    4868              : 
    4869          128 :     {
    4870          128 :       auto snippet
    4871          128 :         = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (context_region, "snippet");
    4872              : 
    4873              :       /* We expect the snippet's "text" to be a copy of the content.  */
    4874          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (snippet, "text",  f.m_content);
    4875              : 
    4876              :       /* We expect the snippet to have a "rendered" whose "text" has a
    4877              :          pure ASCII escaped copy of the line (with labels, etc).  */
    4878          128 :       {
    4879          128 :         auto rendered
    4880          128 :           = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (snippet, "rendered");
    4881          128 :         ASSERT_JSON_STRING_PROPERTY_EQ
    4882              :           (rendered, "text",
    4883              :            "1 | <U+1F602>_foo = <U+03C0>_bar.<U+1F602>_field<U+03C0>;\n"
    4884              :            "  | ^~~~~~~~~~~~~   ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~\n"
    4885              :            "  | |               |            |\n"
    4886              :            "  | label0          label1       label2\n");
    4887              :       }
    4888              :     }
    4889              :   }
    4890          128 :   auto annotations
    4891          128 :     = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (location_obj.get (),
    4892              :                                               "annotations");
    4893          128 :   ASSERT_EQ (annotations->size (), 3);
    4894          128 :   {
    4895          128 :     {
    4896          128 :       auto a0 = (*annotations)[0];
    4897          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a0, "startLine", 1);
    4898          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a0, "startColumn", 1);
    4899          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a0, "endColumn", 7);
    4900          128 :       auto message
    4901          128 :         = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a0, "message");
    4902          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label0");
    4903              :     }
    4904          128 :     {
    4905          128 :       auto a1 = (*annotations)[1];
    4906          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a1, "startLine", 1);
    4907          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a1, "startColumn", 10);
    4908          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a1, "endColumn", 15);
    4909          128 :       auto message
    4910          128 :         = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a1, "message");
    4911          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label1");
    4912              :     }
    4913          128 :     {
    4914          128 :       auto a2 = (*annotations)[2];
    4915          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a2, "startLine", 1);
    4916          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a2, "startColumn", 16);
    4917          128 :       ASSERT_JSON_INT_PROPERTY_EQ (a2, "endColumn", 25);
    4918          128 :       auto message
    4919          128 :         = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (a2, "message");
    4920          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (message, "text", "label2");
    4921              :     }
    4922              :   }
    4923          192 : }
    4924              : 
    4925              : /* Test of reporting a diagnostic at UNKNOWN_LOCATION to a
    4926              :    diagnostics::context and examining the generated sarif_log.
    4927              :    Verify various basic properties. */
    4928              : 
    4929              : static void
    4930            8 : test_simple_log (const sarif_generation_options &sarif_gen_opts)
    4931              : {
    4932            8 :   test_sarif_diagnostic_context dc ("MAIN_INPUT_FILENAME", sarif_gen_opts);
    4933              : 
    4934            8 :   rich_location richloc (line_table, UNKNOWN_LOCATION);
    4935            8 :   dc.report (kind::error, richloc, nullptr, 0, "this is a test: %i", 42);
    4936              : 
    4937            8 :   auto log_ptr = dc.flush_to_object ();
    4938              : 
    4939              :   // 3.13 sarifLog:
    4940            8 :   auto log = log_ptr.get ();
    4941            8 :   const enum sarif_version version = sarif_gen_opts.m_version;
    4942            8 :   ASSERT_JSON_STRING_PROPERTY_EQ (log, "$schema",
    4943              :                                   sarif_version_to_url (version));
    4944            8 :   ASSERT_JSON_STRING_PROPERTY_EQ (log, "version",
    4945              :                                   sarif_version_to_property (version));
    4946              : 
    4947            8 :   auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
    4948            8 :   ASSERT_EQ (runs->size (), 1);
    4949              : 
    4950              :   // 3.14 "run" object:
    4951            8 :   auto run = (*runs)[0];
    4952              : 
    4953            8 :   {
    4954              :     // 3.14.6:
    4955            8 :     auto tool = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (run, "tool");
    4956              : 
    4957            8 :     EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (tool, "driver"); // 3.18.2
    4958              :   }
    4959              : 
    4960            8 :   {
    4961              :     // 3.14.11
    4962            8 :     auto invocations
    4963            8 :       = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "invocations");
    4964            8 :     ASSERT_EQ (invocations->size (), 1);
    4965              : 
    4966            8 :     {
    4967              :       // 3.20 "invocation" object:
    4968            8 :       auto invocation = (*invocations)[0];
    4969              : 
    4970              :       // 3.20.3 arguments property
    4971              : 
    4972              :       // 3.20.7 startTimeUtc property
    4973            8 :       EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (invocation, "startTimeUtc");
    4974              : 
    4975              :       // 3.20.8 endTimeUtc property
    4976            8 :       EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (invocation, "endTimeUtc");
    4977              : 
    4978              :       // 3.20.19 workingDirectory property
    4979            8 :       {
    4980            8 :         auto wd_obj
    4981            8 :           = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (invocation,
    4982              :                                                      "workingDirectory");
    4983            8 :         EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY (wd_obj, "uri");
    4984              :       }
    4985              : 
    4986              :       // 3.20.21 toolExecutionNotifications property
    4987            8 :       auto notifications
    4988            8 :         = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY
    4989              :             (invocation, "toolExecutionNotifications");
    4990            8 :       ASSERT_EQ (notifications->size (), 0);
    4991              :     }
    4992              :   }
    4993              : 
    4994            8 :   {
    4995              :     // 3.14.15:
    4996            8 :     auto artifacts = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "artifacts");
    4997            8 :     ASSERT_EQ (artifacts->size (), 1);
    4998              : 
    4999            8 :     {
    5000              :       // 3.24 "artifact" object:
    5001            8 :       auto artifact = (*artifacts)[0];
    5002              : 
    5003              :       // 3.24.2:
    5004            8 :       auto location
    5005            8 :         = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (artifact, "location");
    5006            8 :       ASSERT_JSON_STRING_PROPERTY_EQ (location, "uri", "MAIN_INPUT_FILENAME");
    5007              : 
    5008              :       // 3.24.6:
    5009            8 :       auto roles = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (artifact, "roles");
    5010            8 :       ASSERT_EQ (roles->size (), 1);
    5011            8 :       {
    5012            8 :         auto role = (*roles)[0];
    5013            8 :         ASSERT_JSON_STRING_EQ (role, "analysisTarget");
    5014              :       }
    5015              :     }
    5016              :   }
    5017              : 
    5018            8 :   {
    5019              :     // 3.14.23:
    5020            8 :     auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
    5021            8 :     ASSERT_EQ (results->size (), 1);
    5022              : 
    5023            8 :     {
    5024              :       // 3.27 "result" object:
    5025            8 :       auto result = (*results)[0];
    5026            8 :       ASSERT_JSON_STRING_PROPERTY_EQ (result, "ruleId", "error");
    5027            8 :       ASSERT_JSON_STRING_PROPERTY_EQ (result, "level", "error"); // 3.27.10
    5028              : 
    5029            8 :       {
    5030              :         // 3.27.11:
    5031            8 :         auto message
    5032            8 :           = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result, "message");
    5033            8 :         ASSERT_JSON_STRING_PROPERTY_EQ (message, "text",
    5034              :                                         "this is a test: 42");
    5035              :       }
    5036              : 
    5037              :       // 3.27.12:
    5038            8 :       auto locations
    5039            8 :         = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (result, "locations");
    5040            8 :       ASSERT_EQ (locations->size (), 0);
    5041              :     }
    5042              :   }
    5043            8 : }
    5044              : 
    5045              : /* As above, but with a "real" location_t.  */
    5046              : 
    5047              : static void
    5048          192 : test_simple_log_2 (const sarif_generation_options &sarif_gen_opts,
    5049              :                    const line_table_case &case_)
    5050              : {
    5051          192 :   auto_fix_quotes fix_quotes;
    5052              : 
    5053          192 :   const char *const content
    5054              :     /* 000000000111111
    5055              :        123456789012345.  */
    5056              :     = "unsinged int i;\n";
    5057          192 :   source_printing_fixture f (case_, content);
    5058          192 :   location_t line_end = linemap_position_for_column (line_table, 31);
    5059              : 
    5060              :   /* Don't attempt to run the tests if column data might be unavailable.  */
    5061          192 :   if (line_end > LINE_MAP_MAX_LOCATION_WITH_COLS)
    5062           64 :     return;
    5063              : 
    5064          128 :   test_sarif_diagnostic_context dc (f.get_filename (), sarif_gen_opts);
    5065              : 
    5066          128 :   const location_t typo_loc
    5067          128 :     = make_location (linemap_position_for_column (line_table, 1),
    5068              :                      linemap_position_for_column (line_table, 1),
    5069              :                      linemap_position_for_column (line_table, 8));
    5070              : 
    5071          128 :   rich_location richloc (line_table, typo_loc);
    5072          128 :   dc.report (kind::error, richloc, nullptr, 0,
    5073              :              "did you misspell %qs again?",
    5074              :              "unsigned");
    5075              : 
    5076          128 :   auto log_ptr = dc.flush_to_object ();
    5077              : 
    5078              :   // 3.13 sarifLog:
    5079          128 :   auto log = log_ptr.get ();
    5080              : 
    5081          128 :   auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
    5082          128 :   ASSERT_EQ (runs->size (), 1);
    5083              : 
    5084              :   // 3.14 "run" object:
    5085          128 :   auto run = (*runs)[0];
    5086              : 
    5087          128 :   {
    5088              :     // 3.14.23:
    5089          128 :     auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
    5090          128 :     ASSERT_EQ (results->size (), 1);
    5091              : 
    5092          128 :     {
    5093              :       // 3.27 "result" object:
    5094          128 :       auto result = (*results)[0];
    5095          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (result, "ruleId", "error");
    5096          128 :       ASSERT_JSON_STRING_PROPERTY_EQ (result, "level", "error"); // 3.27.10
    5097              : 
    5098          128 :       {
    5099              :         // 3.27.11:
    5100          128 :         auto message
    5101          128 :           = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result, "message");
    5102          128 :         ASSERT_JSON_STRING_PROPERTY_EQ (message, "text",
    5103              :                                         "did you misspell `unsigned' again?");
    5104              :       }
    5105              : 
    5106              :       // 3.27.12:
    5107          128 :       auto locations
    5108          128 :         = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (result, "locations");
    5109          128 :       ASSERT_EQ (locations->size (), 1);
    5110              : 
    5111          128 :       {
    5112              :         // 3.28 "location" object:
    5113          128 :         auto location = (*locations)[0];
    5114              : 
    5115          128 :         auto physical_location
    5116          128 :           = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (location,
    5117              :                                                      "physicalLocation");
    5118          128 :         {
    5119          128 :           auto region
    5120          128 :             = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
    5121              :                                                        "region");
    5122          128 :           ASSERT_JSON_INT_PROPERTY_EQ (region, "startLine", 1);
    5123          128 :           ASSERT_JSON_INT_PROPERTY_EQ (region, "startColumn", 1);
    5124          128 :           ASSERT_JSON_INT_PROPERTY_EQ (region, "endColumn", 9);
    5125              :         }
    5126          128 :         {
    5127          128 :           auto context_region
    5128          128 :             = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (physical_location,
    5129              :                                                        "contextRegion");
    5130          128 :           ASSERT_JSON_INT_PROPERTY_EQ (context_region, "startLine", 1);
    5131              : 
    5132          128 :           {
    5133          128 :             auto snippet
    5134          128 :               = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (context_region,
    5135              :                                                          "snippet");
    5136              : 
    5137              :             /* We expect the snippet's "text" to be a copy of the content.  */
    5138          128 :             ASSERT_JSON_STRING_PROPERTY_EQ (snippet, "text",  f.m_content);
    5139              :           }
    5140              :         }
    5141              :       }
    5142              :     }
    5143              :   }
    5144          192 : }
    5145              : 
    5146              : /* Assuming that a single diagnostic has been emitted within
    5147              :    LOG, get a json::object for the result object.  */
    5148              : 
    5149              : static const json::object *
    5150           32 : get_result_from_log (const sarif_log *log)
    5151              : {
    5152           32 :   auto runs = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (log, "runs"); // 3.13.4
    5153           32 :   ASSERT_EQ (runs->size (), 1);
    5154              : 
    5155              :   // 3.14 "run" object:
    5156           32 :   auto run = (*runs)[0];
    5157              : 
    5158              :   // 3.14.23:
    5159           32 :   auto results = EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY (run, "results");
    5160           32 :   ASSERT_EQ (results->size (), 1);
    5161              : 
    5162              :   // 3.27 "result" object:
    5163           32 :   auto result = (*results)[0];
    5164           32 :   return expect_json_object (SELFTEST_LOCATION, result);
    5165              : }
    5166              : 
    5167              : static const json::object *
    5168           16 : get_message_from_result (const sarif_result &result)
    5169              : {
    5170              :   // 3.27.11:
    5171           16 :   auto message_obj
    5172           16 :     = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (&result, "message");
    5173           16 :   return message_obj;
    5174              : }
    5175              : 
    5176              : /* Assuming that a single diagnostic has been emitted to
    5177              :    DC, get a json::object for the message object within
    5178              :    the result.  */
    5179              : 
    5180              : static const json::object *
    5181           32 : get_message_from_log (const sarif_log *log)
    5182              : {
    5183           32 :   auto result_obj = get_result_from_log (log);
    5184              : 
    5185              :   // 3.27.11:
    5186           32 :   auto message_obj
    5187           32 :     = EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY (result_obj, "message");
    5188           32 :   return message_obj;
    5189              : }
    5190              : 
    5191              : /* Tests of messages with embedded links; see SARIF v2.1.0 3.11.6.  */
    5192              : 
    5193              : static void
    5194            8 : test_message_with_embedded_link (const sarif_generation_options &sarif_gen_opts)
    5195              : {
    5196            8 :   auto_fix_quotes fix_quotes;
    5197            8 :   {
    5198            8 :     test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
    5199            8 :     rich_location richloc (line_table, UNKNOWN_LOCATION);
    5200            8 :     dc.report (kind::error, richloc, nullptr, 0,
    5201              :                "before %{text%} after",
    5202              :                "http://example.com");
    5203            8 :     std::unique_ptr<sarif_log> log = dc.flush_to_object ();
    5204              : 
    5205            8 :     auto message_obj = get_message_from_log (log.get ());
    5206            8 :     ASSERT_JSON_STRING_PROPERTY_EQ
    5207              :       (message_obj, "text",
    5208              :        "before [text](http://example.com) after");
    5209            8 :   }
    5210              : 
    5211              :   /* Escaping in message text.
    5212              :      This is "EXAMPLE 1" from 3.11.6.  */
    5213            8 :   {
    5214            8 :     test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
    5215            8 :     rich_location richloc (line_table, UNKNOWN_LOCATION);
    5216              : 
    5217              :     /* Disable "unquoted sequence of 2 consecutive punctuation
    5218              :        characters `]\' in format" warning.  */
    5219              : #if __GNUC__ >= 10
    5220            8 : #  pragma GCC diagnostic push
    5221            8 : #  pragma GCC diagnostic ignored "-Wformat-diag"
    5222              : #endif
    5223            8 :     dc.report (kind::error, richloc, nullptr, 0,
    5224              :                "Prohibited term used in %{para[0]\\spans[2]%}.",
    5225              :                "1");
    5226              : #if __GNUC__ >= 10
    5227            8 : #  pragma GCC diagnostic pop
    5228              : #endif
    5229              : 
    5230            8 :     std::unique_ptr<sarif_log> log = dc.flush_to_object ();
    5231              : 
    5232            8 :     auto message_obj = get_message_from_log (log.get ());
    5233            8 :     ASSERT_JSON_STRING_PROPERTY_EQ
    5234              :       (message_obj, "text",
    5235              :        "Prohibited term used in [para\\[0\\]\\\\spans\\[2\\]](1).");
    5236              :     /* This isn't exactly what EXAMPLE 1 of the spec has; reported as
    5237              :        https://github.com/oasis-tcs/sarif-spec/issues/656  */
    5238            8 :   }
    5239              : 
    5240              :   /* Urlifier.  */
    5241            8 :   {
    5242            8 :     class test_urlifier : public urlifier
    5243              :     {
    5244              :     public:
    5245              :       char *
    5246           16 :       get_url_for_quoted_text (const char *p, size_t sz) const final override
    5247              :       {
    5248           16 :         if (!strncmp (p, "-foption", sz))
    5249            8 :           return xstrdup ("http://example.com");
    5250              :         return nullptr;
    5251              :       }
    5252              :     };
    5253              : 
    5254            8 :     test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
    5255            8 :     dc.push_owned_urlifier (std::make_unique<test_urlifier> ());
    5256            8 :     rich_location richloc (line_table, UNKNOWN_LOCATION);
    5257            8 :     dc.report (kind::error, richloc, nullptr, 0,
    5258              :                "foo %<-foption%> %<unrecognized%> bar");
    5259            8 :     std::unique_ptr<sarif_log> log = dc.flush_to_object ();
    5260              : 
    5261            8 :     auto message_obj = get_message_from_log (log.get ());
    5262            8 :     ASSERT_JSON_STRING_PROPERTY_EQ
    5263              :       (message_obj, "text",
    5264              :        "foo `[-foption](http://example.com)' `unrecognized' bar");
    5265            8 :   }
    5266            8 : }
    5267              : 
    5268              : /* Verify that braces in messages get escaped, as per
    5269              :    3.11.5 ("Messages with placeholders").  */
    5270              : 
    5271              : static void
    5272            8 : test_message_with_braces (const sarif_generation_options &sarif_gen_opts)
    5273              : {
    5274            8 :   auto_fix_quotes fix_quotes;
    5275            8 :   {
    5276            8 :     test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
    5277            8 :     rich_location richloc (line_table, UNKNOWN_LOCATION);
    5278            8 :     dc.report (kind::error, richloc, nullptr, 0,
    5279              :                "open brace: %qs close brace: %qs",
    5280              :                "{", "}");
    5281            8 :     std::unique_ptr<sarif_log> log = dc.flush_to_object ();
    5282              : 
    5283            8 :     auto message_obj = get_message_from_log (log.get ());
    5284            8 :     ASSERT_JSON_STRING_PROPERTY_EQ
    5285              :       (message_obj, "text",
    5286              :        "open brace: `{{' close brace: `}}'");
    5287            8 :   }
    5288            8 : }
    5289              : 
    5290              : static void
    5291            8 : test_buffering (const sarif_generation_options &sarif_gen_opts)
    5292              : {
    5293            8 :   test_sarif_diagnostic_context dc ("test.c", sarif_gen_opts);
    5294              : 
    5295            8 :   diagnostics::buffer buf_a (dc);
    5296            8 :   diagnostics::buffer buf_b (dc);
    5297              : 
    5298            8 :   rich_location rich_loc (line_table, UNKNOWN_LOCATION);
    5299              : 
    5300            8 :   ASSERT_EQ (dc.diagnostic_count (kind::error), 0);
    5301            8 :   ASSERT_EQ (buf_a.diagnostic_count (kind::error), 0);
    5302            8 :   ASSERT_EQ (buf_b.diagnostic_count (kind::error), 0);
    5303            8 :   ASSERT_EQ (dc.num_results (), 0);
    5304            8 :   ASSERT_TRUE (buf_a.empty_p ());
    5305            8 :   ASSERT_TRUE (buf_b.empty_p ());
    5306              : 
    5307              :   /* Unbuffered diagnostic.  */
    5308            8 :   {
    5309            8 :     dc.report (kind::error, rich_loc, nullptr, 0,
    5310              :                "message 1");
    5311              : 
    5312            8 :     ASSERT_EQ (dc.diagnostic_count (kind::error), 1);
    5313            8 :     ASSERT_EQ (buf_a.diagnostic_count (kind::error), 0);
    5314            8 :     ASSERT_EQ (buf_b.diagnostic_count (kind::error), 0);
    5315            8 :     ASSERT_EQ (dc.num_results (), 1);
    5316            8 :     sarif_result &result_obj = dc.get_result (0);
    5317            8 :     auto message_obj = get_message_from_result (result_obj);
    5318            8 :     ASSERT_JSON_STRING_PROPERTY_EQ (message_obj, "text",
    5319              :                                     "message 1");
    5320            8 :     ASSERT_TRUE (buf_a.empty_p ());
    5321            8 :     ASSERT_TRUE (buf_b.empty_p ());
    5322              :   }
    5323              : 
    5324              :   /* Buffer diagnostic into buffer A.  */
    5325            8 :   {
    5326            8 :     dc.set_diagnostic_buffer (&buf_a);
    5327            8 :     dc.report (kind::error, rich_loc, nullptr, 0,
    5328              :                "message in buffer a");
    5329            8 :     ASSERT_EQ (dc.diagnostic_count (kind::error), 1);
    5330            8 :     ASSERT_EQ (buf_a.diagnostic_count (kind::error), 1);
    5331            8 :     ASSERT_EQ (buf_b.diagnostic_count (kind::error), 0);
    5332            8 :     ASSERT_EQ (dc.num_results (), 1);
    5333            8 :     ASSERT_FALSE (buf_a.empty_p ());
    5334            8 :     ASSERT_TRUE (buf_b.empty_p ());
    5335              :   }
    5336              : 
    5337              :   /* Buffer diagnostic into buffer B.  */
    5338            8 :   {
    5339            8 :     dc.set_diagnostic_buffer (&buf_b);
    5340            8 :     dc.report (kind::error, rich_loc, nullptr, 0,
    5341              :                "message in buffer b");
    5342            8 :     ASSERT_EQ (dc.diagnostic_count (kind::error), 1);
    5343            8 :     ASSERT_EQ (buf_a.diagnostic_count (kind::error), 1);
    5344            8 :     ASSERT_EQ (buf_b.diagnostic_count (kind::error), 1);
    5345            8 :     ASSERT_EQ (dc.num_results (), 1);
    5346            8 :     ASSERT_FALSE (buf_a.empty_p ());
    5347            8 :     ASSERT_FALSE (buf_b.empty_p ());
    5348              :   }
    5349              : 
    5350              :   /* Flush buffer B to dc.  */
    5351            8 :   {
    5352            8 :     dc.flush_diagnostic_buffer (buf_b);
    5353            8 :     ASSERT_EQ (dc.diagnostic_count (kind::error), 2);
    5354            8 :     ASSERT_EQ (buf_a.diagnostic_count (kind::error), 1);
    5355            8 :     ASSERT_EQ (buf_b.diagnostic_count (kind::error), 0);
    5356            8 :     ASSERT_EQ (dc.num_results (), 2);
    5357            8 :     sarif_result &result_1_obj = dc.get_result (1);
    5358            8 :     auto message_1_obj = get_message_from_result (result_1_obj);
    5359            8 :     ASSERT_JSON_STRING_PROPERTY_EQ (message_1_obj, "text",
    5360              :                                     "message in buffer b");
    5361            8 :     ASSERT_FALSE (buf_a.empty_p ());
    5362            8 :     ASSERT_TRUE (buf_b.empty_p ());
    5363              :   }
    5364              : 
    5365              :   /* Clear buffer A.  */
    5366            8 :   {
    5367            8 :     dc.clear_diagnostic_buffer (buf_a);
    5368            8 :     ASSERT_EQ (dc.diagnostic_count (kind::error), 2);
    5369            8 :     ASSERT_EQ (buf_a.diagnostic_count (kind::error), 0);
    5370            8 :     ASSERT_EQ (buf_b.diagnostic_count (kind::error), 0);
    5371            8 :     ASSERT_EQ (dc.num_results (), 2);
    5372            8 :     ASSERT_TRUE (buf_a.empty_p ());
    5373            8 :     ASSERT_TRUE (buf_b.empty_p ());
    5374              :   }
    5375            8 : }
    5376              : 
    5377              : template <class ...ArgTypes>
    5378              : static void
    5379          208 : for_each_sarif_gen_option (void (*callback) (const sarif_generation_options &,
    5380              :                                              ArgTypes ...),
    5381              :                            ArgTypes ...args)
    5382              : {
    5383          208 :   sarif_generation_options sarif_gen_opts;
    5384          624 :   for (int version_idx = 0;
    5385          624 :        version_idx < (int)sarif_version::num_versions;
    5386              :        ++version_idx)
    5387              :     {
    5388          416 :       sarif_gen_opts.m_version = static_cast<enum sarif_version> (version_idx);
    5389              : 
    5390          416 :       callback (sarif_gen_opts, args...);
    5391              :     }
    5392          208 : }
    5393              : 
    5394              : static void
    5395           96 : run_line_table_case_tests_per_version (const line_table_case &case_)
    5396              : {
    5397           96 :   for_each_sarif_gen_option<const line_table_case &>
    5398           96 :     (test_make_location_object, case_);
    5399              : 
    5400           96 :   for_each_sarif_gen_option<const line_table_case &>
    5401           96 :     (test_simple_log_2, case_);
    5402           96 : }
    5403              : 
    5404              : /* Run all of the selftests within this file.  */
    5405              : 
    5406              : void
    5407            4 : sarif_sink_cc_tests ()
    5408              : {
    5409            4 :   test_sarif_array_of_unique_1 ();
    5410            4 :   test_sarif_array_of_unique_2 ();
    5411              : 
    5412            4 :   for_each_sarif_gen_option (test_simple_log);
    5413            4 :   for_each_sarif_gen_option (test_message_with_embedded_link);
    5414            4 :   for_each_sarif_gen_option (test_message_with_braces);
    5415            4 :   for_each_sarif_gen_option (test_buffering);
    5416              : 
    5417              :   /* Run tests per (SARIF gen-option, line-table-case) pair.  */
    5418            4 :   for_each_line_table_case (run_line_table_case_tests_per_version);
    5419            4 : }
    5420              : 
    5421              : } // namespace diagnostics::selftest
    5422              : 
    5423              : #endif /* CHECKING_P */
    5424              : 
    5425              : } // namespace diagnostics
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.