blob: 34e4683de7a3fe524d86d3046ab2b8eb322d3ffd [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Joe Perches882ea1d2018-06-07 17:04:33 -07002# SPDX-License-Identifier: GPL-2.0
3#
Dave Jonesdbf004d2010-01-12 16:59:52 -05004# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07005# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08006# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07007# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Joe Perches882ea1d2018-06-07 17:04:33 -07008# (c) 2010-2018 Joe Perches <joe@perches.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07009
10use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020011use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070012use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080013use File::Basename;
14use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070015use Term::ANSIColor qw(:constants);
Geert Uytterhoevencd261492018-08-21 21:57:40 -070016use Encode qw(decode encode);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080019my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070020
Joe Perches000d1cc12011-07-25 17:13:25 -070021my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070022
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070029my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080031my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070032my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070034my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070035my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070037my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080038my $summary = 1;
39my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080040my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070041my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070042my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070043my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080044my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070045my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046my %debug;
Joe Perches34456862013-07-03 15:05:34 -070047my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070048my %use_type = ();
49my @use = ();
50my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070051my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070052my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070053my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080054my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070057my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070058my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070059my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070060my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070061my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070062my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070063my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070064my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070065
66sub help {
67 my ($exitcode) = @_;
68
69 print << "EOM";
70Usage: $P [OPTION]... [FILE]...
71Version: $V
72
73Options:
74 -q, --quiet quiet
75 --no-tree run without a kernel tree
76 --no-signoff do not check for 'Signed-off-by' line
77 --patch treat FILE as patchfile (default)
78 --emacs emacs compile window format
79 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070080 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070081 -g, --git treat FILE as a single commit or git revision range
82 single git commit with:
83 <rev>
84 <rev>^
85 <rev>~n
86 multiple git commits with:
87 <rev1>..<rev2>
88 <rev1>...<rev2>
89 <rev>-<count>
90 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -f, --file treat FILE as regular source file
92 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070094 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070095 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070096 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080097 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070098 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070099 --root=PATH PATH to the kernel tree root
100 --no-summary suppress the per-file summary
101 --mailback only produce a report in case of warnings/errors
102 --summary-file include the filename in summary
103 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
104 'values', 'possible', 'type', and 'attr' (default
105 is all off)
106 --test-only=WORD report only warnings/errors containing WORD
107 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700108 --fix EXPERIMENTAL - may create horrible results
109 If correctable single-line errors exist, create
110 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
111 with potential errors corrected to the preferred
112 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800113 --fix-inplace EXPERIMENTAL - may create horrible results
114 Is the same as --fix, but overwrites the input
115 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700116 --ignore-perl-version override checking of perl version. expect
117 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700118 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700119 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700121 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700122 --color[=WHEN] Use colors 'always', 'never', or only when output
123 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700151 # Also catch when type or level is passed through a variable
152 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700153 push (@types, $_);
154 }
155 @types = sort(uniq(@types));
156 print("#\tMessage type\n\n");
157 foreach my $type (@types) {
158 print(++$count . "\t" . $type . "\n");
159 }
160
161 exit($exitcode);
162}
163
Joe Perches000d1cc12011-07-25 17:13:25 -0700164my $conf = which_conf($configuration_file);
165if (-f $conf) {
166 my @conf_args;
167 open(my $conffile, '<', "$conf")
168 or warn "$P: Can't find a readable $configuration_file file $!\n";
169
170 while (<$conffile>) {
171 my $line = $_;
172
173 $line =~ s/\s*\n?$//g;
174 $line =~ s/^\s*//g;
175 $line =~ s/\s+/ /g;
176
177 next if ($line =~ m/^\s*#/);
178 next if ($line =~ m/^\s*$/);
179
180 my @words = split(" ", $line);
181 foreach my $word (@words) {
182 last if ($word =~ m/^#/);
183 push (@conf_args, $word);
184 }
185 }
186 close($conffile);
187 unshift(@ARGV, @conf_args) if @conf_args;
188}
189
John Brooks737c0762017-07-10 15:52:24 -0700190# Perl's Getopt::Long allows options to take optional arguments after a space.
191# Prevent --color by itself from consuming other arguments
192foreach (@ARGV) {
193 if ($_ eq "--color" || $_ eq "-color") {
194 $_ = "--color=$color";
195 }
196}
197
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700198GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200 'tree!' => \$tree,
201 'signoff!' => \$chk_signoff,
202 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800204 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700205 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700206 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700207 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'subjective!' => \$check,
209 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700210 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700211 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700213 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800214 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700215 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700216 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800217 'summary!' => \$summary,
218 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800219 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700220 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800221 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700222 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800223 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700224 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700225 'codespell!' => \$codespell,
226 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700227 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700228 'color=s' => \$color,
229 'no-color' => \$color, #keep old behaviors of -nocolor
230 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700231 'h|help' => \$help,
232 'version' => \$help
233) or help(1);
234
235help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236
Joe Perches3beb42e2016-05-20 17:04:14 -0700237list_types(0) if ($list_types);
238
Joe Perches9624b8d2014-01-23 15:54:44 -0800239$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700240$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800241
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700242my $exit = 0;
243
Joe Perches5b579802018-08-21 21:57:33 -0700244my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700245if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700246 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700247 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700248 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700249}
250
Allen Hubbe45107ff2016-08-02 14:04:47 -0700251#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700252if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254}
255
John Brooks737c0762017-07-10 15:52:24 -0700256if ($color =~ /^[01]$/) {
257 $color = !$color;
258} elsif ($color =~ /^always$/i) {
259 $color = 1;
260} elsif ($color =~ /^never$/i) {
261 $color = 0;
262} elsif ($color =~ /^auto$/i) {
263 $color = (-t STDOUT);
264} else {
265 die "Invalid color mode: $color\n";
266}
267
Joe Perches91bfe482013-09-11 14:23:59 -0700268sub hash_save_array_words {
269 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700270
Joe Perches91bfe482013-09-11 14:23:59 -0700271 my @array = split(/,/, join(',', @$arrayRef));
272 foreach my $word (@array) {
273 $word =~ s/\s*\n?$//g;
274 $word =~ s/^\s*//g;
275 $word =~ s/\s+/ /g;
276 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700277
Joe Perches91bfe482013-09-11 14:23:59 -0700278 next if ($word =~ m/^\s*#/);
279 next if ($word =~ m/^\s*$/);
280
281 $hashRef->{$word}++;
282 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700283}
284
Joe Perches91bfe482013-09-11 14:23:59 -0700285sub hash_show_words {
286 my ($hashRef, $prefix) = @_;
287
Joe Perches3c816e42015-06-25 15:03:29 -0700288 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700289 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700290 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700291 print " $word";
292 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700293 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700294 }
295}
296
297hash_save_array_words(\%ignore_type, \@ignore);
298hash_save_array_words(\%use_type, \@use);
299
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300my $dbg_values = 0;
301my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700302my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700303my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800305 ## no critic
306 eval "\${dbg_$key} = '$debug{$key}';";
307 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800308}
309
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700310my $rpt_cleaners = 0;
311
Andy Whitcroft8905a672007-11-28 16:21:06 -0800312if ($terse) {
313 $emacs = 1;
314 $quiet++;
315}
316
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700317if ($tree) {
318 if (defined $root) {
319 if (!top_of_kernel_tree($root)) {
320 die "$P: $root: --root does not point at a valid tree\n";
321 }
322 } else {
323 if (top_of_kernel_tree('.')) {
324 $root = '.';
325 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
326 top_of_kernel_tree($1)) {
327 $root = $1;
328 }
329 }
330
331 if (!defined $root) {
332 print "Must be run from the top-level dir. of a kernel tree\n";
333 exit(2);
334 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700335}
336
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337my $emitted_corrupt = 0;
338
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700339our $Ident = qr{
340 [A-Za-z_][A-Za-z\d_]*
341 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
342 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700343our $Storage = qr{extern|static|asmlinkage};
344our $Sparse = qr{
345 __user|
346 __kernel|
347 __force|
348 __iomem|
349 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800350 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700351 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700352 __refconst|
353 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800354 __rcu|
355 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700356 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800357our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700362
Wolfram Sang52131292010-03-05 13:43:51 -0800363# Notes to $Attribute:
364# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365our $Attribute = qr{
366 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700367 __percpu|
368 __nocast|
369 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200370 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700371 __packed__|
372 __packed2__|
373 __naked|
374 __maybe_unused|
375 __always_unused|
376 __noreturn|
377 __used|
378 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800379 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700380 __noclone|
381 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700382 __read_mostly|
383 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700384 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700385 ____cacheline_aligned|
386 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800387 ____cacheline_internodealigned_in_smp|
388 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700390our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700391our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
393our $Lval = qr{$Ident(?:$Member)*};
394
Joe Perches95e2c602013-07-03 15:05:20 -0700395our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
396our $Binary = qr{(?i)0b[01]+$Int_type?};
397our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
398our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700399our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800400our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800401our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
402our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
403our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800404our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700405our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800406our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700407our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700408our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700409our $Operators = qr{
410 <=|>=|==|!=|
411 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700412 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700413 }x;
414
Joe Perches91cb5192014-04-03 14:49:32 -0700415our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
416
Joe Perchesab7e23f2015-04-16 12:44:22 -0700417our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700419our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700420our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700422our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800423our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700424our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800425
Joe Perches15662b32011-10-31 17:13:12 -0700426our $NON_ASCII_UTF8 = qr{
427 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700428 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
429 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
430 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
431 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
432 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
433 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
434}x;
435
Joe Perches15662b32011-10-31 17:13:12 -0700436our $UTF8 = qr{
437 [\x09\x0A\x0D\x20-\x7E] # ASCII
438 | $NON_ASCII_UTF8
439}x;
440
Joe Perchese6176fa2015-06-25 15:02:49 -0700441our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800442our $typeOtherOSTypedefs = qr{(?x:
443 u_(?:char|short|int|long) | # bsd
444 u(?:nchar|short|int|long) # sysv
445)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700446our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700447 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700448 atomic_t
449)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700450our $typeTypedefs = qr{(?x:
451 $typeC99Typedefs\b|
452 $typeOtherOSTypedefs\b|
453 $typeKernelTypedefs\b
454)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700455
Joe Perches6d32f7a2015-11-06 16:31:37 -0800456our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
457
Joe Perches691e6692010-03-05 13:43:51 -0800458our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800459 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700460 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches87bd4992017-11-17 15:28:48 -0800461 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700462 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700463 panic|
Joe Perches06668722013-11-12 15:10:07 -0800464 MODULE_[A-Z_]+|
465 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800466)};
467
Joe Perches20112472011-07-25 17:13:23 -0700468our $signature_tags = qr{(?xi:
469 Signed-off-by:|
470 Acked-by:|
471 Tested-by:|
472 Reviewed-by:|
473 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700474 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700475 To:|
476 Cc:
477)};
478
Joe Perches18130872014-08-06 16:11:22 -0700479our @typeListMisordered = (
480 qr{char\s+(?:un)?signed},
481 qr{int\s+(?:(?:un)?signed\s+)?short\s},
482 qr{int\s+short(?:\s+(?:un)?signed)},
483 qr{short\s+int(?:\s+(?:un)?signed)},
484 qr{(?:un)?signed\s+int\s+short},
485 qr{short\s+(?:un)?signed},
486 qr{long\s+int\s+(?:un)?signed},
487 qr{int\s+long\s+(?:un)?signed},
488 qr{long\s+(?:un)?signed\s+int},
489 qr{int\s+(?:un)?signed\s+long},
490 qr{int\s+(?:un)?signed},
491 qr{int\s+long\s+long\s+(?:un)?signed},
492 qr{long\s+long\s+int\s+(?:un)?signed},
493 qr{long\s+long\s+(?:un)?signed\s+int},
494 qr{long\s+long\s+(?:un)?signed},
495 qr{long\s+(?:un)?signed},
496);
497
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498our @typeList = (
499 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700500 qr{(?:(?:un)?signed\s+)?char},
501 qr{(?:(?:un)?signed\s+)?short\s+int},
502 qr{(?:(?:un)?signed\s+)?short},
503 qr{(?:(?:un)?signed\s+)?int},
504 qr{(?:(?:un)?signed\s+)?long\s+int},
505 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
506 qr{(?:(?:un)?signed\s+)?long\s+long},
507 qr{(?:(?:un)?signed\s+)?long},
508 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509 qr{float},
510 qr{double},
511 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512 qr{struct\s+$Ident},
513 qr{union\s+$Ident},
514 qr{enum\s+$Ident},
515 qr{${Ident}_t},
516 qr{${Ident}_handler},
517 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700518 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800519);
Joe Perches938224b2016-01-20 14:59:15 -0800520
521our $C90_int_types = qr{(?x:
522 long\s+long\s+int\s+(?:un)?signed|
523 long\s+long\s+(?:un)?signed\s+int|
524 long\s+long\s+(?:un)?signed|
525 (?:(?:un)?signed\s+)?long\s+long\s+int|
526 (?:(?:un)?signed\s+)?long\s+long|
527 int\s+long\s+long\s+(?:un)?signed|
528 int\s+(?:(?:un)?signed\s+)?long\s+long|
529
530 long\s+int\s+(?:un)?signed|
531 long\s+(?:un)?signed\s+int|
532 long\s+(?:un)?signed|
533 (?:(?:un)?signed\s+)?long\s+int|
534 (?:(?:un)?signed\s+)?long|
535 int\s+long\s+(?:un)?signed|
536 int\s+(?:(?:un)?signed\s+)?long|
537
538 int\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?int
540)};
541
Alex Dowad485ff232015-06-25 15:02:52 -0700542our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700543our @typeListWithAttr = (
544 @typeList,
545 qr{struct\s+$InitAttribute\s+$Ident},
546 qr{union\s+$InitAttribute\s+$Ident},
547);
548
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700549our @modifierList = (
550 qr{fastcall},
551);
Alex Dowad485ff232015-06-25 15:02:52 -0700552our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800553
Joe Perches24358802014-04-03 14:49:13 -0700554our @mode_permission_funcs = (
555 ["module_param", 3],
556 ["module_param_(?:array|named|string)", 4],
557 ["module_param_array_named", 5],
558 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
559 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700560 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
561 ["IIO_DEV_ATTR_[A-Z_]+", 1],
562 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
563 ["SENSOR_TEMPLATE(?:_2|)", 3],
564 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700565);
566
Joe Perches515a2352014-04-03 14:49:24 -0700567#Create a search pattern for all these functions to speed up a loop below
568our $mode_perms_search = "";
569foreach my $entry (@mode_permission_funcs) {
570 $mode_perms_search .= '|' if ($mode_perms_search ne "");
571 $mode_perms_search .= $entry->[0];
572}
Joe Perches00180462018-02-06 15:38:55 -0800573$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700574
Joe Perchesb392c642015-04-16 12:44:16 -0700575our $mode_perms_world_writable = qr{
576 S_IWUGO |
577 S_IWOTH |
578 S_IRWXUGO |
579 S_IALLUGO |
580 0[0-7][0-7][2367]
581}x;
582
Joe Perchesf90774e2016-10-11 13:51:47 -0700583our %mode_permission_string_types = (
584 "S_IRWXU" => 0700,
585 "S_IRUSR" => 0400,
586 "S_IWUSR" => 0200,
587 "S_IXUSR" => 0100,
588 "S_IRWXG" => 0070,
589 "S_IRGRP" => 0040,
590 "S_IWGRP" => 0020,
591 "S_IXGRP" => 0010,
592 "S_IRWXO" => 0007,
593 "S_IROTH" => 0004,
594 "S_IWOTH" => 0002,
595 "S_IXOTH" => 0001,
596 "S_IRWXUGO" => 0777,
597 "S_IRUGO" => 0444,
598 "S_IWUGO" => 0222,
599 "S_IXUGO" => 0111,
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $mode_perms_string_search = "";
604foreach my $entry (keys %mode_permission_string_types) {
605 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
606 $mode_perms_string_search .= $entry;
607}
Joe Perches00180462018-02-06 15:38:55 -0800608our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
609our $multi_mode_perms_string_search = qr{
610 ${single_mode_perms_string_search}
611 (?:\s*\|\s*${single_mode_perms_string_search})*
612}x;
613
614sub perms_to_octal {
615 my ($string) = @_;
616
617 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
618
619 my $val = "";
620 my $oval = "";
621 my $to = 0;
622 my $curpos = 0;
623 my $lastpos = 0;
624 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
625 $curpos = pos($string);
626 my $match = $2;
627 my $omatch = $1;
628 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
629 $lastpos = $curpos;
630 $to |= $mode_permission_string_types{$match};
631 $val .= '\s*\|\s*' if ($val ne "");
632 $val .= $match;
633 $oval .= $omatch;
634 }
635 $oval =~ s/^\s*\|\s*//;
636 $oval =~ s/\s*\|\s*$//;
637 return sprintf("%04o", $to);
638}
Joe Perchesf90774e2016-10-11 13:51:47 -0700639
Wolfram Sang7840a942010-08-09 17:20:57 -0700640our $allowed_asm_includes = qr{(?x:
641 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700642 memory|
643 time|
644 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700645)};
646# memory.h: ARM has a custom one
647
Kees Cook66b47b42014-10-13 15:51:57 -0700648# Load common spelling mistakes and build regular expression list.
649my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700650my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700651
Joe Perches36061e32014-12-10 15:51:43 -0800652if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800653 while (<$spelling>) {
654 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700655
Joe Perches36061e32014-12-10 15:51:43 -0800656 $line =~ s/\s*\n?$//g;
657 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700658
Joe Perches36061e32014-12-10 15:51:43 -0800659 next if ($line =~ m/^\s*#/);
660 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700661
Joe Perches36061e32014-12-10 15:51:43 -0800662 my ($suspect, $fix) = split(/\|\|/, $line);
663
Joe Perches36061e32014-12-10 15:51:43 -0800664 $spelling_fix{$suspect} = $fix;
665 }
666 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800667} else {
668 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700669}
Kees Cook66b47b42014-10-13 15:51:57 -0700670
Joe Perchesebfd7d62015-04-16 12:44:14 -0700671if ($codespell) {
672 if (open(my $spelling, '<', $codespellfile)) {
673 while (<$spelling>) {
674 my $line = $_;
675
676 $line =~ s/\s*\n?$//g;
677 $line =~ s/^\s*//g;
678
679 next if ($line =~ m/^\s*#/);
680 next if ($line =~ m/^\s*$/);
681 next if ($line =~ m/, disabled/i);
682
683 $line =~ s/,.*$//;
684
685 my ($suspect, $fix) = split(/->/, $line);
686
687 $spelling_fix{$suspect} = $fix;
688 }
689 close($spelling);
690 } else {
691 warn "No codespell typos will be found - file '$codespellfile': $!\n";
692 }
693}
694
695$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
696
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700697sub read_words {
698 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700699
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700700 if (open(my $words, '<', $file)) {
701 while (<$words>) {
702 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700703
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700704 $line =~ s/\s*\n?$//g;
705 $line =~ s/^\s*//g;
706
707 next if ($line =~ m/^\s*#/);
708 next if ($line =~ m/^\s*$/);
709 if ($line =~ /\s/) {
710 print("$file: '$line' invalid - ignored\n");
711 next;
712 }
713
714 $$wordsRef .= '|' if ($$wordsRef ne "");
715 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700716 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700717 close($file);
718 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700719 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700720
721 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700722}
723
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700724my $const_structs = "";
725read_words(\$const_structs, $conststructsfile)
726 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
727
728my $typeOtherTypedefs = "";
729if (length($typedefsfile)) {
730 read_words(\$typeOtherTypedefs, $typedefsfile)
731 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
732}
733$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
734
Andy Whitcroft8905a672007-11-28 16:21:06 -0800735sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700736 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
737 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700738 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700739 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700740 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700741 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700742 (?:$typeTypedefs\b)|
743 (?:${all}\b)
744 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800745 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700746 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800747 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800748 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700749 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700750 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800751 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700752 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800753 }x;
Joe Perches18130872014-08-06 16:11:22 -0700754 $NonptrTypeMisordered = qr{
755 (?:$Modifier\s+|const\s+)*
756 (?:
757 (?:${Misordered}\b)
758 )
759 (?:\s+$Modifier|\s+const)*
760 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700761 $NonptrTypeWithAttr = qr{
762 (?:$Modifier\s+|const\s+)*
763 (?:
764 (?:typeof|__typeof__)\s*\([^\)]*\)|
765 (?:$typeTypedefs\b)|
766 (?:${allWithAttr}\b)
767 )
768 (?:\s+$Modifier|\s+const)*
769 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700771 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700772 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700773 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800774 }x;
Joe Perches18130872014-08-06 16:11:22 -0700775 $TypeMisordered = qr{
776 $NonptrTypeMisordered
777 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
778 (?:\s+$Inline|\s+$Modifier)*
779 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700780 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700781 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800782}
783build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700784
Joe Perches7d2367a2011-07-25 17:13:22 -0700785our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700786
787# Using $balanced_parens, $LvalOrFunc, or $FuncArg
788# requires at least perl version v5.10.0
789# Any use must be runtime checked with $^V
790
791our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700792our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800793our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700794
Joe Perchesf8422302014-08-06 16:11:31 -0700795our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700796 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700797 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700798 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
799 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700800)};
801
Joe Perches7d2367a2011-07-25 17:13:22 -0700802sub deparenthesize {
803 my ($string) = @_;
804 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700805
806 while ($string =~ /^\s*\(.*\)\s*$/) {
807 $string =~ s@^\s*\(\s*@@;
808 $string =~ s@\s*\)\s*$@@;
809 }
810
Joe Perches7d2367a2011-07-25 17:13:22 -0700811 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700812
Joe Perches7d2367a2011-07-25 17:13:22 -0700813 return $string;
814}
815
Joe Perches34456862013-07-03 15:05:34 -0700816sub seed_camelcase_file {
817 my ($file) = @_;
818
819 return if (!(-f $file));
820
821 local $/;
822
823 open(my $include_file, '<', "$file")
824 or warn "$P: Can't read '$file' $!\n";
825 my $text = <$include_file>;
826 close($include_file);
827
828 my @lines = split('\n', $text);
829
830 foreach my $line (@lines) {
831 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
832 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
833 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800834 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
835 $camelcase{$1} = 1;
836 } 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 -0700837 $camelcase{$1} = 1;
838 }
839 }
840}
841
Joe Perches85b0ee12016-10-11 13:51:44 -0700842sub is_maintained_obsolete {
843 my ($filename) = @_;
844
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800845 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700846
Joe Perches0616efa2016-10-11 13:52:02 -0700847 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 -0700848
849 return $status =~ /obsolete/i;
850}
851
Joe Perches34456862013-07-03 15:05:34 -0700852my $camelcase_seeded = 0;
853sub seed_camelcase_includes {
854 return if ($camelcase_seeded);
855
856 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700857 my $camelcase_cache = "";
858 my @include_files = ();
859
860 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700861
Richard Genoud3645e322014-02-10 14:25:32 -0800862 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700863 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
864 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700865 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700866 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700867 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700868 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700869 @include_files = split('\n', $files);
870 foreach my $file (@include_files) {
871 my $date = POSIX::strftime("%Y%m%d%H%M",
872 localtime((stat $file)[9]));
873 $last_mod_date = $date if ($last_mod_date < $date);
874 }
875 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700876 }
Joe Perchesc707a812013-07-08 16:00:43 -0700877
878 if ($camelcase_cache ne "" && -f $camelcase_cache) {
879 open(my $camelcase_file, '<', "$camelcase_cache")
880 or warn "$P: Can't read '$camelcase_cache' $!\n";
881 while (<$camelcase_file>) {
882 chomp;
883 $camelcase{$_} = 1;
884 }
885 close($camelcase_file);
886
887 return;
888 }
889
Richard Genoud3645e322014-02-10 14:25:32 -0800890 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700891 $files = `git ls-files "include/*.h"`;
892 @include_files = split('\n', $files);
893 }
894
Joe Perches34456862013-07-03 15:05:34 -0700895 foreach my $file (@include_files) {
896 seed_camelcase_file($file);
897 }
Joe Perches351b2a12013-07-03 15:05:36 -0700898
Joe Perchesc707a812013-07-08 16:00:43 -0700899 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700900 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700901 open(my $camelcase_file, '>', "$camelcase_cache")
902 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700903 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
904 print $camelcase_file ("$_\n");
905 }
906 close($camelcase_file);
907 }
Joe Perches34456862013-07-03 15:05:34 -0700908}
909
Joe Perchesd311cd42014-08-06 16:10:57 -0700910sub git_commit_info {
911 my ($commit, $id, $desc) = @_;
912
913 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
914
915 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
916 $output =~ s/^\s*//gm;
917 my @lines = split("\n", $output);
918
Joe Perches0d7835f2015-02-13 14:38:35 -0800919 return ($id, $desc) if ($#lines < 0);
920
Joe Perchesd311cd42014-08-06 16:10:57 -0700921 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
922# Maybe one day convert this block of bash into something that returns
923# all matching commit ids, but it's very slow...
924#
925# echo "checking commits $1..."
926# git rev-list --remotes | grep -i "^$1" |
927# while read line ; do
928# git log --format='%H %s' -1 $line |
929# echo "commit $(cut -c 1-12,41-)"
930# done
931 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700932 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700933 } else {
934 $id = substr($lines[0], 0, 12);
935 $desc = substr($lines[0], 41);
936 }
937
938 return ($id, $desc);
939}
940
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700941$chk_signoff = 0 if ($file);
942
Andy Whitcroft00df3442007-06-08 13:47:06 -0700943my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800944my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700945my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700946my @fixed_inserted = ();
947my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700948my $fixlinenr = -1;
949
Du, Changbin4a593c32016-05-20 17:04:16 -0700950# If input is git commits, extract all commits from the commit expressions.
951# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
952die "$P: No git repository found\n" if ($git && !-e ".git");
953
954if ($git) {
955 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700956 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700957 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700958 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
959 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700960 } elsif ($commit_expr =~ m/\.\./) {
961 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700962 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700963 $git_range = "-1 $commit_expr";
964 }
965 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
966 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700967 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
968 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700969 my $sha1 = $1;
970 my $subject = $2;
971 unshift(@commits, $sha1);
972 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700973 }
974 }
975 die "$P: no git commits after extraction!\n" if (@commits == 0);
976 @ARGV = @commits;
977}
978
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800979my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700980for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800981 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700982 if ($git) {
983 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
984 die "$P: $filename: git format-patch failed - $!\n";
985 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800986 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700987 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800988 } elsif ($filename eq '-') {
989 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700990 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800991 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700992 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700993 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 if ($filename eq '-') {
995 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700996 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700997 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800998 } else {
999 $vname = $filename;
1000 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001001 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001002 chomp;
1003 push(@rawlines, $_);
1004 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001005 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001006
1007 if ($#ARGV > 0 && $quiet == 0) {
1008 print '-' x length($vname) . "\n";
1009 print "$vname\n";
1010 print '-' x length($vname) . "\n";
1011 }
1012
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001014 $exit = 1;
1015 }
1016 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001017 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001018 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001019 @fixed_inserted = ();
1020 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001021 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001022 @modifierListFile = ();
1023 @typeListFile = ();
1024 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001025}
1026
Joe Perchesd8469f12015-06-25 15:03:00 -07001027if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001028 hash_show_words(\%use_type, "Used");
1029 hash_show_words(\%ignore_type, "Ignored");
1030
Joe Perches5b579802018-08-21 21:57:33 -07001031 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001032 print << "EOM"
1033
1034NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001035 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001036EOM
1037 }
1038 if ($exit) {
1039 print << "EOM"
1040
1041NOTE: If any of the errors are false positives, please report
1042 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1043EOM
1044 }
1045}
1046
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001047exit($exit);
1048
1049sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001050 my ($root) = @_;
1051
1052 my @tree_check = (
1053 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1054 "README", "Documentation", "arch", "include", "drivers",
1055 "fs", "init", "ipc", "kernel", "lib", "scripts",
1056 );
1057
1058 foreach my $check (@tree_check) {
1059 if (! -e $root . '/' . $check) {
1060 return 0;
1061 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001062 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001063 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001064}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001065
Joe Perches20112472011-07-25 17:13:23 -07001066sub parse_email {
1067 my ($formatted_email) = @_;
1068
1069 my $name = "";
1070 my $address = "";
1071 my $comment = "";
1072
1073 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1074 $name = $1;
1075 $address = $2;
1076 $comment = $3 if defined $3;
1077 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1078 $address = $1;
1079 $comment = $2 if defined $2;
1080 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1081 $address = $1;
1082 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001083 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001084 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001085 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001086 $name =~ s/^\"|\"$//g;
1087 # If there's a name left after stripping spaces and
1088 # leading quotes, and the address doesn't have both
1089 # leading and trailing angle brackets, the address
1090 # is invalid. ie:
1091 # "joe smith joe@smith.com" bad
1092 # "joe smith <joe@smith.com" bad
1093 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1094 $name = "";
1095 $address = "";
1096 $comment = "";
1097 }
1098 }
1099
Joe Perches3705ce52013-07-03 15:05:31 -07001100 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001101 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001102 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001103 $address =~ s/^\<|\>$//g;
1104
1105 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1106 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1107 $name = "\"$name\"";
1108 }
1109
1110 return ($name, $address, $comment);
1111}
1112
1113sub format_email {
1114 my ($name, $address) = @_;
1115
1116 my $formatted_email;
1117
Joe Perches3705ce52013-07-03 15:05:31 -07001118 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001119 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001120 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001121
1122 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1123 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1124 $name = "\"$name\"";
1125 }
1126
1127 if ("$name" eq "") {
1128 $formatted_email = "$address";
1129 } else {
1130 $formatted_email = "$name <$address>";
1131 }
1132
1133 return $formatted_email;
1134}
1135
Joe Perchesd311cd42014-08-06 16:10:57 -07001136sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001137 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001138
Joe Perchesbd474ca2014-08-06 16:11:10 -07001139 foreach my $path (split(/:/, $ENV{PATH})) {
1140 if (-e "$path/$bin") {
1141 return "$path/$bin";
1142 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001143 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001144
Joe Perchesbd474ca2014-08-06 16:11:10 -07001145 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001146}
1147
Joe Perches000d1cc12011-07-25 17:13:25 -07001148sub which_conf {
1149 my ($conf) = @_;
1150
1151 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1152 if (-e "$path/$conf") {
1153 return "$path/$conf";
1154 }
1155 }
1156
1157 return "";
1158}
1159
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001160sub expand_tabs {
1161 my ($str) = @_;
1162
1163 my $res = '';
1164 my $n = 0;
1165 for my $c (split(//, $str)) {
1166 if ($c eq "\t") {
1167 $res .= ' ';
1168 $n++;
1169 for (; ($n % 8) != 0; $n++) {
1170 $res .= ' ';
1171 }
1172 next;
1173 }
1174 $res .= $c;
1175 $n++;
1176 }
1177
1178 return $res;
1179}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001180sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001181 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001182 return $res;
1183}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001184
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001185sub line_stats {
1186 my ($line) = @_;
1187
1188 # Drop the diff line leader and expand tabs
1189 $line =~ s/^.//;
1190 $line = expand_tabs($line);
1191
1192 # Pick the indent from the front of the line.
1193 my ($white) = ($line =~ /^(\s*)/);
1194
1195 return (length($line), length($white));
1196}
1197
Andy Whitcroft773647a2008-03-28 14:15:58 -07001198my $sanitise_quote = '';
1199
1200sub sanitise_line_reset {
1201 my ($in_comment) = @_;
1202
1203 if ($in_comment) {
1204 $sanitise_quote = '*/';
1205 } else {
1206 $sanitise_quote = '';
1207 }
1208}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001209sub sanitise_line {
1210 my ($line) = @_;
1211
1212 my $res = '';
1213 my $l = '';
1214
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001216 my $off = 0;
1217 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001218
Andy Whitcroft773647a2008-03-28 14:15:58 -07001219 # Always copy over the diff marker.
1220 $res = substr($line, 0, 1);
1221
1222 for ($off = 1; $off < length($line); $off++) {
1223 $c = substr($line, $off, 1);
1224
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001225 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001226 # and end, all to $;.
1227 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1228 $sanitise_quote = '*/';
1229
1230 substr($res, $off, 2, "$;$;");
1231 $off++;
1232 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001233 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001234 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001235 $sanitise_quote = '';
1236 substr($res, $off, 2, "$;$;");
1237 $off++;
1238 next;
1239 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001240 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1241 $sanitise_quote = '//';
1242
1243 substr($res, $off, 2, $sanitise_quote);
1244 $off++;
1245 next;
1246 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001247
1248 # A \ in a string means ignore the next character.
1249 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1250 $c eq "\\") {
1251 substr($res, $off, 2, 'XX');
1252 $off++;
1253 next;
1254 }
1255 # Regular quotes.
1256 if ($c eq "'" || $c eq '"') {
1257 if ($sanitise_quote eq '') {
1258 $sanitise_quote = $c;
1259
1260 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001261 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001262 } elsif ($sanitise_quote eq $c) {
1263 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001264 }
1265 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001266
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001267 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001268 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1269 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001270 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1271 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001272 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1273 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001274 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001275 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001276 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 }
1278
Daniel Walker113f04a2009-09-21 17:04:35 -07001279 if ($sanitise_quote eq '//') {
1280 $sanitise_quote = '';
1281 }
1282
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001284 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001285 my $clean = 'X' x length($1);
1286 $res =~ s@\<.*\>@<$clean>@;
1287
1288 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001289 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001290 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001291 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001292 }
1293
Joe Perchesdadf6802016-08-02 14:04:33 -07001294 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1295 my $match = $1;
1296 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1297 }
1298
Andy Whitcroft00df3442007-06-08 13:47:06 -07001299 return $res;
1300}
1301
Joe Perchesa6962d72013-04-29 16:18:13 -07001302sub get_quoted_string {
1303 my ($line, $rawline) = @_;
1304
Joe Perches478b1792018-04-10 16:33:34 -07001305 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001306 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001307 return substr($rawline, $-[0], $+[0] - $-[0]);
1308}
1309
Andy Whitcroft8905a672007-11-28 16:21:06 -08001310sub ctx_statement_block {
1311 my ($linenr, $remain, $off) = @_;
1312 my $line = $linenr - 1;
1313 my $blk = '';
1314 my $soff = $off;
1315 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001316 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001317
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001318 my $loff = 0;
1319
Andy Whitcroft8905a672007-11-28 16:21:06 -08001320 my $type = '';
1321 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001322 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001323 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001324 my $c;
1325 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001326
1327 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001328 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001329 @stack = (['', 0]) if ($#stack == -1);
1330
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001332 # If we are about to drop off the end, pull in more
1333 # context.
1334 if ($off >= $len) {
1335 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001336 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001337 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001338 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001339 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001340 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001341 $len = length($blk);
1342 $line++;
1343 last;
1344 }
1345 # Bail if there is no further context.
1346 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001347 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001348 last;
1349 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001350 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1351 $level++;
1352 $type = '#';
1353 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001354 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001355 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001356 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001357 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001358
Andy Whitcroft773647a2008-03-28 14:15:58 -07001359 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001360
1361 # Handle nested #if/#else.
1362 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1363 push(@stack, [ $type, $level ]);
1364 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1365 ($type, $level) = @{$stack[$#stack - 1]};
1366 } elsif ($remainder =~ /^#\s*endif\b/) {
1367 ($type, $level) = @{pop(@stack)};
1368 }
1369
Andy Whitcroft8905a672007-11-28 16:21:06 -08001370 # Statement ends at the ';' or a close '}' at the
1371 # outermost level.
1372 if ($level == 0 && $c eq ';') {
1373 last;
1374 }
1375
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001376 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001377 if ($level == 0 && $coff_set == 0 &&
1378 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1379 $remainder =~ /^(else)(?:\s|{)/ &&
1380 $remainder !~ /^else\s+if\b/) {
1381 $coff = $off + length($1) - 1;
1382 $coff_set = 1;
1383 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1384 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001385 }
1386
Andy Whitcroft8905a672007-11-28 16:21:06 -08001387 if (($type eq '' || $type eq '(') && $c eq '(') {
1388 $level++;
1389 $type = '(';
1390 }
1391 if ($type eq '(' && $c eq ')') {
1392 $level--;
1393 $type = ($level != 0)? '(' : '';
1394
1395 if ($level == 0 && $coff < $soff) {
1396 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001397 $coff_set = 1;
1398 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001399 }
1400 }
1401 if (($type eq '' || $type eq '{') && $c eq '{') {
1402 $level++;
1403 $type = '{';
1404 }
1405 if ($type eq '{' && $c eq '}') {
1406 $level--;
1407 $type = ($level != 0)? '{' : '';
1408
1409 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001410 if (substr($blk, $off + 1, 1) eq ';') {
1411 $off++;
1412 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001413 last;
1414 }
1415 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001416 # Preprocessor commands end at the newline unless escaped.
1417 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1418 $level--;
1419 $type = '';
1420 $off++;
1421 last;
1422 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001423 $off++;
1424 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001425 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001426 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001427 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001428 $line++;
1429 $remain--;
1430 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001431
1432 my $statement = substr($blk, $soff, $off - $soff + 1);
1433 my $condition = substr($blk, $soff, $coff - $soff + 1);
1434
1435 #warn "STATEMENT<$statement>\n";
1436 #warn "CONDITION<$condition>\n";
1437
Andy Whitcroft773647a2008-03-28 14:15:58 -07001438 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001439
1440 return ($statement, $condition,
1441 $line, $remain + 1, $off - $loff + 1, $level);
1442}
1443
Andy Whitcroftcf655042008-03-04 14:28:20 -08001444sub statement_lines {
1445 my ($stmt) = @_;
1446
1447 # Strip the diff line prefixes and rip blank lines at start and end.
1448 $stmt =~ s/(^|\n)./$1/g;
1449 $stmt =~ s/^\s*//;
1450 $stmt =~ s/\s*$//;
1451
1452 my @stmt_lines = ($stmt =~ /\n/g);
1453
1454 return $#stmt_lines + 2;
1455}
1456
1457sub statement_rawlines {
1458 my ($stmt) = @_;
1459
1460 my @stmt_lines = ($stmt =~ /\n/g);
1461
1462 return $#stmt_lines + 2;
1463}
1464
1465sub statement_block_size {
1466 my ($stmt) = @_;
1467
1468 $stmt =~ s/(^|\n)./$1/g;
1469 $stmt =~ s/^\s*{//;
1470 $stmt =~ s/}\s*$//;
1471 $stmt =~ s/^\s*//;
1472 $stmt =~ s/\s*$//;
1473
1474 my @stmt_lines = ($stmt =~ /\n/g);
1475 my @stmt_statements = ($stmt =~ /;/g);
1476
1477 my $stmt_lines = $#stmt_lines + 2;
1478 my $stmt_statements = $#stmt_statements + 1;
1479
1480 if ($stmt_lines > $stmt_statements) {
1481 return $stmt_lines;
1482 } else {
1483 return $stmt_statements;
1484 }
1485}
1486
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001487sub ctx_statement_full {
1488 my ($linenr, $remain, $off) = @_;
1489 my ($statement, $condition, $level);
1490
1491 my (@chunks);
1492
Andy Whitcroftcf655042008-03-04 14:28:20 -08001493 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001494 ($statement, $condition, $linenr, $remain, $off, $level) =
1495 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001496 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001497 push(@chunks, [ $condition, $statement ]);
1498 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1499 return ($level, $linenr, @chunks);
1500 }
1501
1502 # Pull in the following conditional/block pairs and see if they
1503 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001504 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001505 ($statement, $condition, $linenr, $remain, $off, $level) =
1506 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001507 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001508 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001509 #print "C: push\n";
1510 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001511 }
1512
1513 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001514}
1515
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001516sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001517 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001518 my $line;
1519 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001520 my $blk = '';
1521 my @o;
1522 my @c;
1523 my @res = ();
1524
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001525 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001526 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001527 for ($line = $start; $remain > 0; $line++) {
1528 next if ($rawlines[$line] =~ /^-/);
1529 $remain--;
1530
1531 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001532
1533 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001534 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001535 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001536 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001537 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001538 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001539 $level = pop(@stack);
1540 }
1541
Andy Whitcroft01464f32010-10-26 14:23:19 -07001542 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001543 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1544 if ($off > 0) {
1545 $off--;
1546 next;
1547 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001548
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001549 if ($c eq $close && $level > 0) {
1550 $level--;
1551 last if ($level == 0);
1552 } elsif ($c eq $open) {
1553 $level++;
1554 }
1555 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001556
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001557 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001558 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001559 }
1560
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001561 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001562 }
1563
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001564 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001565}
1566sub ctx_block_outer {
1567 my ($linenr, $remain) = @_;
1568
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001569 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1570 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001571}
1572sub ctx_block {
1573 my ($linenr, $remain) = @_;
1574
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001575 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1576 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001577}
1578sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001579 my ($linenr, $remain, $off) = @_;
1580
1581 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1582 return @r;
1583}
1584sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001585 my ($linenr, $remain) = @_;
1586
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001587 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001588}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001589sub ctx_statement_level {
1590 my ($linenr, $remain, $off) = @_;
1591
1592 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1593}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001594
1595sub ctx_locate_comment {
1596 my ($first_line, $end_line) = @_;
1597
1598 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001599 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001600 return $current_comment if (defined $current_comment);
1601
1602 # Look through the context and try and figure out if there is a
1603 # comment.
1604 my $in_comment = 0;
1605 $current_comment = '';
1606 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001607 my $line = $rawlines[$linenr - 1];
1608 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001609 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1610 $in_comment = 1;
1611 }
1612 if ($line =~ m@/\*@) {
1613 $in_comment = 1;
1614 }
1615 if (!$in_comment && $current_comment ne '') {
1616 $current_comment = '';
1617 }
1618 $current_comment .= $line . "\n" if ($in_comment);
1619 if ($line =~ m@\*/@) {
1620 $in_comment = 0;
1621 }
1622 }
1623
1624 chomp($current_comment);
1625 return($current_comment);
1626}
1627sub ctx_has_comment {
1628 my ($first_line, $end_line) = @_;
1629 my $cmt = ctx_locate_comment($first_line, $end_line);
1630
Andy Whitcroft00df3442007-06-08 13:47:06 -07001631 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001632 ##print "CMMT: $cmt\n";
1633
1634 return ($cmt ne '');
1635}
1636
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001637sub raw_line {
1638 my ($linenr, $cnt) = @_;
1639
1640 my $offset = $linenr - 1;
1641 $cnt++;
1642
1643 my $line;
1644 while ($cnt) {
1645 $line = $rawlines[$offset++];
1646 next if (defined($line) && $line =~ /^-/);
1647 $cnt--;
1648 }
1649
1650 return $line;
1651}
1652
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001653sub get_stat_real {
1654 my ($linenr, $lc) = @_;
1655
1656 my $stat_real = raw_line($linenr, 0);
1657 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1658 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1659 }
1660
1661 return $stat_real;
1662}
1663
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001664sub get_stat_here {
1665 my ($linenr, $cnt, $here) = @_;
1666
1667 my $herectx = $here . "\n";
1668 for (my $n = 0; $n < $cnt; $n++) {
1669 $herectx .= raw_line($linenr, $n) . "\n";
1670 }
1671
1672 return $herectx;
1673}
1674
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001675sub cat_vet {
1676 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001677 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001678
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001679 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001680 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1681 $res .= $1;
1682 if ($2 ne '') {
1683 $coded = sprintf("^%c", unpack('C', $2) + 64);
1684 $res .= $coded;
1685 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001686 }
1687 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001688
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001689 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001690}
1691
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001693my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001695my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001696
1697sub annotate_reset {
1698 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001699 $av_pending = '_';
1700 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001701 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001702}
1703
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001704sub annotate_values {
1705 my ($stream, $type) = @_;
1706
1707 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001708 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001709 my $cur = $stream;
1710
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001711 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001713 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001714 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001715 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001716 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001717 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001718 print "WS($1)\n" if ($dbg_values > 1);
1719 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001720 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001721 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001722 }
1723
Florian Micklerc023e4732011-01-12 16:59:58 -08001724 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001725 print "CAST($1)\n" if ($dbg_values > 1);
1726 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001727 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001728
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001729 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001730 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001731 $type = 'T';
1732
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001733 } elsif ($cur =~ /^($Modifier)\s*/) {
1734 print "MODIFIER($1)\n" if ($dbg_values > 1);
1735 $type = 'T';
1736
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001737 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001738 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001739 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001740 push(@av_paren_type, $type);
1741 if ($2 ne '') {
1742 $av_pending = 'N';
1743 }
1744 $type = 'E';
1745
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001746 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001747 print "UNDEF($1)\n" if ($dbg_values > 1);
1748 $av_preprocessor = 1;
1749 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001751 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001754
1755 push(@av_paren_type, $type);
1756 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001757 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001758
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001759 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001760 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1761 $av_preprocessor = 1;
1762
1763 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1764
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001765 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001766
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001767 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001768 print "PRE_END($1)\n" if ($dbg_values > 1);
1769
1770 $av_preprocessor = 1;
1771
1772 # Assume all arms of the conditional end as this
1773 # one does, and continue as if the #endif was not here.
1774 pop(@av_paren_type);
1775 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001776 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001777
1778 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001779 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001780
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001781 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1782 print "ATTR($1)\n" if ($dbg_values > 1);
1783 $av_pending = $type;
1784 $type = 'N';
1785
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001787 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001788 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001789 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790 }
1791 $type = 'N';
1792
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001793 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001795 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 $type = 'N';
1797
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001798 } elsif ($cur =~/^(case)/o) {
1799 print "CASE($1)\n" if ($dbg_values > 1);
1800 $av_pend_colon = 'C';
1801 $type = 'N';
1802
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001803 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001804 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001805 $type = 'N';
1806
1807 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001808 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001809 push(@av_paren_type, $av_pending);
1810 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001811 $type = 'N';
1812
1813 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001814 my $new_type = pop(@av_paren_type);
1815 if ($new_type ne '_') {
1816 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001817 print "PAREN('$1') -> $type\n"
1818 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001819 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001820 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001821 }
1822
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001823 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001824 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001825 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001826 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001827
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001828 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1829 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001830 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001831 } elsif ($type eq 'E') {
1832 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001833 }
1834 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1835 $type = 'V';
1836
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001837 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001838 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001839 $type = 'V';
1840
1841 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001843 $type = 'N';
1844
Andy Whitcroftcf655042008-03-04 14:28:20 -08001845 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001846 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001847 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001848 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001849
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001850 } elsif ($cur =~/^(,)/) {
1851 print "COMMA($1)\n" if ($dbg_values > 1);
1852 $type = 'C';
1853
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001854 } elsif ($cur =~ /^(\?)/o) {
1855 print "QUESTION($1)\n" if ($dbg_values > 1);
1856 $type = 'N';
1857
1858 } elsif ($cur =~ /^(:)/o) {
1859 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1860
1861 substr($var, length($res), 1, $av_pend_colon);
1862 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1863 $type = 'E';
1864 } else {
1865 $type = 'N';
1866 }
1867 $av_pend_colon = 'O';
1868
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001869 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001870 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001871 $type = 'N';
1872
Andy Whitcroft0d413862008-10-15 22:02:16 -07001873 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001874 my $variant;
1875
1876 print "OPV($1)\n" if ($dbg_values > 1);
1877 if ($type eq 'V') {
1878 $variant = 'B';
1879 } else {
1880 $variant = 'U';
1881 }
1882
1883 substr($var, length($res), 1, $variant);
1884 $type = 'N';
1885
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001886 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001887 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001888 if ($1 ne '++' && $1 ne '--') {
1889 $type = 'N';
1890 }
1891
1892 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 }
1895 if (defined $1) {
1896 $cur = substr($cur, length($1));
1897 $res .= $type x length($1);
1898 }
1899 }
1900
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001901 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001902}
1903
Andy Whitcroft8905a672007-11-28 16:21:06 -08001904sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001905 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001906 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001907 ^(?:
1908 $Modifier|
1909 $Storage|
1910 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001911 DEFINE_\S+
1912 )$|
1913 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001914 goto|
1915 return|
1916 case|
1917 else|
1918 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001919 do|
1920 \#|
1921 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001922 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001923 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001924 )}x;
1925 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1926 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001927 # Check for modifiers.
1928 $possible =~ s/\s*$Storage\s*//g;
1929 $possible =~ s/\s*$Sparse\s*//g;
1930 if ($possible =~ /^\s*$/) {
1931
1932 } elsif ($possible =~ /\s/) {
1933 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001934 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001935 if ($modifier !~ $notPermitted) {
1936 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001937 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001938 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001939 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001940
1941 } else {
1942 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001943 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001944 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001945 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001946 } else {
1947 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001948 }
1949}
1950
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001951my $prefix = '';
1952
Joe Perches000d1cc12011-07-25 17:13:25 -07001953sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001954 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001955
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001956 $type =~ tr/[a-z]/[A-Z]/;
1957
Joe Perchescbec18a2014-04-03 14:49:19 -07001958 return defined $use_type{$type} if (scalar keys %use_type > 0);
1959
1960 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001961}
1962
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001963sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001964 my ($level, $type, $msg) = @_;
1965
1966 if (!show_type($type) ||
1967 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001968 return 0;
1969 }
Joe Perches57230292015-06-25 15:03:03 -07001970 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001971 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001972 if ($level eq 'ERROR') {
1973 $output .= RED;
1974 } elsif ($level eq 'WARNING') {
1975 $output .= YELLOW;
1976 } else {
1977 $output .= GREEN;
1978 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001979 }
Joe Perches57230292015-06-25 15:03:03 -07001980 $output .= $prefix . $level . ':';
1981 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001982 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001983 $output .= "$type:";
1984 }
John Brooks737c0762017-07-10 15:52:24 -07001985 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001986 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001987
1988 if ($showfile) {
1989 my @lines = split("\n", $output, -1);
1990 splice(@lines, 1, 1);
1991 $output = join("\n", @lines);
1992 }
Joe Perches57230292015-06-25 15:03:03 -07001993 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001994
Joe Perches57230292015-06-25 15:03:03 -07001995 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001996
1997 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001998}
Joe Perchescbec18a2014-04-03 14:49:19 -07001999
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002000sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002001 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002002}
Joe Perches000d1cc12011-07-25 17:13:25 -07002003
Joe Perchesd752fcc2014-08-06 16:11:05 -07002004sub fixup_current_range {
2005 my ($lineRef, $offset, $length) = @_;
2006
2007 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2008 my $o = $1;
2009 my $l = $2;
2010 my $no = $o + $offset;
2011 my $nl = $l + $length;
2012 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2013 }
2014}
2015
2016sub fix_inserted_deleted_lines {
2017 my ($linesRef, $insertedRef, $deletedRef) = @_;
2018
2019 my $range_last_linenr = 0;
2020 my $delta_offset = 0;
2021
2022 my $old_linenr = 0;
2023 my $new_linenr = 0;
2024
2025 my $next_insert = 0;
2026 my $next_delete = 0;
2027
2028 my @lines = ();
2029
2030 my $inserted = @{$insertedRef}[$next_insert++];
2031 my $deleted = @{$deletedRef}[$next_delete++];
2032
2033 foreach my $old_line (@{$linesRef}) {
2034 my $save_line = 1;
2035 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002036 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002037 $delta_offset = 0;
2038 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2039 $range_last_linenr = $new_linenr;
2040 fixup_current_range(\$line, $delta_offset, 0);
2041 }
2042
2043 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2044 $deleted = @{$deletedRef}[$next_delete++];
2045 $save_line = 0;
2046 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2047 }
2048
2049 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2050 push(@lines, ${$inserted}{'LINE'});
2051 $inserted = @{$insertedRef}[$next_insert++];
2052 $new_linenr++;
2053 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2054 }
2055
2056 if ($save_line) {
2057 push(@lines, $line);
2058 $new_linenr++;
2059 }
2060
2061 $old_linenr++;
2062 }
2063
2064 return @lines;
2065}
2066
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002067sub fix_insert_line {
2068 my ($linenr, $line) = @_;
2069
2070 my $inserted = {
2071 LINENR => $linenr,
2072 LINE => $line,
2073 };
2074 push(@fixed_inserted, $inserted);
2075}
2076
2077sub fix_delete_line {
2078 my ($linenr, $line) = @_;
2079
2080 my $deleted = {
2081 LINENR => $linenr,
2082 LINE => $line,
2083 };
2084
2085 push(@fixed_deleted, $deleted);
2086}
2087
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002088sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002089 my ($type, $msg) = @_;
2090
2091 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002092 our $clean = 0;
2093 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002094 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002095 }
Joe Perches3705ce52013-07-03 15:05:31 -07002096 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002097}
2098sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002099 my ($type, $msg) = @_;
2100
2101 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002102 our $clean = 0;
2103 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002104 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002105 }
Joe Perches3705ce52013-07-03 15:05:31 -07002106 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002107}
2108sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002109 my ($type, $msg) = @_;
2110
2111 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002112 our $clean = 0;
2113 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002114 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002115 }
Joe Perches3705ce52013-07-03 15:05:31 -07002116 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002117}
2118
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002119sub check_absolute_file {
2120 my ($absolute, $herecurr) = @_;
2121 my $file = $absolute;
2122
2123 ##print "absolute<$absolute>\n";
2124
2125 # See if any suffix of this path is a path within the tree.
2126 while ($file =~ s@^[^/]*/@@) {
2127 if (-f "$root/$file") {
2128 ##print "file<$file>\n";
2129 last;
2130 }
2131 }
2132 if (! -f _) {
2133 return 0;
2134 }
2135
2136 # It is, so see if the prefix is acceptable.
2137 my $prefix = $absolute;
2138 substr($prefix, -length($file)) = '';
2139
2140 ##print "prefix<$prefix>\n";
2141 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002142 WARN("USE_RELATIVE_PATH",
2143 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002144 }
2145}
2146
Joe Perches3705ce52013-07-03 15:05:31 -07002147sub trim {
2148 my ($string) = @_;
2149
Joe Perchesb34c6482013-09-11 14:24:01 -07002150 $string =~ s/^\s+|\s+$//g;
2151
2152 return $string;
2153}
2154
2155sub ltrim {
2156 my ($string) = @_;
2157
2158 $string =~ s/^\s+//;
2159
2160 return $string;
2161}
2162
2163sub rtrim {
2164 my ($string) = @_;
2165
2166 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002167
2168 return $string;
2169}
2170
Joe Perches52ea8502013-11-12 15:10:09 -08002171sub string_find_replace {
2172 my ($string, $find, $replace) = @_;
2173
2174 $string =~ s/$find/$replace/g;
2175
2176 return $string;
2177}
2178
Joe Perches3705ce52013-07-03 15:05:31 -07002179sub tabify {
2180 my ($leading) = @_;
2181
2182 my $source_indent = 8;
2183 my $max_spaces_before_tab = $source_indent - 1;
2184 my $spaces_to_tab = " " x $source_indent;
2185
2186 #convert leading spaces to tabs
2187 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2188 #Remove spaces before a tab
2189 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2190
2191 return "$leading";
2192}
2193
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002194sub pos_last_openparen {
2195 my ($line) = @_;
2196
2197 my $pos = 0;
2198
2199 my $opens = $line =~ tr/\(/\(/;
2200 my $closes = $line =~ tr/\)/\)/;
2201
2202 my $last_openparen = 0;
2203
2204 if (($opens == 0) || ($closes >= $opens)) {
2205 return -1;
2206 }
2207
2208 my $len = length($line);
2209
2210 for ($pos = 0; $pos < $len; $pos++) {
2211 my $string = substr($line, $pos);
2212 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2213 $pos += length($1) - 1;
2214 } elsif (substr($line, $pos, 1) eq '(') {
2215 $last_openparen = $pos;
2216 } elsif (index($string, '(') == -1) {
2217 last;
2218 }
2219 }
2220
Joe Perches91cb5192014-04-03 14:49:32 -07002221 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002222}
2223
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002224sub process {
2225 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002226
2227 my $linenr=0;
2228 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002229 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002230 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002231 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002232
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002233 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002234 my $indent;
2235 my $previndent=0;
2236 my $stashindent=0;
2237
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002238 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002239 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002240 my $author = '';
2241 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002242 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002243 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002244 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002245 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002246 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002247 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002248 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002249 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002250 my $non_utf8_charset = 0;
2251
Joe Perches365dd4e2014-08-06 16:10:42 -07002252 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002253 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002254
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002255 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002256 our $cnt_lines = 0;
2257 our $cnt_error = 0;
2258 our $cnt_warn = 0;
2259 our $cnt_chk = 0;
2260
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002261 # Trace the real file/line as we go.
2262 my $realfile = '';
2263 my $realline = 0;
2264 my $realcnt = 0;
2265 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002266 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002267 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002268 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002269 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002270 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002271
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002272 my $prev_values = 'E';
2273
2274 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002275 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002276 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002277 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002278 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002279
Joe Perches7e51f192013-09-11 14:23:57 -07002280 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002281
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002282 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002283 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002284 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002285 my @setup_docs = ();
2286 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287
Joe Perchesd8b07712013-11-12 15:10:06 -08002288 my $camelcase_file_seeded = 0;
2289
Rob Herring9f3a8992018-04-10 16:33:13 -07002290 my $checklicenseline = 1;
2291
Andy Whitcroft773647a2008-03-28 14:15:58 -07002292 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002293 my $line;
2294 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002295 $linenr++;
2296 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002297
Joe Perches3705ce52013-07-03 15:05:31 -07002298 push(@fixed, $rawline) if ($fix);
2299
Andy Whitcroft773647a2008-03-28 14:15:58 -07002300 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002301 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002302 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002303 $setup_docs = 1;
2304 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002305 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002306 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002307 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002308 $realline=$1-1;
2309 if (defined $2) {
2310 $realcnt=$3+1;
2311 } else {
2312 $realcnt=1+1;
2313 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002314 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002315
2316 # Guestimate if this is a continuing comment. Run
2317 # the context looking for a comment "edge". If this
2318 # edge is a close comment then we must be in a comment
2319 # at context start.
2320 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002321 my $cnt = $realcnt;
2322 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2323 next if (defined $rawlines[$ln - 1] &&
2324 $rawlines[$ln - 1] =~ /^-/);
2325 $cnt--;
2326 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002327 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002328 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2329 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2330 ($edge) = $1;
2331 last;
2332 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333 }
2334 if (defined $edge && $edge eq '*/') {
2335 $in_comment = 1;
2336 }
2337
2338 # Guestimate if this is a continuing comment. If this
2339 # is the start of a diff block and this line starts
2340 # ' *' then it is very likely a comment.
2341 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002342 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002343 {
2344 $in_comment = 1;
2345 }
2346
2347 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2348 sanitise_line_reset($in_comment);
2349
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002350 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002351 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002352 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 $line = sanitise_line($rawline);
2354 }
2355 push(@lines, $line);
2356
2357 if ($realcnt > 1) {
2358 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2359 } else {
2360 $realcnt = 0;
2361 }
2362
2363 #print "==>$rawline\n";
2364 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002365
2366 if ($setup_docs && $line =~ /^\+/) {
2367 push(@setup_docs, $line);
2368 }
2369 }
2370
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002371 $prefix = '';
2372
Andy Whitcroft773647a2008-03-28 14:15:58 -07002373 $realcnt = 0;
2374 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002375 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002376 foreach my $line (@lines) {
2377 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002378 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002379 my $sline = $line; #copy of $line
2380 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002381
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002382 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002383
Joe Perches12c253a2018-06-07 17:10:58 -07002384# check if it's a mode change, rename or start of a patch
2385 if (!$in_commit_log &&
2386 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2387 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2388 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2389 $is_patch = 1;
2390 }
2391
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002392#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002393 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002394 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2395 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002396 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002397 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002398 $realline=$1-1;
2399 if (defined $2) {
2400 $realcnt=$3+1;
2401 } else {
2402 $realcnt=1+1;
2403 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002404 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002405 $prev_values = 'E';
2406
Andy Whitcroft773647a2008-03-28 14:15:58 -07002407 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002408 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002409 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002410 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002411 if ($context =~ /\b(\w+)\s*\(/) {
2412 $context_function = $1;
2413 } else {
2414 undef $context_function;
2415 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002416 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002417
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002418# track the line number as we move through the hunk, note that
2419# new versions of GNU diff omit the leading space on completely
2420# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002421 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002422 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002423 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002424
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002425 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002426 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002427
2428 # Track the previous line.
2429 ($prevline, $stashline) = ($stashline, $line);
2430 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002431 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2432
Andy Whitcroft773647a2008-03-28 14:15:58 -07002433 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002434
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002435 } elsif ($realcnt == 1) {
2436 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002437 }
2438
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002439 my $hunk_line = ($realcnt != 0);
2440
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002441 $here = "#$linenr: " if (!$file);
2442 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002443
Joe Perches2ac73b42014-06-04 16:12:05 -07002444 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002445 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002446 if ($line =~ /^diff --git.*?(\S+)$/) {
2447 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002448 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002449 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002450 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002451 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002452 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002453 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002454 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002455
2456 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002457 if (!$file && $tree && $p1_prefix ne '' &&
2458 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002459 WARN("PATCH_PREFIX",
2460 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002461 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002462
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002463 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002464 ERROR("MODIFIED_INCLUDE_ASM",
2465 "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 -07002466 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002467 $found_file = 1;
2468 }
2469
Joe Perches34d88152015-06-25 15:03:05 -07002470#make up the handle for any error we report on this line
2471 if ($showfile) {
2472 $prefix = "$realfile:$realline: "
2473 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002474 if ($file) {
2475 $prefix = "$filename:$realline: ";
2476 } else {
2477 $prefix = "$filename:$linenr: ";
2478 }
Joe Perches34d88152015-06-25 15:03:05 -07002479 }
2480
Joe Perches2ac73b42014-06-04 16:12:05 -07002481 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002482 if (is_maintained_obsolete($realfile)) {
2483 WARN("OBSOLETE",
2484 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2485 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002486 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002487 $check = 1;
2488 } else {
2489 $check = $check_orig;
2490 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002491 $checklicenseline = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002492 next;
2493 }
2494
Randy Dunlap389834b2007-06-08 13:47:03 -07002495 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002496
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002497 my $hereline = "$here\n$rawline\n";
2498 my $herecurr = "$here\n$rawline\n";
2499 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002500
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002501 $cnt_lines++ if ($realcnt != 0);
2502
Joe Perchese518e9a2015-06-25 15:03:27 -07002503# Check if the commit log has what seems like a diff which can confuse patch
2504 if ($in_commit_log && !$commit_log_has_diff &&
2505 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2506 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2507 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2508 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2509 ERROR("DIFF_IN_COMMIT_MSG",
2510 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2511 $commit_log_has_diff = 1;
2512 }
2513
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002514# Check for incorrect file permissions
2515 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2516 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002517 if ($realfile !~ m@scripts/@ &&
2518 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002519 ERROR("EXECUTE_PERMISSIONS",
2520 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002521 }
2522 }
2523
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002524# Check the patch for a From:
2525 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2526 $author = $1;
2527 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2528 $author =~ s/"//g;
2529 }
2530
Joe Perches20112472011-07-25 17:13:23 -07002531# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002532 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002533 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002534 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002535 if ($author ne '') {
2536 my $l = $line;
2537 $l =~ s/"//g;
2538 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2539 $authorsignoff = 1;
2540 }
2541 }
Joe Perches20112472011-07-25 17:13:23 -07002542 }
2543
Joe Perchese0d975b2014-12-10 15:51:49 -08002544# Check if MAINTAINERS is being updated. If so, there's probably no need to
2545# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2546 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2547 $reported_maintainer_file = 1;
2548 }
2549
Joe Perches20112472011-07-25 17:13:23 -07002550# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002551 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002552 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002553 my $space_before = $1;
2554 my $sign_off = $2;
2555 my $space_after = $3;
2556 my $email = $4;
2557 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2558
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002559 if ($sign_off !~ /$signature_tags/) {
2560 WARN("BAD_SIGN_OFF",
2561 "Non-standard signature: $sign_off\n" . $herecurr);
2562 }
Joe Perches20112472011-07-25 17:13:23 -07002563 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002564 if (WARN("BAD_SIGN_OFF",
2565 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2566 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002567 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002568 "$ucfirst_sign_off $email";
2569 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002570 }
Joe Perches20112472011-07-25 17:13:23 -07002571 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002572 if (WARN("BAD_SIGN_OFF",
2573 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2574 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002575 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002576 "$ucfirst_sign_off $email";
2577 }
2578
Joe Perches20112472011-07-25 17:13:23 -07002579 }
2580 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002581 if (WARN("BAD_SIGN_OFF",
2582 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2583 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002584 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002585 "$ucfirst_sign_off $email";
2586 }
Joe Perches20112472011-07-25 17:13:23 -07002587 }
2588
2589 my ($email_name, $email_address, $comment) = parse_email($email);
2590 my $suggested_email = format_email(($email_name, $email_address));
2591 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002592 ERROR("BAD_SIGN_OFF",
2593 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002594 } else {
2595 my $dequoted = $suggested_email;
2596 $dequoted =~ s/^"//;
2597 $dequoted =~ s/" </ </;
2598 # Don't force email to have quotes
2599 # Allow just an angle bracketed address
2600 if ("$dequoted$comment" ne $email &&
2601 "<$email_address>$comment" ne $email &&
2602 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002603 WARN("BAD_SIGN_OFF",
2604 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002605 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002606 }
Joe Perches7e51f192013-09-11 14:23:57 -07002607
2608# Check for duplicate signatures
2609 my $sig_nospace = $line;
2610 $sig_nospace =~ s/\s//g;
2611 $sig_nospace = lc($sig_nospace);
2612 if (defined $signatures{$sig_nospace}) {
2613 WARN("BAD_SIGN_OFF",
2614 "Duplicate signature\n" . $herecurr);
2615 } else {
2616 $signatures{$sig_nospace} = 1;
2617 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002618 }
2619
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002620# Check email subject for common tools that don't need to be mentioned
2621 if ($in_header_lines &&
2622 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2623 WARN("EMAIL_SUBJECT",
2624 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2625 }
2626
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002627# Check for unwanted Gerrit info
2628 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2629 ERROR("GERRIT_CHANGE_ID",
2630 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2631 }
2632
Joe Perches369c8dd2015-11-06 16:31:34 -08002633# Check if the commit log is in a possible stack dump
2634 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2635 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2636 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2637 # timestamp
2638 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2639 # stack dump address
2640 $commit_log_possible_stack_dump = 1;
2641 }
2642
Joe Perches2a076f42015-04-16 12:44:28 -07002643# Check for line lengths > 75 in commit log, warn once
2644 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002645 length($line) > 75 &&
2646 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2647 # file delta changes
2648 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2649 # filename then :
2650 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2651 # A Fixes: or Link: line
2652 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002653 WARN("COMMIT_LOG_LONG_LINE",
2654 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2655 $commit_log_long_line = 1;
2656 }
2657
Joe Perchesbf4daf12015-09-09 15:37:50 -07002658# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002659 if ($in_commit_log && $commit_log_possible_stack_dump &&
2660 $line =~ /^\s*$/) {
2661 $commit_log_possible_stack_dump = 0;
2662 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002663
Joe Perches0d7835f2015-02-13 14:38:35 -08002664# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002665 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002666 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002667 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002668 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002669 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002670 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2671 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002672 my $init_char = "c";
2673 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002674 my $short = 1;
2675 my $long = 0;
2676 my $case = 1;
2677 my $space = 1;
2678 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002679 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002680 my $id = '0123456789ab';
2681 my $orig_desc = "commit description";
2682 my $description = "";
2683
Joe Perchesfe043ea2015-09-09 15:37:25 -07002684 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2685 $init_char = $1;
2686 $orig_commit = lc($2);
2687 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2688 $orig_commit = lc($1);
2689 }
2690
Joe Perches0d7835f2015-02-13 14:38:35 -08002691 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2692 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2693 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2694 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2695 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2696 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002697 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002698 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2699 defined $rawlines[$linenr] &&
2700 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2701 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002702 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002703 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2704 defined $rawlines[$linenr] &&
2705 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2706 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2707 $orig_desc = $1;
2708 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2709 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002710 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002711 }
2712
2713 ($id, $description) = git_commit_info($orig_commit,
2714 $id, $orig_desc);
2715
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002716 if (defined($id) &&
2717 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002718 ERROR("GIT_COMMIT_ID",
2719 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2720 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002721 }
2722
Joe Perches13f19372014-08-06 16:10:59 -07002723# Check for added, moved or deleted files
2724 if (!$reported_maintainer_file && !$in_commit_log &&
2725 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2726 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2727 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2728 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002729 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002730 $reported_maintainer_file = 1;
2731 WARN("FILE_PATH_CHANGES",
2732 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2733 }
2734
Andy Whitcroft00df3442007-06-08 13:47:06 -07002735# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002736 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002737 ERROR("CORRUPTED_PATCH",
2738 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002739 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002740 }
2741
2742# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2743 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002744 $rawline !~ m/^$UTF8*$/) {
2745 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2746
2747 my $blank = copy_spacing($rawline);
2748 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2749 my $hereptr = "$hereline$ptr\n";
2750
Joe Perches34d99212011-07-25 17:13:26 -07002751 CHK("INVALID_UTF8",
2752 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002753 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002754
Joe Perches15662b32011-10-31 17:13:12 -07002755# Check if it's the start of a commit log
2756# (not a header line and we haven't seen the patch filename)
2757 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002758 !($rawline =~ /^\s+(?:\S|$)/ ||
2759 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002760 $in_header_lines = 0;
2761 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002762 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002763 }
2764
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002765# Check if there is UTF-8 in a commit log when a mail header has explicitly
2766# declined it, i.e defined some charset where it is missing.
2767 if ($in_header_lines &&
2768 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2769 $1 !~ /utf-8/i) {
2770 $non_utf8_charset = 1;
2771 }
2772
2773 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002774 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002775 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002776 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2777 }
2778
Joe Perchesd6430f72016-12-12 16:46:28 -08002779# Check for absolute kernel paths in commit message
2780 if ($tree && $in_commit_log) {
2781 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2782 my $file = $1;
2783
2784 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2785 check_absolute_file($1, $herecurr)) {
2786 #
2787 } else {
2788 check_absolute_file($file, $herecurr);
2789 }
2790 }
2791 }
2792
Kees Cook66b47b42014-10-13 15:51:57 -07002793# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002794 if (defined($misspellings) &&
2795 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002796 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002797 my $typo = $1;
2798 my $typo_fix = $spelling_fix{lc($typo)};
2799 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2800 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002801 my $msg_level = \&WARN;
2802 $msg_level = \&CHK if ($file);
2803 if (&{$msg_level}("TYPO_SPELLING",
2804 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002805 $fix) {
2806 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2807 }
2808 }
2809 }
2810
Andy Whitcroft306708542008-10-15 22:02:28 -07002811# ignore non-hunk lines and lines being removed
2812 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002813
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002814#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002815 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002816 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002817 if (ERROR("DOS_LINE_ENDINGS",
2818 "DOS line endings\n" . $herevet) &&
2819 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002820 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002821 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002822 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2823 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002824 if (ERROR("TRAILING_WHITESPACE",
2825 "trailing whitespace\n" . $herevet) &&
2826 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002827 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002828 }
2829
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002830 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002831 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002832
Josh Triplett4783f892013-11-12 15:10:12 -08002833# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002834 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002835 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002836 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2837 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002838 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002839 my $msg_level = \&ERROR;
2840 $msg_level = \&CHK if ($file);
2841 &{$msg_level}("FSF_MAILING_ADDRESS",
2842 "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 -08002843 }
2844
Andi Kleen33549572010-05-24 14:33:29 -07002845# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002846# Only applies when adding the entry originally, after that we do not have
2847# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002848 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002849 # 'choice' is usually the last thing on the line (though
2850 # Kconfig supports named choices), so use a word boundary
2851 # (\b) rather than a whitespace character (\s)
2852 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07002853 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002854 my $cnt = $realcnt;
2855 my $ln = $linenr + 1;
2856 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002857 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002858 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002859 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002860 $f = $lines[$ln - 1];
2861 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2862 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002863
2864 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002865 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002866
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01002867 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002868 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01002869 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2870 if ($lines[$ln - 1] =~ "---help---") {
2871 WARN("CONFIG_DESCRIPTION",
2872 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2873 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002874 $length = -1;
2875 }
2876
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002877 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002878 $f =~ s/#.*//;
2879 $f =~ s/^\s+//;
2880 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01002881
2882 # This only checks context lines in the patch
2883 # and so hopefully shouldn't trigger false
2884 # positives, even though some of these are
2885 # common words in help texts
2886 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2887 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002888 $is_end = 1;
2889 last;
2890 }
Andi Kleen33549572010-05-24 14:33:29 -07002891 $length++;
2892 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002893 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2894 WARN("CONFIG_DESCRIPTION",
2895 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2896 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002897 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002898 }
2899
Joe Perches628f91a2017-07-10 15:52:07 -07002900# check for MAINTAINERS entries that don't have the right form
2901 if ($realfile =~ /^MAINTAINERS$/ &&
2902 $rawline =~ /^\+[A-Z]:/ &&
2903 $rawline !~ /^\+[A-Z]:\t\S/) {
2904 if (WARN("MAINTAINERS_STYLE",
2905 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2906 $fix) {
2907 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2908 }
2909 }
2910
Christoph Jaeger327953e2015-02-13 14:38:29 -08002911# discourage the use of boolean for type definition attributes of Kconfig options
2912 if ($realfile =~ /Kconfig/ &&
2913 $line =~ /^\+\s*\bboolean\b/) {
2914 WARN("CONFIG_TYPE_BOOLEAN",
2915 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2916 }
2917
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002918 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2919 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2920 my $flag = $1;
2921 my $replacement = {
2922 'EXTRA_AFLAGS' => 'asflags-y',
2923 'EXTRA_CFLAGS' => 'ccflags-y',
2924 'EXTRA_CPPFLAGS' => 'cppflags-y',
2925 'EXTRA_LDFLAGS' => 'ldflags-y',
2926 };
2927
2928 WARN("DEPRECATED_VARIABLE",
2929 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2930 }
2931
Rob Herringbff5da42014-01-23 15:54:51 -08002932# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002933 if (defined $root &&
2934 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2935 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2936
Rob Herringbff5da42014-01-23 15:54:51 -08002937 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2938
Florian Vaussardcc933192014-04-03 14:49:27 -07002939 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2940 my $vp_file = $dt_path . "vendor-prefixes.txt";
2941
Rob Herringbff5da42014-01-23 15:54:51 -08002942 foreach my $compat (@compats) {
2943 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002944 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2945 my $compat3 = $compat;
2946 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2947 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002948 if ( $? >> 8 ) {
2949 WARN("UNDOCUMENTED_DT_STRING",
2950 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2951 }
2952
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002953 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2954 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002955 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002956 if ( $? >> 8 ) {
2957 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002958 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002959 }
2960 }
2961 }
2962
Rob Herring9f3a8992018-04-10 16:33:13 -07002963# check for using SPDX license tag at beginning of files
2964 if ($realline == $checklicenseline) {
2965 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
2966 $checklicenseline = 2;
2967 } elsif ($rawline =~ /^\+/) {
2968 my $comment = "";
2969 if ($realfile =~ /\.(h|s|S)$/) {
2970 $comment = '/*';
2971 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
2972 $comment = '//';
2973 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
2974 $comment = '#';
2975 } elsif ($realfile =~ /\.rst$/) {
2976 $comment = '..';
2977 }
2978
2979 if ($comment !~ /^$/ &&
2980 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
2981 WARN("SPDX_LICENSE_TAG",
2982 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
2983 }
2984 }
2985 }
2986
Andy Whitcroft5368df202008-10-15 22:02:27 -07002987# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002988 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002989
Joe Perches47e0c882015-06-25 15:02:57 -07002990# line length limit (with some exclusions)
2991#
2992# There are a few types of lines that may extend beyond $max_line_length:
2993# logging functions like pr_info that end in a string
2994# lines with a single string
2995# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08002996# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07002997#
2998# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07002999# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003000# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3001# LONG_LINE all other lines longer than $max_line_length
3002#
3003# if LONG_LINE is ignored, the other 2 types are also ignored
3004#
3005
Joe Perchesb4749e92015-07-17 16:24:01 -07003006 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003007 my $msg_type = "LONG_LINE";
3008
3009 # Check the allowed long line types first
3010
3011 # logging functions that end in a string that starts
3012 # before $max_line_length
3013 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3014 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3015 $msg_type = "";
3016
3017 # lines with only strings (w/ possible termination)
3018 # #defines with only strings
3019 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3020 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3021 $msg_type = "";
3022
Joe Perchescc147502017-11-17 15:28:44 -08003023 # More special cases
3024 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3025 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003026 $msg_type = "";
3027
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003028 # URL ($rawline is used in case the URL is in a comment)
3029 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3030 $msg_type = "";
3031
Joe Perches47e0c882015-06-25 15:02:57 -07003032 # Otherwise set the alternate message types
3033
3034 # a comment starts before $max_line_length
3035 } elsif ($line =~ /($;[\s$;]*)$/ &&
3036 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3037 $msg_type = "LONG_LINE_COMMENT"
3038
3039 # a quoted string starts before $max_line_length
3040 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3041 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3042 $msg_type = "LONG_LINE_STRING"
3043 }
3044
3045 if ($msg_type ne "" &&
3046 (show_type("LONG_LINE") || show_type($msg_type))) {
3047 WARN($msg_type,
3048 "line over $max_line_length characters\n" . $herecurr);
3049 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003050 }
3051
Andy Whitcroft8905a672007-11-28 16:21:06 -08003052# check for adding lines without a newline.
3053 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003054 WARN("MISSING_EOF_NEWLINE",
3055 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003056 }
3057
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003058# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003059 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003060
3061# at the beginning of a line any tabs must come first and anything
3062# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003063 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3064 $rawline =~ /^\+\s* \s*/) {
3065 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003066 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003067 if (ERROR("CODE_INDENT",
3068 "code indent should use tabs where possible\n" . $herevet) &&
3069 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003070 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003071 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003072 }
3073
Alberto Panizzo08e44362010-03-05 13:43:54 -08003074# check for space before tabs.
3075 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3076 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003077 if (WARN("SPACE_BEFORE_TAB",
3078 "please, no space before tabs\n" . $herevet) &&
3079 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003080 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003081 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003082 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003083 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003084 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003085 }
3086
Joe Perches6a487212018-04-10 16:34:04 -07003087# check for assignments on the start of a line
3088 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3089 CHK("ASSIGNMENT_CONTINUATIONS",
3090 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3091 }
3092
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003093# check for && or || at the start of a line
3094 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3095 CHK("LOGICAL_CONTINUATIONS",
3096 "Logical continuations should be on the previous line\n" . $hereprev);
3097 }
3098
Joe Perchesa91e8992016-05-20 17:04:05 -07003099# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003100 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003101 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003102 my $indent = length($1);
3103 if ($indent % 8) {
3104 if (WARN("TABSTOP",
3105 "Statements should start on a tabstop\n" . $herecurr) &&
3106 $fix) {
3107 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3108 }
3109 }
3110 }
3111
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003112# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003113 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003114 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003115 $prevline =~ /^\+(\t*)(.*)$/;
3116 my $oldindent = $1;
3117 my $rest = $2;
3118
3119 my $pos = pos_last_openparen($rest);
3120 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003121 $line =~ /^(\+| )([ \t]*)/;
3122 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003123
3124 my $goodtabindent = $oldindent .
3125 "\t" x ($pos / 8) .
3126 " " x ($pos % 8);
3127 my $goodspaceindent = $oldindent . " " x $pos;
3128
3129 if ($newindent ne $goodtabindent &&
3130 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003131
3132 if (CHK("PARENTHESIS_ALIGNMENT",
3133 "Alignment should match open parenthesis\n" . $hereprev) &&
3134 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003135 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003136 s/^\+[ \t]*/\+$goodtabindent/;
3137 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003138 }
3139 }
3140 }
3141
Joe Perches6ab3a972015-04-16 12:44:05 -07003142# check for space after cast like "(int) foo" or "(struct foo) bar"
3143# avoid checking a few false positives:
3144# "sizeof(<type>)" or "__alignof__(<type>)"
3145# function pointer declarations like "(*foo)(int) = bar;"
3146# structure definitions like "(struct foo) { 0 };"
3147# multiline macros that define functions
3148# known attributes or the __attribute__ keyword
3149 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3150 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003151 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003152 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003153 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003154 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003155 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003156 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003157 }
3158
Joe Perches86406b12015-09-09 15:37:41 -07003159# Block comment styles
3160# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003161 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003162 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003163 $rawline =~ /^\+[ \t]*\*/ &&
3164 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003165 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3166 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3167 }
3168
Joe Perches86406b12015-09-09 15:37:41 -07003169# Block comments use * on subsequent lines
3170 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3171 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003172 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003173 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003174 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003175 WARN("BLOCK_COMMENT_STYLE",
3176 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003177 }
3178
Joe Perches86406b12015-09-09 15:37:41 -07003179# Block comments use */ on trailing lines
3180 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003181 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3182 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3183 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003184 WARN("BLOCK_COMMENT_STYLE",
3185 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003186 }
3187
Joe Perches08eb9b82016-10-11 13:51:50 -07003188# Block comment * alignment
3189 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003190 $line =~ /^\+[ \t]*$;/ && #leading comment
3191 $rawline =~ /^\+[ \t]*\*/ && #leading *
3192 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003193 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003194 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3195 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003196 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003197 if (defined($1)) {
3198 $oldindent = expand_tabs($1);
3199 } else {
3200 $prevrawline =~ m@^\+(.*/?)\*@;
3201 $oldindent = expand_tabs($1);
3202 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003203 $rawline =~ m@^\+([ \t]*)\*@;
3204 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003205 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003206 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003207 WARN("BLOCK_COMMENT_STYLE",
3208 "Block comments should align the * on each line\n" . $hereprev);
3209 }
3210 }
3211
Joe Perches7f619192014-08-06 16:10:39 -07003212# check for missing blank lines after struct/union declarations
3213# with exceptions for various attributes and macros
3214 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3215 $line =~ /^\+/ &&
3216 !($line =~ /^\+\s*$/ ||
3217 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3218 $line =~ /^\+\s*MODULE_/i ||
3219 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3220 $line =~ /^\+[a-z_]*init/ ||
3221 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3222 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003223 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003224 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003225 if (CHK("LINE_SPACING",
3226 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3227 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003228 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003229 }
Joe Perches7f619192014-08-06 16:10:39 -07003230 }
3231
Joe Perches365dd4e2014-08-06 16:10:42 -07003232# check for multiple consecutive blank lines
3233 if ($prevline =~ /^[\+ ]\s*$/ &&
3234 $line =~ /^\+\s*$/ &&
3235 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003236 if (CHK("LINE_SPACING",
3237 "Please don't use multiple blank lines\n" . $hereprev) &&
3238 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003239 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003240 }
3241
Joe Perches365dd4e2014-08-06 16:10:42 -07003242 $last_blank_line = $linenr;
3243 }
3244
Joe Perches3b617e32014-04-03 14:49:28 -07003245# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003246 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3247 # actual declarations
3248 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003249 # function pointer declarations
3250 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003251 # foo bar; where foo is some local typedef or #define
3252 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3253 # known declaration macros
3254 $prevline =~ /^\+\s+$declaration_macros/) &&
3255 # for "else if" which can look like "$Ident $Ident"
3256 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3257 # other possible extensions of declaration lines
3258 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3259 # not starting a section or a macro "\" extended line
3260 $prevline =~ /(?:\{\s*|\\)$/) &&
3261 # looks like a declaration
3262 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003263 # function pointer declarations
3264 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003265 # foo bar; where foo is some local typedef or #define
3266 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3267 # known declaration macros
3268 $sline =~ /^\+\s+$declaration_macros/ ||
3269 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003270 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003271 # start or end of block or continuation of declaration
3272 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3273 # bitfield continuation
3274 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3275 # other possible extensions of declaration lines
3276 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3277 # indentation of previous and current line are the same
3278 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003279 if (WARN("LINE_SPACING",
3280 "Missing a blank line after declarations\n" . $hereprev) &&
3281 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003282 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003283 }
Joe Perches3b617e32014-04-03 14:49:28 -07003284 }
3285
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003286# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003287# Exceptions:
3288# 1) within comments
3289# 2) indented preprocessor commands
3290# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003291 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003292 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003293 if (WARN("LEADING_SPACE",
3294 "please, no spaces at the start of a line\n" . $herevet) &&
3295 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003296 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003297 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003298 }
3299
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003300# check we are in a valid C source file if not then ignore this hunk
3301 next if ($realfile !~ /\.(h|c)$/);
3302
Joe Perches5751a242017-11-17 15:28:52 -08003303# check for unusual line ending [ or (
3304 if ($line =~ /^\+.*([\[\(])\s*$/) {
3305 CHK("OPEN_ENDED_LINE",
3306 "Lines should not end with a '$1'\n" . $herecurr);
3307 }
3308
Joe Perches4dbed762017-05-08 15:55:39 -07003309# check if this appears to be the start function declaration, save the name
3310 if ($sline =~ /^\+\{\s*$/ &&
3311 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3312 $context_function = $1;
3313 }
3314
3315# check if this appears to be the end of function declaration
3316 if ($sline =~ /^\+\}\s*$/) {
3317 undef $context_function;
3318 }
3319
Joe Perches032a4c02014-08-06 16:10:29 -07003320# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003321# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003322# if the previous line is a break or return and is indented 1 tab more...
3323 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3324 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003325 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3326 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3327 defined $lines[$linenr] &&
3328 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003329 WARN("UNNECESSARY_ELSE",
3330 "else is not generally useful after a break or return\n" . $hereprev);
3331 }
3332 }
3333
Joe Perchesc00df192014-08-06 16:11:01 -07003334# check indentation of a line with a break;
3335# if the previous line is a goto or return and is indented the same # of tabs
3336 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3337 my $tabs = $1;
3338 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3339 WARN("UNNECESSARY_BREAK",
3340 "break is not useful after a goto or return\n" . $hereprev);
3341 }
3342 }
3343
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003344# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003345 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003346 WARN("CVS_KEYWORD",
3347 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003348 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003349
Joe Perches56e77d72013-02-21 16:44:14 -08003350# check for old HOTPLUG __dev<foo> section markings
3351 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3352 WARN("HOTPLUG_SECTION",
3353 "Using $1 is unnecessary\n" . $herecurr);
3354 }
3355
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003356# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003357 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3358 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003359#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003360 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003361 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003362 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003363 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003364 $stat =~ s/\n./\n /g;
3365 $cond =~ s/\n./\n /g;
3366
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003367#print "linenr<$linenr> <$stat>\n";
3368 # If this statement has no statement boundaries within
3369 # it there is no point in retrying a statement scan
3370 # until we hit end of it.
3371 my $frag = $stat; $frag =~ s/;+\s*$//;
3372 if ($frag !~ /(?:{|;)/) {
3373#print "skip<$line_nr_next>\n";
3374 $suppress_statement = $line_nr_next;
3375 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003376
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003377 # Find the real next line.
3378 $realline_next = $line_nr_next;
3379 if (defined $realline_next &&
3380 (!defined $lines[$realline_next - 1] ||
3381 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3382 $realline_next++;
3383 }
3384
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003385 my $s = $stat;
3386 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003387
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003388 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003389 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003390
3391 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003392 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003393
Andy Whitcroft463f2862009-09-21 17:04:34 -07003394 } elsif ($s =~ /^.\s*else\b/s) {
3395
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003396 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003397 } 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 -07003398 my $type = $1;
3399 $type =~ s/\s+/ /g;
3400 possible($type, "A:" . $s);
3401
Andy Whitcroft8905a672007-11-28 16:21:06 -08003402 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003403 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003404 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003405 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003406
3407 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003408 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003409 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003410 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003411
3412 # Check for any sort of function declaration.
3413 # int foo(something bar, other baz);
3414 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003415 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 -08003416 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003417
Andy Whitcroftcf655042008-03-04 14:28:20 -08003418 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003419 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003420 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003421
Andy Whitcroft8905a672007-11-28 16:21:06 -08003422 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003423 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003424
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003425 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003426 }
3427 }
3428 }
3429
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003430 }
3431
Andy Whitcroft653d4872007-06-23 17:16:34 -07003432#
3433# Checks which may be anchored in the context.
3434#
3435
3436# Check for switch () and associated case and default
3437# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003438 if ($line=~/\bswitch\s*\(.*\)/) {
3439 my $err = '';
3440 my $sep = '';
3441 my @ctx = ctx_block_outer($linenr, $realcnt);
3442 shift(@ctx);
3443 for my $ctx (@ctx) {
3444 my ($clen, $cindent) = line_stats($ctx);
3445 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3446 $indent != $cindent) {
3447 $err .= "$sep$ctx\n";
3448 $sep = '';
3449 } else {
3450 $sep = "[...]\n";
3451 }
3452 }
3453 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003454 ERROR("SWITCH_CASE_INDENT_LEVEL",
3455 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003456 }
3457 }
3458
3459# if/while/etc brace do not go on next line, unless defining a do while loop,
3460# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003461 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 -07003462 my $pre_ctx = "$1$2";
3463
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003464 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003465
3466 if ($line =~ /^\+\t{6,}/) {
3467 WARN("DEEP_INDENTATION",
3468 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3469 }
3470
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003471 my $ctx_cnt = $realcnt - $#ctx - 1;
3472 my $ctx = join("\n", @ctx);
3473
Andy Whitcroft548596d2008-07-23 21:29:01 -07003474 my $ctx_ln = $linenr;
3475 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003476
Andy Whitcroft548596d2008-07-23 21:29:01 -07003477 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3478 defined $lines[$ctx_ln - 1] &&
3479 $lines[$ctx_ln - 1] =~ /^-/)) {
3480 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3481 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003482 $ctx_ln++;
3483 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003484
Andy Whitcroft53210162008-07-23 21:29:03 -07003485 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3486 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003487
Joe Perchesd752fcc2014-08-06 16:11:05 -07003488 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003489 ERROR("OPEN_BRACE",
3490 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003491 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003492 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003493 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3494 $ctx =~ /\)\s*\;\s*$/ &&
3495 defined $lines[$ctx_ln - 1])
3496 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003497 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3498 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003499 WARN("TRAILING_SEMICOLON",
3500 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003501 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003502 }
3503 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003504 }
3505
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003506# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003507 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003508 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3509 ctx_statement_block($linenr, $realcnt, 0)
3510 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003511 my ($s, $c) = ($stat, $cond);
3512
3513 substr($s, 0, length($c), '');
3514
Joe Perches9f5af482015-09-09 15:37:30 -07003515 # remove inline comments
3516 $s =~ s/$;/ /g;
3517 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003518
3519 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003520 my @newlines = ($c =~ /\n/gs);
3521 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003522
Joe Perches9f5af482015-09-09 15:37:30 -07003523 # Make sure we remove the line prefixes as we have
3524 # none on the first line, and are going to readd them
3525 # where necessary.
3526 $s =~ s/\n./\n/gs;
3527 while ($s =~ /\n\s+\\\n/) {
3528 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3529 }
3530
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003531 # We want to check the first line inside the block
3532 # starting at the end of the conditional, so remove:
3533 # 1) any blank line termination
3534 # 2) any opening brace { on end of the line
3535 # 3) any do (...) {
3536 my $continuation = 0;
3537 my $check = 0;
3538 $s =~ s/^.*\bdo\b//;
3539 $s =~ s/^\s*{//;
3540 if ($s =~ s/^\s*\\//) {
3541 $continuation = 1;
3542 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003543 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003544 $check = 1;
3545 $cond_lines++;
3546 }
3547
3548 # Also ignore a loop construct at the end of a
3549 # preprocessor statement.
3550 if (($prevline =~ /^.\s*#\s*define\s/ ||
3551 $prevline =~ /\\\s*$/) && $continuation == 0) {
3552 $check = 0;
3553 }
3554
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003555 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003556 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003557 while ($cond_ptr != $cond_lines) {
3558 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003559
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003560 # If we see an #else/#elif then the code
3561 # is not linear.
3562 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3563 $check = 0;
3564 }
3565
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003566 # Ignore:
3567 # 1) blank lines, they should be at 0,
3568 # 2) preprocessor lines, and
3569 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003570 if ($continuation ||
3571 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003572 $s =~ /^\s*#\s*?/ ||
3573 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003574 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003575 if ($s =~ s/^.*?\n//) {
3576 $cond_lines++;
3577 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003578 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003579 }
3580
3581 my (undef, $sindent) = line_stats("+" . $s);
3582 my $stat_real = raw_line($linenr, $cond_lines);
3583
3584 # Check if either of these lines are modified, else
3585 # this is not this patch's fault.
3586 if (!defined($stat_real) ||
3587 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3588 $check = 0;
3589 }
3590 if (defined($stat_real) && $cond_lines > 1) {
3591 $stat_real = "[...]\n$stat_real";
3592 }
3593
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003594 #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 -07003595
Joe Perches9f5af482015-09-09 15:37:30 -07003596 if ($check && $s ne '' &&
3597 (($sindent % 8) != 0 ||
3598 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003599 ($sindent == $indent &&
3600 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003601 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003602 WARN("SUSPECT_CODE_INDENT",
3603 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003604 }
3605 }
3606
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003607 # Track the 'values' across context and added lines.
3608 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003609 my ($curr_values, $curr_vars) =
3610 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003611 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003612 if ($dbg_values) {
3613 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003614 print "$linenr > .$outline\n";
3615 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003616 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003617 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003618 $prev_values = substr($curr_values, -1);
3619
Andy Whitcroft00df3442007-06-08 13:47:06 -07003620#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003621 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003622
Joe Perches11ca40a2016-12-12 16:46:31 -08003623# check for dereferences that span multiple lines
3624 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3625 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3626 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3627 my $ref = $1;
3628 $line =~ /^.\s*($Lval)/;
3629 $ref .= $1;
3630 $ref =~ s/\s//g;
3631 WARN("MULTILINE_DEREFERENCE",
3632 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3633 }
3634
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003635# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003636 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 -07003637 my $type = $1;
3638 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003639 $var = "" if (!defined $var);
3640 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003641 my $sign = $1;
3642 my $pointer = $2;
3643
3644 $pointer = "" if (!defined $pointer);
3645
3646 if (WARN("UNSPECIFIED_INT",
3647 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3648 $fix) {
3649 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003650 my $comp_pointer = $pointer;
3651 $comp_pointer =~ s/\s//g;
3652 $decl .= $comp_pointer;
3653 $decl = rtrim($decl) if ($var eq "");
3654 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003655 }
3656 }
3657 }
3658
Andy Whitcroft653d4872007-06-23 17:16:34 -07003659# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003660 if ($dbg_type) {
3661 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003662 ERROR("TEST_TYPE",
3663 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003664 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003665 ERROR("TEST_NOT_TYPE",
3666 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003667 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003668 next;
3669 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003670# TEST: allow direct testing of the attribute matcher.
3671 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003672 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003673 ERROR("TEST_ATTR",
3674 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003675 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003676 ERROR("TEST_NOT_ATTR",
3677 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003678 }
3679 next;
3680 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003681
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003682# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003683 if ($line =~ /^.\s*{/ &&
3684 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003685 if (ERROR("OPEN_BRACE",
3686 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003687 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3688 fix_delete_line($fixlinenr - 1, $prevrawline);
3689 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003690 my $fixedline = $prevrawline;
3691 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003692 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003693 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003694 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003695 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003696 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003697 }
3698
Andy Whitcroft653d4872007-06-23 17:16:34 -07003699#
3700# Checks which are anchored on the added line.
3701#
3702
3703# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003704 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003705 my $path = $1;
3706 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003707 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003708 "malformed #include filename\n" . $herecurr);
3709 }
3710 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3711 ERROR("UAPI_INCLUDE",
3712 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003713 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003714 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003715
3716# no C99 // comments
3717 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003718 if (ERROR("C99_COMMENTS",
3719 "do not use C99 // comments\n" . $herecurr) &&
3720 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003721 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003722 if ($line =~ /\/\/(.*)$/) {
3723 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003724 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003725 }
3726 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003727 }
3728 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003729 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003730 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003731
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003732# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3733# the whole statement.
3734#print "APW <$lines[$realline_next - 1]>\n";
3735 if (defined $realline_next &&
3736 exists $lines[$realline_next - 1] &&
3737 !defined $suppress_export{$realline_next} &&
3738 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3739 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003740 # Handle definitions which produce identifiers with
3741 # a prefix:
3742 # XXX(foo);
3743 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003744 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003745 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003746 $name =~ /^${Ident}_$2/) {
3747#print "FOO C name<$name>\n";
3748 $suppress_export{$realline_next} = 1;
3749
3750 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003751 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003752 ^.DEFINE_$Ident\(\Q$name\E\)|
3753 ^.DECLARE_$Ident\(\Q$name\E\)|
3754 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003755 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3756 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003757 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003758#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3759 $suppress_export{$realline_next} = 2;
3760 } else {
3761 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003762 }
3763 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003764 if (!defined $suppress_export{$linenr} &&
3765 $prevline =~ /^.\s*$/ &&
3766 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3767 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3768#print "FOO B <$lines[$linenr - 1]>\n";
3769 $suppress_export{$linenr} = 2;
3770 }
3771 if (defined $suppress_export{$linenr} &&
3772 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003773 WARN("EXPORT_SYMBOL",
3774 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003775 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003776
Joe Eloff5150bda2010-08-09 17:21:00 -07003777# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003778 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003779 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003780 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003781 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003782 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003783 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003784 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003785# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003786 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003787 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003788 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003789 $herecurr) &&
3790 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003791 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003792 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003793 }
3794
Joe Perches18130872014-08-06 16:11:22 -07003795# check for misordered declarations of char/short/int/long with signed/unsigned
3796 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3797 my $tmp = trim($1);
3798 WARN("MISORDERED_TYPE",
3799 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3800 }
3801
Joe Perchescb710ec2010-10-26 14:23:20 -07003802# check for static const char * arrays.
3803 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003804 WARN("STATIC_CONST_CHAR_ARRAY",
3805 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003806 $herecurr);
3807 }
3808
3809# check for static char foo[] = "bar" declarations.
3810 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003811 WARN("STATIC_CONST_CHAR_ARRAY",
3812 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003813 $herecurr);
3814 }
3815
Joe Perchesab7e23f2015-04-16 12:44:22 -07003816# check for const <foo> const where <foo> is not a pointer or array type
3817 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3818 my $found = $1;
3819 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3820 WARN("CONST_CONST",
3821 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3822 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3823 WARN("CONST_CONST",
3824 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3825 }
3826 }
3827
Joe Perches9b0fa602014-04-03 14:49:18 -07003828# check for non-global char *foo[] = {"bar", ...} declarations.
3829 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3830 WARN("STATIC_CONST_CHAR_ARRAY",
3831 "char * array declaration might be better as static const\n" .
3832 $herecurr);
3833 }
3834
Joe Perchesb598b672015-04-16 12:44:36 -07003835# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3836 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3837 my $array = $1;
3838 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3839 my $array_div = $1;
3840 if (WARN("ARRAY_SIZE",
3841 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3842 $fix) {
3843 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3844 }
3845 }
3846 }
3847
Joe Perchesb36190c2014-01-27 17:07:18 -08003848# check for function declarations without arguments like "int foo()"
3849 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3850 if (ERROR("FUNCTION_WITHOUT_ARGS",
3851 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3852 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003853 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003854 }
3855 }
3856
Andy Whitcroft653d4872007-06-23 17:16:34 -07003857# check for new typedefs, only function parameters and sparse annotations
3858# make sense.
3859 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003860 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003861 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003862 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003863 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003864 WARN("NEW_TYPEDEFS",
3865 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003866 }
3867
3868# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003869 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003870 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3871 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003872 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003873
Andy Whitcroft65863862009-01-06 14:41:21 -08003874 # Should start with a space.
3875 $to =~ s/^(\S)/ $1/;
3876 # Should not end with a space.
3877 $to =~ s/\s+$//;
3878 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003879 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003880 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003881
Joe Perches3705ce52013-07-03 15:05:31 -07003882## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003883 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003884 if (ERROR("POINTER_LOCATION",
3885 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3886 $fix) {
3887 my $sub_from = $ident;
3888 my $sub_to = $ident;
3889 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003890 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003891 s@\Q$sub_from\E@$sub_to@;
3892 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003893 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003894 }
3895 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3896 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003897 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003898
Andy Whitcroft65863862009-01-06 14:41:21 -08003899 # Should start with a space.
3900 $to =~ s/^(\S)/ $1/;
3901 # Should not end with a space.
3902 $to =~ s/\s+$//;
3903 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003904 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003905 }
3906 # Modifiers should have spaces.
3907 $to =~ s/(\b$Modifier$)/$1 /;
3908
Joe Perches3705ce52013-07-03 15:05:31 -07003909## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003910 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003911 if (ERROR("POINTER_LOCATION",
3912 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3913 $fix) {
3914
3915 my $sub_from = $match;
3916 my $sub_to = $match;
3917 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003918 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003919 s@\Q$sub_from\E@$sub_to@;
3920 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003921 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003922 }
3923
Joe Perches9d3e3c72015-09-09 15:37:27 -07003924# avoid BUG() or BUG_ON()
3925 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07003926 my $msg_level = \&WARN;
3927 $msg_level = \&CHK if ($file);
3928 &{$msg_level}("AVOID_BUG",
3929 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Joe Perches9d3e3c72015-09-09 15:37:27 -07003930 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003931
Joe Perches9d3e3c72015-09-09 15:37:27 -07003932# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003933 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003934 WARN("LINUX_VERSION_CODE",
3935 "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 -08003936 }
3937
Joe Perches17441222011-06-15 15:08:17 -07003938# check for uses of printk_ratelimit
3939 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003940 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003941 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003942 }
3943
Joe Percheseeef5732017-11-17 15:28:41 -08003944# printk should use KERN_* levels
3945 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3946 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3947 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003948 }
3949
Joe Perches243f3802012-05-31 16:26:09 -07003950 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3951 my $orig = $1;
3952 my $level = lc($orig);
3953 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003954 my $level2 = $level;
3955 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003956 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003957 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003958 }
3959
3960 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003961 if (WARN("PREFER_PR_LEVEL",
3962 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3963 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003964 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003965 s/\bpr_warning\b/pr_warn/;
3966 }
Joe Perches243f3802012-05-31 16:26:09 -07003967 }
3968
Joe Perchesdc139312013-02-21 16:44:13 -08003969 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3970 my $orig = $1;
3971 my $level = lc($orig);
3972 $level = "warn" if ($level eq "warning");
3973 $level = "dbg" if ($level eq "debug");
3974 WARN("PREFER_DEV_LEVEL",
3975 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3976 }
3977
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003978# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3979# number of false positives, but assembly files are not checked, so at
3980# least the arch entry code will not trigger this warning.
3981 if ($line =~ /\bENOSYS\b/) {
3982 WARN("ENOSYS",
3983 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3984 }
3985
Andy Whitcroft653d4872007-06-23 17:16:34 -07003986# function brace can't be on same line, except for #defines of do while,
3987# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07003988 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08003989 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
3990 $sline !~ /\#\s*define\b.*do\s*\{/ &&
3991 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07003992 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08003993 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07003994 $fix) {
3995 fix_delete_line($fixlinenr, $rawline);
3996 my $fixed_line = $rawline;
3997 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3998 my $line1 = $1;
3999 my $line2 = $2;
4000 fix_insert_line($fixlinenr, ltrim($line1));
4001 fix_insert_line($fixlinenr, "\+{");
4002 if ($line2 !~ /^\s*$/) {
4003 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4004 }
4005 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004006 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004007
Andy Whitcroft8905a672007-11-28 16:21:06 -08004008# open braces for enum, union and struct go on the same line.
4009 if ($line =~ /^.\s*{/ &&
4010 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004011 if (ERROR("OPEN_BRACE",
4012 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4013 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4014 fix_delete_line($fixlinenr - 1, $prevrawline);
4015 fix_delete_line($fixlinenr, $rawline);
4016 my $fixedline = rtrim($prevrawline) . " {";
4017 fix_insert_line($fixlinenr, $fixedline);
4018 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004019 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004020 if ($fixedline !~ /^\+\s*$/) {
4021 fix_insert_line($fixlinenr, $fixedline);
4022 }
4023 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004024 }
4025
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004026# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004027 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4028 if (WARN("SPACING",
4029 "missing space after $1 definition\n" . $herecurr) &&
4030 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004031 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004032 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4033 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004034 }
4035
Joe Perches31070b52014-01-23 15:54:49 -08004036# Function pointer declarations
4037# check spacing between type, funcptr, and args
4038# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004039 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004040 my $declare = $1;
4041 my $pre_pointer_space = $2;
4042 my $post_pointer_space = $3;
4043 my $funcname = $4;
4044 my $post_funcname_space = $5;
4045 my $pre_args_space = $6;
4046
Joe Perches91f72e92014-04-03 14:49:12 -07004047# the $Declare variable will capture all spaces after the type
4048# so check it for a missing trailing missing space but pointer return types
4049# don't need a space so don't warn for those.
4050 my $post_declare_space = "";
4051 if ($declare =~ /(\s+)$/) {
4052 $post_declare_space = $1;
4053 $declare = rtrim($declare);
4054 }
4055 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004056 WARN("SPACING",
4057 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004058 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004059 }
4060
4061# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004062# This test is not currently implemented because these declarations are
4063# equivalent to
4064# int foo(int bar, ...)
4065# and this is form shouldn't/doesn't generate a checkpatch warning.
4066#
4067# elsif ($declare =~ /\s{2,}$/) {
4068# WARN("SPACING",
4069# "Multiple spaces after return type\n" . $herecurr);
4070# }
Joe Perches31070b52014-01-23 15:54:49 -08004071
4072# unnecessary space "type ( *funcptr)(args...)"
4073 if (defined $pre_pointer_space &&
4074 $pre_pointer_space =~ /^\s/) {
4075 WARN("SPACING",
4076 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4077 }
4078
4079# unnecessary space "type (* funcptr)(args...)"
4080 if (defined $post_pointer_space &&
4081 $post_pointer_space =~ /^\s/) {
4082 WARN("SPACING",
4083 "Unnecessary space before function pointer name\n" . $herecurr);
4084 }
4085
4086# unnecessary space "type (*funcptr )(args...)"
4087 if (defined $post_funcname_space &&
4088 $post_funcname_space =~ /^\s/) {
4089 WARN("SPACING",
4090 "Unnecessary space after function pointer name\n" . $herecurr);
4091 }
4092
4093# unnecessary space "type (*funcptr) (args...)"
4094 if (defined $pre_args_space &&
4095 $pre_args_space =~ /^\s/) {
4096 WARN("SPACING",
4097 "Unnecessary space before function pointer arguments\n" . $herecurr);
4098 }
4099
4100 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004101 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004102 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004103 }
4104 }
4105
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004106# check for spacing round square brackets; allowed:
4107# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004108# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4109# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004110 while ($line =~ /(.*?\s)\[/g) {
4111 my ($where, $prefix) = ($-[1], $1);
4112 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004113 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004114 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004115 if (ERROR("BRACKET_SPACE",
4116 "space prohibited before open square bracket '['\n" . $herecurr) &&
4117 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004118 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004119 s/^(\+.*?)\s+\[/$1\[/;
4120 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004121 }
4122 }
4123
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004124# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004125 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004126 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004127 my $ctx_before = substr($line, 0, $-[1]);
4128 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004129
4130 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004131 if ($name =~ /^(?:
4132 if|for|while|switch|return|case|
4133 volatile|__volatile__|
4134 __attribute__|format|__extension__|
4135 asm|__asm__)$/x)
4136 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004137 # cpp #define statements have non-optional spaces, ie
4138 # if there is a space between the name and the open
4139 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004140 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004141
4142 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004143 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004144
4145 # If this whole things ends with a type its most
4146 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004147 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004148
4149 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004150 if (WARN("SPACING",
4151 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4152 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004153 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004154 s/\b$name\s+\(/$name\(/;
4155 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004156 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004157 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004158
Andy Whitcroft653d4872007-06-23 17:16:34 -07004159# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004160 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004161 my $fixed_line = "";
4162 my $line_fixed = 0;
4163
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004164 my $ops = qr{
4165 <<=|>>=|<=|>=|==|!=|
4166 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4167 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004168 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004169 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004170 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004171 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004172
4173## print("element count: <" . $#elements . ">\n");
4174## foreach my $el (@elements) {
4175## print("el: <$el>\n");
4176## }
4177
4178 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004179 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004180
Joe Perches3705ce52013-07-03 15:05:31 -07004181 foreach my $el (@elements) {
4182 push(@fix_elements, substr($rawline, $off, length($el)));
4183 $off += length($el);
4184 }
4185
4186 $off = 0;
4187
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004188 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004189 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004190
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004191 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004192
4193 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4194
4195## print("n: <$n> good: <$good>\n");
4196
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004197 $off += length($elements[$n]);
4198
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004199 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004200 my $ca = substr($opline, 0, $off);
4201 my $cc = '';
4202 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4203 $cc = substr($opline, $off + length($elements[$n + 1]));
4204 }
4205 my $cb = "$ca$;$cc";
4206
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004207 my $a = '';
4208 $a = 'V' if ($elements[$n] ne '');
4209 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004210 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004211 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4212 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004213 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004214
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004215 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004216
4217 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004218 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004219 $c = 'V' if ($elements[$n + 2] ne '');
4220 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004221 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004222 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4223 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004224 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004225 } else {
4226 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004227 }
4228
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004229 my $ctx = "${a}x${c}";
4230
4231 my $at = "(ctx:$ctx)";
4232
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004233 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004234 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004235
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004236 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004237 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004238
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004239 # Get the full operator variant.
4240 my $opv = $op . substr($curr_vars, $off, 1);
4241
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004242 # Ignore operators passed as parameters.
4243 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004244 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004245
Andy Whitcroftcf655042008-03-04 14:28:20 -08004246# # Ignore comments
4247# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004248
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004249 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004250 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004251 if ($ctx !~ /.x[WEBC]/ &&
4252 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004253 if (ERROR("SPACING",
4254 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004255 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004256 $line_fixed = 1;
4257 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004258 }
4259
4260 # // is a comment
4261 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004262
Joe Perchesb00e4812014-04-03 14:49:33 -07004263 # : when part of a bitfield
4264 } elsif ($opv eq ':B') {
4265 # skip the bitfield test for now
4266
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004267 # No spaces for:
4268 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004269 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004270 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004271 if (ERROR("SPACING",
4272 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004273 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004274 if (defined $fix_elements[$n + 2]) {
4275 $fix_elements[$n + 2] =~ s/^\s+//;
4276 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004277 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004278 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004279 }
4280
Joe Perches23810972014-12-10 15:51:32 -08004281 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004282 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004283 my $rtrim_before = 0;
4284 my $space_after = 0;
4285 if ($ctx =~ /Wx./) {
4286 if (ERROR("SPACING",
4287 "space prohibited before that '$op' $at\n" . $hereptr)) {
4288 $line_fixed = 1;
4289 $rtrim_before = 1;
4290 }
4291 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004292 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004293 if (ERROR("SPACING",
4294 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004295 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004296 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004297 $space_after = 1;
4298 }
4299 }
4300 if ($rtrim_before || $space_after) {
4301 if ($rtrim_before) {
4302 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4303 } else {
4304 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4305 }
4306 if ($space_after) {
4307 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004308 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004309 }
4310
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004311 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004312 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004313 #warn "'*' is part of type\n";
4314
4315 # unary operators should have a space before and
4316 # none after. May be left adjacent to another
4317 # unary operator, or a cast
4318 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004319 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004320 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004321 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004322 if (ERROR("SPACING",
4323 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004324 if ($n != $last_after + 2) {
4325 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4326 $line_fixed = 1;
4327 }
Joe Perches3705ce52013-07-03 15:05:31 -07004328 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004329 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004330 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004331 # A unary '*' may be const
4332
4333 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004334 if (ERROR("SPACING",
4335 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004336 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004337 if (defined $fix_elements[$n + 2]) {
4338 $fix_elements[$n + 2] =~ s/^\s+//;
4339 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004340 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004341 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004342 }
4343
4344 # unary ++ and unary -- are allowed no space on one side.
4345 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004346 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004347 if (ERROR("SPACING",
4348 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004349 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004350 $line_fixed = 1;
4351 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004352 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004353 if ($ctx =~ /Wx[BE]/ ||
4354 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004355 if (ERROR("SPACING",
4356 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004357 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004358 $line_fixed = 1;
4359 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004360 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004361 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004362 if (ERROR("SPACING",
4363 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004364 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004365 if (defined $fix_elements[$n + 2]) {
4366 $fix_elements[$n + 2] =~ s/^\s+//;
4367 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004368 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004369 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004370 }
4371
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004372 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004373 } elsif ($op eq '<<' or $op eq '>>' or
4374 $op eq '&' or $op eq '^' or $op eq '|' or
4375 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004376 $op eq '*' or $op eq '/' or
4377 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004378 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004379 if ($check) {
4380 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4381 if (CHK("SPACING",
4382 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4383 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4384 $fix_elements[$n + 2] =~ s/^\s+//;
4385 $line_fixed = 1;
4386 }
4387 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4388 if (CHK("SPACING",
4389 "space preferred before that '$op' $at\n" . $hereptr)) {
4390 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4391 $line_fixed = 1;
4392 }
4393 }
4394 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004395 if (ERROR("SPACING",
4396 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004397 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4398 if (defined $fix_elements[$n + 2]) {
4399 $fix_elements[$n + 2] =~ s/^\s+//;
4400 }
Joe Perches3705ce52013-07-03 15:05:31 -07004401 $line_fixed = 1;
4402 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004403 }
4404
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004405 # A colon needs no spaces before when it is
4406 # terminating a case value or a label.
4407 } elsif ($opv eq ':C' || $opv eq ':L') {
4408 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004409 if (ERROR("SPACING",
4410 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004411 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004412 $line_fixed = 1;
4413 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004414 }
4415
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004416 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004417 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004418 my $ok = 0;
4419
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004420 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004421 if (($op eq '<' &&
4422 $cc =~ /^\S+\@\S+>/) ||
4423 ($op eq '>' &&
4424 $ca =~ /<\S+\@\S+$/))
4425 {
4426 $ok = 1;
4427 }
4428
Joe Perchese0df7e12015-04-16 12:44:53 -07004429 # for asm volatile statements
4430 # ignore a colon with another
4431 # colon immediately before or after
4432 if (($op eq ':') &&
4433 ($ca =~ /:$/ || $cc =~ /^:/)) {
4434 $ok = 1;
4435 }
4436
Joe Perches84731622013-11-12 15:10:05 -08004437 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004438 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004439 my $msg_level = \&ERROR;
4440 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004441
Jean Delvare0675a8f2017-09-08 16:16:07 -07004442 if (&{$msg_level}("SPACING",
4443 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004444 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4445 if (defined $fix_elements[$n + 2]) {
4446 $fix_elements[$n + 2] =~ s/^\s+//;
4447 }
Joe Perches3705ce52013-07-03 15:05:31 -07004448 $line_fixed = 1;
4449 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004450 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004451 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004452 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004453
4454## print("n: <$n> GOOD: <$good>\n");
4455
4456 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004457 }
Joe Perches3705ce52013-07-03 15:05:31 -07004458
4459 if (($#elements % 2) == 0) {
4460 $fixed_line = $fixed_line . $fix_elements[$#elements];
4461 }
4462
Joe Perches194f66f2014-08-06 16:11:03 -07004463 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4464 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004465 }
4466
4467
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004468 }
4469
Joe Perches786b6322013-07-03 15:05:32 -07004470# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004471 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004472 if (WARN("SPACING",
4473 "space prohibited before semicolon\n" . $herecurr) &&
4474 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004475 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004476 s/^(\+.*\S)\s+;/$1;/;
4477 }
4478 }
4479
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004480# check for multiple assignments
4481 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004482 CHK("MULTIPLE_ASSIGNMENTS",
4483 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004484 }
4485
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004486## # check for multiple declarations, allowing for a function declaration
4487## # continuation.
4488## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4489## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4490##
4491## # Remove any bracketed sections to ensure we do not
4492## # falsly report the parameters of functions.
4493## my $ln = $line;
4494## while ($ln =~ s/\([^\(\)]*\)//g) {
4495## }
4496## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004497## WARN("MULTIPLE_DECLARATION",
4498## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004499## }
4500## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004501
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004502#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004503 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004504 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004505 if (ERROR("SPACING",
4506 "space required before the open brace '{'\n" . $herecurr) &&
4507 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004508 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004509 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004510 }
4511
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004512## # check for blank lines before declarations
4513## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4514## $prevrawline =~ /^.\s*$/) {
4515## WARN("SPACING",
4516## "No blank lines before declarations\n" . $hereprev);
4517## }
4518##
4519
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004520# closing brace should have a space following it when it has anything
4521# on the line
4522 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004523 if (ERROR("SPACING",
4524 "space required after that close brace '}'\n" . $herecurr) &&
4525 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004526 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004527 s/}((?!(?:,|;|\)))\S)/} $1/;
4528 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004529 }
4530
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004531# check spacing on square brackets
4532 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004533 if (ERROR("SPACING",
4534 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4535 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004536 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004537 s/\[\s+/\[/;
4538 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004539 }
4540 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004541 if (ERROR("SPACING",
4542 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4543 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004544 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004545 s/\s+\]/\]/;
4546 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004547 }
4548
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004549# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004550 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4551 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004552 if (ERROR("SPACING",
4553 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4554 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004555 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004556 s/\(\s+/\(/;
4557 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004558 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004559 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004560 $line !~ /for\s*\(.*;\s+\)/ &&
4561 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004562 if (ERROR("SPACING",
4563 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4564 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004565 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004566 s/\s+\)/\)/;
4567 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004568 }
4569
Joe Perchese2826fd2014-08-06 16:10:48 -07004570# check unnecessary parentheses around addressof/dereference single $Lvals
4571# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4572
4573 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004574 my $var = $1;
4575 if (CHK("UNNECESSARY_PARENTHESES",
4576 "Unnecessary parentheses around $var\n" . $herecurr) &&
4577 $fix) {
4578 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4579 }
4580 }
4581
4582# check for unnecessary parentheses around function pointer uses
4583# ie: (foo->bar)(); should be foo->bar();
4584# but not "if (foo->bar) (" to avoid some false positives
4585 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4586 my $var = $2;
4587 if (CHK("UNNECESSARY_PARENTHESES",
4588 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4589 $fix) {
4590 my $var2 = deparenthesize($var);
4591 $var2 =~ s/\s//g;
4592 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4593 }
4594 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004595
Joe Perches63b7c732017-09-08 16:16:01 -07004596# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004597# when !drivers/staging or command-line uses --strict
4598 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004599 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004600 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4601 my $if_stat = $1;
4602 my $test = substr($2, 1, -1);
4603 my $herectx;
4604 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4605 my $match = $1;
4606 # avoid parentheses around potential macro args
4607 next if ($match =~ /^\s*\w+\s*$/);
4608 if (!defined($herectx)) {
4609 $herectx = $here . "\n";
4610 my $cnt = statement_rawlines($if_stat);
4611 for (my $n = 0; $n < $cnt; $n++) {
4612 my $rl = raw_line($linenr, $n);
4613 $herectx .= $rl . "\n";
4614 last if $rl =~ /^[ \+].*\{/;
4615 }
4616 }
4617 CHK("UNNECESSARY_PARENTHESES",
4618 "Unnecessary parentheses around '$match'\n" . $herectx);
4619 }
4620 }
4621
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004622#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004623 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004624 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004625 if (WARN("INDENTED_LABEL",
4626 "labels should not be indented\n" . $herecurr) &&
4627 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004628 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004629 s/^(.)\s+/$1/;
4630 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004631 }
4632
Joe Perches5b9553a2014-04-03 14:49:21 -07004633# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004634 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004635 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004636 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004637 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4638 my $value = $1;
4639 $value = deparenthesize($value);
4640 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4641 ERROR("RETURN_PARENTHESES",
4642 "return is not a function, parentheses are not required\n" . $herecurr);
4643 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004644 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004645 ERROR("SPACING",
4646 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004647 }
4648 }
Joe Perches507e5142013-11-12 15:10:13 -08004649
Joe Perchesb43ae212014-06-23 13:22:07 -07004650# unnecessary return in a void function
4651# at end-of-function, with the previous line a single leading tab, then return;
4652# and the line before that not a goto label target like "out:"
4653 if ($sline =~ /^[ \+]}\s*$/ &&
4654 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4655 $linenr >= 3 &&
4656 $lines[$linenr - 3] =~ /^[ +]/ &&
4657 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004658 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004659 "void function return statements are not generally useful\n" . $hereprev);
4660 }
Joe Perches9819cf22014-06-04 16:12:09 -07004661
Joe Perches189248d2014-01-23 15:54:47 -08004662# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004663 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004664 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4665 my $openparens = $1;
4666 my $count = $openparens =~ tr@\(@\(@;
4667 my $msg = "";
4668 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4669 my $comp = $4; #Not $1 because of $LvalOrFunc
4670 $msg = " - maybe == should be = ?" if ($comp eq "==");
4671 WARN("UNNECESSARY_PARENTHESES",
4672 "Unnecessary parentheses$msg\n" . $herecurr);
4673 }
4674 }
4675
Joe Perchesc5595fa2015-09-09 15:37:58 -07004676# comparisons with a constant or upper case identifier on the left
4677# avoid cases like "foo + BAR < baz"
4678# only fix matches surrounded by parentheses to avoid incorrect
4679# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004680 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004681 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4682 my $lead = $1;
4683 my $const = $2;
4684 my $comp = $3;
4685 my $to = $4;
4686 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004687 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004688 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4689 WARN("CONSTANT_COMPARISON",
4690 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4691 $fix) {
4692 if ($comp eq "<") {
4693 $newcomp = ">";
4694 } elsif ($comp eq "<=") {
4695 $newcomp = ">=";
4696 } elsif ($comp eq ">") {
4697 $newcomp = "<";
4698 } elsif ($comp eq ">=") {
4699 $newcomp = "<=";
4700 }
4701 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4702 }
4703 }
4704
Joe Perchesf34e4a42015-04-16 12:44:19 -07004705# Return of what appears to be an errno should normally be negative
4706 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004707 my $name = $1;
4708 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004709 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004710 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004711 }
4712 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004713
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004714# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004715 if ($line =~ /\b(if|while|for|switch)\(/) {
4716 if (ERROR("SPACING",
4717 "space required before the open parenthesis '('\n" . $herecurr) &&
4718 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004719 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004720 s/\b(if|while|for|switch)\(/$1 \(/;
4721 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004722 }
4723
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004724# Check for illegal assignment in if conditional -- and check for trailing
4725# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004726 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004727 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4728 ctx_statement_block($linenr, $realcnt, 0)
4729 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004730 my ($stat_next) = ctx_statement_block($line_nr_next,
4731 $remain_next, $off_next);
4732 $stat_next =~ s/\n./\n /g;
4733 ##print "stat<$stat> stat_next<$stat_next>\n";
4734
4735 if ($stat_next =~ /^\s*while\b/) {
4736 # If the statement carries leading newlines,
4737 # then count those as offsets.
4738 my ($whitespace) =
4739 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4740 my $offset =
4741 statement_rawlines($whitespace) - 1;
4742
4743 $suppress_whiletrailers{$line_nr_next +
4744 $offset} = 1;
4745 }
4746 }
4747 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004748 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004749 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004750 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004751
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004752 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004753 ERROR("ASSIGN_IN_IF",
4754 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004755 }
4756
4757 # Find out what is on the end of the line after the
4758 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004759 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004760 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004761 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004762 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4763 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004764 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004765 # Find out how long the conditional actually is.
4766 my @newlines = ($c =~ /\n/gs);
4767 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004768 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004769
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004770 $stat_real = raw_line($linenr, $cond_lines)
4771 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004772 if (defined($stat_real) && $cond_lines > 1) {
4773 $stat_real = "[...]\n$stat_real";
4774 }
4775
Joe Perches000d1cc12011-07-25 17:13:25 -07004776 ERROR("TRAILING_STATEMENTS",
4777 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004778 }
4779 }
4780
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004781# Check for bitwise tests written as boolean
4782 if ($line =~ /
4783 (?:
4784 (?:\[|\(|\&\&|\|\|)
4785 \s*0[xX][0-9]+\s*
4786 (?:\&\&|\|\|)
4787 |
4788 (?:\&\&|\|\|)
4789 \s*0[xX][0-9]+\s*
4790 (?:\&\&|\|\||\)|\])
4791 )/x)
4792 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004793 WARN("HEXADECIMAL_BOOLEAN_TEST",
4794 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004795 }
4796
Andy Whitcroft8905a672007-11-28 16:21:06 -08004797# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004798 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4799 my $s = $1;
4800 $s =~ s/$;//g; # Remove any comments
4801 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004802 ERROR("TRAILING_STATEMENTS",
4803 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004804 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004805 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004806# if should not continue a brace
4807 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004808 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004809 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004810 $herecurr);
4811 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004812# case and default should not have general statements after them
4813 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4814 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004815 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004816 \s*return\s+
4817 )/xg)
4818 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004819 ERROR("TRAILING_STATEMENTS",
4820 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004821 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004822
4823 # Check for }<nl>else {, these must be at the same
4824 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004825 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4826 $previndent == $indent) {
4827 if (ERROR("ELSE_AFTER_BRACE",
4828 "else should follow close brace '}'\n" . $hereprev) &&
4829 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4830 fix_delete_line($fixlinenr - 1, $prevrawline);
4831 fix_delete_line($fixlinenr, $rawline);
4832 my $fixedline = $prevrawline;
4833 $fixedline =~ s/}\s*$//;
4834 if ($fixedline !~ /^\+\s*$/) {
4835 fix_insert_line($fixlinenr, $fixedline);
4836 }
4837 $fixedline = $rawline;
4838 $fixedline =~ s/^(.\s*)else/$1} else/;
4839 fix_insert_line($fixlinenr, $fixedline);
4840 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004841 }
4842
Joe Perches8b8856f2014-08-06 16:11:14 -07004843 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4844 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004845 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4846
4847 # Find out what is on the end of the line after the
4848 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004849 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004850 $s =~ s/\n.*//g;
4851
4852 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004853 if (ERROR("WHILE_AFTER_BRACE",
4854 "while should follow close brace '}'\n" . $hereprev) &&
4855 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4856 fix_delete_line($fixlinenr - 1, $prevrawline);
4857 fix_delete_line($fixlinenr, $rawline);
4858 my $fixedline = $prevrawline;
4859 my $trailing = $rawline;
4860 $trailing =~ s/^\+//;
4861 $trailing = trim($trailing);
4862 $fixedline =~ s/}\s*$/} $trailing/;
4863 fix_insert_line($fixlinenr, $fixedline);
4864 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004865 }
4866 }
4867
Joe Perches95e2c602013-07-03 15:05:20 -07004868#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004869 while ($line =~ m{($Constant|$Lval)}g) {
4870 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004871
4872#gcc binary extension
4873 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004874 if (WARN("GCC_BINARY_CONSTANT",
4875 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4876 $fix) {
4877 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004878 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004879 s/\b$var\b/$hexval/;
4880 }
Joe Perches95e2c602013-07-03 15:05:20 -07004881 }
4882
4883#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004884 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004885 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004886#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004887 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004888#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004889 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4890#Ignore some three character SI units explicitly, like MiB and KHz
4891 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004892 while ($var =~ m{($Ident)}g) {
4893 my $word = $1;
4894 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004895 if ($check) {
4896 seed_camelcase_includes();
4897 if (!$file && !$camelcase_file_seeded) {
4898 seed_camelcase_file($realfile);
4899 $camelcase_file_seeded = 1;
4900 }
4901 }
Joe Perches7e781f62013-09-11 14:23:55 -07004902 if (!defined $camelcase{$word}) {
4903 $camelcase{$word} = 1;
4904 CHK("CAMELCASE",
4905 "Avoid CamelCase: <$word>\n" . $herecurr);
4906 }
Joe Perches34456862013-07-03 15:05:34 -07004907 }
Joe Perches323c1262012-12-17 16:02:07 -08004908 }
4909 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004910
4911#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004912 if ($line =~ /\#\s*define.*\\\s+$/) {
4913 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4914 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4915 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004916 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004917 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004918 }
4919
Fabian Frederick0e212e02015-04-16 12:44:25 -07004920# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4921# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004922 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004923 my $file = "$1.h";
4924 my $checkfile = "include/linux/$file";
4925 if (-f "$root/$checkfile" &&
4926 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004927 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004928 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004929 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4930 if ($asminclude > 0) {
4931 if ($realfile =~ m{^arch/}) {
4932 CHK("ARCH_INCLUDE_LINUX",
4933 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4934 } else {
4935 WARN("INCLUDE_LINUX",
4936 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4937 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004938 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004939 }
4940 }
4941
Andy Whitcroft653d4872007-06-23 17:16:34 -07004942# multi-statement macros should be enclosed in a do while loop, grab the
4943# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004944# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004945 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4946 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004947 my $ln = $linenr;
4948 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004949 my ($off, $dstat, $dcond, $rest);
4950 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004951 my $has_flow_statement = 0;
4952 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004953 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004954 ctx_statement_block($linenr, $realcnt, 0);
4955 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004956 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004957 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004958
Joe Perches08a28432014-10-13 15:51:55 -07004959 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004960 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004961
Joe Perchesf59b64b2016-10-11 13:52:08 -07004962 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4963 my $define_args = $1;
4964 my $define_stmt = $dstat;
4965 my @def_args = ();
4966
4967 if (defined $define_args && $define_args ne "") {
4968 $define_args = substr($define_args, 1, length($define_args) - 2);
4969 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07004970 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004971 @def_args = split(",", $define_args);
4972 }
4973
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004974 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004975 $dstat =~ s/\\\n.//g;
4976 $dstat =~ s/^\s*//s;
4977 $dstat =~ s/\s*$//s;
4978
4979 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004980 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4981 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004982 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004983 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004984 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004985
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004986 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004987 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4988 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004989 {
4990 }
4991
Joe Perches42e15292016-03-15 14:58:01 -07004992 # Make asm volatile uses seem like a generic function
4993 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4994
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004995 my $exceptions = qr{
4996 $Declare|
4997 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004998 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004999 DECLARE_PER_CPU|
5000 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005001 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005002 union|
5003 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005004 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005005 ^\"|\"$|
5006 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005007 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005008 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005009
5010 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005011 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005012 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005013
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005014 if ($dstat ne '' &&
5015 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5016 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005017 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005018 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005019 $dstat !~ /$exceptions/ &&
5020 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005021 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005022 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005023 $dstat !~ /^for\s*$Constant$/ && # for (...)
5024 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5025 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005026 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005027 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005028 {
Joe Perchese7955562017-05-08 15:55:48 -07005029 if ($dstat =~ /^\s*if\b/) {
5030 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5031 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5032 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005033 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5034 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5035 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005036 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005037 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005038 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005039
5040 }
Joe Perches52076492016-10-11 13:52:14 -07005041
5042 # Make $define_stmt single line, comment-free, etc
5043 my @stmt_array = split('\n', $define_stmt);
5044 my $first = 1;
5045 $define_stmt = "";
5046 foreach my $l (@stmt_array) {
5047 $l =~ s/\\$//;
5048 if ($first) {
5049 $define_stmt = $l;
5050 $first = 0;
5051 } elsif ($l =~ /^[\+ ]/) {
5052 $define_stmt .= substr($l, 1);
5053 }
5054 }
5055 $define_stmt =~ s/$;//g;
5056 $define_stmt =~ s/\s+/ /g;
5057 $define_stmt = trim($define_stmt);
5058
Joe Perchesf59b64b2016-10-11 13:52:08 -07005059# check if any macro arguments are reused (ignore '...' and 'type')
5060 foreach my $arg (@def_args) {
5061 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005062 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005063 my $tmp_stmt = $define_stmt;
5064 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5065 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5066 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005067 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005068 if ($use_cnt > 1) {
5069 CHK("MACRO_ARG_REUSE",
5070 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005071 }
5072# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005073 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005074 ((defined($1) && $1 ne ',') ||
5075 (defined($2) && $2 ne ','))) {
5076 CHK("MACRO_ARG_PRECEDENCE",
5077 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005078 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005079 }
Joe Perches5023d342012-12-17 16:01:47 -08005080
Joe Perches08a28432014-10-13 15:51:55 -07005081# check for macros with flow control, but without ## concatenation
5082# ## concatenation is commonly a macro that defines a function so ignore those
5083 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005084 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005085 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005086
Joe Perches08a28432014-10-13 15:51:55 -07005087 WARN("MACRO_WITH_FLOW_CONTROL",
5088 "Macros with flow control statements should be avoided\n" . "$herectx");
5089 }
5090
Joe Perches481eb482012-12-17 16:01:56 -08005091# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005092
5093 } else {
5094 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005095 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5096 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005097 $line =~ /^\+.*\\$/) {
5098 WARN("LINE_CONTINUATIONS",
5099 "Avoid unnecessary line continuations\n" . $herecurr);
5100 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005101 }
5102
Joe Perchesb13edf72012-07-30 14:41:24 -07005103# do {} while (0) macro tests:
5104# single-statement macros do not need to be enclosed in do while (0) loop,
5105# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005106 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005107 $realfile !~ m@/vmlinux.lds.h$@ &&
5108 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5109 my $ln = $linenr;
5110 my $cnt = $realcnt;
5111 my ($off, $dstat, $dcond, $rest);
5112 my $ctx = '';
5113 ($dstat, $dcond, $ln, $cnt, $off) =
5114 ctx_statement_block($linenr, $realcnt, 0);
5115 $ctx = $dstat;
5116
5117 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005118 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005119
5120 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5121 my $stmts = $2;
5122 my $semis = $3;
5123
5124 $ctx =~ s/\n*$//;
5125 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005126 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005127
Joe Perchesac8e97f2012-08-21 16:15:53 -07005128 if (($stmts =~ tr/;/;/) == 1 &&
5129 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005130 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5131 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5132 }
5133 if (defined $semis && $semis ne "") {
5134 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5135 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5136 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005137 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5138 $ctx =~ s/\n*$//;
5139 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005140 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005141
5142 WARN("TRAILING_SEMICOLON",
5143 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005144 }
5145 }
5146
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005147# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005148 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5149 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005150 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005151 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005152 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5153 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005154 my @allowed = ();
5155 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005156 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005157 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005158 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005159 for my $chunk (@chunks) {
5160 my ($cond, $block) = @{$chunk};
5161
Andy Whitcroft773647a2008-03-28 14:15:58 -07005162 # If the condition carries leading newlines, then count those as offsets.
5163 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5164 my $offset = statement_rawlines($whitespace) - 1;
5165
Joe Perchesaad4f612012-03-23 15:02:19 -07005166 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005167 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5168
5169 # We have looked at and allowed this specific line.
5170 $suppress_ifbraces{$ln + $offset} = 1;
5171
5172 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005173 $ln += statement_rawlines($block) - 1;
5174
Andy Whitcroft773647a2008-03-28 14:15:58 -07005175 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005176
5177 $seen++ if ($block =~ /^\s*{/);
5178
Joe Perchesaad4f612012-03-23 15:02:19 -07005179 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005180 if (statement_lines($cond) > 1) {
5181 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005182 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005183 }
5184 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005185 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005186 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005187 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005188 if (statement_block_size($block) > 1) {
5189 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005190 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005191 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005192 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005193 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005194 if ($seen) {
5195 my $sum_allowed = 0;
5196 foreach (@allowed) {
5197 $sum_allowed += $_;
5198 }
5199 if ($sum_allowed == 0) {
5200 WARN("BRACES",
5201 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5202 } elsif ($sum_allowed != $allow &&
5203 $seen != $allow) {
5204 CHK("BRACES",
5205 "braces {} should be used on all arms of this statement\n" . $herectx);
5206 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005207 }
5208 }
5209 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005210 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005211 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005212 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005213
Andy Whitcroftcf655042008-03-04 14:28:20 -08005214 # Check the pre-context.
5215 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5216 #print "APW: ALLOWED: pre<$1>\n";
5217 $allowed = 1;
5218 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005219
5220 my ($level, $endln, @chunks) =
5221 ctx_statement_full($linenr, $realcnt, $-[0]);
5222
Andy Whitcroftcf655042008-03-04 14:28:20 -08005223 # Check the condition.
5224 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005225 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005226 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005227 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005228 }
5229 if (statement_lines($cond) > 1) {
5230 #print "APW: ALLOWED: cond<$cond>\n";
5231 $allowed = 1;
5232 }
5233 if ($block =~/\b(?:if|for|while)\b/) {
5234 #print "APW: ALLOWED: block<$block>\n";
5235 $allowed = 1;
5236 }
5237 if (statement_block_size($block) > 1) {
5238 #print "APW: ALLOWED: lines block<$block>\n";
5239 $allowed = 1;
5240 }
5241 # Check the post-context.
5242 if (defined $chunks[1]) {
5243 my ($cond, $block) = @{$chunks[1]};
5244 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005245 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005246 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005247 if ($block =~ /^\s*\{/) {
5248 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5249 $allowed = 1;
5250 }
5251 }
5252 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005253 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005254 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005255
Joe Perches000d1cc12011-07-25 17:13:25 -07005256 WARN("BRACES",
5257 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005258 }
5259 }
5260
Joe Perchese4c5bab2017-02-24 15:01:41 -08005261# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005262 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5263 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005264 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5265 }
5266
Joe Perches0979ae62012-12-17 16:01:59 -08005267# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005268 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005269 if (CHK("BRACES",
5270 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5271 $fix && $prevrawline =~ /^\+/) {
5272 fix_delete_line($fixlinenr - 1, $prevrawline);
5273 }
Joe Perches0979ae62012-12-17 16:01:59 -08005274 }
Joe Perches77b9a532013-07-03 15:05:29 -07005275 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005276 if (CHK("BRACES",
5277 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5278 $fix) {
5279 fix_delete_line($fixlinenr, $rawline);
5280 }
Joe Perches0979ae62012-12-17 16:01:59 -08005281 }
5282
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005283# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005284 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5285 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005286 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005287 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005288 }
5289
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005290# Check for user-visible strings broken across lines, which breaks the ability
5291# to grep for the string. Make exceptions when the previous string ends in a
5292# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5293# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005294 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005295 $prevline =~ /"\s*$/ &&
5296 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5297 if (WARN("SPLIT_STRING",
5298 "quoted string split across lines\n" . $hereprev) &&
5299 $fix &&
5300 $prevrawline =~ /^\+.*"\s*$/ &&
5301 $last_coalesced_string_linenr != $linenr - 1) {
5302 my $extracted_string = get_quoted_string($line, $rawline);
5303 my $comma_close = "";
5304 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5305 $comma_close = $1;
5306 }
5307
5308 fix_delete_line($fixlinenr - 1, $prevrawline);
5309 fix_delete_line($fixlinenr, $rawline);
5310 my $fixedline = $prevrawline;
5311 $fixedline =~ s/"\s*$//;
5312 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5313 fix_insert_line($fixlinenr - 1, $fixedline);
5314 $fixedline = $rawline;
5315 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5316 if ($fixedline !~ /\+\s*$/) {
5317 fix_insert_line($fixlinenr, $fixedline);
5318 }
5319 $last_coalesced_string_linenr = $linenr;
5320 }
5321 }
5322
5323# check for missing a space in a string concatenation
5324 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5325 WARN('MISSING_SPACE',
5326 "break quoted strings at a space character\n" . $hereprev);
5327 }
5328
Joe Perchese4b7d302017-05-08 15:55:51 -07005329# check for an embedded function name in a string when the function is known
5330# This does not work very well for -f --file checking as it depends on patch
5331# context providing the function name or a single line form for in-file
5332# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005333 if ($line =~ /^\+.*$String/ &&
5334 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005335 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5336 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005337 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005338 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005339 }
5340
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005341# check for spaces before a quoted newline
5342 if ($rawline =~ /^.*\".*\s\\n/) {
5343 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5344 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5345 $fix) {
5346 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5347 }
5348
5349 }
5350
Joe Perchesf17dba42014-10-13 15:51:51 -07005351# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005352 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5353 if (CHK("CONCATENATED_STRING",
5354 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5355 $fix) {
5356 while ($line =~ /($String)/g) {
5357 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5358 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5359 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5360 }
5361 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005362 }
5363
Joe Perches90ad30e2014-12-10 15:51:59 -08005364# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005365 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005366 if (WARN("STRING_FRAGMENTS",
5367 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5368 $fix) {
5369 while ($line =~ /($String)(?=\s*")/g) {
5370 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5371 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5372 }
5373 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005374 }
5375
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005376# check for non-standard and hex prefixed decimal printf formats
5377 my $show_L = 1; #don't show the same defect twice
5378 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005379 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005380 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005381 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005382 # check for %L
5383 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005384 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005385 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5386 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005387 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005388 # check for %Z
5389 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5390 WARN("PRINTF_Z",
5391 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5392 $show_Z = 0;
5393 }
5394 # check for 0x<decimal>
5395 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5396 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005397 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5398 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005399 }
5400
5401# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005402 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005403 WARN("LINE_CONTINUATIONS",
5404 "Avoid line continuations in quoted strings\n" . $herecurr);
5405 }
5406
Andy Whitcroft00df3442007-06-08 13:47:06 -07005407# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005408 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005409 CHK("REDUNDANT_CODE",
5410 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005411 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005412 }
5413
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005414# check for needless "if (<foo>) fn(<foo>)" uses
5415 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005416 my $tested = quotemeta($1);
5417 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5418 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5419 my $func = $1;
5420 if (WARN('NEEDLESS_IF',
5421 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5422 $fix) {
5423 my $do_fix = 1;
5424 my $leading_tabs = "";
5425 my $new_leading_tabs = "";
5426 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5427 $leading_tabs = $1;
5428 } else {
5429 $do_fix = 0;
5430 }
5431 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5432 $new_leading_tabs = $1;
5433 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5434 $do_fix = 0;
5435 }
5436 } else {
5437 $do_fix = 0;
5438 }
5439 if ($do_fix) {
5440 fix_delete_line($fixlinenr - 1, $prevrawline);
5441 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5442 }
5443 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005444 }
5445 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005446
Joe Perchesebfdc402014-08-06 16:10:27 -07005447# check for unnecessary "Out of Memory" messages
5448 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5449 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5450 (defined $1 || defined $3) &&
5451 $linenr > 3) {
5452 my $testval = $2;
5453 my $testline = $lines[$linenr - 3];
5454
5455 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5456# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5457
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005458 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005459 WARN("OOM_MESSAGE",
5460 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5461 }
5462 }
5463
Joe Perchesf78d98f2014-10-13 15:52:01 -07005464# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005465 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005466 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5467 my $level = $1;
5468 if (WARN("UNNECESSARY_KERN_LEVEL",
5469 "Possible unnecessary $level\n" . $herecurr) &&
5470 $fix) {
5471 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5472 }
5473 }
5474
Joe Perches45c55e92017-02-24 15:01:31 -08005475# check for logging continuations
5476 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5477 WARN("LOGGING_CONTINUATION",
5478 "Avoid logging continuation uses where feasible\n" . $herecurr);
5479 }
5480
Joe Perchesabb08a52014-12-10 15:51:46 -08005481# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005482 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005483 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5484 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5485 WARN("MASK_THEN_SHIFT",
5486 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5487 }
5488
Joe Perchesb75ac612014-12-10 15:52:02 -08005489# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005490 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005491 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5492 my $val = $1;
5493 my $equal = "!";
5494 $equal = "" if ($4 eq "!=");
5495 if (CHK("COMPARISON_TO_NULL",
5496 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5497 $fix) {
5498 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5499 }
5500 }
5501 }
5502
Joe Perches8716de32013-09-11 14:24:05 -07005503# check for bad placement of section $InitAttribute (e.g.: __initdata)
5504 if ($line =~ /(\b$InitAttribute\b)/) {
5505 my $attr = $1;
5506 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5507 my $ptr = $1;
5508 my $var = $2;
5509 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5510 ERROR("MISPLACED_INIT",
5511 "$attr should be placed after $var\n" . $herecurr)) ||
5512 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5513 WARN("MISPLACED_INIT",
5514 "$attr should be placed after $var\n" . $herecurr))) &&
5515 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005516 $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 -07005517 }
5518 }
5519 }
5520
Joe Perchese970b8842013-11-12 15:10:10 -08005521# check for $InitAttributeData (ie: __initdata) with const
5522 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5523 my $attr = $1;
5524 $attr =~ /($InitAttributePrefix)(.*)/;
5525 my $attr_prefix = $1;
5526 my $attr_type = $2;
5527 if (ERROR("INIT_ATTRIBUTE",
5528 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5529 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005530 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005531 s/$InitAttributeData/${attr_prefix}initconst/;
5532 }
5533 }
5534
5535# check for $InitAttributeConst (ie: __initconst) without const
5536 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5537 my $attr = $1;
5538 if (ERROR("INIT_ATTRIBUTE",
5539 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005541 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005542 /(^\+\s*(?:static\s+))/;
5543 $lead = rtrim($1);
5544 $lead = "$lead " if ($lead !~ /^\+$/);
5545 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005546 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005547 }
5548 }
5549
Joe Perchesc17893c2015-04-16 12:44:42 -07005550# check for __read_mostly with const non-pointer (should just be const)
5551 if ($line =~ /\b__read_mostly\b/ &&
5552 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5553 if (ERROR("CONST_READ_MOSTLY",
5554 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5555 $fix) {
5556 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5557 }
5558 }
5559
Joe Perchesfbdb8132014-04-03 14:49:14 -07005560# don't use __constant_<foo> functions outside of include/uapi/
5561 if ($realfile !~ m@^include/uapi/@ &&
5562 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5563 my $constant_func = $1;
5564 my $func = $constant_func;
5565 $func =~ s/^__constant_//;
5566 if (WARN("CONSTANT_CONVERSION",
5567 "$constant_func should be $func\n" . $herecurr) &&
5568 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005569 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005570 }
5571 }
5572
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005573# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005574 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005575 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005576 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005577 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005578 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005579 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5580 }
5581 if ($delay > 2000) {
5582 WARN("LONG_UDELAY",
5583 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005584 }
5585 }
5586
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005587# warn about unexpectedly long msleep's
5588 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5589 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005590 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005591 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005592 }
5593 }
5594
Joe Perches36ec1932013-07-03 15:05:25 -07005595# check for comparisons of jiffies
5596 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5597 WARN("JIFFIES_COMPARISON",
5598 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5599 }
5600
Joe Perches9d7a34a2013-07-03 15:05:26 -07005601# check for comparisons of get_jiffies_64()
5602 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5603 WARN("JIFFIES_COMPARISON",
5604 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5605 }
5606
Andy Whitcroft00df3442007-06-08 13:47:06 -07005607# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005608# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005609# print "#ifdef in C files should be avoided\n";
5610# print "$herecurr";
5611# $clean = 0;
5612# }
5613
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005614# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005615 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005616 if (ERROR("SPACING",
5617 "exactly one space required after that #$1\n" . $herecurr) &&
5618 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005619 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005620 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5621 }
5622
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005623 }
5624
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005625# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005626 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5627 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005628 my $which = $1;
5629 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005630 CHK("UNCOMMENTED_DEFINITION",
5631 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005632 }
5633 }
5634# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005635
5636 my $barriers = qr{
5637 mb|
5638 rmb|
5639 wmb|
5640 read_barrier_depends
5641 }x;
5642 my $barrier_stems = qr{
5643 mb__before_atomic|
5644 mb__after_atomic|
5645 store_release|
5646 load_acquire|
5647 store_mb|
5648 (?:$barriers)
5649 }x;
5650 my $all_barriers = qr{
5651 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005652 smp_(?:$barrier_stems)|
5653 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005654 }x;
5655
5656 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005657 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005658 WARN("MEMORY_BARRIER",
5659 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005660 }
5661 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005662
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005663 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5664
5665 if ($realfile !~ m@^include/asm-generic/@ &&
5666 $realfile !~ m@/barrier\.h$@ &&
5667 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5668 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5669 WARN("MEMORY_BARRIER",
5670 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5671 }
5672
Joe Perchescb426e92015-06-25 15:02:46 -07005673# check for waitqueue_active without a comment.
5674 if ($line =~ /\bwaitqueue_active\s*\(/) {
5675 if (!ctx_has_comment($first_line, $linenr)) {
5676 WARN("WAITQUEUE_ACTIVE",
5677 "waitqueue_active without comment\n" . $herecurr);
5678 }
5679 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005680
Paul E. McKenney91db2592017-11-27 09:37:35 -08005681# check for smp_read_barrier_depends and read_barrier_depends
5682 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5683 WARN("READ_BARRIER_DEPENDS",
5684 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5685 }
5686
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005687# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005688 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005689 CHK("ARCH_DEFINES",
5690 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005691 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005692
Joe Perches596ed452017-07-12 14:37:02 -07005693# check that the storage class is not after a type
5694 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005695 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005696 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5697 }
5698# Check that the storage class is at the beginning of a declaration
5699 if ($line =~ /\b$Storage\b/ &&
5700 $line !~ /^.\s*$Storage/ &&
5701 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5702 $1 !~ /[\,\)]\s*$/) {
5703 WARN("STORAGE_CLASS",
5704 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005705 }
5706
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005707# check the location of the inline attribute, that it is between
5708# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005709 if ($line =~ /\b$Type\s+$Inline\b/ ||
5710 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005711 ERROR("INLINE_LOCATION",
5712 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005713 }
5714
Andy Whitcroft8905a672007-11-28 16:21:06 -08005715# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005716 if ($realfile !~ m@\binclude/uapi/@ &&
5717 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005718 if (WARN("INLINE",
5719 "plain inline is preferred over $1\n" . $herecurr) &&
5720 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005721 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005722
5723 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005724 }
5725
Joe Perches3d130fd2011-01-12 17:00:00 -08005726# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005727 if ($realfile !~ m@\binclude/uapi/@ &&
5728 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005729 WARN("PREFER_PACKED",
5730 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005731 }
5732
Joe Perches39b7e282011-07-25 17:13:24 -07005733# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005734 if ($realfile !~ m@\binclude/uapi/@ &&
5735 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005736 WARN("PREFER_ALIGNED",
5737 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005738 }
5739
Joe Perches5f14d3b2012-01-10 15:09:52 -08005740# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005741 if ($realfile !~ m@\binclude/uapi/@ &&
5742 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005743 if (WARN("PREFER_PRINTF",
5744 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5745 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005746 $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 -07005747
5748 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005749 }
5750
Joe Perches6061d942012-03-23 15:02:16 -07005751# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005752 if ($realfile !~ m@\binclude/uapi/@ &&
5753 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005754 if (WARN("PREFER_SCANF",
5755 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005757 $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 -07005758 }
Joe Perches6061d942012-03-23 15:02:16 -07005759 }
5760
Joe Perches619a9082014-12-10 15:51:35 -08005761# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005762 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005763 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5764 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5765 $line =~ /\b__weak\b/)) {
5766 ERROR("WEAK_DECLARATION",
5767 "Using weak declarations can have unintended link defects\n" . $herecurr);
5768 }
5769
Tomas Winklerfd39f902016-12-12 16:46:34 -08005770# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005771 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005772 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005773 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5774 my $type = $1;
5775 if ($type =~ /\b($typeC99Typedefs)\b/) {
5776 $type = $1;
5777 my $kernel_type = 'u';
5778 $kernel_type = 's' if ($type =~ /^_*[si]/);
5779 $type =~ /(\d+)/;
5780 $kernel_type .= $1;
5781 if (CHK("PREFER_KERNEL_TYPES",
5782 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5783 $fix) {
5784 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5785 }
5786 }
5787 }
5788
Joe Perches938224b2016-01-20 14:59:15 -08005789# check for cast of C90 native int or longer types constants
5790 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5791 my $cast = $1;
5792 my $const = $2;
5793 if (WARN("TYPECAST_INT_CONSTANT",
5794 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5795 $fix) {
5796 my $suffix = "";
5797 my $newconst = $const;
5798 $newconst =~ s/${Int_type}$//;
5799 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5800 if ($cast =~ /\blong\s+long\b/) {
5801 $suffix .= 'LL';
5802 } elsif ($cast =~ /\blong\b/) {
5803 $suffix .= 'L';
5804 }
5805 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5806 }
5807 }
5808
Joe Perches8f53a9b2010-03-05 13:43:48 -08005809# check for sizeof(&)
5810 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005811 WARN("SIZEOF_ADDRESS",
5812 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005813 }
5814
Joe Perches66c80b62012-07-30 14:41:22 -07005815# check for sizeof without parenthesis
5816 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005817 if (WARN("SIZEOF_PARENTHESIS",
5818 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5819 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005820 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005821 }
Joe Perches66c80b62012-07-30 14:41:22 -07005822 }
5823
Joe Perches88982fe2012-12-17 16:02:00 -08005824# check for struct spinlock declarations
5825 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5826 WARN("USE_SPINLOCK_T",
5827 "struct spinlock should be spinlock_t\n" . $herecurr);
5828 }
5829
Joe Perchesa6962d72013-04-29 16:18:13 -07005830# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005831 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005832 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005833 $fmt =~ s/%%//g;
5834 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005835 if (WARN("PREFER_SEQ_PUTS",
5836 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5837 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005838 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005839 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005840 }
5841 }
5842
Joe Perches478b1792018-04-10 16:33:34 -07005843# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07005844 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07005845 defined $stat &&
5846 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5847 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005848 my $stat_real;
5849
Joe Perches0b523762017-05-08 15:55:36 -07005850 my $lc = $stat =~ tr@\n@@;
5851 $lc = $lc + $linenr;
5852 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07005853 my $specifier;
5854 my $extension;
5855 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07005856 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5857 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005858
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07005859 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5860 $specifier = $1;
5861 $extension = $2;
5862 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
5863 $bad_specifier = $specifier;
5864 last;
5865 }
5866 if ($extension eq "x" && !defined($stat_real)) {
5867 if (!defined($stat_real)) {
5868 $stat_real = get_stat_real($linenr, $lc);
5869 }
5870 WARN("VSPRINTF_SPECIFIER_PX",
5871 "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
5872 }
5873 }
5874 if ($bad_specifier ne "") {
5875 my $stat_real = get_stat_real($linenr, $lc);
5876 my $ext_type = "Invalid";
5877 my $use = "";
5878 if ($bad_specifier =~ /p[Ff]/) {
5879 $ext_type = "Deprecated";
5880 $use = " - use %pS instead";
5881 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5882 }
5883
5884 WARN("VSPRINTF_POINTER_EXTENSION",
5885 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
5886 }
Joe Perches0b523762017-05-08 15:55:36 -07005887 }
5888 }
5889
Andy Whitcroft554e1652012-01-10 15:09:57 -08005890# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07005891 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005892 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005893 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005894
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005895 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005896 my $ms_val = $7;
5897 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005898
Andy Whitcroft554e1652012-01-10 15:09:57 -08005899 if ($ms_size =~ /^(0x|)0$/i) {
5900 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005901 "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 -08005902 } elsif ($ms_size =~ /^(0x|)1$/i) {
5903 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005904 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5905 }
5906 }
5907
Joe Perches98a9bba2014-01-23 15:54:52 -08005908# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005909# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005910# defined $stat &&
5911# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5912# if (WARN("PREFER_ETHER_ADDR_COPY",
5913# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5914# $fix) {
5915# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5916# }
5917# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005918
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005919# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07005920# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005921# defined $stat &&
5922# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5923# WARN("PREFER_ETHER_ADDR_EQUAL",
5924# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5925# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005926
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005927# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5928# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07005929# if ($perl_version_ok &&
Joe Perchesf3331952016-10-11 13:51:53 -07005930# defined $stat &&
5931# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5932#
5933# my $ms_val = $7;
5934#
5935# if ($ms_val =~ /^(?:0x|)0+$/i) {
5936# if (WARN("PREFER_ETH_ZERO_ADDR",
5937# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5938# $fix) {
5939# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5940# }
5941# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5942# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5943# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5944# $fix) {
5945# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5946# }
5947# }
5948# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005949
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005950# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07005951 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005952 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005953 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005954 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005955 my $call = $1;
5956 my $cast1 = deparenthesize($2);
5957 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005958 my $cast2 = deparenthesize($7);
5959 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005960 my $cast;
5961
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005962 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005963 $cast = "$cast1 or $cast2";
5964 } elsif ($cast1 ne "") {
5965 $cast = $cast1;
5966 } else {
5967 $cast = $cast2;
5968 }
5969 WARN("MINMAX",
5970 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005971 }
5972 }
5973
Joe Perches4a273192012-07-30 14:41:20 -07005974# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07005975 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07005976 defined $stat &&
5977 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5978 my $min = $1;
5979 my $max = $7;
5980 if ($min eq $max) {
5981 WARN("USLEEP_RANGE",
5982 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5983 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5984 $min > $max) {
5985 WARN("USLEEP_RANGE",
5986 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5987 }
5988 }
5989
Joe Perches823b7942013-11-12 15:10:15 -08005990# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07005991 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08005992 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005993 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005994 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5995 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5996 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5997 my $lc = $stat =~ tr@\n@@;
5998 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07005999 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006000 WARN("NAKED_SSCANF",
6001 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6002 }
6003
Joe Perchesafc819a2014-06-04 16:12:08 -07006004# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006005 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006006 defined $stat &&
6007 $line =~ /\bsscanf\b/) {
6008 my $lc = $stat =~ tr@\n@@;
6009 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006010 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006011 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6012 my $format = $6;
6013 my $count = $format =~ tr@%@%@;
6014 if ($count == 1 &&
6015 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6016 WARN("SSCANF_TO_KSTRTO",
6017 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6018 }
6019 }
6020 }
6021
Joe Perches70dc8a42013-09-11 14:23:58 -07006022# check for new externs in .h files.
6023 if ($realfile =~ /\.h$/ &&
6024 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006025 if (CHK("AVOID_EXTERNS",
6026 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006027 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006028 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006029 }
6030 }
6031
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006032# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006033 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006034 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006035 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006036 my $function_name = $1;
6037 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006038
6039 my $s = $stat;
6040 if (defined $cond) {
6041 substr($s, 0, length($cond), '');
6042 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006043 if ($s =~ /^\s*;/ &&
6044 $function_name ne 'uninitialized_var')
6045 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006046 WARN("AVOID_EXTERNS",
6047 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006048 }
6049
6050 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006051 WARN("FUNCTION_ARGUMENTS",
6052 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006053 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006054
6055 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6056 $stat =~ /^.\s*extern\s+/)
6057 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006058 WARN("AVOID_EXTERNS",
6059 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006060 }
6061
Joe Perchesa0ad7592017-07-10 15:52:19 -07006062# check for function declarations that have arguments without identifier names
6063 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006064 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006065 $1 ne "void") {
6066 my $args = trim($1);
6067 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6068 my $arg = trim($1);
6069 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6070 WARN("FUNCTION_ARGUMENTS",
6071 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6072 }
6073 }
6074 }
6075
Joe Perchesa0ad7592017-07-10 15:52:19 -07006076# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006077 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006078 defined $stat &&
6079 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6080 $context_function = $1;
6081
6082# check for multiline function definition with misplaced open brace
6083 my $ok = 0;
6084 my $cnt = statement_rawlines($stat);
6085 my $herectx = $here . "\n";
6086 for (my $n = 0; $n < $cnt; $n++) {
6087 my $rl = raw_line($linenr, $n);
6088 $herectx .= $rl . "\n";
6089 $ok = 1 if ($rl =~ /^[ \+]\{/);
6090 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6091 last if $rl =~ /^[ \+].*\{/;
6092 }
6093 if (!$ok) {
6094 ERROR("OPEN_BRACE",
6095 "open brace '{' following function definitions go on the next line\n" . $herectx);
6096 }
6097 }
6098
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006099# checks for new __setup's
6100 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6101 my $name = $1;
6102
6103 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006104 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006105 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006106 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006107 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006108
6109# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006110 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006111 WARN("UNNECESSARY_CASTS",
6112 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006113 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006114
Joe Perchesa640d252013-07-03 15:05:21 -07006115# alloc style
6116# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006117 if ($perl_version_ok &&
Joe Perchesa640d252013-07-03 15:05:21 -07006118 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6119 CHK("ALLOC_SIZEOF_STRUCT",
6120 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6121 }
6122
Joe Perches60a55362014-06-04 16:12:07 -07006123# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006124 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006125 defined $stat &&
6126 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07006127 my $oldfunc = $3;
6128 my $a1 = $4;
6129 my $a2 = $10;
6130 my $newfunc = "kmalloc_array";
6131 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006132 my $r1 = $a1;
6133 my $r2 = $a2;
6134 if ($a1 =~ /^sizeof\s*\S/) {
6135 $r1 = $a2;
6136 $r2 = $a1;
6137 }
6138 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6139 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006140 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006141 my $herectx = get_stat_here($linenr, $cnt, $here);
6142
Joe Perches60a55362014-06-04 16:12:07 -07006143 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006144 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6145 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006146 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006147 $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 -07006148 }
6149 }
6150 }
6151
Joe Perches972fdea2013-04-29 16:18:12 -07006152# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006153 if ($perl_version_ok &&
Joe Perches972fdea2013-04-29 16:18:12 -07006154 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6155 WARN("KREALLOC_ARG_REUSE",
6156 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6157 }
6158
Joe Perches5ce59ae2013-02-21 16:44:18 -08006159# check for alloc argument mismatch
6160 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6161 WARN("ALLOC_ARRAY_ARGS",
6162 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6163 }
6164
Joe Perchescaf2a542011-01-12 16:59:56 -08006165# check for multiple semicolons
6166 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006167 if (WARN("ONE_SEMICOLON",
6168 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6169 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006170 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006171 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006172 }
6173
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006174# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6175 if ($realfile !~ m@^include/uapi/@ &&
6176 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006177 my $ull = "";
6178 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6179 if (CHK("BIT_MACRO",
6180 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6181 $fix) {
6182 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6183 }
6184 }
6185
Joe Perches2d632742016-05-20 17:04:00 -07006186# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6187 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6188 my $config = $1;
6189 if (WARN("PREFER_IS_ENABLED",
6190 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6191 $fix) {
6192 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6193 }
6194 }
6195
Joe Perchese81f2392014-08-06 16:11:25 -07006196# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006197 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6198 my $has_break = 0;
6199 my $has_statement = 0;
6200 my $count = 0;
6201 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006202 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006203 $prevline--;
6204 my $rline = $rawlines[$prevline - 1];
6205 my $fline = $lines[$prevline - 1];
6206 last if ($fline =~ /^\@\@/);
6207 next if ($fline =~ /^\-/);
6208 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6209 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6210 next if ($fline =~ /^.[\s$;]*$/);
6211 $has_statement = 1;
6212 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006213 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006214 }
6215 if (!$has_break && $has_statement) {
6216 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006217 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006218 }
6219 }
6220
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006221# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006222 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006223 defined $stat &&
6224 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006225 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006226 my $herectx = get_stat_here($linenr, $cnt, $here);
6227
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006228 WARN("DEFAULT_NO_BREAK",
6229 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006230 }
6231
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006232# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006233 if ($line =~ /\b__FUNCTION__\b/) {
6234 if (WARN("USE_FUNC",
6235 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6236 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006237 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006238 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006239 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006240
Joe Perches62ec8182015-02-13 14:38:18 -08006241# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6242 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6243 ERROR("DATE_TIME",
6244 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6245 }
6246
Joe Perches2c924882012-03-23 15:02:20 -07006247# check for use of yield()
6248 if ($line =~ /\byield\s*\(\s*\)/) {
6249 WARN("YIELD",
6250 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6251 }
6252
Joe Perches179f8f42013-07-03 15:05:30 -07006253# check for comparisons against true and false
6254 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6255 my $lead = $1;
6256 my $arg = $2;
6257 my $test = $3;
6258 my $otype = $4;
6259 my $trail = $5;
6260 my $op = "!";
6261
6262 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6263
6264 my $type = lc($otype);
6265 if ($type =~ /^(?:true|false)$/) {
6266 if (("$test" eq "==" && "$type" eq "true") ||
6267 ("$test" eq "!=" && "$type" eq "false")) {
6268 $op = "";
6269 }
6270
6271 CHK("BOOL_COMPARISON",
6272 "Using comparison to $otype is error prone\n" . $herecurr);
6273
6274## maybe suggesting a correct construct would better
6275## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6276
6277 }
6278 }
6279
Joe Perches5d430902018-04-10 16:34:25 -07006280# check for bool bitfields
6281 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6282 WARN("BOOL_BITFIELD",
6283 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6284 }
6285
Joe Perchesd7295932018-08-21 21:57:26 -07006286# check for bool use in .h files
6287 if ($realfile =~ /\.h$/ &&
6288 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6289 CHK("BOOL_MEMBER",
6290 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6291 }
6292
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006293# check for semaphores initialized locked
6294 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006295 WARN("CONSIDER_COMPLETION",
6296 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006297 }
Joe Perches6712d852012-03-23 15:02:20 -07006298
Joe Perches67d0a072011-10-31 17:13:10 -07006299# recommend kstrto* over simple_strto* and strict_strto*
6300 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006301 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006302 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006303 }
Joe Perches6712d852012-03-23 15:02:20 -07006304
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006305# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006306 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006307 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006308 "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 -07006309 }
Joe Perches6712d852012-03-23 15:02:20 -07006310
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006311# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006312# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006313 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006314 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006315 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006316 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006317 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006318
6319# use of NR_CPUS is usually wrong
6320# ignore definitions of NR_CPUS and usage to define arrays as likely right
6321 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006322 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6323 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006324 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6325 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6326 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006327 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006328 WARN("NR_CPUS",
6329 "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 -07006330 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006331
Joe Perches52ea8502013-11-12 15:10:09 -08006332# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6333 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6334 ERROR("DEFINE_ARCH_HAS",
6335 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6336 }
6337
Joe Perchesacd93622015-02-13 14:38:38 -08006338# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006339 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006340 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6341 WARN("LIKELY_MISUSE",
6342 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6343 }
6344
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006345# whine mightly about in_atomic
6346 if ($line =~ /\bin_atomic\s*\(/) {
6347 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006348 ERROR("IN_ATOMIC",
6349 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006350 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006351 WARN("IN_ATOMIC",
6352 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006353 }
6354 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006355
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006356# check for mutex_trylock_recursive usage
6357 if ($line =~ /mutex_trylock_recursive/) {
6358 ERROR("LOCKING",
6359 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6360 }
6361
Peter Zijlstra1704f472010-03-19 01:37:42 +01006362# check for lockdep_set_novalidate_class
6363 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6364 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6365 if ($realfile !~ m@^kernel/lockdep@ &&
6366 $realfile !~ m@^include/linux/lockdep@ &&
6367 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006368 ERROR("LOCKDEP",
6369 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006370 }
6371 }
Dave Jones88f88312011-01-12 16:59:59 -08006372
Joe Perchesb392c642015-04-16 12:44:16 -07006373 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6374 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006375 WARN("EXPORTED_WORLD_WRITABLE",
6376 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006377 }
Joe Perches24358802014-04-03 14:49:13 -07006378
Joe Perches00180462018-02-06 15:38:55 -08006379# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6380# and whether or not function naming is typical and if
6381# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006382 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006383 defined $stat &&
6384 $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
6385 my $var = $1;
6386 my $perms = $2;
6387 my $show = $3;
6388 my $store = $4;
6389 my $octal_perms = perms_to_octal($perms);
6390 if ($show =~ /^${var}_show$/ &&
6391 $store =~ /^${var}_store$/ &&
6392 $octal_perms eq "0644") {
6393 if (WARN("DEVICE_ATTR_RW",
6394 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6395 $fix) {
6396 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6397 }
6398 } elsif ($show =~ /^${var}_show$/ &&
6399 $store =~ /^NULL$/ &&
6400 $octal_perms eq "0444") {
6401 if (WARN("DEVICE_ATTR_RO",
6402 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6403 $fix) {
6404 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6405 }
6406 } elsif ($show =~ /^NULL$/ &&
6407 $store =~ /^${var}_store$/ &&
6408 $octal_perms eq "0200") {
6409 if (WARN("DEVICE_ATTR_WO",
6410 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6411 $fix) {
6412 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6413 }
6414 } elsif ($octal_perms eq "0644" ||
6415 $octal_perms eq "0444" ||
6416 $octal_perms eq "0200") {
6417 my $newshow = "$show";
6418 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6419 my $newstore = $store;
6420 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6421 my $rename = "";
6422 if ($show ne $newshow) {
6423 $rename .= " '$show' to '$newshow'";
6424 }
6425 if ($store ne $newstore) {
6426 $rename .= " '$store' to '$newstore'";
6427 }
6428 WARN("DEVICE_ATTR_FUNCTIONS",
6429 "Consider renaming function(s)$rename\n" . $herecurr);
6430 } else {
6431 WARN("DEVICE_ATTR_PERMS",
6432 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6433 }
6434 }
6435
Joe Perches515a2352014-04-03 14:49:24 -07006436# Mode permission misuses where it seems decimal should be octal
6437# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006438# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6439# specific definition of not visible in sysfs.
6440# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6441# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006442 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006443 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006444 $line =~ /$mode_perms_search/) {
6445 foreach my $entry (@mode_permission_funcs) {
6446 my $func = $entry->[0];
6447 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006448
Joe Perches459cf0a2016-10-11 13:52:19 -07006449 my $lc = $stat =~ tr@\n@@;
6450 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006451 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006452
Joe Perches515a2352014-04-03 14:49:24 -07006453 my $skip_args = "";
6454 if ($arg_pos > 1) {
6455 $arg_pos--;
6456 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6457 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006458 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006459 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006460 my $val = $1;
6461 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006462 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6463 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6464 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006465 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006466 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006467 }
6468 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006469 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006470 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006471 }
Joe Perches24358802014-04-03 14:49:13 -07006472 }
6473 }
6474 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006475
Joe Perches459cf0a2016-10-11 13:52:19 -07006476# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006477 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006478 my $oval = $1;
6479 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006480 if (WARN("SYMBOLIC_PERMS",
6481 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6482 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006483 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006484 }
6485 }
6486
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006487# validate content of MODULE_LICENSE against list from include/linux/module.h
6488 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6489 my $extracted_string = get_quoted_string($line, $rawline);
6490 my $valid_licenses = qr{
6491 GPL|
6492 GPL\ v2|
6493 GPL\ and\ additional\ rights|
6494 Dual\ BSD/GPL|
6495 Dual\ MIT/GPL|
6496 Dual\ MPL/GPL|
6497 Proprietary
6498 }x;
6499 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6500 WARN("MODULE_LICENSE",
6501 "unknown module license " . $extracted_string . "\n" . $herecurr);
6502 }
6503 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006504 }
6505
6506 # If we have no input at all, then there is nothing to report on
6507 # so just keep quiet.
6508 if ($#rawlines == -1) {
6509 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006510 }
6511
Andy Whitcroft8905a672007-11-28 16:21:06 -08006512 # In mailback mode only produce a report in the negative, for
6513 # things that appear to be patches.
6514 if ($mailback && ($clean == 1 || !$is_patch)) {
6515 exit(0);
6516 }
6517
6518 # This is not a patch, and we are are in 'no-patch' mode so
6519 # just keep quiet.
6520 if (!$chk_patch && !$is_patch) {
6521 exit(0);
6522 }
6523
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006524 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006525 ERROR("NOT_UNIFIED_DIFF",
6526 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006527 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006528 if ($is_patch && $has_commit_log && $chk_signoff) {
6529 if ($signoff == 0) {
6530 ERROR("MISSING_SIGN_OFF",
6531 "Missing Signed-off-by: line(s)\n");
6532 } elsif (!$authorsignoff) {
6533 WARN("NO_AUTHOR_SIGN_OFF",
6534 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6535 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006536 }
6537
Andy Whitcroft8905a672007-11-28 16:21:06 -08006538 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006539 if ($summary && !($clean == 1 && $quiet == 1)) {
6540 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006541 print "total: $cnt_error errors, $cnt_warn warnings, " .
6542 (($check)? "$cnt_chk checks, " : "") .
6543 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006544 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006545
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006546 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006547 # If there were any defects found and not already fixing them
6548 if (!$clean and !$fix) {
6549 print << "EOM"
6550
6551NOTE: For some of the reported defects, checkpatch may be able to
6552 mechanically convert to the typical style using --fix or --fix-inplace.
6553EOM
6554 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006555 # If there were whitespace errors which cleanpatch can fix
6556 # then suggest that.
6557 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006558 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006559 print << "EOM"
6560
6561NOTE: Whitespace errors detected.
6562 You may wish to use scripts/cleanpatch or scripts/cleanfile
6563EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006564 }
6565 }
6566
Joe Perchesd752fcc2014-08-06 16:11:05 -07006567 if ($clean == 0 && $fix &&
6568 ("@rawlines" ne "@fixed" ||
6569 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006570 my $newfile = $filename;
6571 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006572 my $linecount = 0;
6573 my $f;
6574
Joe Perchesd752fcc2014-08-06 16:11:05 -07006575 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6576
Joe Perches3705ce52013-07-03 15:05:31 -07006577 open($f, '>', $newfile)
6578 or die "$P: Can't open $newfile for write\n";
6579 foreach my $fixed_line (@fixed) {
6580 $linecount++;
6581 if ($file) {
6582 if ($linecount > 3) {
6583 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006584 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006585 }
6586 } else {
6587 print $f $fixed_line . "\n";
6588 }
6589 }
6590 close($f);
6591
6592 if (!$quiet) {
6593 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006594
Joe Perches3705ce52013-07-03 15:05:31 -07006595Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6596
6597Do _NOT_ trust the results written to this file.
6598Do _NOT_ submit these changes without inspecting them for correctness.
6599
6600This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6601No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006602EOM
6603 }
6604 }
6605
Joe Perchesd8469f12015-06-25 15:03:00 -07006606 if ($quiet == 0) {
6607 print "\n";
6608 if ($clean == 1) {
6609 print "$vname has no obvious style problems and is ready for submission.\n";
6610 } else {
6611 print "$vname has style problems, please review.\n";
6612 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006613 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006614 return $clean;
6615}