17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/ConvertUTF.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/ErrorOr.h"
25#include "llvm/Support/Locale.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/raw_ostream.h"
43 llvm_unreachable(
"Invalid diagnostic type");
54 llvm_unreachable(
"Un-handled DiagnosticsEngine::Level occured.");
68 return Rule.setDefaultConfiguration(Config);
82 const auto *
Diag = dyn_cast_if_present<const Diagnostic *>(D);
95 unsigned RuleIdx = Writer->createRule(Rule);
103 for (
auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
104 Result = addRelatedLocationToResult(
Result, RelLoc, RelPLoc);
105 RelatedLocationsCache.clear();
107 Writer->appendResult(
Result);
120 RelatedLocationsCache.push_back({Loc, PLoc});
124 StringRef ModuleName) {
125 RelatedLocationsCache.push_back({Loc, PLoc});
131 auto Locations = getSarifLocation(Loc, PLoc, Ranges);
132 return Result.addLocations(Locations);
135SarifResult SARIFDiagnostic::addRelatedLocationToResult(SarifResult
Result,
138 auto Locations = getSarifLocation(Loc, PLoc, {});
139 return Result.addRelatedLocations(Locations);
145 SmallVector<CharSourceRange> Locations = {};
147 if (PLoc.isInvalid()) {
149 FileID FID = Loc.getFileID();
152 emitFilename(FE->getName(), Loc.getManager());
159 FileID CaretFileID = Loc.getExpansionLoc().getFileID();
161 auto &SM = Loc.getManager();
162 for (
const CharSourceRange Range : Ranges) {
163 std::optional<CharSourceRange> FileRange =
168 SourceLocation B = FileRange->getBegin();
169 SourceLocation E = FileRange->getEnd();
173 unsigned TokSize = 0;
174 if (FileRange->isTokenRange())
177 FullSourceLoc BF(B, SM), EF(E, SM);
178 SourceLocation BeginLoc = SM.translateLineCol(
179 BF.getFileID(), BF.getLineNumber(), BF.getColumnNumber());
180 SourceLocation EndLoc = SM.translateLineCol(
181 EF.getFileID(), EF.getLineNumber(), EF.getColumnNumber() + TokSize);
184 CharSourceRange{SourceRange{BeginLoc, EndLoc},
false});
189 auto FID = PLoc.getFileID();
191 unsigned int ColNo = (
LangOpts.MSCompatibilityVersion &&
193 ? PLoc.getColumn() - 1
195 SourceLocation DiagLoc = SM.translateLineCol(FID, PLoc.getLine(), ColNo);
198 CharSourceRange
Range = {SourceRange{DiagLoc, DiagLoc},
false};
200 Locations.push_back(std::move(Range));
205llvm::StringRef SARIFDiagnostic::emitFilename(StringRef Filename,
208 auto File = SM.getFileManager().getOptionalFileRef(Filename);
225 SmallString<256> TmpFilename =
File->getName();
226 SM.getFileManager().makeAbsolutePath(TmpFilename);
227 llvm::sys::path::native(TmpFilename);
228 llvm::sys::path::remove_dots(TmpFilename,
true);
229 Filename = StringRef(TmpFilename.data(), TmpFilename.size());
231 Filename = SM.getFileManager().getCanonicalName(*
File);
248 assert(
false &&
"Not implemented in SARIF mode");
253 StringRef ModuleName) {
254 assert(
false &&
"Not implemented in SARIF mode");
Defines the clang::FileManager interface and associated types.
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 clang::SarifDocumentWriter, clang::SarifRule, clang::SarifResult.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Used for handling and querying diagnostic IDs.
std::string getStableID(unsigned DiagID) const
Given a diagnostic ID, return the stable ID of the diagnostic.
llvm::SmallVector< StringRef, 4 > getLegacyStableIDs(unsigned DiagID) const
Given a diagnostic ID, return the previous stable IDs of the diagnostic.
Options for controlling the compiler diagnostics engine.
const LangOptions & LangOpts
DiagnosticOptions & DiagOpts
DiagnosticRenderer(const LangOptions &LangOpts, DiagnosticOptions &DiagOpts)
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
Level
The level of the diagnostic, after it has been through mapping.
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 unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Represents an unpacked "presumed" location which can be presented to the user.
SARIFDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions &DiagOpts, SarifDocumentWriter *Writer)
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag D) override
void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, ArrayRef< CharSourceRange > Ranges) override
Print out the file/line/column information and include trace.
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
This class handles creating a valid SARIF document given various input attributes.
A SARIF Reporting Configuration (reportingConfiguration) object contains properties for a SarifRule t...
static SarifReportingConfiguration create()
SarifReportingConfiguration setLevel(SarifResultLevel TheLevel)
SarifReportingConfiguration setRank(float TheRank)
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)
static SarifResult create(uint32_t RuleIdx)
A SARIF rule (reportingDescriptor object) contains information that describes a reporting item genera...
SarifRule setDeprecatedIds(llvm::ArrayRef< llvm::StringRef > RuleDeprecatedIds)
SarifRule setRuleId(llvm::StringRef RuleId)
static SarifRule create()
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
Top level wrappers for InstallAPI frontend operations.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
SarifResultLevel getSarifResultLevel(DiagnosticsEngine::Level level)
SarifResultLevel
The level of severity associated with a SarifResult.
@ Result
The result type of a method or function.
SarifRule addDiagnosticLevelToRule(SarifRule Rule, DiagnosticsEngine::Level Level)
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.