@@ -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
4646SwiftMangledName 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
141147void 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 }
0 commit comments