Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Dave Jones | dbf004d | 2010-01-12 16:59:52 -0500 | [diff] [blame] | 2 | # (c) 2001, Dave Jones. (the file handling bit) |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
Andy Whitcroft | 2a5a2c2 | 2009-01-06 14:41:23 -0800 | [diff] [blame] | 4 | # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) |
Andy Whitcroft | 131edb3 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 5 | # (c) 2008,2009, Andy Whitcroft <apw@canonical.com> |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 6 | # Licensed under the terms of the GNU GPL License version 2 |
| 7 | |
| 8 | use strict; |
| 9 | |
| 10 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 11 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 12 | |
Andy Whitcroft | 5e8d8f6 | 2009-10-26 16:50:17 -0700 | [diff] [blame] | 13 | my $V = '0.30'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 14 | |
| 15 | use Getopt::Long qw(:config no_auto_abbrev); |
| 16 | |
| 17 | my $quiet = 0; |
| 18 | my $tree = 1; |
| 19 | my $chk_signoff = 1; |
| 20 | my $chk_patch = 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 21 | my $tst_only; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 22 | my $emacs = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 23 | my $terse = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 24 | my $file = 0; |
| 25 | my $check = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 26 | my $summary = 1; |
| 27 | my $mailback = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 28 | my $summary_file = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 29 | my $root; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 30 | my %debug; |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 31 | my $help = 0; |
| 32 | |
| 33 | sub help { |
| 34 | my ($exitcode) = @_; |
| 35 | |
| 36 | print << "EOM"; |
| 37 | Usage: $P [OPTION]... [FILE]... |
| 38 | Version: $V |
| 39 | |
| 40 | Options: |
| 41 | -q, --quiet quiet |
| 42 | --no-tree run without a kernel tree |
| 43 | --no-signoff do not check for 'Signed-off-by' line |
| 44 | --patch treat FILE as patchfile (default) |
| 45 | --emacs emacs compile window format |
| 46 | --terse one line per report |
| 47 | -f, --file treat FILE as regular source file |
| 48 | --subjective, --strict enable more subjective tests |
| 49 | --root=PATH PATH to the kernel tree root |
| 50 | --no-summary suppress the per-file summary |
| 51 | --mailback only produce a report in case of warnings/errors |
| 52 | --summary-file include the filename in summary |
| 53 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of |
| 54 | 'values', 'possible', 'type', and 'attr' (default |
| 55 | is all off) |
| 56 | --test-only=WORD report only warnings/errors containing WORD |
| 57 | literally |
| 58 | -h, --help, --version display this help and exit |
| 59 | |
| 60 | When FILE is - read standard input. |
| 61 | EOM |
| 62 | |
| 63 | exit($exitcode); |
| 64 | } |
| 65 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 66 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 67 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 68 | 'tree!' => \$tree, |
| 69 | 'signoff!' => \$chk_signoff, |
| 70 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 71 | 'emacs!' => \$emacs, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 72 | 'terse!' => \$terse, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 73 | 'f|file!' => \$file, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 74 | 'subjective!' => \$check, |
| 75 | 'strict!' => \$check, |
| 76 | 'root=s' => \$root, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 77 | 'summary!' => \$summary, |
| 78 | 'mailback!' => \$mailback, |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 79 | 'summary-file!' => \$summary_file, |
| 80 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 81 | 'debug=s' => \%debug, |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 82 | 'test-only=s' => \$tst_only, |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 83 | 'h|help' => \$help, |
| 84 | 'version' => \$help |
| 85 | ) or help(1); |
| 86 | |
| 87 | help(0) if ($help); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 88 | |
| 89 | my $exit = 0; |
| 90 | |
| 91 | if ($#ARGV < 0) { |
Hannes Eder | 77f5b10 | 2009-09-21 17:04:37 -0700 | [diff] [blame] | 92 | print "$P: no input files\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 93 | exit(1); |
| 94 | } |
| 95 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 96 | my $dbg_values = 0; |
| 97 | my $dbg_possible = 0; |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 98 | my $dbg_type = 0; |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 99 | my $dbg_attr = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 100 | for my $key (keys %debug) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 101 | ## no critic |
| 102 | eval "\${dbg_$key} = '$debug{$key}';"; |
| 103 | die "$@" if ($@); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 106 | my $rpt_cleaners = 0; |
| 107 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 108 | if ($terse) { |
| 109 | $emacs = 1; |
| 110 | $quiet++; |
| 111 | } |
| 112 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 113 | if ($tree) { |
| 114 | if (defined $root) { |
| 115 | if (!top_of_kernel_tree($root)) { |
| 116 | die "$P: $root: --root does not point at a valid tree\n"; |
| 117 | } |
| 118 | } else { |
| 119 | if (top_of_kernel_tree('.')) { |
| 120 | $root = '.'; |
| 121 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 122 | top_of_kernel_tree($1)) { |
| 123 | $root = $1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (!defined $root) { |
| 128 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 129 | exit(2); |
| 130 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 133 | my $emitted_corrupt = 0; |
| 134 | |
Andy Whitcroft | 2ceb532 | 2009-10-26 16:50:14 -0700 | [diff] [blame] | 135 | our $Ident = qr{ |
| 136 | [A-Za-z_][A-Za-z\d_]* |
| 137 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* |
| 138 | }x; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 139 | our $Storage = qr{extern|static|asmlinkage}; |
| 140 | our $Sparse = qr{ |
| 141 | __user| |
| 142 | __kernel| |
| 143 | __force| |
| 144 | __iomem| |
| 145 | __must_check| |
| 146 | __init_refok| |
Andy Whitcroft | 417495e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 147 | __kprobes| |
| 148 | __ref |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 149 | }x; |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 150 | |
| 151 | # Notes to $Attribute: |
| 152 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 153 | our $Attribute = qr{ |
| 154 | const| |
| 155 | __read_mostly| |
| 156 | __kprobes| |
Wolfram Sang | 5213129 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 157 | __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| |
Andy Whitcroft | 24e1d81 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 158 | ____cacheline_aligned| |
| 159 | ____cacheline_aligned_in_smp| |
Andy Whitcroft | 5fe3af1 | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 160 | ____cacheline_internodealigned_in_smp| |
| 161 | __weak |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 162 | }x; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 163 | our $Modifier; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 164 | our $Inline = qr{inline|__always_inline|noinline}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 165 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 166 | our $Lval = qr{$Ident(?:$Member)*}; |
| 167 | |
| 168 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; |
| 169 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 170 | our $Compare = qr{<=|>=|==|!=|<|>}; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 171 | our $Operators = qr{ |
| 172 | <=|>=|==|!=| |
| 173 | =>|->|<<|>>|<|>|!|~| |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 174 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 175 | }x; |
| 176 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 177 | our $NonptrType; |
| 178 | our $Type; |
| 179 | our $Declare; |
| 180 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 181 | our $UTF8 = qr { |
| 182 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 183 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 184 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 185 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 186 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 187 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 188 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 189 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 190 | }x; |
| 191 | |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 192 | our $typeTypedefs = qr{(?x: |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 193 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 194 | atomic_t |
| 195 | )}; |
| 196 | |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 197 | our $logFunctions = qr{(?x: |
| 198 | printk| |
| 199 | pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)| |
Joe Perches | 8bbea96 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 200 | (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)| |
Joe Perches | 691e669 | 2010-03-05 13:43:51 -0800 | [diff] [blame] | 201 | WARN| |
| 202 | panic |
| 203 | )}; |
| 204 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 205 | our @typeList = ( |
| 206 | qr{void}, |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 207 | qr{(?:unsigned\s+)?char}, |
| 208 | qr{(?:unsigned\s+)?short}, |
| 209 | qr{(?:unsigned\s+)?int}, |
| 210 | qr{(?:unsigned\s+)?long}, |
| 211 | qr{(?:unsigned\s+)?long\s+int}, |
| 212 | qr{(?:unsigned\s+)?long\s+long}, |
| 213 | qr{(?:unsigned\s+)?long\s+long\s+int}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 214 | qr{unsigned}, |
| 215 | qr{float}, |
| 216 | qr{double}, |
| 217 | qr{bool}, |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 218 | qr{struct\s+$Ident}, |
| 219 | qr{union\s+$Ident}, |
| 220 | qr{enum\s+$Ident}, |
| 221 | qr{${Ident}_t}, |
| 222 | qr{${Ident}_handler}, |
| 223 | qr{${Ident}_handler_fn}, |
| 224 | ); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 225 | our @modifierList = ( |
| 226 | qr{fastcall}, |
| 227 | ); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 228 | |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 229 | our $allowed_asm_includes = qr{(?x: |
| 230 | irq| |
| 231 | memory |
| 232 | )}; |
| 233 | # memory.h: ARM has a custom one |
| 234 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 235 | sub build_types { |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 236 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
| 237 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 238 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 239 | $NonptrType = qr{ |
Andy Whitcroft | d2172eb | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 240 | (?:$Modifier\s+|const\s+)* |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 241 | (?: |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 242 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 243 | (?:$typeTypedefs\b)| |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 244 | (?:${all}\b) |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 245 | ) |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 246 | (?:\s+$Modifier|\s+const)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 247 | }x; |
| 248 | $Type = qr{ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 249 | $NonptrType |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 250 | (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 251 | (?:\s+$Inline|\s+$Modifier)* |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 252 | }x; |
| 253 | $Declare = qr{(?:$Storage\s+)?$Type}; |
| 254 | } |
| 255 | build_types(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 256 | |
| 257 | $chk_signoff = 0 if ($file); |
| 258 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 259 | my @dep_includes = (); |
| 260 | my @dep_functions = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 261 | my $removal = "Documentation/feature-removal-schedule.txt"; |
| 262 | if ($tree && -f "$root/$removal") { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 263 | open(my $REMOVE, '<', "$root/$removal") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 264 | die "$P: $removal: open failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 265 | while (<$REMOVE>) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 266 | if (/^Check:\s+(.*\S)/) { |
| 267 | for my $entry (split(/[, ]+/, $1)) { |
| 268 | if ($entry =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 269 | push(@dep_includes, $1); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 270 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 271 | } elsif ($entry !~ m@/@) { |
| 272 | push(@dep_functions, $entry); |
| 273 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 274 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 277 | close($REMOVE); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 280 | my @rawlines = (); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 281 | my @lines = (); |
| 282 | my $vname; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 283 | for my $filename (@ARGV) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 284 | my $FILE; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 285 | if ($file) { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 286 | open($FILE, '-|', "diff -u /dev/null $filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 287 | die "$P: $filename: diff failed - $!\n"; |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 288 | } elsif ($filename eq '-') { |
| 289 | open($FILE, '<&STDIN'); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 290 | } else { |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 291 | open($FILE, '<', "$filename") || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 292 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 293 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 294 | if ($filename eq '-') { |
| 295 | $vname = 'Your patch'; |
| 296 | } else { |
| 297 | $vname = $filename; |
| 298 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 299 | while (<$FILE>) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 300 | chomp; |
| 301 | push(@rawlines, $_); |
| 302 | } |
Andy Whitcroft | 21caa13 | 2009-01-06 14:41:30 -0800 | [diff] [blame] | 303 | close($FILE); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 304 | if (!process($filename)) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 305 | $exit = 1; |
| 306 | } |
| 307 | @rawlines = (); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 308 | @lines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | exit($exit); |
| 312 | |
| 313 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 314 | my ($root) = @_; |
| 315 | |
| 316 | my @tree_check = ( |
| 317 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 318 | "README", "Documentation", "arch", "include", "drivers", |
| 319 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 320 | ); |
| 321 | |
| 322 | foreach my $check (@tree_check) { |
| 323 | if (! -e $root . '/' . $check) { |
| 324 | return 0; |
| 325 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 326 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 327 | return 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | sub expand_tabs { |
| 331 | my ($str) = @_; |
| 332 | |
| 333 | my $res = ''; |
| 334 | my $n = 0; |
| 335 | for my $c (split(//, $str)) { |
| 336 | if ($c eq "\t") { |
| 337 | $res .= ' '; |
| 338 | $n++; |
| 339 | for (; ($n % 8) != 0; $n++) { |
| 340 | $res .= ' '; |
| 341 | } |
| 342 | next; |
| 343 | } |
| 344 | $res .= $c; |
| 345 | $n++; |
| 346 | } |
| 347 | |
| 348 | return $res; |
| 349 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 350 | sub copy_spacing { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 351 | (my $res = shift) =~ tr/\t/ /c; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 352 | return $res; |
| 353 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 354 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 355 | sub line_stats { |
| 356 | my ($line) = @_; |
| 357 | |
| 358 | # Drop the diff line leader and expand tabs |
| 359 | $line =~ s/^.//; |
| 360 | $line = expand_tabs($line); |
| 361 | |
| 362 | # Pick the indent from the front of the line. |
| 363 | my ($white) = ($line =~ /^(\s*)/); |
| 364 | |
| 365 | return (length($line), length($white)); |
| 366 | } |
| 367 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 368 | my $sanitise_quote = ''; |
| 369 | |
| 370 | sub sanitise_line_reset { |
| 371 | my ($in_comment) = @_; |
| 372 | |
| 373 | if ($in_comment) { |
| 374 | $sanitise_quote = '*/'; |
| 375 | } else { |
| 376 | $sanitise_quote = ''; |
| 377 | } |
| 378 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 379 | sub sanitise_line { |
| 380 | my ($line) = @_; |
| 381 | |
| 382 | my $res = ''; |
| 383 | my $l = ''; |
| 384 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 385 | my $qlen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 386 | my $off = 0; |
| 387 | my $c; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 388 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 389 | # Always copy over the diff marker. |
| 390 | $res = substr($line, 0, 1); |
| 391 | |
| 392 | for ($off = 1; $off < length($line); $off++) { |
| 393 | $c = substr($line, $off, 1); |
| 394 | |
| 395 | # Comments we are wacking completly including the begin |
| 396 | # and end, all to $;. |
| 397 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { |
| 398 | $sanitise_quote = '*/'; |
| 399 | |
| 400 | substr($res, $off, 2, "$;$;"); |
| 401 | $off++; |
| 402 | next; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 403 | } |
Andy Whitcroft | 81bc0e0 | 2008-10-15 22:02:26 -0700 | [diff] [blame] | 404 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 405 | $sanitise_quote = ''; |
| 406 | substr($res, $off, 2, "$;$;"); |
| 407 | $off++; |
| 408 | next; |
| 409 | } |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 410 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { |
| 411 | $sanitise_quote = '//'; |
| 412 | |
| 413 | substr($res, $off, 2, $sanitise_quote); |
| 414 | $off++; |
| 415 | next; |
| 416 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 417 | |
| 418 | # A \ in a string means ignore the next character. |
| 419 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && |
| 420 | $c eq "\\") { |
| 421 | substr($res, $off, 2, 'XX'); |
| 422 | $off++; |
| 423 | next; |
| 424 | } |
| 425 | # Regular quotes. |
| 426 | if ($c eq "'" || $c eq '"') { |
| 427 | if ($sanitise_quote eq '') { |
| 428 | $sanitise_quote = $c; |
| 429 | |
| 430 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 431 | next; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 432 | } elsif ($sanitise_quote eq $c) { |
| 433 | $sanitise_quote = ''; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 436 | |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 437 | #print "c<$c> SQ<$sanitise_quote>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 438 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
| 439 | substr($res, $off, 1, $;); |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 440 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
| 441 | substr($res, $off, 1, $;); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 442 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
| 443 | substr($res, $off, 1, 'X'); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 444 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 445 | substr($res, $off, 1, $c); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 446 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Daniel Walker | 113f04a | 2009-09-21 17:04:35 -0700 | [diff] [blame] | 449 | if ($sanitise_quote eq '//') { |
| 450 | $sanitise_quote = ''; |
| 451 | } |
| 452 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 453 | # The pathname on a #include may be surrounded by '<' and '>'. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 454 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 455 | my $clean = 'X' x length($1); |
| 456 | $res =~ s@\<.*\>@<$clean>@; |
| 457 | |
| 458 | # The whole of a #error is a string. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 459 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 460 | my $clean = 'X' x length($1); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 461 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 464 | return $res; |
| 465 | } |
| 466 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 467 | sub ctx_statement_block { |
| 468 | my ($linenr, $remain, $off) = @_; |
| 469 | my $line = $linenr - 1; |
| 470 | my $blk = ''; |
| 471 | my $soff = $off; |
| 472 | my $coff = $off - 1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 473 | my $coff_set = 0; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 474 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 475 | my $loff = 0; |
| 476 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 477 | my $type = ''; |
| 478 | my $level = 0; |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 479 | my @stack = (); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 480 | my $p; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 481 | my $c; |
| 482 | my $len = 0; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 483 | |
| 484 | my $remainder; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 485 | while (1) { |
Andy Whitcroft | a275064 | 2009-01-15 13:51:04 -0800 | [diff] [blame] | 486 | @stack = (['', 0]) if ($#stack == -1); |
| 487 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 488 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 489 | # If we are about to drop off the end, pull in more |
| 490 | # context. |
| 491 | if ($off >= $len) { |
| 492 | for (; $remain > 0; $line++) { |
Andy Whitcroft | dea3349 | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 493 | last if (!defined $lines[$line]); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 494 | next if ($lines[$line] =~ /^-/); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 495 | $remain--; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 496 | $loff = $len; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 497 | $blk .= $lines[$line] . "\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 498 | $len = length($blk); |
| 499 | $line++; |
| 500 | last; |
| 501 | } |
| 502 | # Bail if there is no further context. |
| 503 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 504 | if ($off >= $len) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 505 | last; |
| 506 | } |
| 507 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 508 | $p = $c; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 509 | $c = substr($blk, $off, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 510 | $remainder = substr($blk, $off); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 511 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 512 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 513 | |
| 514 | # Handle nested #if/#else. |
| 515 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { |
| 516 | push(@stack, [ $type, $level ]); |
| 517 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { |
| 518 | ($type, $level) = @{$stack[$#stack - 1]}; |
| 519 | } elsif ($remainder =~ /^#\s*endif\b/) { |
| 520 | ($type, $level) = @{pop(@stack)}; |
| 521 | } |
| 522 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 523 | # Statement ends at the ';' or a close '}' at the |
| 524 | # outermost level. |
| 525 | if ($level == 0 && $c eq ';') { |
| 526 | last; |
| 527 | } |
| 528 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 529 | # An else is really a conditional as long as its not else if |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 530 | if ($level == 0 && $coff_set == 0 && |
| 531 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && |
| 532 | $remainder =~ /^(else)(?:\s|{)/ && |
| 533 | $remainder !~ /^else\s+if\b/) { |
| 534 | $coff = $off + length($1) - 1; |
| 535 | $coff_set = 1; |
| 536 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; |
| 537 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 540 | if (($type eq '' || $type eq '(') && $c eq '(') { |
| 541 | $level++; |
| 542 | $type = '('; |
| 543 | } |
| 544 | if ($type eq '(' && $c eq ')') { |
| 545 | $level--; |
| 546 | $type = ($level != 0)? '(' : ''; |
| 547 | |
| 548 | if ($level == 0 && $coff < $soff) { |
| 549 | $coff = $off; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 550 | $coff_set = 1; |
| 551 | #warn "CSB: mark coff<$coff>\n"; |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | if (($type eq '' || $type eq '{') && $c eq '{') { |
| 555 | $level++; |
| 556 | $type = '{'; |
| 557 | } |
| 558 | if ($type eq '{' && $c eq '}') { |
| 559 | $level--; |
| 560 | $type = ($level != 0)? '{' : ''; |
| 561 | |
| 562 | if ($level == 0) { |
Patrick Pannuto | b998e00 | 2010-08-09 17:21:03 -0700 | [diff] [blame] | 563 | if (substr($blk, $off + 1, 1) eq ';') { |
| 564 | $off++; |
| 565 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 566 | last; |
| 567 | } |
| 568 | } |
| 569 | $off++; |
| 570 | } |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 571 | # We are truly at the end, so shuffle to the next line. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 572 | if ($off == $len) { |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 573 | $loff = $len + 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 574 | $line++; |
| 575 | $remain--; |
| 576 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 577 | |
| 578 | my $statement = substr($blk, $soff, $off - $soff + 1); |
| 579 | my $condition = substr($blk, $soff, $coff - $soff + 1); |
| 580 | |
| 581 | #warn "STATEMENT<$statement>\n"; |
| 582 | #warn "CONDITION<$condition>\n"; |
| 583 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 584 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 585 | |
| 586 | return ($statement, $condition, |
| 587 | $line, $remain + 1, $off - $loff + 1, $level); |
| 588 | } |
| 589 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 590 | sub statement_lines { |
| 591 | my ($stmt) = @_; |
| 592 | |
| 593 | # Strip the diff line prefixes and rip blank lines at start and end. |
| 594 | $stmt =~ s/(^|\n)./$1/g; |
| 595 | $stmt =~ s/^\s*//; |
| 596 | $stmt =~ s/\s*$//; |
| 597 | |
| 598 | my @stmt_lines = ($stmt =~ /\n/g); |
| 599 | |
| 600 | return $#stmt_lines + 2; |
| 601 | } |
| 602 | |
| 603 | sub statement_rawlines { |
| 604 | my ($stmt) = @_; |
| 605 | |
| 606 | my @stmt_lines = ($stmt =~ /\n/g); |
| 607 | |
| 608 | return $#stmt_lines + 2; |
| 609 | } |
| 610 | |
| 611 | sub statement_block_size { |
| 612 | my ($stmt) = @_; |
| 613 | |
| 614 | $stmt =~ s/(^|\n)./$1/g; |
| 615 | $stmt =~ s/^\s*{//; |
| 616 | $stmt =~ s/}\s*$//; |
| 617 | $stmt =~ s/^\s*//; |
| 618 | $stmt =~ s/\s*$//; |
| 619 | |
| 620 | my @stmt_lines = ($stmt =~ /\n/g); |
| 621 | my @stmt_statements = ($stmt =~ /;/g); |
| 622 | |
| 623 | my $stmt_lines = $#stmt_lines + 2; |
| 624 | my $stmt_statements = $#stmt_statements + 1; |
| 625 | |
| 626 | if ($stmt_lines > $stmt_statements) { |
| 627 | return $stmt_lines; |
| 628 | } else { |
| 629 | return $stmt_statements; |
| 630 | } |
| 631 | } |
| 632 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 633 | sub ctx_statement_full { |
| 634 | my ($linenr, $remain, $off) = @_; |
| 635 | my ($statement, $condition, $level); |
| 636 | |
| 637 | my (@chunks); |
| 638 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 639 | # Grab the first conditional/block pair. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 640 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 641 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 642 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 643 | push(@chunks, [ $condition, $statement ]); |
| 644 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { |
| 645 | return ($level, $linenr, @chunks); |
| 646 | } |
| 647 | |
| 648 | # Pull in the following conditional/block pairs and see if they |
| 649 | # could continue the statement. |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 650 | for (;;) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 651 | ($statement, $condition, $linenr, $remain, $off, $level) = |
| 652 | ctx_statement_block($linenr, $remain, $off); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 653 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 654 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 655 | #print "C: push\n"; |
| 656 | push(@chunks, [ $condition, $statement ]); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return ($level, $linenr, @chunks); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 662 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 663 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 664 | my $line; |
| 665 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 666 | my $blk = ''; |
| 667 | my @o; |
| 668 | my @c; |
| 669 | my @res = (); |
| 670 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 671 | my $level = 0; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 672 | my @stack = ($level); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 673 | for ($line = $start; $remain > 0; $line++) { |
| 674 | next if ($rawlines[$line] =~ /^-/); |
| 675 | $remain--; |
| 676 | |
| 677 | $blk .= $rawlines[$line]; |
Andy Whitcroft | 4635f4f | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 678 | |
| 679 | # Handle nested #if/#else. |
| 680 | if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
| 681 | push(@stack, $level); |
| 682 | } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
| 683 | $level = $stack[$#stack - 1]; |
| 684 | } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { |
| 685 | $level = pop(@stack); |
| 686 | } |
| 687 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 688 | foreach my $c (split(//, $rawlines[$line])) { |
| 689 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 690 | if ($off > 0) { |
| 691 | $off--; |
| 692 | next; |
| 693 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 694 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 695 | if ($c eq $close && $level > 0) { |
| 696 | $level--; |
| 697 | last if ($level == 0); |
| 698 | } elsif ($c eq $open) { |
| 699 | $level++; |
| 700 | } |
| 701 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 702 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 703 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 704 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 707 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 710 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 711 | } |
| 712 | sub ctx_block_outer { |
| 713 | my ($linenr, $remain) = @_; |
| 714 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 715 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 716 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 717 | } |
| 718 | sub ctx_block { |
| 719 | my ($linenr, $remain) = @_; |
| 720 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 721 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 722 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 723 | } |
| 724 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 725 | my ($linenr, $remain, $off) = @_; |
| 726 | |
| 727 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 728 | return @r; |
| 729 | } |
| 730 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 731 | my ($linenr, $remain) = @_; |
| 732 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 733 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 734 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 735 | sub ctx_statement_level { |
| 736 | my ($linenr, $remain, $off) = @_; |
| 737 | |
| 738 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 739 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 740 | |
| 741 | sub ctx_locate_comment { |
| 742 | my ($first_line, $end_line) = @_; |
| 743 | |
| 744 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | beae633 | 2008-07-23 21:28:59 -0700 | [diff] [blame] | 745 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 746 | return $current_comment if (defined $current_comment); |
| 747 | |
| 748 | # Look through the context and try and figure out if there is a |
| 749 | # comment. |
| 750 | my $in_comment = 0; |
| 751 | $current_comment = ''; |
| 752 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 753 | my $line = $rawlines[$linenr - 1]; |
| 754 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 755 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 756 | $in_comment = 1; |
| 757 | } |
| 758 | if ($line =~ m@/\*@) { |
| 759 | $in_comment = 1; |
| 760 | } |
| 761 | if (!$in_comment && $current_comment ne '') { |
| 762 | $current_comment = ''; |
| 763 | } |
| 764 | $current_comment .= $line . "\n" if ($in_comment); |
| 765 | if ($line =~ m@\*/@) { |
| 766 | $in_comment = 0; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | chomp($current_comment); |
| 771 | return($current_comment); |
| 772 | } |
| 773 | sub ctx_has_comment { |
| 774 | my ($first_line, $end_line) = @_; |
| 775 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 776 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 777 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 778 | ##print "CMMT: $cmt\n"; |
| 779 | |
| 780 | return ($cmt ne ''); |
| 781 | } |
| 782 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 783 | sub raw_line { |
| 784 | my ($linenr, $cnt) = @_; |
| 785 | |
| 786 | my $offset = $linenr - 1; |
| 787 | $cnt++; |
| 788 | |
| 789 | my $line; |
| 790 | while ($cnt) { |
| 791 | $line = $rawlines[$offset++]; |
| 792 | next if (defined($line) && $line =~ /^-/); |
| 793 | $cnt--; |
| 794 | } |
| 795 | |
| 796 | return $line; |
| 797 | } |
| 798 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 799 | sub cat_vet { |
| 800 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 801 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 802 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 803 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 804 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 805 | $res .= $1; |
| 806 | if ($2 ne '') { |
| 807 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 808 | $res .= $coded; |
| 809 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 810 | } |
| 811 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 812 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 813 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 816 | my $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 817 | my $av_pending; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 818 | my @av_paren_type; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 819 | my $av_pend_colon; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 820 | |
| 821 | sub annotate_reset { |
| 822 | $av_preprocessor = 0; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 823 | $av_pending = '_'; |
| 824 | @av_paren_type = ('E'); |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 825 | $av_pend_colon = 'O'; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 828 | sub annotate_values { |
| 829 | my ($stream, $type) = @_; |
| 830 | |
| 831 | my $res; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 832 | my $var = '_' x length($stream); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 833 | my $cur = $stream; |
| 834 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 835 | print "$stream\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 836 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 837 | while (length($cur)) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 838 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 839 | print " <" . join('', @av_paren_type) . |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 840 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 841 | if ($cur =~ /^(\s+)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 842 | print "WS($1)\n" if ($dbg_values > 1); |
| 843 | if ($1 =~ /\n/ && $av_preprocessor) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 844 | $type = pop(@av_paren_type); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 845 | $av_preprocessor = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Andy Whitcroft | 9446ef5 | 2010-10-26 14:23:13 -0700 | [diff] [blame] | 848 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) { |
| 849 | print "CAST($1)\n" if ($dbg_values > 1); |
| 850 | push(@av_paren_type, $type); |
| 851 | $type = 'C'; |
| 852 | |
Andy Whitcroft | e91b6e2 | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 853 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 854 | print "DECLARE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 855 | $type = 'T'; |
| 856 | |
Andy Whitcroft | 389a2fe | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 857 | } elsif ($cur =~ /^($Modifier)\s*/) { |
| 858 | print "MODIFIER($1)\n" if ($dbg_values > 1); |
| 859 | $type = 'T'; |
| 860 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 861 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 862 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 863 | $av_preprocessor = 1; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 864 | push(@av_paren_type, $type); |
| 865 | if ($2 ne '') { |
| 866 | $av_pending = 'N'; |
| 867 | } |
| 868 | $type = 'E'; |
| 869 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 870 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 871 | print "UNDEF($1)\n" if ($dbg_values > 1); |
| 872 | $av_preprocessor = 1; |
| 873 | push(@av_paren_type, $type); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 874 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 875 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 876 | print "PRE_START($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 877 | $av_preprocessor = 1; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 878 | |
| 879 | push(@av_paren_type, $type); |
| 880 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 881 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 882 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 883 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 884 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); |
| 885 | $av_preprocessor = 1; |
| 886 | |
| 887 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); |
| 888 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 889 | $type = 'E'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 890 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 891 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 892 | print "PRE_END($1)\n" if ($dbg_values > 1); |
| 893 | |
| 894 | $av_preprocessor = 1; |
| 895 | |
| 896 | # Assume all arms of the conditional end as this |
| 897 | # one does, and continue as if the #endif was not here. |
| 898 | pop(@av_paren_type); |
| 899 | push(@av_paren_type, $type); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 900 | $type = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 901 | |
| 902 | } elsif ($cur =~ /^(\\\n)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 903 | print "PRECONT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 904 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 905 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
| 906 | print "ATTR($1)\n" if ($dbg_values > 1); |
| 907 | $av_pending = $type; |
| 908 | $type = 'N'; |
| 909 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 910 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 911 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 912 | if (defined $2) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 913 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 914 | } |
| 915 | $type = 'N'; |
| 916 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 917 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 918 | print "COND($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 919 | $av_pending = 'E'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 920 | $type = 'N'; |
| 921 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 922 | } elsif ($cur =~/^(case)/o) { |
| 923 | print "CASE($1)\n" if ($dbg_values > 1); |
| 924 | $av_pend_colon = 'C'; |
| 925 | $type = 'N'; |
| 926 | |
Andy Whitcroft | 14b111c | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 927 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 928 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 929 | $type = 'N'; |
| 930 | |
| 931 | } elsif ($cur =~ /^(\()/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 932 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 933 | push(@av_paren_type, $av_pending); |
| 934 | $av_pending = '_'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 935 | $type = 'N'; |
| 936 | |
| 937 | } elsif ($cur =~ /^(\))/o) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 938 | my $new_type = pop(@av_paren_type); |
| 939 | if ($new_type ne '_') { |
| 940 | $type = $new_type; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 941 | print "PAREN('$1') -> $type\n" |
| 942 | if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 943 | } else { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 944 | print "PAREN('$1')\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 947 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 948 | print "FUNC($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | c8cb2ca | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 949 | $type = 'V'; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 950 | $av_pending = 'V'; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 951 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 952 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
| 953 | if (defined $2 && $type eq 'C' || $type eq 'T') { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 954 | $av_pend_colon = 'B'; |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 955 | } elsif ($type eq 'E') { |
| 956 | $av_pend_colon = 'L'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 957 | } |
| 958 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); |
| 959 | $type = 'V'; |
| 960 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 961 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 962 | print "IDENT($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 963 | $type = 'V'; |
| 964 | |
| 965 | } elsif ($cur =~ /^($Assignment)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 966 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 967 | $type = 'N'; |
| 968 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 969 | } elsif ($cur =~/^(;|{|})/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 970 | print "END($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 971 | $type = 'E'; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 972 | $av_pend_colon = 'O'; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 973 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 974 | } elsif ($cur =~/^(,)/) { |
| 975 | print "COMMA($1)\n" if ($dbg_values > 1); |
| 976 | $type = 'C'; |
| 977 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 978 | } elsif ($cur =~ /^(\?)/o) { |
| 979 | print "QUESTION($1)\n" if ($dbg_values > 1); |
| 980 | $type = 'N'; |
| 981 | |
| 982 | } elsif ($cur =~ /^(:)/o) { |
| 983 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); |
| 984 | |
| 985 | substr($var, length($res), 1, $av_pend_colon); |
| 986 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { |
| 987 | $type = 'E'; |
| 988 | } else { |
| 989 | $type = 'N'; |
| 990 | } |
| 991 | $av_pend_colon = 'O'; |
| 992 | |
Andy Whitcroft | 8e761b0 | 2009-01-06 14:41:19 -0800 | [diff] [blame] | 993 | } elsif ($cur =~ /^(\[)/o) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 994 | print "CLOSE($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 995 | $type = 'N'; |
| 996 | |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 997 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 998 | my $variant; |
| 999 | |
| 1000 | print "OPV($1)\n" if ($dbg_values > 1); |
| 1001 | if ($type eq 'V') { |
| 1002 | $variant = 'B'; |
| 1003 | } else { |
| 1004 | $variant = 'U'; |
| 1005 | } |
| 1006 | |
| 1007 | substr($var, length($res), 1, $variant); |
| 1008 | $type = 'N'; |
| 1009 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1010 | } elsif ($cur =~ /^($Operators)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1011 | print "OP($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1012 | if ($1 ne '++' && $1 ne '--') { |
| 1013 | $type = 'N'; |
| 1014 | } |
| 1015 | |
| 1016 | } elsif ($cur =~ /(^.)/o) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1017 | print "C($1)\n" if ($dbg_values > 1); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1018 | } |
| 1019 | if (defined $1) { |
| 1020 | $cur = substr($cur, length($1)); |
| 1021 | $res .= $type x length($1); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1025 | return ($res, $var); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1028 | sub possible { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1029 | my ($possible, $line) = @_; |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1030 | my $notPermitted = qr{(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1031 | ^(?: |
| 1032 | $Modifier| |
| 1033 | $Storage| |
| 1034 | $Type| |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1035 | DEFINE_\S+ |
| 1036 | )$| |
| 1037 | ^(?: |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1038 | goto| |
| 1039 | return| |
| 1040 | case| |
| 1041 | else| |
| 1042 | asm|__asm__| |
| 1043 | do |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1044 | )(?:\s|$)| |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1045 | ^(?:typedef|struct|enum)\b |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1046 | )}x; |
| 1047 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); |
| 1048 | if ($possible !~ $notPermitted) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1049 | # Check for modifiers. |
| 1050 | $possible =~ s/\s*$Storage\s*//g; |
| 1051 | $possible =~ s/\s*$Sparse\s*//g; |
| 1052 | if ($possible =~ /^\s*$/) { |
| 1053 | |
| 1054 | } elsif ($possible =~ /\s/) { |
| 1055 | $possible =~ s/\s*$Type\s*//g; |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1056 | for my $modifier (split(' ', $possible)) { |
Andy Whitcroft | 9a974fd | 2009-10-26 16:50:12 -0700 | [diff] [blame] | 1057 | if ($modifier !~ $notPermitted) { |
| 1058 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); |
| 1059 | push(@modifierList, $modifier); |
| 1060 | } |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1061 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1062 | |
| 1063 | } else { |
| 1064 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); |
| 1065 | push(@typeList, $possible); |
| 1066 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1067 | build_types(); |
Andy Whitcroft | 0776e59 | 2008-10-15 22:02:29 -0700 | [diff] [blame] | 1068 | } else { |
| 1069 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1073 | my $prefix = ''; |
| 1074 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1075 | sub report { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1076 | if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { |
| 1077 | return 0; |
| 1078 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1079 | my $line = $prefix . $_[0]; |
| 1080 | |
| 1081 | $line = (split('\n', $line))[0] . "\n" if ($terse); |
| 1082 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1083 | push(our @report, $line); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1084 | |
| 1085 | return 1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1086 | } |
| 1087 | sub report_dump { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1088 | our @report; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1089 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1090 | sub ERROR { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1091 | if (report("ERROR: $_[0]\n")) { |
| 1092 | our $clean = 0; |
| 1093 | our $cnt_error++; |
| 1094 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1095 | } |
| 1096 | sub WARN { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1097 | if (report("WARNING: $_[0]\n")) { |
| 1098 | our $clean = 0; |
| 1099 | our $cnt_warn++; |
| 1100 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1101 | } |
| 1102 | sub CHK { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1103 | if ($check && report("CHECK: $_[0]\n")) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1104 | our $clean = 0; |
| 1105 | our $cnt_chk++; |
| 1106 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1109 | sub check_absolute_file { |
| 1110 | my ($absolute, $herecurr) = @_; |
| 1111 | my $file = $absolute; |
| 1112 | |
| 1113 | ##print "absolute<$absolute>\n"; |
| 1114 | |
| 1115 | # See if any suffix of this path is a path within the tree. |
| 1116 | while ($file =~ s@^[^/]*/@@) { |
| 1117 | if (-f "$root/$file") { |
| 1118 | ##print "file<$file>\n"; |
| 1119 | last; |
| 1120 | } |
| 1121 | } |
| 1122 | if (! -f _) { |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | # It is, so see if the prefix is acceptable. |
| 1127 | my $prefix = $absolute; |
| 1128 | substr($prefix, -length($file)) = ''; |
| 1129 | |
| 1130 | ##print "prefix<$prefix>\n"; |
| 1131 | if ($prefix ne ".../") { |
| 1132 | WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1136 | sub process { |
| 1137 | my $filename = shift; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1138 | |
| 1139 | my $linenr=0; |
| 1140 | my $prevline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1141 | my $prevrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1142 | my $stashline=""; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1143 | my $stashrawline=""; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1144 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1145 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1146 | my $indent; |
| 1147 | my $previndent=0; |
| 1148 | my $stashindent=0; |
| 1149 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1150 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1151 | my $signoff = 0; |
| 1152 | my $is_patch = 0; |
| 1153 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1154 | our @report = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1155 | our $cnt_lines = 0; |
| 1156 | our $cnt_error = 0; |
| 1157 | our $cnt_warn = 0; |
| 1158 | our $cnt_chk = 0; |
| 1159 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1160 | # Trace the real file/line as we go. |
| 1161 | my $realfile = ''; |
| 1162 | my $realline = 0; |
| 1163 | my $realcnt = 0; |
| 1164 | my $here = ''; |
| 1165 | my $in_comment = 0; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1166 | my $comment_edge = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1167 | my $first_line = 0; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1168 | my $p1_prefix = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1169 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1170 | my $prev_values = 'E'; |
| 1171 | |
| 1172 | # suppression flags |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1173 | my %suppress_ifbraces; |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1174 | my %suppress_whiletrailers; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1175 | my %suppress_export; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1176 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1177 | # Pre-scan the patch sanitizing the lines. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1178 | # Pre-scan the patch looking for any __setup documentation. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1179 | # |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1180 | my @setup_docs = (); |
| 1181 | my $setup_docs = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1182 | |
| 1183 | sanitise_line_reset(); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1184 | my $line; |
| 1185 | foreach my $rawline (@rawlines) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1186 | $linenr++; |
| 1187 | $line = $rawline; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1188 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1189 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1190 | $setup_docs = 0; |
| 1191 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 1192 | $setup_docs = 1; |
| 1193 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1194 | #next; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1195 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1196 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
| 1197 | $realline=$1-1; |
| 1198 | if (defined $2) { |
| 1199 | $realcnt=$3+1; |
| 1200 | } else { |
| 1201 | $realcnt=1+1; |
| 1202 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1203 | $in_comment = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1204 | |
| 1205 | # Guestimate if this is a continuing comment. Run |
| 1206 | # the context looking for a comment "edge". If this |
| 1207 | # edge is a close comment then we must be in a comment |
| 1208 | # at context start. |
| 1209 | my $edge; |
Andy Whitcroft | 01fa914 | 2008-10-15 22:02:19 -0700 | [diff] [blame] | 1210 | my $cnt = $realcnt; |
| 1211 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { |
| 1212 | next if (defined $rawlines[$ln - 1] && |
| 1213 | $rawlines[$ln - 1] =~ /^-/); |
| 1214 | $cnt--; |
| 1215 | #print "RAW<$rawlines[$ln - 1]>\n"; |
Andy Whitcroft | 721c1cb | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 1216 | last if (!defined $rawlines[$ln - 1]); |
Andy Whitcroft | fae17da | 2009-01-06 14:41:20 -0800 | [diff] [blame] | 1217 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
| 1218 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { |
| 1219 | ($edge) = $1; |
| 1220 | last; |
| 1221 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1222 | } |
| 1223 | if (defined $edge && $edge eq '*/') { |
| 1224 | $in_comment = 1; |
| 1225 | } |
| 1226 | |
| 1227 | # Guestimate if this is a continuing comment. If this |
| 1228 | # is the start of a diff block and this line starts |
| 1229 | # ' *' then it is very likely a comment. |
| 1230 | if (!defined $edge && |
Andy Whitcroft | 83242e0 | 2009-01-06 14:41:17 -0800 | [diff] [blame] | 1231 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1232 | { |
| 1233 | $in_comment = 1; |
| 1234 | } |
| 1235 | |
| 1236 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; |
| 1237 | sanitise_line_reset($in_comment); |
| 1238 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1239 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1240 | # Standardise the strings and chars within the input to |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1241 | # simplify matching -- only bother with positive lines. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1242 | $line = sanitise_line($rawline); |
| 1243 | } |
| 1244 | push(@lines, $line); |
| 1245 | |
| 1246 | if ($realcnt > 1) { |
| 1247 | $realcnt-- if ($line =~ /^(?:\+| |$)/); |
| 1248 | } else { |
| 1249 | $realcnt = 0; |
| 1250 | } |
| 1251 | |
| 1252 | #print "==>$rawline\n"; |
| 1253 | #print "-->$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1254 | |
| 1255 | if ($setup_docs && $line =~ /^\+/) { |
| 1256 | push(@setup_docs, $line); |
| 1257 | } |
| 1258 | } |
| 1259 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1260 | $prefix = ''; |
| 1261 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1262 | $realcnt = 0; |
| 1263 | $linenr = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1264 | foreach my $line (@lines) { |
| 1265 | $linenr++; |
| 1266 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1267 | my $rawline = $rawlines[$linenr - 1]; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1268 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1269 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1270 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1271 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1272 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1273 | $realline=$1-1; |
| 1274 | if (defined $2) { |
| 1275 | $realcnt=$3+1; |
| 1276 | } else { |
| 1277 | $realcnt=1+1; |
| 1278 | } |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1279 | annotate_reset(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 1280 | $prev_values = 'E'; |
| 1281 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1282 | %suppress_ifbraces = (); |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1283 | %suppress_whiletrailers = (); |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1284 | %suppress_export = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1285 | next; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1286 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1287 | # track the line number as we move through the hunk, note that |
| 1288 | # new versions of GNU diff omit the leading space on completely |
| 1289 | # blank context lines so we need to count that too. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1290 | } elsif ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1291 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1292 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1293 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1294 | # Measure the line length and indent. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1295 | ($length, $indent) = line_stats($rawline); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1296 | |
| 1297 | # Track the previous line. |
| 1298 | ($prevline, $stashline) = ($stashline, $line); |
| 1299 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1300 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
| 1301 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1302 | #warn "line<$line>\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1303 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1304 | } elsif ($realcnt == 1) { |
| 1305 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Andy Whitcroft | cc77cdc | 2009-10-26 16:50:13 -0700 | [diff] [blame] | 1308 | my $hunk_line = ($realcnt != 0); |
| 1309 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1310 | #make up the handle for any error we report on this line |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1311 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 1312 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 1313 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1314 | $here = "#$linenr: " if (!$file); |
| 1315 | $here = "#$realline: " if ($file); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1316 | |
| 1317 | # extract the filename as it passes |
| 1318 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 1319 | $realfile = $1; |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1320 | $realfile =~ s@^([^/]*)/@@; |
| 1321 | |
| 1322 | $p1_prefix = $1; |
Andy Whitcroft | e2f7aa4 | 2009-02-27 14:03:06 -0800 | [diff] [blame] | 1323 | if (!$file && $tree && $p1_prefix ne '' && |
| 1324 | -e "$root/$p1_prefix") { |
Wolfram Sang | 1e85572 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 1325 | WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
| 1326 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1327 | |
Andy Whitcroft | c1ab332 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 1328 | if ($realfile =~ m@^include/asm/@) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1329 | ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); |
| 1330 | } |
| 1331 | next; |
| 1332 | } |
| 1333 | |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 1334 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1335 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1336 | my $hereline = "$here\n$rawline\n"; |
| 1337 | my $herecurr = "$here\n$rawline\n"; |
| 1338 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1339 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1340 | $cnt_lines++ if ($realcnt != 0); |
| 1341 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1342 | #check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1343 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1344 | # This is a signoff, if ugly, so do not double report. |
| 1345 | $signoff++; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1346 | if (!($line =~ /^\s*Signed-off-by:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1347 | WARN("Signed-off-by: is the preferred form\n" . |
| 1348 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1349 | } |
| 1350 | if ($line =~ /^\s*signed-off-by:\S/i) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1351 | WARN("space required after Signed-off-by:\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1352 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1356 | # Check for wrappage within a valid hunk of the file |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1357 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1358 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1359 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Andy Whitcroft | 6ecd967 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1362 | # Check for absolute kernel paths. |
| 1363 | if ($tree) { |
| 1364 | while ($line =~ m{(?:^|\s)(/\S*)}g) { |
| 1365 | my $file = $1; |
| 1366 | |
| 1367 | if ($file =~ m{^(.*?)(?::\d+)+:?$} && |
| 1368 | check_absolute_file($1, $herecurr)) { |
| 1369 | # |
| 1370 | } else { |
| 1371 | check_absolute_file($file, $herecurr); |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1376 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 1377 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1378 | $rawline !~ m/^$UTF8*$/) { |
| 1379 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); |
| 1380 | |
| 1381 | my $blank = copy_spacing($rawline); |
| 1382 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; |
| 1383 | my $hereptr = "$hereline$ptr\n"; |
| 1384 | |
| 1385 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1386 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1387 | |
Andy Whitcroft | 30670854 | 2008-10-15 22:02:28 -0700 | [diff] [blame] | 1388 | # ignore non-hunk lines and lines being removed |
| 1389 | next if (!$hunk_line || $line =~ /^-/); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1390 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1391 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1392 | if ($line =~ /^\+.*\015/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1393 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1394 | ERROR("DOS line endings\n" . $herevet); |
| 1395 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1396 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
| 1397 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1398 | ERROR("trailing whitespace\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1399 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1400 | } |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1401 | |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1402 | # check for Kconfig help text having a real description |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame^] | 1403 | # Only applies when adding the entry originally, after that we do not have |
| 1404 | # sufficient context to determine whether it is indeed long enough. |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1405 | if ($realfile =~ /Kconfig/ && |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame^] | 1406 | $line =~ /\+\s*(?:---)?help(?:---)?$/) { |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1407 | my $length = 0; |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame^] | 1408 | my $cnt = $realcnt; |
| 1409 | my $ln = $linenr + 1; |
| 1410 | my $f; |
| 1411 | my $is_end = 0; |
| 1412 | while ($cnt > 0 && defined $lines[$ln - 1]) { |
| 1413 | $f = $lines[$ln - 1]; |
| 1414 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1415 | $is_end = $lines[$ln - 1] =~ /^\+/; |
| 1416 | $ln++; |
| 1417 | |
| 1418 | next if ($f =~ /^-/); |
| 1419 | $f =~ s/^.//; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1420 | $f =~ s/#.*//; |
| 1421 | $f =~ s/^\s+//; |
| 1422 | next if ($f =~ /^$/); |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame^] | 1423 | if ($f =~ /^\s*config\s/) { |
| 1424 | $is_end = 1; |
| 1425 | last; |
| 1426 | } |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1427 | $length++; |
| 1428 | } |
Andy Whitcroft | 9fe287d7 | 2010-10-26 14:23:15 -0700 | [diff] [blame^] | 1429 | WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4); |
| 1430 | #print "is_end<$is_end> length<$length>\n"; |
Andi Kleen | 3354957 | 2010-05-24 14:33:29 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Andy Whitcroft | 5368df2 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1433 | # check we are in a valid source file if not then ignore this hunk |
| 1434 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
| 1435 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1436 | #80 column limit |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1437 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1438 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
Joe Perches | 8bbea96 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 1439 | !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ || |
| 1440 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && |
Andy Whitcroft | f4c014c | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1441 | $length > 80) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1442 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1443 | WARN("line over 80 characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Joe Perches | 5e79d96 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 1446 | # check for spaces before a quoted newline |
| 1447 | if ($rawline =~ /^.*\".*\s\\n/) { |
| 1448 | WARN("unnecessary whitespace before a quoted newline\n" . $herecurr); |
| 1449 | } |
| 1450 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1451 | # check for adding lines without a newline. |
| 1452 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { |
| 1453 | WARN("adding a line without newline at end of file\n" . $herecurr); |
| 1454 | } |
| 1455 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1456 | # Blackfin: use hi/lo macros |
| 1457 | if ($realfile =~ m@arch/blackfin/.*\.S$@) { |
| 1458 | if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { |
| 1459 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 1460 | ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet); |
| 1461 | } |
| 1462 | if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { |
| 1463 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 1464 | ERROR("use the HI() macro, not (... >> 16)\n" . $herevet); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1468 | # check we are in a valid source file C or perl if not then ignore this hunk |
| 1469 | next if ($realfile !~ /\.(h|c|pl)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1470 | |
| 1471 | # at the beginning of a line any tabs must come first and anything |
| 1472 | # more than 8 must use tabs. |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1473 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
| 1474 | $rawline =~ /^\+\s* \s*/) { |
| 1475 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1476 | ERROR("code indent should use tabs where possible\n" . $herevet); |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 1477 | $rpt_cleaners = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Alberto Panizzo | 08e4436 | 2010-03-05 13:43:54 -0800 | [diff] [blame] | 1480 | # check for space before tabs. |
| 1481 | if ($rawline =~ /^\+/ && $rawline =~ / \t/) { |
| 1482 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
| 1483 | WARN("please, no space before tabs\n" . $herevet); |
| 1484 | } |
| 1485 | |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1486 | # check for spaces at the beginning of a line. |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1487 | # Exceptions: |
| 1488 | # 1) within comments |
| 1489 | # 2) indented preprocessor commands |
| 1490 | # 3) hanging labels |
| 1491 | if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1492 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
Andy Whitcroft | 6b4c5be | 2010-10-26 14:23:11 -0700 | [diff] [blame] | 1493 | WARN("please, no spaces at the start of a line\n" . $herevet); |
Raffaele Recalcati | 5f7ddae | 2010-08-09 17:20:59 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Andy Whitcroft | b9ea10d | 2008-10-15 22:02:24 -0700 | [diff] [blame] | 1496 | # check we are in a valid C source file if not then ignore this hunk |
| 1497 | next if ($realfile !~ /\.(h|c)$/); |
| 1498 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1499 | # check for RCS/CVS revision markers |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1500 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1501 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
| 1502 | } |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1503 | |
Mike Frysinger | 42e41c5 | 2009-09-21 17:04:40 -0700 | [diff] [blame] | 1504 | # Blackfin: don't use __builtin_bfin_[cs]sync |
| 1505 | if ($line =~ /__builtin_bfin_csync/) { |
| 1506 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 1507 | ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet); |
| 1508 | } |
| 1509 | if ($line =~ /__builtin_bfin_ssync/) { |
| 1510 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 1511 | ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet); |
| 1512 | } |
| 1513 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1514 | # Check for potential 'bare' types |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1515 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 1516 | $realline_next); |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 1517 | if ($realcnt && $line =~ /.\s*\S/) { |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1518 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 1519 | ctx_statement_block($linenr, $realcnt, 0); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1520 | $stat =~ s/\n./\n /g; |
| 1521 | $cond =~ s/\n./\n /g; |
| 1522 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1523 | # Find the real next line. |
| 1524 | $realline_next = $line_nr_next; |
| 1525 | if (defined $realline_next && |
| 1526 | (!defined $lines[$realline_next - 1] || |
| 1527 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { |
| 1528 | $realline_next++; |
| 1529 | } |
| 1530 | |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1531 | my $s = $stat; |
| 1532 | $s =~ s/{.*$//s; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1533 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1534 | # Ignore goto labels. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1535 | if ($s =~ /$Ident:\*$/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1536 | |
| 1537 | # Ignore functions being called |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1538 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1539 | |
Andy Whitcroft | 463f286 | 2009-09-21 17:04:34 -0700 | [diff] [blame] | 1540 | } elsif ($s =~ /^.\s*else\b/s) { |
| 1541 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1542 | # declarations always start with types |
Andy Whitcroft | d250658 | 2008-07-23 21:29:09 -0700 | [diff] [blame] | 1543 | } 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) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1544 | my $type = $1; |
| 1545 | $type =~ s/\s+/ /g; |
| 1546 | possible($type, "A:" . $s); |
| 1547 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1548 | # definitions in global scope can only start with types |
Andy Whitcroft | a6a8406 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 1549 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1550 | possible($1, "B:" . $s); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1551 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1552 | |
| 1553 | # any (foo ... *) is a pointer cast, and foo is a type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1554 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1555 | possible($1, "C:" . $s); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1556 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1557 | |
| 1558 | # Check for any sort of function declaration. |
| 1559 | # int foo(something bar, other baz); |
| 1560 | # void (*store_gdt)(x86_descr_ptr *); |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 1561 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1562 | my ($name_len) = length($1); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1563 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1564 | my $ctx = $s; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1565 | substr($ctx, 0, $name_len + 1, ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1566 | $ctx =~ s/\)[^\)]*$//; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1567 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1568 | for my $arg (split(/\s*,\s*/, $ctx)) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1569 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1570 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1571 | possible($1, "D:" . $s); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1578 | # |
| 1579 | # Checks which may be anchored in the context. |
| 1580 | # |
| 1581 | |
| 1582 | # Check for switch () and associated case and default |
| 1583 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1584 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 1585 | my $err = ''; |
| 1586 | my $sep = ''; |
| 1587 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 1588 | shift(@ctx); |
| 1589 | for my $ctx (@ctx) { |
| 1590 | my ($clen, $cindent) = line_stats($ctx); |
| 1591 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 1592 | $indent != $cindent) { |
| 1593 | $err .= "$sep$ctx\n"; |
| 1594 | $sep = ''; |
| 1595 | } else { |
| 1596 | $sep = "[...]\n"; |
| 1597 | } |
| 1598 | } |
| 1599 | if ($err ne '') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1600 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 1605 | # or if that brace on the next line is for something else |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1606 | if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1607 | my $pre_ctx = "$1$2"; |
| 1608 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1609 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1610 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 1611 | my $ctx = join("\n", @ctx); |
| 1612 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1613 | my $ctx_ln = $linenr; |
| 1614 | my $ctx_skip = $realcnt; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1615 | |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1616 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && |
| 1617 | defined $lines[$ctx_ln - 1] && |
| 1618 | $lines[$ctx_ln - 1] =~ /^-/)) { |
| 1619 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; |
| 1620 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1621 | $ctx_ln++; |
| 1622 | } |
Andy Whitcroft | 548596d | 2008-07-23 21:29:01 -0700 | [diff] [blame] | 1623 | |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 1624 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
| 1625 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1626 | |
| 1627 | if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
| 1628 | ERROR("that open brace { should be on the previous line\n" . |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1629 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1630 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1631 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
| 1632 | $ctx =~ /\)\s*\;\s*$/ && |
| 1633 | defined $lines[$ctx_ln - 1]) |
| 1634 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1635 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 1636 | if ($nindent > $indent) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1637 | WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1638 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1643 | # Check relative indent for conditionals and blocks. |
| 1644 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
| 1645 | my ($s, $c) = ($stat, $cond); |
| 1646 | |
| 1647 | substr($s, 0, length($c), ''); |
| 1648 | |
| 1649 | # Make sure we remove the line prefixes as we have |
| 1650 | # none on the first line, and are going to readd them |
| 1651 | # where necessary. |
| 1652 | $s =~ s/\n./\n/gs; |
| 1653 | |
| 1654 | # Find out how long the conditional actually is. |
Andy Whitcroft | 6f779c1 | 2008-10-15 22:02:27 -0700 | [diff] [blame] | 1655 | my @newlines = ($c =~ /\n/gs); |
| 1656 | my $cond_lines = 1 + $#newlines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1657 | |
| 1658 | # We want to check the first line inside the block |
| 1659 | # starting at the end of the conditional, so remove: |
| 1660 | # 1) any blank line termination |
| 1661 | # 2) any opening brace { on end of the line |
| 1662 | # 3) any do (...) { |
| 1663 | my $continuation = 0; |
| 1664 | my $check = 0; |
| 1665 | $s =~ s/^.*\bdo\b//; |
| 1666 | $s =~ s/^\s*{//; |
| 1667 | if ($s =~ s/^\s*\\//) { |
| 1668 | $continuation = 1; |
| 1669 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1670 | if ($s =~ s/^\s*?\n//) { |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1671 | $check = 1; |
| 1672 | $cond_lines++; |
| 1673 | } |
| 1674 | |
| 1675 | # Also ignore a loop construct at the end of a |
| 1676 | # preprocessor statement. |
| 1677 | if (($prevline =~ /^.\s*#\s*define\s/ || |
| 1678 | $prevline =~ /\\\s*$/) && $continuation == 0) { |
| 1679 | $check = 0; |
| 1680 | } |
| 1681 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1682 | my $cond_ptr = -1; |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1683 | $continuation = 0; |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1684 | while ($cond_ptr != $cond_lines) { |
| 1685 | $cond_ptr = $cond_lines; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1686 | |
Andy Whitcroft | f16fa28 | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 1687 | # If we see an #else/#elif then the code |
| 1688 | # is not linear. |
| 1689 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { |
| 1690 | $check = 0; |
| 1691 | } |
| 1692 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1693 | # Ignore: |
| 1694 | # 1) blank lines, they should be at 0, |
| 1695 | # 2) preprocessor lines, and |
| 1696 | # 3) labels. |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1697 | if ($continuation || |
| 1698 | $s =~ /^\s*?\n/ || |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1699 | $s =~ /^\s*#\s*?/ || |
| 1700 | $s =~ /^\s*$Ident\s*:/) { |
Andy Whitcroft | 740504c | 2008-10-15 22:02:35 -0700 | [diff] [blame] | 1701 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
Andy Whitcroft | 30dad6e | 2009-09-21 17:04:36 -0700 | [diff] [blame] | 1702 | if ($s =~ s/^.*?\n//) { |
| 1703 | $cond_lines++; |
| 1704 | } |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1705 | } |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | my (undef, $sindent) = line_stats("+" . $s); |
| 1709 | my $stat_real = raw_line($linenr, $cond_lines); |
| 1710 | |
| 1711 | # Check if either of these lines are modified, else |
| 1712 | # this is not this patch's fault. |
| 1713 | if (!defined($stat_real) || |
| 1714 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { |
| 1715 | $check = 0; |
| 1716 | } |
| 1717 | if (defined($stat_real) && $cond_lines > 1) { |
| 1718 | $stat_real = "[...]\n$stat_real"; |
| 1719 | } |
| 1720 | |
Andy Whitcroft | 9bd49ef | 2008-10-15 22:02:22 -0700 | [diff] [blame] | 1721 | #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"; |
Andy Whitcroft | 4d001e4 | 2008-10-15 22:02:21 -0700 | [diff] [blame] | 1722 | |
| 1723 | if ($check && (($sindent % 8) != 0 || |
| 1724 | ($sindent <= $indent && $s ne ''))) { |
| 1725 | WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
| 1726 | } |
| 1727 | } |
| 1728 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1729 | # Track the 'values' across context and added lines. |
| 1730 | my $opline = $line; $opline =~ s/^./ /; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1731 | my ($curr_values, $curr_vars) = |
| 1732 | annotate_values($opline . "\n", $prev_values); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1733 | $curr_values = $prev_values . $curr_values; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1734 | if ($dbg_values) { |
| 1735 | my $outline = $opline; $outline =~ s/\t/ /g; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1736 | print "$linenr > .$outline\n"; |
| 1737 | print "$linenr > $curr_values\n"; |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1738 | print "$linenr > $curr_vars\n"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1739 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1740 | $prev_values = substr($curr_values, -1); |
| 1741 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1742 | #ignore lines not being added |
| 1743 | if ($line=~/^[^\+]/) {next;} |
| 1744 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1745 | # TEST: allow direct testing of the type matcher. |
Andy Whitcroft | 7429c69 | 2008-07-23 21:29:06 -0700 | [diff] [blame] | 1746 | if ($dbg_type) { |
| 1747 | if ($line =~ /^.\s*$Declare\s*$/) { |
| 1748 | ERROR("TEST: is type\n" . $herecurr); |
| 1749 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { |
| 1750 | ERROR("TEST: is not type ($1 is)\n". $herecurr); |
| 1751 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1752 | next; |
| 1753 | } |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 1754 | # TEST: allow direct testing of the attribute matcher. |
| 1755 | if ($dbg_attr) { |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 1756 | if ($line =~ /^.\s*$Modifier\s*$/) { |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 1757 | ERROR("TEST: is attr\n" . $herecurr); |
Andy Whitcroft | 9360b0e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 1758 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
Andy Whitcroft | a1ef277 | 2008-10-15 22:02:17 -0700 | [diff] [blame] | 1759 | ERROR("TEST: is not attr ($1 is)\n". $herecurr); |
| 1760 | } |
| 1761 | next; |
| 1762 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1763 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1764 | # check for initialisation to aggregates open brace on the next line |
Andy Whitcroft | 99423c2 | 2009-10-26 16:50:15 -0700 | [diff] [blame] | 1765 | if ($line =~ /^.\s*{/ && |
| 1766 | $prevline =~ /(?:^|[^=])=\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1767 | ERROR("that open brace { should be on the previous line\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1770 | # |
| 1771 | # Checks which are anchored on the added line. |
| 1772 | # |
| 1773 | |
| 1774 | # check for malformed paths in #include statements (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1775 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1776 | my $path = $1; |
| 1777 | if ($path =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1778 | ERROR("malformed #include filename\n" . |
| 1779 | $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1780 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1781 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1782 | |
| 1783 | # no C99 // comments |
| 1784 | if ($line =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1785 | ERROR("do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1786 | } |
| 1787 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1788 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1789 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1790 | |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1791 | # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider |
| 1792 | # the whole statement. |
| 1793 | #print "APW <$lines[$realline_next - 1]>\n"; |
| 1794 | if (defined $realline_next && |
| 1795 | exists $lines[$realline_next - 1] && |
| 1796 | !defined $suppress_export{$realline_next} && |
| 1797 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 1798 | $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1799 | my $name = $1; |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1800 | if ($stat !~ /(?: |
| 1801 | \n.}\s*$| |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 1802 | ^.DEFINE_$Ident\(\Q$name\E\)| |
| 1803 | ^.DECLARE_$Ident\(\Q$name\E\)| |
| 1804 | ^.LIST_HEAD\(\Q$name\E\)| |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1805 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
| 1806 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() |
Andy Whitcroft | 4801205 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 1807 | )/x) { |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1808 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
| 1809 | $suppress_export{$realline_next} = 2; |
| 1810 | } else { |
| 1811 | $suppress_export{$realline_next} = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1812 | } |
| 1813 | } |
Andy Whitcroft | 2b474a1 | 2009-10-26 16:50:16 -0700 | [diff] [blame] | 1814 | if (!defined $suppress_export{$linenr} && |
| 1815 | $prevline =~ /^.\s*$/ && |
| 1816 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
| 1817 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 1818 | #print "FOO B <$lines[$linenr - 1]>\n"; |
| 1819 | $suppress_export{$linenr} = 2; |
| 1820 | } |
| 1821 | if (defined $suppress_export{$linenr} && |
| 1822 | $suppress_export{$linenr} == 2) { |
| 1823 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
| 1824 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1825 | |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 1826 | # check for global initialisers. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1827 | if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { |
Joe Eloff | 5150bda | 2010-08-09 17:21:00 -0700 | [diff] [blame] | 1828 | ERROR("do not initialise globals to 0 or NULL\n" . |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1829 | $herecurr); |
| 1830 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1831 | # check for static initialisers. |
Andy Whitcroft | 2d1bafd | 2009-01-06 14:41:28 -0800 | [diff] [blame] | 1832 | if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1833 | ERROR("do not initialise statics to 0 or NULL\n" . |
| 1834 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1837 | # check for new typedefs, only function parameters and sparse annotations |
| 1838 | # make sense. |
| 1839 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 8054576 | 2009-01-06 14:41:26 -0800 | [diff] [blame] | 1840 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1841 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
Andy Whitcroft | 8ed22ca | 2008-10-15 22:02:32 -0700 | [diff] [blame] | 1842 | $line !~ /\b$typeTypedefs\b/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1843 | $line !~ /\b__bitwise(?:__|)\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1844 | WARN("do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | # * goes on variable not on type |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1848 | # (char*[ const]) |
Andy Whitcroft | 00ef4ec | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 1849 | if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1850 | my ($from, $to) = ($1, $1); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1851 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1852 | # Should start with a space. |
| 1853 | $to =~ s/^(\S)/ $1/; |
| 1854 | # Should not end with a space. |
| 1855 | $to =~ s/\s+$//; |
| 1856 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 1857 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1858 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1859 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1860 | #print "from<$from> to<$to>\n"; |
| 1861 | if ($from ne $to) { |
| 1862 | ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); |
| 1863 | } |
Andy Whitcroft | 00ef4ec | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 1864 | } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1865 | my ($from, $to, $ident) = ($1, $1, $2); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1866 | |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1867 | # Should start with a space. |
| 1868 | $to =~ s/^(\S)/ $1/; |
| 1869 | # Should not end with a space. |
| 1870 | $to =~ s/\s+$//; |
| 1871 | # '*'s should not have spaces between. |
Andy Whitcroft | f9a0b3d | 2009-01-15 13:51:05 -0800 | [diff] [blame] | 1872 | while ($to =~ s/\*\s+\*/\*\*/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1873 | } |
| 1874 | # Modifiers should have spaces. |
| 1875 | $to =~ s/(\b$Modifier$)/$1 /; |
| 1876 | |
Andy Whitcroft | 667026e | 2009-02-27 14:03:08 -0800 | [diff] [blame] | 1877 | #print "from<$from> to<$to> ident<$ident>\n"; |
| 1878 | if ($from ne $to && $ident !~ /^$Modifier$/) { |
Andy Whitcroft | 6586386 | 2009-01-06 14:41:21 -0800 | [diff] [blame] | 1879 | ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); |
| 1880 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | # # no BUG() or BUG_ON() |
| 1884 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 1885 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 1886 | # print "$herecurr"; |
| 1887 | # $clean = 0; |
| 1888 | # } |
| 1889 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1890 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1891 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1892 | } |
| 1893 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1894 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 1895 | # same line do not need a level, so we use the current block context |
| 1896 | # to try and find and validate the current printk. In summary the current |
| 1897 | # printk includes all preceeding printk's which have no newline on the end. |
| 1898 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1899 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1900 | my $ok = 0; |
| 1901 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 1902 | #print "CHECK<$lines[$ln - 1]\n"; |
| 1903 | # we have a preceeding printk if it ends |
| 1904 | # with "\n" ignore it, else it is to blame |
| 1905 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 1906 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 1907 | $ok = 1; |
| 1908 | } |
| 1909 | last; |
| 1910 | } |
| 1911 | } |
| 1912 | if ($ok == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1913 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1914 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1917 | # function brace can't be on same line, except for #defines of do while, |
| 1918 | # or if closed on same line |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1919 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
| 1920 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1921 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1922 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1923 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 1924 | # open braces for enum, union and struct go on the same line. |
| 1925 | if ($line =~ /^.\s*{/ && |
| 1926 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
| 1927 | ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); |
| 1928 | } |
| 1929 | |
Andy Whitcroft | 0c73b4e | 2010-10-26 14:23:15 -0700 | [diff] [blame] | 1930 | # missing space after union, struct or enum definition |
| 1931 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { |
| 1932 | WARN("missing space after $1 definition\n" . $herecurr); |
| 1933 | } |
| 1934 | |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1935 | # check for spacing round square brackets; allowed: |
| 1936 | # 1. with a type on the left -- int [] a; |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 1937 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
| 1938 | # 3. inside a curly brace -- = { [0...10] = 5 } |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1939 | while ($line =~ /(.*?\s)\[/g) { |
| 1940 | my ($where, $prefix) = ($-[1], $1); |
| 1941 | if ($prefix !~ /$Type\s+$/ && |
Andy Whitcroft | fe2a7db | 2008-10-15 22:02:15 -0700 | [diff] [blame] | 1942 | ($where != 0 || $prefix !~ /^.\s+$/) && |
| 1943 | $prefix !~ /{\s+$/) { |
Andy Whitcroft | 8d31cfc | 2008-07-23 21:29:02 -0700 | [diff] [blame] | 1944 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); |
| 1945 | } |
| 1946 | } |
| 1947 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1948 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1949 | while ($line =~ /($Ident)\s+\(/g) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1950 | my $name = $1; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1951 | my $ctx_before = substr($line, 0, $-[1]); |
| 1952 | my $ctx = "$ctx_before$name"; |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1953 | |
| 1954 | # Ignore those directives where spaces _are_ permitted. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1955 | if ($name =~ /^(?: |
| 1956 | if|for|while|switch|return|case| |
| 1957 | volatile|__volatile__| |
| 1958 | __attribute__|format|__extension__| |
| 1959 | asm|__asm__)$/x) |
| 1960 | { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1961 | |
| 1962 | # cpp #define statements have non-optional spaces, ie |
| 1963 | # if there is a space between the name and the open |
| 1964 | # parenthesis it is simply not a parameter group. |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1965 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1966 | |
| 1967 | # cpp #elif statement condition may start with a ( |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 1968 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1969 | |
| 1970 | # If this whole things ends with a type its most |
| 1971 | # likely a typedef for a function. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1972 | } elsif ($ctx =~ /$Type$/) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 1973 | |
| 1974 | } else { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1975 | WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1976 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1977 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1978 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1979 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1980 | my $ops = qr{ |
| 1981 | <<=|>>=|<=|>=|==|!=| |
| 1982 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 1983 | =>|->|<<|>>|<|>|=|!|~| |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 1984 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
| 1985 | \?|: |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1986 | }x; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 1987 | my @elements = split(/($ops|;)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1988 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1989 | |
| 1990 | my $blank = copy_spacing($opline); |
| 1991 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1992 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1993 | $off += length($elements[$n]); |
| 1994 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 1995 | # Pick up the preceeding and succeeding characters. |
| 1996 | my $ca = substr($opline, 0, $off); |
| 1997 | my $cc = ''; |
| 1998 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
| 1999 | $cc = substr($opline, $off + length($elements[$n + 1])); |
| 2000 | } |
| 2001 | my $cb = "$ca$;$cc"; |
| 2002 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2003 | my $a = ''; |
| 2004 | $a = 'V' if ($elements[$n] ne ''); |
| 2005 | $a = 'W' if ($elements[$n] =~ /\s$/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2006 | $a = 'C' if ($elements[$n] =~ /$;$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2007 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 2008 | $a = 'O' if ($elements[$n] eq ''); |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2009 | $a = 'E' if ($ca =~ /^\s*$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2010 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2011 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2012 | |
| 2013 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2014 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2015 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 2016 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2017 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2018 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 2019 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 8b1b337 | 2009-01-06 14:41:27 -0800 | [diff] [blame] | 2020 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2021 | } else { |
| 2022 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2025 | my $ctx = "${a}x${c}"; |
| 2026 | |
| 2027 | my $at = "(ctx:$ctx)"; |
| 2028 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2029 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2030 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2031 | |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2032 | # Pull out the value of this operator. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2033 | my $op_type = substr($curr_values, $off + 1, 1); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2034 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2035 | # Get the full operator variant. |
| 2036 | my $opv = $op . substr($curr_vars, $off, 1); |
| 2037 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2038 | # Ignore operators passed as parameters. |
| 2039 | if ($op_type ne 'V' && |
| 2040 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { |
| 2041 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2042 | # # Ignore comments |
| 2043 | # } elsif ($op =~ /^$;+$/) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2044 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2045 | # ; should have either the end of line or a space or \ after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2046 | } elsif ($op eq ';') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2047 | if ($ctx !~ /.x[WEBC]/ && |
| 2048 | $cc !~ /^\\/ && $cc !~ /^;/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2049 | ERROR("space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | # // is a comment |
| 2053 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2054 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2055 | # No spaces for: |
| 2056 | # -> |
| 2057 | # : when part of a bitfield |
| 2058 | } elsif ($op eq '->' || $opv eq ':B') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2059 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2060 | ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | # , must have a space on the right. |
| 2064 | } elsif ($op eq ',') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2065 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2066 | ERROR("space required after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2069 | # '*' as part of a type definition -- reported already. |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2070 | } elsif ($opv eq '*_') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2071 | #warn "'*' is part of type\n"; |
| 2072 | |
| 2073 | # unary operators should have a space before and |
| 2074 | # none after. May be left adjacent to another |
| 2075 | # unary operator, or a cast |
| 2076 | } elsif ($op eq '!' || $op eq '~' || |
Andy Whitcroft | 74048ed | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2077 | $opv eq '*U' || $opv eq '-U' || |
Andy Whitcroft | 0d41386 | 2008-10-15 22:02:16 -0700 | [diff] [blame] | 2078 | $opv eq '&U' || $opv eq '&&U') { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2079 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2080 | ERROR("space required before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2081 | } |
Andy Whitcroft | a3340b3 | 2009-02-27 14:03:07 -0800 | [diff] [blame] | 2082 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2083 | # A unary '*' may be const |
| 2084 | |
| 2085 | } elsif ($ctx =~ /.xW/) { |
Andy Whitcroft | fb9e909 | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 2086 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | # unary ++ and unary -- are allowed no space on one side. |
| 2090 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2091 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
| 2092 | ERROR("space required one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2093 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2094 | if ($ctx =~ /Wx[BE]/ || |
| 2095 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { |
| 2096 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2097 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2098 | if ($ctx =~ /ExW/) { |
| 2099 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); |
| 2100 | } |
| 2101 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2102 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2103 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2104 | } elsif ($op eq '<<' or $op eq '>>' or |
| 2105 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 2106 | $op eq '+' or $op eq '-' or |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2107 | $op eq '*' or $op eq '/' or |
| 2108 | $op eq '%') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2109 | { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2110 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2111 | ERROR("need consistent spacing around '$op' $at\n" . |
| 2112 | $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2115 | # A colon needs no spaces before when it is |
| 2116 | # terminating a case value or a label. |
| 2117 | } elsif ($opv eq ':C' || $opv eq ':L') { |
| 2118 | if ($ctx =~ /Wx./) { |
| 2119 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); |
| 2120 | } |
| 2121 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2122 | # All the others need spaces both sides. |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2123 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2124 | my $ok = 0; |
| 2125 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2126 | # Ignore email addresses <foo@bar> |
Andy Whitcroft | 1f65f94 | 2008-07-23 21:29:10 -0700 | [diff] [blame] | 2127 | if (($op eq '<' && |
| 2128 | $cc =~ /^\S+\@\S+>/) || |
| 2129 | ($op eq '>' && |
| 2130 | $ca =~ /<\S+\@\S+$/)) |
| 2131 | { |
| 2132 | $ok = 1; |
| 2133 | } |
| 2134 | |
| 2135 | # Ignore ?: |
| 2136 | if (($opv eq ':O' && $ca =~ /\?$/) || |
| 2137 | ($op eq '?' && $cc =~ /^:/)) { |
| 2138 | $ok = 1; |
| 2139 | } |
| 2140 | |
| 2141 | if ($ok == 0) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2142 | ERROR("spaces required around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2143 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2144 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2145 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2149 | # check for multiple assignments |
| 2150 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2151 | CHK("multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2154 | ## # check for multiple declarations, allowing for a function declaration |
| 2155 | ## # continuation. |
| 2156 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 2157 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 2158 | ## |
| 2159 | ## # Remove any bracketed sections to ensure we do not |
| 2160 | ## # falsly report the parameters of functions. |
| 2161 | ## my $ln = $line; |
| 2162 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 2163 | ## } |
| 2164 | ## if ($ln =~ /,/) { |
| 2165 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); |
| 2166 | ## } |
| 2167 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2168 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2169 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2170 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 2171 | $line =~ /do{/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2172 | ERROR("space required before the open brace '{'\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | # closing brace should have a space following it when it has anything |
| 2176 | # on the line |
| 2177 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2178 | ERROR("space required after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2181 | # check spacing on square brackets |
| 2182 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2183 | ERROR("space prohibited after that open square bracket '['\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2184 | } |
| 2185 | if ($line =~ /\s\]/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2186 | ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2189 | # check spacing on parentheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2190 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 2191 | $line !~ /for\s*\(\s+;/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2192 | ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2193 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2194 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2195 | $line !~ /for\s*\(.*;\s+\)/ && |
| 2196 | $line !~ /:\s+\)/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2197 | ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2200 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2201 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2202 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2203 | WARN("labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2206 | # Return is not a function. |
| 2207 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { |
| 2208 | my $spacing = $1; |
| 2209 | my $value = $2; |
| 2210 | |
Andy Whitcroft | 86f9d05 | 2009-01-06 14:41:24 -0800 | [diff] [blame] | 2211 | # Flatten any parentheses |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2212 | $value =~ s/\(/ \(/g; |
| 2213 | $value =~ s/\)/\) /g; |
Andy Whitcroft | 63f17f8 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2214 | while ($value =~ s/\[[^\{\}]*\]/1/ || |
| 2215 | $value !~ /(?:$Ident|-?$Constant)\s* |
| 2216 | $Compare\s* |
| 2217 | (?:$Ident|-?$Constant)/x && |
| 2218 | $value =~ s/\([^\(\)]*\)/1/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2219 | } |
Andy Whitcroft | fb2d2c1 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2220 | #print "value<$value>\n"; |
| 2221 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2222 | ERROR("return is not a function, parentheses are not required\n" . $herecurr); |
| 2223 | |
| 2224 | } elsif ($spacing !~ /\s+/) { |
| 2225 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
| 2226 | } |
| 2227 | } |
Andy Whitcroft | 53a3c44 | 2010-10-26 14:23:14 -0700 | [diff] [blame] | 2228 | # Return of what appears to be an errno should normally be -'ve |
| 2229 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
| 2230 | my $name = $1; |
| 2231 | if ($name ne 'EOF' && $name ne 'ERROR') { |
| 2232 | WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
| 2233 | } |
| 2234 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2235 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2236 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2237 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2238 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
Andy Whitcroft | f5fe35d | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2241 | # Check for illegal assignment in if conditional -- and check for trailing |
| 2242 | # statements after the conditional. |
Andy Whitcroft | 170d3a2 | 2008-10-15 22:02:30 -0700 | [diff] [blame] | 2243 | if ($line =~ /do\s*(?!{)/) { |
| 2244 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 2245 | $remain_next, $off_next); |
| 2246 | $stat_next =~ s/\n./\n /g; |
| 2247 | ##print "stat<$stat> stat_next<$stat_next>\n"; |
| 2248 | |
| 2249 | if ($stat_next =~ /^\s*while\b/) { |
| 2250 | # If the statement carries leading newlines, |
| 2251 | # then count those as offsets. |
| 2252 | my ($whitespace) = |
| 2253 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2254 | my $offset = |
| 2255 | statement_rawlines($whitespace) - 1; |
| 2256 | |
| 2257 | $suppress_whiletrailers{$line_nr_next + |
| 2258 | $offset} = 1; |
| 2259 | } |
| 2260 | } |
| 2261 | if (!defined $suppress_whiletrailers{$linenr} && |
| 2262 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2263 | my ($s, $c) = ($stat, $cond); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2264 | |
Andy Whitcroft | b53c8e1 | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 2265 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2266 | ERROR("do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | # Find out what is on the end of the line after the |
| 2270 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2271 | substr($s, 0, length($c), ''); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2272 | $s =~ s/\n.*//g; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2273 | $s =~ s/$;//g; # Remove any comments |
Andy Whitcroft | 5321016 | 2008-07-23 21:29:03 -0700 | [diff] [blame] | 2274 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
| 2275 | $c !~ /}\s*while\s*/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2276 | { |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2277 | # Find out how long the conditional actually is. |
| 2278 | my @newlines = ($c =~ /\n/gs); |
| 2279 | my $cond_lines = 1 + $#newlines; |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2280 | my $stat_real = ''; |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2281 | |
Hidetoshi Seto | 42bdf74 | 2010-03-05 13:43:50 -0800 | [diff] [blame] | 2282 | $stat_real = raw_line($linenr, $cond_lines) |
| 2283 | . "\n" if ($cond_lines); |
Andy Whitcroft | bb44ad3 | 2008-10-15 22:02:34 -0700 | [diff] [blame] | 2284 | if (defined($stat_real) && $cond_lines > 1) { |
| 2285 | $stat_real = "[...]\n$stat_real"; |
| 2286 | } |
| 2287 | |
| 2288 | ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2292 | # Check for bitwise tests written as boolean |
| 2293 | if ($line =~ / |
| 2294 | (?: |
| 2295 | (?:\[|\(|\&\&|\|\|) |
| 2296 | \s*0[xX][0-9]+\s* |
| 2297 | (?:\&\&|\|\|) |
| 2298 | | |
| 2299 | (?:\&\&|\|\|) |
| 2300 | \s*0[xX][0-9]+\s* |
| 2301 | (?:\&\&|\|\||\)|\]) |
| 2302 | )/x) |
| 2303 | { |
| 2304 | WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
| 2305 | } |
| 2306 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2307 | # if and else should not have general statements after it |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2308 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
| 2309 | my $s = $1; |
| 2310 | $s =~ s/$;//g; # Remove any comments |
| 2311 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { |
| 2312 | ERROR("trailing statements should be on next line\n" . $herecurr); |
| 2313 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2314 | } |
Andy Whitcroft | 3966778 | 2009-01-15 13:51:06 -0800 | [diff] [blame] | 2315 | # if should not continue a brace |
| 2316 | if ($line =~ /}\s*if\b/) { |
| 2317 | ERROR("trailing statements should be on next line\n" . |
| 2318 | $herecurr); |
| 2319 | } |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2320 | # case and default should not have general statements after them |
| 2321 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && |
| 2322 | $line !~ /\G(?: |
Andy Whitcroft | 3fef12d | 2008-10-15 22:02:36 -0700 | [diff] [blame] | 2323 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
Andy Whitcroft | a1080bf | 2008-10-15 22:02:25 -0700 | [diff] [blame] | 2324 | \s*return\s+ |
| 2325 | )/xg) |
| 2326 | { |
| 2327 | ERROR("trailing statements should be on next line\n" . $herecurr); |
| 2328 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2329 | |
| 2330 | # Check for }<nl>else {, these must be at the same |
| 2331 | # indent level to be relevant to each other. |
| 2332 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 2333 | $previndent == $indent) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2334 | ERROR("else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2335 | } |
| 2336 | |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2337 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
| 2338 | $previndent == $indent) { |
| 2339 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
| 2340 | |
| 2341 | # Find out what is on the end of the line after the |
| 2342 | # conditional. |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2343 | substr($s, 0, length($c), ''); |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2344 | $s =~ s/\n.*//g; |
| 2345 | |
| 2346 | if ($s =~ /^\s*;/) { |
| 2347 | ERROR("while should follow close brace '}'\n" . $hereprev); |
| 2348 | } |
| 2349 | } |
| 2350 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2351 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 2352 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 2353 | # print "No studly caps, use _\n"; |
| 2354 | # print "$herecurr"; |
| 2355 | # $clean = 0; |
| 2356 | # } |
| 2357 | |
| 2358 | #no spaces allowed after \ in define |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2359 | if ($line=~/\#\s*define.*\\\s$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2360 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2363 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2364 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2365 | my $file = "$1.h"; |
| 2366 | my $checkfile = "include/linux/$file"; |
| 2367 | if (-f "$root/$checkfile" && |
| 2368 | $realfile ne $checkfile && |
Wolfram Sang | 7840a94 | 2010-08-09 17:20:57 -0700 | [diff] [blame] | 2369 | $1 !~ /$allowed_asm_includes/) |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2370 | { |
Andy Whitcroft | e09dec4 | 2008-10-15 22:02:20 -0700 | [diff] [blame] | 2371 | if ($realfile =~ m{^arch/}) { |
| 2372 | CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 2373 | } else { |
| 2374 | WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); |
| 2375 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2379 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 2380 | # first statement and ensure its the whole macro if its not enclosed |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2381 | # in a known good container |
Andy Whitcroft | b8f96a3 | 2008-07-23 21:29:07 -0700 | [diff] [blame] | 2382 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
| 2383 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2384 | my $ln = $linenr; |
| 2385 | my $cnt = $realcnt; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2386 | my ($off, $dstat, $dcond, $rest); |
| 2387 | my $ctx = ''; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2388 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2389 | my $args = defined($1); |
| 2390 | |
| 2391 | # Find the end of the macro and limit our statement |
| 2392 | # search to that. |
| 2393 | while ($cnt > 0 && defined $lines[$ln - 1] && |
| 2394 | $lines[$ln - 1] =~ /^(?:-|..*\\$)/) |
| 2395 | { |
| 2396 | $ctx .= $rawlines[$ln - 1] . "\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 2397 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2398 | $ln++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2399 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2400 | $ctx .= $rawlines[$ln - 1]; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2401 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2402 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 2403 | ctx_statement_block($linenr, $ln - $linenr + 1, 0); |
| 2404 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 2405 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2406 | |
| 2407 | # Extract the remainder of the define (if any) and |
| 2408 | # rip off surrounding spaces, and trailing \'s. |
| 2409 | $rest = ''; |
Andy Whitcroft | 636d140 | 2008-10-15 22:02:18 -0700 | [diff] [blame] | 2410 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { |
| 2411 | #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; |
Andy Whitcroft | a3bb97a | 2008-07-23 21:29:00 -0700 | [diff] [blame] | 2412 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { |
| 2413 | $rest .= substr($lines[$ln - 1], $off) . "\n"; |
| 2414 | $cnt--; |
| 2415 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2416 | $ln++; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2417 | $off = 0; |
| 2418 | } |
| 2419 | $rest =~ s/\\\n.//g; |
| 2420 | $rest =~ s/^\s*//s; |
| 2421 | $rest =~ s/\s*$//s; |
| 2422 | |
| 2423 | # Clean up the original statement. |
| 2424 | if ($args) { |
| 2425 | substr($dstat, 0, length($dcond), ''); |
| 2426 | } else { |
| 2427 | $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; |
| 2428 | } |
Andy Whitcroft | 292f1a9 | 2008-07-23 21:29:11 -0700 | [diff] [blame] | 2429 | $dstat =~ s/$;//g; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2430 | $dstat =~ s/\\\n.//g; |
| 2431 | $dstat =~ s/^\s*//s; |
| 2432 | $dstat =~ s/\s*$//s; |
| 2433 | |
| 2434 | # Flatten any parentheses and braces |
Andy Whitcroft | bf30d6e | 2008-10-15 22:02:33 -0700 | [diff] [blame] | 2435 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 2436 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
| 2437 | $dstat =~ s/\[[^\{\}]*\]/1/) |
| 2438 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2439 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2440 | |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2441 | my $exceptions = qr{ |
| 2442 | $Declare| |
| 2443 | module_param_named| |
| 2444 | MODULE_PARAM_DESC| |
| 2445 | DECLARE_PER_CPU| |
| 2446 | DEFINE_PER_CPU| |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 2447 | __typeof__\(| |
Stefani Seibold | 22fd2d3 | 2010-03-05 13:43:52 -0800 | [diff] [blame] | 2448 | union| |
| 2449 | struct| |
Andy Whitcroft | ea71a0a | 2009-09-21 17:04:38 -0700 | [diff] [blame] | 2450 | \.$Ident\s*=\s*| |
| 2451 | ^\"|\"$ |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2452 | }x; |
Andy Whitcroft | 383099f | 2009-01-06 14:41:18 -0800 | [diff] [blame] | 2453 | #print "REST<$rest> dstat<$dstat>\n"; |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2454 | if ($rest ne '') { |
| 2455 | if ($rest !~ /while\s*\(/ && |
| 2456 | $dstat !~ /$exceptions/) |
| 2457 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2458 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | } elsif ($ctx !~ /;/) { |
| 2462 | if ($dstat ne '' && |
| 2463 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && |
| 2464 | $dstat !~ /$exceptions/ && |
Andy Whitcroft | b132e5d | 2008-10-15 22:02:31 -0700 | [diff] [blame] | 2465 | $dstat !~ /^\.$Ident\s*=/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2466 | $dstat =~ /$Operators/) |
| 2467 | { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2468 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 2469 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2470 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Mike Frysinger | 080ba92 | 2009-01-06 14:41:25 -0800 | [diff] [blame] | 2473 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
| 2474 | # all assignments may have only one of the following with an assignment: |
| 2475 | # . |
| 2476 | # ALIGN(...) |
| 2477 | # VMLINUX_SYMBOL(...) |
| 2478 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { |
| 2479 | WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); |
| 2480 | } |
| 2481 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2482 | # check for redundant bracing round if etc |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2483 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
| 2484 | my ($level, $endln, @chunks) = |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2485 | ctx_statement_full($linenr, $realcnt, 1); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2486 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2487 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
| 2488 | if ($#chunks > 0 && $level == 0) { |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2489 | my $allowed = 0; |
| 2490 | my $seen = 0; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2491 | my $herectx = $here . "\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2492 | my $ln = $linenr - 1; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2493 | for my $chunk (@chunks) { |
| 2494 | my ($cond, $block) = @{$chunk}; |
| 2495 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2496 | # If the condition carries leading newlines, then count those as offsets. |
| 2497 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
| 2498 | my $offset = statement_rawlines($whitespace) - 1; |
| 2499 | |
| 2500 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
| 2501 | |
| 2502 | # We have looked at and allowed this specific line. |
| 2503 | $suppress_ifbraces{$ln + $offset} = 1; |
| 2504 | |
| 2505 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2506 | $ln += statement_rawlines($block) - 1; |
| 2507 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2508 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2509 | |
| 2510 | $seen++ if ($block =~ /^\s*{/); |
| 2511 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2512 | #print "cond<$cond> block<$block> allowed<$allowed>\n"; |
| 2513 | if (statement_lines($cond) > 1) { |
| 2514 | #print "APW: ALLOWED: cond<$cond>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2515 | $allowed = 1; |
| 2516 | } |
| 2517 | if ($block =~/\b(?:if|for|while)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2518 | #print "APW: ALLOWED: block<$block>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2519 | $allowed = 1; |
| 2520 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2521 | if (statement_block_size($block) > 1) { |
| 2522 | #print "APW: ALLOWED: lines block<$block>\n"; |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2523 | $allowed = 1; |
| 2524 | } |
| 2525 | } |
| 2526 | if ($seen && !$allowed) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2527 | WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2531 | if (!defined $suppress_ifbraces{$linenr - 1} && |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2532 | $line =~ /\b(if|while|for|else)\b/) { |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2533 | my $allowed = 0; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2534 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2535 | # Check the pre-context. |
| 2536 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { |
| 2537 | #print "APW: ALLOWED: pre<$1>\n"; |
| 2538 | $allowed = 1; |
| 2539 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2540 | |
| 2541 | my ($level, $endln, @chunks) = |
| 2542 | ctx_statement_full($linenr, $realcnt, $-[0]); |
| 2543 | |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2544 | # Check the condition. |
| 2545 | my ($cond, $block) = @{$chunks[0]}; |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2546 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2547 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2548 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2549 | } |
| 2550 | if (statement_lines($cond) > 1) { |
| 2551 | #print "APW: ALLOWED: cond<$cond>\n"; |
| 2552 | $allowed = 1; |
| 2553 | } |
| 2554 | if ($block =~/\b(?:if|for|while)\b/) { |
| 2555 | #print "APW: ALLOWED: block<$block>\n"; |
| 2556 | $allowed = 1; |
| 2557 | } |
| 2558 | if (statement_block_size($block) > 1) { |
| 2559 | #print "APW: ALLOWED: lines block<$block>\n"; |
| 2560 | $allowed = 1; |
| 2561 | } |
| 2562 | # Check the post-context. |
| 2563 | if (defined $chunks[1]) { |
| 2564 | my ($cond, $block) = @{$chunks[1]}; |
| 2565 | if (defined $cond) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2566 | substr($block, 0, length($cond), ''); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2567 | } |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2568 | if ($block =~ /^\s*\{/) { |
| 2569 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; |
| 2570 | $allowed = 1; |
| 2571 | } |
| 2572 | } |
| 2573 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { |
| 2574 | my $herectx = $here . "\n";; |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2575 | my $cnt = statement_rawlines($block); |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2576 | |
Andy Whitcroft | f055663 | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2577 | for (my $n = 0; $n < $cnt; $n++) { |
| 2578 | $herectx .= raw_line($linenr, $n) . "\n";; |
Andy Whitcroft | cf65504 | 2008-03-04 14:28:20 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2585 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2586 | for my $inc (@dep_includes) { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2587 | if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2588 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2592 | # don't use deprecated functions |
| 2593 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2594 | if ($line =~ /\b$func\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2595 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 2600 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 2601 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2602 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2605 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
| 2606 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { |
| 2607 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); |
| 2608 | } |
| 2609 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2610 | # warn about #if 0 |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2611 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2612 | CHK("if this code is redundant consider removing it\n" . |
| 2613 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2616 | # check for needless kfree() checks |
| 2617 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 2618 | my $expr = $1; |
| 2619 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { |
Wolfram Sang | 3c23214 | 2008-07-23 21:29:05 -0700 | [diff] [blame] | 2620 | WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2621 | } |
| 2622 | } |
Greg Kroah-Hartman | 4c432a8 | 2008-07-23 21:29:04 -0700 | [diff] [blame] | 2623 | # check for needless usb_free_urb() checks |
| 2624 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 2625 | my $expr = $1; |
| 2626 | if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { |
| 2627 | WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); |
| 2628 | } |
| 2629 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2630 | |
Patrick Pannuto | 1a15a25 | 2010-08-09 17:21:01 -0700 | [diff] [blame] | 2631 | # prefer usleep_range over udelay |
| 2632 | if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { |
| 2633 | # ignore udelay's < 10, however |
| 2634 | if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { |
| 2635 | CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); |
| 2636 | } |
| 2637 | } |
| 2638 | |
Patrick Pannuto | 09ef872 | 2010-08-09 17:21:02 -0700 | [diff] [blame] | 2639 | # warn about unexpectedly long msleep's |
| 2640 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { |
| 2641 | if ($1 < 20) { |
| 2642 | WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); |
| 2643 | } |
| 2644 | } |
| 2645 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2646 | # warn about #ifdefs in C files |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2647 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 2648 | # print "#ifdef in C files should be avoided\n"; |
| 2649 | # print "$herecurr"; |
| 2650 | # $clean = 0; |
| 2651 | # } |
| 2652 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2653 | # warn about spacing in #ifdefs |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2654 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 2655 | ERROR("exactly one space required after that #$1\n" . $herecurr); |
| 2656 | } |
| 2657 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2658 | # check for spinlock_t definitions without a comment. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2659 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
| 2660 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2661 | my $which = $1; |
| 2662 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2663 | CHK("$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2664 | } |
| 2665 | } |
| 2666 | # check for memory barriers without a comment. |
| 2667 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 2668 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2669 | CHK("memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | # check of hardware specific defines |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2673 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2674 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2675 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2676 | |
Tobias Klauser | d4977c7 | 2010-05-24 14:33:30 -0700 | [diff] [blame] | 2677 | # Check that the storage class is at the beginning of a declaration |
| 2678 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { |
| 2679 | WARN("storage class should be at the beginning of the declaration\n" . $herecurr) |
| 2680 | } |
| 2681 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2682 | # check the location of the inline attribute, that it is between |
| 2683 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2684 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 2685 | $line =~ /\b$Inline\s+$Storage\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2686 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 2687 | } |
| 2688 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2689 | # Check for __inline__ and __inline, prefer inline |
| 2690 | if ($line =~ /\b(__inline__|__inline)\b/) { |
| 2691 | WARN("plain inline is preferred over $1\n" . $herecurr); |
| 2692 | } |
| 2693 | |
Joe Perches | 8f53a9b | 2010-03-05 13:43:48 -0800 | [diff] [blame] | 2694 | # check for sizeof(&) |
| 2695 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
| 2696 | WARN("sizeof(& should be avoided\n" . $herecurr); |
| 2697 | } |
| 2698 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2699 | # check for new externs in .c files. |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2700 | if ($realfile =~ /\.c$/ && defined $stat && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2701 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2702 | { |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2703 | my $function_name = $1; |
| 2704 | my $paren_space = $2; |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2705 | |
| 2706 | my $s = $stat; |
| 2707 | if (defined $cond) { |
| 2708 | substr($s, 0, length($cond), ''); |
| 2709 | } |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2710 | if ($s =~ /^\s*;/ && |
| 2711 | $function_name ne 'uninitialized_var') |
| 2712 | { |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2713 | WARN("externs should be avoided in .c files\n" . $herecurr); |
| 2714 | } |
| 2715 | |
| 2716 | if ($paren_space =~ /\n/) { |
| 2717 | WARN("arguments for function declarations should follow identifier\n" . $herecurr); |
| 2718 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2719 | |
| 2720 | } elsif ($realfile =~ /\.c$/ && defined $stat && |
| 2721 | $stat =~ /^.\s*extern\s+/) |
| 2722 | { |
| 2723 | WARN("externs should be avoided in .c files\n" . $herecurr); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | # checks for new __setup's |
| 2727 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 2728 | my $name = $1; |
| 2729 | |
| 2730 | if (!grep(/$name/, @setup_docs)) { |
| 2731 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
| 2732 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 2733 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 2734 | |
| 2735 | # check for pointless casting of kmalloc return |
| 2736 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { |
| 2737 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 2738 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2739 | |
| 2740 | # check for gcc specific __FUNCTION__ |
| 2741 | if ($line =~ /__FUNCTION__/) { |
| 2742 | WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
| 2743 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2744 | |
| 2745 | # check for semaphores used as mutexes |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2746 | if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2747 | WARN("mutexes are preferred for single holder semaphores\n" . $herecurr); |
| 2748 | } |
| 2749 | # check for semaphores used as mutexes |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2750 | if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) { |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2751 | WARN("consider using a completion\n" . $herecurr); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 2752 | |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2753 | } |
| 2754 | # recommend strict_strto* over simple_strto* |
| 2755 | if ($line =~ /\bsimple_(strto.*?)\s*\(/) { |
| 2756 | WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); |
| 2757 | } |
Michael Ellerman | f3db663 | 2008-07-23 21:28:57 -0700 | [diff] [blame] | 2758 | # check for __initcall(), use device_initcall() explicitly please |
| 2759 | if ($line =~ /^.\s*__initcall\s*\(/) { |
| 2760 | WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); |
| 2761 | } |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 2762 | # check for various ops structs, ensure they are const. |
| 2763 | my $struct_ops = qr{acpi_dock_ops| |
| 2764 | address_space_operations| |
| 2765 | backlight_ops| |
| 2766 | block_device_operations| |
| 2767 | dentry_operations| |
| 2768 | dev_pm_ops| |
| 2769 | dma_map_ops| |
| 2770 | extent_io_ops| |
| 2771 | file_lock_operations| |
| 2772 | file_operations| |
| 2773 | hv_ops| |
| 2774 | ide_dma_ops| |
| 2775 | intel_dvo_dev_ops| |
| 2776 | item_operations| |
| 2777 | iwl_ops| |
| 2778 | kgdb_arch| |
| 2779 | kgdb_io| |
| 2780 | kset_uevent_ops| |
| 2781 | lock_manager_operations| |
| 2782 | microcode_ops| |
| 2783 | mtrr_ops| |
| 2784 | neigh_ops| |
| 2785 | nlmsvc_binding| |
| 2786 | pci_raw_ops| |
| 2787 | pipe_buf_operations| |
| 2788 | platform_hibernation_ops| |
| 2789 | platform_suspend_ops| |
| 2790 | proto_ops| |
| 2791 | rpc_pipe_ops| |
| 2792 | seq_operations| |
| 2793 | snd_ac97_build_ops| |
| 2794 | soc_pcmcia_socket_ops| |
| 2795 | stacktrace_ops| |
| 2796 | sysfs_ops| |
| 2797 | tty_operations| |
| 2798 | usb_mon_operations| |
| 2799 | wd_ops}x; |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 2800 | if ($line !~ /\bconst\b/ && |
Emese Revfy | 7940484 | 2010-03-05 13:43:53 -0800 | [diff] [blame] | 2801 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
Andy Whitcroft | 6903ffb | 2009-01-15 13:51:07 -0800 | [diff] [blame] | 2802 | WARN("struct $1 should normally be const\n" . |
| 2803 | $herecurr); |
Andy Whitcroft | 2b6db5c | 2009-01-06 14:41:29 -0800 | [diff] [blame] | 2804 | } |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2805 | |
| 2806 | # use of NR_CPUS is usually wrong |
| 2807 | # ignore definitions of NR_CPUS and usage to define arrays as likely right |
| 2808 | if ($line =~ /\bNR_CPUS\b/ && |
Andy Whitcroft | c45dcab | 2008-06-05 22:46:01 -0700 | [diff] [blame] | 2809 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
| 2810 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && |
Andy Whitcroft | 171ae1a | 2008-04-29 00:59:32 -0700 | [diff] [blame] | 2811 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
| 2812 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && |
| 2813 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) |
Andy Whitcroft | 773647a | 2008-03-28 14:15:58 -0700 | [diff] [blame] | 2814 | { |
| 2815 | WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); |
| 2816 | } |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2817 | |
| 2818 | # check for %L{u,d,i} in strings |
| 2819 | my $string; |
| 2820 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { |
| 2821 | $string = substr($rawline, $-[1], $+[1] - $-[1]); |
Andy Whitcroft | 2a1bc5d | 2008-10-15 22:02:23 -0700 | [diff] [blame] | 2822 | $string =~ s/%%/__/g; |
Andy Whitcroft | 9c9ba34 | 2008-04-29 00:59:33 -0700 | [diff] [blame] | 2823 | if ($string =~ /(?<!%)%L[udi]/) { |
| 2824 | WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
| 2825 | last; |
| 2826 | } |
| 2827 | } |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 2828 | |
| 2829 | # whine mightly about in_atomic |
| 2830 | if ($line =~ /\bin_atomic\s*\(/) { |
| 2831 | if ($realfile =~ m@^drivers/@) { |
| 2832 | ERROR("do not use in_atomic in drivers\n" . $herecurr); |
Andy Whitcroft | f4a8773 | 2009-02-27 14:03:05 -0800 | [diff] [blame] | 2833 | } elsif ($realfile !~ m@^kernel/@) { |
Andy Whitcroft | 691d77b | 2009-01-06 14:41:16 -0800 | [diff] [blame] | 2834 | WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
| 2835 | } |
| 2836 | } |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 2837 | |
| 2838 | # check for lockdep_set_novalidate_class |
| 2839 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || |
| 2840 | $line =~ /__lockdep_no_validate__\s*\)/ ) { |
| 2841 | if ($realfile !~ m@^kernel/lockdep@ && |
| 2842 | $realfile !~ m@^include/linux/lockdep@ && |
| 2843 | $realfile !~ m@^drivers/base/core@) { |
| 2844 | ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); |
| 2845 | } |
| 2846 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | # If we have no input at all, then there is nothing to report on |
| 2850 | # so just keep quiet. |
| 2851 | if ($#rawlines == -1) { |
| 2852 | exit(0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2855 | # In mailback mode only produce a report in the negative, for |
| 2856 | # things that appear to be patches. |
| 2857 | if ($mailback && ($clean == 1 || !$is_patch)) { |
| 2858 | exit(0); |
| 2859 | } |
| 2860 | |
| 2861 | # This is not a patch, and we are are in 'no-patch' mode so |
| 2862 | # just keep quiet. |
| 2863 | if (!$chk_patch && !$is_patch) { |
| 2864 | exit(0); |
| 2865 | } |
| 2866 | |
| 2867 | if (!$is_patch) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2868 | ERROR("Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2869 | } |
| 2870 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 2871 | ERROR("Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2874 | print report_dump(); |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2875 | if ($summary && !($clean == 1 && $quiet == 1)) { |
| 2876 | print "$filename " if ($summary_file); |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2877 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 2878 | (($check)? "$cnt_chk checks, " : "") . |
| 2879 | "$cnt_lines lines checked\n"; |
| 2880 | print "\n" if ($quiet == 0); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 2881 | } |
Andy Whitcroft | 8905a67 | 2007-11-28 16:21:06 -0800 | [diff] [blame] | 2882 | |
Andy Whitcroft | d2c0a23 | 2010-10-26 14:23:12 -0700 | [diff] [blame] | 2883 | if ($quiet == 0) { |
| 2884 | # If there were whitespace errors which cleanpatch can fix |
| 2885 | # then suggest that. |
| 2886 | if ($rpt_cleaners) { |
| 2887 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; |
| 2888 | print " scripts/cleanfile\n\n"; |
| 2889 | } |
| 2890 | } |
| 2891 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2892 | if ($clean == 1 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2893 | print "$vname has no obvious style problems and is ready for submission.\n" |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2894 | } |
| 2895 | if ($clean == 0 && $quiet == 0) { |
Andy Whitcroft | c2fdda0 | 2008-02-08 04:20:54 -0800 | [diff] [blame] | 2896 | print "$vname has style problems, please review. If any of these errors\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2897 | print "are false positives report them to the maintainer, see\n"; |
| 2898 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 2899 | } |
Andy Whitcroft | 13214ad | 2008-02-08 04:22:03 -0800 | [diff] [blame] | 2900 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 2901 | return $clean; |
| 2902 | } |