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,32 +48,20 @@ static void archiveFile(const SwiftExtractorConfiguration& config, swift::Source
4948 }
5049}
5150
52- // TODO: This will be factored out/replaced with a 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);
68- }
69- if (decl.getKind () == swift::DeclKind::GenericTypeParam) {
70- // internal mangling does not distinguish generic type parameters with the same name and
71- // position of different functions. We prepend the context (that is, the function) to
72- // circumvent that
73- auto context = llvm::dyn_cast<swift::ValueDecl>(decl.getDeclContext ()->getAsDecl ());
74- assert (context);
75- return mangledDeclName (*context) + ' _' + mangler.mangleAnyDecl (&decl, /* prefix = */ false );
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 += ' _' ;
7661 }
77- return mangler.mangleAnyDecl (&decl, /* prefix = */ false );
62+ SwiftRecursiveMangler mangler;
63+ ret += mangler.mangleDecl (decl).hash ();
64+ return ret;
7865}
7966
8067static fs::path getFilename (swift::ModuleDecl& module ,
@@ -84,20 +71,7 @@ static fs::path getFilename(swift::ModuleDecl& module,
8471 return resolvePath (primaryFile->getFilename ());
8572 }
8673 if (lazyDeclaration) {
87- // this code will be thrown away in the near future
88- auto decl = llvm::dyn_cast<swift::ValueDecl>(lazyDeclaration);
89- CODEQL_ASSERT (decl, " not a ValueDecl" );
90- auto mangled = mangledDeclName (*decl);
91- // mangled name can be too long to use as a file name, so we can't use it directly
92- mangled = picosha2::hash256_hex_string (mangled);
93- std::string ret;
94- ret += module .getRealName ().str ();
95- ret += ' _' ;
96- ret += decl->getBaseName ().userFacingName ();
97- ret += ' _' ;
98- // half a SHA2 is enough
99- ret += std::string_view (mangled).substr (0 , mangled.size () / 2 );
100- return ret;
74+ return mangledDeclName (*lazyDeclaration);
10175 }
10276 // PCM clang module
10377 if (module .isNonSwiftModule ()) {
0 commit comments