blob: ed7928819aa54c6299e759259f1009670952bbaa [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9use POSIX;
Anas Nashifcbfb62c2015-10-14 06:08:54 -040010use File::Basename;
11use Cwd 'abs_path';
12use Term::ANSIColor qw(:constants);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070013
14my $P = $0;
Anas Nashifcbfb62c2015-10-14 06:08:54 -040015my $D = dirname(abs_path($P));
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070016
17my $V = '0.32';
18
19use Getopt::Long qw(:config no_auto_abbrev);
20
21my $quiet = 0;
22my $tree = 1;
23my $chk_signoff = 1;
24my $chk_patch = 1;
25my $tst_only;
26my $emacs = 0;
27my $terse = 0;
Anas Nashifcbfb62c2015-10-14 06:08:54 -040028my $showfile = 0;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070029my $file = 0;
Anas Nashif0f3d5472016-11-07 15:57:57 -050030my $git = 0;
31my %git_commits = ();
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070032my $check = 0;
33my $check_orig = 0;
34my $summary = 1;
35my $mailback = 0;
36my $summary_file = 0;
37my $show_types = 0;
Anas Nashif0f3d5472016-11-07 15:57:57 -050038my $list_types = 0;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070039my $fix = 0;
40my $fix_inplace = 0;
41my $root;
42my %debug;
43my %camelcase = ();
44my %use_type = ();
45my @use = ();
46my %ignore_type = ();
47my @ignore = ();
Anas Nashif49598db2016-11-10 12:48:15 -050048my $help = 0;
Anas Nashif0f3d5472016-11-07 15:57:57 -050049my @exclude = ();
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070050my $configuration_file = ".checkpatch.conf";
51my $max_line_length = 80;
52my $ignore_perl_version = 0;
53my $minimum_perl_version = 5.10.0;
54my $min_conf_desc_length = 4;
55my $spelling_file = "$D/spelling.txt";
Anas Nashifcbfb62c2015-10-14 06:08:54 -040056my $codespell = 0;
57my $codespellfile = "/usr/share/codespell/dictionary.txt";
Anas Nashif0f3d5472016-11-07 15:57:57 -050058my $conststructsfile = "$D/const_structs.checkpatch";
Anas Nashifcbfb62c2015-10-14 06:08:54 -040059my $color = 1;
Anas Nashif0f3d5472016-11-07 15:57:57 -050060my $allow_c99_comments = 1;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070061
62sub help {
63 my ($exitcode) = @_;
64
65 print << "EOM";
66Usage: $P [OPTION]... [FILE]...
67Version: $V
68
69Options:
70 -q, --quiet quiet
71 --no-tree run without a kernel tree
72 --no-signoff do not check for 'Signed-off-by' line
73 --patch treat FILE as patchfile (default)
74 --emacs emacs compile window format
75 --terse one line per report
Anas Nashifcbfb62c2015-10-14 06:08:54 -040076 --showfile emit diffed file position, not input file position
Anas Nashif0f3d5472016-11-07 15:57:57 -050077 -g, --git treat FILE as a single commit or git revision range
78 single git commit with:
79 <rev>
80 <rev>^
81 <rev>~n
82 multiple git commits with:
83 <rev1>..<rev2>
84 <rev1>...<rev2>
85 <rev>-<count>
86 git merges are ignored
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070087 -f, --file treat FILE as regular source file
88 --subjective, --strict enable more subjective tests
Anas Nashif0f3d5472016-11-07 15:57:57 -050089 --list-types list the possible message types
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070090 --types TYPE(,TYPE2...) show only these comma separated message types
91 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Anas Nashif0f3d5472016-11-07 15:57:57 -050092 --show-types show the specific message type in the output
Anas Nashif92a12a12016-05-20 18:26:59 -040093 --exclude DIR(,DIR22...) exclude directories
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070094 --max-line-length=n set the maximum line length, if exceeded, warn
95 --min-conf-desc-length=n set the min description length, if shorter, warn
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070096 --root=PATH PATH to the kernel tree root
97 --no-summary suppress the per-file summary
98 --mailback only produce a report in case of warnings/errors
99 --summary-file include the filename in summary
100 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
101 'values', 'possible', 'type', and 'attr' (default
102 is all off)
103 --test-only=WORD report only warnings/errors containing WORD
104 literally
105 --fix EXPERIMENTAL - may create horrible results
106 If correctable single-line errors exist, create
107 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
108 with potential errors corrected to the preferred
109 checkpatch style
110 --fix-inplace EXPERIMENTAL - may create horrible results
111 Is the same as --fix, but overwrites the input
112 file. It's your fault if there's no backup or git
113 --ignore-perl-version override checking of perl version. expect
114 runtime errors.
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400115 --codespell Use the codespell dictionary for spelling/typos
116 (default:/usr/share/codespell/dictionary.txt)
117 --codespellfile Use this codespell dictionary
118 --color Use colors when output is STDOUT (default: on)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700119 -h, --help, --version display this help and exit
120
121When FILE is - read standard input.
122EOM
123
124 exit($exitcode);
125}
126
Anas Nashif0f3d5472016-11-07 15:57:57 -0500127sub uniq {
128 my %seen;
129 return grep { !$seen{$_}++ } @_;
130}
131
132sub list_types {
133 my ($exitcode) = @_;
134
135 my $count = 0;
136
137 local $/ = undef;
138
139 open(my $script, '<', abs_path($P)) or
140 die "$P: Can't read '$P' $!\n";
141
142 my $text = <$script>;
143 close($script);
144
145 my @types = ();
146 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
147 push (@types, $_);
148 }
149 @types = sort(uniq(@types));
150 print("#\tMessage type\n\n");
151 foreach my $type (@types) {
152 print(++$count . "\t" . $type . "\n");
153 }
154
155 exit($exitcode);
156}
157
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700158my $conf = which_conf($configuration_file);
159if (-f $conf) {
160 my @conf_args;
161 open(my $conffile, '<', "$conf")
162 or warn "$P: Can't find a readable $configuration_file file $!\n";
163
164 while (<$conffile>) {
165 my $line = $_;
166
167 $line =~ s/\s*\n?$//g;
168 $line =~ s/^\s*//g;
169 $line =~ s/\s+/ /g;
170
171 next if ($line =~ m/^\s*#/);
172 next if ($line =~ m/^\s*$/);
173
174 my @words = split(" ", $line);
175 foreach my $word (@words) {
176 last if ($word =~ m/^#/);
177 push (@conf_args, $word);
178 }
179 }
180 close($conffile);
181 unshift(@ARGV, @conf_args) if @conf_args;
182}
183
184GetOptions(
185 'q|quiet+' => \$quiet,
186 'tree!' => \$tree,
187 'signoff!' => \$chk_signoff,
188 'patch!' => \$chk_patch,
189 'emacs!' => \$emacs,
190 'terse!' => \$terse,
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400191 'showfile!' => \$showfile,
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700192 'f|file!' => \$file,
Anas Nashif0f3d5472016-11-07 15:57:57 -0500193 'g|git!' => \$git,
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700194 'subjective!' => \$check,
195 'strict!' => \$check,
196 'ignore=s' => \@ignore,
Anas Nashif0f3d5472016-11-07 15:57:57 -0500197 'exclude=s' => \@exclude,
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700198 'types=s' => \@use,
199 'show-types!' => \$show_types,
Anas Nashif0f3d5472016-11-07 15:57:57 -0500200 'list-types!' => \$list_types,
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700201 'max-line-length=i' => \$max_line_length,
202 'min-conf-desc-length=i' => \$min_conf_desc_length,
203 'root=s' => \$root,
204 'summary!' => \$summary,
205 'mailback!' => \$mailback,
206 'summary-file!' => \$summary_file,
207 'fix!' => \$fix,
208 'fix-inplace!' => \$fix_inplace,
209 'ignore-perl-version!' => \$ignore_perl_version,
210 'debug=s' => \%debug,
211 'test-only=s' => \$tst_only,
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400212 'codespell!' => \$codespell,
213 'codespellfile=s' => \$codespellfile,
214 'color!' => \$color,
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700215 'h|help' => \$help,
216 'version' => \$help
217) or help(1);
218
219help(0) if ($help);
220
Anas Nashif0f3d5472016-11-07 15:57:57 -0500221list_types(0) if ($list_types);
222
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700223$fix = 1 if ($fix_inplace);
224$check_orig = $check;
225
226my $exit = 0;
227
228if ($^V && $^V lt $minimum_perl_version) {
229 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
230 if (!$ignore_perl_version) {
231 exit(1);
232 }
233}
234
Anas Nashif0f3d5472016-11-07 15:57:57 -0500235#if no filenames are given, push '-' to read patch from stdin
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700236if ($#ARGV < 0) {
Anas Nashif0f3d5472016-11-07 15:57:57 -0500237 push(@ARGV, '-');
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700238}
239
240sub hash_save_array_words {
241 my ($hashRef, $arrayRef) = @_;
242
243 my @array = split(/,/, join(',', @$arrayRef));
244 foreach my $word (@array) {
245 $word =~ s/\s*\n?$//g;
246 $word =~ s/^\s*//g;
247 $word =~ s/\s+/ /g;
248 $word =~ tr/[a-z]/[A-Z]/;
249
250 next if ($word =~ m/^\s*#/);
251 next if ($word =~ m/^\s*$/);
252
253 $hashRef->{$word}++;
254 }
255}
256
257sub hash_show_words {
258 my ($hashRef, $prefix) = @_;
259
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400260 if (keys %$hashRef) {
261 print "\nNOTE: $prefix message types:";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700262 foreach my $word (sort keys %$hashRef) {
263 print " $word";
264 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400265 print "\n";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700266 }
267}
268
269hash_save_array_words(\%ignore_type, \@ignore);
270hash_save_array_words(\%use_type, \@use);
271
272my $dbg_values = 0;
273my $dbg_possible = 0;
274my $dbg_type = 0;
275my $dbg_attr = 0;
276for my $key (keys %debug) {
277 ## no critic
278 eval "\${dbg_$key} = '$debug{$key}';";
279 die "$@" if ($@);
280}
281
282my $rpt_cleaners = 0;
283
284if ($terse) {
285 $emacs = 1;
286 $quiet++;
287}
288
289if ($tree) {
290 if (defined $root) {
291 if (!top_of_kernel_tree($root)) {
292 die "$P: $root: --root does not point at a valid tree\n";
293 }
294 } else {
295 if (top_of_kernel_tree('.')) {
296 $root = '.';
297 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
298 top_of_kernel_tree($1)) {
299 $root = $1;
300 }
301 }
302
303 if (!defined $root) {
304 print "Must be run from the top-level dir. of a kernel tree\n";
305 exit(2);
306 }
307}
308
309my $emitted_corrupt = 0;
310
311our $Ident = qr{
312 [A-Za-z_][A-Za-z\d_]*
313 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
314 }x;
315our $Storage = qr{extern|static|asmlinkage};
316our $Sparse = qr{
317 __user|
318 __kernel|
319 __force|
320 __iomem|
321 __must_check|
322 __init_refok|
323 __kprobes|
324 __ref|
Anas Nashif0f3d5472016-11-07 15:57:57 -0500325 __rcu|
326 __private
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700327 }x;
328our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
329our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
330our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
331our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
332our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
333
334# Notes to $Attribute:
335# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
336our $Attribute = qr{
337 const|
338 __percpu|
339 __nocast|
340 __safe|
341 __bitwise__|
342 __packed__|
343 __packed2__|
344 __naked|
345 __maybe_unused|
346 __always_unused|
347 __noreturn|
348 __used|
349 __cold|
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400350 __pure|
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700351 __noclone|
352 __deprecated|
353 __read_mostly|
354 __kprobes|
355 $InitAttribute|
356 ____cacheline_aligned|
357 ____cacheline_aligned_in_smp|
358 ____cacheline_internodealigned_in_smp|
359 __weak
360 }x;
361our $Modifier;
362our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
363our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
364our $Lval = qr{$Ident(?:$Member)*};
365
366our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
367our $Binary = qr{(?i)0b[01]+$Int_type?};
368our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
369our $Int = qr{[0-9]+$Int_type?};
370our $Octal = qr{0[0-7]+$Int_type?};
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400371our $String = qr{"[X\t]*"};
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700372our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
373our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
374our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
375our $Float = qr{$Float_hex|$Float_dec|$Float_int};
376our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
377our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
378our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
379our $Arithmetic = qr{\+|-|\*|\/|%};
380our $Operators = qr{
381 <=|>=|==|!=|
382 =>|->|<<|>>|<|>|!|~|
383 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
384 }x;
385
386our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
387
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400388our $BasicType;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700389our $NonptrType;
390our $NonptrTypeMisordered;
391our $NonptrTypeWithAttr;
392our $Type;
393our $TypeMisordered;
394our $Declare;
395our $DeclareMisordered;
396
397our $NON_ASCII_UTF8 = qr{
398 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
399 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
400 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
401 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
402 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
403 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
404 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
405}x;
406
407our $UTF8 = qr{
408 [\x09\x0A\x0D\x20-\x7E] # ASCII
409 | $NON_ASCII_UTF8
410}x;
411
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400412our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
413our $typeOtherOSTypedefs = qr{(?x:
414 u_(?:char|short|int|long) | # bsd
415 u(?:nchar|short|int|long) # sysv
416)};
417our $typeKernelTypedefs = qr{(?x:
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700418 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
419 atomic_t
420)};
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400421our $typeTypedefs = qr{(?x:
422 $typeC99Typedefs\b|
423 $typeOtherOSTypedefs\b|
424 $typeKernelTypedefs\b
425)};
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700426
Anas Nashif76b09132016-02-20 13:53:35 -0500427our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
428
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700429our $logFunctions = qr{(?x:
430 printk(?:_ratelimited|_once|)|
431 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
432 WARN(?:_RATELIMIT|_ONCE|)|
433 panic|
434 MODULE_[A-Z_]+|
435 seq_vprintf|seq_printf|seq_puts
436)};
437
438our $signature_tags = qr{(?xi:
439 Signed-off-by:|
440 Acked-by:|
441 Tested-by:|
442 Reviewed-by:|
443 Reported-by:|
444 Suggested-by:|
445 To:|
446 Cc:
447)};
448
449our @typeListMisordered = (
450 qr{char\s+(?:un)?signed},
451 qr{int\s+(?:(?:un)?signed\s+)?short\s},
452 qr{int\s+short(?:\s+(?:un)?signed)},
453 qr{short\s+int(?:\s+(?:un)?signed)},
454 qr{(?:un)?signed\s+int\s+short},
455 qr{short\s+(?:un)?signed},
456 qr{long\s+int\s+(?:un)?signed},
457 qr{int\s+long\s+(?:un)?signed},
458 qr{long\s+(?:un)?signed\s+int},
459 qr{int\s+(?:un)?signed\s+long},
460 qr{int\s+(?:un)?signed},
461 qr{int\s+long\s+long\s+(?:un)?signed},
462 qr{long\s+long\s+int\s+(?:un)?signed},
463 qr{long\s+long\s+(?:un)?signed\s+int},
464 qr{long\s+long\s+(?:un)?signed},
465 qr{long\s+(?:un)?signed},
466);
467
468our @typeList = (
469 qr{void},
470 qr{(?:(?:un)?signed\s+)?char},
471 qr{(?:(?:un)?signed\s+)?short\s+int},
472 qr{(?:(?:un)?signed\s+)?short},
473 qr{(?:(?:un)?signed\s+)?int},
474 qr{(?:(?:un)?signed\s+)?long\s+int},
475 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
476 qr{(?:(?:un)?signed\s+)?long\s+long},
477 qr{(?:(?:un)?signed\s+)?long},
478 qr{(?:un)?signed},
479 qr{float},
480 qr{double},
481 qr{bool},
482 qr{struct\s+$Ident},
483 qr{union\s+$Ident},
484 qr{enum\s+$Ident},
485 qr{${Ident}_t},
486 qr{${Ident}_handler},
487 qr{${Ident}_handler_fn},
488 @typeListMisordered,
489);
Anas Nashif76b09132016-02-20 13:53:35 -0500490
491our $C90_int_types = qr{(?x:
492 long\s+long\s+int\s+(?:un)?signed|
493 long\s+long\s+(?:un)?signed\s+int|
494 long\s+long\s+(?:un)?signed|
495 (?:(?:un)?signed\s+)?long\s+long\s+int|
496 (?:(?:un)?signed\s+)?long\s+long|
497 int\s+long\s+long\s+(?:un)?signed|
498 int\s+(?:(?:un)?signed\s+)?long\s+long|
499
500 long\s+int\s+(?:un)?signed|
501 long\s+(?:un)?signed\s+int|
502 long\s+(?:un)?signed|
503 (?:(?:un)?signed\s+)?long\s+int|
504 (?:(?:un)?signed\s+)?long|
505 int\s+long\s+(?:un)?signed|
506 int\s+(?:(?:un)?signed\s+)?long|
507
508 int\s+(?:un)?signed|
509 (?:(?:un)?signed\s+)?int
510)};
511
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400512our @typeListFile = ();
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700513our @typeListWithAttr = (
514 @typeList,
515 qr{struct\s+$InitAttribute\s+$Ident},
516 qr{union\s+$InitAttribute\s+$Ident},
517);
518
519our @modifierList = (
520 qr{fastcall},
521);
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400522our @modifierListFile = ();
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700523
524our @mode_permission_funcs = (
525 ["module_param", 3],
526 ["module_param_(?:array|named|string)", 4],
527 ["module_param_array_named", 5],
528 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
529 ["proc_create(?:_data|)", 2],
Anas Nashif0f3d5472016-11-07 15:57:57 -0500530 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
531 ["IIO_DEV_ATTR_[A-Z_]+", 1],
532 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
533 ["SENSOR_TEMPLATE(?:_2|)", 3],
534 ["__ATTR", 2],
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700535);
536
537#Create a search pattern for all these functions to speed up a loop below
538our $mode_perms_search = "";
539foreach my $entry (@mode_permission_funcs) {
540 $mode_perms_search .= '|' if ($mode_perms_search ne "");
541 $mode_perms_search .= $entry->[0];
542}
543
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400544our $mode_perms_world_writable = qr{
545 S_IWUGO |
546 S_IWOTH |
547 S_IRWXUGO |
548 S_IALLUGO |
549 0[0-7][0-7][2367]
550}x;
551
Anas Nashif0f3d5472016-11-07 15:57:57 -0500552our %mode_permission_string_types = (
553 "S_IRWXU" => 0700,
554 "S_IRUSR" => 0400,
555 "S_IWUSR" => 0200,
556 "S_IXUSR" => 0100,
557 "S_IRWXG" => 0070,
558 "S_IRGRP" => 0040,
559 "S_IWGRP" => 0020,
560 "S_IXGRP" => 0010,
561 "S_IRWXO" => 0007,
562 "S_IROTH" => 0004,
563 "S_IWOTH" => 0002,
564 "S_IXOTH" => 0001,
565 "S_IRWXUGO" => 0777,
566 "S_IRUGO" => 0444,
567 "S_IWUGO" => 0222,
568 "S_IXUGO" => 0111,
569);
570
571#Create a search pattern for all these strings to speed up a loop below
572our $mode_perms_string_search = "";
573foreach my $entry (keys %mode_permission_string_types) {
574 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
575 $mode_perms_string_search .= $entry;
576}
577
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700578our $allowed_asm_includes = qr{(?x:
579 irq|
580 memory|
581 time|
582 reboot
583)};
584# memory.h: ARM has a custom one
585
586# Load common spelling mistakes and build regular expression list.
587my $misspellings;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700588my %spelling_fix;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700589
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400590if (open(my $spelling, '<', $spelling_file)) {
591 while (<$spelling>) {
592 my $line = $_;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700593
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400594 $line =~ s/\s*\n?$//g;
595 $line =~ s/^\s*//g;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700596
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400597 next if ($line =~ m/^\s*#/);
598 next if ($line =~ m/^\s*$/);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700599
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400600 my ($suspect, $fix) = split(/\|\|/, $line);
601
602 $spelling_fix{$suspect} = $fix;
603 }
604 close($spelling);
605} else {
606 warn "No typos will be found - file '$spelling_file': $!\n";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700607}
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400608
609if ($codespell) {
610 if (open(my $spelling, '<', $codespellfile)) {
611 while (<$spelling>) {
612 my $line = $_;
613
614 $line =~ s/\s*\n?$//g;
615 $line =~ s/^\s*//g;
616
617 next if ($line =~ m/^\s*#/);
618 next if ($line =~ m/^\s*$/);
619 next if ($line =~ m/, disabled/i);
620
621 $line =~ s/,.*$//;
622
623 my ($suspect, $fix) = split(/->/, $line);
624
625 $spelling_fix{$suspect} = $fix;
626 }
627 close($spelling);
628 } else {
629 warn "No codespell typos will be found - file '$codespellfile': $!\n";
630 }
631}
632
633$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700634
Anas Nashif0f3d5472016-11-07 15:57:57 -0500635my $const_structs = "";
636if (open(my $conststructs, '<', $conststructsfile)) {
637 while (<$conststructs>) {
638 my $line = $_;
639
640 $line =~ s/\s*\n?$//g;
641 $line =~ s/^\s*//g;
642
643 next if ($line =~ m/^\s*#/);
644 next if ($line =~ m/^\s*$/);
645 if ($line =~ /\s/) {
646 print("$conststructsfile: '$line' invalid - ignored\n");
647 next;
648 }
649
650 $const_structs .= '|' if ($const_structs ne "");
651 $const_structs .= $line;
652 }
653 close($conststructsfile);
654#} else {
655# warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
656}
657
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700658sub build_types {
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400659 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
660 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700661 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
662 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
663 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400664 $BasicType = qr{
665 (?:$typeTypedefs\b)|
666 (?:${all}\b)
667 }x;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700668 $NonptrType = qr{
669 (?:$Modifier\s+|const\s+)*
670 (?:
671 (?:typeof|__typeof__)\s*\([^\)]*\)|
672 (?:$typeTypedefs\b)|
673 (?:${all}\b)
674 )
675 (?:\s+$Modifier|\s+const)*
676 }x;
677 $NonptrTypeMisordered = qr{
678 (?:$Modifier\s+|const\s+)*
679 (?:
680 (?:${Misordered}\b)
681 )
682 (?:\s+$Modifier|\s+const)*
683 }x;
684 $NonptrTypeWithAttr = qr{
685 (?:$Modifier\s+|const\s+)*
686 (?:
687 (?:typeof|__typeof__)\s*\([^\)]*\)|
688 (?:$typeTypedefs\b)|
689 (?:${allWithAttr}\b)
690 )
691 (?:\s+$Modifier|\s+const)*
692 }x;
693 $Type = qr{
694 $NonptrType
695 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
696 (?:\s+$Inline|\s+$Modifier)*
697 }x;
698 $TypeMisordered = qr{
699 $NonptrTypeMisordered
700 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
701 (?:\s+$Inline|\s+$Modifier)*
702 }x;
703 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
704 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
705}
706build_types();
707
708our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
709
710# Using $balanced_parens, $LvalOrFunc, or $FuncArg
711# requires at least perl version v5.10.0
712# Any use must be runtime checked with $^V
713
714our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
715our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400716our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700717
718our $declaration_macros = qr{(?x:
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400719 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700720 (?:$Storage\s+)?LIST_HEAD\s*\(|
721 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
722)};
723
724sub deparenthesize {
725 my ($string) = @_;
726 return "" if (!defined($string));
727
728 while ($string =~ /^\s*\(.*\)\s*$/) {
729 $string =~ s@^\s*\(\s*@@;
730 $string =~ s@\s*\)\s*$@@;
731 }
732
733 $string =~ s@\s+@ @g;
734
735 return $string;
736}
737
738sub seed_camelcase_file {
739 my ($file) = @_;
740
741 return if (!(-f $file));
742
743 local $/;
744
745 open(my $include_file, '<', "$file")
746 or warn "$P: Can't read '$file' $!\n";
747 my $text = <$include_file>;
748 close($include_file);
749
750 my @lines = split('\n', $text);
751
752 foreach my $line (@lines) {
753 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
754 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
755 $camelcase{$1} = 1;
756 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
757 $camelcase{$1} = 1;
758 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
759 $camelcase{$1} = 1;
760 }
761 }
762}
763
764my $camelcase_seeded = 0;
765sub seed_camelcase_includes {
766 return if ($camelcase_seeded);
767
768 my $files;
769 my $camelcase_cache = "";
770 my @include_files = ();
771
772 $camelcase_seeded = 1;
773
774 if (-e ".git") {
775 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
776 chomp $git_last_include_commit;
777 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
778 } else {
779 my $last_mod_date = 0;
780 $files = `find $root/include -name "*.h"`;
781 @include_files = split('\n', $files);
782 foreach my $file (@include_files) {
783 my $date = POSIX::strftime("%Y%m%d%H%M",
784 localtime((stat $file)[9]));
785 $last_mod_date = $date if ($last_mod_date < $date);
786 }
787 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
788 }
789
790 if ($camelcase_cache ne "" && -f $camelcase_cache) {
791 open(my $camelcase_file, '<', "$camelcase_cache")
792 or warn "$P: Can't read '$camelcase_cache' $!\n";
793 while (<$camelcase_file>) {
794 chomp;
795 $camelcase{$_} = 1;
796 }
797 close($camelcase_file);
798
799 return;
800 }
801
802 if (-e ".git") {
803 $files = `git ls-files "include/*.h"`;
804 @include_files = split('\n', $files);
805 }
806
807 foreach my $file (@include_files) {
808 seed_camelcase_file($file);
809 }
810
811 if ($camelcase_cache ne "") {
812 unlink glob ".checkpatch-camelcase.*";
813 open(my $camelcase_file, '>', "$camelcase_cache")
814 or warn "$P: Can't write '$camelcase_cache' $!\n";
815 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
816 print $camelcase_file ("$_\n");
817 }
818 close($camelcase_file);
819 }
820}
821
822sub git_commit_info {
823 my ($commit, $id, $desc) = @_;
824
825 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
826
827 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
828 $output =~ s/^\s*//gm;
829 my @lines = split("\n", $output);
830
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400831 return ($id, $desc) if ($#lines < 0);
832
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700833 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
834# Maybe one day convert this block of bash into something that returns
835# all matching commit ids, but it's very slow...
836#
837# echo "checking commits $1..."
838# git rev-list --remotes | grep -i "^$1" |
839# while read line ; do
840# git log --format='%H %s' -1 $line |
841# echo "commit $(cut -c 1-12,41-)"
842# done
843 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
844 } else {
845 $id = substr($lines[0], 0, 12);
846 $desc = substr($lines[0], 41);
847 }
848
849 return ($id, $desc);
850}
851
852$chk_signoff = 0 if ($file);
853
854my @rawlines = ();
855my @lines = ();
856my @fixed = ();
857my @fixed_inserted = ();
858my @fixed_deleted = ();
859my $fixlinenr = -1;
860
Anas Nashif0f3d5472016-11-07 15:57:57 -0500861# If input is git commits, extract all commits from the commit expressions.
862# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
863die "$P: No git repository found\n" if ($git && !-e ".git");
864
865if ($git) {
866 my @commits = ();
867 foreach my $commit_expr (@ARGV) {
868 my $git_range;
869 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
870 $git_range = "-$2 $1";
871 } elsif ($commit_expr =~ m/\.\./) {
872 $git_range = "$commit_expr";
873 } else {
874 $git_range = "-1 $commit_expr";
875 }
876 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
877 foreach my $line (split(/\n/, $lines)) {
878 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
879 next if (!defined($1) || !defined($2));
880 my $sha1 = $1;
881 my $subject = $2;
882 unshift(@commits, $sha1);
883 $git_commits{$sha1} = $subject;
884 }
885 }
886 die "$P: no git commits after extraction!\n" if (@commits == 0);
887 @ARGV = @commits;
888}
889
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700890my $vname;
891for my $filename (@ARGV) {
892 my $FILE;
Anas Nashif0f3d5472016-11-07 15:57:57 -0500893 if ($git) {
894 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
895 die "$P: $filename: git format-patch failed - $!\n";
896 } elsif ($file) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700897 open($FILE, '-|', "diff -u /dev/null $filename") ||
898 die "$P: $filename: diff failed - $!\n";
899 } elsif ($filename eq '-') {
900 open($FILE, '<&STDIN');
901 } else {
902 open($FILE, '<', "$filename") ||
903 die "$P: $filename: open failed - $!\n";
904 }
905 if ($filename eq '-') {
906 $vname = 'Your patch';
Anas Nashif0f3d5472016-11-07 15:57:57 -0500907 } elsif ($git) {
908 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700909 } else {
910 $vname = $filename;
911 }
912 while (<$FILE>) {
913 chomp;
914 push(@rawlines, $_);
915 }
916 close($FILE);
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400917
918 if ($#ARGV > 0 && $quiet == 0) {
919 print '-' x length($vname) . "\n";
920 print "$vname\n";
921 print '-' x length($vname) . "\n";
922 }
923
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700924 if (!process($filename)) {
925 $exit = 1;
926 }
927 @rawlines = ();
928 @lines = ();
929 @fixed = ();
930 @fixed_inserted = ();
931 @fixed_deleted = ();
932 $fixlinenr = -1;
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400933 @modifierListFile = ();
934 @typeListFile = ();
935 build_types();
936}
937
938if (!$quiet) {
939 hash_show_words(\%use_type, "Used");
940 hash_show_words(\%ignore_type, "Ignored");
941
942 if ($^V lt 5.10.0) {
943 print << "EOM"
944
945NOTE: perl $^V is not modern enough to detect all possible issues.
946 An upgrade to at least perl v5.10.0 is suggested.
947EOM
948 }
949 if ($exit) {
950 print << "EOM"
951
952NOTE: If any of the errors are false positives, please report
Anas Nashif0f3d5472016-11-07 15:57:57 -0500953 them to the maintainer, see CHECKPATCH in MAINTAINERS.
Anas Nashifcbfb62c2015-10-14 06:08:54 -0400954EOM
955 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700956}
957
958exit($exit);
959
960sub top_of_kernel_tree {
961 my ($root) = @_;
962
963 my @tree_check = (
964 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
965 "README", "Documentation", "arch", "include", "drivers",
966 "fs", "init", "ipc", "kernel", "lib", "scripts",
967 );
968
969 foreach my $check (@tree_check) {
970 if (! -e $root . '/' . $check) {
971 return 0;
972 }
973 }
974 return 1;
975}
976
977sub parse_email {
978 my ($formatted_email) = @_;
979
980 my $name = "";
981 my $address = "";
982 my $comment = "";
983
984 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
985 $name = $1;
986 $address = $2;
987 $comment = $3 if defined $3;
988 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
989 $address = $1;
990 $comment = $2 if defined $2;
991 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
992 $address = $1;
993 $comment = $2 if defined $2;
994 $formatted_email =~ s/$address.*$//;
995 $name = $formatted_email;
996 $name = trim($name);
997 $name =~ s/^\"|\"$//g;
998 # If there's a name left after stripping spaces and
999 # leading quotes, and the address doesn't have both
1000 # leading and trailing angle brackets, the address
1001 # is invalid. ie:
1002 # "joe smith joe@smith.com" bad
1003 # "joe smith <joe@smith.com" bad
1004 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1005 $name = "";
1006 $address = "";
1007 $comment = "";
1008 }
1009 }
1010
1011 $name = trim($name);
1012 $name =~ s/^\"|\"$//g;
1013 $address = trim($address);
1014 $address =~ s/^\<|\>$//g;
1015
1016 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1017 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1018 $name = "\"$name\"";
1019 }
1020
1021 return ($name, $address, $comment);
1022}
1023
1024sub format_email {
1025 my ($name, $address) = @_;
1026
1027 my $formatted_email;
1028
1029 $name = trim($name);
1030 $name =~ s/^\"|\"$//g;
1031 $address = trim($address);
1032
1033 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1034 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1035 $name = "\"$name\"";
1036 }
1037
1038 if ("$name" eq "") {
1039 $formatted_email = "$address";
1040 } else {
1041 $formatted_email = "$name <$address>";
1042 }
1043
1044 return $formatted_email;
1045}
1046
1047sub which {
1048 my ($bin) = @_;
1049
1050 foreach my $path (split(/:/, $ENV{PATH})) {
1051 if (-e "$path/$bin") {
1052 return "$path/$bin";
1053 }
1054 }
1055
1056 return "";
1057}
1058
1059sub which_conf {
1060 my ($conf) = @_;
1061
1062 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1063 if (-e "$path/$conf") {
1064 return "$path/$conf";
1065 }
1066 }
1067
1068 return "";
1069}
1070
1071sub expand_tabs {
1072 my ($str) = @_;
1073
1074 my $res = '';
1075 my $n = 0;
1076 for my $c (split(//, $str)) {
1077 if ($c eq "\t") {
1078 $res .= ' ';
1079 $n++;
1080 for (; ($n % 8) != 0; $n++) {
1081 $res .= ' ';
1082 }
1083 next;
1084 }
1085 $res .= $c;
1086 $n++;
1087 }
1088
1089 return $res;
1090}
1091sub copy_spacing {
1092 (my $res = shift) =~ tr/\t/ /c;
1093 return $res;
1094}
1095
1096sub line_stats {
1097 my ($line) = @_;
1098
1099 # Drop the diff line leader and expand tabs
1100 $line =~ s/^.//;
1101 $line = expand_tabs($line);
1102
1103 # Pick the indent from the front of the line.
1104 my ($white) = ($line =~ /^(\s*)/);
1105
1106 return (length($line), length($white));
1107}
1108
1109my $sanitise_quote = '';
1110
1111sub sanitise_line_reset {
1112 my ($in_comment) = @_;
1113
1114 if ($in_comment) {
1115 $sanitise_quote = '*/';
1116 } else {
1117 $sanitise_quote = '';
1118 }
1119}
1120sub sanitise_line {
1121 my ($line) = @_;
1122
1123 my $res = '';
1124 my $l = '';
1125
1126 my $qlen = 0;
1127 my $off = 0;
1128 my $c;
1129
1130 # Always copy over the diff marker.
1131 $res = substr($line, 0, 1);
1132
1133 for ($off = 1; $off < length($line); $off++) {
1134 $c = substr($line, $off, 1);
1135
1136 # Comments we are wacking completly including the begin
1137 # and end, all to $;.
1138 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1139 $sanitise_quote = '*/';
1140
1141 substr($res, $off, 2, "$;$;");
1142 $off++;
1143 next;
1144 }
1145 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1146 $sanitise_quote = '';
1147 substr($res, $off, 2, "$;$;");
1148 $off++;
1149 next;
1150 }
1151 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1152 $sanitise_quote = '//';
1153
1154 substr($res, $off, 2, $sanitise_quote);
1155 $off++;
1156 next;
1157 }
1158
1159 # A \ in a string means ignore the next character.
1160 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1161 $c eq "\\") {
1162 substr($res, $off, 2, 'XX');
1163 $off++;
1164 next;
1165 }
1166 # Regular quotes.
1167 if ($c eq "'" || $c eq '"') {
1168 if ($sanitise_quote eq '') {
1169 $sanitise_quote = $c;
1170
1171 substr($res, $off, 1, $c);
1172 next;
1173 } elsif ($sanitise_quote eq $c) {
1174 $sanitise_quote = '';
1175 }
1176 }
1177
1178 #print "c<$c> SQ<$sanitise_quote>\n";
1179 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1180 substr($res, $off, 1, $;);
1181 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1182 substr($res, $off, 1, $;);
1183 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1184 substr($res, $off, 1, 'X');
1185 } else {
1186 substr($res, $off, 1, $c);
1187 }
1188 }
1189
1190 if ($sanitise_quote eq '//') {
1191 $sanitise_quote = '';
1192 }
1193
1194 # The pathname on a #include may be surrounded by '<' and '>'.
1195 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1196 my $clean = 'X' x length($1);
1197 $res =~ s@\<.*\>@<$clean>@;
1198
1199 # The whole of a #error is a string.
1200 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1201 my $clean = 'X' x length($1);
1202 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1203 }
1204
Anas Nashif0f3d5472016-11-07 15:57:57 -05001205 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1206 my $match = $1;
1207 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1208 }
1209
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001210 return $res;
1211}
1212
1213sub get_quoted_string {
1214 my ($line, $rawline) = @_;
1215
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001216 return "" if ($line !~ m/($String)/g);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001217 return substr($rawline, $-[0], $+[0] - $-[0]);
1218}
1219
1220sub ctx_statement_block {
1221 my ($linenr, $remain, $off) = @_;
1222 my $line = $linenr - 1;
1223 my $blk = '';
1224 my $soff = $off;
1225 my $coff = $off - 1;
1226 my $coff_set = 0;
1227
1228 my $loff = 0;
1229
1230 my $type = '';
1231 my $level = 0;
1232 my @stack = ();
1233 my $p;
1234 my $c;
1235 my $len = 0;
1236
1237 my $remainder;
1238 while (1) {
1239 @stack = (['', 0]) if ($#stack == -1);
1240
1241 #warn "CSB: blk<$blk> remain<$remain>\n";
1242 # If we are about to drop off the end, pull in more
1243 # context.
1244 if ($off >= $len) {
1245 for (; $remain > 0; $line++) {
1246 last if (!defined $lines[$line]);
1247 next if ($lines[$line] =~ /^-/);
1248 $remain--;
1249 $loff = $len;
1250 $blk .= $lines[$line] . "\n";
1251 $len = length($blk);
1252 $line++;
1253 last;
1254 }
1255 # Bail if there is no further context.
1256 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1257 if ($off >= $len) {
1258 last;
1259 }
1260 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1261 $level++;
1262 $type = '#';
1263 }
1264 }
1265 $p = $c;
1266 $c = substr($blk, $off, 1);
1267 $remainder = substr($blk, $off);
1268
1269 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1270
1271 # Handle nested #if/#else.
1272 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1273 push(@stack, [ $type, $level ]);
1274 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1275 ($type, $level) = @{$stack[$#stack - 1]};
1276 } elsif ($remainder =~ /^#\s*endif\b/) {
1277 ($type, $level) = @{pop(@stack)};
1278 }
1279
1280 # Statement ends at the ';' or a close '}' at the
1281 # outermost level.
1282 if ($level == 0 && $c eq ';') {
1283 last;
1284 }
1285
1286 # An else is really a conditional as long as its not else if
1287 if ($level == 0 && $coff_set == 0 &&
1288 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1289 $remainder =~ /^(else)(?:\s|{)/ &&
1290 $remainder !~ /^else\s+if\b/) {
1291 $coff = $off + length($1) - 1;
1292 $coff_set = 1;
1293 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1294 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1295 }
1296
1297 if (($type eq '' || $type eq '(') && $c eq '(') {
1298 $level++;
1299 $type = '(';
1300 }
1301 if ($type eq '(' && $c eq ')') {
1302 $level--;
1303 $type = ($level != 0)? '(' : '';
1304
1305 if ($level == 0 && $coff < $soff) {
1306 $coff = $off;
1307 $coff_set = 1;
1308 #warn "CSB: mark coff<$coff>\n";
1309 }
1310 }
1311 if (($type eq '' || $type eq '{') && $c eq '{') {
1312 $level++;
1313 $type = '{';
1314 }
1315 if ($type eq '{' && $c eq '}') {
1316 $level--;
1317 $type = ($level != 0)? '{' : '';
1318
1319 if ($level == 0) {
1320 if (substr($blk, $off + 1, 1) eq ';') {
1321 $off++;
1322 }
1323 last;
1324 }
1325 }
1326 # Preprocessor commands end at the newline unless escaped.
1327 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1328 $level--;
1329 $type = '';
1330 $off++;
1331 last;
1332 }
1333 $off++;
1334 }
1335 # We are truly at the end, so shuffle to the next line.
1336 if ($off == $len) {
1337 $loff = $len + 1;
1338 $line++;
1339 $remain--;
1340 }
1341
1342 my $statement = substr($blk, $soff, $off - $soff + 1);
1343 my $condition = substr($blk, $soff, $coff - $soff + 1);
1344
1345 #warn "STATEMENT<$statement>\n";
1346 #warn "CONDITION<$condition>\n";
1347
1348 #print "coff<$coff> soff<$off> loff<$loff>\n";
1349
1350 return ($statement, $condition,
1351 $line, $remain + 1, $off - $loff + 1, $level);
1352}
1353
1354sub statement_lines {
1355 my ($stmt) = @_;
1356
1357 # Strip the diff line prefixes and rip blank lines at start and end.
1358 $stmt =~ s/(^|\n)./$1/g;
1359 $stmt =~ s/^\s*//;
1360 $stmt =~ s/\s*$//;
1361
1362 my @stmt_lines = ($stmt =~ /\n/g);
1363
1364 return $#stmt_lines + 2;
1365}
1366
1367sub statement_rawlines {
1368 my ($stmt) = @_;
1369
1370 my @stmt_lines = ($stmt =~ /\n/g);
1371
1372 return $#stmt_lines + 2;
1373}
1374
1375sub statement_block_size {
1376 my ($stmt) = @_;
1377
1378 $stmt =~ s/(^|\n)./$1/g;
1379 $stmt =~ s/^\s*{//;
1380 $stmt =~ s/}\s*$//;
1381 $stmt =~ s/^\s*//;
1382 $stmt =~ s/\s*$//;
1383
1384 my @stmt_lines = ($stmt =~ /\n/g);
1385 my @stmt_statements = ($stmt =~ /;/g);
1386
1387 my $stmt_lines = $#stmt_lines + 2;
1388 my $stmt_statements = $#stmt_statements + 1;
1389
1390 if ($stmt_lines > $stmt_statements) {
1391 return $stmt_lines;
1392 } else {
1393 return $stmt_statements;
1394 }
1395}
1396
1397sub ctx_statement_full {
1398 my ($linenr, $remain, $off) = @_;
1399 my ($statement, $condition, $level);
1400
1401 my (@chunks);
1402
1403 # Grab the first conditional/block pair.
1404 ($statement, $condition, $linenr, $remain, $off, $level) =
1405 ctx_statement_block($linenr, $remain, $off);
1406 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1407 push(@chunks, [ $condition, $statement ]);
1408 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1409 return ($level, $linenr, @chunks);
1410 }
1411
1412 # Pull in the following conditional/block pairs and see if they
1413 # could continue the statement.
1414 for (;;) {
1415 ($statement, $condition, $linenr, $remain, $off, $level) =
1416 ctx_statement_block($linenr, $remain, $off);
1417 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1418 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1419 #print "C: push\n";
1420 push(@chunks, [ $condition, $statement ]);
1421 }
1422
1423 return ($level, $linenr, @chunks);
1424}
1425
1426sub ctx_block_get {
1427 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1428 my $line;
1429 my $start = $linenr - 1;
1430 my $blk = '';
1431 my @o;
1432 my @c;
1433 my @res = ();
1434
1435 my $level = 0;
1436 my @stack = ($level);
1437 for ($line = $start; $remain > 0; $line++) {
1438 next if ($rawlines[$line] =~ /^-/);
1439 $remain--;
1440
1441 $blk .= $rawlines[$line];
1442
1443 # Handle nested #if/#else.
1444 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1445 push(@stack, $level);
1446 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1447 $level = $stack[$#stack - 1];
1448 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1449 $level = pop(@stack);
1450 }
1451
1452 foreach my $c (split(//, $lines[$line])) {
1453 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1454 if ($off > 0) {
1455 $off--;
1456 next;
1457 }
1458
1459 if ($c eq $close && $level > 0) {
1460 $level--;
1461 last if ($level == 0);
1462 } elsif ($c eq $open) {
1463 $level++;
1464 }
1465 }
1466
1467 if (!$outer || $level <= 1) {
1468 push(@res, $rawlines[$line]);
1469 }
1470
1471 last if ($level == 0);
1472 }
1473
1474 return ($level, @res);
1475}
1476sub ctx_block_outer {
1477 my ($linenr, $remain) = @_;
1478
1479 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1480 return @r;
1481}
1482sub ctx_block {
1483 my ($linenr, $remain) = @_;
1484
1485 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1486 return @r;
1487}
1488sub ctx_statement {
1489 my ($linenr, $remain, $off) = @_;
1490
1491 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1492 return @r;
1493}
1494sub ctx_block_level {
1495 my ($linenr, $remain) = @_;
1496
1497 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1498}
1499sub ctx_statement_level {
1500 my ($linenr, $remain, $off) = @_;
1501
1502 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1503}
1504
1505sub ctx_locate_comment {
1506 my ($first_line, $end_line) = @_;
1507
1508 # Catch a comment on the end of the line itself.
1509 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1510 return $current_comment if (defined $current_comment);
1511
1512 # Look through the context and try and figure out if there is a
1513 # comment.
1514 my $in_comment = 0;
1515 $current_comment = '';
1516 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1517 my $line = $rawlines[$linenr - 1];
1518 #warn " $line\n";
1519 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1520 $in_comment = 1;
1521 }
1522 if ($line =~ m@/\*@) {
1523 $in_comment = 1;
1524 }
1525 if (!$in_comment && $current_comment ne '') {
1526 $current_comment = '';
1527 }
1528 $current_comment .= $line . "\n" if ($in_comment);
1529 if ($line =~ m@\*/@) {
1530 $in_comment = 0;
1531 }
1532 }
1533
1534 chomp($current_comment);
1535 return($current_comment);
1536}
1537sub ctx_has_comment {
1538 my ($first_line, $end_line) = @_;
1539 my $cmt = ctx_locate_comment($first_line, $end_line);
1540
1541 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1542 ##print "CMMT: $cmt\n";
1543
1544 return ($cmt ne '');
1545}
1546
1547sub raw_line {
1548 my ($linenr, $cnt) = @_;
1549
1550 my $offset = $linenr - 1;
1551 $cnt++;
1552
1553 my $line;
1554 while ($cnt) {
1555 $line = $rawlines[$offset++];
1556 next if (defined($line) && $line =~ /^-/);
1557 $cnt--;
1558 }
1559
1560 return $line;
1561}
1562
1563sub cat_vet {
1564 my ($vet) = @_;
1565 my ($res, $coded);
1566
1567 $res = '';
1568 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1569 $res .= $1;
1570 if ($2 ne '') {
1571 $coded = sprintf("^%c", unpack('C', $2) + 64);
1572 $res .= $coded;
1573 }
1574 }
1575 $res =~ s/$/\$/;
1576
1577 return $res;
1578}
1579
1580my $av_preprocessor = 0;
1581my $av_pending;
1582my @av_paren_type;
1583my $av_pend_colon;
1584
1585sub annotate_reset {
1586 $av_preprocessor = 0;
1587 $av_pending = '_';
1588 @av_paren_type = ('E');
1589 $av_pend_colon = 'O';
1590}
1591
1592sub annotate_values {
1593 my ($stream, $type) = @_;
1594
1595 my $res;
1596 my $var = '_' x length($stream);
1597 my $cur = $stream;
1598
1599 print "$stream\n" if ($dbg_values > 1);
1600
1601 while (length($cur)) {
1602 @av_paren_type = ('E') if ($#av_paren_type < 0);
1603 print " <" . join('', @av_paren_type) .
1604 "> <$type> <$av_pending>" if ($dbg_values > 1);
1605 if ($cur =~ /^(\s+)/o) {
1606 print "WS($1)\n" if ($dbg_values > 1);
1607 if ($1 =~ /\n/ && $av_preprocessor) {
1608 $type = pop(@av_paren_type);
1609 $av_preprocessor = 0;
1610 }
1611
1612 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1613 print "CAST($1)\n" if ($dbg_values > 1);
1614 push(@av_paren_type, $type);
1615 $type = 'c';
1616
1617 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1618 print "DECLARE($1)\n" if ($dbg_values > 1);
1619 $type = 'T';
1620
1621 } elsif ($cur =~ /^($Modifier)\s*/) {
1622 print "MODIFIER($1)\n" if ($dbg_values > 1);
1623 $type = 'T';
1624
1625 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1626 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1627 $av_preprocessor = 1;
1628 push(@av_paren_type, $type);
1629 if ($2 ne '') {
1630 $av_pending = 'N';
1631 }
1632 $type = 'E';
1633
1634 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1635 print "UNDEF($1)\n" if ($dbg_values > 1);
1636 $av_preprocessor = 1;
1637 push(@av_paren_type, $type);
1638
1639 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1640 print "PRE_START($1)\n" if ($dbg_values > 1);
1641 $av_preprocessor = 1;
1642
1643 push(@av_paren_type, $type);
1644 push(@av_paren_type, $type);
1645 $type = 'E';
1646
1647 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1648 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1649 $av_preprocessor = 1;
1650
1651 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1652
1653 $type = 'E';
1654
1655 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1656 print "PRE_END($1)\n" if ($dbg_values > 1);
1657
1658 $av_preprocessor = 1;
1659
1660 # Assume all arms of the conditional end as this
1661 # one does, and continue as if the #endif was not here.
1662 pop(@av_paren_type);
1663 push(@av_paren_type, $type);
1664 $type = 'E';
1665
1666 } elsif ($cur =~ /^(\\\n)/o) {
1667 print "PRECONT($1)\n" if ($dbg_values > 1);
1668
1669 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1670 print "ATTR($1)\n" if ($dbg_values > 1);
1671 $av_pending = $type;
1672 $type = 'N';
1673
1674 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1675 print "SIZEOF($1)\n" if ($dbg_values > 1);
1676 if (defined $2) {
1677 $av_pending = 'V';
1678 }
1679 $type = 'N';
1680
1681 } elsif ($cur =~ /^(if|while|for)\b/o) {
1682 print "COND($1)\n" if ($dbg_values > 1);
1683 $av_pending = 'E';
1684 $type = 'N';
1685
1686 } elsif ($cur =~/^(case)/o) {
1687 print "CASE($1)\n" if ($dbg_values > 1);
1688 $av_pend_colon = 'C';
1689 $type = 'N';
1690
1691 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1692 print "KEYWORD($1)\n" if ($dbg_values > 1);
1693 $type = 'N';
1694
1695 } elsif ($cur =~ /^(\()/o) {
1696 print "PAREN('$1')\n" if ($dbg_values > 1);
1697 push(@av_paren_type, $av_pending);
1698 $av_pending = '_';
1699 $type = 'N';
1700
1701 } elsif ($cur =~ /^(\))/o) {
1702 my $new_type = pop(@av_paren_type);
1703 if ($new_type ne '_') {
1704 $type = $new_type;
1705 print "PAREN('$1') -> $type\n"
1706 if ($dbg_values > 1);
1707 } else {
1708 print "PAREN('$1')\n" if ($dbg_values > 1);
1709 }
1710
1711 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1712 print "FUNC($1)\n" if ($dbg_values > 1);
1713 $type = 'V';
1714 $av_pending = 'V';
1715
1716 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1717 if (defined $2 && $type eq 'C' || $type eq 'T') {
1718 $av_pend_colon = 'B';
1719 } elsif ($type eq 'E') {
1720 $av_pend_colon = 'L';
1721 }
1722 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1723 $type = 'V';
1724
1725 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1726 print "IDENT($1)\n" if ($dbg_values > 1);
1727 $type = 'V';
1728
1729 } elsif ($cur =~ /^($Assignment)/o) {
1730 print "ASSIGN($1)\n" if ($dbg_values > 1);
1731 $type = 'N';
1732
1733 } elsif ($cur =~/^(;|{|})/) {
1734 print "END($1)\n" if ($dbg_values > 1);
1735 $type = 'E';
1736 $av_pend_colon = 'O';
1737
1738 } elsif ($cur =~/^(,)/) {
1739 print "COMMA($1)\n" if ($dbg_values > 1);
1740 $type = 'C';
1741
1742 } elsif ($cur =~ /^(\?)/o) {
1743 print "QUESTION($1)\n" if ($dbg_values > 1);
1744 $type = 'N';
1745
1746 } elsif ($cur =~ /^(:)/o) {
1747 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1748
1749 substr($var, length($res), 1, $av_pend_colon);
1750 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1751 $type = 'E';
1752 } else {
1753 $type = 'N';
1754 }
1755 $av_pend_colon = 'O';
1756
1757 } elsif ($cur =~ /^(\[)/o) {
1758 print "CLOSE($1)\n" if ($dbg_values > 1);
1759 $type = 'N';
1760
1761 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1762 my $variant;
1763
1764 print "OPV($1)\n" if ($dbg_values > 1);
1765 if ($type eq 'V') {
1766 $variant = 'B';
1767 } else {
1768 $variant = 'U';
1769 }
1770
1771 substr($var, length($res), 1, $variant);
1772 $type = 'N';
1773
1774 } elsif ($cur =~ /^($Operators)/o) {
1775 print "OP($1)\n" if ($dbg_values > 1);
1776 if ($1 ne '++' && $1 ne '--') {
1777 $type = 'N';
1778 }
1779
1780 } elsif ($cur =~ /(^.)/o) {
1781 print "C($1)\n" if ($dbg_values > 1);
1782 }
1783 if (defined $1) {
1784 $cur = substr($cur, length($1));
1785 $res .= $type x length($1);
1786 }
1787 }
1788
1789 return ($res, $var);
1790}
1791
1792sub possible {
1793 my ($possible, $line) = @_;
1794 my $notPermitted = qr{(?:
1795 ^(?:
1796 $Modifier|
1797 $Storage|
1798 $Type|
1799 DEFINE_\S+
1800 )$|
1801 ^(?:
1802 goto|
1803 return|
1804 case|
1805 else|
1806 asm|__asm__|
1807 do|
1808 \#|
1809 \#\#|
1810 )(?:\s|$)|
1811 ^(?:typedef|struct|enum)\b
1812 )}x;
1813 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1814 if ($possible !~ $notPermitted) {
1815 # Check for modifiers.
1816 $possible =~ s/\s*$Storage\s*//g;
1817 $possible =~ s/\s*$Sparse\s*//g;
1818 if ($possible =~ /^\s*$/) {
1819
1820 } elsif ($possible =~ /\s/) {
1821 $possible =~ s/\s*$Type\s*//g;
1822 for my $modifier (split(' ', $possible)) {
1823 if ($modifier !~ $notPermitted) {
1824 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001825 push(@modifierListFile, $modifier);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001826 }
1827 }
1828
1829 } else {
1830 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001831 push(@typeListFile, $possible);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001832 }
1833 build_types();
1834 } else {
1835 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1836 }
1837}
1838
1839my $prefix = '';
1840
1841sub show_type {
1842 my ($type) = @_;
1843
1844 return defined $use_type{$type} if (scalar keys %use_type > 0);
1845
1846 return !defined $ignore_type{$type};
1847}
1848
1849sub report {
1850 my ($level, $type, $msg) = @_;
1851
1852 if (!show_type($type) ||
1853 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1854 return 0;
1855 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001856 my $output = '';
1857 if (-t STDOUT && $color) {
1858 if ($level eq 'ERROR') {
1859 $output .= RED;
1860 } elsif ($level eq 'WARNING') {
1861 $output .= YELLOW;
1862 } else {
1863 $output .= GREEN;
1864 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001865 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001866 $output .= $prefix . $level . ':';
1867 if ($show_types) {
1868 $output .= BLUE if (-t STDOUT && $color);
1869 $output .= "$type:";
1870 }
1871 $output .= RESET if (-t STDOUT && $color);
1872 $output .= ' ' . $msg . "\n";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001873
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001874 if ($showfile) {
1875 my @lines = split("\n", $output, -1);
1876 splice(@lines, 1, 1);
1877 $output = join("\n", @lines);
1878 }
1879 $output = (split('\n', $output))[0] . "\n" if ($terse);
1880
1881 push(our @report, $output);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001882
1883 return 1;
1884}
1885
1886sub report_dump {
1887 our @report;
1888}
1889
1890sub fixup_current_range {
1891 my ($lineRef, $offset, $length) = @_;
1892
1893 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1894 my $o = $1;
1895 my $l = $2;
1896 my $no = $o + $offset;
1897 my $nl = $l + $length;
1898 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1899 }
1900}
1901
1902sub fix_inserted_deleted_lines {
1903 my ($linesRef, $insertedRef, $deletedRef) = @_;
1904
1905 my $range_last_linenr = 0;
1906 my $delta_offset = 0;
1907
1908 my $old_linenr = 0;
1909 my $new_linenr = 0;
1910
1911 my $next_insert = 0;
1912 my $next_delete = 0;
1913
1914 my @lines = ();
1915
1916 my $inserted = @{$insertedRef}[$next_insert++];
1917 my $deleted = @{$deletedRef}[$next_delete++];
1918
1919 foreach my $old_line (@{$linesRef}) {
1920 my $save_line = 1;
1921 my $line = $old_line; #don't modify the array
Anas Nashifcbfb62c2015-10-14 06:08:54 -04001922 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001923 $delta_offset = 0;
1924 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1925 $range_last_linenr = $new_linenr;
1926 fixup_current_range(\$line, $delta_offset, 0);
1927 }
1928
1929 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1930 $deleted = @{$deletedRef}[$next_delete++];
1931 $save_line = 0;
1932 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1933 }
1934
1935 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1936 push(@lines, ${$inserted}{'LINE'});
1937 $inserted = @{$insertedRef}[$next_insert++];
1938 $new_linenr++;
1939 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1940 }
1941
1942 if ($save_line) {
1943 push(@lines, $line);
1944 $new_linenr++;
1945 }
1946
1947 $old_linenr++;
1948 }
1949
1950 return @lines;
1951}
1952
1953sub fix_insert_line {
1954 my ($linenr, $line) = @_;
1955
1956 my $inserted = {
1957 LINENR => $linenr,
1958 LINE => $line,
1959 };
1960 push(@fixed_inserted, $inserted);
1961}
1962
1963sub fix_delete_line {
1964 my ($linenr, $line) = @_;
1965
1966 my $deleted = {
1967 LINENR => $linenr,
1968 LINE => $line,
1969 };
1970
1971 push(@fixed_deleted, $deleted);
1972}
1973
1974sub ERROR {
1975 my ($type, $msg) = @_;
1976
1977 if (report("ERROR", $type, $msg)) {
1978 our $clean = 0;
1979 our $cnt_error++;
1980 return 1;
1981 }
1982 return 0;
1983}
1984sub WARN {
1985 my ($type, $msg) = @_;
1986
1987 if (report("WARNING", $type, $msg)) {
1988 our $clean = 0;
1989 our $cnt_warn++;
1990 return 1;
1991 }
1992 return 0;
1993}
1994sub CHK {
1995 my ($type, $msg) = @_;
1996
1997 if ($check && report("CHECK", $type, $msg)) {
1998 our $clean = 0;
1999 our $cnt_chk++;
2000 return 1;
2001 }
2002 return 0;
2003}
2004
2005sub check_absolute_file {
2006 my ($absolute, $herecurr) = @_;
2007 my $file = $absolute;
2008
2009 ##print "absolute<$absolute>\n";
2010
2011 # See if any suffix of this path is a path within the tree.
2012 while ($file =~ s@^[^/]*/@@) {
2013 if (-f "$root/$file") {
2014 ##print "file<$file>\n";
2015 last;
2016 }
2017 }
2018 if (! -f _) {
2019 return 0;
2020 }
2021
2022 # It is, so see if the prefix is acceptable.
2023 my $prefix = $absolute;
2024 substr($prefix, -length($file)) = '';
2025
2026 ##print "prefix<$prefix>\n";
2027 if ($prefix ne ".../") {
2028 WARN("USE_RELATIVE_PATH",
2029 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2030 }
2031}
2032
2033sub trim {
2034 my ($string) = @_;
2035
2036 $string =~ s/^\s+|\s+$//g;
2037
2038 return $string;
2039}
2040
2041sub ltrim {
2042 my ($string) = @_;
2043
2044 $string =~ s/^\s+//;
2045
2046 return $string;
2047}
2048
2049sub rtrim {
2050 my ($string) = @_;
2051
2052 $string =~ s/\s+$//;
2053
2054 return $string;
2055}
2056
2057sub string_find_replace {
2058 my ($string, $find, $replace) = @_;
2059
2060 $string =~ s/$find/$replace/g;
2061
2062 return $string;
2063}
2064
2065sub tabify {
2066 my ($leading) = @_;
2067
2068 my $source_indent = 8;
2069 my $max_spaces_before_tab = $source_indent - 1;
2070 my $spaces_to_tab = " " x $source_indent;
2071
2072 #convert leading spaces to tabs
2073 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2074 #Remove spaces before a tab
2075 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2076
2077 return "$leading";
2078}
2079
2080sub pos_last_openparen {
2081 my ($line) = @_;
2082
2083 my $pos = 0;
2084
2085 my $opens = $line =~ tr/\(/\(/;
2086 my $closes = $line =~ tr/\)/\)/;
2087
2088 my $last_openparen = 0;
2089
2090 if (($opens == 0) || ($closes >= $opens)) {
2091 return -1;
2092 }
2093
2094 my $len = length($line);
2095
2096 for ($pos = 0; $pos < $len; $pos++) {
2097 my $string = substr($line, $pos);
2098 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2099 $pos += length($1) - 1;
2100 } elsif (substr($line, $pos, 1) eq '(') {
2101 $last_openparen = $pos;
2102 } elsif (index($string, '(') == -1) {
2103 last;
2104 }
2105 }
2106
2107 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2108}
2109
2110sub process {
2111 my $filename = shift;
2112
2113 my $linenr=0;
2114 my $prevline="";
2115 my $prevrawline="";
2116 my $stashline="";
2117 my $stashrawline="";
2118
2119 my $length;
2120 my $indent;
2121 my $previndent=0;
2122 my $stashindent=0;
2123
2124 our $clean = 1;
2125 my $signoff = 0;
2126 my $is_patch = 0;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002127 my $in_header_lines = $file ? 0 : 1;
2128 my $in_commit_log = 0; #Scanning lines before patch
Anas Nashif0f3d5472016-11-07 15:57:57 -05002129 my $has_commit_log = 0; #Encountered lines before patch
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002130 my $commit_log_possible_stack_dump = 0;
2131 my $commit_log_long_line = 0;
2132 my $commit_log_has_diff = 0;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002133 my $reported_maintainer_file = 0;
2134 my $non_utf8_charset = 0;
2135
2136 my $last_blank_line = 0;
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002137 my $last_coalesced_string_linenr = -1;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002138
2139 our @report = ();
2140 our $cnt_lines = 0;
2141 our $cnt_error = 0;
2142 our $cnt_warn = 0;
2143 our $cnt_chk = 0;
2144
2145 # Trace the real file/line as we go.
2146 my $realfile = '';
2147 my $realline = 0;
2148 my $realcnt = 0;
2149 my $here = '';
2150 my $in_comment = 0;
2151 my $comment_edge = 0;
2152 my $first_line = 0;
2153 my $p1_prefix = '';
2154
2155 my $prev_values = 'E';
2156
2157 # suppression flags
2158 my %suppress_ifbraces;
2159 my %suppress_whiletrailers;
2160 my %suppress_export;
2161 my $suppress_statement = 0;
2162
2163 my %signatures = ();
2164
2165 # Pre-scan the patch sanitizing the lines.
2166 # Pre-scan the patch looking for any __setup documentation.
2167 #
2168 my @setup_docs = ();
2169 my $setup_docs = 0;
2170
2171 my $camelcase_file_seeded = 0;
2172
2173 sanitise_line_reset();
2174 my $line;
2175 foreach my $rawline (@rawlines) {
2176 $linenr++;
2177 $line = $rawline;
2178
2179 push(@fixed, $rawline) if ($fix);
2180
2181 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2182 $setup_docs = 0;
2183 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2184 $setup_docs = 1;
2185 }
2186 #next;
2187 }
2188 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2189 $realline=$1-1;
2190 if (defined $2) {
2191 $realcnt=$3+1;
2192 } else {
2193 $realcnt=1+1;
2194 }
2195 $in_comment = 0;
2196
2197 # Guestimate if this is a continuing comment. Run
2198 # the context looking for a comment "edge". If this
2199 # edge is a close comment then we must be in a comment
2200 # at context start.
2201 my $edge;
2202 my $cnt = $realcnt;
2203 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2204 next if (defined $rawlines[$ln - 1] &&
2205 $rawlines[$ln - 1] =~ /^-/);
2206 $cnt--;
2207 #print "RAW<$rawlines[$ln - 1]>\n";
2208 last if (!defined $rawlines[$ln - 1]);
2209 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2210 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2211 ($edge) = $1;
2212 last;
2213 }
2214 }
2215 if (defined $edge && $edge eq '*/') {
2216 $in_comment = 1;
2217 }
2218
2219 # Guestimate if this is a continuing comment. If this
2220 # is the start of a diff block and this line starts
2221 # ' *' then it is very likely a comment.
2222 if (!defined $edge &&
2223 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2224 {
2225 $in_comment = 1;
2226 }
2227
2228 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2229 sanitise_line_reset($in_comment);
2230
2231 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2232 # Standardise the strings and chars within the input to
2233 # simplify matching -- only bother with positive lines.
2234 $line = sanitise_line($rawline);
2235 }
2236 push(@lines, $line);
2237
2238 if ($realcnt > 1) {
2239 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2240 } else {
2241 $realcnt = 0;
2242 }
2243
2244 #print "==>$rawline\n";
2245 #print "-->$line\n";
2246
2247 if ($setup_docs && $line =~ /^\+/) {
2248 push(@setup_docs, $line);
2249 }
2250 }
2251
2252 $prefix = '';
2253
2254 $realcnt = 0;
2255 $linenr = 0;
2256 $fixlinenr = -1;
2257 foreach my $line (@lines) {
2258 $linenr++;
2259 $fixlinenr++;
2260 my $sline = $line; #copy of $line
2261 $sline =~ s/$;/ /g; #with comments as spaces
2262
2263 my $rawline = $rawlines[$linenr - 1];
2264
2265#extract the line range in the file after the patch is applied
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002266 if (!$in_commit_log &&
2267 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002268 $is_patch = 1;
2269 $first_line = $linenr + 1;
2270 $realline=$1-1;
2271 if (defined $2) {
2272 $realcnt=$3+1;
2273 } else {
2274 $realcnt=1+1;
2275 }
2276 annotate_reset();
2277 $prev_values = 'E';
2278
2279 %suppress_ifbraces = ();
2280 %suppress_whiletrailers = ();
2281 %suppress_export = ();
2282 $suppress_statement = 0;
2283 next;
2284
2285# track the line number as we move through the hunk, note that
2286# new versions of GNU diff omit the leading space on completely
2287# blank context lines so we need to count that too.
2288 } elsif ($line =~ /^( |\+|$)/) {
2289 $realline++;
2290 $realcnt-- if ($realcnt != 0);
2291
2292 # Measure the line length and indent.
2293 ($length, $indent) = line_stats($rawline);
2294
2295 # Track the previous line.
2296 ($prevline, $stashline) = ($stashline, $line);
2297 ($previndent, $stashindent) = ($stashindent, $indent);
2298 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2299
2300 #warn "line<$line>\n";
2301
2302 } elsif ($realcnt == 1) {
2303 $realcnt--;
2304 }
2305
2306 my $hunk_line = ($realcnt != 0);
2307
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002308 $here = "#$linenr: " if (!$file);
2309 $here = "#$realline: " if ($file);
2310
2311 my $found_file = 0;
2312 # extract the filename as it passes
2313 if ($line =~ /^diff --git.*?(\S+)$/) {
2314 $realfile = $1;
2315 $realfile =~ s@^([^/]*)/@@ if (!$file);
2316 $in_commit_log = 0;
2317 $found_file = 1;
2318 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2319 $realfile = $1;
2320 $realfile =~ s@^([^/]*)/@@ if (!$file);
2321 $in_commit_log = 0;
2322
2323 $p1_prefix = $1;
2324 if (!$file && $tree && $p1_prefix ne '' &&
2325 -e "$root/$p1_prefix") {
2326 WARN("PATCH_PREFIX",
2327 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2328 }
2329
2330 if ($realfile =~ m@^include/asm/@) {
2331 ERROR("MODIFIED_INCLUDE_ASM",
2332 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2333 }
2334 $found_file = 1;
2335 }
Anas Nashif92a12a12016-05-20 18:26:59 -04002336 my $skipme = 0;
2337 foreach (@exclude) {
2338 if ($realfile =~ m@^(?:$_/)@) {
2339 $skipme = 1;
2340 }
2341 }
2342 if ($skipme) {
2343 next;
2344 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002345
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002346#make up the handle for any error we report on this line
2347 if ($showfile) {
2348 $prefix = "$realfile:$realline: "
2349 } elsif ($emacs) {
2350 if ($file) {
2351 $prefix = "$filename:$realline: ";
2352 } else {
2353 $prefix = "$filename:$linenr: ";
2354 }
2355 }
2356
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002357 if ($found_file) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002358 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002359 $check = 1;
2360 } else {
2361 $check = $check_orig;
2362 }
2363 next;
2364 }
2365
2366 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2367
2368 my $hereline = "$here\n$rawline\n";
2369 my $herecurr = "$here\n$rawline\n";
2370 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2371
2372 $cnt_lines++ if ($realcnt != 0);
2373
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002374# Check if the commit log has what seems like a diff which can confuse patch
2375 if ($in_commit_log && !$commit_log_has_diff &&
2376 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2377 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2378 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2379 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2380 ERROR("DIFF_IN_COMMIT_MSG",
2381 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2382 $commit_log_has_diff = 1;
2383 }
2384
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002385# Check for incorrect file permissions
2386 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2387 my $permhere = $here . "FILE: $realfile\n";
2388 if ($realfile !~ m@scripts/@ &&
2389 $realfile !~ /\.(py|pl|awk|sh)$/) {
2390 ERROR("EXECUTE_PERMISSIONS",
2391 "do not set execute permissions for source files\n" . $permhere);
2392 }
2393 }
2394
2395# Check the patch for a signoff:
2396 if ($line =~ /^\s*signed-off-by:/i) {
2397 $signoff++;
2398 $in_commit_log = 0;
2399 }
2400
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002401# Check if MAINTAINERS is being updated. If so, there's probably no need to
2402# emit the "does MAINTAINERS need updating?" message on file add/move/delete
Anas Nashif0f3d5472016-11-07 15:57:57 -05002403 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002404 $reported_maintainer_file = 1;
2405 }
2406
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002407# Check signature styles
2408 if (!$in_header_lines &&
2409 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2410 my $space_before = $1;
2411 my $sign_off = $2;
2412 my $space_after = $3;
2413 my $email = $4;
2414 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2415
2416 if ($sign_off !~ /$signature_tags/) {
2417 WARN("BAD_SIGN_OFF",
2418 "Non-standard signature: $sign_off\n" . $herecurr);
2419 }
2420 if (defined $space_before && $space_before ne "") {
2421 if (WARN("BAD_SIGN_OFF",
2422 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2423 $fix) {
2424 $fixed[$fixlinenr] =
2425 "$ucfirst_sign_off $email";
2426 }
2427 }
2428 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2429 if (WARN("BAD_SIGN_OFF",
2430 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2431 $fix) {
2432 $fixed[$fixlinenr] =
2433 "$ucfirst_sign_off $email";
2434 }
2435
2436 }
2437 if (!defined $space_after || $space_after ne " ") {
2438 if (WARN("BAD_SIGN_OFF",
2439 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2440 $fix) {
2441 $fixed[$fixlinenr] =
2442 "$ucfirst_sign_off $email";
2443 }
2444 }
2445
2446 my ($email_name, $email_address, $comment) = parse_email($email);
2447 my $suggested_email = format_email(($email_name, $email_address));
2448 if ($suggested_email eq "") {
2449 ERROR("BAD_SIGN_OFF",
2450 "Unrecognized email address: '$email'\n" . $herecurr);
2451 } else {
2452 my $dequoted = $suggested_email;
2453 $dequoted =~ s/^"//;
2454 $dequoted =~ s/" </ </;
2455 # Don't force email to have quotes
2456 # Allow just an angle bracketed address
2457 if ("$dequoted$comment" ne $email &&
2458 "<$email_address>$comment" ne $email &&
2459 "$suggested_email$comment" ne $email) {
2460 WARN("BAD_SIGN_OFF",
2461 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2462 }
2463 }
2464
2465# Check for duplicate signatures
2466 my $sig_nospace = $line;
2467 $sig_nospace =~ s/\s//g;
2468 $sig_nospace = lc($sig_nospace);
2469 if (defined $signatures{$sig_nospace}) {
2470 WARN("BAD_SIGN_OFF",
2471 "Duplicate signature\n" . $herecurr);
2472 } else {
2473 $signatures{$sig_nospace} = 1;
2474 }
2475 }
2476
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002477# Check email subject for common tools that don't need to be mentioned
2478 if ($in_header_lines &&
2479 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2480 WARN("EMAIL_SUBJECT",
2481 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2482 }
2483
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002484# Check for old stable address
2485 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2486 ERROR("STABLE_ADDRESS",
2487 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2488 }
2489
Anas Nashif76b09132016-02-20 13:53:35 -05002490# Check if the commit log is in a possible stack dump
2491 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2492 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2493 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2494 # timestamp
2495 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2496 # stack dump address
2497 $commit_log_possible_stack_dump = 1;
2498 }
2499
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002500# Check for line lengths > 75 in commit log, warn once
2501 if ($in_commit_log && !$commit_log_long_line &&
Anas Nashif76b09132016-02-20 13:53:35 -05002502 length($line) > 75 &&
2503 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2504 # file delta changes
2505 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2506 # filename then :
2507 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2508 # A Fixes: or Link: line
2509 $commit_log_possible_stack_dump)) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002510 WARN("COMMIT_LOG_LONG_LINE",
2511 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2512 $commit_log_long_line = 1;
2513 }
2514
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002515# Reset possible stack dump if a blank line is found
Anas Nashif76b09132016-02-20 13:53:35 -05002516 if ($in_commit_log && $commit_log_possible_stack_dump &&
2517 $line =~ /^\s*$/) {
2518 $commit_log_possible_stack_dump = 0;
2519 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002520
2521# Check for git id commit length and improperly formed commit descriptions
Anas Nashif76b09132016-02-20 13:53:35 -05002522 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Anas Nashif0f3d5472016-11-07 15:57:57 -05002523 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002524 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Anas Nashif0f3d5472016-11-07 15:57:57 -05002525 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Anas Nashif76b09132016-02-20 13:53:35 -05002526 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2527 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002528 my $init_char = "c";
2529 my $orig_commit = "";
2530 my $short = 1;
2531 my $long = 0;
2532 my $case = 1;
2533 my $space = 1;
2534 my $hasdesc = 0;
2535 my $hasparens = 0;
2536 my $id = '0123456789ab';
2537 my $orig_desc = "commit description";
2538 my $description = "";
2539
2540 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2541 $init_char = $1;
2542 $orig_commit = lc($2);
2543 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2544 $orig_commit = lc($1);
2545 }
2546
2547 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2548 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2549 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2550 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2551 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2552 $orig_desc = $1;
2553 $hasparens = 1;
2554 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2555 defined $rawlines[$linenr] &&
2556 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2557 $orig_desc = $1;
2558 $hasparens = 1;
2559 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2560 defined $rawlines[$linenr] &&
2561 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2562 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2563 $orig_desc = $1;
2564 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2565 $orig_desc .= " " . $1;
2566 $hasparens = 1;
2567 }
2568
2569 ($id, $description) = git_commit_info($orig_commit,
2570 $id, $orig_desc);
2571
2572 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2573 ERROR("GIT_COMMIT_ID",
2574 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2575 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002576 }
2577
2578# Check for added, moved or deleted files
2579 if (!$reported_maintainer_file && !$in_commit_log &&
2580 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2581 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2582 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2583 (defined($1) || defined($2))))) {
2584 $reported_maintainer_file = 1;
2585 WARN("FILE_PATH_CHANGES",
2586 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2587 }
2588
2589# Check for wrappage within a valid hunk of the file
2590 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2591 ERROR("CORRUPTED_PATCH",
2592 "patch seems to be corrupt (line wrapped?)\n" .
2593 $herecurr) if (!$emitted_corrupt++);
2594 }
2595
2596# Check for absolute kernel paths.
2597 if ($tree) {
2598 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2599 my $file = $1;
2600
2601 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2602 check_absolute_file($1, $herecurr)) {
2603 #
2604 } else {
2605 check_absolute_file($file, $herecurr);
2606 }
2607 }
2608 }
2609
2610# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2611 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2612 $rawline !~ m/^$UTF8*$/) {
2613 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2614
2615 my $blank = copy_spacing($rawline);
2616 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2617 my $hereptr = "$hereline$ptr\n";
2618
2619 CHK("INVALID_UTF8",
2620 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2621 }
2622
2623# Check if it's the start of a commit log
2624# (not a header line and we haven't seen the patch filename)
2625 if ($in_header_lines && $realfile =~ /^$/ &&
2626 !($rawline =~ /^\s+\S/ ||
2627 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2628 $in_header_lines = 0;
2629 $in_commit_log = 1;
Anas Nashif0f3d5472016-11-07 15:57:57 -05002630 $has_commit_log = 1;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002631 }
2632
2633# Check if there is UTF-8 in a commit log when a mail header has explicitly
2634# declined it, i.e defined some charset where it is missing.
2635 if ($in_header_lines &&
2636 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2637 $1 !~ /utf-8/i) {
2638 $non_utf8_charset = 1;
2639 }
2640
2641 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2642 $rawline =~ /$NON_ASCII_UTF8/) {
2643 WARN("UTF8_BEFORE_PATCH",
2644 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2645 }
2646
2647# Check for various typo / spelling mistakes
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002648 if (defined($misspellings) &&
2649 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2650 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002651 my $typo = $1;
2652 my $typo_fix = $spelling_fix{lc($typo)};
2653 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2654 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2655 my $msg_type = \&WARN;
2656 $msg_type = \&CHK if ($file);
2657 if (&{$msg_type}("TYPO_SPELLING",
2658 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2659 $fix) {
2660 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2661 }
2662 }
2663 }
2664
2665# ignore non-hunk lines and lines being removed
2666 next if (!$hunk_line || $line =~ /^-/);
2667
2668#trailing whitespace
2669 if ($line =~ /^\+.*\015/) {
2670 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2671 if (ERROR("DOS_LINE_ENDINGS",
2672 "DOS line endings\n" . $herevet) &&
2673 $fix) {
2674 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2675 }
2676 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2677 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2678 if (ERROR("TRAILING_WHITESPACE",
2679 "trailing whitespace\n" . $herevet) &&
2680 $fix) {
2681 $fixed[$fixlinenr] =~ s/\s+$//;
2682 }
2683
2684 $rpt_cleaners = 1;
2685 }
2686
2687# Check for FSF mailing addresses.
2688 if ($rawline =~ /\bwrite to the Free/i ||
2689 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2690 $rawline =~ /\b51\s+Franklin\s+St/i) {
2691 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2692 my $msg_type = \&ERROR;
2693 $msg_type = \&CHK if ($file);
2694 &{$msg_type}("FSF_MAILING_ADDRESS",
2695 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
2696 }
2697
2698# check for Kconfig help text having a real description
2699# Only applies when adding the entry originally, after that we do not have
2700# sufficient context to determine whether it is indeed long enough.
2701 if ($realfile =~ /Kconfig/ &&
2702 $line =~ /^\+\s*config\s+/) {
2703 my $length = 0;
2704 my $cnt = $realcnt;
2705 my $ln = $linenr + 1;
2706 my $f;
2707 my $is_start = 0;
2708 my $is_end = 0;
2709 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2710 $f = $lines[$ln - 1];
2711 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2712 $is_end = $lines[$ln - 1] =~ /^\+/;
2713
2714 next if ($f =~ /^-/);
2715 last if (!$file && $f =~ /^\@\@/);
2716
2717 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2718 $is_start = 1;
2719 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2720 $length = -1;
2721 }
2722
2723 $f =~ s/^.//;
2724 $f =~ s/#.*//;
2725 $f =~ s/^\s+//;
2726 next if ($f =~ /^$/);
2727 if ($f =~ /^\s*config\s/) {
2728 $is_end = 1;
2729 last;
2730 }
2731 $length++;
2732 }
2733 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2734 WARN("CONFIG_DESCRIPTION",
2735 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2736 }
2737 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2738 }
2739
2740# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2741 if ($realfile =~ /Kconfig/ &&
2742 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2743 WARN("CONFIG_EXPERIMENTAL",
2744 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2745 }
2746
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002747# discourage the use of boolean for type definition attributes of Kconfig options
2748 if ($realfile =~ /Kconfig/ &&
2749 $line =~ /^\+\s*\bboolean\b/) {
2750 WARN("CONFIG_TYPE_BOOLEAN",
2751 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2752 }
2753
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002754 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2755 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2756 my $flag = $1;
2757 my $replacement = {
2758 'EXTRA_AFLAGS' => 'asflags-y',
2759 'EXTRA_CFLAGS' => 'ccflags-y',
2760 'EXTRA_CPPFLAGS' => 'cppflags-y',
2761 'EXTRA_LDFLAGS' => 'ldflags-y',
2762 };
2763
2764 WARN("DEPRECATED_VARIABLE",
2765 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2766 }
Javier B Perez5eecd882016-06-14 16:47:27 -05002767# Kconfig use tabs and no spaces in line
Anas Nashif34ffa152016-10-18 18:17:58 -04002768 if ($realfile =~ /Kconfig/ && $rawline =~ /^\+ /) {
Javier B Perez5eecd882016-06-14 16:47:27 -05002769 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2770 WARN("LEADING_SPACE",
2771 "please, no spaces at the start of a line\n" . $herevet);
2772 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002773
2774# check for DT compatible documentation
2775 if (defined $root &&
2776 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2777 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2778
2779 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2780
2781 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2782 my $vp_file = $dt_path . "vendor-prefixes.txt";
2783
2784 foreach my $compat (@compats) {
2785 my $compat2 = $compat;
2786 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2787 my $compat3 = $compat;
2788 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2789 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2790 if ( $? >> 8 ) {
2791 WARN("UNDOCUMENTED_DT_STRING",
2792 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2793 }
2794
2795 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2796 my $vendor = $1;
2797 `grep -Eq "^$vendor\\b" $vp_file`;
2798 if ( $? >> 8 ) {
2799 WARN("UNDOCUMENTED_DT_STRING",
2800 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2801 }
2802 }
2803 }
2804
2805# check we are in a valid source file if not then ignore this hunk
2806 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2807
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002808# line length limit (with some exclusions)
2809#
2810# There are a few types of lines that may extend beyond $max_line_length:
2811# logging functions like pr_info that end in a string
2812# lines with a single string
2813# #defines that are a single string
2814#
2815# There are 3 different line length message types:
2816# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2817# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2818# LONG_LINE all other lines longer than $max_line_length
2819#
2820# if LONG_LINE is ignored, the other 2 types are also ignored
2821#
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002822
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002823 if ($line =~ /^\+/ && $length > $max_line_length) {
2824 my $msg_type = "LONG_LINE";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002825
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002826 # Check the allowed long line types first
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002827
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002828 # logging functions that end in a string that starts
2829 # before $max_line_length
2830 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2831 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2832 $msg_type = "";
2833
2834 # lines with only strings (w/ possible termination)
2835 # #defines with only strings
2836 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2837 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2838 $msg_type = "";
2839
Anas Nashif0f3d5472016-11-07 15:57:57 -05002840 # EFI_GUID is another special case
2841 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2842 $msg_type = "";
2843
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002844 # Otherwise set the alternate message types
2845
2846 # a comment starts before $max_line_length
2847 } elsif ($line =~ /($;[\s$;]*)$/ &&
2848 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2849 $msg_type = "LONG_LINE_COMMENT"
2850
2851 # a quoted string starts before $max_line_length
2852 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2853 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2854 $msg_type = "LONG_LINE_STRING"
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002855 }
2856
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002857 if ($msg_type ne "" &&
2858 (show_type("LONG_LINE") || show_type($msg_type))) {
2859 WARN($msg_type,
2860 "line over $max_line_length characters\n" . $herecurr);
2861 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002862 }
2863
2864# check for adding lines without a newline.
2865 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2866 WARN("MISSING_EOF_NEWLINE",
2867 "adding a line without newline at end of file\n" . $herecurr);
2868 }
2869
2870# Blackfin: use hi/lo macros
2871 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2872 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2873 my $herevet = "$here\n" . cat_vet($line) . "\n";
2874 ERROR("LO_MACRO",
2875 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2876 }
2877 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2878 my $herevet = "$here\n" . cat_vet($line) . "\n";
2879 ERROR("HI_MACRO",
2880 "use the HI() macro, not (... >> 16)\n" . $herevet);
2881 }
2882 }
2883
2884# check we are in a valid source file C or perl if not then ignore this hunk
2885 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2886
2887# at the beginning of a line any tabs must come first and anything
2888# more than 8 must use tabs.
2889 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2890 $rawline =~ /^\+\s* \s*/) {
2891 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2892 $rpt_cleaners = 1;
2893 if (ERROR("CODE_INDENT",
2894 "code indent should use tabs where possible\n" . $herevet) &&
2895 $fix) {
2896 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2897 }
2898 }
2899
2900# check for space before tabs.
2901 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2902 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2903 if (WARN("SPACE_BEFORE_TAB",
2904 "please, no space before tabs\n" . $herevet) &&
2905 $fix) {
2906 while ($fixed[$fixlinenr] =~
2907 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2908 while ($fixed[$fixlinenr] =~
2909 s/(^\+.*) +\t/$1\t/) {}
2910 }
2911 }
2912
2913# check for && or || at the start of a line
2914 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2915 CHK("LOGICAL_CONTINUATIONS",
2916 "Logical continuations should be on the previous line\n" . $hereprev);
2917 }
2918
Anas Nashif0f3d5472016-11-07 15:57:57 -05002919# check indentation starts on a tab stop
2920 if ($^V && $^V ge 5.10.0 &&
2921 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2922 my $indent = length($1);
2923 if ($indent % 8) {
2924 if (WARN("TABSTOP",
2925 "Statements should start on a tabstop\n" . $herecurr) &&
2926 $fix) {
2927 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2928 }
2929 }
2930 }
2931
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002932# check multi-line statement indentation matches previous line
2933 if ($^V && $^V ge 5.10.0 &&
2934 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2935 $prevline =~ /^\+(\t*)(.*)$/;
2936 my $oldindent = $1;
2937 my $rest = $2;
2938
2939 my $pos = pos_last_openparen($rest);
2940 if ($pos >= 0) {
2941 $line =~ /^(\+| )([ \t]*)/;
2942 my $newindent = $2;
2943
2944 my $goodtabindent = $oldindent .
2945 "\t" x ($pos / 8) .
2946 " " x ($pos % 8);
2947 my $goodspaceindent = $oldindent . " " x $pos;
2948
2949 if ($newindent ne $goodtabindent &&
2950 $newindent ne $goodspaceindent) {
2951
2952 if (CHK("PARENTHESIS_ALIGNMENT",
2953 "Alignment should match open parenthesis\n" . $hereprev) &&
2954 $fix && $line =~ /^\+/) {
2955 $fixed[$fixlinenr] =~
2956 s/^\+[ \t]*/\+$goodtabindent/;
2957 }
2958 }
2959 }
2960 }
2961
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002962# check for space after cast like "(int) foo" or "(struct foo) bar"
2963# avoid checking a few false positives:
2964# "sizeof(<type>)" or "__alignof__(<type>)"
2965# function pointer declarations like "(*foo)(int) = bar;"
2966# structure definitions like "(struct foo) { 0 };"
2967# multiline macros that define functions
2968# known attributes or the __attribute__ keyword
2969 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2970 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002971 if (CHK("SPACING",
2972 "No space is necessary after a cast\n" . $herecurr) &&
2973 $fix) {
2974 $fixed[$fixlinenr] =~
2975 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2976 }
2977 }
2978
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002979# Block comment styles
2980# Networking with an initial /*
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002981 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2982 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2983 $rawline =~ /^\+[ \t]*\*/ &&
2984 $realline > 2) {
2985 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2986 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2987 }
2988
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002989# Block comments use * on subsequent lines
2990 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
2991 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002992 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2993 $rawline =~ /^\+/ && #line is new
2994 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002995 WARN("BLOCK_COMMENT_STYLE",
2996 "Block comments use * on subsequent lines\n" . $hereprev);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07002997 }
2998
Anas Nashifcbfb62c2015-10-14 06:08:54 -04002999# Block comments use */ on trailing lines
3000 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003001 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3002 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3003 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003004 WARN("BLOCK_COMMENT_STYLE",
3005 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003006 }
3007
Anas Nashif0f3d5472016-11-07 15:57:57 -05003008# Block comment * alignment
3009 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3010 $line =~ /^\+[ \t]*$;/ && #leading comment
3011 $rawline =~ /^\+[ \t]*\*/ && #leading *
3012 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3013 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3014 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3015 my $oldindent;
3016 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3017 if (defined($1)) {
3018 $oldindent = expand_tabs($1);
3019 } else {
3020 $prevrawline =~ m@^\+(.*/?)\*@;
3021 $oldindent = expand_tabs($1);
3022 }
3023 $rawline =~ m@^\+([ \t]*)\*@;
3024 my $newindent = $1;
3025 $newindent = expand_tabs($newindent);
3026 if (length($oldindent) ne length($newindent)) {
3027 WARN("BLOCK_COMMENT_STYLE",
3028 "Block comments should align the * on each line\n" . $hereprev);
3029 }
3030 }
3031
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003032# check for missing blank lines after struct/union declarations
3033# with exceptions for various attributes and macros
3034 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3035 $line =~ /^\+/ &&
3036 !($line =~ /^\+\s*$/ ||
3037 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3038 $line =~ /^\+\s*MODULE_/i ||
3039 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3040 $line =~ /^\+[a-z_]*init/ ||
3041 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3042 $line =~ /^\+\s*DECLARE/ ||
3043 $line =~ /^\+\s*__setup/)) {
3044 if (CHK("LINE_SPACING",
3045 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3046 $fix) {
3047 fix_insert_line($fixlinenr, "\+");
3048 }
3049 }
3050
3051# check for multiple consecutive blank lines
3052 if ($prevline =~ /^[\+ ]\s*$/ &&
3053 $line =~ /^\+\s*$/ &&
3054 $last_blank_line != ($linenr - 1)) {
3055 if (CHK("LINE_SPACING",
3056 "Please don't use multiple blank lines\n" . $hereprev) &&
3057 $fix) {
3058 fix_delete_line($fixlinenr, $rawline);
3059 }
3060
3061 $last_blank_line = $linenr;
3062 }
3063
3064# check for missing blank lines after declarations
3065 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3066 # actual declarations
3067 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3068 # function pointer declarations
3069 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3070 # foo bar; where foo is some local typedef or #define
3071 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3072 # known declaration macros
3073 $prevline =~ /^\+\s+$declaration_macros/) &&
3074 # for "else if" which can look like "$Ident $Ident"
3075 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3076 # other possible extensions of declaration lines
3077 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3078 # not starting a section or a macro "\" extended line
3079 $prevline =~ /(?:\{\s*|\\)$/) &&
3080 # looks like a declaration
3081 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3082 # function pointer declarations
3083 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3084 # foo bar; where foo is some local typedef or #define
Marcus Shawcroftf62e6812017-02-19 09:15:57 +00003085 $sline =~ /^\+\s+(?:volatile\s+)?$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003086 # known declaration macros
3087 $sline =~ /^\+\s+$declaration_macros/ ||
3088 # start of struct or union or enum
3089 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3090 # start or end of block or continuation of declaration
3091 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3092 # bitfield continuation
3093 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3094 # other possible extensions of declaration lines
3095 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3096 # indentation of previous and current line are the same
3097 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3098 if (WARN("LINE_SPACING",
3099 "Missing a blank line after declarations\n" . $hereprev) &&
3100 $fix) {
3101 fix_insert_line($fixlinenr, "\+");
3102 }
3103 }
3104
3105# check for spaces at the beginning of a line.
3106# Exceptions:
3107# 1) within comments
3108# 2) indented preprocessor commands
3109# 3) hanging labels
3110 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3111 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3112 if (WARN("LEADING_SPACE",
3113 "please, no spaces at the start of a line\n" . $herevet) &&
3114 $fix) {
3115 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3116 }
3117 }
3118
3119# check we are in a valid C source file if not then ignore this hunk
3120 next if ($realfile !~ /\.(h|c)$/);
3121
3122# check indentation of any line with a bare else
3123# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3124# if the previous line is a break or return and is indented 1 tab more...
3125 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3126 my $tabs = length($1) + 1;
3127 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3128 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3129 defined $lines[$linenr] &&
3130 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3131 WARN("UNNECESSARY_ELSE",
3132 "else is not generally useful after a break or return\n" . $hereprev);
3133 }
3134 }
3135
3136# check indentation of a line with a break;
3137# if the previous line is a goto or return and is indented the same # of tabs
3138 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3139 my $tabs = $1;
3140 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3141 WARN("UNNECESSARY_BREAK",
3142 "break is not useful after a goto or return\n" . $hereprev);
3143 }
3144 }
3145
3146# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3147 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3148 WARN("CONFIG_EXPERIMENTAL",
3149 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3150 }
3151
3152# check for RCS/CVS revision markers
3153 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3154 WARN("CVS_KEYWORD",
3155 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3156 }
3157
3158# Blackfin: don't use __builtin_bfin_[cs]sync
3159 if ($line =~ /__builtin_bfin_csync/) {
3160 my $herevet = "$here\n" . cat_vet($line) . "\n";
3161 ERROR("CSYNC",
3162 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
3163 }
3164 if ($line =~ /__builtin_bfin_ssync/) {
3165 my $herevet = "$here\n" . cat_vet($line) . "\n";
3166 ERROR("SSYNC",
3167 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
3168 }
3169
3170# check for old HOTPLUG __dev<foo> section markings
3171 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3172 WARN("HOTPLUG_SECTION",
3173 "Using $1 is unnecessary\n" . $herecurr);
3174 }
3175
3176# Check for potential 'bare' types
3177 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3178 $realline_next);
3179#print "LINE<$line>\n";
3180 if ($linenr >= $suppress_statement &&
3181 $realcnt && $sline =~ /.\s*\S/) {
3182 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3183 ctx_statement_block($linenr, $realcnt, 0);
3184 $stat =~ s/\n./\n /g;
3185 $cond =~ s/\n./\n /g;
3186
3187#print "linenr<$linenr> <$stat>\n";
3188 # If this statement has no statement boundaries within
3189 # it there is no point in retrying a statement scan
3190 # until we hit end of it.
3191 my $frag = $stat; $frag =~ s/;+\s*$//;
3192 if ($frag !~ /(?:{|;)/) {
3193#print "skip<$line_nr_next>\n";
3194 $suppress_statement = $line_nr_next;
3195 }
3196
3197 # Find the real next line.
3198 $realline_next = $line_nr_next;
3199 if (defined $realline_next &&
3200 (!defined $lines[$realline_next - 1] ||
3201 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3202 $realline_next++;
3203 }
3204
3205 my $s = $stat;
3206 $s =~ s/{.*$//s;
3207
3208 # Ignore goto labels.
3209 if ($s =~ /$Ident:\*$/s) {
3210
3211 # Ignore functions being called
3212 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3213
3214 } elsif ($s =~ /^.\s*else\b/s) {
3215
3216 # declarations always start with types
3217 } 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) {
3218 my $type = $1;
3219 $type =~ s/\s+/ /g;
3220 possible($type, "A:" . $s);
3221
3222 # definitions in global scope can only start with types
3223 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3224 possible($1, "B:" . $s);
3225 }
3226
3227 # any (foo ... *) is a pointer cast, and foo is a type
3228 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3229 possible($1, "C:" . $s);
3230 }
3231
3232 # Check for any sort of function declaration.
3233 # int foo(something bar, other baz);
3234 # void (*store_gdt)(x86_descr_ptr *);
3235 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3236 my ($name_len) = length($1);
3237
3238 my $ctx = $s;
3239 substr($ctx, 0, $name_len + 1, '');
3240 $ctx =~ s/\)[^\)]*$//;
3241
3242 for my $arg (split(/\s*,\s*/, $ctx)) {
3243 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3244
3245 possible($1, "D:" . $s);
3246 }
3247 }
3248 }
3249
3250 }
3251
3252#
3253# Checks which may be anchored in the context.
3254#
3255
3256# Check for switch () and associated case and default
3257# statements should be at the same indent.
3258 if ($line=~/\bswitch\s*\(.*\)/) {
3259 my $err = '';
3260 my $sep = '';
3261 my @ctx = ctx_block_outer($linenr, $realcnt);
3262 shift(@ctx);
3263 for my $ctx (@ctx) {
3264 my ($clen, $cindent) = line_stats($ctx);
3265 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3266 $indent != $cindent) {
3267 $err .= "$sep$ctx\n";
3268 $sep = '';
3269 } else {
3270 $sep = "[...]\n";
3271 }
3272 }
3273 if ($err ne '') {
3274 ERROR("SWITCH_CASE_INDENT_LEVEL",
3275 "switch and case should be at the same indent\n$hereline$err");
3276 }
3277 }
3278
3279# if/while/etc brace do not go on next line, unless defining a do while loop,
3280# or if that brace on the next line is for something else
3281 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3282 my $pre_ctx = "$1$2";
3283
3284 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3285
3286 if ($line =~ /^\+\t{6,}/) {
3287 WARN("DEEP_INDENTATION",
3288 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3289 }
3290
3291 my $ctx_cnt = $realcnt - $#ctx - 1;
3292 my $ctx = join("\n", @ctx);
3293
3294 my $ctx_ln = $linenr;
3295 my $ctx_skip = $realcnt;
3296
3297 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3298 defined $lines[$ctx_ln - 1] &&
3299 $lines[$ctx_ln - 1] =~ /^-/)) {
3300 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3301 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3302 $ctx_ln++;
3303 }
3304
3305 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3306 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3307
3308 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3309 ERROR("OPEN_BRACE",
3310 "that open brace { should be on the previous line\n" .
3311 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3312 }
3313 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3314 $ctx =~ /\)\s*\;\s*$/ &&
3315 defined $lines[$ctx_ln - 1])
3316 {
3317 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3318 if ($nindent > $indent) {
3319 WARN("TRAILING_SEMICOLON",
3320 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3321 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3322 }
3323 }
3324 }
3325
3326# Check relative indent for conditionals and blocks.
3327 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3328 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3329 ctx_statement_block($linenr, $realcnt, 0)
3330 if (!defined $stat);
3331 my ($s, $c) = ($stat, $cond);
3332
3333 substr($s, 0, length($c), '');
3334
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003335 # remove inline comments
3336 $s =~ s/$;/ /g;
3337 $c =~ s/$;/ /g;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003338
3339 # Find out how long the conditional actually is.
3340 my @newlines = ($c =~ /\n/gs);
3341 my $cond_lines = 1 + $#newlines;
3342
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003343 # Make sure we remove the line prefixes as we have
3344 # none on the first line, and are going to readd them
3345 # where necessary.
3346 $s =~ s/\n./\n/gs;
3347 while ($s =~ /\n\s+\\\n/) {
3348 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3349 }
3350
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003351 # We want to check the first line inside the block
3352 # starting at the end of the conditional, so remove:
3353 # 1) any blank line termination
3354 # 2) any opening brace { on end of the line
3355 # 3) any do (...) {
3356 my $continuation = 0;
3357 my $check = 0;
3358 $s =~ s/^.*\bdo\b//;
3359 $s =~ s/^\s*{//;
3360 if ($s =~ s/^\s*\\//) {
3361 $continuation = 1;
3362 }
3363 if ($s =~ s/^\s*?\n//) {
3364 $check = 1;
3365 $cond_lines++;
3366 }
3367
3368 # Also ignore a loop construct at the end of a
3369 # preprocessor statement.
3370 if (($prevline =~ /^.\s*#\s*define\s/ ||
3371 $prevline =~ /\\\s*$/) && $continuation == 0) {
3372 $check = 0;
3373 }
3374
3375 my $cond_ptr = -1;
3376 $continuation = 0;
3377 while ($cond_ptr != $cond_lines) {
3378 $cond_ptr = $cond_lines;
3379
3380 # If we see an #else/#elif then the code
3381 # is not linear.
3382 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3383 $check = 0;
3384 }
3385
3386 # Ignore:
3387 # 1) blank lines, they should be at 0,
3388 # 2) preprocessor lines, and
3389 # 3) labels.
3390 if ($continuation ||
3391 $s =~ /^\s*?\n/ ||
3392 $s =~ /^\s*#\s*?/ ||
3393 $s =~ /^\s*$Ident\s*:/) {
3394 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3395 if ($s =~ s/^.*?\n//) {
3396 $cond_lines++;
3397 }
3398 }
3399 }
3400
3401 my (undef, $sindent) = line_stats("+" . $s);
3402 my $stat_real = raw_line($linenr, $cond_lines);
3403
3404 # Check if either of these lines are modified, else
3405 # this is not this patch's fault.
3406 if (!defined($stat_real) ||
3407 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3408 $check = 0;
3409 }
3410 if (defined($stat_real) && $cond_lines > 1) {
3411 $stat_real = "[...]\n$stat_real";
3412 }
3413
3414 #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";
3415
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003416 if ($check && $s ne '' &&
3417 (($sindent % 8) != 0 ||
3418 ($sindent < $indent) ||
3419 ($sindent > $indent + 8))) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003420 WARN("SUSPECT_CODE_INDENT",
3421 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3422 }
3423 }
3424
3425 # Track the 'values' across context and added lines.
3426 my $opline = $line; $opline =~ s/^./ /;
3427 my ($curr_values, $curr_vars) =
3428 annotate_values($opline . "\n", $prev_values);
3429 $curr_values = $prev_values . $curr_values;
3430 if ($dbg_values) {
3431 my $outline = $opline; $outline =~ s/\t/ /g;
3432 print "$linenr > .$outline\n";
3433 print "$linenr > $curr_values\n";
3434 print "$linenr > $curr_vars\n";
3435 }
3436 $prev_values = substr($curr_values, -1);
3437
3438#ignore lines not being added
3439 next if ($line =~ /^[^\+]/);
3440
Anas Nashif0f3d5472016-11-07 15:57:57 -05003441# check for declarations of signed or unsigned without int
3442 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3443 my $type = $1;
3444 my $var = $2;
3445 $var = "" if (!defined $var);
3446 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3447 my $sign = $1;
3448 my $pointer = $2;
3449
3450 $pointer = "" if (!defined $pointer);
3451
3452 if (WARN("UNSPECIFIED_INT",
3453 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3454 $fix) {
3455 my $decl = trim($sign) . " int ";
3456 my $comp_pointer = $pointer;
3457 $comp_pointer =~ s/\s//g;
3458 $decl .= $comp_pointer;
3459 $decl = rtrim($decl) if ($var eq "");
3460 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3461 }
3462 }
3463 }
3464
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003465# TEST: allow direct testing of the type matcher.
3466 if ($dbg_type) {
3467 if ($line =~ /^.\s*$Declare\s*$/) {
3468 ERROR("TEST_TYPE",
3469 "TEST: is type\n" . $herecurr);
3470 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3471 ERROR("TEST_NOT_TYPE",
3472 "TEST: is not type ($1 is)\n". $herecurr);
3473 }
3474 next;
3475 }
3476# TEST: allow direct testing of the attribute matcher.
3477 if ($dbg_attr) {
3478 if ($line =~ /^.\s*$Modifier\s*$/) {
3479 ERROR("TEST_ATTR",
3480 "TEST: is attr\n" . $herecurr);
3481 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3482 ERROR("TEST_NOT_ATTR",
3483 "TEST: is not attr ($1 is)\n". $herecurr);
3484 }
3485 next;
3486 }
3487
3488# check for initialisation to aggregates open brace on the next line
3489 if ($line =~ /^.\s*{/ &&
3490 $prevline =~ /(?:^|[^=])=\s*$/) {
3491 if (ERROR("OPEN_BRACE",
3492 "that open brace { should be on the previous line\n" . $hereprev) &&
3493 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3494 fix_delete_line($fixlinenr - 1, $prevrawline);
3495 fix_delete_line($fixlinenr, $rawline);
3496 my $fixedline = $prevrawline;
3497 $fixedline =~ s/\s*=\s*$/ = {/;
3498 fix_insert_line($fixlinenr, $fixedline);
3499 $fixedline = $line;
3500 $fixedline =~ s/^(.\s*){\s*/$1/;
3501 fix_insert_line($fixlinenr, $fixedline);
3502 }
3503 }
3504
3505#
3506# Checks which are anchored on the added line.
3507#
3508
3509# check for malformed paths in #include statements (uses RAW line)
3510 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3511 my $path = $1;
3512 if ($path =~ m{//}) {
3513 ERROR("MALFORMED_INCLUDE",
3514 "malformed #include filename\n" . $herecurr);
3515 }
3516 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3517 ERROR("UAPI_INCLUDE",
3518 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3519 }
3520 }
3521
3522# no C99 // comments
3523 if ($line =~ m{//}) {
3524 if (ERROR("C99_COMMENTS",
3525 "do not use C99 // comments\n" . $herecurr) &&
3526 $fix) {
3527 my $line = $fixed[$fixlinenr];
3528 if ($line =~ /\/\/(.*)$/) {
3529 my $comment = trim($1);
3530 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3531 }
3532 }
3533 }
3534 # Remove C99 comments.
3535 $line =~ s@//.*@@;
3536 $opline =~ s@//.*@@;
3537
3538# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3539# the whole statement.
3540#print "APW <$lines[$realline_next - 1]>\n";
3541 if (defined $realline_next &&
3542 exists $lines[$realline_next - 1] &&
3543 !defined $suppress_export{$realline_next} &&
3544 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3545 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3546 # Handle definitions which produce identifiers with
3547 # a prefix:
3548 # XXX(foo);
3549 # EXPORT_SYMBOL(something_foo);
3550 my $name = $1;
3551 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3552 $name =~ /^${Ident}_$2/) {
3553#print "FOO C name<$name>\n";
3554 $suppress_export{$realline_next} = 1;
3555
3556 } elsif ($stat !~ /(?:
3557 \n.}\s*$|
3558 ^.DEFINE_$Ident\(\Q$name\E\)|
3559 ^.DECLARE_$Ident\(\Q$name\E\)|
3560 ^.LIST_HEAD\(\Q$name\E\)|
3561 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3562 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3563 )/x) {
3564#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3565 $suppress_export{$realline_next} = 2;
3566 } else {
3567 $suppress_export{$realline_next} = 1;
3568 }
3569 }
3570 if (!defined $suppress_export{$linenr} &&
3571 $prevline =~ /^.\s*$/ &&
3572 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3573 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3574#print "FOO B <$lines[$linenr - 1]>\n";
3575 $suppress_export{$linenr} = 2;
3576 }
3577 if (defined $suppress_export{$linenr} &&
3578 $suppress_export{$linenr} == 2) {
3579 WARN("EXPORT_SYMBOL",
3580 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3581 }
3582
3583# check for global initialisers.
Anas Nashif76b09132016-02-20 13:53:35 -05003584 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003585 if (ERROR("GLOBAL_INITIALISERS",
Anas Nashif76b09132016-02-20 13:53:35 -05003586 "do not initialise globals to $1\n" . $herecurr) &&
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003587 $fix) {
Anas Nashif76b09132016-02-20 13:53:35 -05003588 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003589 }
3590 }
3591# check for static initialisers.
Anas Nashif76b09132016-02-20 13:53:35 -05003592 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003593 if (ERROR("INITIALISED_STATIC",
Anas Nashif76b09132016-02-20 13:53:35 -05003594 "do not initialise statics to $1\n" .
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003595 $herecurr) &&
3596 $fix) {
Anas Nashif76b09132016-02-20 13:53:35 -05003597 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003598 }
3599 }
3600
3601# check for misordered declarations of char/short/int/long with signed/unsigned
3602 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3603 my $tmp = trim($1);
3604 WARN("MISORDERED_TYPE",
3605 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3606 }
3607
3608# check for static const char * arrays.
3609 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3610 WARN("STATIC_CONST_CHAR_ARRAY",
3611 "static const char * array should probably be static const char * const\n" .
3612 $herecurr);
3613 }
3614
3615# check for static char foo[] = "bar" declarations.
3616 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3617 WARN("STATIC_CONST_CHAR_ARRAY",
3618 "static char array declaration should probably be static const char\n" .
3619 $herecurr);
3620 }
3621
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003622# check for const <foo> const where <foo> is not a pointer or array type
3623 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3624 my $found = $1;
3625 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3626 WARN("CONST_CONST",
3627 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3628 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3629 WARN("CONST_CONST",
3630 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3631 }
3632 }
3633
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003634# check for non-global char *foo[] = {"bar", ...} declarations.
3635 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3636 WARN("STATIC_CONST_CHAR_ARRAY",
3637 "char * array declaration might be better as static const\n" .
3638 $herecurr);
3639 }
3640
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003641# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3642 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3643 my $array = $1;
3644 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3645 my $array_div = $1;
3646 if (WARN("ARRAY_SIZE",
3647 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3648 $fix) {
3649 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3650 }
3651 }
3652 }
3653
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003654# check for function declarations without arguments like "int foo()"
3655 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3656 if (ERROR("FUNCTION_WITHOUT_ARGS",
3657 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3658 $fix) {
3659 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3660 }
3661 }
3662
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003663# check for new typedefs, only function parameters and sparse annotations
3664# make sense.
3665 if ($line =~ /\btypedef\s/ &&
3666 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3667 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3668 $line !~ /\b$typeTypedefs\b/ &&
3669 $line !~ /\b__bitwise(?:__|)\b/) {
3670 WARN("NEW_TYPEDEFS",
3671 "do not add new typedefs\n" . $herecurr);
3672 }
3673
3674# * goes on variable not on type
3675 # (char*[ const])
3676 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3677 #print "AA<$1>\n";
3678 my ($ident, $from, $to) = ($1, $2, $2);
3679
3680 # Should start with a space.
3681 $to =~ s/^(\S)/ $1/;
3682 # Should not end with a space.
3683 $to =~ s/\s+$//;
3684 # '*'s should not have spaces between.
3685 while ($to =~ s/\*\s+\*/\*\*/) {
3686 }
3687
3688## print "1: from<$from> to<$to> ident<$ident>\n";
3689 if ($from ne $to) {
3690 if (ERROR("POINTER_LOCATION",
3691 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3692 $fix) {
3693 my $sub_from = $ident;
3694 my $sub_to = $ident;
3695 $sub_to =~ s/\Q$from\E/$to/;
3696 $fixed[$fixlinenr] =~
3697 s@\Q$sub_from\E@$sub_to@;
3698 }
3699 }
3700 }
3701 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3702 #print "BB<$1>\n";
3703 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3704
3705 # Should start with a space.
3706 $to =~ s/^(\S)/ $1/;
3707 # Should not end with a space.
3708 $to =~ s/\s+$//;
3709 # '*'s should not have spaces between.
3710 while ($to =~ s/\*\s+\*/\*\*/) {
3711 }
3712 # Modifiers should have spaces.
3713 $to =~ s/(\b$Modifier$)/$1 /;
3714
3715## print "2: from<$from> to<$to> ident<$ident>\n";
3716 if ($from ne $to && $ident !~ /^$Modifier$/) {
3717 if (ERROR("POINTER_LOCATION",
3718 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3719 $fix) {
3720
3721 my $sub_from = $match;
3722 my $sub_to = $match;
3723 $sub_to =~ s/\Q$from\E/$to/;
3724 $fixed[$fixlinenr] =~
3725 s@\Q$sub_from\E@$sub_to@;
3726 }
3727 }
3728 }
3729
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003730# avoid BUG() or BUG_ON()
3731 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3732 my $msg_type = \&WARN;
3733 $msg_type = \&CHK if ($file);
3734 &{$msg_type}("AVOID_BUG",
3735 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3736 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003737
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003738# avoid LINUX_VERSION_CODE
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003739 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3740 WARN("LINUX_VERSION_CODE",
3741 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3742 }
3743
3744# check for uses of printk_ratelimit
3745 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3746 WARN("PRINTK_RATELIMITED",
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003747 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003748 }
3749
3750# printk should use KERN_* levels. Note that follow on printk's on the
3751# same line do not need a level, so we use the current block context
3752# to try and find and validate the current printk. In summary the current
3753# printk includes all preceding printk's which have no newline on the end.
3754# we assume the first bad printk is the one to report.
3755 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3756 my $ok = 0;
3757 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3758 #print "CHECK<$lines[$ln - 1]\n";
3759 # we have a preceding printk if it ends
3760 # with "\n" ignore it, else it is to blame
3761 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3762 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3763 $ok = 1;
3764 }
3765 last;
3766 }
3767 }
3768 if ($ok == 0) {
3769 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3770 "printk() should include KERN_ facility level\n" . $herecurr);
3771 }
3772 }
3773
3774 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3775 my $orig = $1;
3776 my $level = lc($orig);
3777 $level = "warn" if ($level eq "warning");
3778 my $level2 = $level;
3779 $level2 = "dbg" if ($level eq "debug");
3780 WARN("PREFER_PR_LEVEL",
3781 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3782 }
3783
3784 if ($line =~ /\bpr_warning\s*\(/) {
3785 if (WARN("PREFER_PR_LEVEL",
3786 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3787 $fix) {
3788 $fixed[$fixlinenr] =~
3789 s/\bpr_warning\b/pr_warn/;
3790 }
3791 }
3792
3793 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3794 my $orig = $1;
3795 my $level = lc($orig);
3796 $level = "warn" if ($level eq "warning");
3797 $level = "dbg" if ($level eq "debug");
3798 WARN("PREFER_DEV_LEVEL",
3799 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3800 }
3801
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003802# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3803# number of false positives, but assembly files are not checked, so at
3804# least the arch entry code will not trigger this warning.
3805 if ($line =~ /\bENOSYS\b/) {
3806 WARN("ENOSYS",
3807 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3808 }
3809
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003810# function brace can't be on same line, except for #defines of do while,
3811# or if closed on same line
3812 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Anas Nashifcbfb62c2015-10-14 06:08:54 -04003813 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003814 if (ERROR("OPEN_BRACE",
3815 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3816 $fix) {
3817 fix_delete_line($fixlinenr, $rawline);
3818 my $fixed_line = $rawline;
3819 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3820 my $line1 = $1;
3821 my $line2 = $2;
3822 fix_insert_line($fixlinenr, ltrim($line1));
3823 fix_insert_line($fixlinenr, "\+{");
3824 if ($line2 !~ /^\s*$/) {
3825 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3826 }
3827 }
3828 }
3829
3830# open braces for enum, union and struct go on the same line.
3831 if ($line =~ /^.\s*{/ &&
3832 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3833 if (ERROR("OPEN_BRACE",
3834 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3835 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3836 fix_delete_line($fixlinenr - 1, $prevrawline);
3837 fix_delete_line($fixlinenr, $rawline);
3838 my $fixedline = rtrim($prevrawline) . " {";
3839 fix_insert_line($fixlinenr, $fixedline);
3840 $fixedline = $rawline;
3841 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3842 if ($fixedline !~ /^\+\s*$/) {
3843 fix_insert_line($fixlinenr, $fixedline);
3844 }
3845 }
3846 }
3847
3848# missing space after union, struct or enum definition
3849 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3850 if (WARN("SPACING",
3851 "missing space after $1 definition\n" . $herecurr) &&
3852 $fix) {
3853 $fixed[$fixlinenr] =~
3854 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3855 }
3856 }
3857
3858# Function pointer declarations
3859# check spacing between type, funcptr, and args
3860# canonical declaration is "type (*funcptr)(args...)"
3861 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3862 my $declare = $1;
3863 my $pre_pointer_space = $2;
3864 my $post_pointer_space = $3;
3865 my $funcname = $4;
3866 my $post_funcname_space = $5;
3867 my $pre_args_space = $6;
3868
3869# the $Declare variable will capture all spaces after the type
3870# so check it for a missing trailing missing space but pointer return types
3871# don't need a space so don't warn for those.
3872 my $post_declare_space = "";
3873 if ($declare =~ /(\s+)$/) {
3874 $post_declare_space = $1;
3875 $declare = rtrim($declare);
3876 }
3877 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3878 WARN("SPACING",
3879 "missing space after return type\n" . $herecurr);
3880 $post_declare_space = " ";
3881 }
3882
3883# unnecessary space "type (*funcptr)(args...)"
3884# This test is not currently implemented because these declarations are
3885# equivalent to
3886# int foo(int bar, ...)
3887# and this is form shouldn't/doesn't generate a checkpatch warning.
3888#
3889# elsif ($declare =~ /\s{2,}$/) {
3890# WARN("SPACING",
3891# "Multiple spaces after return type\n" . $herecurr);
3892# }
3893
3894# unnecessary space "type ( *funcptr)(args...)"
3895 if (defined $pre_pointer_space &&
3896 $pre_pointer_space =~ /^\s/) {
3897 WARN("SPACING",
3898 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3899 }
3900
3901# unnecessary space "type (* funcptr)(args...)"
3902 if (defined $post_pointer_space &&
3903 $post_pointer_space =~ /^\s/) {
3904 WARN("SPACING",
3905 "Unnecessary space before function pointer name\n" . $herecurr);
3906 }
3907
3908# unnecessary space "type (*funcptr )(args...)"
3909 if (defined $post_funcname_space &&
3910 $post_funcname_space =~ /^\s/) {
3911 WARN("SPACING",
3912 "Unnecessary space after function pointer name\n" . $herecurr);
3913 }
3914
3915# unnecessary space "type (*funcptr) (args...)"
3916 if (defined $pre_args_space &&
3917 $pre_args_space =~ /^\s/) {
3918 WARN("SPACING",
3919 "Unnecessary space before function pointer arguments\n" . $herecurr);
3920 }
3921
3922 if (show_type("SPACING") && $fix) {
3923 $fixed[$fixlinenr] =~
3924 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3925 }
3926 }
3927
3928# check for spacing round square brackets; allowed:
3929# 1. with a type on the left -- int [] a;
3930# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3931# 3. inside a curly brace -- = { [0...10] = 5 }
3932 while ($line =~ /(.*?\s)\[/g) {
3933 my ($where, $prefix) = ($-[1], $1);
3934 if ($prefix !~ /$Type\s+$/ &&
3935 ($where != 0 || $prefix !~ /^.\s+$/) &&
Anas Nashif98c50de2015-11-06 18:23:35 -05003936 $prefix !~ /[{,]\s+$/ &&
3937 $prefix !~ /:\s+$/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07003938 if (ERROR("BRACKET_SPACE",
3939 "space prohibited before open square bracket '['\n" . $herecurr) &&
3940 $fix) {
3941 $fixed[$fixlinenr] =~
3942 s/^(\+.*?)\s+\[/$1\[/;
3943 }
3944 }
3945 }
3946
3947# check for spaces between functions and their parentheses.
3948 while ($line =~ /($Ident)\s+\(/g) {
3949 my $name = $1;
3950 my $ctx_before = substr($line, 0, $-[1]);
3951 my $ctx = "$ctx_before$name";
3952
3953 # Ignore those directives where spaces _are_ permitted.
3954 if ($name =~ /^(?:
3955 if|for|while|switch|return|case|
3956 volatile|__volatile__|
3957 __attribute__|format|__extension__|
3958 asm|__asm__)$/x)
3959 {
3960 # cpp #define statements have non-optional spaces, ie
3961 # if there is a space between the name and the open
3962 # parenthesis it is simply not a parameter group.
3963 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3964
3965 # cpp #elif statement condition may start with a (
3966 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3967
3968 # If this whole things ends with a type its most
3969 # likely a typedef for a function.
3970 } elsif ($ctx =~ /$Type$/) {
3971
3972 } else {
3973 if (WARN("SPACING",
3974 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3975 $fix) {
3976 $fixed[$fixlinenr] =~
3977 s/\b$name\s+\(/$name\(/;
3978 }
3979 }
3980 }
3981
3982# Check operator spacing.
3983 if (!($line=~/\#\s*include/)) {
3984 my $fixed_line = "";
3985 my $line_fixed = 0;
3986
3987 my $ops = qr{
3988 <<=|>>=|<=|>=|==|!=|
3989 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3990 =>|->|<<|>>|<|>|=|!|~|
3991 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
3992 \?:|\?|:
3993 }x;
3994 my @elements = split(/($ops|;)/, $opline);
3995
3996## print("element count: <" . $#elements . ">\n");
3997## foreach my $el (@elements) {
3998## print("el: <$el>\n");
3999## }
4000
4001 my @fix_elements = ();
4002 my $off = 0;
4003
4004 foreach my $el (@elements) {
4005 push(@fix_elements, substr($rawline, $off, length($el)));
4006 $off += length($el);
4007 }
4008
4009 $off = 0;
4010
4011 my $blank = copy_spacing($opline);
4012 my $last_after = -1;
4013
4014 for (my $n = 0; $n < $#elements; $n += 2) {
4015
4016 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4017
4018## print("n: <$n> good: <$good>\n");
4019
4020 $off += length($elements[$n]);
4021
4022 # Pick up the preceding and succeeding characters.
4023 my $ca = substr($opline, 0, $off);
4024 my $cc = '';
4025 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4026 $cc = substr($opline, $off + length($elements[$n + 1]));
4027 }
4028 my $cb = "$ca$;$cc";
4029
4030 my $a = '';
4031 $a = 'V' if ($elements[$n] ne '');
4032 $a = 'W' if ($elements[$n] =~ /\s$/);
4033 $a = 'C' if ($elements[$n] =~ /$;$/);
4034 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4035 $a = 'O' if ($elements[$n] eq '');
4036 $a = 'E' if ($ca =~ /^\s*$/);
4037
4038 my $op = $elements[$n + 1];
4039
4040 my $c = '';
4041 if (defined $elements[$n + 2]) {
4042 $c = 'V' if ($elements[$n + 2] ne '');
4043 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4044 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4045 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4046 $c = 'O' if ($elements[$n + 2] eq '');
4047 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4048 } else {
4049 $c = 'E';
4050 }
4051
4052 my $ctx = "${a}x${c}";
4053
4054 my $at = "(ctx:$ctx)";
4055
4056 my $ptr = substr($blank, 0, $off) . "^";
4057 my $hereptr = "$hereline$ptr\n";
4058
4059 # Pull out the value of this operator.
4060 my $op_type = substr($curr_values, $off + 1, 1);
4061
4062 # Get the full operator variant.
4063 my $opv = $op . substr($curr_vars, $off, 1);
4064
4065 # Ignore operators passed as parameters.
4066 if ($op_type ne 'V' &&
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004067 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004068
4069# # Ignore comments
4070# } elsif ($op =~ /^$;+$/) {
4071
4072 # ; should have either the end of line or a space or \ after it
4073 } elsif ($op eq ';') {
4074 if ($ctx !~ /.x[WEBC]/ &&
4075 $cc !~ /^\\/ && $cc !~ /^;/) {
4076 if (ERROR("SPACING",
4077 "space required after that '$op' $at\n" . $hereptr)) {
4078 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4079 $line_fixed = 1;
4080 }
4081 }
4082
4083 # // is a comment
4084 } elsif ($op eq '//') {
4085
4086 # : when part of a bitfield
4087 } elsif ($opv eq ':B') {
4088 # skip the bitfield test for now
4089
4090 # No spaces for:
4091 # ->
4092 } elsif ($op eq '->') {
4093 if ($ctx =~ /Wx.|.xW/) {
4094 if (ERROR("SPACING",
4095 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4096 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4097 if (defined $fix_elements[$n + 2]) {
4098 $fix_elements[$n + 2] =~ s/^\s+//;
4099 }
4100 $line_fixed = 1;
4101 }
4102 }
4103
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004104 # , must not have a space before and must have a space on the right.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004105 } elsif ($op eq ',') {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004106 my $rtrim_before = 0;
4107 my $space_after = 0;
4108 if ($ctx =~ /Wx./) {
4109 if (ERROR("SPACING",
4110 "space prohibited before that '$op' $at\n" . $hereptr)) {
4111 $line_fixed = 1;
4112 $rtrim_before = 1;
4113 }
4114 }
Anas Nashife7ee0832016-02-20 14:13:16 -05004115 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ && $cc !~ /^\)/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004116 if (ERROR("SPACING",
4117 "space required after that '$op' $at\n" . $hereptr)) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004118 $line_fixed = 1;
4119 $last_after = $n;
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004120 $space_after = 1;
4121 }
4122 }
4123 if ($rtrim_before || $space_after) {
4124 if ($rtrim_before) {
4125 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4126 } else {
4127 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4128 }
4129 if ($space_after) {
4130 $good .= " ";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004131 }
4132 }
4133
4134 # '*' as part of a type definition -- reported already.
4135 } elsif ($opv eq '*_') {
4136 #warn "'*' is part of type\n";
4137
4138 # unary operators should have a space before and
4139 # none after. May be left adjacent to another
4140 # unary operator, or a cast
4141 } elsif ($op eq '!' || $op eq '~' ||
4142 $opv eq '*U' || $opv eq '-U' ||
4143 $opv eq '&U' || $opv eq '&&U') {
4144 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4145 if (ERROR("SPACING",
4146 "space required before that '$op' $at\n" . $hereptr)) {
4147 if ($n != $last_after + 2) {
4148 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4149 $line_fixed = 1;
4150 }
4151 }
4152 }
4153 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4154 # A unary '*' may be const
4155
4156 } elsif ($ctx =~ /.xW/) {
4157 if (ERROR("SPACING",
4158 "space prohibited after that '$op' $at\n" . $hereptr)) {
4159 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4160 if (defined $fix_elements[$n + 2]) {
4161 $fix_elements[$n + 2] =~ s/^\s+//;
4162 }
4163 $line_fixed = 1;
4164 }
4165 }
4166
4167 # unary ++ and unary -- are allowed no space on one side.
4168 } elsif ($op eq '++' or $op eq '--') {
4169 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4170 if (ERROR("SPACING",
4171 "space required one side of that '$op' $at\n" . $hereptr)) {
4172 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4173 $line_fixed = 1;
4174 }
4175 }
4176 if ($ctx =~ /Wx[BE]/ ||
4177 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4178 if (ERROR("SPACING",
4179 "space prohibited before that '$op' $at\n" . $hereptr)) {
4180 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4181 $line_fixed = 1;
4182 }
4183 }
4184 if ($ctx =~ /ExW/) {
4185 if (ERROR("SPACING",
4186 "space prohibited after that '$op' $at\n" . $hereptr)) {
4187 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4188 if (defined $fix_elements[$n + 2]) {
4189 $fix_elements[$n + 2] =~ s/^\s+//;
4190 }
4191 $line_fixed = 1;
4192 }
4193 }
4194
4195 # << and >> may either have or not have spaces both sides
4196 } elsif ($op eq '<<' or $op eq '>>' or
4197 $op eq '&' or $op eq '^' or $op eq '|' or
4198 $op eq '+' or $op eq '-' or
4199 $op eq '*' or $op eq '/' or
4200 $op eq '%')
4201 {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004202 if ($check) {
4203 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4204 if (CHK("SPACING",
4205 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4206 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4207 $fix_elements[$n + 2] =~ s/^\s+//;
4208 $line_fixed = 1;
4209 }
4210 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4211 if (CHK("SPACING",
4212 "space preferred before that '$op' $at\n" . $hereptr)) {
4213 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4214 $line_fixed = 1;
4215 }
4216 }
4217 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004218 if (ERROR("SPACING",
4219 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4220 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4221 if (defined $fix_elements[$n + 2]) {
4222 $fix_elements[$n + 2] =~ s/^\s+//;
4223 }
4224 $line_fixed = 1;
4225 }
4226 }
4227
4228 # A colon needs no spaces before when it is
4229 # terminating a case value or a label.
4230 } elsif ($opv eq ':C' || $opv eq ':L') {
4231 if ($ctx =~ /Wx./) {
4232 if (ERROR("SPACING",
4233 "space prohibited before that '$op' $at\n" . $hereptr)) {
4234 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4235 $line_fixed = 1;
4236 }
4237 }
4238
4239 # All the others need spaces both sides.
4240 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4241 my $ok = 0;
4242
4243 # Ignore email addresses <foo@bar>
4244 if (($op eq '<' &&
4245 $cc =~ /^\S+\@\S+>/) ||
4246 ($op eq '>' &&
4247 $ca =~ /<\S+\@\S+$/))
4248 {
4249 $ok = 1;
4250 }
4251
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004252 # for asm volatile statements
4253 # ignore a colon with another
4254 # colon immediately before or after
4255 if (($op eq ':') &&
4256 ($ca =~ /:$/ || $cc =~ /^:/)) {
4257 $ok = 1;
4258 }
4259
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004260 # messages are ERROR, but ?: are CHK
4261 if ($ok == 0) {
4262 my $msg_type = \&ERROR;
4263 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4264
4265 if (&{$msg_type}("SPACING",
4266 "spaces required around that '$op' $at\n" . $hereptr)) {
4267 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4268 if (defined $fix_elements[$n + 2]) {
4269 $fix_elements[$n + 2] =~ s/^\s+//;
4270 }
4271 $line_fixed = 1;
4272 }
4273 }
4274 }
4275 $off += length($elements[$n + 1]);
4276
4277## print("n: <$n> GOOD: <$good>\n");
4278
4279 $fixed_line = $fixed_line . $good;
4280 }
4281
4282 if (($#elements % 2) == 0) {
4283 $fixed_line = $fixed_line . $fix_elements[$#elements];
4284 }
4285
4286 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4287 $fixed[$fixlinenr] = $fixed_line;
4288 }
4289
4290
4291 }
4292
4293# check for whitespace before a non-naked semicolon
4294 if ($line =~ /^\+.*\S\s+;\s*$/) {
4295 if (WARN("SPACING",
4296 "space prohibited before semicolon\n" . $herecurr) &&
4297 $fix) {
4298 1 while $fixed[$fixlinenr] =~
4299 s/^(\+.*\S)\s+;/$1;/;
4300 }
4301 }
4302
4303# check for multiple assignments
4304 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4305 CHK("MULTIPLE_ASSIGNMENTS",
4306 "multiple assignments should be avoided\n" . $herecurr);
4307 }
4308
4309## # check for multiple declarations, allowing for a function declaration
4310## # continuation.
4311## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4312## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4313##
4314## # Remove any bracketed sections to ensure we do not
4315## # falsly report the parameters of functions.
4316## my $ln = $line;
4317## while ($ln =~ s/\([^\(\)]*\)//g) {
4318## }
4319## if ($ln =~ /,/) {
4320## WARN("MULTIPLE_DECLARATION",
4321## "declaring multiple variables together should be avoided\n" . $herecurr);
4322## }
4323## }
4324
4325#need space before brace following if, while, etc
Anas Nashif0f3d5472016-11-07 15:57:57 -05004326 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004327 $line =~ /do\{/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004328 if (ERROR("SPACING",
4329 "space required before the open brace '{'\n" . $herecurr) &&
4330 $fix) {
4331 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
4332 }
4333 }
4334
4335## # check for blank lines before declarations
4336## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4337## $prevrawline =~ /^.\s*$/) {
4338## WARN("SPACING",
4339## "No blank lines before declarations\n" . $hereprev);
4340## }
4341##
4342
4343# closing brace should have a space following it when it has anything
4344# on the line
4345 if ($line =~ /}(?!(?:,|;|\)))\S/) {
4346 if (ERROR("SPACING",
4347 "space required after that close brace '}'\n" . $herecurr) &&
4348 $fix) {
4349 $fixed[$fixlinenr] =~
4350 s/}((?!(?:,|;|\)))\S)/} $1/;
4351 }
4352 }
4353
4354# check spacing on square brackets
4355 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4356 if (ERROR("SPACING",
4357 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4358 $fix) {
4359 $fixed[$fixlinenr] =~
4360 s/\[\s+/\[/;
4361 }
4362 }
4363 if ($line =~ /\s\]/) {
4364 if (ERROR("SPACING",
4365 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4366 $fix) {
4367 $fixed[$fixlinenr] =~
4368 s/\s+\]/\]/;
4369 }
4370 }
4371
4372# check spacing on parentheses
4373 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4374 $line !~ /for\s*\(\s+;/) {
4375 if (ERROR("SPACING",
4376 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4377 $fix) {
4378 $fixed[$fixlinenr] =~
4379 s/\(\s+/\(/;
4380 }
4381 }
4382 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4383 $line !~ /for\s*\(.*;\s+\)/ &&
4384 $line !~ /:\s+\)/) {
4385 if (ERROR("SPACING",
4386 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4387 $fix) {
4388 $fixed[$fixlinenr] =~
4389 s/\s+\)/\)/;
4390 }
4391 }
4392
4393# check unnecessary parentheses around addressof/dereference single $Lvals
4394# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4395
4396 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004397 my $var = $1;
4398 if (CHK("UNNECESSARY_PARENTHESES",
4399 "Unnecessary parentheses around $var\n" . $herecurr) &&
4400 $fix) {
4401 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4402 }
4403 }
4404
4405# check for unnecessary parentheses around function pointer uses
4406# ie: (foo->bar)(); should be foo->bar();
4407# but not "if (foo->bar) (" to avoid some false positives
4408 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4409 my $var = $2;
4410 if (CHK("UNNECESSARY_PARENTHESES",
4411 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4412 $fix) {
4413 my $var2 = deparenthesize($var);
4414 $var2 =~ s/\s//g;
4415 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4416 }
4417 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004418
4419#goto labels aren't indented, allow a single space however
4420 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4421 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4422 if (WARN("INDENTED_LABEL",
4423 "labels should not be indented\n" . $herecurr) &&
4424 $fix) {
4425 $fixed[$fixlinenr] =~
4426 s/^(.)\s+/$1/;
4427 }
4428 }
4429
4430# return is not a function
4431 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4432 my $spacing = $1;
4433 if ($^V && $^V ge 5.10.0 &&
4434 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4435 my $value = $1;
4436 $value = deparenthesize($value);
4437 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4438 ERROR("RETURN_PARENTHESES",
4439 "return is not a function, parentheses are not required\n" . $herecurr);
4440 }
4441 } elsif ($spacing !~ /\s+/) {
4442 ERROR("SPACING",
4443 "space required before the open parenthesis '('\n" . $herecurr);
4444 }
4445 }
4446
4447# unnecessary return in a void function
4448# at end-of-function, with the previous line a single leading tab, then return;
4449# and the line before that not a goto label target like "out:"
4450 if ($sline =~ /^[ \+]}\s*$/ &&
4451 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4452 $linenr >= 3 &&
4453 $lines[$linenr - 3] =~ /^[ +]/ &&
4454 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4455 WARN("RETURN_VOID",
4456 "void function return statements are not generally useful\n" . $hereprev);
4457 }
4458
4459# if statements using unnecessary parentheses - ie: if ((foo == bar))
4460 if ($^V && $^V ge 5.10.0 &&
4461 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4462 my $openparens = $1;
4463 my $count = $openparens =~ tr@\(@\(@;
4464 my $msg = "";
4465 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4466 my $comp = $4; #Not $1 because of $LvalOrFunc
4467 $msg = " - maybe == should be = ?" if ($comp eq "==");
4468 WARN("UNNECESSARY_PARENTHESES",
4469 "Unnecessary parentheses$msg\n" . $herecurr);
4470 }
4471 }
4472
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004473# comparisons with a constant or upper case identifier on the left
4474# avoid cases like "foo + BAR < baz"
4475# only fix matches surrounded by parentheses to avoid incorrect
4476# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4477 if ($^V && $^V ge 5.10.0 &&
4478 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4479 my $lead = $1;
4480 my $const = $2;
4481 my $comp = $3;
4482 my $to = $4;
4483 my $newcomp = $comp;
Anas Nashif0f3d5472016-11-07 15:57:57 -05004484 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004485 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4486 WARN("CONSTANT_COMPARISON",
4487 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4488 $fix) {
4489 if ($comp eq "<") {
4490 $newcomp = ">";
4491 } elsif ($comp eq "<=") {
4492 $newcomp = ">=";
4493 } elsif ($comp eq ">") {
4494 $newcomp = "<";
4495 } elsif ($comp eq ">=") {
4496 $newcomp = "<=";
4497 }
4498 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4499 }
4500 }
4501
4502# Return of what appears to be an errno should normally be negative
4503 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004504 my $name = $1;
4505 if ($name ne 'EOF' && $name ne 'ERROR') {
4506 WARN("USE_NEGATIVE_ERRNO",
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004507 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004508 }
4509 }
4510
4511# Need a space before open parenthesis after if, while etc
4512 if ($line =~ /\b(if|while|for|switch)\(/) {
4513 if (ERROR("SPACING",
4514 "space required before the open parenthesis '('\n" . $herecurr) &&
4515 $fix) {
4516 $fixed[$fixlinenr] =~
4517 s/\b(if|while|for|switch)\(/$1 \(/;
4518 }
4519 }
4520
4521# Check for illegal assignment in if conditional -- and check for trailing
4522# statements after the conditional.
4523 if ($line =~ /do\s*(?!{)/) {
4524 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4525 ctx_statement_block($linenr, $realcnt, 0)
4526 if (!defined $stat);
4527 my ($stat_next) = ctx_statement_block($line_nr_next,
4528 $remain_next, $off_next);
4529 $stat_next =~ s/\n./\n /g;
4530 ##print "stat<$stat> stat_next<$stat_next>\n";
4531
4532 if ($stat_next =~ /^\s*while\b/) {
4533 # If the statement carries leading newlines,
4534 # then count those as offsets.
4535 my ($whitespace) =
4536 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4537 my $offset =
4538 statement_rawlines($whitespace) - 1;
4539
4540 $suppress_whiletrailers{$line_nr_next +
4541 $offset} = 1;
4542 }
4543 }
4544 if (!defined $suppress_whiletrailers{$linenr} &&
4545 defined($stat) && defined($cond) &&
4546 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4547 my ($s, $c) = ($stat, $cond);
4548
4549 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4550 ERROR("ASSIGN_IN_IF",
4551 "do not use assignment in if condition\n" . $herecurr);
4552 }
4553
4554 # Find out what is on the end of the line after the
4555 # conditional.
4556 substr($s, 0, length($c), '');
4557 $s =~ s/\n.*//g;
4558 $s =~ s/$;//g; # Remove any comments
4559 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4560 $c !~ /}\s*while\s*/)
4561 {
4562 # Find out how long the conditional actually is.
4563 my @newlines = ($c =~ /\n/gs);
4564 my $cond_lines = 1 + $#newlines;
4565 my $stat_real = '';
4566
4567 $stat_real = raw_line($linenr, $cond_lines)
4568 . "\n" if ($cond_lines);
4569 if (defined($stat_real) && $cond_lines > 1) {
4570 $stat_real = "[...]\n$stat_real";
4571 }
4572
4573 ERROR("TRAILING_STATEMENTS",
4574 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4575 }
4576 }
4577
4578# Check for bitwise tests written as boolean
4579 if ($line =~ /
4580 (?:
4581 (?:\[|\(|\&\&|\|\|)
4582 \s*0[xX][0-9]+\s*
4583 (?:\&\&|\|\|)
4584 |
4585 (?:\&\&|\|\|)
4586 \s*0[xX][0-9]+\s*
4587 (?:\&\&|\|\||\)|\])
4588 )/x)
4589 {
4590 WARN("HEXADECIMAL_BOOLEAN_TEST",
4591 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4592 }
4593
4594# if and else should not have general statements after it
4595 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4596 my $s = $1;
4597 $s =~ s/$;//g; # Remove any comments
4598 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4599 ERROR("TRAILING_STATEMENTS",
4600 "trailing statements should be on next line\n" . $herecurr);
4601 }
4602 }
4603# if should not continue a brace
4604 if ($line =~ /}\s*if\b/) {
4605 ERROR("TRAILING_STATEMENTS",
4606 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4607 $herecurr);
4608 }
4609# case and default should not have general statements after them
4610 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4611 $line !~ /\G(?:
4612 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4613 \s*return\s+
4614 )/xg)
4615 {
4616 ERROR("TRAILING_STATEMENTS",
4617 "trailing statements should be on next line\n" . $herecurr);
4618 }
4619
4620 # Check for }<nl>else {, these must be at the same
4621 # indent level to be relevant to each other.
4622 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4623 $previndent == $indent) {
4624 if (ERROR("ELSE_AFTER_BRACE",
4625 "else should follow close brace '}'\n" . $hereprev) &&
4626 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4627 fix_delete_line($fixlinenr - 1, $prevrawline);
4628 fix_delete_line($fixlinenr, $rawline);
4629 my $fixedline = $prevrawline;
4630 $fixedline =~ s/}\s*$//;
4631 if ($fixedline !~ /^\+\s*$/) {
4632 fix_insert_line($fixlinenr, $fixedline);
4633 }
4634 $fixedline = $rawline;
4635 $fixedline =~ s/^(.\s*)else/$1} else/;
4636 fix_insert_line($fixlinenr, $fixedline);
4637 }
4638 }
4639
4640 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4641 $previndent == $indent) {
4642 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4643
4644 # Find out what is on the end of the line after the
4645 # conditional.
4646 substr($s, 0, length($c), '');
4647 $s =~ s/\n.*//g;
4648
4649 if ($s =~ /^\s*;/) {
4650 if (ERROR("WHILE_AFTER_BRACE",
4651 "while should follow close brace '}'\n" . $hereprev) &&
4652 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4653 fix_delete_line($fixlinenr - 1, $prevrawline);
4654 fix_delete_line($fixlinenr, $rawline);
4655 my $fixedline = $prevrawline;
4656 my $trailing = $rawline;
4657 $trailing =~ s/^\+//;
4658 $trailing = trim($trailing);
4659 $fixedline =~ s/}\s*$/} $trailing/;
4660 fix_insert_line($fixlinenr, $fixedline);
4661 }
4662 }
4663 }
4664
4665#Specific variable tests
4666 while ($line =~ m{($Constant|$Lval)}g) {
4667 my $var = $1;
4668
4669#gcc binary extension
4670 if ($var =~ /^$Binary$/) {
4671 if (WARN("GCC_BINARY_CONSTANT",
4672 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4673 $fix) {
4674 my $hexval = sprintf("0x%x", oct($var));
4675 $fixed[$fixlinenr] =~
4676 s/\b$var\b/$hexval/;
4677 }
4678 }
4679
4680#CamelCase
4681 if ($var !~ /^$Constant$/ &&
4682 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4683#Ignore Page<foo> variants
4684 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4685#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004686 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4687#Ignore some three character SI units explicitly, like MiB and KHz
4688 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004689 while ($var =~ m{($Ident)}g) {
4690 my $word = $1;
4691 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4692 if ($check) {
4693 seed_camelcase_includes();
4694 if (!$file && !$camelcase_file_seeded) {
4695 seed_camelcase_file($realfile);
4696 $camelcase_file_seeded = 1;
4697 }
4698 }
4699 if (!defined $camelcase{$word}) {
4700 $camelcase{$word} = 1;
4701 CHK("CAMELCASE",
4702 "Avoid CamelCase: <$word>\n" . $herecurr);
4703 }
4704 }
4705 }
4706 }
4707
4708#no spaces allowed after \ in define
4709 if ($line =~ /\#\s*define.*\\\s+$/) {
4710 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4711 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4712 $fix) {
4713 $fixed[$fixlinenr] =~ s/\s+$//;
4714 }
4715 }
4716
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004717# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4718# itself <asm/foo.h> (uses RAW line)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004719 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4720 my $file = "$1.h";
4721 my $checkfile = "include/linux/$file";
4722 if (-f "$root/$checkfile" &&
4723 $realfile ne $checkfile &&
4724 $1 !~ /$allowed_asm_includes/)
4725 {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004726 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4727 if ($asminclude > 0) {
4728 if ($realfile =~ m{^arch/}) {
4729 CHK("ARCH_INCLUDE_LINUX",
4730 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4731 } else {
4732 WARN("INCLUDE_LINUX",
4733 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4734 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004735 }
4736 }
4737 }
4738
4739# multi-statement macros should be enclosed in a do while loop, grab the
4740# first statement and ensure its the whole macro if its not enclosed
4741# in a known good container
4742 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4743 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4744 my $ln = $linenr;
4745 my $cnt = $realcnt;
4746 my ($off, $dstat, $dcond, $rest);
4747 my $ctx = '';
4748 my $has_flow_statement = 0;
4749 my $has_arg_concat = 0;
4750 ($dstat, $dcond, $ln, $cnt, $off) =
4751 ctx_statement_block($linenr, $realcnt, 0);
4752 $ctx = $dstat;
4753 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4754 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4755
4756 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Anas Nashif76b09132016-02-20 13:53:35 -05004757 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004758
Anas Nashif0f3d5472016-11-07 15:57:57 -05004759 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4760 my $define_args = $1;
4761 my $define_stmt = $dstat;
4762 my @def_args = ();
4763
4764 if (defined $define_args && $define_args ne "") {
4765 $define_args = substr($define_args, 1, length($define_args) - 2);
4766 $define_args =~ s/\s*//g;
4767 @def_args = split(",", $define_args);
4768 }
4769
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004770 $dstat =~ s/$;//g;
4771 $dstat =~ s/\\\n.//g;
4772 $dstat =~ s/^\s*//s;
4773 $dstat =~ s/\s*$//s;
4774
4775 # Flatten any parentheses and braces
4776 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4777 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Anas Nashif76b09132016-02-20 13:53:35 -05004778 $dstat =~ s/.\[[^\[\]]*\]/1/)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004779 {
4780 }
4781
4782 # Flatten any obvious string concatentation.
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004783 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4784 $dstat =~ s/$Ident\s*($String)/$1/)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004785 {
4786 }
4787
Anas Nashif0f3d5472016-11-07 15:57:57 -05004788 # Make asm volatile uses seem like a generic function
4789 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4790
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004791 my $exceptions = qr{
4792 $Declare|
4793 module_param_named|
4794 MODULE_PARM_DESC|
4795 DECLARE_PER_CPU|
4796 DEFINE_PER_CPU|
4797 __typeof__\(|
4798 union|
4799 struct|
4800 \.$Ident\s*=\s*|
Anas Nashif76b09132016-02-20 13:53:35 -05004801 ^\"|\"$|
4802 ^\[
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004803 }x;
4804 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Anas Nashif0f3d5472016-11-07 15:57:57 -05004805
4806 $ctx =~ s/\n*$//;
4807 my $herectx = $here . "\n";
4808 my $stmt_cnt = statement_rawlines($ctx);
4809
4810 for (my $n = 0; $n < $stmt_cnt; $n++) {
4811 $herectx .= raw_line($linenr, $n) . "\n";
4812 }
4813
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004814 if ($dstat ne '' &&
4815 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4816 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4817 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4818 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4819 $dstat !~ /$exceptions/ &&
4820 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4821 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4822 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4823 $dstat !~ /^for\s*$Constant$/ && # for (...)
4824 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4825 $dstat !~ /^do\s*{/ && # do {...
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004826 $dstat !~ /^\(\{/ && # ({...
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004827 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4828 {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004829
4830 if ($dstat =~ /;/) {
Anas Nashif0c8564c2016-04-17 04:50:30 +08004831 WARN("MULTISTATEMENT_MACRO_USE_DO_WHILE",
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004832 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4833 } else {
Anas Nashif0c8564c2016-04-17 04:50:30 +08004834 WARN("COMPLEX_MACRO",
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004835 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4836 }
Anas Nashif0f3d5472016-11-07 15:57:57 -05004837
4838 }
4839
4840 # Make $define_stmt single line, comment-free, etc
4841 my @stmt_array = split('\n', $define_stmt);
4842 my $first = 1;
4843 $define_stmt = "";
4844 foreach my $l (@stmt_array) {
4845 $l =~ s/\\$//;
4846 if ($first) {
4847 $define_stmt = $l;
4848 $first = 0;
4849 } elsif ($l =~ /^[\+ ]/) {
4850 $define_stmt .= substr($l, 1);
4851 }
4852 }
4853 $define_stmt =~ s/$;//g;
4854 $define_stmt =~ s/\s+/ /g;
4855 $define_stmt = trim($define_stmt);
4856
4857# check if any macro arguments are reused (ignore '...' and 'type')
4858 foreach my $arg (@def_args) {
4859 next if ($arg =~ /\.\.\./);
4860 next if ($arg =~ /^type$/i);
4861 my $tmp = $define_stmt;
4862 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
4863 $tmp =~ s/\#+\s*$arg\b//g;
4864 $tmp =~ s/\b$arg\s*\#\#//g;
4865 my $use_cnt = $tmp =~ s/\b$arg\b//g;
4866 if ($use_cnt > 1) {
4867 CHK("MACRO_ARG_REUSE",
4868 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4869 }
4870# check if any macro arguments may have other precedence issues
4871 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4872 ((defined($1) && $1 ne ',') ||
4873 (defined($2) && $2 ne ','))) {
4874 CHK("MACRO_ARG_PRECEDENCE",
4875 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
4876 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004877 }
4878
4879# check for macros with flow control, but without ## concatenation
4880# ## concatenation is commonly a macro that defines a function so ignore those
4881 if ($has_flow_statement && !$has_arg_concat) {
4882 my $herectx = $here . "\n";
4883 my $cnt = statement_rawlines($ctx);
4884
4885 for (my $n = 0; $n < $cnt; $n++) {
4886 $herectx .= raw_line($linenr, $n) . "\n";
4887 }
4888 WARN("MACRO_WITH_FLOW_CONTROL",
4889 "Macros with flow control statements should be avoided\n" . "$herectx");
4890 }
4891
4892# check for line continuations outside of #defines, preprocessor #, and asm
4893
4894 } else {
4895 if ($prevline !~ /^..*\\$/ &&
4896 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4897 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4898 $line =~ /^\+.*\\$/) {
4899 WARN("LINE_CONTINUATIONS",
4900 "Avoid unnecessary line continuations\n" . $herecurr);
4901 }
4902 }
4903
4904# do {} while (0) macro tests:
4905# single-statement macros do not need to be enclosed in do while (0) loop,
4906# macro should not end with a semicolon
4907 if ($^V && $^V ge 5.10.0 &&
4908 $realfile !~ m@/vmlinux.lds.h$@ &&
4909 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4910 my $ln = $linenr;
4911 my $cnt = $realcnt;
4912 my ($off, $dstat, $dcond, $rest);
4913 my $ctx = '';
4914 ($dstat, $dcond, $ln, $cnt, $off) =
4915 ctx_statement_block($linenr, $realcnt, 0);
4916 $ctx = $dstat;
4917
4918 $dstat =~ s/\\\n.//g;
Anas Nashifcbfb62c2015-10-14 06:08:54 -04004919 $dstat =~ s/$;/ /g;
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07004920
4921 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4922 my $stmts = $2;
4923 my $semis = $3;
4924
4925 $ctx =~ s/\n*$//;
4926 my $cnt = statement_rawlines($ctx);
4927 my $herectx = $here . "\n";
4928
4929 for (my $n = 0; $n < $cnt; $n++) {
4930 $herectx .= raw_line($linenr, $n) . "\n";
4931 }
4932
4933 if (($stmts =~ tr/;/;/) == 1 &&
4934 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4935 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4936 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4937 }
4938 if (defined $semis && $semis ne "") {
4939 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4940 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4941 }
4942 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4943 $ctx =~ s/\n*$//;
4944 my $cnt = statement_rawlines($ctx);
4945 my $herectx = $here . "\n";
4946
4947 for (my $n = 0; $n < $cnt; $n++) {
4948 $herectx .= raw_line($linenr, $n) . "\n";
4949 }
4950
4951 WARN("TRAILING_SEMICOLON",
4952 "macros should not use a trailing semicolon\n" . "$herectx");
4953 }
4954 }
4955
4956# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4957# all assignments may have only one of the following with an assignment:
4958# .
4959# ALIGN(...)
4960# VMLINUX_SYMBOL(...)
4961 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4962 WARN("MISSING_VMLINUX_SYMBOL",
4963 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4964 }
4965
4966# check for redundant bracing round if etc
4967 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4968 my ($level, $endln, @chunks) =
4969 ctx_statement_full($linenr, $realcnt, 1);
4970 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4971 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4972 if ($#chunks > 0 && $level == 0) {
4973 my @allowed = ();
4974 my $allow = 0;
4975 my $seen = 0;
4976 my $herectx = $here . "\n";
4977 my $ln = $linenr - 1;
4978 for my $chunk (@chunks) {
4979 my ($cond, $block) = @{$chunk};
4980
4981 # If the condition carries leading newlines, then count those as offsets.
4982 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4983 my $offset = statement_rawlines($whitespace) - 1;
4984
4985 $allowed[$allow] = 0;
4986 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4987
4988 # We have looked at and allowed this specific line.
4989 $suppress_ifbraces{$ln + $offset} = 1;
4990
4991 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4992 $ln += statement_rawlines($block) - 1;
4993
4994 substr($block, 0, length($cond), '');
4995
4996 $seen++ if ($block =~ /^\s*{/);
4997
4998 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4999 if (statement_lines($cond) > 1) {
5000 #print "APW: ALLOWED: cond<$cond>\n";
5001 $allowed[$allow] = 1;
5002 }
5003 if ($block =~/\b(?:if|for|while)\b/) {
5004 #print "APW: ALLOWED: block<$block>\n";
5005 $allowed[$allow] = 1;
5006 }
5007 if (statement_block_size($block) > 1) {
5008 #print "APW: ALLOWED: lines block<$block>\n";
5009 $allowed[$allow] = 1;
5010 }
5011 $allow++;
5012 }
5013 if ($seen) {
5014 my $sum_allowed = 0;
5015 foreach (@allowed) {
5016 $sum_allowed += $_;
5017 }
5018 if ($sum_allowed == 0) {
5019 WARN("BRACES",
5020 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5021 } elsif ($sum_allowed != $allow &&
5022 $seen != $allow) {
5023 CHK("BRACES",
5024 "braces {} should be used on all arms of this statement\n" . $herectx);
5025 }
5026 }
5027 }
5028 }
5029 if (!defined $suppress_ifbraces{$linenr - 1} &&
5030 $line =~ /\b(if|while|for|else)\b/) {
5031 my $allowed = 0;
5032
5033 # Check the pre-context.
5034 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5035 #print "APW: ALLOWED: pre<$1>\n";
5036 $allowed = 1;
5037 }
5038
5039 my ($level, $endln, @chunks) =
5040 ctx_statement_full($linenr, $realcnt, $-[0]);
5041
5042 # Check the condition.
5043 my ($cond, $block) = @{$chunks[0]};
5044 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5045 if (defined $cond) {
5046 substr($block, 0, length($cond), '');
5047 }
5048 if (statement_lines($cond) > 1) {
5049 #print "APW: ALLOWED: cond<$cond>\n";
5050 $allowed = 1;
5051 }
5052 if ($block =~/\b(?:if|for|while)\b/) {
5053 #print "APW: ALLOWED: block<$block>\n";
5054 $allowed = 1;
5055 }
5056 if (statement_block_size($block) > 1) {
5057 #print "APW: ALLOWED: lines block<$block>\n";
5058 $allowed = 1;
5059 }
5060 # Check the post-context.
5061 if (defined $chunks[1]) {
5062 my ($cond, $block) = @{$chunks[1]};
5063 if (defined $cond) {
5064 substr($block, 0, length($cond), '');
5065 }
5066 if ($block =~ /^\s*\{/) {
5067 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5068 $allowed = 1;
5069 }
5070 }
5071 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5072 my $herectx = $here . "\n";
5073 my $cnt = statement_rawlines($block);
5074
5075 for (my $n = 0; $n < $cnt; $n++) {
5076 $herectx .= raw_line($linenr, $n) . "\n";
5077 }
5078
5079 WARN("BRACES",
5080 "braces {} are not necessary for single statement blocks\n" . $herectx);
5081 }
5082 }
5083
5084# check for unnecessary blank lines around braces
5085 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005086 if (CHK("BRACES",
5087 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5088 $fix && $prevrawline =~ /^\+/) {
5089 fix_delete_line($fixlinenr - 1, $prevrawline);
5090 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005091 }
5092 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005093 if (CHK("BRACES",
5094 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5095 $fix) {
5096 fix_delete_line($fixlinenr, $rawline);
5097 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005098 }
5099
5100# no volatiles please
5101 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5102 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5103 WARN("VOLATILE",
5104 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
5105 }
5106
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005107# Check for user-visible strings broken across lines, which breaks the ability
5108# to grep for the string. Make exceptions when the previous string ends in a
5109# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5110# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5111 if ($line =~ /^\+\s*$String/ &&
5112 $prevline =~ /"\s*$/ &&
5113 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5114 if (WARN("SPLIT_STRING",
5115 "quoted string split across lines\n" . $hereprev) &&
5116 $fix &&
5117 $prevrawline =~ /^\+.*"\s*$/ &&
5118 $last_coalesced_string_linenr != $linenr - 1) {
5119 my $extracted_string = get_quoted_string($line, $rawline);
5120 my $comma_close = "";
5121 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5122 $comma_close = $1;
5123 }
5124
5125 fix_delete_line($fixlinenr - 1, $prevrawline);
5126 fix_delete_line($fixlinenr, $rawline);
5127 my $fixedline = $prevrawline;
5128 $fixedline =~ s/"\s*$//;
5129 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5130 fix_insert_line($fixlinenr - 1, $fixedline);
5131 $fixedline = $rawline;
5132 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5133 if ($fixedline !~ /\+\s*$/) {
5134 fix_insert_line($fixlinenr, $fixedline);
5135 }
5136 $last_coalesced_string_linenr = $linenr;
5137 }
5138 }
5139
5140# check for missing a space in a string concatenation
5141 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5142 WARN('MISSING_SPACE',
5143 "break quoted strings at a space character\n" . $hereprev);
5144 }
5145
5146# check for spaces before a quoted newline
5147 if ($rawline =~ /^.*\".*\s\\n/) {
5148 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5149 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5150 $fix) {
5151 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5152 }
5153
5154 }
5155
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005156# concatenated string without spaces between elements
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005157 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005158 CHK("CONCATENATED_STRING",
5159 "Concatenated strings should use spaces between elements\n" . $herecurr);
5160 }
5161
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005162# uncoalesced string fragments
5163 if ($line =~ /$String\s*"/) {
5164 WARN("STRING_FRAGMENTS",
5165 "Consecutive strings are generally better as a single string\n" . $herecurr);
5166 }
5167
5168# check for %L{u,d,i} and 0x%[udi] in strings
5169 my $string;
5170 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5171 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5172 $string =~ s/%%/__/g;
5173 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
5174 WARN("PRINTF_L",
5175 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5176 last;
5177 }
5178 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5179 ERROR("PRINTF_0xDECIMAL",
5180 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5181 }
5182 }
5183
5184# check for line continuations in quoted strings with odd counts of "
5185 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5186 WARN("LINE_CONTINUATIONS",
5187 "Avoid line continuations in quoted strings\n" . $herecurr);
5188 }
5189
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005190# warn about #if 0
5191 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5192 CHK("REDUNDANT_CODE",
5193 "if this code is redundant consider removing it\n" .
5194 $herecurr);
5195 }
5196
5197# check for needless "if (<foo>) fn(<foo>)" uses
5198 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005199 my $tested = quotemeta($1);
5200 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5201 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5202 my $func = $1;
5203 if (WARN('NEEDLESS_IF',
5204 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5205 $fix) {
5206 my $do_fix = 1;
5207 my $leading_tabs = "";
5208 my $new_leading_tabs = "";
5209 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5210 $leading_tabs = $1;
5211 } else {
5212 $do_fix = 0;
5213 }
5214 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5215 $new_leading_tabs = $1;
5216 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5217 $do_fix = 0;
5218 }
5219 } else {
5220 $do_fix = 0;
5221 }
5222 if ($do_fix) {
5223 fix_delete_line($fixlinenr - 1, $prevrawline);
5224 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5225 }
5226 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005227 }
5228 }
5229
5230# check for unnecessary "Out of Memory" messages
5231 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5232 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5233 (defined $1 || defined $3) &&
5234 $linenr > 3) {
5235 my $testval = $2;
5236 my $testline = $lines[$linenr - 3];
5237
5238 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5239# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5240
5241 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5242 WARN("OOM_MESSAGE",
5243 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5244 }
5245 }
5246
5247# check for logging functions with KERN_<LEVEL>
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005248 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005249 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5250 my $level = $1;
5251 if (WARN("UNNECESSARY_KERN_LEVEL",
5252 "Possible unnecessary $level\n" . $herecurr) &&
5253 $fix) {
5254 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5255 }
5256 }
5257
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005258# check for mask then right shift without a parentheses
5259 if ($^V && $^V ge 5.10.0 &&
5260 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5261 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5262 WARN("MASK_THEN_SHIFT",
5263 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5264 }
5265
5266# check for pointer comparisons to NULL
5267 if ($^V && $^V ge 5.10.0) {
5268 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5269 my $val = $1;
5270 my $equal = "!";
5271 $equal = "" if ($4 eq "!=");
5272 if (CHK("COMPARISON_TO_NULL",
5273 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5274 $fix) {
5275 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5276 }
5277 }
5278 }
5279
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005280# check for bad placement of section $InitAttribute (e.g.: __initdata)
5281 if ($line =~ /(\b$InitAttribute\b)/) {
5282 my $attr = $1;
5283 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5284 my $ptr = $1;
5285 my $var = $2;
5286 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5287 ERROR("MISPLACED_INIT",
5288 "$attr should be placed after $var\n" . $herecurr)) ||
5289 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5290 WARN("MISPLACED_INIT",
5291 "$attr should be placed after $var\n" . $herecurr))) &&
5292 $fix) {
5293 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
5294 }
5295 }
5296 }
5297
5298# check for $InitAttributeData (ie: __initdata) with const
5299 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5300 my $attr = $1;
5301 $attr =~ /($InitAttributePrefix)(.*)/;
5302 my $attr_prefix = $1;
5303 my $attr_type = $2;
5304 if (ERROR("INIT_ATTRIBUTE",
5305 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5306 $fix) {
5307 $fixed[$fixlinenr] =~
5308 s/$InitAttributeData/${attr_prefix}initconst/;
5309 }
5310 }
5311
5312# check for $InitAttributeConst (ie: __initconst) without const
5313 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5314 my $attr = $1;
5315 if (ERROR("INIT_ATTRIBUTE",
5316 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5317 $fix) {
5318 my $lead = $fixed[$fixlinenr] =~
5319 /(^\+\s*(?:static\s+))/;
5320 $lead = rtrim($1);
5321 $lead = "$lead " if ($lead !~ /^\+$/);
5322 $lead = "${lead}const ";
5323 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5324 }
5325 }
5326
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005327# check for __read_mostly with const non-pointer (should just be const)
5328 if ($line =~ /\b__read_mostly\b/ &&
5329 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5330 if (ERROR("CONST_READ_MOSTLY",
5331 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5332 $fix) {
5333 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5334 }
5335 }
5336
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005337# don't use __constant_<foo> functions outside of include/uapi/
5338 if ($realfile !~ m@^include/uapi/@ &&
5339 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5340 my $constant_func = $1;
5341 my $func = $constant_func;
5342 $func =~ s/^__constant_//;
5343 if (WARN("CONSTANT_CONVERSION",
5344 "$constant_func should be $func\n" . $herecurr) &&
5345 $fix) {
5346 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5347 }
5348 }
5349
5350# prefer usleep_range over udelay
5351 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5352 my $delay = $1;
5353 # ignore udelay's < 10, however
5354 if (! ($delay < 10) ) {
5355 CHK("USLEEP_RANGE",
5356 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5357 }
5358 if ($delay > 2000) {
5359 WARN("LONG_UDELAY",
5360 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5361 }
5362 }
5363
5364# warn about unexpectedly long msleep's
5365 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5366 if ($1 < 20) {
5367 WARN("MSLEEP",
5368 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5369 }
5370 }
5371
5372# check for comparisons of jiffies
5373 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5374 WARN("JIFFIES_COMPARISON",
5375 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5376 }
5377
5378# check for comparisons of get_jiffies_64()
5379 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5380 WARN("JIFFIES_COMPARISON",
5381 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5382 }
5383
5384# warn about #ifdefs in C files
5385# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5386# print "#ifdef in C files should be avoided\n";
5387# print "$herecurr";
5388# $clean = 0;
5389# }
5390
5391# warn about spacing in #ifdefs
5392 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5393 if (ERROR("SPACING",
5394 "exactly one space required after that #$1\n" . $herecurr) &&
5395 $fix) {
5396 $fixed[$fixlinenr] =~
5397 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5398 }
5399
5400 }
5401
5402# check for spinlock_t definitions without a comment.
5403 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5404 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5405 my $which = $1;
5406 if (!ctx_has_comment($first_line, $linenr)) {
5407 CHK("UNCOMMENTED_DEFINITION",
5408 "$1 definition without comment\n" . $herecurr);
5409 }
5410 }
5411# check for memory barriers without a comment.
Anas Nashif76b09132016-02-20 13:53:35 -05005412
5413 my $barriers = qr{
5414 mb|
5415 rmb|
5416 wmb|
5417 read_barrier_depends
5418 }x;
5419 my $barrier_stems = qr{
5420 mb__before_atomic|
5421 mb__after_atomic|
5422 store_release|
5423 load_acquire|
5424 store_mb|
5425 (?:$barriers)
5426 }x;
5427 my $all_barriers = qr{
5428 (?:$barriers)|
5429 smp_(?:$barrier_stems)|
5430 virt_(?:$barrier_stems)
5431 }x;
5432
5433 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005434 if (!ctx_has_comment($first_line, $linenr)) {
5435 WARN("MEMORY_BARRIER",
5436 "memory barrier without comment\n" . $herecurr);
5437 }
5438 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005439
Anas Nashif76b09132016-02-20 13:53:35 -05005440 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5441
5442 if ($realfile !~ m@^include/asm-generic/@ &&
5443 $realfile !~ m@/barrier\.h$@ &&
5444 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5445 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5446 WARN("MEMORY_BARRIER",
5447 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5448 }
5449
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005450# check for waitqueue_active without a comment.
5451 if ($line =~ /\bwaitqueue_active\s*\(/) {
5452 if (!ctx_has_comment($first_line, $linenr)) {
5453 WARN("WAITQUEUE_ACTIVE",
5454 "waitqueue_active without comment\n" . $herecurr);
5455 }
5456 }
5457
5458# Check for expedited grace periods that interrupt non-idle non-nohz
5459# online CPUs. These expedited can therefore degrade real-time response
5460# if used carelessly, and should be avoided where not absolutely
5461# needed. It is always OK to use synchronize_rcu_expedited() and
5462# synchronize_sched_expedited() at boot time (before real-time applications
5463# start) and in error situations where real-time response is compromised in
5464# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5465# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5466# Of course, nothing comes for free, and srcu_read_lock() and
5467# srcu_read_unlock() do contain full memory barriers in payment for
5468# synchronize_srcu_expedited() non-interruption properties.
5469 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5470 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5471 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5472
5473 }
5474
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005475# check of hardware specific defines
5476 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5477 CHK("ARCH_DEFINES",
5478 "architecture specific defines should be avoided\n" . $herecurr);
5479 }
5480
5481# Check that the storage class is at the beginning of a declaration
5482 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5483 WARN("STORAGE_CLASS",
5484 "storage class should be at the beginning of the declaration\n" . $herecurr)
5485 }
5486
5487# check the location of the inline attribute, that it is between
5488# storage class and type.
5489 if ($line =~ /\b$Type\s+$Inline\b/ ||
5490 $line =~ /\b$Inline\s+$Storage\b/) {
5491 ERROR("INLINE_LOCATION",
5492 "inline keyword should sit between storage class and type\n" . $herecurr);
5493 }
5494
5495# Check for __inline__ and __inline, prefer inline
5496 if ($realfile !~ m@\binclude/uapi/@ &&
5497 $line =~ /\b(__inline__|__inline)\b/) {
5498 if (WARN("INLINE",
5499 "plain inline is preferred over $1\n" . $herecurr) &&
5500 $fix) {
5501 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5502
5503 }
5504 }
5505
5506# Check for __attribute__ packed, prefer __packed
5507 if ($realfile !~ m@\binclude/uapi/@ &&
5508 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5509 WARN("PREFER_PACKED",
5510 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5511 }
5512
5513# Check for __attribute__ aligned, prefer __aligned
5514 if ($realfile !~ m@\binclude/uapi/@ &&
5515 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5516 WARN("PREFER_ALIGNED",
5517 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5518 }
5519
5520# Check for __attribute__ format(printf, prefer __printf
5521 if ($realfile !~ m@\binclude/uapi/@ &&
5522 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5523 if (WARN("PREFER_PRINTF",
5524 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5525 $fix) {
5526 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5527
5528 }
5529 }
5530
5531# Check for __attribute__ format(scanf, prefer __scanf
5532 if ($realfile !~ m@\binclude/uapi/@ &&
5533 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5534 if (WARN("PREFER_SCANF",
5535 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5536 $fix) {
5537 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5538 }
5539 }
5540
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005541# Check for __attribute__ weak, or __weak declarations (may have link issues)
5542 if ($^V && $^V ge 5.10.0 &&
5543 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5544 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5545 $line =~ /\b__weak\b/)) {
5546 ERROR("WEAK_DECLARATION",
5547 "Using weak declarations can have unintended link defects\n" . $herecurr);
5548 }
5549
5550# check for c99 types like uint8_t used outside of uapi/
5551 if ($realfile !~ m@\binclude/uapi/@ &&
5552 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5553 my $type = $1;
5554 if ($type =~ /\b($typeC99Typedefs)\b/) {
5555 $type = $1;
5556 my $kernel_type = 'u';
5557 $kernel_type = 's' if ($type =~ /^_*[si]/);
5558 $type =~ /(\d+)/;
5559 $kernel_type .= $1;
5560 if (CHK("PREFER_KERNEL_TYPES",
5561 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5562 $fix) {
5563 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5564 }
5565 }
5566 }
5567
Anas Nashif76b09132016-02-20 13:53:35 -05005568# check for cast of C90 native int or longer types constants
5569 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5570 my $cast = $1;
5571 my $const = $2;
5572 if (WARN("TYPECAST_INT_CONSTANT",
5573 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5574 $fix) {
5575 my $suffix = "";
5576 my $newconst = $const;
5577 $newconst =~ s/${Int_type}$//;
5578 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5579 if ($cast =~ /\blong\s+long\b/) {
5580 $suffix .= 'LL';
5581 } elsif ($cast =~ /\blong\b/) {
5582 $suffix .= 'L';
5583 }
5584 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5585 }
5586 }
5587
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005588# check for sizeof(&)
5589 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5590 WARN("SIZEOF_ADDRESS",
5591 "sizeof(& should be avoided\n" . $herecurr);
5592 }
5593
5594# check for sizeof without parenthesis
5595 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5596 if (WARN("SIZEOF_PARENTHESIS",
5597 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5598 $fix) {
5599 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5600 }
5601 }
5602
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005603# check for struct spinlock declarations
5604 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5605 WARN("USE_SPINLOCK_T",
5606 "struct spinlock should be spinlock_t\n" . $herecurr);
5607 }
5608
5609# check for seq_printf uses that could be seq_puts
5610 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5611 my $fmt = get_quoted_string($line, $rawline);
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005612 $fmt =~ s/%%//g;
5613 if ($fmt !~ /%/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005614 if (WARN("PREFER_SEQ_PUTS",
5615 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5616 $fix) {
5617 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5618 }
5619 }
5620 }
5621
5622# Check for misused memsets
5623 if ($^V && $^V ge 5.10.0 &&
5624 defined $stat &&
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005625 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005626
5627 my $ms_addr = $2;
5628 my $ms_val = $7;
5629 my $ms_size = $12;
5630
5631 if ($ms_size =~ /^(0x|)0$/i) {
5632 ERROR("MEMSET",
5633 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5634 } elsif ($ms_size =~ /^(0x|)1$/i) {
5635 WARN("MEMSET",
5636 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5637 }
5638 }
5639
5640# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Anas Nashif0f3d5472016-11-07 15:57:57 -05005641# if ($^V && $^V ge 5.10.0 &&
5642# defined $stat &&
5643# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5644# if (WARN("PREFER_ETHER_ADDR_COPY",
5645# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5646# $fix) {
5647# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5648# }
5649# }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005650
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005651# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Anas Nashif0f3d5472016-11-07 15:57:57 -05005652# if ($^V && $^V ge 5.10.0 &&
5653# defined $stat &&
5654# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5655# WARN("PREFER_ETHER_ADDR_EQUAL",
5656# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5657# }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005658
5659# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5660# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Anas Nashif0f3d5472016-11-07 15:57:57 -05005661# if ($^V && $^V ge 5.10.0 &&
5662# defined $stat &&
5663# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5664#
5665# my $ms_val = $7;
5666#
5667# if ($ms_val =~ /^(?:0x|)0+$/i) {
5668# if (WARN("PREFER_ETH_ZERO_ADDR",
5669# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5670# $fix) {
5671# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5672# }
5673# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5674# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5675# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5676# $fix) {
5677# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5678# }
5679# }
5680# }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005681
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005682# typecasts on min/max could be min_t/max_t
5683 if ($^V && $^V ge 5.10.0 &&
5684 defined $stat &&
5685 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5686 if (defined $2 || defined $7) {
5687 my $call = $1;
5688 my $cast1 = deparenthesize($2);
5689 my $arg1 = $3;
5690 my $cast2 = deparenthesize($7);
5691 my $arg2 = $8;
5692 my $cast;
5693
5694 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5695 $cast = "$cast1 or $cast2";
5696 } elsif ($cast1 ne "") {
5697 $cast = $cast1;
5698 } else {
5699 $cast = $cast2;
5700 }
5701 WARN("MINMAX",
5702 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5703 }
5704 }
5705
5706# check usleep_range arguments
5707 if ($^V && $^V ge 5.10.0 &&
5708 defined $stat &&
5709 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5710 my $min = $1;
5711 my $max = $7;
5712 if ($min eq $max) {
5713 WARN("USLEEP_RANGE",
5714 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5715 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5716 $min > $max) {
5717 WARN("USLEEP_RANGE",
5718 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5719 }
5720 }
5721
5722# check for naked sscanf
5723 if ($^V && $^V ge 5.10.0 &&
5724 defined $stat &&
5725 $line =~ /\bsscanf\b/ &&
5726 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5727 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5728 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5729 my $lc = $stat =~ tr@\n@@;
5730 $lc = $lc + $linenr;
5731 my $stat_real = raw_line($linenr, 0);
5732 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5733 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5734 }
5735 WARN("NAKED_SSCANF",
5736 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5737 }
5738
5739# check for simple sscanf that should be kstrto<foo>
5740 if ($^V && $^V ge 5.10.0 &&
5741 defined $stat &&
5742 $line =~ /\bsscanf\b/) {
5743 my $lc = $stat =~ tr@\n@@;
5744 $lc = $lc + $linenr;
5745 my $stat_real = raw_line($linenr, 0);
5746 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5747 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5748 }
5749 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5750 my $format = $6;
5751 my $count = $format =~ tr@%@%@;
5752 if ($count == 1 &&
5753 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5754 WARN("SSCANF_TO_KSTRTO",
5755 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5756 }
5757 }
5758 }
5759
5760# check for new externs in .h files.
5761 if ($realfile =~ /\.h$/ &&
5762 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5763 if (CHK("AVOID_EXTERNS",
5764 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
5765 $fix) {
5766 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
5767 }
5768 }
5769
5770# check for new externs in .c files.
5771 if ($realfile =~ /\.c$/ && defined $stat &&
5772 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5773 {
5774 my $function_name = $1;
5775 my $paren_space = $2;
5776
5777 my $s = $stat;
5778 if (defined $cond) {
5779 substr($s, 0, length($cond), '');
5780 }
5781 if ($s =~ /^\s*;/ &&
5782 $function_name ne 'uninitialized_var')
5783 {
5784 WARN("AVOID_EXTERNS",
5785 "externs should be avoided in .c files\n" . $herecurr);
5786 }
5787
5788 if ($paren_space =~ /\n/) {
5789 WARN("FUNCTION_ARGUMENTS",
5790 "arguments for function declarations should follow identifier\n" . $herecurr);
5791 }
5792
5793 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5794 $stat =~ /^.\s*extern\s+/)
5795 {
5796 WARN("AVOID_EXTERNS",
5797 "externs should be avoided in .c files\n" . $herecurr);
5798 }
5799
Anas Nashif0f3d5472016-11-07 15:57:57 -05005800 if ($realfile =~ /\.[ch]$/ && defined $stat &&
5801 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5802 $1 ne "void") {
5803 my $args = trim($1);
5804 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5805 my $arg = trim($1);
5806 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5807 WARN("FUNCTION_ARGUMENTS",
5808 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5809 }
5810 }
5811 }
5812
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005813# checks for new __setup's
5814 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5815 my $name = $1;
5816
5817 if (!grep(/$name/, @setup_docs)) {
5818 CHK("UNDOCUMENTED_SETUP",
5819 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5820 }
5821 }
5822
5823# check for pointless casting of kmalloc return
5824 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5825 WARN("UNNECESSARY_CASTS",
5826 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
5827 }
5828
5829# alloc style
5830# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5831 if ($^V && $^V ge 5.10.0 &&
5832 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5833 CHK("ALLOC_SIZEOF_STRUCT",
5834 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5835 }
5836
5837# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5838 if ($^V && $^V ge 5.10.0 &&
5839 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
5840 my $oldfunc = $3;
5841 my $a1 = $4;
5842 my $a2 = $10;
5843 my $newfunc = "kmalloc_array";
5844 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
5845 my $r1 = $a1;
5846 my $r2 = $a2;
5847 if ($a1 =~ /^sizeof\s*\S/) {
5848 $r1 = $a2;
5849 $r2 = $a1;
5850 }
5851 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5852 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5853 if (WARN("ALLOC_WITH_MULTIPLY",
5854 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5855 $fix) {
5856 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
5857
5858 }
5859 }
5860 }
5861
5862# check for krealloc arg reuse
5863 if ($^V && $^V ge 5.10.0 &&
5864 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5865 WARN("KREALLOC_ARG_REUSE",
5866 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5867 }
5868
5869# check for alloc argument mismatch
5870 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5871 WARN("ALLOC_ARRAY_ARGS",
5872 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5873 }
5874
5875# check for multiple semicolons
5876 if ($line =~ /;\s*;\s*$/) {
5877 if (WARN("ONE_SEMICOLON",
5878 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5879 $fix) {
5880 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5881 }
5882 }
5883
Anas Nashif0f3d5472016-11-07 15:57:57 -05005884# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5885 if ($realfile !~ m@^include/uapi/@ &&
5886 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005887 my $ull = "";
5888 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5889 if (CHK("BIT_MACRO",
5890 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5891 $fix) {
5892 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5893 }
5894 }
5895
Anas Nashif0f3d5472016-11-07 15:57:57 -05005896# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
5897 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
5898 my $config = $1;
5899 if (WARN("PREFER_IS_ENABLED",
5900 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
5901 $fix) {
5902 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
5903 }
5904 }
5905
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005906# check for case / default statements not preceded by break/fallthrough/switch
5907 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5908 my $has_break = 0;
5909 my $has_statement = 0;
5910 my $count = 0;
5911 my $prevline = $linenr;
5912 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5913 $prevline--;
5914 my $rline = $rawlines[$prevline - 1];
5915 my $fline = $lines[$prevline - 1];
5916 last if ($fline =~ /^\@\@/);
5917 next if ($fline =~ /^\-/);
5918 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5919 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5920 next if ($fline =~ /^.[\s$;]*$/);
5921 $has_statement = 1;
5922 $count++;
5923 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5924 }
5925 if (!$has_break && $has_statement) {
5926 WARN("MISSING_BREAK",
5927 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5928 }
5929 }
5930
5931# check for switch/default statements without a break;
5932 if ($^V && $^V ge 5.10.0 &&
5933 defined $stat &&
5934 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5935 my $ctx = '';
5936 my $herectx = $here . "\n";
5937 my $cnt = statement_rawlines($stat);
5938 for (my $n = 0; $n < $cnt; $n++) {
5939 $herectx .= raw_line($linenr, $n) . "\n";
5940 }
5941 WARN("DEFAULT_NO_BREAK",
5942 "switch default: should use break\n" . $herectx);
5943 }
5944
5945# check for gcc specific __FUNCTION__
5946 if ($line =~ /\b__FUNCTION__\b/) {
5947 if (WARN("USE_FUNC",
5948 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5949 $fix) {
5950 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5951 }
5952 }
5953
Anas Nashifcbfb62c2015-10-14 06:08:54 -04005954# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5955 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5956 ERROR("DATE_TIME",
5957 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5958 }
5959
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005960# check for use of yield()
5961 if ($line =~ /\byield\s*\(\s*\)/) {
5962 WARN("YIELD",
5963 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5964 }
5965
5966# check for comparisons against true and false
5967 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5968 my $lead = $1;
5969 my $arg = $2;
5970 my $test = $3;
5971 my $otype = $4;
5972 my $trail = $5;
5973 my $op = "!";
5974
5975 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5976
5977 my $type = lc($otype);
5978 if ($type =~ /^(?:true|false)$/) {
5979 if (("$test" eq "==" && "$type" eq "true") ||
5980 ("$test" eq "!=" && "$type" eq "false")) {
5981 $op = "";
5982 }
5983
5984 CHK("BOOL_COMPARISON",
5985 "Using comparison to $otype is error prone\n" . $herecurr);
5986
5987## maybe suggesting a correct construct would better
5988## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5989
5990 }
5991 }
5992
5993# check for semaphores initialized locked
5994 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5995 WARN("CONSIDER_COMPLETION",
5996 "consider using a completion\n" . $herecurr);
5997 }
5998
5999# recommend kstrto* over simple_strto* and strict_strto*
6000 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6001 WARN("CONSIDER_KSTRTO",
6002 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6003 }
6004
6005# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6006 if ($line =~ /^.\s*__initcall\s*\(/) {
6007 WARN("USE_DEVICE_INITCALL",
6008 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6009 }
6010
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006011# check for various structs that are normally const (ops, kgdb, device_tree)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006012 if ($line !~ /\bconst\b/ &&
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006013 $line =~ /\bstruct\s+($const_structs)\b/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006014 WARN("CONST_STRUCT",
6015 "struct $1 should normally be const\n" .
6016 $herecurr);
6017 }
6018
6019# use of NR_CPUS is usually wrong
6020# ignore definitions of NR_CPUS and usage to define arrays as likely right
6021 if ($line =~ /\bNR_CPUS\b/ &&
6022 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6023 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6024 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6025 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6026 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6027 {
6028 WARN("NR_CPUS",
6029 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6030 }
6031
6032# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6033 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6034 ERROR("DEFINE_ARCH_HAS",
6035 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6036 }
6037
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006038# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6039 if ($^V && $^V ge 5.10.0 &&
6040 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6041 WARN("LIKELY_MISUSE",
6042 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006043 }
6044
6045# whine mightly about in_atomic
6046 if ($line =~ /\bin_atomic\s*\(/) {
6047 if ($realfile =~ m@^drivers/@) {
6048 ERROR("IN_ATOMIC",
6049 "do not use in_atomic in drivers\n" . $herecurr);
6050 } elsif ($realfile !~ m@^kernel/@) {
6051 WARN("IN_ATOMIC",
6052 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6053 }
6054 }
6055
Anas Nashif0f3d5472016-11-07 15:57:57 -05006056# whine about ACCESS_ONCE
6057 if ($^V && $^V ge 5.10.0 &&
6058 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6059 my $par = $1;
6060 my $eq = $2;
6061 my $fun = $3;
6062 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6063 if (defined($eq)) {
6064 if (WARN("PREFER_WRITE_ONCE",
6065 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6066 $fix) {
6067 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6068 }
6069 } else {
6070 if (WARN("PREFER_READ_ONCE",
6071 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6072 $fix) {
6073 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6074 }
6075 }
6076 }
6077
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006078# check for lockdep_set_novalidate_class
6079 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6080 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6081 if ($realfile !~ m@^kernel/lockdep@ &&
6082 $realfile !~ m@^include/linux/lockdep@ &&
6083 $realfile !~ m@^drivers/base/core@) {
6084 ERROR("LOCKDEP",
6085 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6086 }
6087 }
6088
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006089 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6090 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006091 WARN("EXPORTED_WORLD_WRITABLE",
6092 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6093 }
6094
6095# Mode permission misuses where it seems decimal should be octal
6096# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6097 if ($^V && $^V ge 5.10.0 &&
Anas Nashif0f3d5472016-11-07 15:57:57 -05006098 defined $stat &&
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006099 $line =~ /$mode_perms_search/) {
6100 foreach my $entry (@mode_permission_funcs) {
6101 my $func = $entry->[0];
6102 my $arg_pos = $entry->[1];
6103
Anas Nashif0f3d5472016-11-07 15:57:57 -05006104 my $lc = $stat =~ tr@\n@@;
6105 $lc = $lc + $linenr;
6106 my $stat_real = raw_line($linenr, 0);
6107 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6108 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6109 }
6110
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006111 my $skip_args = "";
6112 if ($arg_pos > 1) {
6113 $arg_pos--;
6114 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6115 }
Anas Nashif0f3d5472016-11-07 15:57:57 -05006116 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6117 if ($stat =~ /$test/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006118 my $val = $1;
6119 $val = $6 if ($skip_args ne "");
Anas Nashif0f3d5472016-11-07 15:57:57 -05006120 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6121 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006122 ERROR("NON_OCTAL_PERMISSIONS",
Anas Nashif0f3d5472016-11-07 15:57:57 -05006123 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6124 }
6125 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006126 ERROR("EXPORTED_WORLD_WRITABLE",
Anas Nashif0f3d5472016-11-07 15:57:57 -05006127 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006128 }
6129 }
6130 }
6131 }
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006132
Anas Nashif0f3d5472016-11-07 15:57:57 -05006133# check for uses of S_<PERMS> that could be octal for readability
6134 if ($line =~ /\b$mode_perms_string_search\b/) {
6135 my $val = "";
6136 my $oval = "";
6137 my $to = 0;
6138 my $curpos = 0;
6139 my $lastpos = 0;
6140 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6141 $curpos = pos($line);
6142 my $match = $2;
6143 my $omatch = $1;
6144 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6145 $lastpos = $curpos;
6146 $to |= $mode_permission_string_types{$match};
6147 $val .= '\s*\|\s*' if ($val ne "");
6148 $val .= $match;
6149 $oval .= $omatch;
6150 }
6151 $oval =~ s/^\s*\|\s*//;
6152 $oval =~ s/\s*\|\s*$//;
6153 my $octal = sprintf("%04o", $to);
6154 if (WARN("SYMBOLIC_PERMS",
6155 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6156 $fix) {
6157 $fixed[$fixlinenr] =~ s/$val/$octal/;
6158 }
6159 }
6160
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006161# validate content of MODULE_LICENSE against list from include/linux/module.h
6162 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6163 my $extracted_string = get_quoted_string($line, $rawline);
6164 my $valid_licenses = qr{
6165 GPL|
6166 GPL\ v2|
6167 GPL\ and\ additional\ rights|
6168 Dual\ BSD/GPL|
6169 Dual\ MIT/GPL|
6170 Dual\ MPL/GPL|
6171 Proprietary
6172 }x;
6173 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6174 WARN("MODULE_LICENSE",
6175 "unknown module license " . $extracted_string . "\n" . $herecurr);
6176 }
6177 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006178 }
6179
6180 # If we have no input at all, then there is nothing to report on
6181 # so just keep quiet.
6182 if ($#rawlines == -1) {
6183 exit(0);
6184 }
6185
6186 # In mailback mode only produce a report in the negative, for
6187 # things that appear to be patches.
6188 if ($mailback && ($clean == 1 || !$is_patch)) {
6189 exit(0);
6190 }
6191
6192 # This is not a patch, and we are are in 'no-patch' mode so
6193 # just keep quiet.
6194 if (!$chk_patch && !$is_patch) {
6195 exit(0);
6196 }
6197
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006198 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006199 ERROR("NOT_UNIFIED_DIFF",
6200 "Does not appear to be a unified-diff format patch\n");
6201 }
Anas Nashif0f3d5472016-11-07 15:57:57 -05006202 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006203 ERROR("MISSING_SIGN_OFF",
6204 "Missing Signed-off-by: line(s)\n");
6205 }
6206
6207 print report_dump();
6208 if ($summary && !($clean == 1 && $quiet == 1)) {
6209 print "$filename " if ($summary_file);
6210 print "total: $cnt_error errors, $cnt_warn warnings, " .
6211 (($check)? "$cnt_chk checks, " : "") .
6212 "$cnt_lines lines checked\n";
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006213 }
6214
6215 if ($quiet == 0) {
Anas Nashif0f3d5472016-11-07 15:57:57 -05006216 # If there were any defects found and not already fixing them
6217 if (!$clean and !$fix) {
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006218 print << "EOM"
6219
Anas Nashif0f3d5472016-11-07 15:57:57 -05006220NOTE: For some of the reported defects, checkpatch may be able to
6221 mechanically convert to the typical style using --fix or --fix-inplace.
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006222EOM
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006223 }
6224 }
6225
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006226 if ($clean == 0 && $fix &&
6227 ("@rawlines" ne "@fixed" ||
6228 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6229 my $newfile = $filename;
6230 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6231 my $linecount = 0;
6232 my $f;
6233
6234 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6235
6236 open($f, '>', $newfile)
6237 or die "$P: Can't open $newfile for write\n";
6238 foreach my $fixed_line (@fixed) {
6239 $linecount++;
6240 if ($file) {
6241 if ($linecount > 3) {
6242 $fixed_line =~ s/^\+//;
6243 print $f $fixed_line . "\n";
6244 }
6245 } else {
6246 print $f $fixed_line . "\n";
6247 }
6248 }
6249 close($f);
6250
6251 if (!$quiet) {
6252 print << "EOM";
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006253
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006254Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6255
6256Do _NOT_ trust the results written to this file.
6257Do _NOT_ submit these changes without inspecting them for correctness.
6258
6259This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6260No warranties, expressed or implied...
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006261EOM
6262 }
6263 }
6264
Anas Nashifcbfb62c2015-10-14 06:08:54 -04006265 if ($quiet == 0) {
6266 print "\n";
6267 if ($clean == 1) {
6268 print "$vname has no obvious style problems and is ready for submission.\n";
6269 } else {
6270 print "$vname has style problems, please review.\n";
6271 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006272 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07006273 return $clean;
6274}