Print this page
2nd round review feedback from rmustacc.

Split Close
Expand all
Collapse all
          --- old/usr/src/test/libc-tests/tests/symbols/symbols_test.c
          +++ new/usr/src/test/libc-tests/tests/symbols/symbols_test.c
↓ open down ↓ 66 lines elided ↑ open up ↑
  67   67  
  68   68  #define MAXENV  64      /* maximum number of environments (bitmask width) */
  69   69  #define MAXHDR  10      /* maximum # headers to require to access symbol */
  70   70  #define MAXARG  20      /* maximum # of arguments */
  71   71  
  72   72  #define WS      " \t"
  73   73  
  74   74  static int next_env = 0;
  75   75  
  76   76  struct compile_env {
  77      -        char            *name;
  78      -        char            *lang;
  79      -        char            *defs;
  80      -        int             index;
       77 +        char            *ce_name;
       78 +        char            *ce_lang;
       79 +        char            *ce_defs;
       80 +        int             ce_index;
  81   81  };
  82   82  
  83   83  static struct compile_env compile_env[MAXENV];
  84   84  
  85   85  struct env_group {
  86      -        char                    *name;
  87      -        uint64_t                mask;
  88      -        struct env_group        *next;
       86 +        char                    *eg_name;
       87 +        uint64_t                eg_mask;
       88 +        struct env_group        *eg_next;
  89   89  };
  90   90  
  91   91  typedef enum { SYM_TYPE, SYM_VALUE, SYM_FUNC } sym_type_t;
  92   92  
  93   93  struct sym_test {
  94      -        char                    *name;
  95      -        sym_type_t              type;
  96      -        char                    *hdrs[MAXHDR];
  97      -        char                    *rtype;
  98      -        char                    *atypes[MAXARG];
  99      -        uint64_t                test_mask;
 100      -        uint64_t                need_mask;
 101      -        char                    *prog;
 102      -        struct sym_test         *next;
       94 +        char                    *st_name;
       95 +        sym_type_t              st_type;
       96 +        char                    *st_hdrs[MAXHDR];
       97 +        char                    *st_rtype;
       98 +        char                    *st_atypes[MAXARG];
       99 +        uint64_t                st_test_mask;
      100 +        uint64_t                st_need_mask;
      101 +        char                    *st_prog;
      102 +        struct sym_test         *st_next;
 103  103  };
 104  104  
 105  105  struct env_group *env_groups = NULL;
 106  106  
 107  107  struct sym_test *sym_tests = NULL;
 108  108  struct sym_test **sym_insert = &sym_tests;
 109  109  
 110  110  static char *
 111  111  mystrdup(const char *s)
 112  112  {
↓ open down ↓ 27 lines elided ↑ open up ↑
 140  140          if (rv < 0) {
 141  141                  perror("vasprintf");
 142  142                  exit(1);
 143  143          }
 144  144  }
 145  145  
 146  146  static void
 147  147  append_sym_test(struct sym_test *st)
 148  148  {
 149  149          *sym_insert = st;
 150      -        sym_insert = &st->next;
      150 +        sym_insert = &st->st_next;
 151  151  }
 152  152  
 153  153  static int
 154  154  find_env_mask(const char *name, uint64_t *mask)
 155  155  {
 156      -        for (int i = 0; i < 64; i++) {
 157      -                if (compile_env[i].name != NULL &&
 158      -                    strcmp(compile_env[i].name, name) == 0) {
      156 +        for (int i = 0; i < MAXENV; i++) {
      157 +                if (compile_env[i].ce_name != NULL &&
      158 +                    strcmp(compile_env[i].ce_name, name) == 0) {
 159  159                          *mask |= (1ULL << i);
 160  160                          return (0);
 161  161                  }
 162  162          }
 163  163  
 164      -        for (struct env_group *eg = env_groups; eg != NULL; eg = eg->next) {
 165      -                if (strcmp(name, eg->name) == 0) {
 166      -                        *mask |= eg->mask;
      164 +        for (struct env_group *eg = env_groups; eg != NULL; eg = eg->eg_next) {
      165 +                if (strcmp(name, eg->eg_name) == 0) {
      166 +                        *mask |= eg->eg_mask;
 167  167                          return (0);
 168  168                  }
 169  169          }
 170  170          return (-1);
 171  171  }
 172  172  
 173  173  
 174  174  static int
 175  175  expand_env(char *list, uint64_t *mask, char **erritem)
 176  176  {
↓ open down ↓ 61 lines elided ↑ open up ↑
 238  238  
 239  239          if (next_env >= MAXENV) {
 240  240                  myasprintf(err, "too many environments");
 241  241                  return (-1);
 242  242          }
 243  243  
 244  244          name = fields[0];
 245  245          lang = fields[1];
 246  246          defs = fields[2];
 247  247  
 248      -        compile_env[next_env].name = mystrdup(name);
 249      -        compile_env[next_env].lang = mystrdup(lang);
 250      -        compile_env[next_env].defs = mystrdup(defs);
 251      -        compile_env[next_env].index = next_env;
      248 +        compile_env[next_env].ce_name = mystrdup(name);
      249 +        compile_env[next_env].ce_lang = mystrdup(lang);
      250 +        compile_env[next_env].ce_defs = mystrdup(defs);
      251 +        compile_env[next_env].ce_index = next_env;
 252  252          next_env++;
 253  253          return (0);
 254  254  }
 255  255  
 256  256  static int
 257  257  do_env_group(char **fields, int nfields, char **err)
 258  258  {
 259  259          char *name;
 260  260          char *list;
 261  261          struct env_group *eg;
↓ open down ↓ 8 lines elided ↑ open up ↑
 270  270          name = fields[0];
 271  271          list = fields[1];
 272  272          mask = 0;
 273  273  
 274  274          if (expand_env(list, &mask, &item) < 0) {
 275  275                  myasprintf(err, "reference to undefined env %s", item);
 276  276                  return (-1);
 277  277          }
 278  278  
 279  279          eg = myzalloc(sizeof (*eg));
 280      -        eg->name = mystrdup(name);
 281      -        eg->mask = mask;
 282      -        eg->next = env_groups;
      280 +        eg->eg_name = mystrdup(name);
      281 +        eg->eg_mask = mask;
      282 +        eg->eg_next = env_groups;
 283  283          env_groups = eg;
 284  284          return (0);
 285  285  }
 286  286  
 287  287  static char *progbuf = NULL;
 288  288  size_t proglen = 0;
 289  289  size_t progsiz = 0;
 290  290  
 291  291  static void 
 292  292  addprogch(char c)
↓ open down ↓ 34 lines elided ↑ open up ↑
 327  327          free(buf);
 328  328  }
 329  329  
 330  330  static void
 331  331  mkprog(struct sym_test *st)
 332  332  {
 333  333          char *s;
 334  334  
 335  335          proglen = 0;
 336  336  
 337      -        for (int i = 0; i < MAXHDR && st->hdrs[i] != NULL; i++) {
 338      -                addprogfmt("#include <%s>\n", st->hdrs[i]);
      337 +        for (int i = 0; i < MAXHDR && st->st_hdrs[i] != NULL; i++) {
      338 +                addprogfmt("#include <%s>\n", st->st_hdrs[i]);
 339  339          }
 340  340  
 341      -        for (s = st->rtype; *s; s++) {
      341 +        for (s = st->st_rtype; *s; s++) {
 342  342                  addprogch(*s);
 343  343                  if (*s == '(') {
 344  344                          s++;
 345  345                          addprogch(*s);
 346  346                          s++;
 347  347                          break;
 348  348                  }
 349  349          }
 350  350          addprogch(' ');
 351  351  
 352  352          /* for function pointers, s is closing suffix, otherwise empty */
 353  353  
 354      -        switch (st->type) {
      354 +        switch (st->st_type) {
 355  355          case SYM_TYPE:
 356  356                  addprogstr("test_type;");
 357  357                  break;
 358  358  
 359  359          case SYM_VALUE:
 360  360                  addprogfmt("test_value%s;\n", s);       /* s usually empty */
 361  361                  addprogstr("void\ntest_func(void)\n{\n");
 362      -                addprogfmt("\ttest_value = %s;\n}", st->name);
      362 +                addprogfmt("\ttest_value = %s;\n}", st->st_name);
 363  363                  break;
 364  364  
 365  365          case SYM_FUNC:
 366  366                  addprogstr("\ntest_func(");
 367      -                for (int i = 0; st->atypes[i] != NULL && i < MAXARG; i++) {
      367 +                for (int i = 0; st->st_atypes[i] != NULL && i < MAXARG; i++) {
 368  368                          int didname = 0;
 369  369                          if (i > 0) {
 370  370                                  addprogstr(", ");
 371  371                          }
 372      -                        if (strcmp(st->atypes[i], "void") == 0) {
      372 +                        if (strcmp(st->st_atypes[i], "void") == 0) {
 373  373                                  didname = 1;
 374  374                          }
 375      -                        if (strcmp(st->atypes[i], "") == 0) {
      375 +                        if (strcmp(st->st_atypes[i], "") == 0) {
 376  376                                  didname = 1;
 377  377                                  addprogstr("void");
 378  378                          }
 379  379  
 380  380                          /* print the argument list */
 381      -                        for (char *a = st->atypes[i]; *a; a++) {
      381 +                        for (char *a = st->st_atypes[i]; *a; a++) {
 382  382                                  if (*a == '(' && a[1] == '*' && !didname) {
 383  383                                          addprogfmt("(*a%d", i);
 384  384                                          didname = 1;
 385  385                                          a++;
 386  386                                  } else if (*a == '[' && !didname) {
 387  387                                          addprogfmt("a%d[", i);
 388  388                                          didname = 1;
 389  389                                  } else {
 390  390                                          addprogch(*a);
 391  391                                  }
 392  392                          }
 393  393                          if (!didname) {
 394  394                                  addprogfmt(" a%d", i);
 395  395                          }
 396  396                  }
 397  397  
 398      -                if (st->atypes[0] == NULL) {
      398 +                if (st->st_atypes[0] == NULL) {
 399  399                          addprogstr("void");
 400  400                  }
 401  401  
 402      -                /* close argument list, and closing ")" for func ptrs */
 403      -                addprogfmt(")%s\n{\n\t", s);    /* NB: s is normally empty */
      402 +                /*
      403 +                 * Close argument list, and closing ")" for func ptrs.
      404 +                 * Note that for non-function pointers, s will be empty
      405 +                 * below, otherwise it points to the trailing argument
      406 +                 * list.
      407 +                 */
      408 +                addprogfmt(")%s\n{\n\t", s);
 404  409  
 405      -                if (strcmp(st->rtype, "") != 0 &&
 406      -                    strcmp(st->rtype, "void") != 0) {
      410 +                if (strcmp(st->st_rtype, "") != 0 &&
      411 +                    strcmp(st->st_rtype, "void") != 0) {
 407  412                          addprogstr("return ");
 408  413                  }
 409  414  
 410  415                  /* add the function call */
 411      -                addprogfmt("%s(", st->name);
 412      -                for (int i = 0; st->atypes[i] != NULL && i < MAXARG; i++) {
 413      -                        if (strcmp(st->atypes[i], "") != 0 &&
 414      -                            strcmp(st->atypes[i], "void") != 0) {
      416 +                addprogfmt("%s(", st->st_name);
      417 +                for (int i = 0; st->st_atypes[i] != NULL && i < MAXARG; i++) {
      418 +                        if (strcmp(st->st_atypes[i], "") != 0 &&
      419 +                            strcmp(st->st_atypes[i], "void") != 0) {
 415  420                                  addprogfmt("%sa%d", i > 0 ? ", " : "", i);
 416  421                          }
 417  422                  }
 418  423  
 419  424                  addprogstr(");\n}");
 420  425                  break;
 421  426          }
 422  427  
 423  428          addprogch('\n');
 424  429  
 425      -        st->prog = progbuf;
      430 +        st->st_prog = progbuf;
 426  431  }
 427  432  
 428  433  static int
 429  434  add_envs(struct sym_test *st, char *envs, char **err)
 430  435  {
 431  436          char *item;
 432      -        if (expand_env_list(envs, &st->test_mask, &st->need_mask, &item) < 0) {
      437 +        if (expand_env_list(envs, &st->st_test_mask, &st->st_need_mask,
      438 +            &item) < 0) {
 433  439                  myasprintf(err, "bad env action %s", item);
 434  440                  return (-1);
 435  441          }
 436  442          return (0);
 437  443  }
 438  444  
 439  445  static int
 440  446  add_headers(struct sym_test *st, char *hdrs, char **err)
 441  447  {
 442  448          int i = 0;
 443  449  
 444  450          for (char *h = strsep(&hdrs, ";"); h != NULL; h = strsep(&hdrs, ";")) {
 445  451                  if (i >= MAXHDR) {
 446  452                          myasprintf(err, "too many headers");
 447  453                          return (-1);
 448  454                  }
 449  455                  test_trim(&h);
 450      -                st->hdrs[i++] = mystrdup(h);
      456 +                st->st_hdrs[i++] = mystrdup(h);
 451  457          }
 452  458  
 453  459          return (0);
 454  460  }
 455  461  
 456  462  static int
 457  463  add_arg_types(struct sym_test *st, char *atype, char **err)
 458  464  {
 459  465          int i = 0;
 460  466          char *a;
 461  467          for (a = strsep(&atype, ";"); a != NULL; a = strsep(&atype, ";")) {
 462  468                  if (i >= MAXARG) {
 463  469                          myasprintf(err, "too many arguments");
 464  470                          return (-1);
 465  471                  }
 466  472                  test_trim(&a);
 467      -                st->atypes[i++] = mystrdup(a);
      473 +                st->st_atypes[i++] = mystrdup(a);
 468  474          }
 469  475  
 470  476          return (0);
 471  477  }
 472  478  
 473  479  static int
 474  480  do_type(char **fields, int nfields, char **err)
 475  481  {
 476  482          char *decl;
 477  483          char *hdrs;
↓ open down ↓ 2 lines elided ↑ open up ↑
 480  486  
 481  487          if (nfields != 3) {
 482  488                  myasprintf(err, "number of fields (%d) != 3", nfields);
 483  489                  return (-1);
 484  490          }
 485  491          decl = fields[0];
 486  492          hdrs = fields[1];
 487  493          envs = fields[2];
 488  494  
 489  495          st = myzalloc(sizeof (*st));
 490      -        st->type = SYM_TYPE;
 491      -        st->name = mystrdup(decl);
 492      -        st->rtype = mystrdup(decl);
      496 +        st->st_type = SYM_TYPE;
      497 +        st->st_name = mystrdup(decl);
      498 +        st->st_rtype = mystrdup(decl);
 493  499  
 494  500          if ((add_envs(st, envs, err) < 0) ||
 495  501              (add_headers(st, hdrs, err) < 0)) {
 496  502                  return (-1);
 497  503          }
 498  504          append_sym_test(st);
 499  505  
 500  506          return (0);
 501  507  }
 502  508  
↓ open down ↓ 9 lines elided ↑ open up ↑
 512  518          if (nfields != 4) {
 513  519                  myasprintf(err, "number of fields (%d) != 4", nfields);
 514  520                  return (-1);
 515  521          }
 516  522          name = fields[0];
 517  523          type = fields[1];
 518  524          hdrs = fields[2];
 519  525          envs = fields[3];
 520  526  
 521  527          st = myzalloc(sizeof (*st));
 522      -        st->type = SYM_VALUE;
 523      -        st->name = mystrdup(name);
 524      -        st->rtype = mystrdup(type);
      528 +        st->st_type = SYM_VALUE;
      529 +        st->st_name = mystrdup(name);
      530 +        st->st_rtype = mystrdup(type);
 525  531  
 526  532          if ((add_envs(st, envs, err) < 0) ||
 527  533              (add_headers(st, hdrs, err) < 0)) {
 528  534                  return (-1);
 529  535          }
 530  536          append_sym_test(st);
 531  537  
 532  538          return (0);
 533  539  }
 534  540  
↓ open down ↓ 11 lines elided ↑ open up ↑
 546  552                  myasprintf(err, "number of fields (%d) != 5", nfields);
 547  553                  return (-1);
 548  554          }
 549  555          name = fields[0];
 550  556          rtype = fields[1];
 551  557          atype = fields[2];
 552  558          hdrs = fields[3];
 553  559          envs = fields[4];
 554  560  
 555  561          st = myzalloc(sizeof (*st));
 556      -        st->type = SYM_FUNC;
 557      -        st->name = mystrdup(name);
 558      -        st->rtype = mystrdup(rtype);
      562 +        st->st_type = SYM_FUNC;
      563 +        st->st_name = mystrdup(name);
      564 +        st->st_rtype = mystrdup(rtype);
 559  565  
 560  566          if ((add_envs(st, envs, err) < 0) ||
 561  567              (add_headers(st, hdrs, err) < 0) ||
 562  568              (add_arg_types(st, atype, err) < 0)) {
 563  569                  return (-1);
 564  570          }
 565  571          append_sym_test(st);
 566  572  
 567  573          return (0);
 568  574  }
 569  575  
 570  576  struct sym_test *
 571  577  next_sym_test(struct sym_test *st)
 572  578  {
 573      -        return (st == NULL ? sym_tests : st->next);
      579 +        return (st == NULL ? sym_tests : st->st_next);
 574  580  }
 575  581  
 576  582  const char *
 577  583  sym_test_prog(struct sym_test *st)
 578  584  {
 579      -        if (st->prog == NULL) {
      585 +        if (st->st_prog == NULL) {
 580  586                  mkprog(st);
 581  587          }
 582      -        return (st->prog);
      588 +        return (st->st_prog);
 583  589  }
 584  590  
 585  591  const char *
 586  592  sym_test_name(struct sym_test *st)
 587  593  {
 588      -        return (st->name);
      594 +        return (st->st_name);
 589  595  }
 590  596  
 591  597  /*
 592  598   * Iterate through tests.  Pass in NULL for cenv to begin the iteration. For
 593  599   * subsequent iterations, use the return value from the previous iteration.
 594  600   * Returns NULL when there are no more environments.
 595  601   */
 596  602  struct compile_env *
 597  603  sym_test_env(struct sym_test *st, struct compile_env *cenv, int *need)
 598  604  {
 599      -        int i = cenv ? cenv->index + 1: 0;
      605 +        int i = cenv ? cenv->ce_index + 1: 0;
 600  606          uint64_t b = 1ULL << i;
 601  607  
 602  608          while ((i < MAXENV) && (b != 0)) {
 603  609                  cenv = &compile_env[i];
 604      -                if (b & st->test_mask) {
 605      -                        *need = (st->need_mask & b) ? 1 : 0;
      610 +                if (b & st->st_test_mask) {
      611 +                        *need = (st->st_need_mask & b) ? 1 : 0;
 606  612                          return (cenv);
 607  613                  }
 608  614                  b <<= 1;
 609  615                  i++;
 610  616          }
 611  617          return (NULL);
 612  618  }
 613  619  
 614  620  const char *
 615  621  env_name(struct compile_env *cenv)
 616  622  {
 617      -        return (cenv->name);
      623 +        return (cenv->ce_name);
 618  624  }
 619  625  
 620  626  const char *
 621  627  env_lang(struct compile_env *cenv)
 622  628  {
 623      -        return (cenv->lang);
      629 +        return (cenv->ce_lang);
 624  630  }
 625  631  
 626  632  const char *
 627  633  env_defs(struct compile_env *cenv)
 628  634  {
 629      -        return (cenv->defs);
      635 +        return (cenv->ce_defs);
 630  636  }
 631  637  
 632  638  static void
 633  639  show_file(test_t t, const char *path)
 634  640  {
 635  641          FILE *f;
 636  642          char *buf = NULL;
 637  643          size_t cap = 0;
 638  644          int line = 1;
 639  645  
↓ open down ↓ 125 lines elided ↑ open up ↑
 765  771                  case 52:        /* GCC */
 766  772                          test_debugf(t, "Found GNU C");
 767  773                          c89flags = "-Wall -Werror -std=c89 " MFLAG;
 768  774                          c99flags = "-Wall -Werror -std=c99 " MFLAG;
 769  775                          if (extra_debug) {
 770  776                                  test_debugf(t, "c89flags: %s", c89flags);
 771  777                                  test_debugf(t, "c99flags: %s", c99flags);
 772  778                          }
 773  779                          test_passed(t);
 774  780                          break;
      781 +                case 99:
      782 +                        test_debugf(t, "Found unknown (unsupported) compiler");
      783 +                        continue;
 775  784                  default:
 776  785                          continue;
 777  786                  }
 778  787                  myasprintf(&compiler, "%s", compilers[i]);
 779  788                  test_debugf(t, "compiler: %s", compiler);
 780  789                  return;
 781  790          }
 782  791          test_failed(t, "No compiler found.");
 783  792  }
 784  793  
↓ open down ↓ 77 lines elided ↑ open up ↑
 862  871          int need;
 863  872  
 864  873          for (st = next_sym_test(NULL); st; st = next_sym_test(st)) {
 865  874                  if ((sym != NULL) && strcmp(sym, sym_test_name(st))) {
 866  875                          continue;
 867  876                  }
 868  877                  /* XXX: we really want a sym_test_desc() */
 869  878                  for (cenv = sym_test_env(st, NULL, &need);
 870  879                      cenv != NULL;
 871  880                      cenv = sym_test_env(st, cenv, &need)) {
 872      -                        t = test_start("%s : %c%s", st->name,
      881 +                        t = test_start("%s : %c%s", sym_test_name(st),
 873  882                              need ? '+' : '-', env_name(cenv));
 874  883                          if (do_compile(t, st, cenv, need) == 0) {
 875  884                                  test_passed(t);
 876  885                          }
 877  886                  }
 878  887          }
 879  888  
 880  889          if (full_count > 0) {
 881  890                  test_summary();
 882  891          }
↓ open down ↓ 66 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX