Print this page
5506 cstyle: warn about #if enumerating ISA defines

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/scripts/cstyle.pl
          +++ new/usr/src/tools/scripts/cstyle.pl
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15  # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16   16  # If applicable, add the following below this CDDL HEADER, with the
  17   17  # fields enclosed by brackets "[]" replaced with your own identifying
  18   18  # information: Portions Copyright [yyyy] [name of copyright owner]
  19   19  #
  20   20  # CDDL HEADER END
  21   21  #
  22   22  #
  23   23  # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24   24  # Use is subject to license terms.
  25      -#
  26      -# @(#)cstyle 1.58 98/09/09 (from shannon)
  27      -#ident  "%Z%%M% %I%     %E% SMI"
       25 +# Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  28   26  #
  29   27  # cstyle - check for some common stylistic errors.
  30   28  #
  31   29  #       cstyle is a sort of "lint" for C coding style.
  32   30  #       It attempts to check for the style used in the
  33   31  #       kernel, sometimes known as "Bill Joy Normal Form".
  34   32  #
  35   33  #       There's a lot this can't check for, like proper indentation
  36   34  #       of code blocks.  There's also a lot more this could check for.
  37   35  #
↓ open down ↓ 437 lines elided ↑ open up ↑
 475  473                  err("missing blank before close comment");
 476  474          }
 477  475          if (/\/\/\S/) {         # C++ comments
 478  476                  err("missing blank after start comment");
 479  477          }
 480  478          # check for unterminated single line comments, but allow them when
 481  479          # they are used to comment out the argument list of a function
 482  480          # declaration.
 483  481          if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
 484  482                  err("unterminated single line comment");
      483 +        }
      484 +
      485 +        # check that #if and #elif don't enumerate ISA defines when there
      486 +        # are more concise ways of checking.  E.g., don't do:
      487 +        #     #if defined(__amd64) || defined(__i386)
      488 +        # when there is:
      489 +        #     #ifdef __x86
      490 +        if ((/^(#if|#elif)\sdefined\((.*)\)\s\|\|\sdefined\((.*)\)/) ||
      491 +            (/^(#if|#elif)\s!defined\((.*)\)\s&&\s!defined\((.*)\)/)) {
      492 +                my $directive = $1;
      493 +                my $first = $2;
      494 +                my $second = $3;
      495 +                ($first, $second) = ($second, $first) if ($first gt $second);
      496 +
      497 +                if (($first eq "__amd64") && ($second eq "__i386")) {
      498 +                        err("$directive checking for $first or $second " .
      499 +                            "instead of __x86");
      500 +                }
 485  501          }
 486  502  
 487  503          if (/^(#else|#endif|#include)(.*)$/) {
 488  504                  $prev = $line;
 489  505                  if ($picky) {
 490  506                          my $directive = $1;
 491  507                          my $clause = $2;
 492  508                          # Enforce ANSI rules for #else and #endif: no noncomment
 493  509                          # identifiers are allowed after #endif or #else.  Allow
 494  510                          # C++ comments since they seem to be a fact of life.
↓ open down ↓ 456 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX