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.
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.
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]
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
26 # @(#)cstyle 1.58 98/09/09 (from shannon)
27 #ident "%Z%%M% %I% %E% SMI"
29 # cstyle - check for some common stylistic errors.
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".
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.
38 # A note to the non perl literate:
40 # perl regular expressions are pretty much like egrep
41 # regular expressions, with the following special symbols
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
49 # \b word boundary (between \w and \W)
50 # \B non-word boundary
60 "usage: cstyle [-chpvCP] [-o constructs] file ...
61 -c check continuation indentation inside functions
62 -h perform heuristic checks that are sometimes wrong
63 -p perform some of the more picky checks
65 -C don't check anything in header block comments
66 -P check for use of non-POSIX types
68 allow a comma-separated list of optional constructs:
69 doxygen allow doxygen-style block comments (/** /*!)
70 splint allow splint-style lint comments (/*@ ... @*/)
75 if (!getopts
("cho:pvCP", \
%opts)) {
80 my $check_continuation = $opts{'c'};
81 my $heuristic = $opts{'h'};
82 my $picky = $opts{'p'};
83 my $verbose = $opts{'v'};
84 my $ignore_hdr_comment = $opts{'C'};
85 my $check_posix_types = $opts{'P'};
87 my $doxygen_comments = 0;
88 my $splint_comments = 0;
90 if (defined($opts{'o'})) {
91 for my $x (split /,/, $opts{'o'}) {
92 if ($x eq "doxygen") {
93 $doxygen_comments = 1;
94 } elsif ($x eq "splint") {
97 print "cstyle: unrecognized construct \"$x\"\n";
104 my ($filename, $line, $prev); # shared globals
107 my $hdr_comment_start;
110 $fmt = "%s: %d: %s\n%s\n";
112 $fmt = "%s: %d: %s\n";
115 if ($doxygen_comments) {
116 # doxygen comments look like "/*!" or "/**"; allow them.
117 $hdr_comment_start = qr/^\s*\/\*[\!\*]?$/;
119 $hdr_comment_start = qr/^\s*\/\*$/;
122 # Note, following must be in single quotes so that \s and \w work right.
123 my $typename = '(int|char|short|long|unsigned|float|double' .
124 '|\w+_t|struct\s+\w+|union\s+\w+|FILE)';
126 # mapping of old types to POSIX compatible types
128 'unchar' => 'uchar_t',
129 'ushort' => 'ushort_t',
131 'ulong' => 'ulong_t',
133 'u_short' => 'ushort_t',
134 'u_long' => 'ulong_t',
135 'u_char' => 'uchar_t',
139 my $lint_re = qr/\/\*(?:
140 ARGSUSED[0-9]*|NOTREACHED|LINTLIBRARY|VARARGS[0-9]*|
141 CONSTCOND|CONSTANTCOND|CONSTANTCONDITION|EMPTY|
142 FALLTHRU|FALLTHROUGH|LINTED.*?|PRINTFLIKE[0-9]*|
143 PROTOLIB[0-9]*|SCANFLIKE[0-9]*|CSTYLED.*?
146 my $splint_re = qr/\/\*@.*?@\*\//x;
148 my $warlock_re = qr/\/\*\s*(?:
149 VARIABLES\ PROTECTED\ BY|
150 MEMBERS\ PROTECTED\ BY|
151 ALL\ MEMBERS\ PROTECTED\ BY|
152 READ-ONLY\ VARIABLES:|
154 VARIABLES\ READABLE\ WITHOUT\ LOCK:|
155 MEMBERS\ READABLE\ WITHOUT\ LOCK:|
157 LOCK\ UNNEEDED\ BECAUSE|
159 LOCK\ HELD\ ON\ ENTRY:|
160 READ\ LOCK\ HELD\ ON\ ENTRY:|
161 WRITE\ LOCK\ HELD\ ON\ ENTRY:|
162 LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
163 READ\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
164 WRITE\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
165 LOCK\ RELEASED\ AS\ SIDE\ EFFECT:|
166 LOCK\ UPGRADED\ AS\ SIDE\ EFFECT:|
167 LOCK\ DOWNGRADED\ AS\ SIDE\ EFFECT:|
168 FUNCTIONS\ CALLED\ THROUGH\ POINTER|
169 FUNCTIONS\ CALLED\ THROUGH\ MEMBER|
173 my $err_stat = 0; # exit status
176 foreach my $arg (@ARGV) {
177 my $fh = new IO::File $arg, "r
";
179 printf "%s: can
not open\n", $arg;
186 &cstyle("<stdin
>", *STDIN);
190 my $no_errs = 0; # set for CSTYLED-protected lines
196 printf $fmt, $filename, $., $error, $line;
198 printf $fmt, $filename, $., $error;
205 my ($prevline, $error) = @_;
206 my $out = $prevline."\n".$line;
209 printf $fmt, $filename, $., $error, $out;
211 printf $fmt, $filename, $., $error;
221 printf $fmt, $filename, $. - 1, $error, $prev;
223 printf $fmt, $filename, $. - 1, $error;
231 my ($fn, $filehandle) = @_;
232 $filename = $fn; # share it globally
238 my $in_header_comment = 0;
239 my $comment_done = 0;
240 my $in_warlock_comment = 0;
242 my $in_function_header = 0;
243 my $function_header_full_indent = 0;
244 my $in_declaration = 0;
251 my ($okmsg, $comment_prefix);
257 line: while (<$filehandle>) {
258 s/\r?\n$//; # strip return and newline
260 # save the original line, then remove all text from within
261 # double or single quotes, we do not want to check such text.
266 # C allows strings to be continued with a backslash at the end of
267 # the line. We translate that into a quoted string on the previous
268 # line followed by an initial quote on the next line.
270 # (we assume that no-one will use backslash-continuation with character
273 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
276 # normal strings and characters
278 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
279 s/"([^\\"]|\\.)*"/\"\"/g;
282 # detect string continuation
284 if ($nocheck || $in_comment) {
288 # Now that all full strings are replaced with "", we check
289 # for unfinished strings continuing onto the next line.
292 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
293 s/^("")*"([^\\"]|\\.)*\\$/""/);
297 # figure out if we are in a cpp directive
299 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
300 $next_in_cpp = $in_cpp && /\\$/; # only if continued
302 # strip off trailing backslashes, which appear in long macros
305 # an /* END CSTYLED */ comment ends a no-check block.
307 if (/\/\* *END *CSTYLED *\*\//) {
315 # a /*CSTYLED*/ comment indicates that the next line is ok.
322 if (/\/\* *CSTYLED.*\*\//) {
323 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
332 # check length of line.
333 # first, a quick check to see if there is any chance of being too long.
334 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
335 # yes, there is a chance.
336 # replace tabs with spaces and check again.
339 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
340 if (length($eline) > 80) {
341 err("line > 80 characters");
345 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
346 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
347 s/[^()]//g; # eliminate all non-parens
348 $note_level += s/\(//g - length; # update paren nest level
352 # a /* BEGIN CSTYLED */ comment starts a no-check block.
353 if (/\/\* *BEGIN *CSTYLED *\*\//) {
357 # a /*CSTYLED*/ comment indicates that the next line is ok.
358 if (/\/\* *CSTYLED.*\*\//) {
359 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
363 if (/\/\/ *CSTYLED/) {
364 /^.*\/\/ *CSTYLED *(.*)$/;
369 # universal checks; apply to everything
371 err("spaces between tabs");
374 err("tabs between spaces");
377 err("space or tab at end of line");
379 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) {
380 err("comment preceded by non-blank");
383 # is this the beginning or ending of a function?
384 # (not if "struct foo\n{\n")
385 if (/^{$/ && $prev =~ /\)\s*(const\s*)?(\/\*.*\*\/\s*)?\\?$/) {
388 $in_function_header = 0;
389 $function_header_full_indent = 0;
393 if (/^}\s*(\/\*.*\*\/\s*)*$/) {
394 if ($prev =~ /^\s*return\s*;/) {
395 err_prev("unneeded return at end of function");
398 reset_indent(); # we don't check between functions
402 if ($in_function_header && ! /^ ./ ) {
403 if (/^{}$/ # empty functions
404 || /;/ #run function with multiline arguments
405 || /#/ #preprocessor commands
406 || /^[^\s\\]*\(.*\)$/ #functions without ; at the end
407 || /^$/ #function declaration can't have empty line
409 $in_function_header = 0;
410 $function_header_full_indent = 0;
411 } elsif ($prev =~ /^__attribute__/) { #__attribute__((*))
412 $in_function_header = 0;
413 $function_header_full_indent = 0;
416 } elsif ($picky && ! (/^\t/ && $function_header_full_indent != 0)) {
418 err
("continuation line should be indented by 4 spaces");
423 # If this matches something of form "foo(", it's probably a function
424 # definition, unless it ends with ") bar;", in which case it's a declaration
425 # that uses a macro to generate the type.
427 if (/^\w+\(/ && !/\) \w+;/) {
428 $in_function_header = 1;
430 $function_header_full_indent = 1;
433 if ($in_function_header && /^{$/) {
434 $in_function_header = 0;
435 $function_header_full_indent = 0;
438 if ($in_function_header && /\);$/) {
439 $in_function_header = 0;
440 $function_header_full_indent = 0;
442 if ($in_function_header && /{$/ ) {
444 err
("opening brace on same line as function header");
446 $in_function_header = 0;
447 $function_header_full_indent = 0;
452 if ($in_warlock_comment && /\*\//) {
453 $in_warlock_comment = 0;
458 # a blank line terminates the declarations within a function.
459 # XXX - but still a problem in sub-blocks.
460 if ($in_declaration && /^$/) {
466 $in_header_comment = 0;
469 # does this looks like the start of a block comment?
470 if (/$hdr_comment_start/) {
472 err
("block comment not indented by tabs");
476 $comment_prefix = $1;
477 if ($comment_prefix eq "") {
478 $in_header_comment = 1;
483 # are we still in the block comment?
485 if (/^$comment_prefix \*\/$/) {
489 err
("improper block comment close")
490 unless ($ignore_hdr_comment && $in_header_comment);
491 } elsif (!/^$comment_prefix \*[ \t]/ &&
492 !/^$comment_prefix \*$/) {
493 err
("improper block comment")
494 unless ($ignore_hdr_comment && $in_header_comment);
498 if ($in_header_comment && $ignore_hdr_comment) {
503 # check for errors that might occur in comments and in code.
505 # allow spaces to be used to draw pictures in all comments.
506 if (/[^ ] / && !/".* .*"/ && !$in_comment) {
507 err
("spaces instead of tabs");
509 if (/^ / && !/^ \*[ \t\/]/ && !/^ \
*$/ &&
510 (!/^ \w/ || $in_function != 0)) {
511 err
("indent by spaces instead of tabs");
513 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
514 err
("continuation line not indented by 4 spaces");
516 if (/$warlock_re/ && !/\*\//) {
517 $in_warlock_comment = 1;
521 if (/^\s*\/\
*./ && !/^\s
*\
/\*.*\*\// && !/$hdr_comment_start/) {
522 err
("improper first line of block comment");
525 if ($in_comment) { # still in comment, don't do further checks
530 if ((/[^(]\/\
*\S
/ || /^\
/\*\S/) &&
531 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
532 err
("missing blank after open comment");
534 if (/\S\*\/[^)]|\S\
*\
/$/ &&
535 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
536 err
("missing blank before close comment");
538 if (/\/\
/\S/) { # C++ comments
539 err
("missing blank after start comment");
541 # check for unterminated single line comments, but allow them when
542 # they are used to comment out the argument list of a function
544 if (/\S.*\/\
*/ && !/\S
.*\
/\*.*\*\// && !/\
(\
/\*/) {
545 err
("unterminated single line comment");
548 if (/^(#else|#endif|#include)(.*)$/) {
553 # Enforce ANSI rules for #else and #endif: no noncomment
554 # identifiers are allowed after #endif or #else. Allow
555 # C++ comments since they seem to be a fact of life.
556 if ((($1 eq "#endif") || ($1 eq "#else")) &&
558 (!($clause =~ /^\s+\/\
*.*\
*\
/$/)) &&
559 (!($clause =~ /^\s+\/\
/.*$/))) {
560 err
("non-comment text following " .
561 "$directive (or malformed $directive " .
569 # delete any comments and check everything else. Note that
570 # ".*?" is a non-greedy match, so that we don't get confused by
571 # multiple comments on the same line.
574 s/\/\/.*$/\ 1/; # C++ comments
576 # delete any trailing whitespace; we have already checked for that.
579 # following checks do not apply to text in comments.
581 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
582 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
583 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
584 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
585 err
("missing space around relational operator");
587 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
588 (/[^-+*\/&|^%!<>=\s
]=[^=]/ && !/[^-+*\
/&|^%!<>=\s]=$/) ||
589 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
590 # XXX - should only check this for C++ code
591 # XXX - there are probably other forms that should be allowed
592 if (!/\soperator=/) {
593 err
("missing space around assignment operator");
596 if (/[,;]\S/ && !/\bfor \(;;\)/) {
597 err
("comma or semicolon followed by non-blank");
599 # allow "for" statements to have empty "while" clauses
600 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
601 err
("comma or semicolon preceded by blank");
603 if (/^\s*(&&|\|\|)/) {
604 err
("improper boolean continuation");
606 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
607 err
("more than one space around boolean operator");
609 if (/\b(for|if|while|switch|sizeof|return|case)\(/) {
610 err
("missing space between keyword and paren");
612 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
613 # multiple "case" and "sizeof" allowed
614 err
("more than one keyword on line");
616 if (/\b(for|if|while|switch|sizeof|return|case)\s\s+\(/ &&
618 err
("extra space between keyword and paren");
620 # try to detect "func (x)" but not "if (x)" or
621 # "#define foo (x)" or "int (*func)();"
624 # strip off all keywords on the line
625 s/\b(for|if|while|switch|return|case|sizeof)\s\(/XXX(/g;
627 s/^#define\s+\w+\s+\(/XXX(/;
628 # do not match things like "void (*f)();"
629 # or "typedef void (func_t)();"
631 s/\b($typename|void)\s+\(+/XXX(/og;
633 err
("extra space between function name and left paren");
637 # try to detect "int foo(x)", but not "extern int foo(x);"
638 # XXX - this still trips over too many legitimate things,
639 # like "int foo(x,\n\ty);"
640 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\ 1)*$/ &&
641 # !/^(extern|static)\b/) {
642 # err("return type of function not on separate line");
644 # this is a close approximation
645 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\ 1)*$/ &&
646 !/^(extern|static)\b/) {
647 err
("return type of function not on separate line");
650 err
("#define followed by space instead of tab");
652 if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
653 err
("unparenthesized return expression");
655 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
656 err
("unparenthesized sizeof expression");
659 err
("whitespace after left paren");
661 # Allow "for" statements to have empty "continue" clauses.
662 # Allow right paren on its own line unless we're being picky (-p).
663 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/ && ($picky || !/^\s*\)/)) {
664 err
("whitespace before right paren");
666 if (/^\s*\(void\)[^ ]/) {
667 err
("missing space after (void) cast");
669 if (/\S\{/ && !/\{\{/) {
670 err
("missing space before left brace");
672 if ($in_function && /^\s+{/ &&
673 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
674 err
("left brace starting a line");
676 if (/}(else|while)/) {
677 err
("missing space after right brace");
679 if (/}\s\s+(else|while)/) {
680 err
("extra space after right brace");
682 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
683 err
("obsolete use of VOID or STATIC");
685 if (/\b$typename\*/o) {
686 err
("missing space between type name and *");
689 err
("preprocessor statement not in column 1");
692 err
("blank after preprocessor #");
694 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
695 err
("don't use boolean ! with comparison functions");
699 # We completely ignore, for purposes of indentation:
700 # * lines outside of functions
701 # * preprocessor lines
703 if ($check_continuation && $in_function && !$in_cpp) {
707 # try to detect spaces after casts, but allow (e.g.)
708 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
709 # "int foo(int) __NORETURN;"
710 if ((/^\($typename( \*+)?\)\s/o ||
711 /\W\($typename( \*+)?\)\s/o) &&
712 !/sizeof\s*\($typename( \*)?\)\s/o &&
713 !/\($typename( \*+)?\)\s+=[^=]/o) {
714 err
("space after cast");
716 if (/\b$typename\s*\*\s/o &&
717 !/\b$typename\s*\*\s+const\b/o) {
718 err
("unary * followed by space");
721 if ($check_posix_types) {
722 # try to detect old non-POSIX types.
723 # POSIX requires all non-standard typedefs to end in _t,
724 # but historically these have been used.
725 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
726 err
("non-POSIX typedef $1 used: use $old2posix{$1} instead");
730 # cannot check this everywhere due to "struct {\n...\n} foo;"
731 if ($in_function && !$in_declaration &&
732 /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|\ 1)*$/ &&
733 !/} (else|while)/ && !/}}/) {
734 err
("possible bad text following right brace");
736 # cannot check this because sub-blocks in
737 # the middle of code are ok
738 if ($in_function && /^\s+{/) {
739 err
("possible left brace starting a line");
743 if ($prev =~ /^\s*}$/) {
745 "else and right brace should be on same line");
752 err
("last line in file is blank");
758 # Continuation-line checking
760 # The rest of this file contains the code for the continuation checking
761 # engine. It's a pretty simple state machine which tracks the expression
762 # depth (unmatched '('s and '['s).
764 # Keep in mind that the argument to process_indent() has already been heavily
765 # processed; all comments have been replaced by control-A, and the contents of
766 # strings and character constants have been elided.
769 my $cont_in; # currently inside of a continuation
770 my $cont_off; # skipping an initializer or definition
771 my $cont_noerr; # suppress cascading errors
772 my $cont_start; # the line being continued
773 my $cont_base; # the base indentation
774 my $cont_first; # this is the first line of a statement
775 my $cont_multiseg; # this continuation has multiple segments
777 my $cont_special; # this is a C statement (if, for, etc.)
778 my $cont_macro; # this is a macro
779 my $cont_case; # this is a multi-line case
781 my @cont_paren; # the stack of unmatched ( and [s we've seen
794 # replace labels with tabs. Note that there may be multiple
799 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
800 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
802 while ($label =~ s/^([^\t]*)(\t+)//) {
803 $_ .= "\t" x
(length($2) + length($1) / 8);
805 $_ .= ("\t" x
(length($label) / 8)).$rest;
815 local $_ = $_[0]; # preserve the global $_
817 s/\ 1//g; # No comments
818 s/\s+$//; # Strip trailing whitespace
820 return if (/^$/); # skip empty lines
822 # regexps used below; keywords taking (), macros, and continued cases
823 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
824 my $macro = '[A-Z_][A-Z_0-9]*\(';
825 my $case = 'case\b[^:]*$';
827 # skip over enumerations, array definitions, initializers, etc.
828 if ($cont_off <= 0 && !/^\s*$special/ &&
829 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*)){/ ||
830 (/^\s*{/ && $prev =~ /=\s*(?:\/\
*.*\
*\
/\s*)*$/))) {
832 $cont_off = tr/{/{/ - tr/}/}/;
836 $cont_off += tr/{/{/ - tr/}/}/;
844 err
("non-continuation indented 4 spaces");
845 $cont_noerr = 1; # stop reporting
847 $_ = delabel
($_); # replace labels with tabs
849 # check if the statement is complete
850 return if (/^\s*\}?$/);
851 return if (/^\s*\}?\s*else\s*\{?$/);
852 return if (/^\s*do\s*\{?$/);
854 return if (/}[,;]?$/);
856 # Allow macros on their own lines
857 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
859 # cases we don't deal with, generally non-kosher
861 err
("stuff after {");
865 # Get the base line, and set up the state machine
873 # certain things need special processing
874 $cont_special = /^\s*$special/?
1 : 0;
875 $cont_macro = /^\s*$macro/?
1 : 0;
876 $cont_case = /^\s*$case/?
1 : 0;
880 # Strings may be pulled back to an earlier (half-)tabstop
881 unless ($cont_noerr || /^$cont_base / ||
882 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
883 err_prefix
($cont_start,
884 "continuation should be indented 4 spaces");
888 my $rest = $_; # keeps the remainder of the line
891 # The split matches 0 characters, so that each 'special' character
892 # is processed separately. Parens and brackets are pushed and
893 # popped off the @cont_paren stack. For normal processing, we wait
894 # until a ; or { terminates the statement. "special" processing
895 # (if/for/while/switch) is allowed to stop when the stack empties,
896 # as is macro processing. Case statements are terminated with a :
897 # and an empty paren stack.
899 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
900 next if (length($_) == 0);
902 # rest contains the remainder of the line
903 my $rxp = "[^\Q$_\E]*\Q$_\E";
907 push @cont_paren, $_;
908 } elsif (/\)/ || /\]/) {
912 my $old = (pop @cont_paren);
913 if (!defined($old)) {
914 err
("unexpected '$cur'");
917 } elsif ($old ne $_) {
918 err
("'$cur' mismatched with '$old'");
924 # If the stack is now empty, do special processing
925 # for if/for/while/switch and macro statements.
927 next if (@cont_paren != 0);
929 if ($rest =~ /^\s*{?$/) {
933 if ($rest =~ /^\s*;$/) {
934 err
("empty if/for/while body ".
935 "not on its own line");
939 if (!$cont_first && $cont_multiseg == 1) {
940 err_prefix
($cont_start,
941 "multiple statements continued ".
942 "over multiple lines");
944 } elsif ($cont_multiseg == 0) {
947 # We've finished this section, start
948 # processing the next.
960 } elsif (!$cont_special) {
961 err
("unexpected ;") if (@cont_paren != 0);
962 if (!$cont_first && $cont_multiseg == 1) {
963 err_prefix
($cont_start,
964 "multiple statements continued ".
965 "over multiple lines");
967 } elsif ($cont_multiseg == 0) {
974 if ($rest =~ /^\s*special/) {
975 err
("if/for/while/switch not started ".
981 err
("{ while in parens/brackets") if (@cont_paren != 0);
982 err
("stuff after {") if ($rest =~ /[^\s}]/);
986 err
("} while in parens/brackets") if (@cont_paren != 0);
987 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
991 err
("stuff after }");
996 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
997 err
("stuff after multi-line case") if ($rest !~ /$^/);
1003 # End of a statement or if/while/for loop. Reset
1004 # cont_special and cont_macro based on the rest of the
1006 $cont_special = ($rest =~ /^\s*$special/)?
1 : 0;
1007 $cont_macro = ($rest =~ /^\s*$macro/)?
1 : 0;
1011 $cont_noerr = 0 if (!$cont_in);