@@ -58,12 +58,48 @@ void SARIFDiagnostic::emitDiagnosticMessage(
5858 if (Loc.isValid ())
5959 Result = addLocationToResult (Result, Loc, PLoc, Ranges, *Diag);
6060
61+ for (auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
62+ Result = addRelatedLocationToResult (Result, RelLoc, RelPLoc);
63+ RelatedLocationsCache.clear ();
64+
6165 Writer->appendResult (Result);
6266}
6367
68+ void SARIFDiagnostic::emitIncludeLocation (FullSourceLoc Loc, PresumedLoc PLoc) {
69+ // We always emit include location before results, for example:
70+ //
71+ // In file included from ...
72+ // In file included from ...
73+ // error: ...
74+ //
75+ // At this time We cannot peek the SarifRule. But what we
76+ // do is to push it into a cache and wait for next time
77+ // \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
78+ RelatedLocationsCache.push_back ({Loc, PLoc});
79+ }
80+
81+ void SARIFDiagnostic::emitImportLocation (FullSourceLoc Loc, PresumedLoc PLoc,
82+ StringRef ModuleName) {
83+ RelatedLocationsCache.push_back ({Loc, PLoc});
84+ }
85+
6486SarifResult SARIFDiagnostic::addLocationToResult (
6587 SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
6688 ArrayRef<CharSourceRange> Ranges, const Diagnostic &Diag) {
89+ auto Locations = getSarifLocation (Loc, PLoc, Ranges);
90+ return Result.addLocations (Locations);
91+ }
92+
93+ SarifResult SARIFDiagnostic::addRelatedLocationToResult (SarifResult Result,
94+ FullSourceLoc Loc,
95+ PresumedLoc PLoc) {
96+ auto Locations = getSarifLocation (Loc, PLoc, {});
97+ return Result.addRelatedLocations (Locations);
98+ }
99+
100+ llvm::SmallVector<CharSourceRange>
101+ SARIFDiagnostic::getSarifLocation (FullSourceLoc Loc, PresumedLoc PLoc,
102+ ArrayRef<CharSourceRange> Ranges) {
67103 SmallVector<CharSourceRange> Locations = {};
68104
69105 if (PLoc.isInvalid ()) {
@@ -75,7 +111,7 @@ SarifResult SARIFDiagnostic::addLocationToResult(
75111 // FIXME(llvm-project/issues/57366): File-only locations
76112 }
77113 }
78- return Result ;
114+ return {} ;
79115 }
80116
81117 FileID CaretFileID = Loc.getExpansionLoc ().getFileID ();
@@ -127,10 +163,11 @@ SarifResult SARIFDiagnostic::addLocationToResult(
127163 SourceLocation DiagLoc = SM.translateLineCol (FID, PLoc.getLine (), ColNo);
128164
129165 // FIXME(llvm-project/issues/57366): Properly process #line directives.
130- Locations.push_back (
131- CharSourceRange{SourceRange{DiagLoc, DiagLoc}, /* ITR = */ false });
166+ CharSourceRange Range = {SourceRange{DiagLoc, DiagLoc}, /* ITR = */ false };
167+ if (Range.isValid ())
168+ Locations.push_back (std::move (Range));
132169
133- return Result. setLocations ( Locations) ;
170+ return Locations;
134171}
135172
136173SarifRule
@@ -207,15 +244,6 @@ void SARIFDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
207244 assert (false && " Not implemented in SARIF mode" );
208245}
209246
210- void SARIFDiagnostic::emitIncludeLocation (FullSourceLoc Loc, PresumedLoc PLoc) {
211- assert (false && " Not implemented in SARIF mode" );
212- }
213-
214- void SARIFDiagnostic::emitImportLocation (FullSourceLoc Loc, PresumedLoc PLoc,
215- StringRef ModuleName) {
216- assert (false && " Not implemented in SARIF mode" );
217- }
218-
219247void SARIFDiagnostic::emitBuildingModuleLocation (FullSourceLoc Loc,
220248 PresumedLoc PLoc,
221249 StringRef ModuleName) {
0 commit comments