Line data Source code
1 : /* SARIF output for diagnostics.
2 : Copyright (C) 2023-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 : #ifndef GCC_DIAGNOSTICS_SARIF_SINK_H
22 : #define GCC_DIAGNOSTICS_SARIF_SINK_H
23 :
24 : #include "json.h"
25 : #include "diagnostics/sink.h"
26 : #include "diagnostics/output-file.h"
27 : #include "diagnostics/logical-locations.h"
28 :
29 : namespace diagnostics {
30 :
31 : namespace digraphs {
32 : class digraph;
33 : class node;
34 : class edge;
35 : }
36 :
37 : /* Enum for choosing what format to serializing the generated SARIF into. */
38 :
39 : enum class sarif_serialization_kind
40 : {
41 : json,
42 :
43 : num_values
44 : };
45 :
46 : extern output_file
47 : open_sarif_output_file (context &dc,
48 : line_maps *line_maps,
49 : const char *base_file_name,
50 : enum sarif_serialization_kind serialization_kind);
51 :
52 : extern sink &
53 : init_sarif_stderr (context &dc,
54 : const line_maps *line_maps,
55 : bool formatted);
56 : extern sink &
57 : init_sarif_file (context &dc,
58 : line_maps *line_maps,
59 : bool formatted,
60 : const char *base_file_name);
61 : extern sink &
62 : init_sarif_stream (context &dc,
63 : const line_maps *line_maps,
64 : bool formatted,
65 : FILE *stream);
66 :
67 : /* Abstract base class for handling JSON output vs other kinds of
68 : serialization of the json tree. */
69 :
70 418 : class sarif_serialization_format
71 : {
72 : public:
73 : virtual ~sarif_serialization_format () {}
74 : virtual void write_to_file (FILE *outf,
75 : const json::value &top) = 0;
76 : virtual void dump (FILE *out, int indent) const = 0;
77 : };
78 :
79 : /* Concrete subclass for serializing SARIF as JSON. */
80 :
81 : class sarif_serialization_format_json : public sarif_serialization_format
82 : {
83 : public:
84 418 : sarif_serialization_format_json (bool formatted)
85 418 : : m_formatted (formatted)
86 : {
87 : }
88 : void write_to_file (FILE *outf, const json::value &top) final override;
89 : void dump (FILE *out, int indent) const final override;
90 :
91 : private:
92 : bool m_formatted;
93 : };
94 :
95 : /* Control of SARIF generation. */
96 :
97 : enum class sarif_version
98 : {
99 : v2_1_0,
100 : v2_2_prerelease_2024_08_08,
101 :
102 : num_versions
103 : };
104 :
105 : /* A bundle of state for controlling what to put in SARIF output,
106 : such as which version of SARIF to generate
107 : (as opposed to SARIF *serialization* options, such as formatting). */
108 :
109 : struct sarif_generation_options
110 : {
111 : sarif_generation_options ();
112 :
113 : void dump (FILE *out, int indent) const;
114 :
115 : enum sarif_version m_version;
116 : bool m_state_graph;
117 : };
118 :
119 : extern std::unique_ptr<sink>
120 : make_sarif_sink (context &dc,
121 : const line_maps &line_maps,
122 : std::unique_ptr<sarif_serialization_format> serialization_format,
123 : const sarif_generation_options &sarif_gen_opts,
124 : output_file output_file_);
125 :
126 : class sarif_builder;
127 : class sarif_location_manager;
128 :
129 : /* Concrete subclass of json::object for SARIF property bags
130 : (SARIF v2.1.0 section 3.8). */
131 :
132 3430 : class sarif_property_bag : public json::object
133 : {
134 : public:
135 : void set_logical_location (const char *property_name,
136 : sarif_builder &,
137 : logical_locations::key logical_loc);
138 : void set_graph (const char *property_name,
139 : sarif_builder &,
140 : sarif_location_manager *sarif_location_mgr,
141 : const digraphs::digraph &g);
142 : };
143 :
144 : /* Concrete subclass of json::object for SARIF objects that can
145 : contain property bags (as per SARIF v2.1.0 section 3.8.1, which has:
146 : "In addition to those properties that are explicitly documented, every
147 : object defined in this document MAY contain a property named properties
148 : whose value is a property bag.") */
149 :
150 12729 : class sarif_object : public json::object
151 : {
152 : public:
153 : sarif_property_bag &get_or_create_properties ();
154 : };
155 :
156 : /* Subclass of sarif_object for SARIF "graph" objects
157 : (SARIF v2.1.0 section 3.39). */
158 :
159 83 : class sarif_graph : public sarif_object
160 : {
161 : };
162 :
163 : /* Subclass of sarif_object for SARIF "node" objects
164 : (SARIF v2.1.0 section 3.40). */
165 :
166 1224 : class sarif_node : public sarif_object
167 : {
168 : };
169 :
170 : /* Subclass of sarif_object for SARIF "edge" objects
171 : (SARIF v2.1.0 section 3.41). */
172 :
173 714 : class sarif_edge : public sarif_object
174 : {
175 : };
176 :
177 : extern std::unique_ptr<sarif_graph>
178 : make_sarif_graph (const digraphs::digraph &g,
179 : sarif_builder *builder,
180 : sarif_location_manager *sarif_location_mgr);
181 :
182 : extern std::unique_ptr<sarif_node>
183 : make_sarif_node (const digraphs::node &n,
184 : sarif_builder *builder,
185 : sarif_location_manager *sarif_location_mgr);
186 :
187 : extern std::unique_ptr<sarif_edge>
188 : make_sarif_edge (const digraphs::edge &e,
189 : sarif_builder *builder);
190 :
191 : extern void
192 : maybe_open_sarif_sink_for_socket (context &ctxt);
193 :
194 : } // namespace diagnostics
195 :
196 : #endif /* ! GCC_DIAGNOSTICS_SARIF_SINK_H */
|