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