Skip to content

Commit 382a52e

Browse files
committed
[AST] NFC: Add Diagnostic::getLocOrDeclLoc
1 parent 0cfb11b commit 382a52e

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ namespace swift {
217217
const class Decl *getDecl() const { return Decl; }
218218
DiagnosticBehavior getBehaviorLimit() const { return BehaviorLimit; }
219219

220+
/// Retrieve the stored SourceLoc, or the location of the stored Decl.
221+
SourceLoc getLocOrDeclLoc() const;
222+
220223
void setLoc(SourceLoc loc) { Loc = loc; }
221224
void setIsChildNote(bool isChildNote) { IsChildNote = isChildNote; }
222225
void setDecl(const class Decl *decl) { Decl = decl; }

lib/AST/DiagnosticEngine.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
180190
static 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

Comments
 (0)