| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use strict;
|
| 8 |
|
| 9 | my $P = $0;
|
| 10 |
|
| 11 | my $V = '0.01';
|
| 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 |
|
| 41 | my @deprecated = ();
|
| 42 | my $removal = 'Documentation/feature-removal-schedule.txt';
|
| 43 | if ($tree && -f $removal) {
|
| 44 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
|
| 45 | while (<REMOVE>) {
|
| 46 | if (/^Files:\s+(.*\S)/) {
|
| 47 | for my $file (split(/[, ]+/, $1)) {
|
| 48 | if ($file =~ m@include/(.*)@) {
|
| 49 | push(@deprecated, $1);
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | my @lines = ();
|
| 57 | while (<>) {
|
| 58 | chomp;
|
| 59 | push(@lines, $_);
|
| 60 | if (eof(ARGV)) {
|
| 61 | if (!process($ARGV, @lines)) {
|
| 62 | $exit = 1;
|
| 63 | }
|
| 64 | @lines = ();
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | exit($exit);
|
| 69 |
|
| 70 | sub top_of_kernel_tree {
|
| 71 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
|
| 72 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
|
| 73 | (-d "Documentation") && (-d "arch") && (-d "include") &&
|
| 74 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
|
| 75 | (-d "kernel") && (-d "lib") && (-d "scripts")) {
|
| 76 | return 1;
|
| 77 | }
|
| 78 | return 0;
|
| 79 | }
|
| 80 |
|
| 81 | sub expand_tabs {
|
| 82 | my ($str) = @_;
|
| 83 |
|
| 84 | my $res = '';
|
| 85 | my $n = 0;
|
| 86 | for my $c (split(//, $str)) {
|
| 87 | if ($c eq "\t") {
|
| 88 | $res .= ' ';
|
| 89 | $n++;
|
| 90 | for (; ($n % 8) != 0; $n++) {
|
| 91 | $res .= ' ';
|
| 92 | }
|
| 93 | next;
|
| 94 | }
|
| 95 | $res .= $c;
|
| 96 | $n++;
|
| 97 | }
|
| 98 |
|
| 99 | return $res;
|
| 100 | }
|
| 101 |
|
| 102 | sub cat_vet {
|
| 103 | my ($vet) = @_;
|
| 104 |
|
| 105 | $vet =~ s/\t/^I/;
|
| 106 | $vet =~ s/$/\$/;
|
| 107 |
|
| 108 | return $vet;
|
| 109 | }
|
| 110 |
|
| 111 | sub process {
|
| 112 | my $filename = shift;
|
| 113 | my @lines = @_;
|
| 114 |
|
| 115 | my $linenr=0;
|
| 116 | my $prevline="";
|
| 117 | my $stashline="";
|
| 118 |
|
| 119 | my $lineforcounting='';
|
| 120 | my $indent;
|
| 121 | my $previndent=0;
|
| 122 | my $stashindent=0;
|
| 123 |
|
| 124 | my $clean = 1;
|
| 125 | my $signoff = 0;
|
| 126 | my $is_patch = 0;
|
| 127 |
|
| 128 | |
| 129 | my $realfile = '';
|
| 130 | my $realline = 0;
|
| 131 | my $realcnt = 0;
|
| 132 | my $here = '';
|
| 133 | my $in_comment = 0;
|
| 134 | my $first_line = 0;
|
| 135 |
|
| 136 | foreach my $line (@lines) {
|
| 137 | $linenr++;
|
| 138 |
|
| 139 | |
| 140 | if ($line=~/^\+\+\+\s+(\S+)/) {
|
| 141 | $realfile=$1;
|
| 142 | $in_comment = 0;
|
| 143 | next;
|
| 144 | }
|
| 145 | |
| 146 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
|
| 147 | $is_patch = 1;
|
| 148 | $first_line = 1;
|
| 149 | $in_comment = 0;
|
| 150 | $realline=$1-1;
|
| 151 | if (defined $2) {
|
| 152 | $realcnt=$3+1;
|
| 153 | } else {
|
| 154 | $realcnt=1+1;
|
| 155 | }
|
| 156 | next;
|
| 157 | }
|
| 158 |
|
| 159 | |
| 160 | if ($line=~/^[ \+]/) {
|
| 161 | $realline++;
|
| 162 | $realcnt-- if ($realcnt != 0);
|
| 163 |
|
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | if ($first_line and $line =~ m@^.\s*\*@) {
|
| 172 | $in_comment = 1;
|
| 173 | }
|
| 174 | if ($line =~ m@/\*@) {
|
| 175 | $in_comment = 1;
|
| 176 | }
|
| 177 | if ($line =~ m@\*/@) {
|
| 178 | $in_comment = 0;
|
| 179 | }
|
| 180 |
|
| 181 | $lineforcounting = $line;
|
| 182 | $lineforcounting =~ s/^\+//;
|
| 183 | $lineforcounting = expand_tabs($lineforcounting);
|
| 184 |
|
| 185 | my ($white) = ($lineforcounting =~ /^(\s*)/);
|
| 186 | $indent = length($white);
|
| 187 |
|
| 188 | |
| 189 | ($prevline, $stashline) = ($stashline, $line);
|
| 190 | ($previndent, $stashindent) = ($stashindent, $indent);
|
| 191 | $first_line = 0;
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | $here = "PATCH: $ARGV:$linenr:";
|
| 196 | $here .= "\nFILE: $realfile:$realline:" if ($realcnt != 0);
|
| 197 |
|
| 198 | my $herecurr = "$here\n$line\n\n";
|
| 199 | my $hereprev = "$here\n$prevline\n$line\n\n";
|
| 200 |
|
| 201 | |
| 202 | if ($line =~ /^\s*Signed-off-by:\s/) {
|
| 203 | $signoff++;
|
| 204 |
|
| 205 | } elsif ($line =~ /^\s*signed-off-by:/i) {
|
| 206 | if (!($line =~ /^\s*Signed-off-by:/)) {
|
| 207 | print "use Signed-off-by:\n";
|
| 208 | print "$herecurr";
|
| 209 | $clean = 0;
|
| 210 | }
|
| 211 | if ($line =~ /^\s*signed-off-by:\S/i) {
|
| 212 | print "need space after Signed-off-by:\n";
|
| 213 | print "$herecurr";
|
| 214 | $clean = 0;
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | |
| 219 | if ($line=~/^[^\+]/) {next;}
|
| 220 |
|
| 221 | |
| 222 | next if ($realfile !~ /\.[hcsS]$/);
|
| 223 |
|
| 224 | |
| 225 | if ($line=~/\S\s+$/) {
|
| 226 | my $herevet = "$here\n" . cat_vet($line) . "\n\n";
|
| 227 | print "trailing whitespace\n";
|
| 228 | print "$herevet";
|
| 229 | $clean = 0;
|
| 230 | }
|
| 231 | |
| 232 | if (!($prevline=~/\/\*\*/) && length($lineforcounting) > 80) {
|
| 233 | print "line over 80 characters\n";
|
| 234 | print "$herecurr";
|
| 235 | $clean = 0;
|
| 236 | }
|
| 237 |
|
| 238 | |
| 239 | next if ($realfile !~ /\.[hc]$/);
|
| 240 |
|
| 241 | |
| 242 | |
| 243 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
|
| 244 | my $herevet = "$here\n" . cat_vet($line) . "\n\n";
|
| 245 | print "use tabs not spaces\n";
|
| 246 | print "$herevet";
|
| 247 | $clean = 0;
|
| 248 | }
|
| 249 |
|
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | next if ($in_comment);
|
| 255 |
|
| 256 | |
| 257 | if ($line =~ m@//@ and !($line =~ m@\".*//.*\"@)) {
|
| 258 | print "do not use C99 // comments\n";
|
| 259 | print "$herecurr";
|
| 260 | $clean = 0;
|
| 261 | }
|
| 262 |
|
| 263 | |
| 264 | $line =~ s@/\*.*\*/@@g;
|
| 265 | $line =~ s@/\*.*@@;
|
| 266 | $line =~ s@.*\*/@@;
|
| 267 | $line =~ s@//.*@@;
|
| 268 |
|
| 269 | |
| 270 | if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) ||
|
| 271 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) {
|
| 272 | if (($prevline !~ /^}/) &&
|
| 273 | ($prevline !~ /^\+}/) &&
|
| 274 | ($prevline !~ /^ }/)) {
|
| 275 | print "EXPORT_SYMBOL(func); should immediately follow its function\n";
|
| 276 | print "$herecurr";
|
| 277 | $clean = 0;
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | |
| 282 | if ($line=~/\s*static\s.*=\s+(0|NULL);/) {
|
| 283 | print "do not initialise statics to 0 or NULL\n";
|
| 284 | print "$herecurr";
|
| 285 | $clean = 0;
|
| 286 | }
|
| 287 |
|
| 288 | |
| 289 | if ($line=~/\s*typedef\s/) {
|
| 290 | print "do not add new typedefs\n";
|
| 291 | print "$herecurr";
|
| 292 | $clean = 0;
|
| 293 | }
|
| 294 |
|
| 295 | |
| 296 | if ($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) {
|
| 297 | print "\"foo* bar\" should be \"foo *bar\"\n";
|
| 298 | print "$herecurr";
|
| 299 | $clean = 0;
|
| 300 | }
|
| 301 |
|
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 |
|
| 309 | |
| 310 | if ($line =~ /\bprintk\((?!KERN_)/) {
|
| 311 | print "printk() should include KERN_ facility level\n";
|
| 312 | print "$herecurr";
|
| 313 | $clean = 0;
|
| 314 | }
|
| 315 |
|
| 316 | |
| 317 | if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and
|
| 318 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
|
| 319 | print "braces following function declarations go on the next line\n";
|
| 320 | print "$herecurr";
|
| 321 | $clean = 0;
|
| 322 | }
|
| 323 | my $opline = $line;
|
| 324 | $opline =~ s/^.//;
|
| 325 | if (!($line=~/\#\s*include/)) {
|
| 326 | |
| 327 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
|
| 328 | for (my $n = 0; $n < $#elements; $n += 2) {
|
| 329 | |
| 330 | |
| 331 | |
| 332 | my $w1 = $elements[$n] =~ /\s$/;
|
| 333 | my $s1 = $elements[$n] =~ /(\[|\(|\s)$/;
|
| 334 | my $o1 = $elements[$n] eq '';
|
| 335 | my $op = $elements[$n + 1];
|
| 336 | my $w2 = 1;
|
| 337 | my $s2 = 1;
|
| 338 | my $o2 = 0;
|
| 339 | |
| 340 | if (defined $elements[$n + 2]) {
|
| 341 | $w2 = $elements[$n + 2] =~ /^\s/;
|
| 342 | $s2 = $elements[$n + 2] =~ /^(\s|\)|\]|;)/;
|
| 343 | $o2 = $elements[$n + 2] eq '';
|
| 344 | }
|
| 345 |
|
| 346 | |
| 347 | my $at = "here: ";
|
| 348 | for (my $m = $n; $m >= 0; $m--) {
|
| 349 | if ($elements[$m] ne '') {
|
| 350 | $at .= $elements[$m];
|
| 351 | last;
|
| 352 | }
|
| 353 | }
|
| 354 | $at .= $op;
|
| 355 | for (my $m = $n + 2; defined $elements[$m]; $m++) {
|
| 356 | if ($elements[$m] ne '') {
|
| 357 | $at .= $elements[$m];
|
| 358 | last;
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | |
| 363 | |
| 364 | next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/);
|
| 365 |
|
| 366 | |
| 367 | if ($op eq ';' or $op eq '//') {
|
| 368 |
|
| 369 | |
| 370 | } elsif ($op eq '->') {
|
| 371 | if ($s1 or $s2) {
|
| 372 | print "no spaces around that '$op' $at\n";
|
| 373 | print "$herecurr";
|
| 374 | $clean = 0;
|
| 375 | }
|
| 376 |
|
| 377 | |
| 378 | } elsif ($op eq ',') {
|
| 379 | if (!$s2) {
|
| 380 | print "need space after that '$op' $at\n";
|
| 381 | print "$herecurr";
|
| 382 | $clean = 0;
|
| 383 | }
|
| 384 |
|
| 385 | |
| 386 | } elsif ($op eq '!' or $op eq '~') {
|
| 387 | if (!$s1 && !$o1) {
|
| 388 | print "need space before that '$op' $at\n";
|
| 389 | print "$herecurr";
|
| 390 | $clean = 0;
|
| 391 | }
|
| 392 | if ($s2) {
|
| 393 | print "no space after that '$op' $at\n";
|
| 394 | print "$herecurr";
|
| 395 | $clean = 0;
|
| 396 | }
|
| 397 |
|
| 398 | |
| 399 | } elsif ($op eq '++' or $op eq '--') {
|
| 400 | if (($s1 && $s2) || ((!$s1 && !$o1) && (!$s2 && !$o2))) {
|
| 401 | print "need space one side of that '$op' $at\n";
|
| 402 | print "$herecurr";
|
| 403 | $clean = 0;
|
| 404 | }
|
| 405 |
|
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | } elsif ($op eq '&' or $op eq '-' or $op eq '*') {
|
| 424 | if ($w2 and !$w1) {
|
| 425 | print "need space before that '$op' $at\n";
|
| 426 | print "$herecurr";
|
| 427 | $clean = 0;
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
|
| 432 | $op eq '^' or $op eq '|')
|
| 433 | {
|
| 434 | if ($s1 != $s2) {
|
| 435 | print "need consistent spacing around '$op' $at\n";
|
| 436 | print "$herecurr";
|
| 437 | $clean = 0;
|
| 438 | }
|
| 439 |
|
| 440 | |
| 441 | } elsif (!$s1 or !$s2) {
|
| 442 | print "need spaces around that '$op' $at\n";
|
| 443 | print "$herecurr";
|
| 444 | $clean = 0;
|
| 445 | }
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | |
| 450 | if ($line=~/\(.*\){/) {
|
| 451 | print "need a space before the brace\n";
|
| 452 | print "$herecurr";
|
| 453 | $clean = 0;
|
| 454 | }
|
| 455 |
|
| 456 | |
| 457 | if ($line=~/^.\s+[A-Za-z\d_]+:/ and
|
| 458 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
|
| 459 | print "labels should not be indented\n";
|
| 460 | print "$herecurr";
|
| 461 | $clean = 0;
|
| 462 | }
|
| 463 |
|
| 464 | |
| 465 | if ($line=~/(if|while|for|switch)\(/) {
|
| 466 | print "need a space before the open parenthesis\n";
|
| 467 | print "$herecurr";
|
| 468 | $clean = 0;
|
| 469 | }
|
| 470 |
|
| 471 | |
| 472 | if ($line=~/(if|while)\s*\(.*[^<>!=]=[^=].*\)/) {
|
| 473 | print "do not use assignment in if condition\n";
|
| 474 | print "$herecurr";
|
| 475 | $clean = 0;
|
| 476 | }
|
| 477 |
|
| 478 | |
| 479 | |
| 480 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
|
| 481 | $previndent == $indent) {
|
| 482 | print "else should follow close brace\n";
|
| 483 | print "$hereprev";
|
| 484 | $clean = 0;
|
| 485 | }
|
| 486 |
|
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | if ($prevline=~/\s*switch\s*\(.*\)/ and $line=~/\s*case\s+/
|
| 492 | and $previndent != $indent) {
|
| 493 | print "switch and case should be at the same indent\n";
|
| 494 | print "$hereprev";
|
| 495 | $clean = 0;
|
| 496 | }
|
| 497 |
|
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 |
|
| 505 | |
| 506 | if ($line=~/\#define.*\\\s$/) {
|
| 507 | print("Whitepspace after \\ makes next lines useless\n");
|
| 508 | print "$herecurr";
|
| 509 | $clean = 0;
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) {
|
| 514 | my $checkfile = "include/linux/$1.h";
|
| 515 | if (-f $checkfile) {
|
| 516 | print "Use #include <linux/$1.h> instead of <asm/$1.h>\n";
|
| 517 | print $herecurr;
|
| 518 | $clean = 0;
|
| 519 | }
|
| 520 | }
|
| 521 |
|
| 522 | |
| 523 | if ($prevline=~/(if|while|for|switch)\s*\(/) {
|
| 524 | my @opened = $prevline=~/\(/g;
|
| 525 | my @closed = $prevline=~/\)/g;
|
| 526 | my $nr_line = $linenr;
|
| 527 | my $remaining = $realcnt;
|
| 528 | my $next_line = $line;
|
| 529 | my $extra_lines = 0;
|
| 530 | my $display_segment = $prevline;
|
| 531 |
|
| 532 | while ($remaining > 0 && scalar @opened > scalar @closed) {
|
| 533 | $prevline .= $next_line;
|
| 534 | $display_segment .= "\n" . $next_line;
|
| 535 | $next_line = $lines[$nr_line];
|
| 536 | $nr_line++;
|
| 537 | $remaining--;
|
| 538 |
|
| 539 | @opened = $prevline=~/\(/g;
|
| 540 | @closed = $prevline=~/\)/g;
|
| 541 | }
|
| 542 |
|
| 543 | if (($prevline=~/(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and
|
| 544 | !($next_line=~/(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) {
|
| 545 | print "That { should be on the previous line\n";
|
| 546 | print "$display_segment\n$next_line\n\n";
|
| 547 | $clean = 0;
|
| 548 | }
|
| 549 | }
|
| 550 |
|
| 551 | |
| 552 | if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and
|
| 553 | !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and
|
| 554 | !($line=~/do.*{/) and !($line=~/\(\{/)) {
|
| 555 | print "Macros with multiple statements should be enclosed in a do - while loop\n";
|
| 556 | print "$hereprev";
|
| 557 | $clean = 0;
|
| 558 | }
|
| 559 |
|
| 560 | |
| 561 | for my $inc (@deprecated) {
|
| 562 | if ($line =~ m@\#\s*include\s*\<$inc>@) {
|
| 563 | print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n";
|
| 564 | print "$herecurr";
|
| 565 | $clean = 0;
|
| 566 | }
|
| 567 | }
|
| 568 |
|
| 569 | |
| 570 | if ($line =~ /\bkernel_thread\b/) {
|
| 571 | print "Don't use kernel_thread(), use kthread(): see Documentation/feature-removal-schedule.txt\n";
|
| 572 | print "$herecurr";
|
| 573 | $clean = 0;
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | if ($chk_patch && !$is_patch) {
|
| 578 | $clean = 0;
|
| 579 | print "Does not appear to be a unified-diff format patch\n";
|
| 580 | }
|
| 581 | if ($is_patch && $chk_signoff && $signoff == 0) {
|
| 582 | $clean = 0;
|
| 583 | print "Missing Signed-off-by: line(s)\n";
|
| 584 | }
|
| 585 |
|
| 586 | if ($clean == 1 && $quiet == 0) {
|
| 587 | print "Your patch has no obvious style problems and is ready for submission.\n"
|
| 588 | }
|
| 589 | if ($clean == 0 && $quiet == 0) {
|
| 590 | print "Your patch has style problems, please review. If any of these errors\n";
|
| 591 | print "are false positives report them to the maintainer, see\n";
|
| 592 | print "CHECKPATCH in MAINTAINERS.\n";
|
| 593 | }
|
| 594 | return $clean;
|
| 595 | }
|