================
@@ -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) {
----------------
zeyi2 wrote:
Similar to the comment above, it would silently drops notes whose locations
cannot be represented as a file range.
An simplified repro:
```cpp
FN()
```
```
clang-tidy -checks='-*,clang-diagnostic-missing-prototypes' \
-allow-no-checks -sarif-export=result.sarif input.cpp -- \
-Wmissing-prototypes '-DFN()=void f() {}'
```
The regular text output has one warning and three notes, including two
`expanded from macro 'FN'` notes without file locations.
SARIF contains the warning, but misses two notes. I think we should preserve
the note messages even when the physical locations are unavailable. This also
happens with `##` macros defined in the source, and I found the same loss in
the cppcheck project. Could we add coverage for both cases?
cc @vbvictor for this as I'm not completely sure how we should handle/support
macros from `-D`.
https://github.com/llvm/llvm-project/pull/219182
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits