================
@@ -195,6 +210,93 @@ 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());
+
+    tooling::FileByteRange FBR;
+    FBR.FilePath = Note.FilePath;
+    FBR.FileOffset = Note.FileOffset;
+    FBR.Length = 1;
+    return getRange(FBR);
+  }
+
+  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);
+  }
+
+  SmallVector<CharSourceRange, 4> getResultRanges(const ClangTidyError &Error,
----------------
BaLiKfromUA wrote:

Not exactly a comment on this line but it might be related:

Have you tested how locations are reported for preprocessor **macro 
expansions**?

Based on my experience with clang-tidy, it sometimes requires extra logic to 
handle them, but I might be wrong!

https://github.com/llvm/llvm-project/pull/219182
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to