[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-08 Thread via cfe-commits

github-actions[bot] wrote:



@anonymouspc Congratulations on having your first Pull Request (PR) merged into 
the LLVM Project!

Your changes will be combined with recent changes from other authors, then 
tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a 
problem with a build, you may receive a report in an email or a comment on this 
PR.

Please check whether problems have been caused by your change specifically, as 
the builds can include changes from many authors. It is not uncommon for your 
change to be included in a build that fails due to someone else's changes, or 
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail 
[here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr).

If your change does cause a problem, it may be reverted, or you can revert it 
yourself. This is a normal part of [LLVM 
development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy).
 You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are 
working as expected, well done!


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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-08 Thread Aaron Ballman via cfe-commits

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-08 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman approved this pull request.

LGTM!

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-08 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- 
clang/include/clang/Basic/Sarif.h 
clang/include/clang/Frontend/SARIFDiagnostic.h clang/lib/Basic/Sarif.cpp 
clang/lib/Frontend/SARIFDiagnostic.cpp 
clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp 
clang/unittests/Basic/SarifTest.cpp --diff_from_common_commit
``

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:





View the diff from clang-format here.


``diff
diff --git a/clang/unittests/Basic/SarifTest.cpp 
b/clang/unittests/Basic/SarifTest.cpp
index 42e85085d..185dfaee1 100644
--- a/clang/unittests/Basic/SarifTest.cpp
+++ b/clang/unittests/Basic/SarifTest.cpp
@@ -290,7 +290,7 @@ TEST_F(SarifDocumentWriterTest, 
checkSerializingResultsWithCustomRuleConfig) {
 TEST_F(SarifDocumentWriterTest, checkSerializingArtifacts) {
   // GIVEN:
   const std::string ExpectedOutput =
-  
R"({"$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/cos02/schemas/sarif-schema-2.1.0.json","runs":[{"artifacts":[{"length":40,"location":{"index":0,"uri":"file:///main.cpp"},"mimeType":"text/plain","roles":["resultFile"]}],"columnKind":"unicodeCodePoints","results":[{"level":"error","locations":[{"physicalLocation":{"artifactLocation":{"index":0,"uri":"file:///main.cpp"},"region":{"endColumn":14,"startColumn":14,"startLine":3}}}],"message":{"text":"expected
 ';' after top level 
declarator"},"relatedLocations":[{"physicalLocation":{"artifactLocation":{"index":0,"uri":"file:///main.cpp"},"region":{"endColumn":14,"startColumn":14,"startLine":3}}}],"ruleId":"clang.unittest","ruleIndex":0}],"tool":{"driver":{"fullName":"sarif
 test 
runner","informationUri":"https://clang.llvm.org/docs/UsersManual.html","language":"en-US","name":"sarif
 
test","rules":[{"defaultConfiguration":{"enabled":true,"level":"warning","rank":-1},"fullDescription":{"text":"Example
 rule created during unit tests"},"id":"clang.unittest","name":"clang unit 
test"}],"version":"1.0.0"}}}],"version":"2.1.0"})";
+  
R"({"$schema":"https://docs.oasis-open.org/sarif/sarif/v2.1.0/cos02/schemas/sarif-schema-2.1.0.json","runs":[{"artifacts":[{"length":40,"location":{"index":0,"uri":"file:///main.cpp"},"mimeType":"text/plain","roles":["resultFile"]}],"columnKind":"unicodeCodePoints","results":[{"level":"error","locations":[{"physicalLocation":{"artifactLocation":{"index":0,"uri":"file:///main.cpp"},"region":{"endColumn":14,"startColumn":14,"startLine":3}}}],"message":{"text":"expected
 ';' after top level 
declarator"},"relatedLocations":[{"physicalLocation":{"artifactLocation":{"index":0,"uri":"file:///main.cpp"},"region":{"endColumn":14,"startColumn":14,"startLine":3}}}],"ruleId":"clang.unittest","ruleIndex":0}],"tool":{"driver":{"fullName":"sarif
 test 
runner","informationUri":"https://clang.llvm.org/docs/UsersManual.html","language":"en-US","name":"sarif
 
test","rules":[{"defaultConfiguration":{"enabled":true,"level":"warning","rank":-1},"fullDescription":{"text":"Example
 rule created during unit tests"},"id":"clang.unittest","name":"clang unit 
test"}],"version":"1.0.0"}}}],"version":"2.1.0"})";
 
   SarifDocumentWriter Writer{SourceMgr};
   const SarifRule &Rule =

``




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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-06 Thread via cfe-commits

anonymouspc wrote:

> Thank you for the fix! I think you should add a release note to 
> `clang/docs/ReleaseNotes.rst` so users know about the fixed crash. I think it 
> probably also makes sense to add a test case to 
> `clang/unittests/Basic/SarifTest.cpp` showing the behavior of include 
> locations and demonstrating the fix.

Sure!
- I added a note to the release notes.
- I modified the `SarifDocumentWriterTest.checkSerializingArtifacts` test in 
`clang/unittests/Basic/SarifTest.cpp`. The `SarifDocumentWriterTest` now checks 
the serialization of both `Location` and the new `relatedLocation`.
  - *It seems that the existing tests of `SarifTest.cpp` focuses on "whether 
the `SarifDocumentWriter` can correctly serialize a given message", instead of 
"how we produce a new diagnostic", So I simply added the same `DiagLoc` (as the 
`Location` one above) to the `relatedLocation` here as well.*
- *Currently the `relatedLocation` always points to an "In file included 
from".*
- *In the future (when clang supports nested/indented results, as both GCC 
and MSVC has already supported nesting/indentation in sarif mode), the 
`relatedLocation` can also refer to a common source range as well.*

Welcome for code review, thank you! Happy post-thanksgiving. :)

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-06 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 0b2bb0614c6c43d4f6d0dfb7d4c32a1acbe346c4 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH] [Clang][Diagnose] Minimal support on emit-include-location in
 sarif mode

Implement minimal relatedLocations support for include/module notes to avoid
crashes when using -fdiagnostics-format=sarif.

add test and release noes
---
 clang/docs/ReleaseNotes.rst   |  4 ++
 clang/include/clang/Basic/Sarif.h | 18 ++-
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 54 ++-
 .../StaticAnalyzer/Core/SarifDiagnostics.cpp  |  2 +-
 clang/unittests/Basic/SarifTest.cpp   |  8 +--
 7 files changed, 84 insertions(+), 20 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3526ffb40f350..db4c2dd769762 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -449,6 +449,10 @@ Improvements to Clang's diagnostics
   comparison operators when mixed with bitwise operators in enum value 
initializers.
   This can be locally disabled by explicitly casting the initializer value.
 
+- Fixed a crash when enabling ``-fdiagnostics-format=sarif`` and the output 
+  carries messages like 'In file included from ...' or 'In module ...'.
+  Now the include/import locations are written into 
`sarif.run.result.relatedLocations`.
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..6f82d253876d9 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -354,16 +355,29 @@ class SarifResult {
 return *this;
   }
 
-  SarifResult setLocations(llvm::ArrayRef DiagLocs) {
+  SarifResult addLocations(llvm::ArrayRef DiagLocs) {
 #ifndef NDEBUG
 for (const auto &Loc : DiagLocs) {
   assert(Loc.isCharRange() &&
  "SARIF Results require character granular source ranges!");
 }
 #endif
-Locations.assign(DiagLocs.begin(), DiagLocs.end());
+Locations.append(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult addRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(
+  Loc.isCharRange() &&
+  "SARIF RelatedLocations require character granular source ranges!");
+}
+#endif
+RelatedLocations.append(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..7a6f27eb3b9fa 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -63,10 +63,20 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, FullSourceLoc Loc,
+ PresumedLoc PLoc);
+
+  llvm::SmallVector
+  getSarifLocation(FullSourceLoc Loc, PresumedLoc PLoc,
+   ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+
+  llvm::SmallVector>
+  RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..2cd32ce97ea85 100644
--- a/clang/lib/Frontend/SARIFDiagnost

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-06 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 49adc295407650f1da1b70ffb1c596907564e0b4 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH 1/2] [Clang][Diagnose] Minimal support on
 emit-include-location in sarif mode

Implement minimal relatedLocations support for include/module notes to avoid
crashes when using -fdiagnostics-format=sarif.
---
 clang/include/clang/Basic/Sarif.h | 14 +
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 54 ++-
 4 files changed, 73 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..b4f3610cc7568 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,19 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(
+  Loc.isCharRange() &&
+  "SARIF RelatedLocations require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..7a6f27eb3b9fa 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -63,10 +63,20 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, FullSourceLoc Loc,
+ PresumedLoc PLoc);
+
+  llvm::SmallVector
+  getSarifLocation(FullSourceLoc Loc, PresumedLoc PLoc,
+   ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+
+  llvm::SmallVector>
+  RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..fd765f0eacbb2 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,48 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
+ StringRef ModuleName) {
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
 SarifResult SARIFDiagnostic::addLocationToResult(
 SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
 ArrayRef Ranges, const Diagnostic &Diag) {
+  auto Locations = getSarifLocation(

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-05 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman commented:

Thank you for the fix! I think you should add a release note to 
`clang/docs/ReleaseNotes.rst` so users know about the fixed crash. I think it 
probably also makes sense to add a test case to 
`clang/unittests/Basic/SarifTest.cpp` showing the behavior of include locations 
and demonstrating the fix.

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-05 Thread via cfe-commits

anonymouspc wrote:

@haoNoQ @Xazax-hun @steakhal 
I'm a new contributor and I hope for a code review. **Thank you sincerely!**

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-05 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 49adc295407650f1da1b70ffb1c596907564e0b4 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH] [Clang][Diagnose] Minimal support on emit-include-location in
 sarif mode

Implement minimal relatedLocations support for include/module notes to avoid
crashes when using -fdiagnostics-format=sarif.
---
 clang/include/clang/Basic/Sarif.h | 14 +
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 54 ++-
 4 files changed, 73 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..b4f3610cc7568 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,19 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(
+  Loc.isCharRange() &&
+  "SARIF RelatedLocations require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..7a6f27eb3b9fa 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -63,10 +63,20 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, FullSourceLoc Loc,
+ PresumedLoc PLoc);
+
+  llvm::SmallVector
+  getSarifLocation(FullSourceLoc Loc, PresumedLoc PLoc,
+   ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+
+  llvm::SmallVector>
+  RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..fd765f0eacbb2 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,48 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
+ StringRef ModuleName) {
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
 SarifResult SARIFDiagnostic::addLocationToResult(
 SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
 ArrayRef Ranges, const Diagnostic &Diag) {
+  auto Locations = getSarifLocation(Loc,

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-05 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 983aa785ee374c9c1dcbee4846dafa3ccc62b793 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH 1/2] [Clang][Diagnose] Minimal support on
 emit-include-location in sarif mode

Implement minimal relatedLocations support for include/module notes to avoid
crashes when using -fdiagnostics-format=sarif.
---
 clang/include/clang/Basic/Sarif.h | 13 +
 .../include/clang/Frontend/SARIFDiagnostic.h  |  8 +++
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 52 ++-
 4 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..3ecbe4754e0df 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF RelatedLocations require character granular source 
ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..2762ad962da59 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -63,10 +63,18 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..684238d246026 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,46 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, 
+ StringRef ModuleName) {
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
 SarifResult SARIFDiagnostic::addLocationToResult(
 SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
 ArrayRef Ranges, const 

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-03 Thread via cfe-commits

anonymouspc wrote:

TODO: 

1. Support nested compile outputs in SARIF mode
   - Attach `SARIFResults.relatedLocations (diagnostics, with level = note)` to 
`SARIFResult.relatedLocations (with level = error/warning)`.
   - *As both GCC and MSVC supports nesting in SARIF mode. Below is an example 
of GCC output as rendered by [my vscode sarif 
extension](https://github.com/anonymouspc/cppsarif):*
   - https://github.com/user-attachments/assets/a8a2-eec0-453f-92b0-1953e1e3f75f";
 />

2. Add `sarif.run.result.relatedLocation.relationship`
   - Introduce `relationship` (with value = either `"include"` or 
`"isIncludedBy"`), to represent inclusion relationships between artifacts.

3. Add compilation options for `clang++` to export `.sarif` files while still 
emitting normal text diagnostics
   - Many of us wanna see text in stderr and at the same time save `.sarif` 
files for other use.
   - Currently, `clangd` immediately crashes when `clang++` is invoked with 
`-fdiagnostics-format=sarif`, because `clangd` simply forwards 
`-fdiagnostics-format` argument but expects text-form diagnostics.
   - GCC solves this via 
`-fdiagnostics-add-output=sarif:file=path/to/file.sarif`.

*(If clang is willing to accept contributions on SARIF diagnostic, I will be 
thrilled to complete the above targets (however, must be after my final exam in 
school at about 12/21...))*

# Thank you!

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 983aa785ee374c9c1dcbee4846dafa3ccc62b793 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH] [Clang][Diagnose] Minimal support on emit-include-location in
 sarif mode

Implement minimal relatedLocations support for include/module notes to avoid
crashes when using -fdiagnostics-format=sarif.
---
 clang/include/clang/Basic/Sarif.h | 13 +
 .../include/clang/Frontend/SARIFDiagnostic.h  |  8 +++
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 52 ++-
 4 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9b..3ecbe4754e0df8 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF RelatedLocations require character granular source 
ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109c..2762ad962da591 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -63,10 +63,18 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e96..448de96d474af2 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e6..684238d2460262 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,46 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, 
+ StringRef ModuleName) {
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
 SarifResult SARIFDiagnostic::addLocationToResult(
 SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
 ArrayRef Ranges, co

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 2151f3b413f839dd8e17acf3e9f718772bb5a770 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH 1/3] [Clang][Diagnose] Minimal support on
 emit-include-location in sarif mode

---
 clang/include/clang/Basic/Sarif.h | 13 +
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 +++-
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 55 ++-
 4 files changed, 71 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..154b72d7aa520 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF Results require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..5efe665b05ba9 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -58,15 +58,23 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   // Shared between SARIFDiagnosticPrinter and this renderer.
   SarifDocumentWriter *Writer;
 
-  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
+  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc, 
   PresumedLoc PLoc,
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..33c3b83496a1b 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,47 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, 
+ StringRef ModuleName) {
+  RelatedLocationsCache.push

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

https://github.com/anonymouspc updated 
https://github.com/llvm/llvm-project/pull/170415

>From 2151f3b413f839dd8e17acf3e9f718772bb5a770 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH 1/2] [Clang][Diagnose] Minimal support on
 emit-include-location in sarif mode

---
 clang/include/clang/Basic/Sarif.h | 13 +
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 +++-
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 55 ++-
 4 files changed, 71 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..154b72d7aa520 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF Results require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..5efe665b05ba9 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -58,15 +58,23 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   // Shared between SARIFDiagnosticPrinter and this renderer.
   SarifDocumentWriter *Writer;
 
-  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
+  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc, 
   PresumedLoc PLoc,
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..33c3b83496a1b 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,47 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
+  RelatedLocationsCache.push_back({Loc, PLoc});
+}
+
+void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, 
+ StringRef ModuleName) {
+  RelatedLocationsCache.push

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: anonymous (anonymouspc)


Changes

Currently, invoking `clang++` with `-fdiagnostics-format=sarif` causes a crash, 
with stack traces indicating that `SARIFDiagnostic::emitIncludeLocation` is 
unimplemented.

This PR adds minimal support for converting `In file included from ...` and `In 
module ...` into `SARIF.result.relatedLocations`. With this change, `clang++ 
-fdiagnostics-format=sarif` no longer crashes and now provides a minimal amount 
of useful information.

Thank you.

---
Full diff: https://github.com/llvm/llvm-project/pull/170415.diff


4 Files Affected:

- (modified) clang/include/clang/Basic/Sarif.h (+13) 
- (modified) clang/include/clang/Frontend/SARIFDiagnostic.h (+9-1) 
- (modified) clang/lib/Basic/Sarif.cpp (+8) 
- (modified) clang/lib/Frontend/SARIFDiagnostic.cpp (+41-14) 


``diff
diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..154b72d7aa520 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF Results require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..5efe665b05ba9 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -58,15 +58,23 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   // Shared between SARIFDiagnosticPrinter and this renderer.
   SarifDocumentWriter *Writer;
 
-  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
+  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc, 
   PresumedLoc PLoc,
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..33c3b83496a1b 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,47 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included from ...
+  // error: ...
+  //
+  // At this time We cannot peek the SarifRule. But what we
+  // do is to push it into a cache and wait for next time
+  // \ref SARIFDiagnostic::emit

[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

github-actions[bot] wrote:



Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this 
page.

If this is not working for you, it is probably because you do not have write 
permissions for the repository. In which case you can instead tag reviewers by 
name in a comment by using `@` followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a 
review by "ping"ing the PR by adding a comment “Ping”. The common courtesy 
"ping" rate is once a week. Please remember that you are asking for valuable 
time from other developers.

If you have further questions, they may be answered by the [LLVM GitHub User 
Guide](https://llvm.org/docs/GitHub.html).

You can also ask questions in a comment on this PR, on the [LLVM 
Discord](https://discord.com/invite/xS7Z362) or on the 
[forums](https://discourse.llvm.org/).

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


[clang] [Clang][Diagnose] Minimal support on emit-include-location in sarif mode (PR #170415)

2025-12-02 Thread via cfe-commits

https://github.com/anonymouspc created 
https://github.com/llvm/llvm-project/pull/170415

Currently, invoking `clang++` with `-fdiagnostics-format=sarif` causes a crash, 
with stack traces indicating that `SARIFDiagnostic::emitIncludeLocation` is 
unimplemented.

This PR adds minimal support for converting `In file included from ...` and `In 
module ...` into `SARIF.result.relatedLocations`. With this change, `clang++ 
-fdiagnostics-format=sarif` no longer crashes and now provides a minimal amount 
of useful information.

Thank you.

>From 2151f3b413f839dd8e17acf3e9f718772bb5a770 Mon Sep 17 00:00:00 2001
From: anonymouspc 
Date: Wed, 3 Dec 2025 11:27:53 +0800
Subject: [PATCH] [Clang][Diagnose] Minimal support on emit-include-location in
 sarif mode

---
 clang/include/clang/Basic/Sarif.h | 13 +
 .../include/clang/Frontend/SARIFDiagnostic.h  | 10 +++-
 clang/lib/Basic/Sarif.cpp |  8 +++
 clang/lib/Frontend/SARIFDiagnostic.cpp| 55 ++-
 4 files changed, 71 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/Sarif.h 
b/clang/include/clang/Basic/Sarif.h
index a88d1ee2965a9..154b72d7aa520 100644
--- a/clang/include/clang/Basic/Sarif.h
+++ b/clang/include/clang/Basic/Sarif.h
@@ -325,6 +325,7 @@ class SarifResult {
   std::string HostedViewerURI;
   llvm::SmallDenseMap PartialFingerprints;
   llvm::SmallVector Locations;
+  llvm::SmallVector RelatedLocations;
   llvm::SmallVector ThreadFlows;
   std::optional LevelOverride;
 
@@ -364,6 +365,18 @@ class SarifResult {
 Locations.assign(DiagLocs.begin(), DiagLocs.end());
 return *this;
   }
+
+  SarifResult setRelatedLocations(llvm::ArrayRef DiagLocs) {
+#ifndef NDEBUG
+for (const auto &Loc : DiagLocs) {
+  assert(Loc.isCharRange() &&
+ "SARIF Results require character granular source ranges!");
+}
+#endif
+RelatedLocations.assign(DiagLocs.begin(), DiagLocs.end());
+return *this;
+  }
+
   SarifResult setThreadFlows(llvm::ArrayRef ThreadFlowResults) {
 ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
 return *this;
diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h 
b/clang/include/clang/Frontend/SARIFDiagnostic.h
index 780f36c874109..5efe665b05ba9 100644
--- a/clang/include/clang/Frontend/SARIFDiagnostic.h
+++ b/clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -58,15 +58,23 @@ class SARIFDiagnostic : public DiagnosticRenderer {
   // Shared between SARIFDiagnosticPrinter and this renderer.
   SarifDocumentWriter *Writer;
 
-  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
+  SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc, 
   PresumedLoc PLoc,
   ArrayRef Ranges,
   const Diagnostic &Diag);
 
+  SarifResult addRelatedLocationToResult(SarifResult Result, 
+ FullSourceLoc Loc, PresumedLoc PLoc);
+  
+  llvm::SmallVector getSarifLocation(FullSourceLoc Loc, 
PresumedLoc PLoc,
+  
ArrayRef Ranges);
+
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
  DiagnosticsEngine::Level Level);
 
   llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
+  
+  llvm::SmallVector> 
RelatedLocationsCache;
 };
 
 } // end namespace clang
diff --git a/clang/lib/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp
index b3fb9a21249e9..448de96d474af 100644
--- a/clang/lib/Basic/Sarif.cpp
+++ b/clang/lib/Basic/Sarif.cpp
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult 
&Result) {
 Ret["locations"] = std::move(Locs);
   }
 
+  if (!Result.RelatedLocations.empty()) {
+json::Array ReLocs;
+for (auto &Range : Result.RelatedLocations) {
+  ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
+}
+Ret["relatedLocations"] = std::move(ReLocs);
+  }
+
   if (!Result.PartialFingerprints.empty()) {
 json::Object fingerprints = {};
 for (auto &pair : Result.PartialFingerprints) {
diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp 
b/clang/lib/Frontend/SARIFDiagnostic.cpp
index ac27d7480de3e..33c3b83496a1b 100644
--- a/clang/lib/Frontend/SARIFDiagnostic.cpp
+++ b/clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -58,12 +58,47 @@ void SARIFDiagnostic::emitDiagnosticMessage(
   if (Loc.isValid())
 Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
 
+  for (auto& [RelLoc, RelPLoc] : RelatedLocationsCache)
+Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
+  RelatedLocationsCache.clear();
+
   Writer->appendResult(Result);
 }
 
+void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) 
{
+  // We always emit include location before results, for example:
+  //
+  // In file included from ...
+  // In file included