Skip to content

Commit e84bdf5

Browse files
committed
Swift: use hashing for lazy decl trap file names
It turns out mangled names can sometimes be too long. While this code will eventually be replaced by our own mangling, we need to use hashing to cut down the names. Module and decl names are preserved in the trap file names for debuggability.
1 parent bfe5db2 commit e84bdf5

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "swift/extractor/infra/SwiftLocationExtractor.h"
1515
#include "swift/extractor/infra/SwiftBodyEmissionStrategy.h"
1616
#include "swift/extractor/mangler/SwiftMangler.h"
17+
#include <picosha2.h>
1718

1819
using namespace codeql;
1920
using namespace std::string_literals;
@@ -50,8 +51,21 @@ static fs::path getFilename(swift::ModuleDecl& module,
5051
return resolvePath(primaryFile->getFilename());
5152
}
5253
if (lazyDeclaration) {
54+
// this code will be thrown away in the near future
5355
SwiftMangler mangler;
54-
return mangler.mangledName(*lazyDeclaration);
56+
auto mangled = mangler.mangledName(*lazyDeclaration);
57+
// mangled name can be too long to use as a file name, so we can't use it directly
58+
mangled = picosha2::hash256_hex_string(mangled);
59+
std::string ret;
60+
ret += module.getRealName().str();
61+
ret += '_';
62+
llvm::SmallVector<char> scratch;
63+
// lazyDeclaration must be a ValueDecl, as already asserted in SwiftMangler::mangledName
64+
ret += llvm::cast<swift::ValueDecl>(lazyDeclaration)->getName().getString(scratch);
65+
ret += '_';
66+
// half a SHA2 is enough
67+
ret += std::string_view(mangled).substr(0, mangled.size() / 2);
68+
return ret;
5569
}
5670
// PCM clang module
5771
if (module.isNonSwiftModule()) {

0 commit comments

Comments
 (0)