2018-10-31 02:55:38 +08:00
|
|
|
//===--- SarifDiagnostics.cpp - Sarif Diagnostics for Paths -----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-31 02:55:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SarifDiagnostics object.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-12 04:54:27 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2018-10-31 02:55:38 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-12-15 04:34:23 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2018-10-31 02:55:38 +08:00
|
|
|
#include "llvm/Support/JSON.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class SarifDiagnostics : public PathDiagnosticConsumer {
|
|
|
|
std::string OutputFile;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SarifDiagnostics(AnalyzerOptions &, const std::string &Output)
|
|
|
|
: OutputFile(Output) {}
|
|
|
|
~SarifDiagnostics() override = default;
|
|
|
|
|
|
|
|
void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
|
|
|
|
FilesMade *FM) override;
|
|
|
|
|
|
|
|
StringRef getName() const override { return "SarifDiagnostics"; }
|
|
|
|
PathGenerationScheme getGenerationScheme() const override { return Minimal; }
|
|
|
|
bool supportsLogicalOpControlFlow() const override { return true; }
|
|
|
|
bool supportsCrossFileDiagnostics() const override { return true; }
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-07-23 15:04:20 +08:00
|
|
|
void ento::createSarifDiagnosticConsumer(
|
|
|
|
AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
|
|
|
|
const std::string &Output, const Preprocessor &,
|
|
|
|
const cross_tu::CrossTranslationUnitContext &) {
|
2018-10-31 02:55:38 +08:00
|
|
|
C.push_back(new SarifDiagnostics(AnalyzerOpts, Output));
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef getFileName(const FileEntry &FE) {
|
|
|
|
StringRef Filename = FE.tryGetRealPathName();
|
|
|
|
if (Filename.empty())
|
|
|
|
Filename = FE.getName();
|
|
|
|
return Filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string percentEncodeURICharacter(char C) {
|
|
|
|
// RFC 3986 claims alpha, numeric, and this handful of
|
|
|
|
// characters are not reserved for the path component and
|
|
|
|
// should be written out directly. Otherwise, percent
|
|
|
|
// encode the character and write that out instead of the
|
|
|
|
// reserved character.
|
|
|
|
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) {
|
2018-10-31 03:06:58 +08:00
|
|
|
llvm::SmallString<32> Ret = StringRef("file://");
|
2018-10-31 02:55:38 +08:00
|
|
|
|
|
|
|
// Get the root name to see if it has a URI authority.
|
|
|
|
StringRef Root = sys::path::root_name(Filename);
|
|
|
|
if (Root.startswith("//")) {
|
|
|
|
// There is an authority, so add it to the URI.
|
|
|
|
Ret += Root.drop_front(2).str();
|
2018-12-15 04:34:23 +08:00
|
|
|
} else if (!Root.empty()) {
|
2018-10-31 02:55:38 +08:00
|
|
|
// There is no authority, so end the component and add the root to the URI.
|
|
|
|
Ret += Twine("/" + Root).str();
|
|
|
|
}
|
|
|
|
|
2018-11-07 05:12:44 +08:00
|
|
|
auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename);
|
2018-11-13 06:32:38 +08:00
|
|
|
assert(Iter != End && "Expected there to be a non-root path component.");
|
|
|
|
// Add the rest of the path components, encoding any reserved characters;
|
|
|
|
// we skip past the first path component, as it was handled it above.
|
|
|
|
std::for_each(++Iter, End, [&Ret](StringRef Component) {
|
|
|
|
// For reasons unknown to me, we may get a backslash with Windows native
|
|
|
|
// paths for the initial backslash following the drive component, which
|
|
|
|
// we need to ignore as a URI path part.
|
|
|
|
if (Component == "\\")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add the separator between the previous path part and the one being
|
|
|
|
// currently processed.
|
|
|
|
Ret += "/";
|
|
|
|
|
|
|
|
// URI encode the part.
|
|
|
|
for (char C : Component) {
|
|
|
|
Ret += percentEncodeURICharacter(C);
|
|
|
|
}
|
|
|
|
});
|
2018-10-31 02:55:38 +08:00
|
|
|
|
|
|
|
return Ret.str().str();
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
static json::Object createArtifactLocation(const FileEntry &FE) {
|
2018-10-31 02:55:38 +08:00
|
|
|
return json::Object{{"uri", fileNameToURI(getFileName(FE))}};
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
static json::Object createArtifact(const FileEntry &FE) {
|
|
|
|
return json::Object{{"location", createArtifactLocation(FE)},
|
2018-10-31 02:55:38 +08:00
|
|
|
{"roles", json::Array{"resultFile"}},
|
|
|
|
{"length", FE.getSize()},
|
|
|
|
{"mimeType", "text/plain"}};
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
static json::Object createArtifactLocation(const FileEntry &FE,
|
|
|
|
json::Array &Artifacts) {
|
2018-10-31 02:55:38 +08:00
|
|
|
std::string FileURI = fileNameToURI(getFileName(FE));
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
// See if the Artifacts array contains this URI already. If it does not,
|
|
|
|
// create a new artifact object to add to the array.
|
|
|
|
auto I = llvm::find_if(Artifacts, [&](const json::Value &File) {
|
2018-12-15 04:34:23 +08:00
|
|
|
if (const json::Object *Obj = File.getAsObject()) {
|
2019-08-27 22:43:54 +08:00
|
|
|
if (const json::Object *FileLoc = Obj->getObject("location")) {
|
2018-12-15 04:34:23 +08:00
|
|
|
Optional<StringRef> URI = FileLoc->getString("uri");
|
2018-12-15 05:14:44 +08:00
|
|
|
return URI && URI->equals(FileURI);
|
2018-12-15 04:34:23 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-15 05:14:44 +08:00
|
|
|
return false;
|
|
|
|
});
|
2018-12-15 04:34:23 +08:00
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
// Calculate the index within the artifact array so it can be stored in
|
2018-12-15 05:14:44 +08:00
|
|
|
// the JSON object.
|
2019-08-27 22:43:54 +08:00
|
|
|
auto Index = static_cast<unsigned>(std::distance(Artifacts.begin(), I));
|
|
|
|
if (I == Artifacts.end())
|
|
|
|
Artifacts.push_back(createArtifact(FE));
|
2018-12-15 04:34:23 +08:00
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
return json::Object{{"uri", FileURI}, {"index", Index}};
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static json::Object createTextRegion(SourceRange R, const SourceManager &SM) {
|
2019-08-27 21:49:45 +08:00
|
|
|
json::Object Region{
|
2018-10-31 02:55:38 +08:00
|
|
|
{"startLine", SM.getExpansionLineNumber(R.getBegin())},
|
|
|
|
{"startColumn", SM.getExpansionColumnNumber(R.getBegin())},
|
2019-08-27 21:49:45 +08:00
|
|
|
};
|
|
|
|
if (R.getBegin() == R.getEnd()) {
|
|
|
|
Region["endColumn"] = SM.getExpansionColumnNumber(R.getBegin());
|
|
|
|
} else {
|
|
|
|
Region["endLine"] = SM.getExpansionLineNumber(R.getEnd());
|
|
|
|
Region["endColumn"] = SM.getExpansionColumnNumber(R.getEnd()) + 1;
|
|
|
|
}
|
|
|
|
return Region;
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static json::Object createPhysicalLocation(SourceRange R, const FileEntry &FE,
|
|
|
|
const SourceManager &SMgr,
|
2019-08-27 22:43:54 +08:00
|
|
|
json::Array &Artifacts) {
|
|
|
|
return json::Object{
|
|
|
|
{{"artifactLocation", createArtifactLocation(FE, Artifacts)},
|
|
|
|
{"region", createTextRegion(R, SMgr)}}};
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class Importance { Important, Essential, Unimportant };
|
|
|
|
|
|
|
|
static StringRef importanceToStr(Importance I) {
|
|
|
|
switch (I) {
|
|
|
|
case Importance::Important:
|
|
|
|
return "important";
|
|
|
|
case Importance::Essential:
|
|
|
|
return "essential";
|
|
|
|
case Importance::Unimportant:
|
|
|
|
return "unimportant";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Fully covered switch is not so fully covered");
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:52:07 +08:00
|
|
|
static json::Object createThreadFlowLocation(json::Object &&Location,
|
2018-10-31 02:55:38 +08:00
|
|
|
Importance I) {
|
2018-11-01 19:52:07 +08:00
|
|
|
return json::Object{{"location", std::move(Location)},
|
2018-10-31 02:55:38 +08:00
|
|
|
{"importance", importanceToStr(I)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
static json::Object createMessage(StringRef Text) {
|
|
|
|
return json::Object{{"text", Text.str()}};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Importance calculateImportance(const PathDiagnosticPiece &Piece) {
|
|
|
|
switch (Piece.getKind()) {
|
[analyzer] [NFC] PathDiagnostic: Create PathDiagnosticPopUpPiece
Summary:
This new piece is similar to our macro expansion printing in HTML reports:
On mouse-hover event it pops up on variables. Similar to note pieces it
supports `plist` diagnostics as well.
It is optional, on by default: `add-pop-up-notes=true`.
Extra: In HTML reports `background-color: LemonChiffon` was too light,
changed to `PaleGoldenRod`.
Reviewers: NoQ, alexfh
Reviewed By: NoQ
Subscribers: cfe-commits, gerazo, gsd, george.karpenkov, alexfh, xazax.hun,
baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho,
Szelethus, donat.nagy, dkrupp
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60670
llvm-svn: 362014
2019-05-30 03:21:59 +08:00
|
|
|
case PathDiagnosticPiece::Call:
|
|
|
|
case PathDiagnosticPiece::Macro:
|
|
|
|
case PathDiagnosticPiece::Note:
|
|
|
|
case PathDiagnosticPiece::PopUp:
|
2018-10-31 02:55:38 +08:00
|
|
|
// FIXME: What should be reported here?
|
|
|
|
break;
|
[analyzer] [NFC] PathDiagnostic: Create PathDiagnosticPopUpPiece
Summary:
This new piece is similar to our macro expansion printing in HTML reports:
On mouse-hover event it pops up on variables. Similar to note pieces it
supports `plist` diagnostics as well.
It is optional, on by default: `add-pop-up-notes=true`.
Extra: In HTML reports `background-color: LemonChiffon` was too light,
changed to `PaleGoldenRod`.
Reviewers: NoQ, alexfh
Reviewed By: NoQ
Subscribers: cfe-commits, gerazo, gsd, george.karpenkov, alexfh, xazax.hun,
baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho,
Szelethus, donat.nagy, dkrupp
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60670
llvm-svn: 362014
2019-05-30 03:21:59 +08:00
|
|
|
case PathDiagnosticPiece::Event:
|
2018-10-31 02:55:38 +08:00
|
|
|
return Piece.getTagStr() == "ConditionBRVisitor" ? Importance::Important
|
|
|
|
: Importance::Essential;
|
[analyzer] [NFC] PathDiagnostic: Create PathDiagnosticPopUpPiece
Summary:
This new piece is similar to our macro expansion printing in HTML reports:
On mouse-hover event it pops up on variables. Similar to note pieces it
supports `plist` diagnostics as well.
It is optional, on by default: `add-pop-up-notes=true`.
Extra: In HTML reports `background-color: LemonChiffon` was too light,
changed to `PaleGoldenRod`.
Reviewers: NoQ, alexfh
Reviewed By: NoQ
Subscribers: cfe-commits, gerazo, gsd, george.karpenkov, alexfh, xazax.hun,
baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho,
Szelethus, donat.nagy, dkrupp
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60670
llvm-svn: 362014
2019-05-30 03:21:59 +08:00
|
|
|
case PathDiagnosticPiece::ControlFlow:
|
2018-10-31 02:55:38 +08:00
|
|
|
return Importance::Unimportant;
|
|
|
|
}
|
|
|
|
return Importance::Unimportant;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json::Object createThreadFlow(const PathPieces &Pieces,
|
2019-08-27 22:43:54 +08:00
|
|
|
json::Array &Artifacts) {
|
2018-10-31 02:55:38 +08:00
|
|
|
const SourceManager &SMgr = Pieces.front()->getLocation().getManager();
|
|
|
|
json::Array Locations;
|
|
|
|
for (const auto &Piece : Pieces) {
|
|
|
|
const PathDiagnosticLocation &P = Piece->getLocation();
|
|
|
|
Locations.push_back(createThreadFlowLocation(
|
2019-08-27 22:20:27 +08:00
|
|
|
createLocation(createPhysicalLocation(
|
|
|
|
P.asRange(),
|
|
|
|
*P.asLocation().getExpansionLoc().getFileEntry(),
|
2019-08-27 22:43:54 +08:00
|
|
|
SMgr, Artifacts),
|
2018-10-31 02:55:38 +08:00
|
|
|
Piece->getString()),
|
|
|
|
calculateImportance(*Piece)));
|
|
|
|
}
|
|
|
|
return json::Object{{"locations", std::move(Locations)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
static json::Object createCodeFlow(const PathPieces &Pieces,
|
2019-08-27 22:43:54 +08:00
|
|
|
json::Array &Artifacts) {
|
2018-10-31 02:55:38 +08:00
|
|
|
return json::Object{
|
2019-08-27 22:43:54 +08:00
|
|
|
{"threadFlows", json::Array{createThreadFlow(Pieces, Artifacts)}}};
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
static json::Object createResult(const PathDiagnostic &Diag,
|
|
|
|
json::Array &Artifacts,
|
2018-12-15 04:34:23 +08:00
|
|
|
const StringMap<unsigned> &RuleMapping) {
|
2018-10-31 02:55:38 +08:00
|
|
|
const PathPieces &Path = Diag.path.flatten(false);
|
|
|
|
const SourceManager &SMgr = Path.front()->getLocation().getManager();
|
|
|
|
|
2018-12-15 04:34:23 +08:00
|
|
|
auto Iter = RuleMapping.find(Diag.getCheckName());
|
|
|
|
assert(Iter != RuleMapping.end() && "Rule ID is not in the array index map?");
|
|
|
|
|
2018-10-31 02:55:38 +08:00
|
|
|
return json::Object{
|
|
|
|
{"message", createMessage(Diag.getVerboseDescription())},
|
2019-08-27 22:43:54 +08:00
|
|
|
{"codeFlows", json::Array{createCodeFlow(Path, Artifacts)}},
|
2018-10-31 02:55:38 +08:00
|
|
|
{"locations",
|
|
|
|
json::Array{createLocation(createPhysicalLocation(
|
|
|
|
Diag.getLocation().asRange(),
|
2019-08-27 22:20:27 +08:00
|
|
|
*Diag.getLocation().asLocation().getExpansionLoc().getFileEntry(),
|
2019-08-27 22:43:54 +08:00
|
|
|
SMgr, Artifacts))}},
|
2018-12-15 04:34:23 +08:00
|
|
|
{"ruleIndex", Iter->getValue()},
|
2018-10-31 02:55:38 +08:00
|
|
|
{"ruleId", Diag.getCheckName()}};
|
|
|
|
}
|
|
|
|
|
2018-11-02 02:57:38 +08:00
|
|
|
static StringRef getRuleDescription(StringRef CheckName) {
|
|
|
|
return llvm::StringSwitch<StringRef>(CheckName)
|
|
|
|
#define GET_CHECKERS
|
[analyzer] Don't display implementation checkers under -analyzer-checker-help, but do under the new flag -analyzer-checker-help-hidden
During my work on analyzer dependencies, I created a great amount of new
checkers that emitted no diagnostics at all, and were purely modeling some
function or another.
However, the user shouldn't really disable/enable these by hand, hence this
patch, which hides these by default. I intentionally chose not to hide alpha
checkers, because they have a scary enough name, in my opinion, to cause no
surprise when they emit false positives or cause crashes.
The patch introduces the Hidden bit into the TableGen files (you may remember
it before I removed it in D53995), and checkers that are either marked as
hidden, or are in a package that is marked hidden won't be displayed under
-analyzer-checker-help. -analyzer-checker-help-hidden, a new flag meant for
developers only, displays the full list.
Differential Revision: https://reviews.llvm.org/D60925
llvm-svn: 359720
2019-05-02 03:56:47 +08:00
|
|
|
#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
|
2018-11-02 02:57:38 +08:00
|
|
|
.Case(FULLNAME, HELPTEXT)
|
|
|
|
#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
|
|
|
|
#undef CHECKER
|
|
|
|
#undef GET_CHECKERS
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-12-21 04:20:20 +08:00
|
|
|
static StringRef getRuleHelpURIStr(StringRef CheckName) {
|
|
|
|
return llvm::StringSwitch<StringRef>(CheckName)
|
|
|
|
#define GET_CHECKERS
|
[analyzer] Don't display implementation checkers under -analyzer-checker-help, but do under the new flag -analyzer-checker-help-hidden
During my work on analyzer dependencies, I created a great amount of new
checkers that emitted no diagnostics at all, and were purely modeling some
function or another.
However, the user shouldn't really disable/enable these by hand, hence this
patch, which hides these by default. I intentionally chose not to hide alpha
checkers, because they have a scary enough name, in my opinion, to cause no
surprise when they emit false positives or cause crashes.
The patch introduces the Hidden bit into the TableGen files (you may remember
it before I removed it in D53995), and checkers that are either marked as
hidden, or are in a package that is marked hidden won't be displayed under
-analyzer-checker-help. -analyzer-checker-help-hidden, a new flag meant for
developers only, displays the full list.
Differential Revision: https://reviews.llvm.org/D60925
llvm-svn: 359720
2019-05-02 03:56:47 +08:00
|
|
|
#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
|
2018-12-21 04:20:20 +08:00
|
|
|
.Case(FULLNAME, DOC_URI)
|
|
|
|
#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
|
|
|
|
#undef CHECKER
|
|
|
|
#undef GET_CHECKERS
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-11-02 02:57:38 +08:00
|
|
|
static json::Object createRule(const PathDiagnostic &Diag) {
|
|
|
|
StringRef CheckName = Diag.getCheckName();
|
2018-12-21 04:20:20 +08:00
|
|
|
json::Object Ret{
|
2018-11-02 02:57:38 +08:00
|
|
|
{"fullDescription", createMessage(getRuleDescription(CheckName))},
|
2019-08-27 22:43:54 +08:00
|
|
|
{"name", CheckName},
|
2018-12-15 04:34:23 +08:00
|
|
|
{"id", CheckName}};
|
2018-12-21 04:20:20 +08:00
|
|
|
|
|
|
|
std::string RuleURI = getRuleHelpURIStr(CheckName);
|
|
|
|
if (!RuleURI.empty())
|
2019-01-10 21:19:48 +08:00
|
|
|
Ret["helpUri"] = RuleURI;
|
2018-12-21 04:20:20 +08:00
|
|
|
|
|
|
|
return Ret;
|
2018-11-02 02:57:38 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 04:34:23 +08:00
|
|
|
static json::Array createRules(std::vector<const PathDiagnostic *> &Diags,
|
|
|
|
StringMap<unsigned> &RuleMapping) {
|
|
|
|
json::Array Rules;
|
2018-11-02 02:57:38 +08:00
|
|
|
llvm::StringSet<> Seen;
|
|
|
|
|
|
|
|
llvm::for_each(Diags, [&](const PathDiagnostic *D) {
|
|
|
|
StringRef RuleID = D->getCheckName();
|
|
|
|
std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(RuleID);
|
2018-12-15 04:34:23 +08:00
|
|
|
if (P.second) {
|
|
|
|
RuleMapping[RuleID] = Rules.size(); // Maps RuleID to an Array Index.
|
|
|
|
Rules.push_back(createRule(*D));
|
|
|
|
}
|
2018-11-02 02:57:38 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return Rules;
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
static json::Object createTool(std::vector<const PathDiagnostic *> &Diags,
|
|
|
|
StringMap<unsigned> &RuleMapping) {
|
|
|
|
return json::Object{
|
|
|
|
{"driver", json::Object{{"name", "clang"},
|
|
|
|
{"fullName", "clang static analyzer"},
|
|
|
|
{"language", "en-US"},
|
|
|
|
{"version", getClangFullVersion()},
|
|
|
|
{"rules", createRules(Diags, RuleMapping)}}}};
|
2018-11-02 02:57:38 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 02:55:38 +08:00
|
|
|
static json::Object createRun(std::vector<const PathDiagnostic *> &Diags) {
|
2019-08-27 22:43:54 +08:00
|
|
|
json::Array Results, Artifacts;
|
2018-12-15 04:34:23 +08:00
|
|
|
StringMap<unsigned> RuleMapping;
|
2019-08-27 22:43:54 +08:00
|
|
|
json::Object Tool = createTool(Diags, RuleMapping);
|
2018-12-15 04:34:23 +08:00
|
|
|
|
2018-10-31 02:55:38 +08:00
|
|
|
llvm::for_each(Diags, [&](const PathDiagnostic *D) {
|
2019-08-27 22:43:54 +08:00
|
|
|
Results.push_back(createResult(*D, Artifacts, RuleMapping));
|
2018-10-31 02:55:38 +08:00
|
|
|
});
|
|
|
|
|
2019-08-27 22:43:54 +08:00
|
|
|
return json::Object{{"tool", std::move(Tool)},
|
2018-10-31 02:55:38 +08:00
|
|
|
{"results", std::move(Results)},
|
2019-08-27 22:43:54 +08:00
|
|
|
{"artifacts", std::move(Artifacts)}};
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SarifDiagnostics::FlushDiagnosticsImpl(
|
|
|
|
std::vector<const PathDiagnostic *> &Diags, FilesMade *) {
|
|
|
|
// We currently overwrite the file if it already exists. However, it may be
|
|
|
|
// useful to add a feature someday that allows the user to append a run to an
|
|
|
|
// existing SARIF file. One danger from that approach is that the size of the
|
|
|
|
// file can become large very quickly, so decoding into JSON to append a run
|
|
|
|
// may be an expensive operation.
|
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
|
2018-10-31 02:55:38 +08:00
|
|
|
if (EC) {
|
|
|
|
llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
|
|
|
|
return;
|
|
|
|
}
|
2018-11-01 19:52:07 +08:00
|
|
|
json::Object Sarif{
|
|
|
|
{"$schema",
|
2019-08-27 22:43:54 +08:00
|
|
|
"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"},
|
|
|
|
{"version", "2.1.0"},
|
2018-11-01 19:52:07 +08:00
|
|
|
{"runs", json::Array{createRun(Diags)}}};
|
[analyzer] SARIF: Add EOF newline; replace diff_sarif
Summary:
This patch applies a change similar to rC363069, but for SARIF files.
The `%diff_sarif` lit substitution invokes `diff` with a non-portable
`-I` option. The intended effect can be achieved by normalizing the
inputs to `diff` beforehand. Such normalization can be done with
`grep -Ev`, which is also used by other tests.
Additionally, this patch updates the SARIF output to have a newline at
the end of the file. This makes it so that the SARIF file qualifies as a
POSIX text file, which increases the consumability of the generated file
in relation to various tools.
Reviewers: NoQ, sfertile, xingxue, jasonliu, daltenty, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, Charusso, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D62952
llvm-svn: 363822
2019-06-19 23:27:35 +08:00
|
|
|
OS << llvm::formatv("{0:2}\n", json::Value(std::move(Sarif)));
|
2018-10-31 02:55:38 +08:00
|
|
|
}
|