Skip to content

RangeInfo: Add a new range info kind that describes part of a parent expression. rdar://32039874 #9707

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
May 17, 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
12 changes: 9 additions & 3 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ enum class RangeKind : int8_t{
SingleDecl,

MultiStatement,
PartOfExpression,
};

struct DeclaredDecl {
Expand Down Expand Up @@ -260,9 +261,12 @@ struct ResolvedRangeInfo {
ArrayRef<DeclaredDecl> DeclaredDecls;
ArrayRef<ReferencedDecl> ReferencedDecls;
DeclContext* RangeContext;
Expr* CommonExprParent;

ResolvedRangeInfo(RangeKind Kind, ReturnTyAndWhetherExit ExitInfo,
CharSourceRange Content, DeclContext* RangeContext,
bool HasSingleEntry, bool ThrowingUnhandledError,
Expr *CommonExprParent, bool HasSingleEntry,
bool ThrowingUnhandledError,
OrphanKind Orphan, ArrayRef<ASTNode> ContainedNodes,
ArrayRef<DeclaredDecl> DeclaredDecls,
ArrayRef<ReferencedDecl> ReferencedDecls): Kind(Kind),
Expand All @@ -272,9 +276,11 @@ struct ResolvedRangeInfo {
Orphan(Orphan), ContainedNodes(ContainedNodes),
DeclaredDecls(DeclaredDecls),
ReferencedDecls(ReferencedDecls),
RangeContext(RangeContext) {}
RangeContext(RangeContext),
CommonExprParent(CommonExprParent) {}
ResolvedRangeInfo(CharSourceRange Content) :
ResolvedRangeInfo(RangeKind::Invalid, {nullptr, false}, Content, nullptr,
ResolvedRangeInfo(RangeKind::Invalid, {nullptr, false}, Content,
nullptr, /*Commom Expr Parent*/nullptr,
/*Single entry*/true, /*unhandled error*/false,
OrphanKind::None, {}, {}, {}) {}
void print(llvm::raw_ostream &OS);
Expand Down
54 changes: 48 additions & 6 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
case RangeKind::SingleExpression: OS << "SingleExpression"; break;
case RangeKind::SingleDecl: OS << "SingleDecl"; break;
case RangeKind::MultiStatement: OS << "MultiStatement"; break;
case RangeKind::PartOfExpression: OS << "PartOfExpression"; break;
case RangeKind::SingleStatement: OS << "SingleStatement"; break;
case RangeKind::Invalid: OS << "Invalid"; break;
}
Expand All @@ -244,6 +245,12 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
OS << "</Context>\n";
}

if (CommonExprParent) {
OS << "<Parent>";
OS << Expr::getKindName(CommonExprParent->getKind());
OS << "</Parent>\n";
}

if (!HasSingleEntry) {
OS << "<Entry>Multi</Entry>\n";
}
Expand Down Expand Up @@ -394,6 +401,7 @@ struct RangeResolver::Implementation {
switch(Kind) {
case RangeKind::Invalid:
case RangeKind::SingleDecl:
case RangeKind::PartOfExpression:
llvm_unreachable("cannot get type.");

// For a single expression, its type is apparent.
Expand Down Expand Up @@ -453,23 +461,29 @@ struct RangeResolver::Implementation {
return ResolvedRangeInfo(RangeKind::SingleExpression,
resolveNodeType(Node, RangeKind::SingleExpression),
Content,
getImmediateContext(), SingleEntry,
getImmediateContext(),
/*Common Parent Expr*/nullptr,
SingleEntry,
UnhandledError, Kind,
llvm::makeArrayRef(ContainedASTNodes),
llvm::makeArrayRef(DeclaredDecls),
llvm::makeArrayRef(ReferencedDecls));
else if (Node.is<Stmt*>())
return ResolvedRangeInfo(RangeKind::SingleStatement,
resolveNodeType(Node, RangeKind::SingleStatement),
Content, getImmediateContext(), SingleEntry,
Content, getImmediateContext(),
/*Common Parent Expr*/nullptr,
SingleEntry,
UnhandledError, Kind,
llvm::makeArrayRef(ContainedASTNodes),
llvm::makeArrayRef(DeclaredDecls),
llvm::makeArrayRef(ReferencedDecls));
else {
assert(Node.is<Decl*>());
return ResolvedRangeInfo(RangeKind::SingleDecl, {nullptr, false}, Content,
getImmediateContext(), SingleEntry,
getImmediateContext(),
/*Common Parent Expr*/nullptr,
SingleEntry,
UnhandledError, Kind,
llvm::makeArrayRef(ContainedASTNodes),
llvm::makeArrayRef(DeclaredDecls),
Expand Down Expand Up @@ -523,6 +537,22 @@ struct RangeResolver::Implementation {
}

void leave(ASTNode Node) {
if (!hasResult() && !Node.isImplicit() && nodeContainSelection(Node)) {
if (auto Parent = Node.is<Expr*>() ? Node.get<Expr*>() : nullptr) {
Result = {
RangeKind::PartOfExpression, {nullptr, false}, Content,
getImmediateContext(),
Parent,
hasSingleEntryPoint(ContainedASTNodes),
hasUnhandledError(ContainedASTNodes),
getOrphanKind(ContainedASTNodes),
llvm::makeArrayRef(ContainedASTNodes),
llvm::makeArrayRef(DeclaredDecls),
llvm::makeArrayRef(ReferencedDecls)
};
}
}

assert(ContextStack.back().Parent.getOpaqueValue() == Node.getOpaqueValue());
ContextStack.pop_back();
}
Expand Down Expand Up @@ -690,12 +720,13 @@ struct RangeResolver::Implementation {
analyzeDecl(D);
auto &DCInfo = getCurrentDC();
switch (getRangeMatchKind(Node.getSourceRange())) {
case RangeMatchKind::NoneMatch:
case RangeMatchKind::NoneMatch: {
// PatternBindingDecl is not visited; we need to explicitly analyze here.
if (auto *VA = dyn_cast_or_null<VarDecl>(D))
if (auto PBD = VA->getParentPatternBinding())
analyze(PBD);
break;
}
case RangeMatchKind::RangeMatch: {
postAnalysis(Node);

Expand Down Expand Up @@ -726,13 +757,13 @@ struct RangeResolver::Implementation {
/* Last node has the type */
resolveNodeType(DCInfo.EndMatches.back(),
RangeKind::MultiStatement), Content,
getImmediateContext(), hasSingleEntryPoint(ContainedASTNodes),
getImmediateContext(), nullptr,
hasSingleEntryPoint(ContainedASTNodes),
hasUnhandledError(ContainedASTNodes),
getOrphanKind(ContainedASTNodes),
llvm::makeArrayRef(ContainedASTNodes),
llvm::makeArrayRef(DeclaredDecls),
llvm::makeArrayRef(ReferencedDecls)};
return;
}
}

Expand All @@ -746,6 +777,17 @@ struct RangeResolver::Implementation {
return true;
}

bool nodeContainSelection(ASTNode Node) {
// If the selection starts before the node, return false.
if (SM.isBeforeInBuffer(Start, Node.getStartLoc()))
return false;
// If the node ends before the selection, return false.
if (SM.isBeforeInBuffer(Lexer::getLocForEndOfToken(SM, Node.getEndLoc()), End))
return false;
// Contained.
return true;
}

bool shouldAnalyze(ASTNode Node) {
// Avoid analyzing implicit nodes.
if (Node.isImplicit())
Expand Down
36 changes: 36 additions & 0 deletions test/IDE/range_info_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,43 @@ public class CC {
return 0
}
}
func getSelf() -> CC {
return self
}
func getSelf(_ i : Int) -> CC {
return self
}
}

func foo1(_ c : CC) -> CC{
_ = c.getSelf().getSelf().getSelf().getSelf()
_ = c.getSelf(1).getSelf(1).getSelf(1).getSelf(1)
return c.getSelf()
}

// RUN: %target-swift-ide-test -range -pos=7:8 -end-pos=7:19 -source-filename %s | %FileCheck %s -check-prefix=CHECK-BOOL
// CHECK-BOOL: <Type>Bool</Type>

// RUN: %target-swift-ide-test -range -pos=22:39 -end-pos=22:48 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR
// RUN: %target-swift-ide-test -range -pos=22:29 -end-pos=22:38 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR
// RUN: %target-swift-ide-test -range -pos=22:19 -end-pos=22:28 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR
// RUN: %target-swift-ide-test -range -pos=22:9 -end-pos=22:18 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR

// RUN: %target-swift-ide-test -range -pos=23:42 -end-pos=23:52 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR1
// RUN: %target-swift-ide-test -range -pos=23:31 -end-pos=23:41 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR1
// RUN: %target-swift-ide-test -range -pos=23:20 -end-pos=23:30 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR1
// RUN: %target-swift-ide-test -range -pos=23:9 -end-pos=23:19 -source-filename %s | %FileCheck %s -check-prefix=CHECK-PART-EXPR1

// CHECK-PART-EXPR: <Kind>PartOfExpression</Kind>
// CHECK-PART-EXPR-NEXT: <Content>getSelf()</Content>
// CHECK-PART-EXPR-NEXT: <Context>swift_ide_test.(file).foo1(_:)</Context>
// CHECK-PART-EXPR-NEXT: <Parent>Call</Parent>
// CHECK-PART-EXPR-NEXT: <ASTNodes>2</ASTNodes>
// CHECK-PART-EXPR-NEXT: <end>

// CHECK-PART-EXPR1: <Kind>PartOfExpression</Kind>
// CHECK-PART-EXPR2: <Content>getSelf(1)</Content>
// CHECK-PART-EXPR2: <Context>swift_ide_test.(file).foo1(_:)</Context>
// CHECK-PART-EXPR2: <Parent>Call</Parent>
// CHECK-PART-EXPR2: <ASTNodes>2</ASTNodes>
// CHECK-PART-EXPR2: <end>
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ getUIDForRangeKind(swift::ide::RangeKind Kind) {
case swift::ide::RangeKind::SingleStatement: return KindRangeSingleStatement;
case swift::ide::RangeKind::SingleDecl: return KindRangeSingleDeclaration;
case swift::ide::RangeKind::MultiStatement: return KindRangeMultiStatement;
case swift::ide::RangeKind::PartOfExpression: return KindRangeInvalid;
case swift::ide::RangeKind::Invalid: return KindRangeInvalid;
}

Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@ static void resolveRange(SwiftLangSupport &Lang,
Receiver(Result);
return;
}
case RangeKind::PartOfExpression:
case RangeKind::Invalid:
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
Expand Down