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*6c72ffaaSAndy Whitcroftmy $V = '0.11'; 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; 20653d4876SAndy Whitcroftmy $tst_type = 0; 21*6c72ffaaSAndy Whitcroftmy $emacs = 0; 22*6c72ffaaSAndy Whitcroftmy $file = 0; 23*6c72ffaaSAndy Whitcroftmy $check = 0; 24*6c72ffaaSAndy Whitcroftmy $root; 250a920b5bSAndy WhitcroftGetOptions( 26*6c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 270a920b5bSAndy Whitcroft 'tree!' => \$tree, 280a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 290a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 30653d4876SAndy Whitcroft 'test-type!' => \$tst_type, 31*6c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 32*6c72ffaaSAndy Whitcroft 'file!' => \$file, 33*6c72ffaaSAndy Whitcroft 'subjective!' => \$check, 34*6c72ffaaSAndy Whitcroft 'strict!' => \$check, 35*6c72ffaaSAndy Whitcroft 'root=s' => \$root, 360a920b5bSAndy Whitcroft) or exit; 370a920b5bSAndy Whitcroft 380a920b5bSAndy Whitcroftmy $exit = 0; 390a920b5bSAndy Whitcroft 400a920b5bSAndy Whitcroftif ($#ARGV < 0) { 4100df344fSAndy Whitcroft print "usage: $P [options] patchfile\n"; 420a920b5bSAndy Whitcroft print "version: $V\n"; 430a920b5bSAndy Whitcroft print "options: -q => quiet\n"; 440a920b5bSAndy Whitcroft print " --no-tree => run without a kernel tree\n"; 45*6c72ffaaSAndy Whitcroft print " --emacs => emacs compile window format\n"; 46*6c72ffaaSAndy Whitcroft print " --file => check a source file\n"; 47*6c72ffaaSAndy Whitcroft print " --strict => enable more subjective tests\n"; 48*6c72ffaaSAndy Whitcroft print " --root => path to the kernel tree root\n"; 490a920b5bSAndy Whitcroft exit(1); 500a920b5bSAndy Whitcroft} 510a920b5bSAndy Whitcroft 52*6c72ffaaSAndy Whitcroftif ($tree) { 53*6c72ffaaSAndy Whitcroft if (defined $root) { 54*6c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 55*6c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 56*6c72ffaaSAndy Whitcroft } 57*6c72ffaaSAndy Whitcroft } else { 58*6c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 59*6c72ffaaSAndy Whitcroft $root = '.'; 60*6c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 61*6c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 62*6c72ffaaSAndy Whitcroft $root = $1; 63*6c72ffaaSAndy Whitcroft } 64*6c72ffaaSAndy Whitcroft } 65*6c72ffaaSAndy Whitcroft 66*6c72ffaaSAndy Whitcroft if (!defined $root) { 670a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 680a920b5bSAndy Whitcroft exit(2); 690a920b5bSAndy Whitcroft } 70*6c72ffaaSAndy Whitcroft} 71*6c72ffaaSAndy Whitcroft 72*6c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 73*6c72ffaaSAndy Whitcroft 74*6c72ffaaSAndy Whitcroftour $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; 75*6c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 76*6c72ffaaSAndy Whitcroftour $Sparse = qr{ 77*6c72ffaaSAndy Whitcroft __user| 78*6c72ffaaSAndy Whitcroft __kernel| 79*6c72ffaaSAndy Whitcroft __force| 80*6c72ffaaSAndy Whitcroft __iomem| 81*6c72ffaaSAndy Whitcroft __must_check| 82*6c72ffaaSAndy Whitcroft __init_refok| 83*6c72ffaaSAndy Whitcroft __kprobes| 84*6c72ffaaSAndy Whitcroft fastcall 85*6c72ffaaSAndy Whitcroft }x; 86*6c72ffaaSAndy Whitcroftour $Attribute = qr{ 87*6c72ffaaSAndy Whitcroft const| 88*6c72ffaaSAndy Whitcroft __read_mostly| 89*6c72ffaaSAndy Whitcroft __kprobes| 90*6c72ffaaSAndy Whitcroft __(?:mem|cpu|dev|)(?:initdata|init) 91*6c72ffaaSAndy Whitcroft }x; 92*6c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 93*6c72ffaaSAndy Whitcroftour $NonptrType = qr{ 94*6c72ffaaSAndy Whitcroft \b 95*6c72ffaaSAndy Whitcroft (?:const\s+)? 96*6c72ffaaSAndy Whitcroft (?:unsigned\s+)? 97*6c72ffaaSAndy Whitcroft (?: 98*6c72ffaaSAndy Whitcroft void| 99*6c72ffaaSAndy Whitcroft char| 100*6c72ffaaSAndy Whitcroft short| 101*6c72ffaaSAndy Whitcroft int| 102*6c72ffaaSAndy Whitcroft long| 103*6c72ffaaSAndy Whitcroft unsigned| 104*6c72ffaaSAndy Whitcroft float| 105*6c72ffaaSAndy Whitcroft double| 106*6c72ffaaSAndy Whitcroft bool| 107*6c72ffaaSAndy Whitcroft long\s+int| 108*6c72ffaaSAndy Whitcroft long\s+long| 109*6c72ffaaSAndy Whitcroft long\s+long\s+int| 110*6c72ffaaSAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 111*6c72ffaaSAndy Whitcroft struct\s+$Ident| 112*6c72ffaaSAndy Whitcroft union\s+$Ident| 113*6c72ffaaSAndy Whitcroft enum\s+$Ident| 114*6c72ffaaSAndy Whitcroft ${Ident}_t| 115*6c72ffaaSAndy Whitcroft ${Ident}_handler| 116*6c72ffaaSAndy Whitcroft ${Ident}_handler_fn 117*6c72ffaaSAndy Whitcroft ) 118*6c72ffaaSAndy Whitcroft (?:\s+$Sparse)* 119*6c72ffaaSAndy Whitcroft \b 120*6c72ffaaSAndy Whitcroft }x; 121*6c72ffaaSAndy Whitcroft 122*6c72ffaaSAndy Whitcroftour $Type = qr{ 123*6c72ffaaSAndy Whitcroft \b$NonptrType\b 124*6c72ffaaSAndy Whitcroft (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? 125*6c72ffaaSAndy Whitcroft (?:\s+$Sparse|\s+$Attribute)* 126*6c72ffaaSAndy Whitcroft }x; 127*6c72ffaaSAndy Whitcroftour $Declare = qr{(?:$Storage\s+)?$Type}; 128*6c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 129*6c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 130*6c72ffaaSAndy Whitcroft 131*6c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 132*6c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 133*6c72ffaaSAndy Whitcroftour $Operators = qr{ 134*6c72ffaaSAndy Whitcroft <=|>=|==|!=| 135*6c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 136*6c72ffaaSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ 137*6c72ffaaSAndy Whitcroft }x; 138*6c72ffaaSAndy Whitcroft 139*6c72ffaaSAndy Whitcroftour $Bare = ''; 140*6c72ffaaSAndy Whitcroft 141*6c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 1420a920b5bSAndy Whitcroft 1434a0df2efSAndy Whitcroftmy @dep_includes = (); 1444a0df2efSAndy Whitcroftmy @dep_functions = (); 145*6c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 146*6c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 147*6c72ffaaSAndy Whitcroft open(REMOVE, "<$root/$removal") || 148*6c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 1490a920b5bSAndy Whitcroft while (<REMOVE>) { 150f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 151f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 152f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 1534a0df2efSAndy Whitcroft push(@dep_includes, $1); 1544a0df2efSAndy Whitcroft 155f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 156f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 157f0a594c1SAndy Whitcroft } 1584a0df2efSAndy Whitcroft } 1590a920b5bSAndy Whitcroft } 1600a920b5bSAndy Whitcroft } 1610a920b5bSAndy Whitcroft} 1620a920b5bSAndy Whitcroft 16300df344fSAndy Whitcroftmy @rawlines = (); 164*6c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 165*6c72ffaaSAndy Whitcroft if ($file) { 166*6c72ffaaSAndy Whitcroft open(FILE, "diff -u /dev/null $filename|") || 167*6c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 168*6c72ffaaSAndy Whitcroft } else { 169*6c72ffaaSAndy Whitcroft open(FILE, "<$filename") || 170*6c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 171*6c72ffaaSAndy Whitcroft } 172*6c72ffaaSAndy Whitcroft while (<FILE>) { 1730a920b5bSAndy Whitcroft chomp; 17400df344fSAndy Whitcroft push(@rawlines, $_); 175*6c72ffaaSAndy Whitcroft } 176*6c72ffaaSAndy Whitcroft close(FILE); 177*6c72ffaaSAndy Whitcroft if (!process($filename, @rawlines)) { 1780a920b5bSAndy Whitcroft $exit = 1; 1790a920b5bSAndy Whitcroft } 18000df344fSAndy Whitcroft @rawlines = (); 1810a920b5bSAndy Whitcroft} 1820a920b5bSAndy Whitcroft 1830a920b5bSAndy Whitcroftexit($exit); 1840a920b5bSAndy Whitcroft 1850a920b5bSAndy Whitcroftsub top_of_kernel_tree { 186*6c72ffaaSAndy Whitcroft my ($root) = @_; 187*6c72ffaaSAndy Whitcroft 188*6c72ffaaSAndy Whitcroft my @tree_check = ( 189*6c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 190*6c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 191*6c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 192*6c72ffaaSAndy Whitcroft ); 193*6c72ffaaSAndy Whitcroft 194*6c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 195*6c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 1960a920b5bSAndy Whitcroft return 0; 1970a920b5bSAndy Whitcroft } 198*6c72ffaaSAndy Whitcroft } 199*6c72ffaaSAndy Whitcroft return 1; 200*6c72ffaaSAndy Whitcroft} 2010a920b5bSAndy Whitcroft 2020a920b5bSAndy Whitcroftsub expand_tabs { 2030a920b5bSAndy Whitcroft my ($str) = @_; 2040a920b5bSAndy Whitcroft 2050a920b5bSAndy Whitcroft my $res = ''; 2060a920b5bSAndy Whitcroft my $n = 0; 2070a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 2080a920b5bSAndy Whitcroft if ($c eq "\t") { 2090a920b5bSAndy Whitcroft $res .= ' '; 2100a920b5bSAndy Whitcroft $n++; 2110a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 2120a920b5bSAndy Whitcroft $res .= ' '; 2130a920b5bSAndy Whitcroft } 2140a920b5bSAndy Whitcroft next; 2150a920b5bSAndy Whitcroft } 2160a920b5bSAndy Whitcroft $res .= $c; 2170a920b5bSAndy Whitcroft $n++; 2180a920b5bSAndy Whitcroft } 2190a920b5bSAndy Whitcroft 2200a920b5bSAndy Whitcroft return $res; 2210a920b5bSAndy Whitcroft} 222*6c72ffaaSAndy Whitcroftsub copy_spacing { 223*6c72ffaaSAndy Whitcroft my ($str) = @_; 224*6c72ffaaSAndy Whitcroft 225*6c72ffaaSAndy Whitcroft my $res = ''; 226*6c72ffaaSAndy Whitcroft for my $c (split(//, $str)) { 227*6c72ffaaSAndy Whitcroft if ($c eq "\t") { 228*6c72ffaaSAndy Whitcroft $res .= $c; 229*6c72ffaaSAndy Whitcroft } else { 230*6c72ffaaSAndy Whitcroft $res .= ' '; 231*6c72ffaaSAndy Whitcroft } 232*6c72ffaaSAndy Whitcroft } 233*6c72ffaaSAndy Whitcroft 234*6c72ffaaSAndy Whitcroft return $res; 235*6c72ffaaSAndy Whitcroft} 2360a920b5bSAndy Whitcroft 2374a0df2efSAndy Whitcroftsub line_stats { 2384a0df2efSAndy Whitcroft my ($line) = @_; 2394a0df2efSAndy Whitcroft 2404a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 2414a0df2efSAndy Whitcroft $line =~ s/^.//; 2424a0df2efSAndy Whitcroft $line = expand_tabs($line); 2434a0df2efSAndy Whitcroft 2444a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 2454a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 2464a0df2efSAndy Whitcroft 2474a0df2efSAndy Whitcroft return (length($line), length($white)); 2484a0df2efSAndy Whitcroft} 2494a0df2efSAndy Whitcroft 25000df344fSAndy Whitcroftsub sanitise_line { 25100df344fSAndy Whitcroft my ($line) = @_; 25200df344fSAndy Whitcroft 25300df344fSAndy Whitcroft my $res = ''; 25400df344fSAndy Whitcroft my $l = ''; 25500df344fSAndy Whitcroft 25600df344fSAndy Whitcroft my $quote = ''; 25700df344fSAndy Whitcroft 25800df344fSAndy Whitcroft foreach my $c (split(//, $line)) { 25900df344fSAndy Whitcroft if ($l ne "\\" && ($c eq "'" || $c eq '"')) { 26000df344fSAndy Whitcroft if ($quote eq '') { 26100df344fSAndy Whitcroft $quote = $c; 26200df344fSAndy Whitcroft $res .= $c; 26300df344fSAndy Whitcroft $l = $c; 26400df344fSAndy Whitcroft next; 26500df344fSAndy Whitcroft } elsif ($quote eq $c) { 26600df344fSAndy Whitcroft $quote = ''; 26700df344fSAndy Whitcroft } 26800df344fSAndy Whitcroft } 26900df344fSAndy Whitcroft if ($quote && $c ne "\t") { 27000df344fSAndy Whitcroft $res .= "X"; 27100df344fSAndy Whitcroft } else { 27200df344fSAndy Whitcroft $res .= $c; 27300df344fSAndy Whitcroft } 27400df344fSAndy Whitcroft 27500df344fSAndy Whitcroft $l = $c; 27600df344fSAndy Whitcroft } 27700df344fSAndy Whitcroft 27800df344fSAndy Whitcroft return $res; 27900df344fSAndy Whitcroft} 28000df344fSAndy Whitcroft 2814a0df2efSAndy Whitcroftsub ctx_block_get { 282f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 2834a0df2efSAndy Whitcroft my $line; 2844a0df2efSAndy Whitcroft my $start = $linenr - 1; 2854a0df2efSAndy Whitcroft my $blk = ''; 2864a0df2efSAndy Whitcroft my @o; 2874a0df2efSAndy Whitcroft my @c; 2884a0df2efSAndy Whitcroft my @res = (); 2894a0df2efSAndy Whitcroft 290f0a594c1SAndy Whitcroft my $level = 0; 29100df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 29200df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 29300df344fSAndy Whitcroft $remain--; 29400df344fSAndy Whitcroft 29500df344fSAndy Whitcroft $blk .= $rawlines[$line]; 296f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$line])) { 297f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 298f0a594c1SAndy Whitcroft if ($off > 0) { 299f0a594c1SAndy Whitcroft $off--; 300f0a594c1SAndy Whitcroft next; 301f0a594c1SAndy Whitcroft } 3024a0df2efSAndy Whitcroft 303f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 304f0a594c1SAndy Whitcroft $level--; 305f0a594c1SAndy Whitcroft last if ($level == 0); 306f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 307f0a594c1SAndy Whitcroft $level++; 308f0a594c1SAndy Whitcroft } 309f0a594c1SAndy Whitcroft } 3104a0df2efSAndy Whitcroft 311f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 31200df344fSAndy Whitcroft push(@res, $rawlines[$line]); 3134a0df2efSAndy Whitcroft } 3144a0df2efSAndy Whitcroft 315f0a594c1SAndy Whitcroft last if ($level == 0); 3164a0df2efSAndy Whitcroft } 3174a0df2efSAndy Whitcroft 318f0a594c1SAndy Whitcroft return ($level, @res); 3194a0df2efSAndy Whitcroft} 3204a0df2efSAndy Whitcroftsub ctx_block_outer { 3214a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 3224a0df2efSAndy Whitcroft 323f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 324f0a594c1SAndy Whitcroft return @r; 3254a0df2efSAndy Whitcroft} 3264a0df2efSAndy Whitcroftsub ctx_block { 3274a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 3284a0df2efSAndy Whitcroft 329f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 330f0a594c1SAndy Whitcroft return @r; 331653d4876SAndy Whitcroft} 332653d4876SAndy Whitcroftsub ctx_statement { 333f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 334f0a594c1SAndy Whitcroft 335f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 336f0a594c1SAndy Whitcroft return @r; 337f0a594c1SAndy Whitcroft} 338f0a594c1SAndy Whitcroftsub ctx_block_level { 339653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 340653d4876SAndy Whitcroft 341f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 3424a0df2efSAndy Whitcroft} 3439c0ca6f9SAndy Whitcroftsub ctx_statement_level { 3449c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 3459c0ca6f9SAndy Whitcroft 3469c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 3479c0ca6f9SAndy Whitcroft} 3484a0df2efSAndy Whitcroft 3494a0df2efSAndy Whitcroftsub ctx_locate_comment { 3504a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 3514a0df2efSAndy Whitcroft 3524a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 35300df344fSAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); 3544a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 3554a0df2efSAndy Whitcroft 3564a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 3574a0df2efSAndy Whitcroft # comment. 3584a0df2efSAndy Whitcroft my $in_comment = 0; 3594a0df2efSAndy Whitcroft $current_comment = ''; 3604a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 36100df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 36200df344fSAndy Whitcroft #warn " $line\n"; 3634a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 3644a0df2efSAndy Whitcroft $in_comment = 1; 3654a0df2efSAndy Whitcroft } 3664a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 3674a0df2efSAndy Whitcroft $in_comment = 1; 3684a0df2efSAndy Whitcroft } 3694a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 3704a0df2efSAndy Whitcroft $current_comment = ''; 3714a0df2efSAndy Whitcroft } 3724a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 3734a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 3744a0df2efSAndy Whitcroft $in_comment = 0; 3754a0df2efSAndy Whitcroft } 3764a0df2efSAndy Whitcroft } 3774a0df2efSAndy Whitcroft 3784a0df2efSAndy Whitcroft chomp($current_comment); 3794a0df2efSAndy Whitcroft return($current_comment); 3804a0df2efSAndy Whitcroft} 3814a0df2efSAndy Whitcroftsub ctx_has_comment { 3824a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 3834a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 3844a0df2efSAndy Whitcroft 38500df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 3864a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 3874a0df2efSAndy Whitcroft 3884a0df2efSAndy Whitcroft return ($cmt ne ''); 3894a0df2efSAndy Whitcroft} 3904a0df2efSAndy Whitcroft 3910a920b5bSAndy Whitcroftsub cat_vet { 3920a920b5bSAndy Whitcroft my ($vet) = @_; 3939c0ca6f9SAndy Whitcroft my ($res, $coded); 3940a920b5bSAndy Whitcroft 3959c0ca6f9SAndy Whitcroft $res = ''; 396*6c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 397*6c72ffaaSAndy Whitcroft $res .= $1; 398*6c72ffaaSAndy Whitcroft if ($2 ne '') { 3999c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 400*6c72ffaaSAndy Whitcroft $res .= $coded; 401*6c72ffaaSAndy Whitcroft } 4029c0ca6f9SAndy Whitcroft } 4039c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 4040a920b5bSAndy Whitcroft 4059c0ca6f9SAndy Whitcroft return $res; 4060a920b5bSAndy Whitcroft} 4070a920b5bSAndy Whitcroft 408*6c72ffaaSAndy Whitcroftsub annotate_values { 409*6c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 410*6c72ffaaSAndy Whitcroft 411*6c72ffaaSAndy Whitcroft my $res; 412*6c72ffaaSAndy Whitcroft my $cur = $stream; 413*6c72ffaaSAndy Whitcroft 414*6c72ffaaSAndy Whitcroft my $debug = 0; 415*6c72ffaaSAndy Whitcroft 416*6c72ffaaSAndy Whitcroft print "$stream\n" if ($debug); 417*6c72ffaaSAndy Whitcroft 418*6c72ffaaSAndy Whitcroft ##my $type = 'N'; 419*6c72ffaaSAndy Whitcroft my $pos = 0; 420*6c72ffaaSAndy Whitcroft my $preprocessor = 0; 421*6c72ffaaSAndy Whitcroft my $paren = 0; 422*6c72ffaaSAndy Whitcroft my @paren_type; 423*6c72ffaaSAndy Whitcroft 424*6c72ffaaSAndy Whitcroft # Include any user defined types we may have found as we went. 425*6c72ffaaSAndy Whitcroft my $type_match = "(?:$Type$Bare)"; 426*6c72ffaaSAndy Whitcroft 427*6c72ffaaSAndy Whitcroft while (length($cur)) { 428*6c72ffaaSAndy Whitcroft print " <$type> " if ($debug); 429*6c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 430*6c72ffaaSAndy Whitcroft print "WS($1)\n" if ($debug); 431*6c72ffaaSAndy Whitcroft if ($1 =~ /\n/ && $preprocessor) { 432*6c72ffaaSAndy Whitcroft $preprocessor = 0; 433*6c72ffaaSAndy Whitcroft $type = 'N'; 434*6c72ffaaSAndy Whitcroft } 435*6c72ffaaSAndy Whitcroft 436*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($type_match)/) { 437*6c72ffaaSAndy Whitcroft print "DECLARE($1)\n" if ($debug); 438*6c72ffaaSAndy Whitcroft $type = 'T'; 439*6c72ffaaSAndy Whitcroft 440*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { 441*6c72ffaaSAndy Whitcroft print "DEFINE($1)\n" if ($debug); 442*6c72ffaaSAndy Whitcroft $preprocessor = 1; 443*6c72ffaaSAndy Whitcroft $paren_type[$paren] = 'N'; 444*6c72ffaaSAndy Whitcroft 445*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) { 446*6c72ffaaSAndy Whitcroft print "PRE($1)\n" if ($debug); 447*6c72ffaaSAndy Whitcroft $preprocessor = 1; 448*6c72ffaaSAndy Whitcroft $type = 'N'; 449*6c72ffaaSAndy Whitcroft 450*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 451*6c72ffaaSAndy Whitcroft print "PRECONT($1)\n" if ($debug); 452*6c72ffaaSAndy Whitcroft 453*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 454*6c72ffaaSAndy Whitcroft print "SIZEOF($1)\n" if ($debug); 455*6c72ffaaSAndy Whitcroft if (defined $2) { 456*6c72ffaaSAndy Whitcroft $paren_type[$paren] = 'V'; 457*6c72ffaaSAndy Whitcroft } 458*6c72ffaaSAndy Whitcroft $type = 'N'; 459*6c72ffaaSAndy Whitcroft 460*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(if|while|typeof)\b/o) { 461*6c72ffaaSAndy Whitcroft print "COND($1)\n" if ($debug); 462*6c72ffaaSAndy Whitcroft $paren_type[$paren] = 'N'; 463*6c72ffaaSAndy Whitcroft $type = 'N'; 464*6c72ffaaSAndy Whitcroft 465*6c72ffaaSAndy Whitcroft } elsif ($cur =~/^(return|case|else)/o) { 466*6c72ffaaSAndy Whitcroft print "KEYWORD($1)\n" if ($debug); 467*6c72ffaaSAndy Whitcroft $type = 'N'; 468*6c72ffaaSAndy Whitcroft 469*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 470*6c72ffaaSAndy Whitcroft print "PAREN('$1')\n" if ($debug); 471*6c72ffaaSAndy Whitcroft $paren++; 472*6c72ffaaSAndy Whitcroft $type = 'N'; 473*6c72ffaaSAndy Whitcroft 474*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 475*6c72ffaaSAndy Whitcroft $paren-- if ($paren > 0); 476*6c72ffaaSAndy Whitcroft if (defined $paren_type[$paren]) { 477*6c72ffaaSAndy Whitcroft $type = $paren_type[$paren]; 478*6c72ffaaSAndy Whitcroft undef $paren_type[$paren]; 479*6c72ffaaSAndy Whitcroft print "PAREN('$1') -> $type\n" if ($debug); 480*6c72ffaaSAndy Whitcroft } else { 481*6c72ffaaSAndy Whitcroft print "PAREN('$1')\n" if ($debug); 482*6c72ffaaSAndy Whitcroft } 483*6c72ffaaSAndy Whitcroft 484*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident)\(/o) { 485*6c72ffaaSAndy Whitcroft print "FUNC($1)\n" if ($debug); 486*6c72ffaaSAndy Whitcroft $paren_type[$paren] = 'V'; 487*6c72ffaaSAndy Whitcroft 488*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 489*6c72ffaaSAndy Whitcroft print "IDENT($1)\n" if ($debug); 490*6c72ffaaSAndy Whitcroft $type = 'V'; 491*6c72ffaaSAndy Whitcroft 492*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 493*6c72ffaaSAndy Whitcroft print "ASSIGN($1)\n" if ($debug); 494*6c72ffaaSAndy Whitcroft $type = 'N'; 495*6c72ffaaSAndy Whitcroft 496*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { 497*6c72ffaaSAndy Whitcroft print "END($1)\n" if ($debug); 498*6c72ffaaSAndy Whitcroft $type = 'N'; 499*6c72ffaaSAndy Whitcroft 500*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 501*6c72ffaaSAndy Whitcroft print "OP($1)\n" if ($debug); 502*6c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 503*6c72ffaaSAndy Whitcroft $type = 'N'; 504*6c72ffaaSAndy Whitcroft } 505*6c72ffaaSAndy Whitcroft 506*6c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 507*6c72ffaaSAndy Whitcroft print "C($1)\n" if ($debug); 508*6c72ffaaSAndy Whitcroft } 509*6c72ffaaSAndy Whitcroft if (defined $1) { 510*6c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 511*6c72ffaaSAndy Whitcroft $res .= $type x length($1); 512*6c72ffaaSAndy Whitcroft } 513*6c72ffaaSAndy Whitcroft } 514*6c72ffaaSAndy Whitcroft 515*6c72ffaaSAndy Whitcroft return $res; 516*6c72ffaaSAndy Whitcroft} 517*6c72ffaaSAndy Whitcroft 518*6c72ffaaSAndy Whitcroftmy $prefix = ''; 519*6c72ffaaSAndy Whitcroft 520f0a594c1SAndy Whitcroftmy @report = (); 521f0a594c1SAndy Whitcroftsub report { 522*6c72ffaaSAndy Whitcroft push(@report, $prefix . $_[0]); 523f0a594c1SAndy Whitcroft} 524f0a594c1SAndy Whitcroftsub report_dump { 525f0a594c1SAndy Whitcroft @report; 526f0a594c1SAndy Whitcroft} 527de7d4f0eSAndy Whitcroftsub ERROR { 528f0a594c1SAndy Whitcroft report("ERROR: $_[0]\n"); 529de7d4f0eSAndy Whitcroft our $clean = 0; 530*6c72ffaaSAndy Whitcroft our $cnt_error++; 531de7d4f0eSAndy Whitcroft} 532de7d4f0eSAndy Whitcroftsub WARN { 533f0a594c1SAndy Whitcroft report("WARNING: $_[0]\n"); 534de7d4f0eSAndy Whitcroft our $clean = 0; 535*6c72ffaaSAndy Whitcroft our $cnt_warn++; 536de7d4f0eSAndy Whitcroft} 537de7d4f0eSAndy Whitcroftsub CHK { 538*6c72ffaaSAndy Whitcroft if ($check) { 539f0a594c1SAndy Whitcroft report("CHECK: $_[0]\n"); 540de7d4f0eSAndy Whitcroft our $clean = 0; 541*6c72ffaaSAndy Whitcroft our $cnt_chk++; 542*6c72ffaaSAndy Whitcroft } 543de7d4f0eSAndy Whitcroft} 544de7d4f0eSAndy Whitcroft 5450a920b5bSAndy Whitcroftsub process { 5460a920b5bSAndy Whitcroft my $filename = shift; 5470a920b5bSAndy Whitcroft my @lines = @_; 5480a920b5bSAndy Whitcroft 5490a920b5bSAndy Whitcroft my $linenr=0; 5500a920b5bSAndy Whitcroft my $prevline=""; 5510a920b5bSAndy Whitcroft my $stashline=""; 5520a920b5bSAndy Whitcroft 5534a0df2efSAndy Whitcroft my $length; 5540a920b5bSAndy Whitcroft my $indent; 5550a920b5bSAndy Whitcroft my $previndent=0; 5560a920b5bSAndy Whitcroft my $stashindent=0; 5570a920b5bSAndy Whitcroft 558de7d4f0eSAndy Whitcroft our $clean = 1; 5590a920b5bSAndy Whitcroft my $signoff = 0; 5600a920b5bSAndy Whitcroft my $is_patch = 0; 5610a920b5bSAndy Whitcroft 562*6c72ffaaSAndy Whitcroft our $cnt_lines = 0; 563*6c72ffaaSAndy Whitcroft our $cnt_error = 0; 564*6c72ffaaSAndy Whitcroft our $cnt_warn = 0; 565*6c72ffaaSAndy Whitcroft our $cnt_chk = 0; 566*6c72ffaaSAndy Whitcroft 5670a920b5bSAndy Whitcroft # Trace the real file/line as we go. 5680a920b5bSAndy Whitcroft my $realfile = ''; 5690a920b5bSAndy Whitcroft my $realline = 0; 5700a920b5bSAndy Whitcroft my $realcnt = 0; 5710a920b5bSAndy Whitcroft my $here = ''; 5720a920b5bSAndy Whitcroft my $in_comment = 0; 5730a920b5bSAndy Whitcroft my $first_line = 0; 5740a920b5bSAndy Whitcroft 575*6c72ffaaSAndy Whitcroft my $prev_values = 'N'; 576653d4876SAndy Whitcroft 5779c0ca6f9SAndy Whitcroft # Possible bare types. 5789c0ca6f9SAndy Whitcroft my @bare = (); 5799c0ca6f9SAndy Whitcroft 580de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 581de7d4f0eSAndy Whitcroft my @setup_docs = (); 582de7d4f0eSAndy Whitcroft my $setup_docs = 0; 583de7d4f0eSAndy Whitcroft foreach my $line (@lines) { 584de7d4f0eSAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 585de7d4f0eSAndy Whitcroft $setup_docs = 0; 586de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 587de7d4f0eSAndy Whitcroft $setup_docs = 1; 588de7d4f0eSAndy Whitcroft } 589de7d4f0eSAndy Whitcroft next; 590de7d4f0eSAndy Whitcroft } 591de7d4f0eSAndy Whitcroft 592de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 593de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 594de7d4f0eSAndy Whitcroft } 595de7d4f0eSAndy Whitcroft } 596de7d4f0eSAndy Whitcroft 597*6c72ffaaSAndy Whitcroft $prefix = ''; 598*6c72ffaaSAndy Whitcroft 5990a920b5bSAndy Whitcroft foreach my $line (@lines) { 6000a920b5bSAndy Whitcroft $linenr++; 6010a920b5bSAndy Whitcroft 602653d4876SAndy Whitcroft my $rawline = $line; 603653d4876SAndy Whitcroft 604*6c72ffaaSAndy Whitcroft 6050a920b5bSAndy Whitcroft#extract the filename as it passes 6060a920b5bSAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 6070a920b5bSAndy Whitcroft $realfile=$1; 60800df344fSAndy Whitcroft $realfile =~ s@^[^/]*/@@; 6090a920b5bSAndy Whitcroft $in_comment = 0; 6100a920b5bSAndy Whitcroft next; 6110a920b5bSAndy Whitcroft } 6120a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 613*6c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 6140a920b5bSAndy Whitcroft $is_patch = 1; 6154a0df2efSAndy Whitcroft $first_line = $linenr + 1; 6160a920b5bSAndy Whitcroft $in_comment = 0; 6170a920b5bSAndy Whitcroft $realline=$1-1; 6180a920b5bSAndy Whitcroft if (defined $2) { 6190a920b5bSAndy Whitcroft $realcnt=$3+1; 6200a920b5bSAndy Whitcroft } else { 6210a920b5bSAndy Whitcroft $realcnt=1+1; 6220a920b5bSAndy Whitcroft } 623*6c72ffaaSAndy Whitcroft $prev_values = 'N'; 6240a920b5bSAndy Whitcroft next; 6250a920b5bSAndy Whitcroft } 6260a920b5bSAndy Whitcroft 6274a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 6284a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 6294a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 6304a0df2efSAndy Whitcroft if ($line =~ /^( |\+|$)/) { 6310a920b5bSAndy Whitcroft $realline++; 632d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 6330a920b5bSAndy Whitcroft 6340a920b5bSAndy Whitcroft # track any sort of multi-line comment. Obviously if 6350a920b5bSAndy Whitcroft # the added text or context do not include the whole 6360a920b5bSAndy Whitcroft # comment we will not see it. Such is life. 6370a920b5bSAndy Whitcroft # 6380a920b5bSAndy Whitcroft # Guestimate if this is a continuing comment. If this 6390a920b5bSAndy Whitcroft # is the start of a diff block and this line starts 6400a920b5bSAndy Whitcroft # ' *' then it is very likely a comment. 6414a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 6420a920b5bSAndy Whitcroft $in_comment = 1; 6430a920b5bSAndy Whitcroft } 6440a920b5bSAndy Whitcroft if ($line =~ m@/\*@) { 6450a920b5bSAndy Whitcroft $in_comment = 1; 6460a920b5bSAndy Whitcroft } 6470a920b5bSAndy Whitcroft if ($line =~ m@\*/@) { 6480a920b5bSAndy Whitcroft $in_comment = 0; 6490a920b5bSAndy Whitcroft } 6500a920b5bSAndy Whitcroft 6514a0df2efSAndy Whitcroft # Measure the line length and indent. 6524a0df2efSAndy Whitcroft ($length, $indent) = line_stats($line); 6530a920b5bSAndy Whitcroft 6540a920b5bSAndy Whitcroft # Track the previous line. 6550a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 6560a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 657*6c72ffaaSAndy Whitcroft 658d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 659d8aaf121SAndy Whitcroft $realcnt--; 6600a920b5bSAndy Whitcroft } 6610a920b5bSAndy Whitcroft 6620a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 663*6c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 664*6c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 665389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 6660a920b5bSAndy Whitcroft 66700df344fSAndy Whitcroft my $hereline = "$here\n$line\n"; 668de7d4f0eSAndy Whitcroft my $herecurr = "$here\n$line\n"; 669de7d4f0eSAndy Whitcroft my $hereprev = "$here\n$prevline\n$line\n"; 6700a920b5bSAndy Whitcroft 671*6c72ffaaSAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 672*6c72ffaaSAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 673*6c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 674*6c72ffaaSAndy Whitcroft 6750a920b5bSAndy Whitcroft#check the patch for a signoff: 676d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 6774a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 6784a0df2efSAndy Whitcroft $signoff++; 6790a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 680de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 681de7d4f0eSAndy Whitcroft $herecurr); 6820a920b5bSAndy Whitcroft } 6830a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 684de7d4f0eSAndy Whitcroft WARN("need space after Signed-off-by:\n" . 685de7d4f0eSAndy Whitcroft $herecurr); 6860a920b5bSAndy Whitcroft } 6870a920b5bSAndy Whitcroft } 6880a920b5bSAndy Whitcroft 68900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 69000df344fSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { 691de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 692*6c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 693de7d4f0eSAndy Whitcroft } 694de7d4f0eSAndy Whitcroft 695de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 696de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 697de7d4f0eSAndy Whitcroft !($line =~ m/^( 698de7d4f0eSAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 699de7d4f0eSAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 700de7d4f0eSAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 701de7d4f0eSAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 702de7d4f0eSAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 703de7d4f0eSAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 704de7d4f0eSAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 705de7d4f0eSAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 706de7d4f0eSAndy Whitcroft )*$/x )) { 707de7d4f0eSAndy Whitcroft ERROR("Invalid UTF-8\n" . $herecurr); 70800df344fSAndy Whitcroft } 7090a920b5bSAndy Whitcroft 71000df344fSAndy Whitcroft#ignore lines being removed 71100df344fSAndy Whitcroft if ($line=~/^-/) {next;} 71200df344fSAndy Whitcroft 71300df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 71400df344fSAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 7150a920b5bSAndy Whitcroft 7160a920b5bSAndy Whitcroft#trailing whitespace 7179c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 7189c0ca6f9SAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n"; 7199c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 7209c0ca6f9SAndy Whitcroft 7219c0ca6f9SAndy Whitcroft } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { 722de7d4f0eSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n"; 723de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 7240a920b5bSAndy Whitcroft } 7250a920b5bSAndy Whitcroft#80 column limit 72600df344fSAndy Whitcroft if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { 727de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 7280a920b5bSAndy Whitcroft } 7290a920b5bSAndy Whitcroft 7300a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk 7310a920b5bSAndy Whitcroft next if ($realfile !~ /\.[hc]$/); 7320a920b5bSAndy Whitcroft 7330a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 7340a920b5bSAndy Whitcroft# more than 8 must use tabs. 7350a920b5bSAndy Whitcroft if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { 736de7d4f0eSAndy Whitcroft my $herevet = "$here\n" . cat_vet($line) . "\n"; 737de7d4f0eSAndy Whitcroft ERROR("use tabs not spaces\n" . $herevet); 7380a920b5bSAndy Whitcroft } 7390a920b5bSAndy Whitcroft 7400a920b5bSAndy Whitcroft# Remove comments from the line before processing. 74122f2a2efSAndy Whitcroft my $comment_edge = ($line =~ s@/\*.*\*/@@g) + 74222f2a2efSAndy Whitcroft ($line =~ s@/\*.*@@) + 74322f2a2efSAndy Whitcroft ($line =~ s@^(.).*\*/@$1@); 74422f2a2efSAndy Whitcroft 74522f2a2efSAndy Whitcroft# The rest of our checks refer specifically to C style 74622f2a2efSAndy Whitcroft# only apply those _outside_ comments. Only skip 74722f2a2efSAndy Whitcroft# lines in the middle of comments. 74822f2a2efSAndy Whitcroft next if (!$comment_edge && $in_comment); 74900df344fSAndy Whitcroft 750653d4876SAndy Whitcroft# Standardise the strings and chars within the input to simplify matching. 751653d4876SAndy Whitcroft $line = sanitise_line($line); 752653d4876SAndy Whitcroft 7539c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 7549c0ca6f9SAndy Whitcroft if ($realcnt && 7559c0ca6f9SAndy Whitcroft $line !~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?$Type\b/ && 7569c0ca6f9SAndy Whitcroft $line !~ /$Ident:\s*$/ && 7579c0ca6f9SAndy Whitcroft $line !~ /^.\s*$Ident\s*\(/ && 758*6c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 7599c0ca6f9SAndy Whitcroft ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?($Ident)\b/ || 760*6c72ffaaSAndy Whitcroft # declarations always start with types 761*6c72ffaaSAndy Whitcroft $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) || 762*6c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 763*6c72ffaaSAndy Whitcroft $line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) { 7649c0ca6f9SAndy Whitcroft my $possible = $1; 7659c0ca6f9SAndy Whitcroft if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && 7669c0ca6f9SAndy Whitcroft $possible ne 'goto' && $possible ne 'return' && 7679c0ca6f9SAndy Whitcroft $possible ne 'struct' && $possible ne 'enum' && 7689c0ca6f9SAndy Whitcroft $possible ne 'case' && $possible ne 'else' && 7699c0ca6f9SAndy Whitcroft $possible ne 'typedef') { 7709c0ca6f9SAndy Whitcroft #print "POSSIBLE<$possible>\n"; 7719c0ca6f9SAndy Whitcroft push(@bare, $possible); 7729c0ca6f9SAndy Whitcroft my $bare = join("|", @bare); 773*6c72ffaaSAndy Whitcroft $Bare = '|' . qr{ 7749c0ca6f9SAndy Whitcroft \b(?:$bare)\b 7759c0ca6f9SAndy Whitcroft (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? 7769c0ca6f9SAndy Whitcroft (?:\s+$Sparse)* 7779c0ca6f9SAndy Whitcroft }x; 7789c0ca6f9SAndy Whitcroft } 7799c0ca6f9SAndy Whitcroft } 7809c0ca6f9SAndy Whitcroft 78100df344fSAndy Whitcroft# 78200df344fSAndy Whitcroft# Checks which may be anchored in the context. 78300df344fSAndy Whitcroft# 78400df344fSAndy Whitcroft 78500df344fSAndy Whitcroft# Check for switch () and associated case and default 78600df344fSAndy Whitcroft# statements should be at the same indent. 78700df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 78800df344fSAndy Whitcroft my $err = ''; 78900df344fSAndy Whitcroft my $sep = ''; 79000df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 79100df344fSAndy Whitcroft shift(@ctx); 79200df344fSAndy Whitcroft for my $ctx (@ctx) { 79300df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 79400df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 79500df344fSAndy Whitcroft $indent != $cindent) { 79600df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 79700df344fSAndy Whitcroft $sep = ''; 79800df344fSAndy Whitcroft } else { 79900df344fSAndy Whitcroft $sep = "[...]\n"; 80000df344fSAndy Whitcroft } 80100df344fSAndy Whitcroft } 80200df344fSAndy Whitcroft if ($err ne '') { 8039c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 804de7d4f0eSAndy Whitcroft } 805de7d4f0eSAndy Whitcroft } 806de7d4f0eSAndy Whitcroft 807de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 808de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 809de7d4f0eSAndy Whitcroft if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { 8109c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 811de7d4f0eSAndy Whitcroft my $ctx_ln = $linenr + $#ctx + 1; 812de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 813de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 814de7d4f0eSAndy Whitcroft 8159c0ca6f9SAndy Whitcroft # Skip over any removed lines in the context following statement. 816de7d4f0eSAndy Whitcroft while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { 817de7d4f0eSAndy Whitcroft $ctx_ln++; 818de7d4f0eSAndy Whitcroft $ctx_cnt--; 819de7d4f0eSAndy Whitcroft } 820de7d4f0eSAndy Whitcroft ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; 821de7d4f0eSAndy Whitcroft 822de7d4f0eSAndy Whitcroft if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 823f0a594c1SAndy Whitcroft ERROR("That open brace { should be on the previous line\n" . 824de7d4f0eSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]"); 82500df344fSAndy Whitcroft } 8269c0ca6f9SAndy Whitcroft if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { 8279c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 8289c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 8299c0ca6f9SAndy Whitcroft WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . 8309c0ca6f9SAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]"); 8319c0ca6f9SAndy Whitcroft } 8329c0ca6f9SAndy Whitcroft } 83300df344fSAndy Whitcroft } 83400df344fSAndy Whitcroft 835*6c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 836*6c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 837*6c72ffaaSAndy Whitcroft my $curr_values = annotate_values($opline . "\n", $prev_values); 838*6c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 839*6c72ffaaSAndy Whitcroft #warn "--> $opline\n"; 840*6c72ffaaSAndy Whitcroft #warn "--> $curr_values ($prev_values)\n"; 841*6c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 842*6c72ffaaSAndy Whitcroft 84300df344fSAndy Whitcroft#ignore lines not being added 84400df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 84500df344fSAndy Whitcroft 846653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 847653d4876SAndy Whitcroft if ($tst_type && $line =~ /^.$Declare$/) { 848de7d4f0eSAndy Whitcroft ERROR("TEST: is type $Declare\n" . $herecurr); 849653d4876SAndy Whitcroft next; 850653d4876SAndy Whitcroft } 851653d4876SAndy Whitcroft 852f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 853f0a594c1SAndy Whitcroft if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 854f0a594c1SAndy Whitcroft $line =~ /^.\s*{/) { 855f0a594c1SAndy Whitcroft ERROR("That open brace { should be on the previous line\n" . $hereprev); 856f0a594c1SAndy Whitcroft } 857f0a594c1SAndy Whitcroft 85800df344fSAndy Whitcroft# 85900df344fSAndy Whitcroft# Checks which are anchored on the added line. 86000df344fSAndy Whitcroft# 86100df344fSAndy Whitcroft 862653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 863653d4876SAndy Whitcroft if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 864653d4876SAndy Whitcroft my $path = $1; 865653d4876SAndy Whitcroft if ($path =~ m{//}) { 866de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 867de7d4f0eSAndy Whitcroft $herecurr); 868653d4876SAndy Whitcroft } 869653d4876SAndy Whitcroft # Sanitise this special form of string. 870653d4876SAndy Whitcroft $path = 'X' x length($path); 871653d4876SAndy Whitcroft $line =~ s{\<.*\>}{<$path>}; 872653d4876SAndy Whitcroft } 873653d4876SAndy Whitcroft 87400df344fSAndy Whitcroft# no C99 // comments 87500df344fSAndy Whitcroft if ($line =~ m{//}) { 876de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 87700df344fSAndy Whitcroft } 87800df344fSAndy Whitcroft # Remove C99 comments. 8790a920b5bSAndy Whitcroft $line =~ s@//.*@@; 880*6c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 8810a920b5bSAndy Whitcroft 8820a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 883653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 884653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 885653d4876SAndy Whitcroft my $name = $1; 8860a920b5bSAndy Whitcroft if (($prevline !~ /^}/) && 8870a920b5bSAndy Whitcroft ($prevline !~ /^\+}/) && 888653d4876SAndy Whitcroft ($prevline !~ /^ }/) && 88922f2a2efSAndy Whitcroft ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { 890de7d4f0eSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 8910a920b5bSAndy Whitcroft } 8920a920b5bSAndy Whitcroft } 8930a920b5bSAndy Whitcroft 894f0a594c1SAndy Whitcroft# check for external initialisers. 895f0a594c1SAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { 896f0a594c1SAndy Whitcroft ERROR("do not initialise externals to 0 or NULL\n" . 897f0a594c1SAndy Whitcroft $herecurr); 898f0a594c1SAndy Whitcroft } 8990a920b5bSAndy Whitcroft# check for static initialisers. 900f0a594c1SAndy Whitcroft if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { 901de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 902de7d4f0eSAndy Whitcroft $herecurr); 9030a920b5bSAndy Whitcroft } 9040a920b5bSAndy Whitcroft 905653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 906653d4876SAndy Whitcroft# make sense. 907653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 9089c0ca6f9SAndy Whitcroft $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && 909653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 910de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 9110a920b5bSAndy Whitcroft } 9120a920b5bSAndy Whitcroft 9130a920b5bSAndy Whitcroft# * goes on variable not on type 914d8aaf121SAndy Whitcroft if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { 915de7d4f0eSAndy Whitcroft ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . 916de7d4f0eSAndy Whitcroft $herecurr); 917d8aaf121SAndy Whitcroft 918d8aaf121SAndy Whitcroft } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { 919de7d4f0eSAndy Whitcroft ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . 920de7d4f0eSAndy Whitcroft $herecurr); 921d8aaf121SAndy Whitcroft 9229c0ca6f9SAndy Whitcroft } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { 923de7d4f0eSAndy Whitcroft ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . 924de7d4f0eSAndy Whitcroft $herecurr); 925d8aaf121SAndy Whitcroft 9269c0ca6f9SAndy Whitcroft } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { 927de7d4f0eSAndy Whitcroft ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . 928de7d4f0eSAndy Whitcroft $herecurr); 9290a920b5bSAndy Whitcroft } 9300a920b5bSAndy Whitcroft 9310a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 9320a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 9330a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 9340a920b5bSAndy Whitcroft# print "$herecurr"; 9350a920b5bSAndy Whitcroft# $clean = 0; 9360a920b5bSAndy Whitcroft# } 9370a920b5bSAndy Whitcroft 93800df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 93900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 94000df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 94100df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 94200df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 943f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 94400df344fSAndy Whitcroft my $ok = 0; 94500df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 94600df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 94700df344fSAndy Whitcroft # we have a preceeding printk if it ends 94800df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 94900df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 95000df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 95100df344fSAndy Whitcroft $ok = 1; 95200df344fSAndy Whitcroft } 95300df344fSAndy Whitcroft last; 95400df344fSAndy Whitcroft } 95500df344fSAndy Whitcroft } 95600df344fSAndy Whitcroft if ($ok == 0) { 957de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 9580a920b5bSAndy Whitcroft } 95900df344fSAndy Whitcroft } 9600a920b5bSAndy Whitcroft 961653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 962653d4876SAndy Whitcroft# or if closed on same line 963d8aaf121SAndy Whitcroft if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and 9640a920b5bSAndy Whitcroft !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 965de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 9660a920b5bSAndy Whitcroft } 967653d4876SAndy Whitcroft 968f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 969*6c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 970*6c72ffaaSAndy Whitcroft if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ && 971f0a594c1SAndy Whitcroft $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { 97222f2a2efSAndy Whitcroft WARN("no space between function name and open parenthesis '('\n" . $herecurr); 973f0a594c1SAndy Whitcroft } 974*6c72ffaaSAndy Whitcroft } 975653d4876SAndy Whitcroft# Check operator spacing. 9760a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 9779c0ca6f9SAndy Whitcroft my $ops = qr{ 9789c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 9799c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 9809c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 9819c0ca6f9SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ 9829c0ca6f9SAndy Whitcroft }x; 9839c0ca6f9SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 98400df344fSAndy Whitcroft my $off = 0; 985*6c72ffaaSAndy Whitcroft 986*6c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 987*6c72ffaaSAndy Whitcroft 9880a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 9894a0df2efSAndy Whitcroft $off += length($elements[$n]); 9904a0df2efSAndy Whitcroft 9914a0df2efSAndy Whitcroft my $a = ''; 9924a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 9934a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 9944a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 9954a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 9964a0df2efSAndy Whitcroft $a = 'E' if ($elements[$n] eq '' && $n == 0); 9974a0df2efSAndy Whitcroft 9980a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 9994a0df2efSAndy Whitcroft 10004a0df2efSAndy Whitcroft my $c = ''; 10010a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 10024a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 10034a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 10044a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 10054a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 100622f2a2efSAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); 10074a0df2efSAndy Whitcroft } else { 10084a0df2efSAndy Whitcroft $c = 'E'; 10090a920b5bSAndy Whitcroft } 10100a920b5bSAndy Whitcroft 101100df344fSAndy Whitcroft # Pick up the preceeding and succeeding characters. 1012de7d4f0eSAndy Whitcroft my $ca = substr($opline, 0, $off); 101300df344fSAndy Whitcroft my $cc = ''; 1014653d4876SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 1015d8aaf121SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 101600df344fSAndy Whitcroft } 1017de7d4f0eSAndy Whitcroft my $cb = "$ca$;$cc"; 101800df344fSAndy Whitcroft 10194a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 10204a0df2efSAndy Whitcroft 10214a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 10224a0df2efSAndy Whitcroft 1023*6c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 1024de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 10250a920b5bSAndy Whitcroft 10269c0ca6f9SAndy Whitcroft # Classify operators into binary, unary, or 10279c0ca6f9SAndy Whitcroft # definitions (* only) where they have more 10289c0ca6f9SAndy Whitcroft # than one mode. 1029*6c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 1030*6c72ffaaSAndy Whitcroft my $op_left = substr($curr_values, $off, 1); 1031*6c72ffaaSAndy Whitcroft my $is_unary; 1032*6c72ffaaSAndy Whitcroft if ($op_type eq 'T') { 10339c0ca6f9SAndy Whitcroft $is_unary = 2; 1034*6c72ffaaSAndy Whitcroft } elsif ($op_left eq 'V') { 1035*6c72ffaaSAndy Whitcroft $is_unary = 0; 1036*6c72ffaaSAndy Whitcroft } else { 1037*6c72ffaaSAndy Whitcroft $is_unary = 1; 10389c0ca6f9SAndy Whitcroft } 10399c0ca6f9SAndy Whitcroft #if ($op eq '-' || $op eq '&' || $op eq '*') { 1040*6c72ffaaSAndy Whitcroft # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; 10419c0ca6f9SAndy Whitcroft #} 10420a920b5bSAndy Whitcroft 1043d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 1044d8aaf121SAndy Whitcroft if ($op eq ';') { 1045de7d4f0eSAndy Whitcroft if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && 1046de7d4f0eSAndy Whitcroft $cc !~ /^;/) { 1047de7d4f0eSAndy Whitcroft ERROR("need space after that '$op' $at\n" . $hereptr); 1048d8aaf121SAndy Whitcroft } 1049d8aaf121SAndy Whitcroft 1050d8aaf121SAndy Whitcroft # // is a comment 1051d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 10520a920b5bSAndy Whitcroft 10530a920b5bSAndy Whitcroft # -> should have no spaces 10540a920b5bSAndy Whitcroft } elsif ($op eq '->') { 10554a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 1056de7d4f0eSAndy Whitcroft ERROR("no spaces around that '$op' $at\n" . $hereptr); 10570a920b5bSAndy Whitcroft } 10580a920b5bSAndy Whitcroft 10590a920b5bSAndy Whitcroft # , must have a space on the right. 10600a920b5bSAndy Whitcroft } elsif ($op eq ',') { 1061d8aaf121SAndy Whitcroft if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { 1062de7d4f0eSAndy Whitcroft ERROR("need space after that '$op' $at\n" . $hereptr); 10630a920b5bSAndy Whitcroft } 10640a920b5bSAndy Whitcroft 10659c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 10669c0ca6f9SAndy Whitcroft } elsif ($op eq '*' && $is_unary == 2) { 10679c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 10689c0ca6f9SAndy Whitcroft 10699c0ca6f9SAndy Whitcroft # unary operators should have a space before and 10709c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 10719c0ca6f9SAndy Whitcroft # unary operator, or a cast 10729c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 10739c0ca6f9SAndy Whitcroft ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { 10749c0ca6f9SAndy Whitcroft if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1075de7d4f0eSAndy Whitcroft ERROR("need space before that '$op' $at\n" . $hereptr); 10760a920b5bSAndy Whitcroft } 10774a0df2efSAndy Whitcroft if ($ctx =~ /.xW/) { 1078de7d4f0eSAndy Whitcroft ERROR("no space after that '$op' $at\n" . $hereptr); 10790a920b5bSAndy Whitcroft } 10800a920b5bSAndy Whitcroft 10810a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 10820a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 1083d8aaf121SAndy Whitcroft if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { 1084de7d4f0eSAndy Whitcroft ERROR("need space one side of that '$op' $at\n" . $hereptr); 10850a920b5bSAndy Whitcroft } 1086d8aaf121SAndy Whitcroft if ($ctx =~ /Wx./ && $cc =~ /^;/) { 1087de7d4f0eSAndy Whitcroft ERROR("no space before that '$op' $at\n" . $hereptr); 1088653d4876SAndy Whitcroft } 10890a920b5bSAndy Whitcroft 10900a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 10919c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 10929c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 10939c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 10949c0ca6f9SAndy Whitcroft $op eq '*' or $op eq '/') 10950a920b5bSAndy Whitcroft { 10969c0ca6f9SAndy Whitcroft if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { 1097de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 1098de7d4f0eSAndy Whitcroft $hereptr); 10990a920b5bSAndy Whitcroft } 11000a920b5bSAndy Whitcroft 11010a920b5bSAndy Whitcroft # All the others need spaces both sides. 11024a0df2efSAndy Whitcroft } elsif ($ctx !~ /[EW]x[WE]/) { 110322f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 110422f2a2efSAndy Whitcroft if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && 110522f2a2efSAndy Whitcroft !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { 1106de7d4f0eSAndy Whitcroft ERROR("need spaces around that '$op' $at\n" . $hereptr); 11070a920b5bSAndy Whitcroft } 110822f2a2efSAndy Whitcroft } 11094a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 11100a920b5bSAndy Whitcroft } 11110a920b5bSAndy Whitcroft } 11120a920b5bSAndy Whitcroft 1113f0a594c1SAndy Whitcroft# check for multiple assignments 1114f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 1115*6c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 1116f0a594c1SAndy Whitcroft } 1117f0a594c1SAndy Whitcroft 111822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 111922f2a2efSAndy Whitcroft## # continuation. 112022f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 112122f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 112222f2a2efSAndy Whitcroft## 112322f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 112422f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 112522f2a2efSAndy Whitcroft## my $ln = $line; 112622f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 112722f2a2efSAndy Whitcroft## } 112822f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 112922f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 113022f2a2efSAndy Whitcroft## } 113122f2a2efSAndy Whitcroft## } 1132f0a594c1SAndy Whitcroft 11330a920b5bSAndy Whitcroft#need space before brace following if, while, etc 113422f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 113522f2a2efSAndy Whitcroft $line =~ /do{/) { 1136de7d4f0eSAndy Whitcroft ERROR("need a space before the open brace '{'\n" . $herecurr); 1137de7d4f0eSAndy Whitcroft } 1138de7d4f0eSAndy Whitcroft 1139de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 1140de7d4f0eSAndy Whitcroft# on the line 1141de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 1142de7d4f0eSAndy Whitcroft ERROR("need a space after that close brace '}'\n" . $herecurr); 11430a920b5bSAndy Whitcroft } 11440a920b5bSAndy Whitcroft 114522f2a2efSAndy Whitcroft# check spacing on square brackets 114622f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 114722f2a2efSAndy Whitcroft ERROR("no space after that open square bracket '['\n" . $herecurr); 114822f2a2efSAndy Whitcroft } 114922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 115022f2a2efSAndy Whitcroft ERROR("no space before that close square bracket ']'\n" . $herecurr); 115122f2a2efSAndy Whitcroft } 115222f2a2efSAndy Whitcroft 115322f2a2efSAndy Whitcroft# check spacing on paretheses 11549c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 11559c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 115622f2a2efSAndy Whitcroft ERROR("no space after that open parenthesis '('\n" . $herecurr); 115722f2a2efSAndy Whitcroft } 11589c0ca6f9SAndy Whitcroft if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && 11599c0ca6f9SAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/) { 116022f2a2efSAndy Whitcroft ERROR("no space before that close parenthesis ')'\n" . $herecurr); 116122f2a2efSAndy Whitcroft } 116222f2a2efSAndy Whitcroft 11630a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 11644a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 11650a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 1166de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 11670a920b5bSAndy Whitcroft } 11680a920b5bSAndy Whitcroft 11690a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 11704a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 1171de7d4f0eSAndy Whitcroft ERROR("need a space before the open parenthesis '('\n" . $herecurr); 11720a920b5bSAndy Whitcroft } 11730a920b5bSAndy Whitcroft 11740a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional. 1175*6c72ffaaSAndy Whitcroft if ($line=~/\bif\s*\(.*[^<>!=]=[^=]/) { 117600df344fSAndy Whitcroft #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 1177de7d4f0eSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 11780a920b5bSAndy Whitcroft } 11790a920b5bSAndy Whitcroft 11800a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 11810a920b5bSAndy Whitcroft # indent level to be relevant to each other. 11820a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 11830a920b5bSAndy Whitcroft $previndent == $indent) { 1184de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 11850a920b5bSAndy Whitcroft } 11860a920b5bSAndy Whitcroft 11870a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 11880a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 11890a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 11900a920b5bSAndy Whitcroft# print "$herecurr"; 11910a920b5bSAndy Whitcroft# $clean = 0; 11920a920b5bSAndy Whitcroft# } 11930a920b5bSAndy Whitcroft 11940a920b5bSAndy Whitcroft#no spaces allowed after \ in define 11950a920b5bSAndy Whitcroft if ($line=~/\#define.*\\\s$/) { 1196de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 11970a920b5bSAndy Whitcroft } 11980a920b5bSAndy Whitcroft 1199653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 1200653d4876SAndy Whitcroft if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 1201*6c72ffaaSAndy Whitcroft my $checkfile = "$root/include/linux/$1.h"; 1202*6c72ffaaSAndy Whitcroft if (-f $checkfile && $1 ne 'irq.h') { 1203de7d4f0eSAndy Whitcroft CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . 1204de7d4f0eSAndy Whitcroft $herecurr); 12050a920b5bSAndy Whitcroft } 12060a920b5bSAndy Whitcroft } 12070a920b5bSAndy Whitcroft 1208d8aaf121SAndy Whitcroft# if and else should not have general statements after it 1209d8aaf121SAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && 1210de7d4f0eSAndy Whitcroft $1 !~ /^\s*(?:\sif|{|\\|$)/) { 1211de7d4f0eSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 1212d8aaf121SAndy Whitcroft } 1213d8aaf121SAndy Whitcroft 1214653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 1215653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 1216653d4876SAndy Whitcroft# in a known goot container 12179c0ca6f9SAndy Whitcroft if ($prevline =~ /\#define.*\\/ && 12189c0ca6f9SAndy Whitcroft $prevline !~/(?:do\s+{|\(\{|\{)/ && 12199c0ca6f9SAndy Whitcroft $line !~ /(?:do\s+{|\(\{|\{)/ && 12209c0ca6f9SAndy Whitcroft $line !~ /^.\s*$Declare\s/) { 1221653d4876SAndy Whitcroft # Grab the first statement, if that is the entire macro 1222653d4876SAndy Whitcroft # its ok. This may start either on the #define line 1223653d4876SAndy Whitcroft # or the one below. 1224d8aaf121SAndy Whitcroft my $ln = $linenr; 1225d8aaf121SAndy Whitcroft my $cnt = $realcnt; 1226f0a594c1SAndy Whitcroft my $off = 0; 1227653d4876SAndy Whitcroft 1228f0a594c1SAndy Whitcroft # If the macro starts on the define line start 1229f0a594c1SAndy Whitcroft # grabbing the statement after the identifier 1230f0a594c1SAndy Whitcroft $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; 1231f0a594c1SAndy Whitcroft ##print "1<$1> 2<$2>\n"; 123222f2a2efSAndy Whitcroft if (defined $2 && $2 ne '') { 1233f0a594c1SAndy Whitcroft $off = length($1); 1234d8aaf121SAndy Whitcroft $ln--; 1235d8aaf121SAndy Whitcroft $cnt++; 1236d8aaf121SAndy Whitcroft } 1237f0a594c1SAndy Whitcroft my @ctx = ctx_statement($ln, $cnt, $off); 1238de7d4f0eSAndy Whitcroft my $ctx_ln = $ln + $#ctx + 1; 1239de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1240de7d4f0eSAndy Whitcroft 1241de7d4f0eSAndy Whitcroft # Pull in any empty extension lines. 1242de7d4f0eSAndy Whitcroft while ($ctx =~ /\\$/ && 1243de7d4f0eSAndy Whitcroft $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { 1244de7d4f0eSAndy Whitcroft $ctx .= $lines[$ctx_ln - 1]; 1245de7d4f0eSAndy Whitcroft $ctx_ln++; 1246de7d4f0eSAndy Whitcroft } 1247d8aaf121SAndy Whitcroft 1248d8aaf121SAndy Whitcroft if ($ctx =~ /\\$/) { 1249d8aaf121SAndy Whitcroft if ($ctx =~ /;/) { 1250de7d4f0eSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 1251d8aaf121SAndy Whitcroft } else { 1252de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 1253d8aaf121SAndy Whitcroft } 12540a920b5bSAndy Whitcroft } 1255653d4876SAndy Whitcroft } 12560a920b5bSAndy Whitcroft 1257f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 1258f0a594c1SAndy Whitcroft if ($line =~ /\b(if|while|for|else)\b/) { 1259f0a594c1SAndy Whitcroft # Locate the end of the opening statement. 1260f0a594c1SAndy Whitcroft my @control = ctx_statement($linenr, $realcnt, 0); 1261f0a594c1SAndy Whitcroft my $nr = $linenr + (scalar(@control) - 1); 1262f0a594c1SAndy Whitcroft my $cnt = $realcnt - (scalar(@control) - 1); 1263f0a594c1SAndy Whitcroft 1264f0a594c1SAndy Whitcroft my $off = $realcnt - $cnt; 1265f0a594c1SAndy Whitcroft #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; 1266f0a594c1SAndy Whitcroft 1267f0a594c1SAndy Whitcroft # If this is is a braced statement group check it 1268f0a594c1SAndy Whitcroft if ($lines[$nr - 1] =~ /{\s*$/) { 1269f0a594c1SAndy Whitcroft my ($lvl, @block) = ctx_block_level($nr, $cnt); 1270f0a594c1SAndy Whitcroft 1271f0a594c1SAndy Whitcroft my $stmt = join(' ', @block); 127222f2a2efSAndy Whitcroft $stmt =~ s/(^[^{]*){//; 127322f2a2efSAndy Whitcroft my $before = $1; 127422f2a2efSAndy Whitcroft $stmt =~ s/}([^}]*$)//; 127522f2a2efSAndy Whitcroft my $after = $1; 1276f0a594c1SAndy Whitcroft 1277f0a594c1SAndy Whitcroft #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; 1278f0a594c1SAndy Whitcroft #print "stmt<$stmt>\n\n"; 1279f0a594c1SAndy Whitcroft 1280f0a594c1SAndy Whitcroft # Count the ;'s if there is fewer than two 1281f0a594c1SAndy Whitcroft # then there can only be one statement, 1282f0a594c1SAndy Whitcroft # if there is a brace inside we cannot 1283f0a594c1SAndy Whitcroft # trivially detect if its one statement. 1284f0a594c1SAndy Whitcroft # Also nested if's often require braces to 1285f0a594c1SAndy Whitcroft # disambiguate the else binding so shhh there. 1286f0a594c1SAndy Whitcroft my @semi = ($stmt =~ /;/g); 128722f2a2efSAndy Whitcroft push(@semi, "/**/") if ($stmt =~ m@/\*@); 1288f0a594c1SAndy Whitcroft ##print "semi<" . scalar(@semi) . ">\n"; 1289f0a594c1SAndy Whitcroft if ($lvl == 0 && scalar(@semi) < 2 && 129022f2a2efSAndy Whitcroft $stmt !~ /{/ && $stmt !~ /\bif\b/ && 129122f2a2efSAndy Whitcroft $before !~ /}/ && $after !~ /{/) { 1292f0a594c1SAndy Whitcroft my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; 1293f0a594c1SAndy Whitcroft shift(@block); 129422f2a2efSAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 1295f0a594c1SAndy Whitcroft } 1296f0a594c1SAndy Whitcroft } 1297f0a594c1SAndy Whitcroft } 1298f0a594c1SAndy Whitcroft 1299653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 13004a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 1301653d4876SAndy Whitcroft if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 1302de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 13030a920b5bSAndy Whitcroft } 13040a920b5bSAndy Whitcroft } 13050a920b5bSAndy Whitcroft 13064a0df2efSAndy Whitcroft# don't use deprecated functions 13074a0df2efSAndy Whitcroft for my $func (@dep_functions) { 130800df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 1309de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 13104a0df2efSAndy Whitcroft } 13114a0df2efSAndy Whitcroft } 13124a0df2efSAndy Whitcroft 13134a0df2efSAndy Whitcroft# no volatiles please 1314*6c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 1315*6c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 1316de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 13174a0df2efSAndy Whitcroft } 13184a0df2efSAndy Whitcroft 13199c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 13209c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 13219c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 13229c0ca6f9SAndy Whitcroft } 13239c0ca6f9SAndy Whitcroft 132400df344fSAndy Whitcroft# warn about #if 0 132500df344fSAndy Whitcroft if ($line =~ /^.#\s*if\s+0\b/) { 1326de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 1327de7d4f0eSAndy Whitcroft $herecurr); 13284a0df2efSAndy Whitcroft } 13294a0df2efSAndy Whitcroft 1330f0a594c1SAndy Whitcroft# check for needless kfree() checks 1331f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 1332f0a594c1SAndy Whitcroft my $expr = $1; 1333f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 1334f0a594c1SAndy Whitcroft WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); 1335f0a594c1SAndy Whitcroft } 1336f0a594c1SAndy Whitcroft } 1337f0a594c1SAndy Whitcroft 133800df344fSAndy Whitcroft# warn about #ifdefs in C files 133900df344fSAndy Whitcroft# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 134000df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 134100df344fSAndy Whitcroft# print "$herecurr"; 134200df344fSAndy Whitcroft# $clean = 0; 134300df344fSAndy Whitcroft# } 134400df344fSAndy Whitcroft 134522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 134622f2a2efSAndy Whitcroft if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { 134722f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 134822f2a2efSAndy Whitcroft } 134922f2a2efSAndy Whitcroft 13504a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 13514a0df2efSAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { 13524a0df2efSAndy Whitcroft my $which = $1; 13534a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 1354de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 13554a0df2efSAndy Whitcroft } 13564a0df2efSAndy Whitcroft } 13574a0df2efSAndy Whitcroft# check for memory barriers without a comment. 13584a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 13594a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 1360de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 13614a0df2efSAndy Whitcroft } 13624a0df2efSAndy Whitcroft } 13634a0df2efSAndy Whitcroft# check of hardware specific defines 136422f2a2efSAndy Whitcroft if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 1365de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 13660a920b5bSAndy Whitcroft } 1367653d4876SAndy Whitcroft 1368de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 1369de7d4f0eSAndy Whitcroft# storage class and type. 13709c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 13719c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 1372de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 1373de7d4f0eSAndy Whitcroft } 1374de7d4f0eSAndy Whitcroft 1375de7d4f0eSAndy Whitcroft# check for new externs in .c files. 1376de7d4f0eSAndy Whitcroft if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { 1377de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 1378de7d4f0eSAndy Whitcroft } 1379de7d4f0eSAndy Whitcroft 1380de7d4f0eSAndy Whitcroft# checks for new __setup's 1381de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 1382de7d4f0eSAndy Whitcroft my $name = $1; 1383de7d4f0eSAndy Whitcroft 1384de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 1385de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 1386de7d4f0eSAndy Whitcroft } 1387653d4876SAndy Whitcroft } 13889c0ca6f9SAndy Whitcroft 13899c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 13909c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 13919c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 13929c0ca6f9SAndy Whitcroft } 13930a920b5bSAndy Whitcroft } 13940a920b5bSAndy Whitcroft 13950a920b5bSAndy Whitcroft if ($chk_patch && !$is_patch) { 1396de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 13970a920b5bSAndy Whitcroft } 13980a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 1399de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 14000a920b5bSAndy Whitcroft } 14010a920b5bSAndy Whitcroft 1402f0a594c1SAndy Whitcroft if ($clean == 0 && ($chk_patch || $is_patch)) { 1403f0a594c1SAndy Whitcroft print report_dump(); 1404*6c72ffaaSAndy Whitcroft if ($quiet < 2) { 1405*6c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 1406*6c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 1407*6c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 1408*6c72ffaaSAndy Whitcroft } 1409f0a594c1SAndy Whitcroft } 14100a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 14110a920b5bSAndy Whitcroft print "Your patch has no obvious style problems and is ready for submission.\n" 14120a920b5bSAndy Whitcroft } 14130a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 14140a920b5bSAndy Whitcroft print "Your patch has style problems, please review. If any of these errors\n"; 14150a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 14160a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 14170a920b5bSAndy Whitcroft } 14180a920b5bSAndy Whitcroft return $clean; 14190a920b5bSAndy Whitcroft} 1420