Print this page
patch cstyle-x86


   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 #
  23 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 # @(#)cstyle 1.58 98/09/09 (from shannon)
  27 #ident  "%Z%%M% %I%     %E% SMI"
  28 #
  29 # cstyle - check for some common stylistic errors.
  30 #
  31 #       cstyle is a sort of "lint" for C coding style.
  32 #       It attempts to check for the style used in the
  33 #       kernel, sometimes known as "Bill Joy Normal Form".
  34 #
  35 #       There's a lot this can't check for, like proper indentation
  36 #       of code blocks.  There's also a lot more this could check for.
  37 #
  38 #       A note to the non perl literate:
  39 #
  40 #               perl regular expressions are pretty much like egrep
  41 #               regular expressions, with the following special symbols
  42 #
  43 #               \s      any space character
  44 #               \S      any non-space character
  45 #               \w      any "word" character [a-zA-Z0-9_]
  46 #               \W      any non-word character
  47 #               \d      a digit [0-9]
  48 #               \D      a non-digit


 465                 $prev = $line;
 466                 next line;
 467         }
 468 
 469         if ((/[^(]\/\*\S/ || /^\/\*\S/) &&
 470             !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
 471                 err("missing blank after open comment");
 472         }
 473         if (/\S\*\/[^)]|\S\*\/$/ &&
 474             !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
 475                 err("missing blank before close comment");
 476         }
 477         if (/\/\/\S/) {         # C++ comments
 478                 err("missing blank after start comment");
 479         }
 480         # check for unterminated single line comments, but allow them when
 481         # they are used to comment out the argument list of a function
 482         # declaration.
 483         if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
 484                 err("unterminated single line comment");
















 485         }
 486 
 487         if (/^(#else|#endif|#include)(.*)$/) {
 488                 $prev = $line;
 489                 if ($picky) {
 490                         my $directive = $1;
 491                         my $clause = $2;
 492                         # Enforce ANSI rules for #else and #endif: no noncomment
 493                         # identifiers are allowed after #endif or #else.  Allow
 494                         # C++ comments since they seem to be a fact of life.
 495                         if ((($1 eq "#endif") || ($1 eq "#else")) &&
 496                             ($clause ne "") &&
 497                             (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
 498                             (!($clause =~ /^\s+\/\/.*$/))) {
 499                                 err("non-comment text following " .
 500                                     "$directive (or malformed $directive " .
 501                                     "directive)");
 502                         }
 503                 }
 504                 next line;




   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 #
  23 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #



  26 # cstyle - check for some common stylistic errors.
  27 #
  28 #       cstyle is a sort of "lint" for C coding style.
  29 #       It attempts to check for the style used in the
  30 #       kernel, sometimes known as "Bill Joy Normal Form".
  31 #
  32 #       There's a lot this can't check for, like proper indentation
  33 #       of code blocks.  There's also a lot more this could check for.
  34 #
  35 #       A note to the non perl literate:
  36 #
  37 #               perl regular expressions are pretty much like egrep
  38 #               regular expressions, with the following special symbols
  39 #
  40 #               \s      any space character
  41 #               \S      any non-space character
  42 #               \w      any "word" character [a-zA-Z0-9_]
  43 #               \W      any non-word character
  44 #               \d      a digit [0-9]
  45 #               \D      a non-digit


 462                 $prev = $line;
 463                 next line;
 464         }
 465 
 466         if ((/[^(]\/\*\S/ || /^\/\*\S/) &&
 467             !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
 468                 err("missing blank after open comment");
 469         }
 470         if (/\S\*\/[^)]|\S\*\/$/ &&
 471             !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
 472                 err("missing blank before close comment");
 473         }
 474         if (/\/\/\S/) {         # C++ comments
 475                 err("missing blank after start comment");
 476         }
 477         # check for unterminated single line comments, but allow them when
 478         # they are used to comment out the argument list of a function
 479         # declaration.
 480         if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
 481                 err("unterminated single line comment");
 482         }
 483 
 484         # check that #if doesn't enumerate ISA defines when there are more
 485         # concise ways of checking.  E.g., don't do:
 486         #     #if defined(__amd64) || defined(__i386)
 487         # when there is:
 488         #     #ifdef __x86
 489         if (/^#if\sdefined\((.*)\)\s\|\|\sdefined\((.*)\)/) {
 490                 my $first = $1;
 491                 my $second = $2;
 492                 ($first, $second) = ($second, $first) if ($first gt $second);
 493 
 494                 if (($first eq "__amd64") && ($second eq "__i386")) {
 495                         err("#if checking for $first or $second instead of " .
 496                             "__x86");
 497                 }
 498         }
 499 
 500         if (/^(#else|#endif|#include)(.*)$/) {
 501                 $prev = $line;
 502                 if ($picky) {
 503                         my $directive = $1;
 504                         my $clause = $2;
 505                         # Enforce ANSI rules for #else and #endif: no noncomment
 506                         # identifiers are allowed after #endif or #else.  Allow
 507                         # C++ comments since they seem to be a fact of life.
 508                         if ((($1 eq "#endif") || ($1 eq "#else")) &&
 509                             ($clause ne "") &&
 510                             (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
 511                             (!($clause =~ /^\s+\/\/.*$/))) {
 512                                 err("non-comment text following " .
 513                                     "$directive (or malformed $directive " .
 514                                     "directive)");
 515                         }
 516                 }
 517                 next line;