Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] ci test #38

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ if (WIN32)
endif()

include_directories(third_party/llvm/include)
include_directories(third_party/swift/include)
add_definitions(
-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1
-DSWIFT_SUPPORT_OLD_MANGLING=1
-DSWIFT_STDLIB_HAS_TYPE_PRINTING=1
)
add_executable(demumble
demumble.cc
third_party/llvm/lib/Demangle/Demangle.cpp
Expand All @@ -47,6 +53,17 @@ add_executable(demumble
third_party/llvm/lib/Demangle/MicrosoftDemangle.cpp
third_party/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
third_party/llvm/lib/Demangle/RustDemangle.cpp
third_party/swift/lib/Demangling/Context.cpp
third_party/swift/lib/Demangling/CrashReporter.cpp
third_party/swift/lib/Demangling/Demangler.cpp
third_party/swift/lib/Demangling/Errors.cpp
third_party/swift/lib/Demangling/ManglingUtils.cpp
third_party/swift/lib/Demangling/NodeDumper.cpp
third_party/swift/lib/Demangling/NodePrinter.cpp
third_party/swift/lib/Demangling/OldDemangler.cpp
third_party/swift/lib/Demangling/OldRemangler.cpp
third_party/swift/lib/Demangling/Punycode.cpp
third_party/swift/lib/Demangling/Remangler.cpp
)
set_target_properties(demumble PROPERTIES CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)
32 changes: 31 additions & 1 deletion demumble.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>

#include "llvm/Demangle/Demangle.h"
#include "swift/Demangling/Demangle.h"

const char kDemumbleVersion[] = "1.2.3.git";

Expand Down Expand Up @@ -33,6 +34,14 @@ static void print_demangled(const char* format, std::string_view s,
} else if (char* ms = llvm::microsoftDemangle(s, n_used, NULL)) {
printf(format, ms, (int)s.size(), s.data());
free(ms);
} else if (swift::Demangle::isSwiftSymbol(s)) {
swift::Demangle::DemangleOptions options;
options.SynthesizeSugarOnTypes = true;
std::string swift = swift::Demangle::demangleSymbolAsString(s, options);
if (swift == s)
printf("%.*s", (int)s.size(), s.data()); // Not a mangled name
else
printf(format, swift.c_str(), (int)s.size(), s.data());
} else {
printf("%.*s", (int)s.size(), s.data());
}
Expand All @@ -49,6 +58,12 @@ static bool is_mangle_char_rust(char c) {
(c >= '0' && c <= '9') || c == '_';
}

static bool is_mangle_char_swift(char c) {
// https://github.com/swiftlang/swift/blob/main/docs/ABI/Mangling.rst
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '_' || c == '$';
}

static bool is_mangle_char_win(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || strchr("?_@$", c);
Expand All @@ -68,6 +83,18 @@ static bool is_plausible_rust_prefix(char* s) {
return s[0] == '_' && s[1] == 'R';
}

static bool is_plausible_swift_prefix(char* s) {
// https://github.com/swiftlang/swift/blob/main/docs/ABI/Mangling.rst
// But also swift/test/Demangle/Inputs/manglings.txt, which has
// _Tt, _TF etc as prefix.

// FIXME: This is missing prefix `@__swiftmacro_`.
return
(s[0] == '$' && s[1] == 's') ||
(s[0] == '_' && s[1] == 'T') ||
(s[0] == '$' && s[1] == 'S');
}

static char buf[8192];
int main(int argc, char* argv[]) {
enum { kPrintAll, kPrintMatching } print_mode = kPrintAll;
Expand Down Expand Up @@ -119,7 +146,7 @@ int main(int argc, char* argv[]) {
char* end = cur + strlen(cur);

while (cur != end) {
size_t offset_to_possible_symbol = strcspn(cur, "_?");
size_t offset_to_possible_symbol = strcspn(cur, "_?$");
if (print_mode == kPrintAll)
printf("%.*s", static_cast<int>(offset_to_possible_symbol), cur);
else if (need_separator)
Expand All @@ -139,6 +166,9 @@ int main(int argc, char* argv[]) {
else if (is_plausible_rust_prefix(cur))
while (cur + n_sym != end && is_mangle_char_rust(cur[n_sym]))
++n_sym;
else if (is_plausible_swift_prefix(cur))
while (cur + n_sym != end && is_mangle_char_swift(cur[n_sym]))
++n_sym;
else {
if (print_mode == kPrintAll)
printf("_");
Expand Down
4 changes: 4 additions & 0 deletions demumble_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
'std::mem::align_of::<f64>\nmylib::foo::bar\n'),
('demumble < _RINvNtC3std3mem8align_ofdE _RNvNvC5mylib3foo3bar',
'std::mem::align_of::<f64>\nmylib::foo::bar\n'),
('demumble _TtP3foo3bar_', 'foo.bar\n'),
('demumble < _TtP3foo3bar_', 'foo.bar\n'),
('demumble $sSS5countSivg', 'Swift.String.count.getter : Swift.Int\n'),
('demumble < $sSS5countSivg', 'Swift.String.count.getter : Swift.Int\n'),
('demumble ?Fxi@@YAHP6AHH@Z@Z', 'int __cdecl Fxi(int (__cdecl *)(int))\n'),
('demumble ??0S@@QEAA@$$QEAU0@@Z', 'public: __cdecl S::S(struct S &&)\n'),
('demumble ??_C@_02PCEFGMJL@hi?$AA@', '"hi"\n'),
Expand Down
84 changes: 84 additions & 0 deletions scripts/copy-swift-demangle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Swift's demangler is much less hermetic than LLVM's. This copies all the
# code needed by Swift's demangler into this repository.

set -eu

# Adopt these three to match your local swift checkout and your local
# llvm checkout and build dir:

LLVM_SRC=$HOME/src/llvm-project/llvm
SWIFT_SRC=$HOME/src/swift
LLVM_HEADER_GEN_SRC=$HOME/src/llvm-project/out/gn/gen/llvm/include/llvm

# The rest only needs updating if the set of needed files changes:

cd "$(dirname "$0")"/..

LLVM_INC_SRC=$LLVM_SRC/include/llvm
LLVM_DST=third_party/llvm
LLVM_INC_DST=$LLVM_DST/include/llvm

SWIFT_INC_SRC=$SWIFT_SRC/include/swift
SWIFT_DST=third_party/swift
SWIFT_INC_DST=$SWIFT_DST/include/swift

rm -rf $LLVM_INC_DST/ADT
rm -rf $LLVM_INC_DST/Config
rm -rf $LLVM_INC_DST/Support
rm -rf $LLVM_DST/include/llvm-c
rm -rf $SWIFT_DST

mkdir -p $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/ADL.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/DenseMapInfo.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/Hashing.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/STLExtras.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/STLForwardCompat.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/STLFunctionalExtras.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/StringRef.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/StringSwitch.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/bit.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/iterator.h $LLVM_INC_DST/ADT
cp "$LLVM_INC_SRC"/ADT/iterator_range.h $LLVM_INC_DST/ADT

mkdir -p $LLVM_INC_DST/Config
cp "$LLVM_HEADER_GEN_SRC"/Config/abi-breaking.h $LLVM_INC_DST/Config
cp "$LLVM_HEADER_GEN_SRC"/Config/llvm-config.h $LLVM_INC_DST/Config

mkdir -p $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/Casting.h $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/Compiler.h $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/DataTypes.h $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/ErrorHandling.h $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/SwapByteOrder.h $LLVM_INC_DST/Support
cp "$LLVM_INC_SRC"/Support/type_traits.h $LLVM_INC_DST/Support

mkdir -p $LLVM_DST/include/llvm-c
cp "$LLVM_SRC"/include/llvm-c/DataTypes.h $LLVM_DST/include/llvm-c

mkdir -p $SWIFT_DST
cp "$SWIFT_SRC"/LICENSE.txt $SWIFT_DST

mkdir -p $SWIFT_INC_DST
cp -R "$SWIFT_INC_SRC"/Demangling $SWIFT_INC_DST
cp "$SWIFT_INC_SRC"/Strings.h $SWIFT_INC_DST

mkdir -p $SWIFT_INC_DST/ABI
cp "$SWIFT_INC_SRC"/ABI/InvertibleProtocols.def $SWIFT_INC_DST/ABI

mkdir -p $SWIFT_INC_DST/AST
cp "$SWIFT_INC_SRC"/AST/Ownership.h $SWIFT_INC_DST/AST
cp "$SWIFT_INC_SRC"/AST/ReferenceStorage.def $SWIFT_INC_DST/AST

mkdir -p $SWIFT_INC_DST/Basic
cp "$SWIFT_INC_SRC"/Basic/Assertions.h $SWIFT_INC_DST/Basic
cp "$SWIFT_INC_SRC"/Basic/InlineBitfield.h $SWIFT_INC_DST/Basic
cp "$SWIFT_INC_SRC"/Basic/LLVM.h $SWIFT_INC_DST/Basic
cp "$SWIFT_INC_SRC"/Basic/MacroRoles.def $SWIFT_INC_DST/Basic
cp "$SWIFT_INC_SRC"/Basic/STLExtras.h $SWIFT_INC_DST/Basic

mkdir -p $SWIFT_DST/lib
cp -R "$SWIFT_SRC"/lib/Demangling $SWIFT_DST/lib
rm $SWIFT_DST/lib/Demangling/CMakeLists.txt
80 changes: 80 additions & 0 deletions third_party/llvm/include/llvm-c/DataTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*===-- include/llvm-c/DataTypes.h - Define fixed size types ------*- C -*-===*\
|* *|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|* Exceptions. *|
|* See https://llvm.org/LICENSE.txt for license information. *|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file contains definitions to figure out the size of _HOST_ data types.*|
|* This file is important because different host OS's define different macros,*|
|* which makes portability tough. This file exports the following *|
|* definitions: *|
|* *|
|* [u]int(32|64)_t : typedefs for signed and unsigned 32/64 bit system types*|
|* [U]INT(8|16|32|64)_(MIN|MAX) : Constants for the min and max values. *|
|* *|
|* No library is required when using these functions. *|
|* *|
|*===----------------------------------------------------------------------===*/

/* Please leave this file C-compatible. */

#ifndef LLVM_C_DATATYPES_H
#define LLVM_C_DATATYPES_H

#include <inttypes.h>
#include <stdint.h>

#ifndef _MSC_VER

#if !defined(UINT32_MAX)
# error "The standard header <cstdint> is not C++11 compliant. Must #define "\
"__STDC_LIMIT_MACROS before #including llvm-c/DataTypes.h"
#endif

#if !defined(UINT32_C)
# error "The standard header <cstdint> is not C++11 compliant. Must #define "\
"__STDC_CONSTANT_MACROS before #including llvm-c/DataTypes.h"
#endif

/* Note that <inttypes.h> includes <stdint.h>, if this is a C99 system. */
#include <sys/types.h>

#ifdef _AIX
// GCC is strict about defining large constants: they must have LL modifier.
#undef INT64_MAX
#undef INT64_MIN
#endif

#else /* _MSC_VER */
#ifdef __cplusplus
#include <cstddef>
#include <cstdlib>
#else
#include <stddef.h>
#include <stdlib.h>
#endif
#include <sys/types.h>

#if defined(_WIN64)
typedef signed __int64 ssize_t;
#else
typedef signed int ssize_t;
#endif /* _WIN64 */

#endif /* _MSC_VER */

/* Set defaults for constants which we cannot find. */
#if !defined(INT64_MAX)
# define INT64_MAX 9223372036854775807LL
#endif
#if !defined(INT64_MIN)
# define INT64_MIN ((-INT64_MAX)-1)
#endif
#if !defined(UINT64_MAX)
# define UINT64_MAX 0xffffffffffffffffULL
#endif

#endif /* LLVM_C_DATATYPES_H */
Loading
Loading