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) 5131edb34SAndy Whitcroft# (c) 2008,2009, 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 1304876830SAndy Whitcroftmy $V = '0.29'; 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; 3177f5b10aSHannes Edermy $help = 0; 3277f5b10aSHannes Eder 3377f5b10aSHannes Edersub help { 3477f5b10aSHannes Eder my ($exitcode) = @_; 3577f5b10aSHannes Eder 3677f5b10aSHannes Eder print << "EOM"; 3777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 3877f5b10aSHannes EderVersion: $V 3977f5b10aSHannes Eder 4077f5b10aSHannes EderOptions: 4177f5b10aSHannes Eder -q, --quiet quiet 4277f5b10aSHannes Eder --no-tree run without a kernel tree 4377f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 4477f5b10aSHannes Eder --patch treat FILE as patchfile (default) 4577f5b10aSHannes Eder --emacs emacs compile window format 4677f5b10aSHannes Eder --terse one line per report 4777f5b10aSHannes Eder -f, --file treat FILE as regular source file 4877f5b10aSHannes Eder --subjective, --strict enable more subjective tests 4977f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 5077f5b10aSHannes Eder --no-summary suppress the per-file summary 5177f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 5277f5b10aSHannes Eder --summary-file include the filename in summary 5377f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 5477f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 5577f5b10aSHannes Eder is all off) 5677f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 5777f5b10aSHannes Eder literally 5877f5b10aSHannes Eder -h, --help, --version display this help and exit 5977f5b10aSHannes Eder 6077f5b10aSHannes EderWhen FILE is - read standard input. 6177f5b10aSHannes EderEOM 6277f5b10aSHannes Eder 6377f5b10aSHannes Eder exit($exitcode); 6477f5b10aSHannes Eder} 6577f5b10aSHannes Eder 660a920b5bSAndy WhitcroftGetOptions( 676c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 680a920b5bSAndy Whitcroft 'tree!' => \$tree, 690a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 700a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 716c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 728905a67cSAndy Whitcroft 'terse!' => \$terse, 7377f5b10aSHannes Eder 'f|file!' => \$file, 746c72ffaaSAndy Whitcroft 'subjective!' => \$check, 756c72ffaaSAndy Whitcroft 'strict!' => \$check, 766c72ffaaSAndy Whitcroft 'root=s' => \$root, 778905a67cSAndy Whitcroft 'summary!' => \$summary, 788905a67cSAndy Whitcroft 'mailback!' => \$mailback, 7913214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 8013214adfSAndy Whitcroft 81c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 82773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 8377f5b10aSHannes Eder 'h|help' => \$help, 8477f5b10aSHannes Eder 'version' => \$help 8577f5b10aSHannes Eder) or help(1); 8677f5b10aSHannes Eder 8777f5b10aSHannes Ederhelp(0) if ($help); 880a920b5bSAndy Whitcroft 890a920b5bSAndy Whitcroftmy $exit = 0; 900a920b5bSAndy Whitcroft 910a920b5bSAndy Whitcroftif ($#ARGV < 0) { 9277f5b10aSHannes Eder print "$P: no input files\n"; 930a920b5bSAndy Whitcroft exit(1); 940a920b5bSAndy Whitcroft} 950a920b5bSAndy Whitcroft 96c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 97c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 987429c690SAndy Whitcroftmy $dbg_type = 0; 99a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 100c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 10121caa13cSAndy Whitcroft ## no critic 10221caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 10321caa13cSAndy Whitcroft die "$@" if ($@); 104c2fdda0dSAndy Whitcroft} 105c2fdda0dSAndy Whitcroft 1068905a67cSAndy Whitcroftif ($terse) { 1078905a67cSAndy Whitcroft $emacs = 1; 1088905a67cSAndy Whitcroft $quiet++; 1098905a67cSAndy Whitcroft} 1108905a67cSAndy Whitcroft 1116c72ffaaSAndy Whitcroftif ($tree) { 1126c72ffaaSAndy Whitcroft if (defined $root) { 1136c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 1146c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 1156c72ffaaSAndy Whitcroft } 1166c72ffaaSAndy Whitcroft } else { 1176c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 1186c72ffaaSAndy Whitcroft $root = '.'; 1196c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 1206c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 1216c72ffaaSAndy Whitcroft $root = $1; 1226c72ffaaSAndy Whitcroft } 1236c72ffaaSAndy Whitcroft } 1246c72ffaaSAndy Whitcroft 1256c72ffaaSAndy Whitcroft if (!defined $root) { 1260a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 1270a920b5bSAndy Whitcroft exit(2); 1280a920b5bSAndy Whitcroft } 1296c72ffaaSAndy Whitcroft} 1306c72ffaaSAndy Whitcroft 1316c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 1326c72ffaaSAndy Whitcroft 133*2ceb532bSAndy Whitcroftour $Ident = qr{ 134*2ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 135*2ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 136*2ceb532bSAndy Whitcroft }x; 1376c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 1386c72ffaaSAndy Whitcroftour $Sparse = qr{ 1396c72ffaaSAndy Whitcroft __user| 1406c72ffaaSAndy Whitcroft __kernel| 1416c72ffaaSAndy Whitcroft __force| 1426c72ffaaSAndy Whitcroft __iomem| 1436c72ffaaSAndy Whitcroft __must_check| 1446c72ffaaSAndy Whitcroft __init_refok| 145417495edSAndy Whitcroft __kprobes| 146417495edSAndy Whitcroft __ref 1476c72ffaaSAndy Whitcroft }x; 1486c72ffaaSAndy Whitcroftour $Attribute = qr{ 1496c72ffaaSAndy Whitcroft const| 1506c72ffaaSAndy Whitcroft __read_mostly| 1516c72ffaaSAndy Whitcroft __kprobes| 15224e1d81aSAndy Whitcroft __(?:mem|cpu|dev|)(?:initdata|init)| 15324e1d81aSAndy Whitcroft ____cacheline_aligned| 15424e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 1555fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 1565fe3af11SAndy Whitcroft __weak 1576c72ffaaSAndy Whitcroft }x; 158c45dcabdSAndy Whitcroftour $Modifier; 1596c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 1606c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 1616c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 1626c72ffaaSAndy Whitcroft 1636c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 1646c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 16586f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 1666c72ffaaSAndy Whitcroftour $Operators = qr{ 1676c72ffaaSAndy Whitcroft <=|>=|==|!=| 1686c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 169c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1706c72ffaaSAndy Whitcroft }x; 1716c72ffaaSAndy Whitcroft 1728905a67cSAndy Whitcroftour $NonptrType; 1738905a67cSAndy Whitcroftour $Type; 1748905a67cSAndy Whitcroftour $Declare; 1758905a67cSAndy Whitcroft 176171ae1a4SAndy Whitcroftour $UTF8 = qr { 177171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 178171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 179171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 180171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 181171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 182171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 183171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 184171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 185171ae1a4SAndy Whitcroft}x; 186171ae1a4SAndy Whitcroft 1878ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 188fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 1898ed22cadSAndy Whitcroft atomic_t 1908ed22cadSAndy Whitcroft)}; 1918ed22cadSAndy Whitcroft 1928905a67cSAndy Whitcroftour @typeList = ( 1938905a67cSAndy Whitcroft qr{void}, 194c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 195c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 196c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 197c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 198c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 199c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 200c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 2018905a67cSAndy Whitcroft qr{unsigned}, 2028905a67cSAndy Whitcroft qr{float}, 2038905a67cSAndy Whitcroft qr{double}, 2048905a67cSAndy Whitcroft qr{bool}, 2058905a67cSAndy Whitcroft qr{struct\s+$Ident}, 2068905a67cSAndy Whitcroft qr{union\s+$Ident}, 2078905a67cSAndy Whitcroft qr{enum\s+$Ident}, 2088905a67cSAndy Whitcroft qr{${Ident}_t}, 2098905a67cSAndy Whitcroft qr{${Ident}_handler}, 2108905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 2118905a67cSAndy Whitcroft); 212c45dcabdSAndy Whitcroftour @modifierList = ( 213c45dcabdSAndy Whitcroft qr{fastcall}, 214c45dcabdSAndy Whitcroft); 2158905a67cSAndy Whitcroft 2168905a67cSAndy Whitcroftsub build_types { 217d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 218d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 219c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 2208905a67cSAndy Whitcroft $NonptrType = qr{ 221d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 222cf655043SAndy Whitcroft (?: 223c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 2248ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 225c45dcabdSAndy Whitcroft (?:${all}\b) 226cf655043SAndy Whitcroft ) 227c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 2288905a67cSAndy Whitcroft }x; 2298905a67cSAndy Whitcroft $Type = qr{ 230c45dcabdSAndy Whitcroft $NonptrType 23165863862SAndy Whitcroft (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 232c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 2338905a67cSAndy Whitcroft }x; 2348905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 2358905a67cSAndy Whitcroft} 2368905a67cSAndy Whitcroftbuild_types(); 2376c72ffaaSAndy Whitcroft 2386c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 2390a920b5bSAndy Whitcroft 2404a0df2efSAndy Whitcroftmy @dep_includes = (); 2414a0df2efSAndy Whitcroftmy @dep_functions = (); 2426c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 2436c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 24421caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 2456c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 24621caa13cSAndy Whitcroft while (<$REMOVE>) { 247f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 248f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 249f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 2504a0df2efSAndy Whitcroft push(@dep_includes, $1); 2514a0df2efSAndy Whitcroft 252f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 253f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 254f0a594c1SAndy Whitcroft } 2554a0df2efSAndy Whitcroft } 2560a920b5bSAndy Whitcroft } 2570a920b5bSAndy Whitcroft } 25821caa13cSAndy Whitcroft close($REMOVE); 2590a920b5bSAndy Whitcroft} 2600a920b5bSAndy Whitcroft 26100df344fSAndy Whitcroftmy @rawlines = (); 262c2fdda0dSAndy Whitcroftmy @lines = (); 263c2fdda0dSAndy Whitcroftmy $vname; 2646c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 26521caa13cSAndy Whitcroft my $FILE; 2666c72ffaaSAndy Whitcroft if ($file) { 26721caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 2686c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 26921caa13cSAndy Whitcroft } elsif ($filename eq '-') { 27021caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 2716c72ffaaSAndy Whitcroft } else { 27221caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 2736c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 2746c72ffaaSAndy Whitcroft } 275c2fdda0dSAndy Whitcroft if ($filename eq '-') { 276c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 277c2fdda0dSAndy Whitcroft } else { 278c2fdda0dSAndy Whitcroft $vname = $filename; 279c2fdda0dSAndy Whitcroft } 28021caa13cSAndy Whitcroft while (<$FILE>) { 2810a920b5bSAndy Whitcroft chomp; 28200df344fSAndy Whitcroft push(@rawlines, $_); 2836c72ffaaSAndy Whitcroft } 28421caa13cSAndy Whitcroft close($FILE); 285c2fdda0dSAndy Whitcroft if (!process($filename)) { 2860a920b5bSAndy Whitcroft $exit = 1; 2870a920b5bSAndy Whitcroft } 28800df344fSAndy Whitcroft @rawlines = (); 28913214adfSAndy Whitcroft @lines = (); 2900a920b5bSAndy Whitcroft} 2910a920b5bSAndy Whitcroft 2920a920b5bSAndy Whitcroftexit($exit); 2930a920b5bSAndy Whitcroft 2940a920b5bSAndy Whitcroftsub top_of_kernel_tree { 2956c72ffaaSAndy Whitcroft my ($root) = @_; 2966c72ffaaSAndy Whitcroft 2976c72ffaaSAndy Whitcroft my @tree_check = ( 2986c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 2996c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 3006c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 3016c72ffaaSAndy Whitcroft ); 3026c72ffaaSAndy Whitcroft 3036c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 3046c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 3050a920b5bSAndy Whitcroft return 0; 3060a920b5bSAndy Whitcroft } 3076c72ffaaSAndy Whitcroft } 3086c72ffaaSAndy Whitcroft return 1; 3096c72ffaaSAndy Whitcroft} 3100a920b5bSAndy Whitcroft 3110a920b5bSAndy Whitcroftsub expand_tabs { 3120a920b5bSAndy Whitcroft my ($str) = @_; 3130a920b5bSAndy Whitcroft 3140a920b5bSAndy Whitcroft my $res = ''; 3150a920b5bSAndy Whitcroft my $n = 0; 3160a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 3170a920b5bSAndy Whitcroft if ($c eq "\t") { 3180a920b5bSAndy Whitcroft $res .= ' '; 3190a920b5bSAndy Whitcroft $n++; 3200a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 3210a920b5bSAndy Whitcroft $res .= ' '; 3220a920b5bSAndy Whitcroft } 3230a920b5bSAndy Whitcroft next; 3240a920b5bSAndy Whitcroft } 3250a920b5bSAndy Whitcroft $res .= $c; 3260a920b5bSAndy Whitcroft $n++; 3270a920b5bSAndy Whitcroft } 3280a920b5bSAndy Whitcroft 3290a920b5bSAndy Whitcroft return $res; 3300a920b5bSAndy Whitcroft} 3316c72ffaaSAndy Whitcroftsub copy_spacing { 332773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 3336c72ffaaSAndy Whitcroft return $res; 3346c72ffaaSAndy Whitcroft} 3350a920b5bSAndy Whitcroft 3364a0df2efSAndy Whitcroftsub line_stats { 3374a0df2efSAndy Whitcroft my ($line) = @_; 3384a0df2efSAndy Whitcroft 3394a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 3404a0df2efSAndy Whitcroft $line =~ s/^.//; 3414a0df2efSAndy Whitcroft $line = expand_tabs($line); 3424a0df2efSAndy Whitcroft 3434a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 3444a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 3454a0df2efSAndy Whitcroft 3464a0df2efSAndy Whitcroft return (length($line), length($white)); 3474a0df2efSAndy Whitcroft} 3484a0df2efSAndy Whitcroft 349773647a0SAndy Whitcroftmy $sanitise_quote = ''; 350773647a0SAndy Whitcroft 351773647a0SAndy Whitcroftsub sanitise_line_reset { 352773647a0SAndy Whitcroft my ($in_comment) = @_; 353773647a0SAndy Whitcroft 354773647a0SAndy Whitcroft if ($in_comment) { 355773647a0SAndy Whitcroft $sanitise_quote = '*/'; 356773647a0SAndy Whitcroft } else { 357773647a0SAndy Whitcroft $sanitise_quote = ''; 358773647a0SAndy Whitcroft } 359773647a0SAndy Whitcroft} 36000df344fSAndy Whitcroftsub sanitise_line { 36100df344fSAndy Whitcroft my ($line) = @_; 36200df344fSAndy Whitcroft 36300df344fSAndy Whitcroft my $res = ''; 36400df344fSAndy Whitcroft my $l = ''; 36500df344fSAndy Whitcroft 366c2fdda0dSAndy Whitcroft my $qlen = 0; 367773647a0SAndy Whitcroft my $off = 0; 368773647a0SAndy Whitcroft my $c; 36900df344fSAndy Whitcroft 370773647a0SAndy Whitcroft # Always copy over the diff marker. 371773647a0SAndy Whitcroft $res = substr($line, 0, 1); 372773647a0SAndy Whitcroft 373773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 374773647a0SAndy Whitcroft $c = substr($line, $off, 1); 375773647a0SAndy Whitcroft 376773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 377773647a0SAndy Whitcroft # and end, all to $;. 378773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 379773647a0SAndy Whitcroft $sanitise_quote = '*/'; 380773647a0SAndy Whitcroft 381773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 382773647a0SAndy Whitcroft $off++; 38300df344fSAndy Whitcroft next; 384773647a0SAndy Whitcroft } 38581bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 386773647a0SAndy Whitcroft $sanitise_quote = ''; 387773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 388773647a0SAndy Whitcroft $off++; 389773647a0SAndy Whitcroft next; 390773647a0SAndy Whitcroft } 391113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 392113f04a8SDaniel Walker $sanitise_quote = '//'; 393113f04a8SDaniel Walker 394113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 395113f04a8SDaniel Walker $off++; 396113f04a8SDaniel Walker next; 397113f04a8SDaniel Walker } 398773647a0SAndy Whitcroft 399773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 400773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 401773647a0SAndy Whitcroft $c eq "\\") { 402773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 403773647a0SAndy Whitcroft $off++; 404773647a0SAndy Whitcroft next; 405773647a0SAndy Whitcroft } 406773647a0SAndy Whitcroft # Regular quotes. 407773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 408773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 409773647a0SAndy Whitcroft $sanitise_quote = $c; 410773647a0SAndy Whitcroft 411773647a0SAndy Whitcroft substr($res, $off, 1, $c); 412773647a0SAndy Whitcroft next; 413773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 414773647a0SAndy Whitcroft $sanitise_quote = ''; 41500df344fSAndy Whitcroft } 41600df344fSAndy Whitcroft } 417773647a0SAndy Whitcroft 418fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 419773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 420773647a0SAndy Whitcroft substr($res, $off, 1, $;); 421113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 422113f04a8SDaniel Walker substr($res, $off, 1, $;); 423773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 424773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 42500df344fSAndy Whitcroft } else { 426773647a0SAndy Whitcroft substr($res, $off, 1, $c); 42700df344fSAndy Whitcroft } 428c2fdda0dSAndy Whitcroft } 429c2fdda0dSAndy Whitcroft 430113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 431113f04a8SDaniel Walker $sanitise_quote = ''; 432113f04a8SDaniel Walker } 433113f04a8SDaniel Walker 434c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 435c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 436c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 437c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 438c2fdda0dSAndy Whitcroft 439c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 440c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 441c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 442c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 443c2fdda0dSAndy Whitcroft } 444c2fdda0dSAndy Whitcroft 44500df344fSAndy Whitcroft return $res; 44600df344fSAndy Whitcroft} 44700df344fSAndy Whitcroft 4488905a67cSAndy Whitcroftsub ctx_statement_block { 4498905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 4508905a67cSAndy Whitcroft my $line = $linenr - 1; 4518905a67cSAndy Whitcroft my $blk = ''; 4528905a67cSAndy Whitcroft my $soff = $off; 4538905a67cSAndy Whitcroft my $coff = $off - 1; 454773647a0SAndy Whitcroft my $coff_set = 0; 4558905a67cSAndy Whitcroft 45613214adfSAndy Whitcroft my $loff = 0; 45713214adfSAndy Whitcroft 4588905a67cSAndy Whitcroft my $type = ''; 4598905a67cSAndy Whitcroft my $level = 0; 460a2750645SAndy Whitcroft my @stack = (); 461cf655043SAndy Whitcroft my $p; 4628905a67cSAndy Whitcroft my $c; 4638905a67cSAndy Whitcroft my $len = 0; 46413214adfSAndy Whitcroft 46513214adfSAndy Whitcroft my $remainder; 4668905a67cSAndy Whitcroft while (1) { 467a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 468a2750645SAndy Whitcroft 469773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 4708905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 4718905a67cSAndy Whitcroft # context. 4728905a67cSAndy Whitcroft if ($off >= $len) { 4738905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 474dea33496SAndy Whitcroft last if (!defined $lines[$line]); 475c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 4768905a67cSAndy Whitcroft $remain--; 47713214adfSAndy Whitcroft $loff = $len; 478c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 4798905a67cSAndy Whitcroft $len = length($blk); 4808905a67cSAndy Whitcroft $line++; 4818905a67cSAndy Whitcroft last; 4828905a67cSAndy Whitcroft } 4838905a67cSAndy Whitcroft # Bail if there is no further context. 4848905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 48513214adfSAndy Whitcroft if ($off >= $len) { 4868905a67cSAndy Whitcroft last; 4878905a67cSAndy Whitcroft } 4888905a67cSAndy Whitcroft } 489cf655043SAndy Whitcroft $p = $c; 4908905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 49113214adfSAndy Whitcroft $remainder = substr($blk, $off); 4928905a67cSAndy Whitcroft 493773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 4944635f4fbSAndy Whitcroft 4954635f4fbSAndy Whitcroft # Handle nested #if/#else. 4964635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 4974635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 4984635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 4994635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 5004635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 5014635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 5024635f4fbSAndy Whitcroft } 5034635f4fbSAndy Whitcroft 5048905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 5058905a67cSAndy Whitcroft # outermost level. 5068905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 5078905a67cSAndy Whitcroft last; 5088905a67cSAndy Whitcroft } 5098905a67cSAndy Whitcroft 51013214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 511773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 512773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 513773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 514773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 515773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 516773647a0SAndy Whitcroft $coff_set = 1; 517773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 518773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 51913214adfSAndy Whitcroft } 52013214adfSAndy Whitcroft 5218905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 5228905a67cSAndy Whitcroft $level++; 5238905a67cSAndy Whitcroft $type = '('; 5248905a67cSAndy Whitcroft } 5258905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 5268905a67cSAndy Whitcroft $level--; 5278905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 5288905a67cSAndy Whitcroft 5298905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 5308905a67cSAndy Whitcroft $coff = $off; 531773647a0SAndy Whitcroft $coff_set = 1; 532773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 5338905a67cSAndy Whitcroft } 5348905a67cSAndy Whitcroft } 5358905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 5368905a67cSAndy Whitcroft $level++; 5378905a67cSAndy Whitcroft $type = '{'; 5388905a67cSAndy Whitcroft } 5398905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 5408905a67cSAndy Whitcroft $level--; 5418905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 5428905a67cSAndy Whitcroft 5438905a67cSAndy Whitcroft if ($level == 0) { 5448905a67cSAndy Whitcroft last; 5458905a67cSAndy Whitcroft } 5468905a67cSAndy Whitcroft } 5478905a67cSAndy Whitcroft $off++; 5488905a67cSAndy Whitcroft } 549a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 55013214adfSAndy Whitcroft if ($off == $len) { 551a3bb97a7SAndy Whitcroft $loff = $len + 1; 55213214adfSAndy Whitcroft $line++; 55313214adfSAndy Whitcroft $remain--; 55413214adfSAndy Whitcroft } 5558905a67cSAndy Whitcroft 5568905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 5578905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 5588905a67cSAndy Whitcroft 5598905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 5608905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 5618905a67cSAndy Whitcroft 562773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 56313214adfSAndy Whitcroft 56413214adfSAndy Whitcroft return ($statement, $condition, 56513214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 56613214adfSAndy Whitcroft} 56713214adfSAndy Whitcroft 568cf655043SAndy Whitcroftsub statement_lines { 569cf655043SAndy Whitcroft my ($stmt) = @_; 570cf655043SAndy Whitcroft 571cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 572cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 573cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 574cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 575cf655043SAndy Whitcroft 576cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 577cf655043SAndy Whitcroft 578cf655043SAndy Whitcroft return $#stmt_lines + 2; 579cf655043SAndy Whitcroft} 580cf655043SAndy Whitcroft 581cf655043SAndy Whitcroftsub statement_rawlines { 582cf655043SAndy Whitcroft my ($stmt) = @_; 583cf655043SAndy Whitcroft 584cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 585cf655043SAndy Whitcroft 586cf655043SAndy Whitcroft return $#stmt_lines + 2; 587cf655043SAndy Whitcroft} 588cf655043SAndy Whitcroft 589cf655043SAndy Whitcroftsub statement_block_size { 590cf655043SAndy Whitcroft my ($stmt) = @_; 591cf655043SAndy Whitcroft 592cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 593cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 594cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 595cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 596cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 597cf655043SAndy Whitcroft 598cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 599cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 600cf655043SAndy Whitcroft 601cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 602cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 603cf655043SAndy Whitcroft 604cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 605cf655043SAndy Whitcroft return $stmt_lines; 606cf655043SAndy Whitcroft } else { 607cf655043SAndy Whitcroft return $stmt_statements; 608cf655043SAndy Whitcroft } 609cf655043SAndy Whitcroft} 610cf655043SAndy Whitcroft 61113214adfSAndy Whitcroftsub ctx_statement_full { 61213214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 61313214adfSAndy Whitcroft my ($statement, $condition, $level); 61413214adfSAndy Whitcroft 61513214adfSAndy Whitcroft my (@chunks); 61613214adfSAndy Whitcroft 617cf655043SAndy Whitcroft # Grab the first conditional/block pair. 61813214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 61913214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 620773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 62113214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 622cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 623cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 624cf655043SAndy Whitcroft } 625cf655043SAndy Whitcroft 626cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 627cf655043SAndy Whitcroft # could continue the statement. 628cf655043SAndy Whitcroft for (;;) { 62913214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 63013214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 631cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 632773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 633cf655043SAndy Whitcroft #print "C: push\n"; 634cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 63513214adfSAndy Whitcroft } 63613214adfSAndy Whitcroft 63713214adfSAndy Whitcroft return ($level, $linenr, @chunks); 6388905a67cSAndy Whitcroft} 6398905a67cSAndy Whitcroft 6404a0df2efSAndy Whitcroftsub ctx_block_get { 641f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 6424a0df2efSAndy Whitcroft my $line; 6434a0df2efSAndy Whitcroft my $start = $linenr - 1; 6444a0df2efSAndy Whitcroft my $blk = ''; 6454a0df2efSAndy Whitcroft my @o; 6464a0df2efSAndy Whitcroft my @c; 6474a0df2efSAndy Whitcroft my @res = (); 6484a0df2efSAndy Whitcroft 649f0a594c1SAndy Whitcroft my $level = 0; 6504635f4fbSAndy Whitcroft my @stack = ($level); 65100df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 65200df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 65300df344fSAndy Whitcroft $remain--; 65400df344fSAndy Whitcroft 65500df344fSAndy Whitcroft $blk .= $rawlines[$line]; 6564635f4fbSAndy Whitcroft 6574635f4fbSAndy Whitcroft # Handle nested #if/#else. 6584635f4fbSAndy Whitcroft if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 6594635f4fbSAndy Whitcroft push(@stack, $level); 6604635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 6614635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 6624635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { 6634635f4fbSAndy Whitcroft $level = pop(@stack); 6644635f4fbSAndy Whitcroft } 6654635f4fbSAndy Whitcroft 666f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$line])) { 667f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 668f0a594c1SAndy Whitcroft if ($off > 0) { 669f0a594c1SAndy Whitcroft $off--; 670f0a594c1SAndy Whitcroft next; 671f0a594c1SAndy Whitcroft } 6724a0df2efSAndy Whitcroft 673f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 674f0a594c1SAndy Whitcroft $level--; 675f0a594c1SAndy Whitcroft last if ($level == 0); 676f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 677f0a594c1SAndy Whitcroft $level++; 678f0a594c1SAndy Whitcroft } 679f0a594c1SAndy Whitcroft } 6804a0df2efSAndy Whitcroft 681f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 68200df344fSAndy Whitcroft push(@res, $rawlines[$line]); 6834a0df2efSAndy Whitcroft } 6844a0df2efSAndy Whitcroft 685f0a594c1SAndy Whitcroft last if ($level == 0); 6864a0df2efSAndy Whitcroft } 6874a0df2efSAndy Whitcroft 688f0a594c1SAndy Whitcroft return ($level, @res); 6894a0df2efSAndy Whitcroft} 6904a0df2efSAndy Whitcroftsub ctx_block_outer { 6914a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6924a0df2efSAndy Whitcroft 693f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 694f0a594c1SAndy Whitcroft return @r; 6954a0df2efSAndy Whitcroft} 6964a0df2efSAndy Whitcroftsub ctx_block { 6974a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 6984a0df2efSAndy Whitcroft 699f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 700f0a594c1SAndy Whitcroft return @r; 701653d4876SAndy Whitcroft} 702653d4876SAndy Whitcroftsub ctx_statement { 703f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 704f0a594c1SAndy Whitcroft 705f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 706f0a594c1SAndy Whitcroft return @r; 707f0a594c1SAndy Whitcroft} 708f0a594c1SAndy Whitcroftsub ctx_block_level { 709653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 710653d4876SAndy Whitcroft 711f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 7124a0df2efSAndy Whitcroft} 7139c0ca6f9SAndy Whitcroftsub ctx_statement_level { 7149c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 7159c0ca6f9SAndy Whitcroft 7169c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 7179c0ca6f9SAndy Whitcroft} 7184a0df2efSAndy Whitcroft 7194a0df2efSAndy Whitcroftsub ctx_locate_comment { 7204a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7214a0df2efSAndy Whitcroft 7224a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 723beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 7244a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 7254a0df2efSAndy Whitcroft 7264a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 7274a0df2efSAndy Whitcroft # comment. 7284a0df2efSAndy Whitcroft my $in_comment = 0; 7294a0df2efSAndy Whitcroft $current_comment = ''; 7304a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 73100df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 73200df344fSAndy Whitcroft #warn " $line\n"; 7334a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 7344a0df2efSAndy Whitcroft $in_comment = 1; 7354a0df2efSAndy Whitcroft } 7364a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 7374a0df2efSAndy Whitcroft $in_comment = 1; 7384a0df2efSAndy Whitcroft } 7394a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 7404a0df2efSAndy Whitcroft $current_comment = ''; 7414a0df2efSAndy Whitcroft } 7424a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 7434a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 7444a0df2efSAndy Whitcroft $in_comment = 0; 7454a0df2efSAndy Whitcroft } 7464a0df2efSAndy Whitcroft } 7474a0df2efSAndy Whitcroft 7484a0df2efSAndy Whitcroft chomp($current_comment); 7494a0df2efSAndy Whitcroft return($current_comment); 7504a0df2efSAndy Whitcroft} 7514a0df2efSAndy Whitcroftsub ctx_has_comment { 7524a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7534a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 7544a0df2efSAndy Whitcroft 75500df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 7564a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 7574a0df2efSAndy Whitcroft 7584a0df2efSAndy Whitcroft return ($cmt ne ''); 7594a0df2efSAndy Whitcroft} 7604a0df2efSAndy Whitcroft 7614d001e4dSAndy Whitcroftsub raw_line { 7624d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 7634d001e4dSAndy Whitcroft 7644d001e4dSAndy Whitcroft my $offset = $linenr - 1; 7654d001e4dSAndy Whitcroft $cnt++; 7664d001e4dSAndy Whitcroft 7674d001e4dSAndy Whitcroft my $line; 7684d001e4dSAndy Whitcroft while ($cnt) { 7694d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 7704d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 7714d001e4dSAndy Whitcroft $cnt--; 7724d001e4dSAndy Whitcroft } 7734d001e4dSAndy Whitcroft 7744d001e4dSAndy Whitcroft return $line; 7754d001e4dSAndy Whitcroft} 7764d001e4dSAndy Whitcroft 7770a920b5bSAndy Whitcroftsub cat_vet { 7780a920b5bSAndy Whitcroft my ($vet) = @_; 7799c0ca6f9SAndy Whitcroft my ($res, $coded); 7800a920b5bSAndy Whitcroft 7819c0ca6f9SAndy Whitcroft $res = ''; 7826c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 7836c72ffaaSAndy Whitcroft $res .= $1; 7846c72ffaaSAndy Whitcroft if ($2 ne '') { 7859c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 7866c72ffaaSAndy Whitcroft $res .= $coded; 7876c72ffaaSAndy Whitcroft } 7889c0ca6f9SAndy Whitcroft } 7899c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 7900a920b5bSAndy Whitcroft 7919c0ca6f9SAndy Whitcroft return $res; 7920a920b5bSAndy Whitcroft} 7930a920b5bSAndy Whitcroft 794c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 795cf655043SAndy Whitcroftmy $av_pending; 796c2fdda0dSAndy Whitcroftmy @av_paren_type; 7971f65f947SAndy Whitcroftmy $av_pend_colon; 798c2fdda0dSAndy Whitcroft 799c2fdda0dSAndy Whitcroftsub annotate_reset { 800c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 801cf655043SAndy Whitcroft $av_pending = '_'; 802cf655043SAndy Whitcroft @av_paren_type = ('E'); 8031f65f947SAndy Whitcroft $av_pend_colon = 'O'; 804c2fdda0dSAndy Whitcroft} 805c2fdda0dSAndy Whitcroft 8066c72ffaaSAndy Whitcroftsub annotate_values { 8076c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 8086c72ffaaSAndy Whitcroft 8096c72ffaaSAndy Whitcroft my $res; 8101f65f947SAndy Whitcroft my $var = '_' x length($stream); 8116c72ffaaSAndy Whitcroft my $cur = $stream; 8126c72ffaaSAndy Whitcroft 813c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 8146c72ffaaSAndy Whitcroft 8156c72ffaaSAndy Whitcroft while (length($cur)) { 816773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 817cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 818171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 8196c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 820c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 821c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 822cf655043SAndy Whitcroft $type = pop(@av_paren_type); 823c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 8246c72ffaaSAndy Whitcroft } 8256c72ffaaSAndy Whitcroft 8268ea3eb9aSAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { 827c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 8286c72ffaaSAndy Whitcroft $type = 'T'; 8296c72ffaaSAndy Whitcroft 830389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 831389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 832389a2fe5SAndy Whitcroft $type = 'T'; 833389a2fe5SAndy Whitcroft 834c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 835171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 836c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 837171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 838171ae1a4SAndy Whitcroft if ($2 ne '') { 839cf655043SAndy Whitcroft $av_pending = 'N'; 840171ae1a4SAndy Whitcroft } 841171ae1a4SAndy Whitcroft $type = 'E'; 842171ae1a4SAndy Whitcroft 843c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 844171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 845171ae1a4SAndy Whitcroft $av_preprocessor = 1; 846171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 8476c72ffaaSAndy Whitcroft 848c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 849cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 850c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 851cf655043SAndy Whitcroft 852cf655043SAndy Whitcroft push(@av_paren_type, $type); 853cf655043SAndy Whitcroft push(@av_paren_type, $type); 854171ae1a4SAndy Whitcroft $type = 'E'; 855cf655043SAndy Whitcroft 856c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 857cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 858cf655043SAndy Whitcroft $av_preprocessor = 1; 859cf655043SAndy Whitcroft 860cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 861cf655043SAndy Whitcroft 862171ae1a4SAndy Whitcroft $type = 'E'; 863cf655043SAndy Whitcroft 864c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 865cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 866cf655043SAndy Whitcroft 867cf655043SAndy Whitcroft $av_preprocessor = 1; 868cf655043SAndy Whitcroft 869cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 870cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 871cf655043SAndy Whitcroft pop(@av_paren_type); 872cf655043SAndy Whitcroft push(@av_paren_type, $type); 873171ae1a4SAndy Whitcroft $type = 'E'; 8746c72ffaaSAndy Whitcroft 8756c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 876c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 8776c72ffaaSAndy Whitcroft 878171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 879171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 880171ae1a4SAndy Whitcroft $av_pending = $type; 881171ae1a4SAndy Whitcroft $type = 'N'; 882171ae1a4SAndy Whitcroft 8836c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 884c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 8856c72ffaaSAndy Whitcroft if (defined $2) { 886cf655043SAndy Whitcroft $av_pending = 'V'; 8876c72ffaaSAndy Whitcroft } 8886c72ffaaSAndy Whitcroft $type = 'N'; 8896c72ffaaSAndy Whitcroft 89014b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 891c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 89214b111c1SAndy Whitcroft $av_pending = 'E'; 8936c72ffaaSAndy Whitcroft $type = 'N'; 8946c72ffaaSAndy Whitcroft 8951f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 8961f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 8971f65f947SAndy Whitcroft $av_pend_colon = 'C'; 8981f65f947SAndy Whitcroft $type = 'N'; 8991f65f947SAndy Whitcroft 90014b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 901c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 9026c72ffaaSAndy Whitcroft $type = 'N'; 9036c72ffaaSAndy Whitcroft 9046c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 905c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 906cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 907cf655043SAndy Whitcroft $av_pending = '_'; 9086c72ffaaSAndy Whitcroft $type = 'N'; 9096c72ffaaSAndy Whitcroft 9106c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 911cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 912cf655043SAndy Whitcroft if ($new_type ne '_') { 913cf655043SAndy Whitcroft $type = $new_type; 914c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 915c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 9166c72ffaaSAndy Whitcroft } else { 917c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 9186c72ffaaSAndy Whitcroft } 9196c72ffaaSAndy Whitcroft 920c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 921c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 922c8cb2ca3SAndy Whitcroft $type = 'V'; 923cf655043SAndy Whitcroft $av_pending = 'V'; 9246c72ffaaSAndy Whitcroft 9258e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 9268e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 9271f65f947SAndy Whitcroft $av_pend_colon = 'B'; 9288e761b04SAndy Whitcroft } elsif ($type eq 'E') { 9298e761b04SAndy Whitcroft $av_pend_colon = 'L'; 9301f65f947SAndy Whitcroft } 9311f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 9321f65f947SAndy Whitcroft $type = 'V'; 9331f65f947SAndy Whitcroft 9346c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 935c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 9366c72ffaaSAndy Whitcroft $type = 'V'; 9376c72ffaaSAndy Whitcroft 9386c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 939c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 9406c72ffaaSAndy Whitcroft $type = 'N'; 9416c72ffaaSAndy Whitcroft 942cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 943c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 94413214adfSAndy Whitcroft $type = 'E'; 9451f65f947SAndy Whitcroft $av_pend_colon = 'O'; 94613214adfSAndy Whitcroft 9478e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 9488e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 9498e761b04SAndy Whitcroft $type = 'C'; 9508e761b04SAndy Whitcroft 9511f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 9521f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 9531f65f947SAndy Whitcroft $type = 'N'; 9541f65f947SAndy Whitcroft 9551f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 9561f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 9571f65f947SAndy Whitcroft 9581f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 9591f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 9601f65f947SAndy Whitcroft $type = 'E'; 9611f65f947SAndy Whitcroft } else { 9621f65f947SAndy Whitcroft $type = 'N'; 9631f65f947SAndy Whitcroft } 9641f65f947SAndy Whitcroft $av_pend_colon = 'O'; 9651f65f947SAndy Whitcroft 9668e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 96713214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 9686c72ffaaSAndy Whitcroft $type = 'N'; 9696c72ffaaSAndy Whitcroft 9700d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 97174048ed8SAndy Whitcroft my $variant; 97274048ed8SAndy Whitcroft 97374048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 97474048ed8SAndy Whitcroft if ($type eq 'V') { 97574048ed8SAndy Whitcroft $variant = 'B'; 97674048ed8SAndy Whitcroft } else { 97774048ed8SAndy Whitcroft $variant = 'U'; 97874048ed8SAndy Whitcroft } 97974048ed8SAndy Whitcroft 98074048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 98174048ed8SAndy Whitcroft $type = 'N'; 98274048ed8SAndy Whitcroft 9836c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 984c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 9856c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 9866c72ffaaSAndy Whitcroft $type = 'N'; 9876c72ffaaSAndy Whitcroft } 9886c72ffaaSAndy Whitcroft 9896c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 990c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 9916c72ffaaSAndy Whitcroft } 9926c72ffaaSAndy Whitcroft if (defined $1) { 9936c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 9946c72ffaaSAndy Whitcroft $res .= $type x length($1); 9956c72ffaaSAndy Whitcroft } 9966c72ffaaSAndy Whitcroft } 9976c72ffaaSAndy Whitcroft 9981f65f947SAndy Whitcroft return ($res, $var); 9996c72ffaaSAndy Whitcroft} 10006c72ffaaSAndy Whitcroft 10018905a67cSAndy Whitcroftsub possible { 100213214adfSAndy Whitcroft my ($possible, $line) = @_; 10039a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 10040776e594SAndy Whitcroft ^(?: 10050776e594SAndy Whitcroft $Modifier| 10060776e594SAndy Whitcroft $Storage| 10070776e594SAndy Whitcroft $Type| 10089a974fdbSAndy Whitcroft DEFINE_\S+ 10099a974fdbSAndy Whitcroft )$| 10109a974fdbSAndy Whitcroft ^(?: 10110776e594SAndy Whitcroft goto| 10120776e594SAndy Whitcroft return| 10130776e594SAndy Whitcroft case| 10140776e594SAndy Whitcroft else| 10150776e594SAndy Whitcroft asm|__asm__| 10160776e594SAndy Whitcroft do 10179a974fdbSAndy Whitcroft )(?:\s|$)| 10180776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 10199a974fdbSAndy Whitcroft )}x; 10209a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 10219a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1022c45dcabdSAndy Whitcroft # Check for modifiers. 1023c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1024c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1025c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1026c45dcabdSAndy Whitcroft 1027c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1028c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1029d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 10309a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1031d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1032d2506586SAndy Whitcroft push(@modifierList, $modifier); 1033d2506586SAndy Whitcroft } 10349a974fdbSAndy Whitcroft } 1035c45dcabdSAndy Whitcroft 1036c45dcabdSAndy Whitcroft } else { 103713214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 10388905a67cSAndy Whitcroft push(@typeList, $possible); 1039c45dcabdSAndy Whitcroft } 10408905a67cSAndy Whitcroft build_types(); 10410776e594SAndy Whitcroft } else { 10420776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 10438905a67cSAndy Whitcroft } 10448905a67cSAndy Whitcroft} 10458905a67cSAndy Whitcroft 10466c72ffaaSAndy Whitcroftmy $prefix = ''; 10476c72ffaaSAndy Whitcroft 1048f0a594c1SAndy Whitcroftsub report { 1049773647a0SAndy Whitcroft if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 1050773647a0SAndy Whitcroft return 0; 1051773647a0SAndy Whitcroft } 10528905a67cSAndy Whitcroft my $line = $prefix . $_[0]; 10538905a67cSAndy Whitcroft 10548905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 10558905a67cSAndy Whitcroft 105613214adfSAndy Whitcroft push(our @report, $line); 1057773647a0SAndy Whitcroft 1058773647a0SAndy Whitcroft return 1; 1059f0a594c1SAndy Whitcroft} 1060f0a594c1SAndy Whitcroftsub report_dump { 106113214adfSAndy Whitcroft our @report; 1062f0a594c1SAndy Whitcroft} 1063de7d4f0eSAndy Whitcroftsub ERROR { 1064773647a0SAndy Whitcroft if (report("ERROR: $_[0]\n")) { 1065de7d4f0eSAndy Whitcroft our $clean = 0; 10666c72ffaaSAndy Whitcroft our $cnt_error++; 1067de7d4f0eSAndy Whitcroft } 1068773647a0SAndy Whitcroft} 1069de7d4f0eSAndy Whitcroftsub WARN { 1070773647a0SAndy Whitcroft if (report("WARNING: $_[0]\n")) { 1071de7d4f0eSAndy Whitcroft our $clean = 0; 10726c72ffaaSAndy Whitcroft our $cnt_warn++; 1073de7d4f0eSAndy Whitcroft } 1074773647a0SAndy Whitcroft} 1075de7d4f0eSAndy Whitcroftsub CHK { 1076773647a0SAndy Whitcroft if ($check && report("CHECK: $_[0]\n")) { 1077de7d4f0eSAndy Whitcroft our $clean = 0; 10786c72ffaaSAndy Whitcroft our $cnt_chk++; 10796c72ffaaSAndy Whitcroft } 1080de7d4f0eSAndy Whitcroft} 1081de7d4f0eSAndy Whitcroft 10826ecd9674SAndy Whitcroftsub check_absolute_file { 10836ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 10846ecd9674SAndy Whitcroft my $file = $absolute; 10856ecd9674SAndy Whitcroft 10866ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 10876ecd9674SAndy Whitcroft 10886ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 10896ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 10906ecd9674SAndy Whitcroft if (-f "$root/$file") { 10916ecd9674SAndy Whitcroft ##print "file<$file>\n"; 10926ecd9674SAndy Whitcroft last; 10936ecd9674SAndy Whitcroft } 10946ecd9674SAndy Whitcroft } 10956ecd9674SAndy Whitcroft if (! -f _) { 10966ecd9674SAndy Whitcroft return 0; 10976ecd9674SAndy Whitcroft } 10986ecd9674SAndy Whitcroft 10996ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 11006ecd9674SAndy Whitcroft my $prefix = $absolute; 11016ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 11026ecd9674SAndy Whitcroft 11036ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 11046ecd9674SAndy Whitcroft if ($prefix ne ".../") { 11056ecd9674SAndy Whitcroft WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); 11066ecd9674SAndy Whitcroft } 11076ecd9674SAndy Whitcroft} 11086ecd9674SAndy Whitcroft 11090a920b5bSAndy Whitcroftsub process { 11100a920b5bSAndy Whitcroft my $filename = shift; 11110a920b5bSAndy Whitcroft 11120a920b5bSAndy Whitcroft my $linenr=0; 11130a920b5bSAndy Whitcroft my $prevline=""; 1114c2fdda0dSAndy Whitcroft my $prevrawline=""; 11150a920b5bSAndy Whitcroft my $stashline=""; 1116c2fdda0dSAndy Whitcroft my $stashrawline=""; 11170a920b5bSAndy Whitcroft 11184a0df2efSAndy Whitcroft my $length; 11190a920b5bSAndy Whitcroft my $indent; 11200a920b5bSAndy Whitcroft my $previndent=0; 11210a920b5bSAndy Whitcroft my $stashindent=0; 11220a920b5bSAndy Whitcroft 1123de7d4f0eSAndy Whitcroft our $clean = 1; 11240a920b5bSAndy Whitcroft my $signoff = 0; 11250a920b5bSAndy Whitcroft my $is_patch = 0; 11260a920b5bSAndy Whitcroft 112713214adfSAndy Whitcroft our @report = (); 11286c72ffaaSAndy Whitcroft our $cnt_lines = 0; 11296c72ffaaSAndy Whitcroft our $cnt_error = 0; 11306c72ffaaSAndy Whitcroft our $cnt_warn = 0; 11316c72ffaaSAndy Whitcroft our $cnt_chk = 0; 11326c72ffaaSAndy Whitcroft 11330a920b5bSAndy Whitcroft # Trace the real file/line as we go. 11340a920b5bSAndy Whitcroft my $realfile = ''; 11350a920b5bSAndy Whitcroft my $realline = 0; 11360a920b5bSAndy Whitcroft my $realcnt = 0; 11370a920b5bSAndy Whitcroft my $here = ''; 11380a920b5bSAndy Whitcroft my $in_comment = 0; 1139c2fdda0dSAndy Whitcroft my $comment_edge = 0; 11400a920b5bSAndy Whitcroft my $first_line = 0; 11411e855726SWolfram Sang my $p1_prefix = ''; 11420a920b5bSAndy Whitcroft 114313214adfSAndy Whitcroft my $prev_values = 'E'; 114413214adfSAndy Whitcroft 114513214adfSAndy Whitcroft # suppression flags 1146773647a0SAndy Whitcroft my %suppress_ifbraces; 1147170d3a22SAndy Whitcroft my %suppress_whiletrailers; 1148653d4876SAndy Whitcroft 1149c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1150de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1151c2fdda0dSAndy Whitcroft # 1152de7d4f0eSAndy Whitcroft my @setup_docs = (); 1153de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1154773647a0SAndy Whitcroft 1155773647a0SAndy Whitcroft sanitise_line_reset(); 1156c2fdda0dSAndy Whitcroft my $line; 1157c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1158773647a0SAndy Whitcroft $linenr++; 1159773647a0SAndy Whitcroft $line = $rawline; 1160c2fdda0dSAndy Whitcroft 1161773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1162de7d4f0eSAndy Whitcroft $setup_docs = 0; 1163de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1164de7d4f0eSAndy Whitcroft $setup_docs = 1; 1165de7d4f0eSAndy Whitcroft } 1166773647a0SAndy Whitcroft #next; 1167de7d4f0eSAndy Whitcroft } 1168773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1169773647a0SAndy Whitcroft $realline=$1-1; 1170773647a0SAndy Whitcroft if (defined $2) { 1171773647a0SAndy Whitcroft $realcnt=$3+1; 1172773647a0SAndy Whitcroft } else { 1173773647a0SAndy Whitcroft $realcnt=1+1; 1174773647a0SAndy Whitcroft } 1175c45dcabdSAndy Whitcroft $in_comment = 0; 1176773647a0SAndy Whitcroft 1177773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1178773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1179773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1180773647a0SAndy Whitcroft # at context start. 1181773647a0SAndy Whitcroft my $edge; 118201fa9147SAndy Whitcroft my $cnt = $realcnt; 118301fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 118401fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 118501fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 118601fa9147SAndy Whitcroft $cnt--; 118701fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1188721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1189fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1190fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1191fae17daeSAndy Whitcroft ($edge) = $1; 1192fae17daeSAndy Whitcroft last; 1193fae17daeSAndy Whitcroft } 1194773647a0SAndy Whitcroft } 1195773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1196773647a0SAndy Whitcroft $in_comment = 1; 1197773647a0SAndy Whitcroft } 1198773647a0SAndy Whitcroft 1199773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1200773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1201773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1202773647a0SAndy Whitcroft if (!defined $edge && 120383242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1204773647a0SAndy Whitcroft { 1205773647a0SAndy Whitcroft $in_comment = 1; 1206773647a0SAndy Whitcroft } 1207773647a0SAndy Whitcroft 1208773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1209773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1210773647a0SAndy Whitcroft 1211171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1212773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1213171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1214773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1215773647a0SAndy Whitcroft } 1216773647a0SAndy Whitcroft push(@lines, $line); 1217773647a0SAndy Whitcroft 1218773647a0SAndy Whitcroft if ($realcnt > 1) { 1219773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1220773647a0SAndy Whitcroft } else { 1221773647a0SAndy Whitcroft $realcnt = 0; 1222773647a0SAndy Whitcroft } 1223773647a0SAndy Whitcroft 1224773647a0SAndy Whitcroft #print "==>$rawline\n"; 1225773647a0SAndy Whitcroft #print "-->$line\n"; 1226de7d4f0eSAndy Whitcroft 1227de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1228de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1229de7d4f0eSAndy Whitcroft } 1230de7d4f0eSAndy Whitcroft } 1231de7d4f0eSAndy Whitcroft 12326c72ffaaSAndy Whitcroft $prefix = ''; 12336c72ffaaSAndy Whitcroft 1234773647a0SAndy Whitcroft $realcnt = 0; 1235773647a0SAndy Whitcroft $linenr = 0; 12360a920b5bSAndy Whitcroft foreach my $line (@lines) { 12370a920b5bSAndy Whitcroft $linenr++; 12380a920b5bSAndy Whitcroft 1239c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 12406c72ffaaSAndy Whitcroft 12410a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 12426c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 12430a920b5bSAndy Whitcroft $is_patch = 1; 12444a0df2efSAndy Whitcroft $first_line = $linenr + 1; 12450a920b5bSAndy Whitcroft $realline=$1-1; 12460a920b5bSAndy Whitcroft if (defined $2) { 12470a920b5bSAndy Whitcroft $realcnt=$3+1; 12480a920b5bSAndy Whitcroft } else { 12490a920b5bSAndy Whitcroft $realcnt=1+1; 12500a920b5bSAndy Whitcroft } 1251c2fdda0dSAndy Whitcroft annotate_reset(); 125213214adfSAndy Whitcroft $prev_values = 'E'; 125313214adfSAndy Whitcroft 1254773647a0SAndy Whitcroft %suppress_ifbraces = (); 1255170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 12560a920b5bSAndy Whitcroft next; 12570a920b5bSAndy Whitcroft 12584a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 12594a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 12604a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1261773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 12620a920b5bSAndy Whitcroft $realline++; 1263d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 12640a920b5bSAndy Whitcroft 12654a0df2efSAndy Whitcroft # Measure the line length and indent. 1266c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 12670a920b5bSAndy Whitcroft 12680a920b5bSAndy Whitcroft # Track the previous line. 12690a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 12700a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1271c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1272c2fdda0dSAndy Whitcroft 1273773647a0SAndy Whitcroft #warn "line<$line>\n"; 12746c72ffaaSAndy Whitcroft 1275d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1276d8aaf121SAndy Whitcroft $realcnt--; 12770a920b5bSAndy Whitcroft } 12780a920b5bSAndy Whitcroft 1279cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1280cc77cdcaSAndy Whitcroft 12810a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1282773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1283773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1284773647a0SAndy Whitcroft 12856c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 12866c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1287773647a0SAndy Whitcroft 1288773647a0SAndy Whitcroft # extract the filename as it passes 1289773647a0SAndy Whitcroft if ($line=~/^\+\+\+\s+(\S+)/) { 1290773647a0SAndy Whitcroft $realfile = $1; 12911e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 12921e855726SWolfram Sang 12931e855726SWolfram Sang $p1_prefix = $1; 1294e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1295e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 12961e855726SWolfram Sang WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 12971e855726SWolfram Sang } 1298773647a0SAndy Whitcroft 1299c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1300773647a0SAndy Whitcroft ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1301773647a0SAndy Whitcroft } 1302773647a0SAndy Whitcroft next; 1303773647a0SAndy Whitcroft } 1304773647a0SAndy Whitcroft 1305389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 13060a920b5bSAndy Whitcroft 1307c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1308c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1309c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 13100a920b5bSAndy Whitcroft 13116c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 13126c72ffaaSAndy Whitcroft 13130a920b5bSAndy Whitcroft#check the patch for a signoff: 1314d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 13154a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 13164a0df2efSAndy Whitcroft $signoff++; 13170a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 1318de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 1319de7d4f0eSAndy Whitcroft $herecurr); 13200a920b5bSAndy Whitcroft } 13210a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 1322773647a0SAndy Whitcroft WARN("space required after Signed-off-by:\n" . 1323de7d4f0eSAndy Whitcroft $herecurr); 13240a920b5bSAndy Whitcroft } 13250a920b5bSAndy Whitcroft } 13260a920b5bSAndy Whitcroft 132700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 13288905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1329de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 13306c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1331de7d4f0eSAndy Whitcroft } 1332de7d4f0eSAndy Whitcroft 13336ecd9674SAndy Whitcroft# Check for absolute kernel paths. 13346ecd9674SAndy Whitcroft if ($tree) { 13356ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 13366ecd9674SAndy Whitcroft my $file = $1; 13376ecd9674SAndy Whitcroft 13386ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 13396ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 13406ecd9674SAndy Whitcroft # 13416ecd9674SAndy Whitcroft } else { 13426ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 13436ecd9674SAndy Whitcroft } 13446ecd9674SAndy Whitcroft } 13456ecd9674SAndy Whitcroft } 13466ecd9674SAndy Whitcroft 1347de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1348de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1349171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1350171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1351171ae1a4SAndy Whitcroft 1352171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1353171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1354171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1355171ae1a4SAndy Whitcroft 1356171ae1a4SAndy Whitcroft ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 135700df344fSAndy Whitcroft } 13580a920b5bSAndy Whitcroft 135930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 136030670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 136100df344fSAndy Whitcroft 13620a920b5bSAndy Whitcroft#trailing whitespace 13639c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1364c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 13659c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 13669c0ca6f9SAndy Whitcroft 1367c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1368c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1369de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 13700a920b5bSAndy Whitcroft } 13715368df20SAndy Whitcroft 13725368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 13735368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 13745368df20SAndy Whitcroft 13750a920b5bSAndy Whitcroft#80 column limit 1376c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1377f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1378f4c014c0SAndy Whitcroft $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && 1379f4c014c0SAndy Whitcroft $length > 80) 1380c45dcabdSAndy Whitcroft { 1381de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 13820a920b5bSAndy Whitcroft } 13830a920b5bSAndy Whitcroft 13848905a67cSAndy Whitcroft# check for adding lines without a newline. 13858905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 13868905a67cSAndy Whitcroft WARN("adding a line without newline at end of file\n" . $herecurr); 13878905a67cSAndy Whitcroft } 13888905a67cSAndy Whitcroft 138942e41c54SMike Frysinger# Blackfin: use hi/lo macros 139042e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 139142e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 139242e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 139342e41c54SMike Frysinger ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 139442e41c54SMike Frysinger } 139542e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 139642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 139742e41c54SMike Frysinger ERROR("use the HI() macro, not (... >> 16)\n" . $herevet); 139842e41c54SMike Frysinger } 139942e41c54SMike Frysinger } 140042e41c54SMike Frysinger 1401b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1402b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 14030a920b5bSAndy Whitcroft 14040a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 14050a920b5bSAndy Whitcroft# more than 8 must use tabs. 1406c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1407c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1408c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1409171ae1a4SAndy Whitcroft ERROR("code indent should use tabs where possible\n" . $herevet); 14100a920b5bSAndy Whitcroft } 14110a920b5bSAndy Whitcroft 1412b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1413b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1414b9ea10d6SAndy Whitcroft 1415c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1416cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1417c2fdda0dSAndy Whitcroft WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1418c2fdda0dSAndy Whitcroft } 141922f2a2efSAndy Whitcroft 142042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 142142e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 142242e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 142342e41c54SMike Frysinger ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 142442e41c54SMike Frysinger } 142542e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 142642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 142742e41c54SMike Frysinger ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 142842e41c54SMike Frysinger } 142942e41c54SMike Frysinger 14309c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 1431170d3a22SAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next); 14329c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1433170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1434f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1435171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1436171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1437171ae1a4SAndy Whitcroft 1438171ae1a4SAndy Whitcroft my $s = $stat; 1439171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1440cf655043SAndy Whitcroft 1441c2fdda0dSAndy Whitcroft # Ignore goto labels. 1442171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1443c2fdda0dSAndy Whitcroft 1444c2fdda0dSAndy Whitcroft # Ignore functions being called 1445171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1446c2fdda0dSAndy Whitcroft 1447463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1448463f2864SAndy Whitcroft 1449c45dcabdSAndy Whitcroft # declarations always start with types 1450d2506586SAndy 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) { 1451c45dcabdSAndy Whitcroft my $type = $1; 1452c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1453c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1454c45dcabdSAndy Whitcroft 14556c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1456a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1457c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1458c2fdda0dSAndy Whitcroft } 14598905a67cSAndy Whitcroft 14606c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 146165863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1462c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 14639c0ca6f9SAndy Whitcroft } 14648905a67cSAndy Whitcroft 14658905a67cSAndy Whitcroft # Check for any sort of function declaration. 14668905a67cSAndy Whitcroft # int foo(something bar, other baz); 14678905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1468171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 14698905a67cSAndy Whitcroft my ($name_len) = length($1); 14708905a67cSAndy Whitcroft 1471cf655043SAndy Whitcroft my $ctx = $s; 1472773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 14738905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1474cf655043SAndy Whitcroft 14758905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1476c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 14778905a67cSAndy Whitcroft 1478c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 14798905a67cSAndy Whitcroft } 14808905a67cSAndy Whitcroft } 14818905a67cSAndy Whitcroft } 14828905a67cSAndy Whitcroft 14839c0ca6f9SAndy Whitcroft } 14849c0ca6f9SAndy Whitcroft 148500df344fSAndy Whitcroft# 148600df344fSAndy Whitcroft# Checks which may be anchored in the context. 148700df344fSAndy Whitcroft# 148800df344fSAndy Whitcroft 148900df344fSAndy Whitcroft# Check for switch () and associated case and default 149000df344fSAndy Whitcroft# statements should be at the same indent. 149100df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 149200df344fSAndy Whitcroft my $err = ''; 149300df344fSAndy Whitcroft my $sep = ''; 149400df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 149500df344fSAndy Whitcroft shift(@ctx); 149600df344fSAndy Whitcroft for my $ctx (@ctx) { 149700df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 149800df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 149900df344fSAndy Whitcroft $indent != $cindent) { 150000df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 150100df344fSAndy Whitcroft $sep = ''; 150200df344fSAndy Whitcroft } else { 150300df344fSAndy Whitcroft $sep = "[...]\n"; 150400df344fSAndy Whitcroft } 150500df344fSAndy Whitcroft } 150600df344fSAndy Whitcroft if ($err ne '') { 15079c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 1508de7d4f0eSAndy Whitcroft } 1509de7d4f0eSAndy Whitcroft } 1510de7d4f0eSAndy Whitcroft 1511de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1512de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1513c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1514773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1515773647a0SAndy Whitcroft 15169c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1517de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1518de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1519de7d4f0eSAndy Whitcroft 1520548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1521548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1522de7d4f0eSAndy Whitcroft 1523548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1524548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1525548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1526548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1527548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1528773647a0SAndy Whitcroft $ctx_ln++; 1529773647a0SAndy Whitcroft } 1530548596d5SAndy Whitcroft 153153210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 153253210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1533773647a0SAndy Whitcroft 1534773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1535773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . 1536c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 153700df344fSAndy Whitcroft } 1538773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1539773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1540773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1541773647a0SAndy Whitcroft { 15429c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 15439c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1544773647a0SAndy Whitcroft WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1545c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 15469c0ca6f9SAndy Whitcroft } 15479c0ca6f9SAndy Whitcroft } 154800df344fSAndy Whitcroft } 154900df344fSAndy Whitcroft 15504d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 15514d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 15524d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 15534d001e4dSAndy Whitcroft 15544d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 15554d001e4dSAndy Whitcroft 15564d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 15574d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 15584d001e4dSAndy Whitcroft # where necessary. 15594d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 15604d001e4dSAndy Whitcroft 15614d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 15626f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 15636f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 15644d001e4dSAndy Whitcroft 15654d001e4dSAndy Whitcroft # We want to check the first line inside the block 15664d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 15674d001e4dSAndy Whitcroft # 1) any blank line termination 15684d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 15694d001e4dSAndy Whitcroft # 3) any do (...) { 15704d001e4dSAndy Whitcroft my $continuation = 0; 15714d001e4dSAndy Whitcroft my $check = 0; 15724d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 15734d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 15744d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 15754d001e4dSAndy Whitcroft $continuation = 1; 15764d001e4dSAndy Whitcroft } 15779bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 15784d001e4dSAndy Whitcroft $check = 1; 15794d001e4dSAndy Whitcroft $cond_lines++; 15804d001e4dSAndy Whitcroft } 15814d001e4dSAndy Whitcroft 15824d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 15834d001e4dSAndy Whitcroft # preprocessor statement. 15844d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 15854d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 15864d001e4dSAndy Whitcroft $check = 0; 15874d001e4dSAndy Whitcroft } 15884d001e4dSAndy Whitcroft 15899bd49efeSAndy Whitcroft my $cond_ptr = -1; 1590740504c6SAndy Whitcroft $continuation = 0; 15919bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 15929bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 15934d001e4dSAndy Whitcroft 1594f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 1595f16fa28fSAndy Whitcroft # is not linear. 1596f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1597f16fa28fSAndy Whitcroft $check = 0; 1598f16fa28fSAndy Whitcroft } 1599f16fa28fSAndy Whitcroft 16009bd49efeSAndy Whitcroft # Ignore: 16019bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 16029bd49efeSAndy Whitcroft # 2) preprocessor lines, and 16039bd49efeSAndy Whitcroft # 3) labels. 1604740504c6SAndy Whitcroft if ($continuation || 1605740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 16069bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 16079bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 1608740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 160930dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 16109bd49efeSAndy Whitcroft $cond_lines++; 16119bd49efeSAndy Whitcroft } 16124d001e4dSAndy Whitcroft } 161330dad6ebSAndy Whitcroft } 16144d001e4dSAndy Whitcroft 16154d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 16164d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 16174d001e4dSAndy Whitcroft 16184d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 16194d001e4dSAndy Whitcroft # this is not this patch's fault. 16204d001e4dSAndy Whitcroft if (!defined($stat_real) || 16214d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 16224d001e4dSAndy Whitcroft $check = 0; 16234d001e4dSAndy Whitcroft } 16244d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 16254d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 16264d001e4dSAndy Whitcroft } 16274d001e4dSAndy Whitcroft 16289bd49efeSAndy 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"; 16294d001e4dSAndy Whitcroft 16304d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 16314d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 16324d001e4dSAndy Whitcroft WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 16334d001e4dSAndy Whitcroft } 16344d001e4dSAndy Whitcroft } 16354d001e4dSAndy Whitcroft 16366c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 16376c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 16381f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 16391f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 16406c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1641c2fdda0dSAndy Whitcroft if ($dbg_values) { 1642c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1643cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1644cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 16451f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 1646c2fdda0dSAndy Whitcroft } 16476c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 16486c72ffaaSAndy Whitcroft 164900df344fSAndy Whitcroft#ignore lines not being added 165000df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 165100df344fSAndy Whitcroft 1652653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 16537429c690SAndy Whitcroft if ($dbg_type) { 16547429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 16557429c690SAndy Whitcroft ERROR("TEST: is type\n" . $herecurr); 16567429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 16577429c690SAndy Whitcroft ERROR("TEST: is not type ($1 is)\n". $herecurr); 16587429c690SAndy Whitcroft } 1659653d4876SAndy Whitcroft next; 1660653d4876SAndy Whitcroft } 1661a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 1662a1ef277eSAndy Whitcroft if ($dbg_attr) { 16639360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 1664a1ef277eSAndy Whitcroft ERROR("TEST: is attr\n" . $herecurr); 16659360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 1666a1ef277eSAndy Whitcroft ERROR("TEST: is not attr ($1 is)\n". $herecurr); 1667a1ef277eSAndy Whitcroft } 1668a1ef277eSAndy Whitcroft next; 1669a1ef277eSAndy Whitcroft } 1670653d4876SAndy Whitcroft 1671f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 1672f0a594c1SAndy Whitcroft if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && 1673f0a594c1SAndy Whitcroft $line =~ /^.\s*{/) { 1674773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . $hereprev); 1675f0a594c1SAndy Whitcroft } 1676f0a594c1SAndy Whitcroft 167700df344fSAndy Whitcroft# 167800df344fSAndy Whitcroft# Checks which are anchored on the added line. 167900df344fSAndy Whitcroft# 168000df344fSAndy Whitcroft 1681653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 1682c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1683653d4876SAndy Whitcroft my $path = $1; 1684653d4876SAndy Whitcroft if ($path =~ m{//}) { 1685de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 1686de7d4f0eSAndy Whitcroft $herecurr); 1687653d4876SAndy Whitcroft } 1688653d4876SAndy Whitcroft } 1689653d4876SAndy Whitcroft 169000df344fSAndy Whitcroft# no C99 // comments 169100df344fSAndy Whitcroft if ($line =~ m{//}) { 1692de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 169300df344fSAndy Whitcroft } 169400df344fSAndy Whitcroft # Remove C99 comments. 16950a920b5bSAndy Whitcroft $line =~ s@//.*@@; 16966c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 16970a920b5bSAndy Whitcroft 16980a920b5bSAndy Whitcroft#EXPORT_SYMBOL should immediately follow its function closing }. 1699653d4876SAndy Whitcroft if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 1700653d4876SAndy Whitcroft ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1701653d4876SAndy Whitcroft my $name = $1; 170248012058SAndy Whitcroft if ($prevline !~ /(?: 170348012058SAndy Whitcroft ^.}| 170448012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 170548012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 170648012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 170748012058SAndy Whitcroft ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 170848012058SAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[) 170948012058SAndy Whitcroft )/x) { 1710de7d4f0eSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 17110a920b5bSAndy Whitcroft } 17120a920b5bSAndy Whitcroft } 17130a920b5bSAndy Whitcroft 1714f0a594c1SAndy Whitcroft# check for external initialisers. 1715c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1716f0a594c1SAndy Whitcroft ERROR("do not initialise externals to 0 or NULL\n" . 1717f0a594c1SAndy Whitcroft $herecurr); 1718f0a594c1SAndy Whitcroft } 17190a920b5bSAndy Whitcroft# check for static initialisers. 17202d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 1721de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 1722de7d4f0eSAndy Whitcroft $herecurr); 17230a920b5bSAndy Whitcroft } 17240a920b5bSAndy Whitcroft 1725653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1726653d4876SAndy Whitcroft# make sense. 1727653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 17288054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 1729c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 17308ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 1731653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1732de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 17330a920b5bSAndy Whitcroft } 17340a920b5bSAndy Whitcroft 17350a920b5bSAndy Whitcroft# * goes on variable not on type 173665863862SAndy Whitcroft # (char*[ const]) 173700ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 173865863862SAndy Whitcroft my ($from, $to) = ($1, $1); 1739d8aaf121SAndy Whitcroft 174065863862SAndy Whitcroft # Should start with a space. 174165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 174265863862SAndy Whitcroft # Should not end with a space. 174365863862SAndy Whitcroft $to =~ s/\s+$//; 174465863862SAndy Whitcroft # '*'s should not have spaces between. 1745f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 174665863862SAndy Whitcroft } 1747d8aaf121SAndy Whitcroft 174865863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 174965863862SAndy Whitcroft if ($from ne $to) { 175065863862SAndy Whitcroft ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 175165863862SAndy Whitcroft } 175200ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 175365863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 1754d8aaf121SAndy Whitcroft 175565863862SAndy Whitcroft # Should start with a space. 175665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 175765863862SAndy Whitcroft # Should not end with a space. 175865863862SAndy Whitcroft $to =~ s/\s+$//; 175965863862SAndy Whitcroft # '*'s should not have spaces between. 1760f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 176165863862SAndy Whitcroft } 176265863862SAndy Whitcroft # Modifiers should have spaces. 176365863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 176465863862SAndy Whitcroft 1765667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 1766667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 176765863862SAndy Whitcroft ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 176865863862SAndy Whitcroft } 17690a920b5bSAndy Whitcroft } 17700a920b5bSAndy Whitcroft 17710a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 17720a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 17730a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 17740a920b5bSAndy Whitcroft# print "$herecurr"; 17750a920b5bSAndy Whitcroft# $clean = 0; 17760a920b5bSAndy Whitcroft# } 17770a920b5bSAndy Whitcroft 17788905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1779c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 17808905a67cSAndy Whitcroft } 17818905a67cSAndy Whitcroft 178200df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 178300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 178400df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 178500df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 178600df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1787f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 178800df344fSAndy Whitcroft my $ok = 0; 178900df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 179000df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 179100df344fSAndy Whitcroft # we have a preceeding printk if it ends 179200df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 179300df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 179400df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 179500df344fSAndy Whitcroft $ok = 1; 179600df344fSAndy Whitcroft } 179700df344fSAndy Whitcroft last; 179800df344fSAndy Whitcroft } 179900df344fSAndy Whitcroft } 180000df344fSAndy Whitcroft if ($ok == 0) { 1801de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 18020a920b5bSAndy Whitcroft } 180300df344fSAndy Whitcroft } 18040a920b5bSAndy Whitcroft 1805653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1806653d4876SAndy Whitcroft# or if closed on same line 1807c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1808c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1809de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 18100a920b5bSAndy Whitcroft } 1811653d4876SAndy Whitcroft 18128905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 18138905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 18148905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 18158905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 18168905a67cSAndy Whitcroft } 18178905a67cSAndy Whitcroft 18188d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 18198d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1820fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1821fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 18228d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 18238d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 18248d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1825fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 1826fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 18278d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 18288d31cfceSAndy Whitcroft } 18298d31cfceSAndy Whitcroft } 18308d31cfceSAndy Whitcroft 1831f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 18326c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 1833c2fdda0dSAndy Whitcroft my $name = $1; 1834773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 1835773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 1836c2fdda0dSAndy Whitcroft 1837c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 1838773647a0SAndy Whitcroft if ($name =~ /^(?: 1839773647a0SAndy Whitcroft if|for|while|switch|return|case| 1840773647a0SAndy Whitcroft volatile|__volatile__| 1841773647a0SAndy Whitcroft __attribute__|format|__extension__| 1842773647a0SAndy Whitcroft asm|__asm__)$/x) 1843773647a0SAndy Whitcroft { 1844c2fdda0dSAndy Whitcroft 1845c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 1846c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 1847c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 1848c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 1849773647a0SAndy Whitcroft 1850773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 1851c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 1852c2fdda0dSAndy Whitcroft 1853c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 1854c2fdda0dSAndy Whitcroft # likely a typedef for a function. 1855773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 1856c2fdda0dSAndy Whitcroft 1857c2fdda0dSAndy Whitcroft } else { 1858773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 1859f0a594c1SAndy Whitcroft } 18606c72ffaaSAndy Whitcroft } 1861653d4876SAndy Whitcroft# Check operator spacing. 18620a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 18639c0ca6f9SAndy Whitcroft my $ops = qr{ 18649c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 18659c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 18669c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 18671f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 18681f65f947SAndy Whitcroft \?|: 18699c0ca6f9SAndy Whitcroft }x; 1870cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 187100df344fSAndy Whitcroft my $off = 0; 18726c72ffaaSAndy Whitcroft 18736c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 18746c72ffaaSAndy Whitcroft 18750a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 18764a0df2efSAndy Whitcroft $off += length($elements[$n]); 18774a0df2efSAndy Whitcroft 1878773647a0SAndy Whitcroft # Pick up the preceeding and succeeding characters. 1879773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 1880773647a0SAndy Whitcroft my $cc = ''; 1881773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 1882773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 1883773647a0SAndy Whitcroft } 1884773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 1885773647a0SAndy Whitcroft 18864a0df2efSAndy Whitcroft my $a = ''; 18874a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 18884a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 1889cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 18904a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 18914a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 1892773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 18934a0df2efSAndy Whitcroft 18940a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 18954a0df2efSAndy Whitcroft 18964a0df2efSAndy Whitcroft my $c = ''; 18970a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 18984a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 18994a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 1900cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 19014a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 19024a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 19038b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 19044a0df2efSAndy Whitcroft } else { 19054a0df2efSAndy Whitcroft $c = 'E'; 19060a920b5bSAndy Whitcroft } 19070a920b5bSAndy Whitcroft 19084a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 19094a0df2efSAndy Whitcroft 19104a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 19114a0df2efSAndy Whitcroft 19126c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 1913de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 19140a920b5bSAndy Whitcroft 191574048ed8SAndy Whitcroft # Pull out the value of this operator. 19166c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 19170a920b5bSAndy Whitcroft 19181f65f947SAndy Whitcroft # Get the full operator variant. 19191f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 19201f65f947SAndy Whitcroft 192113214adfSAndy Whitcroft # Ignore operators passed as parameters. 192213214adfSAndy Whitcroft if ($op_type ne 'V' && 192313214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 192413214adfSAndy Whitcroft 1925cf655043SAndy Whitcroft# # Ignore comments 1926cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 192713214adfSAndy Whitcroft 1928d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 192913214adfSAndy Whitcroft } elsif ($op eq ';') { 1930cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 1931cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 1932773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 1933d8aaf121SAndy Whitcroft } 1934d8aaf121SAndy Whitcroft 1935d8aaf121SAndy Whitcroft # // is a comment 1936d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 19370a920b5bSAndy Whitcroft 19381f65f947SAndy Whitcroft # No spaces for: 19391f65f947SAndy Whitcroft # -> 19401f65f947SAndy Whitcroft # : when part of a bitfield 19411f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 19424a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 1943773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 19440a920b5bSAndy Whitcroft } 19450a920b5bSAndy Whitcroft 19460a920b5bSAndy Whitcroft # , must have a space on the right. 19470a920b5bSAndy Whitcroft } elsif ($op eq ',') { 1948cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 1949773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 19500a920b5bSAndy Whitcroft } 19510a920b5bSAndy Whitcroft 19529c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 195374048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 19549c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 19559c0ca6f9SAndy Whitcroft 19569c0ca6f9SAndy Whitcroft # unary operators should have a space before and 19579c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 19589c0ca6f9SAndy Whitcroft # unary operator, or a cast 19599c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 196074048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 19610d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 1962cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 1963773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 19640a920b5bSAndy Whitcroft } 1965a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 1966171ae1a4SAndy Whitcroft # A unary '*' may be const 1967171ae1a4SAndy Whitcroft 1968171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 1969fb9e9096SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 19700a920b5bSAndy Whitcroft } 19710a920b5bSAndy Whitcroft 19720a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 19730a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 1974773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 1975773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 19760a920b5bSAndy Whitcroft } 1977773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 1978773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 1979773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 1980653d4876SAndy Whitcroft } 1981773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 1982773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 1983773647a0SAndy Whitcroft } 1984773647a0SAndy Whitcroft 19850a920b5bSAndy Whitcroft 19860a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 19879c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 19889c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 19899c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 1990c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 1991c2fdda0dSAndy Whitcroft $op eq '%') 19920a920b5bSAndy Whitcroft { 1993773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 1994de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 1995de7d4f0eSAndy Whitcroft $hereptr); 19960a920b5bSAndy Whitcroft } 19970a920b5bSAndy Whitcroft 19981f65f947SAndy Whitcroft # A colon needs no spaces before when it is 19991f65f947SAndy Whitcroft # terminating a case value or a label. 20001f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 20011f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 20021f65f947SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 20031f65f947SAndy Whitcroft } 20041f65f947SAndy Whitcroft 20050a920b5bSAndy Whitcroft # All the others need spaces both sides. 2006cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 20071f65f947SAndy Whitcroft my $ok = 0; 20081f65f947SAndy Whitcroft 200922f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 20101f65f947SAndy Whitcroft if (($op eq '<' && 20111f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 20121f65f947SAndy Whitcroft ($op eq '>' && 20131f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 20141f65f947SAndy Whitcroft { 20151f65f947SAndy Whitcroft $ok = 1; 20161f65f947SAndy Whitcroft } 20171f65f947SAndy Whitcroft 20181f65f947SAndy Whitcroft # Ignore ?: 20191f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 20201f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 20211f65f947SAndy Whitcroft $ok = 1; 20221f65f947SAndy Whitcroft } 20231f65f947SAndy Whitcroft 20241f65f947SAndy Whitcroft if ($ok == 0) { 2025773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 20260a920b5bSAndy Whitcroft } 202722f2a2efSAndy Whitcroft } 20284a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 20290a920b5bSAndy Whitcroft } 20300a920b5bSAndy Whitcroft } 20310a920b5bSAndy Whitcroft 2032f0a594c1SAndy Whitcroft# check for multiple assignments 2033f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 20346c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 2035f0a594c1SAndy Whitcroft } 2036f0a594c1SAndy Whitcroft 203722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 203822f2a2efSAndy Whitcroft## # continuation. 203922f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 204022f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 204122f2a2efSAndy Whitcroft## 204222f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 204322f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 204422f2a2efSAndy Whitcroft## my $ln = $line; 204522f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 204622f2a2efSAndy Whitcroft## } 204722f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 204822f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 204922f2a2efSAndy Whitcroft## } 205022f2a2efSAndy Whitcroft## } 2051f0a594c1SAndy Whitcroft 20520a920b5bSAndy Whitcroft#need space before brace following if, while, etc 205322f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 205422f2a2efSAndy Whitcroft $line =~ /do{/) { 2055773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 2056de7d4f0eSAndy Whitcroft } 2057de7d4f0eSAndy Whitcroft 2058de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2059de7d4f0eSAndy Whitcroft# on the line 2060de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2061773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 20620a920b5bSAndy Whitcroft } 20630a920b5bSAndy Whitcroft 206422f2a2efSAndy Whitcroft# check spacing on square brackets 206522f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2066773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 206722f2a2efSAndy Whitcroft } 206822f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2069773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 207022f2a2efSAndy Whitcroft } 207122f2a2efSAndy Whitcroft 2072c45dcabdSAndy Whitcroft# check spacing on parentheses 20739c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 20749c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2075773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 207622f2a2efSAndy Whitcroft } 207713214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2078c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2079c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2080773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 208122f2a2efSAndy Whitcroft } 208222f2a2efSAndy Whitcroft 20830a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 20844a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 20850a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2086de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 20870a920b5bSAndy Whitcroft } 20880a920b5bSAndy Whitcroft 2089c45dcabdSAndy Whitcroft# Return is not a function. 2090c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2091c45dcabdSAndy Whitcroft my $spacing = $1; 2092c45dcabdSAndy Whitcroft my $value = $2; 2093c45dcabdSAndy Whitcroft 209486f9d059SAndy Whitcroft # Flatten any parentheses 2095fee61c47SAndy Whitcroft $value =~ s/\)\(/\) \(/g; 209663f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 209763f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 209863f17f89SAndy Whitcroft $Compare\s* 209963f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 210063f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2101c45dcabdSAndy Whitcroft } 2102c45dcabdSAndy Whitcroft 2103c45dcabdSAndy Whitcroft if ($value =~ /^(?:$Ident|-?$Constant)$/) { 2104c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2105c45dcabdSAndy Whitcroft 2106c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2107c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 2108c45dcabdSAndy Whitcroft } 2109c45dcabdSAndy Whitcroft } 2110c45dcabdSAndy Whitcroft 21110a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 21124a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2113773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 21140a920b5bSAndy Whitcroft } 21150a920b5bSAndy Whitcroft 2116f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2117f5fe35ddSAndy Whitcroft# statements after the conditional. 2118170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2119170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2120170d3a22SAndy Whitcroft $remain_next, $off_next); 2121170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2122170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2123170d3a22SAndy Whitcroft 2124170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2125170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2126170d3a22SAndy Whitcroft # then count those as offsets. 2127170d3a22SAndy Whitcroft my ($whitespace) = 2128170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2129170d3a22SAndy Whitcroft my $offset = 2130170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2131170d3a22SAndy Whitcroft 2132170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2133170d3a22SAndy Whitcroft $offset} = 1; 2134170d3a22SAndy Whitcroft } 2135170d3a22SAndy Whitcroft } 2136170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2137170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2138171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 21398905a67cSAndy Whitcroft 2140b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2141c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 21428905a67cSAndy Whitcroft } 21438905a67cSAndy Whitcroft 21448905a67cSAndy Whitcroft # Find out what is on the end of the line after the 21458905a67cSAndy Whitcroft # conditional. 2146773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 21478905a67cSAndy Whitcroft $s =~ s/\n.*//g; 214813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 214953210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 215053210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2151773647a0SAndy Whitcroft { 2152bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2153bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2154bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 2155bb44ad39SAndy Whitcroft 2156bb44ad39SAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 2157bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2158bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2159bb44ad39SAndy Whitcroft } 2160bb44ad39SAndy Whitcroft 2161bb44ad39SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 21628905a67cSAndy Whitcroft } 21638905a67cSAndy Whitcroft } 21648905a67cSAndy Whitcroft 216513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 216613214adfSAndy Whitcroft if ($line =~ / 216713214adfSAndy Whitcroft (?: 216813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 216913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 217013214adfSAndy Whitcroft (?:\&\&|\|\|) 217113214adfSAndy Whitcroft | 217213214adfSAndy Whitcroft (?:\&\&|\|\|) 217313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 217413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 217513214adfSAndy Whitcroft )/x) 217613214adfSAndy Whitcroft { 217713214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 217813214adfSAndy Whitcroft } 217913214adfSAndy Whitcroft 21808905a67cSAndy Whitcroft# if and else should not have general statements after it 218113214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 218213214adfSAndy Whitcroft my $s = $1; 218313214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 218413214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 21858905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 21860a920b5bSAndy Whitcroft } 218713214adfSAndy Whitcroft } 218839667782SAndy Whitcroft# if should not continue a brace 218939667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 219039667782SAndy Whitcroft ERROR("trailing statements should be on next line\n" . 219139667782SAndy Whitcroft $herecurr); 219239667782SAndy Whitcroft } 2193a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2194a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2195a1080bf8SAndy Whitcroft $line !~ /\G(?: 21963fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2197a1080bf8SAndy Whitcroft \s*return\s+ 2198a1080bf8SAndy Whitcroft )/xg) 2199a1080bf8SAndy Whitcroft { 2200a1080bf8SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 2201a1080bf8SAndy Whitcroft } 22020a920b5bSAndy Whitcroft 22030a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 22040a920b5bSAndy Whitcroft # indent level to be relevant to each other. 22050a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 22060a920b5bSAndy Whitcroft $previndent == $indent) { 2207de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 22080a920b5bSAndy Whitcroft } 22090a920b5bSAndy Whitcroft 2210c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2211c2fdda0dSAndy Whitcroft $previndent == $indent) { 2212c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2213c2fdda0dSAndy Whitcroft 2214c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2215c2fdda0dSAndy Whitcroft # conditional. 2216773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2217c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2218c2fdda0dSAndy Whitcroft 2219c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2220c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 2221c2fdda0dSAndy Whitcroft } 2222c2fdda0dSAndy Whitcroft } 2223c2fdda0dSAndy Whitcroft 22240a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 22250a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 22260a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 22270a920b5bSAndy Whitcroft# print "$herecurr"; 22280a920b5bSAndy Whitcroft# $clean = 0; 22290a920b5bSAndy Whitcroft# } 22300a920b5bSAndy Whitcroft 22310a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2232c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2233de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 22340a920b5bSAndy Whitcroft } 22350a920b5bSAndy Whitcroft 2236653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2237c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2238e09dec48SAndy Whitcroft my $file = "$1.h"; 2239e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2240e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2241e09dec48SAndy Whitcroft $realfile ne $checkfile && 2242c45dcabdSAndy Whitcroft $1 ne 'irq') 2243c45dcabdSAndy Whitcroft { 2244e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2245e09dec48SAndy Whitcroft CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2246e09dec48SAndy Whitcroft } else { 2247e09dec48SAndy Whitcroft WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2248e09dec48SAndy Whitcroft } 22490a920b5bSAndy Whitcroft } 22500a920b5bSAndy Whitcroft } 22510a920b5bSAndy Whitcroft 2252653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2253653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2254cf655043SAndy Whitcroft# in a known good container 2255b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2256b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2257d8aaf121SAndy Whitcroft my $ln = $linenr; 2258d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2259c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2260c45dcabdSAndy Whitcroft my $ctx = ''; 2261653d4876SAndy Whitcroft 2262c45dcabdSAndy Whitcroft my $args = defined($1); 2263de7d4f0eSAndy Whitcroft 2264c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2265c45dcabdSAndy Whitcroft # search to that. 2266c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2267c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2268c45dcabdSAndy Whitcroft { 2269c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2270a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2271c45dcabdSAndy Whitcroft $ln++; 2272de7d4f0eSAndy Whitcroft } 2273c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2274d8aaf121SAndy Whitcroft 2275c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2276c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2277c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2278a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2279c45dcabdSAndy Whitcroft 2280c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2281c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2282c45dcabdSAndy Whitcroft $rest = ''; 2283636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2284636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2285a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2286c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2287c45dcabdSAndy Whitcroft $cnt--; 2288a3bb97a7SAndy Whitcroft } 2289a3bb97a7SAndy Whitcroft $ln++; 2290c45dcabdSAndy Whitcroft $off = 0; 2291c45dcabdSAndy Whitcroft } 2292c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2293c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2294c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2295c45dcabdSAndy Whitcroft 2296c45dcabdSAndy Whitcroft # Clean up the original statement. 2297c45dcabdSAndy Whitcroft if ($args) { 2298c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2299d8aaf121SAndy Whitcroft } else { 2300c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2301c45dcabdSAndy Whitcroft } 2302292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2303c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2304c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2305c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2306c45dcabdSAndy Whitcroft 2307c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2308bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2309bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2310bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2311bf30d6edSAndy Whitcroft { 2312c45dcabdSAndy Whitcroft } 2313c45dcabdSAndy Whitcroft 2314c45dcabdSAndy Whitcroft my $exceptions = qr{ 2315c45dcabdSAndy Whitcroft $Declare| 2316c45dcabdSAndy Whitcroft module_param_named| 2317c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2318c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2319c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2320383099fdSAndy Whitcroft __typeof__\(| 2321ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2322ea71a0a0SAndy Whitcroft ^\"|\"$ 2323c45dcabdSAndy Whitcroft }x; 2324383099fdSAndy Whitcroft #print "REST<$rest> dstat<$dstat>\n"; 2325c45dcabdSAndy Whitcroft if ($rest ne '') { 2326c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2327c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2328c45dcabdSAndy Whitcroft { 2329c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2330c45dcabdSAndy Whitcroft } 2331c45dcabdSAndy Whitcroft 2332c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2333c45dcabdSAndy Whitcroft if ($dstat ne '' && 2334c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2335c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2336b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2337c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2338c45dcabdSAndy Whitcroft { 2339de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2340d8aaf121SAndy Whitcroft } 23410a920b5bSAndy Whitcroft } 2342653d4876SAndy Whitcroft } 23430a920b5bSAndy Whitcroft 2344080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2345080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2346080ba929SMike Frysinger# . 2347080ba929SMike Frysinger# ALIGN(...) 2348080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2349080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2350080ba929SMike Frysinger WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2351080ba929SMike Frysinger } 2352080ba929SMike Frysinger 2353f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 235413214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 235513214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2356cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 235713214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2358cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2359cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 236013214adfSAndy Whitcroft my $allowed = 0; 236113214adfSAndy Whitcroft my $seen = 0; 2362773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2363cf655043SAndy Whitcroft my $ln = $linenr - 1; 236413214adfSAndy Whitcroft for my $chunk (@chunks) { 236513214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 236613214adfSAndy Whitcroft 2367773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2368773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2369773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2370773647a0SAndy Whitcroft 2371773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2372773647a0SAndy Whitcroft 2373773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2374773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2375773647a0SAndy Whitcroft 2376773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2377cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2378cf655043SAndy Whitcroft 2379773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 238013214adfSAndy Whitcroft 238113214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 238213214adfSAndy Whitcroft 2383cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2384cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2385cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 238613214adfSAndy Whitcroft $allowed = 1; 238713214adfSAndy Whitcroft } 238813214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2389cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 239013214adfSAndy Whitcroft $allowed = 1; 239113214adfSAndy Whitcroft } 2392cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2393cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 239413214adfSAndy Whitcroft $allowed = 1; 239513214adfSAndy Whitcroft } 239613214adfSAndy Whitcroft } 239713214adfSAndy Whitcroft if ($seen && !$allowed) { 2398cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 239913214adfSAndy Whitcroft } 240013214adfSAndy Whitcroft } 240113214adfSAndy Whitcroft } 2402773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 240313214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2404cf655043SAndy Whitcroft my $allowed = 0; 2405f0a594c1SAndy Whitcroft 2406cf655043SAndy Whitcroft # Check the pre-context. 2407cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2408cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2409cf655043SAndy Whitcroft $allowed = 1; 2410f0a594c1SAndy Whitcroft } 2411773647a0SAndy Whitcroft 2412773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2413773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2414773647a0SAndy Whitcroft 2415cf655043SAndy Whitcroft # Check the condition. 2416cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2417773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2418cf655043SAndy Whitcroft if (defined $cond) { 2419773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2420cf655043SAndy Whitcroft } 2421cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2422cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2423cf655043SAndy Whitcroft $allowed = 1; 2424cf655043SAndy Whitcroft } 2425cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2426cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2427cf655043SAndy Whitcroft $allowed = 1; 2428cf655043SAndy Whitcroft } 2429cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2430cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2431cf655043SAndy Whitcroft $allowed = 1; 2432cf655043SAndy Whitcroft } 2433cf655043SAndy Whitcroft # Check the post-context. 2434cf655043SAndy Whitcroft if (defined $chunks[1]) { 2435cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2436cf655043SAndy Whitcroft if (defined $cond) { 2437773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2438cf655043SAndy Whitcroft } 2439cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2440cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2441cf655043SAndy Whitcroft $allowed = 1; 2442cf655043SAndy Whitcroft } 2443cf655043SAndy Whitcroft } 2444cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2445cf655043SAndy Whitcroft my $herectx = $here . "\n";; 2446f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2447cf655043SAndy Whitcroft 2448f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2449f055663cSAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n";; 2450cf655043SAndy Whitcroft } 2451cf655043SAndy Whitcroft 2452cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 2453f0a594c1SAndy Whitcroft } 2454f0a594c1SAndy Whitcroft } 2455f0a594c1SAndy Whitcroft 2456653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 24574a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2458c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2459de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 24600a920b5bSAndy Whitcroft } 24610a920b5bSAndy Whitcroft } 24620a920b5bSAndy Whitcroft 24634a0df2efSAndy Whitcroft# don't use deprecated functions 24644a0df2efSAndy Whitcroft for my $func (@dep_functions) { 246500df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2466de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 24674a0df2efSAndy Whitcroft } 24684a0df2efSAndy Whitcroft } 24694a0df2efSAndy Whitcroft 24704a0df2efSAndy Whitcroft# no volatiles please 24716c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 24726c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2473de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 24744a0df2efSAndy Whitcroft } 24754a0df2efSAndy Whitcroft 24769c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 24779c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 24789c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 24799c0ca6f9SAndy Whitcroft } 24809c0ca6f9SAndy Whitcroft 248100df344fSAndy Whitcroft# warn about #if 0 2482c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2483de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2484de7d4f0eSAndy Whitcroft $herecurr); 24854a0df2efSAndy Whitcroft } 24864a0df2efSAndy Whitcroft 2487f0a594c1SAndy Whitcroft# check for needless kfree() checks 2488f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2489f0a594c1SAndy Whitcroft my $expr = $1; 2490f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 24913c232147SWolfram Sang WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2492f0a594c1SAndy Whitcroft } 2493f0a594c1SAndy Whitcroft } 24944c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 24954c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 24964c432a8fSGreg Kroah-Hartman my $expr = $1; 24974c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 24984c432a8fSGreg Kroah-Hartman WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 24994c432a8fSGreg Kroah-Hartman } 25004c432a8fSGreg Kroah-Hartman } 2501f0a594c1SAndy Whitcroft 250200df344fSAndy Whitcroft# warn about #ifdefs in C files 2503c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 250400df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 250500df344fSAndy Whitcroft# print "$herecurr"; 250600df344fSAndy Whitcroft# $clean = 0; 250700df344fSAndy Whitcroft# } 250800df344fSAndy Whitcroft 250922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2510c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 251122f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 251222f2a2efSAndy Whitcroft } 251322f2a2efSAndy Whitcroft 25144a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2515171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2516171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 25174a0df2efSAndy Whitcroft my $which = $1; 25184a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2519de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 25204a0df2efSAndy Whitcroft } 25214a0df2efSAndy Whitcroft } 25224a0df2efSAndy Whitcroft# check for memory barriers without a comment. 25234a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 25244a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2525de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 25264a0df2efSAndy Whitcroft } 25274a0df2efSAndy Whitcroft } 25284a0df2efSAndy Whitcroft# check of hardware specific defines 2529c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2530de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 25310a920b5bSAndy Whitcroft } 2532653d4876SAndy Whitcroft 2533de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2534de7d4f0eSAndy Whitcroft# storage class and type. 25359c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 25369c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2537de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2538de7d4f0eSAndy Whitcroft } 2539de7d4f0eSAndy Whitcroft 25408905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 25418905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 25428905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 25438905a67cSAndy Whitcroft } 25448905a67cSAndy Whitcroft 2545de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2546171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2547c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2548171ae1a4SAndy Whitcroft { 2549c45dcabdSAndy Whitcroft my $function_name = $1; 2550c45dcabdSAndy Whitcroft my $paren_space = $2; 2551171ae1a4SAndy Whitcroft 2552171ae1a4SAndy Whitcroft my $s = $stat; 2553171ae1a4SAndy Whitcroft if (defined $cond) { 2554171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2555171ae1a4SAndy Whitcroft } 2556c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2557c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2558c45dcabdSAndy Whitcroft { 2559de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2560de7d4f0eSAndy Whitcroft } 2561de7d4f0eSAndy Whitcroft 2562171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2563171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2564171ae1a4SAndy Whitcroft } 25659c9ba34eSAndy Whitcroft 25669c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 25679c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 25689c9ba34eSAndy Whitcroft { 25699c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2570171ae1a4SAndy Whitcroft } 2571171ae1a4SAndy Whitcroft 2572de7d4f0eSAndy Whitcroft# checks for new __setup's 2573de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2574de7d4f0eSAndy Whitcroft my $name = $1; 2575de7d4f0eSAndy Whitcroft 2576de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2577de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2578de7d4f0eSAndy Whitcroft } 2579653d4876SAndy Whitcroft } 25809c0ca6f9SAndy Whitcroft 25819c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 25829c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 25839c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 25849c0ca6f9SAndy Whitcroft } 258513214adfSAndy Whitcroft 258613214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 258713214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 258813214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 258913214adfSAndy Whitcroft } 2590773647a0SAndy Whitcroft 2591773647a0SAndy Whitcroft# check for semaphores used as mutexes 2592171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2593773647a0SAndy Whitcroft WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); 2594773647a0SAndy Whitcroft } 2595773647a0SAndy Whitcroft# check for semaphores used as mutexes 2596171ae1a4SAndy Whitcroft if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { 2597773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 2598773647a0SAndy Whitcroft } 2599773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto* 2600773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2601773647a0SAndy Whitcroft WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); 2602773647a0SAndy Whitcroft } 2603f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2604f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2605f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2606f3db6639SMichael Ellerman } 26072b6db5cbSAndy Whitcroft# check for struct file_operations, ensure they are const. 26086903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 26096903ffb2SAndy Whitcroft $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) { 26106903ffb2SAndy Whitcroft WARN("struct $1 should normally be const\n" . 26116903ffb2SAndy Whitcroft $herecurr); 26122b6db5cbSAndy Whitcroft } 2613773647a0SAndy Whitcroft 2614773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2615773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2616773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2617c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2618c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2619171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2620171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2621171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2622773647a0SAndy Whitcroft { 2623773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2624773647a0SAndy Whitcroft } 26259c9ba34eSAndy Whitcroft 26269c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 26279c9ba34eSAndy Whitcroft my $string; 26289c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 26299c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 26302a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 26319c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 26329c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 26339c9ba34eSAndy Whitcroft last; 26349c9ba34eSAndy Whitcroft } 26359c9ba34eSAndy Whitcroft } 2636691d77b6SAndy Whitcroft 2637691d77b6SAndy Whitcroft# whine mightly about in_atomic 2638691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 2639691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 2640691d77b6SAndy Whitcroft ERROR("do not use in_atomic in drivers\n" . $herecurr); 2641f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 2642691d77b6SAndy Whitcroft WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2643691d77b6SAndy Whitcroft } 2644691d77b6SAndy Whitcroft } 264513214adfSAndy Whitcroft } 264613214adfSAndy Whitcroft 264713214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 264813214adfSAndy Whitcroft # so just keep quiet. 264913214adfSAndy Whitcroft if ($#rawlines == -1) { 265013214adfSAndy Whitcroft exit(0); 26510a920b5bSAndy Whitcroft } 26520a920b5bSAndy Whitcroft 26538905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 26548905a67cSAndy Whitcroft # things that appear to be patches. 26558905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 26568905a67cSAndy Whitcroft exit(0); 26578905a67cSAndy Whitcroft } 26588905a67cSAndy Whitcroft 26598905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 26608905a67cSAndy Whitcroft # just keep quiet. 26618905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 26628905a67cSAndy Whitcroft exit(0); 26638905a67cSAndy Whitcroft } 26648905a67cSAndy Whitcroft 26658905a67cSAndy Whitcroft if (!$is_patch) { 2666de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 26670a920b5bSAndy Whitcroft } 26680a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2669de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 26700a920b5bSAndy Whitcroft } 26710a920b5bSAndy Whitcroft 2672f0a594c1SAndy Whitcroft print report_dump(); 267313214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 267413214adfSAndy Whitcroft print "$filename " if ($summary_file); 26756c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 26766c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 26776c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 26788905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 26796c72ffaaSAndy Whitcroft } 26808905a67cSAndy Whitcroft 26810a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2682c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 26830a920b5bSAndy Whitcroft } 26840a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2685c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 26860a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 26870a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 26880a920b5bSAndy Whitcroft } 268913214adfSAndy Whitcroft 26900a920b5bSAndy Whitcroft return $clean; 26910a920b5bSAndy Whitcroft} 2692