Skip to content

[RangeInfo] When reporting referenced decls in a given range, exclude those whose decl context is out of the current file. #6404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,21 @@ struct RangeResolver::Implementation {
}

void analyze(ASTNode Node) {
Decl *D = Node.is<Decl*>() ? Node.get<Decl*>() : nullptr;

// Collect declared decls in the range.
if (Node.is<Decl*>()) {
if (auto *VD = dyn_cast<ValueDecl>(Node.get<Decl*>())) {
if (auto *VD = dyn_cast_or_null<ValueDecl>(D)) {
if (isContainedInSelection(CharSourceRange(SM, VD->getStartLoc(),
VD->getEndLoc())))
pushBackDeclUniquely(DeclaredDecls, VD);
}
}

auto &DCInfo = getCurrentDC();
switch (getRangeMatchKind(Node.getSourceRange())) {
case RangeMatchKind::NoneMatch:
// PatternBindingDecl is not visited; we need to explicitly analyze here.
if (auto *VA = dyn_cast_or_null<VarDecl>(D))
analyze(VA->getParentPatternBinding());
return;
case RangeMatchKind::RangeMatch:
Result = getSingleNodeKind(Node);
Expand Down Expand Up @@ -398,6 +403,11 @@ struct RangeResolver::Implementation {
void analyzeDeclRef(ValueDecl *VD, CharSourceRange Range) {
if (!isContainedInSelection(Range))
return;

// If the VD is declared outside of current file, exclude such decl.
if (VD->getDeclContext()->getParentSourceFile() != &File)
return;

// Collect referenced decls in the range.
pushBackDeclUniquely(ReferencedDecls, VD);
}
Expand Down
26 changes: 19 additions & 7 deletions test/IDE/range_info_basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ func foo1() -> Int { return 0 }
class C { func foo() {} }
struct S { func foo() {} }

func foo2() {
let a = 3
var b = a.bigEndian
let c = a.byteSwapped
b = b.bigEndian.bigEndian.byteSwapped
print(b + c)
}

// RUN: %target-swift-ide-test -range -pos=8:1 -end-pos 8:32 -source-filename %s | %FileCheck %s -check-prefix=CHECK1
// RUN: %target-swift-ide-test -range -pos=9:1 -end-pos 9:26 -source-filename %s | %FileCheck %s -check-prefix=CHECK2
// RUN: %target-swift-ide-test -range -pos=10:1 -end-pos 10:27 -source-filename %s | %FileCheck %s -check-prefix=CHECK3
// RUN: %target-swift-ide-test -range -pos=3:1 -end-pos=4:26 -source-filename %s | %FileCheck %s -check-prefix=CHECK4
// RUN: %target-swift-ide-test -range -pos=3:1 -end-pos=5:13 -source-filename %s | %FileCheck %s -check-prefix=CHECK5
// RUN: %target-swift-ide-test -range -pos=4:1 -end-pos=5:13 -source-filename %s | %FileCheck %s -check-prefix=CHECK6
// RUN: %target-swift-ide-test -range -pos=14:1 -end-pos=17:15 -source-filename %s | %FileCheck %s -check-prefix=CHECK7

// CHECK1: <Kind>SingleDecl</Kind>
// CHECK1-NEXT: <Content>func foo1() -> Int { return 0 }</Content>
Expand All @@ -35,25 +43,29 @@ struct S { func foo() {} }
// CHECK4: <Kind>MultiStatement</Kind>
// CHECK4-NEXT: <Content>aaa = aaa + 3
// CHECK4-NEXT: if aaa == 3 { aaa = 4 }</Content>
// CHECK4-NEXT: <Declared>foo</Declared>
// CHECK4-NEXT: <Referenced>aaa</Referenced>
// CHECK4-NEXT: <Referenced>+</Referenced>
// CHECK4-NEXT: <end>

// CHECK5: <Kind>MultiStatement</Kind>
// CHECK5-NEXT: <Content>aaa = aaa + 3
// CHECK5-NEXT: if aaa == 3 { aaa = 4 }
// CHECK5-NEXT: return aaa</Content>
// CHECK5-NEXT: <Declared>foo</Declared>
// CHECK5-NEXT: <Referenced>aaa</Referenced>
// CHECK5-NEXT: <Referenced>+</Referenced>
// CHECK5-NEXT: <Referenced>==</Referenced>
// CHECK5-NEXT: <end>

// CHECK6: <Kind>MultiStatement</Kind>
// CHECK6-NEXT: if aaa == 3 { aaa = 4 }
// CHECK6-NEXT: return aaa</Content>
// CHECK6-NEXT: <Declared>foo</Declared>
// CHECK6-NEXT: <Referenced>aaa</Referenced>
// CHECK6-NEXT: <Referenced>==</Referenced>
// CHECK6-NEXT: <end>

// CHECK7: <Kind>MultiStatement</Kind>
// CHECK7-NEXT: <Content>var b = a.bigEndian
// CHECK7-NEXT: let c = a.byteSwapped
// CHECK7-NEXT: b = b.bigEndian.bigEndian.byteSwapped
// CHECK7-NEXT: print(b + c)</Content>
// CHECK7-NEXT: <Declared>b</Declared>
// CHECK7-NEXT: <Declared>c</Declared>
// CHECK7-NEXT: <Referenced>a</Referenced>
// CHECK7-NEXT: <Referenced>b</Referenced>
// CHECK7-NEXT: <end>