| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use strict;
|
| 8 |
|
| 9 | my $P = $0;
|
| 10 | $P =~ s@.*/@@g;
|
| 11 |
|
| 12 | my $V = '0.09';
|
| 13 |
|
| 14 | use Getopt::Long qw(:config no_auto_abbrev);
|
| 15 |
|
| 16 | my $quiet = 0;
|
| 17 | my $tree = 1;
|
| 18 | my $chk_signoff = 1;
|
| 19 | my $chk_patch = 1;
|
| 20 | my $tst_type = 0;
|
| 21 | GetOptions(
|
| 22 | 'q|quiet' => \$quiet,
|
| 23 | 'tree!' => \$tree,
|
| 24 | 'signoff!' => \$chk_signoff,
|
| 25 | 'patch!' => \$chk_patch,
|
| 26 | 'test-type!' => \$tst_type,
|
| 27 | ) or exit;
|
| 28 |
|
| 29 | my $exit = 0;
|
| 30 |
|
| 31 | if ($#ARGV < 0) {
|
| 32 | print "usage: $P [options] patchfile\n";
|
| 33 | print "version: $V\n";
|
| 34 | print "options: -q => quiet\n";
|
| 35 | print " --no-tree => run without a kernel tree\n";
|
| 36 | exit(1);
|
| 37 | }
|
| 38 |
|
| 39 | if ($tree && !top_of_kernel_tree()) {
|
| 40 | print "Must be run from the top-level dir. of a kernel tree\n";
|
| 41 | exit(2);
|
| 42 | }
|
| 43 |
|
| 44 | my @dep_includes = ();
|
| 45 | my @dep_functions = ();
|
| 46 | my $removal = 'Documentation/feature-removal-schedule.txt';
|
| 47 | if ($tree && -f $removal) {
|
| 48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n";
|
| 49 | while (<REMOVE>) {
|
| 50 | if (/^Check:\s+(.*\S)/) {
|
| 51 | for my $entry (split(/[, ]+/, $1)) {
|
| 52 | if ($entry =~ m@include/(.*)@) {
|
| 53 | push(@dep_includes, $1);
|
| 54 |
|
| 55 | } elsif ($entry !~ m@/@) {
|
| 56 | push(@dep_functions, $entry);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | my @rawlines = ();
|
| 64 | while (<>) {
|
| 65 | chomp;
|
| 66 | push(@rawlines, $_);
|
| 67 | if (eof(ARGV)) {
|
| 68 | if (!process($ARGV, @rawlines)) {
|
| 69 | $exit = 1;
|
| 70 | }
|
| 71 | @rawlines = ();
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | exit($exit);
|
| 76 |
|
| 77 | sub top_of_kernel_tree {
|
| 78 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") &&
|
| 79 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") &&
|
| 80 | (-d "Documentation") && (-d "arch") && (-d "include") &&
|
| 81 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") &&
|
| 82 | (-d "kernel") && (-d "lib") && (-d "scripts")) {
|
| 83 | return 1;
|
| 84 | }
|
| 85 | return 0;
|
| 86 | }
|
| 87 |
|
| 88 | sub expand_tabs {
|
| 89 | my ($str) = @_;
|
| 90 |
|
| 91 | my $res = '';
|
| 92 | my $n = 0;
|
| 93 | for my $c (split(//, $str)) {
|
| 94 | if ($c eq "\t") {
|
| 95 | $res .= ' ';
|
| 96 | $n++;
|
| 97 | for (; ($n % 8) != 0; $n++) {
|
| 98 | $res .= ' ';
|
| 99 | }
|
| 100 | next;
|
| 101 | }
|
| 102 | $res .= $c;
|
| 103 | $n++;
|
| 104 | }
|
| 105 |
|
| 106 | return $res;
|
| 107 | }
|
| 108 |
|
| 109 | sub line_stats {
|
| 110 | my ($line) = @_;
|
| 111 |
|
| 112 | |
| 113 | $line =~ s/^.//;
|
| 114 | $line = expand_tabs($line);
|
| 115 |
|
| 116 | |
| 117 | my ($white) = ($line =~ /^(\s*)/);
|
| 118 |
|
| 119 | return (length($line), length($white));
|
| 120 | }
|
| 121 |
|
| 122 | sub sanitise_line {
|
| 123 | my ($line) = @_;
|
| 124 |
|
| 125 | my $res = '';
|
| 126 | my $l = '';
|
| 127 |
|
| 128 | my $quote = '';
|
| 129 |
|
| 130 | foreach my $c (split(//, $line)) {
|
| 131 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
|
| 132 | if ($quote eq '') {
|
| 133 | $quote = $c;
|
| 134 | $res .= $c;
|
| 135 | $l = $c;
|
| 136 | next;
|
| 137 | } elsif ($quote eq $c) {
|
| 138 | $quote = '';
|
| 139 | }
|
| 140 | }
|
| 141 | if ($quote && $c ne "\t") {
|
| 142 | $res .= "X";
|
| 143 | } else {
|
| 144 | $res .= $c;
|
| 145 | }
|
| 146 |
|
| 147 | $l = $c;
|
| 148 | }
|
| 149 |
|
| 150 | return $res;
|
| 151 | }
|
| 152 |
|
| 153 | sub ctx_block_get {
|
| 154 | my ($linenr, $remain, $outer, $open, $close, $off) = @_;
|
| 155 | my $line;
|
| 156 | my $start = $linenr - 1;
|
| 157 | my $blk = '';
|
| 158 | my @o;
|
| 159 | my @c;
|
| 160 | my @res = ();
|
| 161 |
|
| 162 | my $level = 0;
|
| 163 | for ($line = $start; $remain > 0; $line++) {
|
| 164 | next if ($rawlines[$line] =~ /^-/);
|
| 165 | $remain--;
|
| 166 |
|
| 167 | $blk .= $rawlines[$line];
|
| 168 | foreach my $c (split(//, $rawlines[$line])) {
|
| 169 | |
| 170 | if ($off > 0) {
|
| 171 | $off--;
|
| 172 | next;
|
| 173 | }
|
| 174 |
|
| 175 | if ($c eq $close && $level > 0) {
|
| 176 | $level--;
|
| 177 | last if ($level == 0);
|
| 178 | } elsif ($c eq $open) {
|
| 179 | $level++;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | if (!$outer || $level <= 1) {
|
| 184 | push(@res, $rawlines[$line]);
|
| 185 | }
|
| 186 |
|
| 187 | last if ($level == 0);
|
| 188 | }
|
| 189 |
|
| 190 | return ($level, @res);
|
| 191 | }
|
| 192 | sub ctx_block_outer {
|
| 193 | my ($linenr, $remain) = @_;
|
| 194 |
|
| 195 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
|
| 196 | return @r;
|
| 197 | }
|
| 198 | sub ctx_block {
|
| 199 | my ($linenr, $remain) = @_;
|
| 200 |
|
| 201 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
|
| 202 | return @r;
|
| 203 | }
|
| 204 | sub ctx_statement {
|
| 205 | my ($linenr, $remain, $off) = @_;
|
| 206 |
|
| 207 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
|
| 208 | return @r;
|
| 209 | }
|
| 210 | sub ctx_block_level {
|
| 211 | my ($linenr, $remain) = @_;
|
| 212 |
|
| 213 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
|
| 214 | }
|
| 215 |
|
| 216 | sub ctx_locate_comment {
|
| 217 | my ($first_line, $end_line) = @_;
|
| 218 |
|
| 219 | |
| 220 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
|
| 221 | return $current_comment if (defined $current_comment);
|
| 222 |
|
| 223 | |
| 224 | |
| 225 | my $in_comment = 0;
|
| 226 | $current_comment = '';
|
| 227 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
|
| 228 | my $line = $rawlines[$linenr - 1];
|
| 229 | |
| 230 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
|
| 231 | $in_comment = 1;
|
| 232 | }
|
| 233 | if ($line =~ m@/\*@) {
|
| 234 | $in_comment = 1;
|
| 235 | }
|
| 236 | if (!$in_comment && $current_comment ne '') {
|
| 237 | $current_comment = '';
|
| 238 | }
|
| 239 | $current_comment .= $line . "\n" if ($in_comment);
|
| 240 | if ($line =~ m@\*/@) {
|
| 241 | $in_comment = 0;
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | chomp($current_comment);
|
| 246 | return($current_comment);
|
| 247 | }
|
| 248 | sub ctx_has_comment {
|
| 249 | my ($first_line, $end_line) = @_;
|
| 250 | my $cmt = ctx_locate_comment($first_line, $end_line);
|
| 251 |
|
| 252 | |
| 253 | |
| 254 |
|
| 255 | return ($cmt ne '');
|
| 256 | }
|
| 257 |
|
| 258 | sub cat_vet {
|
| 259 | my ($vet) = @_;
|
| 260 |
|
| 261 | $vet =~ s/\t/^I/;
|
| 262 | $vet =~ s/$/\$/;
|
| 263 |
|
| 264 | return $vet;
|
| 265 | }
|
| 266 |
|
| 267 | my @report = ();
|
| 268 | sub report {
|
| 269 | push(@report, $_[0]);
|
| 270 | }
|
| 271 | sub report_dump {
|
| 272 | @report;
|
| 273 | }
|
| 274 | sub ERROR {
|
| 275 | report("ERROR: $_[0]\n");
|
| 276 | our $clean = 0;
|
| 277 | }
|
| 278 | sub WARN {
|
| 279 | report("WARNING: $_[0]\n");
|
| 280 | our $clean = 0;
|
| 281 | }
|
| 282 | sub CHK {
|
| 283 | report("CHECK: $_[0]\n");
|
| 284 | our $clean = 0;
|
| 285 | }
|
| 286 |
|
| 287 | sub process {
|
| 288 | my $filename = shift;
|
| 289 | my @lines = @_;
|
| 290 |
|
| 291 | my $linenr=0;
|
| 292 | my $prevline="";
|
| 293 | my $stashline="";
|
| 294 |
|
| 295 | my $length;
|
| 296 | my $indent;
|
| 297 | my $previndent=0;
|
| 298 | my $stashindent=0;
|
| 299 |
|
| 300 | our $clean = 1;
|
| 301 | my $signoff = 0;
|
| 302 | my $is_patch = 0;
|
| 303 |
|
| 304 | |
| 305 | my $realfile = '';
|
| 306 | my $realline = 0;
|
| 307 | my $realcnt = 0;
|
| 308 | my $here = '';
|
| 309 | my $in_comment = 0;
|
| 310 | my $first_line = 0;
|
| 311 |
|
| 312 | my $Ident = qr{[A-Za-z\d_]+};
|
| 313 | my $Storage = qr{extern|static};
|
| 314 | my $Sparse = qr{__user|__kernel|__force|__iomem|__must_check|__init_refok};
|
| 315 | my $NonptrType = qr{
|
| 316 | \b
|
| 317 | (?:const\s+)?
|
| 318 | (?:unsigned\s+)?
|
| 319 | (?:
|
| 320 | void|
|
| 321 | char|
|
| 322 | short|
|
| 323 | int|
|
| 324 | long|
|
| 325 | unsigned|
|
| 326 | float|
|
| 327 | double|
|
| 328 | bool|
|
| 329 | long\s+int|
|
| 330 | long\s+long|
|
| 331 | long\s+long\s+int|
|
| 332 | u8|u16|u32|u64|
|
| 333 | s8|s16|s32|s64|
|
| 334 | struct\s+$Ident|
|
| 335 | union\s+$Ident|
|
| 336 | enum\s+$Ident|
|
| 337 | ${Ident}_t
|
| 338 | )
|
| 339 | (?:\s+$Sparse)*
|
| 340 | \b
|
| 341 | }x;
|
| 342 | my $Type = qr{
|
| 343 | \b$NonptrType\b
|
| 344 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
|
| 345 | (?:\s+$Sparse)*
|
| 346 | }x;
|
| 347 | my $Declare = qr{(?:$Storage\s+)?$Type};
|
| 348 | my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit};
|
| 349 |
|
| 350 | my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
|
| 351 | my $Lval = qr{$Ident(?:$Member)*};
|
| 352 |
|
| 353 | |
| 354 | my @setup_docs = ();
|
| 355 | my $setup_docs = 0;
|
| 356 | foreach my $line (@lines) {
|
| 357 | if ($line=~/^\+\+\+\s+(\S+)/) {
|
| 358 | $setup_docs = 0;
|
| 359 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
|
| 360 | $setup_docs = 1;
|
| 361 | }
|
| 362 | next;
|
| 363 | }
|
| 364 |
|
| 365 | if ($setup_docs && $line =~ /^\+/) {
|
| 366 | push(@setup_docs, $line);
|
| 367 | }
|
| 368 | }
|
| 369 |
|
| 370 | foreach my $line (@lines) {
|
| 371 | $linenr++;
|
| 372 |
|
| 373 | my $rawline = $line;
|
| 374 |
|
| 375 | |
| 376 | if ($line=~/^\+\+\+\s+(\S+)/) {
|
| 377 | $realfile=$1;
|
| 378 | $realfile =~ s@^[^/]*/@@;
|
| 379 | $in_comment = 0;
|
| 380 | next;
|
| 381 | }
|
| 382 | |
| 383 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) {
|
| 384 | $is_patch = 1;
|
| 385 | $first_line = $linenr + 1;
|
| 386 | $in_comment = 0;
|
| 387 | $realline=$1-1;
|
| 388 | if (defined $2) {
|
| 389 | $realcnt=$3+1;
|
| 390 | } else {
|
| 391 | $realcnt=1+1;
|
| 392 | }
|
| 393 | next;
|
| 394 | }
|
| 395 |
|
| 396 | |
| 397 | |
| 398 | |
| 399 | if ($line =~ /^( |\+|$)/) {
|
| 400 | $realline++;
|
| 401 | $realcnt-- if ($realcnt != 0);
|
| 402 |
|
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
|
| 411 | $in_comment = 1;
|
| 412 | }
|
| 413 | if ($line =~ m@/\*@) {
|
| 414 | $in_comment = 1;
|
| 415 | }
|
| 416 | if ($line =~ m@\*/@) {
|
| 417 | $in_comment = 0;
|
| 418 | }
|
| 419 |
|
| 420 | |
| 421 | ($length, $indent) = line_stats($line);
|
| 422 |
|
| 423 | |
| 424 | ($prevline, $stashline) = ($stashline, $line);
|
| 425 | ($previndent, $stashindent) = ($stashindent, $indent);
|
| 426 | } elsif ($realcnt == 1) {
|
| 427 | $realcnt--;
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | $here = "#$linenr: ";
|
| 432 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
|
| 433 |
|
| 434 | my $hereline = "$here\n$line\n";
|
| 435 | my $herecurr = "$here\n$line\n";
|
| 436 | my $hereprev = "$here\n$prevline\n$line\n";
|
| 437 |
|
| 438 | |
| 439 | if ($line =~ /^\s*signed-off-by:/i) {
|
| 440 | |
| 441 | $signoff++;
|
| 442 | if (!($line =~ /^\s*Signed-off-by:/)) {
|
| 443 | WARN("Signed-off-by: is the preferred form\n" .
|
| 444 | $herecurr);
|
| 445 | }
|
| 446 | if ($line =~ /^\s*signed-off-by:\S/i) {
|
| 447 | WARN("need space after Signed-off-by:\n" .
|
| 448 | $herecurr);
|
| 449 | }
|
| 450 | }
|
| 451 |
|
| 452 | |
| 453 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) {
|
| 454 | ERROR("patch seems to be corrupt (line wrapped?)\n" .
|
| 455 | $herecurr);
|
| 456 | }
|
| 457 |
|
| 458 | |
| 459 | if (($realfile =~ /^$/ || $line =~ /^\+/) &&
|
| 460 | !($line =~ m/^(
|
| 461 | [\x09\x0A\x0D\x20-\x7E] |
| 462 | | [\xC2-\xDF][\x80-\xBF] |
| 463 | | \xE0[\xA0-\xBF][\x80-\xBF] |
| 464 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} |
| 465 | | \xED[\x80-\x9F][\x80-\xBF] |
| 466 | | \xF0[\x90-\xBF][\x80-\xBF]{2} |
| 467 | | [\xF1-\xF3][\x80-\xBF]{3} |
| 468 | | \xF4[\x80-\x8F][\x80-\xBF]{2} |
| 469 | )*$/x )) {
|
| 470 | ERROR("Invalid UTF-8\n" . $herecurr);
|
| 471 | }
|
| 472 |
|
| 473 | |
| 474 | if ($line=~/^-/) {next;}
|
| 475 |
|
| 476 | |
| 477 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
|
| 478 |
|
| 479 | |
| 480 | if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) {
|
| 481 | my $herevet = "$here\n" . cat_vet($line) . "\n";
|
| 482 | ERROR("trailing whitespace\n" . $herevet);
|
| 483 | }
|
| 484 | |
| 485 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) {
|
| 486 | WARN("line over 80 characters\n" . $herecurr);
|
| 487 | }
|
| 488 |
|
| 489 | |
| 490 | next if ($realfile !~ /\.[hc]$/);
|
| 491 |
|
| 492 | |
| 493 | |
| 494 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) {
|
| 495 | my $herevet = "$here\n" . cat_vet($line) . "\n";
|
| 496 | ERROR("use tabs not spaces\n" . $herevet);
|
| 497 | }
|
| 498 |
|
| 499 | |
| 500 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
|
| 501 | ($line =~ s@/\*.*@@) +
|
| 502 | ($line =~ s@^(.).*\*/@$1@);
|
| 503 |
|
| 504 | |
| 505 | |
| 506 | |
| 507 | next if (!$comment_edge && $in_comment);
|
| 508 |
|
| 509 | |
| 510 | $line = sanitise_line($line);
|
| 511 |
|
| 512 | |
| 513 | |
| 514 | |
| 515 |
|
| 516 | |
| 517 | |
| 518 | if ($line=~/\bswitch\s*\(.*\)/) {
|
| 519 | my $err = '';
|
| 520 | my $sep = '';
|
| 521 | my @ctx = ctx_block_outer($linenr, $realcnt);
|
| 522 | shift(@ctx);
|
| 523 | for my $ctx (@ctx) {
|
| 524 | my ($clen, $cindent) = line_stats($ctx);
|
| 525 | if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
|
| 526 | $indent != $cindent) {
|
| 527 | $err .= "$sep$ctx\n";
|
| 528 | $sep = '';
|
| 529 | } else {
|
| 530 | $sep = "[...]\n";
|
| 531 | }
|
| 532 | }
|
| 533 | if ($err ne '') {
|
| 534 | ERROR("switch and case should be at the same indent\n$hereline\n$err\n");
|
| 535 | }
|
| 536 | }
|
| 537 |
|
| 538 | |
| 539 | |
| 540 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
|
| 541 | my @ctx = ctx_statement($linenr, $realcnt, 0);
|
| 542 | my $ctx_ln = $linenr + $#ctx + 1;
|
| 543 | my $ctx_cnt = $realcnt - $#ctx - 1;
|
| 544 | my $ctx = join("\n", @ctx);
|
| 545 |
|
| 546 | while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
|
| 547 | $ctx_ln++;
|
| 548 | $ctx_cnt--;
|
| 549 | }
|
| 550 | |
| 551 |
|
| 552 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
|
| 553 | ERROR("That open brace { should be on the previous line\n" .
|
| 554 | "$here\n$ctx\n$lines[$ctx_ln - 1]");
|
| 555 | }
|
| 556 | }
|
| 557 |
|
| 558 | |
| 559 | if ($line=~/^[^\+]/) {next;}
|
| 560 |
|
| 561 | |
| 562 | if ($tst_type && $line =~ /^.$Declare$/) {
|
| 563 | ERROR("TEST: is type $Declare\n" . $herecurr);
|
| 564 | next;
|
| 565 | }
|
| 566 |
|
| 567 | |
| 568 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
|
| 569 | $line =~ /^.\s*{/) {
|
| 570 | ERROR("That open brace { should be on the previous line\n" . $hereprev);
|
| 571 | }
|
| 572 |
|
| 573 | |
| 574 | |
| 575 | |
| 576 |
|
| 577 | |
| 578 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
|
| 579 | my $path = $1;
|
| 580 | if ($path =~ m{//}) {
|
| 581 | ERROR("malformed #include filename\n" .
|
| 582 | $herecurr);
|
| 583 | }
|
| 584 | |
| 585 | $path = 'X' x length($path);
|
| 586 | $line =~ s{\<.*\>}{<$path>};
|
| 587 | }
|
| 588 |
|
| 589 | |
| 590 | if ($line =~ m{//}) {
|
| 591 | ERROR("do not use C99 // comments\n" . $herecurr);
|
| 592 | }
|
| 593 | |
| 594 | $line =~ s@//.*@@;
|
| 595 |
|
| 596 | |
| 597 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
|
| 598 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
|
| 599 | my $name = $1;
|
| 600 | if (($prevline !~ /^}/) &&
|
| 601 | ($prevline !~ /^\+}/) &&
|
| 602 | ($prevline !~ /^ }/) &&
|
| 603 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
|
| 604 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
|
| 605 | }
|
| 606 | }
|
| 607 |
|
| 608 | |
| 609 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
|
| 610 | ERROR("do not initialise externals to 0 or NULL\n" .
|
| 611 | $herecurr);
|
| 612 | }
|
| 613 | |
| 614 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
|
| 615 | ERROR("do not initialise statics to 0 or NULL\n" .
|
| 616 | $herecurr);
|
| 617 | }
|
| 618 |
|
| 619 | |
| 620 | |
| 621 | if ($line =~ /\btypedef\s/ &&
|
| 622 | $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ &&
|
| 623 | $line !~ /\b__bitwise(?:__|)\b/) {
|
| 624 | WARN("do not add new typedefs\n" . $herecurr);
|
| 625 | }
|
| 626 |
|
| 627 | |
| 628 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
|
| 629 | ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
|
| 630 | $herecurr);
|
| 631 |
|
| 632 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
|
| 633 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
|
| 634 | $herecurr);
|
| 635 |
|
| 636 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) {
|
| 637 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
|
| 638 | $herecurr);
|
| 639 |
|
| 640 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) {
|
| 641 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
|
| 642 | $herecurr);
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 |
|
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
|
| 658 | my $ok = 0;
|
| 659 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
|
| 660 | |
| 661 | |
| 662 | |
| 663 | if ($lines[$ln - 1] =~ m{\bprintk\(}) {
|
| 664 | if ($rawlines[$ln - 1] !~ m{\\n"}) {
|
| 665 | $ok = 1;
|
| 666 | }
|
| 667 | last;
|
| 668 | }
|
| 669 | }
|
| 670 | if ($ok == 0) {
|
| 671 | WARN("printk() should include KERN_ facility level\n" . $herecurr);
|
| 672 | }
|
| 673 | }
|
| 674 |
|
| 675 | |
| 676 | |
| 677 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and
|
| 678 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
|
| 679 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
|
| 680 | }
|
| 681 |
|
| 682 | |
| 683 | if ($line =~ /($Ident)\s+\(/ &&
|
| 684 | $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
|
| 685 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
|
| 686 | WARN("no space between function name and open parenthesis '('\n" . $herecurr);
|
| 687 | }
|
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | my $opline = $line;
|
| 693 | $opline = expand_tabs($opline);
|
| 694 | $opline =~ s/^./ /;
|
| 695 | if (!($line=~/\#\s*include/)) {
|
| 696 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline);
|
| 697 | my $off = 0;
|
| 698 | for (my $n = 0; $n < $#elements; $n += 2) {
|
| 699 | $off += length($elements[$n]);
|
| 700 |
|
| 701 | my $a = '';
|
| 702 | $a = 'V' if ($elements[$n] ne '');
|
| 703 | $a = 'W' if ($elements[$n] =~ /\s$/);
|
| 704 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
|
| 705 | $a = 'O' if ($elements[$n] eq '');
|
| 706 | $a = 'E' if ($elements[$n] eq '' && $n == 0);
|
| 707 |
|
| 708 | my $op = $elements[$n + 1];
|
| 709 |
|
| 710 | my $c = '';
|
| 711 | if (defined $elements[$n + 2]) {
|
| 712 | $c = 'V' if ($elements[$n + 2] ne '');
|
| 713 | $c = 'W' if ($elements[$n + 2] =~ /^\s/);
|
| 714 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
|
| 715 | $c = 'O' if ($elements[$n + 2] eq '');
|
| 716 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
|
| 717 | } else {
|
| 718 | $c = 'E';
|
| 719 | }
|
| 720 |
|
| 721 | |
| 722 | my $ca = substr($opline, 0, $off);
|
| 723 | my $cc = '';
|
| 724 | if (length($opline) >= ($off + length($elements[$n + 1]))) {
|
| 725 | $cc = substr($opline, $off + length($elements[$n + 1]));
|
| 726 | }
|
| 727 | my $cb = "$ca$;$cc";
|
| 728 |
|
| 729 | my $ctx = "${a}x${c}";
|
| 730 |
|
| 731 | my $at = "(ctx:$ctx)";
|
| 732 |
|
| 733 | my $ptr = (" " x $off) . "^";
|
| 734 | my $hereptr = "$hereline$ptr\n";
|
| 735 |
|
| 736 | |
| 737 |
|
| 738 | |
| 739 | if ($op eq ';') {
|
| 740 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
|
| 741 | $cc !~ /^;/) {
|
| 742 | ERROR("need space after that '$op' $at\n" . $hereptr);
|
| 743 | }
|
| 744 |
|
| 745 | |
| 746 | } elsif ($op eq '//') {
|
| 747 |
|
| 748 | |
| 749 | } elsif ($op eq '->') {
|
| 750 | if ($ctx =~ /Wx.|.xW/) {
|
| 751 | ERROR("no spaces around that '$op' $at\n" . $hereptr);
|
| 752 | }
|
| 753 |
|
| 754 | |
| 755 | } elsif ($op eq ',') {
|
| 756 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
|
| 757 | ERROR("need space after that '$op' $at\n" . $hereptr);
|
| 758 | }
|
| 759 |
|
| 760 | |
| 761 | } elsif ($op eq '!' or $op eq '~') {
|
| 762 | if ($ctx !~ /[WOEB]x./) {
|
| 763 | ERROR("need space before that '$op' $at\n" . $hereptr);
|
| 764 | }
|
| 765 | if ($ctx =~ /.xW/) {
|
| 766 | ERROR("no space after that '$op' $at\n" . $hereptr);
|
| 767 | }
|
| 768 |
|
| 769 | |
| 770 | } elsif ($op eq '++' or $op eq '--') {
|
| 771 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
|
| 772 | ERROR("need space one side of that '$op' $at\n" . $hereptr);
|
| 773 | }
|
| 774 | if ($ctx =~ /Wx./ && $cc =~ /^;/) {
|
| 775 | ERROR("no space before that '$op' $at\n" . $hereptr);
|
| 776 | }
|
| 777 |
|
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | } elsif ($op eq '&' or $op eq '-') {
|
| 791 | if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) {
|
| 792 | ERROR("need space before that '$op' $at\n" . $hereptr);
|
| 793 | }
|
| 794 |
|
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | } elsif ($op eq '*') {
|
| 801 | if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ &&
|
| 802 | $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) {
|
| 803 | ERROR("need space before that '$op' $at\n" . $hereptr);
|
| 804 | }
|
| 805 |
|
| 806 | |
| 807 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or
|
| 808 | $op eq '^' or $op eq '|')
|
| 809 | {
|
| 810 | if ($ctx !~ /VxV|WxW|VxE|WxE/) {
|
| 811 | ERROR("need consistent spacing around '$op' $at\n" .
|
| 812 | $hereptr);
|
| 813 | }
|
| 814 |
|
| 815 | |
| 816 | } elsif ($ctx !~ /[EW]x[WE]/) {
|
| 817 | |
| 818 | if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
|
| 819 | !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
|
| 820 | ERROR("need spaces around that '$op' $at\n" . $hereptr);
|
| 821 | }
|
| 822 | }
|
| 823 | $off += length($elements[$n + 1]);
|
| 824 | }
|
| 825 | }
|
| 826 |
|
| 827 | |
| 828 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
|
| 829 | WARN("multiple assignments should be avoided\n" . $herecurr);
|
| 830 | }
|
| 831 |
|
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 |
|
| 847 | |
| 848 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
|
| 849 | $line =~ /do{/) {
|
| 850 | ERROR("need a space before the open brace '{'\n" . $herecurr);
|
| 851 | }
|
| 852 |
|
| 853 | |
| 854 | |
| 855 | if ($line =~ /}(?!(?:,|;|\)))\S/) {
|
| 856 | ERROR("need a space after that close brace '}'\n" . $herecurr);
|
| 857 | }
|
| 858 |
|
| 859 | |
| 860 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
|
| 861 | ERROR("no space after that open square bracket '['\n" . $herecurr);
|
| 862 | }
|
| 863 | if ($line =~ /\s\]/) {
|
| 864 | ERROR("no space before that close square bracket ']'\n" . $herecurr);
|
| 865 | }
|
| 866 |
|
| 867 | |
| 868 | if ($line =~ /\(\s/ && $line !~ /\(\s*$/) {
|
| 869 | ERROR("no space after that open parenthesis '('\n" . $herecurr);
|
| 870 | }
|
| 871 | if ($line =~ /\s\)/) {
|
| 872 | ERROR("no space before that close parenthesis ')'\n" . $herecurr);
|
| 873 | }
|
| 874 |
|
| 875 | |
| 876 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
|
| 877 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
|
| 878 | WARN("labels should not be indented\n" . $herecurr);
|
| 879 | }
|
| 880 |
|
| 881 | |
| 882 | if ($line=~/\b(if|while|for|switch)\(/) {
|
| 883 | ERROR("need a space before the open parenthesis '('\n" . $herecurr);
|
| 884 | }
|
| 885 |
|
| 886 | |
| 887 | if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) {
|
| 888 | |
| 889 | ERROR("do not use assignment in if condition\n" . $herecurr);
|
| 890 | }
|
| 891 |
|
| 892 | |
| 893 | |
| 894 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
|
| 895 | $previndent == $indent) {
|
| 896 | ERROR("else should follow close brace '}'\n" . $hereprev);
|
| 897 | }
|
| 898 |
|
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 |
|
| 906 | |
| 907 | if ($line=~/\#define.*\\\s$/) {
|
| 908 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
|
| 909 | }
|
| 910 |
|
| 911 | |
| 912 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
|
| 913 | my $checkfile = "include/linux/$1.h";
|
| 914 | if (-f $checkfile) {
|
| 915 | CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
|
| 916 | $herecurr);
|
| 917 | }
|
| 918 | }
|
| 919 |
|
| 920 | |
| 921 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
|
| 922 | $1 !~ /^\s*(?:\sif|{|\\|$)/) {
|
| 923 | ERROR("trailing statements should be on next line\n" . $herecurr);
|
| 924 | }
|
| 925 |
|
| 926 | |
| 927 | |
| 928 | |
| 929 | if (($prevline=~/\#define.*\\/) and
|
| 930 | !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and
|
| 931 | !($line=~/do.*{/) and !($line=~/\(\{/) and
|
| 932 | !($line=~/^.\s*$Declare\s/)) {
|
| 933 | |
| 934 | |
| 935 | |
| 936 | my $ln = $linenr;
|
| 937 | my $cnt = $realcnt;
|
| 938 | my $off = 0;
|
| 939 |
|
| 940 | |
| 941 | |
| 942 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
|
| 943 | |
| 944 | if (defined $2 && $2 ne '') {
|
| 945 | $off = length($1);
|
| 946 | $ln--;
|
| 947 | $cnt++;
|
| 948 | }
|
| 949 | my @ctx = ctx_statement($ln, $cnt, $off);
|
| 950 | my $ctx_ln = $ln + $#ctx + 1;
|
| 951 | my $ctx = join("\n", @ctx);
|
| 952 |
|
| 953 | |
| 954 | while ($ctx =~ /\\$/ &&
|
| 955 | $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
|
| 956 | $ctx .= $lines[$ctx_ln - 1];
|
| 957 | $ctx_ln++;
|
| 958 | }
|
| 959 |
|
| 960 | if ($ctx =~ /\\$/) {
|
| 961 | if ($ctx =~ /;/) {
|
| 962 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
|
| 963 | } else {
|
| 964 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
|
| 965 | }
|
| 966 | }
|
| 967 | }
|
| 968 |
|
| 969 | |
| 970 | if ($line =~ /\b(if|while|for|else)\b/) {
|
| 971 | |
| 972 | my @control = ctx_statement($linenr, $realcnt, 0);
|
| 973 | my $nr = $linenr + (scalar(@control) - 1);
|
| 974 | my $cnt = $realcnt - (scalar(@control) - 1);
|
| 975 |
|
| 976 | my $off = $realcnt - $cnt;
|
| 977 | |
| 978 |
|
| 979 | |
| 980 | if ($lines[$nr - 1] =~ /{\s*$/) {
|
| 981 | my ($lvl, @block) = ctx_block_level($nr, $cnt);
|
| 982 |
|
| 983 | my $stmt = join(' ', @block);
|
| 984 | $stmt =~ s/(^[^{]*){//;
|
| 985 | my $before = $1;
|
| 986 | $stmt =~ s/}([^}]*$)//;
|
| 987 | my $after = $1;
|
| 988 |
|
| 989 | |
| 990 | |
| 991 |
|
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | my @semi = ($stmt =~ /;/g);
|
| 999 | push(@semi, "/**/") if ($stmt =~ m@/\*@);
|
| 1000 | |
| 1001 | if ($lvl == 0 && scalar(@semi) < 2 &&
|
| 1002 | $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
|
| 1003 | $before !~ /}/ && $after !~ /{/) {
|
| 1004 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
|
| 1005 | shift(@block);
|
| 1006 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
|
| 1007 | }
|
| 1008 | }
|
| 1009 | }
|
| 1010 |
|
| 1011 | |
| 1012 | for my $inc (@dep_includes) {
|
| 1013 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
|
| 1014 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
|
| 1015 | }
|
| 1016 | }
|
| 1017 |
|
| 1018 | |
| 1019 | for my $func (@dep_functions) {
|
| 1020 | if ($line =~ /\b$func\b/) {
|
| 1021 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
|
| 1022 | }
|
| 1023 | }
|
| 1024 |
|
| 1025 | |
| 1026 | if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) {
|
| 1027 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
|
| 1028 | }
|
| 1029 |
|
| 1030 | |
| 1031 | if ($line =~ /^.#\s*if\s+0\b/) {
|
| 1032 | CHK("if this code is redundant consider removing it\n" .
|
| 1033 | $herecurr);
|
| 1034 | }
|
| 1035 |
|
| 1036 | |
| 1037 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
|
| 1038 | my $expr = $1;
|
| 1039 | if ($line =~ /\bkfree\(\Q$expr\E\);/) {
|
| 1040 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
|
| 1041 | }
|
| 1042 | }
|
| 1043 |
|
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 |
|
| 1051 | |
| 1052 | if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
|
| 1053 | ERROR("exactly one space required after that #$1\n" . $herecurr);
|
| 1054 | }
|
| 1055 |
|
| 1056 | |
| 1057 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
|
| 1058 | my $which = $1;
|
| 1059 | if (!ctx_has_comment($first_line, $linenr)) {
|
| 1060 | CHK("$1 definition without comment\n" . $herecurr);
|
| 1061 | }
|
| 1062 | }
|
| 1063 | |
| 1064 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
|
| 1065 | if (!ctx_has_comment($first_line, $linenr)) {
|
| 1066 | CHK("memory barrier without comment\n" . $herecurr);
|
| 1067 | }
|
| 1068 | }
|
| 1069 | |
| 1070 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
|
| 1071 | CHK("architecture specific defines should be avoided\n" . $herecurr);
|
| 1072 | }
|
| 1073 |
|
| 1074 | |
| 1075 | |
| 1076 | if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ ||
|
| 1077 | $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) {
|
| 1078 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
|
| 1079 | }
|
| 1080 |
|
| 1081 | |
| 1082 | if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
|
| 1083 | WARN("externs should be avoided in .c files\n" . $herecurr);
|
| 1084 | }
|
| 1085 |
|
| 1086 | |
| 1087 | if ($rawline =~ /\b__setup\("([^"]*)"/) {
|
| 1088 | my $name = $1;
|
| 1089 |
|
| 1090 | if (!grep(/$name/, @setup_docs)) {
|
| 1091 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
|
| 1092 | }
|
| 1093 | }
|
| 1094 | }
|
| 1095 |
|
| 1096 | if ($chk_patch && !$is_patch) {
|
| 1097 | ERROR("Does not appear to be a unified-diff format patch\n");
|
| 1098 | }
|
| 1099 | if ($is_patch && $chk_signoff && $signoff == 0) {
|
| 1100 | ERROR("Missing Signed-off-by: line(s)\n");
|
| 1101 | }
|
| 1102 |
|
| 1103 | if ($clean == 0 && ($chk_patch || $is_patch)) {
|
| 1104 | print report_dump();
|
| 1105 | }
|
| 1106 | if ($clean == 1 && $quiet == 0) {
|
| 1107 | print "Your patch has no obvious style problems and is ready for submission.\n"
|
| 1108 | }
|
| 1109 | if ($clean == 0 && $quiet == 0) {
|
| 1110 | print "Your patch has style problems, please review. If any of these errors\n";
|
| 1111 | print "are false positives report them to the maintainer, see\n";
|
| 1112 | print "CHECKPATCH in MAINTAINERS.\n";
|
| 1113 | }
|
| 1114 | return $clean;
|
| 1115 | }
|