Closed sgraham closed 3 months ago
How hard could it be, I'll give it a try 🙂
(ps: I don't read GitHub notifications; ping me elsewhere for things you'd like me to see faster than "in a few months".)
As far as I can tell, swift has two demanglers. The runtime demangler you linked to, and the toolchain's libDemangling (https://github.com/swiftlang/swift/tree/main/lib/Demangling).
(It looks like new demangling tends to be implemented in the toolchain first, and then reimplemented in the runtime later, e.g.
(There's also lib/SwiftDemangle, but it's only a thin wrapper around lib/Demangling that doesn't really add anything: https://github.com/swiftlang/swift/tree/main/lib/SwiftDemangle)
lib/Demangling is fairly self-contained. This builds it (in a subdir of a swift
checkout):
#!/bin/bash
set -eu
clang -std=c++17 \
-c ../lib/Demangling/*.cpp \
-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1 \
-DSWIFT_SUPPORT_OLD_MANGLING=1 \
-DSWIFT_STDLIB_HAS_TYPE_PRINTING=1 \
-I ../include \
-I ~/src/llvm-project/llvm/include \
-I ~/src/llvm-project/out/gn//gen/llvm/include
clang++ -shared -o libDemangling.dylib *.o
It does depend on Compiler.h in llvm, which in turn depends on the generated llvm-config.h, which is a bit annoying -- but probably no major showstopper.
This copies out all the files that are needed (at https://github.com/swiftlang/swift/commit/a894dd277544b87f71fabf99cc615a2494cc14ea and https://github.com/llvm/llvm-project/commit/3a4c7cc56c07b2db9010c2228fc7cb2a43dd9b2d) and builds:
% pwd
/Users/thakis/src/swift/build-demangle
% time ./standalone-build.sh
./standalone-build.sh 2.65s user 0.36s system 100% cpu 3.004 total
% echo $?
0
thakis@Nicos-MacBook-Pro build-demangle % cat standalone-build.sh
#!/bin/bash
set -eu
LLVM_DIR=$HOME/src/llvm-project/llvm
LLVM_HEADER_GEN=$HOME/src/llvm-project/out/gn/gen
SWIFT_DIR=..
rm -rf llvm swift
mkdir -p llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/ADL.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/DenseMapInfo.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/Hashing.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLExtras.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLForwardCompat.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLFunctionalExtras.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/StringRef.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/StringSwitch.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/bit.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/iterator.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/iterator_range.h llvm/include/llvm/ADT
mkdir -p llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/Casting.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/Compiler.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/DataTypes.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/ErrorHandling.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/SwapByteOrder.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/type_traits.h llvm/include/llvm/Support
mkdir llvm/include/llvm-c
cp $LLVM_DIR/include/llvm-c/DataTypes.h llvm/include/llvm-c
mkdir -p swift/include/swift
cp -R $SWIFT_DIR/include/swift/Demangling swift/include/swift
cp $SWIFT_DIR/include/swift/Strings.h swift/include/swift
mkdir -p swift/include/swift/ABI
cp $SWIFT_DIR/include/swift/ABI/InvertibleProtocols.def swift/include/swift/ABI
mkdir -p swift/include/swift/AST
cp $SWIFT_DIR/include/swift/AST/Ownership.h swift/include/swift/AST
cp $SWIFT_DIR/include/swift/AST/ReferenceStorage.def swift/include/swift/AST
mkdir -p swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/Assertions.h swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/InlineBitfield.h swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/LLVM.h swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/MacroRoles.def swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/STLExtras.h swift/include/swift/Basic
mkdir -p swift/lib
cp -R $SWIFT_DIR/lib/Demangling swift/lib
mkdir -p llvm/include/llvm/Config
cp $LLVM_HEADER_GEN/llvm/include/llvm/Config/abi-breaking.h llvm/include/llvm/Config
cp $LLVM_HEADER_GEN/llvm/include/llvm/Config/llvm-config.h llvm/include/llvm/Config
clang -std=c++17 \
-c swift/lib/Demangling/*.cpp \
-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1 \
-DSWIFT_SUPPORT_OLD_MANGLING=1 \
-DSWIFT_STDLIB_HAS_TYPE_PRINTING=1 \
-I swift/include \
-I llvm/include
clang++ -shared -o libDemangling.dylib *.o
swiftc is a very mangle-y compiler, and I sometimes have mixed C++/Swift stacks. IWBN if I didn't have to multiplex swift-demangle and demumble.
Ref: https://github.com/apple/swift/blob/main/stdlib/public/runtime/Demangle.cpp (I think)