]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/utils/cstyle
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / utils / cstyle
1 #!/usr/bin/env perl
2 #
3 # CDDL HEADER START
4 #
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.
8 #
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.
13 #
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]
19 #
20 # CDDL HEADER END
21 #
22 #
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
25 #
26 # Portions copyright 2017, Intel Corporation.
27 #
28 # @(#)cstyle 1.58 98/09/09 (from shannon)
29 #ident "%Z%%M% %I% %E% SMI"
30 #
31 # cstyle - check for some common stylistic errors.
32 #
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".
36 #
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.
39 #
40 # A note to the non perl literate:
41 #
42 # perl regular expressions are pretty much like egrep
43 # regular expressions, with the following special symbols
44 #
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
49 # \d a digit [0-9]
50 # \D a non-digit
51 # \b word boundary (between \w and \W)
52 # \B non-word boundary
53 #
54
55 require 5.0;
56 use IO::File;
57 use Getopt::Std;
58 use strict;
59 use warnings;
60
61 my $usage =
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
66 -v verbose
67 -C don't check anything in header block comments
68 -P check for use of non-POSIX types
69 -o constructs
70 allow a comma-separated list of optional constructs:
71 doxygen allow doxygen-style block comments (/** /*!)
72 splint allow splint-style lint comments (/*@ ... @*/)
73 ";
74
75 my %opts;
76
77 if (!getopts("cho:pvCP", \%opts)) {
78 print $usage;
79 exit 2;
80 }
81
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'};
88
89 my $doxygen_comments = 0;
90 my $splint_comments = 0;
91
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") {
97 $splint_comments = 1;
98 } else {
99 print "cstyle: unrecognized construct \"$x\"\n";
100 print $usage;
101 exit 2;
102 }
103 }
104 }
105
106 my ($filename, $line, $prev); # shared globals
107
108 my $fmt;
109 my $hdr_comment_start;
110
111 if ($verbose) {
112 $fmt = "%s:%d: %s\n%s\n";
113 } else {
114 $fmt = "%s:%d: %s\n";
115 }
116
117 if ($doxygen_comments) {
118 # doxygen comments look like "/*!" or "/**"; allow them.
119 $hdr_comment_start = qr/^\s*\/\*[\!\*]?$/;
120 } else {
121 $hdr_comment_start = qr/^\s*\/\*$/;
122 }
123
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)';
127
128 # mapping of old types to POSIX compatible types
129 my %old2posix = (
130 'unchar' => 'uchar_t',
131 'ushort' => 'ushort_t',
132 'uint' => 'uint_t',
133 'ulong' => 'ulong_t',
134 'u_int' => 'uint_t',
135 'u_short' => 'ushort_t',
136 'u_long' => 'ulong_t',
137 'u_char' => 'uchar_t',
138 'quad' => 'quad_t'
139 );
140
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.*?
146 )\*\//x;
147
148 my $splint_re = qr/\/\*@.*?@\*\//x;
149
150 my $warlock_re = qr/\/\*\s*(?:
151 VARIABLES\ PROTECTED\ BY|
152 MEMBERS\ PROTECTED\ BY|
153 ALL\ MEMBERS\ PROTECTED\ BY|
154 READ-ONLY\ VARIABLES:|
155 READ-ONLY\ MEMBERS:|
156 VARIABLES\ READABLE\ WITHOUT\ LOCK:|
157 MEMBERS\ READABLE\ WITHOUT\ LOCK:|
158 LOCKS\ COVERED\ BY|
159 LOCK\ UNNEEDED\ BECAUSE|
160 LOCK\ NEEDED:|
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|
172 LOCK\ ORDER:
173 )/x;
174
175 my $err_stat = 0; # exit status
176
177 if ($#ARGV >= 0) {
178 foreach my $arg (@ARGV) {
179 my $fh = new IO::File $arg, "r";
180 if (!defined($fh)) {
181 printf "%s: can not open\n", $arg;
182 } else {
183 &cstyle($arg, $fh);
184 close $fh;
185 }
186 }
187 } else {
188 &cstyle("<stdin>", *STDIN);
189 }
190 exit $err_stat;
191
192 my $no_errs = 0; # set for CSTYLED-protected lines
193
194 sub err($) {
195 my ($error) = @_;
196 unless ($no_errs) {
197 if ($verbose) {
198 printf $fmt, $filename, $., $error, $line;
199 } else {
200 printf $fmt, $filename, $., $error;
201 }
202 $err_stat = 1;
203 }
204 }
205
206 sub err_prefix($$) {
207 my ($prevline, $error) = @_;
208 my $out = $prevline."\n".$line;
209 unless ($no_errs) {
210 printf $fmt, $filename, $., $error, $out;
211 $err_stat = 1;
212 }
213 }
214
215 sub err_prev($) {
216 my ($error) = @_;
217 unless ($no_errs) {
218 printf $fmt, $filename, $. - 1, $error, $prev;
219 $err_stat = 1;
220 }
221 }
222
223 sub cstyle($$) {
224
225 my ($fn, $filehandle) = @_;
226 $filename = $fn; # share it globally
227
228 my $in_cpp = 0;
229 my $next_in_cpp = 0;
230
231 my $in_comment = 0;
232 my $in_header_comment = 0;
233 my $comment_done = 0;
234 my $in_warlock_comment = 0;
235 my $in_function = 0;
236 my $in_function_header = 0;
237 my $in_declaration = 0;
238 my $note_level = 0;
239 my $nextok = 0;
240 my $nocheck = 0;
241
242 my $in_string = 0;
243
244 my ($okmsg, $comment_prefix);
245
246 $line = '';
247 $prev = '';
248 reset_indent();
249
250 line: while (<$filehandle>) {
251 s/\r?\n$//; # strip return and newline
252
253 # save the original line, then remove all text from within
254 # double or single quotes, we do not want to check such text.
255
256 $line = $_;
257
258 #
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.
262 #
263 # (we assume that no-one will use backslash-continuation with character
264 # constants)
265 #
266 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
267
268 #
269 # normal strings and characters
270 #
271 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
272 s/"([^\\"]|\\.)*"/\"\"/g;
273
274 #
275 # detect string continuation
276 #
277 if ($nocheck || $in_comment) {
278 $in_string = 0;
279 } else {
280 #
281 # Now that all full strings are replaced with "", we check
282 # for unfinished strings continuing onto the next line.
283 #
284 $in_string =
285 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
286 s/^("")*"([^\\"]|\\.)*\\$/""/);
287 }
288
289 #
290 # figure out if we are in a cpp directive
291 #
292 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
293 $next_in_cpp = $in_cpp && /\\$/; # only if continued
294
295 # strip off trailing backslashes, which appear in long macros
296 s/\s*\\$//;
297
298 # an /* END CSTYLED */ comment ends a no-check block.
299 if ($nocheck) {
300 if (/\/\* *END *CSTYLED *\*\//) {
301 $nocheck = 0;
302 } else {
303 reset_indent();
304 next line;
305 }
306 }
307
308 # a /*CSTYLED*/ comment indicates that the next line is ok.
309 if ($nextok) {
310 if ($okmsg) {
311 err($okmsg);
312 }
313 $nextok = 0;
314 $okmsg = 0;
315 if (/\/\* *CSTYLED.*\*\//) {
316 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
317 $okmsg = $1;
318 $nextok = 1;
319 }
320 $no_errs = 1;
321 } elsif ($no_errs) {
322 $no_errs = 0;
323 }
324
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.
330 my $eline = $line;
331 1 while $eline =~
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}$//;
342
343 if($eline =~ /[^\\]"|[^\\](\\\\)+"/) {
344 err("line > 80 characters");
345 }
346 } else {
347 err("line > 80 characters");
348 }
349 }
350 }
351
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
356 next;
357 }
358
359 # a /* BEGIN CSTYLED */ comment starts a no-check block.
360 if (/\/\* *BEGIN *CSTYLED *\*\//) {
361 $nocheck = 1;
362 }
363
364 # a /*CSTYLED*/ comment indicates that the next line is ok.
365 if (/\/\* *CSTYLED.*\*\//) {
366 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
367 $okmsg = $1;
368 $nextok = 1;
369 }
370 if (/\/\/ *CSTYLED/) {
371 /^.*\/\/ *CSTYLED *(.*)$/;
372 $okmsg = $1;
373 $nextok = 1;
374 }
375
376 # universal checks; apply to everything
377 if (/\t +\t/) {
378 err("spaces between tabs");
379 }
380 if (/ \t+ /) {
381 err("tabs between spaces");
382 }
383 if (/\s$/) {
384 err("space or tab at end of line");
385 }
386 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) {
387 err("comment preceded by non-blank");
388 }
389
390 # is this the beginning or ending of a function?
391 # (not if "struct foo\n{\n")
392 if (/^{$/ && $prev =~ /\)\s*(const\s*)?(\/\*.*\*\/\s*)?\\?$/) {
393 $in_function = 1;
394 $in_declaration = 1;
395 $in_function_header = 0;
396 $prev = $line;
397 next line;
398 }
399 if (/^}\s*(\/\*.*\*\/\s*)*$/) {
400 if ($prev =~ /^\s*return\s*;/) {
401 err_prev("unneeded return at end of function");
402 }
403 $in_function = 0;
404 reset_indent(); # we don't check between functions
405 $prev = $line;
406 next line;
407 }
408 if (/^\w*\($/) {
409 $in_function_header = 1;
410 }
411
412 if ($in_warlock_comment && /\*\//) {
413 $in_warlock_comment = 0;
414 $prev = $line;
415 next line;
416 }
417
418 # a blank line terminates the declarations within a function.
419 # XXX - but still a problem in sub-blocks.
420 if ($in_declaration && /^$/) {
421 $in_declaration = 0;
422 }
423
424 if ($comment_done) {
425 $in_comment = 0;
426 $in_header_comment = 0;
427 $comment_done = 0;
428 }
429 # does this looks like the start of a block comment?
430 if (/$hdr_comment_start/) {
431 if (!/^\t*\/\*/) {
432 err("block comment not indented by tabs");
433 }
434 $in_comment = 1;
435 /^(\s*)\//;
436 $comment_prefix = $1;
437 if ($comment_prefix eq "") {
438 $in_header_comment = 1;
439 }
440 $prev = $line;
441 next line;
442 }
443 # are we still in the block comment?
444 if ($in_comment) {
445 if (/^$comment_prefix \*\/$/) {
446 $comment_done = 1;
447 } elsif (/\*\//) {
448 $comment_done = 1;
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);
455 }
456 }
457
458 if ($in_header_comment && $ignore_hdr_comment) {
459 $prev = $line;
460 next line;
461 }
462
463 # check for errors that might occur in comments and in code.
464
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");
468 }
469 if (/^ / && !/^ \*[ \t\/]/ && !/^ \*$/ &&
470 (!/^ \w/ || $in_function != 0)) {
471 err("indent by spaces instead of tabs");
472 }
473 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
474 err("continuation line not indented by 4 spaces");
475 }
476 if (/$warlock_re/ && !/\*\//) {
477 $in_warlock_comment = 1;
478 $prev = $line;
479 next line;
480 }
481 if (/^\s*\/\*./ && !/^\s*\/\*.*\*\// && !/$hdr_comment_start/) {
482 err("improper first line of block comment");
483 }
484
485 if ($in_comment) { # still in comment, don't do further checks
486 $prev = $line;
487 next line;
488 }
489
490 if ((/[^(]\/\*\S/ || /^\/\*\S/) &&
491 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
492 err("missing blank after open comment");
493 }
494 if (/\S\*\/[^)]|\S\*\/$/ &&
495 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
496 err("missing blank before close comment");
497 }
498 if (/\/\/\S/) { # C++ comments
499 err("missing blank after start comment");
500 }
501 # check for unterminated single line comments, but allow them when
502 # they are used to comment out the argument list of a function
503 # declaration.
504 if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
505 err("unterminated single line comment");
506 }
507
508 if (/^(#else|#endif|#include)(.*)$/) {
509 $prev = $line;
510 if ($picky) {
511 my $directive = $1;
512 my $clause = $2;
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")) &&
517 ($clause ne "") &&
518 (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
519 (!($clause =~ /^\s+\/\/.*$/))) {
520 err("non-comment text following " .
521 "$directive (or malformed $directive " .
522 "directive)");
523 }
524 }
525 next line;
526 }
527
528 #
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.
532 #
533 s/\/\*.*?\*\//\x01/g;
534 s/\/\/.*$/\x01/; # C++ comments
535
536 # delete any trailing whitespace; we have already checked for that.
537 s/\s*$//;
538
539 # following checks do not apply to text in comments.
540
541 if (/[^ \t\+]\+[^\+=]/ || /[^\+]\+[^ \+=]/) {
542 err("missing space around + operator");
543 }
544 if (/[^ \t]\+=/ || /\+=[^ ]/) {
545 err("missing space around += operator");
546 }
547 if (/[^ \t\-]\-[^\->]/ && !/\(\w+\)\-\w/ && !/[\(\[]\-[\w \t]+[\)\],]/) {
548 err("missing space before - operator");
549 }
550 if (/[^\-]\-[^ \-=>]/ && !/\(\-\w+\)/ &&
551 !/(return|case|=|>|<|\?|:|,|^[ \t]+)[ \t]+\-[\w\(]/ && !/(\([^\)]+\)|\[|\()\-[\w\(\]]/) {
552 err("missing space after - operator");
553 }
554 if (/(return|case|=|\?|:|,|\[)[ \t]+\-[ \t]/ || /[\(\[]\-[ \t]/) {
555 err("extra space after - operator");
556 }
557 if (/[ \t]\+\+ /) {
558 err("extra space before or after ++ operator");
559 }
560 if (/[ \t]\-\- /) {
561 err("extra space before or after -- operator");
562 }
563 if (/[^ \t]\-=/ || /\-=[^ ]/) {
564 err("missing space around -= operator");
565 }
566 if (/[^ \t][\%\/]/ || /[\%\/][^ =]/ || /[\%\/]=[^ ]/) {
567 err("missing space around one of operators: % %= / /=");
568 }
569 if (/[^ \t]\*=/ || /\*=[^ ]/) {
570 err("missing space around *= operator");
571 }
572 if (/[^ \t\(\)\*\[]\*/) {
573 err("missing space before * operator");
574 }
575 if (/\*[^ =\*\w\(,]/ && !/\(.+ \*+\)/ && !/\*\[\]/ &&
576 !/\*\-\-\w/ && !/\*\+\+\w/ && !/\*\)/) {
577 err("missing space after * operator");
578 }
579 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
580 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
581 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
582 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
583 err("missing space around relational operator");
584 }
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");
592 }
593 }
594 if (/[,;]\S/ && !/\bfor \(;;\)/) {
595 err("comma or semicolon followed by non-blank");
596 }
597 # allow "for" statements to have empty "while" clauses
598 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
599 err("comma or semicolon preceded by blank");
600 }
601 if (/^\s*(&&|\|\|)/) {
602 err("improper boolean continuation");
603 }
604 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
605 err("more than one space around boolean operator");
606 }
607 if (/\b(for|if|while|switch|return|case)\(/) {
608 err("missing space between keyword and paren");
609 }
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");
613 }
614 if (/\b(for|if|while|switch|return|case)\s\s+\(/ &&
615 !/^#if\s+\(/) {
616 err("extra space between keyword and paren");
617 }
618 # try to detect "func (x)" but not "if (x)" or
619 # "#define foo (x)" or "int (*func)();"
620 if (/\w\s\(/) {
621 my $s = $_;
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;
625 s/#elif\s\(/XXX(/g;
626 s/^#define\s+\w+\s+\(/XXX(/;
627 # do not match things like "void (*f)();"
628 # or "typedef void (func_t)();"
629 s/\w\s\(+\*/XXX(*/g;
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;
634 if (/\w\s\(/) {
635 err("extra space between function name and left paren");
636 }
637 $_ = $s;
638 }
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");
645 # }
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");
650 }
651 if (/^#define\t/ || /^#ifdef\t/ || /^#ifndef\t/) {
652 err("#define/ifdef/ifndef followed by tab instead of space");
653 }
654 if (/^#define\s\s+/ || /^#ifdef\s\s+/ || /^#ifndef\s\s+/) {
655 err("#define/ifdef/ifndef followed by more than one space");
656 }
657 # AON C-style doesn't require this.
658 #if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
659 # err("unparenthesized return expression");
660 #}
661 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
662 err("unparenthesized sizeof expression");
663 }
664 if (/\b(sizeof|typeof)\b/ && /\b(sizeof|typeof)\s+\(.*\)/) {
665 err("spaces between sizeof/typeof expression and paren");
666 }
667 if (/\(\s/) {
668 err("whitespace after left paren");
669 }
670 # allow "for" statements to have empty "continue" clauses
671 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
672 err("whitespace before right paren");
673 }
674 if (/^\s*\(void\)[^ ]/) {
675 err("missing space after (void) cast");
676 }
677 if (/\S\{/ && !/\{\{/ && !/\(struct \w+\)\{/) {
678 err("missing space before left brace");
679 }
680 if ($in_function && /^\s+{/ &&
681 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
682 err("left brace starting a line");
683 }
684 if (/}(else|while)/) {
685 err("missing space after right brace");
686 }
687 if (/}\s\s+(else|while)/) {
688 err("extra space after right brace");
689 }
690 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
691 err("obsolete use of VOID or STATIC");
692 }
693 if (/\b($typename|void)\*/o) {
694 err("missing space between type name and *");
695 }
696 if (/^\s+#/) {
697 err("preprocessor statement not in column 1");
698 }
699 if (/^#\s/) {
700 err("blank after preprocessor #");
701 }
702 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
703 err("don't use boolean ! with comparison functions");
704 }
705 if (/^\S+\([\S\s]*\)\s*{/) {
706 err("brace of function definition not at beginning of line");
707 }
708 if (/static\s+\S+\s*=\s*(0|NULL)\s*;/) {
709 err("static variable initialized with 0 or NULL");
710 }
711 if (/typedef[\S\s]+\*\s*\w+\s*;/) {
712 err("typedefed pointer type");
713 }
714 if (/unsigned\s+int\s/) {
715 err("'unsigned int' instead of just 'unsigned'");
716 }
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'");
721 }
722
723 #
724 # We completely ignore, for purposes of indentation:
725 # * lines outside of functions
726 # * preprocessor lines
727 #
728 if ($check_continuation && $in_function && !$in_cpp) {
729 process_indent($_);
730 }
731 if ($picky) {
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");
740 }
741 if (/\b($typename|void)\s*\*\s/o &&
742 !/\b($typename|void)\s*\*\s+const\b/o) {
743 err("unary * followed by space");
744 }
745 }
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");
752 }
753 }
754 if ($heuristic) {
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");
760 }
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");
765 }
766 }
767 if (/^\s*else\W/) {
768 if ($prev =~ /^\s*}$/) {
769 err_prefix($prev,
770 "else and right brace should be on same line");
771 }
772 }
773 $prev = $line;
774 }
775
776 if ($prev eq "") {
777 err("last line in file is blank");
778 }
779
780 }
781
782 #
783 # Continuation-line checking
784 #
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).
788 #
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.
792 #
793
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
801
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
805
806 my @cont_paren; # the stack of unmatched ( and [s we've seen
807
808 sub
809 reset_indent()
810 {
811 $cont_in = 0;
812 $cont_off = 0;
813 }
814
815 sub
816 delabel($)
817 {
818 #
819 # replace labels with tabs. Note that there may be multiple
820 # labels on a line.
821 #
822 local $_ = $_[0];
823
824 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
825 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
826 $_ = $pre_tabs;
827 while ($label =~ s/^([^\t]*)(\t+)//) {
828 $_ .= "\t" x (length($2) + length($1) / 8);
829 }
830 $_ .= ("\t" x (length($label) / 8)).$rest;
831 }
832
833 return ($_);
834 }
835
836 sub
837 process_indent($)
838 {
839 require strict;
840 local $_ = $_[0]; # preserve the global $_
841
842 s/\x01//g; # No comments
843 s/\s+$//; # Strip trailing whitespace
844
845 return if (/^$/); # skip empty lines
846
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[^:]*$';
851
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*)*$/))) {
856 $cont_in = 0;
857 $cont_off = tr/{/{/ - tr/}/}/;
858 return;
859 }
860 if ($cont_off) {
861 $cont_off += tr/{/{/ - tr/}/}/;
862 return;
863 }
864
865 if (!$cont_in) {
866 $cont_start = $line;
867
868 if (/^\t* /) {
869 err("non-continuation indented 4 spaces");
870 $cont_noerr = 1; # stop reporting
871 }
872 $_ = delabel($_); # replace labels with tabs
873
874 # check if the statement is complete
875 return if (/^\s*\}?$/);
876 return if (/^\s*\}?\s*else\s*\{?$/);
877 return if (/^\s*do\s*\{?$/);
878 return if (/{$/);
879 return if (/}[,;]?$/);
880
881 # Allow macros on their own lines
882 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
883
884 # cases we don't deal with, generally non-kosher
885 if (/{/) {
886 err("stuff after {");
887 return;
888 }
889
890 # Get the base line, and set up the state machine
891 /^(\t*)/;
892 $cont_base = $1;
893 $cont_in = 1;
894 @cont_paren = ();
895 $cont_first = 1;
896 $cont_multiseg = 0;
897
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;
902 } else {
903 $cont_first = 0;
904
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");
910 }
911 }
912
913 my $rest = $_; # keeps the remainder of the line
914
915 #
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.
923 #
924 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
925 next if (length($_) == 0);
926
927 # rest contains the remainder of the line
928 my $rxp = "[^\Q$_\E]*\Q$_\E";
929 $rest =~ s/^$rxp//;
930
931 if (/\(/ || /\[/) {
932 push @cont_paren, $_;
933 } elsif (/\)/ || /\]/) {
934 my $cur = $_;
935 tr/\)\]/\(\[/;
936
937 my $old = (pop @cont_paren);
938 if (!defined($old)) {
939 err("unexpected '$cur'");
940 $cont_in = 0;
941 last;
942 } elsif ($old ne $_) {
943 err("'$cur' mismatched with '$old'");
944 $cont_in = 0;
945 last;
946 }
947
948 #
949 # If the stack is now empty, do special processing
950 # for if/for/while/switch and macro statements.
951 #
952 next if (@cont_paren != 0);
953 if ($cont_special) {
954 if ($rest =~ /^\s*{?$/) {
955 $cont_in = 0;
956 last;
957 }
958 if ($rest =~ /^\s*;$/) {
959 err("empty if/for/while body ".
960 "not on its own line");
961 $cont_in = 0;
962 last;
963 }
964 if (!$cont_first && $cont_multiseg == 1) {
965 err_prefix($cont_start,
966 "multiple statements continued ".
967 "over multiple lines");
968 $cont_multiseg = 2;
969 } elsif ($cont_multiseg == 0) {
970 $cont_multiseg = 1;
971 }
972 # We've finished this section, start
973 # processing the next.
974 goto section_ended;
975 }
976 if ($cont_macro) {
977 if ($rest =~ /^$/) {
978 $cont_in = 0;
979 last;
980 }
981 }
982 } elsif (/\;/) {
983 if ($cont_case) {
984 err("unexpected ;");
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");
991 $cont_multiseg = 2;
992 } elsif ($cont_multiseg == 0) {
993 $cont_multiseg = 1;
994 }
995 if ($rest =~ /^$/) {
996 $cont_in = 0;
997 last;
998 }
999 if ($rest =~ /^\s*special/) {
1000 err("if/for/while/switch not started ".
1001 "on its own line");
1002 }
1003 goto section_ended;
1004 }
1005 } elsif (/\{/) {
1006 err("{ while in parens/brackets") if (@cont_paren != 0);
1007 err("stuff after {") if ($rest =~ /[^\s}]/);
1008 $cont_in = 0;
1009 last;
1010 } elsif (/\}/) {
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 }");
1015 } else {
1016 err("stuff after }");
1017 }
1018 $cont_in = 0;
1019 last;
1020 }
1021 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
1022 err("stuff after multi-line case") if ($rest !~ /$^/);
1023 $cont_in = 0;
1024 last;
1025 }
1026 next;
1027 section_ended:
1028 # End of a statement or if/while/for loop. Reset
1029 # cont_special and cont_macro based on the rest of the
1030 # line.
1031 $cont_special = ($rest =~ /^\s*$special/)? 1 : 0;
1032 $cont_macro = ($rest =~ /^\s*$macro/)? 1 : 0;
1033 $cont_case = 0;
1034 next;
1035 }
1036 $cont_noerr = 0 if (!$cont_in);
1037 }