Print this page
Rich's feedback

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libsysdemangle/common/cxx.c
          +++ new/usr/src/lib/libdemangle/common/cxx.c
↓ open down ↓ 2 lines elided ↑ open up ↑
   3    3   * LICENSE.TXT contents is available as ../THIRDPARTYLICENSE
   4    4   *
   5    5   *                     The LLVM Compiler Infrastructure
   6    6   *
   7    7   * This file is dual licensed under the MIT and the University of Illinois Open
   8    8   * Source Licenses. See LICENSE.TXT for details.
   9    9   *
  10   10   */
  11   11  
  12   12  /*
  13      - * Copyright 2017 Jason King.
       13 + * Copyright 2018 Jason King.
  14   14   */
  15   15  #include <ctype.h>
  16   16  #include <errno.h>
  17   17  #include <locale.h>
       18 +#include <note.h>
  18   19  #include <string.h>
  19   20  #include <setjmp.h>
  20   21  #include <stdio.h>
  21   22  #include <stdlib.h>
  22   23  #include <sys/isa_defs.h>
  23   24  #include <sys/debug.h>
  24      -#include "sysdemangle.h"
  25      -#include "sysdemangle_int.h"
       25 +#include "demangle-sys.h"
       26 +#include "demangle_int.h"
  26   27  #include "cxx.h"
  27   28  
  28   29  #ifndef ARRAY_SIZE
  29   30  #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
  30   31  #endif
  31   32  
  32   33  #define CPP_QUAL_CONST          (1U)
  33   34  #define CPP_QUAL_VOLATILE       (2U)
  34   35  #define CPP_QUAL_RESTRICT       (4U)
  35   36  
↓ open down ↓ 8 lines elided ↑ open up ↑
  44   45          unsigned        cpp_depth;
  45   46          boolean_t       cpp_parsed_ctor_dtor_cv;
  46   47          boolean_t       cpp_tag_templates;
  47   48          boolean_t       cpp_fix_forward_references;
  48   49          boolean_t       cpp_try_to_parse_template_args;
  49   50          locale_t        cpp_loc;
  50   51  } cpp_db_t;
  51   52  
  52   53  #define CK(x)                                           \
  53   54          do {                                            \
  54      -                if (!(x))                               \
       55 +                if (!(x)) {                             \
  55   56                          longjmp(db->cpp_jmp, 1);        \
       57 +                }                                       \
       58 +        NOTE(CONSTCOND)                                 \
  56   59          } while (0)
  57   60  
  58   61  #define TOP_L(db) (&(name_top(&(db)->cpp_name)->strp_l))
  59   62  #define RLEN(f, l) ((size_t)((l) - (f)))
  60   63  #define NAMT(db, n) (nlen(db) - n)
  61   64  
  62   65  static inline boolean_t is_xdigit(int);
  63   66  
  64   67  static boolean_t nempty(cpp_db_t *);
  65   68  static size_t nlen(cpp_db_t *);
↓ open down ↓ 142 lines elided ↑ open up ↑
 208  211          if (nlen(&db) > 0) {
 209  212                  str_t *s = TOP_L(&db);
 210  213                  result = zalloc(ops, s->str_len + 1);
 211  214                  if (result == NULL)
 212  215                          goto done;
 213  216  
 214  217                  (void) memcpy(result, s->str_s, s->str_len);
 215  218          }
 216  219  
 217  220  done:
 218      -        if (getenv("DEMANGLE_DEBUG") != NULL)
      221 +        if (demangle_debug)
 219  222                  dump(&db, stdout);
 220  223  
 221  224          db_fini(&db);
 222  225          return (result);
 223  226  }
 224  227  
 225  228  static void
 226  229  demangle(const char *first, const char *last, cpp_db_t *db)
 227  230  {
 228  231          const char *t = NULL;
↓ open down ↓ 53 lines elided ↑ open up ↑
 282  285                  return (first);
 283  286  
 284  287          nadd_l(db, first, RLEN(first, last));
 285  288          nfmt(db, " ({0})", NULL);
 286  289  
 287  290          return (last);
 288  291  }
 289  292  
 290  293  /*
 291  294   * _block_invoke
 292      - * _block_invoke<digit>+        XXX: should it be <digit>* ?
      295 + * _block_invoke<digit>*
 293  296   * _block_invoke_<digit>+
 294  297   */
 295  298  static const char *
 296  299  parse_block_invoke(const char *first, const char *last, cpp_db_t *db)
 297  300  {
 298  301          VERIFY3P(first, <=, last);
 299  302  
 300  303          if (last - first < 13)
 301  304                  return (first);
 302  305  
↓ open down ↓ 68 lines elided ↑ open up ↑
 371  374                  goto fail;
 372  375  
 373  376          if (!db->cpp_parsed_ctor_dtor_cv && ends_with_template_args) {
 374  377                  t2 = parse_type(t, last, db);
 375  378                  if (t2 == t || nlen(db) < 2)
 376  379                          goto fail;
 377  380  
 378  381                  str_pair_t *sp = name_top(&db->cpp_name);
 379  382  
 380  383                  if (str_length(&sp->strp_r) == 0)
 381      -                        str_append(&sp->strp_l, " ", 1);
      384 +                        (void) str_append(&sp->strp_l, " ", 1);
 382  385  
 383  386                  nfmt(db, "{0:L}{1:L}", "{1:R}{0:R}");
 384  387                  t = t2;
 385  388          }
 386  389  
 387  390          if (t == last || nempty(db))
 388  391                  goto fail;
 389  392  
 390  393          size_t n = nlen(db);
 391  394  
 392  395          if (t[0] == 'v') {
 393  396                  t++;
 394  397          } else {
 395      -
 396      -                /*CONSTCOND*/
 397      -                while (1) {
      398 +                for (;;) {
 398  399                          t2 = parse_type(t, last, db);
 399  400                          if (t2 == t || t == last)
 400  401                                  break;
 401  402  
 402  403                          t = t2;
 403  404                  }
 404  405          }
 405  406  
 406  407          /*
 407  408           * a bit of a hack, but a template substitution can apparently be
↓ open down ↓ 1126 lines elided ↑ open up ↑
1534 1535  
1535 1536  /* wrap value in () when necessary */
1536 1537  static void
1537 1538  paren(str_pair_t *sp)
1538 1539  {
1539 1540          str_t *l = &sp->strp_l;
1540 1541          str_t *r = &sp->strp_r;
1541 1542  
1542 1543          if (str_length(r) > 1 &&
1543 1544              r->str_s[0] == ' ' && r->str_s[1] == '[') {
1544      -                str_append(l, " (", 2);
1545      -                str_insert(r, 0, ")", 1);
     1545 +                (void) str_append(l, " (", 2);
     1546 +                (void) str_insert(r, 0, ")", 1);
1546 1547          } else if (str_length(r) > 0 && r->str_s[0] == '(') {
1547      -                str_append(l, "(", 1);
1548      -                str_insert(r, 0, ")", 1);
     1548 +                (void) str_append(l, "(", 1);
     1549 +                (void) str_insert(r, 0, ")", 1);
1549 1550          }
1550 1551  }
1551 1552  
1552 1553  /* BEGIN CSTYLED */
1553 1554  /*
1554 1555   * <type> ::= <builtin-type>
1555 1556   *        ::= <function-type>
1556 1557   *        ::= <class-enum-type>
1557 1558   *        ::= <array-type>
1558 1559   *        ::= <pointer-to-member-type>
↓ open down ↓ 47 lines elided ↑ open up ↑
1606 1607                  if (t == first || NAMT(db, n) == 0)
1607 1608                          return (first);
1608 1609                  save_top(db, 1);
1609 1610                  return (t);
1610 1611  
1611 1612          case 'C':
1612 1613                  t = parse_type(first + 1, last, db);
1613 1614                  if (t == first + 1 || NAMT(db, n) == 0)
1614 1615                          return (first);
1615 1616  
1616      -                str_append(TOP_L(db), " complex", 8);
     1617 +                (void) str_append(TOP_L(db), " complex", 8);
1617 1618                  save_top(db, 1);
1618 1619                  return (t);
1619 1620  
1620 1621          case 'F':
1621 1622                  t = parse_function_type(first, last, db);
1622 1623                  if (t == first || NAMT(db, n) == 0)
1623 1624                          return (first);
1624 1625                  save_top(db, 1);
1625 1626                  return (t);
1626 1627  
1627 1628          case 'G':
1628 1629                  t = parse_type(first + 1, last, db);
1629 1630                  if (t == first + 1 || NAMT(db, n) == 0)
1630 1631                          return (first);
1631 1632  
1632      -                str_append(TOP_L(db), " imaginary", 10);
     1633 +                (void) str_append(TOP_L(db), " imaginary", 10);
1633 1634                  save_top(db, 1);
1634 1635                  return (t);
1635 1636  
1636 1637          case 'M':
1637 1638                  t = parse_pointer_to_member_type(first, last, db);
1638 1639                  if (t == first || NAMT(db, n) == 0)
1639 1640                          return (first);
1640 1641                  save_top(db, 1);
1641 1642                  return (t);
1642 1643  
1643 1644          case 'O':
1644 1645                  t = parse_type(first + 1, last, db);
1645 1646                  amt = NAMT(db, n);
1646 1647                  if (t == first + 1 || amt == 0)
1647 1648                          return (first);
1648 1649  
1649 1650                  sp = name_at(&db->cpp_name, amt - 1);
1650 1651                  for (size_t i = 0; i < amt; i++, sp++) {
1651 1652                          paren(sp);
1652 1653                          if (str_pair_len(sp) > 0)
1653      -                                str_append(&sp->strp_l, "&&", 2);
     1654 +                                (void) str_append(&sp->strp_l, "&&", 2);
1654 1655                  }
1655 1656  
1656 1657                  save_top(db, amt);
1657 1658                  return (t);
1658 1659  
1659 1660          case 'P':
1660 1661                  t = parse_type(first + 1, last, db);
1661 1662                  amt = NAMT(db, n);
1662 1663                  if (t == first + 1 || amt == 0)
1663 1664                          return (first);
↓ open down ↓ 1 lines elided ↑ open up ↑
1665 1666                  sp = name_at(&db->cpp_name, amt - 1);
1666 1667                  for (size_t i = 0; i < amt; i++, sp++) {
1667 1668                          str_t *l = &sp->strp_l;
1668 1669  
1669 1670                          if (str_pair_len(sp) == 0)
1670 1671                                  continue;
1671 1672  
1672 1673                          paren(sp);
1673 1674                          if (first[1] != 'U' ||
1674 1675                              strncmp(l->str_s, "objc_object<", 12) != 0) {
1675      -                                str_append(l, "*", 1);
     1676 +                                (void) str_append(l, "*", 1);
1676 1677                          } else {
1677      -                                str_erase(l, 0, 11);
1678      -                                str_insert(l, 0, "id", 2);
     1678 +                                (void) str_erase(l, 0, 11);
     1679 +                                (void) str_insert(l, 0, "id", 2);
1679 1680                          }
1680 1681                  }
1681 1682                  save_top(db, amt);
1682 1683                  return (t);
1683 1684  
1684 1685          case 'R':
1685 1686                  t = parse_type(first + 1, last, db);
1686 1687                  amt = NAMT(db, n);
1687 1688                  if (t == first + 1 || amt == 0)
1688 1689                          return (first);
1689 1690  
1690 1691                  sp = name_at(&db->cpp_name, amt - 1);
1691 1692                  for (size_t i = 0; i < amt; i++, sp++) {
1692 1693                          if (str_length(&sp->strp_l) == 0 &&
1693 1694                              str_length(&sp->strp_r) == 0)
1694 1695                                  continue;
1695 1696  
1696 1697                          paren(sp);
1697      -                        str_append(&sp->strp_l, "&", 1);
     1698 +                        (void) str_append(&sp->strp_l, "&", 1);
1698 1699                  }
1699 1700  
1700 1701                  save_top(db, amt);
1701 1702                  return (t);
1702 1703  
1703 1704          case 'T':
1704 1705                  t = parse_template_param(first, last, db);
1705 1706                  if (t == first)
1706 1707                          return (first);
1707 1708  
↓ open down ↓ 161 lines elided ↑ open up ↑
1869 1870          for (size_t i = 0; i < amt; i++, sp++) {
1870 1871                  str_t *s = NULL;
1871 1872  
1872 1873                  if (!is_func) {
1873 1874                          s = &sp->strp_l;
1874 1875  
1875 1876                          if (str_length(s) == 0)
1876 1877                                  continue;
1877 1878  
1878 1879                          if (cv & 1)
1879      -                                str_append(s, " const", 6);
     1880 +                                (void) str_append(s, " const", 6);
1880 1881                          if (cv & 2)
1881      -                                str_append(s, " volatile", 9);
     1882 +                                (void) str_append(s, " volatile", 9);
1882 1883                          if (cv & 4)
1883      -                                str_append(s, " restrict", 9);
     1884 +                                (void) str_append(s, " restrict", 9);
1884 1885  
1885 1886                          continue;
1886 1887                  }
1887 1888  
1888 1889                  s = &sp->strp_r;
1889 1890                  size_t pos = str_length(s);
1890 1891  
1891 1892                  if (pos > 0 && s->str_s[pos - 1] == '&') {
1892 1893                          pos--;
1893 1894                          if (s->str_s[pos - 1] == '&')
1894 1895                                  pos--;
1895 1896                  }
1896 1897  
1897 1898                  if (cv & 1) {
1898      -                        str_insert(s, pos, " const", 6);
     1899 +                        (void) str_insert(s, pos, " const", 6);
1899 1900                          pos += 6;
1900 1901                  }
1901 1902                  if (cv & 2) {
1902      -                        str_insert(s, pos, " volatile", 9);
     1903 +                        (void) str_insert(s, pos, " volatile", 9);
1903 1904                          pos += 9;
1904 1905                  }
1905 1906                  if (cv & 4) {
1906      -                        str_insert(s, pos, " restrict", 9);
     1907 +                        (void) str_insert(s, pos, " restrict", 9);
1907 1908                  }
1908 1909          }
1909 1910  
1910 1911          save_top(db, amt);
1911 1912          return (t1);
1912 1913  }
1913 1914  
1914 1915  /*
1915 1916   * at <type>            # alignof (a type)
1916 1917   * az <expression>      # alignof (a expression)
↓ open down ↓ 156 lines elided ↑ open up ↑
2073 2074  
2074 2075          if (t == first + 2 || NAMT(db, n) != 1)
2075 2076                  return (first);
2076 2077  
2077 2078          nfmt(db, "typeid ({0})", NULL);
2078 2079          return (t);
2079 2080  }
2080 2081  
2081 2082  /*
2082 2083   * tr                                                   # throw
2083      - * tw <expression>                                      # throw expression
     2084 + * tw <expression>                                      # throw expression
2084 2085   */
2085 2086  static const char *
2086 2087  parse_throw_expr(const char *first, const char *last, cpp_db_t *db)
2087 2088  {
2088 2089          VERIFY3P(first, <=, last);
2089 2090  
2090 2091          if (last - first < 3)
2091 2092                  return (first);
2092 2093  
2093 2094          VERIFY3U(first[0], ==, 't');
↓ open down ↓ 531 lines elided ↑ open up ↑
2625 2626                          return (first);
2626 2627                  }
2627 2628                  break;
2628 2629          case 'D':
2629 2630                  switch (first[1]) {
2630 2631                  case '0':
2631 2632                  case '1':
2632 2633                  case '2':
2633 2634                  case '5':
2634 2635                          basename(db);
2635      -                        str_insert(TOP_L(db), 0, "~", 1);
     2636 +                        (void) str_insert(TOP_L(db), 0, "~", 1);
2636 2637                          break;
2637 2638                  default:
2638 2639                          return (first);
2639 2640                  }
2640 2641                  break;
2641 2642          default:
2642 2643                  return (first);
2643 2644          }
2644 2645  
2645 2646          db->cpp_parsed_ctor_dtor_cv = B_TRUE;
↓ open down ↓ 136 lines elided ↑ open up ↑
2782 2783                  n = snprintf(num.str_s, fd->max_demangled_size, fd->spec,
2783 2784                      conv.ld.v);
2784 2785          }
2785 2786  
2786 2787          if (n >= fd->max_demangled_size || n <= 0) {
2787 2788                  str_fini(&num);
2788 2789                  return (first);
2789 2790          }
2790 2791  
2791 2792          num.str_len = n;
2792      -        name_add_str(&db->cpp_name, &num, NULL);
     2793 +        (void) name_add_str(&db->cpp_name, &num, NULL);
2793 2794  
2794 2795          return (t + 1);
2795 2796  }
2796 2797  
2797 2798  /*
2798 2799   * <expr-primary> ::= L <type> <value number> E # integer literal
2799 2800   *                ::= L <type> <value float> E  # floating literal
2800 2801   *                ::= L <string type> E         # string literal
2801 2802   *                ::= L <nullptr type> E        # nullptr literal (i.e., "LDnE")
2802 2803   *
↓ open down ↓ 593 lines elided ↑ open up ↑
3396 3397          t1 = parse_type(t + 1, last, db);
3397 3398          if (t1 == t + 1 || NAMT(db, n) != 2)
3398 3399                  return (first);
3399 3400  
3400 3401          /*
3401 3402           * if we have  " [xxx]" already, want new result to be
3402 3403           * " [yyy][xxx]"
3403 3404           */
3404 3405          str_t *r = &name_top(&db->cpp_name)->strp_r;
3405 3406          if (r->str_len > 1 && r->str_s[0] == ' ' && r->str_s[1] == '[')
3406      -                str_erase(r, 0, 1);
     3407 +                (void) str_erase(r, 0, 1);
3407 3408  
3408 3409          nfmt(db, "{0:L}", " [{1}]{0:R}");
3409 3410          return (t1);
3410 3411  }
3411 3412  
3412 3413  /* <pointer-to-member-type> ::= M <class type> <member type> */
3413 3414  static const char *
3414 3415  parse_pointer_to_member_type(const char *first, const char *last, cpp_db_t *db)
3415 3416  {
3416 3417          VERIFY3P(first, <=, last);
↓ open down ↓ 61 lines elided ↑ open up ↑
3478 3479          }
3479 3480          if (t == last)
3480 3481                  return (first);
3481 3482  
3482 3483          t2 = parse_base_unresolved_name(t, last, db);
3483 3484          if (t != t2) {
3484 3485                  if (global) {
3485 3486                          if (nempty(db))
3486 3487                                  return (first);
3487 3488  
3488      -                        str_insert(TOP_L(db), 0, "::", 2);
     3489 +                        (void) str_insert(TOP_L(db), 0, "::", 2);
3489 3490                  }
3490 3491                  return (t2);
3491 3492          }
3492 3493  
3493 3494          if (t[0] != 's' || t[1] != 'r' || last - t < 2)
3494 3495                  return (first);
3495 3496  
3496 3497          n = nlen(db);
3497 3498          if (t[2] == 'N') {
3498 3499                  t += 3;
↓ open down ↓ 714 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX