================
@@ -195,6 +211,98 @@ class ErrorReporter {
}
for (const auto &Note : Error.Notes)
reportNote(Note);
+
+ if (SarifWriter)
+ exportSarifResult(Error, Loc);
+ }
+
+ CharSourceRange getNoteRange(const tooling::DiagnosticMessage &Note) {
+ if (!Note.Ranges.empty())
+ return getRange(Note.Ranges.front());
+ const SourceLocation Loc = getLocation(Note.FilePath, Note.FileOffset);
+ if (Loc.isInvalid())
+ return {};
+ return getOffsetRange(SourceMgr, Loc);
+ }
+
+ SmallVector<ThreadFlow, 8> createThreadFlows(const ClangTidyError &Error) {
+ SmallVector<ThreadFlow, 8> Flows;
+ if (!StringRef(Error.DiagnosticName).starts_with("clang-analyzer-"))
+ return Flows;
+
+ Flows.reserve(Error.Notes.size());
+ for (const tooling::DiagnosticMessage &Note : Error.Notes) {
+ Flows.push_back(ThreadFlow::create()
+ .setRange(getNoteRange(Note))
+ .setMessage(Note.Message));
+ }
+ return Flows;
+ }
+
+ void exportSarifResult(const ClangTidyError &Error,
+ const SourceLocation Loc) {
+ assert(SarifWriter &&
+ "SarifWriter must be initialized to export SARIF results");
+
+ const auto [RuleIndexEntryIt, Inserted] =
+ SarifRuleIdx.try_emplace(Error.DiagnosticName, 0);
+ size_t &RuleIndex = RuleIndexEntryIt->second;
+
+ const DiagnosticsEngine::Level EffectiveLevel =
+ Error.IsWarningAsError
+ ? DiagnosticsEngine::Error
+ : static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
+
+ if (Inserted) {
+ const StringRef Name = Error.DiagnosticName;
+ SarifRule Rule = SarifRule::create().setRuleId(Name).setName(Name);
+ Rule = addDiagnosticLevelToRule(Rule, EffectiveLevel);
+ if (SarifWriter)
+ RuleIndex = SarifWriter->createRule(Rule);
+ }
+
+ SarifResult Result =
+ SarifResult::create(RuleIndex)
+ .setDiagnosticMessage(Error.Message.Message)
+ .setDiagnosticLevel(getSarifResultLevel(EffectiveLevel))
+ .addLocations(getResultRanges(Error, Loc));
+
+ if (StringRef(Error.DiagnosticName).starts_with("clang-analyzer-")) {
+ Result = Result.setThreadFlows(createThreadFlows(Error));
+ } else {
+ for (const tooling::DiagnosticMessage &Note : Error.Notes) {
+ const CharSourceRange Range = getNoteRange(Note);
+ if (Range.isValid())
+ Result = Result.addRelatedLocations(Range, Note.Message);
+ }
+ }
+ if (SarifWriter)
+ SarifWriter->appendResult(Result);
+ }
+
+ CharSourceRange getOffsetRange(SourceManager &SM, SourceLocation Loc) {
+ const std::pair<FileID, unsigned> &DecomposedLoc =
SM.getDecomposedLoc(Loc);
+ const FileID FileId = DecomposedLoc.first;
+ const unsigned Offset = DecomposedLoc.second;
+ return CharSourceRange::getCharRange(
+ Loc, Loc.getLocWithOffset(Offset < SM.getFileIDSize(FileId) ? 1 : 0));
+ }
+
+ SmallVector<CharSourceRange, 4> getResultRanges(const ClangTidyError &Error,
+ SourceLocation Loc) {
+ SmallVector<CharSourceRange, 4> Ranges;
+ Ranges.reserve(Error.Message.Ranges.size());
+ for (const FileByteRange &FBR : Error.Message.Ranges)
----------------
zeyi2 wrote:
IMO we should keep the diagnostic location separately from its highlight
ranges. Currently, when a range is present, `Loc` is not represented in the
result at all. `getNoteRange()` has the same issue for notes.
E.g.
```cpp
#define DEREF(p) (*(p))
#define NUL nullptr
int g() { int *ptr = NUL; return DEREF(ptr); }
```
With `-checks='-*,clang-analyzer-core.NullDereference'`, the text warning
points to line 3, column 34, but the SARIF location starts at column 40. The
`expanded from macro 'DEREF'` notes also move from line 1, column 18 to column
21.
https://github.com/llvm/llvm-project/pull/219182
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits