Skip to content

Commit ece56b9

Browse files
committed
Swift: Assign indexes to extensions looking at all the extensions of a type
1 parent 463f79b commit ece56b9

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,21 @@ unsigned SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl,
112112
if (auto found = preloadedExtensionIndexes.extract(decl)) {
113113
return found.mapped();
114114
}
115-
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
116-
llvm::SmallVector<swift::Decl*> siblings;
117-
parentModule->getTopLevelDecls(siblings);
118-
indexExtensions(siblings);
119-
} else if (auto iterableParent = llvm::dyn_cast<swift::IterableDeclContext>(parent)) {
120-
indexExtensions(iterableParent->getAllMembers());
121-
} else {
122-
// TODO use a generic logging handle for Swift entities here, once it's available
123-
CODEQL_ASSERT(false, "non-local context must be module or iterable decl context");
115+
116+
auto index = 0u;
117+
for (const auto& extension : decl->getExtendedNominal()->getExtensions()) {
118+
if (getParent(extension) == parent) {
119+
preloadedExtensionIndexes.emplace(extension, index);
120+
index++;
121+
}
124122
}
123+
125124
auto found = preloadedExtensionIndexes.extract(decl);
126125
// TODO use a generic logging handle for Swift entities here, once it's available
127126
CODEQL_ASSERT(found, "extension not found within parent");
128127
return found.mapped();
129128
}
130129

131-
void SwiftMangler::indexExtensions(llvm::ArrayRef<swift::Decl*> siblings) {
132-
auto index = 0u;
133-
for (auto sibling : siblings) {
134-
if (sibling->getKind() == swift::DeclKind::Extension) {
135-
preloadedExtensionIndexes.emplace(sibling, index);
136-
}
137-
++index;
138-
}
139-
}
140-
141130
SwiftMangledName SwiftMangler::visitGenericTypeParamDecl(const swift::GenericTypeParamDecl* decl) {
142131
auto ret = visitValueDecl(decl, /*force=*/true);
143132
if (decl->isParameterPack()) {

swift/extractor/mangler/SwiftMangler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
112112
virtual SwiftMangledName fetch(const swift::TypeBase* type) = 0;
113113
SwiftMangledName fetch(swift::Type type) { return fetch(type.getPointer()); }
114114

115-
void indexExtensions(llvm::ArrayRef<swift::Decl*> siblings);
116115
unsigned int getExtensionIndex(const swift::ExtensionDecl* decl, const swift::Decl* parent);
117116
static SwiftMangledName initMangled(const swift::TypeBase* type);
118117
SwiftMangledName initMangled(const swift::Decl* decl);

0 commit comments

Comments
 (0)