88#include < swift/AST/ASTContext.h>
99#include < swift/AST/GenericEnvironment.h>
1010#include < swift/AST/GenericParamList.h>
11+ #include < swift/AST/ClangModuleLoader.h>
12+ #include < clang/Basic/Module.h>
1113
1214using namespace codeql ;
1315
@@ -17,6 +19,43 @@ Logger& logger() {
1719 return ret;
1820}
1921
22+ const swift::ModuleDecl* getRealModuleOf (const swift::Decl* decl) {
23+ auto * swiftModule = decl->getModuleContext ();
24+ auto * clangModule = swiftModule->findUnderlyingClangModule ();
25+
26+ if (!clangModule) {
27+ return swiftModule;
28+ }
29+
30+ auto * clangModuleLoader = decl->getASTContext ().getClangModuleLoader ();
31+
32+ if (!clangModuleLoader) {
33+ return swiftModule;
34+ }
35+
36+ static std::unordered_map<const swift::Decl*, const swift::ModuleDecl*> cache;
37+
38+ if (auto result = cache.find (decl); result != cache.end ()) {
39+ return result->second ;
40+ }
41+
42+ for (const auto & submodule : clangModule->submodules ()) {
43+ if (auto * swiftSubmodule = clangModuleLoader->getWrapperForModule (submodule)) {
44+ llvm::SmallVector<swift::Decl*> children;
45+ swiftSubmodule->getTopLevelDecls (children);
46+ for (const auto child : children) {
47+ cache[child] = swiftSubmodule;
48+ }
49+ }
50+ }
51+
52+ if (auto result = cache.find (decl); result != cache.end ()) {
53+ return result->second ;
54+ } else {
55+ return swiftModule;
56+ }
57+ }
58+
2059const swift::Decl* getParent (const swift::Decl* decl) {
2160 auto context = decl->getDeclContext ();
2261 if (context->getContextKind () == swift::DeclContextKind::FileUnit) {
@@ -112,7 +151,8 @@ unsigned SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl,
112151 if (auto found = preloadedExtensionIndexes.extract (decl)) {
113152 return found.mapped ();
114153 }
115- if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
154+ if (llvm::isa<swift::ModuleDecl>(parent)) {
155+ auto * parentModule = getRealModuleOf (decl);
116156 llvm::SmallVector<swift::Decl*> siblings;
117157 parentModule->getTopLevelDecls (siblings);
118158 indexExtensions (siblings);
0 commit comments