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 2016 Nexenta Systems, Inc.
24 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
27 # @(#)cstyle 1.58 98/09/09 (from shannon)
28 #ident "%Z%%M% %I% %E% SMI"
30 # cstyle - check for some common stylistic errors.
32 # cstyle is a sort of "lint" for C coding style.
33 # It attempts to check for the style used in the
34 # kernel, sometimes known as "Bill Joy Normal Form".
36 # There's a lot this can't check for, like proper indentation
37 # of code blocks. There's also a lot more this could check for.
39 # A note to the non perl literate:
41 # perl regular expressions are pretty much like egrep
42 # regular expressions, with the following special symbols
44 # \s any space character
45 # \S any non-space character
46 # \w any "word" character [a-zA-Z0-9_]
47 # \W any non-word character
50 # \b word boundary (between \w and \W)
51 # \B non-word boundary
61 "usage: cstyle [-cghpvCP] [-o constructs] file ...
62 -c check continuation indentation inside functions
63 -g print github actions' workflow commands
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-separated list of optional constructs:
71 doxygen allow doxygen-style block comments (/** /*!)
72 splint allow splint-style lint comments (/*@ ... @*/)
77 if (!getopts
("cgho:pvCP", \
%opts)) {
82 my $check_continuation = $opts{'c'};
83 my $github_workflow = $opts{'g'} || $ENV{'CI'};
84 my $heuristic = $opts{'h'};
85 my $picky = $opts{'p'};
86 my $verbose = $opts{'v'};
87 my $ignore_hdr_comment = $opts{'C'};
88 my $check_posix_types = $opts{'P'};
90 my $doxygen_comments = 0;
91 my $splint_comments = 0;
93 if (defined($opts{'o'})) {
94 for my $x (split /,/, $opts{'o'}) {
95 if ($x eq "doxygen") {
96 $doxygen_comments = 1;
97 } elsif ($x eq "splint") {
100 print "cstyle: unrecognized construct \"$x\"\n";
107 my ($filename, $line, $prev); # shared globals
110 my $hdr_comment_start;
113 $fmt = "%s: %d: %s\n%s\n";
115 $fmt = "%s: %d: %s\n";
118 if ($doxygen_comments) {
119 # doxygen comments look like "/*!" or "/**"; allow them.
120 $hdr_comment_start = qr/^\s*\/\
*[\
!\
*]?
$/;
122 $hdr_comment_start = qr/^\s*\/\
*$/;
125 # Note, following must be in single quotes so that \s and \w work right.
126 my $typename = '(int|char|short|long|unsigned|float|double' .
127 '|\w+_t|struct\s+\w+|union\s+\w+|FILE)';
129 # mapping of old types to POSIX compatible types
131 'unchar' => 'uchar_t',
132 'ushort' => 'ushort_t',
134 'ulong' => 'ulong_t',
136 'u_short' => 'ushort_t',
137 'u_long' => 'ulong_t',
138 'u_char' => 'uchar_t',
142 my $lint_re = qr/\/\
*(?
:
143 ARGSUSED
[0-9]*|NOTREACHED
|LINTLIBRARY
|VARARGS
[0-9]*|
144 CONSTCOND
|CONSTANTCOND
|CONSTANTCONDITION
|EMPTY
|
145 FALLTHRU
|FALLTHROUGH
|LINTED
.*?
|PRINTFLIKE
[0-9]*|
146 PROTOLIB
[0-9]*|SCANFLIKE
[0-9]*|CSTYLED
.*?
149 my $splint_re = qr/\/\
*@
.*?@\
*\
//x;
151 my $warlock_re = qr/\/\
*\s
*(?
:
152 VARIABLES\ PROTECTED\ BY
|
153 MEMBERS\ PROTECTED\ BY
|
154 ALL\ MEMBERS\ PROTECTED\ BY
|
155 READ
-ONLY\ VARIABLES
:|
157 VARIABLES\ READABLE\ WITHOUT\ LOCK
:|
158 MEMBERS\ READABLE\ WITHOUT\ LOCK
:|
160 LOCK\ UNNEEDED\ BECAUSE
|
162 LOCK\ HELD\ ON\ ENTRY
:|
163 READ\ LOCK\ HELD\ ON\ ENTRY
:|
164 WRITE\ LOCK\ HELD\ ON\ ENTRY
:|
165 LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
166 READ\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
167 WRITE\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT
:|
168 LOCK\ RELEASED\ AS\ SIDE\ EFFECT
:|
169 LOCK\ UPGRADED\ AS\ SIDE\ EFFECT
:|
170 LOCK\ DOWNGRADED\ AS\ SIDE\ EFFECT
:|
171 FUNCTIONS\ CALLED\ THROUGH\ POINTER
|
172 FUNCTIONS\ CALLED\ THROUGH\ MEMBER
|
176 my $err_stat = 0; # exit status
179 foreach my $arg (@ARGV) {
180 my $fh = new IO
::File
$arg, "r";
182 printf "%s: can not open\n", $arg;
189 &cstyle
("<stdin>", *STDIN
);
193 my $no_errs = 0; # set for CSTYLED-protected lines
199 printf $fmt, $filename, $., $error, $line;
201 printf $fmt, $filename, $., $error;
203 if ($github_workflow) {
204 printf "::error file=%s,line=%s::%s\n", $filename, $., $error;
211 my ($prevline, $error) = @_;
212 my $out = $prevline."\n".$line;
215 printf $fmt, $filename, $., $error, $out;
217 printf $fmt, $filename, $., $error;
227 printf $fmt, $filename, $. - 1, $error, $prev;
229 printf $fmt, $filename, $. - 1, $error;
237 my ($fn, $filehandle) = @_;
238 $filename = $fn; # share it globally
244 my $in_header_comment = 0;
245 my $comment_done = 0;
246 my $in_warlock_comment = 0;
248 my $in_function_header = 0;
249 my $function_header_full_indent = 0;
250 my $in_declaration = 0;
257 my ($okmsg, $comment_prefix);
263 line
: while (<$filehandle>) {
264 s/\r?\n$//; # strip return and newline
266 # save the original line, then remove all text from within
267 # double or single quotes, we do not want to check such text.
272 # C allows strings to be continued with a backslash at the end of
273 # the line. We translate that into a quoted string on the previous
274 # line followed by an initial quote on the next line.
276 # (we assume that no-one will use backslash-continuation with character
279 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
282 # normal strings and characters
284 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
285 s/"([^\\"]|\\.)*"/\"\"/g;
288 # detect string continuation
290 if ($nocheck || $in_comment) {
294 # Now that all full strings are replaced with "", we check
295 # for unfinished strings continuing onto the next line.
298 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
299 s/^("")*"([^\\"]|\\.)*\\$/""/);
303 # figure out if we are in a cpp directive
305 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
306 $next_in_cpp = $in_cpp && /\\$/; # only if continued
308 # strip off trailing backslashes, which appear in long macros
311 # an /* END CSTYLED */ comment ends a no-check block.
313 if (/\/\
* *END *CSTYLED
*\
*\
//) {
321 # a /*CSTYLED*/ comment indicates that the next line is ok.
328 if (/\/\
* *CSTYLED
.*\
*\
//) {
329 /^.*\/\
* *CSTYLED
*(.*) *\
*\
/.*$/;
338 # check length of line.
339 # first, a quick check to see if there is any chance of being too long.
340 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
341 # yes, there is a chance.
342 # replace tabs with spaces and check again.
345 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
346 if (length($eline) > 80) {
347 err
("line > 80 characters");
351 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
352 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
353 s/[^()]//g; # eliminate all non-parens
354 $note_level += s/\(//g - length; # update paren nest level
358 # a /* BEGIN CSTYLED */ comment starts a no-check block.
359 if (/\/\
* *BEGIN *CSTYLED
*\
*\
//) {
363 # a /*CSTYLED*/ comment indicates that the next line is ok.
364 if (/\/\
* *CSTYLED
.*\
*\
//) {
365 /^.*\/\
* *CSTYLED
*(.*) *\
*\
/.*$/;
369 if (/\/\
/ *CSTYLED/) {
370 /^.*\/\
/ *CSTYLED *(.*)$/;
375 # universal checks; apply to everything
377 err
("spaces between tabs");
380 err
("tabs between spaces");
383 err
("space or tab at end of line");
385 if (/[^ \t(]\/\
*/ && !/\w\
(\
/\*.*\*\/\
);/) {
386 err
("comment preceded by non-blank");
389 # is this the beginning or ending of a function?
390 # (not if "struct foo\n{\n")
391 if (/^\{$/ && $prev =~ /\)\s*(const\s*)?(\/\
*.*\
*\
/\s*)?\\?$/) {
394 $in_function_header = 0;
395 $function_header_full_indent = 0;
399 if (/^\}\s*(\/\
*.*\
*\
/\s*)*$/) {
400 if ($prev =~ /^\s*return\s*;/) {
401 err_prev
("unneeded return at end of function");
404 reset_indent
(); # we don't check between functions
408 if ($in_function_header && ! /^ (\w|\.)/ ) {
409 if (/^\{\}$/ # empty functions
410 || /;/ #run function with multiline arguments
411 || /#/ #preprocessor commands
412 || /^[^\s\\]*\(.*\)$/ #functions without ; at the end
413 || /^$/ #function declaration can't have empty line
415 $in_function_header = 0;
416 $function_header_full_indent = 0;
417 } elsif ($prev =~ /^__attribute__/) { #__attribute__((*))
418 $in_function_header = 0;
419 $function_header_full_indent = 0;
422 } elsif ($picky && ! (/^\t/ && $function_header_full_indent != 0)) {
424 err
("continuation line should be indented by 4 spaces");
429 # If this matches something of form "foo(", it's probably a function
430 # definition, unless it ends with ") bar;", in which case it's a declaration
431 # that uses a macro to generate the type.
433 if (/^\w+\(/ && !/\) \w+;/) {
434 $in_function_header = 1;
436 $function_header_full_indent = 1;
439 if ($in_function_header && /^\{$/) {
440 $in_function_header = 0;
441 $function_header_full_indent = 0;
444 if ($in_function_header && /\);$/) {
445 $in_function_header = 0;
446 $function_header_full_indent = 0;
448 if ($in_function_header && /\{$/ ) {
450 err
("opening brace on same line as function header");
452 $in_function_header = 0;
453 $function_header_full_indent = 0;
458 if ($in_warlock_comment && /\*\//) {
459 $in_warlock_comment = 0;
464 # a blank line terminates the declarations within a function.
465 # XXX - but still a problem in sub-blocks.
466 if ($in_declaration && /^$/) {
472 $in_header_comment = 0;
475 # does this looks like the start of a block comment?
476 if (/$hdr_comment_start/) {
478 err
("block comment not indented by tabs");
482 $comment_prefix = $1;
483 if ($comment_prefix eq "") {
484 $in_header_comment = 1;
489 # are we still in the block comment?
491 if (/^$comment_prefix \*\/$/) {
495 err
("improper block comment close")
496 unless ($ignore_hdr_comment && $in_header_comment);
497 } elsif (!/^$comment_prefix \*[ \t]/ &&
498 !/^$comment_prefix \*$/) {
499 err
("improper block comment")
500 unless ($ignore_hdr_comment && $in_header_comment);
504 if ($in_header_comment && $ignore_hdr_comment) {
509 # check for errors that might occur in comments and in code.
511 # allow spaces to be used to draw pictures in all comments.
512 if (/[^ ] / && !/".* .*"/ && !$in_comment) {
513 err
("spaces instead of tabs");
515 if (/^ / && !/^ \*[ \t\/]/ && !/^ \
*$/ &&
516 (!/^ (\w|\.)/ || $in_function != 0)) {
517 err
("indent by spaces instead of tabs");
519 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
520 err
("continuation line not indented by 4 spaces");
522 if (/$warlock_re/ && !/\*\//) {
523 $in_warlock_comment = 1;
527 if (/^\s*\/\
*./ && !/^\s
*\
/\*.*\*\// && !/$hdr_comment_start/) {
528 err
("improper first line of block comment");
531 if ($in_comment) { # still in comment, don't do further checks
536 if ((/[^(]\/\
*\S
/ || /^\
/\*\S/) &&
537 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
538 err
("missing blank after open comment");
540 if (/\S\*\/[^)]|\S\
*\
/$/ &&
541 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
542 err
("missing blank before close comment");
544 if (/\/\
/\S/) { # C++ comments
545 err
("missing blank after start comment");
547 # check for unterminated single line comments, but allow them when
548 # they are used to comment out the argument list of a function
550 if (/\S.*\/\
*/ && !/\S
.*\
/\*.*\*\// && !/\
(\
/\*/) {
551 err
("unterminated single line comment");
554 if (/^(#else|#endif|#include)(.*)$/) {
559 # Enforce ANSI rules for #else and #endif: no noncomment
560 # identifiers are allowed after #endif or #else. Allow
561 # C++ comments since they seem to be a fact of life.
562 if ((($1 eq "#endif") || ($1 eq "#else")) &&
564 (!($clause =~ /^\s+\/\
*.*\
*\
/$/)) &&
565 (!($clause =~ /^\s+\/\
/.*$/))) {
566 err
("non-comment text following " .
567 "$directive (or malformed $directive " .
575 # delete any comments and check everything else. Note that
576 # ".*?" is a non-greedy match, so that we don't get confused by
577 # multiple comments on the same line.
579 s/\/\*.*?\*\//\x01/g;
580 s/\/\/.*$/\x01/; # C++ comments
582 # delete any trailing whitespace; we have already checked for that.
585 # following checks do not apply to text in comments.
587 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
588 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
589 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
590 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
591 err
("missing space around relational operator");
593 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
594 (/[^-+*\/&|^%!<>=\s
]=[^=]/ && !/[^-+*\
/&|^%!<>=\s]=$/) ||
595 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
596 # XXX - should only check this for C++ code
597 # XXX - there are probably other forms that should be allowed
598 if (!/\soperator=/) {
599 err
("missing space around assignment operator");
602 if (/[,;]\S/ && !/\bfor \(;;\)/) {
603 err
("comma or semicolon followed by non-blank");
605 # allow "for" statements to have empty "while" clauses
606 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
607 err
("comma or semicolon preceded by blank");
609 if (/^\s*(&&|\|\|)/) {
610 err
("improper boolean continuation");
612 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
613 err
("more than one space around boolean operator");
615 if (/\b(for|if|while|switch|sizeof|return|case)\(/) {
616 err
("missing space between keyword and paren");
618 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
619 # multiple "case" and "sizeof" allowed
620 err
("more than one keyword on line");
622 if (/\b(for|if|while|switch|sizeof|return|case)\s\s+\(/ &&
624 err
("extra space between keyword and paren");
626 # try to detect "func (x)" but not "if (x)" or
627 # "#define foo (x)" or "int (*func)();"
630 # strip off all keywords on the line
631 s/\b(for|if|while|switch|return|case|sizeof)\s\(/XXX(/g;
633 s/^#define\s+\w+\s+\(/XXX(/;
634 # do not match things like "void (*f)();"
635 # or "typedef void (func_t)();"
637 s/\b($typename|void)\s+\(+/XXX(/og;
639 err
("extra space between function name and left paren");
643 # try to detect "int foo(x)", but not "extern int foo(x);"
644 # XXX - this still trips over too many legitimate things,
645 # like "int foo(x,\n\ty);"
646 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01)*$/ &&
647 # !/^(extern|static)\b/) {
648 # err("return type of function not on separate line");
650 # this is a close approximation
651 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\x01)*$/ &&
652 !/^(extern|static)\b/) {
653 err
("return type of function not on separate line");
656 err
("#define followed by space instead of tab");
658 if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
659 err
("unparenthesized return expression");
661 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
662 err
("unparenthesized sizeof expression");
665 err
("whitespace after left paren");
667 # Allow "for" statements to have empty "continue" clauses.
668 # Allow right paren on its own line unless we're being picky (-p).
669 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/ && ($picky || !/^\s*\)/)) {
670 err
("whitespace before right paren");
672 if (/^\s*\(void\)[^ ]/) {
673 err
("missing space after (void) cast");
675 if (/\S\{/ && !/\{\{/) {
676 err
("missing space before left brace");
678 if ($in_function && /^\s+\{/ &&
679 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
680 err
("left brace starting a line");
682 if (/\}(else|while)/) {
683 err
("missing space after right brace");
685 if (/\}\s\s+(else|while)/) {
686 err
("extra space after right brace");
688 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
689 err
("obsolete use of VOID or STATIC");
691 if (/\b$typename\*/o) {
692 err
("missing space between type name and *");
695 err
("preprocessor statement not in column 1");
698 err
("blank after preprocessor #");
700 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
701 err
("don't use boolean ! with comparison functions");
705 # We completely ignore, for purposes of indentation:
706 # * lines outside of functions
707 # * preprocessor lines
709 if ($check_continuation && $in_function && !$in_cpp) {
713 # try to detect spaces after casts, but allow (e.g.)
714 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
715 # "int foo(int) __NORETURN;"
716 if ((/^\($typename( \*+)?\)\s/o ||
717 /\W\($typename( \*+)?\)\s/o) &&
718 !/sizeof\s*\($typename( \*)?\)\s/o &&
719 !/\($typename( \*+)?\)\s+=[^=]/o) {
720 err
("space after cast");
722 if (/\b$typename\s*\*\s/o &&
723 !/\b$typename\s*\*\s+const\b/o) {
724 err
("unary * followed by space");
727 if ($check_posix_types) {
728 # try to detect old non-POSIX types.
729 # POSIX requires all non-standard typedefs to end in _t,
730 # but historically these have been used.
731 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
732 err
("non-POSIX typedef $1 used: use $old2posix{$1} instead");
736 # cannot check this everywhere due to "struct {\n...\n} foo;"
737 if ($in_function && !$in_declaration &&
738 /\}./ && !/\}\s+=/ && !/\{.*\}[;,]$/ && !/\}(\s|\x01)*$/ &&
739 !/\} (else|while)/ && !/\}\}/) {
740 err
("possible bad text following right brace");
742 # cannot check this because sub-blocks in
743 # the middle of code are ok
744 if ($in_function && /^\s+\{/) {
745 err
("possible left brace starting a line");
749 if ($prev =~ /^\s*\}$/) {
751 "else and right brace should be on same line");
758 err
("last line in file is blank");
764 # Continuation-line checking
766 # The rest of this file contains the code for the continuation checking
767 # engine. It's a pretty simple state machine which tracks the expression
768 # depth (unmatched '('s and '['s).
770 # Keep in mind that the argument to process_indent() has already been heavily
771 # processed; all comments have been replaced by control-A, and the contents of
772 # strings and character constants have been elided.
775 my $cont_in; # currently inside of a continuation
776 my $cont_off; # skipping an initializer or definition
777 my $cont_noerr; # suppress cascading errors
778 my $cont_start; # the line being continued
779 my $cont_base; # the base indentation
780 my $cont_first; # this is the first line of a statement
781 my $cont_multiseg; # this continuation has multiple segments
783 my $cont_special; # this is a C statement (if, for, etc.)
784 my $cont_macro; # this is a macro
785 my $cont_case; # this is a multi-line case
787 my @cont_paren; # the stack of unmatched ( and [s we've seen
800 # replace labels with tabs. Note that there may be multiple
805 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
806 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
808 while ($label =~ s/^([^\t]*)(\t+)//) {
809 $_ .= "\t" x
(length($2) + length($1) / 8);
811 $_ .= ("\t" x
(length($label) / 8)).$rest;
821 local $_ = $_[0]; # preserve the global $_
823 s/\x01//g; # No comments
824 s/\s+$//; # Strip trailing whitespace
826 return if (/^$/); # skip empty lines
828 # regexps used below; keywords taking (), macros, and continued cases
829 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
830 my $macro = '[A-Z_][A-Z_0-9]*\(';
831 my $case = 'case\b[^:]*$';
833 # skip over enumerations, array definitions, initializers, etc.
834 if ($cont_off <= 0 && !/^\s*$special/ &&
835 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*))\{/ ||
836 (/^\s*\{/ && $prev =~ /=\s*(?:\/\
*.*\
*\
/\s*)*$/))) {
838 $cont_off = tr/{/{/ - tr/}/}/;
842 $cont_off += tr/{/{/ - tr/}/}/;
850 err
("non-continuation indented 4 spaces");
851 $cont_noerr = 1; # stop reporting
853 $_ = delabel
($_); # replace labels with tabs
855 # check if the statement is complete
856 return if (/^\s*\}?$/);
857 return if (/^\s*\}?\s*else\s*\{?$/);
858 return if (/^\s*do\s*\{?$/);
860 return if (/\}[,;]?$/);
862 # Allow macros on their own lines
863 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
865 # cases we don't deal with, generally non-kosher
867 err
("stuff after {");
871 # Get the base line, and set up the state machine
879 # certain things need special processing
880 $cont_special = /^\s*$special/?
1 : 0;
881 $cont_macro = /^\s*$macro/?
1 : 0;
882 $cont_case = /^\s*$case/?
1 : 0;
886 # Strings may be pulled back to an earlier (half-)tabstop
887 unless ($cont_noerr || /^$cont_base / ||
888 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
889 err_prefix
($cont_start,
890 "continuation should be indented 4 spaces");
894 my $rest = $_; # keeps the remainder of the line
897 # The split matches 0 characters, so that each 'special' character
898 # is processed separately. Parens and brackets are pushed and
899 # popped off the @cont_paren stack. For normal processing, we wait
900 # until a ; or { terminates the statement. "special" processing
901 # (if/for/while/switch) is allowed to stop when the stack empties,
902 # as is macro processing. Case statements are terminated with a :
903 # and an empty paren stack.
905 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
906 next if (length($_) == 0);
908 # rest contains the remainder of the line
909 my $rxp = "[^\Q$_\E]*\Q$_\E";
913 push @cont_paren, $_;
914 } elsif (/\)/ || /\]/) {
918 my $old = (pop @cont_paren);
919 if (!defined($old)) {
920 err
("unexpected '$cur'");
923 } elsif ($old ne $_) {
924 err
("'$cur' mismatched with '$old'");
930 # If the stack is now empty, do special processing
931 # for if/for/while/switch and macro statements.
933 next if (@cont_paren != 0);
935 if ($rest =~ /^\s*\{?$/) {
939 if ($rest =~ /^\s*;$/) {
940 err
("empty if/for/while body ".
941 "not on its own line");
945 if (!$cont_first && $cont_multiseg == 1) {
946 err_prefix
($cont_start,
947 "multiple statements continued ".
948 "over multiple lines");
950 } elsif ($cont_multiseg == 0) {
953 # We've finished this section, start
954 # processing the next.
966 } elsif (!$cont_special) {
967 err
("unexpected ;") if (@cont_paren != 0);
968 if (!$cont_first && $cont_multiseg == 1) {
969 err_prefix
($cont_start,
970 "multiple statements continued ".
971 "over multiple lines");
973 } elsif ($cont_multiseg == 0) {
980 if ($rest =~ /^\s*special/) {
981 err
("if/for/while/switch not started ".
987 err
("{ while in parens/brackets") if (@cont_paren != 0);
988 err
("stuff after {") if ($rest =~ /[^\s}]/);
992 err
("} while in parens/brackets") if (@cont_paren != 0);
993 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
997 err
("stuff after }");
1002 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
1003 err
("stuff after multi-line case") if ($rest !~ /$^/);
1009 # End of a statement or if/while/for loop. Reset
1010 # cont_special and cont_macro based on the rest of the
1012 $cont_special = ($rest =~ /^\s*$special/)?
1 : 0;
1013 $cont_macro = ($rest =~ /^\s*$macro/)?
1 : 0;
1017 $cont_noerr = 0 if (!$cont_in);