Skip to content

Commit d1d31ba

Browse files
committed
Swift: Assign indexes to fileprivate ValueDecls
At least in the case of function declarations there can be multiple identical ones within the same module, causing data set check errors if not differentiated.
1 parent 7bf78de commit d1d31ba

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ std::string_view getTypeKindStr(const swift::TypeBase* type) {
4040

4141
} // namespace
4242

43-
std::unordered_map<const swift::Decl*, SwiftMangler::ExtensionIndex>
44-
SwiftMangler::preloadedExtensionIndexes;
43+
std::unordered_map<const swift::Decl*, SwiftMangler::ExtensionOrFilePrivateValueIndex>
44+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes;
4545

4646
SwiftMangledName SwiftMangler::initMangled(const swift::TypeBase* type) {
4747
return {getTypeKindStr(type), '_'};
@@ -75,6 +75,11 @@ SwiftMangledName SwiftMangler::visitValueDecl(const swift::ValueDecl* decl, bool
7575
if (decl->isStatic()) {
7676
ret << "|static";
7777
}
78+
if (decl->getFormalAccess() == swift::AccessLevel::FilePrivate) {
79+
auto parent = getParent(decl);
80+
auto index = getExtensionofFilePrivateValueIndex(decl, parent);
81+
ret << "|fileprivate" << index.index;
82+
}
7883
return ret;
7984
}
8085

@@ -105,17 +110,18 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
105110

106111
auto parent = getParent(decl);
107112
auto target = decl->getExtendedType();
108-
auto index = getExtensionIndex(decl, parent);
113+
auto index = getExtensionofFilePrivateValueIndex(decl, parent);
109114
return initMangled(decl) << fetch(target) << index.index
110115
<< (index.kind == ExtensionKind::clang ? "_clang" : "");
111116
}
112117

113-
SwiftMangler::ExtensionIndex SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl,
114-
const swift::Decl* parent) {
115-
// to avoid iterating multiple times on the parent of multiple extensions, we preload extension
116-
// indexes once for each encountered parent into the `preloadedExtensionIndexes` mapping.
117-
if (auto found = SwiftMangler::preloadedExtensionIndexes.find(decl);
118-
found != SwiftMangler::preloadedExtensionIndexes.end()) {
118+
SwiftMangler::ExtensionOrFilePrivateValueIndex SwiftMangler::getExtensionofFilePrivateValueIndex(
119+
const swift::Decl* decl,
120+
const swift::Decl* parent) {
121+
// to avoid iterating multiple times on the parent, we preload the indexes once for each
122+
// encountered parent.
123+
if (auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
124+
found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end()) {
119125
return found->second;
120126
}
121127
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
@@ -131,18 +137,25 @@ SwiftMangler::ExtensionIndex SwiftMangler::getExtensionIndex(const swift::Extens
131137
// TODO use a generic logging handle for Swift entities here, once it's available
132138
CODEQL_ASSERT(false, "non-local context must be module or iterable decl context");
133139
}
134-
auto found = SwiftMangler::preloadedExtensionIndexes.find(decl);
140+
auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
135141
// TODO use a generic logging handle for Swift entities here, once it's available
136-
CODEQL_ASSERT(found != SwiftMangler::preloadedExtensionIndexes.end(),
137-
"extension not found within parent");
142+
CODEQL_ASSERT(found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end(),
143+
"declaration not found within parent");
138144
return found->second;
139145
}
140146

141147
void SwiftMangler::indexExtensions(llvm::ArrayRef<swift::Decl*> siblings) {
142148
auto index = 0u;
143149
for (auto sibling : siblings) {
144150
if (sibling->getKind() == swift::DeclKind::Extension) {
145-
SwiftMangler::preloadedExtensionIndexes.try_emplace(sibling, ExtensionKind::swift, index);
151+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
152+
sibling, ExtensionKind::swift, index);
153+
index++;
154+
} else if (swift::isa<swift::ValueDecl>(sibling) &&
155+
swift::dyn_cast<swift::ValueDecl>(sibling)->getFormalAccess() ==
156+
swift::AccessLevel::FilePrivate) {
157+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
158+
sibling, ExtensionKind::swift, index);
146159
index++;
147160
}
148161
}
@@ -161,7 +174,8 @@ void SwiftMangler::indexClangExtensions(const clang::Module* clangModule,
161174
swiftSubmodule->getTopLevelDecls(children);
162175
for (const auto child : children) {
163176
if (child->getKind() == swift::DeclKind::Extension) {
164-
SwiftMangler::preloadedExtensionIndexes.try_emplace(child, ExtensionKind::clang, index);
177+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
178+
child, ExtensionKind::clang, index);
165179
index++;
166180
}
167181
}

swift/extractor/mangler/SwiftMangler.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
112112
clang,
113113
};
114114

115-
struct ExtensionIndex {
115+
struct ExtensionOrFilePrivateValueIndex {
116116
const ExtensionKind kind : 1;
117117
const uint32_t index : 31;
118118
};
119119

120-
static std::unordered_map<const swift::Decl*, ExtensionIndex> preloadedExtensionIndexes;
120+
static std::unordered_map<const swift::Decl*, ExtensionOrFilePrivateValueIndex>
121+
preloadedExtensionOrFilePrivateValueIndexes;
121122

122123
virtual SwiftMangledName fetch(const swift::Decl* decl) = 0;
123124
virtual SwiftMangledName fetch(const swift::TypeBase* type) = 0;
@@ -126,7 +127,8 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
126127
void indexExtensions(llvm::ArrayRef<swift::Decl*> siblings);
127128
void indexClangExtensions(const clang::Module* clangModule,
128129
swift::ClangModuleLoader* moduleLoader);
129-
ExtensionIndex getExtensionIndex(const swift::ExtensionDecl* decl, const swift::Decl* parent);
130+
ExtensionOrFilePrivateValueIndex getExtensionofFilePrivateValueIndex(const swift::Decl* decl,
131+
const swift::Decl* parent);
130132
static SwiftMangledName initMangled(const swift::TypeBase* type);
131133
SwiftMangledName initMangled(const swift::Decl* decl);
132134
SwiftMangledName visitTypeDiscriminatedValueDecl(const swift::ValueDecl* decl);

0 commit comments

Comments
 (0)