Skip to content

Commit d93f871

Browse files
authored
Merge pull request #68709 from hamishknight/loc-picking-lawyer
2 parents ebc0be2 + c49d336 commit d93f871

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

lib/IDE/CursorInfo.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ class NodeFinder : ASTWalker {
182182
DeclContextStack.push_back(newDC);
183183
}
184184

185-
if (D->getLoc() != LocToResolve) {
185+
auto *VD = dyn_cast<ValueDecl>(D);
186+
if (!VD)
186187
return Action::Continue();
187-
}
188188

189-
if (auto VD = dyn_cast<ValueDecl>(D)) {
190-
// FIXME: ParamDecls might be closure parameters that can have ambiguous
191-
// types. The current infrastructure of just asking for the VD's type
192-
// doesn't work here. We need to inspect the constraints system solution.
193-
if (VD->hasName() && !isa<ParamDecl>(D)) {
194-
assert(Result == nullptr);
195-
Result = std::make_unique<NodeFinderDeclResult>(VD);
196-
return Action::Stop();
197-
}
198-
}
189+
// FIXME: ParamDecls might be closure parameters that can have ambiguous
190+
// types. The current infrastructure of just asking for the VD's type
191+
// doesn't work here. We need to inspect the constraints system solution.
192+
if (isa<ParamDecl>(VD))
193+
return Action::Continue();
199194

200-
return Action::Continue();
195+
if (!VD->hasName() || VD->getNameLoc() != LocToResolve)
196+
return Action::Continue();
197+
198+
assert(Result == nullptr);
199+
Result = std::make_unique<NodeFinderDeclResult>(VD);
200+
return Action::Stop();
201201
}
202202

203203
PostWalkAction walkToDeclPost(Decl *D) override {
@@ -208,6 +208,23 @@ class NodeFinder : ASTWalker {
208208
return Action::Continue();
209209
}
210210

211+
/// Retrieve the name location for an expression that supports cursor info.
212+
DeclNameLoc getExprNameLoc(Expr *E) {
213+
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
214+
return DRE->getNameLoc();
215+
216+
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E))
217+
return UDRE->getNameLoc();
218+
219+
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(E))
220+
return ODRE->getNameLoc();
221+
222+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E))
223+
return UDE->getNameLoc();
224+
225+
return DeclNameLoc();
226+
}
227+
211228
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
212229
if (auto closure = dyn_cast<ClosureExpr>(E)) {
213230
DeclContextStack.push_back(closure);
@@ -224,25 +241,13 @@ class NodeFinder : ASTWalker {
224241
}
225242
}
226243

227-
if (E->getLoc() != LocToResolve) {
244+
if (getExprNameLoc(E).getBaseNameLoc() != LocToResolve)
228245
return Action::Continue(E);
229-
}
230246

231-
switch (E->getKind()) {
232-
case ExprKind::DeclRef:
233-
case ExprKind::UnresolvedDot:
234-
case ExprKind::UnresolvedDeclRef:
235-
case ExprKind::OverloadedDeclRef: {
236-
assert(Result == nullptr);
237-
Result =
238-
std::make_unique<NodeFinderExprResult>(E, getCurrentDeclContext());
239-
return Action::Stop();
240-
}
241-
default:
242-
break;
243-
}
244-
245-
return Action::Continue(E);
247+
assert(Result == nullptr);
248+
Result =
249+
std::make_unique<NodeFinderExprResult>(E, getCurrentDeclContext());
250+
return Action::Stop();
246251
}
247252

248253
PostWalkResult<Expr *> walkToExprPost(Expr *E) override {

0 commit comments

Comments
 (0)