10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit) 3*00df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 40a920b5bSAndy Whitcroft# (c) 2007, Andy Whitcroft <[email protected]> (new conditions, test suite, etc) 50a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2 60a920b5bSAndy Whitcroft 70a920b5bSAndy Whitcroftuse strict; 80a920b5bSAndy Whitcroft 90a920b5bSAndy Whitcroftmy $P = $0; 10*00df344fSAndy Whitcroft$P =~ s@.*/@@g; 110a920b5bSAndy Whitcroft 12*00df344fSAndy Whitcroftmy $V = '0.04'; 130a920b5bSAndy Whitcroft 140a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 150a920b5bSAndy Whitcroft 160a920b5bSAndy Whitcroftmy $quiet = 0; 170a920b5bSAndy Whitcroftmy $tree = 1; 180a920b5bSAndy Whitcroftmy $chk_signoff = 1; 190a920b5bSAndy Whitcroftmy $chk_patch = 1; 200a920b5bSAndy WhitcroftGetOptions( 210a920b5bSAndy Whitcroft 'q|quiet' => \$quiet, 220a920b5bSAndy Whitcroft 'tree!' => \$tree, 230a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 240a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 250a920b5bSAndy Whitcroft) or exit; 260a920b5bSAndy Whitcroft 270a920b5bSAndy Whitcroftmy $exit = 0; 280a920b5bSAndy Whitcroft 290a920b5bSAndy Whitcroftif ($#ARGV < 0) { 30*00df344fSAndy Whitcroft print "usage: $P [options] patchfile\n"; 310a920b5bSAndy Whitcroft print "version: $V\n"; 320a920b5bSAndy Whitcroft print "options: -q => quiet\n"; 330a920b5bSAndy Whitcroft print " --no-tree => run without a kernel tree\n"; 340a920b5bSAndy Whitcroft exit(1); 350a920b5bSAndy Whitcroft} 360a920b5bSAndy Whitcroft 370a920b5bSAndy Whitcroftif ($tree && !top_of_kernel_tree()) { 380a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 390a920b5bSAndy Whitcroft exit(2); 400a920b5bSAndy Whitcroft} 410a920b5bSAndy Whitcroft 424a0df2efSAndy Whitcroftmy @dep_includes = (); 434a0df2efSAndy Whitcroftmy @dep_functions = (); 440a920b5bSAndy Whitcroftmy $removal = 'Documentation/feature-removal-schedule.txt'; 450a920b5bSAndy Whitcroftif ($tree && -f $removal) { 460a920b5bSAndy Whitcroft open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; 470a920b5bSAndy Whitcroft while (<REMOVE>) { 480a920b5bSAndy Whitcroft if (/^Files:\s+(.*\S)/) { 490a920b5bSAndy Whitcroft for my $file (split(/[, ]+/, $1)) { 500a920b5bSAndy Whitcroft if ($file =~ m@include/(.*)@) { 514a0df2efSAndy Whitcroft push(@dep_includes, $1); 520a920b5bSAndy Whitcroft } 530a920b5bSAndy Whitcroft } 544a0df2efSAndy Whitcroft 554a0df2efSAndy Whitcroft } elsif (/^Funcs:\s+(.*\S)/) { 564a0df2efSAndy Whitcroft for my $func (split(/[, ]+/, $1)) { 574a0df2efSAndy Whitcroft push(@dep_functions, $func); 584a0df2efSAndy Whitcroft } 590a920b5bSAndy Whitcroft } 600a920b5bSAndy Whitcroft } 610a920b5bSAndy Whitcroft} 620a920b5bSAndy Whitcroft 63*00df344fSAndy Whitcroftmy @rawlines = (); 640a920b5bSAndy Whitcroftwhile (<>) { 650a920b5bSAndy Whitcroft chomp; 66*00df344fSAndy Whitcroft push(@rawlines, $_); 670a920b5bSAndy Whitcroft if (eof(ARGV)) { 68*00df344fSAndy Whitcroft if (!process($ARGV, @rawlines)) { 690a920b5bSAndy Whitcroft $exit = 1; 700a920b5bSAndy Whitcroft } 71*00df344fSAndy Whitcroft @rawlines = (); 720a920b5bSAndy Whitcroft } 730a920b5bSAndy Whitcroft} 740a920b5bSAndy Whitcroft 750a920b5bSAndy Whitcroftexit($exit); 760a920b5bSAndy Whitcroft 770a920b5bSAndy Whitcroftsub top_of_kernel_tree { 780a920b5bSAndy Whitcroft if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && 790a920b5bSAndy Whitcroft (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && 800a920b5bSAndy Whitcroft (-d "Documentation") && (-d "arch") && (-d "include") && 810a920b5bSAndy Whitcroft (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && 820a920b5bSAndy Whitcroft (-d "kernel") && (-d "lib") && (-d "scripts")) { 830a920b5bSAndy Whitcroft return 1; 840a920b5bSAndy Whitcroft } 850a920b5bSAndy Whitcroft return 0; 860a920b5bSAndy Whitcroft} 870a920b5bSAndy Whitcroft 880a920b5bSAndy Whitcroftsub expand_tabs { 890a920b5bSAndy Whitcroft my ($str) = @_; 900a920b5bSAndy Whitcroft 910a920b5bSAndy Whitcroft my $res = ''; 920a920b5bSAndy Whitcroft my $n = 0; 930a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 940a920b5bSAndy Whitcroft if ($c eq "\t") { 950a920b5bSAndy Whitcroft $res .= ' '; 960a920b5bSAndy Whitcroft $n++; 970a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 980a920b5bSAndy Whitcroft $res .= ' '; 990a920b5bSAndy Whitcroft } 1000a920b5bSAndy Whitcroft next; 1010a920b5bSAndy Whitcroft } 1020a920b5bSAndy Whitcroft $res .= $c; 1030a920b5bSAndy Whitcroft $n++; 1040a920b5bSAndy Whitcroft } 1050a920b5bSAndy Whitcroft 1060a920b5bSAndy Whitcroft return $res; 1070a920b5bSAndy Whitcroft} 1080a920b5bSAndy Whitcroft 1094a0df2efSAndy Whitcroftsub line_stats { 1104a0df2efSAndy Whitcroft my ($line) = @_; 1114a0df2efSAndy Whitcroft 1124a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 1134a0df2efSAndy Whitcroft $line =~ s/^.//; 1144a0df2efSAndy Whitcroft $line = expand_tabs($line); 1154a0df2efSAndy Whitcroft 1164a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 1174a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 1184a0df2efSAndy Whitcroft 1194a0df2efSAndy Whitcroft return (length($line), length($white)); 1204a0df2efSAndy Whitcroft} 1214a0df2efSAndy Whitcroft 122*00df344fSAndy Whitcroftsub sanitise_line { 123*00df344fSAndy Whitcroft my ($line) = @_; 124*00df344fSAndy Whitcroft 125*00df344fSAndy Whitcroft my $res = ''; 126*00df344fSAndy Whitcroft my $l = ''; 127*00df344fSAndy Whitcroft 128*00df344fSAndy Whitcroft my $quote = ''; 129*00df344fSAndy Whitcroft 130*00df344fSAndy Whitcroft foreach my $c (split(//, $line)) { 131*00df344fSAndy Whitcroft if ($l ne "\\" && ($c eq "'" || $c eq '"')) { 132*00df344fSAndy Whitcroft if ($quote eq '') { 133*00df344fSAndy Whitcroft $quote = $c; 134*00df344fSAndy Whitcroft $res .= $c; 135*00df344fSAndy Whitcroft $l = $c; 136*00df344fSAndy Whitcroft next; 137*00df344fSAndy Whitcroft } elsif ($quote eq $c) { 138*00df344fSAndy Whitcroft $quote = ''; 139*00df344fSAndy Whitcroft } 140*00df344fSAndy Whitcroft } 141*00df344fSAndy Whitcroft if ($quote && $c ne "\t") { 142*00df344fSAndy Whitcroft $res .= "X"; 143*00df344fSAndy Whitcroft } else { 144*00df344fSAndy Whitcroft $res .= $c; 145*00df344fSAndy Whitcroft } 146*00df344fSAndy Whitcroft 147*00df344fSAndy Whitcroft $l = $c; 148*00df344fSAndy Whitcroft } 149*00df344fSAndy Whitcroft 150*00df344fSAndy Whitcroft return $res; 151*00df344fSAndy Whitcroft} 152*00df344fSAndy Whitcroft 1534a0df2efSAndy Whitcroftsub ctx_block_get { 1544a0df2efSAndy Whitcroft my ($linenr, $remain, $outer) = @_; 1554a0df2efSAndy Whitcroft my $line; 1564a0df2efSAndy Whitcroft my $start = $linenr - 1; 1574a0df2efSAndy Whitcroft my $blk = ''; 1584a0df2efSAndy Whitcroft my @o; 1594a0df2efSAndy Whitcroft my @c; 1604a0df2efSAndy Whitcroft my @res = (); 1614a0df2efSAndy Whitcroft 162*00df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 163*00df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 164*00df344fSAndy Whitcroft $remain--; 165*00df344fSAndy Whitcroft 166*00df344fSAndy Whitcroft $blk .= $rawlines[$line]; 1674a0df2efSAndy Whitcroft 1684a0df2efSAndy Whitcroft @o = ($blk =~ /\{/g); 1694a0df2efSAndy Whitcroft @c = ($blk =~ /\}/g); 1704a0df2efSAndy Whitcroft 1714a0df2efSAndy Whitcroft if (!$outer || (scalar(@o) - scalar(@c)) == 1) { 172*00df344fSAndy Whitcroft push(@res, $rawlines[$line]); 1734a0df2efSAndy Whitcroft } 1744a0df2efSAndy Whitcroft 1754a0df2efSAndy Whitcroft last if (scalar(@o) == scalar(@c)); 1764a0df2efSAndy Whitcroft } 1774a0df2efSAndy Whitcroft 1784a0df2efSAndy Whitcroft return @res; 1794a0df2efSAndy Whitcroft} 1804a0df2efSAndy Whitcroftsub ctx_block_outer { 1814a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 1824a0df2efSAndy Whitcroft 1834a0df2efSAndy Whitcroft return ctx_block_get($linenr, $remain, 1); 1844a0df2efSAndy Whitcroft} 1854a0df2efSAndy Whitcroftsub ctx_block { 1864a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 1874a0df2efSAndy Whitcroft 1884a0df2efSAndy Whitcroft return ctx_block_get($linenr, $remain, 0); 1894a0df2efSAndy Whitcroft} 1904a0df2efSAndy Whitcroft 1914a0df2efSAndy Whitcroftsub ctx_locate_comment { 1924a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 1934a0df2efSAndy Whitcroft 1944a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 195*00df344fSAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); 1964a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 1974a0df2efSAndy Whitcroft 1984a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 1994a0df2efSAndy Whitcroft # comment. 2004a0df2efSAndy Whitcroft my $in_comment = 0; 2014a0df2efSAndy Whitcroft $current_comment = ''; 2024a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 203*00df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 204*00df344fSAndy Whitcroft #warn " $line\n"; 2054a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 2064a0df2efSAndy Whitcroft $in_comment = 1; 2074a0df2efSAndy Whitcroft } 2084a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 2094a0df2efSAndy Whitcroft $in_comment = 1; 2104a0df2efSAndy Whitcroft } 2114a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 2124a0df2efSAndy Whitcroft $current_comment = ''; 2134a0df2efSAndy Whitcroft } 2144a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 2154a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 2164a0df2efSAndy Whitcroft $in_comment = 0; 2174a0df2efSAndy Whitcroft } 2184a0df2efSAndy Whitcroft } 2194a0df2efSAndy Whitcroft 2204a0df2efSAndy Whitcroft chomp($current_comment); 2214a0df2efSAndy Whitcroft return($current_comment); 2224a0df2efSAndy Whitcroft} 2234a0df2efSAndy Whitcroftsub ctx_has_comment { 2244a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 2254a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 2264a0df2efSAndy Whitcroft 227*00df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 2284a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 2294a0df2efSAndy Whitcroft 2304a0df2efSAndy Whitcroft return ($cmt ne ''); 2314a0df2efSAndy Whitcroft} 2324a0df2efSAndy Whitcroft 2330a920b5bSAndy Whitcroftsub cat_vet { 2340a920b5bSAndy Whitcroft my ($vet) = @_; 2350a920b5bSAndy Whitcroft 2360a920b5bSAndy Whitcroft $vet =~ s/\t/^I/; 2370a920b5bSAndy Whitcroft $vet =~ s/$/\$/; 2380a920b5bSAndy Whitcroft 2390a920b5bSAndy Whitcroft return $vet; 2400a920b5bSAndy Whitcroft} 2410a920b5bSAndy Whitcroft 2420a920b5bSAndy Whitcroftsub process { 2430a920b5bSAndy Whitcroft my $filename = shift; 2440a920b5bSAndy Whitcroft my @lines = @_; 2450a920b5bSAndy Whitcroft 2460a920b5bSAndy Whitcroft my $linenr=0; 2470a920b5bSAndy Whitcroft my $prevline=""; 2480a920b5bSAndy Whitcroft my $stashline=""; 2490a920b5bSAndy Whitcroft 2504a0df2efSAndy Whitcroft my $length; 2510a920b5bSAndy Whitcroft my $indent; 2520a920b5bSAndy Whitcroft my $previndent=0; 2530a920b5bSAndy Whitcroft my $stashindent=0; 2540a920b5bSAndy Whitcroft 2550a920b5bSAndy Whitcroft my $clean = 1; 2560a920b5bSAndy Whitcroft my $signoff = 0; 2570a920b5bSAndy Whitcroft my $is_patch = 0; 2580a920b5bSAndy Whitcroft 2590a920b5bSAndy Whitcroft # Trace the real file/line as we go. 2600a920b5bSAndy Whitcroft my $realfile = ''; 2610a920b5bSAndy Whitcroft my $realline = 0; 2620a920b5bSAndy Whitcroft my $realcnt = 0; 2630a920b5bSAndy Whitcroft my $here = ''; 2640a920b5bSAndy Whitcroft my $in_comment = 0; 2650a920b5bSAndy Whitcroft my $first_line = 0; 2660a920b5bSAndy Whitcroft 2670a920b5bSAndy Whitcroft foreach my $line (@lines) { 2680a920b5bSAndy Whitcroft $linenr++; 2690a920b5bSAndy Whitcroft 2700a920b5bSAndy Whitcroft#extract the filename as it passes 2710a920b5bSAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 2720a920b5bSAndy Whitcroft $realfile=$1; 273*00df344fSAndy Whitcroft $realfile =~ s@^[^/]*/@@; 2740a920b5bSAndy Whitcroft $in_comment = 0; 2750a920b5bSAndy Whitcroft next; 2760a920b5bSAndy Whitcroft } 2770a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2780a920b5bSAndy Whitcroft if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { 2790a920b5bSAndy Whitcroft $is_patch = 1; 2804a0df2efSAndy Whitcroft $first_line = $linenr + 1; 2810a920b5bSAndy Whitcroft $in_comment = 0; 2820a920b5bSAndy Whitcroft $realline=$1-1; 2830a920b5bSAndy Whitcroft if (defined $2) { 2840a920b5bSAndy Whitcroft $realcnt=$3+1; 2850a920b5bSAndy Whitcroft } else { 2860a920b5bSAndy Whitcroft $realcnt=1+1; 2870a920b5bSAndy Whitcroft } 2880a920b5bSAndy Whitcroft next; 2890a920b5bSAndy Whitcroft } 2900a920b5bSAndy Whitcroft 2914a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 2924a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 2934a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2944a0df2efSAndy Whitcroft if ($line =~ /^( |\+|$)/) { 2950a920b5bSAndy Whitcroft $realline++; 2960a920b5bSAndy Whitcroft 2970a920b5bSAndy Whitcroft # track any sort of multi-line comment. Obviously if 2980a920b5bSAndy Whitcroft # the added text or context do not include the whole 2990a920b5bSAndy Whitcroft # comment we will not see it. Such is life. 3000a920b5bSAndy Whitcroft # 3010a920b5bSAndy Whitcroft # Guestimate if this is a continuing comment. If this 3020a920b5bSAndy Whitcroft # is the start of a diff block and this line starts 3030a920b5bSAndy Whitcroft # ' *' then it is very likely a comment. 3044a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 3050a920b5bSAndy Whitcroft $in_comment = 1; 3060a920b5bSAndy Whitcroft } 3070a920b5bSAndy Whitcroft if ($line =~ m@/\*@) { 3080a920b5bSAndy Whitcroft $in_comment = 1; 3090a920b5bSAndy Whitcroft } 3100a920b5bSAndy Whitcroft if ($line =~ m@\*/@) { 3110a920b5bSAndy Whitcroft $in_comment = 0; 3120a920b5bSAndy Whitcroft } 3130a920b5bSAndy Whitcroft 3144a0df2efSAndy Whitcroft # Measure the line length and indent. 3154a0df2efSAndy Whitcroft ($length, $indent) = line_stats($line); 3160a920b5bSAndy Whitcroft 3170a920b5bSAndy Whitcroft # Track the previous line. 3180a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 3190a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 3200a920b5bSAndy Whitcroft } 321*00df344fSAndy Whitcroft $realcnt-- if ($realcnt != 0); 3220a920b5bSAndy Whitcroft 3230a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 324389834b6SRandy Dunlap $here = "#$linenr: "; 325389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 3260a920b5bSAndy Whitcroft 327*00df344fSAndy Whitcroft my $hereline = "$here\n$line\n"; 3280a920b5bSAndy Whitcroft my $herecurr = "$here\n$line\n\n"; 3290a920b5bSAndy Whitcroft my $hereprev = "$here\n$prevline\n$line\n\n"; 3300a920b5bSAndy Whitcroft 3310a920b5bSAndy Whitcroft#check the patch for a signoff: 3320a920b5bSAndy Whitcroft if ($line =~ /^\s*Signed-off-by:\s/) { 3330a920b5bSAndy Whitcroft $signoff++; 3340a920b5bSAndy Whitcroft 3350a920b5bSAndy Whitcroft } elsif ($line =~ /^\s*signed-off-by:/i) { 3364a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 3374a0df2efSAndy Whitcroft $signoff++; 3380a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 3390a920b5bSAndy Whitcroft print "use Signed-off-by:\n"; 3400a920b5bSAndy Whitcroft print "$herecurr"; 3410a920b5bSAndy Whitcroft $clean = 0; 3420a920b5bSAndy Whitcroft } 3430a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 3440a920b5bSAndy Whitcroft print "need space after Signed-off-by:\n"; 3450a920b5bSAndy Whitcroft print "$herecurr"; 3460a920b5bSAndy Whitcroft $clean = 0; 3470a920b5bSAndy Whitcroft } 3480a920b5bSAndy Whitcroft } 3490a920b5bSAndy Whitcroft 350*00df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 351*00df344fSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { 352*00df344fSAndy Whitcroft print "patch seems to be corrupt (line wrapped?) [$realcnt]\n"; 353*00df344fSAndy Whitcroft print "$herecurr"; 354*00df344fSAndy Whitcroft $clean = 0; 355*00df344fSAndy Whitcroft } 3560a920b5bSAndy Whitcroft 357*00df344fSAndy Whitcroft#ignore lines being removed 358*00df344fSAndy Whitcroft if ($line=~/^-/) {next;} 359*00df344fSAndy Whitcroft 360*00df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 361*00df344fSAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 3620a920b5bSAndy Whitcroft 3630a920b5bSAndy Whitcroft#trailing whitespace 364*00df344fSAndy Whitcroft if ($line=~/\+.*\S\s+$/) { 3650a920b5bSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 3660a920b5bSAndy Whitcroft print "trailing whitespace\n"; 3670a920b5bSAndy Whitcroft print "$herevet"; 3680a920b5bSAndy Whitcroft $clean = 0; 3690a920b5bSAndy Whitcroft } 3700a920b5bSAndy Whitcroft#80 column limit 371*00df344fSAndy Whitcroft if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { 3720a920b5bSAndy Whitcroft print "line over 80 characters\n"; 3730a920b5bSAndy Whitcroft print "$herecurr"; 3740a920b5bSAndy Whitcroft $clean = 0; 3750a920b5bSAndy Whitcroft } 3760a920b5bSAndy Whitcroft 3770a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk 3780a920b5bSAndy Whitcroft next if ($realfile !~ /\.[hc]$/); 3790a920b5bSAndy Whitcroft 3800a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 3810a920b5bSAndy Whitcroft# more than 8 must use tabs. 3820a920b5bSAndy Whitcroft if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { 3830a920b5bSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 3840a920b5bSAndy Whitcroft print "use tabs not spaces\n"; 3850a920b5bSAndy Whitcroft print "$herevet"; 3860a920b5bSAndy Whitcroft $clean = 0; 3870a920b5bSAndy Whitcroft } 3880a920b5bSAndy Whitcroft 3890a920b5bSAndy Whitcroft # 3900a920b5bSAndy Whitcroft # The rest of our checks refer specifically to C style 3910a920b5bSAndy Whitcroft # only apply those _outside_ comments. 3920a920b5bSAndy Whitcroft # 3930a920b5bSAndy Whitcroft next if ($in_comment); 3940a920b5bSAndy Whitcroft 3950a920b5bSAndy Whitcroft # Remove comments from the line before processing. 3960a920b5bSAndy Whitcroft $line =~ s@/\*.*\*/@@g; 3970a920b5bSAndy Whitcroft $line =~ s@/\*.*@@; 3980a920b5bSAndy Whitcroft $line =~ s@.*\*/@@; 399*00df344fSAndy Whitcroft 400*00df344fSAndy Whitcroft # 401*00df344fSAndy Whitcroft # Checks which may be anchored in the context. 402*00df344fSAndy Whitcroft # 403*00df344fSAndy Whitcroft 404*00df344fSAndy Whitcroft # Check for switch () and associated case and default 405*00df344fSAndy Whitcroft # statements should be at the same indent. 406*00df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 407*00df344fSAndy Whitcroft my $err = ''; 408*00df344fSAndy Whitcroft my $sep = ''; 409*00df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 410*00df344fSAndy Whitcroft shift(@ctx); 411*00df344fSAndy Whitcroft for my $ctx (@ctx) { 412*00df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 413*00df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 414*00df344fSAndy Whitcroft $indent != $cindent) { 415*00df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 416*00df344fSAndy Whitcroft $sep = ''; 417*00df344fSAndy Whitcroft } else { 418*00df344fSAndy Whitcroft $sep = "[...]\n"; 419*00df344fSAndy Whitcroft } 420*00df344fSAndy Whitcroft } 421*00df344fSAndy Whitcroft if ($err ne '') { 422*00df344fSAndy Whitcroft print "switch and case should be at the same indent\n"; 423*00df344fSAndy Whitcroft print "$here\n$line\n$err\n"; 424*00df344fSAndy Whitcroft $clean = 0; 425*00df344fSAndy Whitcroft } 426*00df344fSAndy Whitcroft } 427*00df344fSAndy Whitcroft 428*00df344fSAndy Whitcroft#ignore lines not being added 429*00df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 430*00df344fSAndy Whitcroft 431*00df344fSAndy Whitcroft # 432*00df344fSAndy Whitcroft # Checks which are anchored on the added line. 433*00df344fSAndy Whitcroft # 434*00df344fSAndy Whitcroft 435*00df344fSAndy Whitcroft# no C99 // comments 436*00df344fSAndy Whitcroft if ($line =~ m{//}) { 437*00df344fSAndy Whitcroft print "do not use C99 // comments\n"; 438*00df344fSAndy Whitcroft print "$herecurr"; 439*00df344fSAndy Whitcroft $clean = 0; 440*00df344fSAndy Whitcroft } 441*00df344fSAndy Whitcroft # Remove C99 comments. 4420a920b5bSAndy Whitcroft $line =~ s@//.*@@; 4430a920b5bSAndy Whitcroft 444*00df344fSAndy Whitcroft # Standardise the strings and chars within the input 445*00df344fSAndy Whitcroft # to simplify matching. 446*00df344fSAndy Whitcroft $line = sanitise_line($line); 447*00df344fSAndy Whitcroft 4480a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 4490a920b5bSAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) || 4500a920b5bSAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) { 4510a920b5bSAndy Whitcroft if (($prevline !~ /^}/) && 4520a920b5bSAndy Whitcroft ($prevline !~ /^\+}/) && 4530a920b5bSAndy Whitcroft ($prevline !~ /^ }/)) { 4540a920b5bSAndy Whitcroft print "EXPORT_SYMBOL(func); should immediately follow its function\n"; 4550a920b5bSAndy Whitcroft print "$herecurr"; 4560a920b5bSAndy Whitcroft $clean = 0; 4570a920b5bSAndy Whitcroft } 4580a920b5bSAndy Whitcroft } 4590a920b5bSAndy Whitcroft 4600a920b5bSAndy Whitcroft # check for static initialisers. 4610a920b5bSAndy Whitcroft if ($line=~/\s*static\s.*=\s+(0|NULL);/) { 4620a920b5bSAndy Whitcroft print "do not initialise statics to 0 or NULL\n"; 4630a920b5bSAndy Whitcroft print "$herecurr"; 4640a920b5bSAndy Whitcroft $clean = 0; 4650a920b5bSAndy Whitcroft } 4660a920b5bSAndy Whitcroft 4670a920b5bSAndy Whitcroft # check for new typedefs. 4680a920b5bSAndy Whitcroft if ($line=~/\s*typedef\s/) { 4690a920b5bSAndy Whitcroft print "do not add new typedefs\n"; 4700a920b5bSAndy Whitcroft print "$herecurr"; 4710a920b5bSAndy Whitcroft $clean = 0; 4720a920b5bSAndy Whitcroft } 4730a920b5bSAndy Whitcroft 4740a920b5bSAndy Whitcroft# * goes on variable not on type 475*00df344fSAndy Whitcroft my $type = '(?:char|short|int|long|unsigned|float|double|' . 476*00df344fSAndy Whitcroft 'struct\s+[A-Za-z\d_]+|' . 477*00df344fSAndy Whitcroft 'union\s+[A-Za-z\d_]+)'; 478*00df344fSAndy Whitcroft 479*00df344fSAndy Whitcroft if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { 480*00df344fSAndy Whitcroft print "\"foo$1 bar\" should be \"foo $1bar\"\n"; 481*00df344fSAndy Whitcroft print "$herecurr"; 482*00df344fSAndy Whitcroft $clean = 0; 483*00df344fSAndy Whitcroft } 484*00df344fSAndy Whitcroft if ($line =~ m{$type (\*) [A-Za-z\d_]+} || 485*00df344fSAndy Whitcroft $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { 486*00df344fSAndy Whitcroft print "\"foo $1 bar\" should be \"foo $1bar\"\n"; 487*00df344fSAndy Whitcroft print "$herecurr"; 488*00df344fSAndy Whitcroft $clean = 0; 489*00df344fSAndy Whitcroft } 490*00df344fSAndy Whitcroft if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) { 491*00df344fSAndy Whitcroft print "\"(foo$1)\" should be \"(foo $1)\"\n"; 492*00df344fSAndy Whitcroft print "$herecurr"; 493*00df344fSAndy Whitcroft $clean = 0; 494*00df344fSAndy Whitcroft } 495*00df344fSAndy Whitcroft if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) { 496*00df344fSAndy Whitcroft print "\"(foo $1 )\" should be \"(foo $1)\"\n"; 4970a920b5bSAndy Whitcroft print "$herecurr"; 4980a920b5bSAndy Whitcroft $clean = 0; 4990a920b5bSAndy Whitcroft } 5000a920b5bSAndy Whitcroft 5010a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 5020a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 5030a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 5040a920b5bSAndy Whitcroft# print "$herecurr"; 5050a920b5bSAndy Whitcroft# $clean = 0; 5060a920b5bSAndy Whitcroft# } 5070a920b5bSAndy Whitcroft 508*00df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 509*00df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 510*00df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 511*00df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 512*00df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 5130a920b5bSAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)/) { 514*00df344fSAndy Whitcroft my $ok = 0; 515*00df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 516*00df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 517*00df344fSAndy Whitcroft # we have a preceeding printk if it ends 518*00df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 519*00df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 520*00df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 521*00df344fSAndy Whitcroft $ok = 1; 522*00df344fSAndy Whitcroft } 523*00df344fSAndy Whitcroft last; 524*00df344fSAndy Whitcroft } 525*00df344fSAndy Whitcroft } 526*00df344fSAndy Whitcroft if ($ok == 0) { 5270a920b5bSAndy Whitcroft print "printk() should include KERN_ facility level\n"; 5280a920b5bSAndy Whitcroft print "$herecurr"; 5290a920b5bSAndy Whitcroft $clean = 0; 5300a920b5bSAndy Whitcroft } 531*00df344fSAndy Whitcroft } 5320a920b5bSAndy Whitcroft 5330a920b5bSAndy Whitcroft#function brace can't be on same line, except for #defines of do while, or if closed on same line 5340a920b5bSAndy Whitcroft if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and 5350a920b5bSAndy Whitcroft !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 5360a920b5bSAndy Whitcroft print "braces following function declarations go on the next line\n"; 5370a920b5bSAndy Whitcroft print "$herecurr"; 5380a920b5bSAndy Whitcroft $clean = 0; 5390a920b5bSAndy Whitcroft } 5404a0df2efSAndy Whitcroft # Note we expand the line with the leading + as the real 5414a0df2efSAndy Whitcroft # line will be displayed with the leading + and the tabs 5424a0df2efSAndy Whitcroft # will therefore also expand that way. 5430a920b5bSAndy Whitcroft my $opline = $line; 5444a0df2efSAndy Whitcroft $opline = expand_tabs($opline); 5450a920b5bSAndy Whitcroft $opline =~ s/^./ /; 5460a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 5470a920b5bSAndy Whitcroft # Check operator spacing. 5480a920b5bSAndy Whitcroft my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); 549*00df344fSAndy Whitcroft my $off = 0; 5500a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 5514a0df2efSAndy Whitcroft $off += length($elements[$n]); 5524a0df2efSAndy Whitcroft 5534a0df2efSAndy Whitcroft my $a = ''; 5544a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 5554a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 5564a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 5574a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 5584a0df2efSAndy Whitcroft $a = 'E' if ($elements[$n] eq '' && $n == 0); 5594a0df2efSAndy Whitcroft 5600a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 5614a0df2efSAndy Whitcroft 5624a0df2efSAndy Whitcroft my $c = ''; 5630a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 5644a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 5654a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 5664a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 5674a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 5684a0df2efSAndy Whitcroft } else { 5694a0df2efSAndy Whitcroft $c = 'E'; 5700a920b5bSAndy Whitcroft } 5710a920b5bSAndy Whitcroft 572*00df344fSAndy Whitcroft # Pick up the preceeding and succeeding characters. 573*00df344fSAndy Whitcroft my $ca = substr($opline, $off - 1, 1); 574*00df344fSAndy Whitcroft my $cc = ''; 575*00df344fSAndy Whitcroft if (length($opline) > ($off + length($elements[$n]))) { 576*00df344fSAndy Whitcroft $cc = substr($opline, $off + 1 + length($elements[$n]), 1); 577*00df344fSAndy Whitcroft } 578*00df344fSAndy Whitcroft 5794a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 5804a0df2efSAndy Whitcroft 5814a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 5824a0df2efSAndy Whitcroft 5834a0df2efSAndy Whitcroft my $ptr = (" " x $off) . "^"; 584*00df344fSAndy Whitcroft my $hereptr = "$hereline$ptr\n\n"; 5850a920b5bSAndy Whitcroft 5860a920b5bSAndy Whitcroft ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; 5870a920b5bSAndy Whitcroft 5880a920b5bSAndy Whitcroft # We need ; as an operator. // is a comment. 5890a920b5bSAndy Whitcroft if ($op eq ';' or $op eq '//') { 5900a920b5bSAndy Whitcroft 5910a920b5bSAndy Whitcroft # -> should have no spaces 5920a920b5bSAndy Whitcroft } elsif ($op eq '->') { 5934a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 5940a920b5bSAndy Whitcroft print "no spaces around that '$op' $at\n"; 5954a0df2efSAndy Whitcroft print "$hereptr"; 5960a920b5bSAndy Whitcroft $clean = 0; 5970a920b5bSAndy Whitcroft } 5980a920b5bSAndy Whitcroft 5990a920b5bSAndy Whitcroft # , must have a space on the right. 6000a920b5bSAndy Whitcroft } elsif ($op eq ',') { 6014a0df2efSAndy Whitcroft if ($ctx !~ /.xW|.xE/) { 6020a920b5bSAndy Whitcroft print "need space after that '$op' $at\n"; 6034a0df2efSAndy Whitcroft print "$hereptr"; 6040a920b5bSAndy Whitcroft $clean = 0; 6050a920b5bSAndy Whitcroft } 6060a920b5bSAndy Whitcroft 6070a920b5bSAndy Whitcroft # unary ! and unary ~ are allowed no space on the right 6080a920b5bSAndy Whitcroft } elsif ($op eq '!' or $op eq '~') { 6094a0df2efSAndy Whitcroft if ($ctx !~ /[WOEB]x./) { 6100a920b5bSAndy Whitcroft print "need space before that '$op' $at\n"; 6114a0df2efSAndy Whitcroft print "$hereptr"; 6120a920b5bSAndy Whitcroft $clean = 0; 6130a920b5bSAndy Whitcroft } 6144a0df2efSAndy Whitcroft if ($ctx =~ /.xW/) { 6150a920b5bSAndy Whitcroft print "no space after that '$op' $at\n"; 6164a0df2efSAndy Whitcroft print "$hereptr"; 6170a920b5bSAndy Whitcroft $clean = 0; 6180a920b5bSAndy Whitcroft } 6190a920b5bSAndy Whitcroft 6200a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 6210a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 6224a0df2efSAndy Whitcroft if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) { 6230a920b5bSAndy Whitcroft print "need space one side of that '$op' $at\n"; 6244a0df2efSAndy Whitcroft print "$hereptr"; 6250a920b5bSAndy Whitcroft $clean = 0; 6260a920b5bSAndy Whitcroft } 6270a920b5bSAndy Whitcroft 6280a920b5bSAndy Whitcroft # & is both unary and binary 6290a920b5bSAndy Whitcroft # unary: 6300a920b5bSAndy Whitcroft # a &b 6310a920b5bSAndy Whitcroft # binary (consistent spacing): 6320a920b5bSAndy Whitcroft # a&b OK 6330a920b5bSAndy Whitcroft # a & b OK 6340a920b5bSAndy Whitcroft # 6350a920b5bSAndy Whitcroft # boiling down to: if there is a space on the right then there 6360a920b5bSAndy Whitcroft # should be one on the left. 6370a920b5bSAndy Whitcroft # 6380a920b5bSAndy Whitcroft # - is the same 6390a920b5bSAndy Whitcroft # 6404a0df2efSAndy Whitcroft } elsif ($op eq '&' or $op eq '-') { 641*00df344fSAndy Whitcroft if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) { 6420a920b5bSAndy Whitcroft print "need space before that '$op' $at\n"; 6434a0df2efSAndy Whitcroft print "$hereptr"; 6444a0df2efSAndy Whitcroft $clean = 0; 6454a0df2efSAndy Whitcroft } 6464a0df2efSAndy Whitcroft 647*00df344fSAndy Whitcroft # * is the same as & only adding: 648*00df344fSAndy Whitcroft # type: 649*00df344fSAndy Whitcroft # (foo *) 650*00df344fSAndy Whitcroft # (foo **) 651*00df344fSAndy Whitcroft # 6524a0df2efSAndy Whitcroft } elsif ($op eq '*') { 653*00df344fSAndy Whitcroft if ($ca eq '*') { 654*00df344fSAndy Whitcroft if ($cc =~ /\s/) { 655*00df344fSAndy Whitcroft print "no space after that '$op' $at\n"; 656*00df344fSAndy Whitcroft print "$hereptr"; 657*00df344fSAndy Whitcroft $clean = 0; 658*00df344fSAndy Whitcroft } 659*00df344fSAndy Whitcroft } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB/) { 6604a0df2efSAndy Whitcroft print "need space before that '$op' $at\n"; 6614a0df2efSAndy Whitcroft print "$hereptr"; 6620a920b5bSAndy Whitcroft $clean = 0; 6630a920b5bSAndy Whitcroft } 6640a920b5bSAndy Whitcroft 6650a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 6660a920b5bSAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or 6670a920b5bSAndy Whitcroft $op eq '^' or $op eq '|') 6680a920b5bSAndy Whitcroft { 6694a0df2efSAndy Whitcroft if ($ctx !~ /VxV|WxW|VxE|WxE/) { 6700a920b5bSAndy Whitcroft print "need consistent spacing around '$op' $at\n"; 6714a0df2efSAndy Whitcroft print "$hereptr"; 6720a920b5bSAndy Whitcroft $clean = 0; 6730a920b5bSAndy Whitcroft } 6740a920b5bSAndy Whitcroft 6750a920b5bSAndy Whitcroft # All the others need spaces both sides. 6764a0df2efSAndy Whitcroft } elsif ($ctx !~ /[EW]x[WE]/) { 6770a920b5bSAndy Whitcroft print "need spaces around that '$op' $at\n"; 6784a0df2efSAndy Whitcroft print "$hereptr"; 6790a920b5bSAndy Whitcroft $clean = 0; 6800a920b5bSAndy Whitcroft } 6814a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 6820a920b5bSAndy Whitcroft } 6830a920b5bSAndy Whitcroft } 6840a920b5bSAndy Whitcroft 6850a920b5bSAndy Whitcroft#need space before brace following if, while, etc 6860a920b5bSAndy Whitcroft if ($line=~/\(.*\){/) { 6870a920b5bSAndy Whitcroft print "need a space before the brace\n"; 6880a920b5bSAndy Whitcroft print "$herecurr"; 6890a920b5bSAndy Whitcroft $clean = 0; 6900a920b5bSAndy Whitcroft } 6910a920b5bSAndy Whitcroft 6920a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 6934a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 6940a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 6950a920b5bSAndy Whitcroft print "labels should not be indented\n"; 6960a920b5bSAndy Whitcroft print "$herecurr"; 6970a920b5bSAndy Whitcroft $clean = 0; 6980a920b5bSAndy Whitcroft } 6990a920b5bSAndy Whitcroft 7000a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 7014a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 7020a920b5bSAndy Whitcroft print "need a space before the open parenthesis\n"; 7030a920b5bSAndy Whitcroft print "$herecurr"; 7040a920b5bSAndy Whitcroft $clean = 0; 7050a920b5bSAndy Whitcroft } 7060a920b5bSAndy Whitcroft 7070a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional. 7084a0df2efSAndy Whitcroft if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) { 709*00df344fSAndy Whitcroft #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 7104a0df2efSAndy Whitcroft print "do not use assignment in condition\n"; 7110a920b5bSAndy Whitcroft print "$herecurr"; 7120a920b5bSAndy Whitcroft $clean = 0; 7130a920b5bSAndy Whitcroft } 7140a920b5bSAndy Whitcroft 7150a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 7160a920b5bSAndy Whitcroft # indent level to be relevant to each other. 7170a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 7180a920b5bSAndy Whitcroft $previndent == $indent) { 7190a920b5bSAndy Whitcroft print "else should follow close brace\n"; 7200a920b5bSAndy Whitcroft print "$hereprev"; 7210a920b5bSAndy Whitcroft $clean = 0; 7220a920b5bSAndy Whitcroft } 7230a920b5bSAndy Whitcroft 7240a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 7250a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 7260a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 7270a920b5bSAndy Whitcroft# print "$herecurr"; 7280a920b5bSAndy Whitcroft# $clean = 0; 7290a920b5bSAndy Whitcroft# } 7300a920b5bSAndy Whitcroft 7310a920b5bSAndy Whitcroft#no spaces allowed after \ in define 7320a920b5bSAndy Whitcroft if ($line=~/\#define.*\\\s$/) { 7330a920b5bSAndy Whitcroft print("Whitepspace after \\ makes next lines useless\n"); 7340a920b5bSAndy Whitcroft print "$herecurr"; 7350a920b5bSAndy Whitcroft $clean = 0; 7360a920b5bSAndy Whitcroft } 7370a920b5bSAndy Whitcroft 7380a920b5bSAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available. 7390a920b5bSAndy Whitcroft if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) { 7400a920b5bSAndy Whitcroft my $checkfile = "include/linux/$1.h"; 7410a920b5bSAndy Whitcroft if (-f $checkfile) { 7420a920b5bSAndy Whitcroft print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; 7430a920b5bSAndy Whitcroft print $herecurr; 7440a920b5bSAndy Whitcroft $clean = 0; 7450a920b5bSAndy Whitcroft } 7460a920b5bSAndy Whitcroft } 7470a920b5bSAndy Whitcroft 7480a920b5bSAndy Whitcroft#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 7494a0df2efSAndy Whitcroft if ($prevline=~/\b(if|while|for|switch)\s*\(/) { 7500a920b5bSAndy Whitcroft my @opened = $prevline=~/\(/g; 7510a920b5bSAndy Whitcroft my @closed = $prevline=~/\)/g; 7520a920b5bSAndy Whitcroft my $nr_line = $linenr; 753*00df344fSAndy Whitcroft my $remaining = $realcnt - 1; 7540a920b5bSAndy Whitcroft my $next_line = $line; 7550a920b5bSAndy Whitcroft my $extra_lines = 0; 7560a920b5bSAndy Whitcroft my $display_segment = $prevline; 7570a920b5bSAndy Whitcroft 758*00df344fSAndy Whitcroft while ($remaining > 0 && scalar @opened > scalar @closed) { 7590a920b5bSAndy Whitcroft $prevline .= $next_line; 7600a920b5bSAndy Whitcroft $display_segment .= "\n" . $next_line; 7610a920b5bSAndy Whitcroft $next_line = $lines[$nr_line]; 7620a920b5bSAndy Whitcroft $nr_line++; 7630a920b5bSAndy Whitcroft $remaining--; 7640a920b5bSAndy Whitcroft 7650a920b5bSAndy Whitcroft @opened = $prevline=~/\(/g; 7660a920b5bSAndy Whitcroft @closed = $prevline=~/\)/g; 7670a920b5bSAndy Whitcroft } 7680a920b5bSAndy Whitcroft 7694a0df2efSAndy Whitcroft if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and 7704a0df2efSAndy Whitcroft !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) { 7710a920b5bSAndy Whitcroft print "That { should be on the previous line\n"; 7724a0df2efSAndy Whitcroft print "$here\n$display_segment\n$next_line\n\n"; 7730a920b5bSAndy Whitcroft $clean = 0; 7740a920b5bSAndy Whitcroft } 7750a920b5bSAndy Whitcroft } 7760a920b5bSAndy Whitcroft 7770a920b5bSAndy Whitcroft#multiline macros should be enclosed in a do while loop 7780a920b5bSAndy Whitcroft if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and 7790a920b5bSAndy Whitcroft !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and 7800a920b5bSAndy Whitcroft !($line=~/do.*{/) and !($line=~/\(\{/)) { 7810a920b5bSAndy Whitcroft print "Macros with multiple statements should be enclosed in a do - while loop\n"; 7820a920b5bSAndy Whitcroft print "$hereprev"; 7830a920b5bSAndy Whitcroft $clean = 0; 7840a920b5bSAndy Whitcroft } 7850a920b5bSAndy Whitcroft 7860a920b5bSAndy Whitcroft# don't include deprecated include files 7874a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 7880a920b5bSAndy Whitcroft if ($line =~ m@\#\s*include\s*\<$inc>@) { 7890a920b5bSAndy Whitcroft print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; 7900a920b5bSAndy Whitcroft print "$herecurr"; 7910a920b5bSAndy Whitcroft $clean = 0; 7920a920b5bSAndy Whitcroft } 7930a920b5bSAndy Whitcroft } 7940a920b5bSAndy Whitcroft 7954a0df2efSAndy Whitcroft# don't use deprecated functions 7964a0df2efSAndy Whitcroft for my $func (@dep_functions) { 797*00df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 7984a0df2efSAndy Whitcroft print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; 7994a0df2efSAndy Whitcroft print "$herecurr"; 8004a0df2efSAndy Whitcroft $clean = 0; 8014a0df2efSAndy Whitcroft } 8024a0df2efSAndy Whitcroft } 8034a0df2efSAndy Whitcroft 8044a0df2efSAndy Whitcroft# no volatiles please 805*00df344fSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { 8064a0df2efSAndy Whitcroft print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; 8074a0df2efSAndy Whitcroft print "$herecurr"; 8084a0df2efSAndy Whitcroft $clean = 0; 8094a0df2efSAndy Whitcroft } 8104a0df2efSAndy Whitcroft 811*00df344fSAndy Whitcroft# warn about #if 0 812*00df344fSAndy Whitcroft if ($line =~ /^.#\s*if\s+0\b/) { 813*00df344fSAndy Whitcroft print "#if 0 -- if this code redundant remove it\n"; 8144a0df2efSAndy Whitcroft print "$herecurr"; 8154a0df2efSAndy Whitcroft $clean = 0; 8164a0df2efSAndy Whitcroft } 8174a0df2efSAndy Whitcroft 818*00df344fSAndy Whitcroft# warn about #ifdefs in C files 819*00df344fSAndy Whitcroft# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 820*00df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 821*00df344fSAndy Whitcroft# print "$herecurr"; 822*00df344fSAndy Whitcroft# $clean = 0; 823*00df344fSAndy Whitcroft# } 824*00df344fSAndy Whitcroft 8254a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 8264a0df2efSAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { 8274a0df2efSAndy Whitcroft my $which = $1; 8284a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 8294a0df2efSAndy Whitcroft print "$1 definition without comment\n"; 8304a0df2efSAndy Whitcroft print "$herecurr"; 8314a0df2efSAndy Whitcroft $clean = 0; 8324a0df2efSAndy Whitcroft } 8334a0df2efSAndy Whitcroft } 8344a0df2efSAndy Whitcroft# check for memory barriers without a comment. 8354a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 8364a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 8374a0df2efSAndy Whitcroft print "memory barrier without comment\n"; 8384a0df2efSAndy Whitcroft print "$herecurr"; 8394a0df2efSAndy Whitcroft $clean = 0; 8404a0df2efSAndy Whitcroft } 8414a0df2efSAndy Whitcroft } 8424a0df2efSAndy Whitcroft# check of hardware specific defines 8434a0df2efSAndy Whitcroft if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { 8444a0df2efSAndy Whitcroft print "architecture specific defines should be avoided\n"; 8450a920b5bSAndy Whitcroft print "$herecurr"; 8460a920b5bSAndy Whitcroft $clean = 0; 8470a920b5bSAndy Whitcroft } 8480a920b5bSAndy Whitcroft } 8490a920b5bSAndy Whitcroft 8500a920b5bSAndy Whitcroft if ($chk_patch && !$is_patch) { 8510a920b5bSAndy Whitcroft $clean = 0; 8520a920b5bSAndy Whitcroft print "Does not appear to be a unified-diff format patch\n"; 8530a920b5bSAndy Whitcroft } 8540a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 8550a920b5bSAndy Whitcroft $clean = 0; 8560a920b5bSAndy Whitcroft print "Missing Signed-off-by: line(s)\n"; 8570a920b5bSAndy Whitcroft } 8580a920b5bSAndy Whitcroft 8590a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 8600a920b5bSAndy Whitcroft print "Your patch has no obvious style problems and is ready for submission.\n" 8610a920b5bSAndy Whitcroft } 8620a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 8630a920b5bSAndy Whitcroft print "Your patch has style problems, please review. If any of these errors\n"; 8640a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 8650a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 8660a920b5bSAndy Whitcroft } 8670a920b5bSAndy Whitcroft return $clean; 8680a920b5bSAndy Whitcroft} 869