25#include "llvm/ADT/StringMap.h"
26#include "llvm/Support/ConvertUTF.h"
27#include "llvm/Support/JSON.h"
36 std::string OutputFile;
37 const LangOptions &LO;
38 const SourceManager &SM;
39 SarifDocumentWriter SarifWriter;
42 SarifDiagnostics(
const std::string &Output,
const LangOptions &LO,
43 const SourceManager &SM)
44 : OutputFile(Output), LO(LO), SM(SM), SarifWriter(SM) {}
45 ~SarifDiagnostics()
override =
default;
47 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
48 FilesMade *FM)
override;
50 StringRef
getName()
const override {
return "SarifDiagnostics"; }
51 PathGenerationScheme getGenerationScheme()
const override {
return Minimal; }
52 bool supportsLogicalOpControlFlow()
const override {
return true; }
53 bool supportsCrossFileDiagnostics()
const override {
return true; }
56 SarifResult createResult(
const PathDiagnostic *
Diag,
57 const StringMap<uint32_t> &RuleMapping,
58 const LangOptions &LO, FilesMade *FM);
62void ento::createSarifDiagnosticConsumer(
70 createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts),
C, Output, PP,
71 CTU, MacroExpansions);
84 C.push_back(std::make_unique<SarifDiagnostics>(Output, PP.
getLangOpts(),
89 return llvm::StringSwitch<StringRef>(CheckName)
91#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
92 .Case(FULLNAME, HELPTEXT)
93#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
100 return llvm::StringSwitch<StringRef>(CheckName)
102#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
103 .Case(FULLNAME, DOC_URI)
104#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
120 return Piece.
getTagStr() ==
"ConditionBRVisitor"
154 for (
const auto &Piece : Pieces) {
159 Flows.push_back(Flow);
164static StringMap<uint32_t>
167 StringMap<uint32_t> RuleMapping;
168 llvm::StringSet<> Seen;
171 StringRef CheckName = D->getCheckerName();
172 std::pair<llvm::StringSet<>::iterator,
bool> P = Seen.insert(CheckName);
179 size_t RuleIdx = SarifWriter.
createRule(Rule);
180 RuleMapping[CheckName] = RuleIdx;
190 const StringMap<uint32_t> &RuleMapping,
193 StringRef CheckName =
Diag->getCheckerName();
194 uint32_t RuleIdx = RuleMapping.lookup(CheckName);
199 auto IssueHash =
Diag->getIssueHash(SM, LO);
201 std::string HtmlReportURL;
202 if (FM && !FM->empty()) {
204 PDFileEntry::ConsumerFiles *Files = FM->getFiles(*
Diag);
206 auto HtmlFile = llvm::find_if(*Files, [](
const auto &
File) {
209 if (HtmlFile != Files->end()) {
211 llvm::sys::path::parent_path(OutputFile);
212 llvm::sys::path::append(HtmlReportPath, HtmlFile->second);
229void SarifDiagnostics::FlushDiagnosticsImpl(
230 std::vector<const PathDiagnostic *> &Diags, FilesMade *FM) {
237 llvm::raw_fd_ostream
OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
239 llvm::errs() <<
"warning: could not create file: " << EC.message() <<
'\n';
244 SarifWriter.
createRun(
"clang",
"clang static analyzer", ToolVersion);
246 for (
const PathDiagnostic *D : Diags) {
247 SarifResult
Result = createResult(D, RuleMapping, LO, FM);
251 OS << llvm::formatv(
"{0:2}\n", json::Value(std::move(Document)));
#define HTML_DIAGNOSTICS_NAME
Result
Implement __builtin_bit_cast and related operations.
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 StringRef getRuleDescription(StringRef CheckName)
static StringMap< uint32_t > createRuleMapping(const std::vector< const PathDiagnostic * > &Diags, SarifDocumentWriter &SarifWriter)
static const llvm::StringRef IssueHashKey
static ThreadFlowImportance calculateImportance(const PathDiagnosticPiece &Piece)
static CharSourceRange getDisplayCharRange(const PathDiagnosticLocation &Loc, const LangOptions &LO)
Returns the character range to report for Loc.
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 byte-granular source range.
static CharSourceRange getCharRange(SourceRange R)
static CharSourceRange getTokenRange(SourceRange R)
A SourceLocation and its associated SourceManager.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static CharSourceRange getAsCharRange(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Given a token range, produce a corresponding CharSourceRange that is not a token range.
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.
static std::string fileNameToURI(llvm::StringRef Filename)
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 setHostedViewerURI(llvm::StringRef URI)
SarifResult setThreadFlows(llvm::ArrayRef< ThreadFlow > ThreadFlowResults)
SarifResult setDiagnosticMessage(llvm::StringRef Message)
SarifResult setRuleId(llvm::StringRef Id)
SarifResult addLocations(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)
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
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)
Top level wrappers for InstallAPI frontend operations.
std::optional< CharSourceRange > getExpansionRangeInFile(CharSourceRange Range, FileID FID, const SourceManager &SM)
Maps both endpoints of Range to their macro expansion, so that the range can be shown to a user.
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.
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
These options tweak the behavior of path diangostic consumers.