simd_stat: fix undefined CONFIG_KERNEL_MODE_NEON error on armel
[zfs.git] / scripts / cstyle.pl
blob123020b08127b42b7d9caafd854e463b8e05ccfe
1 #!/usr/bin/env perl
3 # CDDL HEADER START
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 https://opensource.org/licenses/CDDL-1.0.
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]
20 # CDDL HEADER END
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
48 # \d a digit [0-9]
49 # \D a non-digit
50 # \b word boundary (between \w and \W)
51 # \B non-word boundary
54 require 5.0;
55 use warnings;
56 use IO::File;
57 use Getopt::Std;
58 use strict;
60 my $usage =
61 "usage: cstyle [-cgpvP] file...
62 -c check continuation indentation inside functions
63 -g print github actions' workflow commands
64 -p perform some of the more picky checks
65 -v verbose
66 -P check for use of non-POSIX types
69 my %opts;
71 if (!getopts("cghpvCP", \%opts)) {
72 print $usage;
73 exit 2;
76 my $check_continuation = $opts{'c'};
77 my $github_workflow = $opts{'g'} || $ENV{'CI'};
78 my $picky = $opts{'p'};
79 my $verbose = $opts{'v'};
80 my $check_posix_types = $opts{'P'};
82 my ($filename, $line, $prev); # shared globals
84 my $fmt;
85 my $hdr_comment_start;
87 if ($verbose) {
88 $fmt = "%s: %d: %s\n%s\n";
89 } else {
90 $fmt = "%s: %d: %s\n";
93 $hdr_comment_start = qr/^\s*\/\*$/;
95 # Note, following must be in single quotes so that \s and \w work right.
96 my $typename = '(int|char|short|long|unsigned|float|double' .
97 '|\w+_t|struct\s+\w+|union\s+\w+|FILE)';
99 # mapping of old types to POSIX compatible types
100 my %old2posix = (
101 'unchar' => 'uchar_t',
102 'ushort' => 'ushort_t',
103 'uint' => 'uint_t',
104 'ulong' => 'ulong_t',
105 'u_int' => 'uint_t',
106 'u_short' => 'ushort_t',
107 'u_long' => 'ulong_t',
108 'u_char' => 'uchar_t',
109 'quad' => 'quad_t'
112 my $lint_re = qr/\/\*(?:
113 NOTREACHED|LINTLIBRARY|VARARGS[0-9]*|
114 CONSTCOND|CONSTANTCOND|CONSTANTCONDITION|EMPTY|
115 FALLTHRU|FALLTHROUGH|LINTED.*?|PRINTFLIKE[0-9]*|
116 PROTOLIB[0-9]*|SCANFLIKE[0-9]*|CSTYLED.*?
117 )\*\//x;
119 my $warlock_re = qr/\/\*\s*(?:
120 VARIABLES\ PROTECTED\ BY|
121 MEMBERS\ PROTECTED\ BY|
122 ALL\ MEMBERS\ PROTECTED\ BY|
123 READ-ONLY\ VARIABLES:|
124 READ-ONLY\ MEMBERS:|
125 VARIABLES\ READABLE\ WITHOUT\ LOCK:|
126 MEMBERS\ READABLE\ WITHOUT\ LOCK:|
127 LOCKS\ COVERED\ BY|
128 LOCK\ UNNEEDED\ BECAUSE|
129 LOCK\ NEEDED:|
130 LOCK\ HELD\ ON\ ENTRY:|
131 READ\ LOCK\ HELD\ ON\ ENTRY:|
132 WRITE\ LOCK\ HELD\ ON\ ENTRY:|
133 LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
134 READ\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
135 WRITE\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
136 LOCK\ RELEASED\ AS\ SIDE\ EFFECT:|
137 LOCK\ UPGRADED\ AS\ SIDE\ EFFECT:|
138 LOCK\ DOWNGRADED\ AS\ SIDE\ EFFECT:|
139 FUNCTIONS\ CALLED\ THROUGH\ POINTER|
140 FUNCTIONS\ CALLED\ THROUGH\ MEMBER|
141 LOCK\ ORDER:
142 )/x;
144 my $err_stat = 0; # exit status
146 if ($#ARGV >= 0) {
147 foreach my $arg (@ARGV) {
148 my $fh = new IO::File $arg, "r";
149 if (!defined($fh)) {
150 printf "%s: can not open\n", $arg;
151 } else {
152 &cstyle($arg, $fh);
153 close $fh;
156 } else {
157 &cstyle("<stdin>", *STDIN);
159 exit $err_stat;
161 my $no_errs = 0; # set for CSTYLED-protected lines
163 sub err($) {
164 my ($error) = @_;
165 unless ($no_errs) {
166 if ($verbose) {
167 printf $fmt, $filename, $., $error, $line;
168 } else {
169 printf $fmt, $filename, $., $error;
171 if ($github_workflow) {
172 printf "::error file=%s,line=%s::%s\n", $filename, $., $error;
174 $err_stat = 1;
178 sub err_prefix($$) {
179 my ($prevline, $error) = @_;
180 my $out = $prevline."\n".$line;
181 unless ($no_errs) {
182 if ($verbose) {
183 printf $fmt, $filename, $., $error, $out;
184 } else {
185 printf $fmt, $filename, $., $error;
187 $err_stat = 1;
191 sub err_prev($) {
192 my ($error) = @_;
193 unless ($no_errs) {
194 if ($verbose) {
195 printf $fmt, $filename, $. - 1, $error, $prev;
196 } else {
197 printf $fmt, $filename, $. - 1, $error;
199 $err_stat = 1;
203 sub cstyle($$) {
205 my ($fn, $filehandle) = @_;
206 $filename = $fn; # share it globally
208 my $in_cpp = 0;
209 my $next_in_cpp = 0;
211 my $in_comment = 0;
212 my $comment_done = 0;
213 my $in_warlock_comment = 0;
214 my $in_macro_call = 0;
215 my $in_function = 0;
216 my $in_function_header = 0;
217 my $function_header_full_indent = 0;
218 my $in_declaration = 0;
219 my $note_level = 0;
220 my $nextok = 0;
221 my $nocheck = 0;
223 my $in_string = 0;
225 my ($okmsg, $comment_prefix);
227 $line = '';
228 $prev = '';
229 reset_indent();
231 line: while (<$filehandle>) {
232 s/\r?\n$//; # strip return and newline
234 # save the original line, then remove all text from within
235 # double or single quotes, we do not want to check such text.
237 $line = $_;
240 # C allows strings to be continued with a backslash at the end of
241 # the line. We translate that into a quoted string on the previous
242 # line followed by an initial quote on the next line.
244 # (we assume that no-one will use backslash-continuation with character
245 # constants)
247 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
250 # normal strings and characters
252 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
253 s/"([^\\"]|\\.)*"/\"\"/g;
256 # detect string continuation
258 if ($nocheck || $in_comment) {
259 $in_string = 0;
260 } else {
262 # Now that all full strings are replaced with "", we check
263 # for unfinished strings continuing onto the next line.
265 $in_string =
266 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
267 s/^("")*"([^\\"]|\\.)*\\$/""/);
271 # figure out if we are in a cpp directive
273 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
274 $next_in_cpp = $in_cpp && /\\$/; # only if continued
276 # strip off trailing backslashes, which appear in long macros
277 s/\s*\\$//;
279 # an /* END CSTYLED */ comment ends a no-check block.
280 if ($nocheck) {
281 if (/\/\* *END *CSTYLED *\*\//) {
282 $nocheck = 0;
283 } else {
284 reset_indent();
285 next line;
289 # a /*CSTYLED*/ comment indicates that the next line is ok.
290 if ($nextok) {
291 if ($okmsg) {
292 err($okmsg);
294 $nextok = 0;
295 $okmsg = 0;
296 if (/\/\* *CSTYLED.*\*\//) {
297 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
298 $okmsg = $1;
299 $nextok = 1;
301 $no_errs = 1;
302 } elsif ($no_errs) {
303 $no_errs = 0;
306 # check length of line.
307 # first, a quick check to see if there is any chance of being too long.
308 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
309 # yes, there is a chance.
310 # replace tabs with spaces and check again.
311 my $eline = $line;
312 1 while $eline =~
313 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
314 if (length($eline) > 80) {
315 err("line > 80 characters");
319 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
320 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
321 s/[^()]//g; # eliminate all non-parens
322 $note_level += s/\(//g - length; # update paren nest level
323 next;
326 # a /* BEGIN CSTYLED */ comment starts a no-check block.
327 if (/\/\* *BEGIN *CSTYLED *\*\//) {
328 $nocheck = 1;
331 # a /*CSTYLED*/ comment indicates that the next line is ok.
332 if (/\/\* *CSTYLED.*\*\//) {
333 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
334 $okmsg = $1;
335 $nextok = 1;
337 if (/\/\/ *CSTYLED/) {
338 /^.*\/\/ *CSTYLED *(.*)$/;
339 $okmsg = $1;
340 $nextok = 1;
343 # universal checks; apply to everything
344 if (/\t +\t/) {
345 err("spaces between tabs");
347 if (/ \t+ /) {
348 err("tabs between spaces");
350 if (/\s$/) {
351 err("space or tab at end of line");
353 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) {
354 err("comment preceded by non-blank");
356 if (/ARGSUSED/) {
357 err("ARGSUSED directive");
360 # is this the beginning or ending of a function?
361 # (not if "struct foo\n{\n")
362 if (/^\{$/ && $prev =~ /\)\s*(const\s*)?(\/\*.*\*\/\s*)?\\?$/) {
363 $in_function = 1;
364 $in_declaration = 1;
365 $in_function_header = 0;
366 $function_header_full_indent = 0;
367 $prev = $line;
368 next line;
370 if (/^\}\s*(\/\*.*\*\/\s*)*$/) {
371 if ($prev =~ /^\s*return\s*;/) {
372 err_prev("unneeded return at end of function");
374 $in_function = 0;
375 reset_indent(); # we don't check between functions
376 $prev = $line;
377 next line;
379 if ($in_function_header && ! /^ (\w|\.)/ ) {
380 if (/^\{\}$/ # empty functions
381 || /;/ #run function with multiline arguments
382 || /#/ #preprocessor commands
383 || /^[^\s\\]*\(.*\)$/ #functions without ; at the end
384 || /^$/ #function declaration can't have empty line
386 $in_function_header = 0;
387 $function_header_full_indent = 0;
388 } elsif ($prev =~ /^__attribute__/) { #__attribute__((*))
389 $in_function_header = 0;
390 $function_header_full_indent = 0;
391 $prev = $line;
392 next line;
393 } elsif ($picky && ! (/^\t/ && $function_header_full_indent != 0)) {
395 err("continuation line should be indented by 4 spaces");
399 # If this looks like a top-level macro invocation, remember it so we
400 # don't mistake it for a function declaration below.
401 if (/^[A-Za-z_][A-Za-z_0-9]*\(/) {
402 $in_macro_call = 1;
406 # If this matches something of form "foo(", it's probably a function
407 # definition, unless it ends with ") bar;", in which case it's a declaration
408 # that uses a macro to generate the type.
410 if (!$in_macro_call && /^\w+\(/ && !/\) \w+;/) {
411 $in_function_header = 1;
412 if (/\($/) {
413 $function_header_full_indent = 1;
416 if ($in_function_header && /^\{$/) {
417 $in_function_header = 0;
418 $function_header_full_indent = 0;
419 $in_function = 1;
421 if ($in_function_header && /\);$/) {
422 $in_function_header = 0;
423 $function_header_full_indent = 0;
425 if ($in_function_header && /\{$/ ) {
426 if ($picky) {
427 err("opening brace on same line as function header");
429 $in_function_header = 0;
430 $function_header_full_indent = 0;
431 $in_function = 1;
432 next line;
435 if ($in_warlock_comment && /\*\//) {
436 $in_warlock_comment = 0;
437 $prev = $line;
438 next line;
441 # a blank line terminates the declarations within a function.
442 # XXX - but still a problem in sub-blocks.
443 if ($in_declaration && /^$/) {
444 $in_declaration = 0;
447 if ($comment_done) {
448 $in_comment = 0;
449 $comment_done = 0;
451 # does this looks like the start of a block comment?
452 if (/$hdr_comment_start/) {
453 if (!/^\t*\/\*/) {
454 err("block comment not indented by tabs");
456 $in_comment = 1;
457 /^(\s*)\//;
458 $comment_prefix = $1;
459 $prev = $line;
460 next line;
462 # are we still in the block comment?
463 if ($in_comment) {
464 if (/^$comment_prefix \*\/$/) {
465 $comment_done = 1;
466 } elsif (/\*\//) {
467 $comment_done = 1;
468 err("improper block comment close");
469 } elsif (!/^$comment_prefix \*[ \t]/ &&
470 !/^$comment_prefix \*$/) {
471 err("improper block comment");
475 # check for errors that might occur in comments and in code.
477 # allow spaces to be used to draw pictures in all comments.
478 if (/[^ ] / && !/".* .*"/ && !$in_comment) {
479 err("spaces instead of tabs");
481 if (/^ / && !/^ \*[ \t\/]/ && !/^ \*$/ &&
482 (!/^ (\w|\.)/ || $in_function != 0)) {
483 err("indent by spaces instead of tabs");
485 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
486 err("continuation line not indented by 4 spaces");
488 if (/$warlock_re/ && !/\*\//) {
489 $in_warlock_comment = 1;
490 $prev = $line;
491 next line;
493 if (/^\s*\/\*./ && !/^\s*\/\*.*\*\// && !/$hdr_comment_start/) {
494 err("improper first line of block comment");
497 if ($in_comment) { # still in comment, don't do further checks
498 $prev = $line;
499 next line;
502 if ((/[^(]\/\*\S/ || /^\/\*\S/) && !/$lint_re/) {
503 err("missing blank after open comment");
505 if (/\S\*\/[^)]|\S\*\/$/ && !/$lint_re/) {
506 err("missing blank before close comment");
508 # check for unterminated single line comments, but allow them when
509 # they are used to comment out the argument list of a function
510 # declaration.
511 if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
512 err("unterminated single line comment");
515 if (/^(#else|#endif|#include)(.*)$/) {
516 $prev = $line;
517 if ($picky) {
518 my $directive = $1;
519 my $clause = $2;
520 # Enforce ANSI rules for #else and #endif: no noncomment
521 # identifiers are allowed after #endif or #else. Allow
522 # C++ comments since they seem to be a fact of life.
523 if ((($1 eq "#endif") || ($1 eq "#else")) &&
524 ($clause ne "") &&
525 (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
526 (!($clause =~ /^\s+\/\/.*$/))) {
527 err("non-comment text following " .
528 "$directive (or malformed $directive " .
529 "directive)");
532 next line;
536 # delete any comments and check everything else. Note that
537 # ".*?" is a non-greedy match, so that we don't get confused by
538 # multiple comments on the same line.
540 s/\/\*.*?\*\//\x01/g;
541 s/\/\/(?:\s.*)?$/\x01/; # Valid C++ comments
543 # After stripping correctly spaced comments, check for (and strip) comments
544 # without a blank. By checking this after clearing out C++ comments that
545 # correctly have a blank, we guarantee URIs in a C++ comment will not cause
546 # an error.
547 if (s!//.*$!\x01!) { # C++ comments
548 err("missing blank after start comment");
551 # delete any trailing whitespace; we have already checked for that.
552 s/\s*$//;
554 # following checks do not apply to text in comments.
556 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
557 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
558 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
559 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
560 err("missing space around relational operator");
562 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
563 (/[^-+*\/&|^%!<>=\s]=[^=]/ && !/[^-+*\/&|^%!<>=\s]=$/) ||
564 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
565 # XXX - should only check this for C++ code
566 # XXX - there are probably other forms that should be allowed
567 if (!/\soperator=/) {
568 err("missing space around assignment operator");
571 if (/[,;]\S/ && !/\bfor \(;;\)/) {
572 err("comma or semicolon followed by non-blank");
574 # allow "for" statements to have empty "while" clauses
575 # allow macro invocations to have empty parameters
576 if (/\s[,;]/ && !/^[\t]+;$/ &&
577 !($in_macro_call || /^\s*for \([^;]*; ;[^;]*\)/)) {
578 err("comma or semicolon preceded by blank");
580 if (/^\s*(&&|\|\|)/) {
581 err("improper boolean continuation");
583 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
584 err("more than one space around boolean operator");
586 if (/\b(for|if|while|switch|sizeof|return|case)\(/) {
587 err("missing space between keyword and paren");
589 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
590 # multiple "case" and "sizeof" allowed
591 err("more than one keyword on line");
593 if (/\b(for|if|while|switch|sizeof|return|case)\s\s+\(/ &&
594 !/^#if\s+\(/) {
595 err("extra space between keyword and paren");
597 # try to detect "func (x)" but not "if (x)" or
598 # "#define foo (x)" or "int (*func)();"
599 if (/\w\s\(/) {
600 my $s = $_;
601 # strip off all keywords on the line
602 s/\b(for|if|while|switch|return|case|sizeof)\s\(/XXX(/g;
603 s/#elif\s\(/XXX(/g;
604 s/^#define\s+\w+\s+\(/XXX(/;
605 # do not match things like "void (*f)();"
606 # or "typedef void (func_t)();"
607 s/\w\s\(+\*/XXX(*/g;
608 s/\b($typename|void)\s+\(+/XXX(/og;
609 if (/\w\s\(/) {
610 err("extra space between function name and left paren");
612 $_ = $s;
614 # try to detect "int foo(x)", but not "extern int foo(x);"
615 # XXX - this still trips over too many legitimate things,
616 # like "int foo(x,\n\ty);"
617 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01)*$/ &&
618 # !/^(extern|static)\b/) {
619 # err("return type of function not on separate line");
621 # this is a close approximation
622 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\x01)*$/ &&
623 !/^(extern|static)\b/) {
624 err("return type of function not on separate line");
626 if (/^#define /) {
627 err("#define followed by space instead of tab");
629 if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
630 err("unparenthesized return expression");
632 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
633 err("unparenthesized sizeof expression");
635 if (/\(\s/) {
636 err("whitespace after left paren");
638 # Allow "for" statements to have empty "continue" clauses.
639 # Allow right paren on its own line unless we're being picky (-p).
640 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/ && ($picky || !/^\s*\)/)) {
641 err("whitespace before right paren");
643 if (/^\s*\(void\)[^ ]/) {
644 err("missing space after (void) cast");
646 if (/\S\{/ && !/\{\{/) {
647 err("missing space before left brace");
649 if ($in_function && /^\s+\{/ &&
650 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
651 err("left brace starting a line");
653 if (/\}(else|while)/) {
654 err("missing space after right brace");
656 if (/\}\s\s+(else|while)/) {
657 err("extra space after right brace");
659 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
660 err("obsolete use of VOID or STATIC");
662 if (/\b$typename\*/o) {
663 err("missing space between type name and *");
665 if (/^\s+#/) {
666 err("preprocessor statement not in column 1");
668 if (/^#\s/) {
669 err("blank after preprocessor #");
671 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
672 err("don't use boolean ! with comparison functions");
676 # We completely ignore, for purposes of indentation:
677 # * lines outside of functions
678 # * preprocessor lines
680 if ($check_continuation && $in_function && !$in_cpp) {
681 process_indent($_);
683 if ($picky) {
684 # try to detect spaces after casts, but allow (e.g.)
685 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
686 # "int foo(int) __NORETURN;"
687 if ((/^\($typename( \*+)?\)\s/o ||
688 /\W\($typename( \*+)?\)\s/o) &&
689 !/sizeof\s*\($typename( \*)?\)\s/o &&
690 !/\($typename( \*+)?\)\s+=[^=]/o) {
691 err("space after cast");
693 if (/\b$typename\s*\*\s/o &&
694 !/\b$typename\s*\*\s+const\b/o) {
695 err("unary * followed by space");
698 if ($check_posix_types && !$in_macro_call) {
699 # try to detect old non-POSIX types.
700 # POSIX requires all non-standard typedefs to end in _t,
701 # but historically these have been used.
703 # We don't check inside macro invocations because macros have
704 # legitmate uses for these names in function generators.
705 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
706 err("non-POSIX typedef $1 used: use $old2posix{$1} instead");
709 if (/^\s*else\W/) {
710 if ($prev =~ /^\s*\}$/) {
711 err_prefix($prev,
712 "else and right brace should be on same line");
716 # Macro invocations end with a closing paren, and possibly a semicolon.
717 # We do this check down here to make sure all the regular checks are
718 # applied to calls that appear entirely on a single line.
719 if ($in_macro_call && /\);?$/) {
720 $in_macro_call = 0;
723 $prev = $line;
726 if ($prev eq "") {
727 err("last line in file is blank");
733 # Continuation-line checking
735 # The rest of this file contains the code for the continuation checking
736 # engine. It's a pretty simple state machine which tracks the expression
737 # depth (unmatched '('s and '['s).
739 # Keep in mind that the argument to process_indent() has already been heavily
740 # processed; all comments have been replaced by control-A, and the contents of
741 # strings and character constants have been elided.
744 my $cont_in; # currently inside of a continuation
745 my $cont_off; # skipping an initializer or definition
746 my $cont_noerr; # suppress cascading errors
747 my $cont_start; # the line being continued
748 my $cont_base; # the base indentation
749 my $cont_first; # this is the first line of a statement
750 my $cont_multiseg; # this continuation has multiple segments
752 my $cont_special; # this is a C statement (if, for, etc.)
753 my $cont_macro; # this is a macro
754 my $cont_case; # this is a multi-line case
756 my @cont_paren; # the stack of unmatched ( and [s we've seen
759 reset_indent()
761 $cont_in = 0;
762 $cont_off = 0;
766 delabel($)
769 # replace labels with tabs. Note that there may be multiple
770 # labels on a line.
772 local $_ = $_[0];
774 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
775 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
776 $_ = $pre_tabs;
777 while ($label =~ s/^([^\t]*)(\t+)//) {
778 $_ .= "\t" x (length($2) + length($1) / 8);
780 $_ .= ("\t" x (length($label) / 8)).$rest;
783 return ($_);
787 process_indent($)
789 require strict;
790 local $_ = $_[0]; # preserve the global $_
792 s/\x01//g; # No comments
793 s/\s+$//; # Strip trailing whitespace
795 return if (/^$/); # skip empty lines
797 # regexps used below; keywords taking (), macros, and continued cases
798 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
799 my $macro = '[A-Z_][A-Z_0-9]*\(';
800 my $case = 'case\b[^:]*$';
802 # skip over enumerations, array definitions, initializers, etc.
803 if ($cont_off <= 0 && !/^\s*$special/ &&
804 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*))\{/ ||
805 (/^\s*\{/ && $prev =~ /=\s*(?:\/\*.*\*\/\s*)*$/))) {
806 $cont_in = 0;
807 $cont_off = tr/{/{/ - tr/}/}/;
808 return;
810 if ($cont_off) {
811 $cont_off += tr/{/{/ - tr/}/}/;
812 return;
815 if (!$cont_in) {
816 $cont_start = $line;
818 if (/^\t* /) {
819 err("non-continuation indented 4 spaces");
820 $cont_noerr = 1; # stop reporting
822 $_ = delabel($_); # replace labels with tabs
824 # check if the statement is complete
825 return if (/^\s*\}?$/);
826 return if (/^\s*\}?\s*else\s*\{?$/);
827 return if (/^\s*do\s*\{?$/);
828 return if (/\{$/);
829 return if (/\}[,;]?$/);
831 # Allow macros on their own lines
832 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
834 # cases we don't deal with, generally non-kosher
835 if (/\{/) {
836 err("stuff after {");
837 return;
840 # Get the base line, and set up the state machine
841 /^(\t*)/;
842 $cont_base = $1;
843 $cont_in = 1;
844 @cont_paren = ();
845 $cont_first = 1;
846 $cont_multiseg = 0;
848 # certain things need special processing
849 $cont_special = /^\s*$special/? 1 : 0;
850 $cont_macro = /^\s*$macro/? 1 : 0;
851 $cont_case = /^\s*$case/? 1 : 0;
852 } else {
853 $cont_first = 0;
855 # Strings may be pulled back to an earlier (half-)tabstop
856 unless ($cont_noerr || /^$cont_base / ||
857 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
858 err_prefix($cont_start,
859 "continuation should be indented 4 spaces");
863 my $rest = $_; # keeps the remainder of the line
866 # The split matches 0 characters, so that each 'special' character
867 # is processed separately. Parens and brackets are pushed and
868 # popped off the @cont_paren stack. For normal processing, we wait
869 # until a ; or { terminates the statement. "special" processing
870 # (if/for/while/switch) is allowed to stop when the stack empties,
871 # as is macro processing. Case statements are terminated with a :
872 # and an empty paren stack.
874 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
875 next if (length($_) == 0);
877 # rest contains the remainder of the line
878 my $rxp = "[^\Q$_\E]*\Q$_\E";
879 $rest =~ s/^$rxp//;
881 if (/\(/ || /\[/) {
882 push @cont_paren, $_;
883 } elsif (/\)/ || /\]/) {
884 my $cur = $_;
885 tr/\)\]/\(\[/;
887 my $old = (pop @cont_paren);
888 if (!defined($old)) {
889 err("unexpected '$cur'");
890 $cont_in = 0;
891 last;
892 } elsif ($old ne $_) {
893 err("'$cur' mismatched with '$old'");
894 $cont_in = 0;
895 last;
899 # If the stack is now empty, do special processing
900 # for if/for/while/switch and macro statements.
902 next if (@cont_paren != 0);
903 if ($cont_special) {
904 if ($rest =~ /^\s*\{?$/) {
905 $cont_in = 0;
906 last;
908 if ($rest =~ /^\s*;$/) {
909 err("empty if/for/while body ".
910 "not on its own line");
911 $cont_in = 0;
912 last;
914 if (!$cont_first && $cont_multiseg == 1) {
915 err_prefix($cont_start,
916 "multiple statements continued ".
917 "over multiple lines");
918 $cont_multiseg = 2;
919 } elsif ($cont_multiseg == 0) {
920 $cont_multiseg = 1;
922 # We've finished this section, start
923 # processing the next.
924 goto section_ended;
926 if ($cont_macro) {
927 if ($rest =~ /^$/) {
928 $cont_in = 0;
929 last;
932 } elsif (/\;/) {
933 if ($cont_case) {
934 err("unexpected ;");
935 } elsif (!$cont_special) {
936 err("unexpected ;") if (@cont_paren != 0);
937 if (!$cont_first && $cont_multiseg == 1) {
938 err_prefix($cont_start,
939 "multiple statements continued ".
940 "over multiple lines");
941 $cont_multiseg = 2;
942 } elsif ($cont_multiseg == 0) {
943 $cont_multiseg = 1;
945 if ($rest =~ /^$/) {
946 $cont_in = 0;
947 last;
949 if ($rest =~ /^\s*special/) {
950 err("if/for/while/switch not started ".
951 "on its own line");
953 goto section_ended;
955 } elsif (/\{/) {
956 err("{ while in parens/brackets") if (@cont_paren != 0);
957 err("stuff after {") if ($rest =~ /[^\s}]/);
958 $cont_in = 0;
959 last;
960 } elsif (/\}/) {
961 err("} while in parens/brackets") if (@cont_paren != 0);
962 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
963 if ($rest =~ /^$/) {
964 err("unexpected }");
965 } else {
966 err("stuff after }");
968 $cont_in = 0;
969 last;
971 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
972 err("stuff after multi-line case") if ($rest !~ /$^/);
973 $cont_in = 0;
974 last;
976 next;
977 section_ended:
978 # End of a statement or if/while/for loop. Reset
979 # cont_special and cont_macro based on the rest of the
980 # line.
981 $cont_special = ($rest =~ /^\s*$special/)? 1 : 0;
982 $cont_macro = ($rest =~ /^\s*$macro/)? 1 : 0;
983 $cont_case = 0;
984 next;
986 $cont_noerr = 0 if (!$cont_in);