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"
51 Rule = addDiagnosticLevelToRule(Rule, Level);
53 unsigned RuleIdx = Writer->createRule(Rule);
61 for (
auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
62 Result = addRelatedLocationToResult(
Result, RelLoc, RelPLoc);
63 RelatedLocationsCache.clear();
65 Writer->appendResult(
Result);
78 RelatedLocationsCache.push_back({Loc, PLoc});
82 StringRef ModuleName) {
83 RelatedLocationsCache.push_back({Loc, PLoc});
89 auto Locations = getSarifLocation(Loc, PLoc, Ranges);
90 return Result.addLocations(Locations);
93SarifResult SARIFDiagnostic::addRelatedLocationToResult(SarifResult Result,
96 auto Locations = getSarifLocation(Loc, PLoc, {});
97 return Result.addRelatedLocations(Locations);
103 SmallVector<CharSourceRange> Locations = {};
105 if (PLoc.isInvalid()) {
107 FileID FID = Loc.getFileID();
110 emitFilename(FE->getName(), Loc.getManager());
117 FileID CaretFileID = Loc.getExpansionLoc().getFileID();
119 for (
const CharSourceRange Range : Ranges) {
121 if (
Range.isInvalid())
124 auto &
SM = Loc.getManager();
125 SourceLocation B =
SM.getExpansionLoc(
Range.getBegin());
126 CharSourceRange ERange =
SM.getExpansionRange(
Range.getEnd());
127 SourceLocation E = ERange.getEnd();
128 bool IsTokenRange = ERange.isTokenRange();
135 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
140 unsigned TokSize = 0;
144 FullSourceLoc BF(B,
SM), EF(E,
SM);
145 SourceLocation BeginLoc =
SM.translateLineCol(
146 BF.getFileID(), BF.getLineNumber(), BF.getColumnNumber());
147 SourceLocation EndLoc =
SM.translateLineCol(
148 EF.getFileID(), EF.getLineNumber(), EF.getColumnNumber() + TokSize);
151 CharSourceRange{SourceRange{BeginLoc, EndLoc},
false});
156 auto &
SM = Loc.getManager();
157 auto FID = PLoc.getFileID();
159 unsigned int ColNo = (
LangOpts.MSCompatibilityVersion &&
161 ? PLoc.getColumn() - 1
163 SourceLocation DiagLoc =
SM.translateLineCol(FID, PLoc.getLine(), ColNo);
166 CharSourceRange
Range = {SourceRange{DiagLoc, DiagLoc},
false};
168 Locations.push_back(std::move(Range));
174SARIFDiagnostic::addDiagnosticLevelToRule(
SarifRule Rule,
195 assert(
false &&
"Invalid diagnostic type");
198 return Rule.setDefaultConfiguration(Config);
201llvm::StringRef SARIFDiagnostic::emitFilename(StringRef Filename,
204 auto File =
SM.getFileManager().getOptionalFileRef(Filename);
221 SmallString<256> TmpFilename =
File->getName();
222 llvm::sys::fs::make_absolute(TmpFilename);
223 llvm::sys::path::native(TmpFilename);
224 llvm::sys::path::remove_dots(TmpFilename,
true);
225 Filename = StringRef(TmpFilename.data(), TmpFilename.size());
227 Filename =
SM.getFileManager().getCanonicalName(*
File);
244 assert(
false &&
"Not implemented in SARIF mode");
249 StringRef ModuleName) {
250 assert(
false &&
"Not implemented in SARIF mode");
Defines the clang::FileManager interface and associated types.
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.
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.
static SarifReportingConfiguration create()
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 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.
const Regex Rule("(.+)/(.+)\\.framework/")
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
std::pair< FileID, unsigned > FileIDAndOffset
@ Result
The result type of a method or function.