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]
22 # Copyright 2015 Toomas Soome <tsoome@me.com>
23 # Copyright 2016 Nexenta Systems, Inc.
25 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
26 # Use is subject to license terms.
28 # Copyright (c) 2015 by Delphix. All rights reserved.
30 # @(#)cstyle 1.58 98/09/09 (from shannon)
32 # cstyle - check for some common stylistic errors.
34 # cstyle is a sort of "lint" for C coding style.
35 # It attempts to check for the style used in the
36 # kernel, sometimes known as "Bill Joy Normal Form".
38 # There's a lot this can't check for, like proper indentation
39 # of code blocks. There's also a lot more this could check for.
41 # A note to the non perl literate:
43 # perl regular expressions are pretty much like egrep
44 # regular expressions, with the following special symbols
46 # \s any space character
47 # \S any non-space character
48 # \w any "word" character [a-zA-Z0-9_]
49 # \W any non-word character
52 # \b word boundary (between \w and \W)
53 # \B non-word boundary
62 "usage: cstyle [-chpvCP] [-o constructs] file ...
63 -c check continuation indentation inside functions
64 -h perform heuristic checks that are sometimes wrong
65 -p perform some of the more picky checks
67 -C don't check anything in header block comments
68 -P check for use of non-POSIX types
70 allow a comma-seperated list of optional constructs:
71 doxygen allow doxygen-style block comments (/** /*!)
72 splint allow splint-style lint comments (/*@ ... @*/)
77 if (!getopts
("cho:pvCP", \
%opts)) {
82 my $check_continuation = $opts{'c'};
83 my $heuristic = $opts{'h'};
84 my $picky = $opts{'p'};
85 my $verbose = $opts{'v'};
86 my $ignore_hdr_comment = $opts{'C'};
87 my $check_posix_types = $opts{'P'};
89 my $doxygen_comments = 0;
90 my $splint_comments = 0;
92 if (defined($opts{'o'})) {
93 for my $x (split /,/, $opts{'o'}) {
94 if ($x eq "doxygen") {
95 $doxygen_comments = 1;
96 } elsif ($x eq "splint") {
99 print "cstyle: unrecognized construct \"$x\"\n";
106 my ($filename, $line, $prev); # shared globals
109 my $hdr_comment_start;
112 $fmt = "%s: %d: %s\n%s\n";
114 $fmt = "%s: %d: %s\n";
117 if ($doxygen_comments) {
118 # doxygen comments look like "/*!" or "/**"; allow them.
119 $hdr_comment_start = qr/^\s*\/\
*[\
!\
*]?
$/;
121 $hdr_comment_start = qr/^\s*\/\
*$/;
124 # Note, following must be in single quotes so that \s and \w work right.
125 my $typename = '(int|char|short|long|unsigned|float|double' .
126 '|\w+_t|struct\s+\w+|union\s+\w+|FILE)';
128 # mapping of old types to POSIX compatible types
130 'unchar' => 'uchar_t',
131 'ushort' => 'ushort_t',
133 'ulong' => 'ulong_t',
135 'u_short' => 'ushort_t',
136 'u_long' => 'ulong_t',
137 'u_char' => 'uchar_t',
141 my $lint_re = qr/\/\
*(?
:
142 ARGSUSED
[0-9]*|NOTREACHED
|LINTLIBRARY
|VARARGS
[0-9]*|
143 CONSTCOND
|CONSTANTCOND
|CONSTANTCONDITION
|EMPTY
|
144 FALLTHRU
|FALLTHROUGH
|LINTED
.*?
|PRINTFLIKE
[0-9]*|
145 PROTOLIB
[0-9]*|SCANFLIKE
[0-9]*|CSTYLED
.*?
148 my $splint_re = qr/\/\
*@
.*?@\
*\
//x;
150 my $warlock_re = qr/\/\
*\s
*(?
:
151 VARIABLES\ PROTECTED\ BY
|
152 MEMBERS\ PROTECTED\ BY
|
153 ALL\ MEMBERS\ PROTECTED\ BY
|
154 READ
-ONLY\ VARIABLES
:|
156 VARIABLES\ READABLE\ WITHOUT\ LOCK
:|
157 MEMBERS\ READABLE\ WITHOUT\ LOCK
:|
159 LOCK\ UNNEEDED\ BECAUSE
|
161 LOCK\ HELD\ ON\ ENTRY
:|
162 READ\ LOCK\ HELD\ ON\ ENTRY
:|
163 WRITE\ LOCK\ HELD\ ON\ ENTRY
:|
164 LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
165 READ\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
166 WRITE\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
167 LOCK\ RELEASED\ AS\ SIDE\ EFFECT
:|
168 LOCK\ UPGRADED\ AS\ SIDE\ EFFECT
:|
169 LOCK\ DOWNGRADED\ AS\ SIDE\ EFFECT
:|
170 FUNCTIONS\ CALLED\ THROUGH\ POINTER
|
171 FUNCTIONS\ CALLED\ THROUGH\ MEMBER
|
175 my $err_stat = 0; # exit status
178 foreach my $arg (@ARGV) {
179 my $fh = new IO
::File
$arg, "r";
181 printf "%s: can not open\n", $arg;
188 &cstyle
("<stdin>", *STDIN
);
192 my $no_errs = 0; # set for CSTYLED-protected lines
198 printf $fmt, $filename, $., $error, $line;
200 printf $fmt, $filename, $., $error;
207 my ($prevline, $error) = @_;
208 my $out = $prevline."\n".$line;
211 printf $fmt, $filename, $., $error, $out;
213 printf $fmt, $filename, $., $error;
223 printf $fmt, $filename, $. - 1, $error, $prev;
225 printf $fmt, $filename, $. - 1, $error;
233 my ($fn, $filehandle) = @_;
234 $filename = $fn; # share it globally
240 my $in_header_comment = 0;
241 my $comment_done = 0;
242 my $in_warlock_comment = 0;
244 my $in_function_header = 0;
245 my $function_header_full_indent = 0;
246 my $in_declaration = 0;
253 my ($okmsg, $comment_prefix);
259 line
: while (<$filehandle>) {
260 s/\r?\n$//; # strip return and newline
262 # save the original line, then remove all text from within
263 # double or single quotes, we do not want to check such text.
268 # C allows strings to be continued with a backslash at the end of
269 # the line. We translate that into a quoted string on the previous
270 # line followed by an initial quote on the next line.
272 # (we assume that no-one will use backslash-continuation with character
275 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
278 # normal strings and characters
280 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
281 s/"([^\\"]|\\.)*"/\"\"/g;
284 # detect string continuation
286 if ($nocheck || $in_comment) {
290 # Now that all full strings are replaced with "", we check
291 # for unfinished strings continuing onto the next line.
294 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
295 s/^("")*"([^\\"]|\\.)*\\$/""/);
299 # figure out if we are in a cpp directive
301 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
302 $next_in_cpp = $in_cpp && /\\$/; # only if continued
304 # strip off trailing backslashes, which appear in long macros
307 # an /* END CSTYLED */ comment ends a no-check block.
309 if (/\/\
* *END *CSTYLED
*\
*\
//) {
317 # a /*CSTYLED*/ comment indicates that the next line is ok.
324 if (/\/\
* *CSTYLED
.*\
*\
//) {
325 /^.*\/\
* *CSTYLED
*(.*) *\
*\
/.*$/;
334 # check length of line.
335 # first, a quick check to see if there is any chance of being too long.
336 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
337 # yes, there is a chance.
338 # replace tabs with spaces and check again.
341 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
342 if (length($eline) > 80) {
343 err
("line > 80 characters");
347 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
348 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
349 s/[^()]//g; # eliminate all non-parens
350 $note_level += s/\(//g - length; # update paren nest level
354 # a /* BEGIN CSTYLED */ comment starts a no-check block.
355 if (/\/\
* *BEGIN *CSTYLED
*\
*\
//) {
359 # a /*CSTYLED*/ comment indicates that the next line is ok.
360 if (/\/\
* *CSTYLED
.*\
*\
//) {
361 /^.*\/\
* *CSTYLED
*(.*) *\
*\
/.*$/;
365 if (/\/\
/ *CSTYLED/) {
366 /^.*\/\
/ *CSTYLED *(.*)$/;
371 # universal checks; apply to everything
373 err
("spaces between tabs");
376 err
("tabs between spaces");
379 err
("space or tab at end of line");
381 if (/[^ \t(]\/\
*/ && !/\w\
(\
/\*.*\*\/\
);/) {
382 err
("comment preceded by non-blank");
385 # is this the beginning or ending of a function?
386 # (not if "struct foo\n{\n")
387 if (/^{$/ && $prev =~ /\)\s*(const\s*)?(\/\
*.*\
*\
/\s*)?\\?$/) {
390 $in_function_header = 0;
391 $function_header_full_indent = 0;
395 if (/^}\s*(\/\
*.*\
*\
/\s*)*$/) {
396 if ($prev =~ /^\s*return\s*;/) {
397 err_prev
("unneeded return at end of function");
400 reset_indent
(); # we don't check between functions
404 if ($in_function_header && ! /^ (\w|\.)/ ) {
406 $in_function_header = 0;
407 $function_header_full_indent = 0;
408 } elsif ($picky && ! (/^\t/ && $function_header_full_indent != 0)) {
409 err
("continuation line should be indented by 4 spaces");
414 # If this matches something of form "foo(", it's probably a function
415 # definition, unless it ends with ") bar;", in which case it's a declaration
416 # that uses a macro to generate the type.
418 if (/^\w+\(/ && !/\) \w+;$/) {
419 $in_function_header = 1;
421 $function_header_full_indent = 1;
424 if ($in_function_header && /^{$/) {
425 $in_function_header = 0;
426 $function_header_full_indent = 0;
429 if ($in_function_header && /\);$/) {
430 $in_function_header = 0;
431 $function_header_full_indent = 0;
433 if ($in_function_header && /{$/ ) {
435 err
("opening brace on same line as function header");
437 $in_function_header = 0;
438 $function_header_full_indent = 0;
443 if ($in_warlock_comment && /\*\//) {
444 $in_warlock_comment = 0;
449 # a blank line terminates the declarations within a function.
450 # XXX - but still a problem in sub-blocks.
451 if ($in_declaration && /^$/) {
457 $in_header_comment = 0;
460 # does this looks like the start of a block comment?
461 if (/$hdr_comment_start/) {
463 err
("block comment not indented by tabs");
467 $comment_prefix = $1;
468 if ($comment_prefix eq "") {
469 $in_header_comment = 1;
474 # are we still in the block comment?
476 if (/^$comment_prefix \*\/$/) {
480 err
("improper block comment close")
481 unless ($ignore_hdr_comment && $in_header_comment);
482 } elsif (!/^$comment_prefix \*[ \t]/ &&
483 !/^$comment_prefix \*$/) {
484 err
("improper block comment")
485 unless ($ignore_hdr_comment && $in_header_comment);
489 if ($in_header_comment && $ignore_hdr_comment) {
494 # check for errors that might occur in comments and in code.
496 # allow spaces to be used to draw pictures in header comments.
497 if (/[^ ] / && !/".* .*"/ && !$in_header_comment) {
498 err
("spaces instead of tabs");
500 if (/^ / && !/^ \*[ \t\/]/ && !/^ \
*$/ &&
501 (!/^ (\w|\.)/ || $in_function != 0)) {
502 err
("indent by spaces instead of tabs");
504 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
505 err
("continuation line not indented by 4 spaces");
507 if (/$warlock_re/ && !/\*\//) {
508 $in_warlock_comment = 1;
512 if (/^\s*\/\
*./ && !/^\s
*\
/\*.*\*\// && !/$hdr_comment_start/) {
513 err
("improper first line of block comment");
516 if ($in_comment) { # still in comment, don't do further checks
521 if ((/[^(]\/\
*\S
/ || /^\
/\*\S/) &&
522 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
523 err
("missing blank after open comment");
525 if (/\S\*\/[^)]|\S\
*\
/$/ &&
526 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
527 err
("missing blank before close comment");
529 if (/\/\
/\S/) { # C++ comments
530 err
("missing blank after start comment");
532 # check for unterminated single line comments, but allow them when
533 # they are used to comment out the argument list of a function
535 if (/\S.*\/\
*/ && !/\S
.*\
/\*.*\*\// && !/\
(\
/\*/) {
536 err
("unterminated single line comment");
539 if (/^(#else|#endif|#include)(.*)$/) {
544 # Enforce ANSI rules for #else and #endif: no noncomment
545 # identifiers are allowed after #endif or #else. Allow
546 # C++ comments since they seem to be a fact of life.
547 if ((($1 eq "#endif") || ($1 eq "#else")) &&
549 (!($clause =~ /^\s+\/\
*.*\
*\
/$/)) &&
550 (!($clause =~ /^\s+\/\
/.*$/))) {
551 err
("non-comment text following " .
552 "$directive (or malformed $directive " .
560 # delete any comments and check everything else. Note that
561 # ".*?" is a non-greedy match, so that we don't get confused by
562 # multiple comments on the same line.
564 s/\/\*.*?\*\//\x01/g;
565 s/\/\/.*$/\x01/; # C++ comments
567 # delete any trailing whitespace; we have already checked for that.
570 # following checks do not apply to text in comments.
572 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
573 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
574 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
575 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
576 err
("missing space around relational operator");
578 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
579 (/[^-+*\/&|^%!<>=\s
]=[^=]/ && !/[^-+*\
/&|^%!<>=\s]=$/) ||
580 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
581 # XXX - should only check this for C++ code
582 # XXX - there are probably other forms that should be allowed
583 if (!/\soperator=/) {
584 err
("missing space around assignment operator");
587 if (/[,;]\S/ && !/\bfor \(;;\)/) {
588 err
("comma or semicolon followed by non-blank");
590 # allow "for" statements to have empty "while" clauses
591 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
592 err
("comma or semicolon preceded by blank");
594 if (/^\s*(&&|\|\|)/) {
595 err
("improper boolean continuation");
597 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
598 err
("more than one space around boolean operator");
600 if (/\b(for|if|while|switch|sizeof|return|case)\(/) {
601 err
("missing space between keyword and paren");
603 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
604 # multiple "case" and "sizeof" allowed
605 err
("more than one keyword on line");
607 if (/\b(for|if|while|switch|sizeof|return|case)\s\s+\(/ &&
609 err
("extra space between keyword and paren");
611 # try to detect "func (x)" but not "if (x)" or
612 # "#define foo (x)" or "int (*func)();"
615 # strip off all keywords on the line
616 s/\b(for|if|while|switch|return|case|sizeof)\s\(/XXX(/g;
618 s/^#define\s+\w+\s+\(/XXX(/;
619 # do not match things like "void (*f)();"
620 # or "typedef void (func_t)();"
622 s/\b($typename|void)\s+\(+/XXX(/og;
624 err
("extra space between function name and left paren");
628 # try to detect "int foo(x)", but not "extern int foo(x);"
629 # XXX - this still trips over too many legitimate things,
630 # like "int foo(x,\n\ty);"
631 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01)*$/ &&
632 # !/^(extern|static)\b/) {
633 # err("return type of function not on separate line");
635 # this is a close approximation
636 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\x01)*$/ &&
637 !/^(extern|static)\b/) {
638 err
("return type of function not on separate line");
641 err
("#define followed by space instead of tab");
643 if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
644 err
("unparenthesized return expression");
646 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
647 err
("unparenthesized sizeof expression");
650 err
("whitespace after left paren");
652 # allow "for" statements to have empty "continue" clauses
653 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
654 err
("whitespace before right paren");
656 if (/^\s*\(void\)[^ ]/) {
657 err
("missing space after (void) cast");
659 if (/\S\{/ && !/\{\{/) {
660 err
("missing space before left brace");
662 if ($in_function && /^\s+{/ &&
663 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
664 err
("left brace starting a line");
666 if (/}(else|while)/) {
667 err
("missing space after right brace");
669 if (/}\s\s+(else|while)/) {
670 err
("extra space after right brace");
672 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
673 err
("obsolete use of VOID or STATIC");
675 if (/\b$typename\*/o) {
676 err
("missing space between type name and *");
679 err
("preprocessor statement not in column 1");
682 err
("blank after preprocessor #");
684 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
685 err
("don't use boolean ! with comparison functions");
689 # We completely ignore, for purposes of indentation:
690 # * lines outside of functions
691 # * preprocessor lines
693 if ($check_continuation && $in_function && !$in_cpp) {
697 # try to detect spaces after casts, but allow (e.g.)
698 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
699 # "int foo(int) __NORETURN;"
700 if ((/^\($typename( \*+)?\)\s/o ||
701 /\W\($typename( \*+)?\)\s/o) &&
702 !/sizeof\s*\($typename( \*)?\)\s/o &&
703 !/\($typename( \*+)?\)\s+=[^=]/o) {
704 err
("space after cast");
706 if (/\b$typename\s*\*\s/o &&
707 !/\b$typename\s*\*\s+const\b/o) {
708 err
("unary * followed by space");
711 if ($check_posix_types) {
712 # try to detect old non-POSIX types.
713 # POSIX requires all non-standard typedefs to end in _t,
714 # but historically these have been used.
715 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
716 err
("non-POSIX typedef $1 used: use $old2posix{$1} instead");
720 # cannot check this everywhere due to "struct {\n...\n} foo;"
721 if ($in_function && !$in_declaration &&
722 /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|\x01)*$/ &&
723 !/} (else|while)/ && !/}}/) {
724 err
("possible bad text following right brace");
726 # cannot check this because sub-blocks in
727 # the middle of code are ok
728 if ($in_function && /^\s+{/) {
729 err
("possible left brace starting a line");
733 if ($prev =~ /^\s*}$/) {
735 "else and right brace should be on same line");
742 err
("last line in file is blank");
748 # Continuation-line checking
750 # The rest of this file contains the code for the continuation checking
751 # engine. It's a pretty simple state machine which tracks the expression
752 # depth (unmatched '('s and '['s).
754 # Keep in mind that the argument to process_indent() has already been heavily
755 # processed; all comments have been replaced by control-A, and the contents of
756 # strings and character constants have been elided.
759 my $cont_in; # currently inside of a continuation
760 my $cont_off; # skipping an initializer or definition
761 my $cont_noerr; # suppress cascading errors
762 my $cont_start; # the line being continued
763 my $cont_base; # the base indentation
764 my $cont_first; # this is the first line of a statement
765 my $cont_multiseg; # this continuation has multiple segments
767 my $cont_special; # this is a C statement (if, for, etc.)
768 my $cont_macro; # this is a macro
769 my $cont_case; # this is a multi-line case
771 my @cont_paren; # the stack of unmatched ( and [s we've seen
784 # replace labels with tabs. Note that there may be multiple
789 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
790 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
792 while ($label =~ s/^([^\t]*)(\t+)//) {
793 $_ .= "\t" x
(length($2) + length($1) / 8);
795 $_ .= ("\t" x
(length($label) / 8)).$rest;
805 local $_ = $_[0]; # preserve the global $_
807 s/\x01//g; # No comments
808 s/\s+$//; # Strip trailing whitespace
810 return if (/^$/); # skip empty lines
812 # regexps used below; keywords taking (), macros, and continued cases
813 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
814 my $macro = '[A-Z_][A-Z_0-9]*\(';
815 my $case = 'case\b[^:]*$';
817 # skip over enumerations, array definitions, initializers, etc.
818 if ($cont_off <= 0 && !/^\s*$special/ &&
819 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*)){/ ||
820 (/^\s*{/ && $prev =~ /=\s*(?:\/\
*.*\
*\
/\s*)*$/))) {
822 $cont_off = tr/{/{/ - tr/}/}/;
826 $cont_off += tr/{/{/ - tr/}/}/;
834 err
("non-continuation indented 4 spaces");
835 $cont_noerr = 1; # stop reporting
837 $_ = delabel
($_); # replace labels with tabs
839 # check if the statement is complete
840 return if (/^\s*\}?$/);
841 return if (/^\s*\}?\s*else\s*\{?$/);
842 return if (/^\s*do\s*\{?$/);
844 return if (/}[,;]?$/);
846 # Allow macros on their own lines
847 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
849 # cases we don't deal with, generally non-kosher
851 err
("stuff after {");
855 # Get the base line, and set up the state machine
863 # certain things need special processing
864 $cont_special = /^\s*$special/?
1 : 0;
865 $cont_macro = /^\s*$macro/?
1 : 0;
866 $cont_case = /^\s*$case/?
1 : 0;
870 # Strings may be pulled back to an earlier (half-)tabstop
871 unless ($cont_noerr || /^$cont_base / ||
872 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
873 err_prefix
($cont_start,
874 "continuation should be indented 4 spaces");
878 my $rest = $_; # keeps the remainder of the line
881 # The split matches 0 characters, so that each 'special' character
882 # is processed separately. Parens and brackets are pushed and
883 # popped off the @cont_paren stack. For normal processing, we wait
884 # until a ; or { terminates the statement. "special" processing
885 # (if/for/while/switch) is allowed to stop when the stack empties,
886 # as is macro processing. Case statements are terminated with a :
887 # and an empty paren stack.
889 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
890 next if (length($_) == 0);
892 # rest contains the remainder of the line
893 my $rxp = "[^\Q$_\E]*\Q$_\E";
897 push @cont_paren, $_;
898 } elsif (/\)/ || /\]/) {
902 my $old = (pop @cont_paren);
903 if (!defined($old)) {
904 err
("unexpected '$cur'");
907 } elsif ($old ne $_) {
908 err
("'$cur' mismatched with '$old'");
914 # If the stack is now empty, do special processing
915 # for if/for/while/switch and macro statements.
917 next if (@cont_paren != 0);
919 if ($rest =~ /^\s*{?$/) {
923 if ($rest =~ /^\s*;$/) {
924 err
("empty if/for/while body ".
925 "not on its own line");
929 if (!$cont_first && $cont_multiseg == 1) {
930 err_prefix
($cont_start,
931 "multiple statements continued ".
932 "over multiple lines");
934 } elsif ($cont_multiseg == 0) {
937 # We've finished this section, start
938 # processing the next.
950 } elsif (!$cont_special) {
951 err
("unexpected ;") if (@cont_paren != 0);
952 if (!$cont_first && $cont_multiseg == 1) {
953 err_prefix
($cont_start,
954 "multiple statements continued ".
955 "over multiple lines");
957 } elsif ($cont_multiseg == 0) {
964 if ($rest =~ /^\s*special/) {
965 err
("if/for/while/switch not started ".
971 err
("{ while in parens/brackets") if (@cont_paren != 0);
972 err
("stuff after {") if ($rest =~ /[^\s}]/);
976 err
("} while in parens/brackets") if (@cont_paren != 0);
977 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
981 err
("stuff after }");
986 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
987 err
("stuff after multi-line case") if ($rest !~ /$^/);
993 # End of a statement or if/while/for loop. Reset
994 # cont_special and cont_macro based on the rest of the
996 $cont_special = ($rest =~ /^\s*$special/)?
1 : 0;
997 $cont_macro = ($rest =~ /^\s*$macro/)?
1 : 0;
1001 $cont_noerr = 0 if (!$cont_in);