Branch data Line data Source code
1 : : /* SARIF output for diagnostics.
2 : : Copyright (C) 2023-2025 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_DIAGNOSTIC_FORMAT_SARIF_H
22 : : #define GCC_DIAGNOSTIC_FORMAT_SARIF_H
23 : :
24 : : #include "json.h"
25 : : #include "diagnostic-format.h"
26 : : #include "diagnostic-output-file.h"
27 : : #include "logical-location.h"
28 : :
29 : : /* Enum for choosing what format to serializing the generated SARIF into. */
30 : :
31 : : enum class sarif_serialization_kind
32 : : {
33 : : json,
34 : :
35 : : num_values
36 : : };
37 : :
38 : : extern diagnostic_output_file
39 : : diagnostic_output_format_open_sarif_file (diagnostic_context &context,
40 : : line_maps *line_maps,
41 : : const char *base_file_name,
42 : : enum sarif_serialization_kind serialization_kind);
43 : :
44 : : extern diagnostic_output_format &
45 : : diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
46 : : const line_maps *line_maps,
47 : : bool formatted);
48 : : extern diagnostic_output_format &
49 : : diagnostic_output_format_init_sarif_file (diagnostic_context &context,
50 : : line_maps *line_maps,
51 : : bool formatted,
52 : : const char *base_file_name);
53 : : extern diagnostic_output_format &
54 : : diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
55 : : const line_maps *line_maps,
56 : : bool formatted,
57 : : FILE *stream);
58 : :
59 : : /* Abstract base class for handling JSON output vs other kinds of
60 : : serialization of the json tree. */
61 : :
62 : 401 : class sarif_serialization_format
63 : : {
64 : : public:
65 : : virtual ~sarif_serialization_format () {}
66 : : virtual void write_to_file (FILE *outf,
67 : : const json::value &top) = 0;
68 : : };
69 : :
70 : : /* Concrete subclass for serializing SARIF as JSON. */
71 : :
72 : : class sarif_serialization_format_json : public sarif_serialization_format
73 : : {
74 : : public:
75 : 401 : sarif_serialization_format_json (bool formatted)
76 : 401 : : m_formatted (formatted)
77 : : {
78 : : }
79 : : void write_to_file (FILE *outf, const json::value &top) final override;
80 : :
81 : : private:
82 : : bool m_formatted;
83 : : };
84 : :
85 : : /* Control of SARIF generation. */
86 : :
87 : : enum class sarif_version
88 : : {
89 : : v2_1_0,
90 : : v2_2_prerelease_2024_08_08,
91 : :
92 : : num_versions
93 : : };
94 : :
95 : : /* A bundle of state for controlling what to put in SARIF output,
96 : : such as which version of SARIF to generate
97 : : (as opposed to SARIF *serialization* options, such as formatting). */
98 : :
99 : : struct sarif_generation_options
100 : : {
101 : : sarif_generation_options ();
102 : :
103 : : enum sarif_version m_version;
104 : : bool m_xml_state;
105 : : };
106 : :
107 : : extern std::unique_ptr<diagnostic_output_format>
108 : : make_sarif_sink (diagnostic_context &context,
109 : : const line_maps &line_maps,
110 : : std::unique_ptr<sarif_serialization_format> serialization_format,
111 : : const sarif_generation_options &sarif_gen_opts,
112 : : diagnostic_output_file output_file);
113 : :
114 : : class sarif_builder;
115 : :
116 : : /* Concrete subclass of json::object for SARIF property bags
117 : : (SARIF v2.1.0 section 3.8). */
118 : :
119 : 306 : class sarif_property_bag : public json::object
120 : : {
121 : : public:
122 : : void set_logical_location (const char *property_name,
123 : : sarif_builder &,
124 : : logical_location logical_loc);
125 : : };
126 : :
127 : : /* Concrete subclass of json::object for SARIF objects that can
128 : : contain property bags (as per SARIF v2.1.0 section 3.8.1, which has:
129 : : "In addition to those properties that are explicitly documented, every
130 : : object defined in this document MAY contain a property named properties
131 : : whose value is a property bag.") */
132 : :
133 : 10055 : class sarif_object : public json::object
134 : : {
135 : : public:
136 : : sarif_property_bag &get_or_create_properties ();
137 : : };
138 : :
139 : : #endif /* ! GCC_DIAGNOSTIC_FORMAT_SARIF_H */
|