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 135e8d8f6fSAndy Whitcroftmy $V = '0.30'; 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. 6944635f4fbSAndy Whitcroft if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 6954635f4fbSAndy Whitcroft push(@stack, $level); 6964635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 6974635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 6984635f4fbSAndy Whitcroft } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { 6994635f4fbSAndy Whitcroft $level = pop(@stack); 7004635f4fbSAndy Whitcroft } 7014635f4fbSAndy Whitcroft 702f0a594c1SAndy Whitcroft foreach my $c (split(//, $rawlines[$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 8629446ef56SAndy Whitcroft } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) { 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" . 1655c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$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" . 1664c45dcabdSAndy Whitcroft "$here\n$ctx\n$lines[$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.*\((.*)\)/)) { 1825*3cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 1826*3cbf62dfSAndy Whitcroft # a prefix: 1827*3cbf62dfSAndy Whitcroft # XXX(foo); 1828*3cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 1829653d4876SAndy Whitcroft my $name = $1; 1830*3cbf62dfSAndy Whitcroft if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && 1831*3cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 1832*3cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 1833*3cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 1834*3cbf62dfSAndy Whitcroft 1835*3cbf62dfSAndy 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 1872653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 1873653d4876SAndy Whitcroft# make sense. 1874653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 18758054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 1876c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 18778ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 1878653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 1879de7d4f0eSAndy Whitcroft WARN("do not add new typedefs\n" . $herecurr); 18800a920b5bSAndy Whitcroft } 18810a920b5bSAndy Whitcroft 18820a920b5bSAndy Whitcroft# * goes on variable not on type 188365863862SAndy Whitcroft # (char*[ const]) 188400ef4eceSAndy Whitcroft if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { 188565863862SAndy Whitcroft my ($from, $to) = ($1, $1); 1886d8aaf121SAndy Whitcroft 188765863862SAndy Whitcroft # Should start with a space. 188865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 188965863862SAndy Whitcroft # Should not end with a space. 189065863862SAndy Whitcroft $to =~ s/\s+$//; 189165863862SAndy Whitcroft # '*'s should not have spaces between. 1892f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 189365863862SAndy Whitcroft } 1894d8aaf121SAndy Whitcroft 189565863862SAndy Whitcroft #print "from<$from> to<$to>\n"; 189665863862SAndy Whitcroft if ($from ne $to) { 189765863862SAndy Whitcroft ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 189865863862SAndy Whitcroft } 189900ef4eceSAndy Whitcroft } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { 190065863862SAndy Whitcroft my ($from, $to, $ident) = ($1, $1, $2); 1901d8aaf121SAndy Whitcroft 190265863862SAndy Whitcroft # Should start with a space. 190365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 190465863862SAndy Whitcroft # Should not end with a space. 190565863862SAndy Whitcroft $to =~ s/\s+$//; 190665863862SAndy Whitcroft # '*'s should not have spaces between. 1907f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 190865863862SAndy Whitcroft } 190965863862SAndy Whitcroft # Modifiers should have spaces. 191065863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 191165863862SAndy Whitcroft 1912667026e7SAndy Whitcroft #print "from<$from> to<$to> ident<$ident>\n"; 1913667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 191465863862SAndy Whitcroft ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); 191565863862SAndy Whitcroft } 19160a920b5bSAndy Whitcroft } 19170a920b5bSAndy Whitcroft 19180a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 19190a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 19200a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 19210a920b5bSAndy Whitcroft# print "$herecurr"; 19220a920b5bSAndy Whitcroft# $clean = 0; 19230a920b5bSAndy Whitcroft# } 19240a920b5bSAndy Whitcroft 19258905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 1926c2fdda0dSAndy Whitcroft WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 19278905a67cSAndy Whitcroft } 19288905a67cSAndy Whitcroft 192900df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 193000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 193100df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 193200df344fSAndy Whitcroft# printk includes all preceeding printk's which have no newline on the end. 193300df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 1934f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 193500df344fSAndy Whitcroft my $ok = 0; 193600df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 193700df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 193800df344fSAndy Whitcroft # we have a preceeding printk if it ends 193900df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 194000df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 194100df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 194200df344fSAndy Whitcroft $ok = 1; 194300df344fSAndy Whitcroft } 194400df344fSAndy Whitcroft last; 194500df344fSAndy Whitcroft } 194600df344fSAndy Whitcroft } 194700df344fSAndy Whitcroft if ($ok == 0) { 1948de7d4f0eSAndy Whitcroft WARN("printk() should include KERN_ facility level\n" . $herecurr); 19490a920b5bSAndy Whitcroft } 195000df344fSAndy Whitcroft } 19510a920b5bSAndy Whitcroft 1952653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 1953653d4876SAndy Whitcroft# or if closed on same line 1954c45dcabdSAndy Whitcroft if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 1955c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 1956de7d4f0eSAndy Whitcroft ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); 19570a920b5bSAndy Whitcroft } 1958653d4876SAndy Whitcroft 19598905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 19608905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 19618905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 19628905a67cSAndy Whitcroft ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 19638905a67cSAndy Whitcroft } 19648905a67cSAndy Whitcroft 19650c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 19660c73b4ebSAndy Whitcroft if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { 19670c73b4ebSAndy Whitcroft WARN("missing space after $1 definition\n" . $herecurr); 19680c73b4ebSAndy Whitcroft } 19690c73b4ebSAndy Whitcroft 19708d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 19718d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 1972fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1973fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 19748d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 19758d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 19768d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 1977fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 1978fe2a7dbcSAndy Whitcroft $prefix !~ /{\s+$/) { 19798d31cfceSAndy Whitcroft ERROR("space prohibited before open square bracket '['\n" . $herecurr); 19808d31cfceSAndy Whitcroft } 19818d31cfceSAndy Whitcroft } 19828d31cfceSAndy Whitcroft 1983f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 19846c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 1985c2fdda0dSAndy Whitcroft my $name = $1; 1986773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 1987773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 1988c2fdda0dSAndy Whitcroft 1989c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 1990773647a0SAndy Whitcroft if ($name =~ /^(?: 1991773647a0SAndy Whitcroft if|for|while|switch|return|case| 1992773647a0SAndy Whitcroft volatile|__volatile__| 1993773647a0SAndy Whitcroft __attribute__|format|__extension__| 1994773647a0SAndy Whitcroft asm|__asm__)$/x) 1995773647a0SAndy Whitcroft { 1996c2fdda0dSAndy Whitcroft 1997c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 1998c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 1999c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 2000c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 2001773647a0SAndy Whitcroft 2002773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 2003c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 2004c2fdda0dSAndy Whitcroft 2005c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 2006c2fdda0dSAndy Whitcroft # likely a typedef for a function. 2007773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 2008c2fdda0dSAndy Whitcroft 2009c2fdda0dSAndy Whitcroft } else { 2010773647a0SAndy Whitcroft WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); 2011f0a594c1SAndy Whitcroft } 20126c72ffaaSAndy Whitcroft } 2013653d4876SAndy Whitcroft# Check operator spacing. 20140a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 20159c0ca6f9SAndy Whitcroft my $ops = qr{ 20169c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 20179c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 20189c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 20191f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 20201f65f947SAndy Whitcroft \?|: 20219c0ca6f9SAndy Whitcroft }x; 2022cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 202300df344fSAndy Whitcroft my $off = 0; 20246c72ffaaSAndy Whitcroft 20256c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 20266c72ffaaSAndy Whitcroft 20270a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 20284a0df2efSAndy Whitcroft $off += length($elements[$n]); 20294a0df2efSAndy Whitcroft 2030773647a0SAndy Whitcroft # Pick up the preceeding and succeeding characters. 2031773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 2032773647a0SAndy Whitcroft my $cc = ''; 2033773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 2034773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 2035773647a0SAndy Whitcroft } 2036773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 2037773647a0SAndy Whitcroft 20384a0df2efSAndy Whitcroft my $a = ''; 20394a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 20404a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 2041cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 20424a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 20434a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 2044773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 20454a0df2efSAndy Whitcroft 20460a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 20474a0df2efSAndy Whitcroft 20484a0df2efSAndy Whitcroft my $c = ''; 20490a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 20504a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 20514a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 2052cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 20534a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 20544a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 20558b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 20564a0df2efSAndy Whitcroft } else { 20574a0df2efSAndy Whitcroft $c = 'E'; 20580a920b5bSAndy Whitcroft } 20590a920b5bSAndy Whitcroft 20604a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 20614a0df2efSAndy Whitcroft 20624a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 20634a0df2efSAndy Whitcroft 20646c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 2065de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 20660a920b5bSAndy Whitcroft 206774048ed8SAndy Whitcroft # Pull out the value of this operator. 20686c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 20690a920b5bSAndy Whitcroft 20701f65f947SAndy Whitcroft # Get the full operator variant. 20711f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 20721f65f947SAndy Whitcroft 207313214adfSAndy Whitcroft # Ignore operators passed as parameters. 207413214adfSAndy Whitcroft if ($op_type ne 'V' && 207513214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 207613214adfSAndy Whitcroft 2077cf655043SAndy Whitcroft# # Ignore comments 2078cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 207913214adfSAndy Whitcroft 2080d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 208113214adfSAndy Whitcroft } elsif ($op eq ';') { 2082cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 2083cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 2084773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 2085d8aaf121SAndy Whitcroft } 2086d8aaf121SAndy Whitcroft 2087d8aaf121SAndy Whitcroft # // is a comment 2088d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 20890a920b5bSAndy Whitcroft 20901f65f947SAndy Whitcroft # No spaces for: 20911f65f947SAndy Whitcroft # -> 20921f65f947SAndy Whitcroft # : when part of a bitfield 20931f65f947SAndy Whitcroft } elsif ($op eq '->' || $opv eq ':B') { 20944a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 2095773647a0SAndy Whitcroft ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); 20960a920b5bSAndy Whitcroft } 20970a920b5bSAndy Whitcroft 20980a920b5bSAndy Whitcroft # , must have a space on the right. 20990a920b5bSAndy Whitcroft } elsif ($op eq ',') { 2100cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2101773647a0SAndy Whitcroft ERROR("space required after that '$op' $at\n" . $hereptr); 21020a920b5bSAndy Whitcroft } 21030a920b5bSAndy Whitcroft 21049c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 210574048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 21069c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 21079c0ca6f9SAndy Whitcroft 21089c0ca6f9SAndy Whitcroft # unary operators should have a space before and 21099c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 21109c0ca6f9SAndy Whitcroft # unary operator, or a cast 21119c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 211274048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 21130d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 2114cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2115773647a0SAndy Whitcroft ERROR("space required before that '$op' $at\n" . $hereptr); 21160a920b5bSAndy Whitcroft } 2117a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2118171ae1a4SAndy Whitcroft # A unary '*' may be const 2119171ae1a4SAndy Whitcroft 2120171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 2121fb9e9096SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 21220a920b5bSAndy Whitcroft } 21230a920b5bSAndy Whitcroft 21240a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 21250a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 2126773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2127773647a0SAndy Whitcroft ERROR("space required one side of that '$op' $at\n" . $hereptr); 21280a920b5bSAndy Whitcroft } 2129773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 2130773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 2131773647a0SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 2132653d4876SAndy Whitcroft } 2133773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 2134773647a0SAndy Whitcroft ERROR("space prohibited after that '$op' $at\n" . $hereptr); 2135773647a0SAndy Whitcroft } 2136773647a0SAndy Whitcroft 21370a920b5bSAndy Whitcroft 21380a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 21399c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 21409c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 21419c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 2142c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 2143c2fdda0dSAndy Whitcroft $op eq '%') 21440a920b5bSAndy Whitcroft { 2145773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 2146de7d4f0eSAndy Whitcroft ERROR("need consistent spacing around '$op' $at\n" . 2147de7d4f0eSAndy Whitcroft $hereptr); 21480a920b5bSAndy Whitcroft } 21490a920b5bSAndy Whitcroft 21501f65f947SAndy Whitcroft # A colon needs no spaces before when it is 21511f65f947SAndy Whitcroft # terminating a case value or a label. 21521f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 21531f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 21541f65f947SAndy Whitcroft ERROR("space prohibited before that '$op' $at\n" . $hereptr); 21551f65f947SAndy Whitcroft } 21561f65f947SAndy Whitcroft 21570a920b5bSAndy Whitcroft # All the others need spaces both sides. 2158cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 21591f65f947SAndy Whitcroft my $ok = 0; 21601f65f947SAndy Whitcroft 216122f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 21621f65f947SAndy Whitcroft if (($op eq '<' && 21631f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 21641f65f947SAndy Whitcroft ($op eq '>' && 21651f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 21661f65f947SAndy Whitcroft { 21671f65f947SAndy Whitcroft $ok = 1; 21681f65f947SAndy Whitcroft } 21691f65f947SAndy Whitcroft 21701f65f947SAndy Whitcroft # Ignore ?: 21711f65f947SAndy Whitcroft if (($opv eq ':O' && $ca =~ /\?$/) || 21721f65f947SAndy Whitcroft ($op eq '?' && $cc =~ /^:/)) { 21731f65f947SAndy Whitcroft $ok = 1; 21741f65f947SAndy Whitcroft } 21751f65f947SAndy Whitcroft 21761f65f947SAndy Whitcroft if ($ok == 0) { 2177773647a0SAndy Whitcroft ERROR("spaces required around that '$op' $at\n" . $hereptr); 21780a920b5bSAndy Whitcroft } 217922f2a2efSAndy Whitcroft } 21804a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 21810a920b5bSAndy Whitcroft } 21820a920b5bSAndy Whitcroft } 21830a920b5bSAndy Whitcroft 2184f0a594c1SAndy Whitcroft# check for multiple assignments 2185f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 21866c72ffaaSAndy Whitcroft CHK("multiple assignments should be avoided\n" . $herecurr); 2187f0a594c1SAndy Whitcroft } 2188f0a594c1SAndy Whitcroft 218922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 219022f2a2efSAndy Whitcroft## # continuation. 219122f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 219222f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 219322f2a2efSAndy Whitcroft## 219422f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 219522f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 219622f2a2efSAndy Whitcroft## my $ln = $line; 219722f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 219822f2a2efSAndy Whitcroft## } 219922f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 220022f2a2efSAndy Whitcroft## WARN("declaring multiple variables together should be avoided\n" . $herecurr); 220122f2a2efSAndy Whitcroft## } 220222f2a2efSAndy Whitcroft## } 2203f0a594c1SAndy Whitcroft 22040a920b5bSAndy Whitcroft#need space before brace following if, while, etc 220522f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 220622f2a2efSAndy Whitcroft $line =~ /do{/) { 2207773647a0SAndy Whitcroft ERROR("space required before the open brace '{'\n" . $herecurr); 2208de7d4f0eSAndy Whitcroft } 2209de7d4f0eSAndy Whitcroft 2210de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 2211de7d4f0eSAndy Whitcroft# on the line 2212de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 2213773647a0SAndy Whitcroft ERROR("space required after that close brace '}'\n" . $herecurr); 22140a920b5bSAndy Whitcroft } 22150a920b5bSAndy Whitcroft 221622f2a2efSAndy Whitcroft# check spacing on square brackets 221722f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 2218773647a0SAndy Whitcroft ERROR("space prohibited after that open square bracket '['\n" . $herecurr); 221922f2a2efSAndy Whitcroft } 222022f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 2221773647a0SAndy Whitcroft ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); 222222f2a2efSAndy Whitcroft } 222322f2a2efSAndy Whitcroft 2224c45dcabdSAndy Whitcroft# check spacing on parentheses 22259c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 22269c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 2227773647a0SAndy Whitcroft ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); 222822f2a2efSAndy Whitcroft } 222913214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 2230c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 2231c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 2232773647a0SAndy Whitcroft ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); 223322f2a2efSAndy Whitcroft } 223422f2a2efSAndy Whitcroft 22350a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 22364a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 22370a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 2238de7d4f0eSAndy Whitcroft WARN("labels should not be indented\n" . $herecurr); 22390a920b5bSAndy Whitcroft } 22400a920b5bSAndy Whitcroft 2241c45dcabdSAndy Whitcroft# Return is not a function. 2242c45dcabdSAndy Whitcroft if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 2243c45dcabdSAndy Whitcroft my $spacing = $1; 2244c45dcabdSAndy Whitcroft my $value = $2; 2245c45dcabdSAndy Whitcroft 224686f9d059SAndy Whitcroft # Flatten any parentheses 2247fb2d2c1bSAndy Whitcroft $value =~ s/\(/ \(/g; 2248fb2d2c1bSAndy Whitcroft $value =~ s/\)/\) /g; 224963f17f89SAndy Whitcroft while ($value =~ s/\[[^\{\}]*\]/1/ || 225063f17f89SAndy Whitcroft $value !~ /(?:$Ident|-?$Constant)\s* 225163f17f89SAndy Whitcroft $Compare\s* 225263f17f89SAndy Whitcroft (?:$Ident|-?$Constant)/x && 225363f17f89SAndy Whitcroft $value =~ s/\([^\(\)]*\)/1/) { 2254c45dcabdSAndy Whitcroft } 2255fb2d2c1bSAndy Whitcroft#print "value<$value>\n"; 2256fb2d2c1bSAndy Whitcroft if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { 2257c45dcabdSAndy Whitcroft ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2258c45dcabdSAndy Whitcroft 2259c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 2260c45dcabdSAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 2261c45dcabdSAndy Whitcroft } 2262c45dcabdSAndy Whitcroft } 226353a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 226453a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 226553a3c448SAndy Whitcroft my $name = $1; 226653a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 226753a3c448SAndy Whitcroft WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr); 226853a3c448SAndy Whitcroft } 226953a3c448SAndy Whitcroft } 2270c45dcabdSAndy Whitcroft 22710a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 22724a0df2efSAndy Whitcroft if ($line=~/\b(if|while|for|switch)\(/) { 2273773647a0SAndy Whitcroft ERROR("space required before the open parenthesis '('\n" . $herecurr); 22740a920b5bSAndy Whitcroft } 22750a920b5bSAndy Whitcroft 2276f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 2277f5fe35ddSAndy Whitcroft# statements after the conditional. 2278170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 2279170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 2280170d3a22SAndy Whitcroft $remain_next, $off_next); 2281170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 2282170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 2283170d3a22SAndy Whitcroft 2284170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 2285170d3a22SAndy Whitcroft # If the statement carries leading newlines, 2286170d3a22SAndy Whitcroft # then count those as offsets. 2287170d3a22SAndy Whitcroft my ($whitespace) = 2288170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 2289170d3a22SAndy Whitcroft my $offset = 2290170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 2291170d3a22SAndy Whitcroft 2292170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 2293170d3a22SAndy Whitcroft $offset} = 1; 2294170d3a22SAndy Whitcroft } 2295170d3a22SAndy Whitcroft } 2296170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 2297170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 2298171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 22998905a67cSAndy Whitcroft 2300b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 2301c2fdda0dSAndy Whitcroft ERROR("do not use assignment in if condition\n" . $herecurr); 23028905a67cSAndy Whitcroft } 23038905a67cSAndy Whitcroft 23048905a67cSAndy Whitcroft # Find out what is on the end of the line after the 23058905a67cSAndy Whitcroft # conditional. 2306773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 23078905a67cSAndy Whitcroft $s =~ s/\n.*//g; 230813214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 230953210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 231053210168SAndy Whitcroft $c !~ /}\s*while\s*/) 2311773647a0SAndy Whitcroft { 2312bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 2313bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 2314bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 231542bdf74cSHidetoshi Seto my $stat_real = ''; 2316bb44ad39SAndy Whitcroft 231742bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 231842bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 2319bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 2320bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 2321bb44ad39SAndy Whitcroft } 2322bb44ad39SAndy Whitcroft 2323bb44ad39SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); 23248905a67cSAndy Whitcroft } 23258905a67cSAndy Whitcroft } 23268905a67cSAndy Whitcroft 232713214adfSAndy Whitcroft# Check for bitwise tests written as boolean 232813214adfSAndy Whitcroft if ($line =~ / 232913214adfSAndy Whitcroft (?: 233013214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 233113214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 233213214adfSAndy Whitcroft (?:\&\&|\|\|) 233313214adfSAndy Whitcroft | 233413214adfSAndy Whitcroft (?:\&\&|\|\|) 233513214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 233613214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 233713214adfSAndy Whitcroft )/x) 233813214adfSAndy Whitcroft { 233913214adfSAndy Whitcroft WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 234013214adfSAndy Whitcroft } 234113214adfSAndy Whitcroft 23428905a67cSAndy Whitcroft# if and else should not have general statements after it 234313214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 234413214adfSAndy Whitcroft my $s = $1; 234513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 234613214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 23478905a67cSAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 23480a920b5bSAndy Whitcroft } 234913214adfSAndy Whitcroft } 235039667782SAndy Whitcroft# if should not continue a brace 235139667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 235239667782SAndy Whitcroft ERROR("trailing statements should be on next line\n" . 235339667782SAndy Whitcroft $herecurr); 235439667782SAndy Whitcroft } 2355a1080bf8SAndy Whitcroft# case and default should not have general statements after them 2356a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 2357a1080bf8SAndy Whitcroft $line !~ /\G(?: 23583fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 2359a1080bf8SAndy Whitcroft \s*return\s+ 2360a1080bf8SAndy Whitcroft )/xg) 2361a1080bf8SAndy Whitcroft { 2362a1080bf8SAndy Whitcroft ERROR("trailing statements should be on next line\n" . $herecurr); 2363a1080bf8SAndy Whitcroft } 23640a920b5bSAndy Whitcroft 23650a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 23660a920b5bSAndy Whitcroft # indent level to be relevant to each other. 23670a920b5bSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and 23680a920b5bSAndy Whitcroft $previndent == $indent) { 2369de7d4f0eSAndy Whitcroft ERROR("else should follow close brace '}'\n" . $hereprev); 23700a920b5bSAndy Whitcroft } 23710a920b5bSAndy Whitcroft 2372c2fdda0dSAndy Whitcroft if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and 2373c2fdda0dSAndy Whitcroft $previndent == $indent) { 2374c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 2375c2fdda0dSAndy Whitcroft 2376c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 2377c2fdda0dSAndy Whitcroft # conditional. 2378773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 2379c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 2380c2fdda0dSAndy Whitcroft 2381c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 2382c2fdda0dSAndy Whitcroft ERROR("while should follow close brace '}'\n" . $hereprev); 2383c2fdda0dSAndy Whitcroft } 2384c2fdda0dSAndy Whitcroft } 2385c2fdda0dSAndy Whitcroft 23860a920b5bSAndy Whitcroft#studly caps, commented out until figure out how to distinguish between use of existing and adding new 23870a920b5bSAndy Whitcroft# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 23880a920b5bSAndy Whitcroft# print "No studly caps, use _\n"; 23890a920b5bSAndy Whitcroft# print "$herecurr"; 23900a920b5bSAndy Whitcroft# $clean = 0; 23910a920b5bSAndy Whitcroft# } 23920a920b5bSAndy Whitcroft 23930a920b5bSAndy Whitcroft#no spaces allowed after \ in define 2394c45dcabdSAndy Whitcroft if ($line=~/\#\s*define.*\\\s$/) { 2395de7d4f0eSAndy Whitcroft WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); 23960a920b5bSAndy Whitcroft } 23970a920b5bSAndy Whitcroft 2398653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 2399c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 2400e09dec48SAndy Whitcroft my $file = "$1.h"; 2401e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 2402e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 2403e09dec48SAndy Whitcroft $realfile ne $checkfile && 24047840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 2405c45dcabdSAndy Whitcroft { 2406e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 2407e09dec48SAndy Whitcroft CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2408e09dec48SAndy Whitcroft } else { 2409e09dec48SAndy Whitcroft WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2410e09dec48SAndy Whitcroft } 24110a920b5bSAndy Whitcroft } 24120a920b5bSAndy Whitcroft } 24130a920b5bSAndy Whitcroft 2414653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 2415653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 2416cf655043SAndy Whitcroft# in a known good container 2417b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 2418b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 2419d8aaf121SAndy Whitcroft my $ln = $linenr; 2420d8aaf121SAndy Whitcroft my $cnt = $realcnt; 2421c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 2422c45dcabdSAndy Whitcroft my $ctx = ''; 2423653d4876SAndy Whitcroft 2424c45dcabdSAndy Whitcroft my $args = defined($1); 2425de7d4f0eSAndy Whitcroft 2426c45dcabdSAndy Whitcroft # Find the end of the macro and limit our statement 2427c45dcabdSAndy Whitcroft # search to that. 2428c45dcabdSAndy Whitcroft while ($cnt > 0 && defined $lines[$ln - 1] && 2429c45dcabdSAndy Whitcroft $lines[$ln - 1] =~ /^(?:-|..*\\$)/) 2430c45dcabdSAndy Whitcroft { 2431c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1] . "\n"; 2432a3bb97a7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 2433c45dcabdSAndy Whitcroft $ln++; 2434de7d4f0eSAndy Whitcroft } 2435c45dcabdSAndy Whitcroft $ctx .= $rawlines[$ln - 1]; 2436d8aaf121SAndy Whitcroft 2437c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 2438c45dcabdSAndy Whitcroft ctx_statement_block($linenr, $ln - $linenr + 1, 0); 2439c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2440a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2441c45dcabdSAndy Whitcroft 2442c45dcabdSAndy Whitcroft # Extract the remainder of the define (if any) and 2443c45dcabdSAndy Whitcroft # rip off surrounding spaces, and trailing \'s. 2444c45dcabdSAndy Whitcroft $rest = ''; 2445636d140aSAndy Whitcroft while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { 2446636d140aSAndy Whitcroft #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; 2447a3bb97a7SAndy Whitcroft if ($off != 0 || $lines[$ln - 1] !~ /^-/) { 2448c45dcabdSAndy Whitcroft $rest .= substr($lines[$ln - 1], $off) . "\n"; 2449c45dcabdSAndy Whitcroft $cnt--; 2450a3bb97a7SAndy Whitcroft } 2451a3bb97a7SAndy Whitcroft $ln++; 2452c45dcabdSAndy Whitcroft $off = 0; 2453c45dcabdSAndy Whitcroft } 2454c45dcabdSAndy Whitcroft $rest =~ s/\\\n.//g; 2455c45dcabdSAndy Whitcroft $rest =~ s/^\s*//s; 2456c45dcabdSAndy Whitcroft $rest =~ s/\s*$//s; 2457c45dcabdSAndy Whitcroft 2458c45dcabdSAndy Whitcroft # Clean up the original statement. 2459c45dcabdSAndy Whitcroft if ($args) { 2460c45dcabdSAndy Whitcroft substr($dstat, 0, length($dcond), ''); 2461d8aaf121SAndy Whitcroft } else { 2462c45dcabdSAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; 2463c45dcabdSAndy Whitcroft } 2464292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 2465c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 2466c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 2467c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 2468c45dcabdSAndy Whitcroft 2469c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 2470bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 2471bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 2472bf30d6edSAndy Whitcroft $dstat =~ s/\[[^\{\}]*\]/1/) 2473bf30d6edSAndy Whitcroft { 2474c45dcabdSAndy Whitcroft } 2475c45dcabdSAndy Whitcroft 2476c45dcabdSAndy Whitcroft my $exceptions = qr{ 2477c45dcabdSAndy Whitcroft $Declare| 2478c45dcabdSAndy Whitcroft module_param_named| 2479c45dcabdSAndy Whitcroft MODULE_PARAM_DESC| 2480c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 2481c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 2482383099fdSAndy Whitcroft __typeof__\(| 248322fd2d3eSStefani Seibold union| 248422fd2d3eSStefani Seibold struct| 2485ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 2486ea71a0a0SAndy Whitcroft ^\"|\"$ 2487c45dcabdSAndy Whitcroft }x; 24885eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 24895eaa20b9SAndy Whitcroft if ($rest ne '' && $rest ne ',') { 2490c45dcabdSAndy Whitcroft if ($rest !~ /while\s*\(/ && 2491c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/) 2492c45dcabdSAndy Whitcroft { 2493c45dcabdSAndy Whitcroft ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); 2494c45dcabdSAndy Whitcroft } 2495c45dcabdSAndy Whitcroft 2496c45dcabdSAndy Whitcroft } elsif ($ctx !~ /;/) { 2497c45dcabdSAndy Whitcroft if ($dstat ne '' && 2498c45dcabdSAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant)$/ && 2499c45dcabdSAndy Whitcroft $dstat !~ /$exceptions/ && 2500b132e5d5SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && 2501c45dcabdSAndy Whitcroft $dstat =~ /$Operators/) 2502c45dcabdSAndy Whitcroft { 2503de7d4f0eSAndy Whitcroft ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); 2504d8aaf121SAndy Whitcroft } 25050a920b5bSAndy Whitcroft } 2506653d4876SAndy Whitcroft } 25070a920b5bSAndy Whitcroft 2508080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 2509080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 2510080ba929SMike Frysinger# . 2511080ba929SMike Frysinger# ALIGN(...) 2512080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 2513080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 2514080ba929SMike Frysinger WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 2515080ba929SMike Frysinger } 2516080ba929SMike Frysinger 2517f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 251813214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 251913214adfSAndy Whitcroft my ($level, $endln, @chunks) = 2520cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 252113214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2522cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2523cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 252413214adfSAndy Whitcroft my $allowed = 0; 252513214adfSAndy Whitcroft my $seen = 0; 2526773647a0SAndy Whitcroft my $herectx = $here . "\n"; 2527cf655043SAndy Whitcroft my $ln = $linenr - 1; 252813214adfSAndy Whitcroft for my $chunk (@chunks) { 252913214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 253013214adfSAndy Whitcroft 2531773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 2532773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2533773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 2534773647a0SAndy Whitcroft 2535773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2536773647a0SAndy Whitcroft 2537773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 2538773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 2539773647a0SAndy Whitcroft 2540773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 2541cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 2542cf655043SAndy Whitcroft 2543773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 254413214adfSAndy Whitcroft 254513214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 254613214adfSAndy Whitcroft 2547cf655043SAndy Whitcroft #print "cond<$cond> block<$block> allowed<$allowed>\n"; 2548cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2549cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 255013214adfSAndy Whitcroft $allowed = 1; 255113214adfSAndy Whitcroft } 255213214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2553cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 255413214adfSAndy Whitcroft $allowed = 1; 255513214adfSAndy Whitcroft } 2556cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2557cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 255813214adfSAndy Whitcroft $allowed = 1; 255913214adfSAndy Whitcroft } 256013214adfSAndy Whitcroft } 256113214adfSAndy Whitcroft if ($seen && !$allowed) { 2562cf655043SAndy Whitcroft WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); 256313214adfSAndy Whitcroft } 256413214adfSAndy Whitcroft } 256513214adfSAndy Whitcroft } 2566773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 256713214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 2568cf655043SAndy Whitcroft my $allowed = 0; 2569f0a594c1SAndy Whitcroft 2570cf655043SAndy Whitcroft # Check the pre-context. 2571cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 2572cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 2573cf655043SAndy Whitcroft $allowed = 1; 2574f0a594c1SAndy Whitcroft } 2575773647a0SAndy Whitcroft 2576773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 2577773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 2578773647a0SAndy Whitcroft 2579cf655043SAndy Whitcroft # Check the condition. 2580cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 2581773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 2582cf655043SAndy Whitcroft if (defined $cond) { 2583773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2584cf655043SAndy Whitcroft } 2585cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 2586cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 2587cf655043SAndy Whitcroft $allowed = 1; 2588cf655043SAndy Whitcroft } 2589cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 2590cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 2591cf655043SAndy Whitcroft $allowed = 1; 2592cf655043SAndy Whitcroft } 2593cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 2594cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 2595cf655043SAndy Whitcroft $allowed = 1; 2596cf655043SAndy Whitcroft } 2597cf655043SAndy Whitcroft # Check the post-context. 2598cf655043SAndy Whitcroft if (defined $chunks[1]) { 2599cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 2600cf655043SAndy Whitcroft if (defined $cond) { 2601773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 2602cf655043SAndy Whitcroft } 2603cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 2604cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 2605cf655043SAndy Whitcroft $allowed = 1; 2606cf655043SAndy Whitcroft } 2607cf655043SAndy Whitcroft } 2608cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2609cf655043SAndy Whitcroft my $herectx = $here . "\n";; 2610f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 2611cf655043SAndy Whitcroft 2612f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 2613f055663cSAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n";; 2614cf655043SAndy Whitcroft } 2615cf655043SAndy Whitcroft 2616cf655043SAndy Whitcroft WARN("braces {} are not necessary for single statement blocks\n" . $herectx); 2617f0a594c1SAndy Whitcroft } 2618f0a594c1SAndy Whitcroft } 2619f0a594c1SAndy Whitcroft 2620653d4876SAndy Whitcroft# don't include deprecated include files (uses RAW line) 26214a0df2efSAndy Whitcroft for my $inc (@dep_includes) { 2622c45dcabdSAndy Whitcroft if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 2623de7d4f0eSAndy Whitcroft ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); 26240a920b5bSAndy Whitcroft } 26250a920b5bSAndy Whitcroft } 26260a920b5bSAndy Whitcroft 26274a0df2efSAndy Whitcroft# don't use deprecated functions 26284a0df2efSAndy Whitcroft for my $func (@dep_functions) { 262900df344fSAndy Whitcroft if ($line =~ /\b$func\b/) { 2630de7d4f0eSAndy Whitcroft ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); 26314a0df2efSAndy Whitcroft } 26324a0df2efSAndy Whitcroft } 26334a0df2efSAndy Whitcroft 26344a0df2efSAndy Whitcroft# no volatiles please 26356c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 26366c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 2637de7d4f0eSAndy Whitcroft WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 26384a0df2efSAndy Whitcroft } 26394a0df2efSAndy Whitcroft 26409c0ca6f9SAndy Whitcroft# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated 26419c0ca6f9SAndy Whitcroft if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { 26429c0ca6f9SAndy Whitcroft ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); 26439c0ca6f9SAndy Whitcroft } 26449c0ca6f9SAndy Whitcroft 264500df344fSAndy Whitcroft# warn about #if 0 2646c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 2647de7d4f0eSAndy Whitcroft CHK("if this code is redundant consider removing it\n" . 2648de7d4f0eSAndy Whitcroft $herecurr); 26494a0df2efSAndy Whitcroft } 26504a0df2efSAndy Whitcroft 2651f0a594c1SAndy Whitcroft# check for needless kfree() checks 2652f0a594c1SAndy Whitcroft if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 2653f0a594c1SAndy Whitcroft my $expr = $1; 2654f0a594c1SAndy Whitcroft if ($line =~ /\bkfree\(\Q$expr\E\);/) { 26553c232147SWolfram Sang WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); 2656f0a594c1SAndy Whitcroft } 2657f0a594c1SAndy Whitcroft } 26584c432a8fSGreg Kroah-Hartman# check for needless usb_free_urb() checks 26594c432a8fSGreg Kroah-Hartman if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 26604c432a8fSGreg Kroah-Hartman my $expr = $1; 26614c432a8fSGreg Kroah-Hartman if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { 26624c432a8fSGreg Kroah-Hartman WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); 26634c432a8fSGreg Kroah-Hartman } 26644c432a8fSGreg Kroah-Hartman } 2665f0a594c1SAndy Whitcroft 26661a15a250SPatrick Pannuto# prefer usleep_range over udelay 26671a15a250SPatrick Pannuto if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { 26681a15a250SPatrick Pannuto # ignore udelay's < 10, however 26691a15a250SPatrick Pannuto if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { 26701a15a250SPatrick Pannuto CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); 26711a15a250SPatrick Pannuto } 26721a15a250SPatrick Pannuto } 26731a15a250SPatrick Pannuto 267409ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 267509ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 267609ef8725SPatrick Pannuto if ($1 < 20) { 267709ef8725SPatrick Pannuto WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); 267809ef8725SPatrick Pannuto } 267909ef8725SPatrick Pannuto } 268009ef8725SPatrick Pannuto 268100df344fSAndy Whitcroft# warn about #ifdefs in C files 2682c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 268300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 268400df344fSAndy Whitcroft# print "$herecurr"; 268500df344fSAndy Whitcroft# $clean = 0; 268600df344fSAndy Whitcroft# } 268700df344fSAndy Whitcroft 268822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 2689c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 269022f2a2efSAndy Whitcroft ERROR("exactly one space required after that #$1\n" . $herecurr); 269122f2a2efSAndy Whitcroft } 269222f2a2efSAndy Whitcroft 26934a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 2694171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 2695171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 26964a0df2efSAndy Whitcroft my $which = $1; 26974a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2698de7d4f0eSAndy Whitcroft CHK("$1 definition without comment\n" . $herecurr); 26994a0df2efSAndy Whitcroft } 27004a0df2efSAndy Whitcroft } 27014a0df2efSAndy Whitcroft# check for memory barriers without a comment. 27024a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 27034a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 2704de7d4f0eSAndy Whitcroft CHK("memory barrier without comment\n" . $herecurr); 27054a0df2efSAndy Whitcroft } 27064a0df2efSAndy Whitcroft } 27074a0df2efSAndy Whitcroft# check of hardware specific defines 2708c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 2709de7d4f0eSAndy Whitcroft CHK("architecture specific defines should be avoided\n" . $herecurr); 27100a920b5bSAndy Whitcroft } 2711653d4876SAndy Whitcroft 2712d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 2713d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 2714d4977c78STobias Klauser WARN("storage class should be at the beginning of the declaration\n" . $herecurr) 2715d4977c78STobias Klauser } 2716d4977c78STobias Klauser 2717de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 2718de7d4f0eSAndy Whitcroft# storage class and type. 27199c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 27209c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 2721de7d4f0eSAndy Whitcroft ERROR("inline keyword should sit between storage class and type\n" . $herecurr); 2722de7d4f0eSAndy Whitcroft } 2723de7d4f0eSAndy Whitcroft 27248905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 27258905a67cSAndy Whitcroft if ($line =~ /\b(__inline__|__inline)\b/) { 27268905a67cSAndy Whitcroft WARN("plain inline is preferred over $1\n" . $herecurr); 27278905a67cSAndy Whitcroft } 27288905a67cSAndy Whitcroft 27298f53a9b8SJoe Perches# check for sizeof(&) 27308f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 27318f53a9b8SJoe Perches WARN("sizeof(& should be avoided\n" . $herecurr); 27328f53a9b8SJoe Perches } 27338f53a9b8SJoe Perches 2734de7d4f0eSAndy Whitcroft# check for new externs in .c files. 2735171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 2736c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2737171ae1a4SAndy Whitcroft { 2738c45dcabdSAndy Whitcroft my $function_name = $1; 2739c45dcabdSAndy Whitcroft my $paren_space = $2; 2740171ae1a4SAndy Whitcroft 2741171ae1a4SAndy Whitcroft my $s = $stat; 2742171ae1a4SAndy Whitcroft if (defined $cond) { 2743171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 2744171ae1a4SAndy Whitcroft } 2745c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 2746c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 2747c45dcabdSAndy Whitcroft { 2748de7d4f0eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2749de7d4f0eSAndy Whitcroft } 2750de7d4f0eSAndy Whitcroft 2751171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 2752171ae1a4SAndy Whitcroft WARN("arguments for function declarations should follow identifier\n" . $herecurr); 2753171ae1a4SAndy Whitcroft } 27549c9ba34eSAndy Whitcroft 27559c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 27569c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 27579c9ba34eSAndy Whitcroft { 27589c9ba34eSAndy Whitcroft WARN("externs should be avoided in .c files\n" . $herecurr); 2759171ae1a4SAndy Whitcroft } 2760171ae1a4SAndy Whitcroft 2761de7d4f0eSAndy Whitcroft# checks for new __setup's 2762de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 2763de7d4f0eSAndy Whitcroft my $name = $1; 2764de7d4f0eSAndy Whitcroft 2765de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 2766de7d4f0eSAndy Whitcroft CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 2767de7d4f0eSAndy Whitcroft } 2768653d4876SAndy Whitcroft } 27699c0ca6f9SAndy Whitcroft 27709c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 27719c0ca6f9SAndy Whitcroft if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 27729c0ca6f9SAndy Whitcroft WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 27739c0ca6f9SAndy Whitcroft } 277413214adfSAndy Whitcroft 277513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 277613214adfSAndy Whitcroft if ($line =~ /__FUNCTION__/) { 277713214adfSAndy Whitcroft WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 277813214adfSAndy Whitcroft } 2779773647a0SAndy Whitcroft 2780773647a0SAndy Whitcroft# check for semaphores used as mutexes 2781171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2782773647a0SAndy Whitcroft WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); 2783773647a0SAndy Whitcroft } 2784773647a0SAndy Whitcroft# check for semaphores used as mutexes 2785171ae1a4SAndy Whitcroft if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { 2786773647a0SAndy Whitcroft WARN("consider using a completion\n" . $herecurr); 27871704f47bSPeter Zijlstra 2788773647a0SAndy Whitcroft } 2789773647a0SAndy Whitcroft# recommend strict_strto* over simple_strto* 2790773647a0SAndy Whitcroft if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2791773647a0SAndy Whitcroft WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); 2792773647a0SAndy Whitcroft } 2793f3db6639SMichael Ellerman# check for __initcall(), use device_initcall() explicitly please 2794f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 2795f3db6639SMichael Ellerman WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2796f3db6639SMichael Ellerman } 279779404849SEmese Revfy# check for various ops structs, ensure they are const. 279879404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 279979404849SEmese Revfy address_space_operations| 280079404849SEmese Revfy backlight_ops| 280179404849SEmese Revfy block_device_operations| 280279404849SEmese Revfy dentry_operations| 280379404849SEmese Revfy dev_pm_ops| 280479404849SEmese Revfy dma_map_ops| 280579404849SEmese Revfy extent_io_ops| 280679404849SEmese Revfy file_lock_operations| 280779404849SEmese Revfy file_operations| 280879404849SEmese Revfy hv_ops| 280979404849SEmese Revfy ide_dma_ops| 281079404849SEmese Revfy intel_dvo_dev_ops| 281179404849SEmese Revfy item_operations| 281279404849SEmese Revfy iwl_ops| 281379404849SEmese Revfy kgdb_arch| 281479404849SEmese Revfy kgdb_io| 281579404849SEmese Revfy kset_uevent_ops| 281679404849SEmese Revfy lock_manager_operations| 281779404849SEmese Revfy microcode_ops| 281879404849SEmese Revfy mtrr_ops| 281979404849SEmese Revfy neigh_ops| 282079404849SEmese Revfy nlmsvc_binding| 282179404849SEmese Revfy pci_raw_ops| 282279404849SEmese Revfy pipe_buf_operations| 282379404849SEmese Revfy platform_hibernation_ops| 282479404849SEmese Revfy platform_suspend_ops| 282579404849SEmese Revfy proto_ops| 282679404849SEmese Revfy rpc_pipe_ops| 282779404849SEmese Revfy seq_operations| 282879404849SEmese Revfy snd_ac97_build_ops| 282979404849SEmese Revfy soc_pcmcia_socket_ops| 283079404849SEmese Revfy stacktrace_ops| 283179404849SEmese Revfy sysfs_ops| 283279404849SEmese Revfy tty_operations| 283379404849SEmese Revfy usb_mon_operations| 283479404849SEmese Revfy wd_ops}x; 28356903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 283679404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 28376903ffb2SAndy Whitcroft WARN("struct $1 should normally be const\n" . 28386903ffb2SAndy Whitcroft $herecurr); 28392b6db5cbSAndy Whitcroft } 2840773647a0SAndy Whitcroft 2841773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 2842773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 2843773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 2844c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 2845c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 2846171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 2847171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 2848171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 2849773647a0SAndy Whitcroft { 2850773647a0SAndy Whitcroft WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 2851773647a0SAndy Whitcroft } 28529c9ba34eSAndy Whitcroft 28539c9ba34eSAndy Whitcroft# check for %L{u,d,i} in strings 28549c9ba34eSAndy Whitcroft my $string; 28559c9ba34eSAndy Whitcroft while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 28569c9ba34eSAndy Whitcroft $string = substr($rawline, $-[1], $+[1] - $-[1]); 28572a1bc5d5SAndy Whitcroft $string =~ s/%%/__/g; 28589c9ba34eSAndy Whitcroft if ($string =~ /(?<!%)%L[udi]/) { 28599c9ba34eSAndy Whitcroft WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 28609c9ba34eSAndy Whitcroft last; 28619c9ba34eSAndy Whitcroft } 28629c9ba34eSAndy Whitcroft } 2863691d77b6SAndy Whitcroft 2864691d77b6SAndy Whitcroft# whine mightly about in_atomic 2865691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 2866691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 2867691d77b6SAndy Whitcroft ERROR("do not use in_atomic in drivers\n" . $herecurr); 2868f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 2869691d77b6SAndy Whitcroft WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2870691d77b6SAndy Whitcroft } 2871691d77b6SAndy Whitcroft } 28721704f47bSPeter Zijlstra 28731704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 28741704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 28751704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 28761704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 28771704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 28781704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 28791704f47bSPeter Zijlstra ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 28801704f47bSPeter Zijlstra } 28811704f47bSPeter Zijlstra } 288213214adfSAndy Whitcroft } 288313214adfSAndy Whitcroft 288413214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 288513214adfSAndy Whitcroft # so just keep quiet. 288613214adfSAndy Whitcroft if ($#rawlines == -1) { 288713214adfSAndy Whitcroft exit(0); 28880a920b5bSAndy Whitcroft } 28890a920b5bSAndy Whitcroft 28908905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 28918905a67cSAndy Whitcroft # things that appear to be patches. 28928905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 28938905a67cSAndy Whitcroft exit(0); 28948905a67cSAndy Whitcroft } 28958905a67cSAndy Whitcroft 28968905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 28978905a67cSAndy Whitcroft # just keep quiet. 28988905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 28998905a67cSAndy Whitcroft exit(0); 29008905a67cSAndy Whitcroft } 29018905a67cSAndy Whitcroft 29028905a67cSAndy Whitcroft if (!$is_patch) { 2903de7d4f0eSAndy Whitcroft ERROR("Does not appear to be a unified-diff format patch\n"); 29040a920b5bSAndy Whitcroft } 29050a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 2906de7d4f0eSAndy Whitcroft ERROR("Missing Signed-off-by: line(s)\n"); 29070a920b5bSAndy Whitcroft } 29080a920b5bSAndy Whitcroft 2909f0a594c1SAndy Whitcroft print report_dump(); 291013214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 291113214adfSAndy Whitcroft print "$filename " if ($summary_file); 29126c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 29136c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 29146c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 29158905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 29166c72ffaaSAndy Whitcroft } 29178905a67cSAndy Whitcroft 2918d2c0a235SAndy Whitcroft if ($quiet == 0) { 2919d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 2920d2c0a235SAndy Whitcroft # then suggest that. 2921d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 2922d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 2923d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 2924d2c0a235SAndy Whitcroft } 2925d2c0a235SAndy Whitcroft } 2926d2c0a235SAndy Whitcroft 29270a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 2928c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 29290a920b5bSAndy Whitcroft } 29300a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 2931c2fdda0dSAndy Whitcroft print "$vname has style problems, please review. If any of these errors\n"; 29320a920b5bSAndy Whitcroft print "are false positives report them to the maintainer, see\n"; 29330a920b5bSAndy Whitcroft print "CHECKPATCH in MAINTAINERS.\n"; 29340a920b5bSAndy Whitcroft } 293513214adfSAndy Whitcroft 29360a920b5bSAndy Whitcroft return $clean; 29370a920b5bSAndy Whitcroft} 2938