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"
55 Rule = addDiagnosticLevelToRule(Rule, Level);
57 unsigned RuleIdx = Writer->createRule(Rule);
65 for (
auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
66 Result = addRelatedLocationToResult(
Result, RelLoc, RelPLoc);
67 RelatedLocationsCache.clear();
69 Writer->appendResult(
Result);
82 RelatedLocationsCache.push_back({Loc, PLoc});
86 StringRef ModuleName) {
87 RelatedLocationsCache.push_back({Loc, PLoc});
93 auto Locations = getSarifLocation(Loc, PLoc, Ranges);
94 return Result.addLocations(Locations);
97SarifResult SARIFDiagnostic::addRelatedLocationToResult(SarifResult Result,
100 auto Locations = getSarifLocation(Loc, PLoc, {});
101 return Result.addRelatedLocations(Locations);
107 SmallVector<CharSourceRange> Locations = {};
109 if (PLoc.isInvalid()) {
111 FileID FID = Loc.getFileID();
114 emitFilename(FE->getName(), Loc.getManager());
121 FileID CaretFileID = Loc.getExpansionLoc().getFileID();
123 for (
const CharSourceRange Range : Ranges) {
125 if (
Range.isInvalid())
128 auto &
SM = Loc.getManager();
129 SourceLocation B =
SM.getExpansionLoc(
Range.getBegin());
130 CharSourceRange ERange =
SM.getExpansionRange(
Range.getEnd());
131 SourceLocation E = ERange.getEnd();
132 bool IsTokenRange = ERange.isTokenRange();
139 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
144 unsigned TokSize = 0;
148 FullSourceLoc BF(B,
SM), EF(E,
SM);
149 SourceLocation BeginLoc =
SM.translateLineCol(
150 BF.getFileID(), BF.getLineNumber(), BF.getColumnNumber());
151 SourceLocation EndLoc =
SM.translateLineCol(
152 EF.getFileID(), EF.getLineNumber(), EF.getColumnNumber() + TokSize);
155 CharSourceRange{SourceRange{BeginLoc, EndLoc},
false});
160 auto &
SM = Loc.getManager();
161 auto FID = PLoc.getFileID();
163 unsigned int ColNo = (
LangOpts.MSCompatibilityVersion &&
165 ? PLoc.getColumn() - 1
167 SourceLocation DiagLoc =
SM.translateLineCol(FID, PLoc.getLine(), ColNo);
170 CharSourceRange
Range = {SourceRange{DiagLoc, DiagLoc},
false};
172 Locations.push_back(std::move(Range));
178SARIFDiagnostic::addDiagnosticLevelToRule(
SarifRule Rule,
199 assert(
false &&
"Invalid diagnostic type");
202 return Rule.setDefaultConfiguration(Config);
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.
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.
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 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.
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.