10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 20a920b5bSAndy Whitcroft# (c) 2001, Dave Jones. <[email protected]> (the file handling bit) 300df344fSAndy 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; 1000df344fSAndy Whitcroft$P =~ s@.*/@@g; 110a920b5bSAndy Whitcroft 12*653d4876SAndy Whitcroftmy $V = '0.05'; 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; 20*653d4876SAndy Whitcroftmy $tst_type = 0; 210a920b5bSAndy WhitcroftGetOptions( 220a920b5bSAndy Whitcroft 'q|quiet' => \$quiet, 230a920b5bSAndy Whitcroft 'tree!' => \$tree, 240a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 250a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 26*653d4876SAndy Whitcroft 'test-type!' => \$tst_type, 270a920b5bSAndy Whitcroft) or exit; 280a920b5bSAndy Whitcroft 290a920b5bSAndy Whitcroftmy $exit = 0; 300a920b5bSAndy Whitcroft 310a920b5bSAndy Whitcroftif ($#ARGV < 0) { 3200df344fSAndy Whitcroft print "usage: $P [options] patchfile\n"; 330a920b5bSAndy Whitcroft print "version: $V\n"; 340a920b5bSAndy Whitcroft print "options: -q => quiet\n"; 350a920b5bSAndy Whitcroft print " --no-tree => run without a kernel tree\n"; 360a920b5bSAndy Whitcroft exit(1); 370a920b5bSAndy Whitcroft} 380a920b5bSAndy Whitcroft 390a920b5bSAndy Whitcroftif ($tree && !top_of_kernel_tree()) { 400a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 410a920b5bSAndy Whitcroft exit(2); 420a920b5bSAndy Whitcroft} 430a920b5bSAndy Whitcroft 444a0df2efSAndy Whitcroftmy @dep_includes = (); 454a0df2efSAndy Whitcroftmy @dep_functions = (); 460a920b5bSAndy Whitcroftmy $removal = 'Documentation/feature-removal-schedule.txt'; 470a920b5bSAndy Whitcroftif ($tree && -f $removal) { 480a920b5bSAndy Whitcroft open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; 490a920b5bSAndy Whitcroft while (<REMOVE>) { 500a920b5bSAndy Whitcroft if (/^Files:\s+(.*\S)/) { 510a920b5bSAndy Whitcroft for my $file (split(/[, ]+/, $1)) { 520a920b5bSAndy Whitcroft if ($file =~ m@include/(.*)@) { 534a0df2efSAndy Whitcroft push(@dep_includes, $1); 540a920b5bSAndy Whitcroft } 550a920b5bSAndy Whitcroft } 564a0df2efSAndy Whitcroft 574a0df2efSAndy Whitcroft } elsif (/^Funcs:\s+(.*\S)/) { 584a0df2efSAndy Whitcroft for my $func (split(/[, ]+/, $1)) { 594a0df2efSAndy Whitcroft push(@dep_functions, $func); 604a0df2efSAndy Whitcroft } 610a920b5bSAndy Whitcroft } 620a920b5bSAndy Whitcroft } 630a920b5bSAndy Whitcroft} 640a920b5bSAndy Whitcroft 6500df344fSAndy Whitcroftmy @rawlines = (); 660a920b5bSAndy Whitcroftwhile (<>) { 670a920b5bSAndy Whitcroft chomp; 6800df344fSAndy Whitcroft push(@rawlines, $_); 690a920b5bSAndy Whitcroft if (eof(ARGV)) { 7000df344fSAndy Whitcroft if (!process($ARGV, @rawlines)) { 710a920b5bSAndy Whitcroft $exit = 1; 720a920b5bSAndy Whitcroft } 7300df344fSAndy Whitcroft @rawlines = (); 740a920b5bSAndy Whitcroft } 750a920b5bSAndy Whitcroft} 760a920b5bSAndy Whitcroft 770a920b5bSAndy Whitcroftexit($exit); 780a920b5bSAndy Whitcroft 790a920b5bSAndy Whitcroftsub top_of_kernel_tree { 800a920b5bSAndy Whitcroft if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && 810a920b5bSAndy Whitcroft (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && 820a920b5bSAndy Whitcroft (-d "Documentation") && (-d "arch") && (-d "include") && 830a920b5bSAndy Whitcroft (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && 840a920b5bSAndy Whitcroft (-d "kernel") && (-d "lib") && (-d "scripts")) { 850a920b5bSAndy Whitcroft return 1; 860a920b5bSAndy Whitcroft } 870a920b5bSAndy Whitcroft return 0; 880a920b5bSAndy Whitcroft} 890a920b5bSAndy Whitcroft 900a920b5bSAndy Whitcroftsub expand_tabs { 910a920b5bSAndy Whitcroft my ($str) = @_; 920a920b5bSAndy Whitcroft 930a920b5bSAndy Whitcroft my $res = ''; 940a920b5bSAndy Whitcroft my $n = 0; 950a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 960a920b5bSAndy Whitcroft if ($c eq "\t") { 970a920b5bSAndy Whitcroft $res .= ' '; 980a920b5bSAndy Whitcroft $n++; 990a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 1000a920b5bSAndy Whitcroft $res .= ' '; 1010a920b5bSAndy Whitcroft } 1020a920b5bSAndy Whitcroft next; 1030a920b5bSAndy Whitcroft } 1040a920b5bSAndy Whitcroft $res .= $c; 1050a920b5bSAndy Whitcroft $n++; 1060a920b5bSAndy Whitcroft } 1070a920b5bSAndy Whitcroft 1080a920b5bSAndy Whitcroft return $res; 1090a920b5bSAndy Whitcroft} 1100a920b5bSAndy Whitcroft 1114a0df2efSAndy Whitcroftsub line_stats { 1124a0df2efSAndy Whitcroft my ($line) = @_; 1134a0df2efSAndy Whitcroft 1144a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 1154a0df2efSAndy Whitcroft $line =~ s/^.//; 1164a0df2efSAndy Whitcroft $line = expand_tabs($line); 1174a0df2efSAndy Whitcroft 1184a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 1194a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 1204a0df2efSAndy Whitcroft 1214a0df2efSAndy Whitcroft return (length($line), length($white)); 1224a0df2efSAndy Whitcroft} 1234a0df2efSAndy Whitcroft 12400df344fSAndy Whitcroftsub sanitise_line { 12500df344fSAndy Whitcroft my ($line) = @_; 12600df344fSAndy Whitcroft 12700df344fSAndy Whitcroft my $res = ''; 12800df344fSAndy Whitcroft my $l = ''; 12900df344fSAndy Whitcroft 13000df344fSAndy Whitcroft my $quote = ''; 13100df344fSAndy Whitcroft 13200df344fSAndy Whitcroft foreach my $c (split(//, $line)) { 13300df344fSAndy Whitcroft if ($l ne "\\" && ($c eq "'" || $c eq '"')) { 13400df344fSAndy Whitcroft if ($quote eq '') { 13500df344fSAndy Whitcroft $quote = $c; 13600df344fSAndy Whitcroft $res .= $c; 13700df344fSAndy Whitcroft $l = $c; 13800df344fSAndy Whitcroft next; 13900df344fSAndy Whitcroft } elsif ($quote eq $c) { 14000df344fSAndy Whitcroft $quote = ''; 14100df344fSAndy Whitcroft } 14200df344fSAndy Whitcroft } 14300df344fSAndy Whitcroft if ($quote && $c ne "\t") { 14400df344fSAndy Whitcroft $res .= "X"; 14500df344fSAndy Whitcroft } else { 14600df344fSAndy Whitcroft $res .= $c; 14700df344fSAndy Whitcroft } 14800df344fSAndy Whitcroft 14900df344fSAndy Whitcroft $l = $c; 15000df344fSAndy Whitcroft } 15100df344fSAndy Whitcroft 15200df344fSAndy Whitcroft return $res; 15300df344fSAndy Whitcroft} 15400df344fSAndy Whitcroft 1554a0df2efSAndy Whitcroftsub ctx_block_get { 156*653d4876SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close) = @_; 1574a0df2efSAndy Whitcroft my $line; 1584a0df2efSAndy Whitcroft my $start = $linenr - 1; 1594a0df2efSAndy Whitcroft my $blk = ''; 1604a0df2efSAndy Whitcroft my @o; 1614a0df2efSAndy Whitcroft my @c; 1624a0df2efSAndy Whitcroft my @res = (); 1634a0df2efSAndy Whitcroft 16400df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 16500df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 16600df344fSAndy Whitcroft $remain--; 16700df344fSAndy Whitcroft 16800df344fSAndy Whitcroft $blk .= $rawlines[$line]; 1694a0df2efSAndy Whitcroft 170*653d4876SAndy Whitcroft @o = ($blk =~ /$open/g); 171*653d4876SAndy Whitcroft @c = ($blk =~ /$close/g); 1724a0df2efSAndy Whitcroft 1734a0df2efSAndy Whitcroft if (!$outer || (scalar(@o) - scalar(@c)) == 1) { 17400df344fSAndy Whitcroft push(@res, $rawlines[$line]); 1754a0df2efSAndy Whitcroft } 1764a0df2efSAndy Whitcroft 1774a0df2efSAndy Whitcroft last if (scalar(@o) == scalar(@c)); 1784a0df2efSAndy Whitcroft } 1794a0df2efSAndy Whitcroft 1804a0df2efSAndy Whitcroft return @res; 1814a0df2efSAndy Whitcroft} 1824a0df2efSAndy Whitcroftsub ctx_block_outer { 1834a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 1844a0df2efSAndy Whitcroft 185*653d4876SAndy Whitcroft return ctx_block_get($linenr, $remain, 1, '\{', '\}'); 1864a0df2efSAndy Whitcroft} 1874a0df2efSAndy Whitcroftsub ctx_block { 1884a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 1894a0df2efSAndy Whitcroft 190*653d4876SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '\{', '\}'); 191*653d4876SAndy Whitcroft} 192*653d4876SAndy Whitcroftsub ctx_statement { 193*653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 194*653d4876SAndy Whitcroft 195*653d4876SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '\(', '\)'); 1964a0df2efSAndy Whitcroft} 1974a0df2efSAndy Whitcroft 1984a0df2efSAndy Whitcroftsub ctx_locate_comment { 1994a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 2004a0df2efSAndy Whitcroft 2014a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 20200df344fSAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); 2034a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 2044a0df2efSAndy Whitcroft 2054a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 2064a0df2efSAndy Whitcroft # comment. 2074a0df2efSAndy Whitcroft my $in_comment = 0; 2084a0df2efSAndy Whitcroft $current_comment = ''; 2094a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 21000df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 21100df344fSAndy Whitcroft #warn " $line\n"; 2124a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 2134a0df2efSAndy Whitcroft $in_comment = 1; 2144a0df2efSAndy Whitcroft } 2154a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 2164a0df2efSAndy Whitcroft $in_comment = 1; 2174a0df2efSAndy Whitcroft } 2184a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 2194a0df2efSAndy Whitcroft $current_comment = ''; 2204a0df2efSAndy Whitcroft } 2214a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 2224a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 2234a0df2efSAndy Whitcroft $in_comment = 0; 2244a0df2efSAndy Whitcroft } 2254a0df2efSAndy Whitcroft } 2264a0df2efSAndy Whitcroft 2274a0df2efSAndy Whitcroft chomp($current_comment); 2284a0df2efSAndy Whitcroft return($current_comment); 2294a0df2efSAndy Whitcroft} 2304a0df2efSAndy Whitcroftsub ctx_has_comment { 2314a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 2324a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 2334a0df2efSAndy Whitcroft 23400df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 2354a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 2364a0df2efSAndy Whitcroft 2374a0df2efSAndy Whitcroft return ($cmt ne ''); 2384a0df2efSAndy Whitcroft} 2394a0df2efSAndy Whitcroft 2400a920b5bSAndy Whitcroftsub cat_vet { 2410a920b5bSAndy Whitcroft my ($vet) = @_; 2420a920b5bSAndy Whitcroft 2430a920b5bSAndy Whitcroft $vet =~ s/\t/^I/; 2440a920b5bSAndy Whitcroft $vet =~ s/$/\$/; 2450a920b5bSAndy Whitcroft 2460a920b5bSAndy Whitcroft return $vet; 2470a920b5bSAndy Whitcroft} 2480a920b5bSAndy Whitcroft 2490a920b5bSAndy Whitcroftsub process { 2500a920b5bSAndy Whitcroft my $filename = shift; 2510a920b5bSAndy Whitcroft my @lines = @_; 2520a920b5bSAndy Whitcroft 2530a920b5bSAndy Whitcroft my $linenr=0; 2540a920b5bSAndy Whitcroft my $prevline=""; 2550a920b5bSAndy Whitcroft my $stashline=""; 2560a920b5bSAndy Whitcroft 2574a0df2efSAndy Whitcroft my $length; 2580a920b5bSAndy Whitcroft my $indent; 2590a920b5bSAndy Whitcroft my $previndent=0; 2600a920b5bSAndy Whitcroft my $stashindent=0; 2610a920b5bSAndy Whitcroft 2620a920b5bSAndy Whitcroft my $clean = 1; 2630a920b5bSAndy Whitcroft my $signoff = 0; 2640a920b5bSAndy Whitcroft my $is_patch = 0; 2650a920b5bSAndy Whitcroft 2660a920b5bSAndy Whitcroft # Trace the real file/line as we go. 2670a920b5bSAndy Whitcroft my $realfile = ''; 2680a920b5bSAndy Whitcroft my $realline = 0; 2690a920b5bSAndy Whitcroft my $realcnt = 0; 2700a920b5bSAndy Whitcroft my $here = ''; 2710a920b5bSAndy Whitcroft my $in_comment = 0; 2720a920b5bSAndy Whitcroft my $first_line = 0; 2730a920b5bSAndy Whitcroft 274*653d4876SAndy Whitcroft my $ident = '[A-Za-z\d_]+'; 275*653d4876SAndy Whitcroft my $storage = '(?:extern|static)'; 276*653d4876SAndy Whitcroft my $sparse = '(?:__user|__kernel|__force|__iomem)'; 277*653d4876SAndy Whitcroft my $type = '(?:unsigned\s+)?' . 278*653d4876SAndy Whitcroft '(?:void|char|short|int|long|unsigned|float|double|' . 279*653d4876SAndy Whitcroft 'long\s+long|' . 280*653d4876SAndy Whitcroft "struct\\s+${ident}|" . 281*653d4876SAndy Whitcroft "union\\s+${ident}|" . 282*653d4876SAndy Whitcroft "${ident}_t)" . 283*653d4876SAndy Whitcroft "(?:\\s+$sparse)*" . 284*653d4876SAndy Whitcroft '(?:\s*\*+)?'; 285*653d4876SAndy Whitcroft my $attribute = '(?:__read_mostly|__init|__initdata)'; 286*653d4876SAndy Whitcroft 287*653d4876SAndy Whitcroft my $Ident = $ident; 288*653d4876SAndy Whitcroft my $Type = $type; 289*653d4876SAndy Whitcroft my $Storage = $storage; 290*653d4876SAndy Whitcroft my $Declare = "(?:$storage\\s+)?$type"; 291*653d4876SAndy Whitcroft my $Attribute = $attribute; 292*653d4876SAndy Whitcroft 2930a920b5bSAndy Whitcroft foreach my $line (@lines) { 2940a920b5bSAndy Whitcroft $linenr++; 2950a920b5bSAndy Whitcroft 296*653d4876SAndy Whitcroft my $rawline = $line; 297*653d4876SAndy Whitcroft 2980a920b5bSAndy Whitcroft#extract the filename as it passes 2990a920b5bSAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 3000a920b5bSAndy Whitcroft $realfile=$1; 30100df344fSAndy Whitcroft $realfile =~ s@^[^/]*/@@; 3020a920b5bSAndy Whitcroft $in_comment = 0; 3030a920b5bSAndy Whitcroft next; 3040a920b5bSAndy Whitcroft } 3050a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 3060a920b5bSAndy Whitcroft if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { 3070a920b5bSAndy Whitcroft $is_patch = 1; 3084a0df2efSAndy Whitcroft $first_line = $linenr + 1; 3090a920b5bSAndy Whitcroft $in_comment = 0; 3100a920b5bSAndy Whitcroft $realline=$1-1; 3110a920b5bSAndy Whitcroft if (defined $2) { 3120a920b5bSAndy Whitcroft $realcnt=$3+1; 3130a920b5bSAndy Whitcroft } else { 3140a920b5bSAndy Whitcroft $realcnt=1+1; 3150a920b5bSAndy Whitcroft } 3160a920b5bSAndy Whitcroft next; 3170a920b5bSAndy Whitcroft } 3180a920b5bSAndy Whitcroft 3194a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 3204a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 3214a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 3224a0df2efSAndy Whitcroft if ($line =~ /^( |\+|$)/) { 3230a920b5bSAndy Whitcroft $realline++; 3240a920b5bSAndy Whitcroft 3250a920b5bSAndy Whitcroft # track any sort of multi-line comment. Obviously if 3260a920b5bSAndy Whitcroft # the added text or context do not include the whole 3270a920b5bSAndy Whitcroft # comment we will not see it. Such is life. 3280a920b5bSAndy Whitcroft # 3290a920b5bSAndy Whitcroft # Guestimate if this is a continuing comment. If this 3300a920b5bSAndy Whitcroft # is the start of a diff block and this line starts 3310a920b5bSAndy Whitcroft # ' *' then it is very likely a comment. 3324a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 3330a920b5bSAndy Whitcroft $in_comment = 1; 3340a920b5bSAndy Whitcroft } 3350a920b5bSAndy Whitcroft if ($line =~ m@/\*@) { 3360a920b5bSAndy Whitcroft $in_comment = 1; 3370a920b5bSAndy Whitcroft } 3380a920b5bSAndy Whitcroft if ($line =~ m@\*/@) { 3390a920b5bSAndy Whitcroft $in_comment = 0; 3400a920b5bSAndy Whitcroft } 3410a920b5bSAndy Whitcroft 3424a0df2efSAndy Whitcroft # Measure the line length and indent. 3434a0df2efSAndy Whitcroft ($length, $indent) = line_stats($line); 3440a920b5bSAndy Whitcroft 3450a920b5bSAndy Whitcroft # Track the previous line. 3460a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 3470a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 3480a920b5bSAndy Whitcroft } 34900df344fSAndy Whitcroft $realcnt-- if ($realcnt != 0); 3500a920b5bSAndy Whitcroft 3510a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 352389834b6SRandy Dunlap $here = "#$linenr: "; 353389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 3540a920b5bSAndy Whitcroft 35500df344fSAndy Whitcroft my $hereline = "$here\n$line\n"; 3560a920b5bSAndy Whitcroft my $herecurr = "$here\n$line\n\n"; 3570a920b5bSAndy Whitcroft my $hereprev = "$here\n$prevline\n$line\n\n"; 3580a920b5bSAndy Whitcroft 3590a920b5bSAndy Whitcroft#check the patch for a signoff: 3600a920b5bSAndy Whitcroft if ($line =~ /^\s*Signed-off-by:\s/) { 3610a920b5bSAndy Whitcroft $signoff++; 3620a920b5bSAndy Whitcroft 3630a920b5bSAndy Whitcroft } elsif ($line =~ /^\s*signed-off-by:/i) { 3644a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 3654a0df2efSAndy Whitcroft $signoff++; 3660a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 3670a920b5bSAndy Whitcroft print "use Signed-off-by:\n"; 3680a920b5bSAndy Whitcroft print "$herecurr"; 3690a920b5bSAndy Whitcroft $clean = 0; 3700a920b5bSAndy Whitcroft } 3710a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 3720a920b5bSAndy Whitcroft print "need space after Signed-off-by:\n"; 3730a920b5bSAndy Whitcroft print "$herecurr"; 3740a920b5bSAndy Whitcroft $clean = 0; 3750a920b5bSAndy Whitcroft } 3760a920b5bSAndy Whitcroft } 3770a920b5bSAndy Whitcroft 37800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 37900df344fSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { 38000df344fSAndy Whitcroft print "patch seems to be corrupt (line wrapped?) [$realcnt]\n"; 38100df344fSAndy Whitcroft print "$herecurr"; 38200df344fSAndy Whitcroft $clean = 0; 38300df344fSAndy Whitcroft } 3840a920b5bSAndy Whitcroft 38500df344fSAndy Whitcroft#ignore lines being removed 38600df344fSAndy Whitcroft if ($line=~/^-/) {next;} 38700df344fSAndy Whitcroft 38800df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 38900df344fSAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 3900a920b5bSAndy Whitcroft 3910a920b5bSAndy Whitcroft#trailing whitespace 392*653d4876SAndy Whitcroft if ($line=~/^\+.*\S\s+$/) { 3930a920b5bSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 3940a920b5bSAndy Whitcroft print "trailing whitespace\n"; 3950a920b5bSAndy Whitcroft print "$herevet"; 3960a920b5bSAndy Whitcroft $clean = 0; 3970a920b5bSAndy Whitcroft } 3980a920b5bSAndy Whitcroft#80 column limit 39900df344fSAndy Whitcroft if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { 4000a920b5bSAndy Whitcroft print "line over 80 characters\n"; 4010a920b5bSAndy Whitcroft print "$herecurr"; 4020a920b5bSAndy Whitcroft $clean = 0; 4030a920b5bSAndy Whitcroft } 4040a920b5bSAndy Whitcroft 4050a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk 4060a920b5bSAndy Whitcroft next if ($realfile !~ /\.[hc]$/); 4070a920b5bSAndy Whitcroft 4080a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 4090a920b5bSAndy Whitcroft# more than 8 must use tabs. 4100a920b5bSAndy Whitcroft if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { 4110a920b5bSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 4120a920b5bSAndy Whitcroft print "use tabs not spaces\n"; 4130a920b5bSAndy Whitcroft print "$herevet"; 4140a920b5bSAndy Whitcroft $clean = 0; 4150a920b5bSAndy Whitcroft } 4160a920b5bSAndy Whitcroft 4170a920b5bSAndy Whitcroft # 4180a920b5bSAndy Whitcroft # The rest of our checks refer specifically to C style 4190a920b5bSAndy Whitcroft # only apply those _outside_ comments. 4200a920b5bSAndy Whitcroft # 4210a920b5bSAndy Whitcroft next if ($in_comment); 4220a920b5bSAndy Whitcroft 4230a920b5bSAndy Whitcroft# Remove comments from the line before processing. 4240a920b5bSAndy Whitcroft $line =~ s@/\*.*\*/@@g; 4250a920b5bSAndy Whitcroft $line =~ s@/\*.*@@; 4260a920b5bSAndy Whitcroft $line =~ s@.*\*/@@; 42700df344fSAndy Whitcroft 428*653d4876SAndy Whitcroft# Standardise the strings and chars within the input to simplify matching. 429*653d4876SAndy Whitcroft $line = sanitise_line($line); 430*653d4876SAndy Whitcroft 43100df344fSAndy Whitcroft# 43200df344fSAndy Whitcroft# Checks which may be anchored in the context. 43300df344fSAndy Whitcroft# 43400df344fSAndy Whitcroft 43500df344fSAndy Whitcroft# Check for switch () and associated case and default 43600df344fSAndy Whitcroft# statements should be at the same indent. 43700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 43800df344fSAndy Whitcroft my $err = ''; 43900df344fSAndy Whitcroft my $sep = ''; 44000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 44100df344fSAndy Whitcroft shift(@ctx); 44200df344fSAndy Whitcroft for my $ctx (@ctx) { 44300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 44400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 44500df344fSAndy Whitcroft $indent != $cindent) { 44600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 44700df344fSAndy Whitcroft $sep = ''; 44800df344fSAndy Whitcroft } else { 44900df344fSAndy Whitcroft $sep = "[...]\n"; 45000df344fSAndy Whitcroft } 45100df344fSAndy Whitcroft } 45200df344fSAndy Whitcroft if ($err ne '') { 45300df344fSAndy Whitcroft print "switch and case should be at the same indent\n"; 45400df344fSAndy Whitcroft print "$here\n$line\n$err\n"; 45500df344fSAndy Whitcroft $clean = 0; 45600df344fSAndy Whitcroft } 45700df344fSAndy Whitcroft } 45800df344fSAndy Whitcroft 45900df344fSAndy Whitcroft#ignore lines not being added 46000df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 46100df344fSAndy Whitcroft 462*653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 463*653d4876SAndy Whitcroft if ($tst_type && $line =~ /^.$Declare$/) { 464*653d4876SAndy Whitcroft print "TEST: is type $Declare\n"; 465*653d4876SAndy Whitcroft print "$herecurr"; 466*653d4876SAndy Whitcroft $clean = 0; 467*653d4876SAndy Whitcroft next; 468*653d4876SAndy Whitcroft } 469*653d4876SAndy Whitcroft 47000df344fSAndy Whitcroft# 47100df344fSAndy Whitcroft# Checks which are anchored on the added line. 47200df344fSAndy Whitcroft# 47300df344fSAndy Whitcroft 474*653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 475*653d4876SAndy Whitcroft if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 476*653d4876SAndy Whitcroft my $path = $1; 477*653d4876SAndy Whitcroft if ($path =~ m{//}) { 478*653d4876SAndy Whitcroft print "malformed #include filename\n"; 479*653d4876SAndy Whitcroft print "$herecurr"; 480*653d4876SAndy Whitcroft $clean = 0; 481*653d4876SAndy Whitcroft } 482*653d4876SAndy Whitcroft # Sanitise this special form of string. 483*653d4876SAndy Whitcroft $path = 'X' x length($path); 484*653d4876SAndy Whitcroft $line =~ s{\<.*\>}{<$path>}; 485*653d4876SAndy Whitcroft } 486*653d4876SAndy Whitcroft 48700df344fSAndy Whitcroft# no C99 // comments 48800df344fSAndy Whitcroft if ($line =~ m{//}) { 48900df344fSAndy Whitcroft print "do not use C99 // comments\n"; 49000df344fSAndy Whitcroft print "$herecurr"; 49100df344fSAndy Whitcroft $clean = 0; 49200df344fSAndy Whitcroft } 49300df344fSAndy Whitcroft # Remove C99 comments. 4940a920b5bSAndy Whitcroft $line =~ s@//.*@@; 4950a920b5bSAndy Whitcroft 4960a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 497*653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 498*653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 499*653d4876SAndy Whitcroft my $name = $1; 5000a920b5bSAndy Whitcroft if (($prevline !~ /^}/) && 5010a920b5bSAndy Whitcroft ($prevline !~ /^\+}/) && 502*653d4876SAndy Whitcroft ($prevline !~ /^ }/) && 503*653d4876SAndy Whitcroft ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { 504*653d4876SAndy Whitcroft print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n"; 5050a920b5bSAndy Whitcroft print "$herecurr"; 5060a920b5bSAndy Whitcroft $clean = 0; 5070a920b5bSAndy Whitcroft } 5080a920b5bSAndy Whitcroft } 5090a920b5bSAndy Whitcroft 5100a920b5bSAndy Whitcroft# check for static initialisers. 5110a920b5bSAndy Whitcroft if ($line=~/\s*static\s.*=\s+(0|NULL);/) { 5120a920b5bSAndy Whitcroft print "do not initialise statics to 0 or NULL\n"; 5130a920b5bSAndy Whitcroft print "$herecurr"; 5140a920b5bSAndy Whitcroft $clean = 0; 5150a920b5bSAndy Whitcroft } 5160a920b5bSAndy Whitcroft 517*653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 518*653d4876SAndy Whitcroft# make sense. 519*653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 520*653d4876SAndy Whitcroft $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ && 521*653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 5220a920b5bSAndy Whitcroft print "do not add new typedefs\n"; 5230a920b5bSAndy Whitcroft print "$herecurr"; 5240a920b5bSAndy Whitcroft $clean = 0; 5250a920b5bSAndy Whitcroft } 5260a920b5bSAndy Whitcroft 5270a920b5bSAndy Whitcroft# * goes on variable not on type 52800df344fSAndy Whitcroft if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { 52900df344fSAndy Whitcroft print "\"foo$1 bar\" should be \"foo $1bar\"\n"; 53000df344fSAndy Whitcroft print "$herecurr"; 53100df344fSAndy Whitcroft $clean = 0; 53200df344fSAndy Whitcroft } 533*653d4876SAndy Whitcroft if ($line =~ m{$Type (\*) [A-Za-z\d_]+} || 53400df344fSAndy Whitcroft $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { 53500df344fSAndy Whitcroft print "\"foo $1 bar\" should be \"foo $1bar\"\n"; 53600df344fSAndy Whitcroft print "$herecurr"; 53700df344fSAndy Whitcroft $clean = 0; 53800df344fSAndy Whitcroft } 53900df344fSAndy Whitcroft if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_](\*+)\)}) { 54000df344fSAndy Whitcroft print "\"(foo$1)\" should be \"(foo $1)\"\n"; 54100df344fSAndy Whitcroft print "$herecurr"; 54200df344fSAndy Whitcroft $clean = 0; 54300df344fSAndy Whitcroft } 54400df344fSAndy Whitcroft if ($line =~ m{\([A-Za-z\d_\s]+[A-Za-z\d_]\s+(\*+)\s+\)}) { 54500df344fSAndy Whitcroft print "\"(foo $1 )\" should be \"(foo $1)\"\n"; 5460a920b5bSAndy Whitcroft print "$herecurr"; 5470a920b5bSAndy Whitcroft $clean = 0; 5480a920b5bSAndy Whitcroft } 5490a920b5bSAndy Whitcroft 5500a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 5510a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 5520a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 5530a920b5bSAndy Whitcroft# print "$herecurr"; 5540a920b5bSAndy Whitcroft# $clean = 0; 5550a920b5bSAndy Whitcroft# } 5560a920b5bSAndy Whitcroft 55700df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 55800df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 55900df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 56000df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 56100df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 5620a920b5bSAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)/) { 56300df344fSAndy Whitcroft my $ok = 0; 56400df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 56500df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 56600df344fSAndy Whitcroft # we have a preceeding printk if it ends 56700df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 56800df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 56900df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 57000df344fSAndy Whitcroft $ok = 1; 57100df344fSAndy Whitcroft } 57200df344fSAndy Whitcroft last; 57300df344fSAndy Whitcroft } 57400df344fSAndy Whitcroft } 57500df344fSAndy Whitcroft if ($ok == 0) { 5760a920b5bSAndy Whitcroft print "printk() should include KERN_ facility level\n"; 5770a920b5bSAndy Whitcroft print "$herecurr"; 5780a920b5bSAndy Whitcroft $clean = 0; 5790a920b5bSAndy Whitcroft } 58000df344fSAndy Whitcroft } 5810a920b5bSAndy Whitcroft 582*653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 583*653d4876SAndy Whitcroft# or if closed on same line 5840a920b5bSAndy Whitcroft if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and 5850a920b5bSAndy Whitcroft !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 5860a920b5bSAndy Whitcroft print "braces following function declarations go on the next line\n"; 5870a920b5bSAndy Whitcroft print "$herecurr"; 5880a920b5bSAndy Whitcroft $clean = 0; 5890a920b5bSAndy Whitcroft } 590*653d4876SAndy Whitcroft 591*653d4876SAndy Whitcroft# Check operator spacing. 5924a0df2efSAndy Whitcroft # Note we expand the line with the leading + as the real 5934a0df2efSAndy Whitcroft # line will be displayed with the leading + and the tabs 5944a0df2efSAndy Whitcroft # will therefore also expand that way. 5950a920b5bSAndy Whitcroft my $opline = $line; 5964a0df2efSAndy Whitcroft $opline = expand_tabs($opline); 5970a920b5bSAndy Whitcroft $opline =~ s/^./ /; 5980a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 5990a920b5bSAndy Whitcroft my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); 60000df344fSAndy Whitcroft my $off = 0; 6010a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 6024a0df2efSAndy Whitcroft $off += length($elements[$n]); 6034a0df2efSAndy Whitcroft 6044a0df2efSAndy Whitcroft my $a = ''; 6054a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 6064a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 6074a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 6084a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 6094a0df2efSAndy Whitcroft $a = 'E' if ($elements[$n] eq '' && $n == 0); 6104a0df2efSAndy Whitcroft 6110a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 6124a0df2efSAndy Whitcroft 6134a0df2efSAndy Whitcroft my $c = ''; 6140a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 6154a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 6164a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 6174a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 6184a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 6194a0df2efSAndy Whitcroft } else { 6204a0df2efSAndy Whitcroft $c = 'E'; 6210a920b5bSAndy Whitcroft } 6220a920b5bSAndy Whitcroft 62300df344fSAndy Whitcroft # Pick up the preceeding and succeeding characters. 62400df344fSAndy Whitcroft my $ca = substr($opline, $off - 1, 1); 62500df344fSAndy Whitcroft my $cc = ''; 626*653d4876SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 627*653d4876SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1]), 1); 62800df344fSAndy Whitcroft } 62900df344fSAndy Whitcroft 6304a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 6314a0df2efSAndy Whitcroft 6324a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 6334a0df2efSAndy Whitcroft 6344a0df2efSAndy Whitcroft my $ptr = (" " x $off) . "^"; 63500df344fSAndy Whitcroft my $hereptr = "$hereline$ptr\n\n"; 6360a920b5bSAndy Whitcroft 6370a920b5bSAndy Whitcroft ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; 6380a920b5bSAndy Whitcroft 6390a920b5bSAndy Whitcroft # We need ; as an operator. // is a comment. 6400a920b5bSAndy Whitcroft if ($op eq ';' or $op eq '//') { 6410a920b5bSAndy Whitcroft 6420a920b5bSAndy Whitcroft # -> should have no spaces 6430a920b5bSAndy Whitcroft } elsif ($op eq '->') { 6444a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 6450a920b5bSAndy Whitcroft print "no spaces around that '$op' $at\n"; 6464a0df2efSAndy Whitcroft print "$hereptr"; 6470a920b5bSAndy Whitcroft $clean = 0; 6480a920b5bSAndy Whitcroft } 6490a920b5bSAndy Whitcroft 6500a920b5bSAndy Whitcroft # , must have a space on the right. 6510a920b5bSAndy Whitcroft } elsif ($op eq ',') { 652*653d4876SAndy Whitcroft if ($ctx !~ /.xW|.xE/ && $cc ne '}') { 6530a920b5bSAndy Whitcroft print "need space after that '$op' $at\n"; 6544a0df2efSAndy Whitcroft print "$hereptr"; 6550a920b5bSAndy Whitcroft $clean = 0; 6560a920b5bSAndy Whitcroft } 6570a920b5bSAndy Whitcroft 6580a920b5bSAndy Whitcroft # unary ! and unary ~ are allowed no space on the right 6590a920b5bSAndy Whitcroft } elsif ($op eq '!' or $op eq '~') { 6604a0df2efSAndy Whitcroft if ($ctx !~ /[WOEB]x./) { 6610a920b5bSAndy Whitcroft print "need space before that '$op' $at\n"; 6624a0df2efSAndy Whitcroft print "$hereptr"; 6630a920b5bSAndy Whitcroft $clean = 0; 6640a920b5bSAndy Whitcroft } 6654a0df2efSAndy Whitcroft if ($ctx =~ /.xW/) { 6660a920b5bSAndy Whitcroft print "no space after that '$op' $at\n"; 6674a0df2efSAndy Whitcroft print "$hereptr"; 6680a920b5bSAndy Whitcroft $clean = 0; 6690a920b5bSAndy Whitcroft } 6700a920b5bSAndy Whitcroft 6710a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 6720a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 673*653d4876SAndy Whitcroft if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) { 6740a920b5bSAndy Whitcroft print "need space one side of that '$op' $at\n"; 6754a0df2efSAndy Whitcroft print "$hereptr"; 6760a920b5bSAndy Whitcroft $clean = 0; 6770a920b5bSAndy Whitcroft } 678*653d4876SAndy Whitcroft if ($ctx =~ /Wx./ && $cc eq ';') { 679*653d4876SAndy Whitcroft print "no space before that '$op' $at\n"; 680*653d4876SAndy Whitcroft print "$hereptr"; 681*653d4876SAndy Whitcroft $clean = 0; 682*653d4876SAndy Whitcroft } 6830a920b5bSAndy Whitcroft 6840a920b5bSAndy Whitcroft # & is both unary and binary 6850a920b5bSAndy Whitcroft # unary: 6860a920b5bSAndy Whitcroft # a &b 6870a920b5bSAndy Whitcroft # binary (consistent spacing): 6880a920b5bSAndy Whitcroft # a&b OK 6890a920b5bSAndy Whitcroft # a & b OK 6900a920b5bSAndy Whitcroft # 6910a920b5bSAndy Whitcroft # boiling down to: if there is a space on the right then there 6920a920b5bSAndy Whitcroft # should be one on the left. 6930a920b5bSAndy Whitcroft # 6940a920b5bSAndy Whitcroft # - is the same 6950a920b5bSAndy Whitcroft # 6964a0df2efSAndy Whitcroft } elsif ($op eq '&' or $op eq '-') { 69700df344fSAndy Whitcroft if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) { 6980a920b5bSAndy Whitcroft print "need space before that '$op' $at\n"; 6994a0df2efSAndy Whitcroft print "$hereptr"; 7004a0df2efSAndy Whitcroft $clean = 0; 7014a0df2efSAndy Whitcroft } 7024a0df2efSAndy Whitcroft 70300df344fSAndy Whitcroft # * is the same as & only adding: 70400df344fSAndy Whitcroft # type: 70500df344fSAndy Whitcroft # (foo *) 70600df344fSAndy Whitcroft # (foo **) 70700df344fSAndy Whitcroft # 7084a0df2efSAndy Whitcroft } elsif ($op eq '*') { 70900df344fSAndy Whitcroft if ($ca eq '*') { 71000df344fSAndy Whitcroft if ($cc =~ /\s/) { 71100df344fSAndy Whitcroft print "no space after that '$op' $at\n"; 71200df344fSAndy Whitcroft print "$hereptr"; 71300df344fSAndy Whitcroft $clean = 0; 71400df344fSAndy Whitcroft } 715*653d4876SAndy Whitcroft } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) { 7164a0df2efSAndy Whitcroft print "need space before that '$op' $at\n"; 7174a0df2efSAndy Whitcroft print "$hereptr"; 7180a920b5bSAndy Whitcroft $clean = 0; 7190a920b5bSAndy Whitcroft } 7200a920b5bSAndy Whitcroft 7210a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 7220a920b5bSAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or 7230a920b5bSAndy Whitcroft $op eq '^' or $op eq '|') 7240a920b5bSAndy Whitcroft { 7254a0df2efSAndy Whitcroft if ($ctx !~ /VxV|WxW|VxE|WxE/) { 7260a920b5bSAndy Whitcroft print "need consistent spacing around '$op' $at\n"; 7274a0df2efSAndy Whitcroft print "$hereptr"; 7280a920b5bSAndy Whitcroft $clean = 0; 7290a920b5bSAndy Whitcroft } 7300a920b5bSAndy Whitcroft 7310a920b5bSAndy Whitcroft # All the others need spaces both sides. 7324a0df2efSAndy Whitcroft } elsif ($ctx !~ /[EW]x[WE]/) { 7330a920b5bSAndy Whitcroft print "need spaces around that '$op' $at\n"; 7344a0df2efSAndy Whitcroft print "$hereptr"; 7350a920b5bSAndy Whitcroft $clean = 0; 7360a920b5bSAndy Whitcroft } 7374a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 7380a920b5bSAndy Whitcroft } 7390a920b5bSAndy Whitcroft } 7400a920b5bSAndy Whitcroft 7410a920b5bSAndy Whitcroft#need space before brace following if, while, etc 7420a920b5bSAndy Whitcroft if ($line=~/\(.*\){/) { 7430a920b5bSAndy Whitcroft print "need a space before the brace\n"; 7440a920b5bSAndy Whitcroft print "$herecurr"; 7450a920b5bSAndy Whitcroft $clean = 0; 7460a920b5bSAndy Whitcroft } 7470a920b5bSAndy Whitcroft 7480a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 7494a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 7500a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 7510a920b5bSAndy Whitcroft print "labels should not be indented\n"; 7520a920b5bSAndy Whitcroft print "$herecurr"; 7530a920b5bSAndy Whitcroft $clean = 0; 7540a920b5bSAndy Whitcroft } 7550a920b5bSAndy Whitcroft 7560a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 7574a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 7580a920b5bSAndy Whitcroft print "need a space before the open parenthesis\n"; 7590a920b5bSAndy Whitcroft print "$herecurr"; 7600a920b5bSAndy Whitcroft $clean = 0; 7610a920b5bSAndy Whitcroft } 7620a920b5bSAndy Whitcroft 7630a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional. 764*653d4876SAndy Whitcroft if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { 76500df344fSAndy Whitcroft #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 766*653d4876SAndy Whitcroft print "do not use assignment in if condition\n"; 7670a920b5bSAndy Whitcroft print "$herecurr"; 7680a920b5bSAndy Whitcroft $clean = 0; 7690a920b5bSAndy Whitcroft } 7700a920b5bSAndy Whitcroft 7710a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 7720a920b5bSAndy Whitcroft # indent level to be relevant to each other. 7730a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 7740a920b5bSAndy Whitcroft $previndent == $indent) { 7750a920b5bSAndy Whitcroft print "else should follow close brace\n"; 7760a920b5bSAndy Whitcroft print "$hereprev"; 7770a920b5bSAndy Whitcroft $clean = 0; 7780a920b5bSAndy Whitcroft } 7790a920b5bSAndy Whitcroft 7800a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 7810a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 7820a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 7830a920b5bSAndy Whitcroft# print "$herecurr"; 7840a920b5bSAndy Whitcroft# $clean = 0; 7850a920b5bSAndy Whitcroft# } 7860a920b5bSAndy Whitcroft 7870a920b5bSAndy Whitcroft#no spaces allowed after \ in define 7880a920b5bSAndy Whitcroft if ($line=~/\#define.*\\\s$/) { 7890a920b5bSAndy Whitcroft print("Whitepspace after \\ makes next lines useless\n"); 7900a920b5bSAndy Whitcroft print "$herecurr"; 7910a920b5bSAndy Whitcroft $clean = 0; 7920a920b5bSAndy Whitcroft } 7930a920b5bSAndy Whitcroft 794*653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 795*653d4876SAndy Whitcroft if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 7960a920b5bSAndy Whitcroft my $checkfile = "include/linux/$1.h"; 7970a920b5bSAndy Whitcroft if (-f $checkfile) { 7980a920b5bSAndy Whitcroft print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; 7990a920b5bSAndy Whitcroft print $herecurr; 8000a920b5bSAndy Whitcroft $clean = 0; 8010a920b5bSAndy Whitcroft } 8020a920b5bSAndy Whitcroft } 8030a920b5bSAndy Whitcroft 804*653d4876SAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 805*653d4876SAndy Whitcroft# or if that brace on the next line is for something else 8064a0df2efSAndy Whitcroft if ($prevline=~/\b(if|while|for|switch)\s*\(/) { 8070a920b5bSAndy Whitcroft my @opened = $prevline=~/\(/g; 8080a920b5bSAndy Whitcroft my @closed = $prevline=~/\)/g; 8090a920b5bSAndy Whitcroft my $nr_line = $linenr; 81000df344fSAndy Whitcroft my $remaining = $realcnt - 1; 8110a920b5bSAndy Whitcroft my $next_line = $line; 8120a920b5bSAndy Whitcroft my $extra_lines = 0; 8130a920b5bSAndy Whitcroft my $display_segment = $prevline; 8140a920b5bSAndy Whitcroft 81500df344fSAndy Whitcroft while ($remaining > 0 && scalar @opened > scalar @closed) { 8160a920b5bSAndy Whitcroft $prevline .= $next_line; 8170a920b5bSAndy Whitcroft $display_segment .= "\n" . $next_line; 8180a920b5bSAndy Whitcroft $next_line = $lines[$nr_line]; 8190a920b5bSAndy Whitcroft $nr_line++; 8200a920b5bSAndy Whitcroft $remaining--; 8210a920b5bSAndy Whitcroft 8220a920b5bSAndy Whitcroft @opened = $prevline=~/\(/g; 8230a920b5bSAndy Whitcroft @closed = $prevline=~/\)/g; 8240a920b5bSAndy Whitcroft } 8250a920b5bSAndy Whitcroft 8264a0df2efSAndy Whitcroft if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and 827*653d4876SAndy Whitcroft !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) { 8280a920b5bSAndy Whitcroft print "That { should be on the previous line\n"; 8294a0df2efSAndy Whitcroft print "$here\n$display_segment\n$next_line\n\n"; 8300a920b5bSAndy Whitcroft $clean = 0; 8310a920b5bSAndy Whitcroft } 8320a920b5bSAndy Whitcroft } 8330a920b5bSAndy Whitcroft 834*653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 835*653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 836*653d4876SAndy Whitcroft# in a known goot container 837*653d4876SAndy Whitcroft if (($prevline=~/\#define.*\\/) and 838*653d4876SAndy Whitcroft !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and 839*653d4876SAndy Whitcroft !($line=~/do.*{/) and !($line=~/\(\{/) and 840*653d4876SAndy Whitcroft !($line=~/^.\s*$Declare\s/)) { 841*653d4876SAndy Whitcroft # Grab the first statement, if that is the entire macro 842*653d4876SAndy Whitcroft # its ok. This may start either on the #define line 843*653d4876SAndy Whitcroft # or the one below. 844*653d4876SAndy Whitcroft my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1)); 845*653d4876SAndy Whitcroft my $ctx2 = join('', ctx_statement($linenr, $realcnt)); 846*653d4876SAndy Whitcroft 847*653d4876SAndy Whitcroft if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) { 8480a920b5bSAndy Whitcroft print "Macros with multiple statements should be enclosed in a do - while loop\n"; 8490a920b5bSAndy Whitcroft print "$hereprev"; 8500a920b5bSAndy Whitcroft $clean = 0; 8510a920b5bSAndy Whitcroft } 852*653d4876SAndy Whitcroft } 8530a920b5bSAndy Whitcroft 854*653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 8554a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 856*653d4876SAndy Whitcroft if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 8570a920b5bSAndy Whitcroft print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; 8580a920b5bSAndy Whitcroft print "$herecurr"; 8590a920b5bSAndy Whitcroft $clean = 0; 8600a920b5bSAndy Whitcroft } 8610a920b5bSAndy Whitcroft } 8620a920b5bSAndy Whitcroft 8634a0df2efSAndy Whitcroft# don't use deprecated functions 8644a0df2efSAndy Whitcroft for my $func (@dep_functions) { 86500df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 8664a0df2efSAndy Whitcroft print "Don't use $func(): see Documentation/feature-removal-schedule.txt\n"; 8674a0df2efSAndy Whitcroft print "$herecurr"; 8684a0df2efSAndy Whitcroft $clean = 0; 8694a0df2efSAndy Whitcroft } 8704a0df2efSAndy Whitcroft } 8714a0df2efSAndy Whitcroft 8724a0df2efSAndy Whitcroft# no volatiles please 87300df344fSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { 8744a0df2efSAndy Whitcroft print "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n"; 8754a0df2efSAndy Whitcroft print "$herecurr"; 8764a0df2efSAndy Whitcroft $clean = 0; 8774a0df2efSAndy Whitcroft } 8784a0df2efSAndy Whitcroft 87900df344fSAndy Whitcroft# warn about #if 0 88000df344fSAndy Whitcroft if ($line =~ /^.#\s*if\s+0\b/) { 88100df344fSAndy Whitcroft print "#if 0 -- if this code redundant remove it\n"; 8824a0df2efSAndy Whitcroft print "$herecurr"; 8834a0df2efSAndy Whitcroft $clean = 0; 8844a0df2efSAndy Whitcroft } 8854a0df2efSAndy Whitcroft 88600df344fSAndy Whitcroft# warn about #ifdefs in C files 88700df344fSAndy Whitcroft# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 88800df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 88900df344fSAndy Whitcroft# print "$herecurr"; 89000df344fSAndy Whitcroft# $clean = 0; 89100df344fSAndy Whitcroft# } 89200df344fSAndy Whitcroft 8934a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 8944a0df2efSAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { 8954a0df2efSAndy Whitcroft my $which = $1; 8964a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 8974a0df2efSAndy Whitcroft print "$1 definition without comment\n"; 8984a0df2efSAndy Whitcroft print "$herecurr"; 8994a0df2efSAndy Whitcroft $clean = 0; 9004a0df2efSAndy Whitcroft } 9014a0df2efSAndy Whitcroft } 9024a0df2efSAndy Whitcroft# check for memory barriers without a comment. 9034a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 9044a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 9054a0df2efSAndy Whitcroft print "memory barrier without comment\n"; 9064a0df2efSAndy Whitcroft print "$herecurr"; 9074a0df2efSAndy Whitcroft $clean = 0; 9084a0df2efSAndy Whitcroft } 9094a0df2efSAndy Whitcroft } 9104a0df2efSAndy Whitcroft# check of hardware specific defines 9114a0df2efSAndy Whitcroft if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { 9124a0df2efSAndy Whitcroft print "architecture specific defines should be avoided\n"; 9130a920b5bSAndy Whitcroft print "$herecurr"; 9140a920b5bSAndy Whitcroft $clean = 0; 9150a920b5bSAndy Whitcroft } 916*653d4876SAndy Whitcroft 917*653d4876SAndy Whitcroft if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || 918*653d4876SAndy Whitcroft $line =~ /\b(?:inline|always_inline)\s+$Storage/) { 919*653d4876SAndy Whitcroft print "inline keyword should sit between storage class and type\n"; 920*653d4876SAndy Whitcroft print "$herecurr"; 921*653d4876SAndy Whitcroft $clean = 0; 922*653d4876SAndy Whitcroft } 9230a920b5bSAndy Whitcroft } 9240a920b5bSAndy Whitcroft 9250a920b5bSAndy Whitcroft if ($chk_patch && !$is_patch) { 9260a920b5bSAndy Whitcroft $clean = 0; 9270a920b5bSAndy Whitcroft print "Does not appear to be a unified-diff format patch\n"; 9280a920b5bSAndy Whitcroft } 9290a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 9300a920b5bSAndy Whitcroft $clean = 0; 9310a920b5bSAndy Whitcroft print "Missing Signed-off-by: line(s)\n"; 9320a920b5bSAndy Whitcroft } 9330a920b5bSAndy Whitcroft 9340a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 9350a920b5bSAndy Whitcroft print "Your patch has no obvious style problems and is ready for submission.\n" 9360a920b5bSAndy Whitcroft } 9370a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 9380a920b5bSAndy Whitcroft print "Your patch has style problems, please review. If any of these errors\n"; 9390a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 9400a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 9410a920b5bSAndy Whitcroft } 9420a920b5bSAndy Whitcroft return $clean; 9430a920b5bSAndy Whitcroft} 944