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