#include "clang/Basic/Sarif.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/Path.h"
#include <string>
#include <utility>
using namespace clang;
using namespace llvm;
using clang::detail::SarifArtifact;
using clang::detail::SarifArtifactLocation;
static StringRef getFileName(const FileEntry &FE) {
StringRef Filename = FE.tryGetRealPathName();
if (Filename.empty())
Filename = FE.getName();
return Filename;
}
static std::string percentEncodeURICharacter(char C) {
if (llvm::isAlnum(C) ||
StringRef::npos != StringRef("-._~:@!$&'()*+,;=").find(C))
return std::string(&C, 1);
return "%" + llvm::toHex(StringRef(&C, 1));
}
static std::string fileNameToURI(StringRef Filename) {
SmallString<32> Ret = StringRef("file://");
StringRef Root = sys::path::root_name(Filename);
if (Root.startswith("//")) {
Ret += Root.drop_front(2).str();
} else if (!Root.empty()) {
Ret += Twine("/" + Root).str();
}
auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename);
assert(Iter != End && "Expected there to be a non-root path component.");
std::for_each(++Iter, End, [&Ret](StringRef Component) {
if (Component == "\\")
return;
Ret += "/";
for (char C : Component) {
Ret += percentEncodeURICharacter(C);
}
});
return std::string(Ret);
}
static unsigned int adjustColumnPos(FullSourceLoc Loc,
unsigned int TokenLen = 0) {
assert(!Loc.isInvalid() && "invalid Loc when adjusting column position");
std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedLoc();
Optional<MemoryBufferRef> Buf =
Loc.getManager().getBufferOrNone(LocInfo.first);
assert(Buf && "got an invalid buffer for the location's file");
assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) &&
"token extends past end of buffer?");
unsigned int Off = LocInfo.second - (Loc.getExpansionColumnNumber() - 1);
unsigned int Ret = 1;
while (Off < (LocInfo.second + TokenLen)) {
Off += getNumBytesForUTF8(Buf->getBuffer()[Off]);
Ret++;
}
return Ret;
}
json::Object createMessage(StringRef Text) {
return json::Object{{"text", Text.str()}};
}
static json::Object createTextRegion(const SourceManager &SM,
const CharSourceRange &R) {
FullSourceLoc FirstTokenLoc{R.getBegin(), SM};
FullSourceLoc LastTokenLoc{R.getEnd(), SM};
json::Object Region{{"startLine", FirstTokenLoc.getExpansionLineNumber()},
{"startColumn", adjustColumnPos(FirstTokenLoc)},
{"endColumn", adjustColumnPos(LastTokenLoc)}};
if (FirstTokenLoc != LastTokenLoc) {
Region["endLine"] = LastTokenLoc.getExpansionLineNumber();
}
return Region;
}
static json::Object createLocation(json::Object &&PhysicalLocation,
StringRef Message = "") {
json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}};
if (!Message.empty())
Ret.insert({"message", createMessage(Message)});
return Ret;
}
static StringRef importanceToStr(ThreadFlowImportance I) {
switch (I) {
case ThreadFlowImportance::Important:
return "important";
case ThreadFlowImportance::Essential:
return "essential";
case ThreadFlowImportance::Unimportant:
return "unimportant";
}
llvm_unreachable("Fully covered switch is not so fully covered");
}
static json::Object
createThreadFlowLocation(json::Object &&Location,
const ThreadFlowImportance &Importance) {
return json::Object{{"location", std::move(Location)},
{"importance", importanceToStr(Importance)}};
}
json::Object
SarifDocumentWriter::createPhysicalLocation(const CharSourceRange &R) {
assert(R.isValid() &&
"Cannot create a physicalLocation from invalid SourceRange!");
assert(R.isCharRange() &&
"Cannot create a physicalLocation from a token range!");
FullSourceLoc Start{R.getBegin(), SourceMgr};
const FileEntry *FE = Start.getExpansionLoc().getFileEntry();
assert(FE != nullptr && "Diagnostic does not exist within a valid file!");
const std::string &FileURI = fileNameToURI(getFileName(*FE));
auto I = CurrentArtifacts.find(FileURI);
if (I == CurrentArtifacts.end()) {
uint32_t Idx = static_cast<uint32_t>(CurrentArtifacts.size());
const SarifArtifactLocation &Location =
SarifArtifactLocation::create(FileURI).setIndex(Idx);
const SarifArtifact &Artifact = SarifArtifact::create(Location)
.setRoles({"resultFile"})
.setLength(FE->getSize())
.setMimeType("text/plain");
auto StatusIter = CurrentArtifacts.insert({FileURI, Artifact});
if (StatusIter.second)
I = StatusIter.first;
}
assert(I != CurrentArtifacts.end() && "Failed to insert new artifact");
const SarifArtifactLocation &Location = I->second.Location;
uint32_t Idx = Location.Index.value();
return json::Object{{{"artifactLocation", json::Object{{{"index", Idx}}}},
{"region", createTextRegion(SourceMgr, R)}}};
}
json::Object &SarifDocumentWriter::getCurrentTool() {
assert(!Closed && "SARIF Document is closed. "
"Need to call createRun() before using getcurrentTool!");
assert(!Runs.empty() && "There are no runs associated with the document!");
return *Runs.back().getAsObject()->get("tool")->getAsObject();
}
void SarifDocumentWriter::reset() {
CurrentRules.clear();
CurrentArtifacts.clear();
}
void SarifDocumentWriter::endRun() {
if (Closed) {
reset();
return;
}
assert(!Runs.empty() && "There are no runs associated with the document!");
json::Object &Tool = getCurrentTool();
json::Array Rules;
for (const SarifRule &R : CurrentRules) {
json::Object Rule{
{"name", R.Name},
{"id", R.Id},
{"fullDescription", json::Object{{"text", R.Description}}}};
if (!R.HelpURI.empty())
Rule["helpUri"] = R.HelpURI;
Rules.emplace_back(std::move(Rule));
}
json::Object &Driver = *Tool.getObject("driver");
Driver["rules"] = std::move(Rules);
json::Object &Run = getCurrentRun();
json::Array *Artifacts = Run.getArray("artifacts");
for (const auto &Pair : CurrentArtifacts) {
const SarifArtifact &A = Pair.getValue();
json::Object Loc{{"uri", A.Location.URI}};
if (A.Location.Index.has_value()) {
Loc["index"] = static_cast<int64_t>(A.Location.Index.value());
}
json::Object Artifact;
Artifact["location"] = std::move(Loc);
if (A.Length.has_value())
Artifact["length"] = static_cast<int64_t>(A.Length.value());
if (!A.Roles.empty())
Artifact["roles"] = json::Array(A.Roles);
if (!A.MimeType.empty())
Artifact["mimeType"] = A.MimeType;
if (A.Offset.has_value())
Artifact["offset"] = A.Offset;
Artifacts->push_back(json::Value(std::move(Artifact)));
}
reset();
Closed = true;
}
json::Array
SarifDocumentWriter::createThreadFlows(ArrayRef<ThreadFlow> ThreadFlows) {
json::Object Ret{{"locations", json::Array{}}};
json::Array Locs;
for (const auto &ThreadFlow : ThreadFlows) {
json::Object PLoc = createPhysicalLocation(ThreadFlow.Range);
json::Object Loc = createLocation(std::move(PLoc), ThreadFlow.Message);
Locs.emplace_back(
createThreadFlowLocation(std::move(Loc), ThreadFlow.Importance));
}
Ret["locations"] = std::move(Locs);
return json::Array{std::move(Ret)};
}
json::Object
SarifDocumentWriter::createCodeFlow(ArrayRef<ThreadFlow> ThreadFlows) {
return json::Object{{"threadFlows", createThreadFlows(ThreadFlows)}};
}
void SarifDocumentWriter::createRun(StringRef ShortToolName,
StringRef LongToolName,
StringRef ToolVersion) {
endRun();
Closed = false;
json::Object Tool{
{"driver",
json::Object{{"name", ShortToolName},
{"fullName", LongToolName},
{"language", "en-US"},
{"version", ToolVersion},
{"informationUri",
"https://clang.llvm.org/docs/UsersManual.html"}}}};
json::Object TheRun{{"tool", std::move(Tool)},
{"results", {}},
{"artifacts", {}},
{"columnKind", "unicodeCodePoints"}};
Runs.emplace_back(std::move(TheRun));
}
json::Object &SarifDocumentWriter::getCurrentRun() {
assert(!Closed &&
"SARIF Document is closed. "
"Can only getCurrentRun() if document is opened via createRun(), "
"create a run first");
assert(!Runs.empty() && "There are no runs associated with the document!");
return *Runs.back().getAsObject();
}
size_t SarifDocumentWriter::createRule(const SarifRule &Rule) {
size_t Ret = CurrentRules.size();
CurrentRules.emplace_back(Rule);
return Ret;
}
void SarifDocumentWriter::appendResult(const SarifResult &Result) {
size_t RuleIdx = Result.RuleIdx;
assert(RuleIdx < CurrentRules.size() &&
"Trying to reference a rule that doesn't exist");
json::Object Ret{{"message", createMessage(Result.DiagnosticMessage)},
{"ruleIndex", static_cast<int64_t>(RuleIdx)},
{"ruleId", CurrentRules[RuleIdx].Id}};
if (!Result.Locations.empty()) {
json::Array Locs;
for (auto &Range : Result.Locations) {
Locs.emplace_back(createLocation(createPhysicalLocation(Range)));
}
Ret["locations"] = std::move(Locs);
}
if (!Result.ThreadFlows.empty())
Ret["codeFlows"] = json::Array{createCodeFlow(Result.ThreadFlows)};
json::Object &Run = getCurrentRun();
json::Array *Results = Run.getArray("results");
Results->emplace_back(std::move(Ret));
}
json::Object SarifDocumentWriter::createDocument() {
endRun();
json::Object Doc{
{"$schema", SchemaURI},
{"version", SchemaVersion},
};
if (!Runs.empty())
Doc["runs"] = json::Array(Runs);
return Doc;
}