Skip to content

RangeInfo: disallow expressions with non-void type at the start or the end of multi-statement selections. #8092

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
Mar 14, 2017
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
4 changes: 2 additions & 2 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ struct ResolvedRangeInfo {
DeclaredDecls(DeclaredDecls),
ReferencedDecls(ReferencedDecls),
RangeContext(RangeContext) {}
ResolvedRangeInfo() :
ResolvedRangeInfo(RangeKind::Invalid, Type(), StringRef(), nullptr,
ResolvedRangeInfo(StringRef Content) :
ResolvedRangeInfo(RangeKind::Invalid, Type(), Content, nullptr,
/*Single entry*/true, /*unhandled error*/false,
OrphanKind::None, {}, {}, {}) {}
void print(llvm::raw_ostream &OS);
Expand Down
41 changes: 38 additions & 3 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ struct RangeResolver::Implementation {
std::vector<ASTNode> EndMatches;
ContextInfo(ASTNode Parent, bool ContainedInRange) : Parent(Parent),
ContainedInRange(ContainedInRange) {}
private:
bool hasStmtlikeNode(ArrayRef<ASTNode> Nodes) {
for (auto N : Nodes) {
if (N.is<Stmt*>())
return true;
// Expression with void type is statement-like.
else if (N.is<Expr*>()) {
auto *E = N.get<Expr*>();
if (auto T = E->getType()) {
if (T->isVoid())
return true;
}
} else {
// Decls are statement like.
return true;
}
}
return false;
}
public:
bool hasStmtMatch(RangeMatchKind Kind) {
switch(Kind) {
case RangeMatchKind::NoneMatch:
case RangeMatchKind::RangeMatch:
llvm_unreachable("cannot answer these.");
case RangeMatchKind::StartMatch:
return hasStmtlikeNode(StartMatches);
case RangeMatchKind::EndMatch:
return hasStmtlikeNode(EndMatches);
}
}
};

std::vector<Token> AllTokens;
Expand Down Expand Up @@ -654,7 +685,11 @@ struct RangeResolver::Implementation {
}
}

if (!DCInfo.StartMatches.empty() && !DCInfo.EndMatches.empty()) {
// Check if the start and end matches have statement-like entities; this
// can avoid picking expressions like "a == b" in a list of selected
// multi-statement at the start (or the end).
if (DCInfo.hasStmtMatch(RangeMatchKind::StartMatch) &&
DCInfo.hasStmtMatch(RangeMatchKind::EndMatch)) {
postAnalysis(DCInfo.EndMatches.back());
Result = {RangeKind::MultiStatement,
/* Last node has the type */
Expand Down Expand Up @@ -682,7 +717,7 @@ struct RangeResolver::Implementation {
ResolvedRangeInfo getResult() {
if (Result.hasValue())
return Result.getValue();
return ResolvedRangeInfo();
return ResolvedRangeInfo(Content);
}

void analyzeDeclRef(ValueDecl *VD, CharSourceRange Range, Type Ty,
Expand Down Expand Up @@ -784,7 +819,7 @@ visitDeclReference(ValueDecl *D, CharSourceRange Range, TypeDecl *CtorTyRef,

ResolvedRangeInfo RangeResolver::resolve() {
if (!Impl)
return ResolvedRangeInfo();
return ResolvedRangeInfo(StringRef());
Impl->enter(ASTNode());
walk(Impl->File);
return Impl->getResult();
Expand Down
10 changes: 10 additions & 0 deletions test/IDE/range_info_basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func foo8(a : [Int]) {
} while i < 100
}

func foo9(a: Int, b: Int) -> Int {
if a == b {
return 0
}
return 0
}

// 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
Expand All @@ -131,6 +138,9 @@ func foo8(a : [Int]) {
// RUN: %target-swift-ide-test -range -pos=102:1 -end-pos=104:6 -source-filename %s | %FileCheck %s -check-prefix=CHECK24
// RUN: %target-swift-ide-test -range -pos=87:1 -end-pos=92:6 -source-filename %s | %FileCheck %s -check-prefix=CHECK25
// RUN: %target-swift-ide-test -range -pos=97:1 -end-pos=104:6 -source-filename %s | %FileCheck %s -check-prefix=CHECK26
// RUN: %target-swift-ide-test -range -pos=109:6 -end-pos=111:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INVALID

// CHECK-INVALID: <Kind>Invalid</Kind>

// CHECK1: <Kind>SingleDecl</Kind>
// CHECK1-NEXT: <Content>func foo1() -> Int { return 0 }</Content>
Expand Down