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 126cbb2e71SAndy Whitcroftmy $V = '0.20'; 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; 21773647a0SAndy Whitcroftmy $tst_only; 226c72ffaaSAndy Whitcroftmy $emacs = 0; 238905a67cSAndy Whitcroftmy $terse = 0; 246c72ffaaSAndy Whitcroftmy $file = 0; 256c72ffaaSAndy Whitcroftmy $check = 0; 268905a67cSAndy Whitcroftmy $summary = 1; 278905a67cSAndy Whitcroftmy $mailback = 0; 2813214adfSAndy Whitcroftmy $summary_file = 0; 296c72ffaaSAndy Whitcroftmy $root; 30c2fdda0dSAndy Whitcroftmy %debug; 310a920b5bSAndy WhitcroftGetOptions( 326c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 330a920b5bSAndy Whitcroft 'tree!' => \$tree, 340a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 350a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 366c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 378905a67cSAndy Whitcroft 'terse!' => \$terse, 386c72ffaaSAndy Whitcroft 'file!' => \$file, 396c72ffaaSAndy Whitcroft 'subjective!' => \$check, 406c72ffaaSAndy Whitcroft 'strict!' => \$check, 416c72ffaaSAndy Whitcroft 'root=s' => \$root, 428905a67cSAndy Whitcroft 'summary!' => \$summary, 438905a67cSAndy Whitcroft 'mailback!' => \$mailback, 4413214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 4513214adfSAndy Whitcroft 46c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 4713214adfSAndy Whitcroft 'test-type!' => \$tst_type, 48773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 490a920b5bSAndy Whitcroft) or exit; 500a920b5bSAndy Whitcroft 510a920b5bSAndy Whitcroftmy $exit = 0; 520a920b5bSAndy Whitcroft 530a920b5bSAndy Whitcroftif ($#ARGV < 0) { 5400df344fSAndy Whitcroft print "usage: $P [options] patchfile\n"; 550a920b5bSAndy Whitcroft print "version: $V\n"; 560a920b5bSAndy Whitcroft print "options: -q => quiet\n"; 570a920b5bSAndy Whitcroft print " --no-tree => run without a kernel tree\n"; 588905a67cSAndy Whitcroft print " --terse => one line per report\n"; 596c72ffaaSAndy Whitcroft print " --emacs => emacs compile window format\n"; 606c72ffaaSAndy Whitcroft print " --file => check a source file\n"; 616c72ffaaSAndy Whitcroft print " --strict => enable more subjective tests\n"; 626c72ffaaSAndy Whitcroft print " --root => path to the kernel tree root\n"; 6313214adfSAndy Whitcroft print " --no-summary => suppress the per-file summary\n"; 6413214adfSAndy Whitcroft print " --summary-file => include the filename in summary\n"; 650a920b5bSAndy Whitcroft exit(1); 660a920b5bSAndy Whitcroft} 670a920b5bSAndy Whitcroft 68c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 69c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 70c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 71c2fdda0dSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';" 72c2fdda0dSAndy Whitcroft} 73c2fdda0dSAndy Whitcroft 748905a67cSAndy Whitcroftif ($terse) { 758905a67cSAndy Whitcroft $emacs = 1; 768905a67cSAndy Whitcroft $quiet++; 778905a67cSAndy Whitcroft} 788905a67cSAndy Whitcroft 796c72ffaaSAndy Whitcroftif ($tree) { 806c72ffaaSAndy Whitcroft if (defined $root) { 816c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 826c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 836c72ffaaSAndy Whitcroft } 846c72ffaaSAndy Whitcroft } else { 856c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 866c72ffaaSAndy Whitcroft $root = '.'; 876c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 886c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 896c72ffaaSAndy Whitcroft $root = $1; 906c72ffaaSAndy Whitcroft } 916c72ffaaSAndy Whitcroft } 926c72ffaaSAndy Whitcroft 936c72ffaaSAndy Whitcroft if (!defined $root) { 940a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 950a920b5bSAndy Whitcroft exit(2); 960a920b5bSAndy Whitcroft } 976c72ffaaSAndy Whitcroft} 986c72ffaaSAndy Whitcroft 996c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 1006c72ffaaSAndy Whitcroft 1016c72ffaaSAndy Whitcroftour $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; 1026c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 1036c72ffaaSAndy Whitcroftour $Sparse = qr{ 1046c72ffaaSAndy Whitcroft __user| 1056c72ffaaSAndy Whitcroft __kernel| 1066c72ffaaSAndy Whitcroft __force| 1076c72ffaaSAndy Whitcroft __iomem| 1086c72ffaaSAndy Whitcroft __must_check| 1096c72ffaaSAndy Whitcroft __init_refok| 110cf655043SAndy Whitcroft __kprobes 1116c72ffaaSAndy Whitcroft }x; 1126c72ffaaSAndy Whitcroftour $Attribute = qr{ 1136c72ffaaSAndy Whitcroft const| 1146c72ffaaSAndy Whitcroft __read_mostly| 1156c72ffaaSAndy Whitcroft __kprobes| 1166c72ffaaSAndy Whitcroft __(?:mem|cpu|dev|)(?:initdata|init) 1176c72ffaaSAndy Whitcroft }x; 118c45dcabdSAndy Whitcroftour $Modifier; 1196c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 1206c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 1216c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 1226c72ffaaSAndy Whitcroft 1236c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 1246c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 1256c72ffaaSAndy Whitcroftour $Operators = qr{ 1266c72ffaaSAndy Whitcroft <=|>=|==|!=| 1276c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 128c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1296c72ffaaSAndy Whitcroft }x; 1306c72ffaaSAndy Whitcroft 1318905a67cSAndy Whitcroftour $NonptrType; 1328905a67cSAndy Whitcroftour $Type; 1338905a67cSAndy Whitcroftour $Declare; 1348905a67cSAndy Whitcroft 135171ae1a4SAndy Whitcroftour $UTF8 = qr { 136171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 137171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 138171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 139171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 140171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 141171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 142171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 143171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 144171ae1a4SAndy Whitcroft}x; 145171ae1a4SAndy Whitcroft 1468905a67cSAndy Whitcroftour @typeList = ( 1478905a67cSAndy Whitcroft qr{void}, 148c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 149c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 150c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 151c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 152c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 153c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 154c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 1558905a67cSAndy Whitcroft qr{unsigned}, 1568905a67cSAndy Whitcroft qr{float}, 1578905a67cSAndy Whitcroft qr{double}, 1588905a67cSAndy Whitcroft qr{bool}, 1598905a67cSAndy Whitcroft qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)}, 1608905a67cSAndy Whitcroft qr{struct\s+$Ident}, 1618905a67cSAndy Whitcroft qr{union\s+$Ident}, 1628905a67cSAndy Whitcroft qr{enum\s+$Ident}, 1638905a67cSAndy Whitcroft qr{${Ident}_t}, 1648905a67cSAndy Whitcroft qr{${Ident}_handler}, 1658905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 1668905a67cSAndy Whitcroft); 167c45dcabdSAndy Whitcroftour @modifierList = ( 168c45dcabdSAndy Whitcroft qr{fastcall}, 169c45dcabdSAndy Whitcroft); 1708905a67cSAndy Whitcroft 1718905a67cSAndy Whitcroftsub build_types { 172c45dcabdSAndy Whitcroft my $mods = "(?: \n" . join("|\n ", @modifierList) . "\n)"; 1738905a67cSAndy Whitcroft my $all = "(?: \n" . join("|\n ", @typeList) . "\n)"; 174c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 1758905a67cSAndy Whitcroft $NonptrType = qr{ 1768905a67cSAndy Whitcroft (?:const\s+)? 177c45dcabdSAndy Whitcroft (?:$mods\s+)? 178cf655043SAndy Whitcroft (?: 179c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 180c45dcabdSAndy Whitcroft (?:${all}\b) 181cf655043SAndy Whitcroft ) 182c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 1838905a67cSAndy Whitcroft }x; 1848905a67cSAndy Whitcroft $Type = qr{ 185c45dcabdSAndy Whitcroft $NonptrType 1868905a67cSAndy Whitcroft (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? 187c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 1888905a67cSAndy Whitcroft }x; 1898905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 1908905a67cSAndy Whitcroft} 1918905a67cSAndy Whitcroftbuild_types(); 1926c72ffaaSAndy Whitcroft 1936c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 1940a920b5bSAndy Whitcroft 1954a0df2efSAndy Whitcroftmy @dep_includes = (); 1964a0df2efSAndy Whitcroftmy @dep_functions = (); 1976c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 1986c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 1996c72ffaaSAndy Whitcroft open(REMOVE, "<$root/$removal") || 2006c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 2010a920b5bSAndy Whitcroft while (<REMOVE>) { 202f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 203f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 204f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 2054a0df2efSAndy Whitcroft push(@dep_includes, $1); 2064a0df2efSAndy Whitcroft 207f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 208f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 209f0a594c1SAndy Whitcroft } 2104a0df2efSAndy Whitcroft } 2110a920b5bSAndy Whitcroft } 2120a920b5bSAndy Whitcroft } 2130a920b5bSAndy Whitcroft} 2140a920b5bSAndy Whitcroft 21500df344fSAndy Whitcroftmy @rawlines = (); 216c2fdda0dSAndy Whitcroftmy @lines = (); 217c2fdda0dSAndy Whitcroftmy $vname; 2186c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 2196c72ffaaSAndy Whitcroft if ($file) { 2206c72ffaaSAndy Whitcroft open(FILE, "diff -u /dev/null $filename|") || 2216c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 2226c72ffaaSAndy Whitcroft } else { 2236c72ffaaSAndy Whitcroft open(FILE, "<$filename") || 2246c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 2256c72ffaaSAndy Whitcroft } 226c2fdda0dSAndy Whitcroft if ($filename eq '-') { 227c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 228c2fdda0dSAndy Whitcroft } else { 229c2fdda0dSAndy Whitcroft $vname = $filename; 230c2fdda0dSAndy Whitcroft } 2316c72ffaaSAndy Whitcroft while (<FILE>) { 2320a920b5bSAndy Whitcroft chomp; 23300df344fSAndy Whitcroft push(@rawlines, $_); 2346c72ffaaSAndy Whitcroft } 2356c72ffaaSAndy Whitcroft close(FILE); 236c2fdda0dSAndy Whitcroft if (!process($filename)) { 2370a920b5bSAndy Whitcroft $exit = 1; 2380a920b5bSAndy Whitcroft } 23900df344fSAndy Whitcroft @rawlines = (); 24013214adfSAndy Whitcroft @lines = (); 2410a920b5bSAndy Whitcroft} 2420a920b5bSAndy Whitcroft 2430a920b5bSAndy Whitcroftexit($exit); 2440a920b5bSAndy Whitcroft 2450a920b5bSAndy Whitcroftsub top_of_kernel_tree { 2466c72ffaaSAndy Whitcroft my ($root) = @_; 2476c72ffaaSAndy Whitcroft 2486c72ffaaSAndy Whitcroft my @tree_check = ( 2496c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 2506c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 2516c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 2526c72ffaaSAndy Whitcroft ); 2536c72ffaaSAndy Whitcroft 2546c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 2556c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 2560a920b5bSAndy Whitcroft return 0; 2570a920b5bSAndy Whitcroft } 2586c72ffaaSAndy Whitcroft } 2596c72ffaaSAndy Whitcroft return 1; 2606c72ffaaSAndy Whitcroft} 2610a920b5bSAndy Whitcroft 2620a920b5bSAndy Whitcroftsub expand_tabs { 2630a920b5bSAndy Whitcroft my ($str) = @_; 2640a920b5bSAndy Whitcroft 2650a920b5bSAndy Whitcroft my $res = ''; 2660a920b5bSAndy Whitcroft my $n = 0; 2670a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 2680a920b5bSAndy Whitcroft if ($c eq "\t") { 2690a920b5bSAndy Whitcroft $res .= ' '; 2700a920b5bSAndy Whitcroft $n++; 2710a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 2720a920b5bSAndy Whitcroft $res .= ' '; 2730a920b5bSAndy Whitcroft } 2740a920b5bSAndy Whitcroft next; 2750a920b5bSAndy Whitcroft } 2760a920b5bSAndy Whitcroft $res .= $c; 2770a920b5bSAndy Whitcroft $n++; 2780a920b5bSAndy Whitcroft } 2790a920b5bSAndy Whitcroft 2800a920b5bSAndy Whitcroft return $res; 2810a920b5bSAndy Whitcroft} 2826c72ffaaSAndy Whitcroftsub copy_spacing { 283773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 2846c72ffaaSAndy Whitcroft return $res; 2856c72ffaaSAndy Whitcroft} 2860a920b5bSAndy Whitcroft 2874a0df2efSAndy Whitcroftsub line_stats { 2884a0df2efSAndy Whitcroft my ($line) = @_; 2894a0df2efSAndy Whitcroft 2904a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 2914a0df2efSAndy Whitcroft $line =~ s/^.//; 2924a0df2efSAndy Whitcroft $line = expand_tabs($line); 2934a0df2efSAndy Whitcroft 2944a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 2954a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 2964a0df2efSAndy Whitcroft 2974a0df2efSAndy Whitcroft return (length($line), length($white)); 2984a0df2efSAndy Whitcroft} 2994a0df2efSAndy Whitcroft 300773647a0SAndy Whitcroftmy $sanitise_quote = ''; 301773647a0SAndy Whitcroft 302773647a0SAndy Whitcroftsub sanitise_line_reset { 303773647a0SAndy Whitcroft my ($in_comment) = @_; 304773647a0SAndy Whitcroft 305773647a0SAndy Whitcroft if ($in_comment) { 306773647a0SAndy Whitcroft $sanitise_quote = '*/'; 307773647a0SAndy Whitcroft } else { 308773647a0SAndy Whitcroft $sanitise_quote = ''; 309773647a0SAndy Whitcroft } 310773647a0SAndy Whitcroft} 31100df344fSAndy Whitcroftsub sanitise_line { 31200df344fSAndy Whitcroft my ($line) = @_; 31300df344fSAndy Whitcroft 31400df344fSAndy Whitcroft my $res = ''; 31500df344fSAndy Whitcroft my $l = ''; 31600df344fSAndy Whitcroft 317c2fdda0dSAndy Whitcroft my $qlen = 0; 318773647a0SAndy Whitcroft my $off = 0; 319773647a0SAndy Whitcroft my $c; 32000df344fSAndy Whitcroft 321773647a0SAndy Whitcroft # Always copy over the diff marker. 322773647a0SAndy Whitcroft $res = substr($line, 0, 1); 323773647a0SAndy Whitcroft 324773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 325773647a0SAndy Whitcroft $c = substr($line, $off, 1); 326773647a0SAndy Whitcroft 327773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 328773647a0SAndy Whitcroft # and end, all to $;. 329773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 330773647a0SAndy Whitcroft $sanitise_quote = '*/'; 331773647a0SAndy Whitcroft 332773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 333773647a0SAndy Whitcroft $off++; 33400df344fSAndy Whitcroft next; 335773647a0SAndy Whitcroft } 336c45dcabdSAndy Whitcroft if (substr($line, $off, 2) eq '*/') { 337773647a0SAndy Whitcroft $sanitise_quote = ''; 338773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 339773647a0SAndy Whitcroft $off++; 340773647a0SAndy Whitcroft next; 341773647a0SAndy Whitcroft } 342773647a0SAndy Whitcroft 343773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 344773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 345773647a0SAndy Whitcroft $c eq "\\") { 346773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 347773647a0SAndy Whitcroft $off++; 348773647a0SAndy Whitcroft next; 349773647a0SAndy Whitcroft } 350773647a0SAndy Whitcroft # Regular quotes. 351773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 352773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 353773647a0SAndy Whitcroft $sanitise_quote = $c; 354773647a0SAndy Whitcroft 355773647a0SAndy Whitcroft substr($res, $off, 1, $c); 356773647a0SAndy Whitcroft next; 357773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 358773647a0SAndy Whitcroft $sanitise_quote = ''; 35900df344fSAndy Whitcroft } 36000df344fSAndy Whitcroft } 361773647a0SAndy Whitcroft 362773647a0SAndy Whitcroft #print "SQ:$sanitise_quote\n"; 363773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 364773647a0SAndy Whitcroft substr($res, $off, 1, $;); 365773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 366773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 36700df344fSAndy Whitcroft } else { 368773647a0SAndy Whitcroft substr($res, $off, 1, $c); 36900df344fSAndy Whitcroft } 370c2fdda0dSAndy Whitcroft } 371c2fdda0dSAndy Whitcroft 372c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 373c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 374c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 375c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 376c2fdda0dSAndy Whitcroft 377c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 378c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 379c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 380c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 381c2fdda0dSAndy Whitcroft } 382c2fdda0dSAndy Whitcroft 38300df344fSAndy Whitcroft return $res; 38400df344fSAndy Whitcroft} 38500df344fSAndy Whitcroft 3868905a67cSAndy Whitcroftsub ctx_statement_block { 3878905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 3888905a67cSAndy Whitcroft my $line = $linenr - 1; 3898905a67cSAndy Whitcroft my $blk = ''; 3908905a67cSAndy Whitcroft my $soff = $off; 3918905a67cSAndy Whitcroft my $coff = $off - 1; 392773647a0SAndy Whitcroft my $coff_set = 0; 3938905a67cSAndy Whitcroft 39413214adfSAndy Whitcroft my $loff = 0; 39513214adfSAndy Whitcroft 3968905a67cSAndy Whitcroft my $type = ''; 3978905a67cSAndy Whitcroft my $level = 0; 398cf655043SAndy Whitcroft my $p; 3998905a67cSAndy Whitcroft my $c; 4008905a67cSAndy Whitcroft my $len = 0; 40113214adfSAndy Whitcroft 40213214adfSAndy Whitcroft my $remainder; 4038905a67cSAndy Whitcroft while (1) { 404773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 4058905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 4068905a67cSAndy Whitcroft # context. 4078905a67cSAndy Whitcroft if ($off >= $len) { 4088905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 409c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 4108905a67cSAndy Whitcroft $remain--; 41113214adfSAndy Whitcroft $loff = $len; 412c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 4138905a67cSAndy Whitcroft $len = length($blk); 4148905a67cSAndy Whitcroft $line++; 4158905a67cSAndy Whitcroft last; 4168905a67cSAndy Whitcroft } 4178905a67cSAndy Whitcroft # Bail if there is no further context. 4188905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 41913214adfSAndy Whitcroft if ($off >= $len) { 4208905a67cSAndy Whitcroft last; 4218905a67cSAndy Whitcroft } 4228905a67cSAndy Whitcroft } 423cf655043SAndy Whitcroft $p = $c; 4248905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 42513214adfSAndy Whitcroft $remainder = substr($blk, $off); 4268905a67cSAndy Whitcroft 427773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 4288905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 4298905a67cSAndy Whitcroft # outermost level. 4308905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 4318905a67cSAndy Whitcroft last; 4328905a67cSAndy Whitcroft } 4338905a67cSAndy Whitcroft 43413214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 435773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 436773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 437773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 438773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 439773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 440773647a0SAndy Whitcroft $coff_set = 1; 441773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 442773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 44313214adfSAndy Whitcroft } 44413214adfSAndy Whitcroft 4458905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 4468905a67cSAndy Whitcroft $level++; 4478905a67cSAndy Whitcroft $type = '('; 4488905a67cSAndy Whitcroft } 4498905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 4508905a67cSAndy Whitcroft $level--; 4518905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 4528905a67cSAndy Whitcroft 4538905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 4548905a67cSAndy Whitcroft $coff = $off; 455773647a0SAndy Whitcroft $coff_set = 1; 456773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 4578905a67cSAndy Whitcroft } 4588905a67cSAndy Whitcroft } 4598905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 4608905a67cSAndy Whitcroft $level++; 4618905a67cSAndy Whitcroft $type = '{'; 4628905a67cSAndy Whitcroft } 4638905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 4648905a67cSAndy Whitcroft $level--; 4658905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 4668905a67cSAndy Whitcroft 4678905a67cSAndy Whitcroft if ($level == 0) { 4688905a67cSAndy Whitcroft last; 4698905a67cSAndy Whitcroft } 4708905a67cSAndy Whitcroft } 4718905a67cSAndy Whitcroft $off++; 4728905a67cSAndy Whitcroft } 473a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 47413214adfSAndy Whitcroft if ($off == $len) { 475a3bb97a7SAndy Whitcroft $loff = $len + 1; 47613214adfSAndy Whitcroft $line++; 47713214adfSAndy Whitcroft $remain--; 47813214adfSAndy Whitcroft } 4798905a67cSAndy Whitcroft 4808905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 4818905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 4828905a67cSAndy Whitcroft 4838905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 4848905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 4858905a67cSAndy Whitcroft 486773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 48713214adfSAndy Whitcroft 48813214adfSAndy Whitcroft return ($statement, $condition, 48913214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 49013214adfSAndy Whitcroft} 49113214adfSAndy Whitcroft 492cf655043SAndy Whitcroftsub statement_lines { 493cf655043SAndy Whitcroft my ($stmt) = @_; 494cf655043SAndy Whitcroft 495cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 496cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 497cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 498cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 499cf655043SAndy Whitcroft 500cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 501cf655043SAndy Whitcroft 502cf655043SAndy Whitcroft return $#stmt_lines + 2; 503cf655043SAndy Whitcroft} 504cf655043SAndy Whitcroft 505cf655043SAndy Whitcroftsub statement_rawlines { 506cf655043SAndy Whitcroft my ($stmt) = @_; 507cf655043SAndy Whitcroft 508cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 509cf655043SAndy Whitcroft 510cf655043SAndy Whitcroft return $#stmt_lines + 2; 511cf655043SAndy Whitcroft} 512cf655043SAndy Whitcroft 513cf655043SAndy Whitcroftsub statement_block_size { 514cf655043SAndy Whitcroft my ($stmt) = @_; 515cf655043SAndy Whitcroft 516cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 517cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 518cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 519cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 520cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 521cf655043SAndy Whitcroft 522cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 523cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 524cf655043SAndy Whitcroft 525cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 526cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 527cf655043SAndy Whitcroft 528cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 529cf655043SAndy Whitcroft return $stmt_lines; 530cf655043SAndy Whitcroft } else { 531cf655043SAndy Whitcroft return $stmt_statements; 532cf655043SAndy Whitcroft } 533cf655043SAndy Whitcroft} 534cf655043SAndy Whitcroft 53513214adfSAndy Whitcroftsub ctx_statement_full { 53613214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 53713214adfSAndy Whitcroft my ($statement, $condition, $level); 53813214adfSAndy Whitcroft 53913214adfSAndy Whitcroft my (@chunks); 54013214adfSAndy Whitcroft 541cf655043SAndy Whitcroft # Grab the first conditional/block pair. 54213214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 54313214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 544773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 54513214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 546cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 547cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 548cf655043SAndy Whitcroft } 549cf655043SAndy Whitcroft 550cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 551cf655043SAndy Whitcroft # could continue the statement. 552cf655043SAndy Whitcroft for (;;) { 55313214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 55413214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 555cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 556773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 557cf655043SAndy Whitcroft #print "C: push\n"; 558cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 55913214adfSAndy Whitcroft } 56013214adfSAndy Whitcroft 56113214adfSAndy Whitcroft return ($level, $linenr, @chunks); 5628905a67cSAndy Whitcroft} 5638905a67cSAndy Whitcroft 5644a0df2efSAndy Whitcroftsub ctx_block_get { 565f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 5664a0df2efSAndy Whitcroft my $line; 5674a0df2efSAndy Whitcroft my $start = $linenr - 1; 5684a0df2efSAndy Whitcroft my $blk = ''; 5694a0df2efSAndy Whitcroft my @o; 5704a0df2efSAndy Whitcroft my @c; 5714a0df2efSAndy Whitcroft my @res = (); 5724a0df2efSAndy Whitcroft 573f0a594c1SAndy Whitcroft my $level = 0; 57400df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 57500df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 57600df344fSAndy Whitcroft $remain--; 57700df344fSAndy Whitcroft 57800df344fSAndy Whitcroft $blk .= $rawlines[$line]; 579f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$line])) { 580f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 581f0a594c1SAndy Whitcroft if ($off > 0) { 582f0a594c1SAndy Whitcroft $off--; 583f0a594c1SAndy Whitcroft next; 584f0a594c1SAndy Whitcroft } 5854a0df2efSAndy Whitcroft 586f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 587f0a594c1SAndy Whitcroft $level--; 588f0a594c1SAndy Whitcroft last if ($level == 0); 589f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 590f0a594c1SAndy Whitcroft $level++; 591f0a594c1SAndy Whitcroft } 592f0a594c1SAndy Whitcroft } 5934a0df2efSAndy Whitcroft 594f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 59500df344fSAndy Whitcroft push(@res, $rawlines[$line]); 5964a0df2efSAndy Whitcroft } 5974a0df2efSAndy Whitcroft 598f0a594c1SAndy Whitcroft last if ($level == 0); 5994a0df2efSAndy Whitcroft } 6004a0df2efSAndy Whitcroft 601f0a594c1SAndy Whitcroft return ($level, @res); 6024a0df2efSAndy Whitcroft} 6034a0df2efSAndy Whitcroftsub ctx_block_outer { 6044a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6054a0df2efSAndy Whitcroft 606f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 607f0a594c1SAndy Whitcroft return @r; 6084a0df2efSAndy Whitcroft} 6094a0df2efSAndy Whitcroftsub ctx_block { 6104a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6114a0df2efSAndy Whitcroft 612f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 613f0a594c1SAndy Whitcroft return @r; 614653d4876SAndy Whitcroft} 615653d4876SAndy Whitcroftsub ctx_statement { 616f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 617f0a594c1SAndy Whitcroft 618f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 619f0a594c1SAndy Whitcroft return @r; 620f0a594c1SAndy Whitcroft} 621f0a594c1SAndy Whitcroftsub ctx_block_level { 622653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 623653d4876SAndy Whitcroft 624f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 6254a0df2efSAndy Whitcroft} 6269c0ca6f9SAndy Whitcroftsub ctx_statement_level { 6279c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 6289c0ca6f9SAndy Whitcroft 6299c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 6309c0ca6f9SAndy Whitcroft} 6314a0df2efSAndy Whitcroft 6324a0df2efSAndy Whitcroftsub ctx_locate_comment { 6334a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 6344a0df2efSAndy Whitcroft 6354a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 636beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 6374a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 6384a0df2efSAndy Whitcroft 6394a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 6404a0df2efSAndy Whitcroft # comment. 6414a0df2efSAndy Whitcroft my $in_comment = 0; 6424a0df2efSAndy Whitcroft $current_comment = ''; 6434a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 64400df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 64500df344fSAndy Whitcroft #warn " $line\n"; 6464a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 6474a0df2efSAndy Whitcroft $in_comment = 1; 6484a0df2efSAndy Whitcroft } 6494a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 6504a0df2efSAndy Whitcroft $in_comment = 1; 6514a0df2efSAndy Whitcroft } 6524a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 6534a0df2efSAndy Whitcroft $current_comment = ''; 6544a0df2efSAndy Whitcroft } 6554a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 6564a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 6574a0df2efSAndy Whitcroft $in_comment = 0; 6584a0df2efSAndy Whitcroft } 6594a0df2efSAndy Whitcroft } 6604a0df2efSAndy Whitcroft 6614a0df2efSAndy Whitcroft chomp($current_comment); 6624a0df2efSAndy Whitcroft return($current_comment); 6634a0df2efSAndy Whitcroft} 6644a0df2efSAndy Whitcroftsub ctx_has_comment { 6654a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 6664a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 6674a0df2efSAndy Whitcroft 66800df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 6694a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 6704a0df2efSAndy Whitcroft 6714a0df2efSAndy Whitcroft return ($cmt ne ''); 6724a0df2efSAndy Whitcroft} 6734a0df2efSAndy Whitcroft 6740a920b5bSAndy Whitcroftsub cat_vet { 6750a920b5bSAndy Whitcroft my ($vet) = @_; 6769c0ca6f9SAndy Whitcroft my ($res, $coded); 6770a920b5bSAndy Whitcroft 6789c0ca6f9SAndy Whitcroft $res = ''; 6796c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 6806c72ffaaSAndy Whitcroft $res .= $1; 6816c72ffaaSAndy Whitcroft if ($2 ne '') { 6829c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 6836c72ffaaSAndy Whitcroft $res .= $coded; 6846c72ffaaSAndy Whitcroft } 6859c0ca6f9SAndy Whitcroft } 6869c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 6870a920b5bSAndy Whitcroft 6889c0ca6f9SAndy Whitcroft return $res; 6890a920b5bSAndy Whitcroft} 6900a920b5bSAndy Whitcroft 691c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 692cf655043SAndy Whitcroftmy $av_pending; 693c2fdda0dSAndy Whitcroftmy @av_paren_type; 694c2fdda0dSAndy Whitcroft 695c2fdda0dSAndy Whitcroftsub annotate_reset { 696c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 697cf655043SAndy Whitcroft $av_pending = '_'; 698cf655043SAndy Whitcroft @av_paren_type = ('E'); 699c2fdda0dSAndy Whitcroft} 700c2fdda0dSAndy Whitcroft 7016c72ffaaSAndy Whitcroftsub annotate_values { 7026c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 7036c72ffaaSAndy Whitcroft 7046c72ffaaSAndy Whitcroft my $res; 7056c72ffaaSAndy Whitcroft my $cur = $stream; 7066c72ffaaSAndy Whitcroft 707c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 7086c72ffaaSAndy Whitcroft 7096c72ffaaSAndy Whitcroft while (length($cur)) { 710773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 711cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 712171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 7136c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 714c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 715c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 716cf655043SAndy Whitcroft $type = pop(@av_paren_type); 717c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 7186c72ffaaSAndy Whitcroft } 7196c72ffaaSAndy Whitcroft 720c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\))/) { 721c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 7226c72ffaaSAndy Whitcroft $type = 'T'; 7236c72ffaaSAndy Whitcroft 724c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 725171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 726c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 727171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 728171ae1a4SAndy Whitcroft if ($2 ne '') { 729cf655043SAndy Whitcroft $av_pending = 'N'; 730171ae1a4SAndy Whitcroft } 731171ae1a4SAndy Whitcroft $type = 'E'; 732171ae1a4SAndy Whitcroft 733c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 734171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 735171ae1a4SAndy Whitcroft $av_preprocessor = 1; 736171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 7376c72ffaaSAndy Whitcroft 738c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 739cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 740c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 741cf655043SAndy Whitcroft 742cf655043SAndy Whitcroft push(@av_paren_type, $type); 743cf655043SAndy Whitcroft push(@av_paren_type, $type); 744171ae1a4SAndy Whitcroft $type = 'E'; 745cf655043SAndy Whitcroft 746c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 747cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 748cf655043SAndy Whitcroft $av_preprocessor = 1; 749cf655043SAndy Whitcroft 750cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 751cf655043SAndy Whitcroft 752171ae1a4SAndy Whitcroft $type = 'E'; 753cf655043SAndy Whitcroft 754c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 755cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 756cf655043SAndy Whitcroft 757cf655043SAndy Whitcroft $av_preprocessor = 1; 758cf655043SAndy Whitcroft 759cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 760cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 761cf655043SAndy Whitcroft pop(@av_paren_type); 762cf655043SAndy Whitcroft push(@av_paren_type, $type); 763171ae1a4SAndy Whitcroft $type = 'E'; 7646c72ffaaSAndy Whitcroft 7656c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 766c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 7676c72ffaaSAndy Whitcroft 768171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 769171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 770171ae1a4SAndy Whitcroft $av_pending = $type; 771171ae1a4SAndy Whitcroft $type = 'N'; 772171ae1a4SAndy Whitcroft 7736c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 774c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 7756c72ffaaSAndy Whitcroft if (defined $2) { 776cf655043SAndy Whitcroft $av_pending = 'V'; 7776c72ffaaSAndy Whitcroft } 7786c72ffaaSAndy Whitcroft $type = 'N'; 7796c72ffaaSAndy Whitcroft 78013214adfSAndy Whitcroft } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) { 781c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 782cf655043SAndy Whitcroft $av_pending = 'N'; 7836c72ffaaSAndy Whitcroft $type = 'N'; 7846c72ffaaSAndy Whitcroft 7856ef9b297SAndy Whitcroft } elsif ($cur =~/^(return|case|else|goto)/o) { 786c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 7876c72ffaaSAndy Whitcroft $type = 'N'; 7886c72ffaaSAndy Whitcroft 7896c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 790c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 791cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 792cf655043SAndy Whitcroft $av_pending = '_'; 7936c72ffaaSAndy Whitcroft $type = 'N'; 7946c72ffaaSAndy Whitcroft 7956c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 796cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 797cf655043SAndy Whitcroft if ($new_type ne '_') { 798cf655043SAndy Whitcroft $type = $new_type; 799c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 800c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 8016c72ffaaSAndy Whitcroft } else { 802c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 8036c72ffaaSAndy Whitcroft } 8046c72ffaaSAndy Whitcroft 805c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 806c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 807c8cb2ca3SAndy Whitcroft $type = 'V'; 808cf655043SAndy Whitcroft $av_pending = 'V'; 8096c72ffaaSAndy Whitcroft 8106c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 811c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 8126c72ffaaSAndy Whitcroft $type = 'V'; 8136c72ffaaSAndy Whitcroft 8146c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 815c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 8166c72ffaaSAndy Whitcroft $type = 'N'; 8176c72ffaaSAndy Whitcroft 818cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 819c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 82013214adfSAndy Whitcroft $type = 'E'; 82113214adfSAndy Whitcroft 822cf655043SAndy Whitcroft } elsif ($cur =~ /^(;|\?|:|\[)/o) { 82313214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 8246c72ffaaSAndy Whitcroft $type = 'N'; 8256c72ffaaSAndy Whitcroft 8266c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 827c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 8286c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 8296c72ffaaSAndy Whitcroft $type = 'N'; 8306c72ffaaSAndy Whitcroft } 8316c72ffaaSAndy Whitcroft 8326c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 833c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 8346c72ffaaSAndy Whitcroft } 8356c72ffaaSAndy Whitcroft if (defined $1) { 8366c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 8376c72ffaaSAndy Whitcroft $res .= $type x length($1); 8386c72ffaaSAndy Whitcroft } 8396c72ffaaSAndy Whitcroft } 8406c72ffaaSAndy Whitcroft 8416c72ffaaSAndy Whitcroft return $res; 8426c72ffaaSAndy Whitcroft} 8436c72ffaaSAndy Whitcroft 8448905a67cSAndy Whitcroftsub possible { 84513214adfSAndy Whitcroft my ($possible, $line) = @_; 8468905a67cSAndy Whitcroft 847c45dcabdSAndy Whitcroft print "CHECK<$possible> ($line)\n" if ($dbg_possible > 1); 8488905a67cSAndy Whitcroft if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && 8498905a67cSAndy Whitcroft $possible ne 'goto' && $possible ne 'return' && 8508905a67cSAndy Whitcroft $possible ne 'case' && $possible ne 'else' && 851d3ddcf47SAndy Whitcroft $possible ne 'asm' && $possible ne '__asm__' && 852c45dcabdSAndy Whitcroft $possible !~ /^(typedef|struct|enum)\b/) { 853c45dcabdSAndy Whitcroft # Check for modifiers. 854c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 855c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 856c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 857c45dcabdSAndy Whitcroft 858c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 859c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 860c45dcabdSAndy Whitcroft warn "MODIFIER: $possible ($line)\n" if ($dbg_possible); 861c45dcabdSAndy Whitcroft push(@modifierList, $possible); 862c45dcabdSAndy Whitcroft 863c45dcabdSAndy Whitcroft } else { 86413214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 8658905a67cSAndy Whitcroft push(@typeList, $possible); 866c45dcabdSAndy Whitcroft } 8678905a67cSAndy Whitcroft build_types(); 8688905a67cSAndy Whitcroft } 8698905a67cSAndy Whitcroft} 8708905a67cSAndy Whitcroft 8716c72ffaaSAndy Whitcroftmy $prefix = ''; 8726c72ffaaSAndy Whitcroft 873f0a594c1SAndy Whitcroftsub report { 874773647a0SAndy Whitcroft if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 875773647a0SAndy Whitcroft return 0; 876773647a0SAndy Whitcroft } 8778905a67cSAndy Whitcroft my $line = $prefix . $_[0]; 8788905a67cSAndy Whitcroft 8798905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 8808905a67cSAndy Whitcroft 88113214adfSAndy Whitcroft push(our @report, $line); 882773647a0SAndy Whitcroft 883773647a0SAndy Whitcroft return 1; 884f0a594c1SAndy Whitcroft} 885f0a594c1SAndy Whitcroftsub report_dump { 88613214adfSAndy Whitcroft our @report; 887f0a594c1SAndy Whitcroft} 888de7d4f0eSAndy Whitcroftsub ERROR { 889773647a0SAndy Whitcroft if (report("ERROR: $_[0]\n")) { 890de7d4f0eSAndy Whitcroft our $clean = 0; 8916c72ffaaSAndy Whitcroft our $cnt_error++; 892de7d4f0eSAndy Whitcroft } 893773647a0SAndy Whitcroft} 894de7d4f0eSAndy Whitcroftsub WARN { 895773647a0SAndy Whitcroft if (report("WARNING: $_[0]\n")) { 896de7d4f0eSAndy Whitcroft our $clean = 0; 8976c72ffaaSAndy Whitcroft our $cnt_warn++; 898de7d4f0eSAndy Whitcroft } 899773647a0SAndy Whitcroft} 900de7d4f0eSAndy Whitcroftsub CHK { 901773647a0SAndy Whitcroft if ($check && report("CHECK: $_[0]\n")) { 902de7d4f0eSAndy Whitcroft our $clean = 0; 9036c72ffaaSAndy Whitcroft our $cnt_chk++; 9046c72ffaaSAndy Whitcroft } 905de7d4f0eSAndy Whitcroft} 906de7d4f0eSAndy Whitcroft 9070a920b5bSAndy Whitcroftsub process { 9080a920b5bSAndy Whitcroft my $filename = shift; 9090a920b5bSAndy Whitcroft 9100a920b5bSAndy Whitcroft my $linenr=0; 9110a920b5bSAndy Whitcroft my $prevline=""; 912c2fdda0dSAndy Whitcroft my $prevrawline=""; 9130a920b5bSAndy Whitcroft my $stashline=""; 914c2fdda0dSAndy Whitcroft my $stashrawline=""; 9150a920b5bSAndy Whitcroft 9164a0df2efSAndy Whitcroft my $length; 9170a920b5bSAndy Whitcroft my $indent; 9180a920b5bSAndy Whitcroft my $previndent=0; 9190a920b5bSAndy Whitcroft my $stashindent=0; 9200a920b5bSAndy Whitcroft 921de7d4f0eSAndy Whitcroft our $clean = 1; 9220a920b5bSAndy Whitcroft my $signoff = 0; 9230a920b5bSAndy Whitcroft my $is_patch = 0; 9240a920b5bSAndy Whitcroft 92513214adfSAndy Whitcroft our @report = (); 9266c72ffaaSAndy Whitcroft our $cnt_lines = 0; 9276c72ffaaSAndy Whitcroft our $cnt_error = 0; 9286c72ffaaSAndy Whitcroft our $cnt_warn = 0; 9296c72ffaaSAndy Whitcroft our $cnt_chk = 0; 9306c72ffaaSAndy Whitcroft 9310a920b5bSAndy Whitcroft # Trace the real file/line as we go. 9320a920b5bSAndy Whitcroft my $realfile = ''; 9330a920b5bSAndy Whitcroft my $realline = 0; 9340a920b5bSAndy Whitcroft my $realcnt = 0; 9350a920b5bSAndy Whitcroft my $here = ''; 9360a920b5bSAndy Whitcroft my $in_comment = 0; 937c2fdda0dSAndy Whitcroft my $comment_edge = 0; 9380a920b5bSAndy Whitcroft my $first_line = 0; 9390a920b5bSAndy Whitcroft 94013214adfSAndy Whitcroft my $prev_values = 'E'; 94113214adfSAndy Whitcroft 94213214adfSAndy Whitcroft # suppression flags 943773647a0SAndy Whitcroft my %suppress_ifbraces; 944653d4876SAndy Whitcroft 945c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 946de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 947c2fdda0dSAndy Whitcroft # 948de7d4f0eSAndy Whitcroft my @setup_docs = (); 949de7d4f0eSAndy Whitcroft my $setup_docs = 0; 950773647a0SAndy Whitcroft 951773647a0SAndy Whitcroft sanitise_line_reset(); 952c2fdda0dSAndy Whitcroft my $line; 953c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 954773647a0SAndy Whitcroft $linenr++; 955773647a0SAndy Whitcroft $line = $rawline; 956c2fdda0dSAndy Whitcroft 957773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 958de7d4f0eSAndy Whitcroft $setup_docs = 0; 959de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 960de7d4f0eSAndy Whitcroft $setup_docs = 1; 961de7d4f0eSAndy Whitcroft } 962773647a0SAndy Whitcroft #next; 963de7d4f0eSAndy Whitcroft } 964773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 965773647a0SAndy Whitcroft $realline=$1-1; 966773647a0SAndy Whitcroft if (defined $2) { 967773647a0SAndy Whitcroft $realcnt=$3+1; 968773647a0SAndy Whitcroft } else { 969773647a0SAndy Whitcroft $realcnt=1+1; 970773647a0SAndy Whitcroft } 971c45dcabdSAndy Whitcroft $in_comment = 0; 972773647a0SAndy Whitcroft 973773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 974773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 975773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 976773647a0SAndy Whitcroft # at context start. 977773647a0SAndy Whitcroft my $edge; 978171ae1a4SAndy Whitcroft for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) { 979773647a0SAndy Whitcroft next if ($line =~ /^-/); 980773647a0SAndy Whitcroft ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); 981773647a0SAndy Whitcroft last if (defined $edge); 982773647a0SAndy Whitcroft } 983773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 984773647a0SAndy Whitcroft $in_comment = 1; 985773647a0SAndy Whitcroft } 986773647a0SAndy Whitcroft 987773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 988773647a0SAndy Whitcroft # is the start of a diff block and this line starts 989773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 990773647a0SAndy Whitcroft if (!defined $edge && 991773647a0SAndy Whitcroft $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@) 992773647a0SAndy Whitcroft { 993773647a0SAndy Whitcroft $in_comment = 1; 994773647a0SAndy Whitcroft } 995773647a0SAndy Whitcroft 996773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 997773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 998773647a0SAndy Whitcroft 999171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1000773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1001171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1002773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1003773647a0SAndy Whitcroft } 1004773647a0SAndy Whitcroft push(@lines, $line); 1005773647a0SAndy Whitcroft 1006773647a0SAndy Whitcroft if ($realcnt > 1) { 1007773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1008773647a0SAndy Whitcroft } else { 1009773647a0SAndy Whitcroft $realcnt = 0; 1010773647a0SAndy Whitcroft } 1011773647a0SAndy Whitcroft 1012773647a0SAndy Whitcroft #print "==>$rawline\n"; 1013773647a0SAndy Whitcroft #print "-->$line\n"; 1014de7d4f0eSAndy Whitcroft 1015de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1016de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1017de7d4f0eSAndy Whitcroft } 1018de7d4f0eSAndy Whitcroft } 1019de7d4f0eSAndy Whitcroft 10206c72ffaaSAndy Whitcroft $prefix = ''; 10216c72ffaaSAndy Whitcroft 1022773647a0SAndy Whitcroft $realcnt = 0; 1023773647a0SAndy Whitcroft $linenr = 0; 10240a920b5bSAndy Whitcroft foreach my $line (@lines) { 10250a920b5bSAndy Whitcroft $linenr++; 10260a920b5bSAndy Whitcroft 1027c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 10286c72ffaaSAndy Whitcroft 10290a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 10306c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 10310a920b5bSAndy Whitcroft $is_patch = 1; 10324a0df2efSAndy Whitcroft $first_line = $linenr + 1; 10330a920b5bSAndy Whitcroft $realline=$1-1; 10340a920b5bSAndy Whitcroft if (defined $2) { 10350a920b5bSAndy Whitcroft $realcnt=$3+1; 10360a920b5bSAndy Whitcroft } else { 10370a920b5bSAndy Whitcroft $realcnt=1+1; 10380a920b5bSAndy Whitcroft } 1039c2fdda0dSAndy Whitcroft annotate_reset(); 104013214adfSAndy Whitcroft $prev_values = 'E'; 104113214adfSAndy Whitcroft 1042773647a0SAndy Whitcroft %suppress_ifbraces = (); 10430a920b5bSAndy Whitcroft next; 10440a920b5bSAndy Whitcroft 10454a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 10464a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 10474a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1048773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 10490a920b5bSAndy Whitcroft $realline++; 1050d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 10510a920b5bSAndy Whitcroft 10524a0df2efSAndy Whitcroft # Measure the line length and indent. 1053c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 10540a920b5bSAndy Whitcroft 10550a920b5bSAndy Whitcroft # Track the previous line. 10560a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 10570a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1058c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1059c2fdda0dSAndy Whitcroft 1060773647a0SAndy Whitcroft #warn "line<$line>\n"; 10616c72ffaaSAndy Whitcroft 1062d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1063d8aaf121SAndy Whitcroft $realcnt--; 10640a920b5bSAndy Whitcroft } 10650a920b5bSAndy Whitcroft 10660a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1067773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1068773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1069773647a0SAndy Whitcroft 10706c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 10716c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1072773647a0SAndy Whitcroft 1073773647a0SAndy Whitcroft # extract the filename as it passes 1074773647a0SAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 1075773647a0SAndy Whitcroft $realfile = $1; 1076773647a0SAndy Whitcroft $realfile =~ s@^[^/]*/@@; 1077773647a0SAndy Whitcroft 1078773647a0SAndy Whitcroft if ($realfile =~ m@include/asm/@) { 1079773647a0SAndy Whitcroft ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1080773647a0SAndy Whitcroft } 1081773647a0SAndy Whitcroft next; 1082773647a0SAndy Whitcroft } 1083773647a0SAndy Whitcroft 1084389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 10850a920b5bSAndy Whitcroft 1086c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1087c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1088c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 10890a920b5bSAndy Whitcroft 10906c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 10916c72ffaaSAndy Whitcroft 10920a920b5bSAndy Whitcroft#check the patch for a signoff: 1093d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 10944a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 10954a0df2efSAndy Whitcroft $signoff++; 10960a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 1097de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 1098de7d4f0eSAndy Whitcroft $herecurr); 10990a920b5bSAndy Whitcroft } 11000a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 1101773647a0SAndy Whitcroft WARN("space required after Signed-off-by:\n" . 1102de7d4f0eSAndy Whitcroft $herecurr); 11030a920b5bSAndy Whitcroft } 11040a920b5bSAndy Whitcroft } 11050a920b5bSAndy Whitcroft 110600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 11078905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1108de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 11096c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1110de7d4f0eSAndy Whitcroft } 1111de7d4f0eSAndy Whitcroft 1112de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1113de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1114171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1115171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1116171ae1a4SAndy Whitcroft 1117171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1118171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1119171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1120171ae1a4SAndy Whitcroft 1121171ae1a4SAndy Whitcroft ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 112200df344fSAndy Whitcroft } 11230a920b5bSAndy Whitcroft 112400df344fSAndy Whitcroft#ignore lines being removed 112500df344fSAndy Whitcroft if ($line=~/^-/) {next;} 112600df344fSAndy Whitcroft 112700df344fSAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 112800df344fSAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 11290a920b5bSAndy Whitcroft 11300a920b5bSAndy Whitcroft#trailing whitespace 11319c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1132c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 11339c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 11349c0ca6f9SAndy Whitcroft 1135c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1136c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1137de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 11380a920b5bSAndy Whitcroft } 11390a920b5bSAndy Whitcroft#80 column limit 1140c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1141f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1142f4c014c0SAndy Whitcroft $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && 1143f4c014c0SAndy Whitcroft $length > 80) 1144c45dcabdSAndy Whitcroft { 1145de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 11460a920b5bSAndy Whitcroft } 11470a920b5bSAndy Whitcroft 11488905a67cSAndy Whitcroft# check for adding lines without a newline. 11498905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 11508905a67cSAndy Whitcroft WARN("adding a line without newline at end of file\n" . $herecurr); 11518905a67cSAndy Whitcroft } 11528905a67cSAndy Whitcroft 11530a920b5bSAndy Whitcroft# check we are in a valid source file *.[hc] if not then ignore this hunk 11540a920b5bSAndy Whitcroft next if ($realfile !~ /\.[hc]$/); 11550a920b5bSAndy Whitcroft 11560a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 11570a920b5bSAndy Whitcroft# more than 8 must use tabs. 1158c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1159c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1160c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1161171ae1a4SAndy Whitcroft ERROR("code indent should use tabs where possible\n" . $herevet); 11620a920b5bSAndy Whitcroft } 11630a920b5bSAndy Whitcroft 1164c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1165cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1166c2fdda0dSAndy Whitcroft WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1167c2fdda0dSAndy Whitcroft } 116822f2a2efSAndy Whitcroft 11699c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 1170171ae1a4SAndy Whitcroft my ($stat, $cond); 11719c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1172171ae1a4SAndy Whitcroft ($stat, $cond) = ctx_statement_block($linenr, 1173171ae1a4SAndy Whitcroft $realcnt, 0); 1174171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1175171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1176171ae1a4SAndy Whitcroft 1177171ae1a4SAndy Whitcroft my $s = $stat; 1178171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1179cf655043SAndy Whitcroft 1180c2fdda0dSAndy Whitcroft # Ignore goto labels. 1181171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1182c2fdda0dSAndy Whitcroft 1183c2fdda0dSAndy Whitcroft # Ignore functions being called 1184171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1185c2fdda0dSAndy Whitcroft 1186c45dcabdSAndy Whitcroft # declarations always start with types 1187c45dcabdSAndy Whitcroft } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))\s*(?:;|=|,|\()/s) { 1188c45dcabdSAndy Whitcroft my $type = $1; 1189c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1190c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1191c45dcabdSAndy Whitcroft 11926c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1193171ae1a4SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) { 1194c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1195c2fdda0dSAndy Whitcroft } 11968905a67cSAndy Whitcroft 11976c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 1198171ae1a4SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) { 1199c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 12009c0ca6f9SAndy Whitcroft } 12018905a67cSAndy Whitcroft 12028905a67cSAndy Whitcroft # Check for any sort of function declaration. 12038905a67cSAndy Whitcroft # int foo(something bar, other baz); 12048905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1205171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 12068905a67cSAndy Whitcroft my ($name_len) = length($1); 12078905a67cSAndy Whitcroft 1208cf655043SAndy Whitcroft my $ctx = $s; 1209773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 12108905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1211cf655043SAndy Whitcroft 12128905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1213c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 12148905a67cSAndy Whitcroft 1215c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 12168905a67cSAndy Whitcroft } 12178905a67cSAndy Whitcroft } 12188905a67cSAndy Whitcroft } 12198905a67cSAndy Whitcroft 12209c0ca6f9SAndy Whitcroft } 12219c0ca6f9SAndy Whitcroft 122200df344fSAndy Whitcroft# 122300df344fSAndy Whitcroft# Checks which may be anchored in the context. 122400df344fSAndy Whitcroft# 122500df344fSAndy Whitcroft 122600df344fSAndy Whitcroft# Check for switch () and associated case and default 122700df344fSAndy Whitcroft# statements should be at the same indent. 122800df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 122900df344fSAndy Whitcroft my $err = ''; 123000df344fSAndy Whitcroft my $sep = ''; 123100df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 123200df344fSAndy Whitcroft shift(@ctx); 123300df344fSAndy Whitcroft for my $ctx (@ctx) { 123400df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 123500df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 123600df344fSAndy Whitcroft $indent != $cindent) { 123700df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 123800df344fSAndy Whitcroft $sep = ''; 123900df344fSAndy Whitcroft } else { 124000df344fSAndy Whitcroft $sep = "[...]\n"; 124100df344fSAndy Whitcroft } 124200df344fSAndy Whitcroft } 124300df344fSAndy Whitcroft if ($err ne '') { 12449c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 1245de7d4f0eSAndy Whitcroft } 1246de7d4f0eSAndy Whitcroft } 1247e2a763c2SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 1248e2a763c2SAndy Whitcroft $line !~ /\G(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$/g) { 1249e2a763c2SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 1250e2a763c2SAndy Whitcroft } 1251de7d4f0eSAndy Whitcroft 1252de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1253de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1254c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1255773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1256773647a0SAndy Whitcroft 12579c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1258de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1259de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1260de7d4f0eSAndy Whitcroft 1261548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1262548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1263de7d4f0eSAndy Whitcroft 1264548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1265548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1266548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1267548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1268548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1269773647a0SAndy Whitcroft $ctx_ln++; 1270773647a0SAndy Whitcroft } 1271548596d5SAndy Whitcroft 1272548596d5SAndy Whitcroft ##print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 1273548596d5SAndy Whitcroft ##print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1274773647a0SAndy Whitcroft 1275773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1276773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . 1277c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 127800df344fSAndy Whitcroft } 1279773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1280773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1281773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1282773647a0SAndy Whitcroft { 12839c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 12849c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1285773647a0SAndy Whitcroft WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1286c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 12879c0ca6f9SAndy Whitcroft } 12889c0ca6f9SAndy Whitcroft } 128900df344fSAndy Whitcroft } 129000df344fSAndy Whitcroft 12916c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 12926c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 12936c72ffaaSAndy Whitcroft my $curr_values = annotate_values($opline . "\n", $prev_values); 12946c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1295c2fdda0dSAndy Whitcroft if ($dbg_values) { 1296c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1297cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1298cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 1299c2fdda0dSAndy Whitcroft } 13006c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 13016c72ffaaSAndy Whitcroft 130200df344fSAndy Whitcroft#ignore lines not being added 130300df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 130400df344fSAndy Whitcroft 1305653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 1306653d4876SAndy Whitcroft if ($tst_type && $line =~ /^.$Declare$/) { 1307de7d4f0eSAndy Whitcroft ERROR("TEST: is type $Declare\n" . $herecurr); 1308653d4876SAndy Whitcroft next; 1309653d4876SAndy Whitcroft } 1310653d4876SAndy Whitcroft 1311f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 1312f0a594c1SAndy Whitcroft if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1313f0a594c1SAndy Whitcroft $line =~ /^.\s*{/) { 1314773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . $hereprev); 1315f0a594c1SAndy Whitcroft } 1316f0a594c1SAndy Whitcroft 131700df344fSAndy Whitcroft# 131800df344fSAndy Whitcroft# Checks which are anchored on the added line. 131900df344fSAndy Whitcroft# 132000df344fSAndy Whitcroft 1321653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 1322c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1323653d4876SAndy Whitcroft my $path = $1; 1324653d4876SAndy Whitcroft if ($path =~ m{//}) { 1325de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 1326de7d4f0eSAndy Whitcroft $herecurr); 1327653d4876SAndy Whitcroft } 1328653d4876SAndy Whitcroft } 1329653d4876SAndy Whitcroft 133000df344fSAndy Whitcroft# no C99 // comments 133100df344fSAndy Whitcroft if ($line =~ m{//}) { 1332de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 133300df344fSAndy Whitcroft } 133400df344fSAndy Whitcroft # Remove C99 comments. 13350a920b5bSAndy Whitcroft $line =~ s@//.*@@; 13366c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 13370a920b5bSAndy Whitcroft 13380a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 1339653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 1340653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1341653d4876SAndy Whitcroft my $name = $1; 13420a920b5bSAndy Whitcroft if (($prevline !~ /^}/) && 13430a920b5bSAndy Whitcroft ($prevline !~ /^\+}/) && 1344653d4876SAndy Whitcroft ($prevline !~ /^ }/) && 1345cf655043SAndy Whitcroft ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) && 1346cf655043SAndy Whitcroft ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) && 1347171ae1a4SAndy Whitcroft ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) && 1348cf655043SAndy Whitcroft ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) { 1349de7d4f0eSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 13500a920b5bSAndy Whitcroft } 13510a920b5bSAndy Whitcroft } 13520a920b5bSAndy Whitcroft 1353f0a594c1SAndy Whitcroft# check for external initialisers. 1354c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1355f0a594c1SAndy Whitcroft ERROR("do not initialise externals to 0 or NULL\n" . 1356f0a594c1SAndy Whitcroft $herecurr); 1357f0a594c1SAndy Whitcroft } 13580a920b5bSAndy Whitcroft# check for static initialisers. 13599c9ba34eSAndy Whitcroft if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) { 1360de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 1361de7d4f0eSAndy Whitcroft $herecurr); 13620a920b5bSAndy Whitcroft } 13630a920b5bSAndy Whitcroft 1364653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1365653d4876SAndy Whitcroft# make sense. 1366653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 13679c0ca6f9SAndy Whitcroft $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && 1368c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 1369653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1370de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 13710a920b5bSAndy Whitcroft } 13720a920b5bSAndy Whitcroft 13730a920b5bSAndy Whitcroft# * goes on variable not on type 1374d8aaf121SAndy Whitcroft if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { 1375de7d4f0eSAndy Whitcroft ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . 1376de7d4f0eSAndy Whitcroft $herecurr); 1377d8aaf121SAndy Whitcroft 1378d8aaf121SAndy Whitcroft } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { 1379de7d4f0eSAndy Whitcroft ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . 1380de7d4f0eSAndy Whitcroft $herecurr); 1381d8aaf121SAndy Whitcroft 13829c0ca6f9SAndy Whitcroft } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { 1383de7d4f0eSAndy Whitcroft ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . 1384de7d4f0eSAndy Whitcroft $herecurr); 1385d8aaf121SAndy Whitcroft 13869c0ca6f9SAndy Whitcroft } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { 1387de7d4f0eSAndy Whitcroft ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . 1388de7d4f0eSAndy Whitcroft $herecurr); 13890a920b5bSAndy Whitcroft } 13900a920b5bSAndy Whitcroft 13910a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 13920a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 13930a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 13940a920b5bSAndy Whitcroft# print "$herecurr"; 13950a920b5bSAndy Whitcroft# $clean = 0; 13960a920b5bSAndy Whitcroft# } 13970a920b5bSAndy Whitcroft 13988905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1399c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 14008905a67cSAndy Whitcroft } 14018905a67cSAndy Whitcroft 140200df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 140300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 140400df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 140500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 140600df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1407f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 140800df344fSAndy Whitcroft my $ok = 0; 140900df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 141000df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 141100df344fSAndy Whitcroft # we have a preceeding printk if it ends 141200df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 141300df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 141400df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 141500df344fSAndy Whitcroft $ok = 1; 141600df344fSAndy Whitcroft } 141700df344fSAndy Whitcroft last; 141800df344fSAndy Whitcroft } 141900df344fSAndy Whitcroft } 142000df344fSAndy Whitcroft if ($ok == 0) { 1421de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 14220a920b5bSAndy Whitcroft } 142300df344fSAndy Whitcroft } 14240a920b5bSAndy Whitcroft 1425653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1426653d4876SAndy Whitcroft# or if closed on same line 1427c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1428c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1429de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 14300a920b5bSAndy Whitcroft } 1431653d4876SAndy Whitcroft 14328905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 14338905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 14348905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 14358905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 14368905a67cSAndy Whitcroft } 14378905a67cSAndy Whitcroft 1438*8d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 1439*8d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1440*8d31cfceSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0..10] = 5, 1441*8d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 1442*8d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 1443*8d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1444*8d31cfceSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/)) { 1445*8d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 1446*8d31cfceSAndy Whitcroft } 1447*8d31cfceSAndy Whitcroft } 1448*8d31cfceSAndy Whitcroft 1449f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 14506c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 1451c2fdda0dSAndy Whitcroft my $name = $1; 1452773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 1453773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 1454c2fdda0dSAndy Whitcroft 1455c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 1456773647a0SAndy Whitcroft if ($name =~ /^(?: 1457773647a0SAndy Whitcroft if|for|while|switch|return|case| 1458773647a0SAndy Whitcroft volatile|__volatile__| 1459773647a0SAndy Whitcroft __attribute__|format|__extension__| 1460773647a0SAndy Whitcroft asm|__asm__)$/x) 1461773647a0SAndy Whitcroft { 1462c2fdda0dSAndy Whitcroft 1463c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 1464c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 1465c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 1466c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 1467773647a0SAndy Whitcroft 1468773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 1469c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 1470c2fdda0dSAndy Whitcroft 1471c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 1472c2fdda0dSAndy Whitcroft # likely a typedef for a function. 1473773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 1474c2fdda0dSAndy Whitcroft 1475c2fdda0dSAndy Whitcroft } else { 1476773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 1477f0a594c1SAndy Whitcroft } 14786c72ffaaSAndy Whitcroft } 1479653d4876SAndy Whitcroft# Check operator spacing. 14800a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 14819c0ca6f9SAndy Whitcroft my $ops = qr{ 14829c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 14839c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 14849c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 1485c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 14869c0ca6f9SAndy Whitcroft }x; 1487cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 148800df344fSAndy Whitcroft my $off = 0; 14896c72ffaaSAndy Whitcroft 14906c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 14916c72ffaaSAndy Whitcroft 14920a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 14934a0df2efSAndy Whitcroft $off += length($elements[$n]); 14944a0df2efSAndy Whitcroft 1495773647a0SAndy Whitcroft # Pick up the preceeding and succeeding characters. 1496773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 1497773647a0SAndy Whitcroft my $cc = ''; 1498773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 1499773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 1500773647a0SAndy Whitcroft } 1501773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 1502773647a0SAndy Whitcroft 15034a0df2efSAndy Whitcroft my $a = ''; 15044a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 15054a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 1506cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 15074a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 15084a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 1509773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 15104a0df2efSAndy Whitcroft 15110a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 15124a0df2efSAndy Whitcroft 15134a0df2efSAndy Whitcroft my $c = ''; 15140a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 15154a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 15164a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1517cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 15184a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 15194a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 152022f2a2efSAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); 15214a0df2efSAndy Whitcroft } else { 15224a0df2efSAndy Whitcroft $c = 'E'; 15230a920b5bSAndy Whitcroft } 15240a920b5bSAndy Whitcroft 15254a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 15264a0df2efSAndy Whitcroft 15274a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 15284a0df2efSAndy Whitcroft 15296c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 1530de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 15310a920b5bSAndy Whitcroft 15329c0ca6f9SAndy Whitcroft # Classify operators into binary, unary, or 15339c0ca6f9SAndy Whitcroft # definitions (* only) where they have more 15349c0ca6f9SAndy Whitcroft # than one mode. 15356c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 15366c72ffaaSAndy Whitcroft my $op_left = substr($curr_values, $off, 1); 15376c72ffaaSAndy Whitcroft my $is_unary; 15386c72ffaaSAndy Whitcroft if ($op_type eq 'T') { 15399c0ca6f9SAndy Whitcroft $is_unary = 2; 15406c72ffaaSAndy Whitcroft } elsif ($op_left eq 'V') { 15416c72ffaaSAndy Whitcroft $is_unary = 0; 15426c72ffaaSAndy Whitcroft } else { 15436c72ffaaSAndy Whitcroft $is_unary = 1; 15449c0ca6f9SAndy Whitcroft } 15459c0ca6f9SAndy Whitcroft #if ($op eq '-' || $op eq '&' || $op eq '*') { 15466c72ffaaSAndy Whitcroft # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; 15479c0ca6f9SAndy Whitcroft #} 15480a920b5bSAndy Whitcroft 154913214adfSAndy Whitcroft # Ignore operators passed as parameters. 155013214adfSAndy Whitcroft if ($op_type ne 'V' && 155113214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 155213214adfSAndy Whitcroft 1553cf655043SAndy Whitcroft# # Ignore comments 1554cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 155513214adfSAndy Whitcroft 1556d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 155713214adfSAndy Whitcroft } elsif ($op eq ';') { 1558cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 1559cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 1560773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 1561d8aaf121SAndy Whitcroft } 1562d8aaf121SAndy Whitcroft 1563d8aaf121SAndy Whitcroft # // is a comment 1564d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 15650a920b5bSAndy Whitcroft 15660a920b5bSAndy Whitcroft # -> should have no spaces 15670a920b5bSAndy Whitcroft } elsif ($op eq '->') { 15684a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 1569773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 15700a920b5bSAndy Whitcroft } 15710a920b5bSAndy Whitcroft 15720a920b5bSAndy Whitcroft # , must have a space on the right. 15730a920b5bSAndy Whitcroft } elsif ($op eq ',') { 1574cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 1575773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 15760a920b5bSAndy Whitcroft } 15770a920b5bSAndy Whitcroft 15789c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 15799c0ca6f9SAndy Whitcroft } elsif ($op eq '*' && $is_unary == 2) { 15809c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 15819c0ca6f9SAndy Whitcroft 15829c0ca6f9SAndy Whitcroft # unary operators should have a space before and 15839c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 15849c0ca6f9SAndy Whitcroft # unary operator, or a cast 15859c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 15869c0ca6f9SAndy Whitcroft ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { 1587cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1588773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 15890a920b5bSAndy Whitcroft } 1590171ae1a4SAndy Whitcroft if ($op eq '*' && $cc =~/\s*const\b/) { 1591171ae1a4SAndy Whitcroft # A unary '*' may be const 1592171ae1a4SAndy Whitcroft 1593171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 1594773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 15950a920b5bSAndy Whitcroft } 15960a920b5bSAndy Whitcroft 15970a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 15980a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 1599773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 1600773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 16010a920b5bSAndy Whitcroft } 1602773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 1603773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1604773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 1605653d4876SAndy Whitcroft } 1606773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 1607773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 1608773647a0SAndy Whitcroft } 1609773647a0SAndy Whitcroft 16100a920b5bSAndy Whitcroft 16110a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 16129c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 16139c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 16149c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 1615c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 1616c2fdda0dSAndy Whitcroft $op eq '%') 16170a920b5bSAndy Whitcroft { 1618773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 1619de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 1620de7d4f0eSAndy Whitcroft $hereptr); 16210a920b5bSAndy Whitcroft } 16220a920b5bSAndy Whitcroft 16230a920b5bSAndy Whitcroft # All the others need spaces both sides. 1624cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 162522f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 162622f2a2efSAndy Whitcroft if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && 162722f2a2efSAndy Whitcroft !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { 1628773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 16290a920b5bSAndy Whitcroft } 163022f2a2efSAndy Whitcroft } 16314a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 16320a920b5bSAndy Whitcroft } 16330a920b5bSAndy Whitcroft } 16340a920b5bSAndy Whitcroft 1635f0a594c1SAndy Whitcroft# check for multiple assignments 1636f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 16376c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 1638f0a594c1SAndy Whitcroft } 1639f0a594c1SAndy Whitcroft 164022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 164122f2a2efSAndy Whitcroft## # continuation. 164222f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 164322f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 164422f2a2efSAndy Whitcroft## 164522f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 164622f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 164722f2a2efSAndy Whitcroft## my $ln = $line; 164822f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 164922f2a2efSAndy Whitcroft## } 165022f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 165122f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 165222f2a2efSAndy Whitcroft## } 165322f2a2efSAndy Whitcroft## } 1654f0a594c1SAndy Whitcroft 16550a920b5bSAndy Whitcroft#need space before brace following if, while, etc 165622f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 165722f2a2efSAndy Whitcroft $line =~ /do{/) { 1658773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 1659de7d4f0eSAndy Whitcroft } 1660de7d4f0eSAndy Whitcroft 1661de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 1662de7d4f0eSAndy Whitcroft# on the line 1663de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 1664773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 16650a920b5bSAndy Whitcroft } 16660a920b5bSAndy Whitcroft 166722f2a2efSAndy Whitcroft# check spacing on square brackets 166822f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 1669773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 167022f2a2efSAndy Whitcroft } 167122f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 1672773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 167322f2a2efSAndy Whitcroft } 167422f2a2efSAndy Whitcroft 1675c45dcabdSAndy Whitcroft# check spacing on parentheses 16769c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 16779c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 1678773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 167922f2a2efSAndy Whitcroft } 168013214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 1681c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 1682c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 1683773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 168422f2a2efSAndy Whitcroft } 168522f2a2efSAndy Whitcroft 16860a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 16874a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 16880a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 1689de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 16900a920b5bSAndy Whitcroft } 16910a920b5bSAndy Whitcroft 1692c45dcabdSAndy Whitcroft# Return is not a function. 1693c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 1694c45dcabdSAndy Whitcroft my $spacing = $1; 1695c45dcabdSAndy Whitcroft my $value = $2; 1696c45dcabdSAndy Whitcroft 1697c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 1698fee61c47SAndy Whitcroft $value =~ s/\)\(/\) \(/g; 1699c45dcabdSAndy Whitcroft while ($value =~ s/\([^\(\)]*\)/1/) { 1700c45dcabdSAndy Whitcroft } 1701c45dcabdSAndy Whitcroft 1702c45dcabdSAndy Whitcroft if ($value =~ /^(?:$Ident|-?$Constant)$/) { 1703c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 1704c45dcabdSAndy Whitcroft 1705c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 1706c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 1707c45dcabdSAndy Whitcroft } 1708c45dcabdSAndy Whitcroft } 1709c45dcabdSAndy Whitcroft 17100a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 17114a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 1712773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 17130a920b5bSAndy Whitcroft } 17140a920b5bSAndy Whitcroft 17150a920b5bSAndy Whitcroft# Check for illegal assignment in if conditional. 17168905a67cSAndy Whitcroft if ($line =~ /\bif\s*\(/) { 1717171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 17188905a67cSAndy Whitcroft 17198905a67cSAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { 1720c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 17218905a67cSAndy Whitcroft } 17228905a67cSAndy Whitcroft 17238905a67cSAndy Whitcroft # Find out what is on the end of the line after the 17248905a67cSAndy Whitcroft # conditional. 1725773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 17268905a67cSAndy Whitcroft $s =~ s/\n.*//g; 172713214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 1728773647a0SAndy Whitcroft if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ && 1729c45dcabdSAndy Whitcroft $c !~ /^.\s*\#\s*if/) 1730773647a0SAndy Whitcroft { 17318905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 17328905a67cSAndy Whitcroft } 17338905a67cSAndy Whitcroft } 17348905a67cSAndy Whitcroft 173513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 173613214adfSAndy Whitcroft if ($line =~ / 173713214adfSAndy Whitcroft (?: 173813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 173913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 174013214adfSAndy Whitcroft (?:\&\&|\|\|) 174113214adfSAndy Whitcroft | 174213214adfSAndy Whitcroft (?:\&\&|\|\|) 174313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 174413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 174513214adfSAndy Whitcroft )/x) 174613214adfSAndy Whitcroft { 174713214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 174813214adfSAndy Whitcroft } 174913214adfSAndy Whitcroft 17508905a67cSAndy Whitcroft# if and else should not have general statements after it 175113214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 175213214adfSAndy Whitcroft my $s = $1; 175313214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 175413214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 17558905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 17560a920b5bSAndy Whitcroft } 175713214adfSAndy Whitcroft } 17580a920b5bSAndy Whitcroft 17590a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 17600a920b5bSAndy Whitcroft # indent level to be relevant to each other. 17610a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 17620a920b5bSAndy Whitcroft $previndent == $indent) { 1763de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 17640a920b5bSAndy Whitcroft } 17650a920b5bSAndy Whitcroft 1766c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 1767c2fdda0dSAndy Whitcroft $previndent == $indent) { 1768c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 1769c2fdda0dSAndy Whitcroft 1770c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 1771c2fdda0dSAndy Whitcroft # conditional. 1772773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 1773c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 1774c2fdda0dSAndy Whitcroft 1775c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 1776c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 1777c2fdda0dSAndy Whitcroft } 1778c2fdda0dSAndy Whitcroft } 1779c2fdda0dSAndy Whitcroft 17800a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 17810a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 17820a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 17830a920b5bSAndy Whitcroft# print "$herecurr"; 17840a920b5bSAndy Whitcroft# $clean = 0; 17850a920b5bSAndy Whitcroft# } 17860a920b5bSAndy Whitcroft 17870a920b5bSAndy Whitcroft#no spaces allowed after \ in define 1788c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 1789de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 17900a920b5bSAndy Whitcroft } 17910a920b5bSAndy Whitcroft 1792653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 1793c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 1794c45dcabdSAndy Whitcroft my $checkfile = "include/linux/$1.h"; 1795c45dcabdSAndy Whitcroft if (-f "$root/$checkfile" && $realfile ne $checkfile && 1796c45dcabdSAndy Whitcroft $1 ne 'irq') 1797c45dcabdSAndy Whitcroft { 1798773647a0SAndy Whitcroft WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . 1799de7d4f0eSAndy Whitcroft $herecurr); 18000a920b5bSAndy Whitcroft } 18010a920b5bSAndy Whitcroft } 18020a920b5bSAndy Whitcroft 1803653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 1804653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 1805cf655043SAndy Whitcroft# in a known good container 1806c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 1807d8aaf121SAndy Whitcroft my $ln = $linenr; 1808d8aaf121SAndy Whitcroft my $cnt = $realcnt; 1809c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 1810c45dcabdSAndy Whitcroft my $ctx = ''; 1811653d4876SAndy Whitcroft 1812c45dcabdSAndy Whitcroft my $args = defined($1); 1813de7d4f0eSAndy Whitcroft 1814c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 1815c45dcabdSAndy Whitcroft # search to that. 1816c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 1817c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 1818c45dcabdSAndy Whitcroft { 1819c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 1820a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 1821c45dcabdSAndy Whitcroft $ln++; 1822de7d4f0eSAndy Whitcroft } 1823c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 1824d8aaf121SAndy Whitcroft 1825c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 1826c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 1827c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 1828a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 1829c45dcabdSAndy Whitcroft 1830c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 1831c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 1832c45dcabdSAndy Whitcroft $rest = ''; 1833a3bb97a7SAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /(?:^|\\)\s*$/)) { 1834a3bb97a7SAndy Whitcroft #print "ADDING $off <" . substr($lines[$ln - 1], $off) . ">\n"; 1835a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 1836c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 1837c45dcabdSAndy Whitcroft $cnt--; 1838a3bb97a7SAndy Whitcroft } 1839a3bb97a7SAndy Whitcroft $ln++; 1840c45dcabdSAndy Whitcroft $off = 0; 1841c45dcabdSAndy Whitcroft } 1842c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 1843c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 1844c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 1845c45dcabdSAndy Whitcroft 1846c45dcabdSAndy Whitcroft # Clean up the original statement. 1847c45dcabdSAndy Whitcroft if ($args) { 1848c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 1849d8aaf121SAndy Whitcroft } else { 1850c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 1851c45dcabdSAndy Whitcroft } 1852c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 1853c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 1854c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 1855c45dcabdSAndy Whitcroft 1856c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 1857c45dcabdSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/) { 1858c45dcabdSAndy Whitcroft } 1859c45dcabdSAndy Whitcroft while ($dstat =~ s/\{[^\{\}]*\}/1/) { 1860c45dcabdSAndy Whitcroft } 1861c45dcabdSAndy Whitcroft 1862c45dcabdSAndy Whitcroft my $exceptions = qr{ 1863c45dcabdSAndy Whitcroft $Declare| 1864c45dcabdSAndy Whitcroft module_param_named| 1865c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 1866c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 1867c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 1868c45dcabdSAndy Whitcroft __typeof__\( 1869c45dcabdSAndy Whitcroft }x; 1870a3bb97a7SAndy Whitcroft #print "REST<$rest>\n"; 1871c45dcabdSAndy Whitcroft if ($rest ne '') { 1872c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 1873c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 1874c45dcabdSAndy Whitcroft { 1875c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 1876c45dcabdSAndy Whitcroft } 1877c45dcabdSAndy Whitcroft 1878c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 1879c45dcabdSAndy Whitcroft if ($dstat ne '' && 1880c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 1881c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 1882c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 1883c45dcabdSAndy Whitcroft { 1884de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 1885d8aaf121SAndy Whitcroft } 18860a920b5bSAndy Whitcroft } 1887653d4876SAndy Whitcroft } 18880a920b5bSAndy Whitcroft 1889f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 189013214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 189113214adfSAndy Whitcroft my ($level, $endln, @chunks) = 1892cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 189313214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 1894cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 1895cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 189613214adfSAndy Whitcroft my $allowed = 0; 189713214adfSAndy Whitcroft my $seen = 0; 1898773647a0SAndy Whitcroft my $herectx = $here . "\n"; 1899cf655043SAndy Whitcroft my $ln = $linenr - 1; 190013214adfSAndy Whitcroft for my $chunk (@chunks) { 190113214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 190213214adfSAndy Whitcroft 1903773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 1904773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 1905773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 1906773647a0SAndy Whitcroft 1907773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 1908773647a0SAndy Whitcroft 1909773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 1910773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 1911773647a0SAndy Whitcroft 1912773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 1913cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 1914cf655043SAndy Whitcroft 1915773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 191613214adfSAndy Whitcroft 191713214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 191813214adfSAndy Whitcroft 1919cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 1920cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 1921cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 192213214adfSAndy Whitcroft $allowed = 1; 192313214adfSAndy Whitcroft } 192413214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 1925cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 192613214adfSAndy Whitcroft $allowed = 1; 192713214adfSAndy Whitcroft } 1928cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 1929cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 193013214adfSAndy Whitcroft $allowed = 1; 193113214adfSAndy Whitcroft } 193213214adfSAndy Whitcroft } 193313214adfSAndy Whitcroft if ($seen && !$allowed) { 1934cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 193513214adfSAndy Whitcroft } 193613214adfSAndy Whitcroft } 193713214adfSAndy Whitcroft } 1938773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 193913214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 1940cf655043SAndy Whitcroft my $allowed = 0; 1941f0a594c1SAndy Whitcroft 1942cf655043SAndy Whitcroft # Check the pre-context. 1943cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 1944cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 1945cf655043SAndy Whitcroft $allowed = 1; 1946f0a594c1SAndy Whitcroft } 1947773647a0SAndy Whitcroft 1948773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 1949773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 1950773647a0SAndy Whitcroft 1951cf655043SAndy Whitcroft # Check the condition. 1952cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 1953773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 1954cf655043SAndy Whitcroft if (defined $cond) { 1955773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 1956cf655043SAndy Whitcroft } 1957cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 1958cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 1959cf655043SAndy Whitcroft $allowed = 1; 1960cf655043SAndy Whitcroft } 1961cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 1962cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 1963cf655043SAndy Whitcroft $allowed = 1; 1964cf655043SAndy Whitcroft } 1965cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 1966cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 1967cf655043SAndy Whitcroft $allowed = 1; 1968cf655043SAndy Whitcroft } 1969cf655043SAndy Whitcroft # Check the post-context. 1970cf655043SAndy Whitcroft if (defined $chunks[1]) { 1971cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 1972cf655043SAndy Whitcroft if (defined $cond) { 1973773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 1974cf655043SAndy Whitcroft } 1975cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 1976cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 1977cf655043SAndy Whitcroft $allowed = 1; 1978cf655043SAndy Whitcroft } 1979cf655043SAndy Whitcroft } 1980cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 1981cf655043SAndy Whitcroft my $herectx = $here . "\n";; 1982cf655043SAndy Whitcroft my $end = $linenr + statement_rawlines($block) - 1; 1983cf655043SAndy Whitcroft 1984cf655043SAndy Whitcroft for (my $ln = $linenr - 1; $ln < $end; $ln++) { 1985cf655043SAndy Whitcroft $herectx .= $rawlines[$ln] . "\n";; 1986cf655043SAndy Whitcroft } 1987cf655043SAndy Whitcroft 1988cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 1989f0a594c1SAndy Whitcroft } 1990f0a594c1SAndy Whitcroft } 1991f0a594c1SAndy Whitcroft 1992653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 19934a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 1994c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 1995de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 19960a920b5bSAndy Whitcroft } 19970a920b5bSAndy Whitcroft } 19980a920b5bSAndy Whitcroft 19994a0df2efSAndy Whitcroft# don't use deprecated functions 20004a0df2efSAndy Whitcroft for my $func (@dep_functions) { 200100df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2002de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 20034a0df2efSAndy Whitcroft } 20044a0df2efSAndy Whitcroft } 20054a0df2efSAndy Whitcroft 20064a0df2efSAndy Whitcroft# no volatiles please 20076c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 20086c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2009de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 20104a0df2efSAndy Whitcroft } 20114a0df2efSAndy Whitcroft 20129c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 20139c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 20149c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 20159c0ca6f9SAndy Whitcroft } 20169c0ca6f9SAndy Whitcroft 201700df344fSAndy Whitcroft# warn about #if 0 2018c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2019de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2020de7d4f0eSAndy Whitcroft $herecurr); 20214a0df2efSAndy Whitcroft } 20224a0df2efSAndy Whitcroft 2023f0a594c1SAndy Whitcroft# check for needless kfree() checks 2024f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2025f0a594c1SAndy Whitcroft my $expr = $1; 2026f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 2027f0a594c1SAndy Whitcroft WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); 2028f0a594c1SAndy Whitcroft } 2029f0a594c1SAndy Whitcroft } 2030f0a594c1SAndy Whitcroft 203100df344fSAndy Whitcroft# warn about #ifdefs in C files 2032c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 203300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 203400df344fSAndy Whitcroft# print "$herecurr"; 203500df344fSAndy Whitcroft# $clean = 0; 203600df344fSAndy Whitcroft# } 203700df344fSAndy Whitcroft 203822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2039c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 204022f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 204122f2a2efSAndy Whitcroft } 204222f2a2efSAndy Whitcroft 20434a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2044171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2045171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 20464a0df2efSAndy Whitcroft my $which = $1; 20474a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2048de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 20494a0df2efSAndy Whitcroft } 20504a0df2efSAndy Whitcroft } 20514a0df2efSAndy Whitcroft# check for memory barriers without a comment. 20524a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 20534a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2054de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 20554a0df2efSAndy Whitcroft } 20564a0df2efSAndy Whitcroft } 20574a0df2efSAndy Whitcroft# check of hardware specific defines 2058c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2059de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 20600a920b5bSAndy Whitcroft } 2061653d4876SAndy Whitcroft 2062de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2063de7d4f0eSAndy Whitcroft# storage class and type. 20649c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 20659c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2066de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2067de7d4f0eSAndy Whitcroft } 2068de7d4f0eSAndy Whitcroft 20698905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 20708905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 20718905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 20728905a67cSAndy Whitcroft } 20738905a67cSAndy Whitcroft 2074de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2075171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2076c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2077171ae1a4SAndy Whitcroft { 2078c45dcabdSAndy Whitcroft my $function_name = $1; 2079c45dcabdSAndy Whitcroft my $paren_space = $2; 2080171ae1a4SAndy Whitcroft 2081171ae1a4SAndy Whitcroft my $s = $stat; 2082171ae1a4SAndy Whitcroft if (defined $cond) { 2083171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2084171ae1a4SAndy Whitcroft } 2085c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2086c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2087c45dcabdSAndy Whitcroft { 2088de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2089de7d4f0eSAndy Whitcroft } 2090de7d4f0eSAndy Whitcroft 2091171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2092171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2093171ae1a4SAndy Whitcroft } 20949c9ba34eSAndy Whitcroft 20959c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 20969c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 20979c9ba34eSAndy Whitcroft { 20989c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2099171ae1a4SAndy Whitcroft } 2100171ae1a4SAndy Whitcroft 2101de7d4f0eSAndy Whitcroft# checks for new __setup's 2102de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2103de7d4f0eSAndy Whitcroft my $name = $1; 2104de7d4f0eSAndy Whitcroft 2105de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2106de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2107de7d4f0eSAndy Whitcroft } 2108653d4876SAndy Whitcroft } 21099c0ca6f9SAndy Whitcroft 21109c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 21119c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 21129c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 21139c0ca6f9SAndy Whitcroft } 211413214adfSAndy Whitcroft 211513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 211613214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 211713214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 211813214adfSAndy Whitcroft } 2119773647a0SAndy Whitcroft 2120773647a0SAndy Whitcroft# check for semaphores used as mutexes 2121171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2122773647a0SAndy Whitcroft WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); 2123773647a0SAndy Whitcroft } 2124773647a0SAndy Whitcroft# check for semaphores used as mutexes 2125171ae1a4SAndy Whitcroft if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { 2126773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 2127773647a0SAndy Whitcroft } 2128773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto* 2129773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2130773647a0SAndy Whitcroft WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); 2131773647a0SAndy Whitcroft } 2132f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2133f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2134f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2135f3db6639SMichael Ellerman } 2136773647a0SAndy Whitcroft 2137773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2138773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2139773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2140c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2141c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2142171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2143171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2144171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2145773647a0SAndy Whitcroft { 2146773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2147773647a0SAndy Whitcroft } 21489c9ba34eSAndy Whitcroft 21499c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 21509c9ba34eSAndy Whitcroft my $string; 21519c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 21529c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 21539c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 21549c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 21559c9ba34eSAndy Whitcroft last; 21569c9ba34eSAndy Whitcroft } 21579c9ba34eSAndy Whitcroft } 215813214adfSAndy Whitcroft } 215913214adfSAndy Whitcroft 216013214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 216113214adfSAndy Whitcroft # so just keep quiet. 216213214adfSAndy Whitcroft if ($#rawlines == -1) { 216313214adfSAndy Whitcroft exit(0); 21640a920b5bSAndy Whitcroft } 21650a920b5bSAndy Whitcroft 21668905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 21678905a67cSAndy Whitcroft # things that appear to be patches. 21688905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 21698905a67cSAndy Whitcroft exit(0); 21708905a67cSAndy Whitcroft } 21718905a67cSAndy Whitcroft 21728905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 21738905a67cSAndy Whitcroft # just keep quiet. 21748905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 21758905a67cSAndy Whitcroft exit(0); 21768905a67cSAndy Whitcroft } 21778905a67cSAndy Whitcroft 21788905a67cSAndy Whitcroft if (!$is_patch) { 2179de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 21800a920b5bSAndy Whitcroft } 21810a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2182de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 21830a920b5bSAndy Whitcroft } 21840a920b5bSAndy Whitcroft 2185f0a594c1SAndy Whitcroft print report_dump(); 218613214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 218713214adfSAndy Whitcroft print "$filename " if ($summary_file); 21886c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 21896c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 21906c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 21918905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 21926c72ffaaSAndy Whitcroft } 21938905a67cSAndy Whitcroft 21940a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2195c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 21960a920b5bSAndy Whitcroft } 21970a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2198c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 21990a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 22000a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 22010a920b5bSAndy Whitcroft } 220213214adfSAndy Whitcroft 22030a920b5bSAndy Whitcroft return $clean; 22040a920b5bSAndy Whitcroft} 2205