Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit) |
| 3 | # (c) 2005, Joel Scohpp <jschopp@austin.ibm.com> (the ugly bit) |
| 4 | # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc) |
| 5 | # Licensed under the terms of the GNU GPL License version 2 |
| 6 | |
| 7 | use strict; |
| 8 | |
| 9 | my $P = $0; |
| 10 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 11 | my $V = '0.03'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 12 | |
| 13 | use Getopt::Long qw(:config no_auto_abbrev); |
| 14 | |
| 15 | my $quiet = 0; |
| 16 | my $tree = 1; |
| 17 | my $chk_signoff = 1; |
| 18 | my $chk_patch = 1; |
| 19 | GetOptions( |
| 20 | 'q|quiet' => \$quiet, |
| 21 | 'tree!' => \$tree, |
| 22 | 'signoff!' => \$chk_signoff, |
| 23 | 'patch!' => \$chk_patch, |
| 24 | ) or exit; |
| 25 | |
| 26 | my $exit = 0; |
| 27 | |
| 28 | if ($#ARGV < 0) { |
| 29 | print "usage: patchstylecheckemail.pl [options] patchfile\n"; |
| 30 | print "version: $V\n"; |
| 31 | print "options: -q => quiet\n"; |
| 32 | print " --no-tree => run without a kernel tree\n"; |
| 33 | exit(1); |
| 34 | } |
| 35 | |
| 36 | if ($tree && !top_of_kernel_tree()) { |
| 37 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 38 | exit(2); |
| 39 | } |
| 40 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 41 | my @dep_includes = (); |
| 42 | my @dep_functions = (); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 43 | my $removal = 'Documentation/feature-removal-schedule.txt'; |
| 44 | if ($tree && -f $removal) { |
| 45 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; |
| 46 | while (<REMOVE>) { |
| 47 | if (/^Files:\s+(.*\S)/) { |
| 48 | for my $file (split(/[, ]+/, $1)) { |
| 49 | if ($file =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 50 | push(@dep_includes, $1); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 53 | |
| 54 | } elsif (/^Funcs:\s+(.*\S)/) { |
| 55 | for my $func (split(/[, ]+/, $1)) { |
| 56 | push(@dep_functions, $func); |
| 57 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | my @lines = (); |
| 63 | while (<>) { |
| 64 | chomp; |
| 65 | push(@lines, $_); |
| 66 | if (eof(ARGV)) { |
| 67 | if (!process($ARGV, @lines)) { |
| 68 | $exit = 1; |
| 69 | } |
| 70 | @lines = (); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | exit($exit); |
| 75 | |
| 76 | sub top_of_kernel_tree { |
| 77 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && |
| 78 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && |
| 79 | (-d "Documentation") && (-d "arch") && (-d "include") && |
| 80 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && |
| 81 | (-d "kernel") && (-d "lib") && (-d "scripts")) { |
| 82 | return 1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | sub expand_tabs { |
| 88 | my ($str) = @_; |
| 89 | |
| 90 | my $res = ''; |
| 91 | my $n = 0; |
| 92 | for my $c (split(//, $str)) { |
| 93 | if ($c eq "\t") { |
| 94 | $res .= ' '; |
| 95 | $n++; |
| 96 | for (; ($n % 8) != 0; $n++) { |
| 97 | $res .= ' '; |
| 98 | } |
| 99 | next; |
| 100 | } |
| 101 | $res .= $c; |
| 102 | $n++; |
| 103 | } |
| 104 | |
| 105 | return $res; |
| 106 | } |
| 107 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 108 | sub line_stats { |
| 109 | my ($line) = @_; |
| 110 | |
| 111 | # Drop the diff line leader and expand tabs |
| 112 | $line =~ s/^.//; |
| 113 | $line = expand_tabs($line); |
| 114 | |
| 115 | # Pick the indent from the front of the line. |
| 116 | my ($white) = ($line =~ /^(\s*)/); |
| 117 | |
| 118 | return (length($line), length($white)); |
| 119 | } |
| 120 | |
| 121 | sub ctx_block_get { |
| 122 | my ($linenr, $remain, $outer) = @_; |
| 123 | my $line; |
| 124 | my $start = $linenr - 1; |
| 125 | my $end = $linenr - 1 + $remain; |
| 126 | my $blk = ''; |
| 127 | my @o; |
| 128 | my @c; |
| 129 | my @res = (); |
| 130 | |
| 131 | for ($line = $start; $line < $end; $line++) { |
| 132 | $blk .= $lines[$line]; |
| 133 | |
| 134 | @o = ($blk =~ /\{/g); |
| 135 | @c = ($blk =~ /\}/g); |
| 136 | |
| 137 | if (!$outer || (scalar(@o) - scalar(@c)) == 1) { |
| 138 | push(@res, $lines[$line]); |
| 139 | } |
| 140 | |
| 141 | last if (scalar(@o) == scalar(@c)); |
| 142 | } |
| 143 | |
| 144 | return @res; |
| 145 | } |
| 146 | sub ctx_block_outer { |
| 147 | my ($linenr, $remain) = @_; |
| 148 | |
| 149 | return ctx_block_get($linenr, $remain, 1); |
| 150 | } |
| 151 | sub ctx_block { |
| 152 | my ($linenr, $remain) = @_; |
| 153 | |
| 154 | return ctx_block_get($linenr, $remain, 0); |
| 155 | } |
| 156 | |
| 157 | sub ctx_locate_comment { |
| 158 | my ($first_line, $end_line) = @_; |
| 159 | |
| 160 | # Catch a comment on the end of the line itself. |
| 161 | my ($current_comment) = ($lines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
| 162 | return $current_comment if (defined $current_comment); |
| 163 | |
| 164 | # Look through the context and try and figure out if there is a |
| 165 | # comment. |
| 166 | my $in_comment = 0; |
| 167 | $current_comment = ''; |
| 168 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
| 169 | my $line = $lines[$linenr - 1]; |
| 170 | ##warn " $line\n"; |
| 171 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 172 | $in_comment = 1; |
| 173 | } |
| 174 | if ($line =~ m@/\*@) { |
| 175 | $in_comment = 1; |
| 176 | } |
| 177 | if (!$in_comment && $current_comment ne '') { |
| 178 | $current_comment = ''; |
| 179 | } |
| 180 | $current_comment .= $line . "\n" if ($in_comment); |
| 181 | if ($line =~ m@\*/@) { |
| 182 | $in_comment = 0; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | chomp($current_comment); |
| 187 | return($current_comment); |
| 188 | } |
| 189 | sub ctx_has_comment { |
| 190 | my ($first_line, $end_line) = @_; |
| 191 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 192 | |
| 193 | ##print "LINE: $lines[$end_line - 1 ]\n"; |
| 194 | ##print "CMMT: $cmt\n"; |
| 195 | |
| 196 | return ($cmt ne ''); |
| 197 | } |
| 198 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 199 | sub cat_vet { |
| 200 | my ($vet) = @_; |
| 201 | |
| 202 | $vet =~ s/\t/^I/; |
| 203 | $vet =~ s/$/\$/; |
| 204 | |
| 205 | return $vet; |
| 206 | } |
| 207 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 208 | sub has_non_quoted { |
| 209 | return ($_[0] =~ m{$_[1]} and $_[0] !~ m{\".*$_[1].*\"}); |
| 210 | } |
| 211 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 212 | sub process { |
| 213 | my $filename = shift; |
| 214 | my @lines = @_; |
| 215 | |
| 216 | my $linenr=0; |
| 217 | my $prevline=""; |
| 218 | my $stashline=""; |
| 219 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 220 | my $length; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 221 | my $indent; |
| 222 | my $previndent=0; |
| 223 | my $stashindent=0; |
| 224 | |
| 225 | my $clean = 1; |
| 226 | my $signoff = 0; |
| 227 | my $is_patch = 0; |
| 228 | |
| 229 | # Trace the real file/line as we go. |
| 230 | my $realfile = ''; |
| 231 | my $realline = 0; |
| 232 | my $realcnt = 0; |
| 233 | my $here = ''; |
| 234 | my $in_comment = 0; |
| 235 | my $first_line = 0; |
| 236 | |
| 237 | foreach my $line (@lines) { |
| 238 | $linenr++; |
| 239 | |
| 240 | #extract the filename as it passes |
| 241 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 242 | $realfile=$1; |
| 243 | $in_comment = 0; |
| 244 | next; |
| 245 | } |
| 246 | #extract the line range in the file after the patch is applied |
| 247 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { |
| 248 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 249 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 250 | $in_comment = 0; |
| 251 | $realline=$1-1; |
| 252 | if (defined $2) { |
| 253 | $realcnt=$3+1; |
| 254 | } else { |
| 255 | $realcnt=1+1; |
| 256 | } |
| 257 | next; |
| 258 | } |
| 259 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 260 | # track the line number as we move through the hunk, note that |
| 261 | # new versions of GNU diff omit the leading space on completely |
| 262 | # blank context lines so we need to count that too. |
| 263 | if ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 264 | $realline++; |
| 265 | $realcnt-- if ($realcnt != 0); |
| 266 | |
| 267 | # track any sort of multi-line comment. Obviously if |
| 268 | # the added text or context do not include the whole |
| 269 | # comment we will not see it. Such is life. |
| 270 | # |
| 271 | # Guestimate if this is a continuing comment. If this |
| 272 | # is the start of a diff block and this line starts |
| 273 | # ' *' then it is very likely a comment. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 274 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 275 | $in_comment = 1; |
| 276 | } |
| 277 | if ($line =~ m@/\*@) { |
| 278 | $in_comment = 1; |
| 279 | } |
| 280 | if ($line =~ m@\*/@) { |
| 281 | $in_comment = 0; |
| 282 | } |
| 283 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 284 | # Measure the line length and indent. |
| 285 | ($length, $indent) = line_stats($line); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 286 | |
| 287 | # Track the previous line. |
| 288 | ($prevline, $stashline) = ($stashline, $line); |
| 289 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | #make up the handle for any error we report on this line |
| 293 | $here = "PATCH: $ARGV:$linenr:"; |
| 294 | $here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0); |
| 295 | |
| 296 | my $herecurr = "$here\n$line\n\n"; |
| 297 | my $hereprev = "$here\n$prevline\n$line\n\n"; |
| 298 | |
| 299 | #check the patch for a signoff: |
| 300 | if ($line =~ /^\s*Signed-off-by:\s/) { |
| 301 | $signoff++; |
| 302 | |
| 303 | } elsif ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 304 | # This is a signoff, if ugly, so do not double report. |
| 305 | $signoff++; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 306 | if (!($line =~ /^\s*Signed-off-by:/)) { |
| 307 | print "use Signed-off-by:\n"; |
| 308 | print "$herecurr"; |
| 309 | $clean = 0; |
| 310 | } |
| 311 | if ($line =~ /^\s*signed-off-by:\S/i) { |
| 312 | print "need space after Signed-off-by:\n"; |
| 313 | print "$herecurr"; |
| 314 | $clean = 0; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | #ignore lines not being added |
| 319 | if ($line=~/^[^\+]/) {next;} |
| 320 | |
| 321 | # check we are in a valid source file *.[hcsS] if not then ignore this hunk |
| 322 | next if ($realfile !~ /\.[hcsS]$/); |
| 323 | |
| 324 | #trailing whitespace |
| 325 | if ($line=~/\S\s+$/) { |
| 326 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; |
| 327 | print "trailing whitespace\n"; |
| 328 | print "$herevet"; |
| 329 | $clean = 0; |
| 330 | } |
| 331 | #80 column limit |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 332 | if (!($prevline=~/\/\*\*/) && $length > 80) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 333 | print "line over 80 characters\n"; |
| 334 | print "$herecurr"; |
| 335 | $clean = 0; |
| 336 | } |
| 337 | |
| 338 | # check we are in a valid source file *.[hc] if not then ignore this hunk |
| 339 | next if ($realfile !~ /\.[hc]$/); |
| 340 | |
| 341 | # at the beginning of a line any tabs must come first and anything |
| 342 | # more than 8 must use tabs. |
| 343 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { |
| 344 | my $herevet = "$here\n" . cat_vet($line) . "\n\n"; |
| 345 | print "use tabs not spaces\n"; |
| 346 | print "$herevet"; |
| 347 | $clean = 0; |
| 348 | } |
| 349 | |
| 350 | # |
| 351 | # The rest of our checks refer specifically to C style |
| 352 | # only apply those _outside_ comments. |
| 353 | # |
| 354 | next if ($in_comment); |
| 355 | |
| 356 | # no C99 // comments |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 357 | if (has_non_quoted($line, '//')) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 358 | print "do not use C99 // comments\n"; |
| 359 | print "$herecurr"; |
| 360 | $clean = 0; |
| 361 | } |
| 362 | |
| 363 | # Remove comments from the line before processing. |
| 364 | $line =~ s@/\*.*\*/@@g; |
| 365 | $line =~ s@/\*.*@@; |
| 366 | $line =~ s@.*\*/@@; |
| 367 | $line =~ s@//.*@@; |
| 368 | |
| 369 | #EXPORT_SYMBOL should immediately follow its function closing }. |
| 370 | if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) || |
| 371 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) { |
| 372 | if (($prevline !~ /^}/) && |
| 373 | ($prevline !~ /^\+}/) && |
| 374 | ($prevline !~ /^ }/)) { |
| 375 | print "EXPORT_SYMBOL(func); should immediately follow its function\n"; |
| 376 | print "$herecurr"; |
| 377 | $clean = 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | # check for static initialisers. |
| 382 | if ($line=~/\s*static\s.*=\s+(0|NULL);/) { |
| 383 | print "do not initialise statics to 0 or NULL\n"; |
| 384 | print "$herecurr"; |
| 385 | $clean = 0; |
| 386 | } |
| 387 | |
| 388 | # check for new typedefs. |
| 389 | if ($line=~/\s*typedef\s/) { |
| 390 | print "do not add new typedefs\n"; |
| 391 | print "$herecurr"; |
| 392 | $clean = 0; |
| 393 | } |
| 394 | |
| 395 | # * goes on variable not on type |
| 396 | if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) { |
| 397 | print "\"foo* bar\" should be \"foo *bar\"\n"; |
| 398 | print "$herecurr"; |
| 399 | $clean = 0; |
| 400 | } |
| 401 | |
| 402 | # # no BUG() or BUG_ON() |
| 403 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 404 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 405 | # print "$herecurr"; |
| 406 | # $clean = 0; |
| 407 | # } |
| 408 | |
| 409 | # printk should use KERN_* levels |
| 410 | if ($line =~ /\bprintk\((?!KERN_)/) { |
| 411 | print "printk() should include KERN_ facility level\n"; |
| 412 | print "$herecurr"; |
| 413 | $clean = 0; |
| 414 | } |
| 415 | |
| 416 | #function brace can't be on same line, except for #defines of do while, or if closed on same line |
| 417 | if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and |
| 418 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
| 419 | print "braces following function declarations go on the next line\n"; |
| 420 | print "$herecurr"; |
| 421 | $clean = 0; |
| 422 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 423 | # Note we expand the line with the leading + as the real |
| 424 | # line will be displayed with the leading + and the tabs |
| 425 | # will therefore also expand that way. |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 426 | my $opline = $line; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 427 | $opline = expand_tabs($opline); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 428 | $opline =~ s/^.//; |
| 429 | if (!($line=~/\#\s*include/)) { |
| 430 | # Check operator spacing. |
| 431 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 432 | my $off = 1; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 433 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 434 | $off += length($elements[$n]); |
| 435 | |
| 436 | my $a = ''; |
| 437 | $a = 'V' if ($elements[$n] ne ''); |
| 438 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 439 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 440 | $a = 'O' if ($elements[$n] eq ''); |
| 441 | $a = 'E' if ($elements[$n] eq '' && $n == 0); |
| 442 | |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 443 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 444 | |
| 445 | my $c = ''; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 446 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 447 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 448 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 449 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 450 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 451 | } else { |
| 452 | $c = 'E'; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 455 | my $ctx = "${a}x${c}"; |
| 456 | |
| 457 | my $at = "(ctx:$ctx)"; |
| 458 | |
| 459 | my $ptr = (" " x $off) . "^"; |
| 460 | my $hereptr = "$here\n$line\n$ptr\n\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 461 | |
| 462 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; |
| 463 | # Skip things apparently in quotes. |
| 464 | next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); |
| 465 | |
| 466 | # We need ; as an operator. // is a comment. |
| 467 | if ($op eq ';' or $op eq '//') { |
| 468 | |
| 469 | # -> should have no spaces |
| 470 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 471 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 472 | print "no spaces around that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 473 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 474 | $clean = 0; |
| 475 | } |
| 476 | |
| 477 | # , must have a space on the right. |
| 478 | } elsif ($op eq ',') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 479 | if ($ctx !~ /.xW|.xE/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 480 | print "need space after that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 481 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 482 | $clean = 0; |
| 483 | } |
| 484 | |
| 485 | # unary ! and unary ~ are allowed no space on the right |
| 486 | } elsif ($op eq '!' or $op eq '~') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 487 | if ($ctx !~ /[WOEB]x./) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 488 | print "need space before that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 489 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 490 | $clean = 0; |
| 491 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 492 | if ($ctx =~ /.xW/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 493 | print "no space after that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 494 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 495 | $clean = 0; |
| 496 | } |
| 497 | |
| 498 | # unary ++ and unary -- are allowed no space on one side. |
| 499 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 500 | if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 501 | print "need space one side of that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 502 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 503 | $clean = 0; |
| 504 | } |
| 505 | |
| 506 | # & is both unary and binary |
| 507 | # unary: |
| 508 | # a &b |
| 509 | # binary (consistent spacing): |
| 510 | # a&b OK |
| 511 | # a & b OK |
| 512 | # |
| 513 | # boiling down to: if there is a space on the right then there |
| 514 | # should be one on the left. |
| 515 | # |
| 516 | # - is the same |
| 517 | # |
| 518 | # * is the same only adding: |
| 519 | # type: |
| 520 | # (foo *) |
| 521 | # (foo **) |
| 522 | # |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 523 | } elsif ($op eq '&' or $op eq '-') { |
| 524 | if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 525 | print "need space before that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 526 | print "$hereptr"; |
| 527 | $clean = 0; |
| 528 | } |
| 529 | |
| 530 | } elsif ($op eq '*') { |
| 531 | if ($ctx !~ /VxV|[EWB]x[WE]|[EWB]x[VO]|[EWO]x[OBV]/) { |
| 532 | print "need space before that '$op' $at\n"; |
| 533 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 534 | $clean = 0; |
| 535 | } |
| 536 | |
| 537 | # << and >> may either have or not have spaces both sides |
| 538 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or |
| 539 | $op eq '^' or $op eq '|') |
| 540 | { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 541 | if ($ctx !~ /VxV|WxW|VxE|WxE/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 542 | print "need consistent spacing around '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 543 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 544 | $clean = 0; |
| 545 | } |
| 546 | |
| 547 | # All the others need spaces both sides. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 548 | } elsif ($ctx !~ /[EW]x[WE]/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 549 | print "need spaces around that '$op' $at\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 550 | print "$hereptr"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 551 | $clean = 0; |
| 552 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 553 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | #need space before brace following if, while, etc |
| 558 | if ($line=~/\(.*\){/) { |
| 559 | print "need a space before the brace\n"; |
| 560 | print "$herecurr"; |
| 561 | $clean = 0; |
| 562 | } |
| 563 | |
| 564 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 565 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 566 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
| 567 | print "labels should not be indented\n"; |
| 568 | print "$herecurr"; |
| 569 | $clean = 0; |
| 570 | } |
| 571 | |
| 572 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 573 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 574 | print "need a space before the open parenthesis\n"; |
| 575 | print "$herecurr"; |
| 576 | $clean = 0; |
| 577 | } |
| 578 | |
| 579 | # Check for illegal assignment in if conditional. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 580 | if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) { |
| 581 | print "do not use assignment in condition\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 582 | print "$herecurr"; |
| 583 | $clean = 0; |
| 584 | } |
| 585 | |
| 586 | # Check for }<nl>else {, these must be at the same |
| 587 | # indent level to be relevant to each other. |
| 588 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 589 | $previndent == $indent) { |
| 590 | print "else should follow close brace\n"; |
| 591 | print "$hereprev"; |
| 592 | $clean = 0; |
| 593 | } |
| 594 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 595 | # Check for switch () and associated case and default |
| 596 | # statements should be at the same indent. |
| 597 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 598 | my $err = ''; |
| 599 | my $sep = ''; |
| 600 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 601 | shift(@ctx); |
| 602 | for my $ctx (@ctx) { |
| 603 | my ($clen, $cindent) = line_stats($ctx); |
| 604 | if ($ctx =~ /\s*(case\s+|default:)/ && |
| 605 | $indent != $cindent) { |
| 606 | $err .= "$sep$ctx\n"; |
| 607 | $sep = ''; |
| 608 | } else { |
| 609 | $sep = "[...]\n"; |
| 610 | } |
| 611 | } |
| 612 | if ($err ne '') { |
| 613 | print "switch and case should be at the same indent\n"; |
| 614 | print "$here\n$line\n$err\n"; |
| 615 | $clean = 0; |
| 616 | } |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 620 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 621 | # print "No studly caps, use _\n"; |
| 622 | # print "$herecurr"; |
| 623 | # $clean = 0; |
| 624 | # } |
| 625 | |
| 626 | #no spaces allowed after \ in define |
| 627 | if ($line=~/\#define.*\\\s$/) { |
| 628 | print("Whitepspace after \\ makes next lines useless\n"); |
| 629 | print "$herecurr"; |
| 630 | $clean = 0; |
| 631 | } |
| 632 | |
| 633 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available. |
| 634 | if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) { |
| 635 | my $checkfile = "include/linux/$1.h"; |
| 636 | if (-f $checkfile) { |
| 637 | print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; |
| 638 | print $herecurr; |
| 639 | $clean = 0; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 644 | if ($prevline=~/\b(if|while|for|switch)\s*\(/) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 645 | my @opened = $prevline=~/\(/g; |
| 646 | my @closed = $prevline=~/\)/g; |
| 647 | my $nr_line = $linenr; |
| 648 | my $remaining = $realcnt; |
| 649 | my $next_line = $line; |
| 650 | my $extra_lines = 0; |
| 651 | my $display_segment = $prevline; |
| 652 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 653 | while ($remaining > 1 && scalar @opened > scalar @closed) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 654 | $prevline .= $next_line; |
| 655 | $display_segment .= "\n" . $next_line; |
| 656 | $next_line = $lines[$nr_line]; |
| 657 | $nr_line++; |
| 658 | $remaining--; |
| 659 | |
| 660 | @opened = $prevline=~/\(/g; |
| 661 | @closed = $prevline=~/\)/g; |
| 662 | } |
| 663 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 664 | if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and |
| 665 | !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 666 | print "That { should be on the previous line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 667 | print "$here\n$display_segment\n$next_line\n\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 668 | $clean = 0; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | #multiline macros should be enclosed in a do while loop |
| 673 | if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and |
| 674 | !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and |
| 675 | !($line=~/do.*{/) and !($line=~/\(\{/)) { |
| 676 | print "Macros with multiple statements should be enclosed in a do - while loop\n"; |
| 677 | print "$hereprev"; |
| 678 | $clean = 0; |
| 679 | } |
| 680 | |
| 681 | # don't include deprecated include files |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 682 | for my $inc (@dep_includes) { |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 683 | if ($line =~ m@\#\s*include\s*\<$inc>@) { |
| 684 | print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; |
| 685 | print "$herecurr"; |
| 686 | $clean = 0; |
| 687 | } |
| 688 | } |
| 689 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame^] | 690 | # don't use deprecated functions |
| 691 | for my $func (@dep_functions) { |
| 692 | if (has_non_quoted($line, '\b' . $func . '\b')) { |
| 693 | print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; |
| 694 | print "$herecurr"; |
| 695 | $clean = 0; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | # no volatiles please |
| 700 | if (has_non_quoted($line, '\bvolatile\b')) { |
| 701 | print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; |
| 702 | print "$herecurr"; |
| 703 | $clean = 0; |
| 704 | } |
| 705 | |
| 706 | # warn about #ifdefs in C files |
| 707 | if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 708 | print "#ifdef in C files should be avoided\n"; |
| 709 | print "$herecurr"; |
| 710 | $clean = 0; |
| 711 | } |
| 712 | |
| 713 | # check for spinlock_t definitions without a comment. |
| 714 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 715 | my $which = $1; |
| 716 | if (!ctx_has_comment($first_line, $linenr)) { |
| 717 | print "$1 definition without comment\n"; |
| 718 | print "$herecurr"; |
| 719 | $clean = 0; |
| 720 | } |
| 721 | } |
| 722 | # check for memory barriers without a comment. |
| 723 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 724 | if (!ctx_has_comment($first_line, $linenr)) { |
| 725 | print "memory barrier without comment\n"; |
| 726 | print "$herecurr"; |
| 727 | $clean = 0; |
| 728 | } |
| 729 | } |
| 730 | # check of hardware specific defines |
| 731 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { |
| 732 | print "architecture specific defines should be avoided\n"; |
Andy Whitcroft | 0a920b5b | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 733 | print "$herecurr"; |
| 734 | $clean = 0; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if ($chk_patch && !$is_patch) { |
| 739 | $clean = 0; |
| 740 | print "Does not appear to be a unified-diff format patch\n"; |
| 741 | } |
| 742 | if ($is_patch && $chk_signoff && $signoff == 0) { |
| 743 | $clean = 0; |
| 744 | print "Missing Signed-off-by: line(s)\n"; |
| 745 | } |
| 746 | |
| 747 | if ($clean == 1 && $quiet == 0) { |
| 748 | print "Your patch has no obvious style problems and is ready for submission.\n" |
| 749 | } |
| 750 | if ($clean == 0 && $quiet == 0) { |
| 751 | print "Your patch has style problems, please review. If any of these errors\n"; |
| 752 | print "are false positives report them to the maintainer, see\n"; |
| 753 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 754 | } |
| 755 | return $clean; |
| 756 | } |