/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Sarif.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- clang/Basic/Sarif.cpp - SarifDocumentWriter class definition ------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// |
9 | | /// \file |
10 | | /// This file contains the declaration of the SARIFDocumentWriter class, and |
11 | | /// associated builders such as: |
12 | | /// - \ref SarifArtifact |
13 | | /// - \ref SarifArtifactLocation |
14 | | /// - \ref SarifRule |
15 | | /// - \ref SarifResult |
16 | | //===----------------------------------------------------------------------===// |
17 | | #include "clang/Basic/Sarif.h" |
18 | | #include "clang/Basic/SourceLocation.h" |
19 | | #include "clang/Basic/SourceManager.h" |
20 | | #include "llvm/ADT/ArrayRef.h" |
21 | | #include "llvm/ADT/STLExtras.h" |
22 | | #include "llvm/ADT/StringMap.h" |
23 | | #include "llvm/ADT/StringRef.h" |
24 | | #include "llvm/Support/ConvertUTF.h" |
25 | | #include "llvm/Support/JSON.h" |
26 | | #include "llvm/Support/Path.h" |
27 | | |
28 | | #include <optional> |
29 | | #include <string> |
30 | | #include <utility> |
31 | | |
32 | | using namespace clang; |
33 | | using namespace llvm; |
34 | | |
35 | | using clang::detail::SarifArtifact; |
36 | | using clang::detail::SarifArtifactLocation; |
37 | | |
38 | 38 | static StringRef getFileName(const FileEntry &FE) { |
39 | 38 | StringRef Filename = FE.tryGetRealPathName(); |
40 | 38 | if (Filename.empty()) |
41 | 5 | Filename = FE.getName(); |
42 | 38 | return Filename; |
43 | 38 | } |
44 | | /// \name URI |
45 | | /// @{ |
46 | | |
47 | | /// \internal |
48 | | /// \brief |
49 | | /// Return the RFC3986 encoding of the input character. |
50 | | /// |
51 | | /// \param C Character to encode to RFC3986. |
52 | | /// |
53 | | /// \return The RFC3986 representation of \c C. |
54 | 3.41k | static std::string percentEncodeURICharacter(char C) { |
55 | | // RFC 3986 claims alpha, numeric, and this handful of |
56 | | // characters are not reserved for the path component and |
57 | | // should be written out directly. Otherwise, percent |
58 | | // encode the character and write that out instead of the |
59 | | // reserved character. |
60 | 3.41k | if (llvm::isAlnum(C) || |
61 | 3.41k | StringRef::npos != StringRef("-._~:@!$&'()*+,;=").find(C)154 ) |
62 | 3.41k | return std::string(&C, 1); |
63 | 0 | return "%" + llvm::toHex(StringRef(&C, 1)); |
64 | 3.41k | } |
65 | | |
66 | | /// \internal |
67 | | /// \brief Return a URI representing the given file name. |
68 | | /// |
69 | | /// \param Filename The filename to be represented as URI. |
70 | | /// |
71 | | /// \return RFC3986 URI representing the input file name. |
72 | 38 | static std::string fileNameToURI(StringRef Filename) { |
73 | 38 | SmallString<32> Ret = StringRef("file://"); |
74 | | |
75 | | // Get the root name to see if it has a URI authority. |
76 | 38 | StringRef Root = sys::path::root_name(Filename); |
77 | 38 | if (Root.startswith("//")) { |
78 | | // There is an authority, so add it to the URI. |
79 | 0 | Ret += Root.drop_front(2).str(); |
80 | 38 | } else if (!Root.empty()) { |
81 | | // There is no authority, so end the component and add the root to the URI. |
82 | 0 | Ret += Twine("/" + Root).str(); |
83 | 0 | } |
84 | | |
85 | 38 | auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename); |
86 | 38 | assert(Iter != End && "Expected there to be a non-root path component."); |
87 | | // Add the rest of the path components, encoding any reserved characters; |
88 | | // we skip past the first path component, as it was handled it above. |
89 | 357 | std::for_each(++Iter, End, [&Ret](StringRef Component) 38 { |
90 | | // For reasons unknown to me, we may get a backslash with Windows native |
91 | | // paths for the initial backslash following the drive component, which |
92 | | // we need to ignore as a URI path part. |
93 | 357 | if (Component == "\\") |
94 | 0 | return; |
95 | | |
96 | | // Add the separator between the previous path part and the one being |
97 | | // currently processed. |
98 | 357 | Ret += "/"; |
99 | | |
100 | | // URI encode the part. |
101 | 3.41k | for (char C : Component) { |
102 | 3.41k | Ret += percentEncodeURICharacter(C); |
103 | 3.41k | } |
104 | 357 | }); |
105 | | |
106 | 38 | return std::string(Ret); |
107 | 38 | } |
108 | | /// @} |
109 | | |
110 | | /// \brief Calculate the column position expressed in the number of UTF-8 code |
111 | | /// points from column start to the source location |
112 | | /// |
113 | | /// \param Loc The source location whose column needs to be calculated. |
114 | | /// \param TokenLen Optional hint for when the token is multiple bytes long. |
115 | | /// |
116 | | /// \return The column number as a UTF-8 aware byte offset from column start to |
117 | | /// the effective source location. |
118 | | static unsigned int adjustColumnPos(FullSourceLoc Loc, |
119 | 76 | unsigned int TokenLen = 0) { |
120 | 76 | assert(!Loc.isInvalid() && "invalid Loc when adjusting column position"); |
121 | | |
122 | 76 | std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedExpansionLoc(); |
123 | 76 | std::optional<MemoryBufferRef> Buf = |
124 | 76 | Loc.getManager().getBufferOrNone(LocInfo.first); |
125 | 76 | assert(Buf && "got an invalid buffer for the location's file"); |
126 | 76 | assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) && |
127 | 76 | "token extends past end of buffer?"); |
128 | | |
129 | | // Adjust the offset to be the start of the line, since we'll be counting |
130 | | // Unicode characters from there until our column offset. |
131 | 76 | unsigned int Off = LocInfo.second - (Loc.getExpansionColumnNumber() - 1); |
132 | 76 | unsigned int Ret = 1; |
133 | 703 | while (Off < (LocInfo.second + TokenLen)) { |
134 | 627 | Off += getNumBytesForUTF8(Buf->getBuffer()[Off]); |
135 | 627 | Ret++; |
136 | 627 | } |
137 | | |
138 | 76 | return Ret; |
139 | 76 | } |
140 | | |
141 | | /// \name SARIF Utilities |
142 | | /// @{ |
143 | | |
144 | | /// \internal |
145 | 39 | json::Object createMessage(StringRef Text) { |
146 | 39 | return json::Object{{"text", Text.str()}}; |
147 | 39 | } |
148 | | |
149 | | /// \internal |
150 | | /// \pre CharSourceRange must be a token range |
151 | | static json::Object createTextRegion(const SourceManager &SM, |
152 | 38 | const CharSourceRange &R) { |
153 | 38 | FullSourceLoc BeginCharLoc{R.getBegin(), SM}; |
154 | 38 | FullSourceLoc EndCharLoc{R.getEnd(), SM}; |
155 | 38 | json::Object Region{{"startLine", BeginCharLoc.getExpansionLineNumber()}, |
156 | 38 | {"startColumn", adjustColumnPos(BeginCharLoc)}}; |
157 | | |
158 | 38 | if (BeginCharLoc == EndCharLoc) { |
159 | 18 | Region["endColumn"] = adjustColumnPos(BeginCharLoc); |
160 | 20 | } else { |
161 | 20 | Region["endLine"] = EndCharLoc.getExpansionLineNumber(); |
162 | 20 | Region["endColumn"] = adjustColumnPos(EndCharLoc); |
163 | 20 | } |
164 | 38 | return Region; |
165 | 38 | } |
166 | | |
167 | | static json::Object createLocation(json::Object &&PhysicalLocation, |
168 | 38 | StringRef Message = "") { |
169 | 38 | json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}}; |
170 | 38 | if (!Message.empty()) |
171 | 19 | Ret.insert({"message", createMessage(Message)}); |
172 | 38 | return Ret; |
173 | 38 | } |
174 | | |
175 | 19 | static StringRef importanceToStr(ThreadFlowImportance I) { |
176 | 19 | switch (I) { |
177 | 3 | case ThreadFlowImportance::Important: |
178 | 3 | return "important"; |
179 | 13 | case ThreadFlowImportance::Essential: |
180 | 13 | return "essential"; |
181 | 3 | case ThreadFlowImportance::Unimportant: |
182 | 3 | return "unimportant"; |
183 | 19 | } |
184 | 0 | llvm_unreachable("Fully covered switch is not so fully covered"); |
185 | 0 | } |
186 | | |
187 | 39 | static StringRef resultLevelToStr(SarifResultLevel R) { |
188 | 39 | switch (R) { |
189 | 0 | case SarifResultLevel::None: |
190 | 0 | return "none"; |
191 | 2 | case SarifResultLevel::Note: |
192 | 2 | return "note"; |
193 | 22 | case SarifResultLevel::Warning: |
194 | 22 | return "warning"; |
195 | 15 | case SarifResultLevel::Error: |
196 | 15 | return "error"; |
197 | 39 | } |
198 | 0 | llvm_unreachable("Potentially un-handled SarifResultLevel. " |
199 | 0 | "Is the switch not fully covered?"); |
200 | 0 | } |
201 | | |
202 | | static json::Object |
203 | | createThreadFlowLocation(json::Object &&Location, |
204 | 19 | const ThreadFlowImportance &Importance) { |
205 | 19 | return json::Object{{"location", std::move(Location)}, |
206 | 19 | {"importance", importanceToStr(Importance)}}; |
207 | 19 | } |
208 | | /// @} |
209 | | |
210 | | json::Object |
211 | 38 | SarifDocumentWriter::createPhysicalLocation(const CharSourceRange &R) { |
212 | 38 | assert(R.isValid() && |
213 | 38 | "Cannot create a physicalLocation from invalid SourceRange!"); |
214 | 38 | assert(R.isCharRange() && |
215 | 38 | "Cannot create a physicalLocation from a token range!"); |
216 | 38 | FullSourceLoc Start{R.getBegin(), SourceMgr}; |
217 | 38 | const FileEntry *FE = Start.getExpansionLoc().getFileEntry(); |
218 | 38 | assert(FE != nullptr && "Diagnostic does not exist within a valid file!"); |
219 | | |
220 | 38 | const std::string &FileURI = fileNameToURI(getFileName(*FE)); |
221 | 38 | auto I = CurrentArtifacts.find(FileURI); |
222 | | |
223 | 38 | if (I == CurrentArtifacts.end()) { |
224 | 8 | uint32_t Idx = static_cast<uint32_t>(CurrentArtifacts.size()); |
225 | 8 | const SarifArtifactLocation &Location = |
226 | 8 | SarifArtifactLocation::create(FileURI).setIndex(Idx); |
227 | 8 | const SarifArtifact &Artifact = SarifArtifact::create(Location) |
228 | 8 | .setRoles({"resultFile"}) |
229 | 8 | .setLength(FE->getSize()) |
230 | 8 | .setMimeType("text/plain"); |
231 | 8 | auto StatusIter = CurrentArtifacts.insert({FileURI, Artifact}); |
232 | | // If inserted, ensure the original iterator points to the newly inserted |
233 | | // element, so it can be used downstream. |
234 | 8 | if (StatusIter.second) |
235 | 8 | I = StatusIter.first; |
236 | 8 | } |
237 | 38 | assert(I != CurrentArtifacts.end() && "Failed to insert new artifact"); |
238 | 38 | const SarifArtifactLocation &Location = I->second.Location; |
239 | 38 | json::Object ArtifactLocationObject{{"uri", Location.URI}}; |
240 | 38 | if (Location.Index.has_value()) |
241 | 38 | ArtifactLocationObject["index"] = *Location.Index; |
242 | 38 | return json::Object{{{"artifactLocation", std::move(ArtifactLocationObject)}, |
243 | 38 | {"region", createTextRegion(SourceMgr, R)}}}; |
244 | 38 | } |
245 | | |
246 | 9 | json::Object &SarifDocumentWriter::getCurrentTool() { |
247 | 9 | assert(!Closed && "SARIF Document is closed. " |
248 | 9 | "Need to call createRun() before using getcurrentTool!"); |
249 | | |
250 | | // Since Closed = false here, expect there to be at least 1 Run, anything |
251 | | // else is an invalid state. |
252 | 9 | assert(!Runs.empty() && "There are no runs associated with the document!"); |
253 | | |
254 | 9 | return *Runs.back().getAsObject()->get("tool")->getAsObject(); |
255 | 9 | } |
256 | | |
257 | 21 | void SarifDocumentWriter::reset() { |
258 | 21 | CurrentRules.clear(); |
259 | 21 | CurrentArtifacts.clear(); |
260 | 21 | } |
261 | | |
262 | 21 | void SarifDocumentWriter::endRun() { |
263 | | // Exit early if trying to close a closed Document. |
264 | 21 | if (Closed) { |
265 | 12 | reset(); |
266 | 12 | return; |
267 | 12 | } |
268 | | |
269 | | // Since Closed = false here, expect there to be at least 1 Run, anything |
270 | | // else is an invalid state. |
271 | 9 | assert(!Runs.empty() && "There are no runs associated with the document!"); |
272 | | |
273 | | // Flush all the rules. |
274 | 9 | json::Object &Tool = getCurrentTool(); |
275 | 9 | json::Array Rules; |
276 | 19 | for (const SarifRule &R : CurrentRules) { |
277 | 19 | json::Object Config{ |
278 | 19 | {"enabled", R.DefaultConfiguration.Enabled}, |
279 | 19 | {"level", resultLevelToStr(R.DefaultConfiguration.Level)}, |
280 | 19 | {"rank", R.DefaultConfiguration.Rank}}; |
281 | 19 | json::Object Rule{ |
282 | 19 | {"name", R.Name}, |
283 | 19 | {"id", R.Id}, |
284 | 19 | {"fullDescription", json::Object{{"text", R.Description}}}, |
285 | 19 | {"defaultConfiguration", std::move(Config)}}; |
286 | 19 | if (!R.HelpURI.empty()) |
287 | 3 | Rule["helpUri"] = R.HelpURI; |
288 | 19 | Rules.emplace_back(std::move(Rule)); |
289 | 19 | } |
290 | 9 | json::Object &Driver = *Tool.getObject("driver"); |
291 | 9 | Driver["rules"] = std::move(Rules); |
292 | | |
293 | | // Flush all the artifacts. |
294 | 9 | json::Object &Run = getCurrentRun(); |
295 | 9 | json::Array *Artifacts = Run.getArray("artifacts"); |
296 | 9 | for (const auto &Pair : CurrentArtifacts) { |
297 | 8 | const SarifArtifact &A = Pair.getValue(); |
298 | 8 | json::Object Loc{{"uri", A.Location.URI}}; |
299 | 8 | if (A.Location.Index.has_value()) { |
300 | 8 | Loc["index"] = static_cast<int64_t>(*A.Location.Index); |
301 | 8 | } |
302 | 8 | json::Object Artifact; |
303 | 8 | Artifact["location"] = std::move(Loc); |
304 | 8 | if (A.Length.has_value()) |
305 | 8 | Artifact["length"] = static_cast<int64_t>(*A.Length); |
306 | 8 | if (!A.Roles.empty()) |
307 | 8 | Artifact["roles"] = json::Array(A.Roles); |
308 | 8 | if (!A.MimeType.empty()) |
309 | 8 | Artifact["mimeType"] = A.MimeType; |
310 | 8 | if (A.Offset.has_value()) |
311 | 0 | Artifact["offset"] = *A.Offset; |
312 | 8 | Artifacts->push_back(json::Value(std::move(Artifact))); |
313 | 8 | } |
314 | | |
315 | | // Clear, reset temporaries before next run. |
316 | 9 | reset(); |
317 | | |
318 | | // Mark the document as closed. |
319 | 9 | Closed = true; |
320 | 9 | } |
321 | | |
322 | | json::Array |
323 | 7 | SarifDocumentWriter::createThreadFlows(ArrayRef<ThreadFlow> ThreadFlows) { |
324 | 7 | json::Object Ret{{"locations", json::Array{}}}; |
325 | 7 | json::Array Locs; |
326 | 19 | for (const auto &ThreadFlow : ThreadFlows) { |
327 | 19 | json::Object PLoc = createPhysicalLocation(ThreadFlow.Range); |
328 | 19 | json::Object Loc = createLocation(std::move(PLoc), ThreadFlow.Message); |
329 | 19 | Locs.emplace_back( |
330 | 19 | createThreadFlowLocation(std::move(Loc), ThreadFlow.Importance)); |
331 | 19 | } |
332 | 7 | Ret["locations"] = std::move(Locs); |
333 | 7 | return json::Array{std::move(Ret)}; |
334 | 7 | } |
335 | | |
336 | | json::Object |
337 | 7 | SarifDocumentWriter::createCodeFlow(ArrayRef<ThreadFlow> ThreadFlows) { |
338 | 7 | return json::Object{{"threadFlows", createThreadFlows(ThreadFlows)}}; |
339 | 7 | } |
340 | | |
341 | | void SarifDocumentWriter::createRun(StringRef ShortToolName, |
342 | | StringRef LongToolName, |
343 | 9 | StringRef ToolVersion) { |
344 | | // Clear resources associated with a previous run. |
345 | 9 | endRun(); |
346 | | |
347 | | // Signify a new run has begun. |
348 | 9 | Closed = false; |
349 | | |
350 | 9 | json::Object Tool{ |
351 | 9 | {"driver", |
352 | 9 | json::Object{{"name", ShortToolName}, |
353 | 9 | {"fullName", LongToolName}, |
354 | 9 | {"language", "en-US"}, |
355 | 9 | {"version", ToolVersion}, |
356 | 9 | {"informationUri", |
357 | 9 | "https://clang.llvm.org/docs/UsersManual.html"}}}}; |
358 | 9 | json::Object TheRun{{"tool", std::move(Tool)}, |
359 | 9 | {"results", {}}, |
360 | 9 | {"artifacts", {}}, |
361 | 9 | {"columnKind", "unicodeCodePoints"}}; |
362 | 9 | Runs.emplace_back(std::move(TheRun)); |
363 | 9 | } |
364 | | |
365 | 29 | json::Object &SarifDocumentWriter::getCurrentRun() { |
366 | 29 | assert(!Closed && |
367 | 29 | "SARIF Document is closed. " |
368 | 29 | "Can only getCurrentRun() if document is opened via createRun(), " |
369 | 29 | "create a run first"); |
370 | | |
371 | | // Since Closed = false here, expect there to be at least 1 Run, anything |
372 | | // else is an invalid state. |
373 | 29 | assert(!Runs.empty() && "There are no runs associated with the document!"); |
374 | 29 | return *Runs.back().getAsObject(); |
375 | 29 | } |
376 | | |
377 | 21 | size_t SarifDocumentWriter::createRule(const SarifRule &Rule) { |
378 | 21 | size_t Ret = CurrentRules.size(); |
379 | 21 | CurrentRules.emplace_back(Rule); |
380 | 21 | return Ret; |
381 | 21 | } |
382 | | |
383 | 20 | void SarifDocumentWriter::appendResult(const SarifResult &Result) { |
384 | 20 | size_t RuleIdx = Result.RuleIdx; |
385 | 20 | assert(RuleIdx < CurrentRules.size() && |
386 | 20 | "Trying to reference a rule that doesn't exist"); |
387 | 20 | const SarifRule &Rule = CurrentRules[RuleIdx]; |
388 | 20 | assert(Rule.DefaultConfiguration.Enabled && |
389 | 20 | "Cannot add a result referencing a disabled Rule"); |
390 | 20 | json::Object Ret{{"message", createMessage(Result.DiagnosticMessage)}, |
391 | 20 | {"ruleIndex", static_cast<int64_t>(RuleIdx)}, |
392 | 20 | {"ruleId", Rule.Id}}; |
393 | 20 | if (!Result.Locations.empty()) { |
394 | 17 | json::Array Locs; |
395 | 19 | for (auto &Range : Result.Locations) { |
396 | 19 | Locs.emplace_back(createLocation(createPhysicalLocation(Range))); |
397 | 19 | } |
398 | 17 | Ret["locations"] = std::move(Locs); |
399 | 17 | } |
400 | 20 | if (!Result.ThreadFlows.empty()) |
401 | 7 | Ret["codeFlows"] = json::Array{createCodeFlow(Result.ThreadFlows)}; |
402 | | |
403 | 20 | Ret["level"] = resultLevelToStr( |
404 | 20 | Result.LevelOverride.value_or(Rule.DefaultConfiguration.Level)); |
405 | | |
406 | 20 | json::Object &Run = getCurrentRun(); |
407 | 20 | json::Array *Results = Run.getArray("results"); |
408 | 20 | Results->emplace_back(std::move(Ret)); |
409 | 20 | } |
410 | | |
411 | 10 | json::Object SarifDocumentWriter::createDocument() { |
412 | | // Flush all temporaries to their destinations if needed. |
413 | 10 | endRun(); |
414 | | |
415 | 10 | json::Object Doc{ |
416 | 10 | {"$schema", SchemaURI}, |
417 | 10 | {"version", SchemaVersion}, |
418 | 10 | }; |
419 | 10 | if (!Runs.empty()) |
420 | 9 | Doc["runs"] = json::Array(Runs); |
421 | 10 | return Doc; |
422 | 10 | } |