10a920b5bSAndy Whitcroft#!/usr/bin/perl -w 2dbf004d7SDave Jones# (c) 2001, Dave Jones. (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) 5015830beSAndy Whitcroft# (c) 2008-2010 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 13267ad8f4SAndy Whitcroftmy $V = '0.31'; 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 106d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 107d2c0a235SAndy Whitcroft 1088905a67cSAndy Whitcroftif ($terse) { 1098905a67cSAndy Whitcroft $emacs = 1; 1108905a67cSAndy Whitcroft $quiet++; 1118905a67cSAndy Whitcroft} 1128905a67cSAndy Whitcroft 1136c72ffaaSAndy Whitcroftif ($tree) { 1146c72ffaaSAndy Whitcroft if (defined $root) { 1156c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 1166c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 1176c72ffaaSAndy Whitcroft } 1186c72ffaaSAndy Whitcroft } else { 1196c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 1206c72ffaaSAndy Whitcroft $root = '.'; 1216c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 1226c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 1236c72ffaaSAndy Whitcroft $root = $1; 1246c72ffaaSAndy Whitcroft } 1256c72ffaaSAndy Whitcroft } 1266c72ffaaSAndy Whitcroft 1276c72ffaaSAndy Whitcroft if (!defined $root) { 1280a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 1290a920b5bSAndy Whitcroft exit(2); 1300a920b5bSAndy Whitcroft } 1316c72ffaaSAndy Whitcroft} 1326c72ffaaSAndy Whitcroft 1336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 1346c72ffaaSAndy Whitcroft 1352ceb532bSAndy Whitcroftour $Ident = qr{ 1362ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 1372ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 1382ceb532bSAndy Whitcroft }x; 1396c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 1406c72ffaaSAndy Whitcroftour $Sparse = qr{ 1416c72ffaaSAndy Whitcroft __user| 1426c72ffaaSAndy Whitcroft __kernel| 1436c72ffaaSAndy Whitcroft __force| 1446c72ffaaSAndy Whitcroft __iomem| 1456c72ffaaSAndy Whitcroft __must_check| 1466c72ffaaSAndy Whitcroft __init_refok| 147417495edSAndy Whitcroft __kprobes| 148417495edSAndy Whitcroft __ref 1496c72ffaaSAndy Whitcroft }x; 15052131292SWolfram Sang 15152131292SWolfram Sang# Notes to $Attribute: 15252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 1536c72ffaaSAndy Whitcroftour $Attribute = qr{ 1546c72ffaaSAndy Whitcroft const| 15503f1df7dSJoe Perches __percpu| 15603f1df7dSJoe Perches __nocast| 15703f1df7dSJoe Perches __safe| 15803f1df7dSJoe Perches __bitwise__| 15903f1df7dSJoe Perches __packed__| 16003f1df7dSJoe Perches __packed2__| 16103f1df7dSJoe Perches __naked| 16203f1df7dSJoe Perches __maybe_unused| 16303f1df7dSJoe Perches __always_unused| 16403f1df7dSJoe Perches __noreturn| 16503f1df7dSJoe Perches __used| 16603f1df7dSJoe Perches __cold| 16703f1df7dSJoe Perches __noclone| 16803f1df7dSJoe Perches __deprecated| 1696c72ffaaSAndy Whitcroft __read_mostly| 1706c72ffaaSAndy Whitcroft __kprobes| 17152131292SWolfram Sang __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| 17224e1d81aSAndy Whitcroft ____cacheline_aligned| 17324e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 1745fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 1755fe3af11SAndy Whitcroft __weak 1766c72ffaaSAndy Whitcroft }x; 177c45dcabdSAndy Whitcroftour $Modifier; 1786c72ffaaSAndy Whitcroftour $Inline = qr{inline|__always_inline|noinline}; 1796c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 1806c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 1816c72ffaaSAndy Whitcroft 1826c72ffaaSAndy Whitcroftour $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; 1836c72ffaaSAndy Whitcroftour $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 18486f9d059SAndy Whitcroftour $Compare = qr{<=|>=|==|!=|<|>}; 1856c72ffaaSAndy Whitcroftour $Operators = qr{ 1866c72ffaaSAndy Whitcroft <=|>=|==|!=| 1876c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 188c2fdda0dSAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% 1896c72ffaaSAndy Whitcroft }x; 1906c72ffaaSAndy Whitcroft 1918905a67cSAndy Whitcroftour $NonptrType; 1928905a67cSAndy Whitcroftour $Type; 1938905a67cSAndy Whitcroftour $Declare; 1948905a67cSAndy Whitcroft 195171ae1a4SAndy Whitcroftour $UTF8 = qr { 196171ae1a4SAndy Whitcroft [\x09\x0A\x0D\x20-\x7E] # ASCII 197171ae1a4SAndy Whitcroft | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 198171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 199171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 200171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 201171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 202171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 203171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 204171ae1a4SAndy Whitcroft}x; 205171ae1a4SAndy Whitcroft 2068ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 207fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 2088ed22cadSAndy Whitcroft atomic_t 2098ed22cadSAndy Whitcroft)}; 2108ed22cadSAndy Whitcroft 211691e669bSJoe Perchesour $logFunctions = qr{(?x: 212691e669bSJoe Perches printk| 213691e669bSJoe Perches pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)| 2148bbea968SJoe Perches (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)| 215691e669bSJoe Perches WARN| 216691e669bSJoe Perches panic 217691e669bSJoe Perches)}; 218691e669bSJoe Perches 2198905a67cSAndy Whitcroftour @typeList = ( 2208905a67cSAndy Whitcroft qr{void}, 221c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?char}, 222c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?short}, 223c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?int}, 224c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long}, 225c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+int}, 226c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long}, 227c45dcabdSAndy Whitcroft qr{(?:unsigned\s+)?long\s+long\s+int}, 2288905a67cSAndy Whitcroft qr{unsigned}, 2298905a67cSAndy Whitcroft qr{float}, 2308905a67cSAndy Whitcroft qr{double}, 2318905a67cSAndy Whitcroft qr{bool}, 2328905a67cSAndy Whitcroft qr{struct\s+$Ident}, 2338905a67cSAndy Whitcroft qr{union\s+$Ident}, 2348905a67cSAndy Whitcroft qr{enum\s+$Ident}, 2358905a67cSAndy Whitcroft qr{${Ident}_t}, 2368905a67cSAndy Whitcroft qr{${Ident}_handler}, 2378905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 2388905a67cSAndy Whitcroft); 239c45dcabdSAndy Whitcroftour @modifierList = ( 240c45dcabdSAndy Whitcroft qr{fastcall}, 241c45dcabdSAndy Whitcroft); 2428905a67cSAndy Whitcroft 2437840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 2447840a94cSWolfram Sang irq| 2457840a94cSWolfram Sang memory 2467840a94cSWolfram Sang)}; 2477840a94cSWolfram Sang# memory.h: ARM has a custom one 2487840a94cSWolfram Sang 2498905a67cSAndy Whitcroftsub build_types { 250d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 251d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 252c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 2538905a67cSAndy Whitcroft $NonptrType = qr{ 254d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 255cf655043SAndy Whitcroft (?: 256c45dcabdSAndy Whitcroft (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| 2578ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 258c45dcabdSAndy Whitcroft (?:${all}\b) 259cf655043SAndy Whitcroft ) 260c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 2618905a67cSAndy Whitcroft }x; 2628905a67cSAndy Whitcroft $Type = qr{ 263c45dcabdSAndy Whitcroft $NonptrType 26465863862SAndy Whitcroft (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? 265c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 2668905a67cSAndy Whitcroft }x; 2678905a67cSAndy Whitcroft $Declare = qr{(?:$Storage\s+)?$Type}; 2688905a67cSAndy Whitcroft} 2698905a67cSAndy Whitcroftbuild_types(); 2706c72ffaaSAndy Whitcroft 2716c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 2720a920b5bSAndy Whitcroft 2734a0df2efSAndy Whitcroftmy @dep_includes = (); 2744a0df2efSAndy Whitcroftmy @dep_functions = (); 2756c72ffaaSAndy Whitcroftmy $removal = "Documentation/feature-removal-schedule.txt"; 2766c72ffaaSAndy Whitcroftif ($tree && -f "$root/$removal") { 27721caa13cSAndy Whitcroft open(my $REMOVE, '<', "$root/$removal") || 2786c72ffaaSAndy Whitcroft die "$P: $removal: open failed - $!\n"; 27921caa13cSAndy Whitcroft while (<$REMOVE>) { 280f0a594c1SAndy Whitcroft if (/^Check:\s+(.*\S)/) { 281f0a594c1SAndy Whitcroft for my $entry (split(/[, ]+/, $1)) { 282f0a594c1SAndy Whitcroft if ($entry =~ m@include/(.*)@) { 2834a0df2efSAndy Whitcroft push(@dep_includes, $1); 2844a0df2efSAndy Whitcroft 285f0a594c1SAndy Whitcroft } elsif ($entry !~ m@/@) { 286f0a594c1SAndy Whitcroft push(@dep_functions, $entry); 287f0a594c1SAndy Whitcroft } 2884a0df2efSAndy Whitcroft } 2890a920b5bSAndy Whitcroft } 2900a920b5bSAndy Whitcroft } 29121caa13cSAndy Whitcroft close($REMOVE); 2920a920b5bSAndy Whitcroft} 2930a920b5bSAndy Whitcroft 29400df344fSAndy Whitcroftmy @rawlines = (); 295c2fdda0dSAndy Whitcroftmy @lines = (); 296c2fdda0dSAndy Whitcroftmy $vname; 2976c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 29821caa13cSAndy Whitcroft my $FILE; 2996c72ffaaSAndy Whitcroft if ($file) { 30021caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 3016c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 30221caa13cSAndy Whitcroft } elsif ($filename eq '-') { 30321caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 3046c72ffaaSAndy Whitcroft } else { 30521caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 3066c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 3076c72ffaaSAndy Whitcroft } 308c2fdda0dSAndy Whitcroft if ($filename eq '-') { 309c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 310c2fdda0dSAndy Whitcroft } else { 311c2fdda0dSAndy Whitcroft $vname = $filename; 312c2fdda0dSAndy Whitcroft } 31321caa13cSAndy Whitcroft while (<$FILE>) { 3140a920b5bSAndy Whitcroft chomp; 31500df344fSAndy Whitcroft push(@rawlines, $_); 3166c72ffaaSAndy Whitcroft } 31721caa13cSAndy Whitcroft close($FILE); 318c2fdda0dSAndy Whitcroft if (!process($filename)) { 3190a920b5bSAndy Whitcroft $exit = 1; 3200a920b5bSAndy Whitcroft } 32100df344fSAndy Whitcroft @rawlines = (); 32213214adfSAndy Whitcroft @lines = (); 3230a920b5bSAndy Whitcroft} 3240a920b5bSAndy Whitcroft 3250a920b5bSAndy Whitcroftexit($exit); 3260a920b5bSAndy Whitcroft 3270a920b5bSAndy Whitcroftsub top_of_kernel_tree { 3286c72ffaaSAndy Whitcroft my ($root) = @_; 3296c72ffaaSAndy Whitcroft 3306c72ffaaSAndy Whitcroft my @tree_check = ( 3316c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 3326c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 3336c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 3346c72ffaaSAndy Whitcroft ); 3356c72ffaaSAndy Whitcroft 3366c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 3376c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 3380a920b5bSAndy Whitcroft return 0; 3390a920b5bSAndy Whitcroft } 3406c72ffaaSAndy Whitcroft } 3416c72ffaaSAndy Whitcroft return 1; 3426c72ffaaSAndy Whitcroft} 3430a920b5bSAndy Whitcroft 3440a920b5bSAndy Whitcroftsub expand_tabs { 3450a920b5bSAndy Whitcroft my ($str) = @_; 3460a920b5bSAndy Whitcroft 3470a920b5bSAndy Whitcroft my $res = ''; 3480a920b5bSAndy Whitcroft my $n = 0; 3490a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 3500a920b5bSAndy Whitcroft if ($c eq "\t") { 3510a920b5bSAndy Whitcroft $res .= ' '; 3520a920b5bSAndy Whitcroft $n++; 3530a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 3540a920b5bSAndy Whitcroft $res .= ' '; 3550a920b5bSAndy Whitcroft } 3560a920b5bSAndy Whitcroft next; 3570a920b5bSAndy Whitcroft } 3580a920b5bSAndy Whitcroft $res .= $c; 3590a920b5bSAndy Whitcroft $n++; 3600a920b5bSAndy Whitcroft } 3610a920b5bSAndy Whitcroft 3620a920b5bSAndy Whitcroft return $res; 3630a920b5bSAndy Whitcroft} 3646c72ffaaSAndy Whitcroftsub copy_spacing { 365773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 3666c72ffaaSAndy Whitcroft return $res; 3676c72ffaaSAndy Whitcroft} 3680a920b5bSAndy Whitcroft 3694a0df2efSAndy Whitcroftsub line_stats { 3704a0df2efSAndy Whitcroft my ($line) = @_; 3714a0df2efSAndy Whitcroft 3724a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 3734a0df2efSAndy Whitcroft $line =~ s/^.//; 3744a0df2efSAndy Whitcroft $line = expand_tabs($line); 3754a0df2efSAndy Whitcroft 3764a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 3774a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 3784a0df2efSAndy Whitcroft 3794a0df2efSAndy Whitcroft return (length($line), length($white)); 3804a0df2efSAndy Whitcroft} 3814a0df2efSAndy Whitcroft 382773647a0SAndy Whitcroftmy $sanitise_quote = ''; 383773647a0SAndy Whitcroft 384773647a0SAndy Whitcroftsub sanitise_line_reset { 385773647a0SAndy Whitcroft my ($in_comment) = @_; 386773647a0SAndy Whitcroft 387773647a0SAndy Whitcroft if ($in_comment) { 388773647a0SAndy Whitcroft $sanitise_quote = '*/'; 389773647a0SAndy Whitcroft } else { 390773647a0SAndy Whitcroft $sanitise_quote = ''; 391773647a0SAndy Whitcroft } 392773647a0SAndy Whitcroft} 39300df344fSAndy Whitcroftsub sanitise_line { 39400df344fSAndy Whitcroft my ($line) = @_; 39500df344fSAndy Whitcroft 39600df344fSAndy Whitcroft my $res = ''; 39700df344fSAndy Whitcroft my $l = ''; 39800df344fSAndy Whitcroft 399c2fdda0dSAndy Whitcroft my $qlen = 0; 400773647a0SAndy Whitcroft my $off = 0; 401773647a0SAndy Whitcroft my $c; 40200df344fSAndy Whitcroft 403773647a0SAndy Whitcroft # Always copy over the diff marker. 404773647a0SAndy Whitcroft $res = substr($line, 0, 1); 405773647a0SAndy Whitcroft 406773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 407773647a0SAndy Whitcroft $c = substr($line, $off, 1); 408773647a0SAndy Whitcroft 409773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 410773647a0SAndy Whitcroft # and end, all to $;. 411773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 412773647a0SAndy Whitcroft $sanitise_quote = '*/'; 413773647a0SAndy Whitcroft 414773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 415773647a0SAndy Whitcroft $off++; 41600df344fSAndy Whitcroft next; 417773647a0SAndy Whitcroft } 41881bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 419773647a0SAndy Whitcroft $sanitise_quote = ''; 420773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 421773647a0SAndy Whitcroft $off++; 422773647a0SAndy Whitcroft next; 423773647a0SAndy Whitcroft } 424113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 425113f04a8SDaniel Walker $sanitise_quote = '//'; 426113f04a8SDaniel Walker 427113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 428113f04a8SDaniel Walker $off++; 429113f04a8SDaniel Walker next; 430113f04a8SDaniel Walker } 431773647a0SAndy Whitcroft 432773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 433773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 434773647a0SAndy Whitcroft $c eq "\\") { 435773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 436773647a0SAndy Whitcroft $off++; 437773647a0SAndy Whitcroft next; 438773647a0SAndy Whitcroft } 439773647a0SAndy Whitcroft # Regular quotes. 440773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 441773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 442773647a0SAndy Whitcroft $sanitise_quote = $c; 443773647a0SAndy Whitcroft 444773647a0SAndy Whitcroft substr($res, $off, 1, $c); 445773647a0SAndy Whitcroft next; 446773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 447773647a0SAndy Whitcroft $sanitise_quote = ''; 44800df344fSAndy Whitcroft } 44900df344fSAndy Whitcroft } 450773647a0SAndy Whitcroft 451fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 452773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 453773647a0SAndy Whitcroft substr($res, $off, 1, $;); 454113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 455113f04a8SDaniel Walker substr($res, $off, 1, $;); 456773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 457773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 45800df344fSAndy Whitcroft } else { 459773647a0SAndy Whitcroft substr($res, $off, 1, $c); 46000df344fSAndy Whitcroft } 461c2fdda0dSAndy Whitcroft } 462c2fdda0dSAndy Whitcroft 463113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 464113f04a8SDaniel Walker $sanitise_quote = ''; 465113f04a8SDaniel Walker } 466113f04a8SDaniel Walker 467c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 468c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 469c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 470c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 471c2fdda0dSAndy Whitcroft 472c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 473c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 474c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 475c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 476c2fdda0dSAndy Whitcroft } 477c2fdda0dSAndy Whitcroft 47800df344fSAndy Whitcroft return $res; 47900df344fSAndy Whitcroft} 48000df344fSAndy Whitcroft 4818905a67cSAndy Whitcroftsub ctx_statement_block { 4828905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 4838905a67cSAndy Whitcroft my $line = $linenr - 1; 4848905a67cSAndy Whitcroft my $blk = ''; 4858905a67cSAndy Whitcroft my $soff = $off; 4868905a67cSAndy Whitcroft my $coff = $off - 1; 487773647a0SAndy Whitcroft my $coff_set = 0; 4888905a67cSAndy Whitcroft 48913214adfSAndy Whitcroft my $loff = 0; 49013214adfSAndy Whitcroft 4918905a67cSAndy Whitcroft my $type = ''; 4928905a67cSAndy Whitcroft my $level = 0; 493a2750645SAndy Whitcroft my @stack = (); 494cf655043SAndy Whitcroft my $p; 4958905a67cSAndy Whitcroft my $c; 4968905a67cSAndy Whitcroft my $len = 0; 49713214adfSAndy Whitcroft 49813214adfSAndy Whitcroft my $remainder; 4998905a67cSAndy Whitcroft while (1) { 500a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 501a2750645SAndy Whitcroft 502773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 5038905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 5048905a67cSAndy Whitcroft # context. 5058905a67cSAndy Whitcroft if ($off >= $len) { 5068905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 507dea33496SAndy Whitcroft last if (!defined $lines[$line]); 508c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 5098905a67cSAndy Whitcroft $remain--; 51013214adfSAndy Whitcroft $loff = $len; 511c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 5128905a67cSAndy Whitcroft $len = length($blk); 5138905a67cSAndy Whitcroft $line++; 5148905a67cSAndy Whitcroft last; 5158905a67cSAndy Whitcroft } 5168905a67cSAndy Whitcroft # Bail if there is no further context. 5178905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 51813214adfSAndy Whitcroft if ($off >= $len) { 5198905a67cSAndy Whitcroft last; 5208905a67cSAndy Whitcroft } 5218905a67cSAndy Whitcroft } 522cf655043SAndy Whitcroft $p = $c; 5238905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 52413214adfSAndy Whitcroft $remainder = substr($blk, $off); 5258905a67cSAndy Whitcroft 526773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 5274635f4fbSAndy Whitcroft 5284635f4fbSAndy Whitcroft # Handle nested #if/#else. 5294635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 5304635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 5314635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 5324635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 5334635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 5344635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 5354635f4fbSAndy Whitcroft } 5364635f4fbSAndy Whitcroft 5378905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 5388905a67cSAndy Whitcroft # outermost level. 5398905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 5408905a67cSAndy Whitcroft last; 5418905a67cSAndy Whitcroft } 5428905a67cSAndy Whitcroft 54313214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 544773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 545773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 546773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 547773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 548773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 549773647a0SAndy Whitcroft $coff_set = 1; 550773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 551773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 55213214adfSAndy Whitcroft } 55313214adfSAndy Whitcroft 5548905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 5558905a67cSAndy Whitcroft $level++; 5568905a67cSAndy Whitcroft $type = '('; 5578905a67cSAndy Whitcroft } 5588905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 5598905a67cSAndy Whitcroft $level--; 5608905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 5618905a67cSAndy Whitcroft 5628905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 5638905a67cSAndy Whitcroft $coff = $off; 564773647a0SAndy Whitcroft $coff_set = 1; 565773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 5668905a67cSAndy Whitcroft } 5678905a67cSAndy Whitcroft } 5688905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 5698905a67cSAndy Whitcroft $level++; 5708905a67cSAndy Whitcroft $type = '{'; 5718905a67cSAndy Whitcroft } 5728905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 5738905a67cSAndy Whitcroft $level--; 5748905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 5758905a67cSAndy Whitcroft 5768905a67cSAndy Whitcroft if ($level == 0) { 577b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 578b998e001SPatrick Pannuto $off++; 579b998e001SPatrick Pannuto } 5808905a67cSAndy Whitcroft last; 5818905a67cSAndy Whitcroft } 5828905a67cSAndy Whitcroft } 5838905a67cSAndy Whitcroft $off++; 5848905a67cSAndy Whitcroft } 585a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 58613214adfSAndy Whitcroft if ($off == $len) { 587a3bb97a7SAndy Whitcroft $loff = $len + 1; 58813214adfSAndy Whitcroft $line++; 58913214adfSAndy Whitcroft $remain--; 59013214adfSAndy Whitcroft } 5918905a67cSAndy Whitcroft 5928905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 5938905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 5948905a67cSAndy Whitcroft 5958905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 5968905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 5978905a67cSAndy Whitcroft 598773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 59913214adfSAndy Whitcroft 60013214adfSAndy Whitcroft return ($statement, $condition, 60113214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 60213214adfSAndy Whitcroft} 60313214adfSAndy Whitcroft 604cf655043SAndy Whitcroftsub statement_lines { 605cf655043SAndy Whitcroft my ($stmt) = @_; 606cf655043SAndy Whitcroft 607cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 608cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 609cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 610cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 611cf655043SAndy Whitcroft 612cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 613cf655043SAndy Whitcroft 614cf655043SAndy Whitcroft return $#stmt_lines + 2; 615cf655043SAndy Whitcroft} 616cf655043SAndy Whitcroft 617cf655043SAndy Whitcroftsub statement_rawlines { 618cf655043SAndy Whitcroft my ($stmt) = @_; 619cf655043SAndy Whitcroft 620cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 621cf655043SAndy Whitcroft 622cf655043SAndy Whitcroft return $#stmt_lines + 2; 623cf655043SAndy Whitcroft} 624cf655043SAndy Whitcroft 625cf655043SAndy Whitcroftsub statement_block_size { 626cf655043SAndy Whitcroft my ($stmt) = @_; 627cf655043SAndy Whitcroft 628cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 629cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 630cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 631cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 632cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 633cf655043SAndy Whitcroft 634cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 635cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 636cf655043SAndy Whitcroft 637cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 638cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 639cf655043SAndy Whitcroft 640cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 641cf655043SAndy Whitcroft return $stmt_lines; 642cf655043SAndy Whitcroft } else { 643cf655043SAndy Whitcroft return $stmt_statements; 644cf655043SAndy Whitcroft } 645cf655043SAndy Whitcroft} 646cf655043SAndy Whitcroft 64713214adfSAndy Whitcroftsub ctx_statement_full { 64813214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 64913214adfSAndy Whitcroft my ($statement, $condition, $level); 65013214adfSAndy Whitcroft 65113214adfSAndy Whitcroft my (@chunks); 65213214adfSAndy Whitcroft 653cf655043SAndy Whitcroft # Grab the first conditional/block pair. 65413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 65513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 656773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 65713214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 658cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 659cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 660cf655043SAndy Whitcroft } 661cf655043SAndy Whitcroft 662cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 663cf655043SAndy Whitcroft # could continue the statement. 664cf655043SAndy Whitcroft for (;;) { 66513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 66613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 667cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 668773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 669cf655043SAndy Whitcroft #print "C: push\n"; 670cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 67113214adfSAndy Whitcroft } 67213214adfSAndy Whitcroft 67313214adfSAndy Whitcroft return ($level, $linenr, @chunks); 6748905a67cSAndy Whitcroft} 6758905a67cSAndy Whitcroft 6764a0df2efSAndy Whitcroftsub ctx_block_get { 677f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 6784a0df2efSAndy Whitcroft my $line; 6794a0df2efSAndy Whitcroft my $start = $linenr - 1; 6804a0df2efSAndy Whitcroft my $blk = ''; 6814a0df2efSAndy Whitcroft my @o; 6824a0df2efSAndy Whitcroft my @c; 6834a0df2efSAndy Whitcroft my @res = (); 6844a0df2efSAndy Whitcroft 685f0a594c1SAndy Whitcroft my $level = 0; 6864635f4fbSAndy Whitcroft my @stack = ($level); 68700df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 68800df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 68900df344fSAndy Whitcroft $remain--; 69000df344fSAndy Whitcroft 69100df344fSAndy Whitcroft $blk .= $rawlines[$line]; 6924635f4fbSAndy Whitcroft 6934635f4fbSAndy Whitcroft # Handle nested #if/#else. 69401464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 6954635f4fbSAndy Whitcroft push(@stack, $level); 69601464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 6974635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 69801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 6994635f4fbSAndy Whitcroft $level = pop(@stack); 7004635f4fbSAndy Whitcroft } 7014635f4fbSAndy Whitcroft 70201464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 703f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 704f0a594c1SAndy Whitcroft if ($off > 0) { 705f0a594c1SAndy Whitcroft $off--; 706f0a594c1SAndy Whitcroft next; 707f0a594c1SAndy Whitcroft } 7084a0df2efSAndy Whitcroft 709f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 710f0a594c1SAndy Whitcroft $level--; 711f0a594c1SAndy Whitcroft last if ($level == 0); 712f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 713f0a594c1SAndy Whitcroft $level++; 714f0a594c1SAndy Whitcroft } 715f0a594c1SAndy Whitcroft } 7164a0df2efSAndy Whitcroft 717f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 71800df344fSAndy Whitcroft push(@res, $rawlines[$line]); 7194a0df2efSAndy Whitcroft } 7204a0df2efSAndy Whitcroft 721f0a594c1SAndy Whitcroft last if ($level == 0); 7224a0df2efSAndy Whitcroft } 7234a0df2efSAndy Whitcroft 724f0a594c1SAndy Whitcroft return ($level, @res); 7254a0df2efSAndy Whitcroft} 7264a0df2efSAndy Whitcroftsub ctx_block_outer { 7274a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 7284a0df2efSAndy Whitcroft 729f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 730f0a594c1SAndy Whitcroft return @r; 7314a0df2efSAndy Whitcroft} 7324a0df2efSAndy Whitcroftsub ctx_block { 7334a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 7344a0df2efSAndy Whitcroft 735f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 736f0a594c1SAndy Whitcroft return @r; 737653d4876SAndy Whitcroft} 738653d4876SAndy Whitcroftsub ctx_statement { 739f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 740f0a594c1SAndy Whitcroft 741f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 742f0a594c1SAndy Whitcroft return @r; 743f0a594c1SAndy Whitcroft} 744f0a594c1SAndy Whitcroftsub ctx_block_level { 745653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 746653d4876SAndy Whitcroft 747f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 7484a0df2efSAndy Whitcroft} 7499c0ca6f9SAndy Whitcroftsub ctx_statement_level { 7509c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 7519c0ca6f9SAndy Whitcroft 7529c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 7539c0ca6f9SAndy Whitcroft} 7544a0df2efSAndy Whitcroft 7554a0df2efSAndy Whitcroftsub ctx_locate_comment { 7564a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7574a0df2efSAndy Whitcroft 7584a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 759beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 7604a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 7614a0df2efSAndy Whitcroft 7624a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 7634a0df2efSAndy Whitcroft # comment. 7644a0df2efSAndy Whitcroft my $in_comment = 0; 7654a0df2efSAndy Whitcroft $current_comment = ''; 7664a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 76700df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 76800df344fSAndy Whitcroft #warn " $line\n"; 7694a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 7704a0df2efSAndy Whitcroft $in_comment = 1; 7714a0df2efSAndy Whitcroft } 7724a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 7734a0df2efSAndy Whitcroft $in_comment = 1; 7744a0df2efSAndy Whitcroft } 7754a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 7764a0df2efSAndy Whitcroft $current_comment = ''; 7774a0df2efSAndy Whitcroft } 7784a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 7794a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 7804a0df2efSAndy Whitcroft $in_comment = 0; 7814a0df2efSAndy Whitcroft } 7824a0df2efSAndy Whitcroft } 7834a0df2efSAndy Whitcroft 7844a0df2efSAndy Whitcroft chomp($current_comment); 7854a0df2efSAndy Whitcroft return($current_comment); 7864a0df2efSAndy Whitcroft} 7874a0df2efSAndy Whitcroftsub ctx_has_comment { 7884a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 7894a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 7904a0df2efSAndy Whitcroft 79100df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 7924a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 7934a0df2efSAndy Whitcroft 7944a0df2efSAndy Whitcroft return ($cmt ne ''); 7954a0df2efSAndy Whitcroft} 7964a0df2efSAndy Whitcroft 7974d001e4dSAndy Whitcroftsub raw_line { 7984d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 7994d001e4dSAndy Whitcroft 8004d001e4dSAndy Whitcroft my $offset = $linenr - 1; 8014d001e4dSAndy Whitcroft $cnt++; 8024d001e4dSAndy Whitcroft 8034d001e4dSAndy Whitcroft my $line; 8044d001e4dSAndy Whitcroft while ($cnt) { 8054d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 8064d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 8074d001e4dSAndy Whitcroft $cnt--; 8084d001e4dSAndy Whitcroft } 8094d001e4dSAndy Whitcroft 8104d001e4dSAndy Whitcroft return $line; 8114d001e4dSAndy Whitcroft} 8124d001e4dSAndy Whitcroft 8130a920b5bSAndy Whitcroftsub cat_vet { 8140a920b5bSAndy Whitcroft my ($vet) = @_; 8159c0ca6f9SAndy Whitcroft my ($res, $coded); 8160a920b5bSAndy Whitcroft 8179c0ca6f9SAndy Whitcroft $res = ''; 8186c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 8196c72ffaaSAndy Whitcroft $res .= $1; 8206c72ffaaSAndy Whitcroft if ($2 ne '') { 8219c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 8226c72ffaaSAndy Whitcroft $res .= $coded; 8236c72ffaaSAndy Whitcroft } 8249c0ca6f9SAndy Whitcroft } 8259c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 8260a920b5bSAndy Whitcroft 8279c0ca6f9SAndy Whitcroft return $res; 8280a920b5bSAndy Whitcroft} 8290a920b5bSAndy Whitcroft 830c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 831cf655043SAndy Whitcroftmy $av_pending; 832c2fdda0dSAndy Whitcroftmy @av_paren_type; 8331f65f947SAndy Whitcroftmy $av_pend_colon; 834c2fdda0dSAndy Whitcroft 835c2fdda0dSAndy Whitcroftsub annotate_reset { 836c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 837cf655043SAndy Whitcroft $av_pending = '_'; 838cf655043SAndy Whitcroft @av_paren_type = ('E'); 8391f65f947SAndy Whitcroft $av_pend_colon = 'O'; 840c2fdda0dSAndy Whitcroft} 841c2fdda0dSAndy Whitcroft 8426c72ffaaSAndy Whitcroftsub annotate_values { 8436c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 8446c72ffaaSAndy Whitcroft 8456c72ffaaSAndy Whitcroft my $res; 8461f65f947SAndy Whitcroft my $var = '_' x length($stream); 8476c72ffaaSAndy Whitcroft my $cur = $stream; 8486c72ffaaSAndy Whitcroft 849c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 8506c72ffaaSAndy Whitcroft 8516c72ffaaSAndy Whitcroft while (length($cur)) { 852773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 853cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 854171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 8556c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 856c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 857c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 858cf655043SAndy Whitcroft $type = pop(@av_paren_type); 859c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 8606c72ffaaSAndy Whitcroft } 8616c72ffaaSAndy Whitcroft 862c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 8639446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 8649446ef56SAndy Whitcroft push(@av_paren_type, $type); 8659446ef56SAndy Whitcroft $type = 'C'; 8669446ef56SAndy Whitcroft 867e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 868c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 8696c72ffaaSAndy Whitcroft $type = 'T'; 8706c72ffaaSAndy Whitcroft 871389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 872389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 873389a2fe5SAndy Whitcroft $type = 'T'; 874389a2fe5SAndy Whitcroft 875c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 876171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 877c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 878171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 879171ae1a4SAndy Whitcroft if ($2 ne '') { 880cf655043SAndy Whitcroft $av_pending = 'N'; 881171ae1a4SAndy Whitcroft } 882171ae1a4SAndy Whitcroft $type = 'E'; 883171ae1a4SAndy Whitcroft 884c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 885171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 886171ae1a4SAndy Whitcroft $av_preprocessor = 1; 887171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 8886c72ffaaSAndy Whitcroft 889c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 890cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 891c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 892cf655043SAndy Whitcroft 893cf655043SAndy Whitcroft push(@av_paren_type, $type); 894cf655043SAndy Whitcroft push(@av_paren_type, $type); 895171ae1a4SAndy Whitcroft $type = 'E'; 896cf655043SAndy Whitcroft 897c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 898cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 899cf655043SAndy Whitcroft $av_preprocessor = 1; 900cf655043SAndy Whitcroft 901cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 902cf655043SAndy Whitcroft 903171ae1a4SAndy Whitcroft $type = 'E'; 904cf655043SAndy Whitcroft 905c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 906cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 907cf655043SAndy Whitcroft 908cf655043SAndy Whitcroft $av_preprocessor = 1; 909cf655043SAndy Whitcroft 910cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 911cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 912cf655043SAndy Whitcroft pop(@av_paren_type); 913cf655043SAndy Whitcroft push(@av_paren_type, $type); 914171ae1a4SAndy Whitcroft $type = 'E'; 9156c72ffaaSAndy Whitcroft 9166c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 917c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 9186c72ffaaSAndy Whitcroft 919171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 920171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 921171ae1a4SAndy Whitcroft $av_pending = $type; 922171ae1a4SAndy Whitcroft $type = 'N'; 923171ae1a4SAndy Whitcroft 9246c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 925c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 9266c72ffaaSAndy Whitcroft if (defined $2) { 927cf655043SAndy Whitcroft $av_pending = 'V'; 9286c72ffaaSAndy Whitcroft } 9296c72ffaaSAndy Whitcroft $type = 'N'; 9306c72ffaaSAndy Whitcroft 93114b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 932c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 93314b111c1SAndy Whitcroft $av_pending = 'E'; 9346c72ffaaSAndy Whitcroft $type = 'N'; 9356c72ffaaSAndy Whitcroft 9361f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 9371f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 9381f65f947SAndy Whitcroft $av_pend_colon = 'C'; 9391f65f947SAndy Whitcroft $type = 'N'; 9401f65f947SAndy Whitcroft 94114b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 942c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 9436c72ffaaSAndy Whitcroft $type = 'N'; 9446c72ffaaSAndy Whitcroft 9456c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 946c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 947cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 948cf655043SAndy Whitcroft $av_pending = '_'; 9496c72ffaaSAndy Whitcroft $type = 'N'; 9506c72ffaaSAndy Whitcroft 9516c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 952cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 953cf655043SAndy Whitcroft if ($new_type ne '_') { 954cf655043SAndy Whitcroft $type = $new_type; 955c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 956c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 9576c72ffaaSAndy Whitcroft } else { 958c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 9596c72ffaaSAndy Whitcroft } 9606c72ffaaSAndy Whitcroft 961c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 962c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 963c8cb2ca3SAndy Whitcroft $type = 'V'; 964cf655043SAndy Whitcroft $av_pending = 'V'; 9656c72ffaaSAndy Whitcroft 9668e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 9678e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 9681f65f947SAndy Whitcroft $av_pend_colon = 'B'; 9698e761b04SAndy Whitcroft } elsif ($type eq 'E') { 9708e761b04SAndy Whitcroft $av_pend_colon = 'L'; 9711f65f947SAndy Whitcroft } 9721f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 9731f65f947SAndy Whitcroft $type = 'V'; 9741f65f947SAndy Whitcroft 9756c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 976c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 9776c72ffaaSAndy Whitcroft $type = 'V'; 9786c72ffaaSAndy Whitcroft 9796c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 980c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 9816c72ffaaSAndy Whitcroft $type = 'N'; 9826c72ffaaSAndy Whitcroft 983cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 984c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 98513214adfSAndy Whitcroft $type = 'E'; 9861f65f947SAndy Whitcroft $av_pend_colon = 'O'; 98713214adfSAndy Whitcroft 9888e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 9898e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 9908e761b04SAndy Whitcroft $type = 'C'; 9918e761b04SAndy Whitcroft 9921f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 9931f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 9941f65f947SAndy Whitcroft $type = 'N'; 9951f65f947SAndy Whitcroft 9961f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 9971f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 9981f65f947SAndy Whitcroft 9991f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 10001f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 10011f65f947SAndy Whitcroft $type = 'E'; 10021f65f947SAndy Whitcroft } else { 10031f65f947SAndy Whitcroft $type = 'N'; 10041f65f947SAndy Whitcroft } 10051f65f947SAndy Whitcroft $av_pend_colon = 'O'; 10061f65f947SAndy Whitcroft 10078e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 100813214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 10096c72ffaaSAndy Whitcroft $type = 'N'; 10106c72ffaaSAndy Whitcroft 10110d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 101274048ed8SAndy Whitcroft my $variant; 101374048ed8SAndy Whitcroft 101474048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 101574048ed8SAndy Whitcroft if ($type eq 'V') { 101674048ed8SAndy Whitcroft $variant = 'B'; 101774048ed8SAndy Whitcroft } else { 101874048ed8SAndy Whitcroft $variant = 'U'; 101974048ed8SAndy Whitcroft } 102074048ed8SAndy Whitcroft 102174048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 102274048ed8SAndy Whitcroft $type = 'N'; 102374048ed8SAndy Whitcroft 10246c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1025c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 10266c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 10276c72ffaaSAndy Whitcroft $type = 'N'; 10286c72ffaaSAndy Whitcroft } 10296c72ffaaSAndy Whitcroft 10306c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1031c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 10326c72ffaaSAndy Whitcroft } 10336c72ffaaSAndy Whitcroft if (defined $1) { 10346c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 10356c72ffaaSAndy Whitcroft $res .= $type x length($1); 10366c72ffaaSAndy Whitcroft } 10376c72ffaaSAndy Whitcroft } 10386c72ffaaSAndy Whitcroft 10391f65f947SAndy Whitcroft return ($res, $var); 10406c72ffaaSAndy Whitcroft} 10416c72ffaaSAndy Whitcroft 10428905a67cSAndy Whitcroftsub possible { 104313214adfSAndy Whitcroft my ($possible, $line) = @_; 10449a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 10450776e594SAndy Whitcroft ^(?: 10460776e594SAndy Whitcroft $Modifier| 10470776e594SAndy Whitcroft $Storage| 10480776e594SAndy Whitcroft $Type| 10499a974fdbSAndy Whitcroft DEFINE_\S+ 10509a974fdbSAndy Whitcroft )$| 10519a974fdbSAndy Whitcroft ^(?: 10520776e594SAndy Whitcroft goto| 10530776e594SAndy Whitcroft return| 10540776e594SAndy Whitcroft case| 10550776e594SAndy Whitcroft else| 10560776e594SAndy Whitcroft asm|__asm__| 10570776e594SAndy Whitcroft do 10589a974fdbSAndy Whitcroft )(?:\s|$)| 10590776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 10609a974fdbSAndy Whitcroft )}x; 10619a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 10629a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1063c45dcabdSAndy Whitcroft # Check for modifiers. 1064c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1065c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1066c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1067c45dcabdSAndy Whitcroft 1068c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1069c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1070d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 10719a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1072d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1073d2506586SAndy Whitcroft push(@modifierList, $modifier); 1074d2506586SAndy Whitcroft } 10759a974fdbSAndy Whitcroft } 1076c45dcabdSAndy Whitcroft 1077c45dcabdSAndy Whitcroft } else { 107813214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 10798905a67cSAndy Whitcroft push(@typeList, $possible); 1080c45dcabdSAndy Whitcroft } 10818905a67cSAndy Whitcroft build_types(); 10820776e594SAndy Whitcroft } else { 10830776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 10848905a67cSAndy Whitcroft } 10858905a67cSAndy Whitcroft} 10868905a67cSAndy Whitcroft 10876c72ffaaSAndy Whitcroftmy $prefix = ''; 10886c72ffaaSAndy Whitcroft 1089f0a594c1SAndy Whitcroftsub report { 1090773647a0SAndy Whitcroft if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { 1091773647a0SAndy Whitcroft return 0; 1092773647a0SAndy Whitcroft } 10938905a67cSAndy Whitcroft my $line = $prefix . $_[0]; 10948905a67cSAndy Whitcroft 10958905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 10968905a67cSAndy Whitcroft 109713214adfSAndy Whitcroft push(our @report, $line); 1098773647a0SAndy Whitcroft 1099773647a0SAndy Whitcroft return 1; 1100f0a594c1SAndy Whitcroft} 1101f0a594c1SAndy Whitcroftsub report_dump { 110213214adfSAndy Whitcroft our @report; 1103f0a594c1SAndy Whitcroft} 1104de7d4f0eSAndy Whitcroftsub ERROR { 1105773647a0SAndy Whitcroft if (report("ERROR: $_[0]\n")) { 1106de7d4f0eSAndy Whitcroft our $clean = 0; 11076c72ffaaSAndy Whitcroft our $cnt_error++; 1108de7d4f0eSAndy Whitcroft } 1109773647a0SAndy Whitcroft} 1110de7d4f0eSAndy Whitcroftsub WARN { 1111773647a0SAndy Whitcroft if (report("WARNING: $_[0]\n")) { 1112de7d4f0eSAndy Whitcroft our $clean = 0; 11136c72ffaaSAndy Whitcroft our $cnt_warn++; 1114de7d4f0eSAndy Whitcroft } 1115773647a0SAndy Whitcroft} 1116de7d4f0eSAndy Whitcroftsub CHK { 1117773647a0SAndy Whitcroft if ($check && report("CHECK: $_[0]\n")) { 1118de7d4f0eSAndy Whitcroft our $clean = 0; 11196c72ffaaSAndy Whitcroft our $cnt_chk++; 11206c72ffaaSAndy Whitcroft } 1121de7d4f0eSAndy Whitcroft} 1122de7d4f0eSAndy Whitcroft 11236ecd9674SAndy Whitcroftsub check_absolute_file { 11246ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 11256ecd9674SAndy Whitcroft my $file = $absolute; 11266ecd9674SAndy Whitcroft 11276ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 11286ecd9674SAndy Whitcroft 11296ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 11306ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 11316ecd9674SAndy Whitcroft if (-f "$root/$file") { 11326ecd9674SAndy Whitcroft ##print "file<$file>\n"; 11336ecd9674SAndy Whitcroft last; 11346ecd9674SAndy Whitcroft } 11356ecd9674SAndy Whitcroft } 11366ecd9674SAndy Whitcroft if (! -f _) { 11376ecd9674SAndy Whitcroft return 0; 11386ecd9674SAndy Whitcroft } 11396ecd9674SAndy Whitcroft 11406ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 11416ecd9674SAndy Whitcroft my $prefix = $absolute; 11426ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 11436ecd9674SAndy Whitcroft 11446ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 11456ecd9674SAndy Whitcroft if ($prefix ne ".../") { 11466ecd9674SAndy Whitcroft WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); 11476ecd9674SAndy Whitcroft } 11486ecd9674SAndy Whitcroft} 11496ecd9674SAndy Whitcroft 11500a920b5bSAndy Whitcroftsub process { 11510a920b5bSAndy Whitcroft my $filename = shift; 11520a920b5bSAndy Whitcroft 11530a920b5bSAndy Whitcroft my $linenr=0; 11540a920b5bSAndy Whitcroft my $prevline=""; 1155c2fdda0dSAndy Whitcroft my $prevrawline=""; 11560a920b5bSAndy Whitcroft my $stashline=""; 1157c2fdda0dSAndy Whitcroft my $stashrawline=""; 11580a920b5bSAndy Whitcroft 11594a0df2efSAndy Whitcroft my $length; 11600a920b5bSAndy Whitcroft my $indent; 11610a920b5bSAndy Whitcroft my $previndent=0; 11620a920b5bSAndy Whitcroft my $stashindent=0; 11630a920b5bSAndy Whitcroft 1164de7d4f0eSAndy Whitcroft our $clean = 1; 11650a920b5bSAndy Whitcroft my $signoff = 0; 11660a920b5bSAndy Whitcroft my $is_patch = 0; 11670a920b5bSAndy Whitcroft 116813214adfSAndy Whitcroft our @report = (); 11696c72ffaaSAndy Whitcroft our $cnt_lines = 0; 11706c72ffaaSAndy Whitcroft our $cnt_error = 0; 11716c72ffaaSAndy Whitcroft our $cnt_warn = 0; 11726c72ffaaSAndy Whitcroft our $cnt_chk = 0; 11736c72ffaaSAndy Whitcroft 11740a920b5bSAndy Whitcroft # Trace the real file/line as we go. 11750a920b5bSAndy Whitcroft my $realfile = ''; 11760a920b5bSAndy Whitcroft my $realline = 0; 11770a920b5bSAndy Whitcroft my $realcnt = 0; 11780a920b5bSAndy Whitcroft my $here = ''; 11790a920b5bSAndy Whitcroft my $in_comment = 0; 1180c2fdda0dSAndy Whitcroft my $comment_edge = 0; 11810a920b5bSAndy Whitcroft my $first_line = 0; 11821e855726SWolfram Sang my $p1_prefix = ''; 11830a920b5bSAndy Whitcroft 118413214adfSAndy Whitcroft my $prev_values = 'E'; 118513214adfSAndy Whitcroft 118613214adfSAndy Whitcroft # suppression flags 1187773647a0SAndy Whitcroft my %suppress_ifbraces; 1188170d3a22SAndy Whitcroft my %suppress_whiletrailers; 11892b474a1aSAndy Whitcroft my %suppress_export; 1190653d4876SAndy Whitcroft 1191c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1192de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1193c2fdda0dSAndy Whitcroft # 1194de7d4f0eSAndy Whitcroft my @setup_docs = (); 1195de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1196773647a0SAndy Whitcroft 1197773647a0SAndy Whitcroft sanitise_line_reset(); 1198c2fdda0dSAndy Whitcroft my $line; 1199c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1200773647a0SAndy Whitcroft $linenr++; 1201773647a0SAndy Whitcroft $line = $rawline; 1202c2fdda0dSAndy Whitcroft 1203773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1204de7d4f0eSAndy Whitcroft $setup_docs = 0; 1205de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1206de7d4f0eSAndy Whitcroft $setup_docs = 1; 1207de7d4f0eSAndy Whitcroft } 1208773647a0SAndy Whitcroft #next; 1209de7d4f0eSAndy Whitcroft } 1210773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1211773647a0SAndy Whitcroft $realline=$1-1; 1212773647a0SAndy Whitcroft if (defined $2) { 1213773647a0SAndy Whitcroft $realcnt=$3+1; 1214773647a0SAndy Whitcroft } else { 1215773647a0SAndy Whitcroft $realcnt=1+1; 1216773647a0SAndy Whitcroft } 1217c45dcabdSAndy Whitcroft $in_comment = 0; 1218773647a0SAndy Whitcroft 1219773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1220773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1221773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1222773647a0SAndy Whitcroft # at context start. 1223773647a0SAndy Whitcroft my $edge; 122401fa9147SAndy Whitcroft my $cnt = $realcnt; 122501fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 122601fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 122701fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 122801fa9147SAndy Whitcroft $cnt--; 122901fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1230721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1231fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1232fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1233fae17daeSAndy Whitcroft ($edge) = $1; 1234fae17daeSAndy Whitcroft last; 1235fae17daeSAndy Whitcroft } 1236773647a0SAndy Whitcroft } 1237773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1238773647a0SAndy Whitcroft $in_comment = 1; 1239773647a0SAndy Whitcroft } 1240773647a0SAndy Whitcroft 1241773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1242773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1243773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1244773647a0SAndy Whitcroft if (!defined $edge && 124583242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1246773647a0SAndy Whitcroft { 1247773647a0SAndy Whitcroft $in_comment = 1; 1248773647a0SAndy Whitcroft } 1249773647a0SAndy Whitcroft 1250773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1251773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1252773647a0SAndy Whitcroft 1253171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1254773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1255171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1256773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1257773647a0SAndy Whitcroft } 1258773647a0SAndy Whitcroft push(@lines, $line); 1259773647a0SAndy Whitcroft 1260773647a0SAndy Whitcroft if ($realcnt > 1) { 1261773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1262773647a0SAndy Whitcroft } else { 1263773647a0SAndy Whitcroft $realcnt = 0; 1264773647a0SAndy Whitcroft } 1265773647a0SAndy Whitcroft 1266773647a0SAndy Whitcroft #print "==>$rawline\n"; 1267773647a0SAndy Whitcroft #print "-->$line\n"; 1268de7d4f0eSAndy Whitcroft 1269de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1270de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1271de7d4f0eSAndy Whitcroft } 1272de7d4f0eSAndy Whitcroft } 1273de7d4f0eSAndy Whitcroft 12746c72ffaaSAndy Whitcroft $prefix = ''; 12756c72ffaaSAndy Whitcroft 1276773647a0SAndy Whitcroft $realcnt = 0; 1277773647a0SAndy Whitcroft $linenr = 0; 12780a920b5bSAndy Whitcroft foreach my $line (@lines) { 12790a920b5bSAndy Whitcroft $linenr++; 12800a920b5bSAndy Whitcroft 1281c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 12826c72ffaaSAndy Whitcroft 12830a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 12846c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 12850a920b5bSAndy Whitcroft $is_patch = 1; 12864a0df2efSAndy Whitcroft $first_line = $linenr + 1; 12870a920b5bSAndy Whitcroft $realline=$1-1; 12880a920b5bSAndy Whitcroft if (defined $2) { 12890a920b5bSAndy Whitcroft $realcnt=$3+1; 12900a920b5bSAndy Whitcroft } else { 12910a920b5bSAndy Whitcroft $realcnt=1+1; 12920a920b5bSAndy Whitcroft } 1293c2fdda0dSAndy Whitcroft annotate_reset(); 129413214adfSAndy Whitcroft $prev_values = 'E'; 129513214adfSAndy Whitcroft 1296773647a0SAndy Whitcroft %suppress_ifbraces = (); 1297170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 12982b474a1aSAndy Whitcroft %suppress_export = (); 12990a920b5bSAndy Whitcroft next; 13000a920b5bSAndy Whitcroft 13014a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 13024a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 13034a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 1304773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 13050a920b5bSAndy Whitcroft $realline++; 1306d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 13070a920b5bSAndy Whitcroft 13084a0df2efSAndy Whitcroft # Measure the line length and indent. 1309c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 13100a920b5bSAndy Whitcroft 13110a920b5bSAndy Whitcroft # Track the previous line. 13120a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 13130a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 1314c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 1315c2fdda0dSAndy Whitcroft 1316773647a0SAndy Whitcroft #warn "line<$line>\n"; 13176c72ffaaSAndy Whitcroft 1318d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 1319d8aaf121SAndy Whitcroft $realcnt--; 13200a920b5bSAndy Whitcroft } 13210a920b5bSAndy Whitcroft 1322cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 1323cc77cdcaSAndy Whitcroft 13240a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 1325773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 1326773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 1327773647a0SAndy Whitcroft 13286c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 13296c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 1330773647a0SAndy Whitcroft 1331773647a0SAndy Whitcroft # extract the filename as it passes 13323bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 13333bf9a009SRabin Vincent $realfile = $1; 13343bf9a009SRabin Vincent $realfile =~ s@^([^/]*)/@@; 13353bf9a009SRabin Vincent 13363bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1337773647a0SAndy Whitcroft $realfile = $1; 13381e855726SWolfram Sang $realfile =~ s@^([^/]*)/@@; 13391e855726SWolfram Sang 13401e855726SWolfram Sang $p1_prefix = $1; 1341e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 1342e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 13431e855726SWolfram Sang WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 13441e855726SWolfram Sang } 1345773647a0SAndy Whitcroft 1346c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 1347773647a0SAndy Whitcroft ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 1348773647a0SAndy Whitcroft } 1349773647a0SAndy Whitcroft next; 1350773647a0SAndy Whitcroft } 1351773647a0SAndy Whitcroft 1352389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 13530a920b5bSAndy Whitcroft 1354c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 1355c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 1356c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 13570a920b5bSAndy Whitcroft 13586c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 13596c72ffaaSAndy Whitcroft 13603bf9a009SRabin Vincent# Check for incorrect file permissions 13613bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 13623bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 13633bf9a009SRabin Vincent if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) { 13643bf9a009SRabin Vincent ERROR("do not set execute permissions for source files\n" . $permhere); 13653bf9a009SRabin Vincent } 13663bf9a009SRabin Vincent } 13673bf9a009SRabin Vincent 13680a920b5bSAndy Whitcroft#check the patch for a signoff: 1369d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 13704a0df2efSAndy Whitcroft # This is a signoff, if ugly, so do not double report. 13714a0df2efSAndy Whitcroft $signoff++; 13720a920b5bSAndy Whitcroft if (!($line =~ /^\s*Signed-off-by:/)) { 1373de7d4f0eSAndy Whitcroft WARN("Signed-off-by: is the preferred form\n" . 1374de7d4f0eSAndy Whitcroft $herecurr); 13750a920b5bSAndy Whitcroft } 13760a920b5bSAndy Whitcroft if ($line =~ /^\s*signed-off-by:\S/i) { 1377773647a0SAndy Whitcroft WARN("space required after Signed-off-by:\n" . 1378de7d4f0eSAndy Whitcroft $herecurr); 13790a920b5bSAndy Whitcroft } 13800a920b5bSAndy Whitcroft } 13810a920b5bSAndy Whitcroft 138200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 13838905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 1384de7d4f0eSAndy Whitcroft ERROR("patch seems to be corrupt (line wrapped?)\n" . 13856c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 1386de7d4f0eSAndy Whitcroft } 1387de7d4f0eSAndy Whitcroft 13886ecd9674SAndy Whitcroft# Check for absolute kernel paths. 13896ecd9674SAndy Whitcroft if ($tree) { 13906ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 13916ecd9674SAndy Whitcroft my $file = $1; 13926ecd9674SAndy Whitcroft 13936ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 13946ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 13956ecd9674SAndy Whitcroft # 13966ecd9674SAndy Whitcroft } else { 13976ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 13986ecd9674SAndy Whitcroft } 13996ecd9674SAndy Whitcroft } 14006ecd9674SAndy Whitcroft } 14016ecd9674SAndy Whitcroft 1402de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 1403de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 1404171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 1405171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 1406171ae1a4SAndy Whitcroft 1407171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 1408171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 1409171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 1410171ae1a4SAndy Whitcroft 1411171ae1a4SAndy Whitcroft ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 141200df344fSAndy Whitcroft } 14130a920b5bSAndy Whitcroft 141430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 141530670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 141600df344fSAndy Whitcroft 14170a920b5bSAndy Whitcroft#trailing whitespace 14189c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 1419c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 14209c0ca6f9SAndy Whitcroft ERROR("DOS line endings\n" . $herevet); 14219c0ca6f9SAndy Whitcroft 1422c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1423c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1424de7d4f0eSAndy Whitcroft ERROR("trailing whitespace\n" . $herevet); 1425d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 14260a920b5bSAndy Whitcroft } 14275368df20SAndy Whitcroft 14283354957aSAndi Kleen# check for Kconfig help text having a real description 14299fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 14309fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 14313354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 14329fe287d7SAndy Whitcroft $line =~ /\+\s*(?:---)?help(?:---)?$/) { 14333354957aSAndi Kleen my $length = 0; 14349fe287d7SAndy Whitcroft my $cnt = $realcnt; 14359fe287d7SAndy Whitcroft my $ln = $linenr + 1; 14369fe287d7SAndy Whitcroft my $f; 14379fe287d7SAndy Whitcroft my $is_end = 0; 14389fe287d7SAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1]) { 14399fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 14409fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 14419fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 14429fe287d7SAndy Whitcroft $ln++; 14439fe287d7SAndy Whitcroft 14449fe287d7SAndy Whitcroft next if ($f =~ /^-/); 14459fe287d7SAndy Whitcroft $f =~ s/^.//; 14463354957aSAndi Kleen $f =~ s/#.*//; 14473354957aSAndi Kleen $f =~ s/^\s+//; 14483354957aSAndi Kleen next if ($f =~ /^$/); 14499fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 14509fe287d7SAndy Whitcroft $is_end = 1; 14519fe287d7SAndy Whitcroft last; 14529fe287d7SAndy Whitcroft } 14533354957aSAndi Kleen $length++; 14543354957aSAndi Kleen } 14559fe287d7SAndy Whitcroft WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4); 14569fe287d7SAndy Whitcroft #print "is_end<$is_end> length<$length>\n"; 14573354957aSAndi Kleen } 14583354957aSAndi Kleen 14595368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 14605368df20SAndy Whitcroft next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 14615368df20SAndy Whitcroft 14620a920b5bSAndy Whitcroft#80 column limit 1463c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1464f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 14658bbea968SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ || 14668bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1467f4c014c0SAndy Whitcroft $length > 80) 1468c45dcabdSAndy Whitcroft { 1469de7d4f0eSAndy Whitcroft WARN("line over 80 characters\n" . $herecurr); 14700a920b5bSAndy Whitcroft } 14710a920b5bSAndy Whitcroft 14725e79d96eSJoe Perches# check for spaces before a quoted newline 14735e79d96eSJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 14745e79d96eSJoe Perches WARN("unnecessary whitespace before a quoted newline\n" . $herecurr); 14755e79d96eSJoe Perches } 14765e79d96eSJoe Perches 14778905a67cSAndy Whitcroft# check for adding lines without a newline. 14788905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 14798905a67cSAndy Whitcroft WARN("adding a line without newline at end of file\n" . $herecurr); 14808905a67cSAndy Whitcroft } 14818905a67cSAndy Whitcroft 148242e41c54SMike Frysinger# Blackfin: use hi/lo macros 148342e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 148442e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 148542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 148642e41c54SMike Frysinger ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 148742e41c54SMike Frysinger } 148842e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 148942e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 149042e41c54SMike Frysinger ERROR("use the HI() macro, not (... >> 16)\n" . $herevet); 149142e41c54SMike Frysinger } 149242e41c54SMike Frysinger } 149342e41c54SMike Frysinger 1494b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 1495b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c|pl)$/); 14960a920b5bSAndy Whitcroft 14970a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 14980a920b5bSAndy Whitcroft# more than 8 must use tabs. 1499c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 1500c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 1501c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1502171ae1a4SAndy Whitcroft ERROR("code indent should use tabs where possible\n" . $herevet); 1503d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 15040a920b5bSAndy Whitcroft } 15050a920b5bSAndy Whitcroft 150608e44365SAlberto Panizzo# check for space before tabs. 150708e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 150808e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 150908e44365SAlberto Panizzo WARN("please, no space before tabs\n" . $herevet); 151008e44365SAlberto Panizzo } 151108e44365SAlberto Panizzo 15125f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 15136b4c5bebSAndy Whitcroft# Exceptions: 15146b4c5bebSAndy Whitcroft# 1) within comments 15156b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 15166b4c5bebSAndy Whitcroft# 3) hanging labels 15176b4c5bebSAndy Whitcroft if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { 15185f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 15196b4c5bebSAndy Whitcroft WARN("please, no spaces at the start of a line\n" . $herevet); 15205f7ddae6SRaffaele Recalcati } 15215f7ddae6SRaffaele Recalcati 1522b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 1523b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 1524b9ea10d6SAndy Whitcroft 1525c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 1526cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1527c2fdda0dSAndy Whitcroft WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); 1528c2fdda0dSAndy Whitcroft } 152922f2a2efSAndy Whitcroft 153042e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 153142e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 153242e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 153342e41c54SMike Frysinger ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 153442e41c54SMike Frysinger } 153542e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 153642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 153742e41c54SMike Frysinger ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 153842e41c54SMike Frysinger } 153942e41c54SMike Frysinger 15409c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 15412b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 15422b474a1aSAndy Whitcroft $realline_next); 15439c9ba34eSAndy Whitcroft if ($realcnt && $line =~ /.\s*\S/) { 1544170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1545f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 1546171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 1547171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 1548171ae1a4SAndy Whitcroft 15492b474a1aSAndy Whitcroft # Find the real next line. 15502b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 15512b474a1aSAndy Whitcroft if (defined $realline_next && 15522b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 15532b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 15542b474a1aSAndy Whitcroft $realline_next++; 15552b474a1aSAndy Whitcroft } 15562b474a1aSAndy Whitcroft 1557171ae1a4SAndy Whitcroft my $s = $stat; 1558171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 1559cf655043SAndy Whitcroft 1560c2fdda0dSAndy Whitcroft # Ignore goto labels. 1561171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 1562c2fdda0dSAndy Whitcroft 1563c2fdda0dSAndy Whitcroft # Ignore functions being called 1564171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 1565c2fdda0dSAndy Whitcroft 1566463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 1567463f2864SAndy Whitcroft 1568c45dcabdSAndy Whitcroft # declarations always start with types 1569d2506586SAndy 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) { 1570c45dcabdSAndy Whitcroft my $type = $1; 1571c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 1572c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 1573c45dcabdSAndy Whitcroft 15746c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 1575a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 1576c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 1577c2fdda0dSAndy Whitcroft } 15788905a67cSAndy Whitcroft 15796c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 158065863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 1581c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 15829c0ca6f9SAndy Whitcroft } 15838905a67cSAndy Whitcroft 15848905a67cSAndy Whitcroft # Check for any sort of function declaration. 15858905a67cSAndy Whitcroft # int foo(something bar, other baz); 15868905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 1587171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 15888905a67cSAndy Whitcroft my ($name_len) = length($1); 15898905a67cSAndy Whitcroft 1590cf655043SAndy Whitcroft my $ctx = $s; 1591773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 15928905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 1593cf655043SAndy Whitcroft 15948905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 1595c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 15968905a67cSAndy Whitcroft 1597c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 15988905a67cSAndy Whitcroft } 15998905a67cSAndy Whitcroft } 16008905a67cSAndy Whitcroft } 16018905a67cSAndy Whitcroft 16029c0ca6f9SAndy Whitcroft } 16039c0ca6f9SAndy Whitcroft 160400df344fSAndy Whitcroft# 160500df344fSAndy Whitcroft# Checks which may be anchored in the context. 160600df344fSAndy Whitcroft# 160700df344fSAndy Whitcroft 160800df344fSAndy Whitcroft# Check for switch () and associated case and default 160900df344fSAndy Whitcroft# statements should be at the same indent. 161000df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 161100df344fSAndy Whitcroft my $err = ''; 161200df344fSAndy Whitcroft my $sep = ''; 161300df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 161400df344fSAndy Whitcroft shift(@ctx); 161500df344fSAndy Whitcroft for my $ctx (@ctx) { 161600df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 161700df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 161800df344fSAndy Whitcroft $indent != $cindent) { 161900df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 162000df344fSAndy Whitcroft $sep = ''; 162100df344fSAndy Whitcroft } else { 162200df344fSAndy Whitcroft $sep = "[...]\n"; 162300df344fSAndy Whitcroft } 162400df344fSAndy Whitcroft } 162500df344fSAndy Whitcroft if ($err ne '') { 16269c0ca6f9SAndy Whitcroft ERROR("switch and case should be at the same indent\n$hereline$err"); 1627de7d4f0eSAndy Whitcroft } 1628de7d4f0eSAndy Whitcroft } 1629de7d4f0eSAndy Whitcroft 1630de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 1631de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 1632c45dcabdSAndy Whitcroft if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 1633773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 1634773647a0SAndy Whitcroft 16359c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1636de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 1637de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 1638de7d4f0eSAndy Whitcroft 1639548596d5SAndy Whitcroft my $ctx_ln = $linenr; 1640548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 1641de7d4f0eSAndy Whitcroft 1642548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 1643548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 1644548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 1645548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 1646548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 1647773647a0SAndy Whitcroft $ctx_ln++; 1648773647a0SAndy Whitcroft } 1649548596d5SAndy Whitcroft 165053210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 165153210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 1652773647a0SAndy Whitcroft 1653773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1654773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . 165501464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 165600df344fSAndy Whitcroft } 1657773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1658773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 1659773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 1660773647a0SAndy Whitcroft { 16619c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 16629c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 1663773647a0SAndy Whitcroft WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 166401464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 16659c0ca6f9SAndy Whitcroft } 16669c0ca6f9SAndy Whitcroft } 166700df344fSAndy Whitcroft } 166800df344fSAndy Whitcroft 16694d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 16704d001e4dSAndy Whitcroft if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 16714d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 16724d001e4dSAndy Whitcroft 16734d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 16744d001e4dSAndy Whitcroft 16754d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 16764d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 16774d001e4dSAndy Whitcroft # where necessary. 16784d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 16794d001e4dSAndy Whitcroft 16804d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 16816f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 16826f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 16834d001e4dSAndy Whitcroft 16844d001e4dSAndy Whitcroft # We want to check the first line inside the block 16854d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 16864d001e4dSAndy Whitcroft # 1) any blank line termination 16874d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 16884d001e4dSAndy Whitcroft # 3) any do (...) { 16894d001e4dSAndy Whitcroft my $continuation = 0; 16904d001e4dSAndy Whitcroft my $check = 0; 16914d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 16924d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 16934d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 16944d001e4dSAndy Whitcroft $continuation = 1; 16954d001e4dSAndy Whitcroft } 16969bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 16974d001e4dSAndy Whitcroft $check = 1; 16984d001e4dSAndy Whitcroft $cond_lines++; 16994d001e4dSAndy Whitcroft } 17004d001e4dSAndy Whitcroft 17014d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 17024d001e4dSAndy Whitcroft # preprocessor statement. 17034d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 17044d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 17054d001e4dSAndy Whitcroft $check = 0; 17064d001e4dSAndy Whitcroft } 17074d001e4dSAndy Whitcroft 17089bd49efeSAndy Whitcroft my $cond_ptr = -1; 1709740504c6SAndy Whitcroft $continuation = 0; 17109bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 17119bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 17124d001e4dSAndy Whitcroft 1713f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 1714f16fa28fSAndy Whitcroft # is not linear. 1715f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 1716f16fa28fSAndy Whitcroft $check = 0; 1717f16fa28fSAndy Whitcroft } 1718f16fa28fSAndy Whitcroft 17199bd49efeSAndy Whitcroft # Ignore: 17209bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 17219bd49efeSAndy Whitcroft # 2) preprocessor lines, and 17229bd49efeSAndy Whitcroft # 3) labels. 1723740504c6SAndy Whitcroft if ($continuation || 1724740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 17259bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 17269bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 1727740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 172830dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 17299bd49efeSAndy Whitcroft $cond_lines++; 17309bd49efeSAndy Whitcroft } 17314d001e4dSAndy Whitcroft } 173230dad6ebSAndy Whitcroft } 17334d001e4dSAndy Whitcroft 17344d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 17354d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 17364d001e4dSAndy Whitcroft 17374d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 17384d001e4dSAndy Whitcroft # this is not this patch's fault. 17394d001e4dSAndy Whitcroft if (!defined($stat_real) || 17404d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 17414d001e4dSAndy Whitcroft $check = 0; 17424d001e4dSAndy Whitcroft } 17434d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 17444d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 17454d001e4dSAndy Whitcroft } 17464d001e4dSAndy Whitcroft 17479bd49efeSAndy 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"; 17484d001e4dSAndy Whitcroft 17494d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 17504d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 17514d001e4dSAndy Whitcroft WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 17524d001e4dSAndy Whitcroft } 17534d001e4dSAndy Whitcroft } 17544d001e4dSAndy Whitcroft 17556c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 17566c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 17571f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 17581f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 17596c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 1760c2fdda0dSAndy Whitcroft if ($dbg_values) { 1761c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 1762cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 1763cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 17641f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 1765c2fdda0dSAndy Whitcroft } 17666c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 17676c72ffaaSAndy Whitcroft 176800df344fSAndy Whitcroft#ignore lines not being added 176900df344fSAndy Whitcroft if ($line=~/^[^\+]/) {next;} 177000df344fSAndy Whitcroft 1771653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 17727429c690SAndy Whitcroft if ($dbg_type) { 17737429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 17747429c690SAndy Whitcroft ERROR("TEST: is type\n" . $herecurr); 17757429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 17767429c690SAndy Whitcroft ERROR("TEST: is not type ($1 is)\n". $herecurr); 17777429c690SAndy Whitcroft } 1778653d4876SAndy Whitcroft next; 1779653d4876SAndy Whitcroft } 1780a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 1781a1ef277eSAndy Whitcroft if ($dbg_attr) { 17829360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 1783a1ef277eSAndy Whitcroft ERROR("TEST: is attr\n" . $herecurr); 17849360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 1785a1ef277eSAndy Whitcroft ERROR("TEST: is not attr ($1 is)\n". $herecurr); 1786a1ef277eSAndy Whitcroft } 1787a1ef277eSAndy Whitcroft next; 1788a1ef277eSAndy Whitcroft } 1789653d4876SAndy Whitcroft 1790f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 179199423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 179299423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 1793773647a0SAndy Whitcroft ERROR("that open brace { should be on the previous line\n" . $hereprev); 1794f0a594c1SAndy Whitcroft } 1795f0a594c1SAndy Whitcroft 179600df344fSAndy Whitcroft# 179700df344fSAndy Whitcroft# Checks which are anchored on the added line. 179800df344fSAndy Whitcroft# 179900df344fSAndy Whitcroft 1800653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 1801c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 1802653d4876SAndy Whitcroft my $path = $1; 1803653d4876SAndy Whitcroft if ($path =~ m{//}) { 1804de7d4f0eSAndy Whitcroft ERROR("malformed #include filename\n" . 1805de7d4f0eSAndy Whitcroft $herecurr); 1806653d4876SAndy Whitcroft } 1807653d4876SAndy Whitcroft } 1808653d4876SAndy Whitcroft 180900df344fSAndy Whitcroft# no C99 // comments 181000df344fSAndy Whitcroft if ($line =~ m{//}) { 1811de7d4f0eSAndy Whitcroft ERROR("do not use C99 // comments\n" . $herecurr); 181200df344fSAndy Whitcroft } 181300df344fSAndy Whitcroft # Remove C99 comments. 18140a920b5bSAndy Whitcroft $line =~ s@//.*@@; 18156c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 18160a920b5bSAndy Whitcroft 18172b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 18182b474a1aSAndy Whitcroft# the whole statement. 18192b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 18202b474a1aSAndy Whitcroft if (defined $realline_next && 18212b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 18222b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 18232b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 18242b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 18253cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 18263cbf62dfSAndy Whitcroft # a prefix: 18273cbf62dfSAndy Whitcroft # XXX(foo); 18283cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 1829653d4876SAndy Whitcroft my $name = $1; 18303cbf62dfSAndy Whitcroft if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && 18313cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 18323cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 18333cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 18343cbf62dfSAndy Whitcroft 18353cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 18362b474a1aSAndy Whitcroft \n.}\s*$| 183748012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 183848012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 183948012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 18402b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 18412b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 184248012058SAndy Whitcroft )/x) { 18432b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 18442b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 18452b474a1aSAndy Whitcroft } else { 18462b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 18470a920b5bSAndy Whitcroft } 18480a920b5bSAndy Whitcroft } 18492b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 18502b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 18512b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 18522b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 18532b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 18542b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 18552b474a1aSAndy Whitcroft } 18562b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 18572b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 18582b474a1aSAndy Whitcroft WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 18592b474a1aSAndy Whitcroft } 18600a920b5bSAndy Whitcroft 18615150bda4SJoe Eloff# check for global initialisers. 1862c45dcabdSAndy Whitcroft if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 18635150bda4SJoe Eloff ERROR("do not initialise globals to 0 or NULL\n" . 1864f0a594c1SAndy Whitcroft $herecurr); 1865f0a594c1SAndy Whitcroft } 18660a920b5bSAndy Whitcroft# check for static initialisers. 18672d1bafd7SAndy Whitcroft if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 1868de7d4f0eSAndy Whitcroft ERROR("do not initialise statics to 0 or NULL\n" . 1869de7d4f0eSAndy Whitcroft $herecurr); 18700a920b5bSAndy Whitcroft } 18710a920b5bSAndy Whitcroft 1872cb710ecaSJoe Perches# check for static const char * arrays. 1873cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 1874cb710ecaSJoe Perches WARN("static const char * array should probably be static const char * const\n" . 1875cb710ecaSJoe Perches $herecurr); 1876cb710ecaSJoe Perches } 1877cb710ecaSJoe Perches 1878cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 1879cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 1880cb710ecaSJoe Perches WARN("static char array declaration should probably be static const char\n" . 1881cb710ecaSJoe Perches $herecurr); 1882cb710ecaSJoe Perches } 1883cb710ecaSJoe Perches 188493ed0e2dSJoe Perches# check for declarations of struct pci_device_id 188593ed0e2dSJoe Perches if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { 188693ed0e2dSJoe Perches WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); 188793ed0e2dSJoe Perches } 188893ed0e2dSJoe Perches 1889653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1890653d4876SAndy Whitcroft# make sense. 1891653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 18928054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 1893c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 18948ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 1895653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1896de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 18970a920b5bSAndy Whitcroft } 18980a920b5bSAndy Whitcroft 18990a920b5bSAndy Whitcroft# * goes on variable not on type 190065863862SAndy Whitcroft # (char*[ const]) 190100ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 190265863862SAndy Whitcroft my ($from, $to) = ($1, $1); 1903d8aaf121SAndy Whitcroft 190465863862SAndy Whitcroft # Should start with a space. 190565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 190665863862SAndy Whitcroft # Should not end with a space. 190765863862SAndy Whitcroft $to =~ s/\s+$//; 190865863862SAndy Whitcroft # '*'s should not have spaces between. 1909f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 191065863862SAndy Whitcroft } 1911d8aaf121SAndy Whitcroft 191265863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 191365863862SAndy Whitcroft if ($from ne $to) { 191465863862SAndy Whitcroft ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 191565863862SAndy Whitcroft } 191600ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 191765863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 1918d8aaf121SAndy Whitcroft 191965863862SAndy Whitcroft # Should start with a space. 192065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 192165863862SAndy Whitcroft # Should not end with a space. 192265863862SAndy Whitcroft $to =~ s/\s+$//; 192365863862SAndy Whitcroft # '*'s should not have spaces between. 1924f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 192565863862SAndy Whitcroft } 192665863862SAndy Whitcroft # Modifiers should have spaces. 192765863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 192865863862SAndy Whitcroft 1929667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 1930667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 193165863862SAndy Whitcroft ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 193265863862SAndy Whitcroft } 19330a920b5bSAndy Whitcroft } 19340a920b5bSAndy Whitcroft 19350a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 19360a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 19370a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 19380a920b5bSAndy Whitcroft# print "$herecurr"; 19390a920b5bSAndy Whitcroft# $clean = 0; 19400a920b5bSAndy Whitcroft# } 19410a920b5bSAndy Whitcroft 19428905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1943c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 19448905a67cSAndy Whitcroft } 19458905a67cSAndy Whitcroft 194600df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 194700df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 194800df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 194925985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 195000df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1951f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 195200df344fSAndy Whitcroft my $ok = 0; 195300df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 195400df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 195525985edcSLucas De Marchi # we have a preceding printk if it ends 195600df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 195700df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 195800df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 195900df344fSAndy Whitcroft $ok = 1; 196000df344fSAndy Whitcroft } 196100df344fSAndy Whitcroft last; 196200df344fSAndy Whitcroft } 196300df344fSAndy Whitcroft } 196400df344fSAndy Whitcroft if ($ok == 0) { 1965de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 19660a920b5bSAndy Whitcroft } 196700df344fSAndy Whitcroft } 19680a920b5bSAndy Whitcroft 1969653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1970653d4876SAndy Whitcroft# or if closed on same line 1971c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1972c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1973de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 19740a920b5bSAndy Whitcroft } 1975653d4876SAndy Whitcroft 19768905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 19778905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 19788905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 19798905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 19808905a67cSAndy Whitcroft } 19818905a67cSAndy Whitcroft 19820c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 19830c73b4ebSAndy Whitcroft if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 19840c73b4ebSAndy Whitcroft WARN("missing space after $1 definition\n" . $herecurr); 19850c73b4ebSAndy Whitcroft } 19860c73b4ebSAndy Whitcroft 19878d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 19888d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1989fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1990fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 19918d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 19928d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 19938d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1994fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 1995fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 19968d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 19978d31cfceSAndy Whitcroft } 19988d31cfceSAndy Whitcroft } 19998d31cfceSAndy Whitcroft 2000f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 20016c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 2002c2fdda0dSAndy Whitcroft my $name = $1; 2003773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 2004773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 2005c2fdda0dSAndy Whitcroft 2006c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 2007773647a0SAndy Whitcroft if ($name =~ /^(?: 2008773647a0SAndy Whitcroft if|for|while|switch|return|case| 2009773647a0SAndy Whitcroft volatile|__volatile__| 2010773647a0SAndy Whitcroft __attribute__|format|__extension__| 2011773647a0SAndy Whitcroft asm|__asm__)$/x) 2012773647a0SAndy Whitcroft { 2013c2fdda0dSAndy Whitcroft 2014c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 2015c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 2016c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2017c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2018773647a0SAndy Whitcroft 2019773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2020c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2021c2fdda0dSAndy Whitcroft 2022c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2023c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2024773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2025c2fdda0dSAndy Whitcroft 2026c2fdda0dSAndy Whitcroft } else { 2027773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 2028f0a594c1SAndy Whitcroft } 20296c72ffaaSAndy Whitcroft } 2030653d4876SAndy Whitcroft# Check operator spacing. 20310a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 20329c0ca6f9SAndy Whitcroft my $ops = qr{ 20339c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 20349c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 20359c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 20361f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 20371f65f947SAndy Whitcroft \?|: 20389c0ca6f9SAndy Whitcroft }x; 2039cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 204000df344fSAndy Whitcroft my $off = 0; 20416c72ffaaSAndy Whitcroft 20426c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 20436c72ffaaSAndy Whitcroft 20440a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 20454a0df2efSAndy Whitcroft $off += length($elements[$n]); 20464a0df2efSAndy Whitcroft 204725985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 2048773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2049773647a0SAndy Whitcroft my $cc = ''; 2050773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2051773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2052773647a0SAndy Whitcroft } 2053773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2054773647a0SAndy Whitcroft 20554a0df2efSAndy Whitcroft my $a = ''; 20564a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 20574a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2058cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 20594a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 20604a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2061773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 20624a0df2efSAndy Whitcroft 20630a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 20644a0df2efSAndy Whitcroft 20654a0df2efSAndy Whitcroft my $c = ''; 20660a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 20674a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 20684a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2069cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 20704a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 20714a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 20728b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 20734a0df2efSAndy Whitcroft } else { 20744a0df2efSAndy Whitcroft $c = 'E'; 20750a920b5bSAndy Whitcroft } 20760a920b5bSAndy Whitcroft 20774a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 20784a0df2efSAndy Whitcroft 20794a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 20804a0df2efSAndy Whitcroft 20816c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2082de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 20830a920b5bSAndy Whitcroft 208474048ed8SAndy Whitcroft # Pull out the value of this operator. 20856c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 20860a920b5bSAndy Whitcroft 20871f65f947SAndy Whitcroft # Get the full operator variant. 20881f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 20891f65f947SAndy Whitcroft 209013214adfSAndy Whitcroft # Ignore operators passed as parameters. 209113214adfSAndy Whitcroft if ($op_type ne 'V' && 209213214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 209313214adfSAndy Whitcroft 2094cf655043SAndy Whitcroft# # Ignore comments 2095cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 209613214adfSAndy Whitcroft 2097d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 209813214adfSAndy Whitcroft } elsif ($op eq ';') { 2099cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2100cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 2101773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 2102d8aaf121SAndy Whitcroft } 2103d8aaf121SAndy Whitcroft 2104d8aaf121SAndy Whitcroft # // is a comment 2105d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 21060a920b5bSAndy Whitcroft 21071f65f947SAndy Whitcroft # No spaces for: 21081f65f947SAndy Whitcroft # -> 21091f65f947SAndy Whitcroft # : when part of a bitfield 21101f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 21114a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 2112773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 21130a920b5bSAndy Whitcroft } 21140a920b5bSAndy Whitcroft 21150a920b5bSAndy Whitcroft # , must have a space on the right. 21160a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2117cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2118773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 21190a920b5bSAndy Whitcroft } 21200a920b5bSAndy Whitcroft 21219c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 212274048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 21239c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 21249c0ca6f9SAndy Whitcroft 21259c0ca6f9SAndy Whitcroft # unary operators should have a space before and 21269c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 21279c0ca6f9SAndy Whitcroft # unary operator, or a cast 21289c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 212974048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 21300d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2131cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2132773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 21330a920b5bSAndy Whitcroft } 2134a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2135171ae1a4SAndy Whitcroft # A unary '*' may be const 2136171ae1a4SAndy Whitcroft 2137171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 2138fb9e9096SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 21390a920b5bSAndy Whitcroft } 21400a920b5bSAndy Whitcroft 21410a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 21420a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2143773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2144773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 21450a920b5bSAndy Whitcroft } 2146773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2147773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2148773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2149653d4876SAndy Whitcroft } 2150773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 2151773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2152773647a0SAndy Whitcroft } 2153773647a0SAndy Whitcroft 21540a920b5bSAndy Whitcroft 21550a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 21569c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 21579c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 21589c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 2159c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 2160c2fdda0dSAndy Whitcroft $op eq '%') 21610a920b5bSAndy Whitcroft { 2162773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2163de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 2164de7d4f0eSAndy Whitcroft $hereptr); 21650a920b5bSAndy Whitcroft } 21660a920b5bSAndy Whitcroft 21671f65f947SAndy Whitcroft # A colon needs no spaces before when it is 21681f65f947SAndy Whitcroft # terminating a case value or a label. 21691f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 21701f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 21711f65f947SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 21721f65f947SAndy Whitcroft } 21731f65f947SAndy Whitcroft 21740a920b5bSAndy Whitcroft # All the others need spaces both sides. 2175cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 21761f65f947SAndy Whitcroft my $ok = 0; 21771f65f947SAndy Whitcroft 217822f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 21791f65f947SAndy Whitcroft if (($op eq '<' && 21801f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 21811f65f947SAndy Whitcroft ($op eq '>' && 21821f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 21831f65f947SAndy Whitcroft { 21841f65f947SAndy Whitcroft $ok = 1; 21851f65f947SAndy Whitcroft } 21861f65f947SAndy Whitcroft 21871f65f947SAndy Whitcroft # Ignore ?: 21881f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 21891f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 21901f65f947SAndy Whitcroft $ok = 1; 21911f65f947SAndy Whitcroft } 21921f65f947SAndy Whitcroft 21931f65f947SAndy Whitcroft if ($ok == 0) { 2194773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 21950a920b5bSAndy Whitcroft } 219622f2a2efSAndy Whitcroft } 21974a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 21980a920b5bSAndy Whitcroft } 21990a920b5bSAndy Whitcroft } 22000a920b5bSAndy Whitcroft 2201f0a594c1SAndy Whitcroft# check for multiple assignments 2202f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 22036c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 2204f0a594c1SAndy Whitcroft } 2205f0a594c1SAndy Whitcroft 220622f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 220722f2a2efSAndy Whitcroft## # continuation. 220822f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 220922f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 221022f2a2efSAndy Whitcroft## 221122f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 221222f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 221322f2a2efSAndy Whitcroft## my $ln = $line; 221422f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 221522f2a2efSAndy Whitcroft## } 221622f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 221722f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 221822f2a2efSAndy Whitcroft## } 221922f2a2efSAndy Whitcroft## } 2220f0a594c1SAndy Whitcroft 22210a920b5bSAndy Whitcroft#need space before brace following if, while, etc 222222f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 222322f2a2efSAndy Whitcroft $line =~ /do{/) { 2224773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 2225de7d4f0eSAndy Whitcroft } 2226de7d4f0eSAndy Whitcroft 2227de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2228de7d4f0eSAndy Whitcroft# on the line 2229de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2230773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 22310a920b5bSAndy Whitcroft } 22320a920b5bSAndy Whitcroft 223322f2a2efSAndy Whitcroft# check spacing on square brackets 223422f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2235773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 223622f2a2efSAndy Whitcroft } 223722f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2238773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 223922f2a2efSAndy Whitcroft } 224022f2a2efSAndy Whitcroft 2241c45dcabdSAndy Whitcroft# check spacing on parentheses 22429c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 22439c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2244773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 224522f2a2efSAndy Whitcroft } 224613214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2247c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2248c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2249773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 225022f2a2efSAndy Whitcroft } 225122f2a2efSAndy Whitcroft 22520a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 22534a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 22540a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2255de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 22560a920b5bSAndy Whitcroft } 22570a920b5bSAndy Whitcroft 2258c45dcabdSAndy Whitcroft# Return is not a function. 2259c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2260c45dcabdSAndy Whitcroft my $spacing = $1; 2261c45dcabdSAndy Whitcroft my $value = $2; 2262c45dcabdSAndy Whitcroft 226386f9d059SAndy Whitcroft # Flatten any parentheses 2264fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 2265fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 226663f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 226763f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 226863f17f89SAndy Whitcroft $Compare\s* 226963f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 227063f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2271c45dcabdSAndy Whitcroft } 2272fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 2273fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2274c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2275c45dcabdSAndy Whitcroft 2276c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2277c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 2278c45dcabdSAndy Whitcroft } 2279c45dcabdSAndy Whitcroft } 228053a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 228153a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 228253a3c448SAndy Whitcroft my $name = $1; 228353a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 228453a3c448SAndy Whitcroft WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr); 228553a3c448SAndy Whitcroft } 228653a3c448SAndy Whitcroft } 2287c45dcabdSAndy Whitcroft 22880a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 22894a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2290773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 22910a920b5bSAndy Whitcroft } 22920a920b5bSAndy Whitcroft 2293f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2294f5fe35ddSAndy Whitcroft# statements after the conditional. 2295170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2296170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2297170d3a22SAndy Whitcroft $remain_next, $off_next); 2298170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2299170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2300170d3a22SAndy Whitcroft 2301170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2302170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2303170d3a22SAndy Whitcroft # then count those as offsets. 2304170d3a22SAndy Whitcroft my ($whitespace) = 2305170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2306170d3a22SAndy Whitcroft my $offset = 2307170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2308170d3a22SAndy Whitcroft 2309170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2310170d3a22SAndy Whitcroft $offset} = 1; 2311170d3a22SAndy Whitcroft } 2312170d3a22SAndy Whitcroft } 2313170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2314170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2315171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 23168905a67cSAndy Whitcroft 2317b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2318c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 23198905a67cSAndy Whitcroft } 23208905a67cSAndy Whitcroft 23218905a67cSAndy Whitcroft # Find out what is on the end of the line after the 23228905a67cSAndy Whitcroft # conditional. 2323773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 23248905a67cSAndy Whitcroft $s =~ s/\n.*//g; 232513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 232653210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 232753210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2328773647a0SAndy Whitcroft { 2329bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2330bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2331bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 233242bdf74cSHidetoshi Seto my $stat_real = ''; 2333bb44ad39SAndy Whitcroft 233442bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 233542bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 2336bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2337bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2338bb44ad39SAndy Whitcroft } 2339bb44ad39SAndy Whitcroft 2340bb44ad39SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 23418905a67cSAndy Whitcroft } 23428905a67cSAndy Whitcroft } 23438905a67cSAndy Whitcroft 234413214adfSAndy Whitcroft# Check for bitwise tests written as boolean 234513214adfSAndy Whitcroft if ($line =~ / 234613214adfSAndy Whitcroft (?: 234713214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 234813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 234913214adfSAndy Whitcroft (?:\&\&|\|\|) 235013214adfSAndy Whitcroft | 235113214adfSAndy Whitcroft (?:\&\&|\|\|) 235213214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 235313214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 235413214adfSAndy Whitcroft )/x) 235513214adfSAndy Whitcroft { 235613214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 235713214adfSAndy Whitcroft } 235813214adfSAndy Whitcroft 23598905a67cSAndy Whitcroft# if and else should not have general statements after it 236013214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 236113214adfSAndy Whitcroft my $s = $1; 236213214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 236313214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 23648905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 23650a920b5bSAndy Whitcroft } 236613214adfSAndy Whitcroft } 236739667782SAndy Whitcroft# if should not continue a brace 236839667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 236939667782SAndy Whitcroft ERROR("trailing statements should be on next line\n" . 237039667782SAndy Whitcroft $herecurr); 237139667782SAndy Whitcroft } 2372a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2373a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2374a1080bf8SAndy Whitcroft $line !~ /\G(?: 23753fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2376a1080bf8SAndy Whitcroft \s*return\s+ 2377a1080bf8SAndy Whitcroft )/xg) 2378a1080bf8SAndy Whitcroft { 2379a1080bf8SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 2380a1080bf8SAndy Whitcroft } 23810a920b5bSAndy Whitcroft 23820a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 23830a920b5bSAndy Whitcroft # indent level to be relevant to each other. 23840a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 23850a920b5bSAndy Whitcroft $previndent == $indent) { 2386de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 23870a920b5bSAndy Whitcroft } 23880a920b5bSAndy Whitcroft 2389c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2390c2fdda0dSAndy Whitcroft $previndent == $indent) { 2391c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2392c2fdda0dSAndy Whitcroft 2393c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2394c2fdda0dSAndy Whitcroft # conditional. 2395773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2396c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2397c2fdda0dSAndy Whitcroft 2398c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2399c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 2400c2fdda0dSAndy Whitcroft } 2401c2fdda0dSAndy Whitcroft } 2402c2fdda0dSAndy Whitcroft 24030a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 24040a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 24050a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 24060a920b5bSAndy Whitcroft# print "$herecurr"; 24070a920b5bSAndy Whitcroft# $clean = 0; 24080a920b5bSAndy Whitcroft# } 24090a920b5bSAndy Whitcroft 24100a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2411c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2412de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 24130a920b5bSAndy Whitcroft } 24140a920b5bSAndy Whitcroft 2415653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2416c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2417e09dec48SAndy Whitcroft my $file = "$1.h"; 2418e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2419e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2420e09dec48SAndy Whitcroft $realfile ne $checkfile && 24217840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 2422c45dcabdSAndy Whitcroft { 2423e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2424e09dec48SAndy Whitcroft CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2425e09dec48SAndy Whitcroft } else { 2426e09dec48SAndy Whitcroft WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2427e09dec48SAndy Whitcroft } 24280a920b5bSAndy Whitcroft } 24290a920b5bSAndy Whitcroft } 24300a920b5bSAndy Whitcroft 2431653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2432653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2433cf655043SAndy Whitcroft# in a known good container 2434b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2435b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2436d8aaf121SAndy Whitcroft my $ln = $linenr; 2437d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2438c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2439c45dcabdSAndy Whitcroft my $ctx = ''; 2440653d4876SAndy Whitcroft 2441c45dcabdSAndy Whitcroft my $args = defined($1); 2442de7d4f0eSAndy Whitcroft 2443c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2444c45dcabdSAndy Whitcroft # search to that. 2445c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2446c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2447c45dcabdSAndy Whitcroft { 2448c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2449a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2450c45dcabdSAndy Whitcroft $ln++; 2451de7d4f0eSAndy Whitcroft } 2452c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2453d8aaf121SAndy Whitcroft 2454c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2455c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2456c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2457a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2458c45dcabdSAndy Whitcroft 2459c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2460c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2461c45dcabdSAndy Whitcroft $rest = ''; 2462636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2463636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2464a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2465c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2466c45dcabdSAndy Whitcroft $cnt--; 2467a3bb97a7SAndy Whitcroft } 2468a3bb97a7SAndy Whitcroft $ln++; 2469c45dcabdSAndy Whitcroft $off = 0; 2470c45dcabdSAndy Whitcroft } 2471c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2472c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2473c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2474c45dcabdSAndy Whitcroft 2475c45dcabdSAndy Whitcroft # Clean up the original statement. 2476c45dcabdSAndy Whitcroft if ($args) { 2477c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2478d8aaf121SAndy Whitcroft } else { 2479c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2480c45dcabdSAndy Whitcroft } 2481292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2482c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2483c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2484c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2485c45dcabdSAndy Whitcroft 2486c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2487bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2488bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2489bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2490bf30d6edSAndy Whitcroft { 2491c45dcabdSAndy Whitcroft } 2492c45dcabdSAndy Whitcroft 2493c45dcabdSAndy Whitcroft my $exceptions = qr{ 2494c45dcabdSAndy Whitcroft $Declare| 2495c45dcabdSAndy Whitcroft module_param_named| 2496c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2497c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2498c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2499383099fdSAndy Whitcroft __typeof__\(| 250022fd2d3eSStefani Seibold union| 250122fd2d3eSStefani Seibold struct| 2502ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2503ea71a0a0SAndy Whitcroft ^\"|\"$ 2504c45dcabdSAndy Whitcroft }x; 25055eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 25065eaa20b9SAndy Whitcroft if ($rest ne '' && $rest ne ',') { 2507c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2508c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2509c45dcabdSAndy Whitcroft { 2510c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2511c45dcabdSAndy Whitcroft } 2512c45dcabdSAndy Whitcroft 2513c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2514c45dcabdSAndy Whitcroft if ($dstat ne '' && 2515c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2516c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2517b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2518c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2519c45dcabdSAndy Whitcroft { 2520de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2521d8aaf121SAndy Whitcroft } 25220a920b5bSAndy Whitcroft } 2523653d4876SAndy Whitcroft } 25240a920b5bSAndy Whitcroft 2525080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2526080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2527080ba929SMike Frysinger# . 2528080ba929SMike Frysinger# ALIGN(...) 2529080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2530080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2531080ba929SMike Frysinger WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2532080ba929SMike Frysinger } 2533080ba929SMike Frysinger 2534f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 253513214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 253613214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2537cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 253813214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2539cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2540cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 254113214adfSAndy Whitcroft my $allowed = 0; 254213214adfSAndy Whitcroft my $seen = 0; 2543773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2544cf655043SAndy Whitcroft my $ln = $linenr - 1; 254513214adfSAndy Whitcroft for my $chunk (@chunks) { 254613214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 254713214adfSAndy Whitcroft 2548773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2549773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2550773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2551773647a0SAndy Whitcroft 2552773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2553773647a0SAndy Whitcroft 2554773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2555773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2556773647a0SAndy Whitcroft 2557773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2558cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2559cf655043SAndy Whitcroft 2560773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 256113214adfSAndy Whitcroft 256213214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 256313214adfSAndy Whitcroft 2564cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2565cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2566cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 256713214adfSAndy Whitcroft $allowed = 1; 256813214adfSAndy Whitcroft } 256913214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2570cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 257113214adfSAndy Whitcroft $allowed = 1; 257213214adfSAndy Whitcroft } 2573cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2574cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 257513214adfSAndy Whitcroft $allowed = 1; 257613214adfSAndy Whitcroft } 257713214adfSAndy Whitcroft } 257813214adfSAndy Whitcroft if ($seen && !$allowed) { 2579cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 258013214adfSAndy Whitcroft } 258113214adfSAndy Whitcroft } 258213214adfSAndy Whitcroft } 2583773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 258413214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2585cf655043SAndy Whitcroft my $allowed = 0; 2586f0a594c1SAndy Whitcroft 2587cf655043SAndy Whitcroft # Check the pre-context. 2588cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2589cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2590cf655043SAndy Whitcroft $allowed = 1; 2591f0a594c1SAndy Whitcroft } 2592773647a0SAndy Whitcroft 2593773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2594773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2595773647a0SAndy Whitcroft 2596cf655043SAndy Whitcroft # Check the condition. 2597cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2598773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2599cf655043SAndy Whitcroft if (defined $cond) { 2600773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2601cf655043SAndy Whitcroft } 2602cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2603cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2604cf655043SAndy Whitcroft $allowed = 1; 2605cf655043SAndy Whitcroft } 2606cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2607cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2608cf655043SAndy Whitcroft $allowed = 1; 2609cf655043SAndy Whitcroft } 2610cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2611cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2612cf655043SAndy Whitcroft $allowed = 1; 2613cf655043SAndy Whitcroft } 2614cf655043SAndy Whitcroft # Check the post-context. 2615cf655043SAndy Whitcroft if (defined $chunks[1]) { 2616cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2617cf655043SAndy Whitcroft if (defined $cond) { 2618773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2619cf655043SAndy Whitcroft } 2620cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2621cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2622cf655043SAndy Whitcroft $allowed = 1; 2623cf655043SAndy Whitcroft } 2624cf655043SAndy Whitcroft } 2625cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2626cf655043SAndy Whitcroft my $herectx = $here . "\n";; 2627f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2628cf655043SAndy Whitcroft 2629f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2630f055663cSAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n";; 2631cf655043SAndy Whitcroft } 2632cf655043SAndy Whitcroft 2633cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 2634f0a594c1SAndy Whitcroft } 2635f0a594c1SAndy Whitcroft } 2636f0a594c1SAndy Whitcroft 2637653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 26384a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2639c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2640de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 26410a920b5bSAndy Whitcroft } 26420a920b5bSAndy Whitcroft } 26430a920b5bSAndy Whitcroft 26444a0df2efSAndy Whitcroft# don't use deprecated functions 26454a0df2efSAndy Whitcroft for my $func (@dep_functions) { 264600df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2647de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 26484a0df2efSAndy Whitcroft } 26494a0df2efSAndy Whitcroft } 26504a0df2efSAndy Whitcroft 26514a0df2efSAndy Whitcroft# no volatiles please 26526c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 26536c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2654de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 26554a0df2efSAndy Whitcroft } 26564a0df2efSAndy Whitcroft 265700df344fSAndy Whitcroft# warn about #if 0 2658c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2659de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2660de7d4f0eSAndy Whitcroft $herecurr); 26614a0df2efSAndy Whitcroft } 26624a0df2efSAndy Whitcroft 2663f0a594c1SAndy Whitcroft# check for needless kfree() checks 2664f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2665f0a594c1SAndy Whitcroft my $expr = $1; 2666f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 26673c232147SWolfram Sang WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2668f0a594c1SAndy Whitcroft } 2669f0a594c1SAndy Whitcroft } 26704c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 26714c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 26724c432a8fSGreg Kroah-Hartman my $expr = $1; 26734c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 26744c432a8fSGreg Kroah-Hartman WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 26754c432a8fSGreg Kroah-Hartman } 26764c432a8fSGreg Kroah-Hartman } 2677f0a594c1SAndy Whitcroft 26781a15a250SPatrick Pannuto# prefer usleep_range over udelay 26791a15a250SPatrick Pannuto if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { 26801a15a250SPatrick Pannuto # ignore udelay's < 10, however 26811a15a250SPatrick Pannuto if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { 26821a15a250SPatrick Pannuto CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 26831a15a250SPatrick Pannuto } 26841a15a250SPatrick Pannuto } 26851a15a250SPatrick Pannuto 268609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 268709ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 268809ef8725SPatrick Pannuto if ($1 < 20) { 268909ef8725SPatrick Pannuto WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 269009ef8725SPatrick Pannuto } 269109ef8725SPatrick Pannuto } 269209ef8725SPatrick Pannuto 269300df344fSAndy Whitcroft# warn about #ifdefs in C files 2694c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 269500df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 269600df344fSAndy Whitcroft# print "$herecurr"; 269700df344fSAndy Whitcroft# $clean = 0; 269800df344fSAndy Whitcroft# } 269900df344fSAndy Whitcroft 270022f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2701c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 270222f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 270322f2a2efSAndy Whitcroft } 270422f2a2efSAndy Whitcroft 27054a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2706171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2707171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 27084a0df2efSAndy Whitcroft my $which = $1; 27094a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2710de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 27114a0df2efSAndy Whitcroft } 27124a0df2efSAndy Whitcroft } 27134a0df2efSAndy Whitcroft# check for memory barriers without a comment. 27144a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 27154a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2716de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 27174a0df2efSAndy Whitcroft } 27184a0df2efSAndy Whitcroft } 27194a0df2efSAndy Whitcroft# check of hardware specific defines 2720c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2721de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 27220a920b5bSAndy Whitcroft } 2723653d4876SAndy Whitcroft 2724d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 2725d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 2726d4977c78STobias Klauser WARN("storage class should be at the beginning of the declaration\n" . $herecurr) 2727d4977c78STobias Klauser } 2728d4977c78STobias Klauser 2729de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2730de7d4f0eSAndy Whitcroft# storage class and type. 27319c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 27329c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2733de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2734de7d4f0eSAndy Whitcroft } 2735de7d4f0eSAndy Whitcroft 27368905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 27378905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 27388905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 27398905a67cSAndy Whitcroft } 27408905a67cSAndy Whitcroft 27413d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 27423d130fd0SJoe Perches if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 27433d130fd0SJoe Perches WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr); 27443d130fd0SJoe Perches } 27453d130fd0SJoe Perches 27468f53a9b8SJoe Perches# check for sizeof(&) 27478f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 27488f53a9b8SJoe Perches WARN("sizeof(& should be avoided\n" . $herecurr); 27498f53a9b8SJoe Perches } 27508f53a9b8SJoe Perches 2751*428e2fdcSJoe Perches# check for line continuations in quoted strings with odd counts of " 2752*428e2fdcSJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 2753*428e2fdcSJoe Perches WARN("Avoid line continuations in quoted strings\n" . $herecurr); 2754*428e2fdcSJoe Perches } 2755*428e2fdcSJoe Perches 2756de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2757171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2758c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2759171ae1a4SAndy Whitcroft { 2760c45dcabdSAndy Whitcroft my $function_name = $1; 2761c45dcabdSAndy Whitcroft my $paren_space = $2; 2762171ae1a4SAndy Whitcroft 2763171ae1a4SAndy Whitcroft my $s = $stat; 2764171ae1a4SAndy Whitcroft if (defined $cond) { 2765171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2766171ae1a4SAndy Whitcroft } 2767c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2768c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2769c45dcabdSAndy Whitcroft { 2770de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2771de7d4f0eSAndy Whitcroft } 2772de7d4f0eSAndy Whitcroft 2773171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2774171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2775171ae1a4SAndy Whitcroft } 27769c9ba34eSAndy Whitcroft 27779c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 27789c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 27799c9ba34eSAndy Whitcroft { 27809c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2781171ae1a4SAndy Whitcroft } 2782171ae1a4SAndy Whitcroft 2783de7d4f0eSAndy Whitcroft# checks for new __setup's 2784de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2785de7d4f0eSAndy Whitcroft my $name = $1; 2786de7d4f0eSAndy Whitcroft 2787de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2788de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2789de7d4f0eSAndy Whitcroft } 2790653d4876SAndy Whitcroft } 27919c0ca6f9SAndy Whitcroft 27929c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 2793caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 27949c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 27959c0ca6f9SAndy Whitcroft } 279613214adfSAndy Whitcroft 2797caf2a54fSJoe Perches# check for multiple semicolons 2798caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 2799caf2a54fSJoe Perches WARN("Statements terminations use 1 semicolon\n" . $herecurr); 2800caf2a54fSJoe Perches } 2801caf2a54fSJoe Perches 280213214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 280313214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 280413214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 280513214adfSAndy Whitcroft } 2806773647a0SAndy Whitcroft 28074882720bSThomas Gleixner# check for semaphores initialized locked 28084882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 2809773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 28101704f47bSPeter Zijlstra 2811773647a0SAndy Whitcroft } 281233ee3b2eSAlexey Dobriyan# recommend kstrto* over simple_strto* 2813773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 281433ee3b2eSAlexey Dobriyan WARN("consider using kstrto* in preference to simple_$1\n" . $herecurr); 2815773647a0SAndy Whitcroft } 2816f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2817f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2818f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2819f3db6639SMichael Ellerman } 282079404849SEmese Revfy# check for various ops structs, ensure they are const. 282179404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 282279404849SEmese Revfy address_space_operations| 282379404849SEmese Revfy backlight_ops| 282479404849SEmese Revfy block_device_operations| 282579404849SEmese Revfy dentry_operations| 282679404849SEmese Revfy dev_pm_ops| 282779404849SEmese Revfy dma_map_ops| 282879404849SEmese Revfy extent_io_ops| 282979404849SEmese Revfy file_lock_operations| 283079404849SEmese Revfy file_operations| 283179404849SEmese Revfy hv_ops| 283279404849SEmese Revfy ide_dma_ops| 283379404849SEmese Revfy intel_dvo_dev_ops| 283479404849SEmese Revfy item_operations| 283579404849SEmese Revfy iwl_ops| 283679404849SEmese Revfy kgdb_arch| 283779404849SEmese Revfy kgdb_io| 283879404849SEmese Revfy kset_uevent_ops| 283979404849SEmese Revfy lock_manager_operations| 284079404849SEmese Revfy microcode_ops| 284179404849SEmese Revfy mtrr_ops| 284279404849SEmese Revfy neigh_ops| 284379404849SEmese Revfy nlmsvc_binding| 284479404849SEmese Revfy pci_raw_ops| 284579404849SEmese Revfy pipe_buf_operations| 284679404849SEmese Revfy platform_hibernation_ops| 284779404849SEmese Revfy platform_suspend_ops| 284879404849SEmese Revfy proto_ops| 284979404849SEmese Revfy rpc_pipe_ops| 285079404849SEmese Revfy seq_operations| 285179404849SEmese Revfy snd_ac97_build_ops| 285279404849SEmese Revfy soc_pcmcia_socket_ops| 285379404849SEmese Revfy stacktrace_ops| 285479404849SEmese Revfy sysfs_ops| 285579404849SEmese Revfy tty_operations| 285679404849SEmese Revfy usb_mon_operations| 285779404849SEmese Revfy wd_ops}x; 28586903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 285979404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 28606903ffb2SAndy Whitcroft WARN("struct $1 should normally be const\n" . 28616903ffb2SAndy Whitcroft $herecurr); 28622b6db5cbSAndy Whitcroft } 2863773647a0SAndy Whitcroft 2864773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2865773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2866773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2867c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2868c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2869171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2870171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2871171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2872773647a0SAndy Whitcroft { 2873773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2874773647a0SAndy Whitcroft } 28759c9ba34eSAndy Whitcroft 28769c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 28779c9ba34eSAndy Whitcroft my $string; 28789c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 28799c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 28802a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 28819c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 28829c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 28839c9ba34eSAndy Whitcroft last; 28849c9ba34eSAndy Whitcroft } 28859c9ba34eSAndy Whitcroft } 2886691d77b6SAndy Whitcroft 2887691d77b6SAndy Whitcroft# whine mightly about in_atomic 2888691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 2889691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 2890691d77b6SAndy Whitcroft ERROR("do not use in_atomic in drivers\n" . $herecurr); 2891f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 2892691d77b6SAndy Whitcroft WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2893691d77b6SAndy Whitcroft } 2894691d77b6SAndy Whitcroft } 28951704f47bSPeter Zijlstra 28961704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 28971704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 28981704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 28991704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 29001704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 29011704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 29021704f47bSPeter Zijlstra ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 29031704f47bSPeter Zijlstra } 29041704f47bSPeter Zijlstra } 290588f8831cSDave Jones 290688f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 290788f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 290888f8831cSDave Jones WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 290988f8831cSDave Jones } 2910309c00c7SDave Jones 2911309c00c7SDave Jones # Check for memset with swapped arguments 2912309c00c7SDave Jones if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) { 2913309c00c7SDave Jones ERROR("memset size is 3rd argument, not the second.\n" . $herecurr); 2914309c00c7SDave Jones } 291513214adfSAndy Whitcroft } 291613214adfSAndy Whitcroft 291713214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 291813214adfSAndy Whitcroft # so just keep quiet. 291913214adfSAndy Whitcroft if ($#rawlines == -1) { 292013214adfSAndy Whitcroft exit(0); 29210a920b5bSAndy Whitcroft } 29220a920b5bSAndy Whitcroft 29238905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 29248905a67cSAndy Whitcroft # things that appear to be patches. 29258905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 29268905a67cSAndy Whitcroft exit(0); 29278905a67cSAndy Whitcroft } 29288905a67cSAndy Whitcroft 29298905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 29308905a67cSAndy Whitcroft # just keep quiet. 29318905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 29328905a67cSAndy Whitcroft exit(0); 29338905a67cSAndy Whitcroft } 29348905a67cSAndy Whitcroft 29358905a67cSAndy Whitcroft if (!$is_patch) { 2936de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 29370a920b5bSAndy Whitcroft } 29380a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2939de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 29400a920b5bSAndy Whitcroft } 29410a920b5bSAndy Whitcroft 2942f0a594c1SAndy Whitcroft print report_dump(); 294313214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 294413214adfSAndy Whitcroft print "$filename " if ($summary_file); 29456c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 29466c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 29476c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 29488905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 29496c72ffaaSAndy Whitcroft } 29508905a67cSAndy Whitcroft 2951d2c0a235SAndy Whitcroft if ($quiet == 0) { 2952d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 2953d2c0a235SAndy Whitcroft # then suggest that. 2954d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 2955d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 2956d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 2957b0781216SMike Frysinger $rpt_cleaners = 0; 2958d2c0a235SAndy Whitcroft } 2959d2c0a235SAndy Whitcroft } 2960d2c0a235SAndy Whitcroft 29610a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2962c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 29630a920b5bSAndy Whitcroft } 29640a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2965c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 29660a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 29670a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 29680a920b5bSAndy Whitcroft } 296913214adfSAndy Whitcroft 29700a920b5bSAndy Whitcroft return $clean; 29710a920b5bSAndy Whitcroft} 2972