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