================
@@ -195,6 +213,94 @@ class ErrorReporter {
     }
     for (const auto &Note : Error.Notes)
       reportNote(Note);
+
+    if (SarifWriter)
+      exportSarifResult(Error, Loc);
+  }
+
+  CharSourceRange getNoteRange(const tooling::DiagnosticMessage &Note) {
+    const SourceLocation Loc = getLocation(Note.FilePath, Note.FileOffset);
+    return Loc.isValid() ? getOffsetRange(SourceMgr, Loc) : CharSourceRange();
+  }
+
+  SmallVector<ThreadFlow, 8> createThreadFlows(const ClangTidyError &Error) {
+    SmallVector<ThreadFlow, 8> 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) {
+    if (!SarifWriter)
+      return;
+
+    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);
+      RuleIndex = SarifWriter->createRule(Rule);
+    }
+
+    SarifResult Result =
+        SarifResult::create(RuleIndex)
+            .setDiagnosticMessage(Error.Message.Message)
+            .setDiagnosticLevel(getSarifResultLevel(EffectiveLevel));
+    // TODO: Extend SarifDocumentWriter to add an annotations property that can
+    // represent highlight ranges, when both a valid location and highlight
+    // ranges are present.
+    Result =
+        (Loc.isValid() ? Result.addLocations(getOffsetRange(SourceMgr, Loc))
+                       : Result.addLocations(getResultRanges(Error)));
+
+    // Same-location, same-message diagnostics from other checks are merged,
+    // with the first alphabetically as the check name, not necessarily the
+    // canon name.
+    if (!Error.EnabledDiagnosticAliases.empty())
+      Result = Result.addProperty(
+          "enabledAliases", llvm::join(Error.EnabledDiagnosticAliases, ","));
+
+    // TODO: CSA stores fullDescription from the HELPTEXT field in 
Checkers.inc,
+    // strip clang-analyzer- prefix then populate fullDescription with this.
+    // Also, add helpUri with this for in-tree checks, pointing to their
+    // documentation pages.
+    if (StringRef(Error.DiagnosticName).starts_with("clang-analyzer-"))
+      Result = Result.setThreadFlows(createThreadFlows(Error));
+    else
+      for (const tooling::DiagnosticMessage &Note : Error.Notes)
+        Result = Result.addRelatedLocations(getNoteRange(Note), Note.Message);
+    if (SarifWriter)
----------------
vbvictor wrote:

Remove this probably? We asserted at the beginning

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