Skip to content

RangeInfo: stuff a bit about whether the selected range exits. #9389

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 6 commits into from
May 8, 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
15 changes: 11 additions & 4 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_IDE_UTILS_H
#define SWIFT_IDE_UTILS_H

#include "llvm/ADT/PointerIntPair.h"
#include "swift/Basic/LLVM.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/Module.h"
Expand Down Expand Up @@ -243,9 +244,12 @@ enum class OrphanKind : int8_t {
Break,
Continue,
};

typedef llvm::PointerIntPair<TypeBase*, 1, bool> ReturnTyAndWhetherExit;

struct ResolvedRangeInfo {
RangeKind Kind;
Type Ty;
ReturnTyAndWhetherExit ExitInfo;
StringRef Content;
bool HasSingleEntry;
bool ThrowingUnhandledError;
Expand All @@ -256,23 +260,26 @@ struct ResolvedRangeInfo {
ArrayRef<DeclaredDecl> DeclaredDecls;
ArrayRef<ReferencedDecl> ReferencedDecls;
DeclContext* RangeContext;
ResolvedRangeInfo(RangeKind Kind, Type Ty, StringRef Content,
ResolvedRangeInfo(RangeKind Kind, ReturnTyAndWhetherExit ExitInfo, StringRef Content,
DeclContext* RangeContext,
bool HasSingleEntry, bool ThrowingUnhandledError,
OrphanKind Orphan, ArrayRef<ASTNode> ContainedNodes,
ArrayRef<DeclaredDecl> DeclaredDecls,
ArrayRef<ReferencedDecl> ReferencedDecls): Kind(Kind),
Ty(Ty), Content(Content), HasSingleEntry(HasSingleEntry),
ExitInfo(ExitInfo), Content(Content),
HasSingleEntry(HasSingleEntry),
ThrowingUnhandledError(ThrowingUnhandledError),
Orphan(Orphan), ContainedNodes(ContainedNodes),
DeclaredDecls(DeclaredDecls),
ReferencedDecls(ReferencedDecls),
RangeContext(RangeContext) {}
ResolvedRangeInfo(StringRef Content) :
ResolvedRangeInfo(RangeKind::Invalid, Type(), Content, nullptr,
ResolvedRangeInfo(RangeKind::Invalid, {nullptr, false}, Content, nullptr,
/*Single entry*/true, /*unhandled error*/false,
OrphanKind::None, {}, {}, {}) {}
void print(llvm::raw_ostream &OS);
bool exit() const { return ExitInfo.getInt(); }
Type getType() const { return ExitInfo.getPointer(); }
};

class RangeResolver : public SourceEntityWalker {
Expand Down
32 changes: 21 additions & 11 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,15 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
OS << "</Kind>\n";

OS << "<Content>" << Content << "</Content>\n";
if (Ty) {

if (auto Ty = ExitInfo.getPointer()) {
OS << "<Type>";
Ty->print(OS);
OS << "</Type>\n";
OS << "</Type>";
if (ExitInfo.getInt()) {
OS << "<Exit>true</Exit>";
}
OS << "\n";
}

if (RangeContext) {
Expand Down Expand Up @@ -382,25 +387,29 @@ struct RangeResolver::Implementation {
std::vector<ASTNode> ContainedASTNodes;

/// Collect the type that an ASTNode should be evaluated to.
Type resolveNodeType(ASTNode N, RangeKind Kind) {
auto VoidTy = Ctx.getVoidDecl()->getDeclaredInterfaceType();
ReturnTyAndWhetherExit resolveNodeType(ASTNode N, RangeKind Kind) {
auto *VoidTy = Ctx.getVoidDecl()->getDeclaredInterfaceType().getPointer();
if (N.isNull())
return VoidTy;
return {VoidTy, false};
switch(Kind) {
case RangeKind::Invalid:
case RangeKind::SingleDecl:
llvm_unreachable("cannot get type.");

// For a single expression, its type is apparent.
case RangeKind::SingleExpression:
return N.get<Expr*>()->getType();
return {N.get<Expr*>()->getType().getPointer(), false};

// For statements, we either resolve to the returning type or Void.
case RangeKind::SingleStatement:
case RangeKind::MultiStatement: {
if (N.is<Stmt*>()) {
if (auto RS = dyn_cast<ReturnStmt>(N.get<Stmt*>())) {
return resolveNodeType(RS->getResult(), RangeKind::SingleExpression);
return {
resolveNodeType(RS->hasResult() ? RS->getResult() : nullptr,
RangeKind::SingleExpression).getPointer(),
true
};
}

// Unbox the brace statement to find its type.
Expand All @@ -419,15 +428,16 @@ struct RangeResolver::Implementation {
RangeKind::SingleStatement);

// If two branches agree on the return type, return that type.
if (ThenTy->isEqual(ElseTy))
if (ThenTy.getPointer()->isEqual(ElseTy.getPointer()) &&
ThenTy.getInt() == ElseTy.getInt())
return ThenTy;

// Otherwise, return the error type.
return Ctx.TheErrorType;
return {Ctx.TheErrorType.getPointer(), false};
}
}
// For other statements, the type should be void.
return VoidTy;
return {VoidTy, false};
}
}
}
Expand Down Expand Up @@ -458,7 +468,7 @@ struct RangeResolver::Implementation {
llvm::makeArrayRef(ReferencedDecls));
else {
assert(Node.is<Decl*>());
return ResolvedRangeInfo(RangeKind::SingleDecl, Type(), Content,
return ResolvedRangeInfo(RangeKind::SingleDecl, {nullptr, false}, Content,
getImmediateContext(), SingleEntry,
UnhandledError, Kind,
llvm::makeArrayRef(ContainedASTNodes),
Expand Down
23 changes: 23 additions & 0 deletions test/IDE/range_info_branches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,31 @@ func foo(_ a: Bool) -> Int{
}
}

func foo1(_ a: Bool) {
if a {}
if a {}
else {}
if a {
return
} else {
return
}
}

// RUN: %target-swift-ide-test -range -pos=2:1 -end-pos 5:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-ERR
// RUN: %target-swift-ide-test -range -pos=6:1 -end-pos 10:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT
// RUN: %target-swift-ide-test -range -pos=14:1 -end-pos 14:10 -source-filename %s | %FileCheck %s -check-prefix=CHECK-VOID-NO-RETURN
// RUN: %target-swift-ide-test -range -pos=15:1 -end-pos 16:10 -source-filename %s | %FileCheck %s -check-prefix=CHECK-VOID-NO-RETURN
// RUN: %target-swift-ide-test -range -pos=17:1 -end-pos 21:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-VOID-RETURN

// CHECK-ERR: <Type><<error type>></Type>
// CHECK-ERR-NOT: <Exit>true</Exit>

// CHECK-INT: <Type>Int</Type>
// CHECK-INT: <Exit>true</Exit>

// CHECK-VOID-NO-RETURN: <Type>Void</Type>
// CHECK-VOID-NO-RETURN-NOT: <Exit>true</Exit>

// CHECK-VOID-RETURN: <Type>Void</Type>
// CHECK-VOID-RETURN: <Exit>true</Exit>
2 changes: 1 addition & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ static void resolveRange(SwiftLangSupport &Lang,
case RangeKind::SingleExpression: {
SmallString<64> SS;
llvm::raw_svector_ostream OS(SS);
Info.Ty.print(OS);
Info.ExitInfo.getPointer()->print(OS);
Result.ExprType = OS.str();
Receiver(Result);
return;
Expand Down