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 13db92a650SAndy Whitcroftmy $V = '0.27'; 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| 113cf655043SAndy Whitcroft __kprobes 1146c72ffaaSAndy Whitcroft }x; 1156c72ffaaSAndy Whitcroftour $Attribute = qr{ 1166c72ffaaSAndy Whitcroft const| 1176c72ffaaSAndy Whitcroft __read_mostly| 1186c72ffaaSAndy Whitcroft __kprobes| 11924e1d81aSAndy Whitcroft __(?:mem|cpu|dev|)(?:initdata|init)| 12024e1d81aSAndy Whitcroft ____cacheline_aligned| 12124e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 1225fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 1235fe3af11SAndy Whitcroft __weak 1246c72ffaaSAndy Whitcroft }x; 125c45dcabdSAndy Whitcroftour $Modifier; 1266c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 1276c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 1286c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 1296c72ffaaSAndy Whitcroft 1306c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 1316c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 13286f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 1336c72ffaaSAndy Whitcroftour $Operators = qr{ 1346c72ffaaSAndy Whitcroft <=|>=|==|!=| 1356c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 136c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1376c72ffaaSAndy Whitcroft }x; 1386c72ffaaSAndy Whitcroft 1398905a67cSAndy Whitcroftour $NonptrType; 1408905a67cSAndy Whitcroftour $Type; 1418905a67cSAndy Whitcroftour $Declare; 1428905a67cSAndy Whitcroft 143171ae1a4SAndy Whitcroftour $UTF8 = qr { 144171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 145171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 146171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 147171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 148171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 149171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 150171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 151171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 152171ae1a4SAndy Whitcroft}x; 153171ae1a4SAndy Whitcroft 1548ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 1558ed22cadSAndy Whitcroft (?:__)?(?:u|s|be|le)(?:\d|\d\d)| 1568ed22cadSAndy Whitcroft atomic_t 1578ed22cadSAndy Whitcroft)}; 1588ed22cadSAndy Whitcroft 1598905a67cSAndy Whitcroftour @typeList = ( 1608905a67cSAndy Whitcroft qr{void}, 161c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 162c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 163c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 164c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 165c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 166c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 167c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 1688905a67cSAndy Whitcroft qr{unsigned}, 1698905a67cSAndy Whitcroft qr{float}, 1708905a67cSAndy Whitcroft qr{double}, 1718905a67cSAndy Whitcroft qr{bool}, 1728905a67cSAndy Whitcroft qr{struct\s+$Ident}, 1738905a67cSAndy Whitcroft qr{union\s+$Ident}, 1748905a67cSAndy Whitcroft qr{enum\s+$Ident}, 1758905a67cSAndy Whitcroft qr{${Ident}_t}, 1768905a67cSAndy Whitcroft qr{${Ident}_handler}, 1778905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 1788905a67cSAndy Whitcroft); 179c45dcabdSAndy Whitcroftour @modifierList = ( 180c45dcabdSAndy Whitcroft qr{fastcall}, 181c45dcabdSAndy Whitcroft); 1828905a67cSAndy Whitcroft 1838905a67cSAndy Whitcroftsub build_types { 184d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 185d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 186c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 1878905a67cSAndy Whitcroft $NonptrType = qr{ 188d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 189cf655043SAndy Whitcroft (?: 190c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 1918ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 192c45dcabdSAndy Whitcroft (?:${all}\b) 193cf655043SAndy Whitcroft ) 194c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 1958905a67cSAndy Whitcroft }x; 1968905a67cSAndy Whitcroft $Type = qr{ 197c45dcabdSAndy Whitcroft $NonptrType 19865863862SAndy Whitcroft (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 199c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 2008905a67cSAndy Whitcroft }x; 2018905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 2028905a67cSAndy Whitcroft} 2038905a67cSAndy Whitcroftbuild_types(); 2046c72ffaaSAndy Whitcroft 2056c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 2060a920b5bSAndy Whitcroft 2074a0df2efSAndy Whitcroftmy @dep_includes = (); 2084a0df2efSAndy Whitcroftmy @dep_functions = (); 2096c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 2106c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 21121caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 2126c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 21321caa13cSAndy Whitcroft while (<$REMOVE>) { 214f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 215f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 216f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 2174a0df2efSAndy Whitcroft push(@dep_includes, $1); 2184a0df2efSAndy Whitcroft 219f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 220f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 221f0a594c1SAndy Whitcroft } 2224a0df2efSAndy Whitcroft } 2230a920b5bSAndy Whitcroft } 2240a920b5bSAndy Whitcroft } 22521caa13cSAndy Whitcroft close($REMOVE); 2260a920b5bSAndy Whitcroft} 2270a920b5bSAndy Whitcroft 22800df344fSAndy Whitcroftmy @rawlines = (); 229c2fdda0dSAndy Whitcroftmy @lines = (); 230c2fdda0dSAndy Whitcroftmy $vname; 2316c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 23221caa13cSAndy Whitcroft my $FILE; 2336c72ffaaSAndy Whitcroft if ($file) { 23421caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 2356c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 23621caa13cSAndy Whitcroft } elsif ($filename eq '-') { 23721caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 2386c72ffaaSAndy Whitcroft } else { 23921caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 2406c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 2416c72ffaaSAndy Whitcroft } 242c2fdda0dSAndy Whitcroft if ($filename eq '-') { 243c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 244c2fdda0dSAndy Whitcroft } else { 245c2fdda0dSAndy Whitcroft $vname = $filename; 246c2fdda0dSAndy Whitcroft } 24721caa13cSAndy Whitcroft while (<$FILE>) { 2480a920b5bSAndy Whitcroft chomp; 24900df344fSAndy Whitcroft push(@rawlines, $_); 2506c72ffaaSAndy Whitcroft } 25121caa13cSAndy Whitcroft close($FILE); 252c2fdda0dSAndy Whitcroft if (!process($filename)) { 2530a920b5bSAndy Whitcroft $exit = 1; 2540a920b5bSAndy Whitcroft } 25500df344fSAndy Whitcroft @rawlines = (); 25613214adfSAndy Whitcroft @lines = (); 2570a920b5bSAndy Whitcroft} 2580a920b5bSAndy Whitcroft 2590a920b5bSAndy Whitcroftexit($exit); 2600a920b5bSAndy Whitcroft 2610a920b5bSAndy Whitcroftsub top_of_kernel_tree { 2626c72ffaaSAndy Whitcroft my ($root) = @_; 2636c72ffaaSAndy Whitcroft 2646c72ffaaSAndy Whitcroft my @tree_check = ( 2656c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 2666c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 2676c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 2686c72ffaaSAndy Whitcroft ); 2696c72ffaaSAndy Whitcroft 2706c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 2716c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 2720a920b5bSAndy Whitcroft return 0; 2730a920b5bSAndy Whitcroft } 2746c72ffaaSAndy Whitcroft } 2756c72ffaaSAndy Whitcroft return 1; 2766c72ffaaSAndy Whitcroft} 2770a920b5bSAndy Whitcroft 2780a920b5bSAndy Whitcroftsub expand_tabs { 2790a920b5bSAndy Whitcroft my ($str) = @_; 2800a920b5bSAndy Whitcroft 2810a920b5bSAndy Whitcroft my $res = ''; 2820a920b5bSAndy Whitcroft my $n = 0; 2830a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 2840a920b5bSAndy Whitcroft if ($c eq "\t") { 2850a920b5bSAndy Whitcroft $res .= ' '; 2860a920b5bSAndy Whitcroft $n++; 2870a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 2880a920b5bSAndy Whitcroft $res .= ' '; 2890a920b5bSAndy Whitcroft } 2900a920b5bSAndy Whitcroft next; 2910a920b5bSAndy Whitcroft } 2920a920b5bSAndy Whitcroft $res .= $c; 2930a920b5bSAndy Whitcroft $n++; 2940a920b5bSAndy Whitcroft } 2950a920b5bSAndy Whitcroft 2960a920b5bSAndy Whitcroft return $res; 2970a920b5bSAndy Whitcroft} 2986c72ffaaSAndy Whitcroftsub copy_spacing { 299773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 3006c72ffaaSAndy Whitcroft return $res; 3016c72ffaaSAndy Whitcroft} 3020a920b5bSAndy Whitcroft 3034a0df2efSAndy Whitcroftsub line_stats { 3044a0df2efSAndy Whitcroft my ($line) = @_; 3054a0df2efSAndy Whitcroft 3064a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 3074a0df2efSAndy Whitcroft $line =~ s/^.//; 3084a0df2efSAndy Whitcroft $line = expand_tabs($line); 3094a0df2efSAndy Whitcroft 3104a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 3114a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 3124a0df2efSAndy Whitcroft 3134a0df2efSAndy Whitcroft return (length($line), length($white)); 3144a0df2efSAndy Whitcroft} 3154a0df2efSAndy Whitcroft 316773647a0SAndy Whitcroftmy $sanitise_quote = ''; 317773647a0SAndy Whitcroft 318773647a0SAndy Whitcroftsub sanitise_line_reset { 319773647a0SAndy Whitcroft my ($in_comment) = @_; 320773647a0SAndy Whitcroft 321773647a0SAndy Whitcroft if ($in_comment) { 322773647a0SAndy Whitcroft $sanitise_quote = '*/'; 323773647a0SAndy Whitcroft } else { 324773647a0SAndy Whitcroft $sanitise_quote = ''; 325773647a0SAndy Whitcroft } 326773647a0SAndy Whitcroft} 32700df344fSAndy Whitcroftsub sanitise_line { 32800df344fSAndy Whitcroft my ($line) = @_; 32900df344fSAndy Whitcroft 33000df344fSAndy Whitcroft my $res = ''; 33100df344fSAndy Whitcroft my $l = ''; 33200df344fSAndy Whitcroft 333c2fdda0dSAndy Whitcroft my $qlen = 0; 334773647a0SAndy Whitcroft my $off = 0; 335773647a0SAndy Whitcroft my $c; 33600df344fSAndy Whitcroft 337773647a0SAndy Whitcroft # Always copy over the diff marker. 338773647a0SAndy Whitcroft $res = substr($line, 0, 1); 339773647a0SAndy Whitcroft 340773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 341773647a0SAndy Whitcroft $c = substr($line, $off, 1); 342773647a0SAndy Whitcroft 343773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 344773647a0SAndy Whitcroft # and end, all to $;. 345773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 346773647a0SAndy Whitcroft $sanitise_quote = '*/'; 347773647a0SAndy Whitcroft 348773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 349773647a0SAndy Whitcroft $off++; 35000df344fSAndy Whitcroft next; 351773647a0SAndy Whitcroft } 35281bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 353773647a0SAndy Whitcroft $sanitise_quote = ''; 354773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 355773647a0SAndy Whitcroft $off++; 356773647a0SAndy Whitcroft next; 357773647a0SAndy Whitcroft } 358773647a0SAndy Whitcroft 359773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 360773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 361773647a0SAndy Whitcroft $c eq "\\") { 362773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 363773647a0SAndy Whitcroft $off++; 364773647a0SAndy Whitcroft next; 365773647a0SAndy Whitcroft } 366773647a0SAndy Whitcroft # Regular quotes. 367773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 368773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 369773647a0SAndy Whitcroft $sanitise_quote = $c; 370773647a0SAndy Whitcroft 371773647a0SAndy Whitcroft substr($res, $off, 1, $c); 372773647a0SAndy Whitcroft next; 373773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 374773647a0SAndy Whitcroft $sanitise_quote = ''; 37500df344fSAndy Whitcroft } 37600df344fSAndy Whitcroft } 377773647a0SAndy Whitcroft 378fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 379773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 380773647a0SAndy Whitcroft substr($res, $off, 1, $;); 381773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 382773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 38300df344fSAndy Whitcroft } else { 384773647a0SAndy Whitcroft substr($res, $off, 1, $c); 38500df344fSAndy Whitcroft } 386c2fdda0dSAndy Whitcroft } 387c2fdda0dSAndy Whitcroft 388c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 389c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 390c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 391c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 392c2fdda0dSAndy Whitcroft 393c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 394c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 395c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 396c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 397c2fdda0dSAndy Whitcroft } 398c2fdda0dSAndy Whitcroft 39900df344fSAndy Whitcroft return $res; 40000df344fSAndy Whitcroft} 40100df344fSAndy Whitcroft 4028905a67cSAndy Whitcroftsub ctx_statement_block { 4038905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 4048905a67cSAndy Whitcroft my $line = $linenr - 1; 4058905a67cSAndy Whitcroft my $blk = ''; 4068905a67cSAndy Whitcroft my $soff = $off; 4078905a67cSAndy Whitcroft my $coff = $off - 1; 408773647a0SAndy Whitcroft my $coff_set = 0; 4098905a67cSAndy Whitcroft 41013214adfSAndy Whitcroft my $loff = 0; 41113214adfSAndy Whitcroft 4128905a67cSAndy Whitcroft my $type = ''; 4138905a67cSAndy Whitcroft my $level = 0; 414a2750645SAndy Whitcroft my @stack = (); 415cf655043SAndy Whitcroft my $p; 4168905a67cSAndy Whitcroft my $c; 4178905a67cSAndy Whitcroft my $len = 0; 41813214adfSAndy Whitcroft 41913214adfSAndy Whitcroft my $remainder; 4208905a67cSAndy Whitcroft while (1) { 421a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 422a2750645SAndy Whitcroft 423773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 4248905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 4258905a67cSAndy Whitcroft # context. 4268905a67cSAndy Whitcroft if ($off >= $len) { 4278905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 428dea33496SAndy Whitcroft last if (!defined $lines[$line]); 429c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 4308905a67cSAndy Whitcroft $remain--; 43113214adfSAndy Whitcroft $loff = $len; 432c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 4338905a67cSAndy Whitcroft $len = length($blk); 4348905a67cSAndy Whitcroft $line++; 4358905a67cSAndy Whitcroft last; 4368905a67cSAndy Whitcroft } 4378905a67cSAndy Whitcroft # Bail if there is no further context. 4388905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 43913214adfSAndy Whitcroft if ($off >= $len) { 4408905a67cSAndy Whitcroft last; 4418905a67cSAndy Whitcroft } 4428905a67cSAndy Whitcroft } 443cf655043SAndy Whitcroft $p = $c; 4448905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 44513214adfSAndy Whitcroft $remainder = substr($blk, $off); 4468905a67cSAndy Whitcroft 447773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 4484635f4fbSAndy Whitcroft 4494635f4fbSAndy Whitcroft # Handle nested #if/#else. 4504635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 4514635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 4524635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 4534635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 4544635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 4554635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 4564635f4fbSAndy Whitcroft } 4574635f4fbSAndy Whitcroft 4588905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 4598905a67cSAndy Whitcroft # outermost level. 4608905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 4618905a67cSAndy Whitcroft last; 4628905a67cSAndy Whitcroft } 4638905a67cSAndy Whitcroft 46413214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 465773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 466773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 467773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 468773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 469773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 470773647a0SAndy Whitcroft $coff_set = 1; 471773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 472773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 47313214adfSAndy Whitcroft } 47413214adfSAndy Whitcroft 4758905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 4768905a67cSAndy Whitcroft $level++; 4778905a67cSAndy Whitcroft $type = '('; 4788905a67cSAndy Whitcroft } 4798905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 4808905a67cSAndy Whitcroft $level--; 4818905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 4828905a67cSAndy Whitcroft 4838905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 4848905a67cSAndy Whitcroft $coff = $off; 485773647a0SAndy Whitcroft $coff_set = 1; 486773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 4878905a67cSAndy Whitcroft } 4888905a67cSAndy 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) { 4988905a67cSAndy Whitcroft last; 4998905a67cSAndy Whitcroft } 5008905a67cSAndy Whitcroft } 5018905a67cSAndy Whitcroft $off++; 5028905a67cSAndy Whitcroft } 503a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 50413214adfSAndy Whitcroft if ($off == $len) { 505a3bb97a7SAndy Whitcroft $loff = $len + 1; 50613214adfSAndy Whitcroft $line++; 50713214adfSAndy Whitcroft $remain--; 50813214adfSAndy Whitcroft } 5098905a67cSAndy Whitcroft 5108905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 5118905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 5128905a67cSAndy Whitcroft 5138905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 5148905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 5158905a67cSAndy Whitcroft 516773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 51713214adfSAndy Whitcroft 51813214adfSAndy Whitcroft return ($statement, $condition, 51913214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 52013214adfSAndy Whitcroft} 52113214adfSAndy Whitcroft 522cf655043SAndy Whitcroftsub statement_lines { 523cf655043SAndy Whitcroft my ($stmt) = @_; 524cf655043SAndy Whitcroft 525cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 526cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 527cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 528cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 529cf655043SAndy Whitcroft 530cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 531cf655043SAndy Whitcroft 532cf655043SAndy Whitcroft return $#stmt_lines + 2; 533cf655043SAndy Whitcroft} 534cf655043SAndy Whitcroft 535cf655043SAndy Whitcroftsub statement_rawlines { 536cf655043SAndy Whitcroft my ($stmt) = @_; 537cf655043SAndy Whitcroft 538cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 539cf655043SAndy Whitcroft 540cf655043SAndy Whitcroft return $#stmt_lines + 2; 541cf655043SAndy Whitcroft} 542cf655043SAndy Whitcroft 543cf655043SAndy Whitcroftsub statement_block_size { 544cf655043SAndy Whitcroft my ($stmt) = @_; 545cf655043SAndy Whitcroft 546cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 547cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 548cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 549cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 550cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 551cf655043SAndy Whitcroft 552cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 553cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 554cf655043SAndy Whitcroft 555cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 556cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 557cf655043SAndy Whitcroft 558cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 559cf655043SAndy Whitcroft return $stmt_lines; 560cf655043SAndy Whitcroft } else { 561cf655043SAndy Whitcroft return $stmt_statements; 562cf655043SAndy Whitcroft } 563cf655043SAndy Whitcroft} 564cf655043SAndy Whitcroft 56513214adfSAndy Whitcroftsub ctx_statement_full { 56613214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 56713214adfSAndy Whitcroft my ($statement, $condition, $level); 56813214adfSAndy Whitcroft 56913214adfSAndy Whitcroft my (@chunks); 57013214adfSAndy Whitcroft 571cf655043SAndy Whitcroft # Grab the first conditional/block pair. 57213214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 57313214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 574773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 57513214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 576cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 577cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 578cf655043SAndy Whitcroft } 579cf655043SAndy Whitcroft 580cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 581cf655043SAndy Whitcroft # could continue the statement. 582cf655043SAndy Whitcroft for (;;) { 58313214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 58413214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 585cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 586773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 587cf655043SAndy Whitcroft #print "C: push\n"; 588cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 58913214adfSAndy Whitcroft } 59013214adfSAndy Whitcroft 59113214adfSAndy Whitcroft return ($level, $linenr, @chunks); 5928905a67cSAndy Whitcroft} 5938905a67cSAndy Whitcroft 5944a0df2efSAndy Whitcroftsub ctx_block_get { 595f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 5964a0df2efSAndy Whitcroft my $line; 5974a0df2efSAndy Whitcroft my $start = $linenr - 1; 5984a0df2efSAndy Whitcroft my $blk = ''; 5994a0df2efSAndy Whitcroft my @o; 6004a0df2efSAndy Whitcroft my @c; 6014a0df2efSAndy Whitcroft my @res = (); 6024a0df2efSAndy Whitcroft 603f0a594c1SAndy Whitcroft my $level = 0; 6044635f4fbSAndy Whitcroft my @stack = ($level); 60500df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 60600df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 60700df344fSAndy Whitcroft $remain--; 60800df344fSAndy Whitcroft 60900df344fSAndy Whitcroft $blk .= $rawlines[$line]; 6104635f4fbSAndy Whitcroft 6114635f4fbSAndy Whitcroft # Handle nested #if/#else. 6124635f4fbSAndy Whitcroft if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 6134635f4fbSAndy Whitcroft push(@stack, $level); 6144635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 6154635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 6164635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { 6174635f4fbSAndy Whitcroft $level = pop(@stack); 6184635f4fbSAndy Whitcroft } 6194635f4fbSAndy Whitcroft 620f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$line])) { 621f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 622f0a594c1SAndy Whitcroft if ($off > 0) { 623f0a594c1SAndy Whitcroft $off--; 624f0a594c1SAndy Whitcroft next; 625f0a594c1SAndy Whitcroft } 6264a0df2efSAndy Whitcroft 627f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 628f0a594c1SAndy Whitcroft $level--; 629f0a594c1SAndy Whitcroft last if ($level == 0); 630f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 631f0a594c1SAndy Whitcroft $level++; 632f0a594c1SAndy Whitcroft } 633f0a594c1SAndy Whitcroft } 6344a0df2efSAndy Whitcroft 635f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 63600df344fSAndy Whitcroft push(@res, $rawlines[$line]); 6374a0df2efSAndy Whitcroft } 6384a0df2efSAndy Whitcroft 639f0a594c1SAndy Whitcroft last if ($level == 0); 6404a0df2efSAndy Whitcroft } 6414a0df2efSAndy Whitcroft 642f0a594c1SAndy Whitcroft return ($level, @res); 6434a0df2efSAndy Whitcroft} 6444a0df2efSAndy Whitcroftsub ctx_block_outer { 6454a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6464a0df2efSAndy Whitcroft 647f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 648f0a594c1SAndy Whitcroft return @r; 6494a0df2efSAndy Whitcroft} 6504a0df2efSAndy Whitcroftsub ctx_block { 6514a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6524a0df2efSAndy Whitcroft 653f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 654f0a594c1SAndy Whitcroft return @r; 655653d4876SAndy Whitcroft} 656653d4876SAndy Whitcroftsub ctx_statement { 657f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 658f0a594c1SAndy Whitcroft 659f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 660f0a594c1SAndy Whitcroft return @r; 661f0a594c1SAndy Whitcroft} 662f0a594c1SAndy Whitcroftsub ctx_block_level { 663653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 664653d4876SAndy Whitcroft 665f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 6664a0df2efSAndy Whitcroft} 6679c0ca6f9SAndy Whitcroftsub ctx_statement_level { 6689c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 6699c0ca6f9SAndy Whitcroft 6709c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 6719c0ca6f9SAndy Whitcroft} 6724a0df2efSAndy Whitcroft 6734a0df2efSAndy Whitcroftsub ctx_locate_comment { 6744a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 6754a0df2efSAndy Whitcroft 6764a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 677beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 6784a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 6794a0df2efSAndy Whitcroft 6804a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 6814a0df2efSAndy Whitcroft # comment. 6824a0df2efSAndy Whitcroft my $in_comment = 0; 6834a0df2efSAndy Whitcroft $current_comment = ''; 6844a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 68500df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 68600df344fSAndy Whitcroft #warn " $line\n"; 6874a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 6884a0df2efSAndy Whitcroft $in_comment = 1; 6894a0df2efSAndy Whitcroft } 6904a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 6914a0df2efSAndy Whitcroft $in_comment = 1; 6924a0df2efSAndy Whitcroft } 6934a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 6944a0df2efSAndy Whitcroft $current_comment = ''; 6954a0df2efSAndy Whitcroft } 6964a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 6974a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 6984a0df2efSAndy Whitcroft $in_comment = 0; 6994a0df2efSAndy Whitcroft } 7004a0df2efSAndy Whitcroft } 7014a0df2efSAndy Whitcroft 7024a0df2efSAndy Whitcroft chomp($current_comment); 7034a0df2efSAndy Whitcroft return($current_comment); 7044a0df2efSAndy Whitcroft} 7054a0df2efSAndy Whitcroftsub ctx_has_comment { 7064a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7074a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 7084a0df2efSAndy Whitcroft 70900df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 7104a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 7114a0df2efSAndy Whitcroft 7124a0df2efSAndy Whitcroft return ($cmt ne ''); 7134a0df2efSAndy Whitcroft} 7144a0df2efSAndy Whitcroft 7154d001e4dSAndy Whitcroftsub raw_line { 7164d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 7174d001e4dSAndy Whitcroft 7184d001e4dSAndy Whitcroft my $offset = $linenr - 1; 7194d001e4dSAndy Whitcroft $cnt++; 7204d001e4dSAndy Whitcroft 7214d001e4dSAndy Whitcroft my $line; 7224d001e4dSAndy Whitcroft while ($cnt) { 7234d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 7244d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 7254d001e4dSAndy Whitcroft $cnt--; 7264d001e4dSAndy Whitcroft } 7274d001e4dSAndy Whitcroft 7284d001e4dSAndy Whitcroft return $line; 7294d001e4dSAndy Whitcroft} 7304d001e4dSAndy Whitcroft 7310a920b5bSAndy Whitcroftsub cat_vet { 7320a920b5bSAndy Whitcroft my ($vet) = @_; 7339c0ca6f9SAndy Whitcroft my ($res, $coded); 7340a920b5bSAndy Whitcroft 7359c0ca6f9SAndy Whitcroft $res = ''; 7366c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 7376c72ffaaSAndy Whitcroft $res .= $1; 7386c72ffaaSAndy Whitcroft if ($2 ne '') { 7399c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 7406c72ffaaSAndy Whitcroft $res .= $coded; 7416c72ffaaSAndy Whitcroft } 7429c0ca6f9SAndy Whitcroft } 7439c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 7440a920b5bSAndy Whitcroft 7459c0ca6f9SAndy Whitcroft return $res; 7460a920b5bSAndy Whitcroft} 7470a920b5bSAndy Whitcroft 748c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 749cf655043SAndy Whitcroftmy $av_pending; 750c2fdda0dSAndy Whitcroftmy @av_paren_type; 7511f65f947SAndy Whitcroftmy $av_pend_colon; 752c2fdda0dSAndy Whitcroft 753c2fdda0dSAndy Whitcroftsub annotate_reset { 754c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 755cf655043SAndy Whitcroft $av_pending = '_'; 756cf655043SAndy Whitcroft @av_paren_type = ('E'); 7571f65f947SAndy Whitcroft $av_pend_colon = 'O'; 758c2fdda0dSAndy Whitcroft} 759c2fdda0dSAndy Whitcroft 7606c72ffaaSAndy Whitcroftsub annotate_values { 7616c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 7626c72ffaaSAndy Whitcroft 7636c72ffaaSAndy Whitcroft my $res; 7641f65f947SAndy Whitcroft my $var = '_' x length($stream); 7656c72ffaaSAndy Whitcroft my $cur = $stream; 7666c72ffaaSAndy Whitcroft 767c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 7686c72ffaaSAndy Whitcroft 7696c72ffaaSAndy Whitcroft while (length($cur)) { 770773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 771cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 772171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 7736c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 774c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 775c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 776cf655043SAndy Whitcroft $type = pop(@av_paren_type); 777c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 7786c72ffaaSAndy Whitcroft } 7796c72ffaaSAndy Whitcroft 7808ea3eb9aSAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { 781c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 7826c72ffaaSAndy Whitcroft $type = 'T'; 7836c72ffaaSAndy Whitcroft 784389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 785389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 786389a2fe5SAndy Whitcroft $type = 'T'; 787389a2fe5SAndy Whitcroft 788c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 789171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 790c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 791171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 792171ae1a4SAndy Whitcroft if ($2 ne '') { 793cf655043SAndy Whitcroft $av_pending = 'N'; 794171ae1a4SAndy Whitcroft } 795171ae1a4SAndy Whitcroft $type = 'E'; 796171ae1a4SAndy Whitcroft 797c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 798171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 799171ae1a4SAndy Whitcroft $av_preprocessor = 1; 800171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 8016c72ffaaSAndy Whitcroft 802c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 803cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 804c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 805cf655043SAndy Whitcroft 806cf655043SAndy Whitcroft push(@av_paren_type, $type); 807cf655043SAndy Whitcroft push(@av_paren_type, $type); 808171ae1a4SAndy Whitcroft $type = 'E'; 809cf655043SAndy Whitcroft 810c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 811cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 812cf655043SAndy Whitcroft $av_preprocessor = 1; 813cf655043SAndy Whitcroft 814cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 815cf655043SAndy Whitcroft 816171ae1a4SAndy Whitcroft $type = 'E'; 817cf655043SAndy Whitcroft 818c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 819cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 820cf655043SAndy Whitcroft 821cf655043SAndy Whitcroft $av_preprocessor = 1; 822cf655043SAndy Whitcroft 823cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 824cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 825cf655043SAndy Whitcroft pop(@av_paren_type); 826cf655043SAndy Whitcroft push(@av_paren_type, $type); 827171ae1a4SAndy Whitcroft $type = 'E'; 8286c72ffaaSAndy Whitcroft 8296c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 830c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 8316c72ffaaSAndy Whitcroft 832171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 833171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 834171ae1a4SAndy Whitcroft $av_pending = $type; 835171ae1a4SAndy Whitcroft $type = 'N'; 836171ae1a4SAndy Whitcroft 8376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 838c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 8396c72ffaaSAndy Whitcroft if (defined $2) { 840cf655043SAndy Whitcroft $av_pending = 'V'; 8416c72ffaaSAndy Whitcroft } 8426c72ffaaSAndy Whitcroft $type = 'N'; 8436c72ffaaSAndy Whitcroft 84414b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 845c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 84614b111c1SAndy Whitcroft $av_pending = 'E'; 8476c72ffaaSAndy Whitcroft $type = 'N'; 8486c72ffaaSAndy Whitcroft 8491f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 8501f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 8511f65f947SAndy Whitcroft $av_pend_colon = 'C'; 8521f65f947SAndy Whitcroft $type = 'N'; 8531f65f947SAndy Whitcroft 85414b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 855c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 8566c72ffaaSAndy Whitcroft $type = 'N'; 8576c72ffaaSAndy Whitcroft 8586c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 859c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 860cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 861cf655043SAndy Whitcroft $av_pending = '_'; 8626c72ffaaSAndy Whitcroft $type = 'N'; 8636c72ffaaSAndy Whitcroft 8646c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 865cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 866cf655043SAndy Whitcroft if ($new_type ne '_') { 867cf655043SAndy Whitcroft $type = $new_type; 868c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 869c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 8706c72ffaaSAndy Whitcroft } else { 871c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 8726c72ffaaSAndy Whitcroft } 8736c72ffaaSAndy Whitcroft 874c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 875c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 876c8cb2ca3SAndy Whitcroft $type = 'V'; 877cf655043SAndy Whitcroft $av_pending = 'V'; 8786c72ffaaSAndy Whitcroft 8798e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 8808e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 8811f65f947SAndy Whitcroft $av_pend_colon = 'B'; 8828e761b04SAndy Whitcroft } elsif ($type eq 'E') { 8838e761b04SAndy Whitcroft $av_pend_colon = 'L'; 8841f65f947SAndy Whitcroft } 8851f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 8861f65f947SAndy Whitcroft $type = 'V'; 8871f65f947SAndy Whitcroft 8886c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 889c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 8906c72ffaaSAndy Whitcroft $type = 'V'; 8916c72ffaaSAndy Whitcroft 8926c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 893c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 8946c72ffaaSAndy Whitcroft $type = 'N'; 8956c72ffaaSAndy Whitcroft 896cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 897c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 89813214adfSAndy Whitcroft $type = 'E'; 8991f65f947SAndy Whitcroft $av_pend_colon = 'O'; 90013214adfSAndy Whitcroft 9018e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 9028e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 9038e761b04SAndy Whitcroft $type = 'C'; 9048e761b04SAndy Whitcroft 9051f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 9061f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 9071f65f947SAndy Whitcroft $type = 'N'; 9081f65f947SAndy Whitcroft 9091f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 9101f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 9111f65f947SAndy Whitcroft 9121f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 9131f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 9141f65f947SAndy Whitcroft $type = 'E'; 9151f65f947SAndy Whitcroft } else { 9161f65f947SAndy Whitcroft $type = 'N'; 9171f65f947SAndy Whitcroft } 9181f65f947SAndy Whitcroft $av_pend_colon = 'O'; 9191f65f947SAndy Whitcroft 9208e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 92113214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 9226c72ffaaSAndy Whitcroft $type = 'N'; 9236c72ffaaSAndy Whitcroft 9240d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 92574048ed8SAndy Whitcroft my $variant; 92674048ed8SAndy Whitcroft 92774048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 92874048ed8SAndy Whitcroft if ($type eq 'V') { 92974048ed8SAndy Whitcroft $variant = 'B'; 93074048ed8SAndy Whitcroft } else { 93174048ed8SAndy Whitcroft $variant = 'U'; 93274048ed8SAndy Whitcroft } 93374048ed8SAndy Whitcroft 93474048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 93574048ed8SAndy Whitcroft $type = 'N'; 93674048ed8SAndy Whitcroft 9376c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 938c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 9396c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 9406c72ffaaSAndy Whitcroft $type = 'N'; 9416c72ffaaSAndy Whitcroft } 9426c72ffaaSAndy Whitcroft 9436c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 944c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 9456c72ffaaSAndy Whitcroft } 9466c72ffaaSAndy Whitcroft if (defined $1) { 9476c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 9486c72ffaaSAndy Whitcroft $res .= $type x length($1); 9496c72ffaaSAndy Whitcroft } 9506c72ffaaSAndy Whitcroft } 9516c72ffaaSAndy Whitcroft 9521f65f947SAndy Whitcroft return ($res, $var); 9536c72ffaaSAndy Whitcroft} 9546c72ffaaSAndy Whitcroft 9558905a67cSAndy Whitcroftsub possible { 95613214adfSAndy Whitcroft my ($possible, $line) = @_; 9578905a67cSAndy Whitcroft 9580776e594SAndy Whitcroft print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 9590776e594SAndy Whitcroft if ($possible !~ /(?: 9600776e594SAndy Whitcroft ^(?: 9610776e594SAndy Whitcroft $Modifier| 9620776e594SAndy Whitcroft $Storage| 9630776e594SAndy Whitcroft $Type| 9640776e594SAndy Whitcroft DEFINE_\S+| 9650776e594SAndy Whitcroft goto| 9660776e594SAndy Whitcroft return| 9670776e594SAndy Whitcroft case| 9680776e594SAndy Whitcroft else| 9690776e594SAndy Whitcroft asm|__asm__| 9700776e594SAndy Whitcroft do 9710776e594SAndy Whitcroft )$| 9720776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 9730776e594SAndy Whitcroft )/x) { 974c45dcabdSAndy Whitcroft # Check for modifiers. 975c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 976c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 977c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 978c45dcabdSAndy Whitcroft 979c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 980c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 981d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 982d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 983d2506586SAndy Whitcroft push(@modifierList, $modifier); 984d2506586SAndy Whitcroft } 985c45dcabdSAndy Whitcroft 986c45dcabdSAndy Whitcroft } else { 98713214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 9888905a67cSAndy Whitcroft push(@typeList, $possible); 989c45dcabdSAndy Whitcroft } 9908905a67cSAndy Whitcroft build_types(); 9910776e594SAndy Whitcroft } else { 9920776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 9938905a67cSAndy Whitcroft } 9948905a67cSAndy Whitcroft} 9958905a67cSAndy Whitcroft 9966c72ffaaSAndy Whitcroftmy $prefix = ''; 9976c72ffaaSAndy Whitcroft 998f0a594c1SAndy Whitcroftsub report { 999773647a0SAndy Whitcroft if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 1000773647a0SAndy Whitcroft return 0; 1001773647a0SAndy Whitcroft } 10028905a67cSAndy Whitcroft my $line = $prefix . $_[0]; 10038905a67cSAndy Whitcroft 10048905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 10058905a67cSAndy Whitcroft 100613214adfSAndy Whitcroft push(our @report, $line); 1007773647a0SAndy Whitcroft 1008773647a0SAndy Whitcroft return 1; 1009f0a594c1SAndy Whitcroft} 1010f0a594c1SAndy Whitcroftsub report_dump { 101113214adfSAndy Whitcroft our @report; 1012f0a594c1SAndy Whitcroft} 1013de7d4f0eSAndy Whitcroftsub ERROR { 1014773647a0SAndy Whitcroft if (report("ERROR: $_[0]\n")) { 1015de7d4f0eSAndy Whitcroft our $clean = 0; 10166c72ffaaSAndy Whitcroft our $cnt_error++; 1017de7d4f0eSAndy Whitcroft } 1018773647a0SAndy Whitcroft} 1019de7d4f0eSAndy Whitcroftsub WARN { 1020773647a0SAndy Whitcroft if (report("WARNING: $_[0]\n")) { 1021de7d4f0eSAndy Whitcroft our $clean = 0; 10226c72ffaaSAndy Whitcroft our $cnt_warn++; 1023de7d4f0eSAndy Whitcroft } 1024773647a0SAndy Whitcroft} 1025de7d4f0eSAndy Whitcroftsub CHK { 1026773647a0SAndy Whitcroft if ($check && report("CHECK: $_[0]\n")) { 1027de7d4f0eSAndy Whitcroft our $clean = 0; 10286c72ffaaSAndy Whitcroft our $cnt_chk++; 10296c72ffaaSAndy Whitcroft } 1030de7d4f0eSAndy Whitcroft} 1031de7d4f0eSAndy Whitcroft 10326ecd9674SAndy Whitcroftsub check_absolute_file { 10336ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 10346ecd9674SAndy Whitcroft my $file = $absolute; 10356ecd9674SAndy Whitcroft 10366ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 10376ecd9674SAndy Whitcroft 10386ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 10396ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 10406ecd9674SAndy Whitcroft if (-f "$root/$file") { 10416ecd9674SAndy Whitcroft ##print "file<$file>\n"; 10426ecd9674SAndy Whitcroft last; 10436ecd9674SAndy Whitcroft } 10446ecd9674SAndy Whitcroft } 10456ecd9674SAndy Whitcroft if (! -f _) { 10466ecd9674SAndy Whitcroft return 0; 10476ecd9674SAndy Whitcroft } 10486ecd9674SAndy Whitcroft 10496ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 10506ecd9674SAndy Whitcroft my $prefix = $absolute; 10516ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 10526ecd9674SAndy Whitcroft 10536ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 10546ecd9674SAndy Whitcroft if ($prefix ne ".../") { 10556ecd9674SAndy Whitcroft WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); 10566ecd9674SAndy Whitcroft } 10576ecd9674SAndy Whitcroft} 10586ecd9674SAndy Whitcroft 10590a920b5bSAndy Whitcroftsub process { 10600a920b5bSAndy Whitcroft my $filename = shift; 10610a920b5bSAndy Whitcroft 10620a920b5bSAndy Whitcroft my $linenr=0; 10630a920b5bSAndy Whitcroft my $prevline=""; 1064c2fdda0dSAndy Whitcroft my $prevrawline=""; 10650a920b5bSAndy Whitcroft my $stashline=""; 1066c2fdda0dSAndy Whitcroft my $stashrawline=""; 10670a920b5bSAndy Whitcroft 10684a0df2efSAndy Whitcroft my $length; 10690a920b5bSAndy Whitcroft my $indent; 10700a920b5bSAndy Whitcroft my $previndent=0; 10710a920b5bSAndy Whitcroft my $stashindent=0; 10720a920b5bSAndy Whitcroft 1073de7d4f0eSAndy Whitcroft our $clean = 1; 10740a920b5bSAndy Whitcroft my $signoff = 0; 10750a920b5bSAndy Whitcroft my $is_patch = 0; 10760a920b5bSAndy Whitcroft 107713214adfSAndy Whitcroft our @report = (); 10786c72ffaaSAndy Whitcroft our $cnt_lines = 0; 10796c72ffaaSAndy Whitcroft our $cnt_error = 0; 10806c72ffaaSAndy Whitcroft our $cnt_warn = 0; 10816c72ffaaSAndy Whitcroft our $cnt_chk = 0; 10826c72ffaaSAndy Whitcroft 10830a920b5bSAndy Whitcroft # Trace the real file/line as we go. 10840a920b5bSAndy Whitcroft my $realfile = ''; 10850a920b5bSAndy Whitcroft my $realline = 0; 10860a920b5bSAndy Whitcroft my $realcnt = 0; 10870a920b5bSAndy Whitcroft my $here = ''; 10880a920b5bSAndy Whitcroft my $in_comment = 0; 1089c2fdda0dSAndy Whitcroft my $comment_edge = 0; 10900a920b5bSAndy Whitcroft my $first_line = 0; 10911e855726SWolfram Sang my $p1_prefix = ''; 10920a920b5bSAndy Whitcroft 109313214adfSAndy Whitcroft my $prev_values = 'E'; 109413214adfSAndy Whitcroft 109513214adfSAndy Whitcroft # suppression flags 1096773647a0SAndy Whitcroft my %suppress_ifbraces; 1097170d3a22SAndy Whitcroft my %suppress_whiletrailers; 1098653d4876SAndy Whitcroft 1099c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1100de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1101c2fdda0dSAndy Whitcroft # 1102de7d4f0eSAndy Whitcroft my @setup_docs = (); 1103de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1104773647a0SAndy Whitcroft 1105773647a0SAndy Whitcroft sanitise_line_reset(); 1106c2fdda0dSAndy Whitcroft my $line; 1107c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1108773647a0SAndy Whitcroft $linenr++; 1109773647a0SAndy Whitcroft $line = $rawline; 1110c2fdda0dSAndy Whitcroft 1111773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1112de7d4f0eSAndy Whitcroft $setup_docs = 0; 1113de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1114de7d4f0eSAndy Whitcroft $setup_docs = 1; 1115de7d4f0eSAndy Whitcroft } 1116773647a0SAndy Whitcroft #next; 1117de7d4f0eSAndy Whitcroft } 1118773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1119773647a0SAndy Whitcroft $realline=$1-1; 1120773647a0SAndy Whitcroft if (defined $2) { 1121773647a0SAndy Whitcroft $realcnt=$3+1; 1122773647a0SAndy Whitcroft } else { 1123773647a0SAndy Whitcroft $realcnt=1+1; 1124773647a0SAndy Whitcroft } 1125c45dcabdSAndy Whitcroft $in_comment = 0; 1126773647a0SAndy Whitcroft 1127773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1128773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1129773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1130773647a0SAndy Whitcroft # at context start. 1131773647a0SAndy Whitcroft my $edge; 113201fa9147SAndy Whitcroft my $cnt = $realcnt; 113301fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 113401fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 113501fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 113601fa9147SAndy Whitcroft $cnt--; 113701fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1138721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1139fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1140fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1141fae17daeSAndy Whitcroft ($edge) = $1; 1142fae17daeSAndy Whitcroft last; 1143fae17daeSAndy Whitcroft } 1144773647a0SAndy Whitcroft } 1145773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1146773647a0SAndy Whitcroft $in_comment = 1; 1147773647a0SAndy Whitcroft } 1148773647a0SAndy Whitcroft 1149773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1150773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1151773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1152773647a0SAndy Whitcroft if (!defined $edge && 115383242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1154773647a0SAndy Whitcroft { 1155773647a0SAndy Whitcroft $in_comment = 1; 1156773647a0SAndy Whitcroft } 1157773647a0SAndy Whitcroft 1158773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1159773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1160773647a0SAndy Whitcroft 1161171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1162773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1163171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1164773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1165773647a0SAndy Whitcroft } 1166773647a0SAndy Whitcroft push(@lines, $line); 1167773647a0SAndy Whitcroft 1168773647a0SAndy Whitcroft if ($realcnt > 1) { 1169773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1170773647a0SAndy Whitcroft } else { 1171773647a0SAndy Whitcroft $realcnt = 0; 1172773647a0SAndy Whitcroft } 1173773647a0SAndy Whitcroft 1174773647a0SAndy Whitcroft #print "==>$rawline\n"; 1175773647a0SAndy Whitcroft #print "-->$line\n"; 1176de7d4f0eSAndy Whitcroft 1177de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1178de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1179de7d4f0eSAndy Whitcroft } 1180de7d4f0eSAndy Whitcroft } 1181de7d4f0eSAndy Whitcroft 11826c72ffaaSAndy Whitcroft $prefix = ''; 11836c72ffaaSAndy Whitcroft 1184773647a0SAndy Whitcroft $realcnt = 0; 1185773647a0SAndy Whitcroft $linenr = 0; 11860a920b5bSAndy Whitcroft foreach my $line (@lines) { 11870a920b5bSAndy Whitcroft $linenr++; 11880a920b5bSAndy Whitcroft 1189c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 119030670854SAndy Whitcroft my $hunk_line = ($realcnt != 0); 11916c72ffaaSAndy Whitcroft 11920a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 11936c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 11940a920b5bSAndy Whitcroft $is_patch = 1; 11954a0df2efSAndy Whitcroft $first_line = $linenr + 1; 11960a920b5bSAndy Whitcroft $realline=$1-1; 11970a920b5bSAndy Whitcroft if (defined $2) { 11980a920b5bSAndy Whitcroft $realcnt=$3+1; 11990a920b5bSAndy Whitcroft } else { 12000a920b5bSAndy Whitcroft $realcnt=1+1; 12010a920b5bSAndy Whitcroft } 1202c2fdda0dSAndy Whitcroft annotate_reset(); 120313214adfSAndy Whitcroft $prev_values = 'E'; 120413214adfSAndy Whitcroft 1205773647a0SAndy Whitcroft %suppress_ifbraces = (); 1206170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 12070a920b5bSAndy Whitcroft next; 12080a920b5bSAndy Whitcroft 12094a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 12104a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 12114a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1212773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 12130a920b5bSAndy Whitcroft $realline++; 1214d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 12150a920b5bSAndy Whitcroft 12164a0df2efSAndy Whitcroft # Measure the line length and indent. 1217c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 12180a920b5bSAndy Whitcroft 12190a920b5bSAndy Whitcroft # Track the previous line. 12200a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 12210a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1222c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1223c2fdda0dSAndy Whitcroft 1224773647a0SAndy Whitcroft #warn "line<$line>\n"; 12256c72ffaaSAndy Whitcroft 1226d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1227d8aaf121SAndy Whitcroft $realcnt--; 12280a920b5bSAndy Whitcroft } 12290a920b5bSAndy Whitcroft 12300a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1231773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1232773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1233773647a0SAndy Whitcroft 12346c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 12356c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1236773647a0SAndy Whitcroft 1237773647a0SAndy Whitcroft # extract the filename as it passes 1238773647a0SAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 1239773647a0SAndy Whitcroft $realfile = $1; 12401e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 12411e855726SWolfram Sang 12421e855726SWolfram Sang $p1_prefix = $1; 1243e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1244e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 12451e855726SWolfram Sang WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 12461e855726SWolfram Sang } 1247773647a0SAndy Whitcroft 1248c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1249773647a0SAndy Whitcroft ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1250773647a0SAndy Whitcroft } 1251773647a0SAndy Whitcroft next; 1252773647a0SAndy Whitcroft } 1253773647a0SAndy Whitcroft 1254389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 12550a920b5bSAndy Whitcroft 1256c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1257c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1258c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 12590a920b5bSAndy Whitcroft 12606c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 12616c72ffaaSAndy Whitcroft 12620a920b5bSAndy Whitcroft#check the patch for a signoff: 1263d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 12644a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 12654a0df2efSAndy Whitcroft $signoff++; 12660a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 1267de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 1268de7d4f0eSAndy Whitcroft $herecurr); 12690a920b5bSAndy Whitcroft } 12700a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 1271773647a0SAndy Whitcroft WARN("space required after Signed-off-by:\n" . 1272de7d4f0eSAndy Whitcroft $herecurr); 12730a920b5bSAndy Whitcroft } 12740a920b5bSAndy Whitcroft } 12750a920b5bSAndy Whitcroft 127600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 12778905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1278de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 12796c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1280de7d4f0eSAndy Whitcroft } 1281de7d4f0eSAndy Whitcroft 12826ecd9674SAndy Whitcroft# Check for absolute kernel paths. 12836ecd9674SAndy Whitcroft if ($tree) { 12846ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 12856ecd9674SAndy Whitcroft my $file = $1; 12866ecd9674SAndy Whitcroft 12876ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 12886ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 12896ecd9674SAndy Whitcroft # 12906ecd9674SAndy Whitcroft } else { 12916ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 12926ecd9674SAndy Whitcroft } 12936ecd9674SAndy Whitcroft } 12946ecd9674SAndy Whitcroft } 12956ecd9674SAndy Whitcroft 1296de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1297de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1298171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1299171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1300171ae1a4SAndy Whitcroft 1301171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1302171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1303171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1304171ae1a4SAndy Whitcroft 1305171ae1a4SAndy Whitcroft ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 130600df344fSAndy Whitcroft } 13070a920b5bSAndy Whitcroft 130830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 130930670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 131000df344fSAndy Whitcroft 13110a920b5bSAndy Whitcroft#trailing whitespace 13129c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1313c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 13149c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 13159c0ca6f9SAndy Whitcroft 1316c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1317c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1318de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 13190a920b5bSAndy Whitcroft } 13205368df20SAndy Whitcroft 13215368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 13225368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 13235368df20SAndy Whitcroft 13240a920b5bSAndy Whitcroft#80 column limit 1325c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1326f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1327f4c014c0SAndy Whitcroft $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && 1328f4c014c0SAndy Whitcroft $length > 80) 1329c45dcabdSAndy Whitcroft { 1330de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 13310a920b5bSAndy Whitcroft } 13320a920b5bSAndy Whitcroft 13338905a67cSAndy Whitcroft# check for adding lines without a newline. 13348905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 13358905a67cSAndy Whitcroft WARN("adding a line without newline at end of file\n" . $herecurr); 13368905a67cSAndy Whitcroft } 13378905a67cSAndy Whitcroft 1338b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1339b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 13400a920b5bSAndy Whitcroft 13410a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 13420a920b5bSAndy Whitcroft# more than 8 must use tabs. 1343c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1344c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1345c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1346171ae1a4SAndy Whitcroft ERROR("code indent should use tabs where possible\n" . $herevet); 13470a920b5bSAndy Whitcroft } 13480a920b5bSAndy Whitcroft 1349b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1350b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1351b9ea10d6SAndy Whitcroft 1352c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1353cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1354c2fdda0dSAndy Whitcroft WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1355c2fdda0dSAndy Whitcroft } 135622f2a2efSAndy Whitcroft 13579c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 1358170d3a22SAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next); 13599c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1360170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1361f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1362171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1363171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1364171ae1a4SAndy Whitcroft 1365171ae1a4SAndy Whitcroft my $s = $stat; 1366171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1367cf655043SAndy Whitcroft 1368c2fdda0dSAndy Whitcroft # Ignore goto labels. 1369171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1370c2fdda0dSAndy Whitcroft 1371c2fdda0dSAndy Whitcroft # Ignore functions being called 1372171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1373c2fdda0dSAndy Whitcroft 1374c45dcabdSAndy Whitcroft # declarations always start with types 1375d2506586SAndy 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) { 1376c45dcabdSAndy Whitcroft my $type = $1; 1377c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1378c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1379c45dcabdSAndy Whitcroft 13806c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1381a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1382c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1383c2fdda0dSAndy Whitcroft } 13848905a67cSAndy Whitcroft 13856c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 138665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1387c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 13889c0ca6f9SAndy Whitcroft } 13898905a67cSAndy Whitcroft 13908905a67cSAndy Whitcroft # Check for any sort of function declaration. 13918905a67cSAndy Whitcroft # int foo(something bar, other baz); 13928905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1393171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 13948905a67cSAndy Whitcroft my ($name_len) = length($1); 13958905a67cSAndy Whitcroft 1396cf655043SAndy Whitcroft my $ctx = $s; 1397773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 13988905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1399cf655043SAndy Whitcroft 14008905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1401c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 14028905a67cSAndy Whitcroft 1403c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 14048905a67cSAndy Whitcroft } 14058905a67cSAndy Whitcroft } 14068905a67cSAndy Whitcroft } 14078905a67cSAndy Whitcroft 14089c0ca6f9SAndy Whitcroft } 14099c0ca6f9SAndy Whitcroft 141000df344fSAndy Whitcroft# 141100df344fSAndy Whitcroft# Checks which may be anchored in the context. 141200df344fSAndy Whitcroft# 141300df344fSAndy Whitcroft 141400df344fSAndy Whitcroft# Check for switch () and associated case and default 141500df344fSAndy Whitcroft# statements should be at the same indent. 141600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 141700df344fSAndy Whitcroft my $err = ''; 141800df344fSAndy Whitcroft my $sep = ''; 141900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 142000df344fSAndy Whitcroft shift(@ctx); 142100df344fSAndy Whitcroft for my $ctx (@ctx) { 142200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 142300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 142400df344fSAndy Whitcroft $indent != $cindent) { 142500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 142600df344fSAndy Whitcroft $sep = ''; 142700df344fSAndy Whitcroft } else { 142800df344fSAndy Whitcroft $sep = "[...]\n"; 142900df344fSAndy Whitcroft } 143000df344fSAndy Whitcroft } 143100df344fSAndy Whitcroft if ($err ne '') { 14329c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 1433de7d4f0eSAndy Whitcroft } 1434de7d4f0eSAndy Whitcroft } 1435de7d4f0eSAndy Whitcroft 1436de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1437de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1438c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1439773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1440773647a0SAndy Whitcroft 14419c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1442de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1443de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1444de7d4f0eSAndy Whitcroft 1445548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1446548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1447de7d4f0eSAndy Whitcroft 1448548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1449548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1450548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1451548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1452548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1453773647a0SAndy Whitcroft $ctx_ln++; 1454773647a0SAndy Whitcroft } 1455548596d5SAndy Whitcroft 145653210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 145753210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1458773647a0SAndy Whitcroft 1459773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1460773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . 1461c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 146200df344fSAndy Whitcroft } 1463773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1464773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1465773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1466773647a0SAndy Whitcroft { 14679c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 14689c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1469773647a0SAndy Whitcroft WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1470c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 14719c0ca6f9SAndy Whitcroft } 14729c0ca6f9SAndy Whitcroft } 147300df344fSAndy Whitcroft } 147400df344fSAndy Whitcroft 14754d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 14764d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 14774d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 14784d001e4dSAndy Whitcroft 14794d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 14804d001e4dSAndy Whitcroft 14814d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 14824d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 14834d001e4dSAndy Whitcroft # where necessary. 14844d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 14854d001e4dSAndy Whitcroft 14864d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 14876f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 14886f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 14894d001e4dSAndy Whitcroft 14904d001e4dSAndy Whitcroft # We want to check the first line inside the block 14914d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 14924d001e4dSAndy Whitcroft # 1) any blank line termination 14934d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 14944d001e4dSAndy Whitcroft # 3) any do (...) { 14954d001e4dSAndy Whitcroft my $continuation = 0; 14964d001e4dSAndy Whitcroft my $check = 0; 14974d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 14984d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 14994d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 15004d001e4dSAndy Whitcroft $continuation = 1; 15014d001e4dSAndy Whitcroft } 15029bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 15034d001e4dSAndy Whitcroft $check = 1; 15044d001e4dSAndy Whitcroft $cond_lines++; 15054d001e4dSAndy Whitcroft } 15064d001e4dSAndy Whitcroft 15074d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 15084d001e4dSAndy Whitcroft # preprocessor statement. 15094d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 15104d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 15114d001e4dSAndy Whitcroft $check = 0; 15124d001e4dSAndy Whitcroft } 15134d001e4dSAndy Whitcroft 15149bd49efeSAndy Whitcroft my $cond_ptr = -1; 1515740504c6SAndy Whitcroft $continuation = 0; 15169bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 15179bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 15184d001e4dSAndy Whitcroft 1519f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 1520f16fa28fSAndy Whitcroft # is not linear. 1521f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1522f16fa28fSAndy Whitcroft $check = 0; 1523f16fa28fSAndy Whitcroft } 1524f16fa28fSAndy Whitcroft 15259bd49efeSAndy Whitcroft # Ignore: 15269bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 15279bd49efeSAndy Whitcroft # 2) preprocessor lines, and 15289bd49efeSAndy Whitcroft # 3) labels. 1529740504c6SAndy Whitcroft if ($continuation || 1530740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 15319bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 15329bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 1533740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 15349bd49efeSAndy Whitcroft $s =~ s/^.*?\n//; 15359bd49efeSAndy Whitcroft $cond_lines++; 15369bd49efeSAndy Whitcroft } 15374d001e4dSAndy Whitcroft } 15384d001e4dSAndy Whitcroft 15394d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 15404d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 15414d001e4dSAndy Whitcroft 15424d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 15434d001e4dSAndy Whitcroft # this is not this patch's fault. 15444d001e4dSAndy Whitcroft if (!defined($stat_real) || 15454d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 15464d001e4dSAndy Whitcroft $check = 0; 15474d001e4dSAndy Whitcroft } 15484d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 15494d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 15504d001e4dSAndy Whitcroft } 15514d001e4dSAndy Whitcroft 15529bd49efeSAndy 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"; 15534d001e4dSAndy Whitcroft 15544d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 15554d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 15564d001e4dSAndy Whitcroft WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 15574d001e4dSAndy Whitcroft } 15584d001e4dSAndy Whitcroft } 15594d001e4dSAndy Whitcroft 15606c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 15616c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 15621f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 15631f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 15646c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1565c2fdda0dSAndy Whitcroft if ($dbg_values) { 1566c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1567cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1568cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 15691f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 1570c2fdda0dSAndy Whitcroft } 15716c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 15726c72ffaaSAndy Whitcroft 157300df344fSAndy Whitcroft#ignore lines not being added 157400df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 157500df344fSAndy Whitcroft 1576653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 15777429c690SAndy Whitcroft if ($dbg_type) { 15787429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 15797429c690SAndy Whitcroft ERROR("TEST: is type\n" . $herecurr); 15807429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 15817429c690SAndy Whitcroft ERROR("TEST: is not type ($1 is)\n". $herecurr); 15827429c690SAndy Whitcroft } 1583653d4876SAndy Whitcroft next; 1584653d4876SAndy Whitcroft } 1585a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 1586a1ef277eSAndy Whitcroft if ($dbg_attr) { 1587a1ef277eSAndy Whitcroft if ($line =~ /^.\s*$Attribute\s*$/) { 1588a1ef277eSAndy Whitcroft ERROR("TEST: is attr\n" . $herecurr); 1589a1ef277eSAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) { 1590a1ef277eSAndy Whitcroft ERROR("TEST: is not attr ($1 is)\n". $herecurr); 1591a1ef277eSAndy Whitcroft } 1592a1ef277eSAndy Whitcroft next; 1593a1ef277eSAndy Whitcroft } 1594653d4876SAndy Whitcroft 1595f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 1596f0a594c1SAndy Whitcroft if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1597f0a594c1SAndy Whitcroft $line =~ /^.\s*{/) { 1598773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . $hereprev); 1599f0a594c1SAndy Whitcroft } 1600f0a594c1SAndy Whitcroft 160100df344fSAndy Whitcroft# 160200df344fSAndy Whitcroft# Checks which are anchored on the added line. 160300df344fSAndy Whitcroft# 160400df344fSAndy Whitcroft 1605653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 1606c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1607653d4876SAndy Whitcroft my $path = $1; 1608653d4876SAndy Whitcroft if ($path =~ m{//}) { 1609de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 1610de7d4f0eSAndy Whitcroft $herecurr); 1611653d4876SAndy Whitcroft } 1612653d4876SAndy Whitcroft } 1613653d4876SAndy Whitcroft 161400df344fSAndy Whitcroft# no C99 // comments 161500df344fSAndy Whitcroft if ($line =~ m{//}) { 1616de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 161700df344fSAndy Whitcroft } 161800df344fSAndy Whitcroft # Remove C99 comments. 16190a920b5bSAndy Whitcroft $line =~ s@//.*@@; 16206c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 16210a920b5bSAndy Whitcroft 16220a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 1623653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 1624653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1625653d4876SAndy Whitcroft my $name = $1; 162648012058SAndy Whitcroft if ($prevline !~ /(?: 162748012058SAndy Whitcroft ^.}| 162848012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 162948012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 163048012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 163148012058SAndy Whitcroft ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 163248012058SAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[) 163348012058SAndy Whitcroft )/x) { 1634de7d4f0eSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 16350a920b5bSAndy Whitcroft } 16360a920b5bSAndy Whitcroft } 16370a920b5bSAndy Whitcroft 1638f0a594c1SAndy Whitcroft# check for external initialisers. 1639c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1640f0a594c1SAndy Whitcroft ERROR("do not initialise externals to 0 or NULL\n" . 1641f0a594c1SAndy Whitcroft $herecurr); 1642f0a594c1SAndy Whitcroft } 16430a920b5bSAndy Whitcroft# check for static initialisers. 16442d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 1645de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 1646de7d4f0eSAndy Whitcroft $herecurr); 16470a920b5bSAndy Whitcroft } 16480a920b5bSAndy Whitcroft 1649653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1650653d4876SAndy Whitcroft# make sense. 1651653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 16528054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 1653c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 16548ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 1655653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1656de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 16570a920b5bSAndy Whitcroft } 16580a920b5bSAndy Whitcroft 16590a920b5bSAndy Whitcroft# * goes on variable not on type 166065863862SAndy Whitcroft # (char*[ const]) 1661*00ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 166265863862SAndy Whitcroft my ($from, $to) = ($1, $1); 1663d8aaf121SAndy Whitcroft 166465863862SAndy Whitcroft # Should start with a space. 166565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 166665863862SAndy Whitcroft # Should not end with a space. 166765863862SAndy Whitcroft $to =~ s/\s+$//; 166865863862SAndy Whitcroft # '*'s should not have spaces between. 1669f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 167065863862SAndy Whitcroft } 1671d8aaf121SAndy Whitcroft 167265863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 167365863862SAndy Whitcroft if ($from ne $to) { 167465863862SAndy Whitcroft ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 167565863862SAndy Whitcroft } 1676*00ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 167765863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 1678d8aaf121SAndy Whitcroft 167965863862SAndy Whitcroft # Should start with a space. 168065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 168165863862SAndy Whitcroft # Should not end with a space. 168265863862SAndy Whitcroft $to =~ s/\s+$//; 168365863862SAndy Whitcroft # '*'s should not have spaces between. 1684f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 168565863862SAndy Whitcroft } 168665863862SAndy Whitcroft # Modifiers should have spaces. 168765863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 168865863862SAndy Whitcroft 168965863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 169065863862SAndy Whitcroft if ($from ne $to) { 169165863862SAndy Whitcroft ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 169265863862SAndy Whitcroft } 16930a920b5bSAndy Whitcroft } 16940a920b5bSAndy Whitcroft 16950a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 16960a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 16970a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 16980a920b5bSAndy Whitcroft# print "$herecurr"; 16990a920b5bSAndy Whitcroft# $clean = 0; 17000a920b5bSAndy Whitcroft# } 17010a920b5bSAndy Whitcroft 17028905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1703c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 17048905a67cSAndy Whitcroft } 17058905a67cSAndy Whitcroft 170600df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 170700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 170800df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 170900df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 171000df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1711f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 171200df344fSAndy Whitcroft my $ok = 0; 171300df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 171400df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 171500df344fSAndy Whitcroft # we have a preceeding printk if it ends 171600df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 171700df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 171800df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 171900df344fSAndy Whitcroft $ok = 1; 172000df344fSAndy Whitcroft } 172100df344fSAndy Whitcroft last; 172200df344fSAndy Whitcroft } 172300df344fSAndy Whitcroft } 172400df344fSAndy Whitcroft if ($ok == 0) { 1725de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 17260a920b5bSAndy Whitcroft } 172700df344fSAndy Whitcroft } 17280a920b5bSAndy Whitcroft 1729653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1730653d4876SAndy Whitcroft# or if closed on same line 1731c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1732c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1733de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 17340a920b5bSAndy Whitcroft } 1735653d4876SAndy Whitcroft 17368905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 17378905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 17388905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 17398905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 17408905a67cSAndy Whitcroft } 17418905a67cSAndy Whitcroft 17428d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 17438d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1744fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1745fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 17468d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 17478d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 17488d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1749fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 1750fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 17518d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 17528d31cfceSAndy Whitcroft } 17538d31cfceSAndy Whitcroft } 17548d31cfceSAndy Whitcroft 1755f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 17566c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 1757c2fdda0dSAndy Whitcroft my $name = $1; 1758773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 1759773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 1760c2fdda0dSAndy Whitcroft 1761c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 1762773647a0SAndy Whitcroft if ($name =~ /^(?: 1763773647a0SAndy Whitcroft if|for|while|switch|return|case| 1764773647a0SAndy Whitcroft volatile|__volatile__| 1765773647a0SAndy Whitcroft __attribute__|format|__extension__| 1766773647a0SAndy Whitcroft asm|__asm__)$/x) 1767773647a0SAndy Whitcroft { 1768c2fdda0dSAndy Whitcroft 1769c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 1770c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 1771c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 1772c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 1773773647a0SAndy Whitcroft 1774773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 1775c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 1776c2fdda0dSAndy Whitcroft 1777c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 1778c2fdda0dSAndy Whitcroft # likely a typedef for a function. 1779773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 1780c2fdda0dSAndy Whitcroft 1781c2fdda0dSAndy Whitcroft } else { 1782773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 1783f0a594c1SAndy Whitcroft } 17846c72ffaaSAndy Whitcroft } 1785653d4876SAndy Whitcroft# Check operator spacing. 17860a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 17879c0ca6f9SAndy Whitcroft my $ops = qr{ 17889c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 17899c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 17909c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 17911f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 17921f65f947SAndy Whitcroft \?|: 17939c0ca6f9SAndy Whitcroft }x; 1794cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 179500df344fSAndy Whitcroft my $off = 0; 17966c72ffaaSAndy Whitcroft 17976c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 17986c72ffaaSAndy Whitcroft 17990a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 18004a0df2efSAndy Whitcroft $off += length($elements[$n]); 18014a0df2efSAndy Whitcroft 1802773647a0SAndy Whitcroft # Pick up the preceeding and succeeding characters. 1803773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 1804773647a0SAndy Whitcroft my $cc = ''; 1805773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 1806773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 1807773647a0SAndy Whitcroft } 1808773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 1809773647a0SAndy Whitcroft 18104a0df2efSAndy Whitcroft my $a = ''; 18114a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 18124a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 1813cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 18144a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 18154a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 1816773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 18174a0df2efSAndy Whitcroft 18180a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 18194a0df2efSAndy Whitcroft 18204a0df2efSAndy Whitcroft my $c = ''; 18210a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 18224a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 18234a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1824cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 18254a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 18264a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 18278b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 18284a0df2efSAndy Whitcroft } else { 18294a0df2efSAndy Whitcroft $c = 'E'; 18300a920b5bSAndy Whitcroft } 18310a920b5bSAndy Whitcroft 18324a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 18334a0df2efSAndy Whitcroft 18344a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 18354a0df2efSAndy Whitcroft 18366c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 1837de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 18380a920b5bSAndy Whitcroft 183974048ed8SAndy Whitcroft # Pull out the value of this operator. 18406c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 18410a920b5bSAndy Whitcroft 18421f65f947SAndy Whitcroft # Get the full operator variant. 18431f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 18441f65f947SAndy Whitcroft 184513214adfSAndy Whitcroft # Ignore operators passed as parameters. 184613214adfSAndy Whitcroft if ($op_type ne 'V' && 184713214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 184813214adfSAndy Whitcroft 1849cf655043SAndy Whitcroft# # Ignore comments 1850cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 185113214adfSAndy Whitcroft 1852d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 185313214adfSAndy Whitcroft } elsif ($op eq ';') { 1854cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 1855cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 1856773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 1857d8aaf121SAndy Whitcroft } 1858d8aaf121SAndy Whitcroft 1859d8aaf121SAndy Whitcroft # // is a comment 1860d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 18610a920b5bSAndy Whitcroft 18621f65f947SAndy Whitcroft # No spaces for: 18631f65f947SAndy Whitcroft # -> 18641f65f947SAndy Whitcroft # : when part of a bitfield 18651f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 18664a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 1867773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 18680a920b5bSAndy Whitcroft } 18690a920b5bSAndy Whitcroft 18700a920b5bSAndy Whitcroft # , must have a space on the right. 18710a920b5bSAndy Whitcroft } elsif ($op eq ',') { 1872cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 1873773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 18740a920b5bSAndy Whitcroft } 18750a920b5bSAndy Whitcroft 18769c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 187774048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 18789c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 18799c0ca6f9SAndy Whitcroft 18809c0ca6f9SAndy Whitcroft # unary operators should have a space before and 18819c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 18829c0ca6f9SAndy Whitcroft # unary operator, or a cast 18839c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 188474048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 18850d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 1886cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1887773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 18880a920b5bSAndy Whitcroft } 1889171ae1a4SAndy Whitcroft if ($op eq '*' && $cc =~/\s*const\b/) { 1890171ae1a4SAndy Whitcroft # A unary '*' may be const 1891171ae1a4SAndy Whitcroft 1892171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 1893773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 18940a920b5bSAndy Whitcroft } 18950a920b5bSAndy Whitcroft 18960a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 18970a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 1898773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 1899773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 19000a920b5bSAndy Whitcroft } 1901773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 1902773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1903773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 1904653d4876SAndy Whitcroft } 1905773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 1906773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 1907773647a0SAndy Whitcroft } 1908773647a0SAndy Whitcroft 19090a920b5bSAndy Whitcroft 19100a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 19119c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 19129c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 19139c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 1914c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 1915c2fdda0dSAndy Whitcroft $op eq '%') 19160a920b5bSAndy Whitcroft { 1917773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 1918de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 1919de7d4f0eSAndy Whitcroft $hereptr); 19200a920b5bSAndy Whitcroft } 19210a920b5bSAndy Whitcroft 19221f65f947SAndy Whitcroft # A colon needs no spaces before when it is 19231f65f947SAndy Whitcroft # terminating a case value or a label. 19241f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 19251f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 19261f65f947SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 19271f65f947SAndy Whitcroft } 19281f65f947SAndy Whitcroft 19290a920b5bSAndy Whitcroft # All the others need spaces both sides. 1930cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 19311f65f947SAndy Whitcroft my $ok = 0; 19321f65f947SAndy Whitcroft 193322f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 19341f65f947SAndy Whitcroft if (($op eq '<' && 19351f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 19361f65f947SAndy Whitcroft ($op eq '>' && 19371f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 19381f65f947SAndy Whitcroft { 19391f65f947SAndy Whitcroft $ok = 1; 19401f65f947SAndy Whitcroft } 19411f65f947SAndy Whitcroft 19421f65f947SAndy Whitcroft # Ignore ?: 19431f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 19441f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 19451f65f947SAndy Whitcroft $ok = 1; 19461f65f947SAndy Whitcroft } 19471f65f947SAndy Whitcroft 19481f65f947SAndy Whitcroft if ($ok == 0) { 1949773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 19500a920b5bSAndy Whitcroft } 195122f2a2efSAndy Whitcroft } 19524a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 19530a920b5bSAndy Whitcroft } 19540a920b5bSAndy Whitcroft } 19550a920b5bSAndy Whitcroft 1956f0a594c1SAndy Whitcroft# check for multiple assignments 1957f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 19586c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 1959f0a594c1SAndy Whitcroft } 1960f0a594c1SAndy Whitcroft 196122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 196222f2a2efSAndy Whitcroft## # continuation. 196322f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 196422f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 196522f2a2efSAndy Whitcroft## 196622f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 196722f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 196822f2a2efSAndy Whitcroft## my $ln = $line; 196922f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 197022f2a2efSAndy Whitcroft## } 197122f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 197222f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 197322f2a2efSAndy Whitcroft## } 197422f2a2efSAndy Whitcroft## } 1975f0a594c1SAndy Whitcroft 19760a920b5bSAndy Whitcroft#need space before brace following if, while, etc 197722f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 197822f2a2efSAndy Whitcroft $line =~ /do{/) { 1979773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 1980de7d4f0eSAndy Whitcroft } 1981de7d4f0eSAndy Whitcroft 1982de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 1983de7d4f0eSAndy Whitcroft# on the line 1984de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 1985773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 19860a920b5bSAndy Whitcroft } 19870a920b5bSAndy Whitcroft 198822f2a2efSAndy Whitcroft# check spacing on square brackets 198922f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 1990773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 199122f2a2efSAndy Whitcroft } 199222f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 1993773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 199422f2a2efSAndy Whitcroft } 199522f2a2efSAndy Whitcroft 1996c45dcabdSAndy Whitcroft# check spacing on parentheses 19979c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 19989c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 1999773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 200022f2a2efSAndy Whitcroft } 200113214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2002c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2003c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2004773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 200522f2a2efSAndy Whitcroft } 200622f2a2efSAndy Whitcroft 20070a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 20084a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 20090a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2010de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 20110a920b5bSAndy Whitcroft } 20120a920b5bSAndy Whitcroft 2013c45dcabdSAndy Whitcroft# Return is not a function. 2014c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2015c45dcabdSAndy Whitcroft my $spacing = $1; 2016c45dcabdSAndy Whitcroft my $value = $2; 2017c45dcabdSAndy Whitcroft 201886f9d059SAndy Whitcroft # Flatten any parentheses 2019fee61c47SAndy Whitcroft $value =~ s/\)\(/\) \(/g; 202063f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 202163f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 202263f17f89SAndy Whitcroft $Compare\s* 202363f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 202463f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2025c45dcabdSAndy Whitcroft } 2026c45dcabdSAndy Whitcroft 2027c45dcabdSAndy Whitcroft if ($value =~ /^(?:$Ident|-?$Constant)$/) { 2028c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2029c45dcabdSAndy Whitcroft 2030c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2031c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 2032c45dcabdSAndy Whitcroft } 2033c45dcabdSAndy Whitcroft } 2034c45dcabdSAndy Whitcroft 20350a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 20364a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2037773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 20380a920b5bSAndy Whitcroft } 20390a920b5bSAndy Whitcroft 2040f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2041f5fe35ddSAndy Whitcroft# statements after the conditional. 2042170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2043170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2044170d3a22SAndy Whitcroft $remain_next, $off_next); 2045170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2046170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2047170d3a22SAndy Whitcroft 2048170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2049170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2050170d3a22SAndy Whitcroft # then count those as offsets. 2051170d3a22SAndy Whitcroft my ($whitespace) = 2052170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2053170d3a22SAndy Whitcroft my $offset = 2054170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2055170d3a22SAndy Whitcroft 2056170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2057170d3a22SAndy Whitcroft $offset} = 1; 2058170d3a22SAndy Whitcroft } 2059170d3a22SAndy Whitcroft } 2060170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2061170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2062171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 20638905a67cSAndy Whitcroft 2064b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2065c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 20668905a67cSAndy Whitcroft } 20678905a67cSAndy Whitcroft 20688905a67cSAndy Whitcroft # Find out what is on the end of the line after the 20698905a67cSAndy Whitcroft # conditional. 2070773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 20718905a67cSAndy Whitcroft $s =~ s/\n.*//g; 207213214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 207353210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 207453210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2075773647a0SAndy Whitcroft { 2076bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2077bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2078bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 2079bb44ad39SAndy Whitcroft 2080bb44ad39SAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 2081bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2082bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2083bb44ad39SAndy Whitcroft } 2084bb44ad39SAndy Whitcroft 2085bb44ad39SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 20868905a67cSAndy Whitcroft } 20878905a67cSAndy Whitcroft } 20888905a67cSAndy Whitcroft 208913214adfSAndy Whitcroft# Check for bitwise tests written as boolean 209013214adfSAndy Whitcroft if ($line =~ / 209113214adfSAndy Whitcroft (?: 209213214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 209313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 209413214adfSAndy Whitcroft (?:\&\&|\|\|) 209513214adfSAndy Whitcroft | 209613214adfSAndy Whitcroft (?:\&\&|\|\|) 209713214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 209813214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 209913214adfSAndy Whitcroft )/x) 210013214adfSAndy Whitcroft { 210113214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 210213214adfSAndy Whitcroft } 210313214adfSAndy Whitcroft 21048905a67cSAndy Whitcroft# if and else should not have general statements after it 210513214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 210613214adfSAndy Whitcroft my $s = $1; 210713214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 210813214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 21098905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 21100a920b5bSAndy Whitcroft } 211113214adfSAndy Whitcroft } 211239667782SAndy Whitcroft# if should not continue a brace 211339667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 211439667782SAndy Whitcroft ERROR("trailing statements should be on next line\n" . 211539667782SAndy Whitcroft $herecurr); 211639667782SAndy Whitcroft } 2117a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2118a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2119a1080bf8SAndy Whitcroft $line !~ /\G(?: 21203fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2121a1080bf8SAndy Whitcroft \s*return\s+ 2122a1080bf8SAndy Whitcroft )/xg) 2123a1080bf8SAndy Whitcroft { 2124a1080bf8SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 2125a1080bf8SAndy Whitcroft } 21260a920b5bSAndy Whitcroft 21270a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 21280a920b5bSAndy Whitcroft # indent level to be relevant to each other. 21290a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 21300a920b5bSAndy Whitcroft $previndent == $indent) { 2131de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 21320a920b5bSAndy Whitcroft } 21330a920b5bSAndy Whitcroft 2134c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2135c2fdda0dSAndy Whitcroft $previndent == $indent) { 2136c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2137c2fdda0dSAndy Whitcroft 2138c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2139c2fdda0dSAndy Whitcroft # conditional. 2140773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2141c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2142c2fdda0dSAndy Whitcroft 2143c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2144c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 2145c2fdda0dSAndy Whitcroft } 2146c2fdda0dSAndy Whitcroft } 2147c2fdda0dSAndy Whitcroft 21480a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 21490a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 21500a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 21510a920b5bSAndy Whitcroft# print "$herecurr"; 21520a920b5bSAndy Whitcroft# $clean = 0; 21530a920b5bSAndy Whitcroft# } 21540a920b5bSAndy Whitcroft 21550a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2156c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2157de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 21580a920b5bSAndy Whitcroft } 21590a920b5bSAndy Whitcroft 2160653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2161c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2162e09dec48SAndy Whitcroft my $file = "$1.h"; 2163e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2164e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2165e09dec48SAndy Whitcroft $realfile ne $checkfile && 2166c45dcabdSAndy Whitcroft $1 ne 'irq') 2167c45dcabdSAndy Whitcroft { 2168e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2169e09dec48SAndy Whitcroft CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2170e09dec48SAndy Whitcroft } else { 2171e09dec48SAndy Whitcroft WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2172e09dec48SAndy Whitcroft } 21730a920b5bSAndy Whitcroft } 21740a920b5bSAndy Whitcroft } 21750a920b5bSAndy Whitcroft 2176653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2177653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2178cf655043SAndy Whitcroft# in a known good container 2179b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2180b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2181d8aaf121SAndy Whitcroft my $ln = $linenr; 2182d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2183c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2184c45dcabdSAndy Whitcroft my $ctx = ''; 2185653d4876SAndy Whitcroft 2186c45dcabdSAndy Whitcroft my $args = defined($1); 2187de7d4f0eSAndy Whitcroft 2188c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2189c45dcabdSAndy Whitcroft # search to that. 2190c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2191c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2192c45dcabdSAndy Whitcroft { 2193c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2194a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2195c45dcabdSAndy Whitcroft $ln++; 2196de7d4f0eSAndy Whitcroft } 2197c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2198d8aaf121SAndy Whitcroft 2199c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2200c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2201c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2202a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2203c45dcabdSAndy Whitcroft 2204c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2205c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2206c45dcabdSAndy Whitcroft $rest = ''; 2207636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2208636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2209a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2210c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2211c45dcabdSAndy Whitcroft $cnt--; 2212a3bb97a7SAndy Whitcroft } 2213a3bb97a7SAndy Whitcroft $ln++; 2214c45dcabdSAndy Whitcroft $off = 0; 2215c45dcabdSAndy Whitcroft } 2216c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2217c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2218c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2219c45dcabdSAndy Whitcroft 2220c45dcabdSAndy Whitcroft # Clean up the original statement. 2221c45dcabdSAndy Whitcroft if ($args) { 2222c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2223d8aaf121SAndy Whitcroft } else { 2224c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2225c45dcabdSAndy Whitcroft } 2226292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2227c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2228c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2229c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2230c45dcabdSAndy Whitcroft 2231c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2232bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2233bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2234bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2235bf30d6edSAndy Whitcroft { 2236c45dcabdSAndy Whitcroft } 2237c45dcabdSAndy Whitcroft 2238c45dcabdSAndy Whitcroft my $exceptions = qr{ 2239c45dcabdSAndy Whitcroft $Declare| 2240c45dcabdSAndy Whitcroft module_param_named| 2241c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2242c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2243c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2244383099fdSAndy Whitcroft __typeof__\(| 2245383099fdSAndy Whitcroft \.$Ident\s*=\s* 2246c45dcabdSAndy Whitcroft }x; 2247383099fdSAndy Whitcroft #print "REST<$rest> dstat<$dstat>\n"; 2248c45dcabdSAndy Whitcroft if ($rest ne '') { 2249c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2250c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2251c45dcabdSAndy Whitcroft { 2252c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2253c45dcabdSAndy Whitcroft } 2254c45dcabdSAndy Whitcroft 2255c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2256c45dcabdSAndy Whitcroft if ($dstat ne '' && 2257c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2258c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2259b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2260c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2261c45dcabdSAndy Whitcroft { 2262de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2263d8aaf121SAndy Whitcroft } 22640a920b5bSAndy Whitcroft } 2265653d4876SAndy Whitcroft } 22660a920b5bSAndy Whitcroft 2267080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2268080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2269080ba929SMike Frysinger# . 2270080ba929SMike Frysinger# ALIGN(...) 2271080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2272080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2273080ba929SMike Frysinger WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2274080ba929SMike Frysinger } 2275080ba929SMike Frysinger 2276f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 227713214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 227813214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2279cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 228013214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2281cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2282cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 228313214adfSAndy Whitcroft my $allowed = 0; 228413214adfSAndy Whitcroft my $seen = 0; 2285773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2286cf655043SAndy Whitcroft my $ln = $linenr - 1; 228713214adfSAndy Whitcroft for my $chunk (@chunks) { 228813214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 228913214adfSAndy Whitcroft 2290773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2291773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2292773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2293773647a0SAndy Whitcroft 2294773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2295773647a0SAndy Whitcroft 2296773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2297773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2298773647a0SAndy Whitcroft 2299773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2300cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2301cf655043SAndy Whitcroft 2302773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 230313214adfSAndy Whitcroft 230413214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 230513214adfSAndy Whitcroft 2306cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2307cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2308cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 230913214adfSAndy Whitcroft $allowed = 1; 231013214adfSAndy Whitcroft } 231113214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2312cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 231313214adfSAndy Whitcroft $allowed = 1; 231413214adfSAndy Whitcroft } 2315cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2316cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 231713214adfSAndy Whitcroft $allowed = 1; 231813214adfSAndy Whitcroft } 231913214adfSAndy Whitcroft } 232013214adfSAndy Whitcroft if ($seen && !$allowed) { 2321cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 232213214adfSAndy Whitcroft } 232313214adfSAndy Whitcroft } 232413214adfSAndy Whitcroft } 2325773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 232613214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2327cf655043SAndy Whitcroft my $allowed = 0; 2328f0a594c1SAndy Whitcroft 2329cf655043SAndy Whitcroft # Check the pre-context. 2330cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2331cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2332cf655043SAndy Whitcroft $allowed = 1; 2333f0a594c1SAndy Whitcroft } 2334773647a0SAndy Whitcroft 2335773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2336773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2337773647a0SAndy Whitcroft 2338cf655043SAndy Whitcroft # Check the condition. 2339cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2340773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2341cf655043SAndy Whitcroft if (defined $cond) { 2342773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2343cf655043SAndy Whitcroft } 2344cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2345cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2346cf655043SAndy Whitcroft $allowed = 1; 2347cf655043SAndy Whitcroft } 2348cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2349cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2350cf655043SAndy Whitcroft $allowed = 1; 2351cf655043SAndy Whitcroft } 2352cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2353cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2354cf655043SAndy Whitcroft $allowed = 1; 2355cf655043SAndy Whitcroft } 2356cf655043SAndy Whitcroft # Check the post-context. 2357cf655043SAndy Whitcroft if (defined $chunks[1]) { 2358cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2359cf655043SAndy Whitcroft if (defined $cond) { 2360773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2361cf655043SAndy Whitcroft } 2362cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2363cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2364cf655043SAndy Whitcroft $allowed = 1; 2365cf655043SAndy Whitcroft } 2366cf655043SAndy Whitcroft } 2367cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2368cf655043SAndy Whitcroft my $herectx = $here . "\n";; 2369f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2370cf655043SAndy Whitcroft 2371f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2372f055663cSAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n";; 2373cf655043SAndy Whitcroft } 2374cf655043SAndy Whitcroft 2375cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 2376f0a594c1SAndy Whitcroft } 2377f0a594c1SAndy Whitcroft } 2378f0a594c1SAndy Whitcroft 2379653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 23804a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2381c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2382de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 23830a920b5bSAndy Whitcroft } 23840a920b5bSAndy Whitcroft } 23850a920b5bSAndy Whitcroft 23864a0df2efSAndy Whitcroft# don't use deprecated functions 23874a0df2efSAndy Whitcroft for my $func (@dep_functions) { 238800df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2389de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 23904a0df2efSAndy Whitcroft } 23914a0df2efSAndy Whitcroft } 23924a0df2efSAndy Whitcroft 23934a0df2efSAndy Whitcroft# no volatiles please 23946c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 23956c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2396de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 23974a0df2efSAndy Whitcroft } 23984a0df2efSAndy Whitcroft 23999c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 24009c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 24019c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 24029c0ca6f9SAndy Whitcroft } 24039c0ca6f9SAndy Whitcroft 240400df344fSAndy Whitcroft# warn about #if 0 2405c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2406de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2407de7d4f0eSAndy Whitcroft $herecurr); 24084a0df2efSAndy Whitcroft } 24094a0df2efSAndy Whitcroft 2410f0a594c1SAndy Whitcroft# check for needless kfree() checks 2411f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2412f0a594c1SAndy Whitcroft my $expr = $1; 2413f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 24143c232147SWolfram Sang WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2415f0a594c1SAndy Whitcroft } 2416f0a594c1SAndy Whitcroft } 24174c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 24184c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 24194c432a8fSGreg Kroah-Hartman my $expr = $1; 24204c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 24214c432a8fSGreg Kroah-Hartman WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 24224c432a8fSGreg Kroah-Hartman } 24234c432a8fSGreg Kroah-Hartman } 2424f0a594c1SAndy Whitcroft 242500df344fSAndy Whitcroft# warn about #ifdefs in C files 2426c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 242700df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 242800df344fSAndy Whitcroft# print "$herecurr"; 242900df344fSAndy Whitcroft# $clean = 0; 243000df344fSAndy Whitcroft# } 243100df344fSAndy Whitcroft 243222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2433c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 243422f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 243522f2a2efSAndy Whitcroft } 243622f2a2efSAndy Whitcroft 24374a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2438171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2439171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 24404a0df2efSAndy Whitcroft my $which = $1; 24414a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2442de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 24434a0df2efSAndy Whitcroft } 24444a0df2efSAndy Whitcroft } 24454a0df2efSAndy Whitcroft# check for memory barriers without a comment. 24464a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 24474a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2448de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 24494a0df2efSAndy Whitcroft } 24504a0df2efSAndy Whitcroft } 24514a0df2efSAndy Whitcroft# check of hardware specific defines 2452c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2453de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 24540a920b5bSAndy Whitcroft } 2455653d4876SAndy Whitcroft 2456de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2457de7d4f0eSAndy Whitcroft# storage class and type. 24589c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 24599c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2460de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2461de7d4f0eSAndy Whitcroft } 2462de7d4f0eSAndy Whitcroft 24638905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 24648905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 24658905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 24668905a67cSAndy Whitcroft } 24678905a67cSAndy Whitcroft 2468de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2469171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2470c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2471171ae1a4SAndy Whitcroft { 2472c45dcabdSAndy Whitcroft my $function_name = $1; 2473c45dcabdSAndy Whitcroft my $paren_space = $2; 2474171ae1a4SAndy Whitcroft 2475171ae1a4SAndy Whitcroft my $s = $stat; 2476171ae1a4SAndy Whitcroft if (defined $cond) { 2477171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2478171ae1a4SAndy Whitcroft } 2479c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2480c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2481c45dcabdSAndy Whitcroft { 2482de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2483de7d4f0eSAndy Whitcroft } 2484de7d4f0eSAndy Whitcroft 2485171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2486171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2487171ae1a4SAndy Whitcroft } 24889c9ba34eSAndy Whitcroft 24899c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 24909c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 24919c9ba34eSAndy Whitcroft { 24929c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2493171ae1a4SAndy Whitcroft } 2494171ae1a4SAndy Whitcroft 2495de7d4f0eSAndy Whitcroft# checks for new __setup's 2496de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2497de7d4f0eSAndy Whitcroft my $name = $1; 2498de7d4f0eSAndy Whitcroft 2499de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2500de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2501de7d4f0eSAndy Whitcroft } 2502653d4876SAndy Whitcroft } 25039c0ca6f9SAndy Whitcroft 25049c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 25059c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 25069c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 25079c0ca6f9SAndy Whitcroft } 250813214adfSAndy Whitcroft 250913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 251013214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 251113214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 251213214adfSAndy Whitcroft } 2513773647a0SAndy Whitcroft 2514773647a0SAndy Whitcroft# check for semaphores used as mutexes 2515171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2516773647a0SAndy Whitcroft WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); 2517773647a0SAndy Whitcroft } 2518773647a0SAndy Whitcroft# check for semaphores used as mutexes 2519171ae1a4SAndy Whitcroft if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { 2520773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 2521773647a0SAndy Whitcroft } 2522773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto* 2523773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2524773647a0SAndy Whitcroft WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); 2525773647a0SAndy Whitcroft } 2526f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2527f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2528f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2529f3db6639SMichael Ellerman } 25302b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const. 25316903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 25326903ffb2SAndy Whitcroft $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) { 25336903ffb2SAndy Whitcroft WARN("struct $1 should normally be const\n" . 25346903ffb2SAndy Whitcroft $herecurr); 25352b6db5cbSAndy Whitcroft } 2536773647a0SAndy Whitcroft 2537773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2538773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2539773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2540c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2541c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2542171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2543171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2544171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2545773647a0SAndy Whitcroft { 2546773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2547773647a0SAndy Whitcroft } 25489c9ba34eSAndy Whitcroft 25499c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 25509c9ba34eSAndy Whitcroft my $string; 25519c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 25529c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 25532a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 25549c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 25559c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 25569c9ba34eSAndy Whitcroft last; 25579c9ba34eSAndy Whitcroft } 25589c9ba34eSAndy Whitcroft } 2559691d77b6SAndy Whitcroft 2560691d77b6SAndy Whitcroft# whine mightly about in_atomic 2561691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 2562691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 2563691d77b6SAndy Whitcroft ERROR("do not use in_atomic in drivers\n" . $herecurr); 2564f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 2565691d77b6SAndy Whitcroft WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2566691d77b6SAndy Whitcroft } 2567691d77b6SAndy Whitcroft } 256813214adfSAndy Whitcroft } 256913214adfSAndy Whitcroft 257013214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 257113214adfSAndy Whitcroft # so just keep quiet. 257213214adfSAndy Whitcroft if ($#rawlines == -1) { 257313214adfSAndy Whitcroft exit(0); 25740a920b5bSAndy Whitcroft } 25750a920b5bSAndy Whitcroft 25768905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 25778905a67cSAndy Whitcroft # things that appear to be patches. 25788905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 25798905a67cSAndy Whitcroft exit(0); 25808905a67cSAndy Whitcroft } 25818905a67cSAndy Whitcroft 25828905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 25838905a67cSAndy Whitcroft # just keep quiet. 25848905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 25858905a67cSAndy Whitcroft exit(0); 25868905a67cSAndy Whitcroft } 25878905a67cSAndy Whitcroft 25888905a67cSAndy Whitcroft if (!$is_patch) { 2589de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 25900a920b5bSAndy Whitcroft } 25910a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2592de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 25930a920b5bSAndy Whitcroft } 25940a920b5bSAndy Whitcroft 2595f0a594c1SAndy Whitcroft print report_dump(); 259613214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 259713214adfSAndy Whitcroft print "$filename " if ($summary_file); 25986c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 25996c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 26006c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 26018905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 26026c72ffaaSAndy Whitcroft } 26038905a67cSAndy Whitcroft 26040a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2605c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 26060a920b5bSAndy Whitcroft } 26070a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2608c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 26090a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 26100a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 26110a920b5bSAndy Whitcroft } 261213214adfSAndy Whitcroft 26130a920b5bSAndy Whitcroft return $clean; 26140a920b5bSAndy Whitcroft} 2615