Print this page
11506 smatch resync

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/smatch_type.c
          +++ new/usr/src/tools/smatch/src/smatch_type.c
↓ open down ↓ 21 lines elided ↑ open up ↑
  22   22  
  23   23  #include "smatch.h"
  24   24  #include "smatch_slist.h"
  25   25  
  26   26  struct symbol *get_real_base_type(struct symbol *sym)
  27   27  {
  28   28          struct symbol *ret;
  29   29  
  30   30          if (!sym)
  31   31                  return NULL;
       32 +        if (sym->type == SYM_BASETYPE)
       33 +                return sym;
  32   34          ret = get_base_type(sym);
  33   35          if (!ret)
  34   36                  return NULL;
  35   37          if (ret->type == SYM_RESTRICT || ret->type == SYM_NODE)
  36   38                  return get_real_base_type(ret);
  37   39          return ret;
  38   40  }
  39   41  
  40   42  int type_bytes(struct symbol *type)
  41   43  {
↓ open down ↓ 26 lines elided ↑ open up ↑
  68   70          if (expr->op == SPECIAL_LEFTSHIFT ||
  69   71              expr->op == SPECIAL_RIGHTSHIFT) {
  70   72                  if (type_positive_bits(left) < 31)
  71   73                          return &int_ctype;
  72   74                  return left;
  73   75          }
  74   76          right = get_type(expr->right);
  75   77          if (!right)
  76   78                  return NULL;
  77   79  
       80 +        if (expr->op == '-' &&
       81 +            (is_ptr_type(left) && is_ptr_type(right)))
       82 +                return ssize_t_ctype;
       83 +
  78   84          if (left->type == SYM_PTR || left->type == SYM_ARRAY)
  79   85                  return left;
  80   86          if (right->type == SYM_PTR || right->type == SYM_ARRAY)
  81   87                  return right;
  82   88  
  83   89          if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
  84   90                  return &int_ctype;
  85   91  
  86   92          if (type_positive_bits(left) > type_positive_bits(right))
  87   93                  return left;
↓ open down ↓ 174 lines elided ↑ open up ↑
 262  268          case EXPR_CONDITIONAL:
 263  269          case EXPR_SELECT:
 264  270                  ret = get_select_type(expr);
 265  271                  break;
 266  272          case EXPR_SIZEOF:
 267  273                  ret = &ulong_ctype;
 268  274                  break;
 269  275          case EXPR_LOGICAL:
 270  276                  ret = &int_ctype;
 271  277                  break;
      278 +        case EXPR_OFFSETOF:
      279 +                ret = &ulong_ctype;
      280 +                break;
 272  281          default:
 273  282                  return NULL;
 274  283          }
 275  284  
 276  285          if (ret && ret->type == SYM_TYPEOF)
 277  286                  ret = get_type(ret->initializer);
 278  287  
 279  288          expr->ctype = ret;
 280  289          return ret;
 281  290  }
 282  291  
 283  292  static struct symbol *get_final_type_helper(struct expression *expr)
 284  293  {
 285  294          /*
 286      -         * I'm not totally positive I understand types...
      295 +         * The problem is that I wrote a bunch of Smatch to think that
      296 +         * you could do get_type() on an expression and it would give
      297 +         * you what the comparison was type promoted to.  This is wrong
      298 +         * but fixing it is a big of work...  Hence this horrible hack.
 287  299           *
 288      -         * So, when you're doing pointer math, and you do a subtraction, then
 289      -         * the sval_binop() and whatever need to know the type of the pointer
 290      -         * so they can figure out the alignment.  But the result is going to be
 291      -         * and ssize_t.  So get_operation_type() gives you the pointer type
 292      -         * and get_type() gives you ssize_t.
 293      -         *
 294      -         * Most of the time the operation type and the final type are the same
 295      -         * but this just handles the few places where they are different.
 296      -         *
 297  300           */
 298  301  
 299  302          expr = strip_parens(expr);
 300  303          if (!expr)
 301  304                  return NULL;
 302  305  
 303      -        switch (expr->type) {
 304      -        case EXPR_COMPARE:
      306 +        if (expr->type == EXPR_COMPARE)
 305  307                  return &int_ctype;
 306      -        case EXPR_BINOP: {
 307      -                struct symbol *left, *right;
 308  308  
 309      -                if (expr->op != '-')
 310      -                        return NULL;
 311      -
 312      -                left = get_type(expr->left);
 313      -                right = get_type(expr->right);
 314      -                if (type_is_ptr(left) || type_is_ptr(right))
 315      -                        return ssize_t_ctype;
 316      -                }
 317      -        }
 318      -
 319  309          return NULL;
 320  310  }
 321  311  
 322  312  struct symbol *get_type(struct expression *expr)
 323  313  {
 324  314          return get_type_helper(expr);
 325  315  }
 326  316  
 327  317  struct symbol *get_final_type(struct expression *expr)
 328  318  {
↓ open down ↓ 61 lines elided ↑ open up ↑
 390  380                  return 0;
 391  381          sym = get_base_type(sym);
 392  382          if (!sym || sym->type != SYM_FN)
 393  383                  return 0;
 394  384          sym = get_base_type(sym);
 395  385          return type_unsigned(sym);
 396  386  }
 397  387  
 398  388  int is_pointer(struct expression *expr)
 399  389  {
 400      -        struct symbol *sym;
 401      -
 402      -        sym = get_type(expr);
 403      -        if (!sym)
 404      -                return 0;
 405      -        if (sym == &string_ctype)
 406      -                return 0;
 407      -        if (sym->type == SYM_PTR)
 408      -                return 1;
 409      -        return 0;
      390 +        return type_is_ptr(get_type(expr));
 410  391  }
 411  392  
 412  393  int returns_pointer(struct symbol *sym)
 413  394  {
 414  395          if (!sym)
 415  396                  return 0;
 416  397          sym = get_base_type(sym);
 417  398          if (!sym || sym->type != SYM_FN)
 418  399                  return 0;
 419  400          sym = get_base_type(sym);
↓ open down ↓ 15 lines elided ↑ open up ↑
 435  416  }
 436  417  
 437  418  sval_t sval_type_min(struct symbol *base_type)
 438  419  {
 439  420          sval_t ret;
 440  421  
 441  422          if (!base_type || !type_bits(base_type))
 442  423                  base_type = &llong_ctype;
 443  424          ret.type = base_type;
 444  425  
 445      -        if (type_unsigned(base_type)) {
      426 +        if (type_unsigned(base_type) || is_ptr_type(base_type)) {
 446  427                  ret.value = 0;
 447  428                  return ret;
 448  429          }
 449  430  
 450  431          ret.value = (~0ULL) << type_positive_bits(base_type);
 451  432  
 452  433          return ret;
 453  434  }
 454  435  
 455  436  int nr_bits(struct expression *expr)
↓ open down ↓ 141 lines elided ↑ open up ↑
 597  578          return NULL;
 598  579  }
 599  580  
 600  581  static struct symbol *get_member_from_string(struct symbol_list *symbol_list, const char *name)
 601  582  {
 602  583          struct symbol *tmp, *sub;
 603  584          int chunk_len;
 604  585  
 605  586          if (strncmp(name, ".", 1) == 0)
 606  587                  name += 1;
 607      -        if (strncmp(name, "->", 2) == 0)
      588 +        else if (strncmp(name, "->", 2) == 0)
 608  589                  name += 2;
 609  590  
 610  591          FOR_EACH_PTR(symbol_list, tmp) {
 611  592                  if (!tmp->ident) {
 612  593                          sub = get_real_base_type(tmp);
 613  594                          sub = get_member_from_string(sub->symbol_list, name);
 614  595                          if (sub)
 615  596                                  return sub;
 616  597                          continue;
 617  598                  }
 618  599  
 619  600                  if (strcmp(tmp->ident->name, name) == 0)
 620  601                          return tmp;
 621  602  
 622      -                chunk_len = strlen(tmp->ident->name);
      603 +                chunk_len = tmp->ident->len;
 623  604                  if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
 624  605                      (name[chunk_len] == '.' || name[chunk_len] == '-')) {
 625  606                          sub = get_real_base_type(tmp);
      607 +                        if (sub->type == SYM_PTR)
      608 +                                sub = get_real_base_type(sub);
 626  609                          return get_member_from_string(sub->symbol_list, name + chunk_len);
 627  610                  }
 628  611  
 629  612          } END_FOR_EACH_PTR(tmp);
 630  613  
 631  614          return NULL;
 632  615  }
 633  616  
 634  617  struct symbol *get_member_type_from_key(struct expression *expr, const char *key)
 635  618  {
↓ open down ↓ 98 lines elided ↑ open up ↑
 734  717  }
 735  718  
 736  719  static int type_str_helper(char *buf, int size, struct symbol *type)
 737  720  {
 738  721          int n;
 739  722  
 740  723          if (!type)
 741  724                  return snprintf(buf, size, "<unknown>");
 742  725  
 743  726          if (type->type == SYM_BASETYPE) {
 744      -                return snprintf(buf, size, base_type_str(type));
      727 +                return snprintf(buf, size, "%s", base_type_str(type));
 745  728          } else if (type->type == SYM_PTR) {
 746  729                  type = get_real_base_type(type);
 747  730                  n = type_str_helper(buf, size, type);
 748  731                  if (n > size)
 749  732                          return n;
 750  733                  return n + snprintf(buf + n, size - n, "*");
 751  734          } else if (type->type == SYM_ARRAY) {
 752  735                  type = get_real_base_type(type);
 753  736                  n = type_str_helper(buf, size, type);
 754  737                  if (n > size)
↓ open down ↓ 33 lines elided ↑ open up ↑
 788  771                  return n + snprintf(buf + n, size - n, ")");
 789  772          } else if (type->type == SYM_NODE) {
 790  773                  n = snprintf(buf, size, "node {");
 791  774                  if (n > size)
 792  775                          return n;
 793  776                  type = get_real_base_type(type);
 794  777                  n += type_str_helper(buf + n, size - n, type);
 795  778                  if (n > size)
 796  779                          return n;
 797  780                  return n + snprintf(buf + n, size - n, "}");
      781 +        } else if (type->type == SYM_ENUM) {
      782 +                return snprintf(buf, size, "enum %s", type->ident ? type->ident->name : "<unknown>");
 798  783          } else {
 799  784                  return snprintf(buf, size, "<type %d>", type->type);
 800  785          }
 801  786  }
 802  787  
 803  788  char *type_to_str(struct symbol *type)
 804  789  {
 805  790          static char buf[256];
 806  791  
 807  792          buf[0] = '\0';
 808  793          type_str_helper(buf, sizeof(buf), type);
 809  794          return buf;
 810  795  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX