/src/CMake/Source/cmSarif.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmSarif.h" |
4 | | |
5 | | #include <memory> |
6 | | #include <utility> |
7 | | |
8 | | #include <cm3p/json/value.h> |
9 | | #include <cm3p/json/writer.h> |
10 | | |
11 | | #include "cmsys/FStream.hxx" |
12 | | |
13 | | namespace cmSarif { |
14 | | |
15 | | constexpr char const* SpecVersion = "2.1.0"; |
16 | | constexpr char const* SpecSchema = |
17 | | "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/" |
18 | | "sarif-schema-2.1.0.json"; |
19 | | |
20 | | Json::Value GetJson(ResultSeverityLevel level) |
21 | 0 | { |
22 | 0 | switch (level) { |
23 | 0 | case ResultSeverityLevel::Warning: |
24 | 0 | return "warning"; |
25 | 0 | case ResultSeverityLevel::Error: |
26 | 0 | return "error"; |
27 | 0 | case ResultSeverityLevel::Note: |
28 | 0 | return "note"; |
29 | 0 | case ResultSeverityLevel::None: |
30 | 0 | default: |
31 | 0 | return "none"; |
32 | 0 | } |
33 | 0 | } |
34 | | |
35 | | Json::Value GetJson(Message const& message) |
36 | 0 | { |
37 | 0 | Json::Value obj(Json::objectValue); |
38 | 0 | obj["text"] = message.Text; |
39 | 0 | return obj; |
40 | 0 | } |
41 | | |
42 | | Json::Value GetJson(ArtifactLocation const& artifactLocation) |
43 | 0 | { |
44 | 0 | Json::Value obj(Json::objectValue); |
45 | 0 | obj["uri"] = artifactLocation.Uri; |
46 | 0 | if (!artifactLocation.UriBaseId.empty()) { |
47 | 0 | obj["uriBaseId"] = artifactLocation.UriBaseId; |
48 | 0 | } |
49 | 0 | return obj; |
50 | 0 | } |
51 | | |
52 | | Json::Value GetJson(Region region) |
53 | 0 | { |
54 | 0 | Json::Value obj(Json::objectValue); |
55 | 0 | obj["startLine"] = Json::Int64(region.StartLine); |
56 | 0 | return obj; |
57 | 0 | } |
58 | | |
59 | | Json::Value GetJson(PhysicalLocation const& physicalLocation) |
60 | 0 | { |
61 | 0 | Json::Value obj(Json::objectValue); |
62 | 0 | obj["artifactLocation"] = cmSarif::GetJson(physicalLocation.Artifact); |
63 | 0 | if (physicalLocation.ArtifactRegion) { |
64 | 0 | obj["region"] = cmSarif::GetJson(*physicalLocation.ArtifactRegion); |
65 | 0 | } |
66 | 0 | return obj; |
67 | 0 | } |
68 | | |
69 | | Json::Value GetJson(LogicalLocation const& logicalLocation) |
70 | 0 | { |
71 | 0 | Json::Value obj(Json::objectValue); |
72 | 0 | obj["name"] = logicalLocation.Name; |
73 | 0 | if (!logicalLocation.Kind.empty()) { |
74 | 0 | obj["kind"] = std::string(logicalLocation.Kind); |
75 | 0 | } |
76 | 0 | return obj; |
77 | 0 | } |
78 | | |
79 | | Json::Value GetJson(Location const& location) |
80 | 0 | { |
81 | 0 | Json::Value obj(Json::objectValue); |
82 | 0 | obj["physicalLocation"] = cmSarif::GetJson(location.Physical); |
83 | 0 | if (!location.Logical.empty()) { |
84 | 0 | Json::Value logical(Json::arrayValue); |
85 | 0 | for (auto const& loc : location.Logical) { |
86 | 0 | logical.append(cmSarif::GetJson(loc)); |
87 | 0 | } |
88 | 0 | obj["logicalLocations"] = logical; |
89 | 0 | } |
90 | 0 | if (location.Message) { |
91 | 0 | obj["message"] = cmSarif::GetJson(*location.Message); |
92 | 0 | } |
93 | |
|
94 | 0 | return obj; |
95 | 0 | } |
96 | | |
97 | | Json::Value GetJson(StackFrame const& frame) |
98 | 0 | { |
99 | 0 | Json::Value frameJson(Json::objectValue); |
100 | 0 | if (frame.Location) { |
101 | 0 | frameJson["location"] = cmSarif::GetJson(*frame.Location); |
102 | 0 | } |
103 | 0 | return frameJson; |
104 | 0 | } |
105 | | |
106 | | Json::Value GetJson(Stack const& stack) |
107 | 0 | { |
108 | 0 | Json::Value stackJson(Json::objectValue); |
109 | 0 | Json::Value frames(Json::arrayValue); |
110 | 0 | for (auto const& frame : stack.Frames) { |
111 | 0 | frames.append(cmSarif::GetJson(frame)); |
112 | 0 | } |
113 | 0 | stackJson["frames"] = frames; |
114 | 0 | return stackJson; |
115 | 0 | } |
116 | | |
117 | | Json::Value GetJson(ReportingDescriptor const& reportingDescriptor) |
118 | 0 | { |
119 | 0 | Json::Value rd(Json::objectValue); |
120 | 0 | rd["id"] = reportingDescriptor.Id; |
121 | 0 | if (reportingDescriptor.Name) { |
122 | 0 | rd["name"] = *reportingDescriptor.Name; |
123 | 0 | } |
124 | 0 | return rd; |
125 | 0 | } |
126 | | |
127 | | Json::Value GetJson(Result const& result) |
128 | 0 | { |
129 | 0 | Json::Value resultJson(Json::objectValue); |
130 | |
|
131 | 0 | if (result.Message) { |
132 | 0 | resultJson["message"] = cmSarif::GetJson(*result.Message); |
133 | 0 | } |
134 | |
|
135 | 0 | if (result.Level) { |
136 | 0 | resultJson["level"] = cmSarif::GetJson(*result.Level); |
137 | 0 | } |
138 | |
|
139 | 0 | if (result.RuleId) { |
140 | 0 | resultJson["ruleId"] = *result.RuleId; |
141 | 0 | } |
142 | 0 | if (result.RuleIndex) { |
143 | 0 | resultJson["ruleIndex"] = Json::UInt64(*result.RuleIndex); |
144 | 0 | } |
145 | |
|
146 | 0 | if (result.Location) { |
147 | 0 | resultJson["locations"][0] = cmSarif::GetJson(*result.Location); |
148 | 0 | } |
149 | |
|
150 | 0 | if (!result.Stacks.empty()) { |
151 | 0 | Json::Value stacks(Json::arrayValue); |
152 | 0 | for (auto const& stack : result.Stacks) { |
153 | 0 | stacks.append(cmSarif::GetJson(stack)); |
154 | 0 | } |
155 | 0 | resultJson["stacks"] = stacks; |
156 | 0 | } |
157 | |
|
158 | 0 | return resultJson; |
159 | 0 | } |
160 | | |
161 | | Json::Value GetJson(ToolComponent const& toolComponent) |
162 | 0 | { |
163 | 0 | Json::Value component(Json::objectValue); |
164 | 0 | component["name"] = toolComponent.Name; |
165 | 0 | component["version"] = toolComponent.Version; |
166 | 0 | Json::Value rules(Json::arrayValue); |
167 | 0 | for (auto const& rule : toolComponent.Rules) { |
168 | 0 | rules.append(cmSarif::GetJson(rule)); |
169 | 0 | } |
170 | 0 | component["rules"] = rules; |
171 | 0 | return component; |
172 | 0 | } |
173 | | |
174 | | Json::Value GetJson(Tool const& tool) |
175 | 0 | { |
176 | 0 | Json::Value toolJson(Json::objectValue); |
177 | 0 | toolJson["driver"] = cmSarif::GetJson(tool.Driver); |
178 | 0 | return toolJson; |
179 | 0 | } |
180 | | |
181 | | Json::Value GetJson(Run const& run) |
182 | 0 | { |
183 | 0 | Json::Value runJson(Json::objectValue); |
184 | 0 | runJson["tool"] = cmSarif::GetJson(run.Tool); |
185 | |
|
186 | 0 | if (!run.OriginalUriBaseIds.empty()) { |
187 | 0 | Json::Value uriBaseIds(Json::objectValue); |
188 | 0 | for (auto const& base : run.OriginalUriBaseIds) { |
189 | 0 | uriBaseIds[base.first] = cmSarif::GetJson(base.second); |
190 | 0 | } |
191 | 0 | runJson["originalUriBaseIds"] = uriBaseIds; |
192 | 0 | } |
193 | |
|
194 | 0 | Json::Value results(Json::arrayValue); |
195 | 0 | for (auto const& result : run.Results) { |
196 | 0 | results.append(cmSarif::GetJson(result)); |
197 | 0 | } |
198 | 0 | runJson["results"] = results; |
199 | |
|
200 | 0 | return runJson; |
201 | 0 | } |
202 | | |
203 | | bool WriteLog(std::string const& path, cmSarif::Run const& run) |
204 | 0 | { |
205 | 0 | cmsys::ofstream outputFile(path.c_str()); |
206 | 0 | if (!outputFile.good()) { |
207 | 0 | return false; |
208 | 0 | } |
209 | | |
210 | 0 | Json::Value root(Json::objectValue); |
211 | 0 | root["version"] = SpecVersion; |
212 | 0 | root["$schema"] = SpecSchema; |
213 | 0 | Json::Value runs(Json::arrayValue); |
214 | 0 | runs.append(cmSarif::GetJson(run)); |
215 | 0 | root["runs"] = runs; |
216 | |
|
217 | 0 | Json::StreamWriterBuilder builder; |
218 | 0 | std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter()); |
219 | 0 | writer->write(root, &outputFile); |
220 | 0 | outputFile.close(); |
221 | |
|
222 | 0 | return true; |
223 | 0 | } |
224 | | |
225 | | } // namespace cmSarif |