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