Swift: extract mangler into a separate class

This commit is contained in:
Alex Denisov
2023-02-28 14:56:48 +01:00
parent 734001b7c4
commit 97d5401118
7 changed files with 68 additions and 23 deletions

View File

@@ -12,6 +12,7 @@ swift_cc_binary(
"//swift/extractor/config",
"//swift/extractor/infra",
"//swift/extractor/invocation",
"//swift/extractor/mangler",
"//swift/extractor/remapping",
"//swift/extractor/translators",
"//swift/third_party/swift-llvm-support",

View File

@@ -0,0 +1,12 @@
load("//swift:rules.bzl", "swift_cc_library")
swift_cc_library(
name = "mangler",
srcs = glob(["*.cpp"]),
hdrs = glob(["*.h"]),
visibility = ["//swift:__subpackages__"],
deps = [
"//swift/extractor/trap",
"//swift/third_party/swift-llvm-support",
],
)

View File

@@ -0,0 +1,32 @@
#include "swift/extractor/mangler/SwiftMangler.h"
#include "swift/extractor/trap/generated/decl/TrapClasses.h"
#include <swift/AST/Module.h>
#include <sstream>
using namespace codeql;
std::string SwiftMangler::mangledName(const swift::Decl& decl) {
assert(llvm::isa<swift::ValueDecl>(decl));
auto& valueDecl = llvm::cast<swift::ValueDecl>(decl);
std::string_view moduleName = decl.getModuleContext()->getRealName().str();
// ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
if (decl.getKind() == swift::DeclKind::Module) {
return std::string{moduleName};
}
std::ostringstream ret;
// stamp all declarations with an id-ref of the containing module
ret << '{' << ModuleDeclTag::prefix << '_' << moduleName << '}';
if (decl.getKind() == swift::DeclKind::TypeAlias) {
// In cases like this (when coming from PCM)
// typealias CFXMLTree = CFTree
// typealias CFXMLTreeRef = CFXMLTree
// mangleAnyDecl mangles both CFXMLTree and CFXMLTreeRef into 'So12CFXMLTreeRefa'
// which is not correct and causes inconsistencies. mangleEntity makes these two distinct
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleEntity(&valueDecl);
} else {
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleAnyDecl(&valueDecl, /* prefix = */ false);
}
return ret.str();
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <vector>
#include <unordered_set>
#include <filesystem>
#include <swift/AST/ASTMangler.h>
namespace codeql {
class SwiftMangler {
public:
std::string mangledName(const swift::Decl& decl);
private:
swift::Mangle::ASTMangler mangler;
};
} // namespace codeql

View File

@@ -7,6 +7,7 @@ swift_cc_library(
visibility = ["//swift:__subpackages__"],
deps = [
"//swift/extractor/infra",
"//swift/extractor/mangler",
"//swift/third_party/swift-llvm-support",
],
)

View File

@@ -3,7 +3,7 @@
#include <swift/AST/GenericParamList.h>
#include <swift/AST/ParameterList.h>
#include "swift/extractor/infra/SwiftDiagnosticKind.h"
#include "swift/AST/PropertyWrappers.h"
#include <swift/AST/PropertyWrappers.h>
namespace codeql {
namespace {
@@ -303,27 +303,7 @@ std::optional<codeql::ModuleDecl> DeclTranslator::translateModuleDecl(
}
std::string DeclTranslator::mangledName(const swift::ValueDecl& decl) {
std::string_view moduleName = decl.getModuleContext()->getRealName().str();
// ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
if (decl.getKind() == swift::DeclKind::Module) {
return std::string{moduleName};
}
std::ostringstream ret;
// stamp all declarations with an id-ref of the containing module
ret << '{' << ModuleDeclTag::prefix << '_' << moduleName << '}';
if (decl.getKind() == swift::DeclKind::TypeAlias) {
// In cases like this (when coming from PCM)
// typealias CFXMLTree = CFTree
// typealias CFXMLTreeRef = CFXMLTree
// mangleAnyDecl mangles both CFXMLTree and CFXMLTreeRef into 'So12CFXMLTreeRefa'
// which is not correct and causes inconsistencies. mangleEntity makes these two distinct
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleEntity(&decl);
} else {
// prefix adds a couple of special symbols, we don't necessary need them
ret << mangler.mangleAnyDecl(&decl, /* prefix = */ false);
}
return ret.str();
return mangler.mangledName(decl);
}
void DeclTranslator::fillAbstractFunctionDecl(const swift::AbstractFunctionDecl& decl,

View File

@@ -5,6 +5,7 @@
#include "swift/extractor/translators/TranslatorBase.h"
#include "swift/extractor/trap/generated/decl/TrapClasses.h"
#include "swift/extractor/mangler/SwiftMangler.h"
namespace codeql {
@@ -86,7 +87,7 @@ class DeclTranslator : public AstTranslatorBase<DeclTranslator> {
entry.module = dispatcher.fetchLabel(decl.getModuleContext());
}
swift::Mangle::ASTMangler mangler;
SwiftMangler mangler;
};
} // namespace codeql