tools/perf/util/demangle-rust-v0.c

Source file repositories/reference/linux-study-clean/tools/perf/util/demangle-rust-v0.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/demangle-rust-v0.c
Extension
.c
Size
60917 bytes
Lines
2043
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct demangle_v0 {
    const char *mangled;
    size_t mangled_len;
};

struct demangle_legacy {
    const char *mangled;
    size_t mangled_len;
    size_t elements;
};

// private version of memrchr to avoid _GNU_SOURCE
static void *demangle_memrchr(const void *s, int c, size_t n) {
    const uint8_t *s_ = s;
    for (; n != 0; n--) {
        if (s_[n-1] == c) {
            return (void*)&s_[n-1];
        }
    }
    return NULL;
}


static bool unicode_iscontrol(uint32_t ch) {
    // this is *technically* a unicode table, but
    // some unicode properties are simpler than you might think
    return ch < 0x20 || (ch >= 0x7f && ch < 0xa0);
}

// "good enough" tables, the only consequence is that when printing
// *constant strings*, some characters are printed as `\u{abcd}` rather than themselves.
//
// I'm leaving these here to allow easily replacing them with actual
// tables if desired.
static bool unicode_isprint(uint32_t ch) {
    if (ch < 0x20) {
        return false;
    }
    if (ch < 0x7f) {
        return true;
    }
    return false;
}

static bool unicode_isgraphemextend(uint32_t ch) {
    (void)ch;
    return false;
}

static bool str_isascii(const char *s, size_t s_len) {
    for (size_t i = 0; i < s_len; i++) {
        if (s[i] & 0x80) {
            return false;
        }
    }

    return true;
}

typedef enum {
    PunycodeOk,
    PunycodeError
} punycode_status;

struct parser {
    // the parser assumes that `sym` has a safe "terminating byte". It might be NUL,
    // but it might also be something else if a symbol is "truncated".
    const char *sym;
    size_t sym_len;
    size_t next;
    uint32_t depth;
};

struct printer {
    demangle_status status; // if status == 0 parser is valid
    struct parser parser;
    char *out; // NULL for no output [in which case out_len is not decremented]
    size_t out_len;
    uint32_t bound_lifetime_depth;
    bool alternate;
};

static NODISCARD overflow_status printer_print_path(struct printer *printer, bool in_value);
static NODISCARD overflow_status printer_print_type(struct printer *printer);
static NODISCARD overflow_status printer_print_const(struct printer *printer, bool in_value);

static NODISCARD demangle_status try_parse_path(struct parser *parser) {
    struct printer printer = {
        DemangleOk,
        *parser,

Annotation

Implementation Notes