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