Skip to content

Commit 9bd3935

Browse files
author
Nathan Hawes
authored
Merge pull request #22793 from nathawes/add-implicit-at-objc-attributes-to-index-request-output
[sourcekitd] Report entities that are implicitly @objc as @objc in the index request responses for compatibility
2 parents 36318c5 + 7680cc6 commit 9bd3935

File tree

5 files changed

+138
-28
lines changed

5 files changed

+138
-28
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %sourcekitd-test -req=index %s -- -Xfrontend -serialize-diagnostics-path -Xfrontend %t.dia %s | %sed_clean > %t.response
2+
// RUN: diff -u %s.response %t.response
3+
// REQUIRES: objc_interop
4+
5+
@objcMembers class FixtureClass25: NSObject {
6+
var someVar: String?
7+
func someMethod() {}
8+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
key.hash: <hash>,
3+
key.dependencies: [
4+
{
5+
key.kind: source.lang.swift.import.module.swift,
6+
key.name: "Swift",
7+
key.filepath: Swift.swiftmodule,
8+
key.hash: <hash>,
9+
key.is_system: 1
10+
}
11+
],
12+
key.entities: [
13+
{
14+
key.kind: source.lang.swift.decl.class,
15+
key.name: "FixtureClass25",
16+
key.usr: "s:17index_objcMembers14FixtureClass25C",
17+
key.line: 5,
18+
key.column: 20,
19+
key.entities: [
20+
{
21+
key.kind: source.lang.swift.decl.var.instance,
22+
key.name: "someVar",
23+
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvp",
24+
key.line: 6,
25+
key.column: 9,
26+
key.entities: [
27+
{
28+
key.kind: source.lang.swift.decl.function.accessor.getter,
29+
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvg",
30+
key.line: 6,
31+
key.column: 9,
32+
key.is_dynamic: 1,
33+
key.is_implicit: 1
34+
},
35+
{
36+
key.kind: source.lang.swift.decl.function.accessor.setter,
37+
key.usr: "s:17index_objcMembers14FixtureClass25C7someVarSSSgvs",
38+
key.line: 6,
39+
key.column: 9,
40+
key.is_dynamic: 1,
41+
key.is_implicit: 1
42+
}
43+
]
44+
},
45+
{
46+
key.kind: source.lang.swift.ref.struct,
47+
key.name: "String",
48+
key.usr: "s:SS",
49+
key.line: 6,
50+
key.column: 18
51+
},
52+
{
53+
key.kind: source.lang.swift.decl.function.method.instance,
54+
key.name: "someMethod()",
55+
key.usr: "s:17index_objcMembers14FixtureClass25C10someMethodyyF",
56+
key.line: 7,
57+
key.column: 10,
58+
key.is_dynamic: 1,
59+
key.attributes: [
60+
{
61+
key.attribute: source.decl.attribute.objc
62+
}
63+
]
64+
},
65+
{
66+
key.kind: source.lang.swift.decl.function.constructor,
67+
key.usr: "s:17index_objcMembers14FixtureClass25CACycfc",
68+
key.line: 5,
69+
key.column: 20,
70+
key.is_implicit: 1,
71+
key.attributes: [
72+
{
73+
key.attribute: source.decl.attribute.objc
74+
}
75+
]
76+
}
77+
],
78+
key.attributes: [
79+
{
80+
key.attribute: source.decl.attribute.objcMembers
81+
}
82+
]
83+
}
84+
]
85+
}

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ class SKIndexDataConsumer : public IndexDataConsumer {
104104
return impl.finishSourceEntity(UID);
105105
}
106106

107+
std::vector<UIdent> getDeclAttributeUIDs(const Decl *decl) {
108+
std::vector<UIdent> uidAttrs =
109+
SwiftLangSupport::UIDsFromDeclAttributes(decl->getAttrs());
110+
111+
// check if we should report an implicit @objc attribute
112+
if (!decl->getAttrs().getAttribute(DeclAttrKind::DAK_ObjC)) {
113+
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
114+
if (VD->isObjC()) {
115+
uidAttrs.push_back(SwiftLangSupport::getUIDForObjCAttr());
116+
}
117+
}
118+
}
119+
return uidAttrs;
120+
}
121+
107122
template <typename F>
108123
bool withEntityInfo(const IndexSymbol &symbol, F func) {
109124
EntityInfo info;
@@ -122,8 +137,7 @@ class SKIndexDataConsumer : public IndexDataConsumer {
122137
info.IsTestCandidate = symbol.symInfo.Properties & SymbolProperty::UnitTest;
123138
std::vector<UIdent> uidAttrs;
124139
if (!isRef) {
125-
uidAttrs =
126-
SwiftLangSupport::UIDsFromDeclAttributes(symbol.decl->getAttrs());
140+
uidAttrs = getDeclAttributeUIDs(symbol.decl);
127141
info.Attrs = uidAttrs;
128142
}
129143
return func(info);
@@ -142,8 +156,7 @@ class SKIndexDataConsumer : public IndexDataConsumer {
142156
info.IsTestCandidate = relation.symInfo.Properties & SymbolProperty::UnitTest;
143157
std::vector<UIdent> uidAttrs;
144158
if (!isRef) {
145-
uidAttrs =
146-
SwiftLangSupport::UIDsFromDeclAttributes(relation.decl->getAttrs());
159+
uidAttrs = getDeclAttributeUIDs(relation.decl);
147160
info.Attrs = uidAttrs;
148161
}
149162
return func(info);

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ using swift::index::SymbolRoleSet;
6060
#define REFACTORING(KIND, NAME, ID) static UIdent Kind##Refactoring##KIND("source.refactoring.kind."#ID);
6161
#include "swift/IDE/RefactoringKinds.def"
6262

63+
static UIdent Attr_IBAction("source.decl.attribute.ibaction");
64+
static UIdent Attr_IBOutlet("source.decl.attribute.iboutlet");
65+
static UIdent Attr_IBDesignable("source.decl.attribute.ibdesignable");
66+
static UIdent Attr_IBInspectable("source.decl.attribute.ibinspectable");
67+
static UIdent Attr_GKInspectable("source.decl.attribute.gkinspectable");
68+
static UIdent Attr_Objc("source.decl.attribute.objc");
69+
static UIdent Attr_ObjcNamed("source.decl.attribute.objc.name");
70+
static UIdent Attr_Private("source.decl.attribute.private");
71+
static UIdent Attr_FilePrivate("source.decl.attribute.fileprivate");
72+
static UIdent Attr_Internal("source.decl.attribute.internal");
73+
static UIdent Attr_Public("source.decl.attribute.public");
74+
static UIdent Attr_Open("source.decl.attribute.open");
75+
static UIdent Attr_Setter_Private("source.decl.attribute.setter_access.private");
76+
static UIdent Attr_Setter_FilePrivate("source.decl.attribute.setter_access.fileprivate");
77+
static UIdent Attr_Setter_Internal("source.decl.attribute.setter_access.internal");
78+
static UIdent Attr_Setter_Public("source.decl.attribute.setter_access.public");
79+
static UIdent Attr_Setter_Open("source.decl.attribute.setter_access.open");
80+
6381
std::unique_ptr<LangSupport>
6482
SourceKit::createSwiftLangSupport(SourceKit::Context &SKCtx) {
6583
return std::unique_ptr<LangSupport>(new SwiftLangSupport(SKCtx));
@@ -264,6 +282,10 @@ SourceKit::UIdent SwiftLangSupport::getUIDForModuleRef() {
264282
return KindRefModule;
265283
}
266284

285+
SourceKit::UIdent SwiftLangSupport::getUIDForObjCAttr() {
286+
return Attr_Objc;
287+
}
288+
267289
UIdent SwiftLangSupport::getUIDForRefactoringKind(ide::RefactoringKind Kind){
268290
switch(Kind) {
269291
case ide::RefactoringKind::None: llvm_unreachable("cannot end up here.");
@@ -639,41 +661,28 @@ Optional<UIdent> SwiftLangSupport::getUIDForDeclAttribute(const swift::DeclAttri
639661
// Check special-case names first.
640662
switch (Attr->getKind()) {
641663
case DAK_IBAction: {
642-
static UIdent Attr_IBAction("source.decl.attribute.ibaction");
643664
return Attr_IBAction;
644665
}
645666
case DAK_IBOutlet: {
646-
static UIdent Attr_IBOutlet("source.decl.attribute.iboutlet");
647667
return Attr_IBOutlet;
648668
}
649669
case DAK_IBDesignable: {
650-
static UIdent Attr_IBDesignable("source.decl.attribute.ibdesignable");
651670
return Attr_IBDesignable;
652671
}
653672
case DAK_IBInspectable: {
654-
static UIdent Attr_IBInspectable("source.decl.attribute.ibinspectable");
655673
return Attr_IBInspectable;
656674
}
657675
case DAK_GKInspectable: {
658-
static UIdent Attr_GKInspectable("source.decl.attribute.gkinspectable");
659676
return Attr_GKInspectable;
660677
}
661678
case DAK_ObjC: {
662-
static UIdent Attr_Objc("source.decl.attribute.objc");
663-
static UIdent Attr_ObjcNamed("source.decl.attribute.objc.name");
664679
if (cast<ObjCAttr>(Attr)->hasName()) {
665680
return Attr_ObjcNamed;
666681
} else {
667682
return Attr_Objc;
668683
}
669684
}
670685
case DAK_AccessControl: {
671-
static UIdent Attr_Private("source.decl.attribute.private");
672-
static UIdent Attr_FilePrivate("source.decl.attribute.fileprivate");
673-
static UIdent Attr_Internal("source.decl.attribute.internal");
674-
static UIdent Attr_Public("source.decl.attribute.public");
675-
static UIdent Attr_Open("source.decl.attribute.open");
676-
677686
switch (cast<AbstractAccessControlAttr>(Attr)->getAccess()) {
678687
case AccessLevel::Private:
679688
return Attr_Private;
@@ -688,23 +697,17 @@ Optional<UIdent> SwiftLangSupport::getUIDForDeclAttribute(const swift::DeclAttri
688697
}
689698
}
690699
case DAK_SetterAccess: {
691-
static UIdent Attr_Private("source.decl.attribute.setter_access.private");
692-
static UIdent Attr_FilePrivate("source.decl.attribute.setter_access.fileprivate");
693-
static UIdent Attr_Internal("source.decl.attribute.setter_access.internal");
694-
static UIdent Attr_Public("source.decl.attribute.setter_access.public");
695-
static UIdent Attr_Open("source.decl.attribute.setter_access.open");
696-
697700
switch (cast<AbstractAccessControlAttr>(Attr)->getAccess()) {
698701
case AccessLevel::Private:
699-
return Attr_Private;
702+
return Attr_Setter_Private;
700703
case AccessLevel::FilePrivate:
701-
return Attr_FilePrivate;
704+
return Attr_Setter_FilePrivate;
702705
case AccessLevel::Internal:
703-
return Attr_Internal;
706+
return Attr_Setter_Internal;
704707
case AccessLevel::Public:
705-
return Attr_Public;
708+
return Attr_Setter_Public;
706709
case AccessLevel::Open:
707-
return Attr_Open;
710+
return Attr_Setter_Open;
708711
}
709712
}
710713

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class SwiftLangSupport : public LangSupport {
325325
swift::AccessorKind AccKind,
326326
bool IsRef = false);
327327
static SourceKit::UIdent getUIDForModuleRef();
328+
static SourceKit::UIdent getUIDForObjCAttr();
328329
static SourceKit::UIdent getUIDForSyntaxNodeKind(
329330
swift::ide::SyntaxNodeKind Kind);
330331
static SourceKit::UIdent getUIDForSyntaxStructureKind(

0 commit comments

Comments
 (0)