20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/ConvertUTF.h"
26#include "llvm/Support/JSON.h"
27#include "llvm/Support/Path.h"
61 if (llvm::isAlnum(
C) ||
62 StringRef::npos != StringRef(
"-._~:@!$&'()*+,;=").find(
C))
63 return std::string(&
C, 1);
64 return "%" + llvm::toHex(StringRef(&
C, 1));
77 StringRef Root = sys::path::root_name(
Filename);
78 if (Root.startswith(
"//")) {
80 Ret += Root.drop_front(2).str();
81 }
else if (!Root.empty()) {
83 Ret += Twine(
"/" + Root).str();
87 assert(
Iter != End &&
"Expected there to be a non-root path component.");
90 for (StringRef Component : llvm::make_range(++
Iter, End)) {
94 if (Component ==
"\\")
102 for (
char C : Component) {
107 return std::string(Ret);
120 unsigned int TokenLen = 0) {
121 assert(!Loc.
isInvalid() &&
"invalid Loc when adjusting column position");
124 std::optional<MemoryBufferRef> Buf =
126 assert(Buf &&
"got an invalid buffer for the location's file");
127 assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) &&
128 "token extends past end of buffer?");
133 unsigned int Ret = 1;
134 while (Off < (LocInfo.second + TokenLen)) {
135 Off += getNumBytesForUTF8(Buf->getBuffer()[Off]);
147 return json::Object{{
"text",
Text.str()}};
156 json::Object Region{{
"startLine", BeginCharLoc.getExpansionLineNumber()},
159 if (BeginCharLoc == EndCharLoc) {
162 Region[
"endLine"] = EndCharLoc.getExpansionLineNumber();
169 StringRef Message =
"") {
170 json::Object Ret{{
"physicalLocation", std::move(PhysicalLocation)}};
171 if (!Message.empty())
178 case ThreadFlowImportance::Important:
180 case ThreadFlowImportance::Essential:
182 case ThreadFlowImportance::Unimportant:
183 return "unimportant";
185 llvm_unreachable(
"Fully covered switch is not so fully covered");
190 case SarifResultLevel::None:
192 case SarifResultLevel::Note:
194 case SarifResultLevel::Warning:
196 case SarifResultLevel::Error:
199 llvm_unreachable(
"Potentially un-handled SarifResultLevel. "
200 "Is the switch not fully covered?");
206 return json::Object{{
"location", std::move(Location)},
214 "Cannot create a physicalLocation from invalid SourceRange!");
216 "Cannot create a physicalLocation from a token range!");
219 assert(FE &&
"Diagnostic does not exist within a valid file!");
222 auto I = CurrentArtifacts.find(FileURI);
224 if (I == CurrentArtifacts.end()) {
225 uint32_t Idx =
static_cast<uint32_t
>(CurrentArtifacts.size());
227 SarifArtifactLocation::create(FileURI).
setIndex(Idx);
228 const SarifArtifact &Artifact = SarifArtifact::create(Location)
232 auto StatusIter = CurrentArtifacts.insert({FileURI, Artifact});
235 if (StatusIter.second)
236 I = StatusIter.first;
238 assert(I != CurrentArtifacts.end() &&
"Failed to insert new artifact");
240 json::Object ArtifactLocationObject{{
"uri", Location.URI}};
241 if (Location.Index.has_value())
242 ArtifactLocationObject[
"index"] = *Location.Index;
243 return json::Object{{{
"artifactLocation", std::move(ArtifactLocationObject)},
247json::Object &SarifDocumentWriter::getCurrentTool() {
248 assert(!Closed &&
"SARIF Document is closed. "
249 "Need to call createRun() before using getcurrentTool!");
253 assert(!Runs.empty() &&
"There are no runs associated with the document!");
255 return *Runs.back().getAsObject()->get(
"tool")->getAsObject();
258void SarifDocumentWriter::reset() {
259 CurrentRules.clear();
260 CurrentArtifacts.clear();
272 assert(!Runs.empty() &&
"There are no runs associated with the document!");
275 json::Object &Tool = getCurrentTool();
277 for (
const SarifRule &R : CurrentRules) {
279 {
"enabled", R.DefaultConfiguration.Enabled},
281 {
"rank", R.DefaultConfiguration.Rank}};
285 {
"fullDescription", json::Object{{
"text", R.Description}}},
286 {
"defaultConfiguration", std::move(Config)}};
287 if (!R.HelpURI.empty())
288 Rule[
"helpUri"] = R.HelpURI;
289 Rules.emplace_back(std::move(Rule));
291 json::Object &Driver = *Tool.getObject(
"driver");
292 Driver[
"rules"] = std::move(Rules);
295 json::Object &Run = getCurrentRun();
296 json::Array *Artifacts = Run.getArray(
"artifacts");
298 for (
const auto &[K,
V] : CurrentArtifacts)
299 Vec.emplace_back(K,
V);
300 llvm::sort(Vec, llvm::less_first());
301 for (
const auto &[_, A] : Vec) {
302 json::Object Loc{{
"uri", A.Location.URI}};
303 if (A.Location.Index.has_value()) {
304 Loc[
"index"] =
static_cast<int64_t
>(*A.Location.Index);
306 json::Object Artifact;
307 Artifact[
"location"] = std::move(Loc);
308 if (A.Length.has_value())
309 Artifact[
"length"] =
static_cast<int64_t
>(*A.Length);
310 if (!A.Roles.empty())
311 Artifact[
"roles"] = json::Array(A.Roles);
312 if (!A.MimeType.empty())
313 Artifact[
"mimeType"] = A.MimeType;
314 if (A.Offset.has_value())
315 Artifact[
"offset"] = *A.Offset;
316 Artifacts->push_back(json::Value(std::move(Artifact)));
328 json::Object Ret{{
"locations", json::Array{}}};
331 json::Object PLoc = createPhysicalLocation(
ThreadFlow.Range);
336 Ret[
"locations"] = std::move(Locs);
337 return json::Array{std::move(Ret)};
342 return json::Object{{
"threadFlows", createThreadFlows(ThreadFlows)}};
346 StringRef LongToolName,
347 StringRef ToolVersion) {
356 json::Object{{
"name", ShortToolName},
357 {
"fullName", LongToolName},
358 {
"language",
"en-US"},
359 {
"version", ToolVersion},
361 "https://clang.llvm.org/docs/UsersManual.html"}}}};
362 json::Object TheRun{{
"tool", std::move(Tool)},
365 {
"columnKind",
"unicodeCodePoints"}};
366 Runs.emplace_back(std::move(TheRun));
369json::Object &SarifDocumentWriter::getCurrentRun() {
371 "SARIF Document is closed. "
372 "Can only getCurrentRun() if document is opened via createRun(), "
373 "create a run first");
377 assert(!Runs.empty() &&
"There are no runs associated with the document!");
378 return *Runs.back().getAsObject();
382 size_t Ret = CurrentRules.size();
383 CurrentRules.emplace_back(Rule);
388 size_t RuleIdx =
Result.RuleIdx;
389 assert(RuleIdx < CurrentRules.size() &&
390 "Trying to reference a rule that doesn't exist");
391 const SarifRule &Rule = CurrentRules[RuleIdx];
392 assert(Rule.DefaultConfiguration.Enabled &&
393 "Cannot add a result referencing a disabled Rule");
395 {
"ruleIndex",
static_cast<int64_t
>(RuleIdx)},
396 {
"ruleId", Rule.Id}};
397 if (!
Result.Locations.empty()) {
399 for (
auto &Range :
Result.Locations) {
402 Ret[
"locations"] = std::move(Locs);
404 if (!
Result.ThreadFlows.empty())
405 Ret[
"codeFlows"] = json::Array{createCodeFlow(
Result.ThreadFlows)};
408 Result.LevelOverride.value_or(Rule.DefaultConfiguration.Level));
410 json::Object &Run = getCurrentRun();
411 json::Array *Results = Run.getArray(
"results");
412 Results->emplace_back(std::move(Ret));
420 {
"$schema", SchemaURI},
421 {
"version", SchemaVersion},
424 Doc[
"runs"] = json::Array(Runs);
static StringRef importanceToStr(ThreadFlowImportance I)
static StringRef getFileName(FileEntryRef FE)
json::Object createMessage(StringRef Text)
static unsigned int adjustColumnPos(FullSourceLoc Loc, unsigned int TokenLen=0)
Calculate the column position expressed in the number of UTF-8 code points from column start to the s...
static std::string percentEncodeURICharacter(char C)
static json::Object createThreadFlowLocation(json::Object &&Location, const ThreadFlowImportance &Importance)
static json::Object createLocation(json::Object &&PhysicalLocation, StringRef Message="")
static json::Object createTextRegion(const SourceManager &SM, const CharSourceRange &R)
static std::string fileNameToURI(StringRef Filename)
static StringRef resultLevelToStr(SarifResultLevel R)
Defines clang::SarifDocumentWriter, clang::SarifRule, clang::SarifResult.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Represents a character-granular source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
const FileEntry & getFileEntry() const
StringRef getName() const
The name of this FileEntry.
StringRef tryGetRealPathName() const
A SourceLocation and its associated SourceManager.
std::pair< FileID, unsigned > getDecomposedExpansionLoc() const
Decompose the underlying SourceLocation into a raw (FileID + Offset) pair, after walking through all ...
unsigned getExpansionColumnNumber(bool *Invalid=nullptr) const
const SourceManager & getManager() const
void createRun(const llvm::StringRef ShortToolName, const llvm::StringRef LongToolName, const llvm::StringRef ToolVersion=CLANG_VERSION_STRING)
Create a new run with which any upcoming analysis will be associated.
size_t createRule(const SarifRule &Rule)
Associate the given rule with the current run.
llvm::json::Object createDocument()
Return the SARIF document in its current state.
void endRun()
If there is a current run, end it.
void appendResult(const SarifResult &SarifResult)
Append a new result to the currently in-flight run.
A SARIF result (also called a "reporting item") is a unit of output produced when one of the tool's r...
A SARIF rule (reportingDescriptor object) contains information that describes a reporting item genera...
This class handles loading and caching of source files into memory.
std::optional< llvm::MemoryBufferRef > getBufferOrNone(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
A thread flow is a sequence of code locations that specify a possible path through a single thread of...
SarifArtifactLocation setIndex(uint32_t Idx)
Since every clang artifact MUST have a location (there being no nested artifacts),...
SarifArtifact setMimeType(llvm::StringRef ArtifactMimeType)
SarifArtifact setRoles(std::initializer_list< llvm::StringRef > ArtifactRoles)
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
SarifResultLevel
The level of severity associated with a SarifResult.
@ C
Languages that the frontend can parse and compile.
@ Result
The result type of a method or function.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
YAML serialization mapping.