]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/utils/cstyle
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 # Portions copyright 2017, Intel Corporation.
28 # @(#)cstyle 1.58 98/09/09 (from shannon)
29 #ident "%Z%%M% %I% %E% SMI"
31 # cstyle - check for some common stylistic errors.
33 # cstyle is a sort of "lint" for C coding style.
34 # It attempts to check for the style used in the
35 # kernel, sometimes known as "Bill Joy Normal Form".
37 # There's a lot this can't check for, like proper indentation
38 # of code blocks. There's also a lot more this could check for.
40 # A note to the non perl literate:
42 # perl regular expressions are pretty much like egrep
43 # regular expressions, with the following special symbols
45 # \s any space character
46 # \S any non-space character
47 # \w any "word" character [a-zA-Z0-9_]
48 # \W any non-word character
51 # \b word boundary (between \w and \W)
52 # \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-separated 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|BOOL)';
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;
210 printf $fmt, $filename, $., $error, $out;
218 printf $fmt, $filename, $. - 1, $error, $prev;
225 my ($fn, $filehandle) = @_;
226 $filename = $fn; # share it globally
232 my $in_header_comment = 0;
233 my $comment_done = 0;
234 my $in_warlock_comment = 0;
236 my $in_function_header = 0;
237 my $in_declaration = 0;
244 my ($okmsg, $comment_prefix);
250 line: while (<$filehandle>) {
251 s/\r?\n$//; # strip return and newline
253 # save the original line, then remove all text from within
254 # double or single quotes, we do not want to check such text.
259 # C allows strings to be continued with a backslash at the end of
260 # the line. We translate that into a quoted string on the previous
261 # line followed by an initial quote on the next line.
263 # (we assume that no-one will use backslash-continuation with character
266 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
269 # normal strings and characters
271 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
272 s/"([^\\"]|\\.)*"/\"\"/g;
275 # detect string continuation
277 if ($nocheck || $in_comment) {
281 # Now that all full strings are replaced with "", we check
282 # for unfinished strings continuing onto the next line.
285 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
286 s/^("")*"([^\\"]|\\.)*\\$/""/);
290 # figure out if we are in a cpp directive
292 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
293 $next_in_cpp = $in_cpp && /\\$/; # only if continued
295 # strip off trailing backslashes, which appear in long macros
298 # an /* END CSTYLED */ comment ends a no-check block.
300 if (/\/\* *END *CSTYLED *\*\//) {
308 # a /*CSTYLED*/ comment indicates that the next line is ok.
315 if (/\/\* *CSTYLED.*\*\//) {
316 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
325 # check length of line.
326 # first, a quick check to see if there is any chance of being too long.
327 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
328 # yes, there is a chance.
329 # replace tabs with spaces and check again.
332 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
333 if (length($eline) > 80) {
334 # allow long line if it is user visible string
335 # find if line start from " or L" and ends
336 # with " + 2 optional characters
337 # (these characters can be i.e. '");' '" \' or '",' etc
...)
338 if($eline =~ /^ *L?".*"[^"]{0,2}$/) {
339 # check if entire line is one string literal
340 $eline =~ s/^ *L?"//;
341 $eline =~ s/"[^"]{0,2}$//;
343 if($eline =~ /[^\\]"|[^\\](\\\\)+"/) {
344 err
("line > 80 characters");
347 err
("line > 80 characters");
352 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
353 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
354 s/[^()]//g; # eliminate all non-parens
355 $note_level += s/\(//g - length; # update paren nest level
359 # a /* BEGIN CSTYLED */ comment starts a no-check block.
360 if (/\/\
* *BEGIN *CSTYLED
*\
*\
//) {
364 # a /*CSTYLED*/ comment indicates that the next line is ok.
365 if (/\/\
* *CSTYLED
.*\
*\
//) {
366 /^.*\/\
* *CSTYLED
*(.*) *\
*\
/.*$/;
370 if (/\/\
/ *CSTYLED/) {
371 /^.*\/\
/ *CSTYLED *(.*)$/;
376 # universal checks; apply to everything
378 err
("spaces between tabs");
381 err
("tabs between spaces");
384 err
("space or tab at end of line");
386 if (/[^ \t(]\/\
*/ && !/\w\
(\
/\*.*\*\/\
);/) {
387 err
("comment preceded by non-blank");
390 # is this the beginning or ending of a function?
391 # (not if "struct foo\n{\n")
392 if (/^{$/ && $prev =~ /\)\s*(const\s*)?(\/\
*.*\
*\
/\s*)?\\?$/) {
395 $in_function_header = 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
409 $in_function_header = 1;
412 if ($in_warlock_comment && /\*\//) {
413 $in_warlock_comment = 0;
418 # a blank line terminates the declarations within a function.
419 # XXX - but still a problem in sub-blocks.
420 if ($in_declaration && /^$/) {
426 $in_header_comment = 0;
429 # does this looks like the start of a block comment?
430 if (/$hdr_comment_start/) {
432 err
("block comment not indented by tabs");
436 $comment_prefix = $1;
437 if ($comment_prefix eq "") {
438 $in_header_comment = 1;
443 # are we still in the block comment?
445 if (/^$comment_prefix \*\/$/) {
449 err
("improper block comment close")
450 unless ($ignore_hdr_comment && $in_header_comment);
451 } elsif (!/^$comment_prefix \*[ \t]/ &&
452 !/^$comment_prefix \*$/) {
453 err
("improper block comment")
454 unless ($ignore_hdr_comment && $in_header_comment);
458 if ($in_header_comment && $ignore_hdr_comment) {
463 # check for errors that might occur in comments and in code.
465 # allow spaces to be used to draw pictures in header and block comments.
466 if (/[^ ] / && !/".* .*"/ && !$in_header_comment && !$in_comment) {
467 err
("spaces instead of tabs");
469 if (/^ / && !/^ \*[ \t\/]/ && !/^ \
*$/ &&
470 (!/^ \w/ || $in_function != 0)) {
471 err
("indent by spaces instead of tabs");
473 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
474 err
("continuation line not indented by 4 spaces");
476 if (/$warlock_re/ && !/\*\//) {
477 $in_warlock_comment = 1;
481 if (/^\s*\/\
*./ && !/^\s
*\
/\*.*\*\// && !/$hdr_comment_start/) {
482 err
("improper first line of block comment");
485 if ($in_comment) { # still in comment, don't do further checks
490 if ((/[^(]\/\
*\S
/ || /^\
/\*\S/) &&
491 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
492 err
("missing blank after open comment");
494 if (/\S\*\/[^)]|\S\
*\
/$/ &&
495 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
496 err
("missing blank before close comment");
498 if (/\/\
/\S/) { # C++ comments
499 err
("missing blank after start comment");
501 # check for unterminated single line comments, but allow them when
502 # they are used to comment out the argument list of a function
504 if (/\S.*\/\
*/ && !/\S
.*\
/\*.*\*\// && !/\
(\
/\*/) {
505 err
("unterminated single line comment");
508 if (/^(#else|#endif|#include)(.*)$/) {
513 # Enforce ANSI rules for #else and #endif: no noncomment
514 # identifiers are allowed after #endif or #else. Allow
515 # C++ comments since they seem to be a fact of life.
516 if ((($1 eq "#endif") || ($1 eq "#else")) &&
518 (!($clause =~ /^\s+\/\
*.*\
*\
/$/)) &&
519 (!($clause =~ /^\s+\/\
/.*$/))) {
520 err
("non-comment text following " .
521 "$directive (or malformed $directive " .
529 # delete any comments and check everything else. Note that
530 # ".*?" is a non-greedy match, so that we don't get confused by
531 # multiple comments on the same line.
533 s/\/\*.*?\*\//\x01/g;
534 s/\/\/.*$/\x01/; # C++ comments
536 # delete any trailing whitespace; we have already checked for that.
539 # following checks do not apply to text in comments.
541 if (/[^ \t\+]\+[^\+=]/ || /[^\+]\+[^ \+=]/) {
542 err
("missing space around + operator");
544 if (/[^ \t]\+=/ || /\+=[^ ]/) {
545 err
("missing space around += operator");
547 if (/[^ \t\-]\-[^\->]/ && !/\(\w+\)\-\w/ && !/[\(\[]\-[\w \t]+[\)\],]/) {
548 err
("missing space before - operator");
550 if (/[^\-]\-[^ \-=>]/ && !/\(\-\w+\)/ &&
551 !/(return|case|=|>|<|\?|:|,|^[ \t]+)[ \t]+\-[\w\(]/ && !/(\([^\)]+\)|\[|\()\-[\w\(\]]/) {
552 err
("missing space after - operator");
554 if (/(return|case|=|\?|:|,|\[)[ \t]+\-[ \t]/ || /[\(\[]\-[ \t]/) {
555 err
("extra space after - operator");
558 err
("extra space before or after ++ operator");
561 err
("extra space before or after -- operator");
563 if (/[^ \t]\-=/ || /\-=[^ ]/) {
564 err
("missing space around -= operator");
566 if (/[^ \t][\%\/]/ || /[\
%\/][^ =]/ || /[\%\/]=[^ ]/) {
567 err
("missing space around one of operators: % %= / /=");
569 if (/[^ \t]\*=/ || /\*=[^ ]/) {
570 err
("missing space around *= operator");
572 if (/[^ \t\(\)\*\[]\*/) {
573 err
("missing space before * operator");
575 if (/\*[^ =\*\w\(,]/ && !/\(.+ \*+\)/ && !/\*\[\]/ &&
576 !/\*\-\-\w/ && !/\*\+\+\w/ && !/\*\)/) {
577 err
("missing space after * operator");
579 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
580 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
581 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
582 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
583 err
("missing space around relational operator");
585 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
586 (/[^-+*\/&|^%!<>=\s
]=[^=]/ && !/[^-+*\
/&|^%!<>=\s]=$/) ||
587 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
588 # XXX - should only check this for C++ code
589 # XXX - there are probably other forms that should be allowed
590 if (!/\soperator=/) {
591 err
("missing space around assignment operator");
594 if (/[,;]\S/ && !/\bfor \(;;\)/) {
595 err
("comma or semicolon followed by non-blank");
597 # allow "for" statements to have empty "while" clauses
598 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
599 err
("comma or semicolon preceded by blank");
601 if (/^\s*(&&|\|\|)/) {
602 err
("improper boolean continuation");
604 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
605 err
("more than one space around boolean operator");
607 if (/\b(for|if|while|switch|return|case)\(/) {
608 err
("missing space between keyword and paren");
610 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
611 # multiple "case" and "sizeof" allowed
612 err
("more than one keyword on line");
614 if (/\b(for|if|while|switch|return|case)\s\s+\(/ &&
616 err
("extra space between keyword and paren");
618 # try to detect "func (x)" but not "if (x)" or
619 # "#define foo (x)" or "int (*func)();"
622 # strip off all keywords on the line
623 s/\b(for|if|while|switch|return|case)\s\(/XXX(/g;
624 s/\b(sizeof|typeof|__typeof__)\s*\(/XXX(/g;
626 s/^#define\s+\w+\s+\(/XXX(/;
627 # do not match things like "void (*f)();"
628 # or "typedef void (func_t)();"
630 s/\b($typename|void)\s+\(+/XXX(/og;
631 s/\btypedef\s($typename|void)\s+\(+/XXX(/og;
632 # do not match "__attribute__ ((format (...)))"
633 s/\b__attribute__\s*\(\(format\s*\(/__attribute__((XXX(/g;
635 err
("extra space between function name and left paren");
639 # try to detect "int foo(x)", but not "extern int foo(x);"
640 # XXX - this still trips over too many legitimate things,
641 # like "int foo(x,\n\ty);"
642 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01)*$/ &&
643 # !/^(extern|static)\b/) {
644 # err("return type of function not on separate line");
646 # this is a close approximation
647 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\x01)*$/ &&
648 !/^(extern|static)\b/) {
649 err
("return type of function not on separate line");
651 if (/^#define\t/ || /^#ifdef\t/ || /^#ifndef\t/) {
652 err
("#define/ifdef/ifndef followed by tab instead of space");
654 if (/^#define\s\s+/ || /^#ifdef\s\s+/ || /^#ifndef\s\s+/) {
655 err
("#define/ifdef/ifndef followed by more than one space");
657 # AON C-style doesn't require this.
658 #if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
659 # err("unparenthesized return expression");
661 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
662 err
("unparenthesized sizeof expression");
664 if (/\b(sizeof|typeof)\b/ && /\b(sizeof|typeof)\s+\(.*\)/) {
665 err
("spaces between sizeof/typeof expression and paren");
668 err
("whitespace after left paren");
670 # allow "for" statements to have empty "continue" clauses
671 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
672 err
("whitespace before right paren");
674 if (/^\s*\(void\)[^ ]/) {
675 err
("missing space after (void) cast");
677 if (/\S\{/ && !/\{\{/ && !/\(struct \w+\)\{/) {
678 err
("missing space before left brace");
680 if ($in_function && /^\s+{/ &&
681 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
682 err
("left brace starting a line");
684 if (/}(else|while)/) {
685 err
("missing space after right brace");
687 if (/}\s\s+(else|while)/) {
688 err
("extra space after right brace");
690 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
691 err
("obsolete use of VOID or STATIC");
693 if (/\b($typename|void)\*/o) {
694 err
("missing space between type name and *");
697 err
("preprocessor statement not in column 1");
700 err
("blank after preprocessor #");
702 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
703 err
("don't use boolean ! with comparison functions");
705 if (/^\S+\([\S\s]*\)\s*{/) {
706 err
("brace of function definition not at beginning of line");
708 if (/static\s+\S+\s*=\s*(0|NULL)\s*;/) {
709 err
("static variable initialized with 0 or NULL");
711 if (/typedef[\S\s]+\*\s*\w+\s*;/) {
712 err
("typedefed pointer type");
714 if (/unsigned\s+int\s/) {
715 err
("'unsigned int' instead of just 'unsigned'");
717 if (/long\s+long\s+int\s/) {
718 err
("'long long int' instead of just 'long long'");
719 } elsif (/long\s+int\s/) {
720 err
("'long int' instead of just 'long'");
724 # We completely ignore, for purposes of indentation:
725 # * lines outside of functions
726 # * preprocessor lines
728 if ($check_continuation && $in_function && !$in_cpp) {
732 # try to detect spaces after casts, but allow (e.g.)
733 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
734 # "int foo(int) __NORETURN;"
735 if ((/^\($typename( \*+)?\)\s/o ||
736 /\W\($typename( \*+)?\)\s/o) &&
737 !/sizeof\($typename( \*)?\)\s/o &&
738 !/\($typename( \*+)?\)\s+=[^=]/o) {
739 err
("space after cast");
741 if (/\b($typename|void)\s*\*\s/o &&
742 !/\b($typename|void)\s*\*\s+const\b/o) {
743 err
("unary * followed by space");
746 if ($check_posix_types) {
747 # try to detect old non-POSIX types.
748 # POSIX requires all non-standard typedefs to end in _t,
749 # but historically these have been used.
750 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
751 err
("non-POSIX typedef $1 used: use $old2posix{$1} instead");
755 # cannot check this everywhere due to "struct {\n...\n} foo;"
756 if ($in_function && !$in_declaration &&
757 /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|\x01)*$/ &&
758 !/} (else|while)/ && !/}}/) {
759 err
("possible bad text following right brace");
761 # cannot check this because sub-blocks in
762 # the middle of code are ok
763 if ($in_function && /^\s+{/) {
764 err
("possible left brace starting a line");
768 if ($prev =~ /^\s*}$/) {
770 "else and right brace should be on same line");
777 err
("last line in file is blank");
783 # Continuation-line checking
785 # The rest of this file contains the code for the continuation checking
786 # engine. It's a pretty simple state machine which tracks the expression
787 # depth (unmatched '('s and '['s).
789 # Keep in mind that the argument to process_indent() has already been heavily
790 # processed; all comments have been replaced by control-A, and the contents of
791 # strings and character constants have been elided.
794 my $cont_in; # currently inside of a continuation
795 my $cont_off; # skipping an initializer or definition
796 my $cont_noerr; # suppress cascading errors
797 my $cont_start; # the line being continued
798 my $cont_base; # the base indentation
799 my $cont_first; # this is the first line of a statement
800 my $cont_multiseg; # this continuation has multiple segments
802 my $cont_special; # this is a C statement (if, for, etc.)
803 my $cont_macro; # this is a macro
804 my $cont_case; # this is a multi-line case
806 my @cont_paren; # the stack of unmatched ( and [s we've seen
819 # replace labels with tabs. Note that there may be multiple
824 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
825 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
827 while ($label =~ s/^([^\t]*)(\t+)//) {
828 $_ .= "\t" x
(length($2) + length($1) / 8);
830 $_ .= ("\t" x
(length($label) / 8)).$rest;
840 local $_ = $_[0]; # preserve the global $_
842 s/\x01//g; # No comments
843 s/\s+$//; # Strip trailing whitespace
845 return if (/^$/); # skip empty lines
847 # regexps used below; keywords taking (), macros, and continued cases
848 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
849 my $macro = '[A-Z_][A-Z_0-9]*\(';
850 my $case = 'case\b[^:]*$';
852 # skip over enumerations, array definitions, initializers, etc.
853 if ($cont_off <= 0 && !/^\s*$special/ &&
854 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*))\{/ ||
855 (/^\s*{/ && $prev =~ /=\s*(?:\/\
*.*\
*\
/\s*)*$/))) {
857 $cont_off = tr/{/{/ - tr/}/}/;
861 $cont_off += tr/{/{/ - tr/}/}/;
869 err
("non-continuation indented 4 spaces");
870 $cont_noerr = 1; # stop reporting
872 $_ = delabel
($_); # replace labels with tabs
874 # check if the statement is complete
875 return if (/^\s*\}?$/);
876 return if (/^\s*\}?\s*else\s*\{?$/);
877 return if (/^\s*do\s*\{?$/);
879 return if (/}[,;]?$/);
881 # Allow macros on their own lines
882 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
884 # cases we don't deal with, generally non-kosher
886 err
("stuff after {");
890 # Get the base line, and set up the state machine
898 # certain things need special processing
899 $cont_special = /^\s*$special/?
1 : 0;
900 $cont_macro = /^\s*$macro/?
1 : 0;
901 $cont_case = /^\s*$case/?
1 : 0;
905 # Strings may be pulled back to an earlier (half-)tabstop
906 unless ($cont_noerr || /^$cont_base / ||
907 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
908 err_prefix
($cont_start,
909 "continuation should be indented 4 spaces");
913 my $rest = $_; # keeps the remainder of the line
916 # The split matches 0 characters, so that each 'special' character
917 # is processed separately. Parens and brackets are pushed and
918 # popped off the @cont_paren stack. For normal processing, we wait
919 # until a ; or { terminates the statement. "special" processing
920 # (if/for/while/switch) is allowed to stop when the stack empties,
921 # as is macro processing. Case statements are terminated with a :
922 # and an empty paren stack.
924 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
925 next if (length($_) == 0);
927 # rest contains the remainder of the line
928 my $rxp = "[^\Q$_\E]*\Q$_\E";
932 push @cont_paren, $_;
933 } elsif (/\)/ || /\]/) {
937 my $old = (pop @cont_paren);
938 if (!defined($old)) {
939 err
("unexpected '$cur'");
942 } elsif ($old ne $_) {
943 err
("'$cur' mismatched with '$old'");
949 # If the stack is now empty, do special processing
950 # for if/for/while/switch and macro statements.
952 next if (@cont_paren != 0);
954 if ($rest =~ /^\s*{?$/) {
958 if ($rest =~ /^\s*;$/) {
959 err
("empty if/for/while body ".
960 "not on its own line");
964 if (!$cont_first && $cont_multiseg == 1) {
965 err_prefix
($cont_start,
966 "multiple statements continued ".
967 "over multiple lines");
969 } elsif ($cont_multiseg == 0) {
972 # We've finished this section, start
973 # processing the next.
985 } elsif (!$cont_special) {
986 err
("unexpected ;") if (@cont_paren != 0);
987 if (!$cont_first && $cont_multiseg == 1) {
988 err_prefix
($cont_start,
989 "multiple statements continued ".
990 "over multiple lines");
992 } elsif ($cont_multiseg == 0) {
999 if ($rest =~ /^\s*special/) {
1000 err
("if/for/while/switch not started ".
1006 err
("{ while in parens/brackets") if (@cont_paren != 0);
1007 err
("stuff after {") if ($rest =~ /[^\s}]/);
1011 err
("} while in parens/brackets") if (@cont_paren != 0);
1012 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
1013 if ($rest =~ /^$/) {
1014 err
("unexpected }");
1016 err
("stuff after }");
1021 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
1022 err
("stuff after multi-line case") if ($rest !~ /$^/);
1028 # End of a statement or if/while/for loop. Reset
1029 # cont_special and cont_macro based on the rest of the
1031 $cont_special = ($rest =~ /^\s*$special/)?
1 : 0;
1032 $cont_macro = ($rest =~ /^\s*$macro/)?
1 : 0;
1036 $cont_noerr = 0 if (!$cont_in);