@@ -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,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 }
0 commit comments