Skip to content

Commit d003140

Browse files
committed
[ConstraintSystem] ExpressionTimer: Accept locators as timer anchors
1 parent 9d29678 commit d003140

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ struct RestrictionOrFix {
167167

168168

169169
class ExpressionTimer {
170-
Expr* E;
170+
using AnchorType = llvm::PointerUnion<Expr *, ConstraintLocator *>;
171+
172+
AnchorType Anchor;
171173
ASTContext &Context;
172174
llvm::TimeRecord StartTime;
173175

@@ -181,8 +183,8 @@ class ExpressionTimer {
181183
public:
182184
/// This constructor sets a default threshold defined for all expressions
183185
/// via compiler flag `solver-expression-time-threshold`.
184-
ExpressionTimer(Expr *E, ConstraintSystem &CS);
185-
ExpressionTimer(Expr *E, ConstraintSystem &CS, unsigned thresholdInMillis);
186+
ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS);
187+
ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS, unsigned thresholdInMillis);
186188

187189
~ExpressionTimer();
188190

lib/Sema/ConstraintSystem.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ using namespace inference;
4343

4444
#define DEBUG_TYPE "ConstraintSystem"
4545

46-
ExpressionTimer::ExpressionTimer(Expr *E, ConstraintSystem &CS)
46+
ExpressionTimer::ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS)
4747
: ExpressionTimer(
48-
E, CS,
48+
Anchor, CS,
4949
CS.getASTContext().TypeCheckerOpts.ExpressionTimeoutThreshold) {}
5050

51-
ExpressionTimer::ExpressionTimer(Expr *E, ConstraintSystem &CS,
51+
ExpressionTimer::ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS,
5252
unsigned thresholdInMillis)
53-
: E(E), Context(CS.getASTContext()),
53+
: Anchor(Anchor), Context(CS.getASTContext()),
5454
StartTime(llvm::TimeRecord::getCurrentTime()),
5555
ThresholdInMillis(thresholdInMillis),
5656
PrintDebugTiming(CS.getASTContext().TypeCheckerOpts.DebugTimeExpressions),
@@ -64,21 +64,39 @@ ExpressionTimer::~ExpressionTimer() {
6464
// Round up to the nearest 100th of a millisecond.
6565
llvm::errs() << llvm::format("%0.2f", ceil(elapsed * 100000) / 100)
6666
<< "ms\t";
67-
E->getLoc().print(llvm::errs(), Context.SourceMgr);
67+
if (auto *E = Anchor.dyn_cast<Expr *>()) {
68+
E->getLoc().print(llvm::errs(), Context.SourceMgr);
69+
} else {
70+
auto *locator = Anchor.get<ConstraintLocator *>();
71+
locator->dump(&Context.SourceMgr, llvm::errs());
72+
}
6873
llvm::errs() << "\n";
6974
}
7075

7176
if (!PrintWarning)
7277
return;
7378

79+
ASTNode anchor;
80+
if (auto *locator = Anchor.dyn_cast<ConstraintLocator *>()) {
81+
anchor = simplifyLocatorToAnchor(locator);
82+
// If locator couldn't be simplified down to a single AST
83+
// element, let's warn about its root.
84+
if (!anchor)
85+
anchor = locator->getAnchor();
86+
} else {
87+
anchor = Anchor.get<Expr *>();
88+
}
89+
7490
const auto WarnLimit = getWarnLimit();
75-
if (WarnLimit != 0 && elapsedMS >= WarnLimit && E->getLoc().isValid())
76-
Context.Diags.diagnose(E->getLoc(), diag::debug_long_expression,
77-
elapsedMS, WarnLimit)
78-
.highlight(E->getSourceRange());
91+
if (WarnLimit != 0 && elapsedMS >= WarnLimit &&
92+
anchor.getStartLoc().isValid()) {
93+
Context.Diags
94+
.diagnose(anchor.getStartLoc(), diag::debug_long_expression, elapsedMS,
95+
WarnLimit)
96+
.highlight(anchor.getSourceRange());
97+
}
7998
}
8099

81-
82100
ConstraintSystem::ConstraintSystem(DeclContext *dc,
83101
ConstraintSystemOptions options)
84102
: Context(dc->getASTContext()), DC(dc), Options(options),

0 commit comments

Comments
 (0)