5 # The contents of this file are subject to the terms of the
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]
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;
|
5 # The contents of this file are subject to the terms of the
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 # Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 #
27 # cstyle - check for some common stylistic errors.
28 #
29 # cstyle is a sort of "lint" for C coding style.
30 # It attempts to check for the style used in the
31 # kernel, sometimes known as "Bill Joy Normal Form".
32 #
33 # There's a lot this can't check for, like proper indentation
34 # of code blocks. There's also a lot more this could check for.
35 #
36 # A note to the non perl literate:
37 #
38 # perl regular expressions are pretty much like egrep
39 # regular expressions, with the following special symbols
40 #
41 # \s any space character
42 # \S any non-space character
43 # \w any "word" character [a-zA-Z0-9_]
44 # \W any non-word character
45 # \d a digit [0-9]
463 $prev = $line;
464 next line;
465 }
466
467 if ((/[^(]\/\*\S/ || /^\/\*\S/) &&
468 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
469 err("missing blank after open comment");
470 }
471 if (/\S\*\/[^)]|\S\*\/$/ &&
472 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
473 err("missing blank before close comment");
474 }
475 if (/\/\/\S/) { # C++ comments
476 err("missing blank after start comment");
477 }
478 # check for unterminated single line comments, but allow them when
479 # they are used to comment out the argument list of a function
480 # declaration.
481 if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
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 }
501 }
502
503 if (/^(#else|#endif|#include)(.*)$/) {
504 $prev = $line;
505 if ($picky) {
506 my $directive = $1;
507 my $clause = $2;
508 # Enforce ANSI rules for #else and #endif: no noncomment
509 # identifiers are allowed after #endif or #else. Allow
510 # C++ comments since they seem to be a fact of life.
511 if ((($1 eq "#endif") || ($1 eq "#else")) &&
512 ($clause ne "") &&
513 (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
514 (!($clause =~ /^\s+\/\/.*$/))) {
515 err("non-comment text following " .
516 "$directive (or malformed $directive " .
517 "directive)");
518 }
519 }
520 next line;
|