Skip to content

Commit d09e2f6

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 d09e2f6

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 42 additions & 23 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,12 @@ 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 = getExtensionOrFilePrivateValueIndex(decl, parent);
81+
ret << "|fileprivate" << index.index
82+
<< (index.kind == ExtensionOrFilePrivateValueKind::clang ? "_clang" : "");
83+
}
7884
return ret;
7985
}
8086

@@ -105,51 +111,63 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
105111

106112
auto parent = getParent(decl);
107113
auto target = decl->getExtendedType();
108-
auto index = getExtensionIndex(decl, parent);
114+
auto index = getExtensionOrFilePrivateValueIndex(decl, parent);
109115
return initMangled(decl) << fetch(target) << index.index
110-
<< (index.kind == ExtensionKind::clang ? "_clang" : "");
116+
<< (index.kind == ExtensionOrFilePrivateValueKind::clang ? "_clang"
117+
: "");
111118
}
112119

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()) {
120+
SwiftMangler::ExtensionOrFilePrivateValueIndex SwiftMangler::getExtensionOrFilePrivateValueIndex(
121+
const swift::Decl* decl,
122+
const swift::Decl* parent) {
123+
// to avoid iterating multiple times on the parent, we preload the indexes once for each
124+
// encountered parent.
125+
if (auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
126+
found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end()) {
119127
return found->second;
120128
}
121129
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
122130
llvm::SmallVector<swift::Decl*> siblings;
123131
parentModule->getTopLevelDecls(siblings);
124-
indexExtensions(siblings);
132+
indexExtensionsAndFilePrivateValues(siblings);
125133
if (auto clangModule = parentModule->findUnderlyingClangModule()) {
126-
indexClangExtensions(clangModule, decl->getASTContext().getClangModuleLoader());
134+
indexClangExtensionsAndFilePrivateValues(clangModule,
135+
decl->getASTContext().getClangModuleLoader());
127136
}
128137
} else if (auto iterableParent = llvm::dyn_cast<swift::IterableDeclContext>(parent)) {
129-
indexExtensions(iterableParent->getAllMembers());
138+
indexExtensionsAndFilePrivateValues(iterableParent->getAllMembers());
130139
} else {
131140
// TODO use a generic logging handle for Swift entities here, once it's available
132141
CODEQL_ASSERT(false, "non-local context must be module or iterable decl context");
133142
}
134-
auto found = SwiftMangler::preloadedExtensionIndexes.find(decl);
143+
auto found = SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.find(decl);
135144
// 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");
145+
CODEQL_ASSERT(found != SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.end(),
146+
"declaration not found within parent");
138147
return found->second;
139148
}
140149

141-
void SwiftMangler::indexExtensions(llvm::ArrayRef<swift::Decl*> siblings) {
150+
bool SwiftMangler::isExtensionOrFilePrivateValue(const swift::Decl* decl) {
151+
return decl->getKind() == swift::DeclKind::Extension ||
152+
(swift::isa<swift::ValueDecl>(decl) &&
153+
swift::dyn_cast<swift::ValueDecl>(decl)->getFormalAccess() ==
154+
swift::AccessLevel::FilePrivate);
155+
}
156+
157+
void SwiftMangler::indexExtensionsAndFilePrivateValues(llvm::ArrayRef<swift::Decl*> siblings) {
142158
auto index = 0u;
143159
for (auto sibling : siblings) {
144-
if (sibling->getKind() == swift::DeclKind::Extension) {
145-
SwiftMangler::preloadedExtensionIndexes.try_emplace(sibling, ExtensionKind::swift, index);
160+
if (isExtensionOrFilePrivateValue(sibling)) {
161+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
162+
sibling, ExtensionOrFilePrivateValueKind::swift, index);
146163
index++;
147164
}
148165
}
149166
}
150167

151-
void SwiftMangler::indexClangExtensions(const clang::Module* clangModule,
152-
swift::ClangModuleLoader* moduleLoader) {
168+
void SwiftMangler::indexClangExtensionsAndFilePrivateValues(
169+
const clang::Module* clangModule,
170+
swift::ClangModuleLoader* moduleLoader) {
153171
if (!moduleLoader) {
154172
return;
155173
}
@@ -160,8 +178,9 @@ void SwiftMangler::indexClangExtensions(const clang::Module* clangModule,
160178
llvm::SmallVector<swift::Decl*> children;
161179
swiftSubmodule->getTopLevelDecls(children);
162180
for (const auto child : children) {
163-
if (child->getKind() == swift::DeclKind::Extension) {
164-
SwiftMangler::preloadedExtensionIndexes.try_emplace(child, ExtensionKind::clang, index);
181+
if (isExtensionOrFilePrivateValue(child)) {
182+
SwiftMangler::preloadedExtensionOrFilePrivateValueIndexes.try_emplace(
183+
child, ExtensionOrFilePrivateValueKind::clang, index);
165184
index++;
166185
}
167186
}

swift/extractor/mangler/SwiftMangler.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,29 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
107107
SwiftMangledName visitPackExpansionType(const swift::PackExpansionType* type);
108108

109109
private:
110-
enum class ExtensionKind : bool {
110+
enum class ExtensionOrFilePrivateValueKind : bool {
111111
swift,
112112
clang,
113113
};
114114

115-
struct ExtensionIndex {
116-
const ExtensionKind kind : 1;
115+
struct ExtensionOrFilePrivateValueIndex {
116+
const ExtensionOrFilePrivateValueKind 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;
124125
SwiftMangledName fetch(swift::Type type) { return fetch(type.getPointer()); }
125126

126-
void indexExtensions(llvm::ArrayRef<swift::Decl*> siblings);
127-
void indexClangExtensions(const clang::Module* clangModule,
128-
swift::ClangModuleLoader* moduleLoader);
129-
ExtensionIndex getExtensionIndex(const swift::ExtensionDecl* decl, const swift::Decl* parent);
127+
bool isExtensionOrFilePrivateValue(const swift::Decl* decl);
128+
void indexExtensionsAndFilePrivateValues(llvm::ArrayRef<swift::Decl*> siblings);
129+
void indexClangExtensionsAndFilePrivateValues(const clang::Module* clangModule,
130+
swift::ClangModuleLoader* moduleLoader);
131+
ExtensionOrFilePrivateValueIndex getExtensionOrFilePrivateValueIndex(const swift::Decl* decl,
132+
const swift::Decl* parent);
130133
static SwiftMangledName initMangled(const swift::TypeBase* type);
131134
SwiftMangledName initMangled(const swift::Decl* decl);
132135
SwiftMangledName visitTypeDiscriminatedValueDecl(const swift::ValueDecl* decl);

0 commit comments

Comments
 (0)