#include "clang/Frontend/SARIFDiagnostic.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/Sarif.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/Locale.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <string>
namespace clang {
SARIFDiagnostic::SARIFDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
DiagnosticOptions *DiagOpts,
SarifDocumentWriter *Writer)
: DiagnosticRenderer(LangOpts, DiagOpts), Writer(Writer) {}
void SARIFDiagnostic::emitDiagnosticMessage(
FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
DiagOrStoredDiag D) {
const auto *Diag = D.dyn_cast<const Diagnostic *>();
if (!Diag)
return;
SarifRule Rule = SarifRule::create().setRuleId(std::to_string(Diag->getID()));
Rule = addDiagnosticLevelToRule(Rule, Level);
unsigned RuleIdx = Writer->createRule(Rule);
SarifResult Result =
SarifResult::create(RuleIdx).setDiagnosticMessage(Message);
if (Loc.isValid())
Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
Writer->appendResult(Result);
}
SarifResult SARIFDiagnostic::addLocationToResult(
SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
ArrayRef<CharSourceRange> Ranges, const Diagnostic &Diag) {
SmallVector<CharSourceRange> Locations = {};
if (PLoc.isInvalid()) {
FileID FID = Loc.getFileID();
if (FID.isValid()) {
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
emitFilename(FE->getName(), Loc.getManager());
}
}
return Result;
}
FileID CaretFileID = Loc.getExpansionLoc().getFileID();
for (const CharSourceRange Range : Ranges) {
if (Range.isInvalid())
continue;
auto &SM = Loc.getManager();
SourceLocation B = SM.getExpansionLoc(Range.getBegin());
CharSourceRange ERange = SM.getExpansionRange(Range.getEnd());
SourceLocation E = ERange.getEnd();
bool IsTokenRange = ERange.isTokenRange();
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
continue;
unsigned TokSize = 0;
if (IsTokenRange)
TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
FullSourceLoc BF(B, SM), EF(E, SM);
SourceLocation BeginLoc = SM.translateLineCol(
BF.getFileID(), BF.getLineNumber(), BF.getColumnNumber());
SourceLocation EndLoc = SM.translateLineCol(
EF.getFileID(), EF.getLineNumber(), EF.getColumnNumber() + TokSize);
Locations.push_back(
CharSourceRange{SourceRange{BeginLoc, EndLoc}, false});
}
auto &SM = Loc.getManager();
auto FID = PLoc.getFileID();
unsigned int ColNo = (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
? PLoc.getColumn() - 1
: PLoc.getColumn();
SourceLocation DiagLoc = SM.translateLineCol(FID, PLoc.getLine(), ColNo);
Locations.push_back(
CharSourceRange{SourceRange{DiagLoc, DiagLoc}, false});
return Result.setLocations(Locations);
}
SarifRule
SARIFDiagnostic::addDiagnosticLevelToRule(SarifRule Rule,
DiagnosticsEngine::Level Level) {
auto Config = SarifReportingConfiguration::create();
switch (Level) {
case DiagnosticsEngine::Note:
Config = Config.setLevel(SarifResultLevel::Note);
break;
case DiagnosticsEngine::Remark:
Config = Config.setLevel(SarifResultLevel::None);
break;
case DiagnosticsEngine::Warning:
Config = Config.setLevel(SarifResultLevel::Warning);
break;
case DiagnosticsEngine::Error:
Config = Config.setLevel(SarifResultLevel::Error).setRank(50);
break;
case DiagnosticsEngine::Fatal:
Config = Config.setLevel(SarifResultLevel::Error).setRank(100);
break;
case DiagnosticsEngine::Ignored:
assert(false && "Invalid diagnostic type");
}
return Rule.setDefaultConfiguration(Config);
}
llvm::StringRef SARIFDiagnostic::emitFilename(StringRef Filename,
const SourceManager &SM) {
if (DiagOpts->AbsolutePath) {
auto File = SM.getFileManager().getOptionalFileRef(Filename);
if (File) {
#ifdef _WIN32
SmallString<256> TmpFilename = File->getName();
llvm::sys::fs::make_absolute(TmpFilename);
llvm::sys::path::native(TmpFilename);
llvm::sys::path::remove_dots(TmpFilename, true);
Filename = StringRef(TmpFilename.data(), TmpFilename.size());
#else
Filename = SM.getFileManager().getCanonicalName(*File);
#endif
}
}
return Filename;
}
void SARIFDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges) {
assert(false && "Not implemented in SARIF mode");
}
void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
assert(false && "Not implemented in SARIF mode");
}
void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
StringRef ModuleName) {
assert(false && "Not implemented in SARIF mode");
}
void SARIFDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc,
PresumedLoc PLoc,
StringRef ModuleName) {
assert(false && "Not implemented in SARIF mode");
}
}