Skip to content

[index] Fix infinite loop while looking at superclasses #23674

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 30, 2019
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
17 changes: 15 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3647,6 +3647,19 @@ class ClassDecl final : public NominalTypeDecl {
Bits.ClassDecl.Circularity = static_cast<unsigned>(circularity);
}

/// Walk this class and all of the superclasses of this class, transitively,
/// invoking the callback function for each class.
///
/// \param fn The callback function that will be invoked for each superclass.
/// It can return \c Continue to continue the traversal. Returning
/// \c SkipChildren halts the search and returns \c false, while returning
/// \c Stop halts the search and returns \c true.
///
/// \returns \c true if \c fn returned \c Stop for any class, \c false
/// otherwise.
bool walkSuperclasses(
llvm::function_ref<TypeWalker::Action(ClassDecl *)> fn) const;

//// Whether this class requires all of its stored properties to
//// have initializers in the class definition.
bool requiresStoredPropertyInits() const {
Expand Down Expand Up @@ -3975,8 +3988,8 @@ class ProtocolDecl final : public NominalTypeDecl {
/// a protocol having nested types (ObjC protocols).
llvm::TinyPtrVector<AssociatedTypeDecl *> getAssociatedTypeMembers() const;

/// Walk all of the protocols inherited by this protocol, transitively,
/// invoking the callback function for each protocol.
/// Walk this protocol and all of the protocols inherited by this protocol,
/// transitively, invoking the callback function for each protocol.
///
/// \param fn The callback function that will be invoked for each inherited
/// protocol. It can return \c Continue to continue the traversal,
Expand Down
21 changes: 21 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3794,6 +3794,27 @@ ClassDecl::findImplementingMethod(const AbstractFunctionDecl *Method) const {
return nullptr;
}

bool ClassDecl::walkSuperclasses(
llvm::function_ref<TypeWalker::Action(ClassDecl *)> fn) const {

SmallPtrSet<ClassDecl *, 8> seen;
auto *cls = const_cast<ClassDecl *>(this);

while (cls && seen.insert(cls).second) {
switch (fn(cls)) {
case TypeWalker::Action::Stop:
return true;
case TypeWalker::Action::SkipChildren:
return false;
case TypeWalker::Action::Continue:
cls = cls->getSuperclassDecl();
continue;
}
}

return false;
}

EnumCaseDecl *EnumCaseDecl::create(SourceLoc CaseLoc,
ArrayRef<EnumElementDecl *> Elements,
DeclContext *DC) {
Expand Down
13 changes: 7 additions & 6 deletions lib/Index/IndexSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ static NominalTypeDecl *getNominalParent(const ValueDecl *D) {
static bool isUnitTestCase(const ClassDecl *D) {
if (!D)
return false;
while (auto *SuperD = D->getSuperclassDecl()) {
if (SuperD->getNameStr() == "XCTestCase")
return true;
D = SuperD;
}
return false;

return D->walkSuperclasses([D](ClassDecl *SuperD) {
if (SuperD != D && // Do not treate XCTestCase itself as a test.
SuperD->getNameStr() == "XCTestCase")
return TypeWalker::Action::Stop; // Found test; stop and return true.
return TypeWalker::Action::Continue;
});
}

static bool isUnitTest(const ValueDecl *D) {
Expand Down
66 changes: 66 additions & 0 deletions test/Index/circular.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s

class SelfCycle : SelfCycle {}
// CHECK: [[@LINE-1]]:7 | class/Swift | SelfCycle | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:19 | class/Swift | SelfCycle | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | SelfCycle | {{\W*}}

class Cycle1_A: Cycle1_B {}
// CHECK: [[@LINE-1]]:7 | class/Swift | Cycle1_A | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:17 | class/Swift | Cycle1_B | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | Cycle1_A | {{[^ ]*}}
class Cycle1_B: Cycle1_A {}
// CHECK: [[@LINE-1]]:7 | class/Swift | Cycle1_B | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:17 | class/Swift | Cycle1_A | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | Cycle1_B | {{[^ ]*}}

class Cycle2_A: Cycle2_C {}
// CHECK: [[@LINE-1]]:7 | class/Swift | Cycle2_A | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:17 | class/Swift | Cycle2_C | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | Cycle2_A | {{[^ ]*}}
class Cycle2_B: Cycle2_A {}
// CHECK: [[@LINE-1]]:7 | class/Swift | Cycle2_B | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:17 | class/Swift | Cycle2_A | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | Cycle2_B | {{[^ ]*}}
class Cycle2_C: Cycle2_B {}
// CHECK: [[@LINE-1]]:7 | class/Swift | Cycle2_C | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:17 | class/Swift | Cycle2_B | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | Cycle2_C | {{[^ ]*}}

class TestCase1: XCTestCase {}
// CHECK: [[@LINE-1]]:7 | class(test)/Swift | TestCase1 | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:18 | class/Swift | XCTestCase | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class(test)/Swift | TestCase1 | {{[^ ]*}}
class XCTestCase: TestCase1 {}
// CHECK: [[@LINE-1]]:7 | class/Swift | XCTestCase | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:19 | class(test)/Swift | TestCase1 | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class/Swift | XCTestCase | {{[^ ]*}}
class TestCase2: TestCase1 {}
// CHECK: [[@LINE-1]]:7 | class(test)/Swift | TestCase2 | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:18 | class(test)/Swift | TestCase1 | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | class(test)/Swift | TestCase2 | {{[^ ]*}}

protocol SelfCycleP: SelfCycleP {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | SelfCycleP | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:22 | protocol/Swift | SelfCycleP | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | SelfCycleP | {{[^ ]*}}
protocol Cycle1P_A: Cycle1P_B {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | Cycle1P_A | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:21 | protocol/Swift | Cycle1P_B | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | Cycle1P_A | {{[^ ]*}}
protocol Cycle1P_B: Cycle1P_A {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | Cycle1P_B | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:21 | protocol/Swift | Cycle1P_A | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | Cycle1P_B | {{[^ ]*}}
protocol Cycle2P_A: Cycle2P_C {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | Cycle2P_A | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:21 | protocol/Swift | Cycle2P_C | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | Cycle2P_A | {{[^ ]*}}
protocol Cycle2P_B: Cycle2P_A {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | Cycle2P_B | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:21 | protocol/Swift | Cycle2P_A | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | Cycle2P_B | {{[^ ]*}}
protocol Cycle2P_C: Cycle2P_B {}
// CHECK: [[@LINE-1]]:10 | protocol/Swift | Cycle2P_C | {{[^ ]*}} | Def | rel: 0
// CHECK: [[@LINE-2]]:21 | protocol/Swift | Cycle2P_B | {{[^ ]*}} | Ref,RelBase | rel: 1
// CHECK: RelBase | protocol/Swift | Cycle2P_C | {{[^ ]*}}