blob: 72d09b3de7b689cae3646ba9c0a4b225146558aa [file] [log] [blame]
Rob Herring7a5277a2017-01-04 10:45:20 -06001#!/usr/bin/env perl
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Rob Herring7a5277a2017-01-04 10:45:20 -06009use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070010use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080011use File::Basename;
12use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070013use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070014
David Keitel72bf3e82016-03-23 20:52:44 -070015use constant BEFORE_SHORTTEXT => 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -080016use constant IN_SHORTTEXT_BLANKLINE => 1;
17use constant IN_SHORTTEXT => 2;
18use constant AFTER_SHORTTEXT => 3;
19use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel72bf3e82016-03-23 20:52:44 -070020use constant SHORTTEXT_LIMIT => 75;
21
Andy Whitcroft0a920b52007-06-01 00:46:48 -070022my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080023my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070024
Joe Perches000d1cc12011-07-25 17:13:25 -070025my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070026
27use Getopt::Long qw(:config no_auto_abbrev);
28
29my $quiet = 0;
30my $tree = 1;
31my $chk_signoff = 1;
32my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080033my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070034my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080036my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070037my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070038my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070039my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070040my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070041my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070042my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080043my $summary = 1;
44my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080045my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070046my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070047my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070048my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080049my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070050my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080051my %debug;
Joe Perches34456862013-07-03 15:05:34 -070052my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070053my %use_type = ();
54my @use = ();
55my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070056my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070057my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070058my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080059my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070060my $ignore_perl_version = 0;
61my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070062my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070063my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070064my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070065my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070066my $conststructsfile = "$D/const_structs.checkpatch";
Joe Perches57230292015-06-25 15:03:03 -070067my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070068my $allow_c99_comments = 1;
Maya Erezec77bd62017-02-26 09:14:43 +020069my $qca_sign_off = 0;
70my $codeaurora_sign_off = 0;
Hannes Eder77f5b102009-09-21 17:04:37 -070071
72sub help {
73 my ($exitcode) = @_;
74
75 print << "EOM";
76Usage: $P [OPTION]... [FILE]...
77Version: $V
78
79Options:
80 -q, --quiet quiet
81 --no-tree run without a kernel tree
82 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080083 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070084 --patch treat FILE as patchfile (default)
85 --emacs emacs compile window format
86 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070087 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070088 -g, --git treat FILE as a single commit or git revision range
89 single git commit with:
90 <rev>
91 <rev>^
92 <rev>~n
93 multiple git commits with:
94 <rev1>..<rev2>
95 <rev1>...<rev2>
96 <rev>-<count>
97 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070098 -f, --file treat FILE as regular source file
99 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -0700100 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700101 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700102 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700103 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800104 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700105 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700106 --root=PATH PATH to the kernel tree root
107 --no-summary suppress the per-file summary
108 --mailback only produce a report in case of warnings/errors
109 --summary-file include the filename in summary
110 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
111 'values', 'possible', 'type', and 'attr' (default
112 is all off)
113 --test-only=WORD report only warnings/errors containing WORD
114 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700115 --fix EXPERIMENTAL - may create horrible results
116 If correctable single-line errors exist, create
117 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
118 with potential errors corrected to the preferred
119 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800120 --fix-inplace EXPERIMENTAL - may create horrible results
121 Is the same as --fix, but overwrites the input
122 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700123 --ignore-perl-version override checking of perl version. expect
124 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700125 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700126 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700127 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700128 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700129 -h, --help, --version display this help and exit
130
131When FILE is - read standard input.
132EOM
133
134 exit($exitcode);
135}
136
Joe Perches3beb42e2016-05-20 17:04:14 -0700137sub uniq {
138 my %seen;
139 return grep { !$seen{$_}++ } @_;
140}
141
142sub list_types {
143 my ($exitcode) = @_;
144
145 my $count = 0;
146
147 local $/ = undef;
148
149 open(my $script, '<', abs_path($P)) or
150 die "$P: Can't read '$P' $!\n";
151
152 my $text = <$script>;
153 close($script);
154
155 my @types = ();
156 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
157 push (@types, $_);
158 }
159 @types = sort(uniq(@types));
160 print("#\tMessage type\n\n");
161 foreach my $type (@types) {
162 print(++$count . "\t" . $type . "\n");
163 }
164
165 exit($exitcode);
166}
167
Joe Perches000d1cc12011-07-25 17:13:25 -0700168my $conf = which_conf($configuration_file);
169if (-f $conf) {
170 my @conf_args;
171 open(my $conffile, '<', "$conf")
172 or warn "$P: Can't find a readable $configuration_file file $!\n";
173
174 while (<$conffile>) {
175 my $line = $_;
176
177 $line =~ s/\s*\n?$//g;
178 $line =~ s/^\s*//g;
179 $line =~ s/\s+/ /g;
180
181 next if ($line =~ m/^\s*#/);
182 next if ($line =~ m/^\s*$/);
183
184 my @words = split(" ", $line);
185 foreach my $word (@words) {
186 last if ($word =~ m/^#/);
187 push (@conf_args, $word);
188 }
189 }
190 close($conffile);
191 unshift(@ARGV, @conf_args) if @conf_args;
192}
193
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700194GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700196 'tree!' => \$tree,
197 'signoff!' => \$chk_signoff,
198 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800199 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700200 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800201 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700202 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700203 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700204 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'subjective!' => \$check,
206 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700207 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700208 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700209 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700210 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800211 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700212 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700213 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800214 'summary!' => \$summary,
215 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800216 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700217 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800218 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700219 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800220 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700221 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700222 'codespell!' => \$codespell,
223 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700224 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700225 'h|help' => \$help,
226 'version' => \$help
227) or help(1);
228
229help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700230
Joe Perches3beb42e2016-05-20 17:04:14 -0700231list_types(0) if ($list_types);
232
Joe Perches9624b8d2014-01-23 15:54:44 -0800233$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700234$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800235
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700236my $exit = 0;
237
Dave Hansend62a2012013-09-11 14:23:56 -0700238if ($^V && $^V lt $minimum_perl_version) {
239 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
240 if (!$ignore_perl_version) {
241 exit(1);
242 }
243}
244
Allen Hubbe45107ff2016-08-02 14:04:47 -0700245#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700246if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700247 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700248}
249
Joe Perches91bfe482013-09-11 14:23:59 -0700250sub hash_save_array_words {
251 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700252
Joe Perches91bfe482013-09-11 14:23:59 -0700253 my @array = split(/,/, join(',', @$arrayRef));
254 foreach my $word (@array) {
255 $word =~ s/\s*\n?$//g;
256 $word =~ s/^\s*//g;
257 $word =~ s/\s+/ /g;
258 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700259
Joe Perches91bfe482013-09-11 14:23:59 -0700260 next if ($word =~ m/^\s*#/);
261 next if ($word =~ m/^\s*$/);
262
263 $hashRef->{$word}++;
264 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700265}
266
Joe Perches91bfe482013-09-11 14:23:59 -0700267sub hash_show_words {
268 my ($hashRef, $prefix) = @_;
269
Joe Perches3c816e42015-06-25 15:03:29 -0700270 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700271 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700272 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700273 print " $word";
274 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700275 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700276 }
277}
278
279hash_save_array_words(\%ignore_type, \@ignore);
280hash_save_array_words(\%use_type, \@use);
281
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800282my $dbg_values = 0;
283my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700284my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700285my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800286for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800287 ## no critic
288 eval "\${dbg_$key} = '$debug{$key}';";
289 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800290}
291
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700292my $rpt_cleaners = 0;
293
Andy Whitcroft8905a672007-11-28 16:21:06 -0800294if ($terse) {
295 $emacs = 1;
296 $quiet++;
297}
298
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700299if ($tree) {
300 if (defined $root) {
301 if (!top_of_kernel_tree($root)) {
302 die "$P: $root: --root does not point at a valid tree\n";
303 }
304 } else {
305 if (top_of_kernel_tree('.')) {
306 $root = '.';
307 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
308 top_of_kernel_tree($1)) {
309 $root = $1;
310 }
311 }
312
313 if (!defined $root) {
314 print "Must be run from the top-level dir. of a kernel tree\n";
315 exit(2);
316 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700317}
318
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319my $emitted_corrupt = 0;
320
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700321our $Ident = qr{
322 [A-Za-z_][A-Za-z\d_]*
323 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
324 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700325our $Storage = qr{extern|static|asmlinkage};
326our $Sparse = qr{
327 __user|
328 __kernel|
329 __force|
330 __iomem|
331 __must_check|
332 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800333 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700334 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800335 __rcu|
336 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800338our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
339our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
340our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
341our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
342our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700343
Wolfram Sang52131292010-03-05 13:43:51 -0800344# Notes to $Attribute:
345# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700346our $Attribute = qr{
347 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700348 __percpu|
349 __nocast|
350 __safe|
351 __bitwise__|
352 __packed__|
353 __packed2__|
354 __naked|
355 __maybe_unused|
356 __always_unused|
357 __noreturn|
358 __used|
359 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800360 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700361 __noclone|
362 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700363 __read_mostly|
364 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700365 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700366 ____cacheline_aligned|
367 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800368 ____cacheline_internodealigned_in_smp|
369 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700371our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700372our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700373our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
374our $Lval = qr{$Ident(?:$Member)*};
375
Joe Perches95e2c602013-07-03 15:05:20 -0700376our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
377our $Binary = qr{(?i)0b[01]+$Int_type?};
378our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
379our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700380our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800381our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800382our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
383our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
384our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800385our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700386our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800387our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700388our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700389our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700390our $Operators = qr{
391 <=|>=|==|!=|
392 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700393 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700394 }x;
395
Joe Perches91cb5192014-04-03 14:49:32 -0700396our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
397
Joe Perchesab7e23f2015-04-16 12:44:22 -0700398our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700400our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700401our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800402our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700403our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800404our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700405our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800406
Joe Perches15662b32011-10-31 17:13:12 -0700407our $NON_ASCII_UTF8 = qr{
408 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700409 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
410 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
411 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
412 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
413 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
414 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
415}x;
416
Joe Perches15662b32011-10-31 17:13:12 -0700417our $UTF8 = qr{
418 [\x09\x0A\x0D\x20-\x7E] # ASCII
419 | $NON_ASCII_UTF8
420}x;
421
Joe Perchese6176fa2015-06-25 15:02:49 -0700422our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800423our $typeOtherOSTypedefs = qr{(?x:
424 u_(?:char|short|int|long) | # bsd
425 u(?:nchar|short|int|long) # sysv
426)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700427our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700428 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700429 atomic_t
430)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700431our $typeTypedefs = qr{(?x:
432 $typeC99Typedefs\b|
433 $typeOtherOSTypedefs\b|
434 $typeKernelTypedefs\b
435)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700436
Joe Perches6d32f7a2015-11-06 16:31:37 -0800437our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
438
Joe Perches691e6692010-03-05 13:43:51 -0800439our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700440 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700441 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700442 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700443 panic|
Joe Perches06668722013-11-12 15:10:07 -0800444 MODULE_[A-Z_]+|
445 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800446)};
447
Joe Perches20112472011-07-25 17:13:23 -0700448our $signature_tags = qr{(?xi:
449 Signed-off-by:|
450 Acked-by:|
451 Tested-by:|
452 Reviewed-by:|
453 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700454 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700455 To:|
456 Cc:
457)};
458
Joe Perches18130872014-08-06 16:11:22 -0700459our @typeListMisordered = (
460 qr{char\s+(?:un)?signed},
461 qr{int\s+(?:(?:un)?signed\s+)?short\s},
462 qr{int\s+short(?:\s+(?:un)?signed)},
463 qr{short\s+int(?:\s+(?:un)?signed)},
464 qr{(?:un)?signed\s+int\s+short},
465 qr{short\s+(?:un)?signed},
466 qr{long\s+int\s+(?:un)?signed},
467 qr{int\s+long\s+(?:un)?signed},
468 qr{long\s+(?:un)?signed\s+int},
469 qr{int\s+(?:un)?signed\s+long},
470 qr{int\s+(?:un)?signed},
471 qr{int\s+long\s+long\s+(?:un)?signed},
472 qr{long\s+long\s+int\s+(?:un)?signed},
473 qr{long\s+long\s+(?:un)?signed\s+int},
474 qr{long\s+long\s+(?:un)?signed},
475 qr{long\s+(?:un)?signed},
476);
477
Andy Whitcroft8905a672007-11-28 16:21:06 -0800478our @typeList = (
479 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700480 qr{(?:(?:un)?signed\s+)?char},
481 qr{(?:(?:un)?signed\s+)?short\s+int},
482 qr{(?:(?:un)?signed\s+)?short},
483 qr{(?:(?:un)?signed\s+)?int},
484 qr{(?:(?:un)?signed\s+)?long\s+int},
485 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
486 qr{(?:(?:un)?signed\s+)?long\s+long},
487 qr{(?:(?:un)?signed\s+)?long},
488 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489 qr{float},
490 qr{double},
491 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800492 qr{struct\s+$Ident},
493 qr{union\s+$Ident},
494 qr{enum\s+$Ident},
495 qr{${Ident}_t},
496 qr{${Ident}_handler},
497 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700498 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800499);
Joe Perches938224b2016-01-20 14:59:15 -0800500
501our $C90_int_types = qr{(?x:
502 long\s+long\s+int\s+(?:un)?signed|
503 long\s+long\s+(?:un)?signed\s+int|
504 long\s+long\s+(?:un)?signed|
505 (?:(?:un)?signed\s+)?long\s+long\s+int|
506 (?:(?:un)?signed\s+)?long\s+long|
507 int\s+long\s+long\s+(?:un)?signed|
508 int\s+(?:(?:un)?signed\s+)?long\s+long|
509
510 long\s+int\s+(?:un)?signed|
511 long\s+(?:un)?signed\s+int|
512 long\s+(?:un)?signed|
513 (?:(?:un)?signed\s+)?long\s+int|
514 (?:(?:un)?signed\s+)?long|
515 int\s+long\s+(?:un)?signed|
516 int\s+(?:(?:un)?signed\s+)?long|
517
518 int\s+(?:un)?signed|
519 (?:(?:un)?signed\s+)?int
520)};
521
Alex Dowad485ff232015-06-25 15:02:52 -0700522our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700523our @typeListWithAttr = (
524 @typeList,
525 qr{struct\s+$InitAttribute\s+$Ident},
526 qr{union\s+$InitAttribute\s+$Ident},
527);
528
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700529our @modifierList = (
530 qr{fastcall},
531);
Alex Dowad485ff232015-06-25 15:02:52 -0700532our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533
Joe Perches24358802014-04-03 14:49:13 -0700534our @mode_permission_funcs = (
535 ["module_param", 3],
536 ["module_param_(?:array|named|string)", 4],
537 ["module_param_array_named", 5],
538 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
539 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700540 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
541 ["IIO_DEV_ATTR_[A-Z_]+", 1],
542 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
543 ["SENSOR_TEMPLATE(?:_2|)", 3],
544 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700545);
546
Joe Perches515a2352014-04-03 14:49:24 -0700547#Create a search pattern for all these functions to speed up a loop below
548our $mode_perms_search = "";
549foreach my $entry (@mode_permission_funcs) {
550 $mode_perms_search .= '|' if ($mode_perms_search ne "");
551 $mode_perms_search .= $entry->[0];
552}
553
Joe Perchesb392c642015-04-16 12:44:16 -0700554our $mode_perms_world_writable = qr{
555 S_IWUGO |
556 S_IWOTH |
557 S_IRWXUGO |
558 S_IALLUGO |
559 0[0-7][0-7][2367]
560}x;
561
Joe Perchesf90774e2016-10-11 13:51:47 -0700562our %mode_permission_string_types = (
563 "S_IRWXU" => 0700,
564 "S_IRUSR" => 0400,
565 "S_IWUSR" => 0200,
566 "S_IXUSR" => 0100,
567 "S_IRWXG" => 0070,
568 "S_IRGRP" => 0040,
569 "S_IWGRP" => 0020,
570 "S_IXGRP" => 0010,
571 "S_IRWXO" => 0007,
572 "S_IROTH" => 0004,
573 "S_IWOTH" => 0002,
574 "S_IXOTH" => 0001,
575 "S_IRWXUGO" => 0777,
576 "S_IRUGO" => 0444,
577 "S_IWUGO" => 0222,
578 "S_IXUGO" => 0111,
579);
580
581#Create a search pattern for all these strings to speed up a loop below
582our $mode_perms_string_search = "";
583foreach my $entry (keys %mode_permission_string_types) {
584 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
585 $mode_perms_string_search .= $entry;
586}
587
Wolfram Sang7840a942010-08-09 17:20:57 -0700588our $allowed_asm_includes = qr{(?x:
589 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700590 memory|
591 time|
592 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700593)};
594# memory.h: ARM has a custom one
595
Kees Cook66b47b42014-10-13 15:51:57 -0700596# Load common spelling mistakes and build regular expression list.
597my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700598my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700599
Joe Perches36061e32014-12-10 15:51:43 -0800600if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800601 while (<$spelling>) {
602 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700603
Joe Perches36061e32014-12-10 15:51:43 -0800604 $line =~ s/\s*\n?$//g;
605 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700606
Joe Perches36061e32014-12-10 15:51:43 -0800607 next if ($line =~ m/^\s*#/);
608 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700609
Joe Perches36061e32014-12-10 15:51:43 -0800610 my ($suspect, $fix) = split(/\|\|/, $line);
611
Joe Perches36061e32014-12-10 15:51:43 -0800612 $spelling_fix{$suspect} = $fix;
613 }
614 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800615} else {
616 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700617}
Kees Cook66b47b42014-10-13 15:51:57 -0700618
Joe Perchesebfd7d62015-04-16 12:44:14 -0700619if ($codespell) {
620 if (open(my $spelling, '<', $codespellfile)) {
621 while (<$spelling>) {
622 my $line = $_;
623
624 $line =~ s/\s*\n?$//g;
625 $line =~ s/^\s*//g;
626
627 next if ($line =~ m/^\s*#/);
628 next if ($line =~ m/^\s*$/);
629 next if ($line =~ m/, disabled/i);
630
631 $line =~ s/,.*$//;
632
633 my ($suspect, $fix) = split(/->/, $line);
634
635 $spelling_fix{$suspect} = $fix;
636 }
637 close($spelling);
638 } else {
639 warn "No codespell typos will be found - file '$codespellfile': $!\n";
640 }
641}
642
643$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
644
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700645my $const_structs = "";
646if (open(my $conststructs, '<', $conststructsfile)) {
647 while (<$conststructs>) {
648 my $line = $_;
649
650 $line =~ s/\s*\n?$//g;
651 $line =~ s/^\s*//g;
652
653 next if ($line =~ m/^\s*#/);
654 next if ($line =~ m/^\s*$/);
655 if ($line =~ /\s/) {
656 print("$conststructsfile: '$line' invalid - ignored\n");
657 next;
658 }
659
660 $const_structs .= '|' if ($const_structs ne "");
661 $const_structs .= $line;
662 }
663 close($conststructsfile);
664} else {
665 warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
666}
667
Andy Whitcroft8905a672007-11-28 16:21:06 -0800668sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700669 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
670 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700671 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700672 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700673 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700674 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700675 (?:$typeTypedefs\b)|
676 (?:${all}\b)
677 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800678 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700679 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800680 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800681 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700682 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700683 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800684 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700685 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800686 }x;
Joe Perches18130872014-08-06 16:11:22 -0700687 $NonptrTypeMisordered = qr{
688 (?:$Modifier\s+|const\s+)*
689 (?:
690 (?:${Misordered}\b)
691 )
692 (?:\s+$Modifier|\s+const)*
693 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700694 $NonptrTypeWithAttr = qr{
695 (?:$Modifier\s+|const\s+)*
696 (?:
697 (?:typeof|__typeof__)\s*\([^\)]*\)|
698 (?:$typeTypedefs\b)|
699 (?:${allWithAttr}\b)
700 )
701 (?:\s+$Modifier|\s+const)*
702 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800703 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700704 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700705 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700706 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800707 }x;
Joe Perches18130872014-08-06 16:11:22 -0700708 $TypeMisordered = qr{
709 $NonptrTypeMisordered
710 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
711 (?:\s+$Inline|\s+$Modifier)*
712 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700713 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700714 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800715}
716build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700717
Joe Perches7d2367a2011-07-25 17:13:22 -0700718our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700719
720# Using $balanced_parens, $LvalOrFunc, or $FuncArg
721# requires at least perl version v5.10.0
722# Any use must be runtime checked with $^V
723
724our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700725our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800726our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700727
Joe Perchesf8422302014-08-06 16:11:31 -0700728our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700729 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700730 (?:$Storage\s+)?LIST_HEAD\s*\(|
731 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
732)};
733
Joe Perches7d2367a2011-07-25 17:13:22 -0700734sub deparenthesize {
735 my ($string) = @_;
736 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700737
738 while ($string =~ /^\s*\(.*\)\s*$/) {
739 $string =~ s@^\s*\(\s*@@;
740 $string =~ s@\s*\)\s*$@@;
741 }
742
Joe Perches7d2367a2011-07-25 17:13:22 -0700743 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700744
Joe Perches7d2367a2011-07-25 17:13:22 -0700745 return $string;
746}
747
Joe Perches34456862013-07-03 15:05:34 -0700748sub seed_camelcase_file {
749 my ($file) = @_;
750
751 return if (!(-f $file));
752
753 local $/;
754
755 open(my $include_file, '<', "$file")
756 or warn "$P: Can't read '$file' $!\n";
757 my $text = <$include_file>;
758 close($include_file);
759
760 my @lines = split('\n', $text);
761
762 foreach my $line (@lines) {
763 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
764 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
765 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800766 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
767 $camelcase{$1} = 1;
768 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700769 $camelcase{$1} = 1;
770 }
771 }
772}
773
Joe Perches85b0ee12016-10-11 13:51:44 -0700774sub is_maintained_obsolete {
775 my ($filename) = @_;
776
777 return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
778
Joe Perches0616efa2016-10-11 13:52:02 -0700779 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700780
781 return $status =~ /obsolete/i;
782}
783
Joe Perches34456862013-07-03 15:05:34 -0700784my $camelcase_seeded = 0;
785sub seed_camelcase_includes {
786 return if ($camelcase_seeded);
787
788 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700789 my $camelcase_cache = "";
790 my @include_files = ();
791
792 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700793
Richard Genoud3645e322014-02-10 14:25:32 -0800794 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700795 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
796 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700797 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700798 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700799 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700800 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700801 @include_files = split('\n', $files);
802 foreach my $file (@include_files) {
803 my $date = POSIX::strftime("%Y%m%d%H%M",
804 localtime((stat $file)[9]));
805 $last_mod_date = $date if ($last_mod_date < $date);
806 }
807 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700808 }
Joe Perchesc707a812013-07-08 16:00:43 -0700809
810 if ($camelcase_cache ne "" && -f $camelcase_cache) {
811 open(my $camelcase_file, '<', "$camelcase_cache")
812 or warn "$P: Can't read '$camelcase_cache' $!\n";
813 while (<$camelcase_file>) {
814 chomp;
815 $camelcase{$_} = 1;
816 }
817 close($camelcase_file);
818
819 return;
820 }
821
Richard Genoud3645e322014-02-10 14:25:32 -0800822 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700823 $files = `git ls-files "include/*.h"`;
824 @include_files = split('\n', $files);
825 }
826
Joe Perches34456862013-07-03 15:05:34 -0700827 foreach my $file (@include_files) {
828 seed_camelcase_file($file);
829 }
Joe Perches351b2a12013-07-03 15:05:36 -0700830
Joe Perchesc707a812013-07-08 16:00:43 -0700831 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700832 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700833 open(my $camelcase_file, '>', "$camelcase_cache")
834 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700835 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
836 print $camelcase_file ("$_\n");
837 }
838 close($camelcase_file);
839 }
Joe Perches34456862013-07-03 15:05:34 -0700840}
841
Joe Perchesd311cd42014-08-06 16:10:57 -0700842sub git_commit_info {
843 my ($commit, $id, $desc) = @_;
844
845 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
846
847 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
848 $output =~ s/^\s*//gm;
849 my @lines = split("\n", $output);
850
Joe Perches0d7835f2015-02-13 14:38:35 -0800851 return ($id, $desc) if ($#lines < 0);
852
Joe Perchesd311cd42014-08-06 16:10:57 -0700853 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
854# Maybe one day convert this block of bash into something that returns
855# all matching commit ids, but it's very slow...
856#
857# echo "checking commits $1..."
858# git rev-list --remotes | grep -i "^$1" |
859# while read line ; do
860# git log --format='%H %s' -1 $line |
861# echo "commit $(cut -c 1-12,41-)"
862# done
863 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
864 } else {
865 $id = substr($lines[0], 0, 12);
866 $desc = substr($lines[0], 41);
867 }
868
869 return ($id, $desc);
870}
871
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700872$chk_signoff = 0 if ($file);
873
Andy Whitcroft00df3442007-06-08 13:47:06 -0700874my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800875my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700876my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700877my @fixed_inserted = ();
878my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700879my $fixlinenr = -1;
880
Du, Changbin4a593c32016-05-20 17:04:16 -0700881# If input is git commits, extract all commits from the commit expressions.
882# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
883die "$P: No git repository found\n" if ($git && !-e ".git");
884
885if ($git) {
886 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700887 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700888 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700889 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
890 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700891 } elsif ($commit_expr =~ m/\.\./) {
892 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700893 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700894 $git_range = "-1 $commit_expr";
895 }
896 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
897 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700898 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
899 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700900 my $sha1 = $1;
901 my $subject = $2;
902 unshift(@commits, $sha1);
903 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700904 }
905 }
906 die "$P: no git commits after extraction!\n" if (@commits == 0);
907 @ARGV = @commits;
908}
909
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800910my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700911for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800912 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700913 if ($git) {
914 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
915 die "$P: $filename: git format-patch failed - $!\n";
916 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800917 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700918 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800919 } elsif ($filename eq '-') {
920 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700921 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800922 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700923 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700924 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800925 if ($filename eq '-') {
926 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700927 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700928 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800929 } else {
930 $vname = $filename;
931 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800932 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700933 chomp;
934 push(@rawlines, $_);
935 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800936 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700937
938 if ($#ARGV > 0 && $quiet == 0) {
939 print '-' x length($vname) . "\n";
940 print "$vname\n";
941 print '-' x length($vname) . "\n";
942 }
943
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800944 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700945 $exit = 1;
946 }
947 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800948 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700949 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700950 @fixed_inserted = ();
951 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700952 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700953 @modifierListFile = ();
954 @typeListFile = ();
955 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700956}
957
Joe Perchesd8469f12015-06-25 15:03:00 -0700958if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700959 hash_show_words(\%use_type, "Used");
960 hash_show_words(\%ignore_type, "Ignored");
961
Joe Perchesd8469f12015-06-25 15:03:00 -0700962 if ($^V lt 5.10.0) {
963 print << "EOM"
964
965NOTE: perl $^V is not modern enough to detect all possible issues.
966 An upgrade to at least perl v5.10.0 is suggested.
967EOM
968 }
969 if ($exit) {
970 print << "EOM"
971
972NOTE: If any of the errors are false positives, please report
973 them to the maintainer, see CHECKPATCH in MAINTAINERS.
974EOM
975 }
976}
977
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700978exit($exit);
979
980sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700981 my ($root) = @_;
982
983 my @tree_check = (
984 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
985 "README", "Documentation", "arch", "include", "drivers",
986 "fs", "init", "ipc", "kernel", "lib", "scripts",
987 );
988
989 foreach my $check (@tree_check) {
990 if (! -e $root . '/' . $check) {
991 return 0;
992 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700993 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700994 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700995}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700996
Joe Perches20112472011-07-25 17:13:23 -0700997sub parse_email {
998 my ($formatted_email) = @_;
999
1000 my $name = "";
1001 my $address = "";
1002 my $comment = "";
1003
1004 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1005 $name = $1;
1006 $address = $2;
1007 $comment = $3 if defined $3;
1008 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1009 $address = $1;
1010 $comment = $2 if defined $2;
1011 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1012 $address = $1;
1013 $comment = $2 if defined $2;
1014 $formatted_email =~ s/$address.*$//;
1015 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001016 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001017 $name =~ s/^\"|\"$//g;
1018 # If there's a name left after stripping spaces and
1019 # leading quotes, and the address doesn't have both
1020 # leading and trailing angle brackets, the address
1021 # is invalid. ie:
1022 # "joe smith joe@smith.com" bad
1023 # "joe smith <joe@smith.com" bad
1024 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1025 $name = "";
1026 $address = "";
1027 $comment = "";
1028 }
1029 }
1030
Joe Perches3705ce52013-07-03 15:05:31 -07001031 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001032 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001033 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001034 $address =~ s/^\<|\>$//g;
1035
1036 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1037 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1038 $name = "\"$name\"";
1039 }
1040
1041 return ($name, $address, $comment);
1042}
1043
1044sub format_email {
1045 my ($name, $address) = @_;
1046
1047 my $formatted_email;
1048
Joe Perches3705ce52013-07-03 15:05:31 -07001049 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001050 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001051 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001052
1053 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1054 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1055 $name = "\"$name\"";
1056 }
1057
1058 if ("$name" eq "") {
1059 $formatted_email = "$address";
1060 } else {
1061 $formatted_email = "$name <$address>";
1062 }
1063
1064 return $formatted_email;
1065}
1066
Joe Perchesd311cd42014-08-06 16:10:57 -07001067sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001068 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001069
Joe Perchesbd474ca2014-08-06 16:11:10 -07001070 foreach my $path (split(/:/, $ENV{PATH})) {
1071 if (-e "$path/$bin") {
1072 return "$path/$bin";
1073 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001074 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001075
Joe Perchesbd474ca2014-08-06 16:11:10 -07001076 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001077}
1078
Joe Perches000d1cc12011-07-25 17:13:25 -07001079sub which_conf {
1080 my ($conf) = @_;
1081
1082 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1083 if (-e "$path/$conf") {
1084 return "$path/$conf";
1085 }
1086 }
1087
1088 return "";
1089}
1090
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001091sub expand_tabs {
1092 my ($str) = @_;
1093
1094 my $res = '';
1095 my $n = 0;
1096 for my $c (split(//, $str)) {
1097 if ($c eq "\t") {
1098 $res .= ' ';
1099 $n++;
1100 for (; ($n % 8) != 0; $n++) {
1101 $res .= ' ';
1102 }
1103 next;
1104 }
1105 $res .= $c;
1106 $n++;
1107 }
1108
1109 return $res;
1110}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001111sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001112 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001113 return $res;
1114}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001115
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001116sub line_stats {
1117 my ($line) = @_;
1118
1119 # Drop the diff line leader and expand tabs
1120 $line =~ s/^.//;
1121 $line = expand_tabs($line);
1122
1123 # Pick the indent from the front of the line.
1124 my ($white) = ($line =~ /^(\s*)/);
1125
1126 return (length($line), length($white));
1127}
1128
Andy Whitcroft773647a2008-03-28 14:15:58 -07001129my $sanitise_quote = '';
1130
1131sub sanitise_line_reset {
1132 my ($in_comment) = @_;
1133
1134 if ($in_comment) {
1135 $sanitise_quote = '*/';
1136 } else {
1137 $sanitise_quote = '';
1138 }
1139}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001140sub sanitise_line {
1141 my ($line) = @_;
1142
1143 my $res = '';
1144 my $l = '';
1145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001147 my $off = 0;
1148 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001149
Andy Whitcroft773647a2008-03-28 14:15:58 -07001150 # Always copy over the diff marker.
1151 $res = substr($line, 0, 1);
1152
1153 for ($off = 1; $off < length($line); $off++) {
1154 $c = substr($line, $off, 1);
1155
1156 # Comments we are wacking completly including the begin
1157 # and end, all to $;.
1158 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1159 $sanitise_quote = '*/';
1160
1161 substr($res, $off, 2, "$;$;");
1162 $off++;
1163 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001164 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001165 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001166 $sanitise_quote = '';
1167 substr($res, $off, 2, "$;$;");
1168 $off++;
1169 next;
1170 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001171 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1172 $sanitise_quote = '//';
1173
1174 substr($res, $off, 2, $sanitise_quote);
1175 $off++;
1176 next;
1177 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001178
1179 # A \ in a string means ignore the next character.
1180 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1181 $c eq "\\") {
1182 substr($res, $off, 2, 'XX');
1183 $off++;
1184 next;
1185 }
1186 # Regular quotes.
1187 if ($c eq "'" || $c eq '"') {
1188 if ($sanitise_quote eq '') {
1189 $sanitise_quote = $c;
1190
1191 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001192 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001193 } elsif ($sanitise_quote eq $c) {
1194 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001195 }
1196 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001197
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001198 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001199 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1200 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001201 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1202 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001203 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1204 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001205 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001206 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001207 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001208 }
1209
Daniel Walker113f04a2009-09-21 17:04:35 -07001210 if ($sanitise_quote eq '//') {
1211 $sanitise_quote = '';
1212 }
1213
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001214 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001215 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001216 my $clean = 'X' x length($1);
1217 $res =~ s@\<.*\>@<$clean>@;
1218
1219 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001220 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001221 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001222 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001223 }
1224
Joe Perchesdadf6802016-08-02 14:04:33 -07001225 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1226 my $match = $1;
1227 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1228 }
1229
Andy Whitcroft00df3442007-06-08 13:47:06 -07001230 return $res;
1231}
1232
Joe Perchesa6962d72013-04-29 16:18:13 -07001233sub get_quoted_string {
1234 my ($line, $rawline) = @_;
1235
Joe Perches33acb542015-06-25 15:02:54 -07001236 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001237 return substr($rawline, $-[0], $+[0] - $-[0]);
1238}
1239
Andy Whitcroft8905a672007-11-28 16:21:06 -08001240sub ctx_statement_block {
1241 my ($linenr, $remain, $off) = @_;
1242 my $line = $linenr - 1;
1243 my $blk = '';
1244 my $soff = $off;
1245 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001246 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001247
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001248 my $loff = 0;
1249
Andy Whitcroft8905a672007-11-28 16:21:06 -08001250 my $type = '';
1251 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001252 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001253 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001254 my $c;
1255 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001256
1257 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001258 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001259 @stack = (['', 0]) if ($#stack == -1);
1260
Andy Whitcroft773647a2008-03-28 14:15:58 -07001261 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001262 # If we are about to drop off the end, pull in more
1263 # context.
1264 if ($off >= $len) {
1265 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001266 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001267 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001269 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001270 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001271 $len = length($blk);
1272 $line++;
1273 last;
1274 }
1275 # Bail if there is no further context.
1276 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001277 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001278 last;
1279 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001280 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1281 $level++;
1282 $type = '#';
1283 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001284 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001285 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001286 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001287 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001288
Andy Whitcroft773647a2008-03-28 14:15:58 -07001289 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001290
1291 # Handle nested #if/#else.
1292 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1293 push(@stack, [ $type, $level ]);
1294 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1295 ($type, $level) = @{$stack[$#stack - 1]};
1296 } elsif ($remainder =~ /^#\s*endif\b/) {
1297 ($type, $level) = @{pop(@stack)};
1298 }
1299
Andy Whitcroft8905a672007-11-28 16:21:06 -08001300 # Statement ends at the ';' or a close '}' at the
1301 # outermost level.
1302 if ($level == 0 && $c eq ';') {
1303 last;
1304 }
1305
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001306 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001307 if ($level == 0 && $coff_set == 0 &&
1308 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1309 $remainder =~ /^(else)(?:\s|{)/ &&
1310 $remainder !~ /^else\s+if\b/) {
1311 $coff = $off + length($1) - 1;
1312 $coff_set = 1;
1313 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1314 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001315 }
1316
Andy Whitcroft8905a672007-11-28 16:21:06 -08001317 if (($type eq '' || $type eq '(') && $c eq '(') {
1318 $level++;
1319 $type = '(';
1320 }
1321 if ($type eq '(' && $c eq ')') {
1322 $level--;
1323 $type = ($level != 0)? '(' : '';
1324
1325 if ($level == 0 && $coff < $soff) {
1326 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001327 $coff_set = 1;
1328 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001329 }
1330 }
1331 if (($type eq '' || $type eq '{') && $c eq '{') {
1332 $level++;
1333 $type = '{';
1334 }
1335 if ($type eq '{' && $c eq '}') {
1336 $level--;
1337 $type = ($level != 0)? '{' : '';
1338
1339 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001340 if (substr($blk, $off + 1, 1) eq ';') {
1341 $off++;
1342 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001343 last;
1344 }
1345 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001346 # Preprocessor commands end at the newline unless escaped.
1347 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1348 $level--;
1349 $type = '';
1350 $off++;
1351 last;
1352 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001353 $off++;
1354 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001355 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001356 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001357 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001358 $line++;
1359 $remain--;
1360 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001361
1362 my $statement = substr($blk, $soff, $off - $soff + 1);
1363 my $condition = substr($blk, $soff, $coff - $soff + 1);
1364
1365 #warn "STATEMENT<$statement>\n";
1366 #warn "CONDITION<$condition>\n";
1367
Andy Whitcroft773647a2008-03-28 14:15:58 -07001368 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001369
1370 return ($statement, $condition,
1371 $line, $remain + 1, $off - $loff + 1, $level);
1372}
1373
Andy Whitcroftcf655042008-03-04 14:28:20 -08001374sub statement_lines {
1375 my ($stmt) = @_;
1376
1377 # Strip the diff line prefixes and rip blank lines at start and end.
1378 $stmt =~ s/(^|\n)./$1/g;
1379 $stmt =~ s/^\s*//;
1380 $stmt =~ s/\s*$//;
1381
1382 my @stmt_lines = ($stmt =~ /\n/g);
1383
1384 return $#stmt_lines + 2;
1385}
1386
1387sub statement_rawlines {
1388 my ($stmt) = @_;
1389
1390 my @stmt_lines = ($stmt =~ /\n/g);
1391
1392 return $#stmt_lines + 2;
1393}
1394
1395sub statement_block_size {
1396 my ($stmt) = @_;
1397
1398 $stmt =~ s/(^|\n)./$1/g;
1399 $stmt =~ s/^\s*{//;
1400 $stmt =~ s/}\s*$//;
1401 $stmt =~ s/^\s*//;
1402 $stmt =~ s/\s*$//;
1403
1404 my @stmt_lines = ($stmt =~ /\n/g);
1405 my @stmt_statements = ($stmt =~ /;/g);
1406
1407 my $stmt_lines = $#stmt_lines + 2;
1408 my $stmt_statements = $#stmt_statements + 1;
1409
1410 if ($stmt_lines > $stmt_statements) {
1411 return $stmt_lines;
1412 } else {
1413 return $stmt_statements;
1414 }
1415}
1416
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001417sub ctx_statement_full {
1418 my ($linenr, $remain, $off) = @_;
1419 my ($statement, $condition, $level);
1420
1421 my (@chunks);
1422
Andy Whitcroftcf655042008-03-04 14:28:20 -08001423 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424 ($statement, $condition, $linenr, $remain, $off, $level) =
1425 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001426 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001427 push(@chunks, [ $condition, $statement ]);
1428 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1429 return ($level, $linenr, @chunks);
1430 }
1431
1432 # Pull in the following conditional/block pairs and see if they
1433 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001434 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001435 ($statement, $condition, $linenr, $remain, $off, $level) =
1436 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001437 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001438 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001439 #print "C: push\n";
1440 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441 }
1442
1443 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001444}
1445
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001446sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001447 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001448 my $line;
1449 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001450 my $blk = '';
1451 my @o;
1452 my @c;
1453 my @res = ();
1454
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001455 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001456 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001457 for ($line = $start; $remain > 0; $line++) {
1458 next if ($rawlines[$line] =~ /^-/);
1459 $remain--;
1460
1461 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001462
1463 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001464 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001465 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001466 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001467 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001468 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001469 $level = pop(@stack);
1470 }
1471
Andy Whitcroft01464f32010-10-26 14:23:19 -07001472 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001473 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1474 if ($off > 0) {
1475 $off--;
1476 next;
1477 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001478
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001479 if ($c eq $close && $level > 0) {
1480 $level--;
1481 last if ($level == 0);
1482 } elsif ($c eq $open) {
1483 $level++;
1484 }
1485 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001486
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001487 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001488 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001489 }
1490
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001491 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001492 }
1493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001494 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001495}
1496sub ctx_block_outer {
1497 my ($linenr, $remain) = @_;
1498
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001499 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1500 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001501}
1502sub ctx_block {
1503 my ($linenr, $remain) = @_;
1504
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001505 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1506 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001507}
1508sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001509 my ($linenr, $remain, $off) = @_;
1510
1511 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1512 return @r;
1513}
1514sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001515 my ($linenr, $remain) = @_;
1516
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001517 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001518}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001519sub ctx_statement_level {
1520 my ($linenr, $remain, $off) = @_;
1521
1522 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1523}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001524
1525sub ctx_locate_comment {
1526 my ($first_line, $end_line) = @_;
1527
1528 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001529 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001530 return $current_comment if (defined $current_comment);
1531
1532 # Look through the context and try and figure out if there is a
1533 # comment.
1534 my $in_comment = 0;
1535 $current_comment = '';
1536 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001537 my $line = $rawlines[$linenr - 1];
1538 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001539 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1540 $in_comment = 1;
1541 }
1542 if ($line =~ m@/\*@) {
1543 $in_comment = 1;
1544 }
1545 if (!$in_comment && $current_comment ne '') {
1546 $current_comment = '';
1547 }
1548 $current_comment .= $line . "\n" if ($in_comment);
1549 if ($line =~ m@\*/@) {
1550 $in_comment = 0;
1551 }
1552 }
1553
1554 chomp($current_comment);
1555 return($current_comment);
1556}
1557sub ctx_has_comment {
1558 my ($first_line, $end_line) = @_;
1559 my $cmt = ctx_locate_comment($first_line, $end_line);
1560
Andy Whitcroft00df3442007-06-08 13:47:06 -07001561 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001562 ##print "CMMT: $cmt\n";
1563
1564 return ($cmt ne '');
1565}
1566
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001567sub raw_line {
1568 my ($linenr, $cnt) = @_;
1569
1570 my $offset = $linenr - 1;
1571 $cnt++;
1572
1573 my $line;
1574 while ($cnt) {
1575 $line = $rawlines[$offset++];
1576 next if (defined($line) && $line =~ /^-/);
1577 $cnt--;
1578 }
1579
1580 return $line;
1581}
1582
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001583sub cat_vet {
1584 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001585 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001586
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001587 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001588 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1589 $res .= $1;
1590 if ($2 ne '') {
1591 $coded = sprintf("^%c", unpack('C', $2) + 64);
1592 $res .= $coded;
1593 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001594 }
1595 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001596
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001597 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001598}
1599
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001600my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001601my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001602my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001603my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001604
1605sub annotate_reset {
1606 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001607 $av_pending = '_';
1608 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001609 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001610}
1611
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001612sub annotate_values {
1613 my ($stream, $type) = @_;
1614
1615 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001616 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001617 my $cur = $stream;
1618
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001619 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001621 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001622 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001623 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001624 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001625 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001626 print "WS($1)\n" if ($dbg_values > 1);
1627 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001628 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001629 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001630 }
1631
Florian Micklerc023e4732011-01-12 16:59:58 -08001632 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001633 print "CAST($1)\n" if ($dbg_values > 1);
1634 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001635 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001636
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001637 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001638 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001639 $type = 'T';
1640
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001641 } elsif ($cur =~ /^($Modifier)\s*/) {
1642 print "MODIFIER($1)\n" if ($dbg_values > 1);
1643 $type = 'T';
1644
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001645 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001646 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001647 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001648 push(@av_paren_type, $type);
1649 if ($2 ne '') {
1650 $av_pending = 'N';
1651 }
1652 $type = 'E';
1653
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001654 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001655 print "UNDEF($1)\n" if ($dbg_values > 1);
1656 $av_preprocessor = 1;
1657 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001658
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001659 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001660 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001661 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001662
1663 push(@av_paren_type, $type);
1664 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001665 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001666
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001667 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001668 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1669 $av_preprocessor = 1;
1670
1671 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1672
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001673 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001674
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001675 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001676 print "PRE_END($1)\n" if ($dbg_values > 1);
1677
1678 $av_preprocessor = 1;
1679
1680 # Assume all arms of the conditional end as this
1681 # one does, and continue as if the #endif was not here.
1682 pop(@av_paren_type);
1683 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001684 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001685
1686 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001687 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001688
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001689 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1690 print "ATTR($1)\n" if ($dbg_values > 1);
1691 $av_pending = $type;
1692 $type = 'N';
1693
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001694 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001695 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001696 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001697 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001698 }
1699 $type = 'N';
1700
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001701 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001702 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001703 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001704 $type = 'N';
1705
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001706 } elsif ($cur =~/^(case)/o) {
1707 print "CASE($1)\n" if ($dbg_values > 1);
1708 $av_pend_colon = 'C';
1709 $type = 'N';
1710
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001711 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001712 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001713 $type = 'N';
1714
1715 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001716 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001717 push(@av_paren_type, $av_pending);
1718 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001719 $type = 'N';
1720
1721 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001722 my $new_type = pop(@av_paren_type);
1723 if ($new_type ne '_') {
1724 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 print "PAREN('$1') -> $type\n"
1726 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001727 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001728 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001729 }
1730
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001731 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001732 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001733 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001734 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001735
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001736 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1737 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001738 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001739 } elsif ($type eq 'E') {
1740 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001741 }
1742 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1743 $type = 'V';
1744
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001745 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001746 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001747 $type = 'V';
1748
1749 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001750 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001751 $type = 'N';
1752
Andy Whitcroftcf655042008-03-04 14:28:20 -08001753 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001754 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001755 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001756 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001757
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001758 } elsif ($cur =~/^(,)/) {
1759 print "COMMA($1)\n" if ($dbg_values > 1);
1760 $type = 'C';
1761
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001762 } elsif ($cur =~ /^(\?)/o) {
1763 print "QUESTION($1)\n" if ($dbg_values > 1);
1764 $type = 'N';
1765
1766 } elsif ($cur =~ /^(:)/o) {
1767 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1768
1769 substr($var, length($res), 1, $av_pend_colon);
1770 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1771 $type = 'E';
1772 } else {
1773 $type = 'N';
1774 }
1775 $av_pend_colon = 'O';
1776
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001777 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001778 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001779 $type = 'N';
1780
Andy Whitcroft0d413862008-10-15 22:02:16 -07001781 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001782 my $variant;
1783
1784 print "OPV($1)\n" if ($dbg_values > 1);
1785 if ($type eq 'V') {
1786 $variant = 'B';
1787 } else {
1788 $variant = 'U';
1789 }
1790
1791 substr($var, length($res), 1, $variant);
1792 $type = 'N';
1793
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001794 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001795 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 if ($1 ne '++' && $1 ne '--') {
1797 $type = 'N';
1798 }
1799
1800 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001801 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001802 }
1803 if (defined $1) {
1804 $cur = substr($cur, length($1));
1805 $res .= $type x length($1);
1806 }
1807 }
1808
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001809 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001810}
1811
Andy Whitcroft8905a672007-11-28 16:21:06 -08001812sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001813 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001814 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001815 ^(?:
1816 $Modifier|
1817 $Storage|
1818 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001819 DEFINE_\S+
1820 )$|
1821 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001822 goto|
1823 return|
1824 case|
1825 else|
1826 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001827 do|
1828 \#|
1829 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001830 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001831 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001832 )}x;
1833 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1834 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001835 # Check for modifiers.
1836 $possible =~ s/\s*$Storage\s*//g;
1837 $possible =~ s/\s*$Sparse\s*//g;
1838 if ($possible =~ /^\s*$/) {
1839
1840 } elsif ($possible =~ /\s/) {
1841 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001842 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001843 if ($modifier !~ $notPermitted) {
1844 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001845 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001846 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001847 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001848
1849 } else {
1850 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001851 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001852 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001853 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001854 } else {
1855 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001856 }
1857}
1858
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001859my $prefix = '';
1860
Joe Perches000d1cc12011-07-25 17:13:25 -07001861sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001862 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001863
Joe Perchescbec18a2014-04-03 14:49:19 -07001864 return defined $use_type{$type} if (scalar keys %use_type > 0);
1865
1866 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001867}
1868
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001869sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001870 my ($level, $type, $msg) = @_;
1871
1872 if (!show_type($type) ||
1873 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001874 return 0;
1875 }
Joe Perches57230292015-06-25 15:03:03 -07001876 my $output = '';
1877 if (-t STDOUT && $color) {
1878 if ($level eq 'ERROR') {
1879 $output .= RED;
1880 } elsif ($level eq 'WARNING') {
1881 $output .= YELLOW;
1882 } else {
1883 $output .= GREEN;
1884 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001885 }
Joe Perches57230292015-06-25 15:03:03 -07001886 $output .= $prefix . $level . ':';
1887 if ($show_types) {
1888 $output .= BLUE if (-t STDOUT && $color);
1889 $output .= "$type:";
1890 }
1891 $output .= RESET if (-t STDOUT && $color);
1892 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001893
1894 if ($showfile) {
1895 my @lines = split("\n", $output, -1);
1896 splice(@lines, 1, 1);
1897 $output = join("\n", @lines);
1898 }
Joe Perches57230292015-06-25 15:03:03 -07001899 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001900
Joe Perches57230292015-06-25 15:03:03 -07001901 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001902
1903 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001904}
Joe Perchescbec18a2014-04-03 14:49:19 -07001905
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001906sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001907 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001908}
Joe Perches000d1cc12011-07-25 17:13:25 -07001909
Joe Perchesd752fcc2014-08-06 16:11:05 -07001910sub fixup_current_range {
1911 my ($lineRef, $offset, $length) = @_;
1912
1913 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1914 my $o = $1;
1915 my $l = $2;
1916 my $no = $o + $offset;
1917 my $nl = $l + $length;
1918 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1919 }
1920}
1921
1922sub fix_inserted_deleted_lines {
1923 my ($linesRef, $insertedRef, $deletedRef) = @_;
1924
1925 my $range_last_linenr = 0;
1926 my $delta_offset = 0;
1927
1928 my $old_linenr = 0;
1929 my $new_linenr = 0;
1930
1931 my $next_insert = 0;
1932 my $next_delete = 0;
1933
1934 my @lines = ();
1935
1936 my $inserted = @{$insertedRef}[$next_insert++];
1937 my $deleted = @{$deletedRef}[$next_delete++];
1938
1939 foreach my $old_line (@{$linesRef}) {
1940 my $save_line = 1;
1941 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001942 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001943 $delta_offset = 0;
1944 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1945 $range_last_linenr = $new_linenr;
1946 fixup_current_range(\$line, $delta_offset, 0);
1947 }
1948
1949 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1950 $deleted = @{$deletedRef}[$next_delete++];
1951 $save_line = 0;
1952 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1953 }
1954
1955 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1956 push(@lines, ${$inserted}{'LINE'});
1957 $inserted = @{$insertedRef}[$next_insert++];
1958 $new_linenr++;
1959 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1960 }
1961
1962 if ($save_line) {
1963 push(@lines, $line);
1964 $new_linenr++;
1965 }
1966
1967 $old_linenr++;
1968 }
1969
1970 return @lines;
1971}
1972
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001973sub fix_insert_line {
1974 my ($linenr, $line) = @_;
1975
1976 my $inserted = {
1977 LINENR => $linenr,
1978 LINE => $line,
1979 };
1980 push(@fixed_inserted, $inserted);
1981}
1982
1983sub fix_delete_line {
1984 my ($linenr, $line) = @_;
1985
1986 my $deleted = {
1987 LINENR => $linenr,
1988 LINE => $line,
1989 };
1990
1991 push(@fixed_deleted, $deleted);
1992}
1993
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001994sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001995 my ($type, $msg) = @_;
1996
1997 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001998 our $clean = 0;
1999 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002000 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002001 }
Joe Perches3705ce52013-07-03 15:05:31 -07002002 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002003}
2004sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002005 my ($type, $msg) = @_;
2006
2007 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 our $clean = 0;
2009 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002010 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002011 }
Joe Perches3705ce52013-07-03 15:05:31 -07002012 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002013}
2014sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002015 my ($type, $msg) = @_;
2016
2017 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002018 our $clean = 0;
2019 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002020 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002021 }
Joe Perches3705ce52013-07-03 15:05:31 -07002022 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002023}
2024
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002025sub check_absolute_file {
2026 my ($absolute, $herecurr) = @_;
2027 my $file = $absolute;
2028
2029 ##print "absolute<$absolute>\n";
2030
2031 # See if any suffix of this path is a path within the tree.
2032 while ($file =~ s@^[^/]*/@@) {
2033 if (-f "$root/$file") {
2034 ##print "file<$file>\n";
2035 last;
2036 }
2037 }
2038 if (! -f _) {
2039 return 0;
2040 }
2041
2042 # It is, so see if the prefix is acceptable.
2043 my $prefix = $absolute;
2044 substr($prefix, -length($file)) = '';
2045
2046 ##print "prefix<$prefix>\n";
2047 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002048 WARN("USE_RELATIVE_PATH",
2049 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002050 }
2051}
2052
Joe Perches3705ce52013-07-03 15:05:31 -07002053sub trim {
2054 my ($string) = @_;
2055
Joe Perchesb34c6482013-09-11 14:24:01 -07002056 $string =~ s/^\s+|\s+$//g;
2057
2058 return $string;
2059}
2060
2061sub ltrim {
2062 my ($string) = @_;
2063
2064 $string =~ s/^\s+//;
2065
2066 return $string;
2067}
2068
2069sub rtrim {
2070 my ($string) = @_;
2071
2072 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002073
2074 return $string;
2075}
2076
Joe Perches52ea8502013-11-12 15:10:09 -08002077sub string_find_replace {
2078 my ($string, $find, $replace) = @_;
2079
2080 $string =~ s/$find/$replace/g;
2081
2082 return $string;
2083}
2084
Joe Perches3705ce52013-07-03 15:05:31 -07002085sub tabify {
2086 my ($leading) = @_;
2087
2088 my $source_indent = 8;
2089 my $max_spaces_before_tab = $source_indent - 1;
2090 my $spaces_to_tab = " " x $source_indent;
2091
2092 #convert leading spaces to tabs
2093 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2094 #Remove spaces before a tab
2095 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2096
2097 return "$leading";
2098}
2099
Gregory Beanf07523e2011-07-06 15:36:58 -07002100sub cleanup_continuation_headers {
2101 # Collapse any header-continuation lines into a single line so they
2102 # can be parsed meaningfully, as the parser only has one line
2103 # of context to work with.
2104 my $again;
2105 do {
2106 $again = 0;
2107 foreach my $n (0 .. scalar(@rawlines) - 2) {
2108 if ($rawlines[$n]=~/^\s*$/) {
2109 # A blank line means there's no more chance
2110 # of finding headers. Shortcut to done.
2111 return;
2112 }
2113 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2114 $rawlines[$n+1]=~/^\s+/) {
2115 # Continuation header. Collapse it.
2116 my $line = splice @rawlines, $n+1, 1;
2117 $line=~s/^\s+/ /;
2118 $rawlines[$n] .= $line;
2119 # We've 'destabilized' the list, so restart.
2120 $again = 1;
2121 last;
2122 }
2123 }
2124 } while ($again);
2125}
2126
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002127sub pos_last_openparen {
2128 my ($line) = @_;
2129
2130 my $pos = 0;
2131
2132 my $opens = $line =~ tr/\(/\(/;
2133 my $closes = $line =~ tr/\)/\)/;
2134
2135 my $last_openparen = 0;
2136
2137 if (($opens == 0) || ($closes >= $opens)) {
2138 return -1;
2139 }
2140
2141 my $len = length($line);
2142
2143 for ($pos = 0; $pos < $len; $pos++) {
2144 my $string = substr($line, $pos);
2145 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2146 $pos += length($1) - 1;
2147 } elsif (substr($line, $pos, 1) eq '(') {
2148 $last_openparen = $pos;
2149 } elsif (index($string, '(') == -1) {
2150 last;
2151 }
2152 }
2153
Joe Perches91cb5192014-04-03 14:49:32 -07002154 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002155}
2156
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002157sub process {
2158 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002159
2160 my $linenr=0;
2161 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002162 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002163 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002164 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002165 my $subjectline="";
2166 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002167
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002168 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002169 my $indent;
2170 my $previndent=0;
2171 my $stashindent=0;
2172
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002173 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002174 my $signoff = 0;
2175 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002176 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002177 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002178 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002179 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002180 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002181 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002182 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002183 my $non_utf8_charset = 0;
2184
Joe Perches365dd4e2014-08-06 16:10:42 -07002185 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002186 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002187
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002188 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002189 our $cnt_lines = 0;
2190 our $cnt_error = 0;
2191 our $cnt_warn = 0;
2192 our $cnt_chk = 0;
2193
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002194 # Trace the real file/line as we go.
2195 my $realfile = '';
2196 my $realline = 0;
2197 my $realcnt = 0;
2198 my $here = '';
2199 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002200 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002201 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002202 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002203
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002204 my $prev_values = 'E';
2205
2206 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002207 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002208 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002209 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002210 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002211
Joe Perches7e51f192013-09-11 14:23:57 -07002212 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002213
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002214 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002215 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002216 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002217 my @setup_docs = ();
2218 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219
Joe Perchesd8b07712013-11-12 15:10:06 -08002220 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002221 my $shorttext = BEFORE_SHORTTEXT;
2222 my $shorttext_exspc = 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002223 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002224
Andy Whitcroft773647a2008-03-28 14:15:58 -07002225 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002226 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002227 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002228
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002229 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002230 $linenr++;
2231 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002232
Joe Perches3705ce52013-07-03 15:05:31 -07002233 push(@fixed, $rawline) if ($fix);
2234
Andy Whitcroft773647a2008-03-28 14:15:58 -07002235 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002236 $setup_docs = 0;
2237 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2238 $setup_docs = 1;
2239 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002240 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002241 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002242 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2243 $realline=$1-1;
2244 if (defined $2) {
2245 $realcnt=$3+1;
2246 } else {
2247 $realcnt=1+1;
2248 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002249 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002250
2251 # Guestimate if this is a continuing comment. Run
2252 # the context looking for a comment "edge". If this
2253 # edge is a close comment then we must be in a comment
2254 # at context start.
2255 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002256 my $cnt = $realcnt;
2257 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2258 next if (defined $rawlines[$ln - 1] &&
2259 $rawlines[$ln - 1] =~ /^-/);
2260 $cnt--;
2261 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002262 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002263 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2264 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2265 ($edge) = $1;
2266 last;
2267 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002268 }
2269 if (defined $edge && $edge eq '*/') {
2270 $in_comment = 1;
2271 }
2272
2273 # Guestimate if this is a continuing comment. If this
2274 # is the start of a diff block and this line starts
2275 # ' *' then it is very likely a comment.
2276 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002277 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002278 {
2279 $in_comment = 1;
2280 }
2281
2282 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2283 sanitise_line_reset($in_comment);
2284
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002285 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002286 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002287 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002288 $line = sanitise_line($rawline);
2289 }
2290 push(@lines, $line);
2291
2292 if ($realcnt > 1) {
2293 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2294 } else {
2295 $realcnt = 0;
2296 }
2297
2298 #print "==>$rawline\n";
2299 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002300
2301 if ($setup_docs && $line =~ /^\+/) {
2302 push(@setup_docs, $line);
2303 }
2304 }
2305
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002306 $prefix = '';
2307
Andy Whitcroft773647a2008-03-28 14:15:58 -07002308 $realcnt = 0;
2309 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002310 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002311 foreach my $line (@lines) {
2312 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002313 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002314 my $sline = $line; #copy of $line
2315 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002317 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002318
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002319#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002320 if (!$in_commit_log &&
2321 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002322 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002323 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002324 $realline=$1-1;
2325 if (defined $2) {
2326 $realcnt=$3+1;
2327 } else {
2328 $realcnt=1+1;
2329 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002330 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002331 $prev_values = 'E';
2332
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002334 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002335 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002336 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002337 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002338
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002339# track the line number as we move through the hunk, note that
2340# new versions of GNU diff omit the leading space on completely
2341# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002342 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002343 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002344 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002345
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002346 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002347 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002348
2349 # Track the previous line.
2350 ($prevline, $stashline) = ($stashline, $line);
2351 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002352 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2353
Andy Whitcroft773647a2008-03-28 14:15:58 -07002354 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002355
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002356 } elsif ($realcnt == 1) {
2357 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002358 }
2359
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002360 my $hunk_line = ($realcnt != 0);
2361
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002362 $here = "#$linenr: " if (!$file);
2363 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002364
Joe Perches2ac73b42014-06-04 16:12:05 -07002365 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002366 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002367 if ($line =~ /^diff --git.*?(\S+)$/) {
2368 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002369 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002370 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002371 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002372 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002373 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002374 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002375 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002376
2377 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002378 if (!$file && $tree && $p1_prefix ne '' &&
2379 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002380 WARN("PATCH_PREFIX",
2381 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002382 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002383
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002384 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002385 ERROR("MODIFIED_INCLUDE_ASM",
2386 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002387 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002388 $found_file = 1;
2389 }
2390
Joe Perches34d88152015-06-25 15:03:05 -07002391#make up the handle for any error we report on this line
2392 if ($showfile) {
2393 $prefix = "$realfile:$realline: "
2394 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002395 if ($file) {
2396 $prefix = "$filename:$realline: ";
2397 } else {
2398 $prefix = "$filename:$linenr: ";
2399 }
Joe Perches34d88152015-06-25 15:03:05 -07002400 }
2401
Joe Perches2ac73b42014-06-04 16:12:05 -07002402 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002403 if (is_maintained_obsolete($realfile)) {
2404 WARN("OBSOLETE",
2405 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2406 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002407 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002408 $check = 1;
2409 } else {
2410 $check = $check_orig;
2411 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002412 next;
2413 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002414 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002415
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002416 my $hereline = "$here\n$rawline\n";
2417 my $herecurr = "$here\n$rawline\n";
2418 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002419
David Keitel72bf3e82016-03-23 20:52:44 -07002420 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002421 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2422 # the subject line was just processed,
2423 # a blank line must be next
2424 WARN("NONBLANK_AFTER_SUMMARY",
2425 "non-blank line after summary line\n" . $herecurr);
2426 $shorttext = IN_SHORTTEXT;
2427 # this non-blank line may or may not be commit text -
2428 # a warning has been generated so assume it is commit
2429 # text and move on
2430 $commit_text_present = 1;
2431 # fall through and treat this line as IN_SHORTTEXT
2432 }
David Keitel72bf3e82016-03-23 20:52:44 -07002433 if ($shorttext == IN_SHORTTEXT) {
2434 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002435 if ($commit_text_present == 0) {
2436 WARN("NO_COMMIT_TEXT",
2437 "please add commit text explaining " .
2438 "*why* the change is needed\n" .
2439 $herecurr);
2440 }
David Keitel72bf3e82016-03-23 20:52:44 -07002441 $shorttext = AFTER_SHORTTEXT;
Steve Muckled360fe42012-03-09 11:15:24 -08002442 } elsif ($line=~/^\s*change-id:/i ||
2443 $line=~/^\s*signed-off-by:/i ||
2444 $line=~/^\s*crs-fixed:/i ||
2445 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002446 # this is a tag, there must be commit
2447 # text by now
2448 if ($commit_text_present == 0) {
2449 WARN("NO_COMMIT_TEXT",
2450 "please add commit text explaining " .
2451 "*why* the change is needed\n" .
2452 $herecurr);
2453 # prevent duplicate warnings
2454 $commit_text_present = 1;
2455 }
2456 } elsif ($line=~/\S/) {
2457 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002458 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002459 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2460 # case of non-blank line in this state handled above
2461 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002462 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002463# The Subject line doesn't have to be the last header in the patch.
2464# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2465# Per RFC5322, continuation lines must be folded, so any left-justified
2466# text which looks like a header is definitely a header.
2467 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2468 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002469 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002470 if (length($line) != 0) {
2471 WARN("NONBLANK_AFTER_SUMMARY",
2472 "non-blank line after " .
2473 "summary line\n" .
2474 $sublinenr . $here .
2475 "\n" . $subjectline .
2476 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002477 # this non-blank line may or may not
2478 # be commit text - a warning has been
2479 # generated so assume it is commit
2480 # text and move on
2481 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002482 }
David Keitele288d232016-03-23 20:57:21 -07002483 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002484 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002485 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002486 # This is the subject line. Go to
2487 # CHECK_NEXT_SHORTTEXT to wait for the commit
2488 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002489 $shorttext = CHECK_NEXT_SHORTTEXT;
2490 $subjectline = $line;
2491 $sublinenr = "#$linenr & ";
2492# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002493 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002494 WARN("LONG_SUMMARY_LINE",
2495 "summary line over " .
2496 SHORTTEXT_LIMIT .
2497 " characters\n" . $herecurr);
2498 }
2499 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002500 # Indented format, this must be the summary
2501 # line (i.e. git show). There will be no more
2502 # headers so we are now in the shorttext.
2503 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002504 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002505 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002506 WARN("LONG_SUMMARY_LINE",
2507 "summary line over " .
2508 SHORTTEXT_LIMIT .
2509 " characters\n" . $herecurr);
2510 }
2511 }
2512 }
2513
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002514 $cnt_lines++ if ($realcnt != 0);
2515
Joe Perchese518e9a2015-06-25 15:03:27 -07002516# Check if the commit log has what seems like a diff which can confuse patch
2517 if ($in_commit_log && !$commit_log_has_diff &&
Mrinal Pandey913412e2020-09-04 16:35:52 -07002518 (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2519 $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
Joe Perchese518e9a2015-06-25 15:03:27 -07002520 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2521 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2522 ERROR("DIFF_IN_COMMIT_MSG",
2523 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2524 $commit_log_has_diff = 1;
2525 }
2526
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002527# Check for incorrect file permissions
2528 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2529 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002530 if ($realfile !~ m@scripts/@ &&
2531 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002532 ERROR("EXECUTE_PERMISSIONS",
2533 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002534 }
2535 }
2536
Joe Perches20112472011-07-25 17:13:23 -07002537# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002538 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002539 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002540 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002541 }
2542
Joe Perchese0d975b2014-12-10 15:51:49 -08002543# Check if MAINTAINERS is being updated. If so, there's probably no need to
2544# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2545 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2546 $reported_maintainer_file = 1;
2547 }
2548
Joe Perches20112472011-07-25 17:13:23 -07002549# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002550 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002551 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002552 my $space_before = $1;
2553 my $sign_off = $2;
2554 my $space_after = $3;
2555 my $email = $4;
2556 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2557
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002558 if ($sign_off !~ /$signature_tags/) {
2559 WARN("BAD_SIGN_OFF",
2560 "Non-standard signature: $sign_off\n" . $herecurr);
2561 }
Joe Perches20112472011-07-25 17:13:23 -07002562 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002563 if (WARN("BAD_SIGN_OFF",
2564 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2565 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002566 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002567 "$ucfirst_sign_off $email";
2568 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002569 }
Joe Perches20112472011-07-25 17:13:23 -07002570 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002571 if (WARN("BAD_SIGN_OFF",
2572 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2573 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002574 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002575 "$ucfirst_sign_off $email";
2576 }
2577
Joe Perches20112472011-07-25 17:13:23 -07002578 }
2579 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002580 if (WARN("BAD_SIGN_OFF",
2581 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2582 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002583 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002584 "$ucfirst_sign_off $email";
2585 }
Joe Perches20112472011-07-25 17:13:23 -07002586 }
2587
2588 my ($email_name, $email_address, $comment) = parse_email($email);
2589 my $suggested_email = format_email(($email_name, $email_address));
2590 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002591 ERROR("BAD_SIGN_OFF",
2592 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002593 } else {
2594 my $dequoted = $suggested_email;
2595 $dequoted =~ s/^"//;
2596 $dequoted =~ s/" </ </;
2597 # Don't force email to have quotes
2598 # Allow just an angle bracketed address
2599 if ("$dequoted$comment" ne $email &&
2600 "<$email_address>$comment" ne $email &&
2601 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002602 WARN("BAD_SIGN_OFF",
2603 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002604 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002605 }
Maya Erezec77bd62017-02-26 09:14:43 +02002606 if ($chk_author) {
2607 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2608 $qca_sign_off = 1;
2609 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2610 $codeaurora_sign_off = 1;
Vijayanand Jittae4a2eae2021-10-28 12:26:41 +05302611 } elsif ($line =~ /^\s*signed-off-by:.*qualcomm\.com/i) {
Maya Erezec77bd62017-02-26 09:14:43 +02002612 WARN("BAD_SIGN_OFF",
2613 "invalid Signed-off-by identity\n" . $line );
2614 }
2615 }
Joe Perches7e51f192013-09-11 14:23:57 -07002616
2617# Check for duplicate signatures
2618 my $sig_nospace = $line;
2619 $sig_nospace =~ s/\s//g;
2620 $sig_nospace = lc($sig_nospace);
2621 if (defined $signatures{$sig_nospace}) {
Jordan Crouse82e60672018-08-08 13:01:36 -06002622 WARN("DUPLICATE_SIGN_OFF",
Joe Perches7e51f192013-09-11 14:23:57 -07002623 "Duplicate signature\n" . $herecurr);
2624 } else {
2625 $signatures{$sig_nospace} = 1;
2626 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002627 }
2628
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002629# Check email subject for common tools that don't need to be mentioned
2630 if ($in_header_lines &&
2631 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2632 WARN("EMAIL_SUBJECT",
2633 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2634 }
2635
Joe Perches9b3189e2014-06-04 16:12:10 -07002636# Check for old stable address
2637 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2638 ERROR("STABLE_ADDRESS",
2639 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2640 }
2641
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002642# Check for unwanted Gerrit info
2643 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2644 ERROR("GERRIT_CHANGE_ID",
2645 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2646 }
2647
Joe Perches369c8dd2015-11-06 16:31:34 -08002648# Check if the commit log is in a possible stack dump
2649 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2650 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2651 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2652 # timestamp
2653 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2654 # stack dump address
2655 $commit_log_possible_stack_dump = 1;
2656 }
2657
Joe Perches2a076f42015-04-16 12:44:28 -07002658# Check for line lengths > 75 in commit log, warn once
2659 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002660 length($line) > 75 &&
2661 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2662 # file delta changes
2663 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2664 # filename then :
2665 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2666 # A Fixes: or Link: line
2667 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002668 WARN("COMMIT_LOG_LONG_LINE",
2669 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2670 $commit_log_long_line = 1;
2671 }
2672
Joe Perchesbf4daf12015-09-09 15:37:50 -07002673# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002674 if ($in_commit_log && $commit_log_possible_stack_dump &&
2675 $line =~ /^\s*$/) {
2676 $commit_log_possible_stack_dump = 0;
2677 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002678
Joe Perches0d7835f2015-02-13 14:38:35 -08002679# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002680 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002681 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wang5b101522017-04-07 15:22:19 -07002682 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002683 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002684 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002685 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2686 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002687 my $init_char = "c";
2688 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002689 my $short = 1;
2690 my $long = 0;
2691 my $case = 1;
2692 my $space = 1;
2693 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002694 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002695 my $id = '0123456789ab';
2696 my $orig_desc = "commit description";
2697 my $description = "";
2698
Joe Perchesfe043ea2015-09-09 15:37:25 -07002699 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2700 $init_char = $1;
2701 $orig_commit = lc($2);
2702 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2703 $orig_commit = lc($1);
2704 }
2705
Joe Perches0d7835f2015-02-13 14:38:35 -08002706 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2707 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2708 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2709 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2710 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2711 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002712 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002713 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2714 defined $rawlines[$linenr] &&
2715 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2716 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002717 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002718 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2719 defined $rawlines[$linenr] &&
2720 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2721 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2722 $orig_desc = $1;
2723 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2724 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002725 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002726 }
2727
2728 ($id, $description) = git_commit_info($orig_commit,
2729 $id, $orig_desc);
2730
Joe Perches19c146a2015-02-13 14:39:00 -08002731 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002732 ERROR("GIT_COMMIT_ID",
2733 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2734 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002735 }
2736
Joe Perches13f19372014-08-06 16:10:59 -07002737# Check for added, moved or deleted files
2738 if (!$reported_maintainer_file && !$in_commit_log &&
2739 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2740 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2741 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2742 (defined($1) || defined($2))))) {
2743 $reported_maintainer_file = 1;
2744 WARN("FILE_PATH_CHANGES",
2745 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2746 }
2747
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002748#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002749 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
Vijayanand Jittae4a2eae2021-10-28 12:26:41 +05302750 $line =~ /^From:.*qualcomm\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002751 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2752 }
2753
Andy Whitcroft00df3442007-06-08 13:47:06 -07002754# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002755 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002756 ERROR("CORRUPTED_PATCH",
2757 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002758 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002759 }
2760
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002761# Check for absolute kernel paths.
2762 if ($tree) {
2763 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2764 my $file = $1;
2765
2766 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2767 check_absolute_file($1, $herecurr)) {
2768 #
2769 } else {
2770 check_absolute_file($file, $herecurr);
2771 }
2772 }
2773 }
2774
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002775# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2776 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002777 $rawline !~ m/^$UTF8*$/) {
2778 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2779
2780 my $blank = copy_spacing($rawline);
2781 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2782 my $hereptr = "$hereline$ptr\n";
2783
Joe Perches34d99212011-07-25 17:13:26 -07002784 CHK("INVALID_UTF8",
2785 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002786 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002787
Joe Perches15662b32011-10-31 17:13:12 -07002788# Check if it's the start of a commit log
2789# (not a header line and we haven't seen the patch filename)
2790 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002791 !($rawline =~ /^\s+\S/ ||
2792 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002793 $in_header_lines = 0;
2794 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002795 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002796 }
2797
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002798# Check if there is UTF-8 in a commit log when a mail header has explicitly
2799# declined it, i.e defined some charset where it is missing.
2800 if ($in_header_lines &&
2801 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2802 $1 !~ /utf-8/i) {
2803 $non_utf8_charset = 1;
2804 }
2805
2806 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002807 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002808 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002809 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2810 }
2811
Kees Cook66b47b42014-10-13 15:51:57 -07002812# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002813 if (defined($misspellings) &&
2814 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002815 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002816 my $typo = $1;
2817 my $typo_fix = $spelling_fix{lc($typo)};
2818 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2819 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2820 my $msg_type = \&WARN;
2821 $msg_type = \&CHK if ($file);
2822 if (&{$msg_type}("TYPO_SPELLING",
2823 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2824 $fix) {
2825 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2826 }
2827 }
2828 }
2829
Andy Whitcroft306708542008-10-15 22:02:28 -07002830# ignore non-hunk lines and lines being removed
2831 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002832
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002833#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002834 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002835 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002836 if (ERROR("DOS_LINE_ENDINGS",
2837 "DOS line endings\n" . $herevet) &&
2838 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002839 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002840 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002841 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2842 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002843 if (ERROR("TRAILING_WHITESPACE",
2844 "trailing whitespace\n" . $herevet) &&
2845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002846 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002847 }
2848
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002849 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002850 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002851
Josh Triplett4783f892013-11-12 15:10:12 -08002852# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002853 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002854 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2855 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002856 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2857 my $msg_type = \&ERROR;
2858 $msg_type = \&CHK if ($file);
2859 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002860 "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)
Josh Triplett4783f892013-11-12 15:10:12 -08002861 }
2862
Andi Kleen33549572010-05-24 14:33:29 -07002863# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002864# Only applies when adding the entry originally, after that we do not have
2865# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002866 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002867 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002868 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002869 my $cnt = $realcnt;
2870 my $ln = $linenr + 1;
2871 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002872 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002873 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002874 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002875 $f = $lines[$ln - 1];
2876 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2877 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002878
2879 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002880 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002881
Joe Perches8d73e0e2014-08-06 16:10:46 -07002882 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002883 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002884 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002885 $length = -1;
2886 }
2887
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002888 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002889 $f =~ s/#.*//;
2890 $f =~ s/^\s+//;
2891 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002892 if ($f =~ /^\s*config\s/) {
2893 $is_end = 1;
2894 last;
2895 }
Andi Kleen33549572010-05-24 14:33:29 -07002896 $length++;
2897 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002898 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2899 WARN("CONFIG_DESCRIPTION",
2900 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2901 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002902 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002903 }
2904
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002905# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2906 if ($realfile =~ /Kconfig/ &&
2907 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2908 WARN("CONFIG_EXPERIMENTAL",
2909 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2910 }
2911
Christoph Jaeger327953e2015-02-13 14:38:29 -08002912# discourage the use of boolean for type definition attributes of Kconfig options
2913 if ($realfile =~ /Kconfig/ &&
2914 $line =~ /^\+\s*\bboolean\b/) {
2915 WARN("CONFIG_TYPE_BOOLEAN",
2916 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2917 }
2918
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002919 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2920 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2921 my $flag = $1;
2922 my $replacement = {
2923 'EXTRA_AFLAGS' => 'asflags-y',
2924 'EXTRA_CFLAGS' => 'ccflags-y',
2925 'EXTRA_CPPFLAGS' => 'cppflags-y',
2926 'EXTRA_LDFLAGS' => 'ldflags-y',
2927 };
2928
2929 WARN("DEPRECATED_VARIABLE",
2930 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2931 }
2932
Rob Herringbff5da42014-01-23 15:54:51 -08002933# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002934 if (defined $root &&
2935 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2936 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2937
Rob Herringbff5da42014-01-23 15:54:51 -08002938 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2939
Florian Vaussardcc933192014-04-03 14:49:27 -07002940 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2941 my $vp_file = $dt_path . "vendor-prefixes.txt";
2942
Rob Herringbff5da42014-01-23 15:54:51 -08002943 foreach my $compat (@compats) {
2944 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002945 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2946 my $compat3 = $compat;
2947 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2948 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002949 if ( $? >> 8 ) {
2950 WARN("UNDOCUMENTED_DT_STRING",
2951 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2952 }
2953
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002954 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2955 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002956 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002957 if ( $? >> 8 ) {
2958 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002959 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002960 }
2961 }
2962 }
2963
Andy Whitcroft5368df22008-10-15 22:02:27 -07002964# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002965 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002966
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002967# do DT checks:
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002968# * Property names should be lower-case
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002969# * Only newline after };
2970
2971 if ($realfile =~ /\.(dts|dtsi)$/ && $line =~ /^\+/) {
2972 if($line =~ /\};.+$/) { # check for any characters after };
2973 ERROR("DT_STYLE", "newline does not follow immediately after };\n" . $herecurr);
2974 }
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002975
2976 if($line =~ /[0-9a-zA-Z,\._\+\?#]+\s*=/ && $line !~ /[0-9a-z,\._\+\?#]+\s*=/) { # find property names with uppercase characters
2977 ERROR("DT_PROPERTY_NAME", "property name is not lowercase\n" . $herecurr);
2978 }
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002979 }
2980
Joe Perches47e0c882015-06-25 15:02:57 -07002981# line length limit (with some exclusions)
2982#
2983# There are a few types of lines that may extend beyond $max_line_length:
2984# logging functions like pr_info that end in a string
2985# lines with a single string
2986# #defines that are a single string
2987#
2988# There are 3 different line length message types:
2989# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2990# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2991# LONG_LINE all other lines longer than $max_line_length
2992#
2993# if LONG_LINE is ignored, the other 2 types are also ignored
2994#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002995 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002996 my $msg_type = "LONG_LINE";
2997
2998 # Check the allowed long line types first
2999
3000 # logging functions that end in a string that starts
3001 # before $max_line_length
3002 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3003 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3004 $msg_type = "";
3005
3006 # lines with only strings (w/ possible termination)
3007 # #defines with only strings
3008 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3009 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3010 $msg_type = "";
3011
Joe Perchesd560a5f2016-08-02 14:04:31 -07003012 # EFI_GUID is another special case
3013 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
3014 $msg_type = "";
3015
Bryan Huntsman9b1dda12018-02-12 17:26:10 -08003016 # Long copyright statements are another special case
3017 } elsif ($rawline =~ /^\+.\*.*copyright.*\(c\).*$/i) {
3018 $msg_type = "";
3019
Joe Perches47e0c882015-06-25 15:02:57 -07003020 # Otherwise set the alternate message types
3021
3022 # a comment starts before $max_line_length
3023 } elsif ($line =~ /($;[\s$;]*)$/ &&
3024 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3025 $msg_type = "LONG_LINE_COMMENT"
3026
3027 # a quoted string starts before $max_line_length
3028 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3029 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3030 $msg_type = "LONG_LINE_STRING"
3031 }
3032
3033 if ($msg_type ne "" &&
3034 (show_type("LONG_LINE") || show_type($msg_type))) {
3035 WARN($msg_type,
3036 "line over $max_line_length characters\n" . $herecurr);
3037 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003038 }
3039
Andy Whitcroft8905a672007-11-28 16:21:06 -08003040# check for adding lines without a newline.
3041 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003042 WARN("MISSING_EOF_NEWLINE",
3043 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003044 }
3045
Mike Frysinger42e41c52009-09-21 17:04:40 -07003046# Blackfin: use hi/lo macros
3047 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3048 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3049 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003050 ERROR("LO_MACRO",
3051 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003052 }
3053 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3054 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003055 ERROR("HI_MACRO",
3056 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003057 }
3058 }
3059
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003060# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003061 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003062
3063# at the beginning of a line any tabs must come first and anything
3064# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003065 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3066 $rawline =~ /^\+\s* \s*/) {
3067 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003068 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003069 if (ERROR("CODE_INDENT",
3070 "code indent should use tabs where possible\n" . $herevet) &&
3071 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003072 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003073 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003074 }
3075
Alberto Panizzo08e44362010-03-05 13:43:54 -08003076# check for space before tabs.
3077 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3078 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003079 if (WARN("SPACE_BEFORE_TAB",
3080 "please, no space before tabs\n" . $herevet) &&
3081 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003082 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003083 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003084 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003085 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003086 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003087 }
3088
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003089# check for && or || at the start of a line
3090 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3091 CHK("LOGICAL_CONTINUATIONS",
3092 "Logical continuations should be on the previous line\n" . $hereprev);
3093 }
3094
Joe Perchesa91e8992016-05-20 17:04:05 -07003095# check indentation starts on a tab stop
3096 if ($^V && $^V ge 5.10.0 &&
3097 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3098 my $indent = length($1);
3099 if ($indent % 8) {
3100 if (WARN("TABSTOP",
3101 "Statements should start on a tabstop\n" . $herecurr) &&
3102 $fix) {
3103 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3104 }
3105 }
3106 }
3107
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003108# check multi-line statement indentation matches previous line
3109 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003110 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003111 $prevline =~ /^\+(\t*)(.*)$/;
3112 my $oldindent = $1;
3113 my $rest = $2;
3114
3115 my $pos = pos_last_openparen($rest);
3116 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003117 $line =~ /^(\+| )([ \t]*)/;
3118 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003119
3120 my $goodtabindent = $oldindent .
3121 "\t" x ($pos / 8) .
3122 " " x ($pos % 8);
3123 my $goodspaceindent = $oldindent . " " x $pos;
3124
3125 if ($newindent ne $goodtabindent &&
3126 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003127
3128 if (CHK("PARENTHESIS_ALIGNMENT",
3129 "Alignment should match open parenthesis\n" . $hereprev) &&
3130 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003131 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003132 s/^\+[ \t]*/\+$goodtabindent/;
3133 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003134 }
3135 }
3136 }
3137
Joe Perches6ab3a972015-04-16 12:44:05 -07003138# check for space after cast like "(int) foo" or "(struct foo) bar"
3139# avoid checking a few false positives:
3140# "sizeof(<type>)" or "__alignof__(<type>)"
3141# function pointer declarations like "(*foo)(int) = bar;"
3142# structure definitions like "(struct foo) { 0 };"
3143# multiline macros that define functions
3144# known attributes or the __attribute__ keyword
3145 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3146 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003147 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003148 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003149 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003150 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003151 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003152 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003153 }
3154
Joe Perches86406b12015-09-09 15:37:41 -07003155# Block comment styles
3156# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003157 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003158 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003159 $rawline =~ /^\+[ \t]*\*/ &&
3160 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003161 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3162 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3163 }
3164
Joe Perches86406b12015-09-09 15:37:41 -07003165# Block comments use * on subsequent lines
3166 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3167 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003168 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003169 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003170 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003171 WARN("BLOCK_COMMENT_STYLE",
3172 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003173 }
3174
Joe Perches86406b12015-09-09 15:37:41 -07003175# Block comments use */ on trailing lines
3176 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003177 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3178 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3179 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003180 WARN("BLOCK_COMMENT_STYLE",
3181 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003182 }
3183
Joe Perches08eb9b82016-10-11 13:51:50 -07003184# Block comment * alignment
3185 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003186 $line =~ /^\+[ \t]*$;/ && #leading comment
3187 $rawline =~ /^\+[ \t]*\*/ && #leading *
3188 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003189 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003190 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3191 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003192 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003193 if (defined($1)) {
3194 $oldindent = expand_tabs($1);
3195 } else {
3196 $prevrawline =~ m@^\+(.*/?)\*@;
3197 $oldindent = expand_tabs($1);
3198 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003199 $rawline =~ m@^\+([ \t]*)\*@;
3200 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003201 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003202 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003203 WARN("BLOCK_COMMENT_STYLE",
3204 "Block comments should align the * on each line\n" . $hereprev);
3205 }
3206 }
3207
Joe Perches7f619192014-08-06 16:10:39 -07003208# check for missing blank lines after struct/union declarations
3209# with exceptions for various attributes and macros
3210 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3211 $line =~ /^\+/ &&
3212 !($line =~ /^\+\s*$/ ||
3213 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3214 $line =~ /^\+\s*MODULE_/i ||
3215 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3216 $line =~ /^\+[a-z_]*init/ ||
3217 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3218 $line =~ /^\+\s*DECLARE/ ||
3219 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003220 if (CHK("LINE_SPACING",
3221 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3222 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003223 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003224 }
Joe Perches7f619192014-08-06 16:10:39 -07003225 }
3226
Joe Perches365dd4e2014-08-06 16:10:42 -07003227# check for multiple consecutive blank lines
3228 if ($prevline =~ /^[\+ ]\s*$/ &&
3229 $line =~ /^\+\s*$/ &&
3230 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003231 if (CHK("LINE_SPACING",
3232 "Please don't use multiple blank lines\n" . $hereprev) &&
3233 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003234 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003235 }
3236
Joe Perches365dd4e2014-08-06 16:10:42 -07003237 $last_blank_line = $linenr;
3238 }
3239
Joe Perches3b617e3b2014-04-03 14:49:28 -07003240# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003241 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3242 # actual declarations
3243 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003244 # function pointer declarations
3245 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003246 # foo bar; where foo is some local typedef or #define
3247 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3248 # known declaration macros
3249 $prevline =~ /^\+\s+$declaration_macros/) &&
3250 # for "else if" which can look like "$Ident $Ident"
3251 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3252 # other possible extensions of declaration lines
3253 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3254 # not starting a section or a macro "\" extended line
3255 $prevline =~ /(?:\{\s*|\\)$/) &&
3256 # looks like a declaration
3257 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003258 # function pointer declarations
3259 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003260 # foo bar; where foo is some local typedef or #define
3261 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3262 # known declaration macros
3263 $sline =~ /^\+\s+$declaration_macros/ ||
3264 # start of struct or union or enum
Joe Perches3b617e3b2014-04-03 14:49:28 -07003265 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003266 # start or end of block or continuation of declaration
3267 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3268 # bitfield continuation
3269 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3270 # other possible extensions of declaration lines
3271 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3272 # indentation of previous and current line are the same
3273 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003274 if (WARN("LINE_SPACING",
3275 "Missing a blank line after declarations\n" . $hereprev) &&
3276 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003277 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003278 }
Joe Perches3b617e3b2014-04-03 14:49:28 -07003279 }
3280
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003281# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003282# Exceptions:
3283# 1) within comments
3284# 2) indented preprocessor commands
3285# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003286 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003287 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003288 if (WARN("LEADING_SPACE",
3289 "please, no spaces at the start of a line\n" . $herevet) &&
3290 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003291 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003292 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003293 }
3294
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003295# check we are in a valid C source file if not then ignore this hunk
3296 next if ($realfile !~ /\.(h|c)$/);
3297
Joe Perches032a4c02014-08-06 16:10:29 -07003298# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003299# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003300# if the previous line is a break or return and is indented 1 tab more...
3301 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3302 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003303 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3304 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3305 defined $lines[$linenr] &&
3306 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003307 WARN("UNNECESSARY_ELSE",
3308 "else is not generally useful after a break or return\n" . $hereprev);
3309 }
3310 }
3311
Joe Perchesc00df192014-08-06 16:11:01 -07003312# check indentation of a line with a break;
3313# if the previous line is a goto or return and is indented the same # of tabs
3314 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3315 my $tabs = $1;
3316 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3317 WARN("UNNECESSARY_BREAK",
3318 "break is not useful after a goto or return\n" . $hereprev);
3319 }
3320 }
3321
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003322# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3323 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3324 WARN("CONFIG_EXPERIMENTAL",
3325 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3326 }
3327
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003328# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003329 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003330 WARN("CVS_KEYWORD",
3331 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003332 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003333
Mike Frysinger42e41c52009-09-21 17:04:40 -07003334# Blackfin: don't use __builtin_bfin_[cs]sync
3335 if ($line =~ /__builtin_bfin_csync/) {
3336 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003337 ERROR("CSYNC",
3338 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003339 }
3340 if ($line =~ /__builtin_bfin_ssync/) {
3341 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003342 ERROR("SSYNC",
3343 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003344 }
3345
Joe Perches56e77d72013-02-21 16:44:14 -08003346# check for old HOTPLUG __dev<foo> section markings
3347 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3348 WARN("HOTPLUG_SECTION",
3349 "Using $1 is unnecessary\n" . $herecurr);
3350 }
3351
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003352# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003353 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3354 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003355#print "LINE<$line>\n";
3356 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003357 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003358 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003359 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003360 $stat =~ s/\n./\n /g;
3361 $cond =~ s/\n./\n /g;
3362
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003363#print "linenr<$linenr> <$stat>\n";
3364 # If this statement has no statement boundaries within
3365 # it there is no point in retrying a statement scan
3366 # until we hit end of it.
3367 my $frag = $stat; $frag =~ s/;+\s*$//;
3368 if ($frag !~ /(?:{|;)/) {
3369#print "skip<$line_nr_next>\n";
3370 $suppress_statement = $line_nr_next;
3371 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003372
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003373 # Find the real next line.
3374 $realline_next = $line_nr_next;
3375 if (defined $realline_next &&
3376 (!defined $lines[$realline_next - 1] ||
3377 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3378 $realline_next++;
3379 }
3380
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003381 my $s = $stat;
3382 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003383
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003384 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003385 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003386
3387 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003388 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003389
Andy Whitcroft463f2862009-09-21 17:04:34 -07003390 } elsif ($s =~ /^.\s*else\b/s) {
3391
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003392 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003393 } 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) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003394 my $type = $1;
3395 $type =~ s/\s+/ /g;
3396 possible($type, "A:" . $s);
3397
Andy Whitcroft8905a672007-11-28 16:21:06 -08003398 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003399 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003400 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003401 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402
3403 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003404 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003405 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003406 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003407
3408 # Check for any sort of function declaration.
3409 # int foo(something bar, other baz);
3410 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003411 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003412 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003413
Andy Whitcroftcf655042008-03-04 14:28:20 -08003414 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003415 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003416 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003417
Andy Whitcroft8905a672007-11-28 16:21:06 -08003418 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003419 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003420
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003421 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003422 }
3423 }
3424 }
3425
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003426 }
3427
Andy Whitcroft653d4872007-06-23 17:16:34 -07003428#
3429# Checks which may be anchored in the context.
3430#
3431
3432# Check for switch () and associated case and default
3433# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003434 if ($line=~/\bswitch\s*\(.*\)/) {
3435 my $err = '';
3436 my $sep = '';
3437 my @ctx = ctx_block_outer($linenr, $realcnt);
3438 shift(@ctx);
3439 for my $ctx (@ctx) {
3440 my ($clen, $cindent) = line_stats($ctx);
3441 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3442 $indent != $cindent) {
3443 $err .= "$sep$ctx\n";
3444 $sep = '';
3445 } else {
3446 $sep = "[...]\n";
3447 }
3448 }
3449 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003450 ERROR("SWITCH_CASE_INDENT_LEVEL",
3451 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003452 }
3453 }
3454
3455# if/while/etc brace do not go on next line, unless defining a do while loop,
3456# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003457 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003458 my $pre_ctx = "$1$2";
3459
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003460 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003461
3462 if ($line =~ /^\+\t{6,}/) {
3463 WARN("DEEP_INDENTATION",
3464 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3465 }
3466
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003467 my $ctx_cnt = $realcnt - $#ctx - 1;
3468 my $ctx = join("\n", @ctx);
3469
Andy Whitcroft548596d2008-07-23 21:29:01 -07003470 my $ctx_ln = $linenr;
3471 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003472
Andy Whitcroft548596d2008-07-23 21:29:01 -07003473 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3474 defined $lines[$ctx_ln - 1] &&
3475 $lines[$ctx_ln - 1] =~ /^-/)) {
3476 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3477 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003478 $ctx_ln++;
3479 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003480
Andy Whitcroft53210162008-07-23 21:29:03 -07003481 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3482 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003483
Joe Perchesd752fcc2014-08-06 16:11:05 -07003484 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003485 ERROR("OPEN_BRACE",
3486 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003487 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003488 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003489 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3490 $ctx =~ /\)\s*\;\s*$/ &&
3491 defined $lines[$ctx_ln - 1])
3492 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003493 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3494 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003495 WARN("TRAILING_SEMICOLON",
3496 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003497 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003498 }
3499 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003500 }
3501
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003502# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003503 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003504 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3505 ctx_statement_block($linenr, $realcnt, 0)
3506 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003507 my ($s, $c) = ($stat, $cond);
3508
3509 substr($s, 0, length($c), '');
3510
Joe Perches9f5af482015-09-09 15:37:30 -07003511 # remove inline comments
3512 $s =~ s/$;/ /g;
3513 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003514
3515 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003516 my @newlines = ($c =~ /\n/gs);
3517 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003518
Joe Perches9f5af482015-09-09 15:37:30 -07003519 # Make sure we remove the line prefixes as we have
3520 # none on the first line, and are going to readd them
3521 # where necessary.
3522 $s =~ s/\n./\n/gs;
3523 while ($s =~ /\n\s+\\\n/) {
3524 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3525 }
3526
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003527 # We want to check the first line inside the block
3528 # starting at the end of the conditional, so remove:
3529 # 1) any blank line termination
3530 # 2) any opening brace { on end of the line
3531 # 3) any do (...) {
3532 my $continuation = 0;
3533 my $check = 0;
3534 $s =~ s/^.*\bdo\b//;
3535 $s =~ s/^\s*{//;
3536 if ($s =~ s/^\s*\\//) {
3537 $continuation = 1;
3538 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003539 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003540 $check = 1;
3541 $cond_lines++;
3542 }
3543
3544 # Also ignore a loop construct at the end of a
3545 # preprocessor statement.
3546 if (($prevline =~ /^.\s*#\s*define\s/ ||
3547 $prevline =~ /\\\s*$/) && $continuation == 0) {
3548 $check = 0;
3549 }
3550
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003551 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003552 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003553 while ($cond_ptr != $cond_lines) {
3554 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003555
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003556 # If we see an #else/#elif then the code
3557 # is not linear.
3558 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3559 $check = 0;
3560 }
3561
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003562 # Ignore:
3563 # 1) blank lines, they should be at 0,
3564 # 2) preprocessor lines, and
3565 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003566 if ($continuation ||
3567 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003568 $s =~ /^\s*#\s*?/ ||
3569 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003570 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003571 if ($s =~ s/^.*?\n//) {
3572 $cond_lines++;
3573 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003574 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003575 }
3576
3577 my (undef, $sindent) = line_stats("+" . $s);
3578 my $stat_real = raw_line($linenr, $cond_lines);
3579
3580 # Check if either of these lines are modified, else
3581 # this is not this patch's fault.
3582 if (!defined($stat_real) ||
3583 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3584 $check = 0;
3585 }
3586 if (defined($stat_real) && $cond_lines > 1) {
3587 $stat_real = "[...]\n$stat_real";
3588 }
3589
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003590 #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";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003591
Joe Perches9f5af482015-09-09 15:37:30 -07003592 if ($check && $s ne '' &&
3593 (($sindent % 8) != 0 ||
3594 ($sindent < $indent) ||
3595 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003596 WARN("SUSPECT_CODE_INDENT",
3597 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003598 }
3599 }
3600
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003601 # Track the 'values' across context and added lines.
3602 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003603 my ($curr_values, $curr_vars) =
3604 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003605 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003606 if ($dbg_values) {
3607 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003608 print "$linenr > .$outline\n";
3609 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003610 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003611 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003612 $prev_values = substr($curr_values, -1);
3613
Andy Whitcroft00df3442007-06-08 13:47:06 -07003614#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003615 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003616
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003617# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003618 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003619 my $type = $1;
3620 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003621 $var = "" if (!defined $var);
3622 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003623 my $sign = $1;
3624 my $pointer = $2;
3625
3626 $pointer = "" if (!defined $pointer);
3627
3628 if (WARN("UNSPECIFIED_INT",
3629 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3630 $fix) {
3631 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003632 my $comp_pointer = $pointer;
3633 $comp_pointer =~ s/\s//g;
3634 $decl .= $comp_pointer;
3635 $decl = rtrim($decl) if ($var eq "");
3636 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003637 }
3638 }
3639 }
3640
Andy Whitcroft653d4872007-06-23 17:16:34 -07003641# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003642 if ($dbg_type) {
3643 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003644 ERROR("TEST_TYPE",
3645 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003646 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003647 ERROR("TEST_NOT_TYPE",
3648 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003649 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003650 next;
3651 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003652# TEST: allow direct testing of the attribute matcher.
3653 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003654 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003655 ERROR("TEST_ATTR",
3656 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003657 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003658 ERROR("TEST_NOT_ATTR",
3659 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003660 }
3661 next;
3662 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003663
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003664# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003665 if ($line =~ /^.\s*{/ &&
3666 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003667 if (ERROR("OPEN_BRACE",
3668 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003669 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3670 fix_delete_line($fixlinenr - 1, $prevrawline);
3671 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003672 my $fixedline = $prevrawline;
3673 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003674 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003675 $fixedline = $line;
Cyril Bur93eae952017-07-10 15:52:21 -07003676 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003677 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003678 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003679 }
3680
Andy Whitcroft653d4872007-06-23 17:16:34 -07003681#
3682# Checks which are anchored on the added line.
3683#
3684
3685# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003686 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003687 my $path = $1;
3688 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003689 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003690 "malformed #include filename\n" . $herecurr);
3691 }
3692 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3693 ERROR("UAPI_INCLUDE",
3694 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003695 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003696 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003697
3698# no C99 // comments
3699 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003700 if (ERROR("C99_COMMENTS",
3701 "do not use C99 // comments\n" . $herecurr) &&
3702 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003703 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003704 if ($line =~ /\/\/(.*)$/) {
3705 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003706 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003707 }
3708 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003709 }
3710 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003711 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003712 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003713
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003714# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3715# the whole statement.
3716#print "APW <$lines[$realline_next - 1]>\n";
3717 if (defined $realline_next &&
3718 exists $lines[$realline_next - 1] &&
3719 !defined $suppress_export{$realline_next} &&
3720 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3721 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003722 # Handle definitions which produce identifiers with
3723 # a prefix:
3724 # XXX(foo);
3725 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003726 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003727 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003728 $name =~ /^${Ident}_$2/) {
3729#print "FOO C name<$name>\n";
3730 $suppress_export{$realline_next} = 1;
3731
3732 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003733 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003734 ^.DEFINE_$Ident\(\Q$name\E\)|
3735 ^.DECLARE_$Ident\(\Q$name\E\)|
3736 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003737 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3738 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003739 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003740#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3741 $suppress_export{$realline_next} = 2;
3742 } else {
3743 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003744 }
3745 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003746 if (!defined $suppress_export{$linenr} &&
3747 $prevline =~ /^.\s*$/ &&
3748 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3749 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3750#print "FOO B <$lines[$linenr - 1]>\n";
3751 $suppress_export{$linenr} = 2;
3752 }
3753 if (defined $suppress_export{$linenr} &&
3754 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003755 WARN("EXPORT_SYMBOL",
3756 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003757 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003758
Joe Eloff5150bda2010-08-09 17:21:00 -07003759# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003760 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003761 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003762 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003763 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003764 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003765 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003766 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003767# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003768 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003769 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003770 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003771 $herecurr) &&
3772 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003773 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003774 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003775 }
3776
Joe Perches18130872014-08-06 16:11:22 -07003777# check for misordered declarations of char/short/int/long with signed/unsigned
3778 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3779 my $tmp = trim($1);
3780 WARN("MISORDERED_TYPE",
3781 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3782 }
3783
Joe Perchescb710ec2010-10-26 14:23:20 -07003784# check for static const char * arrays.
3785 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003786 WARN("STATIC_CONST_CHAR_ARRAY",
3787 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003788 $herecurr);
3789 }
3790
3791# check for static char foo[] = "bar" declarations.
3792 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003793 WARN("STATIC_CONST_CHAR_ARRAY",
3794 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003795 $herecurr);
3796 }
3797
Joe Perchesab7e23f2015-04-16 12:44:22 -07003798# check for const <foo> const where <foo> is not a pointer or array type
3799 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3800 my $found = $1;
3801 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3802 WARN("CONST_CONST",
3803 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3804 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3805 WARN("CONST_CONST",
3806 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3807 }
3808 }
3809
Joe Perches9b0fa602014-04-03 14:49:18 -07003810# check for non-global char *foo[] = {"bar", ...} declarations.
3811 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3812 WARN("STATIC_CONST_CHAR_ARRAY",
3813 "char * array declaration might be better as static const\n" .
3814 $herecurr);
3815 }
3816
Joe Perchesb598b672015-04-16 12:44:36 -07003817# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3818 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3819 my $array = $1;
3820 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3821 my $array_div = $1;
3822 if (WARN("ARRAY_SIZE",
3823 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3824 $fix) {
3825 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3826 }
3827 }
3828 }
3829
Joe Perchesb36190c2014-01-27 17:07:18 -08003830# check for function declarations without arguments like "int foo()"
3831 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3832 if (ERROR("FUNCTION_WITHOUT_ARGS",
3833 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003835 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003836 }
3837 }
3838
Andy Whitcroft653d4872007-06-23 17:16:34 -07003839# check for new typedefs, only function parameters and sparse annotations
3840# make sense.
3841 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003842 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003843 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003844 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003845 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003846 WARN("NEW_TYPEDEFS",
3847 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003848 }
3849
3850# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003851 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003852 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3853 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003854 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003855
Andy Whitcroft65863862009-01-06 14:41:21 -08003856 # Should start with a space.
3857 $to =~ s/^(\S)/ $1/;
3858 # Should not end with a space.
3859 $to =~ s/\s+$//;
3860 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003861 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003862 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003863
Joe Perches3705ce52013-07-03 15:05:31 -07003864## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003865 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003866 if (ERROR("POINTER_LOCATION",
3867 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3868 $fix) {
3869 my $sub_from = $ident;
3870 my $sub_to = $ident;
3871 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003872 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003873 s@\Q$sub_from\E@$sub_to@;
3874 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003875 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003876 }
3877 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3878 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003879 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003880
Andy Whitcroft65863862009-01-06 14:41:21 -08003881 # Should start with a space.
3882 $to =~ s/^(\S)/ $1/;
3883 # Should not end with a space.
3884 $to =~ s/\s+$//;
3885 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003886 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003887 }
3888 # Modifiers should have spaces.
3889 $to =~ s/(\b$Modifier$)/$1 /;
3890
Joe Perches3705ce52013-07-03 15:05:31 -07003891## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003892 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003893 if (ERROR("POINTER_LOCATION",
3894 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3895 $fix) {
3896
3897 my $sub_from = $match;
3898 my $sub_to = $match;
3899 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003900 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003901 s@\Q$sub_from\E@$sub_to@;
3902 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003903 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003904 }
3905
Joe Perches9d3e3c72015-09-09 15:37:27 -07003906# avoid BUG() or BUG_ON()
3907 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3908 my $msg_type = \&WARN;
3909 $msg_type = \&CHK if ($file);
3910 &{$msg_type}("AVOID_BUG",
3911 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3912 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003913
Joe Perches9d3e3c72015-09-09 15:37:27 -07003914# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003915 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003916 WARN("LINUX_VERSION_CODE",
3917 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003918 }
3919
Joe Perches17441222011-06-15 15:08:17 -07003920# check for uses of printk_ratelimit
3921 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003922 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003923 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003924 }
3925
Andy Whitcroft00df3442007-06-08 13:47:06 -07003926# printk should use KERN_* levels. Note that follow on printk's on the
3927# same line do not need a level, so we use the current block context
3928# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003929# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003930# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003931 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003932 my $ok = 0;
3933 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3934 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003935 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003936 # with "\n" ignore it, else it is to blame
3937 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3938 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3939 $ok = 1;
3940 }
3941 last;
3942 }
3943 }
3944 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003945 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3946 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003947 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003948 }
3949
Joe Perches243f3802012-05-31 16:26:09 -07003950 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3951 my $orig = $1;
3952 my $level = lc($orig);
3953 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003954 my $level2 = $level;
3955 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003956 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003957 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003958 }
3959
3960 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003961 if (WARN("PREFER_PR_LEVEL",
3962 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003964 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003965 s/\bpr_warning\b/pr_warn/;
3966 }
Joe Perches243f3802012-05-31 16:26:09 -07003967 }
3968
Joe Perchesdc139312013-02-21 16:44:13 -08003969 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3970 my $orig = $1;
3971 my $level = lc($orig);
3972 $level = "warn" if ($level eq "warning");
3973 $level = "dbg" if ($level eq "debug");
3974 WARN("PREFER_DEV_LEVEL",
3975 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3976 }
3977
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003978# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3979# number of false positives, but assembly files are not checked, so at
3980# least the arch entry code will not trigger this warning.
3981 if ($line =~ /\bENOSYS\b/) {
3982 WARN("ENOSYS",
3983 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3984 }
3985
Andy Whitcroft653d4872007-06-23 17:16:34 -07003986# function brace can't be on same line, except for #defines of do while,
3987# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003988 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003989 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003990 if (ERROR("OPEN_BRACE",
3991 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3992 $fix) {
3993 fix_delete_line($fixlinenr, $rawline);
3994 my $fixed_line = $rawline;
Dwaipayan Ray1ccbd402020-12-15 20:45:02 -08003995 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
Joe Perches8d182472014-08-06 16:11:12 -07003996 my $line1 = $1;
3997 my $line2 = $2;
3998 fix_insert_line($fixlinenr, ltrim($line1));
3999 fix_insert_line($fixlinenr, "\+{");
4000 if ($line2 !~ /^\s*$/) {
4001 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4002 }
4003 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004004 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004005
Andy Whitcroft8905a672007-11-28 16:21:06 -08004006# open braces for enum, union and struct go on the same line.
4007 if ($line =~ /^.\s*{/ &&
4008 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004009 if (ERROR("OPEN_BRACE",
4010 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4011 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4012 fix_delete_line($fixlinenr - 1, $prevrawline);
4013 fix_delete_line($fixlinenr, $rawline);
4014 my $fixedline = rtrim($prevrawline) . " {";
4015 fix_insert_line($fixlinenr, $fixedline);
4016 $fixedline = $rawline;
Cyril Bur93eae952017-07-10 15:52:21 -07004017 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004018 if ($fixedline !~ /^\+\s*$/) {
4019 fix_insert_line($fixlinenr, $fixedline);
4020 }
4021 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004022 }
4023
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004024# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004025 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4026 if (WARN("SPACING",
4027 "missing space after $1 definition\n" . $herecurr) &&
4028 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004029 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004030 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4031 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004032 }
4033
Joe Perches31070b52014-01-23 15:54:49 -08004034# Function pointer declarations
4035# check spacing between type, funcptr, and args
4036# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004037 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004038 my $declare = $1;
4039 my $pre_pointer_space = $2;
4040 my $post_pointer_space = $3;
4041 my $funcname = $4;
4042 my $post_funcname_space = $5;
4043 my $pre_args_space = $6;
4044
Joe Perches91f72e92014-04-03 14:49:12 -07004045# the $Declare variable will capture all spaces after the type
4046# so check it for a missing trailing missing space but pointer return types
4047# don't need a space so don't warn for those.
4048 my $post_declare_space = "";
4049 if ($declare =~ /(\s+)$/) {
4050 $post_declare_space = $1;
4051 $declare = rtrim($declare);
4052 }
4053 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004054 WARN("SPACING",
4055 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004056 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004057 }
4058
4059# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004060# This test is not currently implemented because these declarations are
4061# equivalent to
4062# int foo(int bar, ...)
4063# and this is form shouldn't/doesn't generate a checkpatch warning.
4064#
4065# elsif ($declare =~ /\s{2,}$/) {
4066# WARN("SPACING",
4067# "Multiple spaces after return type\n" . $herecurr);
4068# }
Joe Perches31070b52014-01-23 15:54:49 -08004069
4070# unnecessary space "type ( *funcptr)(args...)"
4071 if (defined $pre_pointer_space &&
4072 $pre_pointer_space =~ /^\s/) {
4073 WARN("SPACING",
4074 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4075 }
4076
4077# unnecessary space "type (* funcptr)(args...)"
4078 if (defined $post_pointer_space &&
4079 $post_pointer_space =~ /^\s/) {
4080 WARN("SPACING",
4081 "Unnecessary space before function pointer name\n" . $herecurr);
4082 }
4083
4084# unnecessary space "type (*funcptr )(args...)"
4085 if (defined $post_funcname_space &&
4086 $post_funcname_space =~ /^\s/) {
4087 WARN("SPACING",
4088 "Unnecessary space after function pointer name\n" . $herecurr);
4089 }
4090
4091# unnecessary space "type (*funcptr) (args...)"
4092 if (defined $pre_args_space &&
4093 $pre_args_space =~ /^\s/) {
4094 WARN("SPACING",
4095 "Unnecessary space before function pointer arguments\n" . $herecurr);
4096 }
4097
4098 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004099 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004100 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004101 }
4102 }
4103
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004104# check for spacing round square brackets; allowed:
4105# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004106# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4107# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004108 while ($line =~ /(.*?\s)\[/g) {
4109 my ($where, $prefix) = ($-[1], $1);
4110 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004111 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004112 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004113 if (ERROR("BRACKET_SPACE",
4114 "space prohibited before open square bracket '['\n" . $herecurr) &&
4115 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004116 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004117 s/^(\+.*?)\s+\[/$1\[/;
4118 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004119 }
4120 }
4121
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004122# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004123 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004124 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004125 my $ctx_before = substr($line, 0, $-[1]);
4126 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004127
4128 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004129 if ($name =~ /^(?:
4130 if|for|while|switch|return|case|
4131 volatile|__volatile__|
4132 __attribute__|format|__extension__|
4133 asm|__asm__)$/x)
4134 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004135 # cpp #define statements have non-optional spaces, ie
4136 # if there is a space between the name and the open
4137 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004138 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004139
4140 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004141 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004142
4143 # If this whole things ends with a type its most
4144 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004145 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004146
4147 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004148 if (WARN("SPACING",
4149 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4150 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004151 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004152 s/\b$name\s+\(/$name\(/;
4153 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004154 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004155 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004156
Andy Whitcroft653d4872007-06-23 17:16:34 -07004157# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004158 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004159 my $fixed_line = "";
4160 my $line_fixed = 0;
4161
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004162 my $ops = qr{
4163 <<=|>>=|<=|>=|==|!=|
4164 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4165 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004166 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004167 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004168 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004169 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004170
4171## print("element count: <" . $#elements . ">\n");
4172## foreach my $el (@elements) {
4173## print("el: <$el>\n");
4174## }
4175
4176 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004177 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004178
Joe Perches3705ce52013-07-03 15:05:31 -07004179 foreach my $el (@elements) {
4180 push(@fix_elements, substr($rawline, $off, length($el)));
4181 $off += length($el);
4182 }
4183
4184 $off = 0;
4185
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004186 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004187 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004188
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004189 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004190
4191 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4192
4193## print("n: <$n> good: <$good>\n");
4194
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004195 $off += length($elements[$n]);
4196
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004197 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004198 my $ca = substr($opline, 0, $off);
4199 my $cc = '';
4200 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4201 $cc = substr($opline, $off + length($elements[$n + 1]));
4202 }
4203 my $cb = "$ca$;$cc";
4204
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004205 my $a = '';
4206 $a = 'V' if ($elements[$n] ne '');
4207 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004208 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004209 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4210 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004211 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004212
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004213 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004214
4215 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004216 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004217 $c = 'V' if ($elements[$n + 2] ne '');
4218 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004219 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004220 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4221 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004222 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004223 } else {
4224 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004225 }
4226
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004227 my $ctx = "${a}x${c}";
4228
4229 my $at = "(ctx:$ctx)";
4230
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004231 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004232 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004233
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004234 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004235 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004236
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004237 # Get the full operator variant.
4238 my $opv = $op . substr($curr_vars, $off, 1);
4239
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004240 # Ignore operators passed as parameters.
4241 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004242 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004243
Andy Whitcroftcf655042008-03-04 14:28:20 -08004244# # Ignore comments
4245# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004246
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004247 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004248 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004249 if ($ctx !~ /.x[WEBC]/ &&
4250 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004251 if (ERROR("SPACING",
4252 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004253 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004254 $line_fixed = 1;
4255 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004256 }
4257
4258 # // is a comment
4259 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004260
Joe Perchesb00e4812014-04-03 14:49:33 -07004261 # : when part of a bitfield
4262 } elsif ($opv eq ':B') {
4263 # skip the bitfield test for now
4264
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004265 # No spaces for:
4266 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004267 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004268 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004269 if (ERROR("SPACING",
4270 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004271 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004272 if (defined $fix_elements[$n + 2]) {
4273 $fix_elements[$n + 2] =~ s/^\s+//;
4274 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004275 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004276 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004277 }
4278
Joe Perches23810972014-12-10 15:51:32 -08004279 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004280 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004281 my $rtrim_before = 0;
4282 my $space_after = 0;
4283 if ($ctx =~ /Wx./) {
4284 if (ERROR("SPACING",
4285 "space prohibited before that '$op' $at\n" . $hereptr)) {
4286 $line_fixed = 1;
4287 $rtrim_before = 1;
4288 }
4289 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004290 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004291 if (ERROR("SPACING",
4292 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004293 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004294 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004295 $space_after = 1;
4296 }
4297 }
4298 if ($rtrim_before || $space_after) {
4299 if ($rtrim_before) {
4300 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4301 } else {
4302 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4303 }
4304 if ($space_after) {
4305 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004306 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004307 }
4308
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004309 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004310 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004311 #warn "'*' is part of type\n";
4312
4313 # unary operators should have a space before and
4314 # none after. May be left adjacent to another
4315 # unary operator, or a cast
4316 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004317 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004318 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004319 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004320 if (ERROR("SPACING",
4321 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004322 if ($n != $last_after + 2) {
4323 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4324 $line_fixed = 1;
4325 }
Joe Perches3705ce52013-07-03 15:05:31 -07004326 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004327 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004328 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004329 # A unary '*' may be const
4330
4331 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004332 if (ERROR("SPACING",
4333 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004334 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004335 if (defined $fix_elements[$n + 2]) {
4336 $fix_elements[$n + 2] =~ s/^\s+//;
4337 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004338 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004339 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004340 }
4341
4342 # unary ++ and unary -- are allowed no space on one side.
4343 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004344 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004345 if (ERROR("SPACING",
4346 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004347 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004348 $line_fixed = 1;
4349 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004350 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004351 if ($ctx =~ /Wx[BE]/ ||
4352 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004353 if (ERROR("SPACING",
4354 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004355 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004356 $line_fixed = 1;
4357 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004358 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004359 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004360 if (ERROR("SPACING",
4361 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004362 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004363 if (defined $fix_elements[$n + 2]) {
4364 $fix_elements[$n + 2] =~ s/^\s+//;
4365 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004366 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004367 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004368 }
4369
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004370 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004371 } elsif ($op eq '<<' or $op eq '>>' or
4372 $op eq '&' or $op eq '^' or $op eq '|' or
4373 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004374 $op eq '*' or $op eq '/' or
4375 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004376 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004377 if ($check) {
4378 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4379 if (CHK("SPACING",
4380 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4381 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4382 $fix_elements[$n + 2] =~ s/^\s+//;
4383 $line_fixed = 1;
4384 }
4385 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4386 if (CHK("SPACING",
4387 "space preferred before that '$op' $at\n" . $hereptr)) {
4388 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4389 $line_fixed = 1;
4390 }
4391 }
4392 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004393 if (ERROR("SPACING",
4394 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004395 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4396 if (defined $fix_elements[$n + 2]) {
4397 $fix_elements[$n + 2] =~ s/^\s+//;
4398 }
Joe Perches3705ce52013-07-03 15:05:31 -07004399 $line_fixed = 1;
4400 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004401 }
4402
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004403 # A colon needs no spaces before when it is
4404 # terminating a case value or a label.
4405 } elsif ($opv eq ':C' || $opv eq ':L') {
4406 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004407 if (ERROR("SPACING",
4408 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004409 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004410 $line_fixed = 1;
4411 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004412 }
4413
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004414 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004415 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004416 my $ok = 0;
4417
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004418 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004419 if (($op eq '<' &&
4420 $cc =~ /^\S+\@\S+>/) ||
4421 ($op eq '>' &&
4422 $ca =~ /<\S+\@\S+$/))
4423 {
4424 $ok = 1;
4425 }
4426
Joe Perchese0df7e12015-04-16 12:44:53 -07004427 # for asm volatile statements
4428 # ignore a colon with another
4429 # colon immediately before or after
4430 if (($op eq ':') &&
4431 ($ca =~ /:$/ || $cc =~ /^:/)) {
4432 $ok = 1;
4433 }
4434
Joe Perches84731622013-11-12 15:10:05 -08004435 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004436 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004437 my $msg_type = \&ERROR;
4438 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4439
4440 if (&{$msg_type}("SPACING",
4441 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004442 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4443 if (defined $fix_elements[$n + 2]) {
4444 $fix_elements[$n + 2] =~ s/^\s+//;
4445 }
Joe Perches3705ce52013-07-03 15:05:31 -07004446 $line_fixed = 1;
4447 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004448 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004449 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004450 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004451
4452## print("n: <$n> GOOD: <$good>\n");
4453
4454 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004455 }
Joe Perches3705ce52013-07-03 15:05:31 -07004456
4457 if (($#elements % 2) == 0) {
4458 $fixed_line = $fixed_line . $fix_elements[$#elements];
4459 }
4460
Joe Perches194f66f2014-08-06 16:11:03 -07004461 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4462 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004463 }
4464
4465
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004466 }
4467
Joe Perches786b6322013-07-03 15:05:32 -07004468# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004469 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004470 if (WARN("SPACING",
4471 "space prohibited before semicolon\n" . $herecurr) &&
4472 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004473 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004474 s/^(\+.*\S)\s+;/$1;/;
4475 }
4476 }
4477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004478# check for multiple assignments
4479 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004480 CHK("MULTIPLE_ASSIGNMENTS",
4481 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004482 }
4483
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004484## # check for multiple declarations, allowing for a function declaration
4485## # continuation.
4486## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4487## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4488##
4489## # Remove any bracketed sections to ensure we do not
4490## # falsly report the parameters of functions.
4491## my $ln = $line;
4492## while ($ln =~ s/\([^\(\)]*\)//g) {
4493## }
4494## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004495## WARN("MULTIPLE_DECLARATION",
4496## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004497## }
4498## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004499
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004500#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004501 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004502 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004503 if (ERROR("SPACING",
4504 "space required before the open brace '{'\n" . $herecurr) &&
4505 $fix) {
Cyril Bur93eae952017-07-10 15:52:21 -07004506 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004507 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004508 }
4509
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004510## # check for blank lines before declarations
4511## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4512## $prevrawline =~ /^.\s*$/) {
4513## WARN("SPACING",
4514## "No blank lines before declarations\n" . $hereprev);
4515## }
4516##
4517
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004518# closing brace should have a space following it when it has anything
4519# on the line
4520 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004521 if (ERROR("SPACING",
4522 "space required after that close brace '}'\n" . $herecurr) &&
4523 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004524 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004525 s/}((?!(?:,|;|\)))\S)/} $1/;
4526 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004527 }
4528
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004529# check spacing on square brackets
4530 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004531 if (ERROR("SPACING",
4532 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4533 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004534 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004535 s/\[\s+/\[/;
4536 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004537 }
4538 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004539 if (ERROR("SPACING",
4540 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4541 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004542 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004543 s/\s+\]/\]/;
4544 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004545 }
4546
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004547# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004548 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004549 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004550 if (ERROR("SPACING",
4551 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4552 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004553 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004554 s/\(\s+/\(/;
4555 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004556 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004557 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004558 $line !~ /for\s*\(.*;\s+\)/ &&
4559 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004560 if (ERROR("SPACING",
4561 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4562 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004563 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004564 s/\s+\)/\)/;
4565 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004566 }
4567
Joe Perchese2826fd2014-08-06 16:10:48 -07004568# check unnecessary parentheses around addressof/dereference single $Lvals
4569# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4570
4571 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004572 my $var = $1;
4573 if (CHK("UNNECESSARY_PARENTHESES",
4574 "Unnecessary parentheses around $var\n" . $herecurr) &&
4575 $fix) {
4576 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4577 }
4578 }
4579
4580# check for unnecessary parentheses around function pointer uses
4581# ie: (foo->bar)(); should be foo->bar();
4582# but not "if (foo->bar) (" to avoid some false positives
4583 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4584 my $var = $2;
4585 if (CHK("UNNECESSARY_PARENTHESES",
4586 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4587 $fix) {
4588 my $var2 = deparenthesize($var);
4589 $var2 =~ s/\s//g;
4590 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4591 }
4592 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004593
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004594#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004595 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004596 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004597 if (WARN("INDENTED_LABEL",
4598 "labels should not be indented\n" . $herecurr) &&
4599 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004600 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004601 s/^(.)\s+/$1/;
4602 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004603 }
4604
Joe Perches5b9553a2014-04-03 14:49:21 -07004605# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004606 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004607 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004608 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004609 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4610 my $value = $1;
4611 $value = deparenthesize($value);
4612 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4613 ERROR("RETURN_PARENTHESES",
4614 "return is not a function, parentheses are not required\n" . $herecurr);
4615 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004616 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004617 ERROR("SPACING",
4618 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004619 }
4620 }
Joe Perches507e5142013-11-12 15:10:13 -08004621
Joe Perchesb43ae212014-06-23 13:22:07 -07004622# unnecessary return in a void function
4623# at end-of-function, with the previous line a single leading tab, then return;
4624# and the line before that not a goto label target like "out:"
4625 if ($sline =~ /^[ \+]}\s*$/ &&
4626 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4627 $linenr >= 3 &&
4628 $lines[$linenr - 3] =~ /^[ +]/ &&
4629 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004630 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004631 "void function return statements are not generally useful\n" . $hereprev);
4632 }
Joe Perches9819cf22014-06-04 16:12:09 -07004633
Joe Perches189248d2014-01-23 15:54:47 -08004634# if statements using unnecessary parentheses - ie: if ((foo == bar))
4635 if ($^V && $^V ge 5.10.0 &&
4636 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4637 my $openparens = $1;
4638 my $count = $openparens =~ tr@\(@\(@;
4639 my $msg = "";
4640 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4641 my $comp = $4; #Not $1 because of $LvalOrFunc
4642 $msg = " - maybe == should be = ?" if ($comp eq "==");
4643 WARN("UNNECESSARY_PARENTHESES",
4644 "Unnecessary parentheses$msg\n" . $herecurr);
4645 }
4646 }
4647
Joe Perchesc5595fa2015-09-09 15:37:58 -07004648# comparisons with a constant or upper case identifier on the left
4649# avoid cases like "foo + BAR < baz"
4650# only fix matches surrounded by parentheses to avoid incorrect
4651# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4652 if ($^V && $^V ge 5.10.0 &&
4653 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4654 my $lead = $1;
4655 my $const = $2;
4656 my $comp = $3;
4657 my $to = $4;
4658 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004659 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004660 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4661 WARN("CONSTANT_COMPARISON",
4662 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4663 $fix) {
4664 if ($comp eq "<") {
4665 $newcomp = ">";
4666 } elsif ($comp eq "<=") {
4667 $newcomp = ">=";
4668 } elsif ($comp eq ">") {
4669 $newcomp = "<";
4670 } elsif ($comp eq ">=") {
4671 $newcomp = "<=";
4672 }
4673 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4674 }
4675 }
4676
Joe Perchesf34e4a42015-04-16 12:44:19 -07004677# Return of what appears to be an errno should normally be negative
4678 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004679 my $name = $1;
4680 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004681 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004682 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004683 }
4684 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004685
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004686# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004687 if ($line =~ /\b(if|while|for|switch)\(/) {
4688 if (ERROR("SPACING",
4689 "space required before the open parenthesis '('\n" . $herecurr) &&
4690 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004691 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004692 s/\b(if|while|for|switch)\(/$1 \(/;
4693 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004694 }
4695
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004696# Check for illegal assignment in if conditional -- and check for trailing
4697# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004698 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004699 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4700 ctx_statement_block($linenr, $realcnt, 0)
4701 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004702 my ($stat_next) = ctx_statement_block($line_nr_next,
4703 $remain_next, $off_next);
4704 $stat_next =~ s/\n./\n /g;
4705 ##print "stat<$stat> stat_next<$stat_next>\n";
4706
4707 if ($stat_next =~ /^\s*while\b/) {
4708 # If the statement carries leading newlines,
4709 # then count those as offsets.
4710 my ($whitespace) =
4711 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4712 my $offset =
4713 statement_rawlines($whitespace) - 1;
4714
4715 $suppress_whiletrailers{$line_nr_next +
4716 $offset} = 1;
4717 }
4718 }
4719 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004720 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004721 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004722 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004723
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004724 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004725 ERROR("ASSIGN_IN_IF",
4726 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004727 }
4728
4729 # Find out what is on the end of the line after the
4730 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004731 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004732 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004733 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004734 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4735 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004736 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004737 # Find out how long the conditional actually is.
4738 my @newlines = ($c =~ /\n/gs);
4739 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004740 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004741
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004742 $stat_real = raw_line($linenr, $cond_lines)
4743 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004744 if (defined($stat_real) && $cond_lines > 1) {
4745 $stat_real = "[...]\n$stat_real";
4746 }
4747
Joe Perches000d1cc12011-07-25 17:13:25 -07004748 ERROR("TRAILING_STATEMENTS",
4749 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004750 }
4751 }
4752
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004753# Check for bitwise tests written as boolean
4754 if ($line =~ /
4755 (?:
4756 (?:\[|\(|\&\&|\|\|)
4757 \s*0[xX][0-9]+\s*
4758 (?:\&\&|\|\|)
4759 |
4760 (?:\&\&|\|\|)
4761 \s*0[xX][0-9]+\s*
4762 (?:\&\&|\|\||\)|\])
4763 )/x)
4764 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004765 WARN("HEXADECIMAL_BOOLEAN_TEST",
4766 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004767 }
4768
Andy Whitcroft8905a672007-11-28 16:21:06 -08004769# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004770 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4771 my $s = $1;
4772 $s =~ s/$;//g; # Remove any comments
4773 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004774 ERROR("TRAILING_STATEMENTS",
4775 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004776 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004777 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004778# if should not continue a brace
4779 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004780 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004781 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004782 $herecurr);
4783 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004784# case and default should not have general statements after them
4785 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4786 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004787 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004788 \s*return\s+
4789 )/xg)
4790 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004791 ERROR("TRAILING_STATEMENTS",
4792 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004793 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004794
4795 # Check for }<nl>else {, these must be at the same
4796 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004797 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4798 $previndent == $indent) {
4799 if (ERROR("ELSE_AFTER_BRACE",
4800 "else should follow close brace '}'\n" . $hereprev) &&
4801 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4802 fix_delete_line($fixlinenr - 1, $prevrawline);
4803 fix_delete_line($fixlinenr, $rawline);
4804 my $fixedline = $prevrawline;
4805 $fixedline =~ s/}\s*$//;
4806 if ($fixedline !~ /^\+\s*$/) {
4807 fix_insert_line($fixlinenr, $fixedline);
4808 }
4809 $fixedline = $rawline;
4810 $fixedline =~ s/^(.\s*)else/$1} else/;
4811 fix_insert_line($fixlinenr, $fixedline);
4812 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004813 }
4814
Joe Perches8b8856f2014-08-06 16:11:14 -07004815 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4816 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004817 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4818
4819 # Find out what is on the end of the line after the
4820 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004821 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004822 $s =~ s/\n.*//g;
4823
4824 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004825 if (ERROR("WHILE_AFTER_BRACE",
4826 "while should follow close brace '}'\n" . $hereprev) &&
4827 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4828 fix_delete_line($fixlinenr - 1, $prevrawline);
4829 fix_delete_line($fixlinenr, $rawline);
4830 my $fixedline = $prevrawline;
4831 my $trailing = $rawline;
4832 $trailing =~ s/^\+//;
4833 $trailing = trim($trailing);
4834 $fixedline =~ s/}\s*$/} $trailing/;
4835 fix_insert_line($fixlinenr, $fixedline);
4836 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004837 }
4838 }
4839
Joe Perches95e2c602013-07-03 15:05:20 -07004840#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004841 while ($line =~ m{($Constant|$Lval)}g) {
4842 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004843
4844#gcc binary extension
4845 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004846 if (WARN("GCC_BINARY_CONSTANT",
4847 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4848 $fix) {
4849 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004850 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004851 s/\b$var\b/$hexval/;
4852 }
Joe Perches95e2c602013-07-03 15:05:20 -07004853 }
4854
4855#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004856 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004857 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004858#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004859 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004860#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004861 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4862#Ignore some three character SI units explicitly, like MiB and KHz
4863 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004864 while ($var =~ m{($Ident)}g) {
4865 my $word = $1;
4866 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004867 if ($check) {
4868 seed_camelcase_includes();
4869 if (!$file && !$camelcase_file_seeded) {
4870 seed_camelcase_file($realfile);
4871 $camelcase_file_seeded = 1;
4872 }
4873 }
Joe Perches7e781f62013-09-11 14:23:55 -07004874 if (!defined $camelcase{$word}) {
4875 $camelcase{$word} = 1;
4876 CHK("CAMELCASE",
4877 "Avoid CamelCase: <$word>\n" . $herecurr);
4878 }
Joe Perches34456862013-07-03 15:05:34 -07004879 }
Joe Perches323c1262012-12-17 16:02:07 -08004880 }
4881 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004882
4883#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004884 if ($line =~ /\#\s*define.*\\\s+$/) {
4885 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4886 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004888 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004889 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004890 }
4891
Fabian Frederick0e212e02015-04-16 12:44:25 -07004892# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4893# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004894 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004895 my $file = "$1.h";
4896 my $checkfile = "include/linux/$file";
4897 if (-f "$root/$checkfile" &&
4898 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004899 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004900 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004901 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4902 if ($asminclude > 0) {
4903 if ($realfile =~ m{^arch/}) {
4904 CHK("ARCH_INCLUDE_LINUX",
4905 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4906 } else {
4907 WARN("INCLUDE_LINUX",
4908 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4909 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004910 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004911 }
4912 }
4913
Andy Whitcroft653d4872007-06-23 17:16:34 -07004914# multi-statement macros should be enclosed in a do while loop, grab the
4915# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004916# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004917 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4918 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004919 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004920 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004921 my ($off, $dstat, $dcond, $rest);
4922 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004923 my $has_flow_statement = 0;
4924 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004925 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004926 ctx_statement_block($linenr, $realcnt, 0);
4927 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004928 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004929 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004930
Joe Perches08a28432014-10-13 15:51:55 -07004931 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004932 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004933
Joe Perchesf59b64b2016-10-11 13:52:08 -07004934 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4935 my $define_args = $1;
4936 my $define_stmt = $dstat;
4937 my @def_args = ();
4938
4939 if (defined $define_args && $define_args ne "") {
4940 $define_args = substr($define_args, 1, length($define_args) - 2);
4941 $define_args =~ s/\s*//g;
4942 @def_args = split(",", $define_args);
4943 }
4944
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004945 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004946 $dstat =~ s/\\\n.//g;
4947 $dstat =~ s/^\s*//s;
4948 $dstat =~ s/\s*$//s;
4949
4950 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004951 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4952 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004953 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004954 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004955 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004956
Gregory Bean02870be2011-05-11 09:11:12 -07004957 # Extremely long macros may fall off the end of the
4958 # available context without closing. Give a dangling
4959 # backslash the benefit of the doubt and allow it
4960 # to gobble any hanging open-parens.
4961 $dstat =~ s/\(.+\\$/1/;
4962
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004963 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004964 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4965 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004966 {
4967 }
4968
Joe Perches42e15292016-03-15 14:58:01 -07004969 # Make asm volatile uses seem like a generic function
4970 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4971
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004972 my $exceptions = qr{
4973 $Declare|
4974 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004975 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004976 DECLARE_PER_CPU|
4977 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004978 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004979 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004980 union|
4981 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004982 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004983 ^\"|\"$|
4984 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004985 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004986 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004987
4988 $ctx =~ s/\n*$//;
4989 my $herectx = $here . "\n";
4990 my $stmt_cnt = statement_rawlines($ctx);
4991
4992 for (my $n = 0; $n < $stmt_cnt; $n++) {
4993 $herectx .= raw_line($linenr, $n) . "\n";
4994 }
4995
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004996 if ($dstat ne '' &&
4997 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4998 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004999 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005000 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005001 $dstat !~ /$exceptions/ &&
5002 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005003 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005004 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005005 $dstat !~ /^for\s*$Constant$/ && # for (...)
5006 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5007 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005008 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005009 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005010 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005011
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005012 if ($dstat =~ /;/) {
5013 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5014 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5015 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005016 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005017 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005018 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005019
5020 }
Joe Perches52076492016-10-11 13:52:14 -07005021
5022 # Make $define_stmt single line, comment-free, etc
5023 my @stmt_array = split('\n', $define_stmt);
5024 my $first = 1;
5025 $define_stmt = "";
5026 foreach my $l (@stmt_array) {
5027 $l =~ s/\\$//;
5028 if ($first) {
5029 $define_stmt = $l;
5030 $first = 0;
5031 } elsif ($l =~ /^[\+ ]/) {
5032 $define_stmt .= substr($l, 1);
5033 }
5034 }
5035 $define_stmt =~ s/$;//g;
5036 $define_stmt =~ s/\s+/ /g;
5037 $define_stmt = trim($define_stmt);
5038
Joe Perchesf59b64b2016-10-11 13:52:08 -07005039# check if any macro arguments are reused (ignore '...' and 'type')
5040 foreach my $arg (@def_args) {
5041 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005042 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005043 my $tmp = $define_stmt;
5044 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches52076492016-10-11 13:52:14 -07005045 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005046 $tmp =~ s/\b$arg\s*\#\#//g;
5047 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5048 if ($use_cnt > 1) {
5049 CHK("MACRO_ARG_REUSE",
5050 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005051 }
5052# check if any macro arguments may have other precedence issues
5053 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5054 ((defined($1) && $1 ne ',') ||
5055 (defined($2) && $2 ne ','))) {
5056 CHK("MACRO_ARG_PRECEDENCE",
5057 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005058 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005059 }
Joe Perches5023d342012-12-17 16:01:47 -08005060
Joe Perches08a28432014-10-13 15:51:55 -07005061# check for macros with flow control, but without ## concatenation
5062# ## concatenation is commonly a macro that defines a function so ignore those
5063 if ($has_flow_statement && !$has_arg_concat) {
5064 my $herectx = $here . "\n";
5065 my $cnt = statement_rawlines($ctx);
5066
5067 for (my $n = 0; $n < $cnt; $n++) {
5068 $herectx .= raw_line($linenr, $n) . "\n";
5069 }
5070 WARN("MACRO_WITH_FLOW_CONTROL",
5071 "Macros with flow control statements should be avoided\n" . "$herectx");
5072 }
5073
Joe Perches481eb482012-12-17 16:01:56 -08005074# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005075
5076 } else {
5077 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005078 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5079 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005080 $line =~ /^\+.*\\$/) {
5081 WARN("LINE_CONTINUATIONS",
5082 "Avoid unnecessary line continuations\n" . $herecurr);
5083 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005084 }
5085
Joe Perchesb13edf72012-07-30 14:41:24 -07005086# do {} while (0) macro tests:
5087# single-statement macros do not need to be enclosed in do while (0) loop,
5088# macro should not end with a semicolon
5089 if ($^V && $^V ge 5.10.0 &&
5090 $realfile !~ m@/vmlinux.lds.h$@ &&
5091 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5092 my $ln = $linenr;
5093 my $cnt = $realcnt;
5094 my ($off, $dstat, $dcond, $rest);
5095 my $ctx = '';
5096 ($dstat, $dcond, $ln, $cnt, $off) =
5097 ctx_statement_block($linenr, $realcnt, 0);
5098 $ctx = $dstat;
5099
5100 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005101 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005102
5103 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5104 my $stmts = $2;
5105 my $semis = $3;
5106
5107 $ctx =~ s/\n*$//;
5108 my $cnt = statement_rawlines($ctx);
5109 my $herectx = $here . "\n";
5110
5111 for (my $n = 0; $n < $cnt; $n++) {
5112 $herectx .= raw_line($linenr, $n) . "\n";
5113 }
5114
Joe Perchesac8e97f2012-08-21 16:15:53 -07005115 if (($stmts =~ tr/;/;/) == 1 &&
5116 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005117 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5118 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5119 }
5120 if (defined $semis && $semis ne "") {
5121 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5122 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5123 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005124 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5125 $ctx =~ s/\n*$//;
5126 my $cnt = statement_rawlines($ctx);
5127 my $herectx = $here . "\n";
5128
5129 for (my $n = 0; $n < $cnt; $n++) {
5130 $herectx .= raw_line($linenr, $n) . "\n";
5131 }
5132
5133 WARN("TRAILING_SEMICOLON",
5134 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005135 }
5136 }
5137
Mike Frysinger080ba922009-01-06 14:41:25 -08005138# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5139# all assignments may have only one of the following with an assignment:
5140# .
5141# ALIGN(...)
5142# VMLINUX_SYMBOL(...)
5143 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005144 WARN("MISSING_VMLINUX_SYMBOL",
5145 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005146 }
5147
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005148# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005149 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5150 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005151 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005152 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005153 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5154 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005155 my @allowed = ();
5156 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005157 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005158 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005159 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005160 for my $chunk (@chunks) {
5161 my ($cond, $block) = @{$chunk};
5162
Andy Whitcroft773647a2008-03-28 14:15:58 -07005163 # If the condition carries leading newlines, then count those as offsets.
5164 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5165 my $offset = statement_rawlines($whitespace) - 1;
5166
Joe Perchesaad4f612012-03-23 15:02:19 -07005167 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005168 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5169
5170 # We have looked at and allowed this specific line.
5171 $suppress_ifbraces{$ln + $offset} = 1;
5172
5173 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005174 $ln += statement_rawlines($block) - 1;
5175
Andy Whitcroft773647a2008-03-28 14:15:58 -07005176 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005177
5178 $seen++ if ($block =~ /^\s*{/);
5179
Joe Perchesaad4f612012-03-23 15:02:19 -07005180 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005181 if (statement_lines($cond) > 1) {
5182 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005183 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005184 }
5185 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005186 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005187 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005188 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005189 if (statement_block_size($block) > 1) {
5190 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005191 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005192 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005193 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005194 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005195 if ($seen) {
5196 my $sum_allowed = 0;
5197 foreach (@allowed) {
5198 $sum_allowed += $_;
5199 }
5200 if ($sum_allowed == 0) {
5201 WARN("BRACES",
5202 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5203 } elsif ($sum_allowed != $allow &&
5204 $seen != $allow) {
5205 CHK("BRACES",
5206 "braces {} should be used on all arms of this statement\n" . $herectx);
5207 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005208 }
5209 }
5210 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005211 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005212 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005213 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005214
Andy Whitcroftcf655042008-03-04 14:28:20 -08005215 # Check the pre-context.
5216 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5217 #print "APW: ALLOWED: pre<$1>\n";
5218 $allowed = 1;
5219 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005220
5221 my ($level, $endln, @chunks) =
5222 ctx_statement_full($linenr, $realcnt, $-[0]);
5223
Andy Whitcroftcf655042008-03-04 14:28:20 -08005224 # Check the condition.
5225 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005226 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005227 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005228 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005229 }
5230 if (statement_lines($cond) > 1) {
5231 #print "APW: ALLOWED: cond<$cond>\n";
5232 $allowed = 1;
5233 }
5234 if ($block =~/\b(?:if|for|while)\b/) {
5235 #print "APW: ALLOWED: block<$block>\n";
5236 $allowed = 1;
5237 }
5238 if (statement_block_size($block) > 1) {
5239 #print "APW: ALLOWED: lines block<$block>\n";
5240 $allowed = 1;
5241 }
5242 # Check the post-context.
5243 if (defined $chunks[1]) {
5244 my ($cond, $block) = @{$chunks[1]};
5245 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005246 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005247 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005248 if ($block =~ /^\s*\{/) {
5249 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5250 $allowed = 1;
5251 }
5252 }
5253 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005254 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005255 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005256
Andy Whitcroftf0556632008-10-15 22:02:23 -07005257 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005258 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005259 }
5260
Joe Perches000d1cc12011-07-25 17:13:25 -07005261 WARN("BRACES",
5262 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005263 }
5264 }
5265
Joe Perches0979ae62012-12-17 16:01:59 -08005266# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005267 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005268 if (CHK("BRACES",
5269 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5270 $fix && $prevrawline =~ /^\+/) {
5271 fix_delete_line($fixlinenr - 1, $prevrawline);
5272 }
Joe Perches0979ae62012-12-17 16:01:59 -08005273 }
Joe Perches77b9a532013-07-03 15:05:29 -07005274 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005275 if (CHK("BRACES",
5276 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5277 $fix) {
5278 fix_delete_line($fixlinenr, $rawline);
5279 }
Joe Perches0979ae62012-12-17 16:01:59 -08005280 }
5281
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005282# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005283 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5284 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005285 WARN("VOLATILE",
5286 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005287 }
5288
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005289# Check for user-visible strings broken across lines, which breaks the ability
5290# to grep for the string. Make exceptions when the previous string ends in a
5291# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5292# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005293 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005294 $prevline =~ /"\s*$/ &&
5295 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5296 if (WARN("SPLIT_STRING",
5297 "quoted string split across lines\n" . $hereprev) &&
5298 $fix &&
5299 $prevrawline =~ /^\+.*"\s*$/ &&
5300 $last_coalesced_string_linenr != $linenr - 1) {
5301 my $extracted_string = get_quoted_string($line, $rawline);
5302 my $comma_close = "";
5303 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5304 $comma_close = $1;
5305 }
5306
5307 fix_delete_line($fixlinenr - 1, $prevrawline);
5308 fix_delete_line($fixlinenr, $rawline);
5309 my $fixedline = $prevrawline;
5310 $fixedline =~ s/"\s*$//;
5311 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5312 fix_insert_line($fixlinenr - 1, $fixedline);
5313 $fixedline = $rawline;
5314 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5315 if ($fixedline !~ /\+\s*$/) {
5316 fix_insert_line($fixlinenr, $fixedline);
5317 }
5318 $last_coalesced_string_linenr = $linenr;
5319 }
5320 }
5321
5322# check for missing a space in a string concatenation
5323 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5324 WARN('MISSING_SPACE',
5325 "break quoted strings at a space character\n" . $hereprev);
5326 }
5327
5328# check for spaces before a quoted newline
5329 if ($rawline =~ /^.*\".*\s\\n/) {
5330 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5331 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5332 $fix) {
5333 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5334 }
5335
5336 }
5337
Joe Perchesf17dba42014-10-13 15:51:51 -07005338# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005339 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005340 CHK("CONCATENATED_STRING",
5341 "Concatenated strings should use spaces between elements\n" . $herecurr);
5342 }
5343
Joe Perches90ad30e2014-12-10 15:51:59 -08005344# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005345 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005346 WARN("STRING_FRAGMENTS",
5347 "Consecutive strings are generally better as a single string\n" . $herecurr);
5348 }
5349
Joe Perches6e300752015-09-09 15:37:47 -07005350# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005351 my $string;
5352 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5353 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5354 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005355 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005356 WARN("PRINTF_L",
5357 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5358 last;
5359 }
Joe Perches6e300752015-09-09 15:37:47 -07005360 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5361 ERROR("PRINTF_0xDECIMAL",
5362 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5363 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005364 }
5365
5366# check for line continuations in quoted strings with odd counts of "
5367 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5368 WARN("LINE_CONTINUATIONS",
5369 "Avoid line continuations in quoted strings\n" . $herecurr);
5370 }
5371
Gregory Bean6c9c6842011-03-17 14:18:20 -07005372# sys_open/read/write/close are not allowed in the kernel
5373 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5374 ERROR("FILE_OPS",
5375 "$1 is inappropriate in kernel code.\n" .
5376 $herecurr);
5377 }
5378
Gregory Beana7cce0a2011-06-07 08:06:45 -07005379# filp_open is a backdoor for sys_open
5380 if ($line =~ /\b(filp_open)\b/) {
5381 ERROR("FILE_OPS",
5382 "$1 is inappropriate in kernel code.\n" .
5383 $herecurr);
5384 }
5385
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005386# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5387 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5388 ERROR("NON_RELAXED_IO",
5389 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5390 "with appropriate memory barriers instead.\n" .
5391 $herecurr);
5392 }
5393
5394# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5395 if ($line =~ /\b((in|out)([bwl]))\b/) {
5396 my ($all, $pref, $suf) = ($1, $2, $3);
5397 $pref =~ s/in/read/;
5398 $pref =~ s/out/write/;
5399 ERROR("NON_RELAXED_IO",
5400 "Use of $all is deprecated: use " .
5401 "__raw_$pref$suf\n\t" .
5402 "with appropriate memory barriers instead.\n" .
5403 $herecurr);
5404 }
5405
Gregory Bean438bf5f2011-06-15 09:32:38 -07005406# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005407 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005408 WARN("ARM_BARRIER",
5409 "Use of dsb is discouranged: prefer mb.\n" .
5410 $herecurr);
5411 }
5412
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005413# unbounded string functions are overflow risks
5414 my %str_fns = (
5415 "sprintf" => "snprintf",
5416 "strcpy" => "strlcpy",
5417 "strncpy" => "strlcpy",
5418 "strcat" => "strlcat",
5419 "strncat" => "strlcat",
5420 "vsprintf" => "vsnprintf",
5421 "strchr" => "strnchr",
5422 "strstr" => "strnstr",
5423 );
5424 foreach my $k (keys %str_fns) {
5425 if ($line =~ /\b$k\b/) {
5426 ERROR("UNBOUNDED_STRING_FNS",
5427 "Use of $k is deprecated: " .
5428 "use $str_fns{$k} instead.\n" .
5429 $herecurr);
5430 }
5431 }
5432
Andy Whitcroft00df3442007-06-08 13:47:06 -07005433# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005434 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005435 WARN("IF_0",
5436 "if this code is redundant consider removing it\n"
5437 . $herecurr);
5438 }
5439
5440# warn about #if 1
5441 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5442 WARN("IF_1",
5443 "if this code is required consider removing"
5444 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005445 }
5446
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005447# check for needless "if (<foo>) fn(<foo>)" uses
5448 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005449 my $tested = quotemeta($1);
5450 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5451 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5452 my $func = $1;
5453 if (WARN('NEEDLESS_IF',
5454 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5455 $fix) {
5456 my $do_fix = 1;
5457 my $leading_tabs = "";
5458 my $new_leading_tabs = "";
5459 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5460 $leading_tabs = $1;
5461 } else {
5462 $do_fix = 0;
5463 }
5464 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5465 $new_leading_tabs = $1;
5466 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5467 $do_fix = 0;
5468 }
5469 } else {
5470 $do_fix = 0;
5471 }
5472 if ($do_fix) {
5473 fix_delete_line($fixlinenr - 1, $prevrawline);
5474 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5475 }
5476 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005477 }
5478 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005479
Joe Perchesebfdc402014-08-06 16:10:27 -07005480# check for unnecessary "Out of Memory" messages
5481 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5482 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5483 (defined $1 || defined $3) &&
5484 $linenr > 3) {
5485 my $testval = $2;
5486 my $testline = $lines[$linenr - 3];
5487
5488 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5489# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5490
5491 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5492 WARN("OOM_MESSAGE",
5493 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5494 }
5495 }
5496
Joe Perchesf78d98f2014-10-13 15:52:01 -07005497# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005498 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005499 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5500 my $level = $1;
5501 if (WARN("UNNECESSARY_KERN_LEVEL",
5502 "Possible unnecessary $level\n" . $herecurr) &&
5503 $fix) {
5504 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5505 }
5506 }
5507
Joe Perchesabb08a52014-12-10 15:51:46 -08005508# check for mask then right shift without a parentheses
5509 if ($^V && $^V ge 5.10.0 &&
5510 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5511 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5512 WARN("MASK_THEN_SHIFT",
5513 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5514 }
5515
Joe Perchesb75ac612014-12-10 15:52:02 -08005516# check for pointer comparisons to NULL
5517 if ($^V && $^V ge 5.10.0) {
5518 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5519 my $val = $1;
5520 my $equal = "!";
5521 $equal = "" if ($4 eq "!=");
5522 if (CHK("COMPARISON_TO_NULL",
5523 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5524 $fix) {
5525 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5526 }
5527 }
5528 }
5529
Joe Perches8716de32013-09-11 14:24:05 -07005530# check for bad placement of section $InitAttribute (e.g.: __initdata)
5531 if ($line =~ /(\b$InitAttribute\b)/) {
5532 my $attr = $1;
5533 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5534 my $ptr = $1;
5535 my $var = $2;
5536 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5537 ERROR("MISPLACED_INIT",
5538 "$attr should be placed after $var\n" . $herecurr)) ||
5539 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5540 WARN("MISPLACED_INIT",
5541 "$attr should be placed after $var\n" . $herecurr))) &&
5542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005543 $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;
Joe Perches8716de32013-09-11 14:24:05 -07005544 }
5545 }
5546 }
5547
Joe Perchese970b882013-11-12 15:10:10 -08005548# check for $InitAttributeData (ie: __initdata) with const
5549 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5550 my $attr = $1;
5551 $attr =~ /($InitAttributePrefix)(.*)/;
5552 my $attr_prefix = $1;
5553 my $attr_type = $2;
5554 if (ERROR("INIT_ATTRIBUTE",
5555 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5556 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005557 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005558 s/$InitAttributeData/${attr_prefix}initconst/;
5559 }
5560 }
5561
5562# check for $InitAttributeConst (ie: __initconst) without const
5563 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5564 my $attr = $1;
5565 if (ERROR("INIT_ATTRIBUTE",
5566 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5567 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005568 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005569 /(^\+\s*(?:static\s+))/;
5570 $lead = rtrim($1);
5571 $lead = "$lead " if ($lead !~ /^\+$/);
5572 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005573 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005574 }
5575 }
5576
Joe Perchesc17893c2015-04-16 12:44:42 -07005577# check for __read_mostly with const non-pointer (should just be const)
5578 if ($line =~ /\b__read_mostly\b/ &&
5579 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5580 if (ERROR("CONST_READ_MOSTLY",
5581 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5582 $fix) {
5583 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5584 }
5585 }
5586
Joe Perchesfbdb8132014-04-03 14:49:14 -07005587# don't use __constant_<foo> functions outside of include/uapi/
5588 if ($realfile !~ m@^include/uapi/@ &&
5589 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5590 my $constant_func = $1;
5591 my $func = $constant_func;
5592 $func =~ s/^__constant_//;
5593 if (WARN("CONSTANT_CONVERSION",
5594 "$constant_func should be $func\n" . $herecurr) &&
5595 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005596 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005597 }
5598 }
5599
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005600# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005601 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005602 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005603 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005604 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005605 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005606 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5607 }
5608 if ($delay > 2000) {
5609 WARN("LONG_UDELAY",
5610 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005611 }
5612 }
5613
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005614# warn about unexpectedly long msleep's
5615 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5616 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005617 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005618 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005619 }
5620 }
5621
Joe Perches36ec1932013-07-03 15:05:25 -07005622# check for comparisons of jiffies
5623 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5624 WARN("JIFFIES_COMPARISON",
5625 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5626 }
5627
Joe Perches9d7a34a2013-07-03 15:05:26 -07005628# check for comparisons of get_jiffies_64()
5629 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5630 WARN("JIFFIES_COMPARISON",
5631 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5632 }
5633
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005634# check the patch for use of mdelay
5635 if ($line =~ /\bmdelay\s*\(/) {
5636 WARN("MDELAY",
5637 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5638 }
5639
Andy Whitcroft00df3442007-06-08 13:47:06 -07005640# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005641# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005642# print "#ifdef in C files should be avoided\n";
5643# print "$herecurr";
5644# $clean = 0;
5645# }
5646
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005647# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005648 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005649 if (ERROR("SPACING",
5650 "exactly one space required after that #$1\n" . $herecurr) &&
5651 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005652 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005653 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5654 }
5655
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005656 }
5657
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005658# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005659 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5660 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005661 my $which = $1;
5662 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005663 CHK("UNCOMMENTED_DEFINITION",
5664 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005665 }
5666 }
5667# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005668
5669 my $barriers = qr{
5670 mb|
5671 rmb|
5672 wmb|
5673 read_barrier_depends
5674 }x;
5675 my $barrier_stems = qr{
5676 mb__before_atomic|
5677 mb__after_atomic|
5678 store_release|
5679 load_acquire|
5680 store_mb|
5681 (?:$barriers)
5682 }x;
5683 my $all_barriers = qr{
5684 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005685 smp_(?:$barrier_stems)|
5686 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005687 }x;
5688
5689 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005690 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005691 WARN("MEMORY_BARRIER",
5692 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005693 }
5694 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005695
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005696 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5697
5698 if ($realfile !~ m@^include/asm-generic/@ &&
5699 $realfile !~ m@/barrier\.h$@ &&
5700 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5701 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5702 WARN("MEMORY_BARRIER",
5703 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5704 }
5705
Joe Perchescb426e92015-06-25 15:02:46 -07005706# check for waitqueue_active without a comment.
5707 if ($line =~ /\bwaitqueue_active\s*\(/) {
5708 if (!ctx_has_comment($first_line, $linenr)) {
5709 WARN("WAITQUEUE_ACTIVE",
5710 "waitqueue_active without comment\n" . $herecurr);
5711 }
5712 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005713
5714# Check for expedited grace periods that interrupt non-idle non-nohz
5715# online CPUs. These expedited can therefore degrade real-time response
5716# if used carelessly, and should be avoided where not absolutely
5717# needed. It is always OK to use synchronize_rcu_expedited() and
5718# synchronize_sched_expedited() at boot time (before real-time applications
5719# start) and in error situations where real-time response is compromised in
5720# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5721# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5722# Of course, nothing comes for free, and srcu_read_lock() and
5723# srcu_read_unlock() do contain full memory barriers in payment for
5724# synchronize_srcu_expedited() non-interruption properties.
5725 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5726 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5727 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5728
5729 }
5730
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005731# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005732 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005733 CHK("ARCH_DEFINES",
5734 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005735 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005736
Tobias Klauserd4977c72010-05-24 14:33:30 -07005737# Check that the storage class is at the beginning of a declaration
5738 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005739 WARN("STORAGE_CLASS",
5740 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005741 }
5742
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005743# check the location of the inline attribute, that it is between
5744# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005745 if ($line =~ /\b$Type\s+$Inline\b/ ||
5746 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005747 ERROR("INLINE_LOCATION",
5748 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005749 }
5750
Andy Whitcroft8905a672007-11-28 16:21:06 -08005751# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005752 if ($realfile !~ m@\binclude/uapi/@ &&
5753 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005754 if (WARN("INLINE",
5755 "plain inline is preferred over $1\n" . $herecurr) &&
5756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005757 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005758
5759 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005760 }
5761
Joe Perches3d130fd2011-01-12 17:00:00 -08005762# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005763 if ($realfile !~ m@\binclude/uapi/@ &&
5764 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005765 WARN("PREFER_PACKED",
5766 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005767 }
5768
Joe Perches39b7e282011-07-25 17:13:24 -07005769# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005770 if ($realfile !~ m@\binclude/uapi/@ &&
5771 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005772 WARN("PREFER_ALIGNED",
5773 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005774 }
5775
Joe Perches5f14d3b2012-01-10 15:09:52 -08005776# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005777 if ($realfile !~ m@\binclude/uapi/@ &&
5778 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005779 if (WARN("PREFER_PRINTF",
5780 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5781 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005782 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005783
5784 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005785 }
5786
Joe Perches6061d942012-03-23 15:02:16 -07005787# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005788 if ($realfile !~ m@\binclude/uapi/@ &&
5789 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005790 if (WARN("PREFER_SCANF",
5791 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005793 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005794 }
Joe Perches6061d942012-03-23 15:02:16 -07005795 }
5796
Joe Perches619a9082014-12-10 15:51:35 -08005797# Check for __attribute__ weak, or __weak declarations (may have link issues)
5798 if ($^V && $^V ge 5.10.0 &&
5799 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5800 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5801 $line =~ /\b__weak\b/)) {
5802 ERROR("WEAK_DECLARATION",
5803 "Using weak declarations can have unintended link defects\n" . $herecurr);
5804 }
5805
Joe Perchese6176fa2015-06-25 15:02:49 -07005806# check for c99 types like uint8_t used outside of uapi/
5807 if ($realfile !~ m@\binclude/uapi/@ &&
5808 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5809 my $type = $1;
5810 if ($type =~ /\b($typeC99Typedefs)\b/) {
5811 $type = $1;
5812 my $kernel_type = 'u';
5813 $kernel_type = 's' if ($type =~ /^_*[si]/);
5814 $type =~ /(\d+)/;
5815 $kernel_type .= $1;
5816 if (CHK("PREFER_KERNEL_TYPES",
5817 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5818 $fix) {
5819 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5820 }
5821 }
5822 }
5823
Joe Perches938224b2016-01-20 14:59:15 -08005824# check for cast of C90 native int or longer types constants
5825 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5826 my $cast = $1;
5827 my $const = $2;
5828 if (WARN("TYPECAST_INT_CONSTANT",
5829 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5830 $fix) {
5831 my $suffix = "";
5832 my $newconst = $const;
5833 $newconst =~ s/${Int_type}$//;
5834 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5835 if ($cast =~ /\blong\s+long\b/) {
5836 $suffix .= 'LL';
5837 } elsif ($cast =~ /\blong\b/) {
5838 $suffix .= 'L';
5839 }
5840 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5841 }
5842 }
5843
Joe Perches8f53a9b2010-03-05 13:43:48 -08005844# check for sizeof(&)
5845 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005846 WARN("SIZEOF_ADDRESS",
5847 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005848 }
5849
Joe Perches66c80b62012-07-30 14:41:22 -07005850# check for sizeof without parenthesis
5851 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005852 if (WARN("SIZEOF_PARENTHESIS",
5853 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5854 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005855 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005856 }
Joe Perches66c80b62012-07-30 14:41:22 -07005857 }
5858
Joe Perches88982fe2012-12-17 16:02:00 -08005859# check for struct spinlock declarations
5860 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5861 WARN("USE_SPINLOCK_T",
5862 "struct spinlock should be spinlock_t\n" . $herecurr);
5863 }
5864
Joe Perchesa6962d72013-04-29 16:18:13 -07005865# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005866 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005867 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005868 $fmt =~ s/%%//g;
5869 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005870 if (WARN("PREFER_SEQ_PUTS",
5871 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5872 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005873 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005874 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005875 }
5876 }
5877
Tobin C. Harding5c145bd2017-11-23 10:59:45 +11005878 # check for vsprintf extension %p<foo> misuses
5879 if ($^V && $^V ge 5.10.0 &&
5880 defined $stat &&
5881 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5882 $1 !~ /^_*volatile_*$/) {
5883 my $bad_extension = "";
5884 my $lc = $stat =~ tr@\n@@;
5885 $lc = $lc + $linenr;
5886 for (my $count = $linenr; $count <= $lc; $count++) {
5887 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5888 $fmt =~ s/%%//g;
5889 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) {
5890 $bad_extension = $1;
5891 last;
5892 }
5893 }
5894 if ($bad_extension ne "") {
5895 my $stat_real = raw_line($linenr, 0);
5896 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5897 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5898 }
5899 WARN("VSPRINTF_POINTER_EXTENSION",
5900 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5901 }
5902 }
5903
Andy Whitcroft554e1652012-01-10 15:09:57 -08005904# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005905 if ($^V && $^V ge 5.10.0 &&
5906 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005907 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005908
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005909 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005910 my $ms_val = $7;
5911 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005912
Andy Whitcroft554e1652012-01-10 15:09:57 -08005913 if ($ms_size =~ /^(0x|)0$/i) {
5914 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005915 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005916 } elsif ($ms_size =~ /^(0x|)1$/i) {
5917 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005918 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5919 }
5920 }
5921
Joe Perches98a9bba2014-01-23 15:54:52 -08005922# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005923# if ($^V && $^V ge 5.10.0 &&
5924# defined $stat &&
5925# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5926# if (WARN("PREFER_ETHER_ADDR_COPY",
5927# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5928# $fix) {
5929# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5930# }
5931# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005932
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005933# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005934# if ($^V && $^V ge 5.10.0 &&
5935# defined $stat &&
5936# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5937# WARN("PREFER_ETHER_ADDR_EQUAL",
5938# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5939# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005940
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005941# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5942# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005943# if ($^V && $^V ge 5.10.0 &&
5944# defined $stat &&
5945# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5946#
5947# my $ms_val = $7;
5948#
5949# if ($ms_val =~ /^(?:0x|)0+$/i) {
5950# if (WARN("PREFER_ETH_ZERO_ADDR",
5951# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5952# $fix) {
5953# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5954# }
5955# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5956# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5957# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5958# $fix) {
5959# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5960# }
5961# }
5962# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005963
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005964# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005965 if ($^V && $^V ge 5.10.0 &&
5966 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005967 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005968 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005969 my $call = $1;
5970 my $cast1 = deparenthesize($2);
5971 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005972 my $cast2 = deparenthesize($7);
5973 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005974 my $cast;
5975
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005976 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005977 $cast = "$cast1 or $cast2";
5978 } elsif ($cast1 ne "") {
5979 $cast = $cast1;
5980 } else {
5981 $cast = $cast2;
5982 }
5983 WARN("MINMAX",
5984 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005985 }
5986 }
5987
Joe Perches4a273192012-07-30 14:41:20 -07005988# check usleep_range arguments
5989 if ($^V && $^V ge 5.10.0 &&
5990 defined $stat &&
5991 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5992 my $min = $1;
5993 my $max = $7;
5994 if ($min eq $max) {
5995 WARN("USLEEP_RANGE",
5996 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5997 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5998 $min > $max) {
5999 WARN("USLEEP_RANGE",
6000 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6001 }
6002 }
6003
Joe Perches823b7942013-11-12 15:10:15 -08006004# check for naked sscanf
6005 if ($^V && $^V ge 5.10.0 &&
6006 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006007 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006008 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6009 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6010 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6011 my $lc = $stat =~ tr@\n@@;
6012 $lc = $lc + $linenr;
6013 my $stat_real = raw_line($linenr, 0);
6014 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6015 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6016 }
6017 WARN("NAKED_SSCANF",
6018 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6019 }
6020
Joe Perchesafc819a2014-06-04 16:12:08 -07006021# check for simple sscanf that should be kstrto<foo>
6022 if ($^V && $^V ge 5.10.0 &&
6023 defined $stat &&
6024 $line =~ /\bsscanf\b/) {
6025 my $lc = $stat =~ tr@\n@@;
6026 $lc = $lc + $linenr;
6027 my $stat_real = raw_line($linenr, 0);
6028 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6029 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6030 }
6031 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6032 my $format = $6;
6033 my $count = $format =~ tr@%@%@;
6034 if ($count == 1 &&
6035 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6036 WARN("SSCANF_TO_KSTRTO",
6037 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6038 }
6039 }
6040 }
6041
Joe Perches70dc8a42013-09-11 14:23:58 -07006042# check for new externs in .h files.
6043 if ($realfile =~ /\.h$/ &&
6044 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006045 if (CHK("AVOID_EXTERNS",
6046 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006047 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006048 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006049 }
6050 }
6051
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006052# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006053 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006054 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006055 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006056 my $function_name = $1;
6057 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006058
6059 my $s = $stat;
6060 if (defined $cond) {
6061 substr($s, 0, length($cond), '');
6062 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006063 if ($s =~ /^\s*;/ &&
6064 $function_name ne 'uninitialized_var')
6065 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006066 WARN("AVOID_EXTERNS",
6067 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006068 }
6069
6070 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006071 WARN("FUNCTION_ARGUMENTS",
6072 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006073 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006074
6075 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6076 $stat =~ /^.\s*extern\s+/)
6077 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006078 WARN("AVOID_EXTERNS",
6079 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006080 }
6081
Joe Perchesca0d8922016-10-11 13:52:16 -07006082 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6083 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6084 $1 ne "void") {
6085 my $args = trim($1);
6086 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6087 my $arg = trim($1);
6088 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6089 WARN("FUNCTION_ARGUMENTS",
6090 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6091 }
6092 }
6093 }
6094
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006095# checks for new __setup's
6096 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6097 my $name = $1;
6098
6099 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006100 CHK("UNDOCUMENTED_SETUP",
6101 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006102 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006103 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006104
6105# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006106 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006107 WARN("UNNECESSARY_CASTS",
6108 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006109 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006110
Joe Perchesa640d252013-07-03 15:05:21 -07006111# alloc style
6112# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6113 if ($^V && $^V ge 5.10.0 &&
6114 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6115 CHK("ALLOC_SIZEOF_STRUCT",
6116 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6117 }
6118
Joe Perches60a55362014-06-04 16:12:07 -07006119# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6120 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006121 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07006122 my $oldfunc = $3;
6123 my $a1 = $4;
6124 my $a2 = $10;
6125 my $newfunc = "kmalloc_array";
6126 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006127 my $r1 = $a1;
6128 my $r2 = $a2;
6129 if ($a1 =~ /^sizeof\s*\S/) {
6130 $r1 = $a2;
6131 $r2 = $a1;
6132 }
6133 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6134 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006135 if (WARN("ALLOC_WITH_MULTIPLY",
6136 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6137 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006138 $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;
Joe Perches60a55362014-06-04 16:12:07 -07006139
6140 }
6141 }
6142 }
6143
Joe Perches972fdea2013-04-29 16:18:12 -07006144# check for krealloc arg reuse
6145 if ($^V && $^V ge 5.10.0 &&
6146 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6147 WARN("KREALLOC_ARG_REUSE",
6148 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6149 }
6150
Joe Perches5ce59ae2013-02-21 16:44:18 -08006151# check for alloc argument mismatch
6152 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6153 WARN("ALLOC_ARRAY_ARGS",
6154 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6155 }
6156
Joe Perchescaf2a542011-01-12 16:59:56 -08006157# check for multiple semicolons
6158 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006159 if (WARN("ONE_SEMICOLON",
6160 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6161 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006162 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006163 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006164 }
6165
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006166# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6167 if ($realfile !~ m@^include/uapi/@ &&
6168 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006169 my $ull = "";
6170 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6171 if (CHK("BIT_MACRO",
6172 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6173 $fix) {
6174 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6175 }
6176 }
6177
Joe Perches2d632742016-05-20 17:04:00 -07006178# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6179 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6180 my $config = $1;
6181 if (WARN("PREFER_IS_ENABLED",
6182 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6183 $fix) {
6184 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6185 }
6186 }
6187
Joe Perchese81f2392014-08-06 16:11:25 -07006188# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006189 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6190 my $has_break = 0;
6191 my $has_statement = 0;
6192 my $count = 0;
6193 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006194 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006195 $prevline--;
6196 my $rline = $rawlines[$prevline - 1];
6197 my $fline = $lines[$prevline - 1];
6198 last if ($fline =~ /^\@\@/);
6199 next if ($fline =~ /^\-/);
6200 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6201 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6202 next if ($fline =~ /^.[\s$;]*$/);
6203 $has_statement = 1;
6204 $count++;
6205 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6206 }
6207 if (!$has_break && $has_statement) {
6208 WARN("MISSING_BREAK",
6209 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6210 }
6211 }
6212
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006213# check for switch/default statements without a break;
6214 if ($^V && $^V ge 5.10.0 &&
6215 defined $stat &&
6216 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6217 my $ctx = '';
6218 my $herectx = $here . "\n";
6219 my $cnt = statement_rawlines($stat);
6220 for (my $n = 0; $n < $cnt; $n++) {
6221 $herectx .= raw_line($linenr, $n) . "\n";
6222 }
6223 WARN("DEFAULT_NO_BREAK",
6224 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006225 }
6226
Patrick Pannutof370e252010-07-23 13:34:18 -07006227# check for return codes on error paths
6228 if ($line =~ /\breturn\s+-\d+/) {
6229 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006230 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006231 }
6232
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006233# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006234 if ($line =~ /\b__FUNCTION__\b/) {
6235 if (WARN("USE_FUNC",
6236 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6237 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006238 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006239 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006241
Joe Perches62ec8182015-02-13 14:38:18 -08006242# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6243 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6244 ERROR("DATE_TIME",
6245 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6246 }
6247
Joe Perches2c924882012-03-23 15:02:20 -07006248# check for use of yield()
6249 if ($line =~ /\byield\s*\(\s*\)/) {
6250 WARN("YIELD",
6251 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6252 }
6253
Joe Perches179f8f42013-07-03 15:05:30 -07006254# check for comparisons against true and false
6255 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6256 my $lead = $1;
6257 my $arg = $2;
6258 my $test = $3;
6259 my $otype = $4;
6260 my $trail = $5;
6261 my $op = "!";
6262
6263 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6264
6265 my $type = lc($otype);
6266 if ($type =~ /^(?:true|false)$/) {
6267 if (("$test" eq "==" && "$type" eq "true") ||
6268 ("$test" eq "!=" && "$type" eq "false")) {
6269 $op = "";
6270 }
6271
6272 CHK("BOOL_COMPARISON",
6273 "Using comparison to $otype is error prone\n" . $herecurr);
6274
6275## maybe suggesting a correct construct would better
6276## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6277
6278 }
6279 }
6280
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006281# check for semaphores initialized locked
6282 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006283 WARN("CONSIDER_COMPLETION",
6284 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006285 }
Joe Perches6712d852012-03-23 15:02:20 -07006286
Joe Perches67d0a072011-10-31 17:13:10 -07006287# recommend kstrto* over simple_strto* and strict_strto*
6288 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006289 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006290 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006291 }
Joe Perches6712d852012-03-23 15:02:20 -07006292
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006293# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006294 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006295 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006296 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07006297 }
Joe Perches6712d852012-03-23 15:02:20 -07006298
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006299# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006300 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006301 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006302 WARN("CONST_STRUCT",
6303 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006304 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006305 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006306
6307# use of NR_CPUS is usually wrong
6308# ignore definitions of NR_CPUS and usage to define arrays as likely right
6309 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006310 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6311 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006312 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6313 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6314 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006315 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006316 WARN("NR_CPUS",
6317 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006318 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006319
Joe Perches52ea8502013-11-12 15:10:09 -08006320# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6321 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6322 ERROR("DEFINE_ARCH_HAS",
6323 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6324 }
6325
Joe Perchesacd93622015-02-13 14:38:38 -08006326# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6327 if ($^V && $^V ge 5.10.0 &&
6328 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6329 WARN("LIKELY_MISUSE",
6330 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6331 }
6332
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006333# whine mightly about in_atomic
6334 if ($line =~ /\bin_atomic\s*\(/) {
6335 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006336 ERROR("IN_ATOMIC",
6337 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006338 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006339 WARN("IN_ATOMIC",
6340 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006341 }
6342 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006343
Joe Perches481aea52016-05-20 17:04:08 -07006344# whine about ACCESS_ONCE
6345 if ($^V && $^V ge 5.10.0 &&
6346 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6347 my $par = $1;
6348 my $eq = $2;
6349 my $fun = $3;
6350 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6351 if (defined($eq)) {
6352 if (WARN("PREFER_WRITE_ONCE",
6353 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6354 $fix) {
6355 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6356 }
6357 } else {
6358 if (WARN("PREFER_READ_ONCE",
6359 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6360 $fix) {
6361 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6362 }
6363 }
6364 }
6365
Peter Zijlstra1704f472010-03-19 01:37:42 +01006366# check for lockdep_set_novalidate_class
6367 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6368 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6369 if ($realfile !~ m@^kernel/lockdep@ &&
6370 $realfile !~ m@^include/linux/lockdep@ &&
6371 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006372 ERROR("LOCKDEP",
6373 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006374 }
6375 }
Dave Jones88f88312011-01-12 16:59:59 -08006376
Joe Perchesb392c642015-04-16 12:44:16 -07006377 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6378 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006379 WARN("EXPORTED_WORLD_WRITABLE",
6380 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006381 }
Joe Perches24358802014-04-03 14:49:13 -07006382
Joe Perches515a2352014-04-03 14:49:24 -07006383# Mode permission misuses where it seems decimal should be octal
6384# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6385 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006386 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006387 $line =~ /$mode_perms_search/) {
6388 foreach my $entry (@mode_permission_funcs) {
6389 my $func = $entry->[0];
6390 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006391
Joe Perches459cf0a2016-10-11 13:52:19 -07006392 my $lc = $stat =~ tr@\n@@;
6393 $lc = $lc + $linenr;
6394 my $stat_real = raw_line($linenr, 0);
6395 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6396 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6397 }
6398
Joe Perches515a2352014-04-03 14:49:24 -07006399 my $skip_args = "";
6400 if ($arg_pos > 1) {
6401 $arg_pos--;
6402 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6403 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006404 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006405 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006406 my $val = $1;
6407 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006408 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6409 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006410 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006411 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006412 }
6413 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006414 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006415 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006416 }
Joe Perches24358802014-04-03 14:49:13 -07006417 }
6418 }
6419 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006420
Joe Perches459cf0a2016-10-11 13:52:19 -07006421# check for uses of S_<PERMS> that could be octal for readability
6422 if ($line =~ /\b$mode_perms_string_search\b/) {
6423 my $val = "";
6424 my $oval = "";
6425 my $to = 0;
6426 my $curpos = 0;
6427 my $lastpos = 0;
6428 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6429 $curpos = pos($line);
6430 my $match = $2;
6431 my $omatch = $1;
6432 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6433 $lastpos = $curpos;
6434 $to |= $mode_permission_string_types{$match};
6435 $val .= '\s*\|\s*' if ($val ne "");
6436 $val .= $match;
6437 $oval .= $omatch;
6438 }
6439 $oval =~ s/^\s*\|\s*//;
6440 $oval =~ s/\s*\|\s*$//;
6441 my $octal = sprintf("%04o", $to);
6442 if (WARN("SYMBOLIC_PERMS",
6443 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6444 $fix) {
6445 $fixed[$fixlinenr] =~ s/$val/$octal/;
6446 }
6447 }
6448
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006449# validate content of MODULE_LICENSE against list from include/linux/module.h
6450 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6451 my $extracted_string = get_quoted_string($line, $rawline);
6452 my $valid_licenses = qr{
6453 GPL|
6454 GPL\ v2|
6455 GPL\ and\ additional\ rights|
6456 Dual\ BSD/GPL|
6457 Dual\ MIT/GPL|
6458 Dual\ MPL/GPL|
6459 Proprietary
6460 }x;
6461 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6462 WARN("MODULE_LICENSE",
6463 "unknown module license " . $extracted_string . "\n" . $herecurr);
6464 }
6465 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006466 }
6467
Maya Erezec77bd62017-02-26 09:14:43 +02006468 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6469 WARN("BAD_SIGN_OFF",
6470 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6471 }
6472
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006473 # If we have no input at all, then there is nothing to report on
6474 # so just keep quiet.
6475 if ($#rawlines == -1) {
6476 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006477 }
6478
Andy Whitcroft8905a672007-11-28 16:21:06 -08006479 # In mailback mode only produce a report in the negative, for
6480 # things that appear to be patches.
6481 if ($mailback && ($clean == 1 || !$is_patch)) {
6482 exit(0);
6483 }
6484
6485 # This is not a patch, and we are are in 'no-patch' mode so
6486 # just keep quiet.
6487 if (!$chk_patch && !$is_patch) {
6488 exit(0);
6489 }
6490
Joe Perches06330fc2015-06-25 15:03:11 -07006491 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006492 ERROR("NOT_UNIFIED_DIFF",
6493 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006494 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006495 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006496 ERROR("MISSING_SIGN_OFF",
6497 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006498 }
6499
Andy Whitcroft8905a672007-11-28 16:21:06 -08006500 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006501 if ($summary && !($clean == 1 && $quiet == 1)) {
6502 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006503 print "total: $cnt_error errors, $cnt_warn warnings, " .
6504 (($check)? "$cnt_chk checks, " : "") .
6505 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006506 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006507
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006508 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006509 # If there were any defects found and not already fixing them
6510 if (!$clean and !$fix) {
6511 print << "EOM"
6512
6513NOTE: For some of the reported defects, checkpatch may be able to
6514 mechanically convert to the typical style using --fix or --fix-inplace.
6515EOM
6516 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006517 # If there were whitespace errors which cleanpatch can fix
6518 # then suggest that.
6519 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006520 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006521 print << "EOM"
6522
6523NOTE: Whitespace errors detected.
6524 You may wish to use scripts/cleanpatch or scripts/cleanfile
6525EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006526 }
6527 }
6528
Joe Perchesd752fcc2014-08-06 16:11:05 -07006529 if ($clean == 0 && $fix &&
6530 ("@rawlines" ne "@fixed" ||
6531 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006532 my $newfile = $filename;
6533 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006534 my $linecount = 0;
6535 my $f;
6536
Joe Perchesd752fcc2014-08-06 16:11:05 -07006537 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6538
Joe Perches3705ce52013-07-03 15:05:31 -07006539 open($f, '>', $newfile)
6540 or die "$P: Can't open $newfile for write\n";
6541 foreach my $fixed_line (@fixed) {
6542 $linecount++;
6543 if ($file) {
6544 if ($linecount > 3) {
6545 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006546 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006547 }
6548 } else {
6549 print $f $fixed_line . "\n";
6550 }
6551 }
6552 close($f);
6553
6554 if (!$quiet) {
6555 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006556
Joe Perches3705ce52013-07-03 15:05:31 -07006557Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6558
6559Do _NOT_ trust the results written to this file.
6560Do _NOT_ submit these changes without inspecting them for correctness.
6561
6562This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6563No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006564EOM
6565 }
6566 }
6567
Joe Perchesd8469f12015-06-25 15:03:00 -07006568 if ($quiet == 0) {
6569 print "\n";
6570 if ($clean == 1) {
6571 print "$vname has no obvious style problems and is ready for submission.\n";
6572 } else {
6573 print "$vname has style problems, please review.\n";
6574 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006575 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006576 return $clean;
6577}