1414#include " swift/extractor/infra/file/Path.h"
1515#include " swift/extractor/infra/SwiftLocationExtractor.h"
1616#include " swift/extractor/infra/SwiftBodyEmissionStrategy.h"
17- #include " swift/extractor/mangler/SwiftMangler.h"
1817#include " swift/logging/SwiftAssert.h"
1918
2019using namespace codeql ;
@@ -49,24 +48,20 @@ static void archiveFile(const SwiftExtractorConfiguration& config, swift::Source
4948 }
5049}
5150
52- // TODO: This should be factored out/replaced with simplified version of custom mangling
53- static std::string mangledDeclName (const swift::ValueDecl& decl) {
54- std::string_view moduleName = decl.getModuleContext ()->getRealName ().str ();
55- // ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
56- if (decl.getKind () == swift::DeclKind::Module) {
57- return std::string{moduleName};
58- }
59- swift::Mangle::ASTMangler mangler;
60- if (decl.getKind () == swift::DeclKind::TypeAlias) {
61- // In cases like this (when coming from PCM)
62- // typealias CFXMLTree = CFTree
63- // typealias CFXMLTreeRef = CFXMLTree
64- // mangleAnyDecl mangles both CFXMLTree and CFXMLTreeRef into 'So12CFXMLTreeRefa'
65- // which is not correct and causes inconsistencies. mangleEntity makes these two distinct
66- // prefix adds a couple of special symbols, we don't necessary need them
67- return mangler.mangleEntity (&decl);
51+ static std::string mangledDeclName (const swift::Decl& decl) {
52+ // SwiftRecursiveMangler mangled name cannot be used directly as it can be too long for file names
53+ // so we build some minimal human readable info for debuggability and append a hash
54+ auto ret = decl.getModuleContext ()->getRealName ().str ().str ();
55+ ret += ' /' ;
56+ ret += swift::Decl::getKindName (decl.getKind ());
57+ ret += ' _' ;
58+ if (auto valueDecl = llvm::dyn_cast<swift::ValueDecl>(&decl); valueDecl && valueDecl->hasName ()) {
59+ ret += valueDecl->getBaseName ().userFacingName ();
60+ ret += ' _' ;
6861 }
69- return mangler.mangleAnyDecl (&decl, /* prefix = */ false );
62+ SwiftRecursiveMangler mangler;
63+ ret += mangler.mangleDecl (decl).hash ();
64+ return ret;
7065}
7166
7267static fs::path getFilename (swift::ModuleDecl& module ,
@@ -76,20 +71,7 @@ static fs::path getFilename(swift::ModuleDecl& module,
7671 return resolvePath (primaryFile->getFilename ());
7772 }
7873 if (lazyDeclaration) {
79- // this code will be thrown away in the near future
80- auto decl = llvm::dyn_cast<swift::ValueDecl>(lazyDeclaration);
81- CODEQL_ASSERT (decl, " not a ValueDecl" );
82- auto mangled = mangledDeclName (*decl);
83- // mangled name can be too long to use as a file name, so we can't use it directly
84- mangled = picosha2::hash256_hex_string (mangled);
85- std::string ret;
86- ret += module .getRealName ().str ();
87- ret += ' _' ;
88- ret += decl->getBaseName ().userFacingName ();
89- ret += ' _' ;
90- // half a SHA2 is enough
91- ret += std::string_view (mangled).substr (0 , mangled.size () / 2 );
92- return ret;
74+ return mangledDeclName (*lazyDeclaration);
9375 }
9476 // PCM clang module
9577 if (module .isNonSwiftModule ()) {
0 commit comments