================
@@ -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:

Yeah, it's crashing:

```cpp
#define DEREF(p) (*(p))
#define NUL nullptr

int g() {
  int *ptr = NUL;
  return DEREF(ptr);
}
```


``` shell
build/bin/clang-tidy a.cpp -checks='-*,clang-analyzer-core.*' 
-sarif-export=a.sarif --
1 warning generated.
/home/balik/Desktop/llvm-project-pr219182/a.cpp:6:10: warning: Dereference of 
null pointer (loaded from variable 'ptr') [clang-analyzer-core.NullDereference]
    6 |   return DEREF(ptr);
      |          ^
/home/balik/Desktop/llvm-project-pr219182/a.cpp:1:18: note: expanded from macro 
'DEREF'
    1 | #define DEREF(p) (*(p))
      |                  ^  ~
/home/balik/Desktop/llvm-project-pr219182/a.cpp:5:3: note: 'ptr' initialized to 
a null pointer value
    5 |   int *ptr = NUL;
      |   ^~~~~~~~
/home/balik/Desktop/llvm-project-pr219182/a.cpp:6:10: note: Dereference of null 
pointer (loaded from variable 'ptr')
    6 |   return DEREF(ptr);
      |          ^
/home/balik/Desktop/llvm-project-pr219182/a.cpp:1:18: note: expanded from macro 
'DEREF'
    1 | #define DEREF(p) (*(p))
      |                  ^  ~
clang-tidy: 
/home/balik/Desktop/llvm-project-pr219182/clang/lib/Basic/Sarif.cpp:210: 
llvm::json::Object clang::SarifDocumentWriter::createPhysicalLocation(const 
clang::CharSourceRange&): Assertion `R.isValid() && "Cannot create a 
physicalLocation from invalid SourceRange!"' failed.

```

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