blob: 81ff8e207b6908fd60ea444d53ee80677ed2810f [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
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;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
David Keitel72bf3e82016-03-23 20:52:44 -070014use constant BEFORE_SHORTTEXT => 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -080015use constant IN_SHORTTEXT_BLANKLINE => 1;
16use constant IN_SHORTTEXT => 2;
17use constant AFTER_SHORTTEXT => 3;
18use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel72bf3e82016-03-23 20:52:44 -070019use constant SHORTTEXT_LIMIT => 75;
20
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080022my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070023
Joe Perches000d1cc12011-07-25 17:13:25 -070024my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070025
26use Getopt::Long qw(:config no_auto_abbrev);
27
28my $quiet = 0;
29my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080032my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070033my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070036my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070038my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070039my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070040my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070041my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080042my $summary = 1;
43my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070045my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070046my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070047my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080048my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070049my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080050my %debug;
Joe Perches34456862013-07-03 15:05:34 -070051my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070052my %use_type = ();
53my @use = ();
54my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070055my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070056my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070057my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080058my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070059my $ignore_perl_version = 0;
60my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070061my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070062my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070063my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070064my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070065my $conststructsfile = "$D/const_structs.checkpatch";
Joe Perches57230292015-06-25 15:03:03 -070066my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070067my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070068
69sub help {
70 my ($exitcode) = @_;
71
72 print << "EOM";
73Usage: $P [OPTION]... [FILE]...
74Version: $V
75
76Options:
77 -q, --quiet quiet
78 --no-tree run without a kernel tree
79 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080080 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070081 --patch treat FILE as patchfile (default)
82 --emacs emacs compile window format
83 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070084 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070085 -g, --git treat FILE as a single commit or git revision range
86 single git commit with:
87 <rev>
88 <rev>^
89 <rev>~n
90 multiple git commits with:
91 <rev1>..<rev2>
92 <rev1>...<rev2>
93 <rev>-<count>
94 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070095 -f, --file treat FILE as regular source file
96 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070097 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070098 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070099 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700100 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800101 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700102 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700103 --root=PATH PATH to the kernel tree root
104 --no-summary suppress the per-file summary
105 --mailback only produce a report in case of warnings/errors
106 --summary-file include the filename in summary
107 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
108 'values', 'possible', 'type', and 'attr' (default
109 is all off)
110 --test-only=WORD report only warnings/errors containing WORD
111 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700112 --fix EXPERIMENTAL - may create horrible results
113 If correctable single-line errors exist, create
114 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
115 with potential errors corrected to the preferred
116 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800117 --fix-inplace EXPERIMENTAL - may create horrible results
118 Is the same as --fix, but overwrites the input
119 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700120 --ignore-perl-version override checking of perl version. expect
121 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700123 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700125 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 -h, --help, --version display this help and exit
127
128When FILE is - read standard input.
129EOM
130
131 exit($exitcode);
132}
133
Joe Perches3beb42e2016-05-20 17:04:14 -0700134sub uniq {
135 my %seen;
136 return grep { !$seen{$_}++ } @_;
137}
138
139sub list_types {
140 my ($exitcode) = @_;
141
142 my $count = 0;
143
144 local $/ = undef;
145
146 open(my $script, '<', abs_path($P)) or
147 die "$P: Can't read '$P' $!\n";
148
149 my $text = <$script>;
150 close($script);
151
152 my @types = ();
153 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
154 push (@types, $_);
155 }
156 @types = sort(uniq(@types));
157 print("#\tMessage type\n\n");
158 foreach my $type (@types) {
159 print(++$count . "\t" . $type . "\n");
160 }
161
162 exit($exitcode);
163}
164
Joe Perches000d1cc12011-07-25 17:13:25 -0700165my $conf = which_conf($configuration_file);
166if (-f $conf) {
167 my @conf_args;
168 open(my $conffile, '<', "$conf")
169 or warn "$P: Can't find a readable $configuration_file file $!\n";
170
171 while (<$conffile>) {
172 my $line = $_;
173
174 $line =~ s/\s*\n?$//g;
175 $line =~ s/^\s*//g;
176 $line =~ s/\s+/ /g;
177
178 next if ($line =~ m/^\s*#/);
179 next if ($line =~ m/^\s*$/);
180
181 my @words = split(" ", $line);
182 foreach my $word (@words) {
183 last if ($word =~ m/^#/);
184 push (@conf_args, $word);
185 }
186 }
187 close($conffile);
188 unshift(@ARGV, @conf_args) if @conf_args;
189}
190
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700191GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700192 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700193 'tree!' => \$tree,
194 'signoff!' => \$chk_signoff,
195 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800196 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700197 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800198 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700199 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700200 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700201 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202 'subjective!' => \$check,
203 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700204 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700205 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700206 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700207 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800208 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700209 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800211 'summary!' => \$summary,
212 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800213 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700214 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800215 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700216 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800217 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700218 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700219 'codespell!' => \$codespell,
220 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700221 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700222 'h|help' => \$help,
223 'version' => \$help
224) or help(1);
225
226help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700227
Joe Perches3beb42e2016-05-20 17:04:14 -0700228list_types(0) if ($list_types);
229
Joe Perches9624b8d2014-01-23 15:54:44 -0800230$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700231$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800232
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700233my $exit = 0;
234
Dave Hansend62a2012013-09-11 14:23:56 -0700235if ($^V && $^V lt $minimum_perl_version) {
236 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
237 if (!$ignore_perl_version) {
238 exit(1);
239 }
240}
241
Allen Hubbe45107ff2016-08-02 14:04:47 -0700242#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700244 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700245}
246
Joe Perches91bfe482013-09-11 14:23:59 -0700247sub hash_save_array_words {
248 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700249
Joe Perches91bfe482013-09-11 14:23:59 -0700250 my @array = split(/,/, join(',', @$arrayRef));
251 foreach my $word (@array) {
252 $word =~ s/\s*\n?$//g;
253 $word =~ s/^\s*//g;
254 $word =~ s/\s+/ /g;
255 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700256
Joe Perches91bfe482013-09-11 14:23:59 -0700257 next if ($word =~ m/^\s*#/);
258 next if ($word =~ m/^\s*$/);
259
260 $hashRef->{$word}++;
261 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700262}
263
Joe Perches91bfe482013-09-11 14:23:59 -0700264sub hash_show_words {
265 my ($hashRef, $prefix) = @_;
266
Joe Perches3c816e42015-06-25 15:03:29 -0700267 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700268 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700269 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700270 print " $word";
271 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700272 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700273 }
274}
275
276hash_save_array_words(\%ignore_type, \@ignore);
277hash_save_array_words(\%use_type, \@use);
278
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800279my $dbg_values = 0;
280my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700281my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700282my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800283for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800284 ## no critic
285 eval "\${dbg_$key} = '$debug{$key}';";
286 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800287}
288
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700289my $rpt_cleaners = 0;
290
Andy Whitcroft8905a672007-11-28 16:21:06 -0800291if ($terse) {
292 $emacs = 1;
293 $quiet++;
294}
295
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700296if ($tree) {
297 if (defined $root) {
298 if (!top_of_kernel_tree($root)) {
299 die "$P: $root: --root does not point at a valid tree\n";
300 }
301 } else {
302 if (top_of_kernel_tree('.')) {
303 $root = '.';
304 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
305 top_of_kernel_tree($1)) {
306 $root = $1;
307 }
308 }
309
310 if (!defined $root) {
311 print "Must be run from the top-level dir. of a kernel tree\n";
312 exit(2);
313 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700314}
315
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700316my $emitted_corrupt = 0;
317
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700318our $Ident = qr{
319 [A-Za-z_][A-Za-z\d_]*
320 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
321 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700322our $Storage = qr{extern|static|asmlinkage};
323our $Sparse = qr{
324 __user|
325 __kernel|
326 __force|
327 __iomem|
328 __must_check|
329 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800330 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700331 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800332 __rcu|
333 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700334 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800335our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
336our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
337our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
338our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
339our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700340
Wolfram Sang52131292010-03-05 13:43:51 -0800341# Notes to $Attribute:
342# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700343our $Attribute = qr{
344 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700345 __percpu|
346 __nocast|
347 __safe|
348 __bitwise__|
349 __packed__|
350 __packed2__|
351 __naked|
352 __maybe_unused|
353 __always_unused|
354 __noreturn|
355 __used|
356 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800357 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700358 __noclone|
359 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700360 __read_mostly|
361 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700362 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700363 ____cacheline_aligned|
364 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800365 ____cacheline_internodealigned_in_smp|
366 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700368our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700369our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700370our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
371our $Lval = qr{$Ident(?:$Member)*};
372
Joe Perches95e2c602013-07-03 15:05:20 -0700373our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
374our $Binary = qr{(?i)0b[01]+$Int_type?};
375our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
376our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700377our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800378our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800379our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
380our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
381our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800382our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700383our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800384our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700385our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700386our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387our $Operators = qr{
388 <=|>=|==|!=|
389 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700390 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700391 }x;
392
Joe Perches91cb5192014-04-03 14:49:32 -0700393our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
394
Joe Perchesab7e23f2015-04-16 12:44:22 -0700395our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800396our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700397our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700398our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700400our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700402our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403
Joe Perches15662b32011-10-31 17:13:12 -0700404our $NON_ASCII_UTF8 = qr{
405 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700406 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
407 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
408 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
409 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
410 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
411 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
412}x;
413
Joe Perches15662b32011-10-31 17:13:12 -0700414our $UTF8 = qr{
415 [\x09\x0A\x0D\x20-\x7E] # ASCII
416 | $NON_ASCII_UTF8
417}x;
418
Joe Perchese6176fa2015-06-25 15:02:49 -0700419our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800420our $typeOtherOSTypedefs = qr{(?x:
421 u_(?:char|short|int|long) | # bsd
422 u(?:nchar|short|int|long) # sysv
423)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700424our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700425 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700426 atomic_t
427)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700428our $typeTypedefs = qr{(?x:
429 $typeC99Typedefs\b|
430 $typeOtherOSTypedefs\b|
431 $typeKernelTypedefs\b
432)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700433
Joe Perches6d32f7a2015-11-06 16:31:37 -0800434our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
435
Joe Perches691e6692010-03-05 13:43:51 -0800436our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700437 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700438 (?:[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 -0700439 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700440 panic|
Joe Perches06668722013-11-12 15:10:07 -0800441 MODULE_[A-Z_]+|
442 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800443)};
444
Joe Perches20112472011-07-25 17:13:23 -0700445our $signature_tags = qr{(?xi:
446 Signed-off-by:|
447 Acked-by:|
448 Tested-by:|
449 Reviewed-by:|
450 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700451 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700452 To:|
453 Cc:
454)};
455
Joe Perches18130872014-08-06 16:11:22 -0700456our @typeListMisordered = (
457 qr{char\s+(?:un)?signed},
458 qr{int\s+(?:(?:un)?signed\s+)?short\s},
459 qr{int\s+short(?:\s+(?:un)?signed)},
460 qr{short\s+int(?:\s+(?:un)?signed)},
461 qr{(?:un)?signed\s+int\s+short},
462 qr{short\s+(?:un)?signed},
463 qr{long\s+int\s+(?:un)?signed},
464 qr{int\s+long\s+(?:un)?signed},
465 qr{long\s+(?:un)?signed\s+int},
466 qr{int\s+(?:un)?signed\s+long},
467 qr{int\s+(?:un)?signed},
468 qr{int\s+long\s+long\s+(?:un)?signed},
469 qr{long\s+long\s+int\s+(?:un)?signed},
470 qr{long\s+long\s+(?:un)?signed\s+int},
471 qr{long\s+long\s+(?:un)?signed},
472 qr{long\s+(?:un)?signed},
473);
474
Andy Whitcroft8905a672007-11-28 16:21:06 -0800475our @typeList = (
476 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700477 qr{(?:(?:un)?signed\s+)?char},
478 qr{(?:(?:un)?signed\s+)?short\s+int},
479 qr{(?:(?:un)?signed\s+)?short},
480 qr{(?:(?:un)?signed\s+)?int},
481 qr{(?:(?:un)?signed\s+)?long\s+int},
482 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
483 qr{(?:(?:un)?signed\s+)?long\s+long},
484 qr{(?:(?:un)?signed\s+)?long},
485 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800486 qr{float},
487 qr{double},
488 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800489 qr{struct\s+$Ident},
490 qr{union\s+$Ident},
491 qr{enum\s+$Ident},
492 qr{${Ident}_t},
493 qr{${Ident}_handler},
494 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700495 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800496);
Joe Perches938224b2016-01-20 14:59:15 -0800497
498our $C90_int_types = qr{(?x:
499 long\s+long\s+int\s+(?:un)?signed|
500 long\s+long\s+(?:un)?signed\s+int|
501 long\s+long\s+(?:un)?signed|
502 (?:(?:un)?signed\s+)?long\s+long\s+int|
503 (?:(?:un)?signed\s+)?long\s+long|
504 int\s+long\s+long\s+(?:un)?signed|
505 int\s+(?:(?:un)?signed\s+)?long\s+long|
506
507 long\s+int\s+(?:un)?signed|
508 long\s+(?:un)?signed\s+int|
509 long\s+(?:un)?signed|
510 (?:(?:un)?signed\s+)?long\s+int|
511 (?:(?:un)?signed\s+)?long|
512 int\s+long\s+(?:un)?signed|
513 int\s+(?:(?:un)?signed\s+)?long|
514
515 int\s+(?:un)?signed|
516 (?:(?:un)?signed\s+)?int
517)};
518
Alex Dowad485ff232015-06-25 15:02:52 -0700519our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700520our @typeListWithAttr = (
521 @typeList,
522 qr{struct\s+$InitAttribute\s+$Ident},
523 qr{union\s+$InitAttribute\s+$Ident},
524);
525
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700526our @modifierList = (
527 qr{fastcall},
528);
Alex Dowad485ff232015-06-25 15:02:52 -0700529our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800530
Joe Perches24358802014-04-03 14:49:13 -0700531our @mode_permission_funcs = (
532 ["module_param", 3],
533 ["module_param_(?:array|named|string)", 4],
534 ["module_param_array_named", 5],
535 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
536 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700537 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
538 ["IIO_DEV_ATTR_[A-Z_]+", 1],
539 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
540 ["SENSOR_TEMPLATE(?:_2|)", 3],
541 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700542);
543
Joe Perches515a2352014-04-03 14:49:24 -0700544#Create a search pattern for all these functions to speed up a loop below
545our $mode_perms_search = "";
546foreach my $entry (@mode_permission_funcs) {
547 $mode_perms_search .= '|' if ($mode_perms_search ne "");
548 $mode_perms_search .= $entry->[0];
549}
550
Joe Perchesb392c642015-04-16 12:44:16 -0700551our $mode_perms_world_writable = qr{
552 S_IWUGO |
553 S_IWOTH |
554 S_IRWXUGO |
555 S_IALLUGO |
556 0[0-7][0-7][2367]
557}x;
558
Joe Perchesf90774e2016-10-11 13:51:47 -0700559our %mode_permission_string_types = (
560 "S_IRWXU" => 0700,
561 "S_IRUSR" => 0400,
562 "S_IWUSR" => 0200,
563 "S_IXUSR" => 0100,
564 "S_IRWXG" => 0070,
565 "S_IRGRP" => 0040,
566 "S_IWGRP" => 0020,
567 "S_IXGRP" => 0010,
568 "S_IRWXO" => 0007,
569 "S_IROTH" => 0004,
570 "S_IWOTH" => 0002,
571 "S_IXOTH" => 0001,
572 "S_IRWXUGO" => 0777,
573 "S_IRUGO" => 0444,
574 "S_IWUGO" => 0222,
575 "S_IXUGO" => 0111,
576);
577
578#Create a search pattern for all these strings to speed up a loop below
579our $mode_perms_string_search = "";
580foreach my $entry (keys %mode_permission_string_types) {
581 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
582 $mode_perms_string_search .= $entry;
583}
584
Wolfram Sang7840a942010-08-09 17:20:57 -0700585our $allowed_asm_includes = qr{(?x:
586 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700587 memory|
588 time|
589 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700590)};
591# memory.h: ARM has a custom one
592
Kees Cook66b47b42014-10-13 15:51:57 -0700593# Load common spelling mistakes and build regular expression list.
594my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700595my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700596
Joe Perches36061e32014-12-10 15:51:43 -0800597if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800598 while (<$spelling>) {
599 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700600
Joe Perches36061e32014-12-10 15:51:43 -0800601 $line =~ s/\s*\n?$//g;
602 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700603
Joe Perches36061e32014-12-10 15:51:43 -0800604 next if ($line =~ m/^\s*#/);
605 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700606
Joe Perches36061e32014-12-10 15:51:43 -0800607 my ($suspect, $fix) = split(/\|\|/, $line);
608
Joe Perches36061e32014-12-10 15:51:43 -0800609 $spelling_fix{$suspect} = $fix;
610 }
611 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800612} else {
613 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700614}
Kees Cook66b47b42014-10-13 15:51:57 -0700615
Joe Perchesebfd7d62015-04-16 12:44:14 -0700616if ($codespell) {
617 if (open(my $spelling, '<', $codespellfile)) {
618 while (<$spelling>) {
619 my $line = $_;
620
621 $line =~ s/\s*\n?$//g;
622 $line =~ s/^\s*//g;
623
624 next if ($line =~ m/^\s*#/);
625 next if ($line =~ m/^\s*$/);
626 next if ($line =~ m/, disabled/i);
627
628 $line =~ s/,.*$//;
629
630 my ($suspect, $fix) = split(/->/, $line);
631
632 $spelling_fix{$suspect} = $fix;
633 }
634 close($spelling);
635 } else {
636 warn "No codespell typos will be found - file '$codespellfile': $!\n";
637 }
638}
639
640$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
641
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700642my $const_structs = "";
643if (open(my $conststructs, '<', $conststructsfile)) {
644 while (<$conststructs>) {
645 my $line = $_;
646
647 $line =~ s/\s*\n?$//g;
648 $line =~ s/^\s*//g;
649
650 next if ($line =~ m/^\s*#/);
651 next if ($line =~ m/^\s*$/);
652 if ($line =~ /\s/) {
653 print("$conststructsfile: '$line' invalid - ignored\n");
654 next;
655 }
656
657 $const_structs .= '|' if ($const_structs ne "");
658 $const_structs .= $line;
659 }
660 close($conststructsfile);
661} else {
662 warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
663}
664
Andy Whitcroft8905a672007-11-28 16:21:06 -0800665sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700666 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
667 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700668 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700669 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700670 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700671 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700672 (?:$typeTypedefs\b)|
673 (?:${all}\b)
674 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800675 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700676 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800677 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800678 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700679 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700680 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800681 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700682 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800683 }x;
Joe Perches18130872014-08-06 16:11:22 -0700684 $NonptrTypeMisordered = qr{
685 (?:$Modifier\s+|const\s+)*
686 (?:
687 (?:${Misordered}\b)
688 )
689 (?:\s+$Modifier|\s+const)*
690 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700691 $NonptrTypeWithAttr = qr{
692 (?:$Modifier\s+|const\s+)*
693 (?:
694 (?:typeof|__typeof__)\s*\([^\)]*\)|
695 (?:$typeTypedefs\b)|
696 (?:${allWithAttr}\b)
697 )
698 (?:\s+$Modifier|\s+const)*
699 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800700 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700701 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700702 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700703 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800704 }x;
Joe Perches18130872014-08-06 16:11:22 -0700705 $TypeMisordered = qr{
706 $NonptrTypeMisordered
707 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
708 (?:\s+$Inline|\s+$Modifier)*
709 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700710 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700711 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800712}
713build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700714
Joe Perches7d2367a2011-07-25 17:13:22 -0700715our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700716
717# Using $balanced_parens, $LvalOrFunc, or $FuncArg
718# requires at least perl version v5.10.0
719# Any use must be runtime checked with $^V
720
721our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700722our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800723our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700724
Joe Perchesf8422302014-08-06 16:11:31 -0700725our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700726 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700727 (?:$Storage\s+)?LIST_HEAD\s*\(|
728 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
729)};
730
Joe Perches7d2367a2011-07-25 17:13:22 -0700731sub deparenthesize {
732 my ($string) = @_;
733 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700734
735 while ($string =~ /^\s*\(.*\)\s*$/) {
736 $string =~ s@^\s*\(\s*@@;
737 $string =~ s@\s*\)\s*$@@;
738 }
739
Joe Perches7d2367a2011-07-25 17:13:22 -0700740 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700741
Joe Perches7d2367a2011-07-25 17:13:22 -0700742 return $string;
743}
744
Joe Perches34456862013-07-03 15:05:34 -0700745sub seed_camelcase_file {
746 my ($file) = @_;
747
748 return if (!(-f $file));
749
750 local $/;
751
752 open(my $include_file, '<', "$file")
753 or warn "$P: Can't read '$file' $!\n";
754 my $text = <$include_file>;
755 close($include_file);
756
757 my @lines = split('\n', $text);
758
759 foreach my $line (@lines) {
760 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
761 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
762 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800763 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
764 $camelcase{$1} = 1;
765 } 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 -0700766 $camelcase{$1} = 1;
767 }
768 }
769}
770
Joe Perches85b0ee12016-10-11 13:51:44 -0700771sub is_maintained_obsolete {
772 my ($filename) = @_;
773
774 return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
775
Joe Perches0616efa2016-10-11 13:52:02 -0700776 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 -0700777
778 return $status =~ /obsolete/i;
779}
780
Joe Perches34456862013-07-03 15:05:34 -0700781my $camelcase_seeded = 0;
782sub seed_camelcase_includes {
783 return if ($camelcase_seeded);
784
785 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700786 my $camelcase_cache = "";
787 my @include_files = ();
788
789 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700790
Richard Genoud3645e322014-02-10 14:25:32 -0800791 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700792 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
793 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700794 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700795 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700796 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700797 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700798 @include_files = split('\n', $files);
799 foreach my $file (@include_files) {
800 my $date = POSIX::strftime("%Y%m%d%H%M",
801 localtime((stat $file)[9]));
802 $last_mod_date = $date if ($last_mod_date < $date);
803 }
804 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700805 }
Joe Perchesc707a812013-07-08 16:00:43 -0700806
807 if ($camelcase_cache ne "" && -f $camelcase_cache) {
808 open(my $camelcase_file, '<', "$camelcase_cache")
809 or warn "$P: Can't read '$camelcase_cache' $!\n";
810 while (<$camelcase_file>) {
811 chomp;
812 $camelcase{$_} = 1;
813 }
814 close($camelcase_file);
815
816 return;
817 }
818
Richard Genoud3645e322014-02-10 14:25:32 -0800819 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700820 $files = `git ls-files "include/*.h"`;
821 @include_files = split('\n', $files);
822 }
823
Joe Perches34456862013-07-03 15:05:34 -0700824 foreach my $file (@include_files) {
825 seed_camelcase_file($file);
826 }
Joe Perches351b2a12013-07-03 15:05:36 -0700827
Joe Perchesc707a812013-07-08 16:00:43 -0700828 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700829 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700830 open(my $camelcase_file, '>', "$camelcase_cache")
831 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700832 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
833 print $camelcase_file ("$_\n");
834 }
835 close($camelcase_file);
836 }
Joe Perches34456862013-07-03 15:05:34 -0700837}
838
Joe Perchesd311cd42014-08-06 16:10:57 -0700839sub git_commit_info {
840 my ($commit, $id, $desc) = @_;
841
842 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
843
844 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
845 $output =~ s/^\s*//gm;
846 my @lines = split("\n", $output);
847
Joe Perches0d7835f2015-02-13 14:38:35 -0800848 return ($id, $desc) if ($#lines < 0);
849
Joe Perchesd311cd42014-08-06 16:10:57 -0700850 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
851# Maybe one day convert this block of bash into something that returns
852# all matching commit ids, but it's very slow...
853#
854# echo "checking commits $1..."
855# git rev-list --remotes | grep -i "^$1" |
856# while read line ; do
857# git log --format='%H %s' -1 $line |
858# echo "commit $(cut -c 1-12,41-)"
859# done
860 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
861 } else {
862 $id = substr($lines[0], 0, 12);
863 $desc = substr($lines[0], 41);
864 }
865
866 return ($id, $desc);
867}
868
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700869$chk_signoff = 0 if ($file);
870
Andy Whitcroft00df3442007-06-08 13:47:06 -0700871my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800872my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700873my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700874my @fixed_inserted = ();
875my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700876my $fixlinenr = -1;
877
Du, Changbin4a593c32016-05-20 17:04:16 -0700878# If input is git commits, extract all commits from the commit expressions.
879# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
880die "$P: No git repository found\n" if ($git && !-e ".git");
881
882if ($git) {
883 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700884 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700885 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700886 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
887 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700888 } elsif ($commit_expr =~ m/\.\./) {
889 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700890 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700891 $git_range = "-1 $commit_expr";
892 }
893 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
894 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700895 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
896 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700897 my $sha1 = $1;
898 my $subject = $2;
899 unshift(@commits, $sha1);
900 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700901 }
902 }
903 die "$P: no git commits after extraction!\n" if (@commits == 0);
904 @ARGV = @commits;
905}
906
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800907my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700908for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800909 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700910 if ($git) {
911 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
912 die "$P: $filename: git format-patch failed - $!\n";
913 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800914 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800916 } elsif ($filename eq '-') {
917 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700918 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800919 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700920 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700921 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800922 if ($filename eq '-') {
923 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700924 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700925 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800926 } else {
927 $vname = $filename;
928 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800929 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700930 chomp;
931 push(@rawlines, $_);
932 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800933 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700934
935 if ($#ARGV > 0 && $quiet == 0) {
936 print '-' x length($vname) . "\n";
937 print "$vname\n";
938 print '-' x length($vname) . "\n";
939 }
940
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800941 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700942 $exit = 1;
943 }
944 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800945 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700946 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700947 @fixed_inserted = ();
948 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700949 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700950 @modifierListFile = ();
951 @typeListFile = ();
952 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700953}
954
Joe Perchesd8469f12015-06-25 15:03:00 -0700955if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700956 hash_show_words(\%use_type, "Used");
957 hash_show_words(\%ignore_type, "Ignored");
958
Joe Perchesd8469f12015-06-25 15:03:00 -0700959 if ($^V lt 5.10.0) {
960 print << "EOM"
961
962NOTE: perl $^V is not modern enough to detect all possible issues.
963 An upgrade to at least perl v5.10.0 is suggested.
964EOM
965 }
966 if ($exit) {
967 print << "EOM"
968
969NOTE: If any of the errors are false positives, please report
970 them to the maintainer, see CHECKPATCH in MAINTAINERS.
971EOM
972 }
973}
974
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700975exit($exit);
976
977sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700978 my ($root) = @_;
979
980 my @tree_check = (
981 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
982 "README", "Documentation", "arch", "include", "drivers",
983 "fs", "init", "ipc", "kernel", "lib", "scripts",
984 );
985
986 foreach my $check (@tree_check) {
987 if (! -e $root . '/' . $check) {
988 return 0;
989 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700990 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700992}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700993
Joe Perches20112472011-07-25 17:13:23 -0700994sub parse_email {
995 my ($formatted_email) = @_;
996
997 my $name = "";
998 my $address = "";
999 my $comment = "";
1000
1001 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1002 $name = $1;
1003 $address = $2;
1004 $comment = $3 if defined $3;
1005 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1006 $address = $1;
1007 $comment = $2 if defined $2;
1008 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1009 $address = $1;
1010 $comment = $2 if defined $2;
1011 $formatted_email =~ s/$address.*$//;
1012 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001013 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001014 $name =~ s/^\"|\"$//g;
1015 # If there's a name left after stripping spaces and
1016 # leading quotes, and the address doesn't have both
1017 # leading and trailing angle brackets, the address
1018 # is invalid. ie:
1019 # "joe smith joe@smith.com" bad
1020 # "joe smith <joe@smith.com" bad
1021 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1022 $name = "";
1023 $address = "";
1024 $comment = "";
1025 }
1026 }
1027
Joe Perches3705ce52013-07-03 15:05:31 -07001028 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001029 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001030 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001031 $address =~ s/^\<|\>$//g;
1032
1033 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1034 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1035 $name = "\"$name\"";
1036 }
1037
1038 return ($name, $address, $comment);
1039}
1040
1041sub format_email {
1042 my ($name, $address) = @_;
1043
1044 my $formatted_email;
1045
Joe Perches3705ce52013-07-03 15:05:31 -07001046 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001047 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001048 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001049
1050 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1051 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1052 $name = "\"$name\"";
1053 }
1054
1055 if ("$name" eq "") {
1056 $formatted_email = "$address";
1057 } else {
1058 $formatted_email = "$name <$address>";
1059 }
1060
1061 return $formatted_email;
1062}
1063
Joe Perchesd311cd42014-08-06 16:10:57 -07001064sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001065 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001066
Joe Perchesbd474ca2014-08-06 16:11:10 -07001067 foreach my $path (split(/:/, $ENV{PATH})) {
1068 if (-e "$path/$bin") {
1069 return "$path/$bin";
1070 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001071 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001072
Joe Perchesbd474ca2014-08-06 16:11:10 -07001073 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001074}
1075
Joe Perches000d1cc12011-07-25 17:13:25 -07001076sub which_conf {
1077 my ($conf) = @_;
1078
1079 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1080 if (-e "$path/$conf") {
1081 return "$path/$conf";
1082 }
1083 }
1084
1085 return "";
1086}
1087
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001088sub expand_tabs {
1089 my ($str) = @_;
1090
1091 my $res = '';
1092 my $n = 0;
1093 for my $c (split(//, $str)) {
1094 if ($c eq "\t") {
1095 $res .= ' ';
1096 $n++;
1097 for (; ($n % 8) != 0; $n++) {
1098 $res .= ' ';
1099 }
1100 next;
1101 }
1102 $res .= $c;
1103 $n++;
1104 }
1105
1106 return $res;
1107}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001108sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001109 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110 return $res;
1111}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001112
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001113sub line_stats {
1114 my ($line) = @_;
1115
1116 # Drop the diff line leader and expand tabs
1117 $line =~ s/^.//;
1118 $line = expand_tabs($line);
1119
1120 # Pick the indent from the front of the line.
1121 my ($white) = ($line =~ /^(\s*)/);
1122
1123 return (length($line), length($white));
1124}
1125
Andy Whitcroft773647a2008-03-28 14:15:58 -07001126my $sanitise_quote = '';
1127
1128sub sanitise_line_reset {
1129 my ($in_comment) = @_;
1130
1131 if ($in_comment) {
1132 $sanitise_quote = '*/';
1133 } else {
1134 $sanitise_quote = '';
1135 }
1136}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001137sub sanitise_line {
1138 my ($line) = @_;
1139
1140 my $res = '';
1141 my $l = '';
1142
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001143 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001144 my $off = 0;
1145 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001146
Andy Whitcroft773647a2008-03-28 14:15:58 -07001147 # Always copy over the diff marker.
1148 $res = substr($line, 0, 1);
1149
1150 for ($off = 1; $off < length($line); $off++) {
1151 $c = substr($line, $off, 1);
1152
1153 # Comments we are wacking completly including the begin
1154 # and end, all to $;.
1155 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1156 $sanitise_quote = '*/';
1157
1158 substr($res, $off, 2, "$;$;");
1159 $off++;
1160 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001162 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001163 $sanitise_quote = '';
1164 substr($res, $off, 2, "$;$;");
1165 $off++;
1166 next;
1167 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001168 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1169 $sanitise_quote = '//';
1170
1171 substr($res, $off, 2, $sanitise_quote);
1172 $off++;
1173 next;
1174 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001175
1176 # A \ in a string means ignore the next character.
1177 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1178 $c eq "\\") {
1179 substr($res, $off, 2, 'XX');
1180 $off++;
1181 next;
1182 }
1183 # Regular quotes.
1184 if ($c eq "'" || $c eq '"') {
1185 if ($sanitise_quote eq '') {
1186 $sanitise_quote = $c;
1187
1188 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001189 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001190 } elsif ($sanitise_quote eq $c) {
1191 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001192 }
1193 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001194
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001195 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1197 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001198 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1199 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1201 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001202 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001203 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001204 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001205 }
1206
Daniel Walker113f04a2009-09-21 17:04:35 -07001207 if ($sanitise_quote eq '//') {
1208 $sanitise_quote = '';
1209 }
1210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001211 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001212 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 my $clean = 'X' x length($1);
1214 $res =~ s@\<.*\>@<$clean>@;
1215
1216 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001217 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001218 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001219 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001220 }
1221
Joe Perchesdadf6802016-08-02 14:04:33 -07001222 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1223 my $match = $1;
1224 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1225 }
1226
Andy Whitcroft00df3442007-06-08 13:47:06 -07001227 return $res;
1228}
1229
Joe Perchesa6962d72013-04-29 16:18:13 -07001230sub get_quoted_string {
1231 my ($line, $rawline) = @_;
1232
Joe Perches33acb542015-06-25 15:02:54 -07001233 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001234 return substr($rawline, $-[0], $+[0] - $-[0]);
1235}
1236
Andy Whitcroft8905a672007-11-28 16:21:06 -08001237sub ctx_statement_block {
1238 my ($linenr, $remain, $off) = @_;
1239 my $line = $linenr - 1;
1240 my $blk = '';
1241 my $soff = $off;
1242 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001243 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001244
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001245 my $loff = 0;
1246
Andy Whitcroft8905a672007-11-28 16:21:06 -08001247 my $type = '';
1248 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001249 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001250 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001251 my $c;
1252 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001253
1254 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001255 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001256 @stack = (['', 0]) if ($#stack == -1);
1257
Andy Whitcroft773647a2008-03-28 14:15:58 -07001258 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001259 # If we are about to drop off the end, pull in more
1260 # context.
1261 if ($off >= $len) {
1262 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001263 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001264 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001265 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001266 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001267 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001268 $len = length($blk);
1269 $line++;
1270 last;
1271 }
1272 # Bail if there is no further context.
1273 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001274 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001275 last;
1276 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001277 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1278 $level++;
1279 $type = '#';
1280 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001281 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001282 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001283 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001284 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285
Andy Whitcroft773647a2008-03-28 14:15:58 -07001286 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001287
1288 # Handle nested #if/#else.
1289 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1290 push(@stack, [ $type, $level ]);
1291 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1292 ($type, $level) = @{$stack[$#stack - 1]};
1293 } elsif ($remainder =~ /^#\s*endif\b/) {
1294 ($type, $level) = @{pop(@stack)};
1295 }
1296
Andy Whitcroft8905a672007-11-28 16:21:06 -08001297 # Statement ends at the ';' or a close '}' at the
1298 # outermost level.
1299 if ($level == 0 && $c eq ';') {
1300 last;
1301 }
1302
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001303 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001304 if ($level == 0 && $coff_set == 0 &&
1305 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1306 $remainder =~ /^(else)(?:\s|{)/ &&
1307 $remainder !~ /^else\s+if\b/) {
1308 $coff = $off + length($1) - 1;
1309 $coff_set = 1;
1310 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1311 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001312 }
1313
Andy Whitcroft8905a672007-11-28 16:21:06 -08001314 if (($type eq '' || $type eq '(') && $c eq '(') {
1315 $level++;
1316 $type = '(';
1317 }
1318 if ($type eq '(' && $c eq ')') {
1319 $level--;
1320 $type = ($level != 0)? '(' : '';
1321
1322 if ($level == 0 && $coff < $soff) {
1323 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001324 $coff_set = 1;
1325 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001326 }
1327 }
1328 if (($type eq '' || $type eq '{') && $c eq '{') {
1329 $level++;
1330 $type = '{';
1331 }
1332 if ($type eq '{' && $c eq '}') {
1333 $level--;
1334 $type = ($level != 0)? '{' : '';
1335
1336 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001337 if (substr($blk, $off + 1, 1) eq ';') {
1338 $off++;
1339 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001340 last;
1341 }
1342 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001343 # Preprocessor commands end at the newline unless escaped.
1344 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1345 $level--;
1346 $type = '';
1347 $off++;
1348 last;
1349 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001350 $off++;
1351 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001352 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001353 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001354 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 $line++;
1356 $remain--;
1357 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358
1359 my $statement = substr($blk, $soff, $off - $soff + 1);
1360 my $condition = substr($blk, $soff, $coff - $soff + 1);
1361
1362 #warn "STATEMENT<$statement>\n";
1363 #warn "CONDITION<$condition>\n";
1364
Andy Whitcroft773647a2008-03-28 14:15:58 -07001365 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001366
1367 return ($statement, $condition,
1368 $line, $remain + 1, $off - $loff + 1, $level);
1369}
1370
Andy Whitcroftcf655042008-03-04 14:28:20 -08001371sub statement_lines {
1372 my ($stmt) = @_;
1373
1374 # Strip the diff line prefixes and rip blank lines at start and end.
1375 $stmt =~ s/(^|\n)./$1/g;
1376 $stmt =~ s/^\s*//;
1377 $stmt =~ s/\s*$//;
1378
1379 my @stmt_lines = ($stmt =~ /\n/g);
1380
1381 return $#stmt_lines + 2;
1382}
1383
1384sub statement_rawlines {
1385 my ($stmt) = @_;
1386
1387 my @stmt_lines = ($stmt =~ /\n/g);
1388
1389 return $#stmt_lines + 2;
1390}
1391
1392sub statement_block_size {
1393 my ($stmt) = @_;
1394
1395 $stmt =~ s/(^|\n)./$1/g;
1396 $stmt =~ s/^\s*{//;
1397 $stmt =~ s/}\s*$//;
1398 $stmt =~ s/^\s*//;
1399 $stmt =~ s/\s*$//;
1400
1401 my @stmt_lines = ($stmt =~ /\n/g);
1402 my @stmt_statements = ($stmt =~ /;/g);
1403
1404 my $stmt_lines = $#stmt_lines + 2;
1405 my $stmt_statements = $#stmt_statements + 1;
1406
1407 if ($stmt_lines > $stmt_statements) {
1408 return $stmt_lines;
1409 } else {
1410 return $stmt_statements;
1411 }
1412}
1413
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001414sub ctx_statement_full {
1415 my ($linenr, $remain, $off) = @_;
1416 my ($statement, $condition, $level);
1417
1418 my (@chunks);
1419
Andy Whitcroftcf655042008-03-04 14:28:20 -08001420 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001421 ($statement, $condition, $linenr, $remain, $off, $level) =
1422 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001423 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001424 push(@chunks, [ $condition, $statement ]);
1425 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1426 return ($level, $linenr, @chunks);
1427 }
1428
1429 # Pull in the following conditional/block pairs and see if they
1430 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001432 ($statement, $condition, $linenr, $remain, $off, $level) =
1433 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001434 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001435 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 #print "C: push\n";
1437 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001438 }
1439
1440 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001441}
1442
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001443sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001444 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001445 my $line;
1446 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001447 my $blk = '';
1448 my @o;
1449 my @c;
1450 my @res = ();
1451
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001452 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001453 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001454 for ($line = $start; $remain > 0; $line++) {
1455 next if ($rawlines[$line] =~ /^-/);
1456 $remain--;
1457
1458 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001459
1460 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001461 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001462 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001463 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001464 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001465 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001466 $level = pop(@stack);
1467 }
1468
Andy Whitcroft01464f32010-10-26 14:23:19 -07001469 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001470 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1471 if ($off > 0) {
1472 $off--;
1473 next;
1474 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001475
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001476 if ($c eq $close && $level > 0) {
1477 $level--;
1478 last if ($level == 0);
1479 } elsif ($c eq $open) {
1480 $level++;
1481 }
1482 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001483
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001484 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001485 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001486 }
1487
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001488 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001489 }
1490
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001491 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001492}
1493sub ctx_block_outer {
1494 my ($linenr, $remain) = @_;
1495
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001496 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1497 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001498}
1499sub ctx_block {
1500 my ($linenr, $remain) = @_;
1501
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001502 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1503 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001504}
1505sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001506 my ($linenr, $remain, $off) = @_;
1507
1508 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1509 return @r;
1510}
1511sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001512 my ($linenr, $remain) = @_;
1513
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001514 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001515}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001516sub ctx_statement_level {
1517 my ($linenr, $remain, $off) = @_;
1518
1519 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1520}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001521
1522sub ctx_locate_comment {
1523 my ($first_line, $end_line) = @_;
1524
1525 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001526 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001527 return $current_comment if (defined $current_comment);
1528
1529 # Look through the context and try and figure out if there is a
1530 # comment.
1531 my $in_comment = 0;
1532 $current_comment = '';
1533 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001534 my $line = $rawlines[$linenr - 1];
1535 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001536 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1537 $in_comment = 1;
1538 }
1539 if ($line =~ m@/\*@) {
1540 $in_comment = 1;
1541 }
1542 if (!$in_comment && $current_comment ne '') {
1543 $current_comment = '';
1544 }
1545 $current_comment .= $line . "\n" if ($in_comment);
1546 if ($line =~ m@\*/@) {
1547 $in_comment = 0;
1548 }
1549 }
1550
1551 chomp($current_comment);
1552 return($current_comment);
1553}
1554sub ctx_has_comment {
1555 my ($first_line, $end_line) = @_;
1556 my $cmt = ctx_locate_comment($first_line, $end_line);
1557
Andy Whitcroft00df3442007-06-08 13:47:06 -07001558 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001559 ##print "CMMT: $cmt\n";
1560
1561 return ($cmt ne '');
1562}
1563
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001564sub raw_line {
1565 my ($linenr, $cnt) = @_;
1566
1567 my $offset = $linenr - 1;
1568 $cnt++;
1569
1570 my $line;
1571 while ($cnt) {
1572 $line = $rawlines[$offset++];
1573 next if (defined($line) && $line =~ /^-/);
1574 $cnt--;
1575 }
1576
1577 return $line;
1578}
1579
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001580sub cat_vet {
1581 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001582 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001583
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001584 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001585 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1586 $res .= $1;
1587 if ($2 ne '') {
1588 $coded = sprintf("^%c", unpack('C', $2) + 64);
1589 $res .= $coded;
1590 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001591 }
1592 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001593
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001594 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001595}
1596
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001597my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001598my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001599my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001600my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001601
1602sub annotate_reset {
1603 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001604 $av_pending = '_';
1605 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001606 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001607}
1608
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001609sub annotate_values {
1610 my ($stream, $type) = @_;
1611
1612 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001613 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001614 my $cur = $stream;
1615
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001616 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001617
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001618 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001619 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001620 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001621 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001622 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001623 print "WS($1)\n" if ($dbg_values > 1);
1624 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001625 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001626 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001627 }
1628
Florian Micklerc023e4732011-01-12 16:59:58 -08001629 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001630 print "CAST($1)\n" if ($dbg_values > 1);
1631 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001632 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001633
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001634 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001635 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001636 $type = 'T';
1637
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001638 } elsif ($cur =~ /^($Modifier)\s*/) {
1639 print "MODIFIER($1)\n" if ($dbg_values > 1);
1640 $type = 'T';
1641
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001642 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001643 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001644 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001645 push(@av_paren_type, $type);
1646 if ($2 ne '') {
1647 $av_pending = 'N';
1648 }
1649 $type = 'E';
1650
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001651 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001652 print "UNDEF($1)\n" if ($dbg_values > 1);
1653 $av_preprocessor = 1;
1654 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001655
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001656 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001657 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001658 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001659
1660 push(@av_paren_type, $type);
1661 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001662 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001663
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001664 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1666 $av_preprocessor = 1;
1667
1668 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1669
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001670 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001671
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001672 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673 print "PRE_END($1)\n" if ($dbg_values > 1);
1674
1675 $av_preprocessor = 1;
1676
1677 # Assume all arms of the conditional end as this
1678 # one does, and continue as if the #endif was not here.
1679 pop(@av_paren_type);
1680 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001681 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001682
1683 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001684 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001685
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001686 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1687 print "ATTR($1)\n" if ($dbg_values > 1);
1688 $av_pending = $type;
1689 $type = 'N';
1690
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001691 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001693 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001694 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001695 }
1696 $type = 'N';
1697
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001698 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001699 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001700 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001701 $type = 'N';
1702
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001703 } elsif ($cur =~/^(case)/o) {
1704 print "CASE($1)\n" if ($dbg_values > 1);
1705 $av_pend_colon = 'C';
1706 $type = 'N';
1707
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001708 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001709 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001710 $type = 'N';
1711
1712 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001713 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001714 push(@av_paren_type, $av_pending);
1715 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001716 $type = 'N';
1717
1718 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001719 my $new_type = pop(@av_paren_type);
1720 if ($new_type ne '_') {
1721 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001722 print "PAREN('$1') -> $type\n"
1723 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 }
1727
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001728 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001729 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001730 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001731 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001732
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001733 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1734 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001735 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001736 } elsif ($type eq 'E') {
1737 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001738 }
1739 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1740 $type = 'V';
1741
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001743 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001744 $type = 'V';
1745
1746 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001747 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001748 $type = 'N';
1749
Andy Whitcroftcf655042008-03-04 14:28:20 -08001750 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001751 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001752 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001753 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001754
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001755 } elsif ($cur =~/^(,)/) {
1756 print "COMMA($1)\n" if ($dbg_values > 1);
1757 $type = 'C';
1758
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001759 } elsif ($cur =~ /^(\?)/o) {
1760 print "QUESTION($1)\n" if ($dbg_values > 1);
1761 $type = 'N';
1762
1763 } elsif ($cur =~ /^(:)/o) {
1764 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1765
1766 substr($var, length($res), 1, $av_pend_colon);
1767 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1768 $type = 'E';
1769 } else {
1770 $type = 'N';
1771 }
1772 $av_pend_colon = 'O';
1773
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001774 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001775 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001776 $type = 'N';
1777
Andy Whitcroft0d413862008-10-15 22:02:16 -07001778 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001779 my $variant;
1780
1781 print "OPV($1)\n" if ($dbg_values > 1);
1782 if ($type eq 'V') {
1783 $variant = 'B';
1784 } else {
1785 $variant = 'U';
1786 }
1787
1788 substr($var, length($res), 1, $variant);
1789 $type = 'N';
1790
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001791 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001792 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793 if ($1 ne '++' && $1 ne '--') {
1794 $type = 'N';
1795 }
1796
1797 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001798 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001799 }
1800 if (defined $1) {
1801 $cur = substr($cur, length($1));
1802 $res .= $type x length($1);
1803 }
1804 }
1805
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001806 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001807}
1808
Andy Whitcroft8905a672007-11-28 16:21:06 -08001809sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001810 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001811 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001812 ^(?:
1813 $Modifier|
1814 $Storage|
1815 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001816 DEFINE_\S+
1817 )$|
1818 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001819 goto|
1820 return|
1821 case|
1822 else|
1823 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001824 do|
1825 \#|
1826 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001827 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001828 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001829 )}x;
1830 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1831 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001832 # Check for modifiers.
1833 $possible =~ s/\s*$Storage\s*//g;
1834 $possible =~ s/\s*$Sparse\s*//g;
1835 if ($possible =~ /^\s*$/) {
1836
1837 } elsif ($possible =~ /\s/) {
1838 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001839 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001840 if ($modifier !~ $notPermitted) {
1841 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001842 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001843 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001844 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001845
1846 } else {
1847 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001848 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001849 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001850 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001851 } else {
1852 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001853 }
1854}
1855
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001856my $prefix = '';
1857
Joe Perches000d1cc12011-07-25 17:13:25 -07001858sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001859 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001860
Joe Perchescbec18a2014-04-03 14:49:19 -07001861 return defined $use_type{$type} if (scalar keys %use_type > 0);
1862
1863 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001864}
1865
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001866sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001867 my ($level, $type, $msg) = @_;
1868
1869 if (!show_type($type) ||
1870 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001871 return 0;
1872 }
Joe Perches57230292015-06-25 15:03:03 -07001873 my $output = '';
1874 if (-t STDOUT && $color) {
1875 if ($level eq 'ERROR') {
1876 $output .= RED;
1877 } elsif ($level eq 'WARNING') {
1878 $output .= YELLOW;
1879 } else {
1880 $output .= GREEN;
1881 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001882 }
Joe Perches57230292015-06-25 15:03:03 -07001883 $output .= $prefix . $level . ':';
1884 if ($show_types) {
1885 $output .= BLUE if (-t STDOUT && $color);
1886 $output .= "$type:";
1887 }
1888 $output .= RESET if (-t STDOUT && $color);
1889 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001890
1891 if ($showfile) {
1892 my @lines = split("\n", $output, -1);
1893 splice(@lines, 1, 1);
1894 $output = join("\n", @lines);
1895 }
Joe Perches57230292015-06-25 15:03:03 -07001896 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001897
Joe Perches57230292015-06-25 15:03:03 -07001898 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001899
1900 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001901}
Joe Perchescbec18a2014-04-03 14:49:19 -07001902
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001903sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001904 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001905}
Joe Perches000d1cc12011-07-25 17:13:25 -07001906
Joe Perchesd752fcc2014-08-06 16:11:05 -07001907sub fixup_current_range {
1908 my ($lineRef, $offset, $length) = @_;
1909
1910 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1911 my $o = $1;
1912 my $l = $2;
1913 my $no = $o + $offset;
1914 my $nl = $l + $length;
1915 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1916 }
1917}
1918
1919sub fix_inserted_deleted_lines {
1920 my ($linesRef, $insertedRef, $deletedRef) = @_;
1921
1922 my $range_last_linenr = 0;
1923 my $delta_offset = 0;
1924
1925 my $old_linenr = 0;
1926 my $new_linenr = 0;
1927
1928 my $next_insert = 0;
1929 my $next_delete = 0;
1930
1931 my @lines = ();
1932
1933 my $inserted = @{$insertedRef}[$next_insert++];
1934 my $deleted = @{$deletedRef}[$next_delete++];
1935
1936 foreach my $old_line (@{$linesRef}) {
1937 my $save_line = 1;
1938 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001939 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001940 $delta_offset = 0;
1941 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1942 $range_last_linenr = $new_linenr;
1943 fixup_current_range(\$line, $delta_offset, 0);
1944 }
1945
1946 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1947 $deleted = @{$deletedRef}[$next_delete++];
1948 $save_line = 0;
1949 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1950 }
1951
1952 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1953 push(@lines, ${$inserted}{'LINE'});
1954 $inserted = @{$insertedRef}[$next_insert++];
1955 $new_linenr++;
1956 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1957 }
1958
1959 if ($save_line) {
1960 push(@lines, $line);
1961 $new_linenr++;
1962 }
1963
1964 $old_linenr++;
1965 }
1966
1967 return @lines;
1968}
1969
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001970sub fix_insert_line {
1971 my ($linenr, $line) = @_;
1972
1973 my $inserted = {
1974 LINENR => $linenr,
1975 LINE => $line,
1976 };
1977 push(@fixed_inserted, $inserted);
1978}
1979
1980sub fix_delete_line {
1981 my ($linenr, $line) = @_;
1982
1983 my $deleted = {
1984 LINENR => $linenr,
1985 LINE => $line,
1986 };
1987
1988 push(@fixed_deleted, $deleted);
1989}
1990
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001991sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001992 my ($type, $msg) = @_;
1993
1994 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995 our $clean = 0;
1996 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001997 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001998 }
Joe Perches3705ce52013-07-03 15:05:31 -07001999 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002000}
2001sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002002 my ($type, $msg) = @_;
2003
2004 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002005 our $clean = 0;
2006 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002007 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002008 }
Joe Perches3705ce52013-07-03 15:05:31 -07002009 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002010}
2011sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002012 my ($type, $msg) = @_;
2013
2014 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002015 our $clean = 0;
2016 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002017 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002018 }
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002020}
2021
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002022sub check_absolute_file {
2023 my ($absolute, $herecurr) = @_;
2024 my $file = $absolute;
2025
2026 ##print "absolute<$absolute>\n";
2027
2028 # See if any suffix of this path is a path within the tree.
2029 while ($file =~ s@^[^/]*/@@) {
2030 if (-f "$root/$file") {
2031 ##print "file<$file>\n";
2032 last;
2033 }
2034 }
2035 if (! -f _) {
2036 return 0;
2037 }
2038
2039 # It is, so see if the prefix is acceptable.
2040 my $prefix = $absolute;
2041 substr($prefix, -length($file)) = '';
2042
2043 ##print "prefix<$prefix>\n";
2044 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002045 WARN("USE_RELATIVE_PATH",
2046 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002047 }
2048}
2049
Joe Perches3705ce52013-07-03 15:05:31 -07002050sub trim {
2051 my ($string) = @_;
2052
Joe Perchesb34c6482013-09-11 14:24:01 -07002053 $string =~ s/^\s+|\s+$//g;
2054
2055 return $string;
2056}
2057
2058sub ltrim {
2059 my ($string) = @_;
2060
2061 $string =~ s/^\s+//;
2062
2063 return $string;
2064}
2065
2066sub rtrim {
2067 my ($string) = @_;
2068
2069 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002070
2071 return $string;
2072}
2073
Joe Perches52ea8502013-11-12 15:10:09 -08002074sub string_find_replace {
2075 my ($string, $find, $replace) = @_;
2076
2077 $string =~ s/$find/$replace/g;
2078
2079 return $string;
2080}
2081
Joe Perches3705ce52013-07-03 15:05:31 -07002082sub tabify {
2083 my ($leading) = @_;
2084
2085 my $source_indent = 8;
2086 my $max_spaces_before_tab = $source_indent - 1;
2087 my $spaces_to_tab = " " x $source_indent;
2088
2089 #convert leading spaces to tabs
2090 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2091 #Remove spaces before a tab
2092 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2093
2094 return "$leading";
2095}
2096
Gregory Beanf07523e2011-07-06 15:36:58 -07002097sub cleanup_continuation_headers {
2098 # Collapse any header-continuation lines into a single line so they
2099 # can be parsed meaningfully, as the parser only has one line
2100 # of context to work with.
2101 my $again;
2102 do {
2103 $again = 0;
2104 foreach my $n (0 .. scalar(@rawlines) - 2) {
2105 if ($rawlines[$n]=~/^\s*$/) {
2106 # A blank line means there's no more chance
2107 # of finding headers. Shortcut to done.
2108 return;
2109 }
2110 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2111 $rawlines[$n+1]=~/^\s+/) {
2112 # Continuation header. Collapse it.
2113 my $line = splice @rawlines, $n+1, 1;
2114 $line=~s/^\s+/ /;
2115 $rawlines[$n] .= $line;
2116 # We've 'destabilized' the list, so restart.
2117 $again = 1;
2118 last;
2119 }
2120 }
2121 } while ($again);
2122}
2123
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002124sub pos_last_openparen {
2125 my ($line) = @_;
2126
2127 my $pos = 0;
2128
2129 my $opens = $line =~ tr/\(/\(/;
2130 my $closes = $line =~ tr/\)/\)/;
2131
2132 my $last_openparen = 0;
2133
2134 if (($opens == 0) || ($closes >= $opens)) {
2135 return -1;
2136 }
2137
2138 my $len = length($line);
2139
2140 for ($pos = 0; $pos < $len; $pos++) {
2141 my $string = substr($line, $pos);
2142 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2143 $pos += length($1) - 1;
2144 } elsif (substr($line, $pos, 1) eq '(') {
2145 $last_openparen = $pos;
2146 } elsif (index($string, '(') == -1) {
2147 last;
2148 }
2149 }
2150
Joe Perches91cb5192014-04-03 14:49:32 -07002151 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002152}
2153
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002154sub process {
2155 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002156
2157 my $linenr=0;
2158 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002159 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002160 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002162 my $subjectline="";
2163 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002164
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002165 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166 my $indent;
2167 my $previndent=0;
2168 my $stashindent=0;
2169
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002170 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002171 my $signoff = 0;
2172 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002173 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002174 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002175 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002176 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002177 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002178 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002179 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002180 my $non_utf8_charset = 0;
2181
Joe Perches365dd4e2014-08-06 16:10:42 -07002182 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002183 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002184
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002185 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002186 our $cnt_lines = 0;
2187 our $cnt_error = 0;
2188 our $cnt_warn = 0;
2189 our $cnt_chk = 0;
2190
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002191 # Trace the real file/line as we go.
2192 my $realfile = '';
2193 my $realline = 0;
2194 my $realcnt = 0;
2195 my $here = '';
2196 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002197 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002198 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002199 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002200
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002201 my $prev_values = 'E';
2202
2203 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002204 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002205 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002206 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002207 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002208
Joe Perches7e51f192013-09-11 14:23:57 -07002209 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002210
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002211 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002212 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002214 my @setup_docs = ();
2215 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002216
Joe Perchesd8b07712013-11-12 15:10:06 -08002217 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002218 my $shorttext = BEFORE_SHORTTEXT;
2219 my $shorttext_exspc = 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002220 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002221
Andy Whitcroft773647a2008-03-28 14:15:58 -07002222 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002223 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002224 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002225
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002226 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002227 $linenr++;
2228 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002229
Joe Perches3705ce52013-07-03 15:05:31 -07002230 push(@fixed, $rawline) if ($fix);
2231
Andy Whitcroft773647a2008-03-28 14:15:58 -07002232 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002233 $setup_docs = 0;
2234 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2235 $setup_docs = 1;
2236 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002237 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002238 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2240 $realline=$1-1;
2241 if (defined $2) {
2242 $realcnt=$3+1;
2243 } else {
2244 $realcnt=1+1;
2245 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002246 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002247
2248 # Guestimate if this is a continuing comment. Run
2249 # the context looking for a comment "edge". If this
2250 # edge is a close comment then we must be in a comment
2251 # at context start.
2252 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002253 my $cnt = $realcnt;
2254 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2255 next if (defined $rawlines[$ln - 1] &&
2256 $rawlines[$ln - 1] =~ /^-/);
2257 $cnt--;
2258 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002259 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002260 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2261 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2262 ($edge) = $1;
2263 last;
2264 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002265 }
2266 if (defined $edge && $edge eq '*/') {
2267 $in_comment = 1;
2268 }
2269
2270 # Guestimate if this is a continuing comment. If this
2271 # is the start of a diff block and this line starts
2272 # ' *' then it is very likely a comment.
2273 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002274 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002275 {
2276 $in_comment = 1;
2277 }
2278
2279 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2280 sanitise_line_reset($in_comment);
2281
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002282 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002283 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 $line = sanitise_line($rawline);
2286 }
2287 push(@lines, $line);
2288
2289 if ($realcnt > 1) {
2290 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2291 } else {
2292 $realcnt = 0;
2293 }
2294
2295 #print "==>$rawline\n";
2296 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002297
2298 if ($setup_docs && $line =~ /^\+/) {
2299 push(@setup_docs, $line);
2300 }
2301 }
2302
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002303 $prefix = '';
2304
Andy Whitcroft773647a2008-03-28 14:15:58 -07002305 $realcnt = 0;
2306 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002307 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002308 foreach my $line (@lines) {
2309 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002310 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002311 my $sline = $line; #copy of $line
2312 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002313
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002314 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002315
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002316#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002317 if (!$in_commit_log &&
2318 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002319 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002320 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002321 $realline=$1-1;
2322 if (defined $2) {
2323 $realcnt=$3+1;
2324 } else {
2325 $realcnt=1+1;
2326 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002327 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002328 $prev_values = 'E';
2329
Andy Whitcroft773647a2008-03-28 14:15:58 -07002330 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002331 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002332 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002333 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002334 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002335
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002336# track the line number as we move through the hunk, note that
2337# new versions of GNU diff omit the leading space on completely
2338# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002339 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002340 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002341 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002343 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002344 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002345
2346 # Track the previous line.
2347 ($prevline, $stashline) = ($stashline, $line);
2348 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002349 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2350
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002352
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002353 } elsif ($realcnt == 1) {
2354 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002355 }
2356
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002357 my $hunk_line = ($realcnt != 0);
2358
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002359 $here = "#$linenr: " if (!$file);
2360 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002361
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002362 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002364 if ($line =~ /^diff --git.*?(\S+)$/) {
2365 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002366 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002367 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002368 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002369 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002370 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002371 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002372 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002373
2374 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002375 if (!$file && $tree && $p1_prefix ne '' &&
2376 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002377 WARN("PATCH_PREFIX",
2378 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002379 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002380
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002381 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002382 ERROR("MODIFIED_INCLUDE_ASM",
2383 "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 -07002384 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002385 $found_file = 1;
2386 }
2387
Joe Perches34d88152015-06-25 15:03:05 -07002388#make up the handle for any error we report on this line
2389 if ($showfile) {
2390 $prefix = "$realfile:$realline: "
2391 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002392 if ($file) {
2393 $prefix = "$filename:$realline: ";
2394 } else {
2395 $prefix = "$filename:$linenr: ";
2396 }
Joe Perches34d88152015-06-25 15:03:05 -07002397 }
2398
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002399 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002400 if (is_maintained_obsolete($realfile)) {
2401 WARN("OBSOLETE",
2402 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2403 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002404 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002405 $check = 1;
2406 } else {
2407 $check = $check_orig;
2408 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002409 next;
2410 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002411 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002412
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002413 my $hereline = "$here\n$rawline\n";
2414 my $herecurr = "$here\n$rawline\n";
2415 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002416
David Keitel72bf3e82016-03-23 20:52:44 -07002417 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002418 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2419 # the subject line was just processed,
2420 # a blank line must be next
2421 WARN("NONBLANK_AFTER_SUMMARY",
2422 "non-blank line after summary line\n" . $herecurr);
2423 $shorttext = IN_SHORTTEXT;
2424 # this non-blank line may or may not be commit text -
2425 # a warning has been generated so assume it is commit
2426 # text and move on
2427 $commit_text_present = 1;
2428 # fall through and treat this line as IN_SHORTTEXT
2429 }
David Keitel72bf3e82016-03-23 20:52:44 -07002430 if ($shorttext == IN_SHORTTEXT) {
2431 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002432 if ($commit_text_present == 0) {
2433 WARN("NO_COMMIT_TEXT",
2434 "please add commit text explaining " .
2435 "*why* the change is needed\n" .
2436 $herecurr);
2437 }
David Keitel72bf3e82016-03-23 20:52:44 -07002438 $shorttext = AFTER_SHORTTEXT;
2439 } elsif (length($line) > (SHORTTEXT_LIMIT +
2440 $shorttext_exspc)
2441 && $line !~ /^:([0-7]{6}\s){2}
2442 ([[:xdigit:]]+\.*
2443 \s){2}\w+\s\w+/xms) {
2444 WARN("LONG_COMMIT_TEXT",
2445 "commit text line over " .
2446 SHORTTEXT_LIMIT .
2447 " characters\n" . $herecurr);
Steve Muckled360fe42012-03-09 11:15:24 -08002448 } elsif ($line=~/^\s*change-id:/i ||
2449 $line=~/^\s*signed-off-by:/i ||
2450 $line=~/^\s*crs-fixed:/i ||
2451 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002452 # this is a tag, there must be commit
2453 # text by now
2454 if ($commit_text_present == 0) {
2455 WARN("NO_COMMIT_TEXT",
2456 "please add commit text explaining " .
2457 "*why* the change is needed\n" .
2458 $herecurr);
2459 # prevent duplicate warnings
2460 $commit_text_present = 1;
2461 }
2462 } elsif ($line=~/\S/) {
2463 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002464 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002465 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2466 # case of non-blank line in this state handled above
2467 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002468 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002469# The Subject line doesn't have to be the last header in the patch.
2470# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2471# Per RFC5322, continuation lines must be folded, so any left-justified
2472# text which looks like a header is definitely a header.
2473 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2474 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002475 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002476 if (length($line) != 0) {
2477 WARN("NONBLANK_AFTER_SUMMARY",
2478 "non-blank line after " .
2479 "summary line\n" .
2480 $sublinenr . $here .
2481 "\n" . $subjectline .
2482 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002483 # this non-blank line may or may not
2484 # be commit text - a warning has been
2485 # generated so assume it is commit
2486 # text and move on
2487 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002488 }
David Keitele288d232016-03-23 20:57:21 -07002489 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002490 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002491 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002492 # This is the subject line. Go to
2493 # CHECK_NEXT_SHORTTEXT to wait for the commit
2494 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002495 $shorttext = CHECK_NEXT_SHORTTEXT;
2496 $subjectline = $line;
2497 $sublinenr = "#$linenr & ";
2498# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002499 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002500 WARN("LONG_SUMMARY_LINE",
2501 "summary line over " .
2502 SHORTTEXT_LIMIT .
2503 " characters\n" . $herecurr);
2504 }
2505 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002506 # Indented format, this must be the summary
2507 # line (i.e. git show). There will be no more
2508 # headers so we are now in the shorttext.
2509 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002510 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002511 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002512 WARN("LONG_SUMMARY_LINE",
2513 "summary line over " .
2514 SHORTTEXT_LIMIT .
2515 " characters\n" . $herecurr);
2516 }
2517 }
2518 }
2519
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002520 $cnt_lines++ if ($realcnt != 0);
2521
Joe Perchese518e9a2015-06-25 15:03:27 -07002522# Check if the commit log has what seems like a diff which can confuse patch
2523 if ($in_commit_log && !$commit_log_has_diff &&
2524 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2525 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2526 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2527 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2528 ERROR("DIFF_IN_COMMIT_MSG",
2529 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2530 $commit_log_has_diff = 1;
2531 }
2532
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002533# Check for incorrect file permissions
2534 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2535 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002536 if ($realfile !~ m@scripts/@ &&
2537 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002538 ERROR("EXECUTE_PERMISSIONS",
2539 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002540 }
2541 }
2542
Joe Perches20112472011-07-25 17:13:23 -07002543# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002544 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002545 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002546 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002547 }
2548
Joe Perchese0d975b2014-12-10 15:51:49 -08002549# Check if MAINTAINERS is being updated. If so, there's probably no need to
2550# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2551 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2552 $reported_maintainer_file = 1;
2553 }
2554
Joe Perches20112472011-07-25 17:13:23 -07002555# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002556 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002557 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002558 my $space_before = $1;
2559 my $sign_off = $2;
2560 my $space_after = $3;
2561 my $email = $4;
2562 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2563
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002564 if ($sign_off !~ /$signature_tags/) {
2565 WARN("BAD_SIGN_OFF",
2566 "Non-standard signature: $sign_off\n" . $herecurr);
2567 }
Joe Perches20112472011-07-25 17:13:23 -07002568 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002569 if (WARN("BAD_SIGN_OFF",
2570 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2571 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002572 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002573 "$ucfirst_sign_off $email";
2574 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002575 }
Joe Perches20112472011-07-25 17:13:23 -07002576 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002577 if (WARN("BAD_SIGN_OFF",
2578 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002580 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002581 "$ucfirst_sign_off $email";
2582 }
2583
Joe Perches20112472011-07-25 17:13:23 -07002584 }
2585 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002586 if (WARN("BAD_SIGN_OFF",
2587 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2588 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002589 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002590 "$ucfirst_sign_off $email";
2591 }
Joe Perches20112472011-07-25 17:13:23 -07002592 }
2593
2594 my ($email_name, $email_address, $comment) = parse_email($email);
2595 my $suggested_email = format_email(($email_name, $email_address));
2596 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002597 ERROR("BAD_SIGN_OFF",
2598 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002599 } else {
2600 my $dequoted = $suggested_email;
2601 $dequoted =~ s/^"//;
2602 $dequoted =~ s/" </ </;
2603 # Don't force email to have quotes
2604 # Allow just an angle bracketed address
2605 if ("$dequoted$comment" ne $email &&
2606 "<$email_address>$comment" ne $email &&
2607 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002608 WARN("BAD_SIGN_OFF",
2609 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002610 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002611 }
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002612 if ($chk_author && $line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002613 WARN("BAD_SIGN_OFF",
2614 "invalid Signed-off-by identity\n" . $line );
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}) {
2622 WARN("BAD_SIGN_OFF",
2623 "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 &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002682 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002683 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002684 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2685 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002686 my $init_char = "c";
2687 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002688 my $short = 1;
2689 my $long = 0;
2690 my $case = 1;
2691 my $space = 1;
2692 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002693 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002694 my $id = '0123456789ab';
2695 my $orig_desc = "commit description";
2696 my $description = "";
2697
Joe Perchesfe043ea2015-09-09 15:37:25 -07002698 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2699 $init_char = $1;
2700 $orig_commit = lc($2);
2701 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2702 $orig_commit = lc($1);
2703 }
2704
Joe Perches0d7835f2015-02-13 14:38:35 -08002705 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2706 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2707 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2708 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2709 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2710 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002711 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002712 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2713 defined $rawlines[$linenr] &&
2714 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2715 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002716 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002717 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2718 defined $rawlines[$linenr] &&
2719 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2720 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2721 $orig_desc = $1;
2722 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2723 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002724 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002725 }
2726
2727 ($id, $description) = git_commit_info($orig_commit,
2728 $id, $orig_desc);
2729
Joe Perches19c146a2015-02-13 14:39:00 -08002730 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002731 ERROR("GIT_COMMIT_ID",
2732 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2733 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002734 }
2735
Joe Perches13f19372014-08-06 16:10:59 -07002736# Check for added, moved or deleted files
2737 if (!$reported_maintainer_file && !$in_commit_log &&
2738 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2739 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2740 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2741 (defined($1) || defined($2))))) {
2742 $reported_maintainer_file = 1;
2743 WARN("FILE_PATH_CHANGES",
2744 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2745 }
2746
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002747#check the patch for invalid author credentials
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002748 if ($chk_author && $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002749 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2750 }
2751
Andy Whitcroft00df3442007-06-08 13:47:06 -07002752# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002753 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002754 ERROR("CORRUPTED_PATCH",
2755 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002756 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002757 }
2758
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002759# Check for absolute kernel paths.
2760 if ($tree) {
2761 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2762 my $file = $1;
2763
2764 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2765 check_absolute_file($1, $herecurr)) {
2766 #
2767 } else {
2768 check_absolute_file($file, $herecurr);
2769 }
2770 }
2771 }
2772
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002773# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2774 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002775 $rawline !~ m/^$UTF8*$/) {
2776 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2777
2778 my $blank = copy_spacing($rawline);
2779 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2780 my $hereptr = "$hereline$ptr\n";
2781
Joe Perches34d99212011-07-25 17:13:26 -07002782 CHK("INVALID_UTF8",
2783 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002784 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002785
Joe Perches15662b32011-10-31 17:13:12 -07002786# Check if it's the start of a commit log
2787# (not a header line and we haven't seen the patch filename)
2788 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002789 !($rawline =~ /^\s+\S/ ||
2790 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002791 $in_header_lines = 0;
2792 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002793 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002794 }
2795
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002796# Check if there is UTF-8 in a commit log when a mail header has explicitly
2797# declined it, i.e defined some charset where it is missing.
2798 if ($in_header_lines &&
2799 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2800 $1 !~ /utf-8/i) {
2801 $non_utf8_charset = 1;
2802 }
2803
2804 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002805 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002806 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002807 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2808 }
2809
Kees Cook66b47b42014-10-13 15:51:57 -07002810# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002811 if (defined($misspellings) &&
2812 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002813 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002814 my $typo = $1;
2815 my $typo_fix = $spelling_fix{lc($typo)};
2816 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2817 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2818 my $msg_type = \&WARN;
2819 $msg_type = \&CHK if ($file);
2820 if (&{$msg_type}("TYPO_SPELLING",
2821 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2822 $fix) {
2823 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2824 }
2825 }
2826 }
2827
Andy Whitcroft306708542008-10-15 22:02:28 -07002828# ignore non-hunk lines and lines being removed
2829 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002830
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002831#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002832 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002833 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002834 if (ERROR("DOS_LINE_ENDINGS",
2835 "DOS line endings\n" . $herevet) &&
2836 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002837 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002838 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002839 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2840 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002841 if (ERROR("TRAILING_WHITESPACE",
2842 "trailing whitespace\n" . $herevet) &&
2843 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002844 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002845 }
2846
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002847 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002848 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002849
Josh Triplett4783f892013-11-12 15:10:12 -08002850# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002851 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002852 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2853 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002854 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2855 my $msg_type = \&ERROR;
2856 $msg_type = \&CHK if ($file);
2857 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002858 "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 -08002859 }
2860
Andi Kleen33549572010-05-24 14:33:29 -07002861# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002862# Only applies when adding the entry originally, after that we do not have
2863# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002864 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002865 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002866 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002867 my $cnt = $realcnt;
2868 my $ln = $linenr + 1;
2869 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002870 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002871 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002872 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002873 $f = $lines[$ln - 1];
2874 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2875 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002876
2877 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002878 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002879
Joe Perches8d73e0e2014-08-06 16:10:46 -07002880 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002881 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002882 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002883 $length = -1;
2884 }
2885
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002886 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002887 $f =~ s/#.*//;
2888 $f =~ s/^\s+//;
2889 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002890 if ($f =~ /^\s*config\s/) {
2891 $is_end = 1;
2892 last;
2893 }
Andi Kleen33549572010-05-24 14:33:29 -07002894 $length++;
2895 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002896 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2897 WARN("CONFIG_DESCRIPTION",
2898 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2899 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002900 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002901 }
2902
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002903# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2904 if ($realfile =~ /Kconfig/ &&
2905 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2906 WARN("CONFIG_EXPERIMENTAL",
2907 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2908 }
2909
Christoph Jaeger327953e2015-02-13 14:38:29 -08002910# discourage the use of boolean for type definition attributes of Kconfig options
2911 if ($realfile =~ /Kconfig/ &&
2912 $line =~ /^\+\s*\bboolean\b/) {
2913 WARN("CONFIG_TYPE_BOOLEAN",
2914 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2915 }
2916
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002917 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2918 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2919 my $flag = $1;
2920 my $replacement = {
2921 'EXTRA_AFLAGS' => 'asflags-y',
2922 'EXTRA_CFLAGS' => 'ccflags-y',
2923 'EXTRA_CPPFLAGS' => 'cppflags-y',
2924 'EXTRA_LDFLAGS' => 'ldflags-y',
2925 };
2926
2927 WARN("DEPRECATED_VARIABLE",
2928 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2929 }
2930
Rob Herringbff5da42014-01-23 15:54:51 -08002931# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002932 if (defined $root &&
2933 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2934 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2935
Rob Herringbff5da42014-01-23 15:54:51 -08002936 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2937
Florian Vaussardcc933192014-04-03 14:49:27 -07002938 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2939 my $vp_file = $dt_path . "vendor-prefixes.txt";
2940
Rob Herringbff5da42014-01-23 15:54:51 -08002941 foreach my $compat (@compats) {
2942 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002943 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2944 my $compat3 = $compat;
2945 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2946 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002947 if ( $? >> 8 ) {
2948 WARN("UNDOCUMENTED_DT_STRING",
2949 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2950 }
2951
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002952 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2953 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002954 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002955 if ( $? >> 8 ) {
2956 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002957 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002958 }
2959 }
2960 }
2961
Andy Whitcroft5368df22008-10-15 22:02:27 -07002962# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002963 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002964
Joe Perches47e0c882015-06-25 15:02:57 -07002965# line length limit (with some exclusions)
2966#
2967# There are a few types of lines that may extend beyond $max_line_length:
2968# logging functions like pr_info that end in a string
2969# lines with a single string
2970# #defines that are a single string
2971#
2972# There are 3 different line length message types:
2973# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2974# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2975# LONG_LINE all other lines longer than $max_line_length
2976#
2977# if LONG_LINE is ignored, the other 2 types are also ignored
2978#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002979 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002980 my $msg_type = "LONG_LINE";
2981
2982 # Check the allowed long line types first
2983
2984 # logging functions that end in a string that starts
2985 # before $max_line_length
2986 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2987 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2988 $msg_type = "";
2989
2990 # lines with only strings (w/ possible termination)
2991 # #defines with only strings
2992 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2993 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2994 $msg_type = "";
2995
Joe Perchesd560a5f2016-08-02 14:04:31 -07002996 # EFI_GUID is another special case
2997 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2998 $msg_type = "";
2999
Joe Perches47e0c882015-06-25 15:02:57 -07003000 # Otherwise set the alternate message types
3001
3002 # a comment starts before $max_line_length
3003 } elsif ($line =~ /($;[\s$;]*)$/ &&
3004 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3005 $msg_type = "LONG_LINE_COMMENT"
3006
3007 # a quoted string starts before $max_line_length
3008 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3009 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3010 $msg_type = "LONG_LINE_STRING"
3011 }
3012
3013 if ($msg_type ne "" &&
3014 (show_type("LONG_LINE") || show_type($msg_type))) {
3015 WARN($msg_type,
3016 "line over $max_line_length characters\n" . $herecurr);
3017 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003018 }
3019
Andy Whitcroft8905a672007-11-28 16:21:06 -08003020# check for adding lines without a newline.
3021 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003022 WARN("MISSING_EOF_NEWLINE",
3023 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003024 }
3025
Mike Frysinger42e41c52009-09-21 17:04:40 -07003026# Blackfin: use hi/lo macros
3027 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3028 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3029 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003030 ERROR("LO_MACRO",
3031 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003032 }
3033 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3034 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003035 ERROR("HI_MACRO",
3036 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003037 }
3038 }
3039
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003040# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003041 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003042
3043# at the beginning of a line any tabs must come first and anything
3044# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003045 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3046 $rawline =~ /^\+\s* \s*/) {
3047 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003048 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003049 if (ERROR("CODE_INDENT",
3050 "code indent should use tabs where possible\n" . $herevet) &&
3051 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003052 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003053 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003054 }
3055
Alberto Panizzo08e44362010-03-05 13:43:54 -08003056# check for space before tabs.
3057 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3058 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003059 if (WARN("SPACE_BEFORE_TAB",
3060 "please, no space before tabs\n" . $herevet) &&
3061 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003062 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003063 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003064 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003065 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003066 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003067 }
3068
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003069# check for && or || at the start of a line
3070 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3071 CHK("LOGICAL_CONTINUATIONS",
3072 "Logical continuations should be on the previous line\n" . $hereprev);
3073 }
3074
Joe Perchesa91e8992016-05-20 17:04:05 -07003075# check indentation starts on a tab stop
3076 if ($^V && $^V ge 5.10.0 &&
3077 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3078 my $indent = length($1);
3079 if ($indent % 8) {
3080 if (WARN("TABSTOP",
3081 "Statements should start on a tabstop\n" . $herecurr) &&
3082 $fix) {
3083 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3084 }
3085 }
3086 }
3087
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003088# check multi-line statement indentation matches previous line
3089 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003090 $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 -07003091 $prevline =~ /^\+(\t*)(.*)$/;
3092 my $oldindent = $1;
3093 my $rest = $2;
3094
3095 my $pos = pos_last_openparen($rest);
3096 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003097 $line =~ /^(\+| )([ \t]*)/;
3098 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003099
3100 my $goodtabindent = $oldindent .
3101 "\t" x ($pos / 8) .
3102 " " x ($pos % 8);
3103 my $goodspaceindent = $oldindent . " " x $pos;
3104
3105 if ($newindent ne $goodtabindent &&
3106 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003107
3108 if (CHK("PARENTHESIS_ALIGNMENT",
3109 "Alignment should match open parenthesis\n" . $hereprev) &&
3110 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003111 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003112 s/^\+[ \t]*/\+$goodtabindent/;
3113 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003114 }
3115 }
3116 }
3117
Joe Perches6ab3a972015-04-16 12:44:05 -07003118# check for space after cast like "(int) foo" or "(struct foo) bar"
3119# avoid checking a few false positives:
3120# "sizeof(<type>)" or "__alignof__(<type>)"
3121# function pointer declarations like "(*foo)(int) = bar;"
3122# structure definitions like "(struct foo) { 0 };"
3123# multiline macros that define functions
3124# known attributes or the __attribute__ keyword
3125 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3126 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003127 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003128 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003129 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003130 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003131 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003132 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003133 }
3134
Joe Perches86406b12015-09-09 15:37:41 -07003135# Block comment styles
3136# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003137 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003138 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003139 $rawline =~ /^\+[ \t]*\*/ &&
3140 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003141 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3142 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3143 }
3144
Joe Perches86406b12015-09-09 15:37:41 -07003145# Block comments use * on subsequent lines
3146 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3147 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003148 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003149 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003150 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003151 WARN("BLOCK_COMMENT_STYLE",
3152 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003153 }
3154
Joe Perches86406b12015-09-09 15:37:41 -07003155# Block comments use */ on trailing lines
3156 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003157 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3158 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3159 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003160 WARN("BLOCK_COMMENT_STYLE",
3161 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003162 }
3163
Joe Perches08eb9b82016-10-11 13:51:50 -07003164# Block comment * alignment
3165 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003166 $line =~ /^\+[ \t]*$;/ && #leading comment
3167 $rawline =~ /^\+[ \t]*\*/ && #leading *
3168 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003169 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003170 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3171 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003172 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003173 if (defined($1)) {
3174 $oldindent = expand_tabs($1);
3175 } else {
3176 $prevrawline =~ m@^\+(.*/?)\*@;
3177 $oldindent = expand_tabs($1);
3178 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003179 $rawline =~ m@^\+([ \t]*)\*@;
3180 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003181 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003182 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003183 WARN("BLOCK_COMMENT_STYLE",
3184 "Block comments should align the * on each line\n" . $hereprev);
3185 }
3186 }
3187
Joe Perches7f619192014-08-06 16:10:39 -07003188# check for missing blank lines after struct/union declarations
3189# with exceptions for various attributes and macros
3190 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3191 $line =~ /^\+/ &&
3192 !($line =~ /^\+\s*$/ ||
3193 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3194 $line =~ /^\+\s*MODULE_/i ||
3195 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3196 $line =~ /^\+[a-z_]*init/ ||
3197 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3198 $line =~ /^\+\s*DECLARE/ ||
3199 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003200 if (CHK("LINE_SPACING",
3201 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3202 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003203 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003204 }
Joe Perches7f619192014-08-06 16:10:39 -07003205 }
3206
Joe Perches365dd4e2014-08-06 16:10:42 -07003207# check for multiple consecutive blank lines
3208 if ($prevline =~ /^[\+ ]\s*$/ &&
3209 $line =~ /^\+\s*$/ &&
3210 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003211 if (CHK("LINE_SPACING",
3212 "Please don't use multiple blank lines\n" . $hereprev) &&
3213 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003214 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003215 }
3216
Joe Perches365dd4e2014-08-06 16:10:42 -07003217 $last_blank_line = $linenr;
3218 }
3219
Joe Perches3b617e32014-04-03 14:49:28 -07003220# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003221 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3222 # actual declarations
3223 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003224 # function pointer declarations
3225 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003226 # foo bar; where foo is some local typedef or #define
3227 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3228 # known declaration macros
3229 $prevline =~ /^\+\s+$declaration_macros/) &&
3230 # for "else if" which can look like "$Ident $Ident"
3231 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3232 # other possible extensions of declaration lines
3233 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3234 # not starting a section or a macro "\" extended line
3235 $prevline =~ /(?:\{\s*|\\)$/) &&
3236 # looks like a declaration
3237 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003238 # function pointer declarations
3239 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003240 # foo bar; where foo is some local typedef or #define
3241 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3242 # known declaration macros
3243 $sline =~ /^\+\s+$declaration_macros/ ||
3244 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003245 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003246 # start or end of block or continuation of declaration
3247 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3248 # bitfield continuation
3249 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3250 # other possible extensions of declaration lines
3251 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3252 # indentation of previous and current line are the same
3253 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003254 if (WARN("LINE_SPACING",
3255 "Missing a blank line after declarations\n" . $hereprev) &&
3256 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003257 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003258 }
Joe Perches3b617e32014-04-03 14:49:28 -07003259 }
3260
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003261# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003262# Exceptions:
3263# 1) within comments
3264# 2) indented preprocessor commands
3265# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003266 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003267 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003268 if (WARN("LEADING_SPACE",
3269 "please, no spaces at the start of a line\n" . $herevet) &&
3270 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003271 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003272 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003273 }
3274
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003275# check we are in a valid C source file if not then ignore this hunk
3276 next if ($realfile !~ /\.(h|c)$/);
3277
Joe Perches032a4c02014-08-06 16:10:29 -07003278# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003279# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003280# if the previous line is a break or return and is indented 1 tab more...
3281 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3282 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003283 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3284 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3285 defined $lines[$linenr] &&
3286 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003287 WARN("UNNECESSARY_ELSE",
3288 "else is not generally useful after a break or return\n" . $hereprev);
3289 }
3290 }
3291
Joe Perchesc00df192014-08-06 16:11:01 -07003292# check indentation of a line with a break;
3293# if the previous line is a goto or return and is indented the same # of tabs
3294 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3295 my $tabs = $1;
3296 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3297 WARN("UNNECESSARY_BREAK",
3298 "break is not useful after a goto or return\n" . $hereprev);
3299 }
3300 }
3301
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003302# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3303 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3304 WARN("CONFIG_EXPERIMENTAL",
3305 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3306 }
3307
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003308# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003309 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003310 WARN("CVS_KEYWORD",
3311 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003312 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003313
Mike Frysinger42e41c52009-09-21 17:04:40 -07003314# Blackfin: don't use __builtin_bfin_[cs]sync
3315 if ($line =~ /__builtin_bfin_csync/) {
3316 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003317 ERROR("CSYNC",
3318 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003319 }
3320 if ($line =~ /__builtin_bfin_ssync/) {
3321 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003322 ERROR("SSYNC",
3323 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003324 }
3325
Joe Perches56e77d72013-02-21 16:44:14 -08003326# check for old HOTPLUG __dev<foo> section markings
3327 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3328 WARN("HOTPLUG_SECTION",
3329 "Using $1 is unnecessary\n" . $herecurr);
3330 }
3331
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003332# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003333 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3334 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003335#print "LINE<$line>\n";
3336 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003337 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003338 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003339 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003340 $stat =~ s/\n./\n /g;
3341 $cond =~ s/\n./\n /g;
3342
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003343#print "linenr<$linenr> <$stat>\n";
3344 # If this statement has no statement boundaries within
3345 # it there is no point in retrying a statement scan
3346 # until we hit end of it.
3347 my $frag = $stat; $frag =~ s/;+\s*$//;
3348 if ($frag !~ /(?:{|;)/) {
3349#print "skip<$line_nr_next>\n";
3350 $suppress_statement = $line_nr_next;
3351 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003352
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003353 # Find the real next line.
3354 $realline_next = $line_nr_next;
3355 if (defined $realline_next &&
3356 (!defined $lines[$realline_next - 1] ||
3357 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3358 $realline_next++;
3359 }
3360
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003361 my $s = $stat;
3362 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003363
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003364 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003365 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003366
3367 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003368 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003369
Andy Whitcroft463f2862009-09-21 17:04:34 -07003370 } elsif ($s =~ /^.\s*else\b/s) {
3371
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003372 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003373 } 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 -07003374 my $type = $1;
3375 $type =~ s/\s+/ /g;
3376 possible($type, "A:" . $s);
3377
Andy Whitcroft8905a672007-11-28 16:21:06 -08003378 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003379 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003380 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003381 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003382
3383 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003384 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003385 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003386 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003387
3388 # Check for any sort of function declaration.
3389 # int foo(something bar, other baz);
3390 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003391 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 -08003392 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003393
Andy Whitcroftcf655042008-03-04 14:28:20 -08003394 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003395 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003396 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003397
Andy Whitcroft8905a672007-11-28 16:21:06 -08003398 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003399 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003400
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003401 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402 }
3403 }
3404 }
3405
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003406 }
3407
Andy Whitcroft653d4872007-06-23 17:16:34 -07003408#
3409# Checks which may be anchored in the context.
3410#
3411
3412# Check for switch () and associated case and default
3413# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003414 if ($line=~/\bswitch\s*\(.*\)/) {
3415 my $err = '';
3416 my $sep = '';
3417 my @ctx = ctx_block_outer($linenr, $realcnt);
3418 shift(@ctx);
3419 for my $ctx (@ctx) {
3420 my ($clen, $cindent) = line_stats($ctx);
3421 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3422 $indent != $cindent) {
3423 $err .= "$sep$ctx\n";
3424 $sep = '';
3425 } else {
3426 $sep = "[...]\n";
3427 }
3428 }
3429 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003430 ERROR("SWITCH_CASE_INDENT_LEVEL",
3431 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003432 }
3433 }
3434
3435# if/while/etc brace do not go on next line, unless defining a do while loop,
3436# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003437 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 -07003438 my $pre_ctx = "$1$2";
3439
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003440 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003441
3442 if ($line =~ /^\+\t{6,}/) {
3443 WARN("DEEP_INDENTATION",
3444 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3445 }
3446
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003447 my $ctx_cnt = $realcnt - $#ctx - 1;
3448 my $ctx = join("\n", @ctx);
3449
Andy Whitcroft548596d2008-07-23 21:29:01 -07003450 my $ctx_ln = $linenr;
3451 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003452
Andy Whitcroft548596d2008-07-23 21:29:01 -07003453 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3454 defined $lines[$ctx_ln - 1] &&
3455 $lines[$ctx_ln - 1] =~ /^-/)) {
3456 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3457 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003458 $ctx_ln++;
3459 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003460
Andy Whitcroft53210162008-07-23 21:29:03 -07003461 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3462 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003463
Joe Perchesd752fcc2014-08-06 16:11:05 -07003464 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003465 ERROR("OPEN_BRACE",
3466 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003467 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003468 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003469 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3470 $ctx =~ /\)\s*\;\s*$/ &&
3471 defined $lines[$ctx_ln - 1])
3472 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003473 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3474 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003475 WARN("TRAILING_SEMICOLON",
3476 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003477 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003478 }
3479 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003480 }
3481
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003482# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003483 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 -08003484 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3485 ctx_statement_block($linenr, $realcnt, 0)
3486 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003487 my ($s, $c) = ($stat, $cond);
3488
3489 substr($s, 0, length($c), '');
3490
Joe Perches9f5af482015-09-09 15:37:30 -07003491 # remove inline comments
3492 $s =~ s/$;/ /g;
3493 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003494
3495 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003496 my @newlines = ($c =~ /\n/gs);
3497 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003498
Joe Perches9f5af482015-09-09 15:37:30 -07003499 # Make sure we remove the line prefixes as we have
3500 # none on the first line, and are going to readd them
3501 # where necessary.
3502 $s =~ s/\n./\n/gs;
3503 while ($s =~ /\n\s+\\\n/) {
3504 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3505 }
3506
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003507 # We want to check the first line inside the block
3508 # starting at the end of the conditional, so remove:
3509 # 1) any blank line termination
3510 # 2) any opening brace { on end of the line
3511 # 3) any do (...) {
3512 my $continuation = 0;
3513 my $check = 0;
3514 $s =~ s/^.*\bdo\b//;
3515 $s =~ s/^\s*{//;
3516 if ($s =~ s/^\s*\\//) {
3517 $continuation = 1;
3518 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003519 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003520 $check = 1;
3521 $cond_lines++;
3522 }
3523
3524 # Also ignore a loop construct at the end of a
3525 # preprocessor statement.
3526 if (($prevline =~ /^.\s*#\s*define\s/ ||
3527 $prevline =~ /\\\s*$/) && $continuation == 0) {
3528 $check = 0;
3529 }
3530
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003531 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003532 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003533 while ($cond_ptr != $cond_lines) {
3534 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003535
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003536 # If we see an #else/#elif then the code
3537 # is not linear.
3538 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3539 $check = 0;
3540 }
3541
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003542 # Ignore:
3543 # 1) blank lines, they should be at 0,
3544 # 2) preprocessor lines, and
3545 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003546 if ($continuation ||
3547 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003548 $s =~ /^\s*#\s*?/ ||
3549 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003550 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003551 if ($s =~ s/^.*?\n//) {
3552 $cond_lines++;
3553 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003554 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003555 }
3556
3557 my (undef, $sindent) = line_stats("+" . $s);
3558 my $stat_real = raw_line($linenr, $cond_lines);
3559
3560 # Check if either of these lines are modified, else
3561 # this is not this patch's fault.
3562 if (!defined($stat_real) ||
3563 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3564 $check = 0;
3565 }
3566 if (defined($stat_real) && $cond_lines > 1) {
3567 $stat_real = "[...]\n$stat_real";
3568 }
3569
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003570 #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 -07003571
Joe Perches9f5af482015-09-09 15:37:30 -07003572 if ($check && $s ne '' &&
3573 (($sindent % 8) != 0 ||
3574 ($sindent < $indent) ||
3575 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003576 WARN("SUSPECT_CODE_INDENT",
3577 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003578 }
3579 }
3580
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003581 # Track the 'values' across context and added lines.
3582 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003583 my ($curr_values, $curr_vars) =
3584 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003585 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003586 if ($dbg_values) {
3587 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003588 print "$linenr > .$outline\n";
3589 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003590 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003591 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003592 $prev_values = substr($curr_values, -1);
3593
Andy Whitcroft00df3442007-06-08 13:47:06 -07003594#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003595 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003596
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003597# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003598 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 -07003599 my $type = $1;
3600 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003601 $var = "" if (!defined $var);
3602 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003603 my $sign = $1;
3604 my $pointer = $2;
3605
3606 $pointer = "" if (!defined $pointer);
3607
3608 if (WARN("UNSPECIFIED_INT",
3609 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3610 $fix) {
3611 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003612 my $comp_pointer = $pointer;
3613 $comp_pointer =~ s/\s//g;
3614 $decl .= $comp_pointer;
3615 $decl = rtrim($decl) if ($var eq "");
3616 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003617 }
3618 }
3619 }
3620
Andy Whitcroft653d4872007-06-23 17:16:34 -07003621# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003622 if ($dbg_type) {
3623 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003624 ERROR("TEST_TYPE",
3625 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003626 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003627 ERROR("TEST_NOT_TYPE",
3628 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003629 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003630 next;
3631 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003632# TEST: allow direct testing of the attribute matcher.
3633 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003634 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003635 ERROR("TEST_ATTR",
3636 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003637 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003638 ERROR("TEST_NOT_ATTR",
3639 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003640 }
3641 next;
3642 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003643
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003644# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003645 if ($line =~ /^.\s*{/ &&
3646 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003647 if (ERROR("OPEN_BRACE",
3648 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003649 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3650 fix_delete_line($fixlinenr - 1, $prevrawline);
3651 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003652 my $fixedline = $prevrawline;
3653 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003654 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003655 $fixedline = $line;
3656 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003657 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003658 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003659 }
3660
Andy Whitcroft653d4872007-06-23 17:16:34 -07003661#
3662# Checks which are anchored on the added line.
3663#
3664
3665# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003666 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003667 my $path = $1;
3668 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003669 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003670 "malformed #include filename\n" . $herecurr);
3671 }
3672 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3673 ERROR("UAPI_INCLUDE",
3674 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003675 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003676 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003677
3678# no C99 // comments
3679 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003680 if (ERROR("C99_COMMENTS",
3681 "do not use C99 // comments\n" . $herecurr) &&
3682 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003683 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003684 if ($line =~ /\/\/(.*)$/) {
3685 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003686 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003687 }
3688 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003689 }
3690 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003691 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003692 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003693
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003694# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3695# the whole statement.
3696#print "APW <$lines[$realline_next - 1]>\n";
3697 if (defined $realline_next &&
3698 exists $lines[$realline_next - 1] &&
3699 !defined $suppress_export{$realline_next} &&
3700 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3701 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003702 # Handle definitions which produce identifiers with
3703 # a prefix:
3704 # XXX(foo);
3705 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003706 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003707 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003708 $name =~ /^${Ident}_$2/) {
3709#print "FOO C name<$name>\n";
3710 $suppress_export{$realline_next} = 1;
3711
3712 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003713 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003714 ^.DEFINE_$Ident\(\Q$name\E\)|
3715 ^.DECLARE_$Ident\(\Q$name\E\)|
3716 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003717 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3718 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003719 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003720#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3721 $suppress_export{$realline_next} = 2;
3722 } else {
3723 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003724 }
3725 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003726 if (!defined $suppress_export{$linenr} &&
3727 $prevline =~ /^.\s*$/ &&
3728 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3729 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3730#print "FOO B <$lines[$linenr - 1]>\n";
3731 $suppress_export{$linenr} = 2;
3732 }
3733 if (defined $suppress_export{$linenr} &&
3734 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003735 WARN("EXPORT_SYMBOL",
3736 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003737 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003738
Joe Eloff5150bda2010-08-09 17:21:00 -07003739# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003740 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003741 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003742 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003743 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003744 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003745 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003746 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003747# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003748 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003749 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003750 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003751 $herecurr) &&
3752 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003753 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003754 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003755 }
3756
Joe Perches18130872014-08-06 16:11:22 -07003757# check for misordered declarations of char/short/int/long with signed/unsigned
3758 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3759 my $tmp = trim($1);
3760 WARN("MISORDERED_TYPE",
3761 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3762 }
3763
Joe Perchescb710ec2010-10-26 14:23:20 -07003764# check for static const char * arrays.
3765 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003766 WARN("STATIC_CONST_CHAR_ARRAY",
3767 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003768 $herecurr);
3769 }
3770
3771# check for static char foo[] = "bar" declarations.
3772 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003773 WARN("STATIC_CONST_CHAR_ARRAY",
3774 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003775 $herecurr);
3776 }
3777
Joe Perchesab7e23f2015-04-16 12:44:22 -07003778# check for const <foo> const where <foo> is not a pointer or array type
3779 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3780 my $found = $1;
3781 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3782 WARN("CONST_CONST",
3783 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3784 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3785 WARN("CONST_CONST",
3786 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3787 }
3788 }
3789
Joe Perches9b0fa602014-04-03 14:49:18 -07003790# check for non-global char *foo[] = {"bar", ...} declarations.
3791 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3792 WARN("STATIC_CONST_CHAR_ARRAY",
3793 "char * array declaration might be better as static const\n" .
3794 $herecurr);
3795 }
3796
Joe Perchesb598b672015-04-16 12:44:36 -07003797# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3798 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3799 my $array = $1;
3800 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3801 my $array_div = $1;
3802 if (WARN("ARRAY_SIZE",
3803 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3804 $fix) {
3805 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3806 }
3807 }
3808 }
3809
Joe Perchesb36190c2014-01-27 17:07:18 -08003810# check for function declarations without arguments like "int foo()"
3811 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3812 if (ERROR("FUNCTION_WITHOUT_ARGS",
3813 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3814 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003815 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003816 }
3817 }
3818
Andy Whitcroft653d4872007-06-23 17:16:34 -07003819# check for new typedefs, only function parameters and sparse annotations
3820# make sense.
3821 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003822 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003823 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003824 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003825 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003826 WARN("NEW_TYPEDEFS",
3827 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003828 }
3829
3830# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003831 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003832 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3833 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003834 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003835
Andy Whitcroft65863862009-01-06 14:41:21 -08003836 # Should start with a space.
3837 $to =~ s/^(\S)/ $1/;
3838 # Should not end with a space.
3839 $to =~ s/\s+$//;
3840 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003841 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003842 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003843
Joe Perches3705ce52013-07-03 15:05:31 -07003844## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003845 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003846 if (ERROR("POINTER_LOCATION",
3847 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3848 $fix) {
3849 my $sub_from = $ident;
3850 my $sub_to = $ident;
3851 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003852 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003853 s@\Q$sub_from\E@$sub_to@;
3854 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003855 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003856 }
3857 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3858 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003859 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003860
Andy Whitcroft65863862009-01-06 14:41:21 -08003861 # Should start with a space.
3862 $to =~ s/^(\S)/ $1/;
3863 # Should not end with a space.
3864 $to =~ s/\s+$//;
3865 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003866 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003867 }
3868 # Modifiers should have spaces.
3869 $to =~ s/(\b$Modifier$)/$1 /;
3870
Joe Perches3705ce52013-07-03 15:05:31 -07003871## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003872 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003873 if (ERROR("POINTER_LOCATION",
3874 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3875 $fix) {
3876
3877 my $sub_from = $match;
3878 my $sub_to = $match;
3879 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003880 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003881 s@\Q$sub_from\E@$sub_to@;
3882 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003883 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003884 }
3885
Joe Perches9d3e3c72015-09-09 15:37:27 -07003886# avoid BUG() or BUG_ON()
3887 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3888 my $msg_type = \&WARN;
3889 $msg_type = \&CHK if ($file);
3890 &{$msg_type}("AVOID_BUG",
3891 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3892 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003893
Joe Perches9d3e3c72015-09-09 15:37:27 -07003894# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003895 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003896 WARN("LINUX_VERSION_CODE",
3897 "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 -08003898 }
3899
Joe Perches17441222011-06-15 15:08:17 -07003900# check for uses of printk_ratelimit
3901 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003902 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003903 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003904 }
3905
Andy Whitcroft00df3442007-06-08 13:47:06 -07003906# printk should use KERN_* levels. Note that follow on printk's on the
3907# same line do not need a level, so we use the current block context
3908# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003909# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003910# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003911 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003912 my $ok = 0;
3913 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3914 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003915 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003916 # with "\n" ignore it, else it is to blame
3917 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3918 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3919 $ok = 1;
3920 }
3921 last;
3922 }
3923 }
3924 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003925 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3926 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003927 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003928 }
3929
Joe Perches243f3802012-05-31 16:26:09 -07003930 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3931 my $orig = $1;
3932 my $level = lc($orig);
3933 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003934 my $level2 = $level;
3935 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003936 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003937 "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 -07003938 }
3939
3940 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003941 if (WARN("PREFER_PR_LEVEL",
3942 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3943 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003944 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003945 s/\bpr_warning\b/pr_warn/;
3946 }
Joe Perches243f3802012-05-31 16:26:09 -07003947 }
3948
Joe Perchesdc139312013-02-21 16:44:13 -08003949 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3950 my $orig = $1;
3951 my $level = lc($orig);
3952 $level = "warn" if ($level eq "warning");
3953 $level = "dbg" if ($level eq "debug");
3954 WARN("PREFER_DEV_LEVEL",
3955 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3956 }
3957
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003958# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3959# number of false positives, but assembly files are not checked, so at
3960# least the arch entry code will not trigger this warning.
3961 if ($line =~ /\bENOSYS\b/) {
3962 WARN("ENOSYS",
3963 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3964 }
3965
Andy Whitcroft653d4872007-06-23 17:16:34 -07003966# function brace can't be on same line, except for #defines of do while,
3967# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003968 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003969 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003970 if (ERROR("OPEN_BRACE",
3971 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3972 $fix) {
3973 fix_delete_line($fixlinenr, $rawline);
3974 my $fixed_line = $rawline;
3975 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3976 my $line1 = $1;
3977 my $line2 = $2;
3978 fix_insert_line($fixlinenr, ltrim($line1));
3979 fix_insert_line($fixlinenr, "\+{");
3980 if ($line2 !~ /^\s*$/) {
3981 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3982 }
3983 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003984 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003985
Andy Whitcroft8905a672007-11-28 16:21:06 -08003986# open braces for enum, union and struct go on the same line.
3987 if ($line =~ /^.\s*{/ &&
3988 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003989 if (ERROR("OPEN_BRACE",
3990 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3991 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3992 fix_delete_line($fixlinenr - 1, $prevrawline);
3993 fix_delete_line($fixlinenr, $rawline);
3994 my $fixedline = rtrim($prevrawline) . " {";
3995 fix_insert_line($fixlinenr, $fixedline);
3996 $fixedline = $rawline;
3997 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3998 if ($fixedline !~ /^\+\s*$/) {
3999 fix_insert_line($fixlinenr, $fixedline);
4000 }
4001 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004002 }
4003
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004004# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004005 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4006 if (WARN("SPACING",
4007 "missing space after $1 definition\n" . $herecurr) &&
4008 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004009 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004010 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4011 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004012 }
4013
Joe Perches31070b52014-01-23 15:54:49 -08004014# Function pointer declarations
4015# check spacing between type, funcptr, and args
4016# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004017 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004018 my $declare = $1;
4019 my $pre_pointer_space = $2;
4020 my $post_pointer_space = $3;
4021 my $funcname = $4;
4022 my $post_funcname_space = $5;
4023 my $pre_args_space = $6;
4024
Joe Perches91f72e92014-04-03 14:49:12 -07004025# the $Declare variable will capture all spaces after the type
4026# so check it for a missing trailing missing space but pointer return types
4027# don't need a space so don't warn for those.
4028 my $post_declare_space = "";
4029 if ($declare =~ /(\s+)$/) {
4030 $post_declare_space = $1;
4031 $declare = rtrim($declare);
4032 }
4033 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004034 WARN("SPACING",
4035 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004036 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004037 }
4038
4039# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004040# This test is not currently implemented because these declarations are
4041# equivalent to
4042# int foo(int bar, ...)
4043# and this is form shouldn't/doesn't generate a checkpatch warning.
4044#
4045# elsif ($declare =~ /\s{2,}$/) {
4046# WARN("SPACING",
4047# "Multiple spaces after return type\n" . $herecurr);
4048# }
Joe Perches31070b52014-01-23 15:54:49 -08004049
4050# unnecessary space "type ( *funcptr)(args...)"
4051 if (defined $pre_pointer_space &&
4052 $pre_pointer_space =~ /^\s/) {
4053 WARN("SPACING",
4054 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4055 }
4056
4057# unnecessary space "type (* funcptr)(args...)"
4058 if (defined $post_pointer_space &&
4059 $post_pointer_space =~ /^\s/) {
4060 WARN("SPACING",
4061 "Unnecessary space before function pointer name\n" . $herecurr);
4062 }
4063
4064# unnecessary space "type (*funcptr )(args...)"
4065 if (defined $post_funcname_space &&
4066 $post_funcname_space =~ /^\s/) {
4067 WARN("SPACING",
4068 "Unnecessary space after function pointer name\n" . $herecurr);
4069 }
4070
4071# unnecessary space "type (*funcptr) (args...)"
4072 if (defined $pre_args_space &&
4073 $pre_args_space =~ /^\s/) {
4074 WARN("SPACING",
4075 "Unnecessary space before function pointer arguments\n" . $herecurr);
4076 }
4077
4078 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004079 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004080 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004081 }
4082 }
4083
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004084# check for spacing round square brackets; allowed:
4085# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004086# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4087# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004088 while ($line =~ /(.*?\s)\[/g) {
4089 my ($where, $prefix) = ($-[1], $1);
4090 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004091 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004092 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004093 if (ERROR("BRACKET_SPACE",
4094 "space prohibited before open square bracket '['\n" . $herecurr) &&
4095 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004096 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004097 s/^(\+.*?)\s+\[/$1\[/;
4098 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004099 }
4100 }
4101
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004102# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004103 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004104 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004105 my $ctx_before = substr($line, 0, $-[1]);
4106 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004107
4108 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004109 if ($name =~ /^(?:
4110 if|for|while|switch|return|case|
4111 volatile|__volatile__|
4112 __attribute__|format|__extension__|
4113 asm|__asm__)$/x)
4114 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004115 # cpp #define statements have non-optional spaces, ie
4116 # if there is a space between the name and the open
4117 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004118 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004119
4120 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004121 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004122
4123 # If this whole things ends with a type its most
4124 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004125 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004126
4127 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004128 if (WARN("SPACING",
4129 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4130 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004131 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004132 s/\b$name\s+\(/$name\(/;
4133 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004134 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004135 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004136
Andy Whitcroft653d4872007-06-23 17:16:34 -07004137# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004138 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004139 my $fixed_line = "";
4140 my $line_fixed = 0;
4141
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004142 my $ops = qr{
4143 <<=|>>=|<=|>=|==|!=|
4144 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4145 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004146 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004147 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004148 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004149 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004150
4151## print("element count: <" . $#elements . ">\n");
4152## foreach my $el (@elements) {
4153## print("el: <$el>\n");
4154## }
4155
4156 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004157 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004158
Joe Perches3705ce52013-07-03 15:05:31 -07004159 foreach my $el (@elements) {
4160 push(@fix_elements, substr($rawline, $off, length($el)));
4161 $off += length($el);
4162 }
4163
4164 $off = 0;
4165
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004166 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004167 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004168
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004169 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004170
4171 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4172
4173## print("n: <$n> good: <$good>\n");
4174
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004175 $off += length($elements[$n]);
4176
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004177 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004178 my $ca = substr($opline, 0, $off);
4179 my $cc = '';
4180 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4181 $cc = substr($opline, $off + length($elements[$n + 1]));
4182 }
4183 my $cb = "$ca$;$cc";
4184
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004185 my $a = '';
4186 $a = 'V' if ($elements[$n] ne '');
4187 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004188 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004189 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4190 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004191 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004192
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004193 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004194
4195 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004196 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004197 $c = 'V' if ($elements[$n + 2] ne '');
4198 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004199 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004200 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4201 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004202 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004203 } else {
4204 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004205 }
4206
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004207 my $ctx = "${a}x${c}";
4208
4209 my $at = "(ctx:$ctx)";
4210
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004211 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004212 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004213
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004214 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004215 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004216
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004217 # Get the full operator variant.
4218 my $opv = $op . substr($curr_vars, $off, 1);
4219
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004220 # Ignore operators passed as parameters.
4221 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004222 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004223
Andy Whitcroftcf655042008-03-04 14:28:20 -08004224# # Ignore comments
4225# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004226
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004227 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004228 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004229 if ($ctx !~ /.x[WEBC]/ &&
4230 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004231 if (ERROR("SPACING",
4232 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004233 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004234 $line_fixed = 1;
4235 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004236 }
4237
4238 # // is a comment
4239 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004240
Joe Perchesb00e4812014-04-03 14:49:33 -07004241 # : when part of a bitfield
4242 } elsif ($opv eq ':B') {
4243 # skip the bitfield test for now
4244
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004245 # No spaces for:
4246 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004247 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004248 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004249 if (ERROR("SPACING",
4250 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004251 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004252 if (defined $fix_elements[$n + 2]) {
4253 $fix_elements[$n + 2] =~ s/^\s+//;
4254 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004255 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004256 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004257 }
4258
Joe Perches23810972014-12-10 15:51:32 -08004259 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004260 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004261 my $rtrim_before = 0;
4262 my $space_after = 0;
4263 if ($ctx =~ /Wx./) {
4264 if (ERROR("SPACING",
4265 "space prohibited before that '$op' $at\n" . $hereptr)) {
4266 $line_fixed = 1;
4267 $rtrim_before = 1;
4268 }
4269 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004270 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004271 if (ERROR("SPACING",
4272 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004273 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004274 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004275 $space_after = 1;
4276 }
4277 }
4278 if ($rtrim_before || $space_after) {
4279 if ($rtrim_before) {
4280 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4281 } else {
4282 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4283 }
4284 if ($space_after) {
4285 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004286 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004287 }
4288
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004289 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004290 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004291 #warn "'*' is part of type\n";
4292
4293 # unary operators should have a space before and
4294 # none after. May be left adjacent to another
4295 # unary operator, or a cast
4296 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004297 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004298 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004299 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004300 if (ERROR("SPACING",
4301 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004302 if ($n != $last_after + 2) {
4303 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4304 $line_fixed = 1;
4305 }
Joe Perches3705ce52013-07-03 15:05:31 -07004306 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004307 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004308 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004309 # A unary '*' may be const
4310
4311 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004312 if (ERROR("SPACING",
4313 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004314 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004315 if (defined $fix_elements[$n + 2]) {
4316 $fix_elements[$n + 2] =~ s/^\s+//;
4317 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004318 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004319 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004320 }
4321
4322 # unary ++ and unary -- are allowed no space on one side.
4323 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004324 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004325 if (ERROR("SPACING",
4326 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004327 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004328 $line_fixed = 1;
4329 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004330 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004331 if ($ctx =~ /Wx[BE]/ ||
4332 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004333 if (ERROR("SPACING",
4334 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004335 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004336 $line_fixed = 1;
4337 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004340 if (ERROR("SPACING",
4341 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004342 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004343 if (defined $fix_elements[$n + 2]) {
4344 $fix_elements[$n + 2] =~ s/^\s+//;
4345 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004346 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004347 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004348 }
4349
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004350 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004351 } elsif ($op eq '<<' or $op eq '>>' or
4352 $op eq '&' or $op eq '^' or $op eq '|' or
4353 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004354 $op eq '*' or $op eq '/' or
4355 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004356 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004357 if ($check) {
4358 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4359 if (CHK("SPACING",
4360 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4361 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4362 $fix_elements[$n + 2] =~ s/^\s+//;
4363 $line_fixed = 1;
4364 }
4365 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4366 if (CHK("SPACING",
4367 "space preferred before that '$op' $at\n" . $hereptr)) {
4368 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4369 $line_fixed = 1;
4370 }
4371 }
4372 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004373 if (ERROR("SPACING",
4374 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004375 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4376 if (defined $fix_elements[$n + 2]) {
4377 $fix_elements[$n + 2] =~ s/^\s+//;
4378 }
Joe Perches3705ce52013-07-03 15:05:31 -07004379 $line_fixed = 1;
4380 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004381 }
4382
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004383 # A colon needs no spaces before when it is
4384 # terminating a case value or a label.
4385 } elsif ($opv eq ':C' || $opv eq ':L') {
4386 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004387 if (ERROR("SPACING",
4388 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004389 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004390 $line_fixed = 1;
4391 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004392 }
4393
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004394 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004395 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004396 my $ok = 0;
4397
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004398 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004399 if (($op eq '<' &&
4400 $cc =~ /^\S+\@\S+>/) ||
4401 ($op eq '>' &&
4402 $ca =~ /<\S+\@\S+$/))
4403 {
4404 $ok = 1;
4405 }
4406
Joe Perchese0df7e12015-04-16 12:44:53 -07004407 # for asm volatile statements
4408 # ignore a colon with another
4409 # colon immediately before or after
4410 if (($op eq ':') &&
4411 ($ca =~ /:$/ || $cc =~ /^:/)) {
4412 $ok = 1;
4413 }
4414
Joe Perches84731622013-11-12 15:10:05 -08004415 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004416 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004417 my $msg_type = \&ERROR;
4418 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4419
4420 if (&{$msg_type}("SPACING",
4421 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004422 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4423 if (defined $fix_elements[$n + 2]) {
4424 $fix_elements[$n + 2] =~ s/^\s+//;
4425 }
Joe Perches3705ce52013-07-03 15:05:31 -07004426 $line_fixed = 1;
4427 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004428 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004429 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004430 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004431
4432## print("n: <$n> GOOD: <$good>\n");
4433
4434 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004435 }
Joe Perches3705ce52013-07-03 15:05:31 -07004436
4437 if (($#elements % 2) == 0) {
4438 $fixed_line = $fixed_line . $fix_elements[$#elements];
4439 }
4440
Joe Perches194f66f2014-08-06 16:11:03 -07004441 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4442 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004443 }
4444
4445
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004446 }
4447
Joe Perches786b6322013-07-03 15:05:32 -07004448# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004449 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004450 if (WARN("SPACING",
4451 "space prohibited before semicolon\n" . $herecurr) &&
4452 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004453 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004454 s/^(\+.*\S)\s+;/$1;/;
4455 }
4456 }
4457
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004458# check for multiple assignments
4459 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004460 CHK("MULTIPLE_ASSIGNMENTS",
4461 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004462 }
4463
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004464## # check for multiple declarations, allowing for a function declaration
4465## # continuation.
4466## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4467## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4468##
4469## # Remove any bracketed sections to ensure we do not
4470## # falsly report the parameters of functions.
4471## my $ln = $line;
4472## while ($ln =~ s/\([^\(\)]*\)//g) {
4473## }
4474## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004475## WARN("MULTIPLE_DECLARATION",
4476## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004477## }
4478## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004479
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004480#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004481 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004482 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004483 if (ERROR("SPACING",
4484 "space required before the open brace '{'\n" . $herecurr) &&
4485 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004486 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004487 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004488 }
4489
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004490## # check for blank lines before declarations
4491## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4492## $prevrawline =~ /^.\s*$/) {
4493## WARN("SPACING",
4494## "No blank lines before declarations\n" . $hereprev);
4495## }
4496##
4497
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004498# closing brace should have a space following it when it has anything
4499# on the line
4500 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004501 if (ERROR("SPACING",
4502 "space required after that close brace '}'\n" . $herecurr) &&
4503 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004504 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004505 s/}((?!(?:,|;|\)))\S)/} $1/;
4506 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004507 }
4508
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004509# check spacing on square brackets
4510 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004511 if (ERROR("SPACING",
4512 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4513 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004514 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004515 s/\[\s+/\[/;
4516 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004517 }
4518 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004519 if (ERROR("SPACING",
4520 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4521 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004522 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004523 s/\s+\]/\]/;
4524 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004525 }
4526
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004527# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004528 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004529 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004530 if (ERROR("SPACING",
4531 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004533 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004534 s/\(\s+/\(/;
4535 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004536 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004537 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004538 $line !~ /for\s*\(.*;\s+\)/ &&
4539 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004540 if (ERROR("SPACING",
4541 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004543 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004544 s/\s+\)/\)/;
4545 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004546 }
4547
Joe Perchese2826fd2014-08-06 16:10:48 -07004548# check unnecessary parentheses around addressof/dereference single $Lvals
4549# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4550
4551 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004552 my $var = $1;
4553 if (CHK("UNNECESSARY_PARENTHESES",
4554 "Unnecessary parentheses around $var\n" . $herecurr) &&
4555 $fix) {
4556 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4557 }
4558 }
4559
4560# check for unnecessary parentheses around function pointer uses
4561# ie: (foo->bar)(); should be foo->bar();
4562# but not "if (foo->bar) (" to avoid some false positives
4563 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4564 my $var = $2;
4565 if (CHK("UNNECESSARY_PARENTHESES",
4566 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4567 $fix) {
4568 my $var2 = deparenthesize($var);
4569 $var2 =~ s/\s//g;
4570 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4571 }
4572 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004573
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004574#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004575 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004576 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004577 if (WARN("INDENTED_LABEL",
4578 "labels should not be indented\n" . $herecurr) &&
4579 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004580 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004581 s/^(.)\s+/$1/;
4582 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004583 }
4584
Joe Perches5b9553a2014-04-03 14:49:21 -07004585# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004586 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004587 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004588 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004589 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4590 my $value = $1;
4591 $value = deparenthesize($value);
4592 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4593 ERROR("RETURN_PARENTHESES",
4594 "return is not a function, parentheses are not required\n" . $herecurr);
4595 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004596 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004597 ERROR("SPACING",
4598 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004599 }
4600 }
Joe Perches507e5142013-11-12 15:10:13 -08004601
Joe Perchesb43ae212014-06-23 13:22:07 -07004602# unnecessary return in a void function
4603# at end-of-function, with the previous line a single leading tab, then return;
4604# and the line before that not a goto label target like "out:"
4605 if ($sline =~ /^[ \+]}\s*$/ &&
4606 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4607 $linenr >= 3 &&
4608 $lines[$linenr - 3] =~ /^[ +]/ &&
4609 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004610 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004611 "void function return statements are not generally useful\n" . $hereprev);
4612 }
Joe Perches9819cf22014-06-04 16:12:09 -07004613
Joe Perches189248d2014-01-23 15:54:47 -08004614# if statements using unnecessary parentheses - ie: if ((foo == bar))
4615 if ($^V && $^V ge 5.10.0 &&
4616 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4617 my $openparens = $1;
4618 my $count = $openparens =~ tr@\(@\(@;
4619 my $msg = "";
4620 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4621 my $comp = $4; #Not $1 because of $LvalOrFunc
4622 $msg = " - maybe == should be = ?" if ($comp eq "==");
4623 WARN("UNNECESSARY_PARENTHESES",
4624 "Unnecessary parentheses$msg\n" . $herecurr);
4625 }
4626 }
4627
Joe Perchesc5595fa2015-09-09 15:37:58 -07004628# comparisons with a constant or upper case identifier on the left
4629# avoid cases like "foo + BAR < baz"
4630# only fix matches surrounded by parentheses to avoid incorrect
4631# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4632 if ($^V && $^V ge 5.10.0 &&
4633 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4634 my $lead = $1;
4635 my $const = $2;
4636 my $comp = $3;
4637 my $to = $4;
4638 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004639 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004640 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4641 WARN("CONSTANT_COMPARISON",
4642 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4643 $fix) {
4644 if ($comp eq "<") {
4645 $newcomp = ">";
4646 } elsif ($comp eq "<=") {
4647 $newcomp = ">=";
4648 } elsif ($comp eq ">") {
4649 $newcomp = "<";
4650 } elsif ($comp eq ">=") {
4651 $newcomp = "<=";
4652 }
4653 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4654 }
4655 }
4656
Joe Perchesf34e4a42015-04-16 12:44:19 -07004657# Return of what appears to be an errno should normally be negative
4658 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004659 my $name = $1;
4660 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004661 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004662 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004663 }
4664 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004665
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004666# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004667 if ($line =~ /\b(if|while|for|switch)\(/) {
4668 if (ERROR("SPACING",
4669 "space required before the open parenthesis '('\n" . $herecurr) &&
4670 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004671 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004672 s/\b(if|while|for|switch)\(/$1 \(/;
4673 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004674 }
4675
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004676# Check for illegal assignment in if conditional -- and check for trailing
4677# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004678 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004679 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4680 ctx_statement_block($linenr, $realcnt, 0)
4681 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004682 my ($stat_next) = ctx_statement_block($line_nr_next,
4683 $remain_next, $off_next);
4684 $stat_next =~ s/\n./\n /g;
4685 ##print "stat<$stat> stat_next<$stat_next>\n";
4686
4687 if ($stat_next =~ /^\s*while\b/) {
4688 # If the statement carries leading newlines,
4689 # then count those as offsets.
4690 my ($whitespace) =
4691 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4692 my $offset =
4693 statement_rawlines($whitespace) - 1;
4694
4695 $suppress_whiletrailers{$line_nr_next +
4696 $offset} = 1;
4697 }
4698 }
4699 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004700 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004701 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004702 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004703
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004704 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004705 ERROR("ASSIGN_IN_IF",
4706 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004707 }
4708
4709 # Find out what is on the end of the line after the
4710 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004711 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004712 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004713 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004714 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4715 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004716 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004717 # Find out how long the conditional actually is.
4718 my @newlines = ($c =~ /\n/gs);
4719 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004720 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004721
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004722 $stat_real = raw_line($linenr, $cond_lines)
4723 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004724 if (defined($stat_real) && $cond_lines > 1) {
4725 $stat_real = "[...]\n$stat_real";
4726 }
4727
Joe Perches000d1cc12011-07-25 17:13:25 -07004728 ERROR("TRAILING_STATEMENTS",
4729 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004730 }
4731 }
4732
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004733# Check for bitwise tests written as boolean
4734 if ($line =~ /
4735 (?:
4736 (?:\[|\(|\&\&|\|\|)
4737 \s*0[xX][0-9]+\s*
4738 (?:\&\&|\|\|)
4739 |
4740 (?:\&\&|\|\|)
4741 \s*0[xX][0-9]+\s*
4742 (?:\&\&|\|\||\)|\])
4743 )/x)
4744 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004745 WARN("HEXADECIMAL_BOOLEAN_TEST",
4746 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004747 }
4748
Andy Whitcroft8905a672007-11-28 16:21:06 -08004749# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004750 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4751 my $s = $1;
4752 $s =~ s/$;//g; # Remove any comments
4753 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004754 ERROR("TRAILING_STATEMENTS",
4755 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004756 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004757 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004758# if should not continue a brace
4759 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004760 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004761 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004762 $herecurr);
4763 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004764# case and default should not have general statements after them
4765 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4766 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004767 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004768 \s*return\s+
4769 )/xg)
4770 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004771 ERROR("TRAILING_STATEMENTS",
4772 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004773 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004774
4775 # Check for }<nl>else {, these must be at the same
4776 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004777 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4778 $previndent == $indent) {
4779 if (ERROR("ELSE_AFTER_BRACE",
4780 "else should follow close brace '}'\n" . $hereprev) &&
4781 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4782 fix_delete_line($fixlinenr - 1, $prevrawline);
4783 fix_delete_line($fixlinenr, $rawline);
4784 my $fixedline = $prevrawline;
4785 $fixedline =~ s/}\s*$//;
4786 if ($fixedline !~ /^\+\s*$/) {
4787 fix_insert_line($fixlinenr, $fixedline);
4788 }
4789 $fixedline = $rawline;
4790 $fixedline =~ s/^(.\s*)else/$1} else/;
4791 fix_insert_line($fixlinenr, $fixedline);
4792 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004793 }
4794
Joe Perches8b8856f2014-08-06 16:11:14 -07004795 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4796 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004797 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4798
4799 # Find out what is on the end of the line after the
4800 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004801 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004802 $s =~ s/\n.*//g;
4803
4804 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004805 if (ERROR("WHILE_AFTER_BRACE",
4806 "while should follow close brace '}'\n" . $hereprev) &&
4807 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4808 fix_delete_line($fixlinenr - 1, $prevrawline);
4809 fix_delete_line($fixlinenr, $rawline);
4810 my $fixedline = $prevrawline;
4811 my $trailing = $rawline;
4812 $trailing =~ s/^\+//;
4813 $trailing = trim($trailing);
4814 $fixedline =~ s/}\s*$/} $trailing/;
4815 fix_insert_line($fixlinenr, $fixedline);
4816 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004817 }
4818 }
4819
Joe Perches95e2c602013-07-03 15:05:20 -07004820#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004821 while ($line =~ m{($Constant|$Lval)}g) {
4822 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004823
4824#gcc binary extension
4825 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004826 if (WARN("GCC_BINARY_CONSTANT",
4827 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4828 $fix) {
4829 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004830 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004831 s/\b$var\b/$hexval/;
4832 }
Joe Perches95e2c602013-07-03 15:05:20 -07004833 }
4834
4835#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004836 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004837 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004838#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004839 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004840#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004841 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4842#Ignore some three character SI units explicitly, like MiB and KHz
4843 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004844 while ($var =~ m{($Ident)}g) {
4845 my $word = $1;
4846 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004847 if ($check) {
4848 seed_camelcase_includes();
4849 if (!$file && !$camelcase_file_seeded) {
4850 seed_camelcase_file($realfile);
4851 $camelcase_file_seeded = 1;
4852 }
4853 }
Joe Perches7e781f62013-09-11 14:23:55 -07004854 if (!defined $camelcase{$word}) {
4855 $camelcase{$word} = 1;
4856 CHK("CAMELCASE",
4857 "Avoid CamelCase: <$word>\n" . $herecurr);
4858 }
Joe Perches34456862013-07-03 15:05:34 -07004859 }
Joe Perches323c1262012-12-17 16:02:07 -08004860 }
4861 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004862
4863#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004864 if ($line =~ /\#\s*define.*\\\s+$/) {
4865 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4866 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004868 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004869 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004870 }
4871
Fabian Frederick0e212e02015-04-16 12:44:25 -07004872# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4873# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004874 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004875 my $file = "$1.h";
4876 my $checkfile = "include/linux/$file";
4877 if (-f "$root/$checkfile" &&
4878 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004879 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004880 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004881 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4882 if ($asminclude > 0) {
4883 if ($realfile =~ m{^arch/}) {
4884 CHK("ARCH_INCLUDE_LINUX",
4885 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4886 } else {
4887 WARN("INCLUDE_LINUX",
4888 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4889 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004890 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004891 }
4892 }
4893
Andy Whitcroft653d4872007-06-23 17:16:34 -07004894# multi-statement macros should be enclosed in a do while loop, grab the
4895# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004896# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004897 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4898 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004899 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004900 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004901 my ($off, $dstat, $dcond, $rest);
4902 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004903 my $has_flow_statement = 0;
4904 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004905 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004906 ctx_statement_block($linenr, $realcnt, 0);
4907 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004908 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004909 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004910
Joe Perches08a28432014-10-13 15:51:55 -07004911 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004912 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004913
Joe Perchesf59b64b2016-10-11 13:52:08 -07004914 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4915 my $define_args = $1;
4916 my $define_stmt = $dstat;
4917 my @def_args = ();
4918
4919 if (defined $define_args && $define_args ne "") {
4920 $define_args = substr($define_args, 1, length($define_args) - 2);
4921 $define_args =~ s/\s*//g;
4922 @def_args = split(",", $define_args);
4923 }
4924
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004925 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004926 $dstat =~ s/\\\n.//g;
4927 $dstat =~ s/^\s*//s;
4928 $dstat =~ s/\s*$//s;
4929
4930 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004931 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4932 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004933 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004934 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004935 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004936
Gregory Bean02870be2011-05-11 09:11:12 -07004937 # Extremely long macros may fall off the end of the
4938 # available context without closing. Give a dangling
4939 # backslash the benefit of the doubt and allow it
4940 # to gobble any hanging open-parens.
4941 $dstat =~ s/\(.+\\$/1/;
4942
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004943 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004944 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4945 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004946 {
4947 }
4948
Joe Perches42e15292016-03-15 14:58:01 -07004949 # Make asm volatile uses seem like a generic function
4950 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4951
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004952 my $exceptions = qr{
4953 $Declare|
4954 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004955 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004956 DECLARE_PER_CPU|
4957 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004958 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004959 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004960 union|
4961 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004962 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004963 ^\"|\"$|
4964 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004965 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004966 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004967
4968 $ctx =~ s/\n*$//;
4969 my $herectx = $here . "\n";
4970 my $stmt_cnt = statement_rawlines($ctx);
4971
4972 for (my $n = 0; $n < $stmt_cnt; $n++) {
4973 $herectx .= raw_line($linenr, $n) . "\n";
4974 }
4975
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004976 if ($dstat ne '' &&
4977 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4978 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004979 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004980 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004981 $dstat !~ /$exceptions/ &&
4982 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004983 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004984 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004985 $dstat !~ /^for\s*$Constant$/ && # for (...)
4986 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4987 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004988 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004989 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004990 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004991
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004992 if ($dstat =~ /;/) {
4993 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4994 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4995 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004996 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004997 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004998 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07004999
5000 }
Joe Perches52076492016-10-11 13:52:14 -07005001
5002 # Make $define_stmt single line, comment-free, etc
5003 my @stmt_array = split('\n', $define_stmt);
5004 my $first = 1;
5005 $define_stmt = "";
5006 foreach my $l (@stmt_array) {
5007 $l =~ s/\\$//;
5008 if ($first) {
5009 $define_stmt = $l;
5010 $first = 0;
5011 } elsif ($l =~ /^[\+ ]/) {
5012 $define_stmt .= substr($l, 1);
5013 }
5014 }
5015 $define_stmt =~ s/$;//g;
5016 $define_stmt =~ s/\s+/ /g;
5017 $define_stmt = trim($define_stmt);
5018
Joe Perchesf59b64b2016-10-11 13:52:08 -07005019# check if any macro arguments are reused (ignore '...' and 'type')
5020 foreach my $arg (@def_args) {
5021 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005022 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005023 my $tmp = $define_stmt;
5024 $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 -07005025 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005026 $tmp =~ s/\b$arg\s*\#\#//g;
5027 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5028 if ($use_cnt > 1) {
5029 CHK("MACRO_ARG_REUSE",
5030 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005031 }
5032# check if any macro arguments may have other precedence issues
5033 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5034 ((defined($1) && $1 ne ',') ||
5035 (defined($2) && $2 ne ','))) {
5036 CHK("MACRO_ARG_PRECEDENCE",
5037 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005038 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005039 }
Joe Perches5023d342012-12-17 16:01:47 -08005040
Joe Perches08a28432014-10-13 15:51:55 -07005041# check for macros with flow control, but without ## concatenation
5042# ## concatenation is commonly a macro that defines a function so ignore those
5043 if ($has_flow_statement && !$has_arg_concat) {
5044 my $herectx = $here . "\n";
5045 my $cnt = statement_rawlines($ctx);
5046
5047 for (my $n = 0; $n < $cnt; $n++) {
5048 $herectx .= raw_line($linenr, $n) . "\n";
5049 }
5050 WARN("MACRO_WITH_FLOW_CONTROL",
5051 "Macros with flow control statements should be avoided\n" . "$herectx");
5052 }
5053
Joe Perches481eb482012-12-17 16:01:56 -08005054# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005055
5056 } else {
5057 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005058 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5059 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005060 $line =~ /^\+.*\\$/) {
5061 WARN("LINE_CONTINUATIONS",
5062 "Avoid unnecessary line continuations\n" . $herecurr);
5063 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005064 }
5065
Joe Perchesb13edf72012-07-30 14:41:24 -07005066# do {} while (0) macro tests:
5067# single-statement macros do not need to be enclosed in do while (0) loop,
5068# macro should not end with a semicolon
5069 if ($^V && $^V ge 5.10.0 &&
5070 $realfile !~ m@/vmlinux.lds.h$@ &&
5071 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5072 my $ln = $linenr;
5073 my $cnt = $realcnt;
5074 my ($off, $dstat, $dcond, $rest);
5075 my $ctx = '';
5076 ($dstat, $dcond, $ln, $cnt, $off) =
5077 ctx_statement_block($linenr, $realcnt, 0);
5078 $ctx = $dstat;
5079
5080 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005081 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005082
5083 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5084 my $stmts = $2;
5085 my $semis = $3;
5086
5087 $ctx =~ s/\n*$//;
5088 my $cnt = statement_rawlines($ctx);
5089 my $herectx = $here . "\n";
5090
5091 for (my $n = 0; $n < $cnt; $n++) {
5092 $herectx .= raw_line($linenr, $n) . "\n";
5093 }
5094
Joe Perchesac8e97f2012-08-21 16:15:53 -07005095 if (($stmts =~ tr/;/;/) == 1 &&
5096 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005097 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5098 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5099 }
5100 if (defined $semis && $semis ne "") {
5101 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5102 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5103 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005104 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5105 $ctx =~ s/\n*$//;
5106 my $cnt = statement_rawlines($ctx);
5107 my $herectx = $here . "\n";
5108
5109 for (my $n = 0; $n < $cnt; $n++) {
5110 $herectx .= raw_line($linenr, $n) . "\n";
5111 }
5112
5113 WARN("TRAILING_SEMICOLON",
5114 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005115 }
5116 }
5117
Mike Frysinger080ba922009-01-06 14:41:25 -08005118# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5119# all assignments may have only one of the following with an assignment:
5120# .
5121# ALIGN(...)
5122# VMLINUX_SYMBOL(...)
5123 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005124 WARN("MISSING_VMLINUX_SYMBOL",
5125 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005126 }
5127
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005128# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005129 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5130 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005131 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005132 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005133 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5134 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005135 my @allowed = ();
5136 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005137 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005138 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005139 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005140 for my $chunk (@chunks) {
5141 my ($cond, $block) = @{$chunk};
5142
Andy Whitcroft773647a2008-03-28 14:15:58 -07005143 # If the condition carries leading newlines, then count those as offsets.
5144 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5145 my $offset = statement_rawlines($whitespace) - 1;
5146
Joe Perchesaad4f612012-03-23 15:02:19 -07005147 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005148 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5149
5150 # We have looked at and allowed this specific line.
5151 $suppress_ifbraces{$ln + $offset} = 1;
5152
5153 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005154 $ln += statement_rawlines($block) - 1;
5155
Andy Whitcroft773647a2008-03-28 14:15:58 -07005156 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005157
5158 $seen++ if ($block =~ /^\s*{/);
5159
Joe Perchesaad4f612012-03-23 15:02:19 -07005160 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005161 if (statement_lines($cond) > 1) {
5162 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005163 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005164 }
5165 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005166 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005167 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005168 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005169 if (statement_block_size($block) > 1) {
5170 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005171 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005172 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005173 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005174 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005175 if ($seen) {
5176 my $sum_allowed = 0;
5177 foreach (@allowed) {
5178 $sum_allowed += $_;
5179 }
5180 if ($sum_allowed == 0) {
5181 WARN("BRACES",
5182 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5183 } elsif ($sum_allowed != $allow &&
5184 $seen != $allow) {
5185 CHK("BRACES",
5186 "braces {} should be used on all arms of this statement\n" . $herectx);
5187 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005188 }
5189 }
5190 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005191 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005192 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005193 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005194
Andy Whitcroftcf655042008-03-04 14:28:20 -08005195 # Check the pre-context.
5196 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5197 #print "APW: ALLOWED: pre<$1>\n";
5198 $allowed = 1;
5199 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005200
5201 my ($level, $endln, @chunks) =
5202 ctx_statement_full($linenr, $realcnt, $-[0]);
5203
Andy Whitcroftcf655042008-03-04 14:28:20 -08005204 # Check the condition.
5205 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005206 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005207 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005208 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005209 }
5210 if (statement_lines($cond) > 1) {
5211 #print "APW: ALLOWED: cond<$cond>\n";
5212 $allowed = 1;
5213 }
5214 if ($block =~/\b(?:if|for|while)\b/) {
5215 #print "APW: ALLOWED: block<$block>\n";
5216 $allowed = 1;
5217 }
5218 if (statement_block_size($block) > 1) {
5219 #print "APW: ALLOWED: lines block<$block>\n";
5220 $allowed = 1;
5221 }
5222 # Check the post-context.
5223 if (defined $chunks[1]) {
5224 my ($cond, $block) = @{$chunks[1]};
5225 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005226 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005227 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005228 if ($block =~ /^\s*\{/) {
5229 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5230 $allowed = 1;
5231 }
5232 }
5233 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005234 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005235 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005236
Andy Whitcroftf0556632008-10-15 22:02:23 -07005237 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005238 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005239 }
5240
Joe Perches000d1cc12011-07-25 17:13:25 -07005241 WARN("BRACES",
5242 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005243 }
5244 }
5245
Joe Perches0979ae62012-12-17 16:01:59 -08005246# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005247 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005248 if (CHK("BRACES",
5249 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5250 $fix && $prevrawline =~ /^\+/) {
5251 fix_delete_line($fixlinenr - 1, $prevrawline);
5252 }
Joe Perches0979ae62012-12-17 16:01:59 -08005253 }
Joe Perches77b9a532013-07-03 15:05:29 -07005254 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005255 if (CHK("BRACES",
5256 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5257 $fix) {
5258 fix_delete_line($fixlinenr, $rawline);
5259 }
Joe Perches0979ae62012-12-17 16:01:59 -08005260 }
5261
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005262# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005263 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5264 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005265 WARN("VOLATILE",
5266 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005267 }
5268
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005269# Check for user-visible strings broken across lines, which breaks the ability
5270# to grep for the string. Make exceptions when the previous string ends in a
5271# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5272# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005273 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005274 $prevline =~ /"\s*$/ &&
5275 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5276 if (WARN("SPLIT_STRING",
5277 "quoted string split across lines\n" . $hereprev) &&
5278 $fix &&
5279 $prevrawline =~ /^\+.*"\s*$/ &&
5280 $last_coalesced_string_linenr != $linenr - 1) {
5281 my $extracted_string = get_quoted_string($line, $rawline);
5282 my $comma_close = "";
5283 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5284 $comma_close = $1;
5285 }
5286
5287 fix_delete_line($fixlinenr - 1, $prevrawline);
5288 fix_delete_line($fixlinenr, $rawline);
5289 my $fixedline = $prevrawline;
5290 $fixedline =~ s/"\s*$//;
5291 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5292 fix_insert_line($fixlinenr - 1, $fixedline);
5293 $fixedline = $rawline;
5294 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5295 if ($fixedline !~ /\+\s*$/) {
5296 fix_insert_line($fixlinenr, $fixedline);
5297 }
5298 $last_coalesced_string_linenr = $linenr;
5299 }
5300 }
5301
5302# check for missing a space in a string concatenation
5303 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5304 WARN('MISSING_SPACE',
5305 "break quoted strings at a space character\n" . $hereprev);
5306 }
5307
5308# check for spaces before a quoted newline
5309 if ($rawline =~ /^.*\".*\s\\n/) {
5310 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5311 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5312 $fix) {
5313 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5314 }
5315
5316 }
5317
Joe Perchesf17dba42014-10-13 15:51:51 -07005318# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005319 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005320 CHK("CONCATENATED_STRING",
5321 "Concatenated strings should use spaces between elements\n" . $herecurr);
5322 }
5323
Joe Perches90ad30e2014-12-10 15:51:59 -08005324# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005325 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005326 WARN("STRING_FRAGMENTS",
5327 "Consecutive strings are generally better as a single string\n" . $herecurr);
5328 }
5329
Joe Perches6e300752015-09-09 15:37:47 -07005330# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005331 my $string;
5332 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5333 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5334 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005335 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005336 WARN("PRINTF_L",
5337 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5338 last;
5339 }
Joe Perches6e300752015-09-09 15:37:47 -07005340 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5341 ERROR("PRINTF_0xDECIMAL",
5342 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5343 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005344 }
5345
5346# check for line continuations in quoted strings with odd counts of "
5347 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5348 WARN("LINE_CONTINUATIONS",
5349 "Avoid line continuations in quoted strings\n" . $herecurr);
5350 }
5351
Gregory Bean6c9c6842011-03-17 14:18:20 -07005352# sys_open/read/write/close are not allowed in the kernel
5353 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5354 ERROR("FILE_OPS",
5355 "$1 is inappropriate in kernel code.\n" .
5356 $herecurr);
5357 }
5358
Gregory Beana7cce0a2011-06-07 08:06:45 -07005359# filp_open is a backdoor for sys_open
5360 if ($line =~ /\b(filp_open)\b/) {
5361 ERROR("FILE_OPS",
5362 "$1 is inappropriate in kernel code.\n" .
5363 $herecurr);
5364 }
5365
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005366# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5367 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5368 ERROR("NON_RELAXED_IO",
5369 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5370 "with appropriate memory barriers instead.\n" .
5371 $herecurr);
5372 }
5373
5374# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5375 if ($line =~ /\b((in|out)([bwl]))\b/) {
5376 my ($all, $pref, $suf) = ($1, $2, $3);
5377 $pref =~ s/in/read/;
5378 $pref =~ s/out/write/;
5379 ERROR("NON_RELAXED_IO",
5380 "Use of $all is deprecated: use " .
5381 "__raw_$pref$suf\n\t" .
5382 "with appropriate memory barriers instead.\n" .
5383 $herecurr);
5384 }
5385
Gregory Bean438bf5f2011-06-15 09:32:38 -07005386# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005387 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005388 WARN("ARM_BARRIER",
5389 "Use of dsb is discouranged: prefer mb.\n" .
5390 $herecurr);
5391 }
5392
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005393# unbounded string functions are overflow risks
5394 my %str_fns = (
5395 "sprintf" => "snprintf",
5396 "strcpy" => "strlcpy",
5397 "strncpy" => "strlcpy",
5398 "strcat" => "strlcat",
5399 "strncat" => "strlcat",
5400 "vsprintf" => "vsnprintf",
5401 "strchr" => "strnchr",
5402 "strstr" => "strnstr",
5403 );
5404 foreach my $k (keys %str_fns) {
5405 if ($line =~ /\b$k\b/) {
5406 ERROR("UNBOUNDED_STRING_FNS",
5407 "Use of $k is deprecated: " .
5408 "use $str_fns{$k} instead.\n" .
5409 $herecurr);
5410 }
5411 }
5412
Andy Whitcroft00df3442007-06-08 13:47:06 -07005413# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005414 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005415 WARN("IF_0",
5416 "if this code is redundant consider removing it\n"
5417 . $herecurr);
5418 }
5419
5420# warn about #if 1
5421 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5422 WARN("IF_1",
5423 "if this code is required consider removing"
5424 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005425 }
5426
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005427# check for needless "if (<foo>) fn(<foo>)" uses
5428 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005429 my $tested = quotemeta($1);
5430 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5431 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5432 my $func = $1;
5433 if (WARN('NEEDLESS_IF',
5434 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5435 $fix) {
5436 my $do_fix = 1;
5437 my $leading_tabs = "";
5438 my $new_leading_tabs = "";
5439 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5440 $leading_tabs = $1;
5441 } else {
5442 $do_fix = 0;
5443 }
5444 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5445 $new_leading_tabs = $1;
5446 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5447 $do_fix = 0;
5448 }
5449 } else {
5450 $do_fix = 0;
5451 }
5452 if ($do_fix) {
5453 fix_delete_line($fixlinenr - 1, $prevrawline);
5454 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5455 }
5456 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005457 }
5458 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005459
Joe Perchesebfdc402014-08-06 16:10:27 -07005460# check for unnecessary "Out of Memory" messages
5461 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5462 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5463 (defined $1 || defined $3) &&
5464 $linenr > 3) {
5465 my $testval = $2;
5466 my $testline = $lines[$linenr - 3];
5467
5468 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5469# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5470
5471 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5472 WARN("OOM_MESSAGE",
5473 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5474 }
5475 }
5476
Joe Perchesf78d98f2014-10-13 15:52:01 -07005477# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005478 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005479 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5480 my $level = $1;
5481 if (WARN("UNNECESSARY_KERN_LEVEL",
5482 "Possible unnecessary $level\n" . $herecurr) &&
5483 $fix) {
5484 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5485 }
5486 }
5487
Joe Perchesabb08a52014-12-10 15:51:46 -08005488# check for mask then right shift without a parentheses
5489 if ($^V && $^V ge 5.10.0 &&
5490 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5491 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5492 WARN("MASK_THEN_SHIFT",
5493 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5494 }
5495
Joe Perchesb75ac612014-12-10 15:52:02 -08005496# check for pointer comparisons to NULL
5497 if ($^V && $^V ge 5.10.0) {
5498 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5499 my $val = $1;
5500 my $equal = "!";
5501 $equal = "" if ($4 eq "!=");
5502 if (CHK("COMPARISON_TO_NULL",
5503 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5504 $fix) {
5505 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5506 }
5507 }
5508 }
5509
Joe Perches8716de32013-09-11 14:24:05 -07005510# check for bad placement of section $InitAttribute (e.g.: __initdata)
5511 if ($line =~ /(\b$InitAttribute\b)/) {
5512 my $attr = $1;
5513 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5514 my $ptr = $1;
5515 my $var = $2;
5516 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5517 ERROR("MISPLACED_INIT",
5518 "$attr should be placed after $var\n" . $herecurr)) ||
5519 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5520 WARN("MISPLACED_INIT",
5521 "$attr should be placed after $var\n" . $herecurr))) &&
5522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005523 $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 -07005524 }
5525 }
5526 }
5527
Joe Perchese970b882013-11-12 15:10:10 -08005528# check for $InitAttributeData (ie: __initdata) with const
5529 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5530 my $attr = $1;
5531 $attr =~ /($InitAttributePrefix)(.*)/;
5532 my $attr_prefix = $1;
5533 my $attr_type = $2;
5534 if (ERROR("INIT_ATTRIBUTE",
5535 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5536 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005537 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005538 s/$InitAttributeData/${attr_prefix}initconst/;
5539 }
5540 }
5541
5542# check for $InitAttributeConst (ie: __initconst) without const
5543 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5544 my $attr = $1;
5545 if (ERROR("INIT_ATTRIBUTE",
5546 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5547 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005548 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005549 /(^\+\s*(?:static\s+))/;
5550 $lead = rtrim($1);
5551 $lead = "$lead " if ($lead !~ /^\+$/);
5552 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005553 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005554 }
5555 }
5556
Joe Perchesc17893c2015-04-16 12:44:42 -07005557# check for __read_mostly with const non-pointer (should just be const)
5558 if ($line =~ /\b__read_mostly\b/ &&
5559 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5560 if (ERROR("CONST_READ_MOSTLY",
5561 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5562 $fix) {
5563 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5564 }
5565 }
5566
Joe Perchesfbdb8132014-04-03 14:49:14 -07005567# don't use __constant_<foo> functions outside of include/uapi/
5568 if ($realfile !~ m@^include/uapi/@ &&
5569 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5570 my $constant_func = $1;
5571 my $func = $constant_func;
5572 $func =~ s/^__constant_//;
5573 if (WARN("CONSTANT_CONVERSION",
5574 "$constant_func should be $func\n" . $herecurr) &&
5575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005576 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005577 }
5578 }
5579
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005580# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005581 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005582 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005583 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005584 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005585 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005586 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5587 }
5588 if ($delay > 2000) {
5589 WARN("LONG_UDELAY",
5590 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005591 }
5592 }
5593
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005594# warn about unexpectedly long msleep's
5595 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5596 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005597 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005598 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005599 }
5600 }
5601
Joe Perches36ec1932013-07-03 15:05:25 -07005602# check for comparisons of jiffies
5603 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5604 WARN("JIFFIES_COMPARISON",
5605 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5606 }
5607
Joe Perches9d7a34a2013-07-03 15:05:26 -07005608# check for comparisons of get_jiffies_64()
5609 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5610 WARN("JIFFIES_COMPARISON",
5611 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5612 }
5613
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005614# check the patch for use of mdelay
5615 if ($line =~ /\bmdelay\s*\(/) {
5616 WARN("MDELAY",
5617 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5618 }
5619
Andy Whitcroft00df3442007-06-08 13:47:06 -07005620# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005621# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005622# print "#ifdef in C files should be avoided\n";
5623# print "$herecurr";
5624# $clean = 0;
5625# }
5626
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005627# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005628 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005629 if (ERROR("SPACING",
5630 "exactly one space required after that #$1\n" . $herecurr) &&
5631 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005632 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005633 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5634 }
5635
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005636 }
5637
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005638# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005639 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5640 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005641 my $which = $1;
5642 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005643 CHK("UNCOMMENTED_DEFINITION",
5644 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005645 }
5646 }
5647# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005648
5649 my $barriers = qr{
5650 mb|
5651 rmb|
5652 wmb|
5653 read_barrier_depends
5654 }x;
5655 my $barrier_stems = qr{
5656 mb__before_atomic|
5657 mb__after_atomic|
5658 store_release|
5659 load_acquire|
5660 store_mb|
5661 (?:$barriers)
5662 }x;
5663 my $all_barriers = qr{
5664 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005665 smp_(?:$barrier_stems)|
5666 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005667 }x;
5668
5669 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005670 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005671 WARN("MEMORY_BARRIER",
5672 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005673 }
5674 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005675
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005676 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5677
5678 if ($realfile !~ m@^include/asm-generic/@ &&
5679 $realfile !~ m@/barrier\.h$@ &&
5680 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5681 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5682 WARN("MEMORY_BARRIER",
5683 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5684 }
5685
Joe Perchescb426e92015-06-25 15:02:46 -07005686# check for waitqueue_active without a comment.
5687 if ($line =~ /\bwaitqueue_active\s*\(/) {
5688 if (!ctx_has_comment($first_line, $linenr)) {
5689 WARN("WAITQUEUE_ACTIVE",
5690 "waitqueue_active without comment\n" . $herecurr);
5691 }
5692 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005693
5694# Check for expedited grace periods that interrupt non-idle non-nohz
5695# online CPUs. These expedited can therefore degrade real-time response
5696# if used carelessly, and should be avoided where not absolutely
5697# needed. It is always OK to use synchronize_rcu_expedited() and
5698# synchronize_sched_expedited() at boot time (before real-time applications
5699# start) and in error situations where real-time response is compromised in
5700# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5701# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5702# Of course, nothing comes for free, and srcu_read_lock() and
5703# srcu_read_unlock() do contain full memory barriers in payment for
5704# synchronize_srcu_expedited() non-interruption properties.
5705 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5706 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5707 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5708
5709 }
5710
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005711# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005712 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005713 CHK("ARCH_DEFINES",
5714 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005715 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005716
Tobias Klauserd4977c72010-05-24 14:33:30 -07005717# Check that the storage class is at the beginning of a declaration
5718 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005719 WARN("STORAGE_CLASS",
5720 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005721 }
5722
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005723# check the location of the inline attribute, that it is between
5724# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005725 if ($line =~ /\b$Type\s+$Inline\b/ ||
5726 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005727 ERROR("INLINE_LOCATION",
5728 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005729 }
5730
Andy Whitcroft8905a672007-11-28 16:21:06 -08005731# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005732 if ($realfile !~ m@\binclude/uapi/@ &&
5733 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005734 if (WARN("INLINE",
5735 "plain inline is preferred over $1\n" . $herecurr) &&
5736 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005737 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005738
5739 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005740 }
5741
Joe Perches3d130fd2011-01-12 17:00:00 -08005742# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005743 if ($realfile !~ m@\binclude/uapi/@ &&
5744 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005745 WARN("PREFER_PACKED",
5746 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005747 }
5748
Joe Perches39b7e282011-07-25 17:13:24 -07005749# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005750 if ($realfile !~ m@\binclude/uapi/@ &&
5751 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005752 WARN("PREFER_ALIGNED",
5753 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005754 }
5755
Joe Perches5f14d3b2012-01-10 15:09:52 -08005756# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005757 if ($realfile !~ m@\binclude/uapi/@ &&
5758 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005759 if (WARN("PREFER_PRINTF",
5760 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005762 $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 -07005763
5764 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005765 }
5766
Joe Perches6061d942012-03-23 15:02:16 -07005767# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005768 if ($realfile !~ m@\binclude/uapi/@ &&
5769 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005770 if (WARN("PREFER_SCANF",
5771 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5772 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005773 $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 -07005774 }
Joe Perches6061d942012-03-23 15:02:16 -07005775 }
5776
Joe Perches619a9082014-12-10 15:51:35 -08005777# Check for __attribute__ weak, or __weak declarations (may have link issues)
5778 if ($^V && $^V ge 5.10.0 &&
5779 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5780 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5781 $line =~ /\b__weak\b/)) {
5782 ERROR("WEAK_DECLARATION",
5783 "Using weak declarations can have unintended link defects\n" . $herecurr);
5784 }
5785
Joe Perchese6176fa2015-06-25 15:02:49 -07005786# check for c99 types like uint8_t used outside of uapi/
5787 if ($realfile !~ m@\binclude/uapi/@ &&
5788 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5789 my $type = $1;
5790 if ($type =~ /\b($typeC99Typedefs)\b/) {
5791 $type = $1;
5792 my $kernel_type = 'u';
5793 $kernel_type = 's' if ($type =~ /^_*[si]/);
5794 $type =~ /(\d+)/;
5795 $kernel_type .= $1;
5796 if (CHK("PREFER_KERNEL_TYPES",
5797 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5798 $fix) {
5799 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5800 }
5801 }
5802 }
5803
Joe Perches938224b2016-01-20 14:59:15 -08005804# check for cast of C90 native int or longer types constants
5805 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5806 my $cast = $1;
5807 my $const = $2;
5808 if (WARN("TYPECAST_INT_CONSTANT",
5809 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5810 $fix) {
5811 my $suffix = "";
5812 my $newconst = $const;
5813 $newconst =~ s/${Int_type}$//;
5814 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5815 if ($cast =~ /\blong\s+long\b/) {
5816 $suffix .= 'LL';
5817 } elsif ($cast =~ /\blong\b/) {
5818 $suffix .= 'L';
5819 }
5820 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5821 }
5822 }
5823
Joe Perches8f53a9b2010-03-05 13:43:48 -08005824# check for sizeof(&)
5825 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005826 WARN("SIZEOF_ADDRESS",
5827 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005828 }
5829
Joe Perches66c80b62012-07-30 14:41:22 -07005830# check for sizeof without parenthesis
5831 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005832 if (WARN("SIZEOF_PARENTHESIS",
5833 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005835 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005836 }
Joe Perches66c80b62012-07-30 14:41:22 -07005837 }
5838
Joe Perches88982fe2012-12-17 16:02:00 -08005839# check for struct spinlock declarations
5840 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5841 WARN("USE_SPINLOCK_T",
5842 "struct spinlock should be spinlock_t\n" . $herecurr);
5843 }
5844
Joe Perchesa6962d72013-04-29 16:18:13 -07005845# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005846 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005847 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005848 $fmt =~ s/%%//g;
5849 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005850 if (WARN("PREFER_SEQ_PUTS",
5851 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5852 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005853 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005854 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005855 }
5856 }
5857
Andy Whitcroft554e1652012-01-10 15:09:57 -08005858# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005859 if ($^V && $^V ge 5.10.0 &&
5860 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005861 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005862
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005863 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005864 my $ms_val = $7;
5865 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005866
Andy Whitcroft554e1652012-01-10 15:09:57 -08005867 if ($ms_size =~ /^(0x|)0$/i) {
5868 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005869 "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 -08005870 } elsif ($ms_size =~ /^(0x|)1$/i) {
5871 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005872 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5873 }
5874 }
5875
Joe Perches98a9bba2014-01-23 15:54:52 -08005876# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005877# if ($^V && $^V ge 5.10.0 &&
5878# defined $stat &&
5879# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5880# if (WARN("PREFER_ETHER_ADDR_COPY",
5881# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5882# $fix) {
5883# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5884# }
5885# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005886
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005887# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005888# if ($^V && $^V ge 5.10.0 &&
5889# defined $stat &&
5890# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5891# WARN("PREFER_ETHER_ADDR_EQUAL",
5892# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5893# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005894
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005895# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5896# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005897# if ($^V && $^V ge 5.10.0 &&
5898# defined $stat &&
5899# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5900#
5901# my $ms_val = $7;
5902#
5903# if ($ms_val =~ /^(?:0x|)0+$/i) {
5904# if (WARN("PREFER_ETH_ZERO_ADDR",
5905# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5906# $fix) {
5907# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5908# }
5909# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5910# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5911# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5912# $fix) {
5913# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5914# }
5915# }
5916# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005917
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005918# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005919 if ($^V && $^V ge 5.10.0 &&
5920 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005921 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005922 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005923 my $call = $1;
5924 my $cast1 = deparenthesize($2);
5925 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005926 my $cast2 = deparenthesize($7);
5927 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005928 my $cast;
5929
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005930 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005931 $cast = "$cast1 or $cast2";
5932 } elsif ($cast1 ne "") {
5933 $cast = $cast1;
5934 } else {
5935 $cast = $cast2;
5936 }
5937 WARN("MINMAX",
5938 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005939 }
5940 }
5941
Joe Perches4a273192012-07-30 14:41:20 -07005942# check usleep_range arguments
5943 if ($^V && $^V ge 5.10.0 &&
5944 defined $stat &&
5945 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5946 my $min = $1;
5947 my $max = $7;
5948 if ($min eq $max) {
5949 WARN("USLEEP_RANGE",
5950 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5951 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5952 $min > $max) {
5953 WARN("USLEEP_RANGE",
5954 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5955 }
5956 }
5957
Joe Perches823b7942013-11-12 15:10:15 -08005958# check for naked sscanf
5959 if ($^V && $^V ge 5.10.0 &&
5960 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005961 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005962 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5963 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5964 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5965 my $lc = $stat =~ tr@\n@@;
5966 $lc = $lc + $linenr;
5967 my $stat_real = raw_line($linenr, 0);
5968 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5969 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5970 }
5971 WARN("NAKED_SSCANF",
5972 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5973 }
5974
Joe Perchesafc819a2014-06-04 16:12:08 -07005975# check for simple sscanf that should be kstrto<foo>
5976 if ($^V && $^V ge 5.10.0 &&
5977 defined $stat &&
5978 $line =~ /\bsscanf\b/) {
5979 my $lc = $stat =~ tr@\n@@;
5980 $lc = $lc + $linenr;
5981 my $stat_real = raw_line($linenr, 0);
5982 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5983 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5984 }
5985 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5986 my $format = $6;
5987 my $count = $format =~ tr@%@%@;
5988 if ($count == 1 &&
5989 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5990 WARN("SSCANF_TO_KSTRTO",
5991 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5992 }
5993 }
5994 }
5995
Joe Perches70dc8a42013-09-11 14:23:58 -07005996# check for new externs in .h files.
5997 if ($realfile =~ /\.h$/ &&
5998 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005999 if (CHK("AVOID_EXTERNS",
6000 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006001 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006002 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006003 }
6004 }
6005
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006006# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006007 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006008 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006009 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006010 my $function_name = $1;
6011 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006012
6013 my $s = $stat;
6014 if (defined $cond) {
6015 substr($s, 0, length($cond), '');
6016 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006017 if ($s =~ /^\s*;/ &&
6018 $function_name ne 'uninitialized_var')
6019 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006020 WARN("AVOID_EXTERNS",
6021 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006022 }
6023
6024 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006025 WARN("FUNCTION_ARGUMENTS",
6026 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006027 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006028
6029 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6030 $stat =~ /^.\s*extern\s+/)
6031 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006032 WARN("AVOID_EXTERNS",
6033 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006034 }
6035
Joe Perchesca0d8922016-10-11 13:52:16 -07006036 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6037 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6038 $1 ne "void") {
6039 my $args = trim($1);
6040 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6041 my $arg = trim($1);
6042 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6043 WARN("FUNCTION_ARGUMENTS",
6044 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6045 }
6046 }
6047 }
6048
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006049# checks for new __setup's
6050 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6051 my $name = $1;
6052
6053 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006054 CHK("UNDOCUMENTED_SETUP",
6055 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006056 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006057 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006058
6059# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006060 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006061 WARN("UNNECESSARY_CASTS",
6062 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006063 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006064
Joe Perchesa640d252013-07-03 15:05:21 -07006065# alloc style
6066# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6067 if ($^V && $^V ge 5.10.0 &&
6068 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6069 CHK("ALLOC_SIZEOF_STRUCT",
6070 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6071 }
6072
Joe Perches60a55362014-06-04 16:12:07 -07006073# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6074 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006075 $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 -07006076 my $oldfunc = $3;
6077 my $a1 = $4;
6078 my $a2 = $10;
6079 my $newfunc = "kmalloc_array";
6080 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006081 my $r1 = $a1;
6082 my $r2 = $a2;
6083 if ($a1 =~ /^sizeof\s*\S/) {
6084 $r1 = $a2;
6085 $r2 = $a1;
6086 }
6087 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6088 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006089 if (WARN("ALLOC_WITH_MULTIPLY",
6090 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6091 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006092 $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 -07006093
6094 }
6095 }
6096 }
6097
Joe Perches972fdea2013-04-29 16:18:12 -07006098# check for krealloc arg reuse
6099 if ($^V && $^V ge 5.10.0 &&
6100 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6101 WARN("KREALLOC_ARG_REUSE",
6102 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6103 }
6104
Joe Perches5ce59ae2013-02-21 16:44:18 -08006105# check for alloc argument mismatch
6106 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6107 WARN("ALLOC_ARRAY_ARGS",
6108 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6109 }
6110
Joe Perchescaf2a542011-01-12 16:59:56 -08006111# check for multiple semicolons
6112 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006113 if (WARN("ONE_SEMICOLON",
6114 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6115 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006116 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006117 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006118 }
6119
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006120# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6121 if ($realfile !~ m@^include/uapi/@ &&
6122 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006123 my $ull = "";
6124 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6125 if (CHK("BIT_MACRO",
6126 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6127 $fix) {
6128 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6129 }
6130 }
6131
Joe Perches2d632742016-05-20 17:04:00 -07006132# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6133 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6134 my $config = $1;
6135 if (WARN("PREFER_IS_ENABLED",
6136 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6137 $fix) {
6138 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6139 }
6140 }
6141
Joe Perchese81f2392014-08-06 16:11:25 -07006142# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006143 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6144 my $has_break = 0;
6145 my $has_statement = 0;
6146 my $count = 0;
6147 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006148 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006149 $prevline--;
6150 my $rline = $rawlines[$prevline - 1];
6151 my $fline = $lines[$prevline - 1];
6152 last if ($fline =~ /^\@\@/);
6153 next if ($fline =~ /^\-/);
6154 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6155 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6156 next if ($fline =~ /^.[\s$;]*$/);
6157 $has_statement = 1;
6158 $count++;
6159 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6160 }
6161 if (!$has_break && $has_statement) {
6162 WARN("MISSING_BREAK",
6163 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6164 }
6165 }
6166
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006167# check for switch/default statements without a break;
6168 if ($^V && $^V ge 5.10.0 &&
6169 defined $stat &&
6170 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6171 my $ctx = '';
6172 my $herectx = $here . "\n";
6173 my $cnt = statement_rawlines($stat);
6174 for (my $n = 0; $n < $cnt; $n++) {
6175 $herectx .= raw_line($linenr, $n) . "\n";
6176 }
6177 WARN("DEFAULT_NO_BREAK",
6178 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006179 }
6180
Patrick Pannutof370e252010-07-23 13:34:18 -07006181# check for return codes on error paths
6182 if ($line =~ /\breturn\s+-\d+/) {
6183 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006184 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006185 }
6186
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006187# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006188 if ($line =~ /\b__FUNCTION__\b/) {
6189 if (WARN("USE_FUNC",
6190 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6191 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006192 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006193 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006194 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006195
Joe Perches62ec8182015-02-13 14:38:18 -08006196# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6197 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6198 ERROR("DATE_TIME",
6199 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6200 }
6201
Joe Perches2c924882012-03-23 15:02:20 -07006202# check for use of yield()
6203 if ($line =~ /\byield\s*\(\s*\)/) {
6204 WARN("YIELD",
6205 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6206 }
6207
Joe Perches179f8f42013-07-03 15:05:30 -07006208# check for comparisons against true and false
6209 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6210 my $lead = $1;
6211 my $arg = $2;
6212 my $test = $3;
6213 my $otype = $4;
6214 my $trail = $5;
6215 my $op = "!";
6216
6217 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6218
6219 my $type = lc($otype);
6220 if ($type =~ /^(?:true|false)$/) {
6221 if (("$test" eq "==" && "$type" eq "true") ||
6222 ("$test" eq "!=" && "$type" eq "false")) {
6223 $op = "";
6224 }
6225
6226 CHK("BOOL_COMPARISON",
6227 "Using comparison to $otype is error prone\n" . $herecurr);
6228
6229## maybe suggesting a correct construct would better
6230## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6231
6232 }
6233 }
6234
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006235# check for semaphores initialized locked
6236 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006237 WARN("CONSIDER_COMPLETION",
6238 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006239 }
Joe Perches6712d852012-03-23 15:02:20 -07006240
Joe Perches67d0a072011-10-31 17:13:10 -07006241# recommend kstrto* over simple_strto* and strict_strto*
6242 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006243 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006244 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006245 }
Joe Perches6712d852012-03-23 15:02:20 -07006246
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006247# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006248 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006249 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006250 "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 -07006251 }
Joe Perches6712d852012-03-23 15:02:20 -07006252
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006253# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006254 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006255 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006256 WARN("CONST_STRUCT",
6257 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006258 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006259 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006260
6261# use of NR_CPUS is usually wrong
6262# ignore definitions of NR_CPUS and usage to define arrays as likely right
6263 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006264 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6265 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006266 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6267 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6268 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006269 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006270 WARN("NR_CPUS",
6271 "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 -07006272 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006273
Joe Perches52ea8502013-11-12 15:10:09 -08006274# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6275 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6276 ERROR("DEFINE_ARCH_HAS",
6277 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6278 }
6279
Joe Perchesacd93622015-02-13 14:38:38 -08006280# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6281 if ($^V && $^V ge 5.10.0 &&
6282 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6283 WARN("LIKELY_MISUSE",
6284 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6285 }
6286
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006287# whine mightly about in_atomic
6288 if ($line =~ /\bin_atomic\s*\(/) {
6289 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006290 ERROR("IN_ATOMIC",
6291 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006292 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006293 WARN("IN_ATOMIC",
6294 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006295 }
6296 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006297
Joe Perches481aea52016-05-20 17:04:08 -07006298# whine about ACCESS_ONCE
6299 if ($^V && $^V ge 5.10.0 &&
6300 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6301 my $par = $1;
6302 my $eq = $2;
6303 my $fun = $3;
6304 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6305 if (defined($eq)) {
6306 if (WARN("PREFER_WRITE_ONCE",
6307 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6308 $fix) {
6309 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6310 }
6311 } else {
6312 if (WARN("PREFER_READ_ONCE",
6313 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6314 $fix) {
6315 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6316 }
6317 }
6318 }
6319
Peter Zijlstra1704f472010-03-19 01:37:42 +01006320# check for lockdep_set_novalidate_class
6321 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6322 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6323 if ($realfile !~ m@^kernel/lockdep@ &&
6324 $realfile !~ m@^include/linux/lockdep@ &&
6325 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006326 ERROR("LOCKDEP",
6327 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006328 }
6329 }
Dave Jones88f88312011-01-12 16:59:59 -08006330
Joe Perchesb392c642015-04-16 12:44:16 -07006331 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6332 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006333 WARN("EXPORTED_WORLD_WRITABLE",
6334 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006335 }
Joe Perches24358802014-04-03 14:49:13 -07006336
Joe Perches515a2352014-04-03 14:49:24 -07006337# Mode permission misuses where it seems decimal should be octal
6338# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6339 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006340 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006341 $line =~ /$mode_perms_search/) {
6342 foreach my $entry (@mode_permission_funcs) {
6343 my $func = $entry->[0];
6344 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006345
Joe Perches459cf0a2016-10-11 13:52:19 -07006346 my $lc = $stat =~ tr@\n@@;
6347 $lc = $lc + $linenr;
6348 my $stat_real = raw_line($linenr, 0);
6349 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6350 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6351 }
6352
Joe Perches515a2352014-04-03 14:49:24 -07006353 my $skip_args = "";
6354 if ($arg_pos > 1) {
6355 $arg_pos--;
6356 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6357 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006358 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006359 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006360 my $val = $1;
6361 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006362 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6363 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006364 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006365 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006366 }
6367 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006368 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006369 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006370 }
Joe Perches24358802014-04-03 14:49:13 -07006371 }
6372 }
6373 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006374
Joe Perches459cf0a2016-10-11 13:52:19 -07006375# check for uses of S_<PERMS> that could be octal for readability
6376 if ($line =~ /\b$mode_perms_string_search\b/) {
6377 my $val = "";
6378 my $oval = "";
6379 my $to = 0;
6380 my $curpos = 0;
6381 my $lastpos = 0;
6382 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6383 $curpos = pos($line);
6384 my $match = $2;
6385 my $omatch = $1;
6386 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6387 $lastpos = $curpos;
6388 $to |= $mode_permission_string_types{$match};
6389 $val .= '\s*\|\s*' if ($val ne "");
6390 $val .= $match;
6391 $oval .= $omatch;
6392 }
6393 $oval =~ s/^\s*\|\s*//;
6394 $oval =~ s/\s*\|\s*$//;
6395 my $octal = sprintf("%04o", $to);
6396 if (WARN("SYMBOLIC_PERMS",
6397 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6398 $fix) {
6399 $fixed[$fixlinenr] =~ s/$val/$octal/;
6400 }
6401 }
6402
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006403# validate content of MODULE_LICENSE against list from include/linux/module.h
6404 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6405 my $extracted_string = get_quoted_string($line, $rawline);
6406 my $valid_licenses = qr{
6407 GPL|
6408 GPL\ v2|
6409 GPL\ and\ additional\ rights|
6410 Dual\ BSD/GPL|
6411 Dual\ MIT/GPL|
6412 Dual\ MPL/GPL|
6413 Proprietary
6414 }x;
6415 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6416 WARN("MODULE_LICENSE",
6417 "unknown module license " . $extracted_string . "\n" . $herecurr);
6418 }
6419 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006420 }
6421
6422 # If we have no input at all, then there is nothing to report on
6423 # so just keep quiet.
6424 if ($#rawlines == -1) {
6425 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006426 }
6427
Andy Whitcroft8905a672007-11-28 16:21:06 -08006428 # In mailback mode only produce a report in the negative, for
6429 # things that appear to be patches.
6430 if ($mailback && ($clean == 1 || !$is_patch)) {
6431 exit(0);
6432 }
6433
6434 # This is not a patch, and we are are in 'no-patch' mode so
6435 # just keep quiet.
6436 if (!$chk_patch && !$is_patch) {
6437 exit(0);
6438 }
6439
Joe Perches06330fc2015-06-25 15:03:11 -07006440 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006441 ERROR("NOT_UNIFIED_DIFF",
6442 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006443 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006444 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006445 ERROR("MISSING_SIGN_OFF",
6446 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006447 }
6448
Andy Whitcroft8905a672007-11-28 16:21:06 -08006449 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006450 if ($summary && !($clean == 1 && $quiet == 1)) {
6451 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006452 print "total: $cnt_error errors, $cnt_warn warnings, " .
6453 (($check)? "$cnt_chk checks, " : "") .
6454 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006455 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006456
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006457 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006458 # If there were any defects found and not already fixing them
6459 if (!$clean and !$fix) {
6460 print << "EOM"
6461
6462NOTE: For some of the reported defects, checkpatch may be able to
6463 mechanically convert to the typical style using --fix or --fix-inplace.
6464EOM
6465 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006466 # If there were whitespace errors which cleanpatch can fix
6467 # then suggest that.
6468 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006469 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006470 print << "EOM"
6471
6472NOTE: Whitespace errors detected.
6473 You may wish to use scripts/cleanpatch or scripts/cleanfile
6474EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006475 }
6476 }
6477
Joe Perchesd752fcc2014-08-06 16:11:05 -07006478 if ($clean == 0 && $fix &&
6479 ("@rawlines" ne "@fixed" ||
6480 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006481 my $newfile = $filename;
6482 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006483 my $linecount = 0;
6484 my $f;
6485
Joe Perchesd752fcc2014-08-06 16:11:05 -07006486 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6487
Joe Perches3705ce52013-07-03 15:05:31 -07006488 open($f, '>', $newfile)
6489 or die "$P: Can't open $newfile for write\n";
6490 foreach my $fixed_line (@fixed) {
6491 $linecount++;
6492 if ($file) {
6493 if ($linecount > 3) {
6494 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006495 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006496 }
6497 } else {
6498 print $f $fixed_line . "\n";
6499 }
6500 }
6501 close($f);
6502
6503 if (!$quiet) {
6504 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006505
Joe Perches3705ce52013-07-03 15:05:31 -07006506Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6507
6508Do _NOT_ trust the results written to this file.
6509Do _NOT_ submit these changes without inspecting them for correctness.
6510
6511This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6512No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006513EOM
6514 }
6515 }
6516
Joe Perchesd8469f12015-06-25 15:03:00 -07006517 if ($quiet == 0) {
6518 print "\n";
6519 if ($clean == 1) {
6520 print "$vname has no obvious style problems and is ready for submission.\n";
6521 } else {
6522 print "$vname has style problems, please review.\n";
6523 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006524 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006525 return $clean;
6526}