From cd838963a098d72806cfb9adad3c92d570b6b0f9 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Fri, 29 May 2020 23:13:00 +0200 Subject: [PATCH 1/2] Index objcdynamic --- include/swift/AST/ASTWalker.h | 1 + lib/IDE/SourceEntityWalker.cpp | 6 ++++++ test/Index/index_objc_dynamic_refs.swift | 26 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 test/Index/index_objc_dynamic_refs.swift diff --git a/include/swift/AST/ASTWalker.h b/include/swift/AST/ASTWalker.h index d29b3c3f60c88..af4308ecd8db4 100644 --- a/include/swift/AST/ASTWalker.h +++ b/include/swift/AST/ASTWalker.h @@ -37,6 +37,7 @@ enum class SemaReferenceKind : uint8_t { TypeRef, EnumElementRef, SubscriptRef, + DynamicMemberRef, }; struct ReferenceMetaData { diff --git a/lib/IDE/SourceEntityWalker.cpp b/lib/IDE/SourceEntityWalker.cpp index b57a33811927c..7b1d389c068ed 100644 --- a/lib/IDE/SourceEntityWalker.cpp +++ b/lib/IDE/SourceEntityWalker.cpp @@ -511,6 +511,12 @@ std::pair SemaAnnotator::walkToExprPre(Expr *E) { return doSkipChildren(); } + } else if (auto DMRE = dyn_cast(E)) { + if (!passReference(DMRE->getMember().getDecl(), DMRE->getType(), + DMRE->getNameLoc(), + ReferenceMetaData(SemaReferenceKind::DynamicMemberRef, + OpAccess))) + return stopTraversal; } return { true, E }; diff --git a/test/Index/index_objc_dynamic_refs.swift b/test/Index/index_objc_dynamic_refs.swift new file mode 100644 index 0000000000000..5700787017178 --- /dev/null +++ b/test/Index/index_objc_dynamic_refs.swift @@ -0,0 +1,26 @@ +// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s +// REQUIRES: objc_interop + +import Foundation + +@objc protocol AProtocol { + @objc optional func dynamicMethod() + // CHECK: [[@LINE-1]]:25 | instance-method/Swift | dynamicMethod() | [[DynamicMethod_USR:.*]] | Def + @objc optional var property: String { get } + // CHECK: [[@LINE-1]]:24 | instance-property/Swift | property | [[DynamicProperty_USR:.*]] | Def +} + +class AClass { + + weak var objcDelegate: AProtocol? + // CHECK: [[@LINE-1]]:14 | instance-property/Swift | objcDelegate | [[Delegate_USR:.*]] | Def + + func doSomething() { + objcDelegate?.dynamicMethod?() + // CHECK: [[@LINE-1]]:23 | instance-method/Swift | dynamicMethod() | [[DynamicMethod_USR]] | Ref + // CHECK: [[@LINE-2]]:9 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + _ = objcDelegate?.property + // CHECK: [[@LINE-1]]:27 | instance-property/Swift | property | [[DynamicProperty_USR]] | Ref + // CHECK: [[@LINE-2]]:13 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + } +} From 286b6cbb8f4c413941a5acb81c26d79d9161dc51 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Sat, 30 May 2020 14:34:42 +0200 Subject: [PATCH 2/2] Visit base first --- lib/IDE/SourceEntityWalker.cpp | 5 +++++ test/Index/index_objc_dynamic_refs.swift | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/IDE/SourceEntityWalker.cpp b/lib/IDE/SourceEntityWalker.cpp index 7b1d389c068ed..f2dc3e165a072 100644 --- a/lib/IDE/SourceEntityWalker.cpp +++ b/lib/IDE/SourceEntityWalker.cpp @@ -512,11 +512,16 @@ std::pair SemaAnnotator::walkToExprPre(Expr *E) { return doSkipChildren(); } } else if (auto DMRE = dyn_cast(E)) { + // Visit in source order. + if (!DMRE->getBase()->walk(*this)) + return stopTraversal; if (!passReference(DMRE->getMember().getDecl(), DMRE->getType(), DMRE->getNameLoc(), ReferenceMetaData(SemaReferenceKind::DynamicMemberRef, OpAccess))) return stopTraversal; + // We already visited the children. + return doSkipChildren(); } return { true, E }; diff --git a/test/Index/index_objc_dynamic_refs.swift b/test/Index/index_objc_dynamic_refs.swift index 5700787017178..c42805b38b5fc 100644 --- a/test/Index/index_objc_dynamic_refs.swift +++ b/test/Index/index_objc_dynamic_refs.swift @@ -17,10 +17,10 @@ class AClass { func doSomething() { objcDelegate?.dynamicMethod?() - // CHECK: [[@LINE-1]]:23 | instance-method/Swift | dynamicMethod() | [[DynamicMethod_USR]] | Ref - // CHECK: [[@LINE-2]]:9 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + // CHECK: [[@LINE-1]]:9 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + // CHECK: [[@LINE-2]]:23 | instance-method/Swift | dynamicMethod() | [[DynamicMethod_USR]] | Ref _ = objcDelegate?.property - // CHECK: [[@LINE-1]]:27 | instance-property/Swift | property | [[DynamicProperty_USR]] | Ref - // CHECK: [[@LINE-2]]:13 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + // CHECK: [[@LINE-1]]:13 | instance-property/Swift | objcDelegate | [[Delegate_USR]] | Ref + // CHECK: [[@LINE-2]]:27 | instance-property/Swift | property | [[DynamicProperty_USR]] | Ref } }