@@ -177,6 +177,16 @@ std::optional<const DiagnosticInfo *> Diagnostic::getWrappedDiagnostic() const {
177
177
return std::nullopt ;
178
178
}
179
179
180
+ SourceLoc Diagnostic::getLocOrDeclLoc () const {
181
+ if (auto loc = getLoc ())
182
+ return loc;
183
+
184
+ if (auto *D = getDecl ())
185
+ return D->getLoc ();
186
+
187
+ return SourceLoc ();
188
+ }
189
+
180
190
static CharSourceRange toCharSourceRange (SourceManager &SM, SourceRange SR) {
181
191
return CharSourceRange (SM, SR.Start , Lexer::getLocForEndOfToken (SM, SR.End ));
182
192
}
@@ -1433,24 +1443,18 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
1433
1443
return std::nullopt ;
1434
1444
1435
1445
// Figure out the source location.
1436
- SourceLoc loc = diagnostic.getLoc ();
1446
+ SourceLoc loc = diagnostic.getLocOrDeclLoc ();
1437
1447
if (loc.isInvalid () && diagnostic.getDecl ()) {
1448
+ // If the location of the decl is invalid, try to pretty-print it into a
1449
+ // buffer and capture the source location there. Make sure we don't have an
1450
+ // active request running since printing AST can kick requests that may
1451
+ // themselves emit diagnostics. This won't help the underlying cycle, but it
1452
+ // at least stops us from overflowing the stack.
1438
1453
const Decl *decl = diagnostic.getDecl ();
1439
- // If a declaration was provided instead of a location, and that declaration
1440
- // has a location we can point to, use that location.
1441
- loc = decl->getLoc ();
1442
-
1443
- // If the location of the decl is invalid still, try to pretty-print the
1444
- // declaration into a buffer and capture the source location there. Make
1445
- // sure we don't have an active request running since printing AST can
1446
- // kick requests that may themselves emit diagnostics. This won't help the
1447
- // underlying cycle, but it at least stops us from overflowing the stack.
1448
- if (loc.isInvalid ()) {
1449
- PrettyPrintDeclRequest req (decl);
1450
- auto &eval = decl->getASTContext ().evaluator ;
1451
- if (!eval.hasActiveRequest (req))
1452
- loc = evaluateOrDefault (eval, req, SourceLoc ());
1453
- }
1454
+ PrettyPrintDeclRequest req (decl);
1455
+ auto &eval = decl->getASTContext ().evaluator ;
1456
+ if (!eval.hasActiveRequest (req))
1457
+ loc = evaluateOrDefault (eval, req, SourceLoc ());
1454
1458
}
1455
1459
1456
1460
auto groupID = diagnostic.getGroupID ();
0 commit comments