Skip to content

Commit 57fa43c

Browse files
authored
[RangeInfo] Avoid analyzing implicit ASTNodes. rdar://31773556 (swiftlang#9210)
1 parent 7913e98 commit 57fa43c

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

include/swift/AST/ASTNode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ namespace swift {
6161
FUNC(Expr)
6262
FUNC(Decl)
6363
#undef FUNC
64+
65+
/// Whether the AST node is implicit.
66+
bool isImplicit() const;
6467
};
6568
} // namespace swift
6669

lib/AST/ASTNode.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ DeclContext *ASTNode::getAsDeclContext() const {
5656
return nullptr;
5757
}
5858

59+
bool ASTNode::isImplicit() const {
60+
if (const Expr *E = this->dyn_cast<Expr*>())
61+
return E->isImplicit();
62+
if (const Stmt *S = this->dyn_cast<Stmt*>())
63+
return S->isImplicit();
64+
if (const Decl *D = this->dyn_cast<Decl*>())
65+
return D->isImplicit();
66+
llvm_unreachable("unsupported AST node");
67+
}
68+
5969
void ASTNode::walk(ASTWalker &Walker) {
6070
if (Expr *E = this->dyn_cast<Expr*>())
6171
E->walk(Walker);

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ struct RangeResolver::Implementation {
649649
}
650650

651651
void analyze(ASTNode Node) {
652+
if (!shouldAnalyze(Node))
653+
return;
652654
Decl *D = Node.is<Decl*>() ? Node.get<Decl*>() : nullptr;
653655
analyzeDecl(D);
654656
auto &DCInfo = getCurrentDC();
@@ -675,16 +677,12 @@ struct RangeResolver::Implementation {
675677
break;
676678
}
677679

678-
// If the node's parent is not contained in the range under question but the
679-
// node itself is, we keep track of the node as top-level contained node.
680-
if (!getCurrentDC().ContainedInRange &&
681-
isContainedInSelection(CharSourceRange(SM, Node.getStartLoc(),
682-
Node.getEndLoc()))) {
683-
if (std::find_if(ContainedASTNodes.begin(), ContainedASTNodes.end(),
684-
[&](ASTNode N) { return SM.rangeContains(N.getSourceRange(),
685-
Node.getSourceRange()); }) == ContainedASTNodes.end()) {
686-
ContainedASTNodes.push_back(Node);
687-
}
680+
// If no parent is considered as a contained node; this node should be
681+
// a top-level contained node.
682+
if (std::none_of(ContainedASTNodes.begin(), ContainedASTNodes.end(),
683+
[&](ASTNode N) { return SM.rangeContains(N.getSourceRange(),
684+
Node.getSourceRange()); })) {
685+
ContainedASTNodes.push_back(Node);
688686
}
689687

690688
if (DCInfo.isMultiStatement()) {
@@ -713,6 +711,18 @@ struct RangeResolver::Implementation {
713711
return true;
714712
}
715713

714+
bool shouldAnalyze(ASTNode Node) {
715+
// Avoid analyzing implicit nodes.
716+
if (Node.isImplicit())
717+
return false;
718+
// Avoid analyzing nodes that are not enclosed.
719+
if (SM.isBeforeInBuffer(End, Node.getEndLoc()))
720+
return false;
721+
if (SM.isBeforeInBuffer(Node.getStartLoc(), Start))
722+
return false;
723+
return true;
724+
}
725+
716726
ResolvedRangeInfo getResult() {
717727
if (Result.hasValue())
718728
return Result.getValue();

test/IDE/range_info_basics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ func test_no_pattern_binding(_ parameters: [String: Any]) -> String {
164164
// RUN: %target-swift-ide-test -range -pos=109:6 -end-pos=111:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INVALID
165165
// RUN: %target-swift-ide-test -range -pos=114:1 -end-pos=115:15 -source-filename %s | %FileCheck %s -check-prefix=CHECK27
166166
// RUN: %target-swift-ide-test -range -pos=118:1 -end-pos=119:15 -source-filename %s | %FileCheck %s -check-prefix=CHECK27
167-
// RUN: %target-swift-ide-test -range -pos=126:11 -end-pos=126:12 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT
167+
// RUN: %target-swift-ide-test -range -pos=126:11 -end-pos=126:12 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT-LVALUE
168168
// RUN: %target-swift-ide-test -range -pos=126:11 -end-pos=126:20 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT
169-
// RUN: %target-swift-ide-test -range -pos=127:7 -end-pos=127:8 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT
169+
// RUN: %target-swift-ide-test -range -pos=127:7 -end-pos=127:8 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT-LVALUE
170170
// RUN: %target-swift-ide-test -range -pos=127:3 -end-pos=127:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT-LVALUE
171171
// RUN: %target-swift-ide-test -range -pos=128:13 -end-pos=128:15 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT-INOUT
172172
// RUN: %target-swift-ide-test -range -pos=118:1 -end-pos=120:22 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT

test/IDE/range_info_implicit.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class CC {
2+
public init() {
3+
}
4+
}
5+
6+
// RUN: %target-swift-ide-test -range -pos=3:1 -end-pos=3:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INVALID
7+
// CHECK-INVALID: <Kind>Invalid</Kind>

0 commit comments

Comments
 (0)