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 https://opensource.org/licenses/CDDL-1.0. 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# Copyright 2016 Nexenta Systems, Inc. 23# 24# Copyright 2008 Sun Microsystems, Inc. All rights reserved. 25# Use is subject to license terms. 26# 27# @(#)cstyle 1.58 98/09/09 (from shannon) 28#ident "%Z%%M% %I% %E% SMI" 29# 30# cstyle - check for some common stylistic errors. 31# 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". 35# 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. 38# 39# A note to the non perl literate: 40# 41# perl regular expressions are pretty much like egrep 42# regular expressions, with the following special symbols 43# 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 52# 53 54require 5.0; 55use warnings; 56use IO::File; 57use Getopt::Std; 58use strict; 59 60my $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 67"; 68 69my %opts; 70 71if (!getopts("cghpvCP", \%opts)) { 72 print $usage; 73 exit 2; 74} 75 76my $check_continuation = $opts{'c'}; 77my $github_workflow = $opts{'g'} || $ENV{'CI'}; 78my $picky = $opts{'p'}; 79my $verbose = $opts{'v'}; 80my $check_posix_types = $opts{'P'}; 81 82my ($filename, $line, $prev); # shared globals 83 84my $fmt; 85my $hdr_comment_start; 86 87if ($verbose) { 88 $fmt = "%s: %d: %s\n%s\n"; 89} else { 90 $fmt = "%s: %d: %s\n"; 91} 92 93$hdr_comment_start = qr/^\s*\/\*$/; 94 95# Note, following must be in single quotes so that \s and \w work right. 96my $typename = '(int|char|short|long|unsigned|float|double' . 97 '|\w+_t|struct\s+\w+|union\s+\w+|FILE)'; 98 99# mapping of old types to POSIX compatible types 100my %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' 110); 111 112my $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; 118 119my $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; 143 144my $err_stat = 0; # exit status 145 146if ($#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; 154 } 155 } 156} else { 157 &cstyle("<stdin>", *STDIN); 158} 159exit $err_stat; 160 161my $no_errs = 0; # set for CSTYLED-protected lines 162 163sub err($) { 164 my ($error) = @_; 165 unless ($no_errs) { 166 if ($verbose) { 167 printf $fmt, $filename, $., $error, $line; 168 } else { 169 printf $fmt, $filename, $., $error; 170 } 171 if ($github_workflow) { 172 printf "::error file=%s,line=%s::%s\n", $filename, $., $error; 173 } 174 $err_stat = 1; 175 } 176} 177 178sub 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; 186 } 187 $err_stat = 1; 188 } 189} 190 191sub 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; 198 } 199 $err_stat = 1; 200 } 201} 202 203sub cstyle($$) { 204 205my ($fn, $filehandle) = @_; 206$filename = $fn; # share it globally 207 208my $in_cpp = 0; 209my $next_in_cpp = 0; 210 211my $in_comment = 0; 212my $comment_done = 0; 213my $in_warlock_comment = 0; 214my $in_macro_call = 0; 215my $in_function = 0; 216my $in_function_header = 0; 217my $function_header_full_indent = 0; 218my $in_declaration = 0; 219my $note_level = 0; 220my $nextok = 0; 221my $nocheck = 0; 222 223my $in_string = 0; 224 225my ($okmsg, $comment_prefix); 226 227$line = ''; 228$prev = ''; 229reset_indent(); 230 231line: while (<$filehandle>) { 232 s/\r?\n$//; # strip return and newline 233 234 # save the original line, then remove all text from within 235 # double or single quotes, we do not want to check such text. 236 237 $line = $_; 238 239 # 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. 243 # 244 # (we assume that no-one will use backslash-continuation with character 245 # constants) 246 # 247 $_ = '"' . $_ if ($in_string && !$nocheck && !$in_comment); 248 249 # 250 # normal strings and characters 251 # 252 s/'([^\\']|\\[^xX0]|\\0[0-9]*|\\[xX][0-9a-fA-F]*)'/''/g; 253 s/"([^\\"]|\\.)*"/\"\"/g; 254 255 # 256 # detect string continuation 257 # 258 if ($nocheck || $in_comment) { 259 $in_string = 0; 260 } else { 261 # 262 # Now that all full strings are replaced with "", we check 263 # for unfinished strings continuing onto the next line. 264 # 265 $in_string = 266 (s/([^"](?:"")*)"([^\\"]|\\.)*\\$/$1""/ || 267 s/^("")*"([^\\"]|\\.)*\\$/""/); 268 } 269 270 # 271 # figure out if we are in a cpp directive 272 # 273 $in_cpp = $next_in_cpp || /^\s*#/; # continued or started 274 $next_in_cpp = $in_cpp && /\\$/; # only if continued 275 276 # strip off trailing backslashes, which appear in long macros 277 s/\s*\\$//; 278 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; 286 } 287 } 288 289 # a /*CSTYLED*/ comment indicates that the next line is ok. 290 if ($nextok) { 291 if ($okmsg) { 292 err($okmsg); 293 } 294 $nextok = 0; 295 $okmsg = 0; 296 if (/\/\* *CSTYLED.*\*\//) { 297 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/; 298 $okmsg = $1; 299 $nextok = 1; 300 } 301 $no_errs = 1; 302 } elsif ($no_errs) { 303 $no_errs = 0; 304 } 305 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"); 316 } 317 } 318 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; 324 } 325 326 # a /* BEGIN CSTYLED */ comment starts a no-check block. 327 if (/\/\* *BEGIN *CSTYLED *\*\//) { 328 $nocheck = 1; 329 } 330 331 # a /*CSTYLED*/ comment indicates that the next line is ok. 332 if (/\/\* *CSTYLED.*\*\//) { 333 /^.*\/\* *CSTYLED *(.*) *\*\/.*$/; 334 $okmsg = $1; 335 $nextok = 1; 336 } 337 if (/\/\/ *CSTYLED/) { 338 /^.*\/\/ *CSTYLED *(.*)$/; 339 $okmsg = $1; 340 $nextok = 1; 341 } 342 343 # universal checks; apply to everything 344 if (/\t +\t/) { 345 err("spaces between tabs"); 346 } 347 if (/ \t+ /) { 348 err("tabs between spaces"); 349 } 350 if (/\s$/) { 351 err("space or tab at end of line"); 352 } 353 if (/[^ \t(]\/\*/ && !/\w\(\/\*.*\*\/\);/) { 354 err("comment preceded by non-blank"); 355 } 356 if (/ARGSUSED/) { 357 err("ARGSUSED directive"); 358 } 359 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; 369 } 370 if (/^\}\s*(\/\*.*\*\/\s*)*$/) { 371 if ($prev =~ /^\s*return\s*;/) { 372 err_prev("unneeded return at end of function"); 373 } 374 $in_function = 0; 375 reset_indent(); # we don't check between functions 376 $prev = $line; 377 next line; 378 } 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 385 ) { 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)) { 394 395 err("continuation line should be indented by 4 spaces"); 396 } 397 } 398 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; 403 } 404 405 # 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. 409 # 410 if (!$in_macro_call && /^\w+\(/ && !/\) \w+;/) { 411 $in_function_header = 1; 412 if (/\($/) { 413 $function_header_full_indent = 1; 414 } 415 } 416 if ($in_function_header && /^\{$/) { 417 $in_function_header = 0; 418 $function_header_full_indent = 0; 419 $in_function = 1; 420 } 421 if ($in_function_header && /\);$/) { 422 $in_function_header = 0; 423 $function_header_full_indent = 0; 424 } 425 if ($in_function_header && /\{$/ ) { 426 if ($picky) { 427 err("opening brace on same line as function header"); 428 } 429 $in_function_header = 0; 430 $function_header_full_indent = 0; 431 $in_function = 1; 432 next line; 433 } 434 435 if ($in_warlock_comment && /\*\//) { 436 $in_warlock_comment = 0; 437 $prev = $line; 438 next line; 439 } 440 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; 445 } 446 447 if ($comment_done) { 448 $in_comment = 0; 449 $comment_done = 0; 450 } 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"); 455 } 456 $in_comment = 1; 457 /^(\s*)\//; 458 $comment_prefix = $1; 459 $prev = $line; 460 next line; 461 } 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"); 472 } 473 } 474 475 # check for errors that might occur in comments and in code. 476 477 # allow spaces to be used to draw pictures in all comments. 478 if (/[^ ] / && !/".* .*"/ && !$in_comment) { 479 err("spaces instead of tabs"); 480 } 481 if (/^ / && !/^ \*[ \t\/]/ && !/^ \*$/ && 482 (!/^ (\w|\.)/ || $in_function != 0)) { 483 err("indent by spaces instead of tabs"); 484 } 485 if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) { 486 err("continuation line not indented by 4 spaces"); 487 } 488 if (/$warlock_re/ && !/\*\//) { 489 $in_warlock_comment = 1; 490 $prev = $line; 491 next line; 492 } 493 if (/^\s*\/\*./ && !/^\s*\/\*.*\*\// && !/$hdr_comment_start/) { 494 err("improper first line of block comment"); 495 } 496 497 if ($in_comment) { # still in comment, don't do further checks 498 $prev = $line; 499 next line; 500 } 501 502 if ((/[^(]\/\*\S/ || /^\/\*\S/) && !/$lint_re/) { 503 err("missing blank after open comment"); 504 } 505 if (/\S\*\/[^)]|\S\*\/$/ && !/$lint_re/) { 506 err("missing blank before close comment"); 507 } 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"); 513 } 514 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)"); 530 } 531 } 532 next line; 533 } 534 535 # 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. 539 # 540 s/\/\*.*?\*\///g; 541 s/\/\/(?:\s.*)?$//; # Valid C++ comments 542 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!//.*$!!) { # C++ comments 548 err("missing blank after start comment"); 549 } 550 551 # delete any trailing whitespace; we have already checked for that. 552 s/\s*$//; 553 554 # following checks do not apply to text in comments. 555 556 if (/[^<>\s][!<>=]=/ || /[^<>][!<>=]=[^\s,]/ || 557 (/[^->]>[^,=>\s]/ && !/[^->]>$/) || 558 (/[^<]<[^,=<\s]/ && !/[^<]<$/) || 559 /[^<\s]<[^<]/ || /[^->\s]>[^>]/) { 560 err("missing space around relational operator"); 561 } 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"); 569 } 570 } 571 if (/[,;]\S/ && !/\bfor \(;;\)/) { 572 err("comma or semicolon followed by non-blank"); 573 } 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"); 579 } 580 if (/^\s*(&&|\|\|)/) { 581 err("improper boolean continuation"); 582 } 583 if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) { 584 err("more than one space around boolean operator"); 585 } 586 if (/\b(for|if|while|switch|sizeof|return|case)\(/) { 587 err("missing space between keyword and paren"); 588 } 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"); 592 } 593 if (/\b(for|if|while|switch|sizeof|return|case)\s\s+\(/ && 594 !/^#if\s+\(/) { 595 err("extra space between keyword and paren"); 596 } 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"); 611 } 612 $_ = $s; 613 } 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|)*$/ && 618# !/^(extern|static)\b/) { 619# err("return type of function not on separate line"); 620# } 621 # this is a close approximation 622 if (/^(\w+(\s|\*)+)+\w+\(.*\)(\s|)*$/ && 623 !/^(extern|static)\b/) { 624 err("return type of function not on separate line"); 625 } 626 if (/^#define /) { 627 err("#define followed by space instead of tab"); 628 } 629 if (/^\s*return\W[^;]*;/ && !/^\s*return\s*\(.*\);/) { 630 err("unparenthesized return expression"); 631 } 632 if (/\bsizeof\b/ && !/\bsizeof\s*\(.*\)/) { 633 err("unparenthesized sizeof expression"); 634 } 635 if (/\(\s/) { 636 err("whitespace after left paren"); 637 } 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"); 642 } 643 if (/^\s*\(void\)[^ ]/) { 644 err("missing space after (void) cast"); 645 } 646 if (/\S\{/ && !/\{\{/) { 647 err("missing space before left brace"); 648 } 649 if ($in_function && /^\s+\{/ && 650 ($prev =~ /\)\s*$/ || $prev =~ /\bstruct\s+\w+$/)) { 651 err("left brace starting a line"); 652 } 653 if (/\}(else|while)/) { 654 err("missing space after right brace"); 655 } 656 if (/\}\s\s+(else|while)/) { 657 err("extra space after right brace"); 658 } 659 if (/\b_VOID\b|\bVOID\b|\bSTATIC\b/) { 660 err("obsolete use of VOID or STATIC"); 661 } 662 if (/\b$typename\*/o) { 663 err("missing space between type name and *"); 664 } 665 if (/^\s+#/) { 666 err("preprocessor statement not in column 1"); 667 } 668 if (/^#\s/) { 669 err("blank after preprocessor #"); 670 } 671 if (/!\s*(strcmp|strncmp|bcmp)\s*\(/) { 672 err("don't use boolean ! with comparison functions"); 673 } 674 675 # 676 # We completely ignore, for purposes of indentation: 677 # * lines outside of functions 678 # * preprocessor lines 679 # 680 if ($check_continuation && $in_function && !$in_cpp) { 681 process_indent($_); 682 } 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"); 692 } 693 if (/\b$typename\s*\*\s/o && 694 !/\b$typename\s*\*\s+const\b/o) { 695 err("unary * followed by space"); 696 } 697 } 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. 702 # 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"); 707 } 708 } 709 if (/^\s*else\W/) { 710 if ($prev =~ /^\s*\}$/) { 711 err_prefix($prev, 712 "else and right brace should be on same line"); 713 } 714 } 715 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; 721 } 722 723 $prev = $line; 724} 725 726if ($prev eq "") { 727 err("last line in file is blank"); 728} 729 730} 731 732# 733# Continuation-line checking 734# 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). 738# 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. 742# 743 744my $cont_in; # currently inside of a continuation 745my $cont_off; # skipping an initializer or definition 746my $cont_noerr; # suppress cascading errors 747my $cont_start; # the line being continued 748my $cont_base; # the base indentation 749my $cont_first; # this is the first line of a statement 750my $cont_multiseg; # this continuation has multiple segments 751 752my $cont_special; # this is a C statement (if, for, etc.) 753my $cont_macro; # this is a macro 754my $cont_case; # this is a multi-line case 755 756my @cont_paren; # the stack of unmatched ( and [s we've seen 757 758sub 759reset_indent() 760{ 761 $cont_in = 0; 762 $cont_off = 0; 763} 764 765sub 766delabel($) 767{ 768 # 769 # replace labels with tabs. Note that there may be multiple 770 # labels on a line. 771 # 772 local $_ = $_[0]; 773 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); 779 } 780 $_ .= ("\t" x (length($label) / 8)).$rest; 781 } 782 783 return ($_); 784} 785 786sub 787process_indent($) 788{ 789 require strict; 790 local $_ = $_[0]; # preserve the global $_ 791 792 s///g; # No comments 793 s/\s+$//; # Strip trailing whitespace 794 795 return if (/^$/); # skip empty lines 796 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[^:]*$'; 801 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; 809 } 810 if ($cont_off) { 811 $cont_off += tr/{/{/ - tr/}/}/; 812 return; 813 } 814 815 if (!$cont_in) { 816 $cont_start = $line; 817 818 if (/^\t* /) { 819 err("non-continuation indented 4 spaces"); 820 $cont_noerr = 1; # stop reporting 821 } 822 $_ = delabel($_); # replace labels with tabs 823 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 (/\}[,;]?$/); 830 831 # Allow macros on their own lines 832 return if (/^\s*[A-Z_][A-Z_0-9]*$/); 833 834 # cases we don't deal with, generally non-kosher 835 if (/\{/) { 836 err("stuff after {"); 837 return; 838 } 839 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; 847 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; 854 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"); 860 } 861 } 862 863 my $rest = $_; # keeps the remainder of the line 864 865 # 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. 873 # 874 foreach $_ (split /[^\(\)\[\]\{\}\;\:]*/) { 875 next if (length($_) == 0); 876 877 # rest contains the remainder of the line 878 my $rxp = "[^\Q$_\E]*\Q$_\E"; 879 $rest =~ s/^$rxp//; 880 881 if (/\(/ || /\[/) { 882 push @cont_paren, $_; 883 } elsif (/\)/ || /\]/) { 884 my $cur = $_; 885 tr/\)\]/\(\[/; 886 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; 896 } 897 898 # 899 # If the stack is now empty, do special processing 900 # for if/for/while/switch and macro statements. 901 # 902 next if (@cont_paren != 0); 903 if ($cont_special) { 904 if ($rest =~ /^\s*\{?$/) { 905 $cont_in = 0; 906 last; 907 } 908 if ($rest =~ /^\s*;$/) { 909 err("empty if/for/while body ". 910 "not on its own line"); 911 $cont_in = 0; 912 last; 913 } 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; 921 } 922 # We've finished this section, start 923 # processing the next. 924 goto section_ended; 925 } 926 if ($cont_macro) { 927 if ($rest =~ /^$/) { 928 $cont_in = 0; 929 last; 930 } 931 } 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; 944 } 945 if ($rest =~ /^$/) { 946 $cont_in = 0; 947 last; 948 } 949 if ($rest =~ /^\s*special/) { 950 err("if/for/while/switch not started ". 951 "on its own line"); 952 } 953 goto section_ended; 954 } 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 }"); 967 } 968 $cont_in = 0; 969 last; 970 } 971 } elsif (/\:/ && $cont_case && @cont_paren == 0) { 972 err("stuff after multi-line case") if ($rest !~ /$^/); 973 $cont_in = 0; 974 last; 975 } 976 next; 977section_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; 985 } 986 $cont_noerr = 0 if (!$cont_in); 987} 988