Commit | Line | Data |
---|---|---|
0a920b5b | 1 | #!/usr/bin/perl -w |
dbf004d7 | 2 | # (c) 2001, Dave Jones. (the file handling bit) |
00df344f | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
2a5a2c25 | 4 | # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) |
015830be | 5 | # (c) 2008-2010 Andy Whitcroft <apw@canonical.com> |
0a920b5b AW |
6 | # Licensed under the terms of the GNU GPL License version 2 |
7 | ||
8 | use strict; | |
9 | ||
10 | my $P = $0; | |
00df344f | 11 | $P =~ s@.*/@@g; |
0a920b5b | 12 | |
267ad8f4 | 13 | my $V = '0.31'; |
0a920b5b AW |
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; | |
773647a0 | 21 | my $tst_only; |
6c72ffaa | 22 | my $emacs = 0; |
8905a67c | 23 | my $terse = 0; |
6c72ffaa AW |
24 | my $file = 0; |
25 | my $check = 0; | |
8905a67c AW |
26 | my $summary = 1; |
27 | my $mailback = 0; | |
13214adf | 28 | my $summary_file = 0; |
6c72ffaa | 29 | my $root; |
c2fdda0d | 30 | my %debug; |
77f5b10a HE |
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 | ||
0a920b5b | 66 | GetOptions( |
6c72ffaa | 67 | 'q|quiet+' => \$quiet, |
0a920b5b AW |
68 | 'tree!' => \$tree, |
69 | 'signoff!' => \$chk_signoff, | |
70 | 'patch!' => \$chk_patch, | |
6c72ffaa | 71 | 'emacs!' => \$emacs, |
8905a67c | 72 | 'terse!' => \$terse, |
77f5b10a | 73 | 'f|file!' => \$file, |
6c72ffaa AW |
74 | 'subjective!' => \$check, |
75 | 'strict!' => \$check, | |
76 | 'root=s' => \$root, | |
8905a67c AW |
77 | 'summary!' => \$summary, |
78 | 'mailback!' => \$mailback, | |
13214adf AW |
79 | 'summary-file!' => \$summary_file, |
80 | ||
c2fdda0d | 81 | 'debug=s' => \%debug, |
773647a0 | 82 | 'test-only=s' => \$tst_only, |
77f5b10a HE |
83 | 'h|help' => \$help, |
84 | 'version' => \$help | |
85 | ) or help(1); | |
86 | ||
87 | help(0) if ($help); | |
0a920b5b AW |
88 | |
89 | my $exit = 0; | |
90 | ||
91 | if ($#ARGV < 0) { | |
77f5b10a | 92 | print "$P: no input files\n"; |
0a920b5b AW |
93 | exit(1); |
94 | } | |
95 | ||
c2fdda0d AW |
96 | my $dbg_values = 0; |
97 | my $dbg_possible = 0; | |
7429c690 | 98 | my $dbg_type = 0; |
a1ef277e | 99 | my $dbg_attr = 0; |
c2fdda0d | 100 | for my $key (keys %debug) { |
21caa13c AW |
101 | ## no critic |
102 | eval "\${dbg_$key} = '$debug{$key}';"; | |
103 | die "$@" if ($@); | |
c2fdda0d AW |
104 | } |
105 | ||
d2c0a235 AW |
106 | my $rpt_cleaners = 0; |
107 | ||
8905a67c AW |
108 | if ($terse) { |
109 | $emacs = 1; | |
110 | $quiet++; | |
111 | } | |
112 | ||
6c72ffaa AW |
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 | } | |
0a920b5b AW |
131 | } |
132 | ||
6c72ffaa AW |
133 | my $emitted_corrupt = 0; |
134 | ||
2ceb532b AW |
135 | our $Ident = qr{ |
136 | [A-Za-z_][A-Za-z\d_]* | |
137 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* | |
138 | }x; | |
6c72ffaa AW |
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| | |
417495ed AW |
147 | __kprobes| |
148 | __ref | |
6c72ffaa | 149 | }x; |
52131292 WS |
150 | |
151 | # Notes to $Attribute: | |
152 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check | |
6c72ffaa AW |
153 | our $Attribute = qr{ |
154 | const| | |
03f1df7d JP |
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| | |
6c72ffaa AW |
169 | __read_mostly| |
170 | __kprobes| | |
52131292 | 171 | __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| |
24e1d81a AW |
172 | ____cacheline_aligned| |
173 | ____cacheline_aligned_in_smp| | |
5fe3af11 AW |
174 | ____cacheline_internodealigned_in_smp| |
175 | __weak | |
6c72ffaa | 176 | }x; |
c45dcabd | 177 | our $Modifier; |
6c72ffaa | 178 | our $Inline = qr{inline|__always_inline|noinline}; |
6c72ffaa AW |
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{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | |
86f9d059 | 184 | our $Compare = qr{<=|>=|==|!=|<|>}; |
6c72ffaa AW |
185 | our $Operators = qr{ |
186 | <=|>=|==|!=| | |
187 | =>|->|<<|>>|<|>|!|~| | |
c2fdda0d | 188 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
6c72ffaa AW |
189 | }x; |
190 | ||
8905a67c AW |
191 | our $NonptrType; |
192 | our $Type; | |
193 | our $Declare; | |
194 | ||
171ae1a4 AW |
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 | ||
8ed22cad | 206 | our $typeTypedefs = qr{(?x: |
fb9e9096 | 207 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
8ed22cad AW |
208 | atomic_t |
209 | )}; | |
210 | ||
691e669b JP |
211 | our $logFunctions = qr{(?x: |
212 | printk| | |
213 | pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)| | |
8bbea968 | 214 | (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)| |
691e669b JP |
215 | WARN| |
216 | panic | |
217 | )}; | |
218 | ||
8905a67c AW |
219 | our @typeList = ( |
220 | qr{void}, | |
c45dcabd AW |
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}, | |
8905a67c AW |
228 | qr{unsigned}, |
229 | qr{float}, | |
230 | qr{double}, | |
231 | qr{bool}, | |
8905a67c AW |
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 | ); | |
c45dcabd AW |
239 | our @modifierList = ( |
240 | qr{fastcall}, | |
241 | ); | |
8905a67c | 242 | |
7840a94c WS |
243 | our $allowed_asm_includes = qr{(?x: |
244 | irq| | |
245 | memory | |
246 | )}; | |
247 | # memory.h: ARM has a custom one | |
248 | ||
8905a67c | 249 | sub build_types { |
d2172eb5 AW |
250 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; |
251 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; | |
c8cb2ca3 | 252 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; |
8905a67c | 253 | $NonptrType = qr{ |
d2172eb5 | 254 | (?:$Modifier\s+|const\s+)* |
cf655043 | 255 | (?: |
c45dcabd | 256 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| |
8ed22cad | 257 | (?:$typeTypedefs\b)| |
c45dcabd | 258 | (?:${all}\b) |
cf655043 | 259 | ) |
c8cb2ca3 | 260 | (?:\s+$Modifier|\s+const)* |
8905a67c AW |
261 | }x; |
262 | $Type = qr{ | |
c45dcabd | 263 | $NonptrType |
65863862 | 264 | (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? |
c8cb2ca3 | 265 | (?:\s+$Inline|\s+$Modifier)* |
8905a67c AW |
266 | }x; |
267 | $Declare = qr{(?:$Storage\s+)?$Type}; | |
268 | } | |
269 | build_types(); | |
6c72ffaa AW |
270 | |
271 | $chk_signoff = 0 if ($file); | |
272 | ||
4a0df2ef AW |
273 | my @dep_includes = (); |
274 | my @dep_functions = (); | |
6c72ffaa AW |
275 | my $removal = "Documentation/feature-removal-schedule.txt"; |
276 | if ($tree && -f "$root/$removal") { | |
21caa13c | 277 | open(my $REMOVE, '<', "$root/$removal") || |
6c72ffaa | 278 | die "$P: $removal: open failed - $!\n"; |
21caa13c | 279 | while (<$REMOVE>) { |
f0a594c1 AW |
280 | if (/^Check:\s+(.*\S)/) { |
281 | for my $entry (split(/[, ]+/, $1)) { | |
282 | if ($entry =~ m@include/(.*)@) { | |
4a0df2ef | 283 | push(@dep_includes, $1); |
4a0df2ef | 284 | |
f0a594c1 AW |
285 | } elsif ($entry !~ m@/@) { |
286 | push(@dep_functions, $entry); | |
287 | } | |
4a0df2ef | 288 | } |
0a920b5b AW |
289 | } |
290 | } | |
21caa13c | 291 | close($REMOVE); |
0a920b5b AW |
292 | } |
293 | ||
00df344f | 294 | my @rawlines = (); |
c2fdda0d AW |
295 | my @lines = (); |
296 | my $vname; | |
6c72ffaa | 297 | for my $filename (@ARGV) { |
21caa13c | 298 | my $FILE; |
6c72ffaa | 299 | if ($file) { |
21caa13c | 300 | open($FILE, '-|', "diff -u /dev/null $filename") || |
6c72ffaa | 301 | die "$P: $filename: diff failed - $!\n"; |
21caa13c AW |
302 | } elsif ($filename eq '-') { |
303 | open($FILE, '<&STDIN'); | |
6c72ffaa | 304 | } else { |
21caa13c | 305 | open($FILE, '<', "$filename") || |
6c72ffaa | 306 | die "$P: $filename: open failed - $!\n"; |
0a920b5b | 307 | } |
c2fdda0d AW |
308 | if ($filename eq '-') { |
309 | $vname = 'Your patch'; | |
310 | } else { | |
311 | $vname = $filename; | |
312 | } | |
21caa13c | 313 | while (<$FILE>) { |
6c72ffaa AW |
314 | chomp; |
315 | push(@rawlines, $_); | |
316 | } | |
21caa13c | 317 | close($FILE); |
c2fdda0d | 318 | if (!process($filename)) { |
6c72ffaa AW |
319 | $exit = 1; |
320 | } | |
321 | @rawlines = (); | |
13214adf | 322 | @lines = (); |
0a920b5b AW |
323 | } |
324 | ||
325 | exit($exit); | |
326 | ||
327 | sub top_of_kernel_tree { | |
6c72ffaa AW |
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 | } | |
0a920b5b | 340 | } |
6c72ffaa | 341 | return 1; |
0a920b5b AW |
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 | } | |
6c72ffaa | 364 | sub copy_spacing { |
773647a0 | 365 | (my $res = shift) =~ tr/\t/ /c; |
6c72ffaa AW |
366 | return $res; |
367 | } | |
0a920b5b | 368 | |
4a0df2ef AW |
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 | ||
773647a0 AW |
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 | } | |
00df344f AW |
393 | sub sanitise_line { |
394 | my ($line) = @_; | |
395 | ||
396 | my $res = ''; | |
397 | my $l = ''; | |
398 | ||
c2fdda0d | 399 | my $qlen = 0; |
773647a0 AW |
400 | my $off = 0; |
401 | my $c; | |
00df344f | 402 | |
773647a0 AW |
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; | |
00df344f | 417 | } |
81bc0e02 | 418 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { |
773647a0 AW |
419 | $sanitise_quote = ''; |
420 | substr($res, $off, 2, "$;$;"); | |
421 | $off++; | |
422 | next; | |
c2fdda0d | 423 | } |
113f04a8 DW |
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 | } | |
773647a0 AW |
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; | |
00df344f | 438 | } |
773647a0 AW |
439 | # Regular quotes. |
440 | if ($c eq "'" || $c eq '"') { | |
441 | if ($sanitise_quote eq '') { | |
442 | $sanitise_quote = $c; | |
00df344f | 443 | |
773647a0 AW |
444 | substr($res, $off, 1, $c); |
445 | next; | |
446 | } elsif ($sanitise_quote eq $c) { | |
447 | $sanitise_quote = ''; | |
448 | } | |
449 | } | |
00df344f | 450 | |
fae17dae | 451 | #print "c<$c> SQ<$sanitise_quote>\n"; |
773647a0 AW |
452 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { |
453 | substr($res, $off, 1, $;); | |
113f04a8 DW |
454 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { |
455 | substr($res, $off, 1, $;); | |
773647a0 AW |
456 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { |
457 | substr($res, $off, 1, 'X'); | |
458 | } else { | |
459 | substr($res, $off, 1, $c); | |
460 | } | |
c2fdda0d AW |
461 | } |
462 | ||
113f04a8 DW |
463 | if ($sanitise_quote eq '//') { |
464 | $sanitise_quote = ''; | |
465 | } | |
466 | ||
c2fdda0d | 467 | # The pathname on a #include may be surrounded by '<' and '>'. |
c45dcabd | 468 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { |
c2fdda0d AW |
469 | my $clean = 'X' x length($1); |
470 | $res =~ s@\<.*\>@<$clean>@; | |
471 | ||
472 | # The whole of a #error is a string. | |
c45dcabd | 473 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { |
c2fdda0d | 474 | my $clean = 'X' x length($1); |
c45dcabd | 475 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; |
c2fdda0d AW |
476 | } |
477 | ||
00df344f AW |
478 | return $res; |
479 | } | |
480 | ||
8905a67c AW |
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; | |
773647a0 | 487 | my $coff_set = 0; |
8905a67c | 488 | |
13214adf AW |
489 | my $loff = 0; |
490 | ||
8905a67c AW |
491 | my $type = ''; |
492 | my $level = 0; | |
a2750645 | 493 | my @stack = (); |
cf655043 | 494 | my $p; |
8905a67c AW |
495 | my $c; |
496 | my $len = 0; | |
13214adf AW |
497 | |
498 | my $remainder; | |
8905a67c | 499 | while (1) { |
a2750645 AW |
500 | @stack = (['', 0]) if ($#stack == -1); |
501 | ||
773647a0 | 502 | #warn "CSB: blk<$blk> remain<$remain>\n"; |
8905a67c AW |
503 | # If we are about to drop off the end, pull in more |
504 | # context. | |
505 | if ($off >= $len) { | |
506 | for (; $remain > 0; $line++) { | |
dea33496 | 507 | last if (!defined $lines[$line]); |
c2fdda0d | 508 | next if ($lines[$line] =~ /^-/); |
8905a67c | 509 | $remain--; |
13214adf | 510 | $loff = $len; |
c2fdda0d | 511 | $blk .= $lines[$line] . "\n"; |
8905a67c AW |
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"; | |
13214adf | 518 | if ($off >= $len) { |
8905a67c AW |
519 | last; |
520 | } | |
521 | } | |
cf655043 | 522 | $p = $c; |
8905a67c | 523 | $c = substr($blk, $off, 1); |
13214adf | 524 | $remainder = substr($blk, $off); |
8905a67c | 525 | |
773647a0 | 526 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; |
4635f4fb AW |
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 | ||
8905a67c AW |
537 | # Statement ends at the ';' or a close '}' at the |
538 | # outermost level. | |
539 | if ($level == 0 && $c eq ';') { | |
540 | last; | |
541 | } | |
542 | ||
13214adf | 543 | # An else is really a conditional as long as its not else if |
773647a0 AW |
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"; | |
13214adf AW |
552 | } |
553 | ||
8905a67c AW |
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; | |
773647a0 AW |
564 | $coff_set = 1; |
565 | #warn "CSB: mark coff<$coff>\n"; | |
8905a67c AW |
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) { | |
b998e001 PP |
577 | if (substr($blk, $off + 1, 1) eq ';') { |
578 | $off++; | |
579 | } | |
8905a67c AW |
580 | last; |
581 | } | |
582 | } | |
583 | $off++; | |
584 | } | |
a3bb97a7 | 585 | # We are truly at the end, so shuffle to the next line. |
13214adf | 586 | if ($off == $len) { |
a3bb97a7 | 587 | $loff = $len + 1; |
13214adf AW |
588 | $line++; |
589 | $remain--; | |
590 | } | |
8905a67c AW |
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 | ||
773647a0 | 598 | #print "coff<$coff> soff<$off> loff<$loff>\n"; |
13214adf AW |
599 | |
600 | return ($statement, $condition, | |
601 | $line, $remain + 1, $off - $loff + 1, $level); | |
602 | } | |
603 | ||
cf655043 AW |
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 | ||
13214adf AW |
647 | sub ctx_statement_full { |
648 | my ($linenr, $remain, $off) = @_; | |
649 | my ($statement, $condition, $level); | |
650 | ||
651 | my (@chunks); | |
652 | ||
cf655043 | 653 | # Grab the first conditional/block pair. |
13214adf AW |
654 | ($statement, $condition, $linenr, $remain, $off, $level) = |
655 | ctx_statement_block($linenr, $remain, $off); | |
773647a0 | 656 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; |
cf655043 AW |
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. | |
13214adf | 664 | for (;;) { |
13214adf AW |
665 | ($statement, $condition, $linenr, $remain, $off, $level) = |
666 | ctx_statement_block($linenr, $remain, $off); | |
cf655043 | 667 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; |
773647a0 | 668 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); |
cf655043 AW |
669 | #print "C: push\n"; |
670 | push(@chunks, [ $condition, $statement ]); | |
13214adf AW |
671 | } |
672 | ||
673 | return ($level, $linenr, @chunks); | |
8905a67c AW |
674 | } |
675 | ||
4a0df2ef | 676 | sub ctx_block_get { |
f0a594c1 | 677 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
4a0df2ef AW |
678 | my $line; |
679 | my $start = $linenr - 1; | |
4a0df2ef AW |
680 | my $blk = ''; |
681 | my @o; | |
682 | my @c; | |
683 | my @res = (); | |
684 | ||
f0a594c1 | 685 | my $level = 0; |
4635f4fb | 686 | my @stack = ($level); |
00df344f AW |
687 | for ($line = $start; $remain > 0; $line++) { |
688 | next if ($rawlines[$line] =~ /^-/); | |
689 | $remain--; | |
690 | ||
691 | $blk .= $rawlines[$line]; | |
4635f4fb AW |
692 | |
693 | # Handle nested #if/#else. | |
01464f30 | 694 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
4635f4fb | 695 | push(@stack, $level); |
01464f30 | 696 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
4635f4fb | 697 | $level = $stack[$#stack - 1]; |
01464f30 | 698 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
4635f4fb AW |
699 | $level = pop(@stack); |
700 | } | |
701 | ||
01464f30 | 702 | foreach my $c (split(//, $lines[$line])) { |
f0a594c1 AW |
703 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
704 | if ($off > 0) { | |
705 | $off--; | |
706 | next; | |
707 | } | |
4a0df2ef | 708 | |
f0a594c1 AW |
709 | if ($c eq $close && $level > 0) { |
710 | $level--; | |
711 | last if ($level == 0); | |
712 | } elsif ($c eq $open) { | |
713 | $level++; | |
714 | } | |
715 | } | |
4a0df2ef | 716 | |
f0a594c1 | 717 | if (!$outer || $level <= 1) { |
00df344f | 718 | push(@res, $rawlines[$line]); |
4a0df2ef AW |
719 | } |
720 | ||
f0a594c1 | 721 | last if ($level == 0); |
4a0df2ef AW |
722 | } |
723 | ||
f0a594c1 | 724 | return ($level, @res); |
4a0df2ef AW |
725 | } |
726 | sub ctx_block_outer { | |
727 | my ($linenr, $remain) = @_; | |
728 | ||
f0a594c1 AW |
729 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
730 | return @r; | |
4a0df2ef AW |
731 | } |
732 | sub ctx_block { | |
733 | my ($linenr, $remain) = @_; | |
734 | ||
f0a594c1 AW |
735 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
736 | return @r; | |
653d4876 AW |
737 | } |
738 | sub ctx_statement { | |
f0a594c1 AW |
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 { | |
653d4876 AW |
745 | my ($linenr, $remain) = @_; |
746 | ||
f0a594c1 | 747 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
4a0df2ef | 748 | } |
9c0ca6f9 AW |
749 | sub ctx_statement_level { |
750 | my ($linenr, $remain, $off) = @_; | |
751 | ||
752 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
753 | } | |
4a0df2ef AW |
754 | |
755 | sub ctx_locate_comment { | |
756 | my ($first_line, $end_line) = @_; | |
757 | ||
758 | # Catch a comment on the end of the line itself. | |
beae6332 | 759 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); |
4a0df2ef AW |
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++) { | |
00df344f AW |
767 | my $line = $rawlines[$linenr - 1]; |
768 | #warn " $line\n"; | |
4a0df2ef AW |
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 | ||
00df344f | 791 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
4a0df2ef AW |
792 | ##print "CMMT: $cmt\n"; |
793 | ||
794 | return ($cmt ne ''); | |
795 | } | |
796 | ||
4d001e4d AW |
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 | ||
6c72ffaa AW |
813 | sub cat_vet { |
814 | my ($vet) = @_; | |
815 | my ($res, $coded); | |
9c0ca6f9 | 816 | |
6c72ffaa AW |
817 | $res = ''; |
818 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { | |
819 | $res .= $1; | |
820 | if ($2 ne '') { | |
821 | $coded = sprintf("^%c", unpack('C', $2) + 64); | |
822 | $res .= $coded; | |
9c0ca6f9 AW |
823 | } |
824 | } | |
6c72ffaa | 825 | $res =~ s/$/\$/; |
9c0ca6f9 | 826 | |
6c72ffaa | 827 | return $res; |
9c0ca6f9 AW |
828 | } |
829 | ||
c2fdda0d | 830 | my $av_preprocessor = 0; |
cf655043 | 831 | my $av_pending; |
c2fdda0d | 832 | my @av_paren_type; |
1f65f947 | 833 | my $av_pend_colon; |
c2fdda0d AW |
834 | |
835 | sub annotate_reset { | |
836 | $av_preprocessor = 0; | |
cf655043 AW |
837 | $av_pending = '_'; |
838 | @av_paren_type = ('E'); | |
1f65f947 | 839 | $av_pend_colon = 'O'; |
c2fdda0d AW |
840 | } |
841 | ||
6c72ffaa AW |
842 | sub annotate_values { |
843 | my ($stream, $type) = @_; | |
0a920b5b | 844 | |
6c72ffaa | 845 | my $res; |
1f65f947 | 846 | my $var = '_' x length($stream); |
6c72ffaa AW |
847 | my $cur = $stream; |
848 | ||
c2fdda0d | 849 | print "$stream\n" if ($dbg_values > 1); |
6c72ffaa | 850 | |
6c72ffaa | 851 | while (length($cur)) { |
773647a0 | 852 | @av_paren_type = ('E') if ($#av_paren_type < 0); |
cf655043 | 853 | print " <" . join('', @av_paren_type) . |
171ae1a4 | 854 | "> <$type> <$av_pending>" if ($dbg_values > 1); |
6c72ffaa | 855 | if ($cur =~ /^(\s+)/o) { |
c2fdda0d AW |
856 | print "WS($1)\n" if ($dbg_values > 1); |
857 | if ($1 =~ /\n/ && $av_preprocessor) { | |
cf655043 | 858 | $type = pop(@av_paren_type); |
c2fdda0d | 859 | $av_preprocessor = 0; |
6c72ffaa AW |
860 | } |
861 | ||
c023e473 | 862 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
9446ef56 AW |
863 | print "CAST($1)\n" if ($dbg_values > 1); |
864 | push(@av_paren_type, $type); | |
865 | $type = 'C'; | |
866 | ||
e91b6e26 | 867 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
c2fdda0d | 868 | print "DECLARE($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
869 | $type = 'T'; |
870 | ||
389a2fe5 AW |
871 | } elsif ($cur =~ /^($Modifier)\s*/) { |
872 | print "MODIFIER($1)\n" if ($dbg_values > 1); | |
873 | $type = 'T'; | |
874 | ||
c45dcabd | 875 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { |
171ae1a4 | 876 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); |
c2fdda0d | 877 | $av_preprocessor = 1; |
171ae1a4 AW |
878 | push(@av_paren_type, $type); |
879 | if ($2 ne '') { | |
880 | $av_pending = 'N'; | |
881 | } | |
882 | $type = 'E'; | |
883 | ||
c45dcabd | 884 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { |
171ae1a4 AW |
885 | print "UNDEF($1)\n" if ($dbg_values > 1); |
886 | $av_preprocessor = 1; | |
887 | push(@av_paren_type, $type); | |
6c72ffaa | 888 | |
c45dcabd | 889 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { |
cf655043 | 890 | print "PRE_START($1)\n" if ($dbg_values > 1); |
c2fdda0d | 891 | $av_preprocessor = 1; |
cf655043 AW |
892 | |
893 | push(@av_paren_type, $type); | |
894 | push(@av_paren_type, $type); | |
171ae1a4 | 895 | $type = 'E'; |
cf655043 | 896 | |
c45dcabd | 897 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { |
cf655043 AW |
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 | ||
171ae1a4 | 903 | $type = 'E'; |
cf655043 | 904 | |
c45dcabd | 905 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { |
cf655043 AW |
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); | |
171ae1a4 | 914 | $type = 'E'; |
6c72ffaa AW |
915 | |
916 | } elsif ($cur =~ /^(\\\n)/o) { | |
c2fdda0d | 917 | print "PRECONT($1)\n" if ($dbg_values > 1); |
6c72ffaa | 918 | |
171ae1a4 AW |
919 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { |
920 | print "ATTR($1)\n" if ($dbg_values > 1); | |
921 | $av_pending = $type; | |
922 | $type = 'N'; | |
923 | ||
6c72ffaa | 924 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
c2fdda0d | 925 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
6c72ffaa | 926 | if (defined $2) { |
cf655043 | 927 | $av_pending = 'V'; |
6c72ffaa AW |
928 | } |
929 | $type = 'N'; | |
930 | ||
14b111c1 | 931 | } elsif ($cur =~ /^(if|while|for)\b/o) { |
c2fdda0d | 932 | print "COND($1)\n" if ($dbg_values > 1); |
14b111c1 | 933 | $av_pending = 'E'; |
6c72ffaa AW |
934 | $type = 'N'; |
935 | ||
1f65f947 AW |
936 | } elsif ($cur =~/^(case)/o) { |
937 | print "CASE($1)\n" if ($dbg_values > 1); | |
938 | $av_pend_colon = 'C'; | |
939 | $type = 'N'; | |
940 | ||
14b111c1 | 941 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { |
c2fdda0d | 942 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
943 | $type = 'N'; |
944 | ||
945 | } elsif ($cur =~ /^(\()/o) { | |
c2fdda0d | 946 | print "PAREN('$1')\n" if ($dbg_values > 1); |
cf655043 AW |
947 | push(@av_paren_type, $av_pending); |
948 | $av_pending = '_'; | |
6c72ffaa AW |
949 | $type = 'N'; |
950 | ||
951 | } elsif ($cur =~ /^(\))/o) { | |
cf655043 AW |
952 | my $new_type = pop(@av_paren_type); |
953 | if ($new_type ne '_') { | |
954 | $type = $new_type; | |
c2fdda0d AW |
955 | print "PAREN('$1') -> $type\n" |
956 | if ($dbg_values > 1); | |
6c72ffaa | 957 | } else { |
c2fdda0d | 958 | print "PAREN('$1')\n" if ($dbg_values > 1); |
6c72ffaa AW |
959 | } |
960 | ||
c8cb2ca3 | 961 | } elsif ($cur =~ /^($Ident)\s*\(/o) { |
c2fdda0d | 962 | print "FUNC($1)\n" if ($dbg_values > 1); |
c8cb2ca3 | 963 | $type = 'V'; |
cf655043 | 964 | $av_pending = 'V'; |
6c72ffaa | 965 | |
8e761b04 AW |
966 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { |
967 | if (defined $2 && $type eq 'C' || $type eq 'T') { | |
1f65f947 | 968 | $av_pend_colon = 'B'; |
8e761b04 AW |
969 | } elsif ($type eq 'E') { |
970 | $av_pend_colon = 'L'; | |
1f65f947 AW |
971 | } |
972 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); | |
973 | $type = 'V'; | |
974 | ||
6c72ffaa | 975 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
c2fdda0d | 976 | print "IDENT($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
977 | $type = 'V'; |
978 | ||
979 | } elsif ($cur =~ /^($Assignment)/o) { | |
c2fdda0d | 980 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
981 | $type = 'N'; |
982 | ||
cf655043 | 983 | } elsif ($cur =~/^(;|{|})/) { |
c2fdda0d | 984 | print "END($1)\n" if ($dbg_values > 1); |
13214adf | 985 | $type = 'E'; |
1f65f947 AW |
986 | $av_pend_colon = 'O'; |
987 | ||
8e761b04 AW |
988 | } elsif ($cur =~/^(,)/) { |
989 | print "COMMA($1)\n" if ($dbg_values > 1); | |
990 | $type = 'C'; | |
991 | ||
1f65f947 AW |
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'; | |
13214adf | 1006 | |
8e761b04 | 1007 | } elsif ($cur =~ /^(\[)/o) { |
13214adf | 1008 | print "CLOSE($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
1009 | $type = 'N'; |
1010 | ||
0d413866 | 1011 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { |
74048ed8 AW |
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 | ||
6c72ffaa | 1024 | } elsif ($cur =~ /^($Operators)/o) { |
c2fdda0d | 1025 | print "OP($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
1026 | if ($1 ne '++' && $1 ne '--') { |
1027 | $type = 'N'; | |
1028 | } | |
1029 | ||
1030 | } elsif ($cur =~ /(^.)/o) { | |
c2fdda0d | 1031 | print "C($1)\n" if ($dbg_values > 1); |
6c72ffaa AW |
1032 | } |
1033 | if (defined $1) { | |
1034 | $cur = substr($cur, length($1)); | |
1035 | $res .= $type x length($1); | |
1036 | } | |
9c0ca6f9 | 1037 | } |
0a920b5b | 1038 | |
1f65f947 | 1039 | return ($res, $var); |
0a920b5b AW |
1040 | } |
1041 | ||
8905a67c | 1042 | sub possible { |
13214adf | 1043 | my ($possible, $line) = @_; |
9a974fdb | 1044 | my $notPermitted = qr{(?: |
0776e594 AW |
1045 | ^(?: |
1046 | $Modifier| | |
1047 | $Storage| | |
1048 | $Type| | |
9a974fdb AW |
1049 | DEFINE_\S+ |
1050 | )$| | |
1051 | ^(?: | |
0776e594 AW |
1052 | goto| |
1053 | return| | |
1054 | case| | |
1055 | else| | |
1056 | asm|__asm__| | |
1057 | do | |
9a974fdb | 1058 | )(?:\s|$)| |
0776e594 | 1059 | ^(?:typedef|struct|enum)\b |
9a974fdb AW |
1060 | )}x; |
1061 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); | |
1062 | if ($possible !~ $notPermitted) { | |
c45dcabd AW |
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; | |
d2506586 | 1070 | for my $modifier (split(' ', $possible)) { |
9a974fdb AW |
1071 | if ($modifier !~ $notPermitted) { |
1072 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); | |
1073 | push(@modifierList, $modifier); | |
1074 | } | |
d2506586 | 1075 | } |
c45dcabd AW |
1076 | |
1077 | } else { | |
1078 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); | |
1079 | push(@typeList, $possible); | |
1080 | } | |
8905a67c | 1081 | build_types(); |
0776e594 AW |
1082 | } else { |
1083 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); | |
8905a67c AW |
1084 | } |
1085 | } | |
1086 | ||
6c72ffaa AW |
1087 | my $prefix = ''; |
1088 | ||
f0a594c1 | 1089 | sub report { |
773647a0 AW |
1090 | if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { |
1091 | return 0; | |
1092 | } | |
8905a67c AW |
1093 | my $line = $prefix . $_[0]; |
1094 | ||
1095 | $line = (split('\n', $line))[0] . "\n" if ($terse); | |
1096 | ||
13214adf | 1097 | push(our @report, $line); |
773647a0 AW |
1098 | |
1099 | return 1; | |
f0a594c1 AW |
1100 | } |
1101 | sub report_dump { | |
13214adf | 1102 | our @report; |
f0a594c1 | 1103 | } |
de7d4f0e | 1104 | sub ERROR { |
773647a0 AW |
1105 | if (report("ERROR: $_[0]\n")) { |
1106 | our $clean = 0; | |
1107 | our $cnt_error++; | |
1108 | } | |
de7d4f0e AW |
1109 | } |
1110 | sub WARN { | |
773647a0 AW |
1111 | if (report("WARNING: $_[0]\n")) { |
1112 | our $clean = 0; | |
1113 | our $cnt_warn++; | |
1114 | } | |
de7d4f0e AW |
1115 | } |
1116 | sub CHK { | |
773647a0 | 1117 | if ($check && report("CHECK: $_[0]\n")) { |
6c72ffaa AW |
1118 | our $clean = 0; |
1119 | our $cnt_chk++; | |
1120 | } | |
de7d4f0e AW |
1121 | } |
1122 | ||
6ecd9674 AW |
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 | ||
0a920b5b AW |
1150 | sub process { |
1151 | my $filename = shift; | |
0a920b5b AW |
1152 | |
1153 | my $linenr=0; | |
1154 | my $prevline=""; | |
c2fdda0d | 1155 | my $prevrawline=""; |
0a920b5b | 1156 | my $stashline=""; |
c2fdda0d | 1157 | my $stashrawline=""; |
0a920b5b | 1158 | |
4a0df2ef | 1159 | my $length; |
0a920b5b AW |
1160 | my $indent; |
1161 | my $previndent=0; | |
1162 | my $stashindent=0; | |
1163 | ||
de7d4f0e | 1164 | our $clean = 1; |
0a920b5b AW |
1165 | my $signoff = 0; |
1166 | my $is_patch = 0; | |
1167 | ||
13214adf | 1168 | our @report = (); |
6c72ffaa AW |
1169 | our $cnt_lines = 0; |
1170 | our $cnt_error = 0; | |
1171 | our $cnt_warn = 0; | |
1172 | our $cnt_chk = 0; | |
1173 | ||
0a920b5b AW |
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; | |
c2fdda0d | 1180 | my $comment_edge = 0; |
0a920b5b | 1181 | my $first_line = 0; |
1e855726 | 1182 | my $p1_prefix = ''; |
0a920b5b | 1183 | |
13214adf AW |
1184 | my $prev_values = 'E'; |
1185 | ||
1186 | # suppression flags | |
773647a0 | 1187 | my %suppress_ifbraces; |
170d3a22 | 1188 | my %suppress_whiletrailers; |
2b474a1a | 1189 | my %suppress_export; |
653d4876 | 1190 | |
c2fdda0d | 1191 | # Pre-scan the patch sanitizing the lines. |
de7d4f0e | 1192 | # Pre-scan the patch looking for any __setup documentation. |
c2fdda0d | 1193 | # |
de7d4f0e AW |
1194 | my @setup_docs = (); |
1195 | my $setup_docs = 0; | |
773647a0 AW |
1196 | |
1197 | sanitise_line_reset(); | |
c2fdda0d AW |
1198 | my $line; |
1199 | foreach my $rawline (@rawlines) { | |
773647a0 AW |
1200 | $linenr++; |
1201 | $line = $rawline; | |
c2fdda0d | 1202 | |
773647a0 | 1203 | if ($rawline=~/^\+\+\+\s+(\S+)/) { |
de7d4f0e AW |
1204 | $setup_docs = 0; |
1205 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { | |
1206 | $setup_docs = 1; | |
1207 | } | |
773647a0 AW |
1208 | #next; |
1209 | } | |
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 | } | |
c45dcabd | 1217 | $in_comment = 0; |
773647a0 AW |
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; | |
01fa9147 AW |
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"; | |
721c1cb6 | 1230 | last if (!defined $rawlines[$ln - 1]); |
fae17dae AW |
1231 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && |
1232 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { | |
1233 | ($edge) = $1; | |
1234 | last; | |
1235 | } | |
773647a0 AW |
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 && | |
83242e0c | 1245 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) |
773647a0 AW |
1246 | { |
1247 | $in_comment = 1; | |
1248 | } | |
1249 | ||
1250 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; | |
1251 | sanitise_line_reset($in_comment); | |
1252 | ||
171ae1a4 | 1253 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { |
773647a0 | 1254 | # Standardise the strings and chars within the input to |
171ae1a4 | 1255 | # simplify matching -- only bother with positive lines. |
773647a0 | 1256 | $line = sanitise_line($rawline); |
de7d4f0e | 1257 | } |
773647a0 AW |
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"; | |
de7d4f0e AW |
1268 | |
1269 | if ($setup_docs && $line =~ /^\+/) { | |
1270 | push(@setup_docs, $line); | |
1271 | } | |
1272 | } | |
1273 | ||
6c72ffaa AW |
1274 | $prefix = ''; |
1275 | ||
773647a0 AW |
1276 | $realcnt = 0; |
1277 | $linenr = 0; | |
0a920b5b AW |
1278 | foreach my $line (@lines) { |
1279 | $linenr++; | |
1280 | ||
c2fdda0d | 1281 | my $rawline = $rawlines[$linenr - 1]; |
6c72ffaa | 1282 | |
0a920b5b | 1283 | #extract the line range in the file after the patch is applied |
6c72ffaa | 1284 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
0a920b5b | 1285 | $is_patch = 1; |
4a0df2ef | 1286 | $first_line = $linenr + 1; |
0a920b5b AW |
1287 | $realline=$1-1; |
1288 | if (defined $2) { | |
1289 | $realcnt=$3+1; | |
1290 | } else { | |
1291 | $realcnt=1+1; | |
1292 | } | |
c2fdda0d | 1293 | annotate_reset(); |
13214adf AW |
1294 | $prev_values = 'E'; |
1295 | ||
773647a0 | 1296 | %suppress_ifbraces = (); |
170d3a22 | 1297 | %suppress_whiletrailers = (); |
2b474a1a | 1298 | %suppress_export = (); |
0a920b5b | 1299 | next; |
0a920b5b | 1300 | |
4a0df2ef AW |
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. | |
773647a0 | 1304 | } elsif ($line =~ /^( |\+|$)/) { |
0a920b5b | 1305 | $realline++; |
d8aaf121 | 1306 | $realcnt-- if ($realcnt != 0); |
0a920b5b | 1307 | |
4a0df2ef | 1308 | # Measure the line length and indent. |
c2fdda0d | 1309 | ($length, $indent) = line_stats($rawline); |
0a920b5b AW |
1310 | |
1311 | # Track the previous line. | |
1312 | ($prevline, $stashline) = ($stashline, $line); | |
1313 | ($previndent, $stashindent) = ($stashindent, $indent); | |
c2fdda0d AW |
1314 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); |
1315 | ||
773647a0 | 1316 | #warn "line<$line>\n"; |
6c72ffaa | 1317 | |
d8aaf121 AW |
1318 | } elsif ($realcnt == 1) { |
1319 | $realcnt--; | |
0a920b5b AW |
1320 | } |
1321 | ||
cc77cdca AW |
1322 | my $hunk_line = ($realcnt != 0); |
1323 | ||
0a920b5b | 1324 | #make up the handle for any error we report on this line |
773647a0 AW |
1325 | $prefix = "$filename:$realline: " if ($emacs && $file); |
1326 | $prefix = "$filename:$linenr: " if ($emacs && !$file); | |
1327 | ||
6c72ffaa AW |
1328 | $here = "#$linenr: " if (!$file); |
1329 | $here = "#$realline: " if ($file); | |
773647a0 AW |
1330 | |
1331 | # extract the filename as it passes | |
3bf9a009 RV |
1332 | if ($line =~ /^diff --git.*?(\S+)$/) { |
1333 | $realfile = $1; | |
1334 | $realfile =~ s@^([^/]*)/@@; | |
1335 | ||
1336 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { | |
773647a0 | 1337 | $realfile = $1; |
1e855726 WS |
1338 | $realfile =~ s@^([^/]*)/@@; |
1339 | ||
1340 | $p1_prefix = $1; | |
e2f7aa4b AW |
1341 | if (!$file && $tree && $p1_prefix ne '' && |
1342 | -e "$root/$p1_prefix") { | |
1e855726 WS |
1343 | WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); |
1344 | } | |
773647a0 | 1345 | |
c1ab3326 | 1346 | if ($realfile =~ m@^include/asm/@) { |
773647a0 AW |
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 | ||
389834b6 | 1352 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
0a920b5b | 1353 | |
c2fdda0d AW |
1354 | my $hereline = "$here\n$rawline\n"; |
1355 | my $herecurr = "$here\n$rawline\n"; | |
1356 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; | |
0a920b5b | 1357 | |
6c72ffaa AW |
1358 | $cnt_lines++ if ($realcnt != 0); |
1359 | ||
3bf9a009 RV |
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 | ||
0a920b5b | 1368 | #check the patch for a signoff: |
d8aaf121 | 1369 | if ($line =~ /^\s*signed-off-by:/i) { |
4a0df2ef AW |
1370 | # This is a signoff, if ugly, so do not double report. |
1371 | $signoff++; | |
0a920b5b | 1372 | if (!($line =~ /^\s*Signed-off-by:/)) { |
de7d4f0e AW |
1373 | WARN("Signed-off-by: is the preferred form\n" . |
1374 | $herecurr); | |
0a920b5b AW |
1375 | } |
1376 | if ($line =~ /^\s*signed-off-by:\S/i) { | |
773647a0 | 1377 | WARN("space required after Signed-off-by:\n" . |
de7d4f0e | 1378 | $herecurr); |
0a920b5b AW |
1379 | } |
1380 | } | |
1381 | ||
00df344f | 1382 | # Check for wrappage within a valid hunk of the file |
8905a67c | 1383 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { |
de7d4f0e | 1384 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
6c72ffaa | 1385 | $herecurr) if (!$emitted_corrupt++); |
de7d4f0e AW |
1386 | } |
1387 | ||
6ecd9674 AW |
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 | ||
de7d4f0e AW |
1402 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
1403 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | |
171ae1a4 AW |
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); | |
00df344f AW |
1412 | } |
1413 | ||
30670854 AW |
1414 | # ignore non-hunk lines and lines being removed |
1415 | next if (!$hunk_line || $line =~ /^-/); | |
0a920b5b | 1416 | |
0a920b5b | 1417 | #trailing whitespace |
9c0ca6f9 | 1418 | if ($line =~ /^\+.*\015/) { |
c2fdda0d | 1419 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
9c0ca6f9 AW |
1420 | ERROR("DOS line endings\n" . $herevet); |
1421 | ||
c2fdda0d AW |
1422 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
1423 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
de7d4f0e | 1424 | ERROR("trailing whitespace\n" . $herevet); |
d2c0a235 | 1425 | $rpt_cleaners = 1; |
0a920b5b | 1426 | } |
5368df20 | 1427 | |
3354957a | 1428 | # check for Kconfig help text having a real description |
9fe287d7 AW |
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. | |
3354957a | 1431 | if ($realfile =~ /Kconfig/ && |
9fe287d7 | 1432 | $line =~ /\+\s*(?:---)?help(?:---)?$/) { |
3354957a | 1433 | my $length = 0; |
9fe287d7 AW |
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/^.//; | |
3354957a AK |
1446 | $f =~ s/#.*//; |
1447 | $f =~ s/^\s+//; | |
1448 | next if ($f =~ /^$/); | |
9fe287d7 AW |
1449 | if ($f =~ /^\s*config\s/) { |
1450 | $is_end = 1; | |
1451 | last; | |
1452 | } | |
3354957a AK |
1453 | $length++; |
1454 | } | |
9fe287d7 AW |
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"; | |
3354957a AK |
1457 | } |
1458 | ||
5368df20 AW |
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 | ||
0a920b5b | 1462 | #80 column limit |
c45dcabd | 1463 | if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && |
f4c014c0 | 1464 | $rawline !~ /^.\s*\*\s*\@$Ident\s/ && |
8bbea968 JP |
1465 | !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ || |
1466 | $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && | |
f4c014c0 | 1467 | $length > 80) |
c45dcabd | 1468 | { |
de7d4f0e | 1469 | WARN("line over 80 characters\n" . $herecurr); |
0a920b5b AW |
1470 | } |
1471 | ||
5e79d96e JP |
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 | ||
8905a67c AW |
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 | ||
42e41c54 MF |
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 | ||
b9ea10d6 AW |
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)$/); | |
0a920b5b AW |
1496 | |
1497 | # at the beginning of a line any tabs must come first and anything | |
1498 | # more than 8 must use tabs. | |
c2fdda0d AW |
1499 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
1500 | $rawline =~ /^\+\s* \s*/) { | |
1501 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
171ae1a4 | 1502 | ERROR("code indent should use tabs where possible\n" . $herevet); |
d2c0a235 | 1503 | $rpt_cleaners = 1; |
0a920b5b AW |
1504 | } |
1505 | ||
08e44365 AP |
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 | ||
5f7ddae6 | 1512 | # check for spaces at the beginning of a line. |
6b4c5beb AW |
1513 | # Exceptions: |
1514 | # 1) within comments | |
1515 | # 2) indented preprocessor commands | |
1516 | # 3) hanging labels | |
1517 | if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { | |
5f7ddae6 | 1518 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
6b4c5beb | 1519 | WARN("please, no spaces at the start of a line\n" . $herevet); |
5f7ddae6 RR |
1520 | } |
1521 | ||
b9ea10d6 AW |
1522 | # check we are in a valid C source file if not then ignore this hunk |
1523 | next if ($realfile !~ /\.(h|c)$/); | |
1524 | ||
c2fdda0d | 1525 | # check for RCS/CVS revision markers |
cf655043 | 1526 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { |
c2fdda0d AW |
1527 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
1528 | } | |
22f2a2ef | 1529 | |
42e41c54 MF |
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 | ||
9c0ca6f9 | 1540 | # Check for potential 'bare' types |
2b474a1a AW |
1541 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
1542 | $realline_next); | |
9c9ba34e | 1543 | if ($realcnt && $line =~ /.\s*\S/) { |
170d3a22 | 1544 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
f5fe35dd | 1545 | ctx_statement_block($linenr, $realcnt, 0); |
171ae1a4 AW |
1546 | $stat =~ s/\n./\n /g; |
1547 | $cond =~ s/\n./\n /g; | |
1548 | ||
2b474a1a AW |
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 | ||
171ae1a4 AW |
1557 | my $s = $stat; |
1558 | $s =~ s/{.*$//s; | |
cf655043 | 1559 | |
c2fdda0d | 1560 | # Ignore goto labels. |
171ae1a4 | 1561 | if ($s =~ /$Ident:\*$/s) { |
c2fdda0d AW |
1562 | |
1563 | # Ignore functions being called | |
171ae1a4 | 1564 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { |
c2fdda0d | 1565 | |
463f2864 AW |
1566 | } elsif ($s =~ /^.\s*else\b/s) { |
1567 | ||
c45dcabd | 1568 | # declarations always start with types |
d2506586 | 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) { |
c45dcabd AW |
1570 | my $type = $1; |
1571 | $type =~ s/\s+/ /g; | |
1572 | possible($type, "A:" . $s); | |
1573 | ||
8905a67c | 1574 | # definitions in global scope can only start with types |
a6a84062 | 1575 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { |
c45dcabd | 1576 | possible($1, "B:" . $s); |
c2fdda0d | 1577 | } |
8905a67c AW |
1578 | |
1579 | # any (foo ... *) is a pointer cast, and foo is a type | |
65863862 | 1580 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { |
c45dcabd | 1581 | possible($1, "C:" . $s); |
8905a67c AW |
1582 | } |
1583 | ||
1584 | # Check for any sort of function declaration. | |
1585 | # int foo(something bar, other baz); | |
1586 | # void (*store_gdt)(x86_descr_ptr *); | |
171ae1a4 | 1587 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { |
8905a67c | 1588 | my ($name_len) = length($1); |
8905a67c | 1589 | |
cf655043 | 1590 | my $ctx = $s; |
773647a0 | 1591 | substr($ctx, 0, $name_len + 1, ''); |
8905a67c | 1592 | $ctx =~ s/\)[^\)]*$//; |
cf655043 | 1593 | |
8905a67c | 1594 | for my $arg (split(/\s*,\s*/, $ctx)) { |
c45dcabd | 1595 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { |
8905a67c | 1596 | |
c45dcabd | 1597 | possible($1, "D:" . $s); |
8905a67c AW |
1598 | } |
1599 | } | |
9c0ca6f9 | 1600 | } |
8905a67c | 1601 | |
9c0ca6f9 AW |
1602 | } |
1603 | ||
653d4876 AW |
1604 | # |
1605 | # Checks which may be anchored in the context. | |
1606 | # | |
00df344f | 1607 | |
653d4876 AW |
1608 | # Check for switch () and associated case and default |
1609 | # statements should be at the same indent. | |
00df344f AW |
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 '') { | |
9c0ca6f9 | 1626 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
de7d4f0e AW |
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 | |
c45dcabd | 1632 | if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { |
773647a0 AW |
1633 | my $pre_ctx = "$1$2"; |
1634 | ||
9c0ca6f9 | 1635 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
de7d4f0e AW |
1636 | my $ctx_cnt = $realcnt - $#ctx - 1; |
1637 | my $ctx = join("\n", @ctx); | |
1638 | ||
548596d5 AW |
1639 | my $ctx_ln = $linenr; |
1640 | my $ctx_skip = $realcnt; | |
773647a0 | 1641 | |
548596d5 AW |
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] !~ /^-/); | |
de7d4f0e | 1647 | $ctx_ln++; |
de7d4f0e | 1648 | } |
548596d5 | 1649 | |
53210168 AW |
1650 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; |
1651 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; | |
de7d4f0e | 1652 | |
773647a0 AW |
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" . | |
01464f30 | 1655 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
00df344f | 1656 | } |
773647a0 AW |
1657 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
1658 | $ctx =~ /\)\s*\;\s*$/ && | |
1659 | defined $lines[$ctx_ln - 1]) | |
1660 | { | |
9c0ca6f9 AW |
1661 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
1662 | if ($nindent > $indent) { | |
773647a0 | 1663 | WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . |
01464f30 | 1664 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
9c0ca6f9 AW |
1665 | } |
1666 | } | |
00df344f AW |
1667 | } |
1668 | ||
4d001e4d AW |
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. | |
6f779c18 AW |
1681 | my @newlines = ($c =~ /\n/gs); |
1682 | my $cond_lines = 1 + $#newlines; | |
4d001e4d AW |
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 | } | |
9bd49efe | 1696 | if ($s =~ s/^\s*?\n//) { |
4d001e4d AW |
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 | ||
9bd49efe | 1708 | my $cond_ptr = -1; |
740504c6 | 1709 | $continuation = 0; |
9bd49efe AW |
1710 | while ($cond_ptr != $cond_lines) { |
1711 | $cond_ptr = $cond_lines; | |
1712 | ||
f16fa28f AW |
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 | ||
9bd49efe AW |
1719 | # Ignore: |
1720 | # 1) blank lines, they should be at 0, | |
1721 | # 2) preprocessor lines, and | |
1722 | # 3) labels. | |
740504c6 AW |
1723 | if ($continuation || |
1724 | $s =~ /^\s*?\n/ || | |
9bd49efe AW |
1725 | $s =~ /^\s*#\s*?/ || |
1726 | $s =~ /^\s*$Ident\s*:/) { | |
740504c6 | 1727 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; |
30dad6eb AW |
1728 | if ($s =~ s/^.*?\n//) { |
1729 | $cond_lines++; | |
1730 | } | |
9bd49efe | 1731 | } |
4d001e4d AW |
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 | ||
9bd49efe | 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"; |
4d001e4d AW |
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 | ||
6c72ffaa AW |
1755 | # Track the 'values' across context and added lines. |
1756 | my $opline = $line; $opline =~ s/^./ /; | |
1f65f947 AW |
1757 | my ($curr_values, $curr_vars) = |
1758 | annotate_values($opline . "\n", $prev_values); | |
6c72ffaa | 1759 | $curr_values = $prev_values . $curr_values; |
c2fdda0d AW |
1760 | if ($dbg_values) { |
1761 | my $outline = $opline; $outline =~ s/\t/ /g; | |
cf655043 AW |
1762 | print "$linenr > .$outline\n"; |
1763 | print "$linenr > $curr_values\n"; | |
1f65f947 | 1764 | print "$linenr > $curr_vars\n"; |
c2fdda0d | 1765 | } |
6c72ffaa AW |
1766 | $prev_values = substr($curr_values, -1); |
1767 | ||
00df344f AW |
1768 | #ignore lines not being added |
1769 | if ($line=~/^[^\+]/) {next;} | |
1770 | ||
653d4876 | 1771 | # TEST: allow direct testing of the type matcher. |
7429c690 AW |
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 | } | |
653d4876 AW |
1778 | next; |
1779 | } | |
a1ef277e AW |
1780 | # TEST: allow direct testing of the attribute matcher. |
1781 | if ($dbg_attr) { | |
9360b0e5 | 1782 | if ($line =~ /^.\s*$Modifier\s*$/) { |
a1ef277e | 1783 | ERROR("TEST: is attr\n" . $herecurr); |
9360b0e5 | 1784 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { |
a1ef277e AW |
1785 | ERROR("TEST: is not attr ($1 is)\n". $herecurr); |
1786 | } | |
1787 | next; | |
1788 | } | |
653d4876 | 1789 | |
f0a594c1 | 1790 | # check for initialisation to aggregates open brace on the next line |
99423c20 AW |
1791 | if ($line =~ /^.\s*{/ && |
1792 | $prevline =~ /(?:^|[^=])=\s*$/) { | |
773647a0 | 1793 | ERROR("that open brace { should be on the previous line\n" . $hereprev); |
f0a594c1 AW |
1794 | } |
1795 | ||
653d4876 AW |
1796 | # |
1797 | # Checks which are anchored on the added line. | |
1798 | # | |
1799 | ||
1800 | # check for malformed paths in #include statements (uses RAW line) | |
c45dcabd | 1801 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { |
653d4876 AW |
1802 | my $path = $1; |
1803 | if ($path =~ m{//}) { | |
de7d4f0e AW |
1804 | ERROR("malformed #include filename\n" . |
1805 | $herecurr); | |
653d4876 | 1806 | } |
653d4876 | 1807 | } |
00df344f | 1808 | |
0a920b5b | 1809 | # no C99 // comments |
00df344f | 1810 | if ($line =~ m{//}) { |
de7d4f0e | 1811 | ERROR("do not use C99 // comments\n" . $herecurr); |
0a920b5b | 1812 | } |
00df344f | 1813 | # Remove C99 comments. |
0a920b5b | 1814 | $line =~ s@//.*@@; |
6c72ffaa | 1815 | $opline =~ s@//.*@@; |
0a920b5b | 1816 | |
2b474a1a AW |
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.*\((.*)\)/)) { | |
3cbf62df AW |
1825 | # Handle definitions which produce identifiers with |
1826 | # a prefix: | |
1827 | # XXX(foo); | |
1828 | # EXPORT_SYMBOL(something_foo); | |
653d4876 | 1829 | my $name = $1; |
3cbf62df AW |
1830 | if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && |
1831 | $name =~ /^${Ident}_$2/) { | |
1832 | #print "FOO C name<$name>\n"; | |
1833 | $suppress_export{$realline_next} = 1; | |
1834 | ||
1835 | } elsif ($stat !~ /(?: | |
2b474a1a | 1836 | \n.}\s*$| |
48012058 AW |
1837 | ^.DEFINE_$Ident\(\Q$name\E\)| |
1838 | ^.DECLARE_$Ident\(\Q$name\E\)| | |
1839 | ^.LIST_HEAD\(\Q$name\E\)| | |
2b474a1a AW |
1840 | ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| |
1841 | \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() | |
48012058 | 1842 | )/x) { |
2b474a1a AW |
1843 | #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; |
1844 | $suppress_export{$realline_next} = 2; | |
1845 | } else { | |
1846 | $suppress_export{$realline_next} = 1; | |
0a920b5b AW |
1847 | } |
1848 | } | |
2b474a1a AW |
1849 | if (!defined $suppress_export{$linenr} && |
1850 | $prevline =~ /^.\s*$/ && | |
1851 | ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || | |
1852 | $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { | |
1853 | #print "FOO B <$lines[$linenr - 1]>\n"; | |
1854 | $suppress_export{$linenr} = 2; | |
1855 | } | |
1856 | if (defined $suppress_export{$linenr} && | |
1857 | $suppress_export{$linenr} == 2) { | |
1858 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); | |
1859 | } | |
0a920b5b | 1860 | |
5150bda4 | 1861 | # check for global initialisers. |
c45dcabd | 1862 | if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { |
5150bda4 | 1863 | ERROR("do not initialise globals to 0 or NULL\n" . |
f0a594c1 AW |
1864 | $herecurr); |
1865 | } | |
653d4876 | 1866 | # check for static initialisers. |
2d1bafd7 | 1867 | if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { |
de7d4f0e AW |
1868 | ERROR("do not initialise statics to 0 or NULL\n" . |
1869 | $herecurr); | |
0a920b5b AW |
1870 | } |
1871 | ||
cb710eca JP |
1872 | # check for static const char * arrays. |
1873 | if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { | |
1874 | WARN("static const char * array should probably be static const char * const\n" . | |
1875 | $herecurr); | |
1876 | } | |
1877 | ||
1878 | # check for static char foo[] = "bar" declarations. | |
1879 | if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { | |
1880 | WARN("static char array declaration should probably be static const char\n" . | |
1881 | $herecurr); | |
1882 | } | |
1883 | ||
93ed0e2d JP |
1884 | # check for declarations of struct pci_device_id |
1885 | if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { | |
1886 | WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); | |
1887 | } | |
1888 | ||
653d4876 AW |
1889 | # check for new typedefs, only function parameters and sparse annotations |
1890 | # make sense. | |
1891 | if ($line =~ /\btypedef\s/ && | |
8054576d | 1892 | $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && |
c45dcabd | 1893 | $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && |
8ed22cad | 1894 | $line !~ /\b$typeTypedefs\b/ && |
653d4876 | 1895 | $line !~ /\b__bitwise(?:__|)\b/) { |
de7d4f0e | 1896 | WARN("do not add new typedefs\n" . $herecurr); |
0a920b5b AW |
1897 | } |
1898 | ||
1899 | # * goes on variable not on type | |
65863862 | 1900 | # (char*[ const]) |
00ef4ece | 1901 | if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { |
65863862 AW |
1902 | my ($from, $to) = ($1, $1); |
1903 | ||
1904 | # Should start with a space. | |
1905 | $to =~ s/^(\S)/ $1/; | |
1906 | # Should not end with a space. | |
1907 | $to =~ s/\s+$//; | |
1908 | # '*'s should not have spaces between. | |
f9a0b3d1 | 1909 | while ($to =~ s/\*\s+\*/\*\*/) { |
65863862 | 1910 | } |
d8aaf121 | 1911 | |
65863862 AW |
1912 | #print "from<$from> to<$to>\n"; |
1913 | if ($from ne $to) { | |
1914 | ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); | |
1915 | } | |
00ef4ece | 1916 | } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { |
65863862 AW |
1917 | my ($from, $to, $ident) = ($1, $1, $2); |
1918 | ||
1919 | # Should start with a space. | |
1920 | $to =~ s/^(\S)/ $1/; | |
1921 | # Should not end with a space. | |
1922 | $to =~ s/\s+$//; | |
1923 | # '*'s should not have spaces between. | |
f9a0b3d1 | 1924 | while ($to =~ s/\*\s+\*/\*\*/) { |
65863862 AW |
1925 | } |
1926 | # Modifiers should have spaces. | |
1927 | $to =~ s/(\b$Modifier$)/$1 /; | |
d8aaf121 | 1928 | |
667026e7 AW |
1929 | #print "from<$from> to<$to> ident<$ident>\n"; |
1930 | if ($from ne $to && $ident !~ /^$Modifier$/) { | |
65863862 AW |
1931 | ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); |
1932 | } | |
0a920b5b AW |
1933 | } |
1934 | ||
1935 | # # no BUG() or BUG_ON() | |
1936 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { | |
1937 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; | |
1938 | # print "$herecurr"; | |
1939 | # $clean = 0; | |
1940 | # } | |
1941 | ||
8905a67c | 1942 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
c2fdda0d | 1943 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
8905a67c AW |
1944 | } |
1945 | ||
00df344f AW |
1946 | # printk should use KERN_* levels. Note that follow on printk's on the |
1947 | # same line do not need a level, so we use the current block context | |
1948 | # to try and find and validate the current printk. In summary the current | |
1949 | # printk includes all preceeding printk's which have no newline on the end. | |
1950 | # we assume the first bad printk is the one to report. | |
f0a594c1 | 1951 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
00df344f AW |
1952 | my $ok = 0; |
1953 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { | |
1954 | #print "CHECK<$lines[$ln - 1]\n"; | |
1955 | # we have a preceeding printk if it ends | |
1956 | # with "\n" ignore it, else it is to blame | |
1957 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { | |
1958 | if ($rawlines[$ln - 1] !~ m{\\n"}) { | |
1959 | $ok = 1; | |
1960 | } | |
1961 | last; | |
1962 | } | |
1963 | } | |
1964 | if ($ok == 0) { | |
de7d4f0e | 1965 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
00df344f | 1966 | } |
0a920b5b AW |
1967 | } |
1968 | ||
653d4876 AW |
1969 | # function brace can't be on same line, except for #defines of do while, |
1970 | # or if closed on same line | |
c45dcabd AW |
1971 | if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and |
1972 | !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { | |
de7d4f0e | 1973 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
0a920b5b | 1974 | } |
653d4876 | 1975 | |
8905a67c AW |
1976 | # open braces for enum, union and struct go on the same line. |
1977 | if ($line =~ /^.\s*{/ && | |
1978 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { | |
1979 | ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); | |
1980 | } | |
1981 | ||
0c73b4eb AW |
1982 | # missing space after union, struct or enum definition |
1983 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { | |
1984 | WARN("missing space after $1 definition\n" . $herecurr); | |
1985 | } | |
1986 | ||
8d31cfce AW |
1987 | # check for spacing round square brackets; allowed: |
1988 | # 1. with a type on the left -- int [] a; | |
fe2a7dbc AW |
1989 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
1990 | # 3. inside a curly brace -- = { [0...10] = 5 } | |
8d31cfce AW |
1991 | while ($line =~ /(.*?\s)\[/g) { |
1992 | my ($where, $prefix) = ($-[1], $1); | |
1993 | if ($prefix !~ /$Type\s+$/ && | |
fe2a7dbc AW |
1994 | ($where != 0 || $prefix !~ /^.\s+$/) && |
1995 | $prefix !~ /{\s+$/) { | |
8d31cfce AW |
1996 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); |
1997 | } | |
1998 | } | |
1999 | ||
f0a594c1 | 2000 | # check for spaces between functions and their parentheses. |
6c72ffaa | 2001 | while ($line =~ /($Ident)\s+\(/g) { |
c2fdda0d | 2002 | my $name = $1; |
773647a0 AW |
2003 | my $ctx_before = substr($line, 0, $-[1]); |
2004 | my $ctx = "$ctx_before$name"; | |
c2fdda0d AW |
2005 | |
2006 | # Ignore those directives where spaces _are_ permitted. | |
773647a0 AW |
2007 | if ($name =~ /^(?: |
2008 | if|for|while|switch|return|case| | |
2009 | volatile|__volatile__| | |
2010 | __attribute__|format|__extension__| | |
2011 | asm|__asm__)$/x) | |
2012 | { | |
c2fdda0d AW |
2013 | |
2014 | # cpp #define statements have non-optional spaces, ie | |
2015 | # if there is a space between the name and the open | |
2016 | # parenthesis it is simply not a parameter group. | |
c45dcabd | 2017 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { |
773647a0 AW |
2018 | |
2019 | # cpp #elif statement condition may start with a ( | |
c45dcabd | 2020 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { |
c2fdda0d AW |
2021 | |
2022 | # If this whole things ends with a type its most | |
2023 | # likely a typedef for a function. | |
773647a0 | 2024 | } elsif ($ctx =~ /$Type$/) { |
c2fdda0d AW |
2025 | |
2026 | } else { | |
773647a0 | 2027 | WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr); |
6c72ffaa | 2028 | } |
f0a594c1 | 2029 | } |
653d4876 | 2030 | # Check operator spacing. |
0a920b5b | 2031 | if (!($line=~/\#\s*include/)) { |
9c0ca6f9 AW |
2032 | my $ops = qr{ |
2033 | <<=|>>=|<=|>=|==|!=| | |
2034 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | |
2035 | =>|->|<<|>>|<|>|=|!|~| | |
1f65f947 AW |
2036 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| |
2037 | \?|: | |
9c0ca6f9 | 2038 | }x; |
cf655043 | 2039 | my @elements = split(/($ops|;)/, $opline); |
00df344f | 2040 | my $off = 0; |
6c72ffaa AW |
2041 | |
2042 | my $blank = copy_spacing($opline); | |
2043 | ||
0a920b5b | 2044 | for (my $n = 0; $n < $#elements; $n += 2) { |
4a0df2ef AW |
2045 | $off += length($elements[$n]); |
2046 | ||
773647a0 AW |
2047 | # Pick up the preceeding and succeeding characters. |
2048 | my $ca = substr($opline, 0, $off); | |
2049 | my $cc = ''; | |
2050 | if (length($opline) >= ($off + length($elements[$n + 1]))) { | |
2051 | $cc = substr($opline, $off + length($elements[$n + 1])); | |
2052 | } | |
2053 | my $cb = "$ca$;$cc"; | |
2054 | ||
4a0df2ef AW |
2055 | my $a = ''; |
2056 | $a = 'V' if ($elements[$n] ne ''); | |
2057 | $a = 'W' if ($elements[$n] =~ /\s$/); | |
cf655043 | 2058 | $a = 'C' if ($elements[$n] =~ /$;$/); |
4a0df2ef AW |
2059 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
2060 | $a = 'O' if ($elements[$n] eq ''); | |
773647a0 | 2061 | $a = 'E' if ($ca =~ /^\s*$/); |
4a0df2ef | 2062 | |
0a920b5b | 2063 | my $op = $elements[$n + 1]; |
4a0df2ef AW |
2064 | |
2065 | my $c = ''; | |
0a920b5b | 2066 | if (defined $elements[$n + 2]) { |
4a0df2ef AW |
2067 | $c = 'V' if ($elements[$n + 2] ne ''); |
2068 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | |
cf655043 | 2069 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); |
4a0df2ef AW |
2070 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
2071 | $c = 'O' if ($elements[$n + 2] eq ''); | |
8b1b3378 | 2072 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); |
4a0df2ef AW |
2073 | } else { |
2074 | $c = 'E'; | |
0a920b5b AW |
2075 | } |
2076 | ||
4a0df2ef AW |
2077 | my $ctx = "${a}x${c}"; |
2078 | ||
2079 | my $at = "(ctx:$ctx)"; | |
2080 | ||
6c72ffaa | 2081 | my $ptr = substr($blank, 0, $off) . "^"; |
de7d4f0e | 2082 | my $hereptr = "$hereline$ptr\n"; |
0a920b5b | 2083 | |
74048ed8 | 2084 | # Pull out the value of this operator. |
6c72ffaa | 2085 | my $op_type = substr($curr_values, $off + 1, 1); |
0a920b5b | 2086 | |
1f65f947 AW |
2087 | # Get the full operator variant. |
2088 | my $opv = $op . substr($curr_vars, $off, 1); | |
2089 | ||
13214adf AW |
2090 | # Ignore operators passed as parameters. |
2091 | if ($op_type ne 'V' && | |
2092 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { | |
2093 | ||
cf655043 AW |
2094 | # # Ignore comments |
2095 | # } elsif ($op =~ /^$;+$/) { | |
13214adf | 2096 | |
d8aaf121 | 2097 | # ; should have either the end of line or a space or \ after it |
13214adf | 2098 | } elsif ($op eq ';') { |
cf655043 AW |
2099 | if ($ctx !~ /.x[WEBC]/ && |
2100 | $cc !~ /^\\/ && $cc !~ /^;/) { | |
773647a0 | 2101 | ERROR("space required after that '$op' $at\n" . $hereptr); |
d8aaf121 AW |
2102 | } |
2103 | ||
2104 | # // is a comment | |
2105 | } elsif ($op eq '//') { | |
0a920b5b | 2106 | |
1f65f947 AW |
2107 | # No spaces for: |
2108 | # -> | |
2109 | # : when part of a bitfield | |
2110 | } elsif ($op eq '->' || $opv eq ':B') { | |
4a0df2ef | 2111 | if ($ctx =~ /Wx.|.xW/) { |
773647a0 | 2112 | ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); |
0a920b5b AW |
2113 | } |
2114 | ||
2115 | # , must have a space on the right. | |
2116 | } elsif ($op eq ',') { | |
cf655043 | 2117 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { |
773647a0 | 2118 | ERROR("space required after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
2119 | } |
2120 | ||
9c0ca6f9 | 2121 | # '*' as part of a type definition -- reported already. |
74048ed8 | 2122 | } elsif ($opv eq '*_') { |
9c0ca6f9 AW |
2123 | #warn "'*' is part of type\n"; |
2124 | ||
2125 | # unary operators should have a space before and | |
2126 | # none after. May be left adjacent to another | |
2127 | # unary operator, or a cast | |
2128 | } elsif ($op eq '!' || $op eq '~' || | |
74048ed8 | 2129 | $opv eq '*U' || $opv eq '-U' || |
0d413866 | 2130 | $opv eq '&U' || $opv eq '&&U') { |
cf655043 | 2131 | if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
773647a0 | 2132 | ERROR("space required before that '$op' $at\n" . $hereptr); |
0a920b5b | 2133 | } |
a3340b35 | 2134 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { |
171ae1a4 AW |
2135 | # A unary '*' may be const |
2136 | ||
2137 | } elsif ($ctx =~ /.xW/) { | |
fb9e9096 | 2138 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); |
0a920b5b AW |
2139 | } |
2140 | ||
2141 | # unary ++ and unary -- are allowed no space on one side. | |
2142 | } elsif ($op eq '++' or $op eq '--') { | |
773647a0 AW |
2143 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { |
2144 | ERROR("space required one side of that '$op' $at\n" . $hereptr); | |
2145 | } | |
2146 | if ($ctx =~ /Wx[BE]/ || | |
2147 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { | |
2148 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); | |
0a920b5b | 2149 | } |
773647a0 AW |
2150 | if ($ctx =~ /ExW/) { |
2151 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); | |
653d4876 | 2152 | } |
0a920b5b | 2153 | |
773647a0 | 2154 | |
0a920b5b | 2155 | # << and >> may either have or not have spaces both sides |
9c0ca6f9 AW |
2156 | } elsif ($op eq '<<' or $op eq '>>' or |
2157 | $op eq '&' or $op eq '^' or $op eq '|' or | |
2158 | $op eq '+' or $op eq '-' or | |
c2fdda0d AW |
2159 | $op eq '*' or $op eq '/' or |
2160 | $op eq '%') | |
0a920b5b | 2161 | { |
773647a0 | 2162 | if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { |
de7d4f0e AW |
2163 | ERROR("need consistent spacing around '$op' $at\n" . |
2164 | $hereptr); | |
0a920b5b AW |
2165 | } |
2166 | ||
1f65f947 AW |
2167 | # A colon needs no spaces before when it is |
2168 | # terminating a case value or a label. | |
2169 | } elsif ($opv eq ':C' || $opv eq ':L') { | |
2170 | if ($ctx =~ /Wx./) { | |
2171 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); | |
2172 | } | |
2173 | ||
0a920b5b | 2174 | # All the others need spaces both sides. |
cf655043 | 2175 | } elsif ($ctx !~ /[EWC]x[CWE]/) { |
1f65f947 AW |
2176 | my $ok = 0; |
2177 | ||
22f2a2ef | 2178 | # Ignore email addresses <foo@bar> |
1f65f947 AW |
2179 | if (($op eq '<' && |
2180 | $cc =~ /^\S+\@\S+>/) || | |
2181 | ($op eq '>' && | |
2182 | $ca =~ /<\S+\@\S+$/)) | |
2183 | { | |
2184 | $ok = 1; | |
2185 | } | |
2186 | ||
2187 | # Ignore ?: | |
2188 | if (($opv eq ':O' && $ca =~ /\?$/) || | |
2189 | ($op eq '?' && $cc =~ /^:/)) { | |
2190 | $ok = 1; | |
2191 | } | |
2192 | ||
2193 | if ($ok == 0) { | |
773647a0 | 2194 | ERROR("spaces required around that '$op' $at\n" . $hereptr); |
22f2a2ef | 2195 | } |
0a920b5b | 2196 | } |
4a0df2ef | 2197 | $off += length($elements[$n + 1]); |
0a920b5b AW |
2198 | } |
2199 | } | |
2200 | ||
f0a594c1 AW |
2201 | # check for multiple assignments |
2202 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { | |
6c72ffaa | 2203 | CHK("multiple assignments should be avoided\n" . $herecurr); |
f0a594c1 AW |
2204 | } |
2205 | ||
22f2a2ef AW |
2206 | ## # check for multiple declarations, allowing for a function declaration |
2207 | ## # continuation. | |
2208 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && | |
2209 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { | |
2210 | ## | |
2211 | ## # Remove any bracketed sections to ensure we do not | |
2212 | ## # falsly report the parameters of functions. | |
2213 | ## my $ln = $line; | |
2214 | ## while ($ln =~ s/\([^\(\)]*\)//g) { | |
2215 | ## } | |
2216 | ## if ($ln =~ /,/) { | |
2217 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); | |
2218 | ## } | |
2219 | ## } | |
f0a594c1 | 2220 | |
0a920b5b | 2221 | #need space before brace following if, while, etc |
22f2a2ef AW |
2222 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
2223 | $line =~ /do{/) { | |
773647a0 | 2224 | ERROR("space required before the open brace '{'\n" . $herecurr); |
de7d4f0e AW |
2225 | } |
2226 | ||
2227 | # closing brace should have a space following it when it has anything | |
2228 | # on the line | |
2229 | if ($line =~ /}(?!(?:,|;|\)))\S/) { | |
773647a0 | 2230 | ERROR("space required after that close brace '}'\n" . $herecurr); |
0a920b5b AW |
2231 | } |
2232 | ||
22f2a2ef AW |
2233 | # check spacing on square brackets |
2234 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { | |
773647a0 | 2235 | ERROR("space prohibited after that open square bracket '['\n" . $herecurr); |
22f2a2ef AW |
2236 | } |
2237 | if ($line =~ /\s\]/) { | |
773647a0 | 2238 | ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); |
22f2a2ef AW |
2239 | } |
2240 | ||
c45dcabd | 2241 | # check spacing on parentheses |
9c0ca6f9 AW |
2242 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
2243 | $line !~ /for\s*\(\s+;/) { | |
773647a0 | 2244 | ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); |
22f2a2ef | 2245 | } |
13214adf | 2246 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && |
c45dcabd AW |
2247 | $line !~ /for\s*\(.*;\s+\)/ && |
2248 | $line !~ /:\s+\)/) { | |
773647a0 | 2249 | ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); |
22f2a2ef AW |
2250 | } |
2251 | ||
0a920b5b | 2252 | #goto labels aren't indented, allow a single space however |
4a0df2ef | 2253 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
0a920b5b | 2254 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
de7d4f0e | 2255 | WARN("labels should not be indented\n" . $herecurr); |
0a920b5b AW |
2256 | } |
2257 | ||
c45dcabd AW |
2258 | # Return is not a function. |
2259 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { | |
2260 | my $spacing = $1; | |
2261 | my $value = $2; | |
2262 | ||
86f9d059 | 2263 | # Flatten any parentheses |
fb2d2c1b AW |
2264 | $value =~ s/\(/ \(/g; |
2265 | $value =~ s/\)/\) /g; | |
63f17f89 AW |
2266 | while ($value =~ s/\[[^\{\}]*\]/1/ || |
2267 | $value !~ /(?:$Ident|-?$Constant)\s* | |
2268 | $Compare\s* | |
2269 | (?:$Ident|-?$Constant)/x && | |
2270 | $value =~ s/\([^\(\)]*\)/1/) { | |
c45dcabd | 2271 | } |
fb2d2c1b AW |
2272 | #print "value<$value>\n"; |
2273 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { | |
c45dcabd AW |
2274 | ERROR("return is not a function, parentheses are not required\n" . $herecurr); |
2275 | ||
2276 | } elsif ($spacing !~ /\s+/) { | |
2277 | ERROR("space required before the open parenthesis '('\n" . $herecurr); | |
2278 | } | |
2279 | } | |
53a3c448 AW |
2280 | # Return of what appears to be an errno should normally be -'ve |
2281 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { | |
2282 | my $name = $1; | |
2283 | if ($name ne 'EOF' && $name ne 'ERROR') { | |
2284 | WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr); | |
2285 | } | |
2286 | } | |
c45dcabd | 2287 | |
0a920b5b | 2288 | # Need a space before open parenthesis after if, while etc |
4a0df2ef | 2289 | if ($line=~/\b(if|while|for|switch)\(/) { |
773647a0 | 2290 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
0a920b5b AW |
2291 | } |
2292 | ||
f5fe35dd AW |
2293 | # Check for illegal assignment in if conditional -- and check for trailing |
2294 | # statements after the conditional. | |
170d3a22 AW |
2295 | if ($line =~ /do\s*(?!{)/) { |
2296 | my ($stat_next) = ctx_statement_block($line_nr_next, | |
2297 | $remain_next, $off_next); | |
2298 | $stat_next =~ s/\n./\n /g; | |
2299 | ##print "stat<$stat> stat_next<$stat_next>\n"; | |
2300 | ||
2301 | if ($stat_next =~ /^\s*while\b/) { | |
2302 | # If the statement carries leading newlines, | |
2303 | # then count those as offsets. | |
2304 | my ($whitespace) = | |
2305 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); | |
2306 | my $offset = | |
2307 | statement_rawlines($whitespace) - 1; | |
2308 | ||
2309 | $suppress_whiletrailers{$line_nr_next + | |
2310 | $offset} = 1; | |
2311 | } | |
2312 | } | |
2313 | if (!defined $suppress_whiletrailers{$linenr} && | |
2314 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { | |
171ae1a4 | 2315 | my ($s, $c) = ($stat, $cond); |
8905a67c | 2316 | |
b53c8e10 | 2317 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { |
c2fdda0d | 2318 | ERROR("do not use assignment in if condition\n" . $herecurr); |
8905a67c AW |
2319 | } |
2320 | ||
2321 | # Find out what is on the end of the line after the | |
2322 | # conditional. | |
773647a0 | 2323 | substr($s, 0, length($c), ''); |
8905a67c | 2324 | $s =~ s/\n.*//g; |
13214adf | 2325 | $s =~ s/$;//g; # Remove any comments |
53210168 AW |
2326 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && |
2327 | $c !~ /}\s*while\s*/) | |
773647a0 | 2328 | { |
bb44ad39 AW |
2329 | # Find out how long the conditional actually is. |
2330 | my @newlines = ($c =~ /\n/gs); | |
2331 | my $cond_lines = 1 + $#newlines; | |
42bdf74c | 2332 | my $stat_real = ''; |
bb44ad39 | 2333 | |
42bdf74c HS |
2334 | $stat_real = raw_line($linenr, $cond_lines) |
2335 | . "\n" if ($cond_lines); | |
bb44ad39 AW |
2336 | if (defined($stat_real) && $cond_lines > 1) { |
2337 | $stat_real = "[...]\n$stat_real"; | |
2338 | } | |
2339 | ||
2340 | ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); | |
8905a67c AW |
2341 | } |
2342 | } | |
2343 | ||
13214adf AW |
2344 | # Check for bitwise tests written as boolean |
2345 | if ($line =~ / | |
2346 | (?: | |
2347 | (?:\[|\(|\&\&|\|\|) | |
2348 | \s*0[xX][0-9]+\s* | |
2349 | (?:\&\&|\|\|) | |
2350 | | | |
2351 | (?:\&\&|\|\|) | |
2352 | \s*0[xX][0-9]+\s* | |
2353 | (?:\&\&|\|\||\)|\]) | |
2354 | )/x) | |
2355 | { | |
2356 | WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); | |
2357 | } | |
2358 | ||
8905a67c | 2359 | # if and else should not have general statements after it |
13214adf AW |
2360 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { |
2361 | my $s = $1; | |
2362 | $s =~ s/$;//g; # Remove any comments | |
2363 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { | |
2364 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
2365 | } | |
0a920b5b | 2366 | } |
39667782 AW |
2367 | # if should not continue a brace |
2368 | if ($line =~ /}\s*if\b/) { | |
2369 | ERROR("trailing statements should be on next line\n" . | |
2370 | $herecurr); | |
2371 | } | |
a1080bf8 AW |
2372 | # case and default should not have general statements after them |
2373 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && | |
2374 | $line !~ /\G(?: | |
3fef12d6 | 2375 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| |
a1080bf8 AW |
2376 | \s*return\s+ |
2377 | )/xg) | |
2378 | { | |
2379 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
2380 | } | |
0a920b5b AW |
2381 | |
2382 | # Check for }<nl>else {, these must be at the same | |
2383 | # indent level to be relevant to each other. | |
2384 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and | |
2385 | $previndent == $indent) { | |
de7d4f0e | 2386 | ERROR("else should follow close brace '}'\n" . $hereprev); |
0a920b5b AW |
2387 | } |
2388 | ||
c2fdda0d AW |
2389 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and |
2390 | $previndent == $indent) { | |
2391 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | |
2392 | ||
2393 | # Find out what is on the end of the line after the | |
2394 | # conditional. | |
773647a0 | 2395 | substr($s, 0, length($c), ''); |
c2fdda0d AW |
2396 | $s =~ s/\n.*//g; |
2397 | ||
2398 | if ($s =~ /^\s*;/) { | |
2399 | ERROR("while should follow close brace '}'\n" . $hereprev); | |
2400 | } | |
2401 | } | |
2402 | ||
0a920b5b AW |
2403 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
2404 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | |
2405 | # print "No studly caps, use _\n"; | |
2406 | # print "$herecurr"; | |
2407 | # $clean = 0; | |
2408 | # } | |
2409 | ||
2410 | #no spaces allowed after \ in define | |
c45dcabd | 2411 | if ($line=~/\#\s*define.*\\\s$/) { |
de7d4f0e | 2412 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
0a920b5b AW |
2413 | } |
2414 | ||
653d4876 | 2415 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
c45dcabd | 2416 | if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { |
e09dec48 AW |
2417 | my $file = "$1.h"; |
2418 | my $checkfile = "include/linux/$file"; | |
2419 | if (-f "$root/$checkfile" && | |
2420 | $realfile ne $checkfile && | |
7840a94c | 2421 | $1 !~ /$allowed_asm_includes/) |
c45dcabd | 2422 | { |
e09dec48 AW |
2423 | if ($realfile =~ m{^arch/}) { |
2424 | CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); | |
2425 | } else { | |
2426 | WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); | |
2427 | } | |
0a920b5b AW |
2428 | } |
2429 | } | |
2430 | ||
653d4876 AW |
2431 | # multi-statement macros should be enclosed in a do while loop, grab the |
2432 | # first statement and ensure its the whole macro if its not enclosed | |
cf655043 | 2433 | # in a known good container |
b8f96a31 AW |
2434 | if ($realfile !~ m@/vmlinux.lds.h$@ && |
2435 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { | |
d8aaf121 AW |
2436 | my $ln = $linenr; |
2437 | my $cnt = $realcnt; | |
c45dcabd AW |
2438 | my ($off, $dstat, $dcond, $rest); |
2439 | my $ctx = ''; | |
653d4876 | 2440 | |
c45dcabd AW |
2441 | my $args = defined($1); |
2442 | ||
2443 | # Find the end of the macro and limit our statement | |
2444 | # search to that. | |
2445 | while ($cnt > 0 && defined $lines[$ln - 1] && | |
2446 | $lines[$ln - 1] =~ /^(?:-|..*\\$)/) | |
2447 | { | |
2448 | $ctx .= $rawlines[$ln - 1] . "\n"; | |
a3bb97a7 | 2449 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
c45dcabd | 2450 | $ln++; |
c45dcabd AW |
2451 | } |
2452 | $ctx .= $rawlines[$ln - 1]; | |
2453 | ||
2454 | ($dstat, $dcond, $ln, $cnt, $off) = | |
2455 | ctx_statement_block($linenr, $ln - $linenr + 1, 0); | |
2456 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; | |
a3bb97a7 | 2457 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
c45dcabd AW |
2458 | |
2459 | # Extract the remainder of the define (if any) and | |
2460 | # rip off surrounding spaces, and trailing \'s. | |
2461 | $rest = ''; | |
636d140a AW |
2462 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { |
2463 | #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; | |
a3bb97a7 AW |
2464 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { |
2465 | $rest .= substr($lines[$ln - 1], $off) . "\n"; | |
2466 | $cnt--; | |
2467 | } | |
c45dcabd | 2468 | $ln++; |
c45dcabd AW |
2469 | $off = 0; |
2470 | } | |
2471 | $rest =~ s/\\\n.//g; | |
2472 | $rest =~ s/^\s*//s; | |
2473 | $rest =~ s/\s*$//s; | |
2474 | ||
2475 | # Clean up the original statement. | |
2476 | if ($args) { | |
2477 | substr($dstat, 0, length($dcond), ''); | |
2478 | } else { | |
2479 | $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; | |
d8aaf121 | 2480 | } |
292f1a9b | 2481 | $dstat =~ s/$;//g; |
c45dcabd AW |
2482 | $dstat =~ s/\\\n.//g; |
2483 | $dstat =~ s/^\s*//s; | |
2484 | $dstat =~ s/\s*$//s; | |
de7d4f0e | 2485 | |
c45dcabd | 2486 | # Flatten any parentheses and braces |
bf30d6ed AW |
2487 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
2488 | $dstat =~ s/\{[^\{\}]*\}/1/ || | |
2489 | $dstat =~ s/\[[^\{\}]*\]/1/) | |
2490 | { | |
de7d4f0e | 2491 | } |
d8aaf121 | 2492 | |
c45dcabd AW |
2493 | my $exceptions = qr{ |
2494 | $Declare| | |
2495 | module_param_named| | |
2496 | MODULE_PARAM_DESC| | |
2497 | DECLARE_PER_CPU| | |
2498 | DEFINE_PER_CPU| | |
383099fd | 2499 | __typeof__\(| |
22fd2d3e SS |
2500 | union| |
2501 | struct| | |
ea71a0a0 AW |
2502 | \.$Ident\s*=\s*| |
2503 | ^\"|\"$ | |
c45dcabd | 2504 | }x; |
5eaa20b9 AW |
2505 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
2506 | if ($rest ne '' && $rest ne ',') { | |
c45dcabd AW |
2507 | if ($rest !~ /while\s*\(/ && |
2508 | $dstat !~ /$exceptions/) | |
2509 | { | |
de7d4f0e | 2510 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); |
c45dcabd AW |
2511 | } |
2512 | ||
2513 | } elsif ($ctx !~ /;/) { | |
2514 | if ($dstat ne '' && | |
2515 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && | |
2516 | $dstat !~ /$exceptions/ && | |
b132e5d5 | 2517 | $dstat !~ /^\.$Ident\s*=/ && |
c45dcabd AW |
2518 | $dstat =~ /$Operators/) |
2519 | { | |
de7d4f0e | 2520 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
d8aaf121 | 2521 | } |
653d4876 | 2522 | } |
0a920b5b AW |
2523 | } |
2524 | ||
080ba929 MF |
2525 | # make sure symbols are always wrapped with VMLINUX_SYMBOL() ... |
2526 | # all assignments may have only one of the following with an assignment: | |
2527 | # . | |
2528 | # ALIGN(...) | |
2529 | # VMLINUX_SYMBOL(...) | |
2530 | if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { | |
2531 | WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); | |
2532 | } | |
2533 | ||
f0a594c1 | 2534 | # check for redundant bracing round if etc |
13214adf AW |
2535 | if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { |
2536 | my ($level, $endln, @chunks) = | |
cf655043 | 2537 | ctx_statement_full($linenr, $realcnt, 1); |
13214adf | 2538 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
cf655043 AW |
2539 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
2540 | if ($#chunks > 0 && $level == 0) { | |
13214adf AW |
2541 | my $allowed = 0; |
2542 | my $seen = 0; | |
773647a0 | 2543 | my $herectx = $here . "\n"; |
cf655043 | 2544 | my $ln = $linenr - 1; |
13214adf AW |
2545 | for my $chunk (@chunks) { |
2546 | my ($cond, $block) = @{$chunk}; | |
2547 | ||
773647a0 AW |
2548 | # If the condition carries leading newlines, then count those as offsets. |
2549 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); | |
2550 | my $offset = statement_rawlines($whitespace) - 1; | |
2551 | ||
2552 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; | |
2553 | ||
2554 | # We have looked at and allowed this specific line. | |
2555 | $suppress_ifbraces{$ln + $offset} = 1; | |
2556 | ||
2557 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; | |
cf655043 AW |
2558 | $ln += statement_rawlines($block) - 1; |
2559 | ||
773647a0 | 2560 | substr($block, 0, length($cond), ''); |
13214adf AW |
2561 | |
2562 | $seen++ if ($block =~ /^\s*{/); | |
2563 | ||
cf655043 AW |
2564 | #print "cond<$cond> block<$block> allowed<$allowed>\n"; |
2565 | if (statement_lines($cond) > 1) { | |
2566 | #print "APW: ALLOWED: cond<$cond>\n"; | |
13214adf AW |
2567 | $allowed = 1; |
2568 | } | |
2569 | if ($block =~/\b(?:if|for|while)\b/) { | |
cf655043 | 2570 | #print "APW: ALLOWED: block<$block>\n"; |
13214adf AW |
2571 | $allowed = 1; |
2572 | } | |
cf655043 AW |
2573 | if (statement_block_size($block) > 1) { |
2574 | #print "APW: ALLOWED: lines block<$block>\n"; | |
13214adf AW |
2575 | $allowed = 1; |
2576 | } | |
2577 | } | |
2578 | if ($seen && !$allowed) { | |
cf655043 | 2579 | WARN("braces {} are not necessary for any arm of this statement\n" . $herectx); |
13214adf AW |
2580 | } |
2581 | } | |
2582 | } | |
773647a0 | 2583 | if (!defined $suppress_ifbraces{$linenr - 1} && |
13214adf | 2584 | $line =~ /\b(if|while|for|else)\b/) { |
cf655043 AW |
2585 | my $allowed = 0; |
2586 | ||
2587 | # Check the pre-context. | |
2588 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { | |
2589 | #print "APW: ALLOWED: pre<$1>\n"; | |
2590 | $allowed = 1; | |
2591 | } | |
773647a0 AW |
2592 | |
2593 | my ($level, $endln, @chunks) = | |
2594 | ctx_statement_full($linenr, $realcnt, $-[0]); | |
2595 | ||
cf655043 AW |
2596 | # Check the condition. |
2597 | my ($cond, $block) = @{$chunks[0]}; | |
773647a0 | 2598 | #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; |
cf655043 | 2599 | if (defined $cond) { |
773647a0 | 2600 | substr($block, 0, length($cond), ''); |
cf655043 AW |
2601 | } |
2602 | if (statement_lines($cond) > 1) { | |
2603 | #print "APW: ALLOWED: cond<$cond>\n"; | |
2604 | $allowed = 1; | |
2605 | } | |
2606 | if ($block =~/\b(?:if|for|while)\b/) { | |
2607 | #print "APW: ALLOWED: block<$block>\n"; | |
2608 | $allowed = 1; | |
2609 | } | |
2610 | if (statement_block_size($block) > 1) { | |
2611 | #print "APW: ALLOWED: lines block<$block>\n"; | |
2612 | $allowed = 1; | |
2613 | } | |
2614 | # Check the post-context. | |
2615 | if (defined $chunks[1]) { | |
2616 | my ($cond, $block) = @{$chunks[1]}; | |
2617 | if (defined $cond) { | |
773647a0 | 2618 | substr($block, 0, length($cond), ''); |
cf655043 AW |
2619 | } |
2620 | if ($block =~ /^\s*\{/) { | |
2621 | #print "APW: ALLOWED: chunk-1 block<$block>\n"; | |
2622 | $allowed = 1; | |
2623 | } | |
2624 | } | |
2625 | if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { | |
2626 | my $herectx = $here . "\n";; | |
f055663c | 2627 | my $cnt = statement_rawlines($block); |
cf655043 | 2628 | |
f055663c AW |
2629 | for (my $n = 0; $n < $cnt; $n++) { |
2630 | $herectx .= raw_line($linenr, $n) . "\n";; | |
f0a594c1 | 2631 | } |
cf655043 AW |
2632 | |
2633 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); | |
f0a594c1 AW |
2634 | } |
2635 | } | |
2636 | ||
653d4876 | 2637 | # don't include deprecated include files (uses RAW line) |
4a0df2ef | 2638 | for my $inc (@dep_includes) { |
c45dcabd | 2639 | if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { |
de7d4f0e | 2640 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
0a920b5b AW |
2641 | } |
2642 | } | |
2643 | ||
4a0df2ef AW |
2644 | # don't use deprecated functions |
2645 | for my $func (@dep_functions) { | |
00df344f | 2646 | if ($line =~ /\b$func\b/) { |
de7d4f0e | 2647 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
4a0df2ef AW |
2648 | } |
2649 | } | |
2650 | ||
2651 | # no volatiles please | |
6c72ffaa AW |
2652 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
2653 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { | |
de7d4f0e | 2654 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
4a0df2ef AW |
2655 | } |
2656 | ||
9c0ca6f9 AW |
2657 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
2658 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { | |
2659 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); | |
2660 | } | |
2661 | ||
00df344f | 2662 | # warn about #if 0 |
c45dcabd | 2663 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { |
de7d4f0e AW |
2664 | CHK("if this code is redundant consider removing it\n" . |
2665 | $herecurr); | |
4a0df2ef AW |
2666 | } |
2667 | ||
f0a594c1 AW |
2668 | # check for needless kfree() checks |
2669 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { | |
2670 | my $expr = $1; | |
2671 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { | |
3c232147 | 2672 | WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev); |
f0a594c1 AW |
2673 | } |
2674 | } | |
4c432a8f GKH |
2675 | # check for needless usb_free_urb() checks |
2676 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { | |
2677 | my $expr = $1; | |
2678 | if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) { | |
2679 | WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev); | |
2680 | } | |
2681 | } | |
f0a594c1 | 2682 | |
1a15a250 PP |
2683 | # prefer usleep_range over udelay |
2684 | if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) { | |
2685 | # ignore udelay's < 10, however | |
2686 | if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) { | |
2687 | CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line); | |
2688 | } | |
2689 | } | |
2690 | ||
09ef8725 PP |
2691 | # warn about unexpectedly long msleep's |
2692 | if ($line =~ /\bmsleep\s*\((\d+)\);/) { | |
2693 | if ($1 < 20) { | |
2694 | WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line); | |
2695 | } | |
2696 | } | |
2697 | ||
00df344f | 2698 | # warn about #ifdefs in C files |
c45dcabd | 2699 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
00df344f AW |
2700 | # print "#ifdef in C files should be avoided\n"; |
2701 | # print "$herecurr"; | |
2702 | # $clean = 0; | |
2703 | # } | |
2704 | ||
22f2a2ef | 2705 | # warn about spacing in #ifdefs |
c45dcabd | 2706 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { |
22f2a2ef AW |
2707 | ERROR("exactly one space required after that #$1\n" . $herecurr); |
2708 | } | |
2709 | ||
4a0df2ef | 2710 | # check for spinlock_t definitions without a comment. |
171ae1a4 AW |
2711 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || |
2712 | $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { | |
4a0df2ef AW |
2713 | my $which = $1; |
2714 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 2715 | CHK("$1 definition without comment\n" . $herecurr); |
4a0df2ef AW |
2716 | } |
2717 | } | |
2718 | # check for memory barriers without a comment. | |
2719 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { | |
2720 | if (!ctx_has_comment($first_line, $linenr)) { | |
de7d4f0e | 2721 | CHK("memory barrier without comment\n" . $herecurr); |
4a0df2ef AW |
2722 | } |
2723 | } | |
2724 | # check of hardware specific defines | |
c45dcabd | 2725 | if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
de7d4f0e | 2726 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
0a920b5b | 2727 | } |
653d4876 | 2728 | |
d4977c78 TK |
2729 | # Check that the storage class is at the beginning of a declaration |
2730 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { | |
2731 | WARN("storage class should be at the beginning of the declaration\n" . $herecurr) | |
2732 | } | |
2733 | ||
de7d4f0e AW |
2734 | # check the location of the inline attribute, that it is between |
2735 | # storage class and type. | |
9c0ca6f9 AW |
2736 | if ($line =~ /\b$Type\s+$Inline\b/ || |
2737 | $line =~ /\b$Inline\s+$Storage\b/) { | |
de7d4f0e AW |
2738 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
2739 | } | |
2740 | ||
8905a67c AW |
2741 | # Check for __inline__ and __inline, prefer inline |
2742 | if ($line =~ /\b(__inline__|__inline)\b/) { | |
2743 | WARN("plain inline is preferred over $1\n" . $herecurr); | |
2744 | } | |
2745 | ||
3d130fd0 JP |
2746 | # Check for __attribute__ packed, prefer __packed |
2747 | if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { | |
2748 | WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr); | |
2749 | } | |
2750 | ||
8f53a9b8 JP |
2751 | # check for sizeof(&) |
2752 | if ($line =~ /\bsizeof\s*\(\s*\&/) { | |
2753 | WARN("sizeof(& should be avoided\n" . $herecurr); | |
2754 | } | |
2755 | ||
de7d4f0e | 2756 | # check for new externs in .c files. |
171ae1a4 | 2757 | if ($realfile =~ /\.c$/ && defined $stat && |
c45dcabd | 2758 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
171ae1a4 | 2759 | { |
c45dcabd AW |
2760 | my $function_name = $1; |
2761 | my $paren_space = $2; | |
171ae1a4 AW |
2762 | |
2763 | my $s = $stat; | |
2764 | if (defined $cond) { | |
2765 | substr($s, 0, length($cond), ''); | |
2766 | } | |
c45dcabd AW |
2767 | if ($s =~ /^\s*;/ && |
2768 | $function_name ne 'uninitialized_var') | |
2769 | { | |
171ae1a4 AW |
2770 | WARN("externs should be avoided in .c files\n" . $herecurr); |
2771 | } | |
2772 | ||
2773 | if ($paren_space =~ /\n/) { | |
2774 | WARN("arguments for function declarations should follow identifier\n" . $herecurr); | |
2775 | } | |
9c9ba34e AW |
2776 | |
2777 | } elsif ($realfile =~ /\.c$/ && defined $stat && | |
2778 | $stat =~ /^.\s*extern\s+/) | |
2779 | { | |
2780 | WARN("externs should be avoided in .c files\n" . $herecurr); | |
de7d4f0e AW |
2781 | } |
2782 | ||
2783 | # checks for new __setup's | |
2784 | if ($rawline =~ /\b__setup\("([^"]*)"/) { | |
2785 | my $name = $1; | |
2786 | ||
2787 | if (!grep(/$name/, @setup_docs)) { | |
2788 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); | |
2789 | } | |
653d4876 | 2790 | } |
9c0ca6f9 AW |
2791 | |
2792 | # check for pointless casting of kmalloc return | |
caf2a54f | 2793 | if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { |
9c0ca6f9 AW |
2794 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
2795 | } | |
13214adf | 2796 | |
caf2a54f JP |
2797 | # check for multiple semicolons |
2798 | if ($line =~ /;\s*;\s*$/) { | |
2799 | WARN("Statements terminations use 1 semicolon\n" . $herecurr); | |
2800 | } | |
2801 | ||
13214adf AW |
2802 | # check for gcc specific __FUNCTION__ |
2803 | if ($line =~ /__FUNCTION__/) { | |
2804 | WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); | |
2805 | } | |
773647a0 | 2806 | |
4882720b TG |
2807 | # check for semaphores initialized locked |
2808 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { | |
773647a0 | 2809 | WARN("consider using a completion\n" . $herecurr); |
1704f47b | 2810 | |
773647a0 AW |
2811 | } |
2812 | # recommend strict_strto* over simple_strto* | |
2813 | if ($line =~ /\bsimple_(strto.*?)\s*\(/) { | |
2814 | WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr); | |
2815 | } | |
f3db6639 ME |
2816 | # check for __initcall(), use device_initcall() explicitly please |
2817 | if ($line =~ /^.\s*__initcall\s*\(/) { | |
2818 | WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); | |
2819 | } | |
79404849 ER |
2820 | # check for various ops structs, ensure they are const. |
2821 | my $struct_ops = qr{acpi_dock_ops| | |
2822 | address_space_operations| | |
2823 | backlight_ops| | |
2824 | block_device_operations| | |
2825 | dentry_operations| | |
2826 | dev_pm_ops| | |
2827 | dma_map_ops| | |
2828 | extent_io_ops| | |
2829 | file_lock_operations| | |
2830 | file_operations| | |
2831 | hv_ops| | |
2832 | ide_dma_ops| | |
2833 | intel_dvo_dev_ops| | |
2834 | item_operations| | |
2835 | iwl_ops| | |
2836 | kgdb_arch| | |
2837 | kgdb_io| | |
2838 | kset_uevent_ops| | |
2839 | lock_manager_operations| | |
2840 | microcode_ops| | |
2841 | mtrr_ops| | |
2842 | neigh_ops| | |
2843 | nlmsvc_binding| | |
2844 | pci_raw_ops| | |
2845 | pipe_buf_operations| | |
2846 | platform_hibernation_ops| | |
2847 | platform_suspend_ops| | |
2848 | proto_ops| | |
2849 | rpc_pipe_ops| | |
2850 | seq_operations| | |
2851 | snd_ac97_build_ops| | |
2852 | soc_pcmcia_socket_ops| | |
2853 | stacktrace_ops| | |
2854 | sysfs_ops| | |
2855 | tty_operations| | |
2856 | usb_mon_operations| | |
2857 | wd_ops}x; | |
6903ffb2 | 2858 | if ($line !~ /\bconst\b/ && |
79404849 | 2859 | $line =~ /\bstruct\s+($struct_ops)\b/) { |
6903ffb2 AW |
2860 | WARN("struct $1 should normally be const\n" . |
2861 | $herecurr); | |
2b6db5cb | 2862 | } |
773647a0 AW |
2863 | |
2864 | # use of NR_CPUS is usually wrong | |
2865 | # ignore definitions of NR_CPUS and usage to define arrays as likely right | |
2866 | if ($line =~ /\bNR_CPUS\b/ && | |
c45dcabd AW |
2867 | $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && |
2868 | $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && | |
171ae1a4 AW |
2869 | $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && |
2870 | $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && | |
2871 | $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) | |
773647a0 AW |
2872 | { |
2873 | WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); | |
2874 | } | |
9c9ba34e AW |
2875 | |
2876 | # check for %L{u,d,i} in strings | |
2877 | my $string; | |
2878 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { | |
2879 | $string = substr($rawline, $-[1], $+[1] - $-[1]); | |
2a1bc5d5 | 2880 | $string =~ s/%%/__/g; |
9c9ba34e AW |
2881 | if ($string =~ /(?<!%)%L[udi]/) { |
2882 | WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); | |
2883 | last; | |
2884 | } | |
2885 | } | |
691d77b6 AW |
2886 | |
2887 | # whine mightly about in_atomic | |
2888 | if ($line =~ /\bin_atomic\s*\(/) { | |
2889 | if ($realfile =~ m@^drivers/@) { | |
2890 | ERROR("do not use in_atomic in drivers\n" . $herecurr); | |
f4a87736 | 2891 | } elsif ($realfile !~ m@^kernel/@) { |
691d77b6 AW |
2892 | WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); |
2893 | } | |
2894 | } | |
1704f47b PZ |
2895 | |
2896 | # check for lockdep_set_novalidate_class | |
2897 | if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || | |
2898 | $line =~ /__lockdep_no_validate__\s*\)/ ) { | |
2899 | if ($realfile !~ m@^kernel/lockdep@ && | |
2900 | $realfile !~ m@^include/linux/lockdep@ && | |
2901 | $realfile !~ m@^drivers/base/core@) { | |
2902 | ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); | |
2903 | } | |
2904 | } | |
88f8831c DJ |
2905 | |
2906 | if ($line =~ /debugfs_create_file.*S_IWUGO/ || | |
2907 | $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { | |
2908 | WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); | |
2909 | } | |
13214adf AW |
2910 | } |
2911 | ||
2912 | # If we have no input at all, then there is nothing to report on | |
2913 | # so just keep quiet. | |
2914 | if ($#rawlines == -1) { | |
2915 | exit(0); | |
0a920b5b AW |
2916 | } |
2917 | ||
8905a67c AW |
2918 | # In mailback mode only produce a report in the negative, for |
2919 | # things that appear to be patches. | |
2920 | if ($mailback && ($clean == 1 || !$is_patch)) { | |
2921 | exit(0); | |
2922 | } | |
2923 | ||
2924 | # This is not a patch, and we are are in 'no-patch' mode so | |
2925 | # just keep quiet. | |
2926 | if (!$chk_patch && !$is_patch) { | |
2927 | exit(0); | |
2928 | } | |
2929 | ||
2930 | if (!$is_patch) { | |
de7d4f0e | 2931 | ERROR("Does not appear to be a unified-diff format patch\n"); |
0a920b5b AW |
2932 | } |
2933 | if ($is_patch && $chk_signoff && $signoff == 0) { | |
de7d4f0e | 2934 | ERROR("Missing Signed-off-by: line(s)\n"); |
0a920b5b AW |
2935 | } |
2936 | ||
8905a67c | 2937 | print report_dump(); |
13214adf AW |
2938 | if ($summary && !($clean == 1 && $quiet == 1)) { |
2939 | print "$filename " if ($summary_file); | |
8905a67c AW |
2940 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
2941 | (($check)? "$cnt_chk checks, " : "") . | |
2942 | "$cnt_lines lines checked\n"; | |
2943 | print "\n" if ($quiet == 0); | |
f0a594c1 | 2944 | } |
8905a67c | 2945 | |
d2c0a235 AW |
2946 | if ($quiet == 0) { |
2947 | # If there were whitespace errors which cleanpatch can fix | |
2948 | # then suggest that. | |
2949 | if ($rpt_cleaners) { | |
2950 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; | |
2951 | print " scripts/cleanfile\n\n"; | |
2952 | } | |
2953 | } | |
2954 | ||
0a920b5b | 2955 | if ($clean == 1 && $quiet == 0) { |
c2fdda0d | 2956 | print "$vname has no obvious style problems and is ready for submission.\n" |
0a920b5b AW |
2957 | } |
2958 | if ($clean == 0 && $quiet == 0) { | |
c2fdda0d | 2959 | print "$vname has style problems, please review. If any of these errors\n"; |
0a920b5b AW |
2960 | print "are false positives report them to the maintainer, see\n"; |
2961 | print "CHECKPATCH in MAINTAINERS.\n"; | |
2962 | } | |
13214adf | 2963 | |
0a920b5b AW |
2964 | return $clean; |
2965 | } |