Skip to content

Commit f06b568

Browse files
committed
[IDE] Drop an unnecessarily inefficient use of depth maps
1 parent 99555dc commit f06b568

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,30 +2939,36 @@ static NumberLiteralExpr *getTrailingNumberLiteral(ResolvedCursorInfo Tok) {
29392939
// This cursor must point to the start of an expression.
29402940
if (Tok.Kind != CursorInfoKind::ExprStart)
29412941
return nullptr;
2942-
Expr *Parent = Tok.TrailingExpr;
2943-
assert(Parent);
2944-
2945-
// Check if an expression is a number literal.
2946-
auto IsLiteralNumber = [&](Expr *E) -> NumberLiteralExpr* {
2947-
if (auto *NL = dyn_cast<NumberLiteralExpr>(E)) {
2948-
2949-
// The sub-expression must have the same start loc with the outermost
2950-
// expression, i.e. the cursor position.
2951-
if (Parent->getStartLoc().getOpaquePointerValue() ==
2952-
E->getStartLoc().getOpaquePointerValue()) {
2953-
return NL;
2954-
}
2955-
}
2956-
return nullptr;
2957-
};
2942+
29582943
// For every sub-expression, try to find the literal expression that matches
29592944
// our criteria.
2960-
for (auto Pair: Parent->getDepthMap()) {
2961-
if (auto Result = IsLiteralNumber(Pair.getFirst())) {
2962-
return Result;
2945+
class FindLiteralNumber : public ASTWalker {
2946+
Expr * const parent;
2947+
2948+
public:
2949+
NumberLiteralExpr *found = nullptr;
2950+
2951+
explicit FindLiteralNumber(Expr *parent) : parent(parent) { }
2952+
2953+
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
2954+
if (auto *literal = dyn_cast<NumberLiteralExpr>(expr)) {
2955+
// The sub-expression must have the same start loc with the outermost
2956+
// expression, i.e. the cursor position.
2957+
if (!found &&
2958+
parent->getStartLoc().getOpaquePointerValue() ==
2959+
expr->getStartLoc().getOpaquePointerValue()) {
2960+
found = literal;
2961+
}
2962+
}
2963+
2964+
return { found == nullptr, expr };
29632965
}
2964-
}
2965-
return nullptr;
2966+
};
2967+
2968+
auto parent = Tok.TrailingExpr;
2969+
FindLiteralNumber finder(parent);
2970+
parent->walk(finder);
2971+
return finder.found;
29662972
}
29672973

29682974
static std::string insertUnderscore(StringRef Text) {

0 commit comments

Comments
 (0)