Skip to content

Commit a3b4c32

Browse files
committed
Swift: stamp all named declarations with an id-ref to the containing module
1 parent f7a046c commit a3b4c32

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,15 @@ std::optional<codeql::ModuleDecl> DeclTranslator::translateModuleDecl(
303303
}
304304

305305
std::string DeclTranslator::mangledName(const swift::ValueDecl& decl) {
306+
std::string_view moduleName = decl.getModuleContext()->getRealName().str();
306307
// ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
307-
// TODO find a more unique string working also when different modules are compiled with the same
308-
// name
309-
std::ostringstream ret;
310308
if (decl.getKind() == swift::DeclKind::Module) {
311-
ret << static_cast<const swift::ModuleDecl&>(decl).getRealName().str().str();
312-
} else if (decl.getKind() == swift::DeclKind::TypeAlias) {
309+
return std::string{moduleName};
310+
}
311+
std::ostringstream ret;
312+
// stamp all declarations with an id-ref of the containing module
313+
ret << '{' << ModuleDeclTag::prefix << '_' << moduleName << '}';
314+
if (decl.getKind() == swift::DeclKind::TypeAlias) {
313315
// In cases like this (when coming from PCM)
314316
// typealias CFXMLTree = CFTree
315317
// typealias CFXMLTreeRef = CFXMLTree
@@ -321,14 +323,6 @@ std::string DeclTranslator::mangledName(const swift::ValueDecl& decl) {
321323
// prefix adds a couple of special symbols, we don't necessary need them
322324
ret << mangler.mangleAnyDecl(&decl, /* prefix = */ false);
323325
}
324-
// there can be separate declarations (`VarDecl` or `AccessorDecl`) which are effectively the same
325-
// (with equal mangled name) but come from different clang modules. This is the case for example
326-
// for glibc constants like `L_SET` that appear in both `SwiftGlibc` and `CDispatch`.
327-
// For the moment, we sidestep the problem by making them separate entities in the DB
328-
// TODO find a more solid solution
329-
if (decl.getModuleContext()->isNonSwiftModule()) {
330-
ret << '_' << decl.getModuleContext()->getRealName().str().str();
331-
}
332326
return ret.str();
333327
}
334328

0 commit comments

Comments
 (0)