21#include "llvm/ADT/StringMap.h"
22#include "llvm/Support/ConvertUTF.h"
23#include "llvm/Support/JSON.h"
32 std::string OutputFile;
33 const LangOptions &LO;
34 SarifDocumentWriter SarifWriter;
37 SarifDiagnostics(
const std::string &Output,
const LangOptions &LO,
38 const SourceManager &
SM)
39 : OutputFile(Output), LO(LO), SarifWriter(
SM) {}
40 ~SarifDiagnostics()
override =
default;
42 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
43 FilesMade *FM)
override;
45 StringRef
getName()
const override {
return "SarifDiagnostics"; }
46 PathGenerationScheme getGenerationScheme()
const override {
return Minimal; }
47 bool supportsLogicalOpControlFlow()
const override {
return true; }
48 bool supportsCrossFileDiagnostics()
const override {
return true; }
52void ento::createSarifDiagnosticConsumer(
60 createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts),
C, Output, PP,
61 CTU, MacroExpansions);
74 C.push_back(std::make_unique<SarifDiagnostics>(Output, PP.
getLangOpts(),
79 return llvm::StringSwitch<StringRef>(CheckName)
81#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
82 .Case(FULLNAME, HELPTEXT)
83#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
90 return llvm::StringSwitch<StringRef>(CheckName)
92#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
93 .Case(FULLNAME, DOC_URI)
94#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
110 return Piece.
getTagStr() ==
"ConditionBRVisitor"
142 for (
const auto &Piece : Pieces) {
144 Piece->getLocation().asRange(), Piece->getLocation().getManager(), LO);
149 Flows.push_back(Flow);
154static StringMap<uint32_t>
157 StringMap<uint32_t> RuleMapping;
158 llvm::StringSet<> Seen;
161 StringRef CheckName = D->getCheckerName();
162 std::pair<llvm::StringSet<>::iterator,
bool> P = Seen.insert(CheckName);
169 size_t RuleIdx = SarifWriter.
createRule(Rule);
170 RuleMapping[CheckName] = RuleIdx;
177 const StringMap<uint32_t> &RuleMapping,
180 StringRef CheckName =
Diag->getCheckerName();
181 uint32_t RuleIdx = RuleMapping.lookup(CheckName);
183 Diag->getLocation().asRange(),
Diag->getLocation().getManager(), LO);
191 .setThreadFlows(Flows);
195void SarifDiagnostics::FlushDiagnosticsImpl(
196 std::vector<const PathDiagnostic *> &Diags, FilesMade *) {
203 llvm::raw_fd_ostream
OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
205 llvm::errs() <<
"warning: could not create file: " << EC.message() <<
'\n';
210 SarifWriter.
createRun(
"clang",
"clang static analyzer", ToolVersion);
212 for (
const PathDiagnostic *D : Diags) {
217 OS << llvm::formatv(
"{0:2}\n", json::Value(std::move(Document)));
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::Preprocessor interface.
static StringRef getRuleHelpURIStr(StringRef CheckName)
static CharSourceRange convertTokenRangeToCharRange(const SourceRange &R, const SourceManager &SM, const LangOptions &LO)
Accepts a SourceRange corresponding to a pair of the first and last tokens and converts to a Characte...
static StringRef getRuleDescription(StringRef CheckName)
static SarifResult createResult(const PathDiagnostic *Diag, const StringMap< uint32_t > &RuleMapping, const LangOptions &LO)
static StringMap< uint32_t > createRuleMapping(const std::vector< const PathDiagnostic * > &Diags, SarifDocumentWriter &SarifWriter)
static ThreadFlowImportance calculateImportance(const PathDiagnosticPiece &Piece)
static SmallVector< ThreadFlow, 8 > createThreadFlows(const PathDiagnostic *Diag, const LangOptions &LO)
Defines clang::SarifDocumentWriter, clang::SarifRule, clang::SarifResult.
Defines the SourceManager interface.
Defines version macros and version-related utility functions for Clang.
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
MacroExpansionContext tracks the macro expansions processed by the Preprocessor.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
SourceManager & getSourceManager() const
const LangOptions & getLangOpts() const
This class handles creating a valid SARIF document given various input attributes.
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 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...
SarifResult setDiagnosticMessage(llvm::StringRef Message)
SarifResult setRuleId(llvm::StringRef Id)
SarifResult setLocations(llvm::ArrayRef< CharSourceRange > DiagLocs)
SarifResult setDiagnosticLevel(const SarifResultLevel &TheLevel)
static SarifResult create(uint32_t RuleIdx)
SarifRule setDescription(llvm::StringRef RuleDesc)
SarifRule setHelpURI(llvm::StringRef RuleHelpURI)
SarifRule setRuleId(llvm::StringRef RuleId)
static SarifRule create()
SarifRule setName(llvm::StringRef RuleName)
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
ThreadFlow setImportance(const ThreadFlowImportance &ItemImportance)
ThreadFlow setRange(const CharSourceRange &ItemRange)
static ThreadFlow create()
ThreadFlow setMessage(llvm::StringRef ItemMessage)
This class is used for tools that requires cross translation unit capability.
StringRef getTagStr() const
Return the string representation of the tag.
PathDiagnostic - PathDiagnostic objects represent a single path-sensitive diagnostic.
A Range represents the closed range [from, to].
std::vector< std::unique_ptr< PathDiagnosticConsumer > > PathDiagnosticConsumers
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
void createSarifDiagnosticConsumerImpl(PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C, const std::string &Output, const Preprocessor &PP)
Creates and registers a SARIF diagnostic consumer, without any additional text consumer.
StringRef getName(const HeaderType T)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
std::string getClangFullVersion()
Retrieves a string representing the complete clang version, which includes the clang version number,...
Diagnostic wrappers for TextAPI types for error reporting.
These options tweak the behavior of path diangostic consumers.