Debian Codegraph @

src/deps/miniasync/utils/cstyle

raw
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: CDDL-1.0
3 # Copyright 2008 Sun Microsystems, Inc.
4 # Copyright 2017-2020, Intel Corporation
5
6 # @(#)cstyle 1.58 98/09/09 (from shannon)
7 #ident "%Z%%M% %I% %E% SMI"
8 #
9 # cstyle - check for some common stylistic errors.
10 #
11 # cstyle is a sort of "lint" for C coding style.
12 # It attempts to check for the style used in the
13 # kernel, sometimes known as "Bill Joy Normal Form".
14 #
15 # There's a lot this can't check for, like proper indentation
16 # of code blocks. There's also a lot more this could check for.
17 #
18 # A note to the non perl literate:
19 #
20 # perl regular expressions are pretty much like egrep
21 # regular expressions, with the following special symbols
22 #
23 # \s any space character
24 # \S any non-space character
25 # \w any "word" character [a-zA-Z0-9_]
26 # \W any non-word character
27 # \d a digit [0-9]
28 # \D a non-digit
29 # \b word boundary (between \w and \W)
30 # \B non-word boundary
31 #
32
33 require 5.0;
34 use IO::File;
35 use Getopt::Std;
36 use strict;
37 use warnings;
38
39 my $usage =
40 "usage: cstyle [-chpvCP] [-o constructs] file ...
41 -c check continuation indentation inside functions
42 -h perform heuristic checks that are sometimes wrong
43 -p perform some of the more picky checks
44 -v verbose
45 -C don't check anything in header block comments
46 -P check for use of non-POSIX types
47 -o constructs
48 allow a comma-separated list of optional constructs:
49 doxygen allow doxygen-style block comments (/** /*!)
50 src2man allow src2man-style block comments (/** n)
51 splint allow splint-style lint comments (/*@ ... @*/)
52 ";
53
54 my %opts;
55
56 if (!getopts("cho:pvCP", \%opts)) {
57 print $usage;
58 exit 2;
59 }
60
61 my $check_continuation = $opts{'c'};
62 my $heuristic = $opts{'h'};
63 my $picky = $opts{'p'};
64 my $verbose = $opts{'v'};
65 my $ignore_hdr_comment = $opts{'C'};
66 my $check_posix_types = $opts{'P'};
67
68 my $doxygen_comments = 0;
69 my $splint_comments = 0;
70 my $src2man_comments = 0;
71
72 if (defined($opts{'o'})) {
73 for my $x (split /,/, $opts{'o'}) {
74 if ($x eq "doxygen") {
75 $doxygen_comments = 1;
76 } elsif ($x eq "splint") {
77 $splint_comments = 1;
78 } elsif ($x eq "src2man") {
79 $src2man_comments = 1;
80 } else {
81 print "cstyle: unrecognized construct \"$x\"\n";
82 print $usage;
83 exit 2;
84 }
85 }
86 }
87
88 my ($filename, $line, $prev); # shared globals
89
90 my $fmt;
91 my $hdr_comment_start;
92
93 if ($verbose) {
94 $fmt = "%s:%d: %s\n%s\n";
95 } else {
96 $fmt = "%s:%d: %s\n";
97 }
98
99 if ($doxygen_comments) {
100 # doxygen comments look like "/*!" or "/**"; allow them.
101 $hdr_comment_start = qr/^\s*\/\*[\!\*]?$/;
102 } elsif ($src2man_comments) {
103 # src2man comments look like "/** n",
104 # where 'n' stands for a manual's section number; allow them.
105 $hdr_comment_start = qr/^\s*\/\*$|^\s*\/\*\* [0-9]{1}$/;
106 } else {
107 $hdr_comment_start = qr/^\s*\/\*$/;
108 }
109
110 # Note, following must be in single quotes so that \s and \w work right.
111 my $typename = '(int|char|short|long|unsigned|float|double' .
112 '|\w+_t|struct\s+\w+|union\s+\w+|FILE|BOOL)';
113
114 # mapping of old types to POSIX compatible types
115 my %old2posix = (
116 'unchar' => 'uchar_t',
117 'ushort' => 'ushort_t',
118 'uint' => 'uint_t',
119 'ulong' => 'ulong_t',
120 'u_int' => 'uint_t',
121 'u_short' => 'ushort_t',
122 'u_long' => 'ulong_t',
123 'u_char' => 'uchar_t',
124 'quad' => 'quad_t'
125 );
126
127 my $lint_re = qr/\/\*(?:
128 ARGSUSED[0-9]*|NOTREACHED|LINTLIBRARY|VARARGS[0-9]*|
129 CONSTCOND|CONSTANTCOND|CONSTANTCONDITION|EMPTY|
130 FALLTHRU|FALLTHROUGH|LINTED.*?|PRINTFLIKE[0-9]*|
131 PROTOLIB[0-9]*|SCANFLIKE[0-9]*|CSTYLED.*?
132 )\*\//x;
133
134 my $splint_re = qr/\/\*@.*?@\*\//x;
135
136 my $warlock_re = qr/\/\*\s*(?:
137 VARIABLES\ PROTECTED\ BY|
138 MEMBERS\ PROTECTED\ BY|
139 ALL\ MEMBERS\ PROTECTED\ BY|
140 READ-ONLY\ VARIABLES:|
141 READ-ONLY\ MEMBERS:|
142 VARIABLES\ READABLE\ WITHOUT\ LOCK:|
143 MEMBERS\ READABLE\ WITHOUT\ LOCK:|
144 LOCKS\ COVERED\ BY|
145 LOCK\ UNNEEDED\ BECAUSE|
146 LOCK\ NEEDED:|
147 LOCK\ HELD\ ON\ ENTRY:|
148 READ\ LOCK\ HELD\ ON\ ENTRY:|
149 WRITE\ LOCK\ HELD\ ON\ ENTRY:|
150 LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
151 READ\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
152 WRITE\ LOCK\ ACQUIRED\ AS\ SIDE\ EFFECT:|
153 LOCK\ RELEASED\ AS\ SIDE\ EFFECT:|
154 LOCK\ UPGRADED\ AS\ SIDE\ EFFECT:|
155 LOCK\ DOWNGRADED\ AS\ SIDE\ EFFECT:|
156 FUNCTIONS\ CALLED\ THROUGH\ POINTER|
157 FUNCTIONS\ CALLED\ THROUGH\ MEMBER|
158 LOCK\ ORDER:
159 )/x;
160
161 my $err_stat = 0; # exit status
162
163 if ($#ARGV >= 0) {
164 foreach my $arg (@ARGV) {
165 my $fh = new IO::File $arg, "r";
166 if (!defined($fh)) {
167 printf "%s: can not open\n", $arg;
168 } else {
169 &cstyle($arg, $fh);
170 close $fh;
171 }
172 }
173 } else {
174 &cstyle("<stdin>", *STDIN);
175 }
176 exit $err_stat;
177
178 my $no_errs = 0; # set for CSTYLED-protected lines
179
180 sub err($) {
181 my ($error) = @_;
182 unless ($no_errs) {
183 if ($verbose) {
184 printf $fmt, $filename, $., $error, $line;
185 } else {
186 printf $fmt, $filename, $., $error;
187 }
188 $err_stat = 1;
189 }
190 }
191
192 sub err_prefix($$) {
193 my ($prevline, $error) = @_;
194 my $out = $prevline."\n".$line;
195 unless ($no_errs) {
196 printf $fmt, $filename, $., $error, $out;
197 $err_stat = 1;
198 }
199 }
200
201 sub err_prev($) {
202 my ($error) = @_;
203 unless ($no_errs) {
204 printf $fmt, $filename, $. - 1, $error, $prev;
205 $err_stat = 1;
206 }
207 }
208
209 sub cstyle($$) {
210
211 my ($fn, $filehandle) = @_;
212 $filename = $fn; # share it globally
213
214 my $in_cpp = 0;
215 my $next_in_cpp = 0;
216
217 my $in_comment = 0;
218 my $in_header_comment = 0;
219 my $comment_done = 0;
220 my $in_warlock_comment = 0;
221 my $in_function = 0;
222 my $in_function_header = 0;
223 my $in_declaration = 0;
224 my $note_level = 0;
225 my $nextok = 0;
226 my $nocheck = 0;
227
228 my $in_string = 0;
229
230 my ($okmsg, $comment_prefix);
231
232 $line = '';
233 $prev = '';
234 reset_indent();
235
236 line: while (<$filehandle>) {
237 s/\r?\n$//; # strip return and newline
238
239 # save the original line, then remove all text from within
240 # double or single quotes, we do not want to check such text.
241
242 $line = $_;
243
244 #
245 # C allows strings to be continued with a backslash at the end of
246 # the line. We translate that into a quoted string on the previous
247 # line followed by an initial quote on the next line.
248 #
249 # (we assume that no-one will use backslash-continuation with character
250 # constants)
251 #
252 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment);
253
254 #
255 # normal strings and characters
256 #
257 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g;
258 s/"([^\\"]|\\.)*"/\"\"/g;
259
260 #
261 # detect string continuation
262 #
263 if ($nocheck || $in_comment) {
264 $in_string = 0;
265 } else {
266 #
267 # Now that all full strings are replaced with "", we check
268 # for unfinished strings continuing onto the next line.
269 #
270 $in_string =
271 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ ||
272 s/^("")*"([^\\"]|\\.)*\\$/""/);
273 }
274
275 #
276 # figure out if we are in a cpp directive
277 #
278 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started
279 $next_in_cpp = $in_cpp && /\\$/; # only if continued
280
281 # strip off trailing backslashes, which appear in long macros
282 s/\s*\\$//;
283
284 # an /* END CSTYLED */ comment ends a no-check block.
285 if ($nocheck) {
286 if (/\/\* *END *CSTYLED *\*\//) {
287 $nocheck = 0;
288 } else {
289 reset_indent();
290 next line;
291 }
292 }
293
294 # a /*CSTYLED*/ comment indicates that the next line is ok.
295 if ($nextok) {
296 if ($okmsg) {
297 err($okmsg);
298 }
299 $nextok = 0;
300 $okmsg = 0;
301 if (/\/\* *CSTYLED.*\*\//) {
302 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
303 $okmsg = $1;
304 $nextok = 1;
305 }
306 $no_errs = 1;
307 } elsif ($no_errs) {
308 $no_errs = 0;
309 }
310
311 # check length of line.
312 # first, a quick check to see if there is any chance of being too long.
313 if (($line =~ tr/\t/\t/) * 7 + length($line) > 80) {
314 # yes, there is a chance.
315 # replace tabs with spaces and check again.
316 my $eline = $line;
317 1 while $eline =~
318 s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
319 if (length($eline) > 80) {
320 # allow long line if it is user visible string
321 # find if line start from " and ends
322 # with " + 2 optional characters
323 # (these characters can be i.e. '");' '" \' or '",' etc...)
324 if($eline =~ /^ *".*"[^"]{0,2}$/) {
325 # check if entire line is one string literal
326 $eline =~ s/^ *"//;
327 $eline =~ s/"[^"]{0,2}$//;
328
329 if($eline =~ /[^\\]"|[^\\](\\\\)+"/) {
330 err("line > 80 characters");
331 }
332 # allow long line if it is an URL
333 } elsif (!($eline =~ / https?:\/\//)) {
334 err("line > 80 characters");
335 }
336 }
337 }
338
339 # ignore NOTE(...) annotations (assumes NOTE is on lines by itself).
340 if ($note_level || /\b_?NOTE\s*\(/) { # if in NOTE or this is NOTE
341 s/[^()]//g; # eliminate all non-parens
342 $note_level += s/\(//g - length; # update paren nest level
343 next;
344 }
345
346 # a /* BEGIN CSTYLED */ comment starts a no-check block.
347 if (/\/\* *BEGIN *CSTYLED *\*\//) {
348 $nocheck = 1;
349 }
350
351 # a /*CSTYLED*/ comment indicates that the next line is ok.
352 if (/\/\* *CSTYLED.*\*\//) {
353 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/;
354 $okmsg = $1;
355 $nextok = 1;
356 }
357 if (/\/\/ *CSTYLED/) {
358 /^.*\/\/ *CSTYLED *(.*)$/;
359 $okmsg = $1;
360 $nextok = 1;
361 }
362
363 # universal checks; apply to everything
364 if (/\t +\t/) {
365 err("spaces between tabs");
366 }
367 if (/ \t+ /) {
368 err("tabs between spaces");
369 }
370 if (/\s$/) {
371 err("space or tab at end of line");
372 }
373 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) {
374 err("comment preceded by non-blank");
375 }
376
377 # is this the beginning or ending of a function?
378 # (not if "struct foo\n{\n")
379 if (/^{$/ && $prev =~ /\)\s*(const\s*)?(\/\*.*\*\/\s*)?\\?$/) {
380 $in_function = 1;
381 $in_declaration = 1;
382 $in_function_header = 0;
383 $prev = $line;
384 next line;
385 }
386 if (/^}\s*(\/\*.*\*\/\s*)*$/) {
387 if ($prev =~ /^\s*return\s*;/) {
388 err_prev("unneeded return at end of function");
389 }
390 $in_function = 0;
391 reset_indent(); # we don't check between functions
392 $prev = $line;
393 next line;
394 }
395 if (/^\w*\($/) {
396 $in_function_header = 1;
397 }
398
399 if ($in_warlock_comment && /\*\//) {
400 $in_warlock_comment = 0;
401 $prev = $line;
402 next line;
403 }
404
405 # a blank line terminates the declarations within a function.
406 # XXX - but still a problem in sub-blocks.
407 if ($in_declaration && /^$/) {
408 $in_declaration = 0;
409 }
410
411 if ($comment_done) {
412 $in_comment = 0;
413 $in_header_comment = 0;
414 $comment_done = 0;
415 }
416 # does this looks like the start of a block comment?
417 if (/$hdr_comment_start/) {
418 if (!/^\t*\/\*/) {
419 err("block comment not indented by tabs");
420 }
421 $in_comment = 1;
422 /^(\s*)\//;
423 $comment_prefix = $1;
424 if ($comment_prefix eq "") {
425 $in_header_comment = 1;
426 }
427 $prev = $line;
428 next line;
429 }
430 # are we still in the block comment?
431 if ($in_comment) {
432 if (/^$comment_prefix \*\/$/) {
433 $comment_done = 1;
434 } elsif (/\*\//) {
435 $comment_done = 1;
436 err("improper block comment close")
437 unless ($ignore_hdr_comment && $in_header_comment);
438 } elsif (!/^$comment_prefix \*[ \t]/ &&
439 !/^$comment_prefix \*$/) {
440 err("improper block comment")
441 unless ($ignore_hdr_comment && $in_header_comment);
442 }
443 }
444
445 if ($in_header_comment && $ignore_hdr_comment) {
446 $prev = $line;
447 next line;
448 }
449
450 # check for errors that might occur in comments and in code.
451
452 # allow spaces to be used to draw pictures in header and block comments.
453 if (/[^ ] / && !/".* .*"/ && !$in_header_comment && !$in_comment) {
454 err("spaces instead of tabs");
455 }
456 if (/^ / && !/^ \*[ \t\/]/ && !/^ \*$/ &&
457 (!/^ \w/ || $in_function != 0)) {
458 err("indent by spaces instead of tabs");
459 }
460 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
461 err("continuation line not indented by 4 spaces");
462 }
463 if (/ a$/ || / an$/ || / the$/) {
464 err("orphaned pronoun at the end of the line");
465 }
466 if (/$warlock_re/ && !/\*\//) {
467 $in_warlock_comment = 1;
468 $prev = $line;
469 next line;
470 }
471 if (/^\s*\/\*./ && !/^\s*\/\*.*\*\// && !/$hdr_comment_start/) {
472 err("improper first line of block comment");
473 }
474
475 if ($in_comment) { # still in comment, don't do further checks
476 $prev = $line;
477 next line;
478 }
479
480 if ((/[^(]\/\*\S/ || /^\/\*\S/) &&
481 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
482 err("missing blank after open comment");
483 }
484 if (/\S\*\/[^)]|\S\*\/$/ &&
485 !(/$lint_re/ || ($splint_comments && /$splint_re/))) {
486 err("missing blank before close comment");
487 }
488 if (/\/\/\S/) { # C++ comments
489 err("missing blank after start comment");
490 }
491 # check for unterminated single line comments, but allow them when
492 # they are used to comment out the argument list of a function
493 # declaration.
494 if (/\S.*\/\*/ && !/\S.*\/\*.*\*\// && !/\(\/\*/) {
495 err("unterminated single line comment");
496 }
497
498 if (/^(#else|#endif|#include)(.*)$/) {
499 $prev = $line;
500 if ($picky) {
501 my $directive = $1;
502 my $clause = $2;
503 # Enforce ANSI rules for #else and #endif: no noncomment
504 # identifiers are allowed after #endif or #else. Allow
505 # C++ comments since they seem to be a fact of life.
506 if ((($1 eq "#endif") || ($1 eq "#else")) &&
507 ($clause ne "") &&
508 (!($clause =~ /^\s+\/\*.*\*\/$/)) &&
509 (!($clause =~ /^\s+\/\/.*$/))) {
510 err("non-comment text following " .
511 "$directive (or malformed $directive " .
512 "directive)");
513 }
514 }
515 next line;
516 }
517
518 #
519 # delete any comments and check everything else. Note that
520 # ".*?" is a non-greedy match, so that we don't get confused by
521 # multiple comments on the same line.
522 #
523 s/\/\*.*?\*\//\x01/g;
524 s/\/\/.*$/\x01/; # C++ comments
525
526 # delete any trailing whitespace; we have already checked for that.
527 s/\s*$//;
528
529 # following checks do not apply to text in comments.
530
531 if (/[^ \t\+]\+[^\+=]/ || /[^\+]\+[^ \+=]/) {
532 err("missing space around + operator");
533 }
534 if (/[^ \t]\+=/ || /\+=[^ ]/) {
535 err("missing space around += operator");
536 }
537 if (/[^ \t\-]\-[^\->]/ && !/\(\w+\)\-\w/ && !/[\(\[\{]\-[\w \t]+[\)\]\},]/) {
538 err("missing space before - operator");
539 }
540 if (/[^\-]\-[^ \-=>]/ && !/\(\-\w+\)/ &&
541 !/(return|case|=|>|<|\?|:|,|^[ \t]+)[ \t]+\-[\w\(]/ && !/(\([^\)]+\)|\[|\(|\{)\-[\w\(\]]/) {
542 err("missing space after - operator");
543 }
544 if (/(return|case|=|\?|:|,|\[)[ \t]+\-[ \t]/ || /[\(\[]\-[ \t]/) {
545 err("extra space after - operator");
546 }
547 if (/[ \t(\[]\+\+ / || / \+\+[)\]]/) {
548 err("extra space before or after ++ operator");
549 }
550 if (/[ \t(\[]\-\- / || / \-\-[)\]]/) {
551 err("extra space before or after -- operator");
552 }
553 if (/[^ \t]\-=/ || /\-=[^ ]/) {
554 err("missing space around -= operator");
555 }
556 if (/[^ \t][\%\/]/ || /[\%\/][^ =]/ || /[\%\/]=[^ ]/) {
557 err("missing space around one of operators: % %= / /=");
558 }
559 if (/[^ \t]\*=/ || /\*=[^ ]/) {
560 err("missing space around *= operator");
561 }
562 if (/[^ \t\(\)\*\[]\*/) {
563 err("missing space before * operator");
564 }
565 if (/\*[^ =\*\w\(,]/ && !/\(.+ \*+\)/ && !/\*\[\]/ &&
566 !/\*\-\-\w/ && !/\*\+\+\w/ && !/\*\)/) {
567 err("missing space after * operator");
568 }
569 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ ||
570 (/[^->]>[^,=>\s]/ && !/[^->]>$/) ||
571 (/[^<]<[^,=<\s]/ && !/[^<]<$/) ||
572 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) {
573 err("missing space around relational operator");
574 }
575 if (/\S>>=/ || /\S<<=/ || />>=\S/ || /<<=\S/ || /\S[-+*\/&|^%]=/ ||
576 (/[^-+*\/&|^%!<>=\s]=[^=]/ && !/[^-+*\/&|^%!<>=\s]=$/) ||
577 (/[^!<>=]=[^=\s]/ && !/[^!<>=]=$/)) {
578 # XXX - should only check this for C++ code
579 # XXX - there are probably other forms that should be allowed
580 if (!/\soperator=/) {
581 err("missing space around assignment operator");
582 }
583 }
584 if (/[,;]\S/ && !/\bfor \(;;\)/) {
585 err("comma or semicolon followed by non-blank");
586 }
587 # allow "for" statements to have empty "while" clauses
588 if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
589 err("comma or semicolon preceded by blank");
590 }
591 if (/^\s*(&&|\|\|)/) {
592 err("improper boolean continuation");
593 }
594 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
595 err("more than one space around boolean operator");
596 }
597 if (/\b(for|if|while|switch|return|case)\(/) {
598 err("missing space between keyword and paren");
599 }
600 if (/(\b(for|if|while|switch|return)\b.*){2,}/ && !/^#define/) {
601 # multiple "case" and "sizeof" allowed
602 err("more than one keyword on line");
603 }
604 if (/\b(for|if|while|switch|return|case)\s\s+\(/ &&
605 !/^#if\s+\(/) {
606 err("extra space between keyword and paren");
607 }
608 # try to detect "func (x)" but not "if (x)" or
609 # "#define foo (x)" or "int (*func)();"
610 if (/\w\s\(/) {
611 my $s = $_;
612 # strip off all keywords on the line
613 s/\b(for|if|while|switch|return|case)\s\(/XXX(/g;
614 s/\b(sizeof|typeof|__typeof__)\s*\(/XXX(/g;
615 s/#elif\s\(/XXX(/g;
616 s/^#define\s+\w+\s+\(/XXX(/;
617 # do not match things like "void (*f)();"
618 # or "typedef void (func_t)();"
619 s/\w\s\(+\*/XXX(*/g;
620 s/\b($typename|void)\s+\(+/XXX(/og;
621 s/\btypedef\s($typename|void)\s+\(+/XXX(/og;
622 # do not match "__attribute__ ((format (...)))"
623 s/\b__attribute__\s*\(\(format\s*\(/__attribute__((XXX(/g;
624 if (/\w\s\(/) {
625 err("extra space between function name and left paren");
626 }
627 $_ = $s;
628 }
629 # try to detect "int foo(x)", but not "extern int foo(x);"
630 # XXX - this still trips over too many legitimate things,
631 # like "int foo(x,\n\ty);"
632 # if (/^(\w+(\s|\*)+)+\w+\(/ && !/\)[;,](\s|\x01)*$/ &&
633 # !/^(extern|static)\b/) {
634 # err("return type of function not on separate line");
635 # }
636 # this is a close approximation
637 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|\x01)*$/ &&
638 !/^(extern|static)\b/) {
639 err("return type of function not on separate line");
640 }
641 if (/^#define\t/ || /^#ifdef\t/ || /^#ifndef\t/) {
642 err("#define/ifdef/ifndef followed by tab instead of space");
643 }
644 if (/^#define\s\s+/ || /^#ifdef\s\s+/ || /^#ifndef\s\s+/) {
645 err("#define/ifdef/ifndef followed by more than one space");
646 }
647 # AON C-style doesn't require this.
648 #if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) {
649 # err("unparenthesized return expression");
650 #}
651 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) {
652 err("unparenthesized sizeof expression");
653 }
654 if (/\b(sizeof|typeof)\b/ && /\b(sizeof|typeof)\s+\(.*\)/) {
655 err("spaces between sizeof/typeof expression and paren");
656 }
657 if (/\(\s/) {
658 err("whitespace after left paren");
659 }
660 # allow "for" statements to have empty "continue" clauses
661 if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
662 err("whitespace before right paren");
663 }
664 if (/^\s*\(void\)[^ ]/) {
665 err("missing space after (void) cast");
666 }
667 if (/\S\{/ && !/\{\{/ && !/\(struct \w+\)\{/) {
668 err("missing space before left brace");
669 }
670 if ($in_function && /^\s+{/ &&
671 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) {
672 err("left brace starting a line");
673 }
674 if (/}(else|while)/) {
675 err("missing space after right brace");
676 }
677 if (/}\s\s+(else|while)/) {
678 err("extra space after right brace");
679 }
680 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) {
681 err("obsolete use of VOID or STATIC");
682 }
683 if (/\b($typename|void)\*/o) {
684 err("missing space between type name and *");
685 }
686 if (/^\s+#/) {
687 err("preprocessor statement not in column 1");
688 }
689 if (/^#\s/) {
690 err("blank after preprocessor #");
691 }
692 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) {
693 err("don't use boolean ! with comparison functions");
694 }
695 if (/^\S+\([\S\s]*\)\s*{/) {
696 err("brace of function definition not at beginning of line");
697 }
698 if (/static\s+\S+\s*=\s*(0|NULL)\s*;/) {
699 err("static variable initialized with 0 or NULL");
700 }
701 if (/typedef[\S\s]+\*\s*\w+\s*;/) {
702 err("typedefed pointer type");
703 }
704 if (/unsigned\s+int\s/) {
705 err("'unsigned int' instead of just 'unsigned'");
706 }
707 if (/long\s+long\s+int\s/) {
708 err("'long long int' instead of just 'long long'");
709 } elsif (/long\s+int\s/) {
710 err("'long int' instead of just 'long'");
711 }
712
713 #
714 # We completely ignore, for purposes of indentation:
715 # * lines outside of functions
716 # * preprocessor lines
717 #
718 if ($check_continuation && $in_function && !$in_cpp) {
719 process_indent($_);
720 }
721 if ($picky) {
722 # try to detect spaces after casts, but allow (e.g.)
723 # "sizeof (int) + 1", "void (*funcptr)(int) = foo;", and
724 # "int foo(int) __NORETURN;"
725 if ((/^\($typename( \*+)?\)\s/o ||
726 /\W\($typename( \*+)?\)\s/o) &&
727 !/sizeof\($typename( \*)?\)\s/o &&
728 !/\($typename( \*+)?\)\s+=[^=]/o) {
729 err("space after cast");
730 }
731 if (/\b($typename|void)\s*\*\s/o &&
732 !/\b($typename|void)\s*\*\s+const\b/o) {
733 err("unary * followed by space");
734 }
735 }
736 if ($check_posix_types) {
737 # try to detect old non-POSIX types.
738 # POSIX requires all non-standard typedefs to end in _t,
739 # but historically these have been used.
740 if (/\b(unchar|ushort|uint|ulong|u_int|u_short|u_long|u_char|quad)\b/) {
741 err("non-POSIX typedef $1 used: use $old2posix{$1} instead");
742 }
743 }
744 if ($heuristic) {
745 # cannot check this everywhere due to "struct {\n...\n} foo;"
746 if ($in_function && !$in_declaration &&
747 /}./ && !/}\s+=/ && !/{.*}[;,]$/ && !/}(\s|\x01)*$/ &&
748 !/} (else|while)/ && !/}}/) {
749 err("possible bad text following right brace");
750 }
751 # cannot check this because sub-blocks in
752 # the middle of code are ok
753 if ($in_function && /^\s+{/) {
754 err("possible left brace starting a line");
755 }
756 }
757 if (/^\s*else\W/) {
758 if ($prev =~ /^\s*}$/) {
759 err_prefix($prev,
760 "else and right brace should be on same line");
761 }
762 }
763 $prev = $line;
764 }
765
766 if ($prev eq "") {
767 err("last line in file is blank");
768 }
769
770 }
771
772 #
773 # Continuation-line checking
774 #
775 # The rest of this file contains the code for the continuation checking
776 # engine. It's a pretty simple state machine which tracks the expression
777 # depth (unmatched '('s and '['s).
778 #
779 # Keep in mind that the argument to process_indent() has already been heavily
780 # processed; all comments have been replaced by control-A, and the contents of
781 # strings and character constants have been elided.
782 #
783
784 my $cont_in; # currently inside of a continuation
785 my $cont_off; # skipping an initializer or definition
786 my $cont_noerr; # suppress cascading errors
787 my $cont_start; # the line being continued
788 my $cont_base; # the base indentation
789 my $cont_first; # this is the first line of a statement
790 my $cont_multiseg; # this continuation has multiple segments
791
792 my $cont_special; # this is a C statement (if, for, etc.)
793 my $cont_macro; # this is a macro
794 my $cont_case; # this is a multi-line case
795
796 my @cont_paren; # the stack of unmatched ( and [s we've seen
797
798 sub
799 reset_indent()
800 {
801 $cont_in = 0;
802 $cont_off = 0;
803 }
804
805 sub
806 delabel($)
807 {
808 #
809 # replace labels with tabs. Note that there may be multiple
810 # labels on a line.
811 #
812 local $_ = $_[0];
813
814 while (/^(\t*)( *(?:(?:\w+\s*)|(?:case\b[^:]*)): *)(.*)$/) {
815 my ($pre_tabs, $label, $rest) = ($1, $2, $3);
816 $_ = $pre_tabs;
817 while ($label =~ s/^([^\t]*)(\t+)//) {
818 $_ .= "\t" x (length($2) + length($1) / 8);
819 }
820 $_ .= ("\t" x (length($label) / 8)).$rest;
821 }
822
823 return ($_);
824 }
825
826 sub
827 process_indent($)
828 {
829 require strict;
830 local $_ = $_[0]; # preserve the global $_
831
832 s/\x01//g; # No comments
833 s/\s+$//; # Strip trailing whitespace
834
835 return if (/^$/); # skip empty lines
836
837 # regexps used below; keywords taking (), macros, and continued cases
838 my $special = '(?:(?:\}\s*)?else\s+)?(?:if|for|while|switch)\b';
839 my $macro = '[A-Z_][A-Z_0-9]*\(';
840 my $case = 'case\b[^:]*$';
841
842 # skip over enumerations, array definitions, initializers, etc.
843 if ($cont_off <= 0 && !/^\s*$special/ &&
844 (/(?:(?:\b(?:enum|struct|union)\s*[^\{]*)|(?:\s+=\s*))\{/ ||
845 (/^\s*{/ && $prev =~ /=\s*(?:\/\*.*\*\/\s*)*$/))) {
846 $cont_in = 0;
847 $cont_off = tr/{/{/ - tr/}/}/;
848 return;
849 }
850 if ($cont_off) {
851 $cont_off += tr/{/{/ - tr/}/}/;
852 return;
853 }
854
855 if (!$cont_in) {
856 $cont_start = $line;
857
858 if (/^\t* /) {
859 err("non-continuation indented 4 spaces");
860 $cont_noerr = 1; # stop reporting
861 }
862 $_ = delabel($_); # replace labels with tabs
863
864 # check if the statement is complete
865 return if (/^\s*\}?$/);
866 return if (/^\s*\}?\s*else\s*\{?$/);
867 return if (/^\s*do\s*\{?$/);
868 return if (/{$/);
869 return if (/}[,;]?$/);
870
871 # Allow macros on their own lines
872 return if (/^\s*[A-Z_][A-Z_0-9]*$/);
873
874 # cases we don't deal with, generally non-kosher
875 if (/{/) {
876 err("stuff after {");
877 return;
878 }
879
880 # Get the base line, and set up the state machine
881 /^(\t*)/;
882 $cont_base = $1;
883 $cont_in = 1;
884 @cont_paren = ();
885 $cont_first = 1;
886 $cont_multiseg = 0;
887
888 # certain things need special processing
889 $cont_special = /^\s*$special/? 1 : 0;
890 $cont_macro = /^\s*$macro/? 1 : 0;
891 $cont_case = /^\s*$case/? 1 : 0;
892 } else {
893 $cont_first = 0;
894
895 # Strings may be pulled back to an earlier (half-)tabstop
896 unless ($cont_noerr || /^$cont_base / ||
897 (/^\t*(?: )?(?:gettext\()?\"/ && !/^$cont_base\t/)) {
898 err_prefix($cont_start,
899 "continuation should be indented 4 spaces");
900 }
901 }
902
903 my $rest = $_; # keeps the remainder of the line
904
905 #
906 # The split matches 0 characters, so that each 'special' character
907 # is processed separately. Parens and brackets are pushed and
908 # popped off the @cont_paren stack. For normal processing, we wait
909 # until a ; or { terminates the statement. "special" processing
910 # (if/for/while/switch) is allowed to stop when the stack empties,
911 # as is macro processing. Case statements are terminated with a :
912 # and an empty paren stack.
913 #
914 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) {
915 next if (length($_) == 0);
916
917 # rest contains the remainder of the line
918 my $rxp = "[^\Q$_\E]*\Q$_\E";
919 $rest =~ s/^$rxp//;
920
921 if (/\(/ || /\[/) {
922 push @cont_paren, $_;
923 } elsif (/\)/ || /\]/) {
924 my $cur = $_;
925 tr/\)\]/\(\[/;
926
927 my $old = (pop @cont_paren);
928 if (!defined($old)) {
929 err("unexpected '$cur'");
930 $cont_in = 0;
931 last;
932 } elsif ($old ne $_) {
933 err("'$cur' mismatched with '$old'");
934 $cont_in = 0;
935 last;
936 }
937
938 #
939 # If the stack is now empty, do special processing
940 # for if/for/while/switch and macro statements.
941 #
942 next if (@cont_paren != 0);
943 if ($cont_special) {
944 if ($rest =~ /^\s*{?$/) {
945 $cont_in = 0;
946 last;
947 }
948 if ($rest =~ /^\s*;$/) {
949 err("empty if/for/while body ".
950 "not on its own line");
951 $cont_in = 0;
952 last;
953 }
954 if (!$cont_first && $cont_multiseg == 1) {
955 err_prefix($cont_start,
956 "multiple statements continued ".
957 "over multiple lines");
958 $cont_multiseg = 2;
959 } elsif ($cont_multiseg == 0) {
960 $cont_multiseg = 1;
961 }
962 # We've finished this section, start
963 # processing the next.
964 goto section_ended;
965 }
966 if ($cont_macro) {
967 if ($rest =~ /^$/) {
968 $cont_in = 0;
969 last;
970 }
971 }
972 } elsif (/\;/) {
973 if ($cont_case) {
974 err("unexpected ;");
975 } elsif (!$cont_special) {
976 err("unexpected ;") if (@cont_paren != 0);
977 if (!$cont_first && $cont_multiseg == 1) {
978 err_prefix($cont_start,
979 "multiple statements continued ".
980 "over multiple lines");
981 $cont_multiseg = 2;
982 } elsif ($cont_multiseg == 0) {
983 $cont_multiseg = 1;
984 }
985 if ($rest =~ /^$/) {
986 $cont_in = 0;
987 last;
988 }
989 if ($rest =~ /^\s*special/) {
990 err("if/for/while/switch not started ".
991 "on its own line");
992 }
993 goto section_ended;
994 }
995 } elsif (/\{/) {
996 err("{ while in parens/brackets") if (@cont_paren != 0);
997 err("stuff after {") if ($rest =~ /[^\s}]/);
998 $cont_in = 0;
999 last;
1000 } elsif (/\}/) {
1001 err("} while in parens/brackets") if (@cont_paren != 0);
1002 if (!$cont_special && $rest !~ /^\s*(while|else)\b/) {
1003 if ($rest =~ /^$/) {
1004 err("unexpected }");
1005 } else {
1006 err("stuff after }");
1007 }
1008 $cont_in = 0;
1009 last;
1010 }
1011 } elsif (/\:/ && $cont_case && @cont_paren == 0) {
1012 err("stuff after multi-line case") if ($rest !~ /$^/);
1013 $cont_in = 0;
1014 last;
1015 }
1016 next;
1017 section_ended:
1018 # End of a statement or if/while/for loop. Reset
1019 # cont_special and cont_macro based on the rest of the
1020 # line.
1021 $cont_special = ($rest =~ /^\s*$special/)? 1 : 0;
1022 $cont_macro = ($rest =~ /^\s*$macro/)? 1 : 0;
1023 $cont_case = 0;
1024 next;
1025 }
1026 $cont_noerr = 0 if (!$cont_in);
1027 }