Skip to content

[sourcekitd] Report entities that are implicitly @objc as @objc in the index request responses for compatibility #22793

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
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
8 changes: 8 additions & 0 deletions test/SourceKit/Indexing/index_objcMembers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %sourcekitd-test -req=index %s -- -Xfrontend -serialize-diagnostics-path -Xfrontend %t.dia %s | %sed_clean > %t.response
// RUN: diff -u %s.response %t.response
// REQUIRES: objc_interop

@objcMembers class FixtureClass25: NSObject {
var someVar: String?
func someMethod() {}
}
85 changes: 85 additions & 0 deletions test/SourceKit/Indexing/index_objcMembers.swift.response
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
key.hash: <hash>,
key.dependencies: [
{
key.kind: source.lang.swift.import.module.swift,
key.name: "Swift",
key.filepath: Swift.swiftmodule,
key.hash: <hash>,
key.is_system: 1
}
],
key.entities: [
{
key.kind: source.lang.swift.decl.class,
key.name: "FixtureClass25",
key.usr: "s:17index_objcMembers14FixtureClass25C",
key.line: 5,
key.column: 20,
key.entities: [
{
key.kind: source.lang.swift.decl.var.instance,
key.name: "someVar",
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvp",
key.line: 6,
key.column: 9,
key.entities: [
{
key.kind: source.lang.swift.decl.function.accessor.getter,
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvg",
key.line: 6,
key.column: 9,
key.is_dynamic: 1,
key.is_implicit: 1
},
{
key.kind: source.lang.swift.decl.function.accessor.setter,
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvs",
key.line: 6,
key.column: 9,
key.is_dynamic: 1,
key.is_implicit: 1
}
]
},
{
key.kind: source.lang.swift.ref.struct,
key.name: "String",
key.usr: "s:SS",
key.line: 6,
key.column: 18
},
{
key.kind: source.lang.swift.decl.function.method.instance,
key.name: "someMethod()",
key.usr: "s:17index_objcMembers14FixtureClass25C10someMethodyyF",
key.line: 7,
key.column: 10,
key.is_dynamic: 1,
key.attributes: [
{
key.attribute: source.decl.attribute.objc
}
]
},
{
key.kind: source.lang.swift.decl.function.constructor,
key.usr: "s:17index_objcMembers14FixtureClass25CACycfc",
key.line: 5,
key.column: 20,
key.is_implicit: 1,
key.attributes: [
{
key.attribute: source.decl.attribute.objc
}
]
}
],
key.attributes: [
{
key.attribute: source.decl.attribute.objcMembers
}
]
}
]
}
21 changes: 17 additions & 4 deletions tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ class SKIndexDataConsumer : public IndexDataConsumer {
return impl.finishSourceEntity(UID);
}

std::vector<UIdent> getDeclAttributeUIDs(const Decl *decl) {
std::vector<UIdent> uidAttrs =
SwiftLangSupport::UIDsFromDeclAttributes(decl->getAttrs());

// check if we should report an implicit @objc attribute
if (!decl->getAttrs().getAttribute(DeclAttrKind::DAK_ObjC)) {
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
if (VD->isObjC()) {
uidAttrs.push_back(SwiftLangSupport::getUIDForObjCAttr());
}
}
}
return uidAttrs;
}

template <typename F>
bool withEntityInfo(const IndexSymbol &symbol, F func) {
EntityInfo info;
Expand All @@ -122,8 +137,7 @@ class SKIndexDataConsumer : public IndexDataConsumer {
info.IsTestCandidate = symbol.symInfo.Properties & SymbolProperty::UnitTest;
std::vector<UIdent> uidAttrs;
if (!isRef) {
uidAttrs =
SwiftLangSupport::UIDsFromDeclAttributes(symbol.decl->getAttrs());
uidAttrs = getDeclAttributeUIDs(symbol.decl);
info.Attrs = uidAttrs;
}
return func(info);
Expand All @@ -142,8 +156,7 @@ class SKIndexDataConsumer : public IndexDataConsumer {
info.IsTestCandidate = relation.symInfo.Properties & SymbolProperty::UnitTest;
std::vector<UIdent> uidAttrs;
if (!isRef) {
uidAttrs =
SwiftLangSupport::UIDsFromDeclAttributes(relation.decl->getAttrs());
uidAttrs = getDeclAttributeUIDs(relation.decl);
info.Attrs = uidAttrs;
}
return func(info);
Expand Down
51 changes: 27 additions & 24 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ using swift::index::SymbolRoleSet;
#define REFACTORING(KIND, NAME, ID) static UIdent Kind##Refactoring##KIND("source.refactoring.kind."#ID);
#include "swift/IDE/RefactoringKinds.def"

static UIdent Attr_IBAction("source.decl.attribute.ibaction");
static UIdent Attr_IBOutlet("source.decl.attribute.iboutlet");
static UIdent Attr_IBDesignable("source.decl.attribute.ibdesignable");
static UIdent Attr_IBInspectable("source.decl.attribute.ibinspectable");
static UIdent Attr_GKInspectable("source.decl.attribute.gkinspectable");
static UIdent Attr_Objc("source.decl.attribute.objc");
static UIdent Attr_ObjcNamed("source.decl.attribute.objc.name");
static UIdent Attr_Private("source.decl.attribute.private");
static UIdent Attr_FilePrivate("source.decl.attribute.fileprivate");
static UIdent Attr_Internal("source.decl.attribute.internal");
static UIdent Attr_Public("source.decl.attribute.public");
static UIdent Attr_Open("source.decl.attribute.open");
static UIdent Attr_Setter_Private("source.decl.attribute.setter_access.private");
static UIdent Attr_Setter_FilePrivate("source.decl.attribute.setter_access.fileprivate");
static UIdent Attr_Setter_Internal("source.decl.attribute.setter_access.internal");
static UIdent Attr_Setter_Public("source.decl.attribute.setter_access.public");
static UIdent Attr_Setter_Open("source.decl.attribute.setter_access.open");

std::unique_ptr<LangSupport>
SourceKit::createSwiftLangSupport(SourceKit::Context &SKCtx) {
return std::unique_ptr<LangSupport>(new SwiftLangSupport(SKCtx));
Expand Down Expand Up @@ -264,6 +282,10 @@ SourceKit::UIdent SwiftLangSupport::getUIDForModuleRef() {
return KindRefModule;
}

SourceKit::UIdent SwiftLangSupport::getUIDForObjCAttr() {
return Attr_Objc;
}

UIdent SwiftLangSupport::getUIDForRefactoringKind(ide::RefactoringKind Kind){
switch(Kind) {
case ide::RefactoringKind::None: llvm_unreachable("cannot end up here.");
Expand Down Expand Up @@ -639,41 +661,28 @@ Optional<UIdent> SwiftLangSupport::getUIDForDeclAttribute(const swift::DeclAttri
// Check special-case names first.
switch (Attr->getKind()) {
case DAK_IBAction: {
static UIdent Attr_IBAction("source.decl.attribute.ibaction");
return Attr_IBAction;
}
case DAK_IBOutlet: {
static UIdent Attr_IBOutlet("source.decl.attribute.iboutlet");
return Attr_IBOutlet;
}
case DAK_IBDesignable: {
static UIdent Attr_IBDesignable("source.decl.attribute.ibdesignable");
return Attr_IBDesignable;
}
case DAK_IBInspectable: {
static UIdent Attr_IBInspectable("source.decl.attribute.ibinspectable");
return Attr_IBInspectable;
}
case DAK_GKInspectable: {
static UIdent Attr_GKInspectable("source.decl.attribute.gkinspectable");
return Attr_GKInspectable;
}
case DAK_ObjC: {
static UIdent Attr_Objc("source.decl.attribute.objc");
static UIdent Attr_ObjcNamed("source.decl.attribute.objc.name");
if (cast<ObjCAttr>(Attr)->hasName()) {
return Attr_ObjcNamed;
} else {
return Attr_Objc;
}
}
case DAK_AccessControl: {
static UIdent Attr_Private("source.decl.attribute.private");
static UIdent Attr_FilePrivate("source.decl.attribute.fileprivate");
static UIdent Attr_Internal("source.decl.attribute.internal");
static UIdent Attr_Public("source.decl.attribute.public");
static UIdent Attr_Open("source.decl.attribute.open");

switch (cast<AbstractAccessControlAttr>(Attr)->getAccess()) {
case AccessLevel::Private:
return Attr_Private;
Expand All @@ -688,23 +697,17 @@ Optional<UIdent> SwiftLangSupport::getUIDForDeclAttribute(const swift::DeclAttri
}
}
case DAK_SetterAccess: {
static UIdent Attr_Private("source.decl.attribute.setter_access.private");
static UIdent Attr_FilePrivate("source.decl.attribute.setter_access.fileprivate");
static UIdent Attr_Internal("source.decl.attribute.setter_access.internal");
static UIdent Attr_Public("source.decl.attribute.setter_access.public");
static UIdent Attr_Open("source.decl.attribute.setter_access.open");

switch (cast<AbstractAccessControlAttr>(Attr)->getAccess()) {
case AccessLevel::Private:
return Attr_Private;
return Attr_Setter_Private;
case AccessLevel::FilePrivate:
return Attr_FilePrivate;
return Attr_Setter_FilePrivate;
case AccessLevel::Internal:
return Attr_Internal;
return Attr_Setter_Internal;
case AccessLevel::Public:
return Attr_Public;
return Attr_Setter_Public;
case AccessLevel::Open:
return Attr_Open;
return Attr_Setter_Open;
}
}

Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ class SwiftLangSupport : public LangSupport {
swift::AccessorKind AccKind,
bool IsRef = false);
static SourceKit::UIdent getUIDForModuleRef();
static SourceKit::UIdent getUIDForObjCAttr();
static SourceKit::UIdent getUIDForSyntaxNodeKind(
swift::ide::SyntaxNodeKind Kind);
static SourceKit::UIdent getUIDForSyntaxStructureKind(
Expand Down