| // SPDX-License-Identifier: GPL-2.0 |
| #include "util/string2.h" |
| #include "demangle-ocaml.h" |
| static const char *caml_prefix = "caml"; |
| static const size_t caml_prefix_len = 4; |
| /* mangled OCaml symbols start with "caml" followed by an upper-case letter */ |
| ocaml_is_mangled(const char *sym) |
| return 0 == strncmp(sym, caml_prefix, caml_prefix_len) |
| && isupper(sym[caml_prefix_len]); |
| * sym: a symbol which may have been mangled by the OCaml compiler |
| * if the input doesn't look like a mangled OCaml symbol, NULL is returned |
| * otherwise, a newly allocated string containing the demangled symbol is returned |
| ocaml_demangle_sym(const char *sym) |
| if (!ocaml_is_mangled(sym)) { |
| /* the demangled symbol is always smaller than the mangled symbol */ |
| result = malloc(len + 1); |
| if (sym[i] == '_' && sym[i + 1] == '_') { |
| else if (sym[i] == '$' && isxdigit(sym[i + 1]) && isxdigit(sym[i + 2])) { |
| /* "$xx" is a hex-encoded character */ |
| result[j++] = (hex(sym[i + 1]) << 4) | hex(sym[i + 2]); |