10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 2f4432c5cSDave Jones# (c) 2001, Dave Jones. <[email protected]> (the file handling bit) 300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit) 42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) 52a5a2c25SAndy Whitcroft# (c) 2008, Andy Whitcroft <[email protected]> 60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2 70a920b5bSAndy Whitcroft 80a920b5bSAndy Whitcroftuse strict; 90a920b5bSAndy Whitcroft 100a920b5bSAndy Whitcroftmy $P = $0; 1100df344fSAndy Whitcroft$P =~ s@.*/@@g; 120a920b5bSAndy Whitcroft 13bea5606dSAndy Whitcroftmy $V = '0.28'; 140a920b5bSAndy Whitcroft 150a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 160a920b5bSAndy Whitcroft 170a920b5bSAndy Whitcroftmy $quiet = 0; 180a920b5bSAndy Whitcroftmy $tree = 1; 190a920b5bSAndy Whitcroftmy $chk_signoff = 1; 200a920b5bSAndy Whitcroftmy $chk_patch = 1; 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, 47773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 480a920b5bSAndy Whitcroft) or exit; 490a920b5bSAndy Whitcroft 500a920b5bSAndy Whitcroftmy $exit = 0; 510a920b5bSAndy Whitcroft 520a920b5bSAndy Whitcroftif ($#ARGV < 0) { 5300df344fSAndy Whitcroft print "usage: $P [options] patchfile\n"; 540a920b5bSAndy Whitcroft print "version: $V\n"; 550a920b5bSAndy Whitcroft print "options: -q => quiet\n"; 560a920b5bSAndy Whitcroft print " --no-tree => run without a kernel tree\n"; 578905a67cSAndy Whitcroft print " --terse => one line per report\n"; 586c72ffaaSAndy Whitcroft print " --emacs => emacs compile window format\n"; 596c72ffaaSAndy Whitcroft print " --file => check a source file\n"; 606c72ffaaSAndy Whitcroft print " --strict => enable more subjective tests\n"; 616c72ffaaSAndy Whitcroft print " --root => path to the kernel tree root\n"; 6213214adfSAndy Whitcroft print " --no-summary => suppress the per-file summary\n"; 6313214adfSAndy Whitcroft print " --summary-file => include the filename in summary\n"; 640a920b5bSAndy Whitcroft exit(1); 650a920b5bSAndy Whitcroft} 660a920b5bSAndy Whitcroft 67c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 68c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 697429c690SAndy Whitcroftmy $dbg_type = 0; 70a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 71c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 7221caa13cSAndy Whitcroft ## no critic 7321caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 7421caa13cSAndy Whitcroft die "$@" if ($@); 75c2fdda0dSAndy Whitcroft} 76c2fdda0dSAndy Whitcroft 778905a67cSAndy Whitcroftif ($terse) { 788905a67cSAndy Whitcroft $emacs = 1; 798905a67cSAndy Whitcroft $quiet++; 808905a67cSAndy Whitcroft} 818905a67cSAndy Whitcroft 826c72ffaaSAndy Whitcroftif ($tree) { 836c72ffaaSAndy Whitcroft if (defined $root) { 846c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 856c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 866c72ffaaSAndy Whitcroft } 876c72ffaaSAndy Whitcroft } else { 886c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 896c72ffaaSAndy Whitcroft $root = '.'; 906c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 916c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 926c72ffaaSAndy Whitcroft $root = $1; 936c72ffaaSAndy Whitcroft } 946c72ffaaSAndy Whitcroft } 956c72ffaaSAndy Whitcroft 966c72ffaaSAndy Whitcroft if (!defined $root) { 970a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 980a920b5bSAndy Whitcroft exit(2); 990a920b5bSAndy Whitcroft } 1006c72ffaaSAndy Whitcroft} 1016c72ffaaSAndy Whitcroft 1026c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 1036c72ffaaSAndy Whitcroft 1046c72ffaaSAndy Whitcroftour $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; 1056c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 1066c72ffaaSAndy Whitcroftour $Sparse = qr{ 1076c72ffaaSAndy Whitcroft __user| 1086c72ffaaSAndy Whitcroft __kernel| 1096c72ffaaSAndy Whitcroft __force| 1106c72ffaaSAndy Whitcroft __iomem| 1116c72ffaaSAndy Whitcroft __must_check| 1126c72ffaaSAndy Whitcroft __init_refok| 113417495edSAndy Whitcroft __kprobes| 114417495edSAndy Whitcroft __ref 1156c72ffaaSAndy Whitcroft }x; 1166c72ffaaSAndy Whitcroftour $Attribute = qr{ 1176c72ffaaSAndy Whitcroft const| 1186c72ffaaSAndy Whitcroft __read_mostly| 1196c72ffaaSAndy Whitcroft __kprobes| 12024e1d81aSAndy Whitcroft __(?:mem|cpu|dev|)(?:initdata|init)| 12124e1d81aSAndy Whitcroft ____cacheline_aligned| 12224e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 1235fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 1245fe3af11SAndy Whitcroft __weak 1256c72ffaaSAndy Whitcroft }x; 126c45dcabdSAndy Whitcroftour $Modifier; 1276c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 1286c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 1296c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 1306c72ffaaSAndy Whitcroft 1316c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 1326c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 13386f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 1346c72ffaaSAndy Whitcroftour $Operators = qr{ 1356c72ffaaSAndy Whitcroft <=|>=|==|!=| 1366c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 137c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1386c72ffaaSAndy Whitcroft }x; 1396c72ffaaSAndy Whitcroft 1408905a67cSAndy Whitcroftour $NonptrType; 1418905a67cSAndy Whitcroftour $Type; 1428905a67cSAndy Whitcroftour $Declare; 1438905a67cSAndy Whitcroft 144171ae1a4SAndy Whitcroftour $UTF8 = qr { 145171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 146171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 147171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 148171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 149171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 150171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 151171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 152171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 153171ae1a4SAndy Whitcroft}x; 154171ae1a4SAndy Whitcroft 1558ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 1568ed22cadSAndy Whitcroft (?:__)?(?:u|s|be|le)(?:\d|\d\d)| 1578ed22cadSAndy Whitcroft atomic_t 1588ed22cadSAndy Whitcroft)}; 1598ed22cadSAndy Whitcroft 1608905a67cSAndy Whitcroftour @typeList = ( 1618905a67cSAndy Whitcroft qr{void}, 162c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 163c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 164c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 165c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 166c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 167c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 168c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 1698905a67cSAndy Whitcroft qr{unsigned}, 1708905a67cSAndy Whitcroft qr{float}, 1718905a67cSAndy Whitcroft qr{double}, 1728905a67cSAndy Whitcroft qr{bool}, 1738905a67cSAndy Whitcroft qr{struct\s+$Ident}, 1748905a67cSAndy Whitcroft qr{union\s+$Ident}, 1758905a67cSAndy Whitcroft qr{enum\s+$Ident}, 1768905a67cSAndy Whitcroft qr{${Ident}_t}, 1778905a67cSAndy Whitcroft qr{${Ident}_handler}, 1788905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 1798905a67cSAndy Whitcroft); 180c45dcabdSAndy Whitcroftour @modifierList = ( 181c45dcabdSAndy Whitcroft qr{fastcall}, 182c45dcabdSAndy Whitcroft); 1838905a67cSAndy Whitcroft 1848905a67cSAndy Whitcroftsub build_types { 185d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 186d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 187c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 1888905a67cSAndy Whitcroft $NonptrType = qr{ 189d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 190cf655043SAndy Whitcroft (?: 191c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 1928ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 193c45dcabdSAndy Whitcroft (?:${all}\b) 194cf655043SAndy Whitcroft ) 195c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 1968905a67cSAndy Whitcroft }x; 1978905a67cSAndy Whitcroft $Type = qr{ 198c45dcabdSAndy Whitcroft $NonptrType 19965863862SAndy Whitcroft (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 200c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 2018905a67cSAndy Whitcroft }x; 2028905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 2038905a67cSAndy Whitcroft} 2048905a67cSAndy Whitcroftbuild_types(); 2056c72ffaaSAndy Whitcroft 2066c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 2070a920b5bSAndy Whitcroft 2084a0df2efSAndy Whitcroftmy @dep_includes = (); 2094a0df2efSAndy Whitcroftmy @dep_functions = (); 2106c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 2116c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 21221caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 2136c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 21421caa13cSAndy Whitcroft while (<$REMOVE>) { 215f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 216f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 217f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 2184a0df2efSAndy Whitcroft push(@dep_includes, $1); 2194a0df2efSAndy Whitcroft 220f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 221f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 222f0a594c1SAndy Whitcroft } 2234a0df2efSAndy Whitcroft } 2240a920b5bSAndy Whitcroft } 2250a920b5bSAndy Whitcroft } 22621caa13cSAndy Whitcroft close($REMOVE); 2270a920b5bSAndy Whitcroft} 2280a920b5bSAndy Whitcroft 22900df344fSAndy Whitcroftmy @rawlines = (); 230c2fdda0dSAndy Whitcroftmy @lines = (); 231c2fdda0dSAndy Whitcroftmy $vname; 2326c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 23321caa13cSAndy Whitcroft my $FILE; 2346c72ffaaSAndy Whitcroft if ($file) { 23521caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 2366c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 23721caa13cSAndy Whitcroft } elsif ($filename eq '-') { 23821caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 2396c72ffaaSAndy Whitcroft } else { 24021caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 2416c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 2426c72ffaaSAndy Whitcroft } 243c2fdda0dSAndy Whitcroft if ($filename eq '-') { 244c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 245c2fdda0dSAndy Whitcroft } else { 246c2fdda0dSAndy Whitcroft $vname = $filename; 247c2fdda0dSAndy Whitcroft } 24821caa13cSAndy Whitcroft while (<$FILE>) { 2490a920b5bSAndy Whitcroft chomp; 25000df344fSAndy Whitcroft push(@rawlines, $_); 2516c72ffaaSAndy Whitcroft } 25221caa13cSAndy Whitcroft close($FILE); 253c2fdda0dSAndy Whitcroft if (!process($filename)) { 2540a920b5bSAndy Whitcroft $exit = 1; 2550a920b5bSAndy Whitcroft } 25600df344fSAndy Whitcroft @rawlines = (); 25713214adfSAndy Whitcroft @lines = (); 2580a920b5bSAndy Whitcroft} 2590a920b5bSAndy Whitcroft 2600a920b5bSAndy Whitcroftexit($exit); 2610a920b5bSAndy Whitcroft 2620a920b5bSAndy Whitcroftsub top_of_kernel_tree { 2636c72ffaaSAndy Whitcroft my ($root) = @_; 2646c72ffaaSAndy Whitcroft 2656c72ffaaSAndy Whitcroft my @tree_check = ( 2666c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 2676c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 2686c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 2696c72ffaaSAndy Whitcroft ); 2706c72ffaaSAndy Whitcroft 2716c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 2726c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 2730a920b5bSAndy Whitcroft return 0; 2740a920b5bSAndy Whitcroft } 2756c72ffaaSAndy Whitcroft } 2766c72ffaaSAndy Whitcroft return 1; 2776c72ffaaSAndy Whitcroft} 2780a920b5bSAndy Whitcroft 2790a920b5bSAndy Whitcroftsub expand_tabs { 2800a920b5bSAndy Whitcroft my ($str) = @_; 2810a920b5bSAndy Whitcroft 2820a920b5bSAndy Whitcroft my $res = ''; 2830a920b5bSAndy Whitcroft my $n = 0; 2840a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 2850a920b5bSAndy Whitcroft if ($c eq "\t") { 2860a920b5bSAndy Whitcroft $res .= ' '; 2870a920b5bSAndy Whitcroft $n++; 2880a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 2890a920b5bSAndy Whitcroft $res .= ' '; 2900a920b5bSAndy Whitcroft } 2910a920b5bSAndy Whitcroft next; 2920a920b5bSAndy Whitcroft } 2930a920b5bSAndy Whitcroft $res .= $c; 2940a920b5bSAndy Whitcroft $n++; 2950a920b5bSAndy Whitcroft } 2960a920b5bSAndy Whitcroft 2970a920b5bSAndy Whitcroft return $res; 2980a920b5bSAndy Whitcroft} 2996c72ffaaSAndy Whitcroftsub copy_spacing { 300773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 3016c72ffaaSAndy Whitcroft return $res; 3026c72ffaaSAndy Whitcroft} 3030a920b5bSAndy Whitcroft 3044a0df2efSAndy Whitcroftsub line_stats { 3054a0df2efSAndy Whitcroft my ($line) = @_; 3064a0df2efSAndy Whitcroft 3074a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 3084a0df2efSAndy Whitcroft $line =~ s/^.//; 3094a0df2efSAndy Whitcroft $line = expand_tabs($line); 3104a0df2efSAndy Whitcroft 3114a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 3124a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 3134a0df2efSAndy Whitcroft 3144a0df2efSAndy Whitcroft return (length($line), length($white)); 3154a0df2efSAndy Whitcroft} 3164a0df2efSAndy Whitcroft 317773647a0SAndy Whitcroftmy $sanitise_quote = ''; 318773647a0SAndy Whitcroft 319773647a0SAndy Whitcroftsub sanitise_line_reset { 320773647a0SAndy Whitcroft my ($in_comment) = @_; 321773647a0SAndy Whitcroft 322773647a0SAndy Whitcroft if ($in_comment) { 323773647a0SAndy Whitcroft $sanitise_quote = '*/'; 324773647a0SAndy Whitcroft } else { 325773647a0SAndy Whitcroft $sanitise_quote = ''; 326773647a0SAndy Whitcroft } 327773647a0SAndy Whitcroft} 32800df344fSAndy Whitcroftsub sanitise_line { 32900df344fSAndy Whitcroft my ($line) = @_; 33000df344fSAndy Whitcroft 33100df344fSAndy Whitcroft my $res = ''; 33200df344fSAndy Whitcroft my $l = ''; 33300df344fSAndy Whitcroft 334c2fdda0dSAndy Whitcroft my $qlen = 0; 335773647a0SAndy Whitcroft my $off = 0; 336773647a0SAndy Whitcroft my $c; 33700df344fSAndy Whitcroft 338773647a0SAndy Whitcroft # Always copy over the diff marker. 339773647a0SAndy Whitcroft $res = substr($line, 0, 1); 340773647a0SAndy Whitcroft 341773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 342773647a0SAndy Whitcroft $c = substr($line, $off, 1); 343773647a0SAndy Whitcroft 344773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 345773647a0SAndy Whitcroft # and end, all to $;. 346773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 347773647a0SAndy Whitcroft $sanitise_quote = '*/'; 348773647a0SAndy Whitcroft 349773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 350773647a0SAndy Whitcroft $off++; 35100df344fSAndy Whitcroft next; 352773647a0SAndy Whitcroft } 35381bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 354773647a0SAndy Whitcroft $sanitise_quote = ''; 355773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 356773647a0SAndy Whitcroft $off++; 357773647a0SAndy Whitcroft next; 358773647a0SAndy Whitcroft } 359113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 360113f04a8SDaniel Walker $sanitise_quote = '//'; 361113f04a8SDaniel Walker 362113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 363113f04a8SDaniel Walker $off++; 364113f04a8SDaniel Walker next; 365113f04a8SDaniel Walker } 366773647a0SAndy Whitcroft 367773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 368773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 369773647a0SAndy Whitcroft $c eq "\\") { 370773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 371773647a0SAndy Whitcroft $off++; 372773647a0SAndy Whitcroft next; 373773647a0SAndy Whitcroft } 374773647a0SAndy Whitcroft # Regular quotes. 375773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 376773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 377773647a0SAndy Whitcroft $sanitise_quote = $c; 378773647a0SAndy Whitcroft 379773647a0SAndy Whitcroft substr($res, $off, 1, $c); 380773647a0SAndy Whitcroft next; 381773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 382773647a0SAndy Whitcroft $sanitise_quote = ''; 38300df344fSAndy Whitcroft } 38400df344fSAndy Whitcroft } 385773647a0SAndy Whitcroft 386fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 387773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 388773647a0SAndy Whitcroft substr($res, $off, 1, $;); 389113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 390113f04a8SDaniel Walker substr($res, $off, 1, $;); 391773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 392773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 39300df344fSAndy Whitcroft } else { 394773647a0SAndy Whitcroft substr($res, $off, 1, $c); 39500df344fSAndy Whitcroft } 396c2fdda0dSAndy Whitcroft } 397c2fdda0dSAndy Whitcroft 398113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 399113f04a8SDaniel Walker $sanitise_quote = ''; 400113f04a8SDaniel Walker } 401113f04a8SDaniel Walker 402c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 403c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 404c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 405c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 406c2fdda0dSAndy Whitcroft 407c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 408c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 409c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 410c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 411c2fdda0dSAndy Whitcroft } 412c2fdda0dSAndy Whitcroft 41300df344fSAndy Whitcroft return $res; 41400df344fSAndy Whitcroft} 41500df344fSAndy Whitcroft 4168905a67cSAndy Whitcroftsub ctx_statement_block { 4178905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 4188905a67cSAndy Whitcroft my $line = $linenr - 1; 4198905a67cSAndy Whitcroft my $blk = ''; 4208905a67cSAndy Whitcroft my $soff = $off; 4218905a67cSAndy Whitcroft my $coff = $off - 1; 422773647a0SAndy Whitcroft my $coff_set = 0; 4238905a67cSAndy Whitcroft 42413214adfSAndy Whitcroft my $loff = 0; 42513214adfSAndy Whitcroft 4268905a67cSAndy Whitcroft my $type = ''; 4278905a67cSAndy Whitcroft my $level = 0; 428a2750645SAndy Whitcroft my @stack = (); 429cf655043SAndy Whitcroft my $p; 4308905a67cSAndy Whitcroft my $c; 4318905a67cSAndy Whitcroft my $len = 0; 43213214adfSAndy Whitcroft 43313214adfSAndy Whitcroft my $remainder; 4348905a67cSAndy Whitcroft while (1) { 435a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 436a2750645SAndy Whitcroft 437773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 4388905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 4398905a67cSAndy Whitcroft # context. 4408905a67cSAndy Whitcroft if ($off >= $len) { 4418905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 442dea33496SAndy Whitcroft last if (!defined $lines[$line]); 443c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 4448905a67cSAndy Whitcroft $remain--; 44513214adfSAndy Whitcroft $loff = $len; 446c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 4478905a67cSAndy Whitcroft $len = length($blk); 4488905a67cSAndy Whitcroft $line++; 4498905a67cSAndy Whitcroft last; 4508905a67cSAndy Whitcroft } 4518905a67cSAndy Whitcroft # Bail if there is no further context. 4528905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 45313214adfSAndy Whitcroft if ($off >= $len) { 4548905a67cSAndy Whitcroft last; 4558905a67cSAndy Whitcroft } 4568905a67cSAndy Whitcroft } 457cf655043SAndy Whitcroft $p = $c; 4588905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 45913214adfSAndy Whitcroft $remainder = substr($blk, $off); 4608905a67cSAndy Whitcroft 461773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 4624635f4fbSAndy Whitcroft 4634635f4fbSAndy Whitcroft # Handle nested #if/#else. 4644635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 4654635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 4664635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 4674635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 4684635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 4694635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 4704635f4fbSAndy Whitcroft } 4714635f4fbSAndy Whitcroft 4728905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 4738905a67cSAndy Whitcroft # outermost level. 4748905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 4758905a67cSAndy Whitcroft last; 4768905a67cSAndy Whitcroft } 4778905a67cSAndy Whitcroft 47813214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 479773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 480773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 481773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 482773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 483773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 484773647a0SAndy Whitcroft $coff_set = 1; 485773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 486773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 48713214adfSAndy Whitcroft } 48813214adfSAndy Whitcroft 4898905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 4908905a67cSAndy Whitcroft $level++; 4918905a67cSAndy Whitcroft $type = '('; 4928905a67cSAndy Whitcroft } 4938905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 4948905a67cSAndy Whitcroft $level--; 4958905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 4968905a67cSAndy Whitcroft 4978905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 4988905a67cSAndy Whitcroft $coff = $off; 499773647a0SAndy Whitcroft $coff_set = 1; 500773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 5018905a67cSAndy Whitcroft } 5028905a67cSAndy Whitcroft } 5038905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 5048905a67cSAndy Whitcroft $level++; 5058905a67cSAndy Whitcroft $type = '{'; 5068905a67cSAndy Whitcroft } 5078905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 5088905a67cSAndy Whitcroft $level--; 5098905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 5108905a67cSAndy Whitcroft 5118905a67cSAndy Whitcroft if ($level == 0) { 5128905a67cSAndy Whitcroft last; 5138905a67cSAndy Whitcroft } 5148905a67cSAndy Whitcroft } 5158905a67cSAndy Whitcroft $off++; 5168905a67cSAndy Whitcroft } 517a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 51813214adfSAndy Whitcroft if ($off == $len) { 519a3bb97a7SAndy Whitcroft $loff = $len + 1; 52013214adfSAndy Whitcroft $line++; 52113214adfSAndy Whitcroft $remain--; 52213214adfSAndy Whitcroft } 5238905a67cSAndy Whitcroft 5248905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 5258905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 5268905a67cSAndy Whitcroft 5278905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 5288905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 5298905a67cSAndy Whitcroft 530773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 53113214adfSAndy Whitcroft 53213214adfSAndy Whitcroft return ($statement, $condition, 53313214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 53413214adfSAndy Whitcroft} 53513214adfSAndy Whitcroft 536cf655043SAndy Whitcroftsub statement_lines { 537cf655043SAndy Whitcroft my ($stmt) = @_; 538cf655043SAndy Whitcroft 539cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 540cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 541cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 542cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 543cf655043SAndy Whitcroft 544cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 545cf655043SAndy Whitcroft 546cf655043SAndy Whitcroft return $#stmt_lines + 2; 547cf655043SAndy Whitcroft} 548cf655043SAndy Whitcroft 549cf655043SAndy Whitcroftsub statement_rawlines { 550cf655043SAndy Whitcroft my ($stmt) = @_; 551cf655043SAndy Whitcroft 552cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 553cf655043SAndy Whitcroft 554cf655043SAndy Whitcroft return $#stmt_lines + 2; 555cf655043SAndy Whitcroft} 556cf655043SAndy Whitcroft 557cf655043SAndy Whitcroftsub statement_block_size { 558cf655043SAndy Whitcroft my ($stmt) = @_; 559cf655043SAndy Whitcroft 560cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 561cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 562cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 563cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 564cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 565cf655043SAndy Whitcroft 566cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 567cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 568cf655043SAndy Whitcroft 569cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 570cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 571cf655043SAndy Whitcroft 572cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 573cf655043SAndy Whitcroft return $stmt_lines; 574cf655043SAndy Whitcroft } else { 575cf655043SAndy Whitcroft return $stmt_statements; 576cf655043SAndy Whitcroft } 577cf655043SAndy Whitcroft} 578cf655043SAndy Whitcroft 57913214adfSAndy Whitcroftsub ctx_statement_full { 58013214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 58113214adfSAndy Whitcroft my ($statement, $condition, $level); 58213214adfSAndy Whitcroft 58313214adfSAndy Whitcroft my (@chunks); 58413214adfSAndy Whitcroft 585cf655043SAndy Whitcroft # Grab the first conditional/block pair. 58613214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 58713214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 588773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 58913214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 590cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 591cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 592cf655043SAndy Whitcroft } 593cf655043SAndy Whitcroft 594cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 595cf655043SAndy Whitcroft # could continue the statement. 596cf655043SAndy Whitcroft for (;;) { 59713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 59813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 599cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 600773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 601cf655043SAndy Whitcroft #print "C: push\n"; 602cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 60313214adfSAndy Whitcroft } 60413214adfSAndy Whitcroft 60513214adfSAndy Whitcroft return ($level, $linenr, @chunks); 6068905a67cSAndy Whitcroft} 6078905a67cSAndy Whitcroft 6084a0df2efSAndy Whitcroftsub ctx_block_get { 609f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 6104a0df2efSAndy Whitcroft my $line; 6114a0df2efSAndy Whitcroft my $start = $linenr - 1; 6124a0df2efSAndy Whitcroft my $blk = ''; 6134a0df2efSAndy Whitcroft my @o; 6144a0df2efSAndy Whitcroft my @c; 6154a0df2efSAndy Whitcroft my @res = (); 6164a0df2efSAndy Whitcroft 617f0a594c1SAndy Whitcroft my $level = 0; 6184635f4fbSAndy Whitcroft my @stack = ($level); 61900df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 62000df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 62100df344fSAndy Whitcroft $remain--; 62200df344fSAndy Whitcroft 62300df344fSAndy Whitcroft $blk .= $rawlines[$line]; 6244635f4fbSAndy Whitcroft 6254635f4fbSAndy Whitcroft # Handle nested #if/#else. 6264635f4fbSAndy Whitcroft if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 6274635f4fbSAndy Whitcroft push(@stack, $level); 6284635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 6294635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 6304635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { 6314635f4fbSAndy Whitcroft $level = pop(@stack); 6324635f4fbSAndy Whitcroft } 6334635f4fbSAndy Whitcroft 634f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$line])) { 635f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 636f0a594c1SAndy Whitcroft if ($off > 0) { 637f0a594c1SAndy Whitcroft $off--; 638f0a594c1SAndy Whitcroft next; 639f0a594c1SAndy Whitcroft } 6404a0df2efSAndy Whitcroft 641f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 642f0a594c1SAndy Whitcroft $level--; 643f0a594c1SAndy Whitcroft last if ($level == 0); 644f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 645f0a594c1SAndy Whitcroft $level++; 646f0a594c1SAndy Whitcroft } 647f0a594c1SAndy Whitcroft } 6484a0df2efSAndy Whitcroft 649f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 65000df344fSAndy Whitcroft push(@res, $rawlines[$line]); 6514a0df2efSAndy Whitcroft } 6524a0df2efSAndy Whitcroft 653f0a594c1SAndy Whitcroft last if ($level == 0); 6544a0df2efSAndy Whitcroft } 6554a0df2efSAndy Whitcroft 656f0a594c1SAndy Whitcroft return ($level, @res); 6574a0df2efSAndy Whitcroft} 6584a0df2efSAndy Whitcroftsub ctx_block_outer { 6594a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6604a0df2efSAndy Whitcroft 661f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 662f0a594c1SAndy Whitcroft return @r; 6634a0df2efSAndy Whitcroft} 6644a0df2efSAndy Whitcroftsub ctx_block { 6654a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6664a0df2efSAndy Whitcroft 667f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 668f0a594c1SAndy Whitcroft return @r; 669653d4876SAndy Whitcroft} 670653d4876SAndy Whitcroftsub ctx_statement { 671f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 672f0a594c1SAndy Whitcroft 673f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 674f0a594c1SAndy Whitcroft return @r; 675f0a594c1SAndy Whitcroft} 676f0a594c1SAndy Whitcroftsub ctx_block_level { 677653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 678653d4876SAndy Whitcroft 679f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 6804a0df2efSAndy Whitcroft} 6819c0ca6f9SAndy Whitcroftsub ctx_statement_level { 6829c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 6839c0ca6f9SAndy Whitcroft 6849c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 6859c0ca6f9SAndy Whitcroft} 6864a0df2efSAndy Whitcroft 6874a0df2efSAndy Whitcroftsub ctx_locate_comment { 6884a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 6894a0df2efSAndy Whitcroft 6904a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 691beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 6924a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 6934a0df2efSAndy Whitcroft 6944a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 6954a0df2efSAndy Whitcroft # comment. 6964a0df2efSAndy Whitcroft my $in_comment = 0; 6974a0df2efSAndy Whitcroft $current_comment = ''; 6984a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 69900df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 70000df344fSAndy Whitcroft #warn " $line\n"; 7014a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 7024a0df2efSAndy Whitcroft $in_comment = 1; 7034a0df2efSAndy Whitcroft } 7044a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 7054a0df2efSAndy Whitcroft $in_comment = 1; 7064a0df2efSAndy Whitcroft } 7074a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 7084a0df2efSAndy Whitcroft $current_comment = ''; 7094a0df2efSAndy Whitcroft } 7104a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 7114a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 7124a0df2efSAndy Whitcroft $in_comment = 0; 7134a0df2efSAndy Whitcroft } 7144a0df2efSAndy Whitcroft } 7154a0df2efSAndy Whitcroft 7164a0df2efSAndy Whitcroft chomp($current_comment); 7174a0df2efSAndy Whitcroft return($current_comment); 7184a0df2efSAndy Whitcroft} 7194a0df2efSAndy Whitcroftsub ctx_has_comment { 7204a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7214a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 7224a0df2efSAndy Whitcroft 72300df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 7244a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 7254a0df2efSAndy Whitcroft 7264a0df2efSAndy Whitcroft return ($cmt ne ''); 7274a0df2efSAndy Whitcroft} 7284a0df2efSAndy Whitcroft 7294d001e4dSAndy Whitcroftsub raw_line { 7304d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 7314d001e4dSAndy Whitcroft 7324d001e4dSAndy Whitcroft my $offset = $linenr - 1; 7334d001e4dSAndy Whitcroft $cnt++; 7344d001e4dSAndy Whitcroft 7354d001e4dSAndy Whitcroft my $line; 7364d001e4dSAndy Whitcroft while ($cnt) { 7374d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 7384d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 7394d001e4dSAndy Whitcroft $cnt--; 7404d001e4dSAndy Whitcroft } 7414d001e4dSAndy Whitcroft 7424d001e4dSAndy Whitcroft return $line; 7434d001e4dSAndy Whitcroft} 7444d001e4dSAndy Whitcroft 7450a920b5bSAndy Whitcroftsub cat_vet { 7460a920b5bSAndy Whitcroft my ($vet) = @_; 7479c0ca6f9SAndy Whitcroft my ($res, $coded); 7480a920b5bSAndy Whitcroft 7499c0ca6f9SAndy Whitcroft $res = ''; 7506c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 7516c72ffaaSAndy Whitcroft $res .= $1; 7526c72ffaaSAndy Whitcroft if ($2 ne '') { 7539c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 7546c72ffaaSAndy Whitcroft $res .= $coded; 7556c72ffaaSAndy Whitcroft } 7569c0ca6f9SAndy Whitcroft } 7579c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 7580a920b5bSAndy Whitcroft 7599c0ca6f9SAndy Whitcroft return $res; 7600a920b5bSAndy Whitcroft} 7610a920b5bSAndy Whitcroft 762c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 763cf655043SAndy Whitcroftmy $av_pending; 764c2fdda0dSAndy Whitcroftmy @av_paren_type; 7651f65f947SAndy Whitcroftmy $av_pend_colon; 766c2fdda0dSAndy Whitcroft 767c2fdda0dSAndy Whitcroftsub annotate_reset { 768c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 769cf655043SAndy Whitcroft $av_pending = '_'; 770cf655043SAndy Whitcroft @av_paren_type = ('E'); 7711f65f947SAndy Whitcroft $av_pend_colon = 'O'; 772c2fdda0dSAndy Whitcroft} 773c2fdda0dSAndy Whitcroft 7746c72ffaaSAndy Whitcroftsub annotate_values { 7756c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 7766c72ffaaSAndy Whitcroft 7776c72ffaaSAndy Whitcroft my $res; 7781f65f947SAndy Whitcroft my $var = '_' x length($stream); 7796c72ffaaSAndy Whitcroft my $cur = $stream; 7806c72ffaaSAndy Whitcroft 781c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 7826c72ffaaSAndy Whitcroft 7836c72ffaaSAndy Whitcroft while (length($cur)) { 784773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 785cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 786171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 7876c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 788c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 789c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 790cf655043SAndy Whitcroft $type = pop(@av_paren_type); 791c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 7926c72ffaaSAndy Whitcroft } 7936c72ffaaSAndy Whitcroft 7948ea3eb9aSAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { 795c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 7966c72ffaaSAndy Whitcroft $type = 'T'; 7976c72ffaaSAndy Whitcroft 798389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 799389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 800389a2fe5SAndy Whitcroft $type = 'T'; 801389a2fe5SAndy Whitcroft 802c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 803171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 804c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 805171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 806171ae1a4SAndy Whitcroft if ($2 ne '') { 807cf655043SAndy Whitcroft $av_pending = 'N'; 808171ae1a4SAndy Whitcroft } 809171ae1a4SAndy Whitcroft $type = 'E'; 810171ae1a4SAndy Whitcroft 811c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 812171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 813171ae1a4SAndy Whitcroft $av_preprocessor = 1; 814171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 8156c72ffaaSAndy Whitcroft 816c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 817cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 818c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 819cf655043SAndy Whitcroft 820cf655043SAndy Whitcroft push(@av_paren_type, $type); 821cf655043SAndy Whitcroft push(@av_paren_type, $type); 822171ae1a4SAndy Whitcroft $type = 'E'; 823cf655043SAndy Whitcroft 824c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 825cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 826cf655043SAndy Whitcroft $av_preprocessor = 1; 827cf655043SAndy Whitcroft 828cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 829cf655043SAndy Whitcroft 830171ae1a4SAndy Whitcroft $type = 'E'; 831cf655043SAndy Whitcroft 832c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 833cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 834cf655043SAndy Whitcroft 835cf655043SAndy Whitcroft $av_preprocessor = 1; 836cf655043SAndy Whitcroft 837cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 838cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 839cf655043SAndy Whitcroft pop(@av_paren_type); 840cf655043SAndy Whitcroft push(@av_paren_type, $type); 841171ae1a4SAndy Whitcroft $type = 'E'; 8426c72ffaaSAndy Whitcroft 8436c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 844c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 8456c72ffaaSAndy Whitcroft 846171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 847171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 848171ae1a4SAndy Whitcroft $av_pending = $type; 849171ae1a4SAndy Whitcroft $type = 'N'; 850171ae1a4SAndy Whitcroft 8516c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 852c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 8536c72ffaaSAndy Whitcroft if (defined $2) { 854cf655043SAndy Whitcroft $av_pending = 'V'; 8556c72ffaaSAndy Whitcroft } 8566c72ffaaSAndy Whitcroft $type = 'N'; 8576c72ffaaSAndy Whitcroft 85814b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 859c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 86014b111c1SAndy Whitcroft $av_pending = 'E'; 8616c72ffaaSAndy Whitcroft $type = 'N'; 8626c72ffaaSAndy Whitcroft 8631f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 8641f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 8651f65f947SAndy Whitcroft $av_pend_colon = 'C'; 8661f65f947SAndy Whitcroft $type = 'N'; 8671f65f947SAndy Whitcroft 86814b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 869c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 8706c72ffaaSAndy Whitcroft $type = 'N'; 8716c72ffaaSAndy Whitcroft 8726c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 873c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 874cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 875cf655043SAndy Whitcroft $av_pending = '_'; 8766c72ffaaSAndy Whitcroft $type = 'N'; 8776c72ffaaSAndy Whitcroft 8786c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 879cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 880cf655043SAndy Whitcroft if ($new_type ne '_') { 881cf655043SAndy Whitcroft $type = $new_type; 882c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 883c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 8846c72ffaaSAndy Whitcroft } else { 885c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 8866c72ffaaSAndy Whitcroft } 8876c72ffaaSAndy Whitcroft 888c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 889c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 890c8cb2ca3SAndy Whitcroft $type = 'V'; 891cf655043SAndy Whitcroft $av_pending = 'V'; 8926c72ffaaSAndy Whitcroft 8938e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 8948e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 8951f65f947SAndy Whitcroft $av_pend_colon = 'B'; 8968e761b04SAndy Whitcroft } elsif ($type eq 'E') { 8978e761b04SAndy Whitcroft $av_pend_colon = 'L'; 8981f65f947SAndy Whitcroft } 8991f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 9001f65f947SAndy Whitcroft $type = 'V'; 9011f65f947SAndy Whitcroft 9026c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 903c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 9046c72ffaaSAndy Whitcroft $type = 'V'; 9056c72ffaaSAndy Whitcroft 9066c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 907c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 9086c72ffaaSAndy Whitcroft $type = 'N'; 9096c72ffaaSAndy Whitcroft 910cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 911c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 91213214adfSAndy Whitcroft $type = 'E'; 9131f65f947SAndy Whitcroft $av_pend_colon = 'O'; 91413214adfSAndy Whitcroft 9158e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 9168e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 9178e761b04SAndy Whitcroft $type = 'C'; 9188e761b04SAndy Whitcroft 9191f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 9201f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 9211f65f947SAndy Whitcroft $type = 'N'; 9221f65f947SAndy Whitcroft 9231f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 9241f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 9251f65f947SAndy Whitcroft 9261f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 9271f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 9281f65f947SAndy Whitcroft $type = 'E'; 9291f65f947SAndy Whitcroft } else { 9301f65f947SAndy Whitcroft $type = 'N'; 9311f65f947SAndy Whitcroft } 9321f65f947SAndy Whitcroft $av_pend_colon = 'O'; 9331f65f947SAndy Whitcroft 9348e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 93513214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 9366c72ffaaSAndy Whitcroft $type = 'N'; 9376c72ffaaSAndy Whitcroft 9380d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 93974048ed8SAndy Whitcroft my $variant; 94074048ed8SAndy Whitcroft 94174048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 94274048ed8SAndy Whitcroft if ($type eq 'V') { 94374048ed8SAndy Whitcroft $variant = 'B'; 94474048ed8SAndy Whitcroft } else { 94574048ed8SAndy Whitcroft $variant = 'U'; 94674048ed8SAndy Whitcroft } 94774048ed8SAndy Whitcroft 94874048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 94974048ed8SAndy Whitcroft $type = 'N'; 95074048ed8SAndy Whitcroft 9516c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 952c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 9536c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 9546c72ffaaSAndy Whitcroft $type = 'N'; 9556c72ffaaSAndy Whitcroft } 9566c72ffaaSAndy Whitcroft 9576c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 958c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 9596c72ffaaSAndy Whitcroft } 9606c72ffaaSAndy Whitcroft if (defined $1) { 9616c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 9626c72ffaaSAndy Whitcroft $res .= $type x length($1); 9636c72ffaaSAndy Whitcroft } 9646c72ffaaSAndy Whitcroft } 9656c72ffaaSAndy Whitcroft 9661f65f947SAndy Whitcroft return ($res, $var); 9676c72ffaaSAndy Whitcroft} 9686c72ffaaSAndy Whitcroft 9698905a67cSAndy Whitcroftsub possible { 97013214adfSAndy Whitcroft my ($possible, $line) = @_; 9718905a67cSAndy Whitcroft 9720776e594SAndy Whitcroft print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 9730776e594SAndy Whitcroft if ($possible !~ /(?: 9740776e594SAndy Whitcroft ^(?: 9750776e594SAndy Whitcroft $Modifier| 9760776e594SAndy Whitcroft $Storage| 9770776e594SAndy Whitcroft $Type| 9780776e594SAndy Whitcroft DEFINE_\S+| 9790776e594SAndy Whitcroft goto| 9800776e594SAndy Whitcroft return| 9810776e594SAndy Whitcroft case| 9820776e594SAndy Whitcroft else| 9830776e594SAndy Whitcroft asm|__asm__| 9840776e594SAndy Whitcroft do 9850776e594SAndy Whitcroft )$| 9860776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 9870776e594SAndy Whitcroft )/x) { 988c45dcabdSAndy Whitcroft # Check for modifiers. 989c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 990c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 991c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 992c45dcabdSAndy Whitcroft 993c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 994c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 995d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 996d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 997d2506586SAndy Whitcroft push(@modifierList, $modifier); 998d2506586SAndy Whitcroft } 999c45dcabdSAndy Whitcroft 1000c45dcabdSAndy Whitcroft } else { 100113214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 10028905a67cSAndy Whitcroft push(@typeList, $possible); 1003c45dcabdSAndy Whitcroft } 10048905a67cSAndy Whitcroft build_types(); 10050776e594SAndy Whitcroft } else { 10060776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 10078905a67cSAndy Whitcroft } 10088905a67cSAndy Whitcroft} 10098905a67cSAndy Whitcroft 10106c72ffaaSAndy Whitcroftmy $prefix = ''; 10116c72ffaaSAndy Whitcroft 1012f0a594c1SAndy Whitcroftsub report { 1013773647a0SAndy Whitcroft if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 1014773647a0SAndy Whitcroft return 0; 1015773647a0SAndy Whitcroft } 10168905a67cSAndy Whitcroft my $line = $prefix . $_[0]; 10178905a67cSAndy Whitcroft 10188905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 10198905a67cSAndy Whitcroft 102013214adfSAndy Whitcroft push(our @report, $line); 1021773647a0SAndy Whitcroft 1022773647a0SAndy Whitcroft return 1; 1023f0a594c1SAndy Whitcroft} 1024f0a594c1SAndy Whitcroftsub report_dump { 102513214adfSAndy Whitcroft our @report; 1026f0a594c1SAndy Whitcroft} 1027de7d4f0eSAndy Whitcroftsub ERROR { 1028773647a0SAndy Whitcroft if (report("ERROR: $_[0]\n")) { 1029de7d4f0eSAndy Whitcroft our $clean = 0; 10306c72ffaaSAndy Whitcroft our $cnt_error++; 1031de7d4f0eSAndy Whitcroft } 1032773647a0SAndy Whitcroft} 1033de7d4f0eSAndy Whitcroftsub WARN { 1034773647a0SAndy Whitcroft if (report("WARNING: $_[0]\n")) { 1035de7d4f0eSAndy Whitcroft our $clean = 0; 10366c72ffaaSAndy Whitcroft our $cnt_warn++; 1037de7d4f0eSAndy Whitcroft } 1038773647a0SAndy Whitcroft} 1039de7d4f0eSAndy Whitcroftsub CHK { 1040773647a0SAndy Whitcroft if ($check && report("CHECK: $_[0]\n")) { 1041de7d4f0eSAndy Whitcroft our $clean = 0; 10426c72ffaaSAndy Whitcroft our $cnt_chk++; 10436c72ffaaSAndy Whitcroft } 1044de7d4f0eSAndy Whitcroft} 1045de7d4f0eSAndy Whitcroft 10466ecd9674SAndy Whitcroftsub check_absolute_file { 10476ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 10486ecd9674SAndy Whitcroft my $file = $absolute; 10496ecd9674SAndy Whitcroft 10506ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 10516ecd9674SAndy Whitcroft 10526ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 10536ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 10546ecd9674SAndy Whitcroft if (-f "$root/$file") { 10556ecd9674SAndy Whitcroft ##print "file<$file>\n"; 10566ecd9674SAndy Whitcroft last; 10576ecd9674SAndy Whitcroft } 10586ecd9674SAndy Whitcroft } 10596ecd9674SAndy Whitcroft if (! -f _) { 10606ecd9674SAndy Whitcroft return 0; 10616ecd9674SAndy Whitcroft } 10626ecd9674SAndy Whitcroft 10636ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 10646ecd9674SAndy Whitcroft my $prefix = $absolute; 10656ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 10666ecd9674SAndy Whitcroft 10676ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 10686ecd9674SAndy Whitcroft if ($prefix ne ".../") { 10696ecd9674SAndy Whitcroft WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); 10706ecd9674SAndy Whitcroft } 10716ecd9674SAndy Whitcroft} 10726ecd9674SAndy Whitcroft 10730a920b5bSAndy Whitcroftsub process { 10740a920b5bSAndy Whitcroft my $filename = shift; 10750a920b5bSAndy Whitcroft 10760a920b5bSAndy Whitcroft my $linenr=0; 10770a920b5bSAndy Whitcroft my $prevline=""; 1078c2fdda0dSAndy Whitcroft my $prevrawline=""; 10790a920b5bSAndy Whitcroft my $stashline=""; 1080c2fdda0dSAndy Whitcroft my $stashrawline=""; 10810a920b5bSAndy Whitcroft 10824a0df2efSAndy Whitcroft my $length; 10830a920b5bSAndy Whitcroft my $indent; 10840a920b5bSAndy Whitcroft my $previndent=0; 10850a920b5bSAndy Whitcroft my $stashindent=0; 10860a920b5bSAndy Whitcroft 1087de7d4f0eSAndy Whitcroft our $clean = 1; 10880a920b5bSAndy Whitcroft my $signoff = 0; 10890a920b5bSAndy Whitcroft my $is_patch = 0; 10900a920b5bSAndy Whitcroft 109113214adfSAndy Whitcroft our @report = (); 10926c72ffaaSAndy Whitcroft our $cnt_lines = 0; 10936c72ffaaSAndy Whitcroft our $cnt_error = 0; 10946c72ffaaSAndy Whitcroft our $cnt_warn = 0; 10956c72ffaaSAndy Whitcroft our $cnt_chk = 0; 10966c72ffaaSAndy Whitcroft 10970a920b5bSAndy Whitcroft # Trace the real file/line as we go. 10980a920b5bSAndy Whitcroft my $realfile = ''; 10990a920b5bSAndy Whitcroft my $realline = 0; 11000a920b5bSAndy Whitcroft my $realcnt = 0; 11010a920b5bSAndy Whitcroft my $here = ''; 11020a920b5bSAndy Whitcroft my $in_comment = 0; 1103c2fdda0dSAndy Whitcroft my $comment_edge = 0; 11040a920b5bSAndy Whitcroft my $first_line = 0; 11051e855726SWolfram Sang my $p1_prefix = ''; 11060a920b5bSAndy Whitcroft 110713214adfSAndy Whitcroft my $prev_values = 'E'; 110813214adfSAndy Whitcroft 110913214adfSAndy Whitcroft # suppression flags 1110773647a0SAndy Whitcroft my %suppress_ifbraces; 1111170d3a22SAndy Whitcroft my %suppress_whiletrailers; 1112653d4876SAndy Whitcroft 1113c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1114de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1115c2fdda0dSAndy Whitcroft # 1116de7d4f0eSAndy Whitcroft my @setup_docs = (); 1117de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1118773647a0SAndy Whitcroft 1119773647a0SAndy Whitcroft sanitise_line_reset(); 1120c2fdda0dSAndy Whitcroft my $line; 1121c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1122773647a0SAndy Whitcroft $linenr++; 1123773647a0SAndy Whitcroft $line = $rawline; 1124c2fdda0dSAndy Whitcroft 1125773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1126de7d4f0eSAndy Whitcroft $setup_docs = 0; 1127de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1128de7d4f0eSAndy Whitcroft $setup_docs = 1; 1129de7d4f0eSAndy Whitcroft } 1130773647a0SAndy Whitcroft #next; 1131de7d4f0eSAndy Whitcroft } 1132773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1133773647a0SAndy Whitcroft $realline=$1-1; 1134773647a0SAndy Whitcroft if (defined $2) { 1135773647a0SAndy Whitcroft $realcnt=$3+1; 1136773647a0SAndy Whitcroft } else { 1137773647a0SAndy Whitcroft $realcnt=1+1; 1138773647a0SAndy Whitcroft } 1139c45dcabdSAndy Whitcroft $in_comment = 0; 1140773647a0SAndy Whitcroft 1141773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1142773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1143773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1144773647a0SAndy Whitcroft # at context start. 1145773647a0SAndy Whitcroft my $edge; 114601fa9147SAndy Whitcroft my $cnt = $realcnt; 114701fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 114801fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 114901fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 115001fa9147SAndy Whitcroft $cnt--; 115101fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1152721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1153fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1154fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1155fae17daeSAndy Whitcroft ($edge) = $1; 1156fae17daeSAndy Whitcroft last; 1157fae17daeSAndy Whitcroft } 1158773647a0SAndy Whitcroft } 1159773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1160773647a0SAndy Whitcroft $in_comment = 1; 1161773647a0SAndy Whitcroft } 1162773647a0SAndy Whitcroft 1163773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1164773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1165773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1166773647a0SAndy Whitcroft if (!defined $edge && 116783242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1168773647a0SAndy Whitcroft { 1169773647a0SAndy Whitcroft $in_comment = 1; 1170773647a0SAndy Whitcroft } 1171773647a0SAndy Whitcroft 1172773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1173773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1174773647a0SAndy Whitcroft 1175171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1176773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1177171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1178773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1179773647a0SAndy Whitcroft } 1180773647a0SAndy Whitcroft push(@lines, $line); 1181773647a0SAndy Whitcroft 1182773647a0SAndy Whitcroft if ($realcnt > 1) { 1183773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1184773647a0SAndy Whitcroft } else { 1185773647a0SAndy Whitcroft $realcnt = 0; 1186773647a0SAndy Whitcroft } 1187773647a0SAndy Whitcroft 1188773647a0SAndy Whitcroft #print "==>$rawline\n"; 1189773647a0SAndy Whitcroft #print "-->$line\n"; 1190de7d4f0eSAndy Whitcroft 1191de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1192de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1193de7d4f0eSAndy Whitcroft } 1194de7d4f0eSAndy Whitcroft } 1195de7d4f0eSAndy Whitcroft 11966c72ffaaSAndy Whitcroft $prefix = ''; 11976c72ffaaSAndy Whitcroft 1198773647a0SAndy Whitcroft $realcnt = 0; 1199773647a0SAndy Whitcroft $linenr = 0; 12000a920b5bSAndy Whitcroft foreach my $line (@lines) { 12010a920b5bSAndy Whitcroft $linenr++; 12020a920b5bSAndy Whitcroft 1203c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 120430670854SAndy Whitcroft my $hunk_line = ($realcnt != 0); 12056c72ffaaSAndy Whitcroft 12060a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 12076c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 12080a920b5bSAndy Whitcroft $is_patch = 1; 12094a0df2efSAndy Whitcroft $first_line = $linenr + 1; 12100a920b5bSAndy Whitcroft $realline=$1-1; 12110a920b5bSAndy Whitcroft if (defined $2) { 12120a920b5bSAndy Whitcroft $realcnt=$3+1; 12130a920b5bSAndy Whitcroft } else { 12140a920b5bSAndy Whitcroft $realcnt=1+1; 12150a920b5bSAndy Whitcroft } 1216c2fdda0dSAndy Whitcroft annotate_reset(); 121713214adfSAndy Whitcroft $prev_values = 'E'; 121813214adfSAndy Whitcroft 1219773647a0SAndy Whitcroft %suppress_ifbraces = (); 1220170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 12210a920b5bSAndy Whitcroft next; 12220a920b5bSAndy Whitcroft 12234a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 12244a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 12254a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1226773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 12270a920b5bSAndy Whitcroft $realline++; 1228d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 12290a920b5bSAndy Whitcroft 12304a0df2efSAndy Whitcroft # Measure the line length and indent. 1231c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 12320a920b5bSAndy Whitcroft 12330a920b5bSAndy Whitcroft # Track the previous line. 12340a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 12350a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1236c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1237c2fdda0dSAndy Whitcroft 1238773647a0SAndy Whitcroft #warn "line<$line>\n"; 12396c72ffaaSAndy Whitcroft 1240d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1241d8aaf121SAndy Whitcroft $realcnt--; 12420a920b5bSAndy Whitcroft } 12430a920b5bSAndy Whitcroft 12440a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1245773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1246773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1247773647a0SAndy Whitcroft 12486c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 12496c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1250773647a0SAndy Whitcroft 1251773647a0SAndy Whitcroft # extract the filename as it passes 1252773647a0SAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 1253773647a0SAndy Whitcroft $realfile = $1; 12541e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 12551e855726SWolfram Sang 12561e855726SWolfram Sang $p1_prefix = $1; 1257e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1258e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 12591e855726SWolfram Sang WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 12601e855726SWolfram Sang } 1261773647a0SAndy Whitcroft 1262c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1263773647a0SAndy Whitcroft ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1264773647a0SAndy Whitcroft } 1265773647a0SAndy Whitcroft next; 1266773647a0SAndy Whitcroft } 1267773647a0SAndy Whitcroft 1268389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 12690a920b5bSAndy Whitcroft 1270c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1271c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1272c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 12730a920b5bSAndy Whitcroft 12746c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 12756c72ffaaSAndy Whitcroft 12760a920b5bSAndy Whitcroft#check the patch for a signoff: 1277d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 12784a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 12794a0df2efSAndy Whitcroft $signoff++; 12800a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 1281de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 1282de7d4f0eSAndy Whitcroft $herecurr); 12830a920b5bSAndy Whitcroft } 12840a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 1285773647a0SAndy Whitcroft WARN("space required after Signed-off-by:\n" . 1286de7d4f0eSAndy Whitcroft $herecurr); 12870a920b5bSAndy Whitcroft } 12880a920b5bSAndy Whitcroft } 12890a920b5bSAndy Whitcroft 129000df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 12918905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1292de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 12936c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1294de7d4f0eSAndy Whitcroft } 1295de7d4f0eSAndy Whitcroft 12966ecd9674SAndy Whitcroft# Check for absolute kernel paths. 12976ecd9674SAndy Whitcroft if ($tree) { 12986ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 12996ecd9674SAndy Whitcroft my $file = $1; 13006ecd9674SAndy Whitcroft 13016ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 13026ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 13036ecd9674SAndy Whitcroft # 13046ecd9674SAndy Whitcroft } else { 13056ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 13066ecd9674SAndy Whitcroft } 13076ecd9674SAndy Whitcroft } 13086ecd9674SAndy Whitcroft } 13096ecd9674SAndy Whitcroft 1310de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1311de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1312171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1313171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1314171ae1a4SAndy Whitcroft 1315171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1316171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1317171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1318171ae1a4SAndy Whitcroft 1319171ae1a4SAndy Whitcroft ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 132000df344fSAndy Whitcroft } 13210a920b5bSAndy Whitcroft 132230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 132330670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 132400df344fSAndy Whitcroft 13250a920b5bSAndy Whitcroft#trailing whitespace 13269c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1327c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 13289c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 13299c0ca6f9SAndy Whitcroft 1330c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1331c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1332de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 13330a920b5bSAndy Whitcroft } 13345368df20SAndy Whitcroft 13355368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 13365368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 13375368df20SAndy Whitcroft 13380a920b5bSAndy Whitcroft#80 column limit 1339c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1340f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1341f4c014c0SAndy Whitcroft $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && 1342f4c014c0SAndy Whitcroft $length > 80) 1343c45dcabdSAndy Whitcroft { 1344de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 13450a920b5bSAndy Whitcroft } 13460a920b5bSAndy Whitcroft 13478905a67cSAndy Whitcroft# check for adding lines without a newline. 13488905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 13498905a67cSAndy Whitcroft WARN("adding a line without newline at end of file\n" . $herecurr); 13508905a67cSAndy Whitcroft } 13518905a67cSAndy Whitcroft 1352b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1353b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 13540a920b5bSAndy Whitcroft 13550a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 13560a920b5bSAndy Whitcroft# more than 8 must use tabs. 1357c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1358c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1359c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1360171ae1a4SAndy Whitcroft ERROR("code indent should use tabs where possible\n" . $herevet); 13610a920b5bSAndy Whitcroft } 13620a920b5bSAndy Whitcroft 1363b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1364b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1365b9ea10d6SAndy Whitcroft 1366c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1367cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1368c2fdda0dSAndy Whitcroft WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1369c2fdda0dSAndy Whitcroft } 137022f2a2efSAndy Whitcroft 13719c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 1372170d3a22SAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next); 13739c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1374170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1375f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1376171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1377171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1378171ae1a4SAndy Whitcroft 1379171ae1a4SAndy Whitcroft my $s = $stat; 1380171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1381cf655043SAndy Whitcroft 1382c2fdda0dSAndy Whitcroft # Ignore goto labels. 1383171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1384c2fdda0dSAndy Whitcroft 1385c2fdda0dSAndy Whitcroft # Ignore functions being called 1386171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1387c2fdda0dSAndy Whitcroft 1388463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1389463f2864SAndy Whitcroft 1390c45dcabdSAndy Whitcroft # declarations always start with types 1391d2506586SAndy Whitcroft } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) { 1392c45dcabdSAndy Whitcroft my $type = $1; 1393c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1394c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1395c45dcabdSAndy Whitcroft 13966c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1397a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1398c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1399c2fdda0dSAndy Whitcroft } 14008905a67cSAndy Whitcroft 14016c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 140265863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1403c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 14049c0ca6f9SAndy Whitcroft } 14058905a67cSAndy Whitcroft 14068905a67cSAndy Whitcroft # Check for any sort of function declaration. 14078905a67cSAndy Whitcroft # int foo(something bar, other baz); 14088905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1409171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 14108905a67cSAndy Whitcroft my ($name_len) = length($1); 14118905a67cSAndy Whitcroft 1412cf655043SAndy Whitcroft my $ctx = $s; 1413773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 14148905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1415cf655043SAndy Whitcroft 14168905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1417c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 14188905a67cSAndy Whitcroft 1419c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 14208905a67cSAndy Whitcroft } 14218905a67cSAndy Whitcroft } 14228905a67cSAndy Whitcroft } 14238905a67cSAndy Whitcroft 14249c0ca6f9SAndy Whitcroft } 14259c0ca6f9SAndy Whitcroft 142600df344fSAndy Whitcroft# 142700df344fSAndy Whitcroft# Checks which may be anchored in the context. 142800df344fSAndy Whitcroft# 142900df344fSAndy Whitcroft 143000df344fSAndy Whitcroft# Check for switch () and associated case and default 143100df344fSAndy Whitcroft# statements should be at the same indent. 143200df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 143300df344fSAndy Whitcroft my $err = ''; 143400df344fSAndy Whitcroft my $sep = ''; 143500df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 143600df344fSAndy Whitcroft shift(@ctx); 143700df344fSAndy Whitcroft for my $ctx (@ctx) { 143800df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 143900df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 144000df344fSAndy Whitcroft $indent != $cindent) { 144100df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 144200df344fSAndy Whitcroft $sep = ''; 144300df344fSAndy Whitcroft } else { 144400df344fSAndy Whitcroft $sep = "[...]\n"; 144500df344fSAndy Whitcroft } 144600df344fSAndy Whitcroft } 144700df344fSAndy Whitcroft if ($err ne '') { 14489c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 1449de7d4f0eSAndy Whitcroft } 1450de7d4f0eSAndy Whitcroft } 1451de7d4f0eSAndy Whitcroft 1452de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1453de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1454c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1455773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1456773647a0SAndy Whitcroft 14579c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1458de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1459de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1460de7d4f0eSAndy Whitcroft 1461548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1462548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1463de7d4f0eSAndy Whitcroft 1464548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1465548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1466548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1467548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1468548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1469773647a0SAndy Whitcroft $ctx_ln++; 1470773647a0SAndy Whitcroft } 1471548596d5SAndy Whitcroft 147253210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 147353210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1474773647a0SAndy Whitcroft 1475773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1476773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . 1477c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 147800df344fSAndy Whitcroft } 1479773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1480773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1481773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1482773647a0SAndy Whitcroft { 14839c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 14849c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1485773647a0SAndy Whitcroft WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1486c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 14879c0ca6f9SAndy Whitcroft } 14889c0ca6f9SAndy Whitcroft } 148900df344fSAndy Whitcroft } 149000df344fSAndy Whitcroft 14914d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 14924d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 14934d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 14944d001e4dSAndy Whitcroft 14954d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 14964d001e4dSAndy Whitcroft 14974d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 14984d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 14994d001e4dSAndy Whitcroft # where necessary. 15004d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 15014d001e4dSAndy Whitcroft 15024d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 15036f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 15046f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 15054d001e4dSAndy Whitcroft 15064d001e4dSAndy Whitcroft # We want to check the first line inside the block 15074d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 15084d001e4dSAndy Whitcroft # 1) any blank line termination 15094d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 15104d001e4dSAndy Whitcroft # 3) any do (...) { 15114d001e4dSAndy Whitcroft my $continuation = 0; 15124d001e4dSAndy Whitcroft my $check = 0; 15134d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 15144d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 15154d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 15164d001e4dSAndy Whitcroft $continuation = 1; 15174d001e4dSAndy Whitcroft } 15189bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 15194d001e4dSAndy Whitcroft $check = 1; 15204d001e4dSAndy Whitcroft $cond_lines++; 15214d001e4dSAndy Whitcroft } 15224d001e4dSAndy Whitcroft 15234d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 15244d001e4dSAndy Whitcroft # preprocessor statement. 15254d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 15264d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 15274d001e4dSAndy Whitcroft $check = 0; 15284d001e4dSAndy Whitcroft } 15294d001e4dSAndy Whitcroft 15309bd49efeSAndy Whitcroft my $cond_ptr = -1; 1531740504c6SAndy Whitcroft $continuation = 0; 15329bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 15339bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 15344d001e4dSAndy Whitcroft 1535f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 1536f16fa28fSAndy Whitcroft # is not linear. 1537f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1538f16fa28fSAndy Whitcroft $check = 0; 1539f16fa28fSAndy Whitcroft } 1540f16fa28fSAndy Whitcroft 15419bd49efeSAndy Whitcroft # Ignore: 15429bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 15439bd49efeSAndy Whitcroft # 2) preprocessor lines, and 15449bd49efeSAndy Whitcroft # 3) labels. 1545740504c6SAndy Whitcroft if ($continuation || 1546740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 15479bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 15489bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 1549740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 1550*30dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 15519bd49efeSAndy Whitcroft $cond_lines++; 15529bd49efeSAndy Whitcroft } 15534d001e4dSAndy Whitcroft } 1554*30dad6ebSAndy Whitcroft } 15554d001e4dSAndy Whitcroft 15564d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 15574d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 15584d001e4dSAndy Whitcroft 15594d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 15604d001e4dSAndy Whitcroft # this is not this patch's fault. 15614d001e4dSAndy Whitcroft if (!defined($stat_real) || 15624d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 15634d001e4dSAndy Whitcroft $check = 0; 15644d001e4dSAndy Whitcroft } 15654d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 15664d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 15674d001e4dSAndy Whitcroft } 15684d001e4dSAndy Whitcroft 15699bd49efeSAndy Whitcroft #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n"; 15704d001e4dSAndy Whitcroft 15714d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 15724d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 15734d001e4dSAndy Whitcroft WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 15744d001e4dSAndy Whitcroft } 15754d001e4dSAndy Whitcroft } 15764d001e4dSAndy Whitcroft 15776c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 15786c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 15791f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 15801f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 15816c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1582c2fdda0dSAndy Whitcroft if ($dbg_values) { 1583c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1584cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1585cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 15861f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 1587c2fdda0dSAndy Whitcroft } 15886c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 15896c72ffaaSAndy Whitcroft 159000df344fSAndy Whitcroft#ignore lines not being added 159100df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 159200df344fSAndy Whitcroft 1593653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 15947429c690SAndy Whitcroft if ($dbg_type) { 15957429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 15967429c690SAndy Whitcroft ERROR("TEST: is type\n" . $herecurr); 15977429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 15987429c690SAndy Whitcroft ERROR("TEST: is not type ($1 is)\n". $herecurr); 15997429c690SAndy Whitcroft } 1600653d4876SAndy Whitcroft next; 1601653d4876SAndy Whitcroft } 1602a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 1603a1ef277eSAndy Whitcroft if ($dbg_attr) { 16049360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 1605a1ef277eSAndy Whitcroft ERROR("TEST: is attr\n" . $herecurr); 16069360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 1607a1ef277eSAndy Whitcroft ERROR("TEST: is not attr ($1 is)\n". $herecurr); 1608a1ef277eSAndy Whitcroft } 1609a1ef277eSAndy Whitcroft next; 1610a1ef277eSAndy Whitcroft } 1611653d4876SAndy Whitcroft 1612f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 1613f0a594c1SAndy Whitcroft if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1614f0a594c1SAndy Whitcroft $line =~ /^.\s*{/) { 1615773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . $hereprev); 1616f0a594c1SAndy Whitcroft } 1617f0a594c1SAndy Whitcroft 161800df344fSAndy Whitcroft# 161900df344fSAndy Whitcroft# Checks which are anchored on the added line. 162000df344fSAndy Whitcroft# 162100df344fSAndy Whitcroft 1622653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 1623c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1624653d4876SAndy Whitcroft my $path = $1; 1625653d4876SAndy Whitcroft if ($path =~ m{//}) { 1626de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 1627de7d4f0eSAndy Whitcroft $herecurr); 1628653d4876SAndy Whitcroft } 1629653d4876SAndy Whitcroft } 1630653d4876SAndy Whitcroft 163100df344fSAndy Whitcroft# no C99 // comments 163200df344fSAndy Whitcroft if ($line =~ m{//}) { 1633de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 163400df344fSAndy Whitcroft } 163500df344fSAndy Whitcroft # Remove C99 comments. 16360a920b5bSAndy Whitcroft $line =~ s@//.*@@; 16376c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 16380a920b5bSAndy Whitcroft 16390a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 1640653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 1641653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1642653d4876SAndy Whitcroft my $name = $1; 164348012058SAndy Whitcroft if ($prevline !~ /(?: 164448012058SAndy Whitcroft ^.}| 164548012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 164648012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 164748012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 164848012058SAndy Whitcroft ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 164948012058SAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[) 165048012058SAndy Whitcroft )/x) { 1651de7d4f0eSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 16520a920b5bSAndy Whitcroft } 16530a920b5bSAndy Whitcroft } 16540a920b5bSAndy Whitcroft 1655f0a594c1SAndy Whitcroft# check for external initialisers. 1656c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1657f0a594c1SAndy Whitcroft ERROR("do not initialise externals to 0 or NULL\n" . 1658f0a594c1SAndy Whitcroft $herecurr); 1659f0a594c1SAndy Whitcroft } 16600a920b5bSAndy Whitcroft# check for static initialisers. 16612d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 1662de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 1663de7d4f0eSAndy Whitcroft $herecurr); 16640a920b5bSAndy Whitcroft } 16650a920b5bSAndy Whitcroft 1666653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1667653d4876SAndy Whitcroft# make sense. 1668653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 16698054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 1670c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 16718ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 1672653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1673de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 16740a920b5bSAndy Whitcroft } 16750a920b5bSAndy Whitcroft 16760a920b5bSAndy Whitcroft# * goes on variable not on type 167765863862SAndy Whitcroft # (char*[ const]) 167800ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 167965863862SAndy Whitcroft my ($from, $to) = ($1, $1); 1680d8aaf121SAndy Whitcroft 168165863862SAndy Whitcroft # Should start with a space. 168265863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 168365863862SAndy Whitcroft # Should not end with a space. 168465863862SAndy Whitcroft $to =~ s/\s+$//; 168565863862SAndy Whitcroft # '*'s should not have spaces between. 1686f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 168765863862SAndy Whitcroft } 1688d8aaf121SAndy Whitcroft 168965863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 169065863862SAndy Whitcroft if ($from ne $to) { 169165863862SAndy Whitcroft ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 169265863862SAndy Whitcroft } 169300ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 169465863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 1695d8aaf121SAndy Whitcroft 169665863862SAndy Whitcroft # Should start with a space. 169765863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 169865863862SAndy Whitcroft # Should not end with a space. 169965863862SAndy Whitcroft $to =~ s/\s+$//; 170065863862SAndy Whitcroft # '*'s should not have spaces between. 1701f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 170265863862SAndy Whitcroft } 170365863862SAndy Whitcroft # Modifiers should have spaces. 170465863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 170565863862SAndy Whitcroft 1706667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 1707667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 170865863862SAndy Whitcroft ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 170965863862SAndy Whitcroft } 17100a920b5bSAndy Whitcroft } 17110a920b5bSAndy Whitcroft 17120a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 17130a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 17140a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 17150a920b5bSAndy Whitcroft# print "$herecurr"; 17160a920b5bSAndy Whitcroft# $clean = 0; 17170a920b5bSAndy Whitcroft# } 17180a920b5bSAndy Whitcroft 17198905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1720c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 17218905a67cSAndy Whitcroft } 17228905a67cSAndy Whitcroft 172300df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 172400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 172500df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 172600df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 172700df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1728f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 172900df344fSAndy Whitcroft my $ok = 0; 173000df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 173100df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 173200df344fSAndy Whitcroft # we have a preceeding printk if it ends 173300df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 173400df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 173500df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 173600df344fSAndy Whitcroft $ok = 1; 173700df344fSAndy Whitcroft } 173800df344fSAndy Whitcroft last; 173900df344fSAndy Whitcroft } 174000df344fSAndy Whitcroft } 174100df344fSAndy Whitcroft if ($ok == 0) { 1742de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 17430a920b5bSAndy Whitcroft } 174400df344fSAndy Whitcroft } 17450a920b5bSAndy Whitcroft 1746653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1747653d4876SAndy Whitcroft# or if closed on same line 1748c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1749c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1750de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 17510a920b5bSAndy Whitcroft } 1752653d4876SAndy Whitcroft 17538905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 17548905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 17558905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 17568905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 17578905a67cSAndy Whitcroft } 17588905a67cSAndy Whitcroft 17598d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 17608d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1761fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1762fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 17638d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 17648d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 17658d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1766fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 1767fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 17688d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 17698d31cfceSAndy Whitcroft } 17708d31cfceSAndy Whitcroft } 17718d31cfceSAndy Whitcroft 1772f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 17736c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 1774c2fdda0dSAndy Whitcroft my $name = $1; 1775773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 1776773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 1777c2fdda0dSAndy Whitcroft 1778c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 1779773647a0SAndy Whitcroft if ($name =~ /^(?: 1780773647a0SAndy Whitcroft if|for|while|switch|return|case| 1781773647a0SAndy Whitcroft volatile|__volatile__| 1782773647a0SAndy Whitcroft __attribute__|format|__extension__| 1783773647a0SAndy Whitcroft asm|__asm__)$/x) 1784773647a0SAndy Whitcroft { 1785c2fdda0dSAndy Whitcroft 1786c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 1787c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 1788c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 1789c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 1790773647a0SAndy Whitcroft 1791773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 1792c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 1793c2fdda0dSAndy Whitcroft 1794c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 1795c2fdda0dSAndy Whitcroft # likely a typedef for a function. 1796773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 1797c2fdda0dSAndy Whitcroft 1798c2fdda0dSAndy Whitcroft } else { 1799773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 1800f0a594c1SAndy Whitcroft } 18016c72ffaaSAndy Whitcroft } 1802653d4876SAndy Whitcroft# Check operator spacing. 18030a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 18049c0ca6f9SAndy Whitcroft my $ops = qr{ 18059c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 18069c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 18079c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 18081f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 18091f65f947SAndy Whitcroft \?|: 18109c0ca6f9SAndy Whitcroft }x; 1811cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 181200df344fSAndy Whitcroft my $off = 0; 18136c72ffaaSAndy Whitcroft 18146c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 18156c72ffaaSAndy Whitcroft 18160a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 18174a0df2efSAndy Whitcroft $off += length($elements[$n]); 18184a0df2efSAndy Whitcroft 1819773647a0SAndy Whitcroft # Pick up the preceeding and succeeding characters. 1820773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 1821773647a0SAndy Whitcroft my $cc = ''; 1822773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 1823773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 1824773647a0SAndy Whitcroft } 1825773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 1826773647a0SAndy Whitcroft 18274a0df2efSAndy Whitcroft my $a = ''; 18284a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 18294a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 1830cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 18314a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 18324a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 1833773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 18344a0df2efSAndy Whitcroft 18350a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 18364a0df2efSAndy Whitcroft 18374a0df2efSAndy Whitcroft my $c = ''; 18380a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 18394a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 18404a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1841cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 18424a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 18434a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 18448b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 18454a0df2efSAndy Whitcroft } else { 18464a0df2efSAndy Whitcroft $c = 'E'; 18470a920b5bSAndy Whitcroft } 18480a920b5bSAndy Whitcroft 18494a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 18504a0df2efSAndy Whitcroft 18514a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 18524a0df2efSAndy Whitcroft 18536c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 1854de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 18550a920b5bSAndy Whitcroft 185674048ed8SAndy Whitcroft # Pull out the value of this operator. 18576c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 18580a920b5bSAndy Whitcroft 18591f65f947SAndy Whitcroft # Get the full operator variant. 18601f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 18611f65f947SAndy Whitcroft 186213214adfSAndy Whitcroft # Ignore operators passed as parameters. 186313214adfSAndy Whitcroft if ($op_type ne 'V' && 186413214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 186513214adfSAndy Whitcroft 1866cf655043SAndy Whitcroft# # Ignore comments 1867cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 186813214adfSAndy Whitcroft 1869d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 187013214adfSAndy Whitcroft } elsif ($op eq ';') { 1871cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 1872cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 1873773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 1874d8aaf121SAndy Whitcroft } 1875d8aaf121SAndy Whitcroft 1876d8aaf121SAndy Whitcroft # // is a comment 1877d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 18780a920b5bSAndy Whitcroft 18791f65f947SAndy Whitcroft # No spaces for: 18801f65f947SAndy Whitcroft # -> 18811f65f947SAndy Whitcroft # : when part of a bitfield 18821f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 18834a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 1884773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 18850a920b5bSAndy Whitcroft } 18860a920b5bSAndy Whitcroft 18870a920b5bSAndy Whitcroft # , must have a space on the right. 18880a920b5bSAndy Whitcroft } elsif ($op eq ',') { 1889cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 1890773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 18910a920b5bSAndy Whitcroft } 18920a920b5bSAndy Whitcroft 18939c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 189474048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 18959c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 18969c0ca6f9SAndy Whitcroft 18979c0ca6f9SAndy Whitcroft # unary operators should have a space before and 18989c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 18999c0ca6f9SAndy Whitcroft # unary operator, or a cast 19009c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 190174048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 19020d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 1903cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1904773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 19050a920b5bSAndy Whitcroft } 1906a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 1907171ae1a4SAndy Whitcroft # A unary '*' may be const 1908171ae1a4SAndy Whitcroft 1909171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 1910a3340b35SAndy Whitcroft ERROR("Aspace prohibited after that '$op' $at\n" . $hereptr); 19110a920b5bSAndy Whitcroft } 19120a920b5bSAndy Whitcroft 19130a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 19140a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 1915773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 1916773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 19170a920b5bSAndy Whitcroft } 1918773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 1919773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1920773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 1921653d4876SAndy Whitcroft } 1922773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 1923773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 1924773647a0SAndy Whitcroft } 1925773647a0SAndy Whitcroft 19260a920b5bSAndy Whitcroft 19270a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 19289c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 19299c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 19309c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 1931c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 1932c2fdda0dSAndy Whitcroft $op eq '%') 19330a920b5bSAndy Whitcroft { 1934773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 1935de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 1936de7d4f0eSAndy Whitcroft $hereptr); 19370a920b5bSAndy Whitcroft } 19380a920b5bSAndy Whitcroft 19391f65f947SAndy Whitcroft # A colon needs no spaces before when it is 19401f65f947SAndy Whitcroft # terminating a case value or a label. 19411f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 19421f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 19431f65f947SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 19441f65f947SAndy Whitcroft } 19451f65f947SAndy Whitcroft 19460a920b5bSAndy Whitcroft # All the others need spaces both sides. 1947cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 19481f65f947SAndy Whitcroft my $ok = 0; 19491f65f947SAndy Whitcroft 195022f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 19511f65f947SAndy Whitcroft if (($op eq '<' && 19521f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 19531f65f947SAndy Whitcroft ($op eq '>' && 19541f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 19551f65f947SAndy Whitcroft { 19561f65f947SAndy Whitcroft $ok = 1; 19571f65f947SAndy Whitcroft } 19581f65f947SAndy Whitcroft 19591f65f947SAndy Whitcroft # Ignore ?: 19601f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 19611f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 19621f65f947SAndy Whitcroft $ok = 1; 19631f65f947SAndy Whitcroft } 19641f65f947SAndy Whitcroft 19651f65f947SAndy Whitcroft if ($ok == 0) { 1966773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 19670a920b5bSAndy Whitcroft } 196822f2a2efSAndy Whitcroft } 19694a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 19700a920b5bSAndy Whitcroft } 19710a920b5bSAndy Whitcroft } 19720a920b5bSAndy Whitcroft 1973f0a594c1SAndy Whitcroft# check for multiple assignments 1974f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 19756c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 1976f0a594c1SAndy Whitcroft } 1977f0a594c1SAndy Whitcroft 197822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 197922f2a2efSAndy Whitcroft## # continuation. 198022f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 198122f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 198222f2a2efSAndy Whitcroft## 198322f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 198422f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 198522f2a2efSAndy Whitcroft## my $ln = $line; 198622f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 198722f2a2efSAndy Whitcroft## } 198822f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 198922f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 199022f2a2efSAndy Whitcroft## } 199122f2a2efSAndy Whitcroft## } 1992f0a594c1SAndy Whitcroft 19930a920b5bSAndy Whitcroft#need space before brace following if, while, etc 199422f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 199522f2a2efSAndy Whitcroft $line =~ /do{/) { 1996773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 1997de7d4f0eSAndy Whitcroft } 1998de7d4f0eSAndy Whitcroft 1999de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2000de7d4f0eSAndy Whitcroft# on the line 2001de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2002773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 20030a920b5bSAndy Whitcroft } 20040a920b5bSAndy Whitcroft 200522f2a2efSAndy Whitcroft# check spacing on square brackets 200622f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2007773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 200822f2a2efSAndy Whitcroft } 200922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2010773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 201122f2a2efSAndy Whitcroft } 201222f2a2efSAndy Whitcroft 2013c45dcabdSAndy Whitcroft# check spacing on parentheses 20149c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 20159c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2016773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 201722f2a2efSAndy Whitcroft } 201813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2019c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2020c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2021773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 202222f2a2efSAndy Whitcroft } 202322f2a2efSAndy Whitcroft 20240a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 20254a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 20260a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2027de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 20280a920b5bSAndy Whitcroft } 20290a920b5bSAndy Whitcroft 2030c45dcabdSAndy Whitcroft# Return is not a function. 2031c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2032c45dcabdSAndy Whitcroft my $spacing = $1; 2033c45dcabdSAndy Whitcroft my $value = $2; 2034c45dcabdSAndy Whitcroft 203586f9d059SAndy Whitcroft # Flatten any parentheses 2036fee61c47SAndy Whitcroft $value =~ s/\)\(/\) \(/g; 203763f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 203863f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 203963f17f89SAndy Whitcroft $Compare\s* 204063f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 204163f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2042c45dcabdSAndy Whitcroft } 2043c45dcabdSAndy Whitcroft 2044c45dcabdSAndy Whitcroft if ($value =~ /^(?:$Ident|-?$Constant)$/) { 2045c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2046c45dcabdSAndy Whitcroft 2047c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2048c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 2049c45dcabdSAndy Whitcroft } 2050c45dcabdSAndy Whitcroft } 2051c45dcabdSAndy Whitcroft 20520a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 20534a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2054773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 20550a920b5bSAndy Whitcroft } 20560a920b5bSAndy Whitcroft 2057f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2058f5fe35ddSAndy Whitcroft# statements after the conditional. 2059170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2060170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2061170d3a22SAndy Whitcroft $remain_next, $off_next); 2062170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2063170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2064170d3a22SAndy Whitcroft 2065170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2066170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2067170d3a22SAndy Whitcroft # then count those as offsets. 2068170d3a22SAndy Whitcroft my ($whitespace) = 2069170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2070170d3a22SAndy Whitcroft my $offset = 2071170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2072170d3a22SAndy Whitcroft 2073170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2074170d3a22SAndy Whitcroft $offset} = 1; 2075170d3a22SAndy Whitcroft } 2076170d3a22SAndy Whitcroft } 2077170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2078170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2079171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 20808905a67cSAndy Whitcroft 2081b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2082c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 20838905a67cSAndy Whitcroft } 20848905a67cSAndy Whitcroft 20858905a67cSAndy Whitcroft # Find out what is on the end of the line after the 20868905a67cSAndy Whitcroft # conditional. 2087773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 20888905a67cSAndy Whitcroft $s =~ s/\n.*//g; 208913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 209053210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 209153210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2092773647a0SAndy Whitcroft { 2093bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2094bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2095bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 2096bb44ad39SAndy Whitcroft 2097bb44ad39SAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 2098bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2099bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2100bb44ad39SAndy Whitcroft } 2101bb44ad39SAndy Whitcroft 2102bb44ad39SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 21038905a67cSAndy Whitcroft } 21048905a67cSAndy Whitcroft } 21058905a67cSAndy Whitcroft 210613214adfSAndy Whitcroft# Check for bitwise tests written as boolean 210713214adfSAndy Whitcroft if ($line =~ / 210813214adfSAndy Whitcroft (?: 210913214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 211013214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 211113214adfSAndy Whitcroft (?:\&\&|\|\|) 211213214adfSAndy Whitcroft | 211313214adfSAndy Whitcroft (?:\&\&|\|\|) 211413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 211513214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 211613214adfSAndy Whitcroft )/x) 211713214adfSAndy Whitcroft { 211813214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 211913214adfSAndy Whitcroft } 212013214adfSAndy Whitcroft 21218905a67cSAndy Whitcroft# if and else should not have general statements after it 212213214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 212313214adfSAndy Whitcroft my $s = $1; 212413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 212513214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 21268905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 21270a920b5bSAndy Whitcroft } 212813214adfSAndy Whitcroft } 212939667782SAndy Whitcroft# if should not continue a brace 213039667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 213139667782SAndy Whitcroft ERROR("trailing statements should be on next line\n" . 213239667782SAndy Whitcroft $herecurr); 213339667782SAndy Whitcroft } 2134a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2135a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2136a1080bf8SAndy Whitcroft $line !~ /\G(?: 21373fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2138a1080bf8SAndy Whitcroft \s*return\s+ 2139a1080bf8SAndy Whitcroft )/xg) 2140a1080bf8SAndy Whitcroft { 2141a1080bf8SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 2142a1080bf8SAndy Whitcroft } 21430a920b5bSAndy Whitcroft 21440a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 21450a920b5bSAndy Whitcroft # indent level to be relevant to each other. 21460a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 21470a920b5bSAndy Whitcroft $previndent == $indent) { 2148de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 21490a920b5bSAndy Whitcroft } 21500a920b5bSAndy Whitcroft 2151c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2152c2fdda0dSAndy Whitcroft $previndent == $indent) { 2153c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2154c2fdda0dSAndy Whitcroft 2155c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2156c2fdda0dSAndy Whitcroft # conditional. 2157773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2158c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2159c2fdda0dSAndy Whitcroft 2160c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2161c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 2162c2fdda0dSAndy Whitcroft } 2163c2fdda0dSAndy Whitcroft } 2164c2fdda0dSAndy Whitcroft 21650a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 21660a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 21670a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 21680a920b5bSAndy Whitcroft# print "$herecurr"; 21690a920b5bSAndy Whitcroft# $clean = 0; 21700a920b5bSAndy Whitcroft# } 21710a920b5bSAndy Whitcroft 21720a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2173c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2174de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 21750a920b5bSAndy Whitcroft } 21760a920b5bSAndy Whitcroft 2177653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2178c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2179e09dec48SAndy Whitcroft my $file = "$1.h"; 2180e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2181e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2182e09dec48SAndy Whitcroft $realfile ne $checkfile && 2183c45dcabdSAndy Whitcroft $1 ne 'irq') 2184c45dcabdSAndy Whitcroft { 2185e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2186e09dec48SAndy Whitcroft CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2187e09dec48SAndy Whitcroft } else { 2188e09dec48SAndy Whitcroft WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2189e09dec48SAndy Whitcroft } 21900a920b5bSAndy Whitcroft } 21910a920b5bSAndy Whitcroft } 21920a920b5bSAndy Whitcroft 2193653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2194653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2195cf655043SAndy Whitcroft# in a known good container 2196b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2197b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2198d8aaf121SAndy Whitcroft my $ln = $linenr; 2199d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2200c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2201c45dcabdSAndy Whitcroft my $ctx = ''; 2202653d4876SAndy Whitcroft 2203c45dcabdSAndy Whitcroft my $args = defined($1); 2204de7d4f0eSAndy Whitcroft 2205c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2206c45dcabdSAndy Whitcroft # search to that. 2207c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2208c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2209c45dcabdSAndy Whitcroft { 2210c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2211a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2212c45dcabdSAndy Whitcroft $ln++; 2213de7d4f0eSAndy Whitcroft } 2214c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2215d8aaf121SAndy Whitcroft 2216c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2217c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2218c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2219a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2220c45dcabdSAndy Whitcroft 2221c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2222c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2223c45dcabdSAndy Whitcroft $rest = ''; 2224636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2225636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2226a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2227c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2228c45dcabdSAndy Whitcroft $cnt--; 2229a3bb97a7SAndy Whitcroft } 2230a3bb97a7SAndy Whitcroft $ln++; 2231c45dcabdSAndy Whitcroft $off = 0; 2232c45dcabdSAndy Whitcroft } 2233c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2234c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2235c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2236c45dcabdSAndy Whitcroft 2237c45dcabdSAndy Whitcroft # Clean up the original statement. 2238c45dcabdSAndy Whitcroft if ($args) { 2239c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2240d8aaf121SAndy Whitcroft } else { 2241c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2242c45dcabdSAndy Whitcroft } 2243292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2244c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2245c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2246c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2247c45dcabdSAndy Whitcroft 2248c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2249bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2250bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2251bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2252bf30d6edSAndy Whitcroft { 2253c45dcabdSAndy Whitcroft } 2254c45dcabdSAndy Whitcroft 2255c45dcabdSAndy Whitcroft my $exceptions = qr{ 2256c45dcabdSAndy Whitcroft $Declare| 2257c45dcabdSAndy Whitcroft module_param_named| 2258c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2259c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2260c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2261383099fdSAndy Whitcroft __typeof__\(| 2262383099fdSAndy Whitcroft \.$Ident\s*=\s* 2263c45dcabdSAndy Whitcroft }x; 2264383099fdSAndy Whitcroft #print "REST<$rest> dstat<$dstat>\n"; 2265c45dcabdSAndy Whitcroft if ($rest ne '') { 2266c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2267c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2268c45dcabdSAndy Whitcroft { 2269c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2270c45dcabdSAndy Whitcroft } 2271c45dcabdSAndy Whitcroft 2272c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2273c45dcabdSAndy Whitcroft if ($dstat ne '' && 2274c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2275c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2276b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2277c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2278c45dcabdSAndy Whitcroft { 2279de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2280d8aaf121SAndy Whitcroft } 22810a920b5bSAndy Whitcroft } 2282653d4876SAndy Whitcroft } 22830a920b5bSAndy Whitcroft 2284080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2285080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2286080ba929SMike Frysinger# . 2287080ba929SMike Frysinger# ALIGN(...) 2288080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2289080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2290080ba929SMike Frysinger WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2291080ba929SMike Frysinger } 2292080ba929SMike Frysinger 2293f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 229413214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 229513214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2296cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 229713214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2298cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2299cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 230013214adfSAndy Whitcroft my $allowed = 0; 230113214adfSAndy Whitcroft my $seen = 0; 2302773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2303cf655043SAndy Whitcroft my $ln = $linenr - 1; 230413214adfSAndy Whitcroft for my $chunk (@chunks) { 230513214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 230613214adfSAndy Whitcroft 2307773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2308773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2309773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2310773647a0SAndy Whitcroft 2311773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2312773647a0SAndy Whitcroft 2313773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2314773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2315773647a0SAndy Whitcroft 2316773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2317cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2318cf655043SAndy Whitcroft 2319773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 232013214adfSAndy Whitcroft 232113214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 232213214adfSAndy Whitcroft 2323cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2324cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2325cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 232613214adfSAndy Whitcroft $allowed = 1; 232713214adfSAndy Whitcroft } 232813214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2329cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 233013214adfSAndy Whitcroft $allowed = 1; 233113214adfSAndy Whitcroft } 2332cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2333cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 233413214adfSAndy Whitcroft $allowed = 1; 233513214adfSAndy Whitcroft } 233613214adfSAndy Whitcroft } 233713214adfSAndy Whitcroft if ($seen && !$allowed) { 2338cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 233913214adfSAndy Whitcroft } 234013214adfSAndy Whitcroft } 234113214adfSAndy Whitcroft } 2342773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 234313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2344cf655043SAndy Whitcroft my $allowed = 0; 2345f0a594c1SAndy Whitcroft 2346cf655043SAndy Whitcroft # Check the pre-context. 2347cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2348cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2349cf655043SAndy Whitcroft $allowed = 1; 2350f0a594c1SAndy Whitcroft } 2351773647a0SAndy Whitcroft 2352773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2353773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2354773647a0SAndy Whitcroft 2355cf655043SAndy Whitcroft # Check the condition. 2356cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2357773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2358cf655043SAndy Whitcroft if (defined $cond) { 2359773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2360cf655043SAndy Whitcroft } 2361cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2362cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2363cf655043SAndy Whitcroft $allowed = 1; 2364cf655043SAndy Whitcroft } 2365cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2366cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2367cf655043SAndy Whitcroft $allowed = 1; 2368cf655043SAndy Whitcroft } 2369cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2370cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2371cf655043SAndy Whitcroft $allowed = 1; 2372cf655043SAndy Whitcroft } 2373cf655043SAndy Whitcroft # Check the post-context. 2374cf655043SAndy Whitcroft if (defined $chunks[1]) { 2375cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2376cf655043SAndy Whitcroft if (defined $cond) { 2377773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2378cf655043SAndy Whitcroft } 2379cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2380cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2381cf655043SAndy Whitcroft $allowed = 1; 2382cf655043SAndy Whitcroft } 2383cf655043SAndy Whitcroft } 2384cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2385cf655043SAndy Whitcroft my $herectx = $here . "\n";; 2386f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2387cf655043SAndy Whitcroft 2388f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2389f055663cSAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n";; 2390cf655043SAndy Whitcroft } 2391cf655043SAndy Whitcroft 2392cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 2393f0a594c1SAndy Whitcroft } 2394f0a594c1SAndy Whitcroft } 2395f0a594c1SAndy Whitcroft 2396653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 23974a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2398c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2399de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 24000a920b5bSAndy Whitcroft } 24010a920b5bSAndy Whitcroft } 24020a920b5bSAndy Whitcroft 24034a0df2efSAndy Whitcroft# don't use deprecated functions 24044a0df2efSAndy Whitcroft for my $func (@dep_functions) { 240500df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2406de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 24074a0df2efSAndy Whitcroft } 24084a0df2efSAndy Whitcroft } 24094a0df2efSAndy Whitcroft 24104a0df2efSAndy Whitcroft# no volatiles please 24116c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 24126c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2413de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 24144a0df2efSAndy Whitcroft } 24154a0df2efSAndy Whitcroft 24169c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 24179c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 24189c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 24199c0ca6f9SAndy Whitcroft } 24209c0ca6f9SAndy Whitcroft 242100df344fSAndy Whitcroft# warn about #if 0 2422c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2423de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2424de7d4f0eSAndy Whitcroft $herecurr); 24254a0df2efSAndy Whitcroft } 24264a0df2efSAndy Whitcroft 2427f0a594c1SAndy Whitcroft# check for needless kfree() checks 2428f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2429f0a594c1SAndy Whitcroft my $expr = $1; 2430f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 24313c232147SWolfram Sang WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2432f0a594c1SAndy Whitcroft } 2433f0a594c1SAndy Whitcroft } 24344c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 24354c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 24364c432a8fSGreg Kroah-Hartman my $expr = $1; 24374c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 24384c432a8fSGreg Kroah-Hartman WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 24394c432a8fSGreg Kroah-Hartman } 24404c432a8fSGreg Kroah-Hartman } 2441f0a594c1SAndy Whitcroft 244200df344fSAndy Whitcroft# warn about #ifdefs in C files 2443c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 244400df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 244500df344fSAndy Whitcroft# print "$herecurr"; 244600df344fSAndy Whitcroft# $clean = 0; 244700df344fSAndy Whitcroft# } 244800df344fSAndy Whitcroft 244922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2450c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 245122f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 245222f2a2efSAndy Whitcroft } 245322f2a2efSAndy Whitcroft 24544a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2455171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2456171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 24574a0df2efSAndy Whitcroft my $which = $1; 24584a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2459de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 24604a0df2efSAndy Whitcroft } 24614a0df2efSAndy Whitcroft } 24624a0df2efSAndy Whitcroft# check for memory barriers without a comment. 24634a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 24644a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2465de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 24664a0df2efSAndy Whitcroft } 24674a0df2efSAndy Whitcroft } 24684a0df2efSAndy Whitcroft# check of hardware specific defines 2469c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2470de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 24710a920b5bSAndy Whitcroft } 2472653d4876SAndy Whitcroft 2473de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2474de7d4f0eSAndy Whitcroft# storage class and type. 24759c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 24769c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2477de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2478de7d4f0eSAndy Whitcroft } 2479de7d4f0eSAndy Whitcroft 24808905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 24818905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 24828905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 24838905a67cSAndy Whitcroft } 24848905a67cSAndy Whitcroft 2485de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2486171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2487c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2488171ae1a4SAndy Whitcroft { 2489c45dcabdSAndy Whitcroft my $function_name = $1; 2490c45dcabdSAndy Whitcroft my $paren_space = $2; 2491171ae1a4SAndy Whitcroft 2492171ae1a4SAndy Whitcroft my $s = $stat; 2493171ae1a4SAndy Whitcroft if (defined $cond) { 2494171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2495171ae1a4SAndy Whitcroft } 2496c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2497c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2498c45dcabdSAndy Whitcroft { 2499de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2500de7d4f0eSAndy Whitcroft } 2501de7d4f0eSAndy Whitcroft 2502171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2503171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2504171ae1a4SAndy Whitcroft } 25059c9ba34eSAndy Whitcroft 25069c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 25079c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 25089c9ba34eSAndy Whitcroft { 25099c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2510171ae1a4SAndy Whitcroft } 2511171ae1a4SAndy Whitcroft 2512de7d4f0eSAndy Whitcroft# checks for new __setup's 2513de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2514de7d4f0eSAndy Whitcroft my $name = $1; 2515de7d4f0eSAndy Whitcroft 2516de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2517de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2518de7d4f0eSAndy Whitcroft } 2519653d4876SAndy Whitcroft } 25209c0ca6f9SAndy Whitcroft 25219c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 25229c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 25239c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 25249c0ca6f9SAndy Whitcroft } 252513214adfSAndy Whitcroft 252613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 252713214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 252813214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 252913214adfSAndy Whitcroft } 2530773647a0SAndy Whitcroft 2531773647a0SAndy Whitcroft# check for semaphores used as mutexes 2532171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2533773647a0SAndy Whitcroft WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); 2534773647a0SAndy Whitcroft } 2535773647a0SAndy Whitcroft# check for semaphores used as mutexes 2536171ae1a4SAndy Whitcroft if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { 2537773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 2538773647a0SAndy Whitcroft } 2539773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto* 2540773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2541773647a0SAndy Whitcroft WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); 2542773647a0SAndy Whitcroft } 2543f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2544f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2545f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2546f3db6639SMichael Ellerman } 25472b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const. 25486903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 25496903ffb2SAndy Whitcroft $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) { 25506903ffb2SAndy Whitcroft WARN("struct $1 should normally be const\n" . 25516903ffb2SAndy Whitcroft $herecurr); 25522b6db5cbSAndy Whitcroft } 2553773647a0SAndy Whitcroft 2554773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2555773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2556773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2557c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2558c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2559171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2560171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2561171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2562773647a0SAndy Whitcroft { 2563773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2564773647a0SAndy Whitcroft } 25659c9ba34eSAndy Whitcroft 25669c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 25679c9ba34eSAndy Whitcroft my $string; 25689c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 25699c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 25702a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 25719c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 25729c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 25739c9ba34eSAndy Whitcroft last; 25749c9ba34eSAndy Whitcroft } 25759c9ba34eSAndy Whitcroft } 2576691d77b6SAndy Whitcroft 2577691d77b6SAndy Whitcroft# whine mightly about in_atomic 2578691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 2579691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 2580691d77b6SAndy Whitcroft ERROR("do not use in_atomic in drivers\n" . $herecurr); 2581f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 2582691d77b6SAndy Whitcroft WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2583691d77b6SAndy Whitcroft } 2584691d77b6SAndy Whitcroft } 258513214adfSAndy Whitcroft } 258613214adfSAndy Whitcroft 258713214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 258813214adfSAndy Whitcroft # so just keep quiet. 258913214adfSAndy Whitcroft if ($#rawlines == -1) { 259013214adfSAndy Whitcroft exit(0); 25910a920b5bSAndy Whitcroft } 25920a920b5bSAndy Whitcroft 25938905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 25948905a67cSAndy Whitcroft # things that appear to be patches. 25958905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 25968905a67cSAndy Whitcroft exit(0); 25978905a67cSAndy Whitcroft } 25988905a67cSAndy Whitcroft 25998905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 26008905a67cSAndy Whitcroft # just keep quiet. 26018905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 26028905a67cSAndy Whitcroft exit(0); 26038905a67cSAndy Whitcroft } 26048905a67cSAndy Whitcroft 26058905a67cSAndy Whitcroft if (!$is_patch) { 2606de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 26070a920b5bSAndy Whitcroft } 26080a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2609de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 26100a920b5bSAndy Whitcroft } 26110a920b5bSAndy Whitcroft 2612f0a594c1SAndy Whitcroft print report_dump(); 261313214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 261413214adfSAndy Whitcroft print "$filename " if ($summary_file); 26156c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 26166c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 26176c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 26188905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 26196c72ffaaSAndy Whitcroft } 26208905a67cSAndy Whitcroft 26210a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2622c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 26230a920b5bSAndy Whitcroft } 26240a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2625c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 26260a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 26270a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 26280a920b5bSAndy Whitcroft } 262913214adfSAndy Whitcroft 26300a920b5bSAndy Whitcroft return $clean; 26310a920b5bSAndy Whitcroft} 2632