Skip to content

Commit ad34f89

Browse files
committed
[index] Add UnitTest SymbolSubKind and collapse the IndexSymbol inheritance
Fold UnitTest into the subkind, now that it's a bitself, and then remove the unnecessary inheritance for IndexSymbol and its SourceKit indexing equivalent.
1 parent 6096b8b commit ad34f89

File tree

5 files changed

+30
-90
lines changed

5 files changed

+30
-90
lines changed

include/swift/Index/IndexSymbol.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ enum class SymbolSubKind : uint32_t {
7272
ExtensionOfClass = 1 << 7,
7373
ExtensionOfEnum = 1 << 8,
7474
ExtensionOfProtocol = 1 << 9,
75+
76+
UnitTest = 1 << 10,
7577
};
7678

7779
typedef uint32_t SymbolSubKindSet;
@@ -90,9 +92,6 @@ using SymbolRole = clang::index::SymbolRole;
9092
using SymbolRoleSet = clang::index::SymbolRoleSet;
9193

9294
struct IndexSymbol {
93-
enum TypeKind { Base, FuncDecl };
94-
TypeKind entityType = Base;
95-
9695
SymbolKind kind;
9796
SymbolSubKindSet subKinds = SymbolSubKindSet(0);
9897
SymbolRoleSet roles = SymbolRoleSet(0);
@@ -106,18 +105,8 @@ struct IndexSymbol {
106105
unsigned column = 0;
107106

108107
IndexSymbol() = default;
109-
110-
protected:
111-
IndexSymbol(TypeKind TK) : entityType(TK) {}
112108
};
113109

114-
struct FuncDeclIndexSymbol : public IndexSymbol {
115-
bool IsTestCandidate = false;
116-
117-
FuncDeclIndexSymbol() : IndexSymbol(FuncDecl) {}
118-
};
119-
120-
121110
SymbolKind getSymbolKindForDecl(const Decl *D);
122111

123112
} // end namespace index

lib/Index/Index.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
278278

279279
bool initIndexSymbol(ValueDecl *D, SourceLoc Loc, bool IsRef,
280280
IndexSymbol &Info);
281-
bool initFuncDeclIndexSymbol(ValueDecl *D, FuncDeclIndexSymbol &Info);
281+
bool initFuncDeclIndexSymbol(ValueDecl *D, IndexSymbol &Info);
282282
bool initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE, ValueDecl *D,
283283
SourceLoc Loc, IndexSymbol &Info);
284284

@@ -458,7 +458,7 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
458458
return false;
459459

460460
if (isa<FuncDecl>(D)) {
461-
FuncDeclIndexSymbol Info;
461+
IndexSymbol Info;
462462
if (initFuncDeclIndexSymbol(D, Info))
463463
return false;
464464

@@ -818,11 +818,13 @@ static bool isTestCandidate(ValueDecl *D) {
818818
}
819819

820820
bool IndexSwiftASTWalker::initFuncDeclIndexSymbol(ValueDecl *D,
821-
FuncDeclIndexSymbol &Info) {
821+
IndexSymbol &Info) {
822822
if (initIndexSymbol(D, D->getLoc(), /*IsRef=*/false, Info))
823823
return true;
824824

825-
Info.IsTestCandidate = isTestCandidate(D);
825+
if (isTestCandidate(D))
826+
Info.subKinds |= SymbolSubKind::UnitTest;
827+
826828
if (auto Group = D->getGroupName())
827829
Info.group = Group.getValue();
828830
return false;

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,17 @@ namespace SourceKit {
3030
class Context;
3131

3232
struct EntityInfo {
33-
enum TypeKind {
34-
Base,
35-
FuncDecl,
36-
CallReference
37-
};
38-
TypeKind EntityType = Base;
39-
4033
UIdent Kind;
4134
StringRef Name;
4235
StringRef USR;
4336
StringRef Group;
37+
StringRef ReceiverUSR;
38+
bool IsDynamic = false;
39+
bool IsTestCandidate = false;
4440
unsigned Line = 0;
4541
unsigned Column = 0;
4642

4743
EntityInfo() = default;
48-
49-
protected:
50-
EntityInfo(TypeKind TK) : EntityType(TK) { }
51-
};
52-
53-
struct FuncDeclEntityInfo : public EntityInfo {
54-
bool IsTestCandidate = false;
55-
56-
FuncDeclEntityInfo() : EntityInfo(FuncDecl) { }
57-
};
58-
59-
struct CallRefEntityInfo : public EntityInfo {
60-
StringRef ReceiverUSR;
61-
bool IsDynamic = false;
62-
63-
CallRefEntityInfo() : EntityInfo(CallReference) { }
6444
};
6545

6646
class IndexingConsumer {

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,19 @@ class SKIndexDataConsumer : public IndexDataConsumer {
9292

9393
template <typename F>
9494
bool withEntityInfo(const IndexSymbol &symbol, F func) {
95-
auto initEntity = [](EntityInfo &info, const IndexSymbol &symbol) {
96-
bool isRef = symbol.roles & (unsigned)SymbolRole::Reference;
97-
info.Kind = SwiftLangSupport::getUIDForSymbol(symbol.kind, symbol.subKinds,
98-
isRef);
99-
info.Name = symbol.name;
100-
info.USR = symbol.USR;
101-
info.Group = symbol.group;
102-
info.Line = symbol.line;
103-
info.Column = symbol.column;
104-
};
105-
106-
if (symbol.roles & (unsigned)SymbolRole::Call) {
107-
assert(symbol.entityType == IndexSymbol::Base);
108-
CallRefEntityInfo info;
109-
initEntity(info, symbol);
110-
info.ReceiverUSR = symbol.receiverUSR;
111-
info.IsDynamic = symbol.roles & (unsigned)SymbolRole::Dynamic;
112-
return func(info);
113-
}
114-
115-
switch (symbol.entityType) {
116-
case IndexSymbol::Base: {
117-
EntityInfo info;
118-
initEntity(info, symbol);
119-
return func(info);
120-
}
121-
case IndexSymbol::FuncDecl: {
122-
FuncDeclEntityInfo info;
123-
initEntity(info, symbol);
124-
info.IsTestCandidate =
125-
static_cast<const FuncDeclIndexSymbol &>(symbol).IsTestCandidate;
126-
return func(info);
127-
}
128-
}
129-
llvm_unreachable("unexpected symbol kind");
95+
EntityInfo info;
96+
bool isRef = symbol.roles & (unsigned)SymbolRole::Reference;
97+
info.Kind = SwiftLangSupport::getUIDForSymbol(symbol.kind, symbol.subKinds,
98+
isRef);
99+
info.Name = symbol.name;
100+
info.USR = symbol.USR;
101+
info.Group = symbol.group;
102+
info.Line = symbol.line;
103+
info.Column = symbol.column;
104+
info.ReceiverUSR = symbol.receiverUSR;
105+
info.IsDynamic = symbol.roles & (unsigned)SymbolRole::Dynamic;
106+
info.IsTestCandidate = symbol.subKinds & SymbolSubKind::UnitTest;
107+
return func(info);
130108
}
131109

132110
private:

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -882,21 +882,12 @@ bool SKIndexingConsumer::startSourceEntity(const EntityInfo &Info) {
882882
if (!Info.Group.empty())
883883
Elem.set(KeyGroupName, Info.Group);
884884

885-
if (Info.EntityType == EntityInfo::FuncDecl) {
886-
const FuncDeclEntityInfo &FDInfo =
887-
static_cast<const FuncDeclEntityInfo &>(Info);
888-
if (FDInfo.IsTestCandidate)
889-
Elem.setBool(KeyIsTestCandidate, true);
890-
}
891-
892-
if (Info.EntityType == EntityInfo::CallReference) {
893-
const CallRefEntityInfo &CRInfo =
894-
static_cast<const CallRefEntityInfo &>(Info);
895-
if (!CRInfo.ReceiverUSR.empty())
896-
Elem.set(KeyReceiverUSR, CRInfo.ReceiverUSR);
897-
if (CRInfo.IsDynamic)
898-
Elem.setBool(KeyIsDynamic, true);
899-
}
885+
if (!Info.ReceiverUSR.empty())
886+
Elem.set(KeyReceiverUSR, Info.ReceiverUSR);
887+
if (Info.IsDynamic)
888+
Elem.setBool(KeyIsDynamic, true);
889+
if (Info.IsTestCandidate)
890+
Elem.setBool(KeyIsTestCandidate, true);
900891

901892
EntitiesStack.push_back({ Info.Kind, Elem, ResponseBuilder::Array(),
902893
ResponseBuilder::Array()});

0 commit comments

Comments
 (0)