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