~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/demangle-ocaml.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include "util/string2.h"
  5 
  6 #include "demangle-ocaml.h"
  7 
  8 #include <linux/ctype.h>
  9 
 10 static const char *caml_prefix = "caml";
 11 static const size_t caml_prefix_len = 4;
 12 
 13 /* mangled OCaml symbols start with "caml" followed by an upper-case letter */
 14 static bool
 15 ocaml_is_mangled(const char *sym)
 16 {
 17         return 0 == strncmp(sym, caml_prefix, caml_prefix_len)
 18                 && isupper(sym[caml_prefix_len]);
 19 }
 20 
 21 /*
 22  * input:
 23  *     sym: a symbol which may have been mangled by the OCaml compiler
 24  * return:
 25  *     if the input doesn't look like a mangled OCaml symbol, NULL is returned
 26  *     otherwise, a newly allocated string containing the demangled symbol is returned
 27  */
 28 char *
 29 ocaml_demangle_sym(const char *sym)
 30 {
 31         char *result;
 32         int j = 0;
 33         int i;
 34         int len;
 35 
 36         if (!ocaml_is_mangled(sym)) {
 37                 return NULL;
 38         }
 39 
 40         len = strlen(sym);
 41 
 42         /* the demangled symbol is always smaller than the mangled symbol */
 43         result = malloc(len + 1);
 44         if (!result)
 45                 return NULL;
 46 
 47         /* skip "caml" prefix */
 48         i = caml_prefix_len;
 49 
 50         while (i < len) {
 51                 if (sym[i] == '_' && sym[i + 1] == '_') {
 52                         /* "__" -> "." */
 53                         result[j++] = '.';
 54                         i += 2;
 55                 }
 56                 else if (sym[i] == '$' && isxdigit(sym[i + 1]) && isxdigit(sym[i + 2])) {
 57                         /* "$xx" is a hex-encoded character */
 58                         result[j++] = (hex(sym[i + 1]) << 4) | hex(sym[i + 2]);
 59                         i += 3;
 60                 }
 61                 else {
 62                         result[j++] = sym[i++];
 63                 }
 64         }
 65         result[j] = '\0';
 66 
 67         return result;
 68 }
 69 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php