Skip to content

Commit 8a1eebf

Browse files
committed
Swift: Work around assertion failures in mangler
1 parent 6e83743 commit 8a1eebf

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
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

1214
using 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+
2059
const 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

Comments
 (0)