@@ -177,6 +177,16 @@ std::optional<const DiagnosticInfo *> Diagnostic::getWrappedDiagnostic() const {
177177 return std::nullopt ;
178178}
179179
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+
180190static CharSourceRange toCharSourceRange (SourceManager &SM, SourceRange SR) {
181191 return CharSourceRange (SM, SR.Start , Lexer::getLocForEndOfToken (SM, SR.End ));
182192}
@@ -1433,24 +1443,18 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
14331443 return std::nullopt ;
14341444
14351445 // Figure out the source location.
1436- SourceLoc loc = diagnostic.getLoc ();
1446+ SourceLoc loc = diagnostic.getLocOrDeclLoc ();
14371447 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.
14381453 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 ());
14541458 }
14551459
14561460 auto groupID = diagnostic.getGroupID ();
0 commit comments