20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/ConvertUTF.h"
24#include "llvm/Support/JSON.h"
25#include "llvm/Support/Path.h"
59 if (llvm::isAlnum(
C) || StringRef(
"-._~:@!$&'()*+,;=").
contains(
C))
60 return std::string(&
C, 1);
61 return "%" + llvm::toHex(StringRef(&
C, 1));
74 StringRef Root = sys::path::root_name(
Filename);
75 if (Root.starts_with(
"//")) {
77 Ret += Root.drop_front(2).str();
78 }
else if (!Root.empty()) {
80 Ret += Twine(
"/" + Root).str();
84 assert(
Iter != End &&
"Expected there to be a non-root path component.");
87 for (StringRef Component : llvm::make_range(++
Iter, End)) {
91 if (Component ==
"\\")
99 for (
char C : Component) {
104 return std::string(Ret);
117 unsigned int TokenLen = 0) {
118 assert(!
Loc.
isInvalid() &&
"invalid Loc when adjusting column position");
121 std::optional<MemoryBufferRef> Buf =
122 Loc.getManager().getBufferOrNone(LocInfo.first);
123 assert(Buf &&
"got an invalid buffer for the location's file");
124 assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) &&
125 "token extends past end of buffer?");
129 unsigned int Off = LocInfo.second - (
Loc.getExpansionColumnNumber() - 1);
130 unsigned int Ret = 1;
131 while (Off < (LocInfo.second + TokenLen)) {
132 Off += getNumBytesForUTF8(Buf->getBuffer()[Off]);
144 return json::Object{{
"text",
Text.str()}};
153 json::Object Region{{
"startLine", BeginCharLoc.getExpansionLineNumber()},
156 if (BeginCharLoc == EndCharLoc) {
159 Region[
"endLine"] = EndCharLoc.getExpansionLineNumber();
166 StringRef Message =
"") {
167 json::Object Ret{{
"physicalLocation", std::move(PhysicalLocation)}};
168 if (!Message.empty())
175 case ThreadFlowImportance::Important:
177 case ThreadFlowImportance::Essential:
179 case ThreadFlowImportance::Unimportant:
180 return "unimportant";
182 llvm_unreachable(
"Fully covered switch is not so fully covered");
187 case SarifResultLevel::None:
189 case SarifResultLevel::Note:
191 case SarifResultLevel::Warning:
193 case SarifResultLevel::Error:
196 llvm_unreachable(
"Potentially un-handled SarifResultLevel. "
197 "Is the switch not fully covered?");
203 return json::Object{{
"location", std::move(Location)},
211 "Cannot create a physicalLocation from invalid SourceRange!");
213 "Cannot create a physicalLocation from a token range!");
216 assert(FE &&
"Diagnostic does not exist within a valid file!");
219 auto I = CurrentArtifacts.find(FileURI);
221 if (I == CurrentArtifacts.end()) {
224 SarifArtifactLocation::create(FileURI).
setIndex(Idx);
225 const SarifArtifact &Artifact = SarifArtifact::create(Location)
229 auto StatusIter = CurrentArtifacts.insert({FileURI, Artifact});
232 if (StatusIter.second)
233 I = StatusIter.first;
235 assert(I != CurrentArtifacts.end() &&
"Failed to insert new artifact");
237 json::Object ArtifactLocationObject{{
"uri", Location.URI}};
238 if (Location.Index.has_value())
239 ArtifactLocationObject[
"index"] = *Location.Index;
240 return json::Object{{{
"artifactLocation", std::move(ArtifactLocationObject)},
244json::Object &SarifDocumentWriter::getCurrentTool() {
245 assert(!Closed &&
"SARIF Document is closed. "
246 "Need to call createRun() before using getcurrentTool!");
250 assert(!Runs.empty() &&
"There are no runs associated with the document!");
252 return *Runs.back().getAsObject()->get(
"tool")->getAsObject();
255void SarifDocumentWriter::reset() {
256 CurrentRules.clear();
257 CurrentArtifacts.clear();
269 assert(!Runs.empty() &&
"There are no runs associated with the document!");
272 json::Object &Tool = getCurrentTool();
274 for (
const SarifRule &R : CurrentRules) {
276 {
"enabled", R.DefaultConfiguration.Enabled},
278 {
"rank", R.DefaultConfiguration.Rank}};
282 {
"fullDescription", json::Object{{
"text", R.Description}}},
283 {
"defaultConfiguration", std::move(Config)}};
284 if (!R.HelpURI.empty())
285 Rule[
"helpUri"] = R.HelpURI;
286 Rules.emplace_back(std::move(Rule));
288 json::Object &Driver = *Tool.getObject(
"driver");
289 Driver[
"rules"] = std::move(Rules);
292 json::Object &Run = getCurrentRun();
293 json::Array *Artifacts = Run.getArray(
"artifacts");
295 for (
const auto &[K,
V] : CurrentArtifacts)
296 Vec.emplace_back(K,
V);
297 llvm::sort(Vec, llvm::less_first());
298 for (
const auto &[_, A] : Vec) {
299 json::Object
Loc{{
"uri", A.Location.URI}};
300 if (A.Location.Index.has_value()) {
301 Loc[
"index"] =
static_cast<int64_t
>(*A.Location.Index);
303 json::Object Artifact;
304 Artifact[
"location"] = std::move(
Loc);
305 if (A.Length.has_value())
306 Artifact[
"length"] =
static_cast<int64_t
>(*A.Length);
307 if (!A.Roles.empty())
308 Artifact[
"roles"] = json::Array(A.Roles);
309 if (!A.MimeType.empty())
310 Artifact[
"mimeType"] = A.MimeType;
311 if (A.Offset.has_value())
312 Artifact[
"offset"] = *A.Offset;
313 Artifacts->push_back(json::Value(std::move(Artifact)));
325 json::Object Ret{{
"locations", json::Array{}}};
328 json::Object PLoc = createPhysicalLocation(
ThreadFlow.Range);
333 Ret[
"locations"] = std::move(Locs);
334 return json::Array{std::move(Ret)};
339 return json::Object{{
"threadFlows", createThreadFlows(ThreadFlows)}};
343 StringRef LongToolName,
344 StringRef ToolVersion) {
353 json::Object{{
"name", ShortToolName},
354 {
"fullName", LongToolName},
355 {
"language",
"en-US"},
356 {
"version", ToolVersion},
358 "https://clang.llvm.org/docs/UsersManual.html"}}}};
359 json::Object TheRun{{
"tool", std::move(Tool)},
362 {
"columnKind",
"unicodeCodePoints"}};
363 Runs.emplace_back(std::move(TheRun));
366json::Object &SarifDocumentWriter::getCurrentRun() {
368 "SARIF Document is closed. "
369 "Can only getCurrentRun() if document is opened via createRun(), "
370 "create a run first");
374 assert(!Runs.empty() &&
"There are no runs associated with the document!");
375 return *Runs.back().getAsObject();
379 size_t Ret = CurrentRules.size();
380 CurrentRules.emplace_back(Rule);
385 size_t RuleIdx =
Result.RuleIdx;
386 assert(RuleIdx < CurrentRules.size() &&
387 "Trying to reference a rule that doesn't exist");
388 const SarifRule &Rule = CurrentRules[RuleIdx];
389 assert(Rule.DefaultConfiguration.Enabled &&
390 "Cannot add a result referencing a disabled Rule");
392 {
"ruleIndex",
static_cast<int64_t
>(RuleIdx)},
393 {
"ruleId", Rule.Id}};
394 if (!
Result.Locations.empty()) {
399 Ret[
"locations"] = std::move(Locs);
401 if (!
Result.ThreadFlows.empty())
402 Ret[
"codeFlows"] = json::Array{createCodeFlow(
Result.ThreadFlows)};
405 Result.LevelOverride.value_or(Rule.DefaultConfiguration.Level));
407 json::Object &Run = getCurrentRun();
408 json::Array *Results = Run.getArray(
"results");
409 Results->emplace_back(std::move(Ret));
417 {
"$schema", SchemaURI},
418 {
"version", SchemaVersion},
421 Doc[
"runs"] = json::Array(Runs);
static StringRef importanceToStr(ThreadFlowImportance I)
static json::Object createMessage(StringRef Text)
static StringRef getFileName(FileEntryRef FE)
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.
static bool contains(const std::set< tok::TokenKind > &Terminators, const Token &Tok)
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.
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.
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)
The JSON file list parser is used to communicate input to InstallAPI.
std::pair< FileID, unsigned > FileIDAndOffset
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
SarifResultLevel
The level of severity associated with a SarifResult.
@ Result
The result type of a method or function.
Diagnostic wrappers for TextAPI types for error reporting.