|
| 1 | +#ifndef _H_DEMANGLE_V0_H |
| 2 | +#define _H_DEMANGLE_V0_H |
| 3 | + |
| 4 | +#ifdef __cplusplus |
| 5 | +extern "C" { |
| 6 | +#endif |
| 7 | + |
| 8 | +#include <stddef.h> |
| 9 | + |
| 10 | +#if defined(__GNUC__) || defined(__clang__) |
| 11 | +#define DEMANGLE_NODISCARD __attribute__((warn_unused_result)) |
| 12 | +#else |
| 13 | +#define DEMANGLE_NODISCARD |
| 14 | +#endif |
| 15 | + |
| 16 | +typedef enum { |
| 17 | + OverflowOk, |
| 18 | + OverflowOverflow |
| 19 | +} overflow_status; |
| 20 | + |
| 21 | +enum demangle_style { |
| 22 | + DemangleStyleUnknown = 0, |
| 23 | + DemangleStyleLegacy, |
| 24 | + DemangleStyleV0, |
| 25 | +}; |
| 26 | + |
| 27 | +// Not using a union here to make the struct easier to copy-paste if needed. |
| 28 | +struct demangle { |
| 29 | + enum demangle_style style; |
| 30 | + // points to the "mangled" part of the name, |
| 31 | + // not including `ZN` or `R` prefixes. |
| 32 | + const char *mangled; |
| 33 | + size_t mangled_len; |
| 34 | + // In DemangleStyleLegacy, is the number of path elements |
| 35 | + size_t elements; |
| 36 | + // while it's called "original", it will not contain `.llvm.9D1C9369@@16` suffixes |
| 37 | + // that are to be ignored. |
| 38 | + const char *original; |
| 39 | + size_t original_len; |
| 40 | + // Contains the part after the mangled name that is to be outputted, |
| 41 | + // which can be `.exit.i.i` suffixes LLVM sometimes adds. |
| 42 | + const char *suffix; |
| 43 | + size_t suffix_len; |
| 44 | +}; |
| 45 | + |
| 46 | +// if the length of the output buffer is less than `output_len-OVERFLOW_MARGIN`, |
| 47 | +// the demangler will return `OverflowOverflow` even if there is no overflow. |
| 48 | +#define OVERFLOW_MARGIN 4 |
| 49 | + |
| 50 | +/// Demangle a C string that refers to a Rust symbol and put the demangle intermediate result in `res`. |
| 51 | +/// Beware that `res` contains references into `s`. If `s` is modified (or free'd) before calling |
| 52 | +/// `rust_demangle_display_demangle` behavior is undefined. |
| 53 | +/// |
| 54 | +/// Use `rust_demangle_display_demangle` to convert it to an actual string. |
| 55 | +void rust_demangle_demangle(const char *s, struct demangle *res); |
| 56 | + |
| 57 | +/// Write the string in a `struct demangle` into a buffer. |
| 58 | +/// |
| 59 | +/// Return `OverflowOk` if the output buffer was sufficiently big, `OverflowOverflow` if it wasn't. |
| 60 | +/// This function is `O(n)` in the length of the input + *output* [$], but the demangled output of demangling a symbol can |
| 61 | +/// be exponentially[$$] large, therefore it is recommended to have a sane bound (`rust-demangle` |
| 62 | +/// uses 1,000,000 bytes) on `len`. |
| 63 | +/// |
| 64 | +/// `alternate`, if true, uses the less verbose alternate formatting (Rust `{:#}`) is used, which does not show |
| 65 | +/// symbol hashes and types of constant ints. |
| 66 | +/// |
| 67 | +/// [$] It's `O(n * MAX_DEPTH)`, but `MAX_DEPTH` is a constant 300 and therefore it's `O(n)` |
| 68 | +/// [$$] Technically, bounded by `O(n^MAX_DEPTH)`, but this is practically exponential. |
| 69 | +DEMANGLE_NODISCARD overflow_status rust_demangle_display_demangle(struct demangle const *res, char *out, size_t len, bool alternate); |
| 70 | + |
| 71 | +/// Returns true if `res` refers to a known valid Rust demangling style, false if it's an unknown style. |
| 72 | +bool rust_demangle_is_known(struct demangle *res); |
| 73 | + |
| 74 | +#undef DEMANGLE_NODISCARD |
| 75 | + |
| 76 | +#ifdef __cplusplus |
| 77 | +} |
| 78 | +#endif |
| 79 | + |
| 80 | +#endif |
0 commit comments