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