blob: 2b32816f5c2e581b165b388aa85b2ef0d294cde9 [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";
Vadim Bendebury98005e82019-03-07 16:28:38 -080064my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
Joe Perchesdbbf8692019-09-25 16:46:52 -070065# git output parsing needs US English output, so first set backtick child process LANGUAGE
66my $git_command ='export LANGUAGE=en_US.UTF-8; git';
Hannes Eder77f5b102009-09-21 17:04:37 -070067
68sub help {
69 my ($exitcode) = @_;
70
71 print << "EOM";
72Usage: $P [OPTION]... [FILE]...
73Version: $V
74
75Options:
76 -q, --quiet quiet
77 --no-tree run without a kernel tree
78 --no-signoff do not check for 'Signed-off-by' line
79 --patch treat FILE as patchfile (default)
80 --emacs emacs compile window format
81 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070082 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070083 -g, --git treat FILE as a single commit or git revision range
84 single git commit with:
85 <rev>
86 <rev>^
87 <rev>~n
88 multiple git commits with:
89 <rev1>..<rev2>
90 <rev1>...<rev2>
91 <rev>-<count>
92 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070093 -f, --file treat FILE as regular source file
94 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070096 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070097 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070098 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080099 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700100 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700101 --root=PATH PATH to the kernel tree root
102 --no-summary suppress the per-file summary
103 --mailback only produce a report in case of warnings/errors
104 --summary-file include the filename in summary
105 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
106 'values', 'possible', 'type', and 'attr' (default
107 is all off)
108 --test-only=WORD report only warnings/errors containing WORD
109 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700110 --fix EXPERIMENTAL - may create horrible results
111 If correctable single-line errors exist, create
112 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
113 with potential errors corrected to the preferred
114 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800115 --fix-inplace EXPERIMENTAL - may create horrible results
116 Is the same as --fix, but overwrites the input
117 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700118 --ignore-perl-version override checking of perl version. expect
119 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700121 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700123 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700124 --color[=WHEN] Use colors 'always', 'never', or only when output
125 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700126 -h, --help, --version display this help and exit
127
128When FILE is - read standard input.
129EOM
130
131 exit($exitcode);
132}
133
Joe Perches3beb42e2016-05-20 17:04:14 -0700134sub uniq {
135 my %seen;
136 return grep { !$seen{$_}++ } @_;
137}
138
139sub list_types {
140 my ($exitcode) = @_;
141
142 my $count = 0;
143
144 local $/ = undef;
145
146 open(my $script, '<', abs_path($P)) or
147 die "$P: Can't read '$P' $!\n";
148
149 my $text = <$script>;
150 close($script);
151
152 my @types = ();
Jean Delvare0547fa52017-09-08 16:16:11 -0700153 # Also catch when type or level is passed through a variable
154 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Joe Perches3beb42e2016-05-20 17:04:14 -0700155 push (@types, $_);
156 }
157 @types = sort(uniq(@types));
158 print("#\tMessage type\n\n");
159 foreach my $type (@types) {
160 print(++$count . "\t" . $type . "\n");
161 }
162
163 exit($exitcode);
164}
165
Joe Perches000d1cc12011-07-25 17:13:25 -0700166my $conf = which_conf($configuration_file);
167if (-f $conf) {
168 my @conf_args;
169 open(my $conffile, '<', "$conf")
170 or warn "$P: Can't find a readable $configuration_file file $!\n";
171
172 while (<$conffile>) {
173 my $line = $_;
174
175 $line =~ s/\s*\n?$//g;
176 $line =~ s/^\s*//g;
177 $line =~ s/\s+/ /g;
178
179 next if ($line =~ m/^\s*#/);
180 next if ($line =~ m/^\s*$/);
181
182 my @words = split(" ", $line);
183 foreach my $word (@words) {
184 last if ($word =~ m/^#/);
185 push (@conf_args, $word);
186 }
187 }
188 close($conffile);
189 unshift(@ARGV, @conf_args) if @conf_args;
190}
191
John Brooks737c0762017-07-10 15:52:24 -0700192# Perl's Getopt::Long allows options to take optional arguments after a space.
193# Prevent --color by itself from consuming other arguments
194foreach (@ARGV) {
195 if ($_ eq "--color" || $_ eq "-color") {
196 $_ = "--color=$color";
197 }
198}
199
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700200GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700201 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700202 'tree!' => \$tree,
203 'signoff!' => \$chk_signoff,
204 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700205 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800206 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700207 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700208 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700209 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700210 'subjective!' => \$check,
211 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700212 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700213 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700214 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700215 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800216 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700217 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700218 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800219 'summary!' => \$summary,
220 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800221 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700222 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800223 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700224 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800225 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700226 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700227 'codespell!' => \$codespell,
228 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700229 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700230 'color=s' => \$color,
231 'no-color' => \$color, #keep old behaviors of -nocolor
232 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700233 'h|help' => \$help,
234 'version' => \$help
235) or help(1);
236
237help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700238
Joe Perches3beb42e2016-05-20 17:04:14 -0700239list_types(0) if ($list_types);
240
Joe Perches9624b8d2014-01-23 15:54:44 -0800241$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700242$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800243
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700244my $exit = 0;
245
Joe Perches5b579802018-08-21 21:57:33 -0700246my $perl_version_ok = 1;
Dave Hansend62a2012013-09-11 14:23:56 -0700247if ($^V && $^V lt $minimum_perl_version) {
Joe Perches5b579802018-08-21 21:57:33 -0700248 $perl_version_ok = 0;
Dave Hansend62a2012013-09-11 14:23:56 -0700249 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Joe Perches5b579802018-08-21 21:57:33 -0700250 exit(1) if (!$ignore_perl_version);
Dave Hansend62a2012013-09-11 14:23:56 -0700251}
252
Allen Hubbe45107ff2016-08-02 14:04:47 -0700253#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700254if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700255 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700256}
257
John Brooks737c0762017-07-10 15:52:24 -0700258if ($color =~ /^[01]$/) {
259 $color = !$color;
260} elsif ($color =~ /^always$/i) {
261 $color = 1;
262} elsif ($color =~ /^never$/i) {
263 $color = 0;
264} elsif ($color =~ /^auto$/i) {
265 $color = (-t STDOUT);
266} else {
267 die "Invalid color mode: $color\n";
268}
269
Joe Perches91bfe482013-09-11 14:23:59 -0700270sub hash_save_array_words {
271 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700272
Joe Perches91bfe482013-09-11 14:23:59 -0700273 my @array = split(/,/, join(',', @$arrayRef));
274 foreach my $word (@array) {
275 $word =~ s/\s*\n?$//g;
276 $word =~ s/^\s*//g;
277 $word =~ s/\s+/ /g;
278 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700279
Joe Perches91bfe482013-09-11 14:23:59 -0700280 next if ($word =~ m/^\s*#/);
281 next if ($word =~ m/^\s*$/);
282
283 $hashRef->{$word}++;
284 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700285}
286
Joe Perches91bfe482013-09-11 14:23:59 -0700287sub hash_show_words {
288 my ($hashRef, $prefix) = @_;
289
Joe Perches3c816e42015-06-25 15:03:29 -0700290 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700291 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700292 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700293 print " $word";
294 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700295 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700296 }
297}
298
299hash_save_array_words(\%ignore_type, \@ignore);
300hash_save_array_words(\%use_type, \@use);
301
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302my $dbg_values = 0;
303my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700304my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700305my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800306for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800307 ## no critic
308 eval "\${dbg_$key} = '$debug{$key}';";
309 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800310}
311
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700312my $rpt_cleaners = 0;
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314if ($terse) {
315 $emacs = 1;
316 $quiet++;
317}
318
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700319if ($tree) {
320 if (defined $root) {
321 if (!top_of_kernel_tree($root)) {
322 die "$P: $root: --root does not point at a valid tree\n";
323 }
324 } else {
325 if (top_of_kernel_tree('.')) {
326 $root = '.';
327 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
328 top_of_kernel_tree($1)) {
329 $root = $1;
330 }
331 }
332
333 if (!defined $root) {
334 print "Must be run from the top-level dir. of a kernel tree\n";
335 exit(2);
336 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700337}
338
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339my $emitted_corrupt = 0;
340
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700341our $Ident = qr{
342 [A-Za-z_][A-Za-z\d_]*
343 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
344 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Storage = qr{extern|static|asmlinkage};
346our $Sparse = qr{
347 __user|
348 __kernel|
349 __force|
350 __iomem|
351 __must_check|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800352 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700353 __ref|
Geert Uytterhoeven33aa4592018-08-21 21:57:36 -0700354 __refconst|
355 __refdata|
Boqun Fengad315452015-12-29 12:18:46 +0800356 __rcu|
357 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700358 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800359our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
360our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
361our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
362our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
363our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700364
Wolfram Sang52131292010-03-05 13:43:51 -0800365# Notes to $Attribute:
366# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700367our $Attribute = qr{
368 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700369 __percpu|
370 __nocast|
371 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200372 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700373 __packed__|
374 __packed2__|
375 __naked|
376 __maybe_unused|
377 __always_unused|
378 __noreturn|
379 __used|
380 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800381 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700382 __noclone|
383 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 __read_mostly|
Joe Perchesc5967e92018-09-04 15:46:20 -0700385 __ro_after_init|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700386 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700387 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700388 ____cacheline_aligned|
389 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800390 ____cacheline_internodealigned_in_smp|
391 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700392 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700393our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700394our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700395our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
396our $Lval = qr{$Ident(?:$Member)*};
397
Joe Perches95e2c602013-07-03 15:05:20 -0700398our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
399our $Binary = qr{(?i)0b[01]+$Int_type?};
400our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
401our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700402our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800403our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800404our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
405our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
406our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800407our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700408our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800409our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700410our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700411our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700412our $Operators = qr{
413 <=|>=|==|!=|
414 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700415 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700416 }x;
417
Joe Perches91cb5192014-04-03 14:49:32 -0700418our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
419
Joe Perchesab7e23f2015-04-16 12:44:22 -0700420our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800421our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700422our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700423our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700425our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800426our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700427our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800428
Joe Perches15662b32011-10-31 17:13:12 -0700429our $NON_ASCII_UTF8 = qr{
430 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700431 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
432 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
433 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
434 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
435 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
436 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
437}x;
438
Joe Perches15662b32011-10-31 17:13:12 -0700439our $UTF8 = qr{
440 [\x09\x0A\x0D\x20-\x7E] # ASCII
441 | $NON_ASCII_UTF8
442}x;
443
Joe Perchese6176fa2015-06-25 15:02:49 -0700444our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800445our $typeOtherOSTypedefs = qr{(?x:
446 u_(?:char|short|int|long) | # bsd
447 u(?:nchar|short|int|long) # sysv
448)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700449our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700450 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700451 atomic_t
452)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700453our $typeTypedefs = qr{(?x:
454 $typeC99Typedefs\b|
455 $typeOtherOSTypedefs\b|
456 $typeKernelTypedefs\b
457)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700458
Joe Perches6d32f7a2015-11-06 16:31:37 -0800459our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
460
Joe Perches691e6692010-03-05 13:43:51 -0800461our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800462 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700463 (?:[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 -0800464 TP_printk|
Joe Perches6e60c022011-07-25 17:13:27 -0700465 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700466 panic|
Joe Perches06668722013-11-12 15:10:07 -0800467 MODULE_[A-Z_]+|
468 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800469)};
470
Joe Perchese29a70f2019-03-07 16:28:35 -0800471our $allocFunctions = qr{(?x:
472 (?:(?:devm_)?
473 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
474 kstrdup(?:_const)? |
475 kmemdup(?:_nul)?) |
476 (?:\w+)?alloc_skb(?:ip_align)? |
477 # dev_alloc_skb/netdev_alloc_skb, et al
478 dma_alloc_coherent
479)};
480
Joe Perches20112472011-07-25 17:13:23 -0700481our $signature_tags = qr{(?xi:
482 Signed-off-by:|
Jorge Ramirez-Ortizd4994802019-01-03 15:29:12 -0800483 Co-developed-by:|
Joe Perches20112472011-07-25 17:13:23 -0700484 Acked-by:|
485 Tested-by:|
486 Reviewed-by:|
487 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700488 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700489 To:|
490 Cc:
491)};
492
Joe Perches18130872014-08-06 16:11:22 -0700493our @typeListMisordered = (
494 qr{char\s+(?:un)?signed},
495 qr{int\s+(?:(?:un)?signed\s+)?short\s},
496 qr{int\s+short(?:\s+(?:un)?signed)},
497 qr{short\s+int(?:\s+(?:un)?signed)},
498 qr{(?:un)?signed\s+int\s+short},
499 qr{short\s+(?:un)?signed},
500 qr{long\s+int\s+(?:un)?signed},
501 qr{int\s+long\s+(?:un)?signed},
502 qr{long\s+(?:un)?signed\s+int},
503 qr{int\s+(?:un)?signed\s+long},
504 qr{int\s+(?:un)?signed},
505 qr{int\s+long\s+long\s+(?:un)?signed},
506 qr{long\s+long\s+int\s+(?:un)?signed},
507 qr{long\s+long\s+(?:un)?signed\s+int},
508 qr{long\s+long\s+(?:un)?signed},
509 qr{long\s+(?:un)?signed},
510);
511
Andy Whitcroft8905a672007-11-28 16:21:06 -0800512our @typeList = (
513 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700514 qr{(?:(?:un)?signed\s+)?char},
515 qr{(?:(?:un)?signed\s+)?short\s+int},
516 qr{(?:(?:un)?signed\s+)?short},
517 qr{(?:(?:un)?signed\s+)?int},
518 qr{(?:(?:un)?signed\s+)?long\s+int},
519 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
520 qr{(?:(?:un)?signed\s+)?long\s+long},
521 qr{(?:(?:un)?signed\s+)?long},
522 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523 qr{float},
524 qr{double},
525 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800526 qr{struct\s+$Ident},
527 qr{union\s+$Ident},
528 qr{enum\s+$Ident},
529 qr{${Ident}_t},
530 qr{${Ident}_handler},
531 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700532 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800533);
Joe Perches938224b2016-01-20 14:59:15 -0800534
535our $C90_int_types = qr{(?x:
536 long\s+long\s+int\s+(?:un)?signed|
537 long\s+long\s+(?:un)?signed\s+int|
538 long\s+long\s+(?:un)?signed|
539 (?:(?:un)?signed\s+)?long\s+long\s+int|
540 (?:(?:un)?signed\s+)?long\s+long|
541 int\s+long\s+long\s+(?:un)?signed|
542 int\s+(?:(?:un)?signed\s+)?long\s+long|
543
544 long\s+int\s+(?:un)?signed|
545 long\s+(?:un)?signed\s+int|
546 long\s+(?:un)?signed|
547 (?:(?:un)?signed\s+)?long\s+int|
548 (?:(?:un)?signed\s+)?long|
549 int\s+long\s+(?:un)?signed|
550 int\s+(?:(?:un)?signed\s+)?long|
551
552 int\s+(?:un)?signed|
553 (?:(?:un)?signed\s+)?int
554)};
555
Alex Dowad485ff232015-06-25 15:02:52 -0700556our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700557our @typeListWithAttr = (
558 @typeList,
559 qr{struct\s+$InitAttribute\s+$Ident},
560 qr{union\s+$InitAttribute\s+$Ident},
561);
562
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700563our @modifierList = (
564 qr{fastcall},
565);
Alex Dowad485ff232015-06-25 15:02:52 -0700566our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800567
Joe Perches24358802014-04-03 14:49:13 -0700568our @mode_permission_funcs = (
569 ["module_param", 3],
570 ["module_param_(?:array|named|string)", 4],
571 ["module_param_array_named", 5],
572 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
573 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700574 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
575 ["IIO_DEV_ATTR_[A-Z_]+", 1],
576 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
577 ["SENSOR_TEMPLATE(?:_2|)", 3],
578 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700579);
580
Joe Perches515a2352014-04-03 14:49:24 -0700581#Create a search pattern for all these functions to speed up a loop below
582our $mode_perms_search = "";
583foreach my $entry (@mode_permission_funcs) {
584 $mode_perms_search .= '|' if ($mode_perms_search ne "");
585 $mode_perms_search .= $entry->[0];
586}
Joe Perches00180462018-02-06 15:38:55 -0800587$mode_perms_search = "(?:${mode_perms_search})";
Joe Perches515a2352014-04-03 14:49:24 -0700588
Joe Perches9189c7e2018-09-07 15:26:18 -0700589our %deprecated_apis = (
590 "synchronize_rcu_bh" => "synchronize_rcu",
591 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
592 "call_rcu_bh" => "call_rcu",
593 "rcu_barrier_bh" => "rcu_barrier",
594 "synchronize_sched" => "synchronize_rcu",
595 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
596 "call_rcu_sched" => "call_rcu",
597 "rcu_barrier_sched" => "rcu_barrier",
598 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
599 "cond_synchronize_sched" => "cond_synchronize_rcu",
600);
601
602#Create a search pattern for all these strings to speed up a loop below
603our $deprecated_apis_search = "";
604foreach my $entry (keys %deprecated_apis) {
605 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
606 $deprecated_apis_search .= $entry;
607}
608$deprecated_apis_search = "(?:${deprecated_apis_search})";
609
Joe Perchesb392c642015-04-16 12:44:16 -0700610our $mode_perms_world_writable = qr{
611 S_IWUGO |
612 S_IWOTH |
613 S_IRWXUGO |
614 S_IALLUGO |
615 0[0-7][0-7][2367]
616}x;
617
Joe Perchesf90774e2016-10-11 13:51:47 -0700618our %mode_permission_string_types = (
619 "S_IRWXU" => 0700,
620 "S_IRUSR" => 0400,
621 "S_IWUSR" => 0200,
622 "S_IXUSR" => 0100,
623 "S_IRWXG" => 0070,
624 "S_IRGRP" => 0040,
625 "S_IWGRP" => 0020,
626 "S_IXGRP" => 0010,
627 "S_IRWXO" => 0007,
628 "S_IROTH" => 0004,
629 "S_IWOTH" => 0002,
630 "S_IXOTH" => 0001,
631 "S_IRWXUGO" => 0777,
632 "S_IRUGO" => 0444,
633 "S_IWUGO" => 0222,
634 "S_IXUGO" => 0111,
635);
636
637#Create a search pattern for all these strings to speed up a loop below
638our $mode_perms_string_search = "";
639foreach my $entry (keys %mode_permission_string_types) {
640 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
641 $mode_perms_string_search .= $entry;
642}
Joe Perches00180462018-02-06 15:38:55 -0800643our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
644our $multi_mode_perms_string_search = qr{
645 ${single_mode_perms_string_search}
646 (?:\s*\|\s*${single_mode_perms_string_search})*
647}x;
648
649sub perms_to_octal {
650 my ($string) = @_;
651
652 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
653
654 my $val = "";
655 my $oval = "";
656 my $to = 0;
657 my $curpos = 0;
658 my $lastpos = 0;
659 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
660 $curpos = pos($string);
661 my $match = $2;
662 my $omatch = $1;
663 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
664 $lastpos = $curpos;
665 $to |= $mode_permission_string_types{$match};
666 $val .= '\s*\|\s*' if ($val ne "");
667 $val .= $match;
668 $oval .= $omatch;
669 }
670 $oval =~ s/^\s*\|\s*//;
671 $oval =~ s/\s*\|\s*$//;
672 return sprintf("%04o", $to);
673}
Joe Perchesf90774e2016-10-11 13:51:47 -0700674
Wolfram Sang7840a942010-08-09 17:20:57 -0700675our $allowed_asm_includes = qr{(?x:
676 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700677 memory|
678 time|
679 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700680)};
681# memory.h: ARM has a custom one
682
Kees Cook66b47b42014-10-13 15:51:57 -0700683# Load common spelling mistakes and build regular expression list.
684my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700685my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700686
Joe Perches36061e32014-12-10 15:51:43 -0800687if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800688 while (<$spelling>) {
689 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700690
Joe Perches36061e32014-12-10 15:51:43 -0800691 $line =~ s/\s*\n?$//g;
692 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700693
Joe Perches36061e32014-12-10 15:51:43 -0800694 next if ($line =~ m/^\s*#/);
695 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700696
Joe Perches36061e32014-12-10 15:51:43 -0800697 my ($suspect, $fix) = split(/\|\|/, $line);
698
Joe Perches36061e32014-12-10 15:51:43 -0800699 $spelling_fix{$suspect} = $fix;
700 }
701 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800702} else {
703 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700704}
Kees Cook66b47b42014-10-13 15:51:57 -0700705
Joe Perchesebfd7d62015-04-16 12:44:14 -0700706if ($codespell) {
707 if (open(my $spelling, '<', $codespellfile)) {
708 while (<$spelling>) {
709 my $line = $_;
710
711 $line =~ s/\s*\n?$//g;
712 $line =~ s/^\s*//g;
713
714 next if ($line =~ m/^\s*#/);
715 next if ($line =~ m/^\s*$/);
716 next if ($line =~ m/, disabled/i);
717
718 $line =~ s/,.*$//;
719
720 my ($suspect, $fix) = split(/->/, $line);
721
722 $spelling_fix{$suspect} = $fix;
723 }
724 close($spelling);
725 } else {
726 warn "No codespell typos will be found - file '$codespellfile': $!\n";
727 }
728}
729
730$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
731
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700732sub read_words {
733 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700734
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700735 if (open(my $words, '<', $file)) {
736 while (<$words>) {
737 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700738
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700739 $line =~ s/\s*\n?$//g;
740 $line =~ s/^\s*//g;
741
742 next if ($line =~ m/^\s*#/);
743 next if ($line =~ m/^\s*$/);
744 if ($line =~ /\s/) {
745 print("$file: '$line' invalid - ignored\n");
746 next;
747 }
748
749 $$wordsRef .= '|' if ($$wordsRef ne "");
750 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700751 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700752 close($file);
753 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700754 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700755
756 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700757}
758
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700759my $const_structs = "";
760read_words(\$const_structs, $conststructsfile)
761 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
762
763my $typeOtherTypedefs = "";
764if (length($typedefsfile)) {
765 read_words(\$typeOtherTypedefs, $typedefsfile)
766 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
767}
768$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
769
Andy Whitcroft8905a672007-11-28 16:21:06 -0800770sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700771 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
772 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700773 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700774 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700775 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700776 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700777 (?:$typeTypedefs\b)|
778 (?:${all}\b)
779 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800780 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700781 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800782 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800783 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700784 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700785 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700787 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800788 }x;
Joe Perches18130872014-08-06 16:11:22 -0700789 $NonptrTypeMisordered = qr{
790 (?:$Modifier\s+|const\s+)*
791 (?:
792 (?:${Misordered}\b)
793 )
794 (?:\s+$Modifier|\s+const)*
795 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700796 $NonptrTypeWithAttr = qr{
797 (?:$Modifier\s+|const\s+)*
798 (?:
799 (?:typeof|__typeof__)\s*\([^\)]*\)|
800 (?:$typeTypedefs\b)|
801 (?:${allWithAttr}\b)
802 )
803 (?:\s+$Modifier|\s+const)*
804 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700806 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700807 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700808 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800809 }x;
Joe Perches18130872014-08-06 16:11:22 -0700810 $TypeMisordered = qr{
811 $NonptrTypeMisordered
812 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
813 (?:\s+$Inline|\s+$Modifier)*
814 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700815 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700816 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800817}
818build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700819
Joe Perches7d2367a2011-07-25 17:13:22 -0700820our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700821
822# Using $balanced_parens, $LvalOrFunc, or $FuncArg
823# requires at least perl version v5.10.0
824# Any use must be runtime checked with $^V
825
826our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700827our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800828our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700829
Joe Perchesf8422302014-08-06 16:11:31 -0700830our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700831 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700832 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Gilad Ben-Yossef3d102fc2018-04-10 16:33:17 -0700833 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
834 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Joe Perchesf8422302014-08-06 16:11:31 -0700835)};
836
Joe Perches7d2367a2011-07-25 17:13:22 -0700837sub deparenthesize {
838 my ($string) = @_;
839 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700840
841 while ($string =~ /^\s*\(.*\)\s*$/) {
842 $string =~ s@^\s*\(\s*@@;
843 $string =~ s@\s*\)\s*$@@;
844 }
845
Joe Perches7d2367a2011-07-25 17:13:22 -0700846 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700847
Joe Perches7d2367a2011-07-25 17:13:22 -0700848 return $string;
849}
850
Joe Perches34456862013-07-03 15:05:34 -0700851sub seed_camelcase_file {
852 my ($file) = @_;
853
854 return if (!(-f $file));
855
856 local $/;
857
858 open(my $include_file, '<', "$file")
859 or warn "$P: Can't read '$file' $!\n";
860 my $text = <$include_file>;
861 close($include_file);
862
863 my @lines = split('\n', $text);
864
865 foreach my $line (@lines) {
866 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
867 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
868 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800869 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
870 $camelcase{$1} = 1;
871 } 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 -0700872 $camelcase{$1} = 1;
873 }
874 }
875}
876
Joe Perchescd28b112019-12-04 16:52:09 -0800877our %maintained_status = ();
878
Joe Perches85b0ee12016-10-11 13:51:44 -0700879sub is_maintained_obsolete {
880 my ($filename) = @_;
881
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800882 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700883
Joe Perchescd28b112019-12-04 16:52:09 -0800884 if (!exists($maintained_status{$filename})) {
885 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
886 }
Joe Perches85b0ee12016-10-11 13:51:44 -0700887
Joe Perchescd28b112019-12-04 16:52:09 -0800888 return $maintained_status{$filename} =~ /obsolete/i;
Joe Perches85b0ee12016-10-11 13:51:44 -0700889}
890
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700891sub is_SPDX_License_valid {
892 my ($license) = @_;
893
Joe Perches56294112018-08-21 21:58:04 -0700894 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700895
Joe Perches56294112018-08-21 21:58:04 -0700896 my $root_path = abs_path($root);
897 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
Joe Perches3b6e8ac2018-08-21 21:57:47 -0700898 return 0 if ($status ne "");
899 return 1;
900}
901
Joe Perches34456862013-07-03 15:05:34 -0700902my $camelcase_seeded = 0;
903sub seed_camelcase_includes {
904 return if ($camelcase_seeded);
905
906 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700907 my $camelcase_cache = "";
908 my @include_files = ();
909
910 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700911
Richard Genoud3645e322014-02-10 14:25:32 -0800912 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700913 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Joe Perches351b2a12013-07-03 15:05:36 -0700914 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700915 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700916 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700917 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700918 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700919 @include_files = split('\n', $files);
920 foreach my $file (@include_files) {
921 my $date = POSIX::strftime("%Y%m%d%H%M",
922 localtime((stat $file)[9]));
923 $last_mod_date = $date if ($last_mod_date < $date);
924 }
925 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700926 }
Joe Perchesc707a812013-07-08 16:00:43 -0700927
928 if ($camelcase_cache ne "" && -f $camelcase_cache) {
929 open(my $camelcase_file, '<', "$camelcase_cache")
930 or warn "$P: Can't read '$camelcase_cache' $!\n";
931 while (<$camelcase_file>) {
932 chomp;
933 $camelcase{$_} = 1;
934 }
935 close($camelcase_file);
936
937 return;
938 }
939
Richard Genoud3645e322014-02-10 14:25:32 -0800940 if (-e ".git") {
Joe Perchesdbbf8692019-09-25 16:46:52 -0700941 $files = `${git_command} ls-files "include/*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700942 @include_files = split('\n', $files);
943 }
944
Joe Perches34456862013-07-03 15:05:34 -0700945 foreach my $file (@include_files) {
946 seed_camelcase_file($file);
947 }
Joe Perches351b2a12013-07-03 15:05:36 -0700948
Joe Perchesc707a812013-07-08 16:00:43 -0700949 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700950 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700951 open(my $camelcase_file, '>', "$camelcase_cache")
952 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700953 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
954 print $camelcase_file ("$_\n");
955 }
956 close($camelcase_file);
957 }
Joe Perches34456862013-07-03 15:05:34 -0700958}
959
Joe Perchesd311cd42014-08-06 16:10:57 -0700960sub git_commit_info {
961 my ($commit, $id, $desc) = @_;
962
963 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
964
Joe Perchesdbbf8692019-09-25 16:46:52 -0700965 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Joe Perchesd311cd42014-08-06 16:10:57 -0700966 $output =~ s/^\s*//gm;
967 my @lines = split("\n", $output);
968
Joe Perches0d7835f2015-02-13 14:38:35 -0800969 return ($id, $desc) if ($#lines < 0);
970
Sean Christopherson5a7f4452019-09-25 16:46:49 -0700971 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Joe Perchesd311cd42014-08-06 16:10:57 -0700972# Maybe one day convert this block of bash into something that returns
973# all matching commit ids, but it's very slow...
974#
975# echo "checking commits $1..."
976# git rev-list --remotes | grep -i "^$1" |
977# while read line ; do
978# git log --format='%H %s' -1 $line |
979# echo "commit $(cut -c 1-12,41-)"
980# done
981 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700982 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700983 } else {
984 $id = substr($lines[0], 0, 12);
985 $desc = substr($lines[0], 41);
986 }
987
988 return ($id, $desc);
989}
990
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700991$chk_signoff = 0 if ($file);
992
Andy Whitcroft00df3442007-06-08 13:47:06 -0700993my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700995my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700996my @fixed_inserted = ();
997my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700998my $fixlinenr = -1;
999
Du, Changbin4a593c32016-05-20 17:04:16 -07001000# If input is git commits, extract all commits from the commit expressions.
1001# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1002die "$P: No git repository found\n" if ($git && !-e ".git");
1003
1004if ($git) {
1005 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001006 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -07001007 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -07001008 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1009 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -07001010 } elsif ($commit_expr =~ m/\.\./) {
1011 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -07001012 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001013 $git_range = "-1 $commit_expr";
1014 }
Joe Perchesdbbf8692019-09-25 16:46:52 -07001015 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001016 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -07001017 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1018 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001019 my $sha1 = $1;
1020 my $subject = $2;
1021 unshift(@commits, $sha1);
1022 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -07001023 }
1024 }
1025 die "$P: no git commits after extraction!\n" if (@commits == 0);
1026 @ARGV = @commits;
1027}
1028
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001029my $vname;
Vadim Bendebury98005e82019-03-07 16:28:38 -08001030$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001031for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001032 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -07001033 if ($git) {
1034 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1035 die "$P: $filename: git format-patch failed - $!\n";
1036 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001037 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001038 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -08001039 } elsif ($filename eq '-') {
1040 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -08001042 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001044 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 if ($filename eq '-') {
1046 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -07001047 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -07001048 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001049 } else {
1050 $vname = $filename;
1051 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001052 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001053 chomp;
1054 push(@rawlines, $_);
1055 }
Andy Whitcroft21caa132009-01-06 14:41:30 -08001056 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -07001057
1058 if ($#ARGV > 0 && $quiet == 0) {
1059 print '-' x length($vname) . "\n";
1060 print "$vname\n";
1061 print '-' x length($vname) . "\n";
1062 }
1063
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001065 $exit = 1;
1066 }
1067 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001068 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -07001069 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -07001070 @fixed_inserted = ();
1071 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -07001072 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -07001073 @modifierListFile = ();
1074 @typeListFile = ();
1075 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001076}
1077
Joe Perchesd8469f12015-06-25 15:03:00 -07001078if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -07001079 hash_show_words(\%use_type, "Used");
1080 hash_show_words(\%ignore_type, "Ignored");
1081
Joe Perches5b579802018-08-21 21:57:33 -07001082 if (!$perl_version_ok) {
Joe Perchesd8469f12015-06-25 15:03:00 -07001083 print << "EOM"
1084
1085NOTE: perl $^V is not modern enough to detect all possible issues.
Joe Perches5b579802018-08-21 21:57:33 -07001086 An upgrade to at least perl $minimum_perl_version is suggested.
Joe Perchesd8469f12015-06-25 15:03:00 -07001087EOM
1088 }
1089 if ($exit) {
1090 print << "EOM"
1091
1092NOTE: If any of the errors are false positives, please report
1093 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1094EOM
1095 }
1096}
1097
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001098exit($exit);
1099
1100sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001101 my ($root) = @_;
1102
1103 my @tree_check = (
1104 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1105 "README", "Documentation", "arch", "include", "drivers",
1106 "fs", "init", "ipc", "kernel", "lib", "scripts",
1107 );
1108
1109 foreach my $check (@tree_check) {
1110 if (! -e $root . '/' . $check) {
1111 return 0;
1112 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001113 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001114 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001115}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001116
Joe Perches20112472011-07-25 17:13:23 -07001117sub parse_email {
1118 my ($formatted_email) = @_;
1119
1120 my $name = "";
Joe Perchesdfa05c22020-04-06 20:10:48 -07001121 my $name_comment = "";
Joe Perches20112472011-07-25 17:13:23 -07001122 my $address = "";
1123 my $comment = "";
1124
1125 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1126 $name = $1;
1127 $address = $2;
1128 $comment = $3 if defined $3;
1129 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1130 $address = $1;
1131 $comment = $2 if defined $2;
1132 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1133 $address = $1;
1134 $comment = $2 if defined $2;
Joe Perches85e12062018-04-10 16:33:09 -07001135 $formatted_email =~ s/\Q$address\E.*$//;
Joe Perches20112472011-07-25 17:13:23 -07001136 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001137 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001138 $name =~ s/^\"|\"$//g;
1139 # If there's a name left after stripping spaces and
1140 # leading quotes, and the address doesn't have both
1141 # leading and trailing angle brackets, the address
1142 # is invalid. ie:
1143 # "joe smith joe@smith.com" bad
1144 # "joe smith <joe@smith.com" bad
1145 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1146 $name = "";
1147 $address = "";
1148 $comment = "";
1149 }
1150 }
1151
Joe Perches3705ce52013-07-03 15:05:31 -07001152 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001153 $name =~ s/^\"|\"$//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07001154 $name =~ s/(\s*\([^\)]+\))\s*//;
1155 if (defined($1)) {
1156 $name_comment = trim($1);
1157 }
Joe Perches3705ce52013-07-03 15:05:31 -07001158 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001159 $address =~ s/^\<|\>$//g;
1160
1161 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1162 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1163 $name = "\"$name\"";
1164 }
1165
Joe Perchesdfa05c22020-04-06 20:10:48 -07001166 return ($name, $name_comment, $address, $comment);
Joe Perches20112472011-07-25 17:13:23 -07001167}
1168
1169sub format_email {
1170 my ($name, $address) = @_;
1171
1172 my $formatted_email;
1173
Joe Perches3705ce52013-07-03 15:05:31 -07001174 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001175 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001176 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001177
1178 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1179 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1180 $name = "\"$name\"";
1181 }
1182
1183 if ("$name" eq "") {
1184 $formatted_email = "$address";
1185 } else {
1186 $formatted_email = "$name <$address>";
1187 }
1188
1189 return $formatted_email;
1190}
1191
Joe Perchesdfa05c22020-04-06 20:10:48 -07001192sub reformat_email {
1193 my ($email) = @_;
1194
1195 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1196 return format_email($email_name, $email_address);
1197}
1198
1199sub same_email_addresses {
1200 my ($email1, $email2) = @_;
1201
1202 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1203 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1204
1205 return $email1_name eq $email2_name &&
1206 $email1_address eq $email2_address;
1207}
1208
Joe Perchesd311cd42014-08-06 16:10:57 -07001209sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001210 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001211
Joe Perchesbd474ca2014-08-06 16:11:10 -07001212 foreach my $path (split(/:/, $ENV{PATH})) {
1213 if (-e "$path/$bin") {
1214 return "$path/$bin";
1215 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001216 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001217
Joe Perchesbd474ca2014-08-06 16:11:10 -07001218 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001219}
1220
Joe Perches000d1cc12011-07-25 17:13:25 -07001221sub which_conf {
1222 my ($conf) = @_;
1223
1224 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1225 if (-e "$path/$conf") {
1226 return "$path/$conf";
1227 }
1228 }
1229
1230 return "";
1231}
1232
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001233sub expand_tabs {
1234 my ($str) = @_;
1235
1236 my $res = '';
1237 my $n = 0;
1238 for my $c (split(//, $str)) {
1239 if ($c eq "\t") {
1240 $res .= ' ';
1241 $n++;
1242 for (; ($n % 8) != 0; $n++) {
1243 $res .= ' ';
1244 }
1245 next;
1246 }
1247 $res .= $c;
1248 $n++;
1249 }
1250
1251 return $res;
1252}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001253sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001254 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 return $res;
1256}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001257
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001258sub line_stats {
1259 my ($line) = @_;
1260
1261 # Drop the diff line leader and expand tabs
1262 $line =~ s/^.//;
1263 $line = expand_tabs($line);
1264
1265 # Pick the indent from the front of the line.
1266 my ($white) = ($line =~ /^(\s*)/);
1267
1268 return (length($line), length($white));
1269}
1270
Andy Whitcroft773647a2008-03-28 14:15:58 -07001271my $sanitise_quote = '';
1272
1273sub sanitise_line_reset {
1274 my ($in_comment) = @_;
1275
1276 if ($in_comment) {
1277 $sanitise_quote = '*/';
1278 } else {
1279 $sanitise_quote = '';
1280 }
1281}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001282sub sanitise_line {
1283 my ($line) = @_;
1284
1285 my $res = '';
1286 my $l = '';
1287
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001289 my $off = 0;
1290 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001291
Andy Whitcroft773647a2008-03-28 14:15:58 -07001292 # Always copy over the diff marker.
1293 $res = substr($line, 0, 1);
1294
1295 for ($off = 1; $off < length($line); $off++) {
1296 $c = substr($line, $off, 1);
1297
Claudio Fontana8d2e11b2018-04-10 16:33:42 -07001298 # Comments we are whacking completely including the begin
Andy Whitcroft773647a2008-03-28 14:15:58 -07001299 # and end, all to $;.
1300 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1301 $sanitise_quote = '*/';
1302
1303 substr($res, $off, 2, "$;$;");
1304 $off++;
1305 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001306 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001307 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001308 $sanitise_quote = '';
1309 substr($res, $off, 2, "$;$;");
1310 $off++;
1311 next;
1312 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001313 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1314 $sanitise_quote = '//';
1315
1316 substr($res, $off, 2, $sanitise_quote);
1317 $off++;
1318 next;
1319 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001320
1321 # A \ in a string means ignore the next character.
1322 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1323 $c eq "\\") {
1324 substr($res, $off, 2, 'XX');
1325 $off++;
1326 next;
1327 }
1328 # Regular quotes.
1329 if ($c eq "'" || $c eq '"') {
1330 if ($sanitise_quote eq '') {
1331 $sanitise_quote = $c;
1332
1333 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001334 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001335 } elsif ($sanitise_quote eq $c) {
1336 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001337 }
1338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001340 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001341 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1342 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001343 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1344 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001345 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1346 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001347 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001348 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001349 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 }
1351
Daniel Walker113f04a2009-09-21 17:04:35 -07001352 if ($sanitise_quote eq '//') {
1353 $sanitise_quote = '';
1354 }
1355
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001357 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001358 my $clean = 'X' x length($1);
1359 $res =~ s@\<.*\>@<$clean>@;
1360
1361 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001362 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001364 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001365 }
1366
Joe Perchesdadf6802016-08-02 14:04:33 -07001367 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1368 my $match = $1;
1369 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1370 }
1371
Andy Whitcroft00df3442007-06-08 13:47:06 -07001372 return $res;
1373}
1374
Joe Perchesa6962d72013-04-29 16:18:13 -07001375sub get_quoted_string {
1376 my ($line, $rawline) = @_;
1377
Joe Perches478b1792018-04-10 16:33:34 -07001378 return "" if (!defined($line) || !defined($rawline));
Joe Perches33acb542015-06-25 15:02:54 -07001379 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001380 return substr($rawline, $-[0], $+[0] - $-[0]);
1381}
1382
Andy Whitcroft8905a672007-11-28 16:21:06 -08001383sub ctx_statement_block {
1384 my ($linenr, $remain, $off) = @_;
1385 my $line = $linenr - 1;
1386 my $blk = '';
1387 my $soff = $off;
1388 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001389 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001390
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001391 my $loff = 0;
1392
Andy Whitcroft8905a672007-11-28 16:21:06 -08001393 my $type = '';
1394 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001395 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001396 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001397 my $c;
1398 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001399
1400 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001401 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001402 @stack = (['', 0]) if ($#stack == -1);
1403
Andy Whitcroft773647a2008-03-28 14:15:58 -07001404 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001405 # If we are about to drop off the end, pull in more
1406 # context.
1407 if ($off >= $len) {
1408 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001409 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001410 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001411 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001412 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001413 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001414 $len = length($blk);
1415 $line++;
1416 last;
1417 }
1418 # Bail if there is no further context.
1419 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001420 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001421 last;
1422 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001423 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1424 $level++;
1425 $type = '#';
1426 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001427 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001428 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001429 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001430 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001431
Andy Whitcroft773647a2008-03-28 14:15:58 -07001432 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001433
1434 # Handle nested #if/#else.
1435 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1436 push(@stack, [ $type, $level ]);
1437 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1438 ($type, $level) = @{$stack[$#stack - 1]};
1439 } elsif ($remainder =~ /^#\s*endif\b/) {
1440 ($type, $level) = @{pop(@stack)};
1441 }
1442
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443 # Statement ends at the ';' or a close '}' at the
1444 # outermost level.
1445 if ($level == 0 && $c eq ';') {
1446 last;
1447 }
1448
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001449 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001450 if ($level == 0 && $coff_set == 0 &&
1451 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1452 $remainder =~ /^(else)(?:\s|{)/ &&
1453 $remainder !~ /^else\s+if\b/) {
1454 $coff = $off + length($1) - 1;
1455 $coff_set = 1;
1456 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1457 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001458 }
1459
Andy Whitcroft8905a672007-11-28 16:21:06 -08001460 if (($type eq '' || $type eq '(') && $c eq '(') {
1461 $level++;
1462 $type = '(';
1463 }
1464 if ($type eq '(' && $c eq ')') {
1465 $level--;
1466 $type = ($level != 0)? '(' : '';
1467
1468 if ($level == 0 && $coff < $soff) {
1469 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001470 $coff_set = 1;
1471 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001472 }
1473 }
1474 if (($type eq '' || $type eq '{') && $c eq '{') {
1475 $level++;
1476 $type = '{';
1477 }
1478 if ($type eq '{' && $c eq '}') {
1479 $level--;
1480 $type = ($level != 0)? '{' : '';
1481
1482 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001483 if (substr($blk, $off + 1, 1) eq ';') {
1484 $off++;
1485 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001486 last;
1487 }
1488 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001489 # Preprocessor commands end at the newline unless escaped.
1490 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1491 $level--;
1492 $type = '';
1493 $off++;
1494 last;
1495 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496 $off++;
1497 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001498 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001499 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001500 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001501 $line++;
1502 $remain--;
1503 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001504
1505 my $statement = substr($blk, $soff, $off - $soff + 1);
1506 my $condition = substr($blk, $soff, $coff - $soff + 1);
1507
1508 #warn "STATEMENT<$statement>\n";
1509 #warn "CONDITION<$condition>\n";
1510
Andy Whitcroft773647a2008-03-28 14:15:58 -07001511 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001512
1513 return ($statement, $condition,
1514 $line, $remain + 1, $off - $loff + 1, $level);
1515}
1516
Andy Whitcroftcf655042008-03-04 14:28:20 -08001517sub statement_lines {
1518 my ($stmt) = @_;
1519
1520 # Strip the diff line prefixes and rip blank lines at start and end.
1521 $stmt =~ s/(^|\n)./$1/g;
1522 $stmt =~ s/^\s*//;
1523 $stmt =~ s/\s*$//;
1524
1525 my @stmt_lines = ($stmt =~ /\n/g);
1526
1527 return $#stmt_lines + 2;
1528}
1529
1530sub statement_rawlines {
1531 my ($stmt) = @_;
1532
1533 my @stmt_lines = ($stmt =~ /\n/g);
1534
1535 return $#stmt_lines + 2;
1536}
1537
1538sub statement_block_size {
1539 my ($stmt) = @_;
1540
1541 $stmt =~ s/(^|\n)./$1/g;
1542 $stmt =~ s/^\s*{//;
1543 $stmt =~ s/}\s*$//;
1544 $stmt =~ s/^\s*//;
1545 $stmt =~ s/\s*$//;
1546
1547 my @stmt_lines = ($stmt =~ /\n/g);
1548 my @stmt_statements = ($stmt =~ /;/g);
1549
1550 my $stmt_lines = $#stmt_lines + 2;
1551 my $stmt_statements = $#stmt_statements + 1;
1552
1553 if ($stmt_lines > $stmt_statements) {
1554 return $stmt_lines;
1555 } else {
1556 return $stmt_statements;
1557 }
1558}
1559
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001560sub ctx_statement_full {
1561 my ($linenr, $remain, $off) = @_;
1562 my ($statement, $condition, $level);
1563
1564 my (@chunks);
1565
Andy Whitcroftcf655042008-03-04 14:28:20 -08001566 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001567 ($statement, $condition, $linenr, $remain, $off, $level) =
1568 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001569 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001570 push(@chunks, [ $condition, $statement ]);
1571 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1572 return ($level, $linenr, @chunks);
1573 }
1574
1575 # Pull in the following conditional/block pairs and see if they
1576 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001577 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001578 ($statement, $condition, $linenr, $remain, $off, $level) =
1579 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001580 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001582 #print "C: push\n";
1583 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001584 }
1585
1586 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001587}
1588
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001589sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001590 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001591 my $line;
1592 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001593 my $blk = '';
1594 my @o;
1595 my @c;
1596 my @res = ();
1597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001599 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001600 for ($line = $start; $remain > 0; $line++) {
1601 next if ($rawlines[$line] =~ /^-/);
1602 $remain--;
1603
1604 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001605
1606 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001607 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001608 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001609 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001610 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001611 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001612 $level = pop(@stack);
1613 }
1614
Andy Whitcroft01464f32010-10-26 14:23:19 -07001615 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001616 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1617 if ($off > 0) {
1618 $off--;
1619 next;
1620 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001621
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001622 if ($c eq $close && $level > 0) {
1623 $level--;
1624 last if ($level == 0);
1625 } elsif ($c eq $open) {
1626 $level++;
1627 }
1628 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001629
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001630 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001631 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001632 }
1633
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001634 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001635 }
1636
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001637 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001638}
1639sub ctx_block_outer {
1640 my ($linenr, $remain) = @_;
1641
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001642 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1643 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001644}
1645sub ctx_block {
1646 my ($linenr, $remain) = @_;
1647
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001648 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1649 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001650}
1651sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001652 my ($linenr, $remain, $off) = @_;
1653
1654 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1655 return @r;
1656}
1657sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001658 my ($linenr, $remain) = @_;
1659
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001660 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001661}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001662sub ctx_statement_level {
1663 my ($linenr, $remain, $off) = @_;
1664
1665 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1666}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001667
1668sub ctx_locate_comment {
1669 my ($first_line, $end_line) = @_;
1670
1671 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001672 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001673 return $current_comment if (defined $current_comment);
1674
1675 # Look through the context and try and figure out if there is a
1676 # comment.
1677 my $in_comment = 0;
1678 $current_comment = '';
1679 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001680 my $line = $rawlines[$linenr - 1];
1681 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001682 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1683 $in_comment = 1;
1684 }
1685 if ($line =~ m@/\*@) {
1686 $in_comment = 1;
1687 }
1688 if (!$in_comment && $current_comment ne '') {
1689 $current_comment = '';
1690 }
1691 $current_comment .= $line . "\n" if ($in_comment);
1692 if ($line =~ m@\*/@) {
1693 $in_comment = 0;
1694 }
1695 }
1696
1697 chomp($current_comment);
1698 return($current_comment);
1699}
1700sub ctx_has_comment {
1701 my ($first_line, $end_line) = @_;
1702 my $cmt = ctx_locate_comment($first_line, $end_line);
1703
Andy Whitcroft00df3442007-06-08 13:47:06 -07001704 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001705 ##print "CMMT: $cmt\n";
1706
1707 return ($cmt ne '');
1708}
1709
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001710sub raw_line {
1711 my ($linenr, $cnt) = @_;
1712
1713 my $offset = $linenr - 1;
1714 $cnt++;
1715
1716 my $line;
1717 while ($cnt) {
1718 $line = $rawlines[$offset++];
1719 next if (defined($line) && $line =~ /^-/);
1720 $cnt--;
1721 }
1722
1723 return $line;
1724}
1725
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07001726sub get_stat_real {
1727 my ($linenr, $lc) = @_;
1728
1729 my $stat_real = raw_line($linenr, 0);
1730 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1731 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1732 }
1733
1734 return $stat_real;
1735}
1736
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07001737sub get_stat_here {
1738 my ($linenr, $cnt, $here) = @_;
1739
1740 my $herectx = $here . "\n";
1741 for (my $n = 0; $n < $cnt; $n++) {
1742 $herectx .= raw_line($linenr, $n) . "\n";
1743 }
1744
1745 return $herectx;
1746}
1747
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001748sub cat_vet {
1749 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001750 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001751
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001752 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001753 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1754 $res .= $1;
1755 if ($2 ne '') {
1756 $coded = sprintf("^%c", unpack('C', $2) + 64);
1757 $res .= $coded;
1758 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001759 }
1760 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001761
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001762 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001763}
1764
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001765my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001766my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001767my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001768my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001769
1770sub annotate_reset {
1771 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001772 $av_pending = '_';
1773 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001774 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001775}
1776
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001777sub annotate_values {
1778 my ($stream, $type) = @_;
1779
1780 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001781 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001782 my $cur = $stream;
1783
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001784 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001785
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001787 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001788 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001789 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001790 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001791 print "WS($1)\n" if ($dbg_values > 1);
1792 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001793 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 }
1796
Florian Micklerc023e4732011-01-12 16:59:58 -08001797 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001798 print "CAST($1)\n" if ($dbg_values > 1);
1799 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001800 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001801
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001802 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001803 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001804 $type = 'T';
1805
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001806 } elsif ($cur =~ /^($Modifier)\s*/) {
1807 print "MODIFIER($1)\n" if ($dbg_values > 1);
1808 $type = 'T';
1809
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001810 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001811 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001812 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001813 push(@av_paren_type, $type);
1814 if ($2 ne '') {
1815 $av_pending = 'N';
1816 }
1817 $type = 'E';
1818
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001819 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001820 print "UNDEF($1)\n" if ($dbg_values > 1);
1821 $av_preprocessor = 1;
1822 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001823
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001824 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001825 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001826 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001827
1828 push(@av_paren_type, $type);
1829 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001830 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001831
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001832 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1834 $av_preprocessor = 1;
1835
1836 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1837
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001838 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001839
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001840 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001841 print "PRE_END($1)\n" if ($dbg_values > 1);
1842
1843 $av_preprocessor = 1;
1844
1845 # Assume all arms of the conditional end as this
1846 # one does, and continue as if the #endif was not here.
1847 pop(@av_paren_type);
1848 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001849 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001850
1851 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001853
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001854 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1855 print "ATTR($1)\n" if ($dbg_values > 1);
1856 $av_pending = $type;
1857 $type = 'N';
1858
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001859 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001860 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001861 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001862 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001863 }
1864 $type = 'N';
1865
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001866 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001867 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001868 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 $type = 'N';
1870
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001871 } elsif ($cur =~/^(case)/o) {
1872 print "CASE($1)\n" if ($dbg_values > 1);
1873 $av_pend_colon = 'C';
1874 $type = 'N';
1875
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001876 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001877 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001878 $type = 'N';
1879
1880 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001881 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001882 push(@av_paren_type, $av_pending);
1883 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001884 $type = 'N';
1885
1886 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001887 my $new_type = pop(@av_paren_type);
1888 if ($new_type ne '_') {
1889 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890 print "PAREN('$1') -> $type\n"
1891 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001892 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001893 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001894 }
1895
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001896 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001897 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001898 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001899 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001901 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1902 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001903 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001904 } elsif ($type eq 'E') {
1905 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001906 }
1907 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1908 $type = 'V';
1909
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001910 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001911 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001912 $type = 'V';
1913
1914 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001915 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001916 $type = 'N';
1917
Andy Whitcroftcf655042008-03-04 14:28:20 -08001918 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001919 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001920 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001921 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001922
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001923 } elsif ($cur =~/^(,)/) {
1924 print "COMMA($1)\n" if ($dbg_values > 1);
1925 $type = 'C';
1926
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001927 } elsif ($cur =~ /^(\?)/o) {
1928 print "QUESTION($1)\n" if ($dbg_values > 1);
1929 $type = 'N';
1930
1931 } elsif ($cur =~ /^(:)/o) {
1932 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1933
1934 substr($var, length($res), 1, $av_pend_colon);
1935 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1936 $type = 'E';
1937 } else {
1938 $type = 'N';
1939 }
1940 $av_pend_colon = 'O';
1941
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001942 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001943 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944 $type = 'N';
1945
Andy Whitcroft0d413862008-10-15 22:02:16 -07001946 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001947 my $variant;
1948
1949 print "OPV($1)\n" if ($dbg_values > 1);
1950 if ($type eq 'V') {
1951 $variant = 'B';
1952 } else {
1953 $variant = 'U';
1954 }
1955
1956 substr($var, length($res), 1, $variant);
1957 $type = 'N';
1958
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001959 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001960 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001961 if ($1 ne '++' && $1 ne '--') {
1962 $type = 'N';
1963 }
1964
1965 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001966 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001967 }
1968 if (defined $1) {
1969 $cur = substr($cur, length($1));
1970 $res .= $type x length($1);
1971 }
1972 }
1973
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001974 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001975}
1976
Andy Whitcroft8905a672007-11-28 16:21:06 -08001977sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001978 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001979 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001980 ^(?:
1981 $Modifier|
1982 $Storage|
1983 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001984 DEFINE_\S+
1985 )$|
1986 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001987 goto|
1988 return|
1989 case|
1990 else|
1991 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001992 do|
1993 \#|
1994 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001995 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001996 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001997 )}x;
1998 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1999 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002000 # Check for modifiers.
2001 $possible =~ s/\s*$Storage\s*//g;
2002 $possible =~ s/\s*$Sparse\s*//g;
2003 if ($possible =~ /^\s*$/) {
2004
2005 } elsif ($possible =~ /\s/) {
2006 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07002007 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002008 if ($modifier !~ $notPermitted) {
2009 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002010 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07002011 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07002012 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002013
2014 } else {
2015 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07002016 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002017 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002018 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07002019 } else {
2020 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002021 }
2022}
2023
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024my $prefix = '';
2025
Joe Perches000d1cc12011-07-25 17:13:25 -07002026sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07002027 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07002028
Alexey Dobriyan522b8372017-02-27 14:30:05 -08002029 $type =~ tr/[a-z]/[A-Z]/;
2030
Joe Perchescbec18a2014-04-03 14:49:19 -07002031 return defined $use_type{$type} if (scalar keys %use_type > 0);
2032
2033 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07002034}
2035
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002036sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07002037 my ($level, $type, $msg) = @_;
2038
2039 if (!show_type($type) ||
2040 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002041 return 0;
2042 }
Joe Perches57230292015-06-25 15:03:03 -07002043 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07002044 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07002045 if ($level eq 'ERROR') {
2046 $output .= RED;
2047 } elsif ($level eq 'WARNING') {
2048 $output .= YELLOW;
2049 } else {
2050 $output .= GREEN;
2051 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002052 }
Joe Perches57230292015-06-25 15:03:03 -07002053 $output .= $prefix . $level . ':';
2054 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07002055 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002056 $output .= "$type:";
2057 }
John Brooks737c0762017-07-10 15:52:24 -07002058 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07002059 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07002060
2061 if ($showfile) {
2062 my @lines = split("\n", $output, -1);
2063 splice(@lines, 1, 1);
2064 $output = join("\n", @lines);
2065 }
Joe Perches57230292015-06-25 15:03:03 -07002066 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002067
Joe Perches57230292015-06-25 15:03:03 -07002068 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002069
2070 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002071}
Joe Perchescbec18a2014-04-03 14:49:19 -07002072
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002073sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002074 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002075}
Joe Perches000d1cc12011-07-25 17:13:25 -07002076
Joe Perchesd752fcc2014-08-06 16:11:05 -07002077sub fixup_current_range {
2078 my ($lineRef, $offset, $length) = @_;
2079
2080 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2081 my $o = $1;
2082 my $l = $2;
2083 my $no = $o + $offset;
2084 my $nl = $l + $length;
2085 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2086 }
2087}
2088
2089sub fix_inserted_deleted_lines {
2090 my ($linesRef, $insertedRef, $deletedRef) = @_;
2091
2092 my $range_last_linenr = 0;
2093 my $delta_offset = 0;
2094
2095 my $old_linenr = 0;
2096 my $new_linenr = 0;
2097
2098 my $next_insert = 0;
2099 my $next_delete = 0;
2100
2101 my @lines = ();
2102
2103 my $inserted = @{$insertedRef}[$next_insert++];
2104 my $deleted = @{$deletedRef}[$next_delete++];
2105
2106 foreach my $old_line (@{$linesRef}) {
2107 my $save_line = 1;
2108 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07002109 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07002110 $delta_offset = 0;
2111 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2112 $range_last_linenr = $new_linenr;
2113 fixup_current_range(\$line, $delta_offset, 0);
2114 }
2115
2116 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2117 $deleted = @{$deletedRef}[$next_delete++];
2118 $save_line = 0;
2119 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2120 }
2121
2122 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2123 push(@lines, ${$inserted}{'LINE'});
2124 $inserted = @{$insertedRef}[$next_insert++];
2125 $new_linenr++;
2126 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2127 }
2128
2129 if ($save_line) {
2130 push(@lines, $line);
2131 $new_linenr++;
2132 }
2133
2134 $old_linenr++;
2135 }
2136
2137 return @lines;
2138}
2139
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002140sub fix_insert_line {
2141 my ($linenr, $line) = @_;
2142
2143 my $inserted = {
2144 LINENR => $linenr,
2145 LINE => $line,
2146 };
2147 push(@fixed_inserted, $inserted);
2148}
2149
2150sub fix_delete_line {
2151 my ($linenr, $line) = @_;
2152
2153 my $deleted = {
2154 LINENR => $linenr,
2155 LINE => $line,
2156 };
2157
2158 push(@fixed_deleted, $deleted);
2159}
2160
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002161sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002162 my ($type, $msg) = @_;
2163
2164 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002165 our $clean = 0;
2166 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002167 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002168 }
Joe Perches3705ce52013-07-03 15:05:31 -07002169 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002170}
2171sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002172 my ($type, $msg) = @_;
2173
2174 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002175 our $clean = 0;
2176 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002177 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002178 }
Joe Perches3705ce52013-07-03 15:05:31 -07002179 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002180}
2181sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002182 my ($type, $msg) = @_;
2183
2184 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002185 our $clean = 0;
2186 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002187 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 }
Joe Perches3705ce52013-07-03 15:05:31 -07002189 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002190}
2191
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002192sub check_absolute_file {
2193 my ($absolute, $herecurr) = @_;
2194 my $file = $absolute;
2195
2196 ##print "absolute<$absolute>\n";
2197
2198 # See if any suffix of this path is a path within the tree.
2199 while ($file =~ s@^[^/]*/@@) {
2200 if (-f "$root/$file") {
2201 ##print "file<$file>\n";
2202 last;
2203 }
2204 }
2205 if (! -f _) {
2206 return 0;
2207 }
2208
2209 # It is, so see if the prefix is acceptable.
2210 my $prefix = $absolute;
2211 substr($prefix, -length($file)) = '';
2212
2213 ##print "prefix<$prefix>\n";
2214 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002215 WARN("USE_RELATIVE_PATH",
2216 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002217 }
2218}
2219
Joe Perches3705ce52013-07-03 15:05:31 -07002220sub trim {
2221 my ($string) = @_;
2222
Joe Perchesb34c6482013-09-11 14:24:01 -07002223 $string =~ s/^\s+|\s+$//g;
2224
2225 return $string;
2226}
2227
2228sub ltrim {
2229 my ($string) = @_;
2230
2231 $string =~ s/^\s+//;
2232
2233 return $string;
2234}
2235
2236sub rtrim {
2237 my ($string) = @_;
2238
2239 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002240
2241 return $string;
2242}
2243
Joe Perches52ea8502013-11-12 15:10:09 -08002244sub string_find_replace {
2245 my ($string, $find, $replace) = @_;
2246
2247 $string =~ s/$find/$replace/g;
2248
2249 return $string;
2250}
2251
Joe Perches3705ce52013-07-03 15:05:31 -07002252sub tabify {
2253 my ($leading) = @_;
2254
2255 my $source_indent = 8;
2256 my $max_spaces_before_tab = $source_indent - 1;
2257 my $spaces_to_tab = " " x $source_indent;
2258
2259 #convert leading spaces to tabs
2260 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2261 #Remove spaces before a tab
2262 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2263
2264 return "$leading";
2265}
2266
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002267sub pos_last_openparen {
2268 my ($line) = @_;
2269
2270 my $pos = 0;
2271
2272 my $opens = $line =~ tr/\(/\(/;
2273 my $closes = $line =~ tr/\)/\)/;
2274
2275 my $last_openparen = 0;
2276
2277 if (($opens == 0) || ($closes >= $opens)) {
2278 return -1;
2279 }
2280
2281 my $len = length($line);
2282
2283 for ($pos = 0; $pos < $len; $pos++) {
2284 my $string = substr($line, $pos);
2285 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2286 $pos += length($1) - 1;
2287 } elsif (substr($line, $pos, 1) eq '(') {
2288 $last_openparen = $pos;
2289 } elsif (index($string, '(') == -1) {
2290 last;
2291 }
2292 }
2293
Joe Perches91cb5192014-04-03 14:49:32 -07002294 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002295}
2296
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002297sub get_raw_comment {
2298 my ($line, $rawline) = @_;
2299 my $comment = '';
2300
2301 for my $i (0 .. (length($line) - 1)) {
2302 if (substr($line, $i, 1) eq "$;") {
2303 $comment .= substr($rawline, $i, 1);
2304 }
2305 }
2306
2307 return $comment;
2308}
2309
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002310sub process {
2311 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002312
2313 my $linenr=0;
2314 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002315 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002316 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002317 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002318
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002319 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002320 my $indent;
2321 my $previndent=0;
2322 my $stashindent=0;
2323
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002324 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002325 my $signoff = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002326 my $author = '';
2327 my $authorsignoff = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002328 my $is_patch = 0;
Rob Herring133712a2018-08-21 21:58:16 -07002329 my $is_binding_patch = -1;
Joe Perches29ee1b02014-08-06 16:10:35 -07002330 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002331 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002332 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches490b2922018-08-21 21:58:01 -07002333 my $commit_log_lines = 0; #Number of commit log lines
Joe Perches77cb8542017-02-24 15:01:28 -08002334 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002335 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002336 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002337 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002338 my $non_utf8_charset = 0;
2339
Joe Perches365dd4e2014-08-06 16:10:42 -07002340 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002341 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002342
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002343 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002344 our $cnt_lines = 0;
2345 our $cnt_error = 0;
2346 our $cnt_warn = 0;
2347 our $cnt_chk = 0;
2348
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002349 # Trace the real file/line as we go.
2350 my $realfile = '';
2351 my $realline = 0;
2352 my $realcnt = 0;
2353 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002354 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002355 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002356 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002357 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002358 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002359
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002360 my $prev_values = 'E';
2361
2362 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002364 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002365 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002366 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002367
Joe Perches7e51f192013-09-11 14:23:57 -07002368 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002369
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002370 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002371 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002372 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002373 my @setup_docs = ();
2374 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002375
Joe Perchesd8b07712013-11-12 15:10:06 -08002376 my $camelcase_file_seeded = 0;
2377
Rob Herring9f3a8992018-04-10 16:33:13 -07002378 my $checklicenseline = 1;
2379
Andy Whitcroft773647a2008-03-28 14:15:58 -07002380 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002381 my $line;
2382 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002383 $linenr++;
2384 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002385
Joe Perches3705ce52013-07-03 15:05:31 -07002386 push(@fixed, $rawline) if ($fix);
2387
Andy Whitcroft773647a2008-03-28 14:15:58 -07002388 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002389 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002390 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002391 $setup_docs = 1;
2392 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002393 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002394 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002395 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002396 $realline=$1-1;
2397 if (defined $2) {
2398 $realcnt=$3+1;
2399 } else {
2400 $realcnt=1+1;
2401 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002402 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002403
2404 # Guestimate if this is a continuing comment. Run
2405 # the context looking for a comment "edge". If this
2406 # edge is a close comment then we must be in a comment
2407 # at context start.
2408 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002409 my $cnt = $realcnt;
2410 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2411 next if (defined $rawlines[$ln - 1] &&
2412 $rawlines[$ln - 1] =~ /^-/);
2413 $cnt--;
2414 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002415 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002416 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2417 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2418 ($edge) = $1;
2419 last;
2420 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002421 }
2422 if (defined $edge && $edge eq '*/') {
2423 $in_comment = 1;
2424 }
2425
2426 # Guestimate if this is a continuing comment. If this
2427 # is the start of a diff block and this line starts
2428 # ' *' then it is very likely a comment.
2429 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002430 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002431 {
2432 $in_comment = 1;
2433 }
2434
2435 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2436 sanitise_line_reset($in_comment);
2437
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002438 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002439 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002440 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002441 $line = sanitise_line($rawline);
2442 }
2443 push(@lines, $line);
2444
2445 if ($realcnt > 1) {
2446 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2447 } else {
2448 $realcnt = 0;
2449 }
2450
2451 #print "==>$rawline\n";
2452 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002453
2454 if ($setup_docs && $line =~ /^\+/) {
2455 push(@setup_docs, $line);
2456 }
2457 }
2458
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002459 $prefix = '';
2460
Andy Whitcroft773647a2008-03-28 14:15:58 -07002461 $realcnt = 0;
2462 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002463 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002464 foreach my $line (@lines) {
2465 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002466 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002467 my $sline = $line; #copy of $line
2468 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002469
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002470 my $rawline = $rawlines[$linenr - 1];
Joe Perchesf36d3eb2020-04-06 20:10:58 -07002471 my $raw_comment = get_raw_comment($line, $rawline);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002472
Joe Perches12c253a2018-06-07 17:10:58 -07002473# check if it's a mode change, rename or start of a patch
2474 if (!$in_commit_log &&
2475 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2476 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2477 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2478 $is_patch = 1;
2479 }
2480
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002481#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002482 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002483 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2484 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002485 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002486 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002487 $realline=$1-1;
2488 if (defined $2) {
2489 $realcnt=$3+1;
2490 } else {
2491 $realcnt=1+1;
2492 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002493 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002494 $prev_values = 'E';
2495
Andy Whitcroft773647a2008-03-28 14:15:58 -07002496 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002497 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002498 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002499 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002500 if ($context =~ /\b(\w+)\s*\(/) {
2501 $context_function = $1;
2502 } else {
2503 undef $context_function;
2504 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002505 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002506
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002507# track the line number as we move through the hunk, note that
2508# new versions of GNU diff omit the leading space on completely
2509# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002510 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002511 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002512 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002513
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002514 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002515 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002516
2517 # Track the previous line.
2518 ($prevline, $stashline) = ($stashline, $line);
2519 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002520 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2521
Andy Whitcroft773647a2008-03-28 14:15:58 -07002522 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002523
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002524 } elsif ($realcnt == 1) {
2525 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002526 }
2527
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002528 my $hunk_line = ($realcnt != 0);
2529
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002530 $here = "#$linenr: " if (!$file);
2531 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002532
Joe Perches2ac73b42014-06-04 16:12:05 -07002533 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002534 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002535 if ($line =~ /^diff --git.*?(\S+)$/) {
2536 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002537 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002538 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002539 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002540 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002541 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002542 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002543 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002544
2545 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002546 if (!$file && $tree && $p1_prefix ne '' &&
2547 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002548 WARN("PATCH_PREFIX",
2549 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002550 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002551
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002552 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002553 ERROR("MODIFIED_INCLUDE_ASM",
2554 "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 -07002555 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002556 $found_file = 1;
2557 }
2558
Joe Perches34d88152015-06-25 15:03:05 -07002559#make up the handle for any error we report on this line
2560 if ($showfile) {
2561 $prefix = "$realfile:$realline: "
2562 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002563 if ($file) {
2564 $prefix = "$filename:$realline: ";
2565 } else {
2566 $prefix = "$filename:$linenr: ";
2567 }
Joe Perches34d88152015-06-25 15:03:05 -07002568 }
2569
Joe Perches2ac73b42014-06-04 16:12:05 -07002570 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002571 if (is_maintained_obsolete($realfile)) {
2572 WARN("OBSOLETE",
2573 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2574 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002575 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002576 $check = 1;
2577 } else {
2578 $check = $check_orig;
2579 }
Rob Herring9f3a8992018-04-10 16:33:13 -07002580 $checklicenseline = 1;
Rob Herring133712a2018-08-21 21:58:16 -07002581
2582 if ($realfile !~ /^MAINTAINERS/) {
2583 my $last_binding_patch = $is_binding_patch;
2584
2585 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2586
2587 if (($last_binding_patch != -1) &&
2588 ($last_binding_patch ^ $is_binding_patch)) {
2589 WARN("DT_SPLIT_BINDING_PATCH",
2590 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2591 }
2592 }
2593
Andy Whitcroft773647a2008-03-28 14:15:58 -07002594 next;
2595 }
2596
Randy Dunlap389834b2007-06-08 13:47:03 -07002597 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002598
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002599 my $hereline = "$here\n$rawline\n";
2600 my $herecurr = "$here\n$rawline\n";
2601 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002602
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002603 $cnt_lines++ if ($realcnt != 0);
2604
Joe Perches490b2922018-08-21 21:58:01 -07002605# Verify the existence of a commit log if appropriate
2606# 2 is used because a $signature is counted in $commit_log_lines
2607 if ($in_commit_log) {
2608 if ($line !~ /^\s*$/) {
2609 $commit_log_lines++; #could be a $signature
2610 }
2611 } elsif ($has_commit_log && $commit_log_lines < 2) {
2612 WARN("COMMIT_MESSAGE",
2613 "Missing commit description - Add an appropriate one\n");
2614 $commit_log_lines = 2; #warn only once
2615 }
2616
Joe Perchese518e9a2015-06-25 15:03:27 -07002617# Check if the commit log has what seems like a diff which can confuse patch
2618 if ($in_commit_log && !$commit_log_has_diff &&
2619 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2620 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2621 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2622 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2623 ERROR("DIFF_IN_COMMIT_MSG",
2624 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2625 $commit_log_has_diff = 1;
2626 }
2627
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002628# Check for incorrect file permissions
2629 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2630 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002631 if ($realfile !~ m@scripts/@ &&
2632 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002633 ERROR("EXECUTE_PERMISSIONS",
2634 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002635 }
2636 }
2637
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002638# Check the patch for a From:
2639 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2640 $author = $1;
2641 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2642 $author =~ s/"//g;
Joe Perchesdfa05c22020-04-06 20:10:48 -07002643 $author = reformat_email($author);
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002644 }
2645
Joe Perches20112472011-07-25 17:13:23 -07002646# Check the patch for a signoff:
Joe Perchesdfa05c22020-04-06 20:10:48 -07002647 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002648 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002649 $in_commit_log = 0;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002650 if ($author ne '') {
Joe Perchesdfa05c22020-04-06 20:10:48 -07002651 if (same_email_addresses($1, $author)) {
2652 $authorsignoff = 1;
Geert Uytterhoevencd261492018-08-21 21:57:40 -07002653 }
2654 }
Joe Perches20112472011-07-25 17:13:23 -07002655 }
2656
Joe Perchese0d975b2014-12-10 15:51:49 -08002657# Check if MAINTAINERS is being updated. If so, there's probably no need to
2658# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2659 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2660 $reported_maintainer_file = 1;
2661 }
2662
Joe Perches20112472011-07-25 17:13:23 -07002663# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002664 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002665 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002666 my $space_before = $1;
2667 my $sign_off = $2;
2668 my $space_after = $3;
2669 my $email = $4;
2670 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2671
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002672 if ($sign_off !~ /$signature_tags/) {
2673 WARN("BAD_SIGN_OFF",
2674 "Non-standard signature: $sign_off\n" . $herecurr);
2675 }
Joe Perches20112472011-07-25 17:13:23 -07002676 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002677 if (WARN("BAD_SIGN_OFF",
2678 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2679 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002680 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002681 "$ucfirst_sign_off $email";
2682 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002683 }
Joe Perches20112472011-07-25 17:13:23 -07002684 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002685 if (WARN("BAD_SIGN_OFF",
2686 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2687 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002688 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002689 "$ucfirst_sign_off $email";
2690 }
2691
Joe Perches20112472011-07-25 17:13:23 -07002692 }
2693 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002694 if (WARN("BAD_SIGN_OFF",
2695 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2696 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002697 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002698 "$ucfirst_sign_off $email";
2699 }
Joe Perches20112472011-07-25 17:13:23 -07002700 }
2701
Joe Perchesdfa05c22020-04-06 20:10:48 -07002702 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Perches20112472011-07-25 17:13:23 -07002703 my $suggested_email = format_email(($email_name, $email_address));
2704 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002705 ERROR("BAD_SIGN_OFF",
2706 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002707 } else {
2708 my $dequoted = $suggested_email;
2709 $dequoted =~ s/^"//;
2710 $dequoted =~ s/" </ </;
2711 # Don't force email to have quotes
2712 # Allow just an angle bracketed address
Joe Perchesdfa05c22020-04-06 20:10:48 -07002713 if (!same_email_addresses($email, $suggested_email)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002714 WARN("BAD_SIGN_OFF",
2715 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002716 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002717 }
Joe Perches7e51f192013-09-11 14:23:57 -07002718
2719# Check for duplicate signatures
2720 my $sig_nospace = $line;
2721 $sig_nospace =~ s/\s//g;
2722 $sig_nospace = lc($sig_nospace);
2723 if (defined $signatures{$sig_nospace}) {
2724 WARN("BAD_SIGN_OFF",
2725 "Duplicate signature\n" . $herecurr);
2726 } else {
2727 $signatures{$sig_nospace} = 1;
2728 }
Sean Christopherson6c5d24e2019-03-22 14:11:37 -07002729
2730# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2731 if ($sign_off =~ /^co-developed-by:$/i) {
2732 if ($email eq $author) {
2733 WARN("BAD_SIGN_OFF",
2734 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2735 }
2736 if (!defined $lines[$linenr]) {
2737 WARN("BAD_SIGN_OFF",
2738 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2739 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2740 WARN("BAD_SIGN_OFF",
2741 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2742 } elsif ($1 ne $email) {
2743 WARN("BAD_SIGN_OFF",
2744 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2745 }
2746 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002747 }
2748
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002749# Check email subject for common tools that don't need to be mentioned
2750 if ($in_header_lines &&
2751 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2752 WARN("EMAIL_SUBJECT",
2753 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2754 }
2755
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002756# Check for unwanted Gerrit info
2757 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2758 ERROR("GERRIT_CHANGE_ID",
2759 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2760 }
2761
Joe Perches369c8dd2015-11-06 16:31:34 -08002762# Check if the commit log is in a possible stack dump
2763 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2764 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2765 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2766 # timestamp
Joe Perches634cffc2019-09-25 16:46:32 -07002767 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2768 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2769 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2770 # stack dump address styles
Joe Perches369c8dd2015-11-06 16:31:34 -08002771 $commit_log_possible_stack_dump = 1;
2772 }
2773
Joe Perches2a076f42015-04-16 12:44:28 -07002774# Check for line lengths > 75 in commit log, warn once
2775 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002776 length($line) > 75 &&
2777 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2778 # file delta changes
2779 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2780 # filename then :
2781 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2782 # A Fixes: or Link: line
2783 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002784 WARN("COMMIT_LOG_LONG_LINE",
2785 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2786 $commit_log_long_line = 1;
2787 }
2788
Joe Perchesbf4daf12015-09-09 15:37:50 -07002789# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002790 if ($in_commit_log && $commit_log_possible_stack_dump &&
2791 $line =~ /^\s*$/) {
2792 $commit_log_possible_stack_dump = 0;
2793 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002794
Joe Perches0d7835f2015-02-13 14:38:35 -08002795# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002796 if ($in_commit_log && !$commit_log_possible_stack_dump &&
John Hubbarda8972572020-04-06 20:10:55 -07002797 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002798 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002799 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002800 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002801 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2802 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002803 my $init_char = "c";
2804 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002805 my $short = 1;
2806 my $long = 0;
2807 my $case = 1;
2808 my $space = 1;
2809 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002810 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002811 my $id = '0123456789ab';
2812 my $orig_desc = "commit description";
2813 my $description = "";
2814
Joe Perchesfe043ea2015-09-09 15:37:25 -07002815 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2816 $init_char = $1;
2817 $orig_commit = lc($2);
2818 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2819 $orig_commit = lc($1);
2820 }
2821
Joe Perches0d7835f2015-02-13 14:38:35 -08002822 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2823 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2824 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2825 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2826 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2827 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002828 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002829 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2830 defined $rawlines[$linenr] &&
2831 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2832 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002833 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002834 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2835 defined $rawlines[$linenr] &&
2836 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2837 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2838 $orig_desc = $1;
2839 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2840 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002841 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002842 }
2843
2844 ($id, $description) = git_commit_info($orig_commit,
2845 $id, $orig_desc);
2846
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002847 if (defined($id) &&
2848 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002849 ERROR("GIT_COMMIT_ID",
2850 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2851 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002852 }
2853
Joe Perches13f19372014-08-06 16:10:59 -07002854# Check for added, moved or deleted files
2855 if (!$reported_maintainer_file && !$in_commit_log &&
2856 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2857 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2858 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2859 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002860 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002861 $reported_maintainer_file = 1;
2862 WARN("FILE_PATH_CHANGES",
2863 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2864 }
2865
Rob Herringe400edb2019-09-12 15:18:20 +01002866# Check for adding new DT bindings not in schema format
2867 if (!$in_commit_log &&
2868 ($line =~ /^new file mode\s*\d+\s*$/) &&
2869 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2870 WARN("DT_SCHEMA_BINDING_PATCH",
2871 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2872 }
2873
Andy Whitcroft00df3442007-06-08 13:47:06 -07002874# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002875 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002876 ERROR("CORRUPTED_PATCH",
2877 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002878 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002879 }
2880
2881# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2882 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002883 $rawline !~ m/^$UTF8*$/) {
2884 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2885
2886 my $blank = copy_spacing($rawline);
2887 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2888 my $hereptr = "$hereline$ptr\n";
2889
Joe Perches34d99212011-07-25 17:13:26 -07002890 CHK("INVALID_UTF8",
2891 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002892 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002893
Joe Perches15662b32011-10-31 17:13:12 -07002894# Check if it's the start of a commit log
2895# (not a header line and we haven't seen the patch filename)
2896 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002897 !($rawline =~ /^\s+(?:\S|$)/ ||
2898 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002899 $in_header_lines = 0;
2900 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002901 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002902 }
2903
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002904# Check if there is UTF-8 in a commit log when a mail header has explicitly
2905# declined it, i.e defined some charset where it is missing.
2906 if ($in_header_lines &&
2907 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2908 $1 !~ /utf-8/i) {
2909 $non_utf8_charset = 1;
2910 }
2911
2912 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002913 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002914 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002915 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2916 }
2917
Joe Perchesd6430f72016-12-12 16:46:28 -08002918# Check for absolute kernel paths in commit message
2919 if ($tree && $in_commit_log) {
2920 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2921 my $file = $1;
2922
2923 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2924 check_absolute_file($1, $herecurr)) {
2925 #
2926 } else {
2927 check_absolute_file($file, $herecurr);
2928 }
2929 }
2930 }
2931
Kees Cook66b47b42014-10-13 15:51:57 -07002932# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002933 if (defined($misspellings) &&
2934 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002935 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002936 my $typo = $1;
2937 my $typo_fix = $spelling_fix{lc($typo)};
2938 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2939 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Jean Delvare0675a8f2017-09-08 16:16:07 -07002940 my $msg_level = \&WARN;
2941 $msg_level = \&CHK if ($file);
2942 if (&{$msg_level}("TYPO_SPELLING",
2943 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Kees Cook66b47b42014-10-13 15:51:57 -07002944 $fix) {
2945 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2946 }
2947 }
2948 }
2949
Matteo Crocea8dd86b2019-09-25 16:46:38 -07002950# check for invalid commit id
2951 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2952 my $id;
2953 my $description;
2954 ($id, $description) = git_commit_info($2, undef, undef);
2955 if (!defined($id)) {
2956 WARN("UNKNOWN_COMMIT_ID",
2957 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2958 }
2959 }
2960
Andy Whitcroft306708542008-10-15 22:02:28 -07002961# ignore non-hunk lines and lines being removed
2962 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002963
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002964#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002965 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002966 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002967 if (ERROR("DOS_LINE_ENDINGS",
2968 "DOS line endings\n" . $herevet) &&
2969 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002970 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002971 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002972 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2973 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002974 if (ERROR("TRAILING_WHITESPACE",
2975 "trailing whitespace\n" . $herevet) &&
2976 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002977 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002978 }
2979
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002980 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002981 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002982
Josh Triplett4783f892013-11-12 15:10:12 -08002983# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002984 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002985 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002986 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2987 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002988 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Jean Delvare0675a8f2017-09-08 16:16:07 -07002989 my $msg_level = \&ERROR;
2990 $msg_level = \&CHK if ($file);
2991 &{$msg_level}("FSF_MAILING_ADDRESS",
2992 "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 -08002993 }
2994
Andi Kleen33549572010-05-24 14:33:29 -07002995# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002996# Only applies when adding the entry originally, after that we do not have
2997# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002998 if ($realfile =~ /Kconfig/ &&
Ulf Magnusson678ae162018-02-16 21:22:54 +01002999 # 'choice' is usually the last thing on the line (though
3000 # Kconfig supports named choices), so use a word boundary
3001 # (\b) rather than a whitespace character (\s)
3002 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Andi Kleen33549572010-05-24 14:33:29 -07003003 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003004 my $cnt = $realcnt;
3005 my $ln = $linenr + 1;
3006 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003007 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003008 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08003009 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003010 $f = $lines[$ln - 1];
3011 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3012 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003013
3014 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07003015 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08003016
Ulf Magnusson86adf1a2018-02-16 21:22:53 +01003017 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08003018 $is_start = 1;
Ulf Magnusson84af7a62018-02-16 21:22:55 +01003019 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3020 if ($lines[$ln - 1] =~ "---help---") {
3021 WARN("CONFIG_DESCRIPTION",
3022 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3023 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003024 $length = -1;
3025 }
3026
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003027 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07003028 $f =~ s/#.*//;
3029 $f =~ s/^\s+//;
3030 next if ($f =~ /^$/);
Ulf Magnusson678ae162018-02-16 21:22:54 +01003031
3032 # This only checks context lines in the patch
3033 # and so hopefully shouldn't trigger false
3034 # positives, even though some of these are
3035 # common words in help texts
3036 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3037 if|endif|menu|endmenu|source)\b/x) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07003038 $is_end = 1;
3039 last;
3040 }
Andi Kleen33549572010-05-24 14:33:29 -07003041 $length++;
3042 }
Vadim Bendebury56193272014-10-13 15:51:48 -07003043 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3044 WARN("CONFIG_DESCRIPTION",
3045 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3046 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08003047 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07003048 }
3049
Joe Perches628f91a2017-07-10 15:52:07 -07003050# check for MAINTAINERS entries that don't have the right form
3051 if ($realfile =~ /^MAINTAINERS$/ &&
3052 $rawline =~ /^\+[A-Z]:/ &&
3053 $rawline !~ /^\+[A-Z]:\t\S/) {
3054 if (WARN("MAINTAINERS_STYLE",
3055 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3056 $fix) {
3057 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3058 }
3059 }
3060
Christoph Jaeger327953e2015-02-13 14:38:29 -08003061# discourage the use of boolean for type definition attributes of Kconfig options
3062 if ($realfile =~ /Kconfig/ &&
3063 $line =~ /^\+\s*\bboolean\b/) {
3064 WARN("CONFIG_TYPE_BOOLEAN",
3065 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3066 }
3067
Arnaud Lacombec68e5872011-08-15 01:07:14 -04003068 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3069 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3070 my $flag = $1;
3071 my $replacement = {
3072 'EXTRA_AFLAGS' => 'asflags-y',
3073 'EXTRA_CFLAGS' => 'ccflags-y',
3074 'EXTRA_CPPFLAGS' => 'cppflags-y',
3075 'EXTRA_LDFLAGS' => 'ldflags-y',
3076 };
3077
3078 WARN("DEPRECATED_VARIABLE",
3079 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3080 }
3081
Rob Herringbff5da42014-01-23 15:54:51 -08003082# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07003083 if (defined $root &&
3084 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3085 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3086
Rob Herringbff5da42014-01-23 15:54:51 -08003087 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3088
Florian Vaussardcc933192014-04-03 14:49:27 -07003089 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Rob Herring852d0952019-05-22 09:55:34 -05003090 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Florian Vaussardcc933192014-04-03 14:49:27 -07003091
Rob Herringbff5da42014-01-23 15:54:51 -08003092 foreach my $compat (@compats) {
3093 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07003094 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3095 my $compat3 = $compat;
3096 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3097 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08003098 if ( $? >> 8 ) {
3099 WARN("UNDOCUMENTED_DT_STRING",
3100 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3101 }
3102
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07003103 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3104 my $vendor = $1;
Rob Herring852d0952019-05-22 09:55:34 -05003105 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08003106 if ( $? >> 8 ) {
3107 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07003108 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08003109 }
3110 }
3111 }
3112
Rob Herring9f3a8992018-04-10 16:33:13 -07003113# check for using SPDX license tag at beginning of files
3114 if ($realline == $checklicenseline) {
3115 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3116 $checklicenseline = 2;
3117 } elsif ($rawline =~ /^\+/) {
3118 my $comment = "";
3119 if ($realfile =~ /\.(h|s|S)$/) {
3120 $comment = '/*';
3121 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3122 $comment = '//';
Lubomir Rintelc8df0ab2020-04-06 20:10:51 -07003123 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Rob Herring9f3a8992018-04-10 16:33:13 -07003124 $comment = '#';
3125 } elsif ($realfile =~ /\.rst$/) {
3126 $comment = '..';
3127 }
3128
Joe Perchesfdf13692019-03-07 16:28:32 -08003129# check SPDX comment style for .[chsS] files
3130 if ($realfile =~ /\.[chsS]$/ &&
3131 $rawline =~ /SPDX-License-Identifier:/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003132 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
Joe Perchesfdf13692019-03-07 16:28:32 -08003133 WARN("SPDX_LICENSE_TAG",
3134 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3135 }
3136
Rob Herring9f3a8992018-04-10 16:33:13 -07003137 if ($comment !~ /^$/ &&
Joe Perchesffbce892019-09-25 16:46:35 -07003138 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3139 WARN("SPDX_LICENSE_TAG",
3140 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Joe Perches3b6e8ac2018-08-21 21:57:47 -07003141 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Joe Perchesffbce892019-09-25 16:46:35 -07003142 my $spdx_license = $1;
3143 if (!is_SPDX_License_valid($spdx_license)) {
3144 WARN("SPDX_LICENSE_TAG",
3145 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3146 }
Rob Herring9f3a8992018-04-10 16:33:13 -07003147 }
3148 }
3149 }
3150
Andy Whitcroft5368df202008-10-15 22:02:27 -07003151# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08003152 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07003153
Joe Perchesa8da38a2019-03-07 16:28:42 -08003154# check for using SPDX-License-Identifier on the wrong line number
3155 if ($realline != $checklicenseline &&
3156 $rawline =~ /\bSPDX-License-Identifier:/ &&
3157 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3158 WARN("SPDX_LICENSE_TAG",
3159 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3160 }
3161
Joe Perches47e0c882015-06-25 15:02:57 -07003162# line length limit (with some exclusions)
3163#
3164# There are a few types of lines that may extend beyond $max_line_length:
3165# logging functions like pr_info that end in a string
3166# lines with a single string
3167# #defines that are a single string
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003168# lines with an RFC3986 like URL
Joe Perches47e0c882015-06-25 15:02:57 -07003169#
3170# There are 3 different line length message types:
Jean Delvareab1ecab2017-09-08 16:16:04 -07003171# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Joe Perches47e0c882015-06-25 15:02:57 -07003172# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3173# LONG_LINE all other lines longer than $max_line_length
3174#
3175# if LONG_LINE is ignored, the other 2 types are also ignored
3176#
3177
Joe Perchesb4749e92015-07-17 16:24:01 -07003178 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07003179 my $msg_type = "LONG_LINE";
3180
3181 # Check the allowed long line types first
3182
3183 # logging functions that end in a string that starts
3184 # before $max_line_length
3185 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3186 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3187 $msg_type = "";
3188
3189 # lines with only strings (w/ possible termination)
3190 # #defines with only strings
3191 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3192 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3193 $msg_type = "";
3194
Joe Perchescc147502017-11-17 15:28:44 -08003195 # More special cases
3196 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3197 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
Joe Perchesd560a5f2016-08-02 14:04:31 -07003198 $msg_type = "";
3199
Andreas Brauchli2e4bbbc2018-02-06 15:38:45 -08003200 # URL ($rawline is used in case the URL is in a comment)
3201 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3202 $msg_type = "";
3203
Joe Perches47e0c882015-06-25 15:02:57 -07003204 # Otherwise set the alternate message types
3205
3206 # a comment starts before $max_line_length
3207 } elsif ($line =~ /($;[\s$;]*)$/ &&
3208 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3209 $msg_type = "LONG_LINE_COMMENT"
3210
3211 # a quoted string starts before $max_line_length
3212 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3213 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3214 $msg_type = "LONG_LINE_STRING"
3215 }
3216
3217 if ($msg_type ne "" &&
3218 (show_type("LONG_LINE") || show_type($msg_type))) {
3219 WARN($msg_type,
3220 "line over $max_line_length characters\n" . $herecurr);
3221 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003222 }
3223
Andy Whitcroft8905a672007-11-28 16:21:06 -08003224# check for adding lines without a newline.
3225 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003226 WARN("MISSING_EOF_NEWLINE",
3227 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003228 }
3229
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003230# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003231 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003232
3233# at the beginning of a line any tabs must come first and anything
3234# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003235 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3236 $rawline =~ /^\+\s* \s*/) {
3237 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003238 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003239 if (ERROR("CODE_INDENT",
3240 "code indent should use tabs where possible\n" . $herevet) &&
3241 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003242 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003243 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003244 }
3245
Alberto Panizzo08e44362010-03-05 13:43:54 -08003246# check for space before tabs.
3247 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3248 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003249 if (WARN("SPACE_BEFORE_TAB",
3250 "please, no space before tabs\n" . $herevet) &&
3251 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003252 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003253 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003254 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003255 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003256 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003257 }
3258
Joe Perches6a487212018-04-10 16:34:04 -07003259# check for assignments on the start of a line
3260 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3261 CHK("ASSIGNMENT_CONTINUATIONS",
3262 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3263 }
3264
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003265# check for && or || at the start of a line
3266 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3267 CHK("LOGICAL_CONTINUATIONS",
3268 "Logical continuations should be on the previous line\n" . $hereprev);
3269 }
3270
Joe Perchesa91e8992016-05-20 17:04:05 -07003271# check indentation starts on a tab stop
Joe Perches5b579802018-08-21 21:57:33 -07003272 if ($perl_version_ok &&
Joe Perchesbd491112018-02-06 15:39:06 -08003273 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Joe Perchesa91e8992016-05-20 17:04:05 -07003274 my $indent = length($1);
3275 if ($indent % 8) {
3276 if (WARN("TABSTOP",
3277 "Statements should start on a tabstop\n" . $herecurr) &&
3278 $fix) {
3279 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3280 }
3281 }
3282 }
3283
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003284# check multi-line statement indentation matches previous line
Joe Perches5b579802018-08-21 21:57:33 -07003285 if ($perl_version_ok &&
Joe Perchesfd71f632017-07-10 15:52:30 -07003286 $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 -07003287 $prevline =~ /^\+(\t*)(.*)$/;
3288 my $oldindent = $1;
3289 my $rest = $2;
3290
3291 my $pos = pos_last_openparen($rest);
3292 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003293 $line =~ /^(\+| )([ \t]*)/;
3294 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003295
3296 my $goodtabindent = $oldindent .
3297 "\t" x ($pos / 8) .
3298 " " x ($pos % 8);
3299 my $goodspaceindent = $oldindent . " " x $pos;
3300
3301 if ($newindent ne $goodtabindent &&
3302 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003303
3304 if (CHK("PARENTHESIS_ALIGNMENT",
3305 "Alignment should match open parenthesis\n" . $hereprev) &&
3306 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003307 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003308 s/^\+[ \t]*/\+$goodtabindent/;
3309 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003310 }
3311 }
3312 }
3313
Joe Perches6ab3a972015-04-16 12:44:05 -07003314# check for space after cast like "(int) foo" or "(struct foo) bar"
3315# avoid checking a few false positives:
3316# "sizeof(<type>)" or "__alignof__(<type>)"
3317# function pointer declarations like "(*foo)(int) = bar;"
3318# structure definitions like "(struct foo) { 0 };"
3319# multiline macros that define functions
3320# known attributes or the __attribute__ keyword
3321 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3322 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003323 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003324 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003325 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003326 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003327 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003328 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003329 }
3330
Joe Perches86406b12015-09-09 15:37:41 -07003331# Block comment styles
3332# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003333 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003334 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003335 $rawline =~ /^\+[ \t]*\*/ &&
3336 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003337 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3338 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3339 }
3340
Joe Perches86406b12015-09-09 15:37:41 -07003341# Block comments use * on subsequent lines
3342 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3343 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003344 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003345 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003346 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003347 WARN("BLOCK_COMMENT_STYLE",
3348 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003349 }
3350
Joe Perches86406b12015-09-09 15:37:41 -07003351# Block comments use */ on trailing lines
3352 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003353 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3354 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3355 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003356 WARN("BLOCK_COMMENT_STYLE",
3357 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003358 }
3359
Joe Perches08eb9b82016-10-11 13:51:50 -07003360# Block comment * alignment
3361 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003362 $line =~ /^\+[ \t]*$;/ && #leading comment
3363 $rawline =~ /^\+[ \t]*\*/ && #leading *
3364 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003365 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003366 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3367 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003368 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003369 if (defined($1)) {
3370 $oldindent = expand_tabs($1);
3371 } else {
3372 $prevrawline =~ m@^\+(.*/?)\*@;
3373 $oldindent = expand_tabs($1);
3374 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003375 $rawline =~ m@^\+([ \t]*)\*@;
3376 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003377 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003378 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003379 WARN("BLOCK_COMMENT_STYLE",
3380 "Block comments should align the * on each line\n" . $hereprev);
3381 }
3382 }
3383
Joe Perches7f619192014-08-06 16:10:39 -07003384# check for missing blank lines after struct/union declarations
3385# with exceptions for various attributes and macros
3386 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3387 $line =~ /^\+/ &&
3388 !($line =~ /^\+\s*$/ ||
3389 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3390 $line =~ /^\+\s*MODULE_/i ||
3391 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3392 $line =~ /^\+[a-z_]*init/ ||
3393 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3394 $line =~ /^\+\s*DECLARE/ ||
Masahiro Yamada0bc989f2017-11-17 15:28:55 -08003395 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Joe Perches7f619192014-08-06 16:10:39 -07003396 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003397 if (CHK("LINE_SPACING",
3398 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3399 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003400 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003401 }
Joe Perches7f619192014-08-06 16:10:39 -07003402 }
3403
Joe Perches365dd4e2014-08-06 16:10:42 -07003404# check for multiple consecutive blank lines
3405 if ($prevline =~ /^[\+ ]\s*$/ &&
3406 $line =~ /^\+\s*$/ &&
3407 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003408 if (CHK("LINE_SPACING",
3409 "Please don't use multiple blank lines\n" . $hereprev) &&
3410 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003411 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003412 }
3413
Joe Perches365dd4e2014-08-06 16:10:42 -07003414 $last_blank_line = $linenr;
3415 }
3416
Joe Perches3b617e32014-04-03 14:49:28 -07003417# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003418 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3419 # actual declarations
3420 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003421 # function pointer declarations
3422 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003423 # foo bar; where foo is some local typedef or #define
3424 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3425 # known declaration macros
3426 $prevline =~ /^\+\s+$declaration_macros/) &&
3427 # for "else if" which can look like "$Ident $Ident"
3428 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3429 # other possible extensions of declaration lines
3430 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3431 # not starting a section or a macro "\" extended line
3432 $prevline =~ /(?:\{\s*|\\)$/) &&
3433 # looks like a declaration
3434 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003435 # function pointer declarations
3436 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003437 # foo bar; where foo is some local typedef or #define
3438 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3439 # known declaration macros
3440 $sline =~ /^\+\s+$declaration_macros/ ||
3441 # start of struct or union or enum
Joe Perches328b5f42018-09-04 15:46:06 -07003442 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003443 # start or end of block or continuation of declaration
3444 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3445 # bitfield continuation
3446 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3447 # other possible extensions of declaration lines
3448 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3449 # indentation of previous and current line are the same
3450 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003451 if (WARN("LINE_SPACING",
3452 "Missing a blank line after declarations\n" . $hereprev) &&
3453 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003454 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003455 }
Joe Perches3b617e32014-04-03 14:49:28 -07003456 }
3457
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003458# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003459# Exceptions:
3460# 1) within comments
3461# 2) indented preprocessor commands
3462# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003463 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003464 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003465 if (WARN("LEADING_SPACE",
3466 "please, no spaces at the start of a line\n" . $herevet) &&
3467 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003468 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003469 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003470 }
3471
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003472# check we are in a valid C source file if not then ignore this hunk
3473 next if ($realfile !~ /\.(h|c)$/);
3474
Joe Perches5751a242017-11-17 15:28:52 -08003475# check for unusual line ending [ or (
3476 if ($line =~ /^\+.*([\[\(])\s*$/) {
3477 CHK("OPEN_ENDED_LINE",
3478 "Lines should not end with a '$1'\n" . $herecurr);
3479 }
3480
Joe Perches4dbed762017-05-08 15:55:39 -07003481# check if this appears to be the start function declaration, save the name
3482 if ($sline =~ /^\+\{\s*$/ &&
3483 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3484 $context_function = $1;
3485 }
3486
3487# check if this appears to be the end of function declaration
3488 if ($sline =~ /^\+\}\s*$/) {
3489 undef $context_function;
3490 }
3491
Joe Perches032a4c02014-08-06 16:10:29 -07003492# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003493# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003494# if the previous line is a break or return and is indented 1 tab more...
3495 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3496 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003497 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3498 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3499 defined $lines[$linenr] &&
3500 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003501 WARN("UNNECESSARY_ELSE",
3502 "else is not generally useful after a break or return\n" . $hereprev);
3503 }
3504 }
3505
Joe Perchesc00df192014-08-06 16:11:01 -07003506# check indentation of a line with a break;
3507# if the previous line is a goto or return and is indented the same # of tabs
3508 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3509 my $tabs = $1;
3510 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3511 WARN("UNNECESSARY_BREAK",
3512 "break is not useful after a goto or return\n" . $hereprev);
3513 }
3514 }
3515
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003516# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003517 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003518 WARN("CVS_KEYWORD",
3519 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003520 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003521
Joe Perches56e77d72013-02-21 16:44:14 -08003522# check for old HOTPLUG __dev<foo> section markings
3523 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3524 WARN("HOTPLUG_SECTION",
3525 "Using $1 is unnecessary\n" . $herecurr);
3526 }
3527
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003528# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003529 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3530 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003531#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003532 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003533 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003534 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003535 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003536 $stat =~ s/\n./\n /g;
3537 $cond =~ s/\n./\n /g;
3538
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003539#print "linenr<$linenr> <$stat>\n";
3540 # If this statement has no statement boundaries within
3541 # it there is no point in retrying a statement scan
3542 # until we hit end of it.
3543 my $frag = $stat; $frag =~ s/;+\s*$//;
3544 if ($frag !~ /(?:{|;)/) {
3545#print "skip<$line_nr_next>\n";
3546 $suppress_statement = $line_nr_next;
3547 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003548
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003549 # Find the real next line.
3550 $realline_next = $line_nr_next;
3551 if (defined $realline_next &&
3552 (!defined $lines[$realline_next - 1] ||
3553 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3554 $realline_next++;
3555 }
3556
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003557 my $s = $stat;
3558 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003559
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003560 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003561 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003562
3563 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003564 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003565
Andy Whitcroft463f2862009-09-21 17:04:34 -07003566 } elsif ($s =~ /^.\s*else\b/s) {
3567
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003568 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003569 } 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 -07003570 my $type = $1;
3571 $type =~ s/\s+/ /g;
3572 possible($type, "A:" . $s);
3573
Andy Whitcroft8905a672007-11-28 16:21:06 -08003574 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003575 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003576 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003577 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003578
3579 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003580 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003581 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003582 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003583
3584 # Check for any sort of function declaration.
3585 # int foo(something bar, other baz);
3586 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003587 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 -08003588 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003589
Andy Whitcroftcf655042008-03-04 14:28:20 -08003590 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003591 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003592 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003593
Andy Whitcroft8905a672007-11-28 16:21:06 -08003594 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003595 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003596
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003597 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003598 }
3599 }
3600 }
3601
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003602 }
3603
Andy Whitcroft653d4872007-06-23 17:16:34 -07003604#
3605# Checks which may be anchored in the context.
3606#
3607
3608# Check for switch () and associated case and default
3609# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003610 if ($line=~/\bswitch\s*\(.*\)/) {
3611 my $err = '';
3612 my $sep = '';
3613 my @ctx = ctx_block_outer($linenr, $realcnt);
3614 shift(@ctx);
3615 for my $ctx (@ctx) {
3616 my ($clen, $cindent) = line_stats($ctx);
3617 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3618 $indent != $cindent) {
3619 $err .= "$sep$ctx\n";
3620 $sep = '';
3621 } else {
3622 $sep = "[...]\n";
3623 }
3624 }
3625 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003626 ERROR("SWITCH_CASE_INDENT_LEVEL",
3627 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003628 }
3629 }
3630
3631# if/while/etc brace do not go on next line, unless defining a do while loop,
3632# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003633 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 -07003634 my $pre_ctx = "$1$2";
3635
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003636 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003637
3638 if ($line =~ /^\+\t{6,}/) {
3639 WARN("DEEP_INDENTATION",
3640 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3641 }
3642
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003643 my $ctx_cnt = $realcnt - $#ctx - 1;
3644 my $ctx = join("\n", @ctx);
3645
Andy Whitcroft548596d2008-07-23 21:29:01 -07003646 my $ctx_ln = $linenr;
3647 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003648
Andy Whitcroft548596d2008-07-23 21:29:01 -07003649 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3650 defined $lines[$ctx_ln - 1] &&
3651 $lines[$ctx_ln - 1] =~ /^-/)) {
3652 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3653 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003654 $ctx_ln++;
3655 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003656
Andy Whitcroft53210162008-07-23 21:29:03 -07003657 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3658 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003659
Joe Perchesd752fcc2014-08-06 16:11:05 -07003660 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003661 ERROR("OPEN_BRACE",
3662 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003663 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003664 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003665 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3666 $ctx =~ /\)\s*\;\s*$/ &&
3667 defined $lines[$ctx_ln - 1])
3668 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003669 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3670 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003671 WARN("TRAILING_SEMICOLON",
3672 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003673 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003674 }
3675 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003676 }
3677
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003678# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003679 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 -08003680 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3681 ctx_statement_block($linenr, $realcnt, 0)
3682 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003683 my ($s, $c) = ($stat, $cond);
3684
3685 substr($s, 0, length($c), '');
3686
Joe Perches9f5af482015-09-09 15:37:30 -07003687 # remove inline comments
3688 $s =~ s/$;/ /g;
3689 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003690
3691 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003692 my @newlines = ($c =~ /\n/gs);
3693 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003694
Joe Perches9f5af482015-09-09 15:37:30 -07003695 # Make sure we remove the line prefixes as we have
3696 # none on the first line, and are going to readd them
3697 # where necessary.
3698 $s =~ s/\n./\n/gs;
3699 while ($s =~ /\n\s+\\\n/) {
3700 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3701 }
3702
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003703 # We want to check the first line inside the block
3704 # starting at the end of the conditional, so remove:
3705 # 1) any blank line termination
3706 # 2) any opening brace { on end of the line
3707 # 3) any do (...) {
3708 my $continuation = 0;
3709 my $check = 0;
3710 $s =~ s/^.*\bdo\b//;
3711 $s =~ s/^\s*{//;
3712 if ($s =~ s/^\s*\\//) {
3713 $continuation = 1;
3714 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003715 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003716 $check = 1;
3717 $cond_lines++;
3718 }
3719
3720 # Also ignore a loop construct at the end of a
3721 # preprocessor statement.
3722 if (($prevline =~ /^.\s*#\s*define\s/ ||
3723 $prevline =~ /\\\s*$/) && $continuation == 0) {
3724 $check = 0;
3725 }
3726
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003727 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003728 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003729 while ($cond_ptr != $cond_lines) {
3730 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003731
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003732 # If we see an #else/#elif then the code
3733 # is not linear.
3734 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3735 $check = 0;
3736 }
3737
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003738 # Ignore:
3739 # 1) blank lines, they should be at 0,
3740 # 2) preprocessor lines, and
3741 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003742 if ($continuation ||
3743 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003744 $s =~ /^\s*#\s*?/ ||
3745 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003746 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003747 if ($s =~ s/^.*?\n//) {
3748 $cond_lines++;
3749 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003750 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003751 }
3752
3753 my (undef, $sindent) = line_stats("+" . $s);
3754 my $stat_real = raw_line($linenr, $cond_lines);
3755
3756 # Check if either of these lines are modified, else
3757 # this is not this patch's fault.
3758 if (!defined($stat_real) ||
3759 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3760 $check = 0;
3761 }
3762 if (defined($stat_real) && $cond_lines > 1) {
3763 $stat_real = "[...]\n$stat_real";
3764 }
3765
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003766 #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 -07003767
Joe Perches9f5af482015-09-09 15:37:30 -07003768 if ($check && $s ne '' &&
3769 (($sindent % 8) != 0 ||
3770 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003771 ($sindent == $indent &&
3772 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003773 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003774 WARN("SUSPECT_CODE_INDENT",
3775 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003776 }
3777 }
3778
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003779 # Track the 'values' across context and added lines.
3780 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003781 my ($curr_values, $curr_vars) =
3782 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003783 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003784 if ($dbg_values) {
3785 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003786 print "$linenr > .$outline\n";
3787 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003788 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003789 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003790 $prev_values = substr($curr_values, -1);
3791
Andy Whitcroft00df3442007-06-08 13:47:06 -07003792#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003793 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003794
Joe Perches11ca40a2016-12-12 16:46:31 -08003795# check for dereferences that span multiple lines
3796 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3797 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3798 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3799 my $ref = $1;
3800 $line =~ /^.\s*($Lval)/;
3801 $ref .= $1;
3802 $ref =~ s/\s//g;
3803 WARN("MULTILINE_DEREFERENCE",
3804 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3805 }
3806
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003807# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003808 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 -07003809 my $type = $1;
3810 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003811 $var = "" if (!defined $var);
3812 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003813 my $sign = $1;
3814 my $pointer = $2;
3815
3816 $pointer = "" if (!defined $pointer);
3817
3818 if (WARN("UNSPECIFIED_INT",
3819 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3820 $fix) {
3821 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003822 my $comp_pointer = $pointer;
3823 $comp_pointer =~ s/\s//g;
3824 $decl .= $comp_pointer;
3825 $decl = rtrim($decl) if ($var eq "");
3826 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003827 }
3828 }
3829 }
3830
Andy Whitcroft653d4872007-06-23 17:16:34 -07003831# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003832 if ($dbg_type) {
3833 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003834 ERROR("TEST_TYPE",
3835 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003836 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003837 ERROR("TEST_NOT_TYPE",
3838 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003839 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003840 next;
3841 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003842# TEST: allow direct testing of the attribute matcher.
3843 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003844 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003845 ERROR("TEST_ATTR",
3846 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003847 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003848 ERROR("TEST_NOT_ATTR",
3849 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003850 }
3851 next;
3852 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003853
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003854# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003855 if ($line =~ /^.\s*{/ &&
3856 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003857 if (ERROR("OPEN_BRACE",
3858 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003859 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3860 fix_delete_line($fixlinenr - 1, $prevrawline);
3861 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003862 my $fixedline = $prevrawline;
3863 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003864 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003865 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003866 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003867 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003868 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003869 }
3870
Andy Whitcroft653d4872007-06-23 17:16:34 -07003871#
3872# Checks which are anchored on the added line.
3873#
3874
3875# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003876 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003877 my $path = $1;
3878 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003879 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003880 "malformed #include filename\n" . $herecurr);
3881 }
3882 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3883 ERROR("UAPI_INCLUDE",
3884 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003885 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003886 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003887
3888# no C99 // comments
3889 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003890 if (ERROR("C99_COMMENTS",
3891 "do not use C99 // comments\n" . $herecurr) &&
3892 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003893 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003894 if ($line =~ /\/\/(.*)$/) {
3895 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003896 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003897 }
3898 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003899 }
3900 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003901 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003902 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003903
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003904# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3905# the whole statement.
3906#print "APW <$lines[$realline_next - 1]>\n";
3907 if (defined $realline_next &&
3908 exists $lines[$realline_next - 1] &&
3909 !defined $suppress_export{$realline_next} &&
3910 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3911 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003912 # Handle definitions which produce identifiers with
3913 # a prefix:
3914 # XXX(foo);
3915 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003916 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003917 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003918 $name =~ /^${Ident}_$2/) {
3919#print "FOO C name<$name>\n";
3920 $suppress_export{$realline_next} = 1;
3921
3922 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003923 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003924 ^.DEFINE_$Ident\(\Q$name\E\)|
3925 ^.DECLARE_$Ident\(\Q$name\E\)|
3926 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003927 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3928 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003929 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003930#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3931 $suppress_export{$realline_next} = 2;
3932 } else {
3933 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003934 }
3935 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003936 if (!defined $suppress_export{$linenr} &&
3937 $prevline =~ /^.\s*$/ &&
3938 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3939 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3940#print "FOO B <$lines[$linenr - 1]>\n";
3941 $suppress_export{$linenr} = 2;
3942 }
3943 if (defined $suppress_export{$linenr} &&
3944 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003945 WARN("EXPORT_SYMBOL",
3946 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003947 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003948
Joe Eloff5150bda2010-08-09 17:21:00 -07003949# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003950 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003951 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003952 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003953 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003954 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003955 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003956 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003957# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003958 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003959 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003960 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003961 $herecurr) &&
3962 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003963 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003964 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003965 }
3966
Joe Perches18130872014-08-06 16:11:22 -07003967# check for misordered declarations of char/short/int/long with signed/unsigned
3968 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3969 my $tmp = trim($1);
3970 WARN("MISORDERED_TYPE",
3971 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3972 }
3973
Joe Perches809e0822018-08-21 21:58:12 -07003974# check for unnecessary <signed> int declarations of short/long/long long
3975 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3976 my $type = trim($1);
3977 next if ($type !~ /\bint\b/);
3978 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3979 my $new_type = $type;
3980 $new_type =~ s/\b\s*int\s*\b/ /;
3981 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3982 $new_type =~ s/^const\s+//;
3983 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3984 $new_type = "const $new_type" if ($type =~ /^const\b/);
3985 $new_type =~ s/\s+/ /g;
3986 $new_type = trim($new_type);
3987 if (WARN("UNNECESSARY_INT",
3988 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3989 $fix) {
3990 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3991 }
3992 }
3993
Joe Perchescb710ec2010-10-26 14:23:20 -07003994# check for static const char * arrays.
3995 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003996 WARN("STATIC_CONST_CHAR_ARRAY",
3997 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003998 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08003999 }
4000
4001# check for initialized const char arrays that should be static const
4002 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4003 if (WARN("STATIC_CONST_CHAR_ARRAY",
4004 "const array should probably be static const\n" . $herecurr) &&
4005 $fix) {
4006 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4007 }
4008 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004009
4010# check for static char foo[] = "bar" declarations.
4011 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004012 WARN("STATIC_CONST_CHAR_ARRAY",
4013 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07004014 $herecurr);
Joe Perches77b8c0a2019-01-03 15:26:59 -08004015 }
Joe Perchescb710ec2010-10-26 14:23:20 -07004016
Joe Perchesab7e23f2015-04-16 12:44:22 -07004017# check for const <foo> const where <foo> is not a pointer or array type
4018 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4019 my $found = $1;
4020 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4021 WARN("CONST_CONST",
4022 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4023 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4024 WARN("CONST_CONST",
4025 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4026 }
4027 }
4028
Joe Perches9b0fa602014-04-03 14:49:18 -07004029# check for non-global char *foo[] = {"bar", ...} declarations.
4030 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4031 WARN("STATIC_CONST_CHAR_ARRAY",
4032 "char * array declaration might be better as static const\n" .
4033 $herecurr);
4034 }
4035
Joe Perchesb598b672015-04-16 12:44:36 -07004036# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4037 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4038 my $array = $1;
4039 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4040 my $array_div = $1;
4041 if (WARN("ARRAY_SIZE",
4042 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4043 $fix) {
4044 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4045 }
4046 }
4047 }
4048
Joe Perchesb36190c2014-01-27 17:07:18 -08004049# check for function declarations without arguments like "int foo()"
4050 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4051 if (ERROR("FUNCTION_WITHOUT_ARGS",
4052 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4053 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004054 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08004055 }
4056 }
4057
Andy Whitcroft653d4872007-06-23 17:16:34 -07004058# check for new typedefs, only function parameters and sparse annotations
4059# make sense.
4060 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08004061 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004062 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07004063 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02004064 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004065 WARN("NEW_TYPEDEFS",
4066 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004067 }
4068
4069# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08004070 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004071 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4072 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004073 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004074
Andy Whitcroft65863862009-01-06 14:41:21 -08004075 # Should start with a space.
4076 $to =~ s/^(\S)/ $1/;
4077 # Should not end with a space.
4078 $to =~ s/\s+$//;
4079 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004080 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004081 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004082
Joe Perches3705ce52013-07-03 15:05:31 -07004083## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08004084 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07004085 if (ERROR("POINTER_LOCATION",
4086 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4087 $fix) {
4088 my $sub_from = $ident;
4089 my $sub_to = $ident;
4090 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004091 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004092 s@\Q$sub_from\E@$sub_to@;
4093 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004094 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08004095 }
4096 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4097 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07004098 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004099
Andy Whitcroft65863862009-01-06 14:41:21 -08004100 # Should start with a space.
4101 $to =~ s/^(\S)/ $1/;
4102 # Should not end with a space.
4103 $to =~ s/\s+$//;
4104 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08004105 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08004106 }
4107 # Modifiers should have spaces.
4108 $to =~ s/(\b$Modifier$)/$1 /;
4109
Joe Perches3705ce52013-07-03 15:05:31 -07004110## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08004111 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004112 if (ERROR("POINTER_LOCATION",
4113 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4114 $fix) {
4115
4116 my $sub_from = $match;
4117 my $sub_to = $match;
4118 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07004119 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004120 s@\Q$sub_from\E@$sub_to@;
4121 }
Andy Whitcroft65863862009-01-06 14:41:21 -08004122 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004123 }
4124
Joe Perches9d3e3c72015-09-09 15:37:27 -07004125# avoid BUG() or BUG_ON()
4126 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004127 my $msg_level = \&WARN;
4128 $msg_level = \&CHK if ($file);
4129 &{$msg_level}("AVOID_BUG",
4130 "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 -07004131 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004132
Joe Perches9d3e3c72015-09-09 15:37:27 -07004133# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08004134 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004135 WARN("LINUX_VERSION_CODE",
4136 "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 -08004137 }
4138
Joe Perches17441222011-06-15 15:08:17 -07004139# check for uses of printk_ratelimit
4140 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004141 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08004142 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07004143 }
4144
Joe Percheseeef5732017-11-17 15:28:41 -08004145# printk should use KERN_* levels
4146 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4147 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4148 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004149 }
4150
Joe Perches243f3802012-05-31 16:26:09 -07004151 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4152 my $orig = $1;
4153 my $level = lc($orig);
4154 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07004155 my $level2 = $level;
4156 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07004157 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07004158 "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 -07004159 }
4160
Joe Perchesdc139312013-02-21 16:44:13 -08004161 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4162 my $orig = $1;
4163 my $level = lc($orig);
4164 $level = "warn" if ($level eq "warning");
4165 $level = "dbg" if ($level eq "debug");
4166 WARN("PREFER_DEV_LEVEL",
4167 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4168 }
4169
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07004170# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4171# number of false positives, but assembly files are not checked, so at
4172# least the arch entry code will not trigger this warning.
4173 if ($line =~ /\bENOSYS\b/) {
4174 WARN("ENOSYS",
4175 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4176 }
4177
Andy Whitcroft653d4872007-06-23 17:16:34 -07004178# function brace can't be on same line, except for #defines of do while,
4179# or if closed on same line
Joe Perches5b579802018-08-21 21:57:33 -07004180 if ($perl_version_ok &&
Joe Perches2d453e32018-02-06 15:39:09 -08004181 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4182 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4183 $sline !~ /}/) {
Joe Perches8d182472014-08-06 16:11:12 -07004184 if (ERROR("OPEN_BRACE",
Joe Perches2d453e32018-02-06 15:39:09 -08004185 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Joe Perches8d182472014-08-06 16:11:12 -07004186 $fix) {
4187 fix_delete_line($fixlinenr, $rawline);
4188 my $fixed_line = $rawline;
4189 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4190 my $line1 = $1;
4191 my $line2 = $2;
4192 fix_insert_line($fixlinenr, ltrim($line1));
4193 fix_insert_line($fixlinenr, "\+{");
4194 if ($line2 !~ /^\s*$/) {
4195 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4196 }
4197 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004198 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004199
Andy Whitcroft8905a672007-11-28 16:21:06 -08004200# open braces for enum, union and struct go on the same line.
4201 if ($line =~ /^.\s*{/ &&
4202 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004203 if (ERROR("OPEN_BRACE",
4204 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4205 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4206 fix_delete_line($fixlinenr - 1, $prevrawline);
4207 fix_delete_line($fixlinenr, $rawline);
4208 my $fixedline = rtrim($prevrawline) . " {";
4209 fix_insert_line($fixlinenr, $fixedline);
4210 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07004211 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004212 if ($fixedline !~ /^\+\s*$/) {
4213 fix_insert_line($fixlinenr, $fixedline);
4214 }
4215 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004216 }
4217
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004218# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004219 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4220 if (WARN("SPACING",
4221 "missing space after $1 definition\n" . $herecurr) &&
4222 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004223 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004224 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4225 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004226 }
4227
Joe Perches31070b52014-01-23 15:54:49 -08004228# Function pointer declarations
4229# check spacing between type, funcptr, and args
4230# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004231 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004232 my $declare = $1;
4233 my $pre_pointer_space = $2;
4234 my $post_pointer_space = $3;
4235 my $funcname = $4;
4236 my $post_funcname_space = $5;
4237 my $pre_args_space = $6;
4238
Joe Perches91f72e92014-04-03 14:49:12 -07004239# the $Declare variable will capture all spaces after the type
4240# so check it for a missing trailing missing space but pointer return types
4241# don't need a space so don't warn for those.
4242 my $post_declare_space = "";
4243 if ($declare =~ /(\s+)$/) {
4244 $post_declare_space = $1;
4245 $declare = rtrim($declare);
4246 }
4247 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004248 WARN("SPACING",
4249 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004250 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004251 }
4252
4253# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004254# This test is not currently implemented because these declarations are
4255# equivalent to
4256# int foo(int bar, ...)
4257# and this is form shouldn't/doesn't generate a checkpatch warning.
4258#
4259# elsif ($declare =~ /\s{2,}$/) {
4260# WARN("SPACING",
4261# "Multiple spaces after return type\n" . $herecurr);
4262# }
Joe Perches31070b52014-01-23 15:54:49 -08004263
4264# unnecessary space "type ( *funcptr)(args...)"
4265 if (defined $pre_pointer_space &&
4266 $pre_pointer_space =~ /^\s/) {
4267 WARN("SPACING",
4268 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4269 }
4270
4271# unnecessary space "type (* funcptr)(args...)"
4272 if (defined $post_pointer_space &&
4273 $post_pointer_space =~ /^\s/) {
4274 WARN("SPACING",
4275 "Unnecessary space before function pointer name\n" . $herecurr);
4276 }
4277
4278# unnecessary space "type (*funcptr )(args...)"
4279 if (defined $post_funcname_space &&
4280 $post_funcname_space =~ /^\s/) {
4281 WARN("SPACING",
4282 "Unnecessary space after function pointer name\n" . $herecurr);
4283 }
4284
4285# unnecessary space "type (*funcptr) (args...)"
4286 if (defined $pre_args_space &&
4287 $pre_args_space =~ /^\s/) {
4288 WARN("SPACING",
4289 "Unnecessary space before function pointer arguments\n" . $herecurr);
4290 }
4291
4292 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004293 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004294 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004295 }
4296 }
4297
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004298# check for spacing round square brackets; allowed:
4299# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004300# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4301# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004302 while ($line =~ /(.*?\s)\[/g) {
4303 my ($where, $prefix) = ($-[1], $1);
4304 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004305 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardt38dca982018-04-10 16:34:14 -07004306 $prefix !~ /[{,:]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004307 if (ERROR("BRACKET_SPACE",
4308 "space prohibited before open square bracket '['\n" . $herecurr) &&
4309 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004310 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004311 s/^(\+.*?)\s+\[/$1\[/;
4312 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004313 }
4314 }
4315
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004316# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004317 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004318 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004319 my $ctx_before = substr($line, 0, $-[1]);
4320 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004321
4322 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004323 if ($name =~ /^(?:
4324 if|for|while|switch|return|case|
4325 volatile|__volatile__|
4326 __attribute__|format|__extension__|
4327 asm|__asm__)$/x)
4328 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004329 # cpp #define statements have non-optional spaces, ie
4330 # if there is a space between the name and the open
4331 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004332 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004333
4334 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004335 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004336
4337 # If this whole things ends with a type its most
4338 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004340
4341 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004342 if (WARN("SPACING",
4343 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4344 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004345 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004346 s/\b$name\s+\(/$name\(/;
4347 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004348 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004349 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004350
Andy Whitcroft653d4872007-06-23 17:16:34 -07004351# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004352 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004353 my $fixed_line = "";
4354 my $line_fixed = 0;
4355
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004356 my $ops = qr{
4357 <<=|>>=|<=|>=|==|!=|
4358 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4359 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004360 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004361 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004362 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004363 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004364
4365## print("element count: <" . $#elements . ">\n");
4366## foreach my $el (@elements) {
4367## print("el: <$el>\n");
4368## }
4369
4370 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004371 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004372
Joe Perches3705ce52013-07-03 15:05:31 -07004373 foreach my $el (@elements) {
4374 push(@fix_elements, substr($rawline, $off, length($el)));
4375 $off += length($el);
4376 }
4377
4378 $off = 0;
4379
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004380 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004381 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004382
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004383 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004384
4385 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4386
4387## print("n: <$n> good: <$good>\n");
4388
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004389 $off += length($elements[$n]);
4390
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004391 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004392 my $ca = substr($opline, 0, $off);
4393 my $cc = '';
4394 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4395 $cc = substr($opline, $off + length($elements[$n + 1]));
4396 }
4397 my $cb = "$ca$;$cc";
4398
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004399 my $a = '';
4400 $a = 'V' if ($elements[$n] ne '');
4401 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004402 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004403 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4404 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004405 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004406
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004407 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004408
4409 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004410 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004411 $c = 'V' if ($elements[$n + 2] ne '');
4412 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004413 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004414 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4415 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004416 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004417 } else {
4418 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004419 }
4420
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004421 my $ctx = "${a}x${c}";
4422
4423 my $at = "(ctx:$ctx)";
4424
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004425 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004426 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004427
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004428 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004429 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004430
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004431 # Get the full operator variant.
4432 my $opv = $op . substr($curr_vars, $off, 1);
4433
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004434 # Ignore operators passed as parameters.
4435 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004436 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004437
Andy Whitcroftcf655042008-03-04 14:28:20 -08004438# # Ignore comments
4439# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004440
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004441 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004442 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004443 if ($ctx !~ /.x[WEBC]/ &&
4444 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004445 if (ERROR("SPACING",
4446 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004447 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004448 $line_fixed = 1;
4449 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004450 }
4451
4452 # // is a comment
4453 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004454
Joe Perchesb00e4812014-04-03 14:49:33 -07004455 # : when part of a bitfield
4456 } elsif ($opv eq ':B') {
4457 # skip the bitfield test for now
4458
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004459 # No spaces for:
4460 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004461 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004462 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004463 if (ERROR("SPACING",
4464 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004465 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004466 if (defined $fix_elements[$n + 2]) {
4467 $fix_elements[$n + 2] =~ s/^\s+//;
4468 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004469 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004470 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004471 }
4472
Joe Perches23810972014-12-10 15:51:32 -08004473 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004474 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004475 my $rtrim_before = 0;
4476 my $space_after = 0;
4477 if ($ctx =~ /Wx./) {
4478 if (ERROR("SPACING",
4479 "space prohibited before that '$op' $at\n" . $hereptr)) {
4480 $line_fixed = 1;
4481 $rtrim_before = 1;
4482 }
4483 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004484 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004485 if (ERROR("SPACING",
4486 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004487 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004488 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004489 $space_after = 1;
4490 }
4491 }
4492 if ($rtrim_before || $space_after) {
4493 if ($rtrim_before) {
4494 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4495 } else {
4496 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4497 }
4498 if ($space_after) {
4499 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004500 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004501 }
4502
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004503 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004504 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004505 #warn "'*' is part of type\n";
4506
4507 # unary operators should have a space before and
4508 # none after. May be left adjacent to another
4509 # unary operator, or a cast
4510 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004511 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004512 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004513 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004514 if (ERROR("SPACING",
4515 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004516 if ($n != $last_after + 2) {
4517 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4518 $line_fixed = 1;
4519 }
Joe Perches3705ce52013-07-03 15:05:31 -07004520 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004521 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004522 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004523 # A unary '*' may be const
4524
4525 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004526 if (ERROR("SPACING",
4527 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004528 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004529 if (defined $fix_elements[$n + 2]) {
4530 $fix_elements[$n + 2] =~ s/^\s+//;
4531 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004532 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004533 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004534 }
4535
4536 # unary ++ and unary -- are allowed no space on one side.
4537 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004538 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004539 if (ERROR("SPACING",
4540 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004541 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004542 $line_fixed = 1;
4543 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004544 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004545 if ($ctx =~ /Wx[BE]/ ||
4546 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004547 if (ERROR("SPACING",
4548 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004549 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004550 $line_fixed = 1;
4551 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004552 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004553 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004554 if (ERROR("SPACING",
4555 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004556 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004557 if (defined $fix_elements[$n + 2]) {
4558 $fix_elements[$n + 2] =~ s/^\s+//;
4559 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004560 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004561 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004562 }
4563
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004564 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004565 } elsif ($op eq '<<' or $op eq '>>' or
4566 $op eq '&' or $op eq '^' or $op eq '|' or
4567 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004568 $op eq '*' or $op eq '/' or
4569 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004570 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004571 if ($check) {
4572 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4573 if (CHK("SPACING",
4574 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4575 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4576 $fix_elements[$n + 2] =~ s/^\s+//;
4577 $line_fixed = 1;
4578 }
4579 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4580 if (CHK("SPACING",
4581 "space preferred before that '$op' $at\n" . $hereptr)) {
4582 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4583 $line_fixed = 1;
4584 }
4585 }
4586 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004587 if (ERROR("SPACING",
4588 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004589 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4590 if (defined $fix_elements[$n + 2]) {
4591 $fix_elements[$n + 2] =~ s/^\s+//;
4592 }
Joe Perches3705ce52013-07-03 15:05:31 -07004593 $line_fixed = 1;
4594 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004595 }
4596
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004597 # A colon needs no spaces before when it is
4598 # terminating a case value or a label.
4599 } elsif ($opv eq ':C' || $opv eq ':L') {
4600 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004601 if (ERROR("SPACING",
4602 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004603 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004604 $line_fixed = 1;
4605 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004606 }
4607
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004608 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004609 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004610 my $ok = 0;
4611
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004612 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004613 if (($op eq '<' &&
4614 $cc =~ /^\S+\@\S+>/) ||
4615 ($op eq '>' &&
4616 $ca =~ /<\S+\@\S+$/))
4617 {
4618 $ok = 1;
4619 }
4620
Joe Perchese0df7e12015-04-16 12:44:53 -07004621 # for asm volatile statements
4622 # ignore a colon with another
4623 # colon immediately before or after
4624 if (($op eq ':') &&
4625 ($ca =~ /:$/ || $cc =~ /^:/)) {
4626 $ok = 1;
4627 }
4628
Joe Perches84731622013-11-12 15:10:05 -08004629 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004630 if ($ok == 0) {
Jean Delvare0675a8f2017-09-08 16:16:07 -07004631 my $msg_level = \&ERROR;
4632 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Joe Perches84731622013-11-12 15:10:05 -08004633
Jean Delvare0675a8f2017-09-08 16:16:07 -07004634 if (&{$msg_level}("SPACING",
4635 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004636 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4637 if (defined $fix_elements[$n + 2]) {
4638 $fix_elements[$n + 2] =~ s/^\s+//;
4639 }
Joe Perches3705ce52013-07-03 15:05:31 -07004640 $line_fixed = 1;
4641 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004642 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004643 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004644 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004645
4646## print("n: <$n> GOOD: <$good>\n");
4647
4648 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004649 }
Joe Perches3705ce52013-07-03 15:05:31 -07004650
4651 if (($#elements % 2) == 0) {
4652 $fixed_line = $fixed_line . $fix_elements[$#elements];
4653 }
4654
Joe Perches194f66f2014-08-06 16:11:03 -07004655 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4656 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004657 }
4658
4659
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004660 }
4661
Joe Perches786b6322013-07-03 15:05:32 -07004662# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004663 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004664 if (WARN("SPACING",
4665 "space prohibited before semicolon\n" . $herecurr) &&
4666 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004667 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004668 s/^(\+.*\S)\s+;/$1;/;
4669 }
4670 }
4671
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004672# check for multiple assignments
4673 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004674 CHK("MULTIPLE_ASSIGNMENTS",
4675 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004676 }
4677
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004678## # check for multiple declarations, allowing for a function declaration
4679## # continuation.
4680## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4681## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4682##
4683## # Remove any bracketed sections to ensure we do not
4684## # falsly report the parameters of functions.
4685## my $ln = $line;
4686## while ($ln =~ s/\([^\(\)]*\)//g) {
4687## }
4688## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004689## WARN("MULTIPLE_DECLARATION",
4690## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004691## }
4692## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004693
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004694#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004695 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004696 $line =~ /\b(?:else|do)\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004697 if (ERROR("SPACING",
4698 "space required before the open brace '{'\n" . $herecurr) &&
4699 $fix) {
Michal Zylowski6ad724e2018-08-21 21:58:08 -07004700 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004701 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004702 }
4703
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004704## # check for blank lines before declarations
4705## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4706## $prevrawline =~ /^.\s*$/) {
4707## WARN("SPACING",
4708## "No blank lines before declarations\n" . $hereprev);
4709## }
4710##
4711
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004712# closing brace should have a space following it when it has anything
4713# on the line
Joe Perches94fb9842019-09-25 16:46:47 -07004714 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004715 if (ERROR("SPACING",
4716 "space required after that close brace '}'\n" . $herecurr) &&
4717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004718 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004719 s/}((?!(?:,|;|\)))\S)/} $1/;
4720 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004721 }
4722
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004723# check spacing on square brackets
4724 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004725 if (ERROR("SPACING",
4726 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4727 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004728 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004729 s/\[\s+/\[/;
4730 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004731 }
4732 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004733 if (ERROR("SPACING",
4734 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4735 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004736 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004737 s/\s+\]/\]/;
4738 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004739 }
4740
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004741# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004742 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4743 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004744 if (ERROR("SPACING",
4745 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4746 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004747 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004748 s/\(\s+/\(/;
4749 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004750 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004751 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004752 $line !~ /for\s*\(.*;\s+\)/ &&
4753 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004754 if (ERROR("SPACING",
4755 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004757 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004758 s/\s+\)/\)/;
4759 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004760 }
4761
Joe Perchese2826fd2014-08-06 16:10:48 -07004762# check unnecessary parentheses around addressof/dereference single $Lvals
4763# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4764
4765 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004766 my $var = $1;
4767 if (CHK("UNNECESSARY_PARENTHESES",
4768 "Unnecessary parentheses around $var\n" . $herecurr) &&
4769 $fix) {
4770 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4771 }
4772 }
4773
4774# check for unnecessary parentheses around function pointer uses
4775# ie: (foo->bar)(); should be foo->bar();
4776# but not "if (foo->bar) (" to avoid some false positives
4777 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4778 my $var = $2;
4779 if (CHK("UNNECESSARY_PARENTHESES",
4780 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4781 $fix) {
4782 my $var2 = deparenthesize($var);
4783 $var2 =~ s/\s//g;
4784 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4785 }
4786 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004787
Joe Perches63b7c732017-09-08 16:16:01 -07004788# check for unnecessary parentheses around comparisons in if uses
Joe Perchesa032aa42018-02-06 15:39:03 -08004789# when !drivers/staging or command-line uses --strict
4790 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Joe Perches5b579802018-08-21 21:57:33 -07004791 $perl_version_ok && defined($stat) &&
Joe Perches63b7c732017-09-08 16:16:01 -07004792 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4793 my $if_stat = $1;
4794 my $test = substr($2, 1, -1);
4795 my $herectx;
4796 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4797 my $match = $1;
4798 # avoid parentheses around potential macro args
4799 next if ($match =~ /^\s*\w+\s*$/);
4800 if (!defined($herectx)) {
4801 $herectx = $here . "\n";
4802 my $cnt = statement_rawlines($if_stat);
4803 for (my $n = 0; $n < $cnt; $n++) {
4804 my $rl = raw_line($linenr, $n);
4805 $herectx .= $rl . "\n";
4806 last if $rl =~ /^[ \+].*\{/;
4807 }
4808 }
4809 CHK("UNNECESSARY_PARENTHESES",
4810 "Unnecessary parentheses around '$match'\n" . $herectx);
4811 }
4812 }
4813
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004814#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004815 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004816 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004817 if (WARN("INDENTED_LABEL",
4818 "labels should not be indented\n" . $herecurr) &&
4819 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004820 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004821 s/^(.)\s+/$1/;
4822 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004823 }
4824
Joe Perches5b9553a2014-04-03 14:49:21 -07004825# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004826 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004827 my $spacing = $1;
Joe Perches5b579802018-08-21 21:57:33 -07004828 if ($perl_version_ok &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004829 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4830 my $value = $1;
4831 $value = deparenthesize($value);
4832 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4833 ERROR("RETURN_PARENTHESES",
4834 "return is not a function, parentheses are not required\n" . $herecurr);
4835 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004836 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004837 ERROR("SPACING",
4838 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004839 }
4840 }
Joe Perches507e5142013-11-12 15:10:13 -08004841
Joe Perchesb43ae212014-06-23 13:22:07 -07004842# unnecessary return in a void function
4843# at end-of-function, with the previous line a single leading tab, then return;
4844# and the line before that not a goto label target like "out:"
4845 if ($sline =~ /^[ \+]}\s*$/ &&
4846 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4847 $linenr >= 3 &&
4848 $lines[$linenr - 3] =~ /^[ +]/ &&
4849 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004850 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004851 "void function return statements are not generally useful\n" . $hereprev);
4852 }
Joe Perches9819cf22014-06-04 16:12:09 -07004853
Joe Perches189248d2014-01-23 15:54:47 -08004854# if statements using unnecessary parentheses - ie: if ((foo == bar))
Joe Perches5b579802018-08-21 21:57:33 -07004855 if ($perl_version_ok &&
Joe Perches189248d2014-01-23 15:54:47 -08004856 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4857 my $openparens = $1;
4858 my $count = $openparens =~ tr@\(@\(@;
4859 my $msg = "";
4860 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4861 my $comp = $4; #Not $1 because of $LvalOrFunc
4862 $msg = " - maybe == should be = ?" if ($comp eq "==");
4863 WARN("UNNECESSARY_PARENTHESES",
4864 "Unnecessary parentheses$msg\n" . $herecurr);
4865 }
4866 }
4867
Joe Perchesc5595fa2015-09-09 15:37:58 -07004868# comparisons with a constant or upper case identifier on the left
4869# avoid cases like "foo + BAR < baz"
4870# only fix matches surrounded by parentheses to avoid incorrect
4871# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Joe Perches5b579802018-08-21 21:57:33 -07004872 if ($perl_version_ok &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004873 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4874 my $lead = $1;
4875 my $const = $2;
4876 my $comp = $3;
4877 my $to = $4;
4878 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004879 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004880 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4881 WARN("CONSTANT_COMPARISON",
4882 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4883 $fix) {
4884 if ($comp eq "<") {
4885 $newcomp = ">";
4886 } elsif ($comp eq "<=") {
4887 $newcomp = ">=";
4888 } elsif ($comp eq ">") {
4889 $newcomp = "<";
4890 } elsif ($comp eq ">=") {
4891 $newcomp = "<=";
4892 }
4893 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4894 }
4895 }
4896
Joe Perchesf34e4a42015-04-16 12:44:19 -07004897# Return of what appears to be an errno should normally be negative
4898 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004899 my $name = $1;
4900 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004901 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004902 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004903 }
4904 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004905
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004906# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004907 if ($line =~ /\b(if|while|for|switch)\(/) {
4908 if (ERROR("SPACING",
4909 "space required before the open parenthesis '('\n" . $herecurr) &&
4910 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004911 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004912 s/\b(if|while|for|switch)\(/$1 \(/;
4913 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004914 }
4915
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004916# Check for illegal assignment in if conditional -- and check for trailing
4917# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004918 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004919 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4920 ctx_statement_block($linenr, $realcnt, 0)
4921 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004922 my ($stat_next) = ctx_statement_block($line_nr_next,
4923 $remain_next, $off_next);
4924 $stat_next =~ s/\n./\n /g;
4925 ##print "stat<$stat> stat_next<$stat_next>\n";
4926
4927 if ($stat_next =~ /^\s*while\b/) {
4928 # If the statement carries leading newlines,
4929 # then count those as offsets.
4930 my ($whitespace) =
4931 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4932 my $offset =
4933 statement_rawlines($whitespace) - 1;
4934
4935 $suppress_whiletrailers{$line_nr_next +
4936 $offset} = 1;
4937 }
4938 }
4939 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004940 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004941 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004942 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004943
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004944 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004945 ERROR("ASSIGN_IN_IF",
4946 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004947 }
4948
4949 # Find out what is on the end of the line after the
4950 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004951 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004952 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004953 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004954 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4955 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004956 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004957 # Find out how long the conditional actually is.
4958 my @newlines = ($c =~ /\n/gs);
4959 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004960 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004961
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004962 $stat_real = raw_line($linenr, $cond_lines)
4963 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004964 if (defined($stat_real) && $cond_lines > 1) {
4965 $stat_real = "[...]\n$stat_real";
4966 }
4967
Joe Perches000d1cc12011-07-25 17:13:25 -07004968 ERROR("TRAILING_STATEMENTS",
4969 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004970 }
4971 }
4972
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004973# Check for bitwise tests written as boolean
4974 if ($line =~ /
4975 (?:
4976 (?:\[|\(|\&\&|\|\|)
4977 \s*0[xX][0-9]+\s*
4978 (?:\&\&|\|\|)
4979 |
4980 (?:\&\&|\|\|)
4981 \s*0[xX][0-9]+\s*
4982 (?:\&\&|\|\||\)|\])
4983 )/x)
4984 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004985 WARN("HEXADECIMAL_BOOLEAN_TEST",
4986 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004987 }
4988
Andy Whitcroft8905a672007-11-28 16:21:06 -08004989# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004990 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4991 my $s = $1;
4992 $s =~ s/$;//g; # Remove any comments
4993 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004994 ERROR("TRAILING_STATEMENTS",
4995 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004996 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004997 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004998# if should not continue a brace
4999 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005000 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07005001 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08005002 $herecurr);
5003 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005004# case and default should not have general statements after them
5005 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5006 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07005007 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005008 \s*return\s+
5009 )/xg)
5010 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005011 ERROR("TRAILING_STATEMENTS",
5012 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07005013 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005014
5015 # Check for }<nl>else {, these must be at the same
5016 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07005017 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5018 $previndent == $indent) {
5019 if (ERROR("ELSE_AFTER_BRACE",
5020 "else should follow close brace '}'\n" . $hereprev) &&
5021 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5022 fix_delete_line($fixlinenr - 1, $prevrawline);
5023 fix_delete_line($fixlinenr, $rawline);
5024 my $fixedline = $prevrawline;
5025 $fixedline =~ s/}\s*$//;
5026 if ($fixedline !~ /^\+\s*$/) {
5027 fix_insert_line($fixlinenr, $fixedline);
5028 }
5029 $fixedline = $rawline;
5030 $fixedline =~ s/^(.\s*)else/$1} else/;
5031 fix_insert_line($fixlinenr, $fixedline);
5032 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005033 }
5034
Joe Perches8b8856f2014-08-06 16:11:14 -07005035 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5036 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005037 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5038
5039 # Find out what is on the end of the line after the
5040 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07005041 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005042 $s =~ s/\n.*//g;
5043
5044 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07005045 if (ERROR("WHILE_AFTER_BRACE",
5046 "while should follow close brace '}'\n" . $hereprev) &&
5047 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5048 fix_delete_line($fixlinenr - 1, $prevrawline);
5049 fix_delete_line($fixlinenr, $rawline);
5050 my $fixedline = $prevrawline;
5051 my $trailing = $rawline;
5052 $trailing =~ s/^\+//;
5053 $trailing = trim($trailing);
5054 $fixedline =~ s/}\s*$/} $trailing/;
5055 fix_insert_line($fixlinenr, $fixedline);
5056 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005057 }
5058 }
5059
Joe Perches95e2c602013-07-03 15:05:20 -07005060#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08005061 while ($line =~ m{($Constant|$Lval)}g) {
5062 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07005063
Joe Perches95e2c602013-07-03 15:05:20 -07005064#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07005065 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07005066 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07005067#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07005068 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perchesd439e6a2019-12-04 16:52:06 -08005069#Ignore SI style variants like nS, mV and dB
5070#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5071 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Julius Wernerf5123572014-12-10 15:51:54 -08005072#Ignore some three character SI units explicitly, like MiB and KHz
5073 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07005074 while ($var =~ m{($Ident)}g) {
5075 my $word = $1;
5076 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08005077 if ($check) {
5078 seed_camelcase_includes();
5079 if (!$file && !$camelcase_file_seeded) {
5080 seed_camelcase_file($realfile);
5081 $camelcase_file_seeded = 1;
5082 }
5083 }
Joe Perches7e781f62013-09-11 14:23:55 -07005084 if (!defined $camelcase{$word}) {
5085 $camelcase{$word} = 1;
5086 CHK("CAMELCASE",
5087 "Avoid CamelCase: <$word>\n" . $herecurr);
5088 }
Joe Perches34456862013-07-03 15:05:34 -07005089 }
Joe Perches323c1262012-12-17 16:02:07 -08005090 }
5091 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005092
5093#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07005094 if ($line =~ /\#\s*define.*\\\s+$/) {
5095 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5096 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5097 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005098 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005099 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005100 }
5101
Fabian Frederick0e212e02015-04-16 12:44:25 -07005102# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5103# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005104 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005105 my $file = "$1.h";
5106 my $checkfile = "include/linux/$file";
5107 if (-f "$root/$checkfile" &&
5108 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07005109 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005110 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07005111 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5112 if ($asminclude > 0) {
5113 if ($realfile =~ m{^arch/}) {
5114 CHK("ARCH_INCLUDE_LINUX",
5115 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5116 } else {
5117 WARN("INCLUDE_LINUX",
5118 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5119 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07005120 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005121 }
5122 }
5123
Andy Whitcroft653d4872007-06-23 17:16:34 -07005124# multi-statement macros should be enclosed in a do while loop, grab the
5125# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08005126# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07005127 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5128 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005129 my $ln = $linenr;
5130 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005131 my ($off, $dstat, $dcond, $rest);
5132 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07005133 my $has_flow_statement = 0;
5134 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005135 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005136 ctx_statement_block($linenr, $realcnt, 0);
5137 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005138 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07005139 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005140
Joe Perches08a28432014-10-13 15:51:55 -07005141 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08005142 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07005143
Joe Perchesf59b64b2016-10-11 13:52:08 -07005144 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5145 my $define_args = $1;
5146 my $define_stmt = $dstat;
5147 my @def_args = ();
5148
5149 if (defined $define_args && $define_args ne "") {
5150 $define_args = substr($define_args, 1, length($define_args) - 2);
5151 $define_args =~ s/\s*//g;
Joe Perches8c8c45c2018-08-21 21:57:43 -07005152 $define_args =~ s/\\\+?//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005153 @def_args = split(",", $define_args);
5154 }
5155
Andy Whitcroft292f1a92008-07-23 21:29:11 -07005156 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005157 $dstat =~ s/\\\n.//g;
5158 $dstat =~ s/^\s*//s;
5159 $dstat =~ s/\s*$//s;
5160
5161 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005162 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5163 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005164 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07005165 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005166 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005167
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005168 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07005169 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5170 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07005171 {
5172 }
5173
Joe Perches42e15292016-03-15 14:58:01 -07005174 # Make asm volatile uses seem like a generic function
5175 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5176
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005177 my $exceptions = qr{
5178 $Declare|
5179 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07005180 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005181 DECLARE_PER_CPU|
5182 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08005183 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08005184 union|
5185 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07005186 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08005187 ^\"|\"$|
5188 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005189 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07005190 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07005191
5192 $ctx =~ s/\n*$//;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005193 my $stmt_cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005194 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005195
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005196 if ($dstat ne '' &&
5197 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5198 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07005199 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07005200 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005201 $dstat !~ /$exceptions/ &&
5202 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005203 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005204 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005205 $dstat !~ /^for\s*$Constant$/ && # for (...)
5206 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5207 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005208 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005209 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005210 {
Joe Perchese7955562017-05-08 15:55:48 -07005211 if ($dstat =~ /^\s*if\b/) {
5212 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5213 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5214 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005215 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5216 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5217 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005218 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005219 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005220 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005221
5222 }
Joe Perches52076492016-10-11 13:52:14 -07005223
5224 # Make $define_stmt single line, comment-free, etc
5225 my @stmt_array = split('\n', $define_stmt);
5226 my $first = 1;
5227 $define_stmt = "";
5228 foreach my $l (@stmt_array) {
5229 $l =~ s/\\$//;
5230 if ($first) {
5231 $define_stmt = $l;
5232 $first = 0;
5233 } elsif ($l =~ /^[\+ ]/) {
5234 $define_stmt .= substr($l, 1);
5235 }
5236 }
5237 $define_stmt =~ s/$;//g;
5238 $define_stmt =~ s/\s+/ /g;
5239 $define_stmt = trim($define_stmt);
5240
Joe Perchesf59b64b2016-10-11 13:52:08 -07005241# check if any macro arguments are reused (ignore '...' and 'type')
5242 foreach my $arg (@def_args) {
5243 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005244 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07005245 my $tmp_stmt = $define_stmt;
Brendan Jackman6dba8242019-09-25 16:46:41 -07005246 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches7fe528a22017-07-10 15:52:27 -07005247 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5248 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Joe Perchesd41362e2018-05-25 14:48:04 -07005249 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005250 if ($use_cnt > 1) {
5251 CHK("MACRO_ARG_REUSE",
5252 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005253 }
5254# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07005255 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07005256 ((defined($1) && $1 ne ',') ||
5257 (defined($2) && $2 ne ','))) {
5258 CHK("MACRO_ARG_PRECEDENCE",
5259 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005260 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005261 }
Joe Perches5023d342012-12-17 16:01:47 -08005262
Joe Perches08a28432014-10-13 15:51:55 -07005263# check for macros with flow control, but without ## concatenation
5264# ## concatenation is commonly a macro that defines a function so ignore those
5265 if ($has_flow_statement && !$has_arg_concat) {
Joe Perches08a28432014-10-13 15:51:55 -07005266 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005267 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perches08a28432014-10-13 15:51:55 -07005268
Joe Perches08a28432014-10-13 15:51:55 -07005269 WARN("MACRO_WITH_FLOW_CONTROL",
5270 "Macros with flow control statements should be avoided\n" . "$herectx");
5271 }
5272
Joe Perches481eb482012-12-17 16:01:56 -08005273# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005274
5275 } else {
5276 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005277 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5278 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005279 $line =~ /^\+.*\\$/) {
5280 WARN("LINE_CONTINUATIONS",
5281 "Avoid unnecessary line continuations\n" . $herecurr);
5282 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005283 }
5284
Joe Perchesb13edf72012-07-30 14:41:24 -07005285# do {} while (0) macro tests:
5286# single-statement macros do not need to be enclosed in do while (0) loop,
5287# macro should not end with a semicolon
Joe Perches5b579802018-08-21 21:57:33 -07005288 if ($perl_version_ok &&
Joe Perchesb13edf72012-07-30 14:41:24 -07005289 $realfile !~ m@/vmlinux.lds.h$@ &&
5290 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5291 my $ln = $linenr;
5292 my $cnt = $realcnt;
5293 my ($off, $dstat, $dcond, $rest);
5294 my $ctx = '';
5295 ($dstat, $dcond, $ln, $cnt, $off) =
5296 ctx_statement_block($linenr, $realcnt, 0);
5297 $ctx = $dstat;
5298
5299 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005300 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005301
5302 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5303 my $stmts = $2;
5304 my $semis = $3;
5305
5306 $ctx =~ s/\n*$//;
5307 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005308 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesb13edf72012-07-30 14:41:24 -07005309
Joe Perchesac8e97f2012-08-21 16:15:53 -07005310 if (($stmts =~ tr/;/;/) == 1 &&
5311 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005312 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5313 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5314 }
5315 if (defined $semis && $semis ne "") {
5316 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5317 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5318 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005319 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5320 $ctx =~ s/\n*$//;
5321 my $cnt = statement_rawlines($ctx);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005322 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005323
5324 WARN("TRAILING_SEMICOLON",
5325 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005326 }
5327 }
5328
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005329# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005330 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5331 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005332 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005333 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005334 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5335 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005336 my @allowed = ();
5337 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005338 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005339 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005340 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005341 for my $chunk (@chunks) {
5342 my ($cond, $block) = @{$chunk};
5343
Andy Whitcroft773647a2008-03-28 14:15:58 -07005344 # If the condition carries leading newlines, then count those as offsets.
5345 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5346 my $offset = statement_rawlines($whitespace) - 1;
5347
Joe Perchesaad4f612012-03-23 15:02:19 -07005348 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005349 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5350
5351 # We have looked at and allowed this specific line.
5352 $suppress_ifbraces{$ln + $offset} = 1;
5353
5354 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005355 $ln += statement_rawlines($block) - 1;
5356
Andy Whitcroft773647a2008-03-28 14:15:58 -07005357 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005358
5359 $seen++ if ($block =~ /^\s*{/);
5360
Joe Perchesaad4f612012-03-23 15:02:19 -07005361 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005362 if (statement_lines($cond) > 1) {
5363 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005364 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005365 }
5366 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005367 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005368 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005369 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005370 if (statement_block_size($block) > 1) {
5371 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005372 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005373 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005374 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005375 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005376 if ($seen) {
5377 my $sum_allowed = 0;
5378 foreach (@allowed) {
5379 $sum_allowed += $_;
5380 }
5381 if ($sum_allowed == 0) {
5382 WARN("BRACES",
5383 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5384 } elsif ($sum_allowed != $allow &&
5385 $seen != $allow) {
5386 CHK("BRACES",
5387 "braces {} should be used on all arms of this statement\n" . $herectx);
5388 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005389 }
5390 }
5391 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005392 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005393 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005394 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005395
Andy Whitcroftcf655042008-03-04 14:28:20 -08005396 # Check the pre-context.
5397 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5398 #print "APW: ALLOWED: pre<$1>\n";
5399 $allowed = 1;
5400 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005401
5402 my ($level, $endln, @chunks) =
5403 ctx_statement_full($linenr, $realcnt, $-[0]);
5404
Andy Whitcroftcf655042008-03-04 14:28:20 -08005405 # Check the condition.
5406 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005407 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005408 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005409 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005410 }
5411 if (statement_lines($cond) > 1) {
5412 #print "APW: ALLOWED: cond<$cond>\n";
5413 $allowed = 1;
5414 }
5415 if ($block =~/\b(?:if|for|while)\b/) {
5416 #print "APW: ALLOWED: block<$block>\n";
5417 $allowed = 1;
5418 }
5419 if (statement_block_size($block) > 1) {
5420 #print "APW: ALLOWED: lines block<$block>\n";
5421 $allowed = 1;
5422 }
5423 # Check the post-context.
5424 if (defined $chunks[1]) {
5425 my ($cond, $block) = @{$chunks[1]};
5426 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005427 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005428 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005429 if ($block =~ /^\s*\{/) {
5430 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5431 $allowed = 1;
5432 }
5433 }
5434 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Andy Whitcroftf0556632008-10-15 22:02:23 -07005435 my $cnt = statement_rawlines($block);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07005436 my $herectx = get_stat_here($linenr, $cnt, $here);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005437
Joe Perches000d1cc12011-07-25 17:13:25 -07005438 WARN("BRACES",
5439 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005440 }
5441 }
5442
Joe Perchese4c5bab2017-02-24 15:01:41 -08005443# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005444 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5445 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005446 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5447 }
5448
Joe Perches0979ae62012-12-17 16:01:59 -08005449# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005450 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005451 if (CHK("BRACES",
5452 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5453 $fix && $prevrawline =~ /^\+/) {
5454 fix_delete_line($fixlinenr - 1, $prevrawline);
5455 }
Joe Perches0979ae62012-12-17 16:01:59 -08005456 }
Joe Perches77b9a532013-07-03 15:05:29 -07005457 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005458 if (CHK("BRACES",
5459 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5460 $fix) {
5461 fix_delete_line($fixlinenr, $rawline);
5462 }
Joe Perches0979ae62012-12-17 16:01:59 -08005463 }
5464
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005465# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005466 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5467 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005468 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005469 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005470 }
5471
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005472# Check for user-visible strings broken across lines, which breaks the ability
5473# to grep for the string. Make exceptions when the previous string ends in a
5474# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5475# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005476 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005477 $prevline =~ /"\s*$/ &&
5478 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5479 if (WARN("SPLIT_STRING",
5480 "quoted string split across lines\n" . $hereprev) &&
5481 $fix &&
5482 $prevrawline =~ /^\+.*"\s*$/ &&
5483 $last_coalesced_string_linenr != $linenr - 1) {
5484 my $extracted_string = get_quoted_string($line, $rawline);
5485 my $comma_close = "";
5486 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5487 $comma_close = $1;
5488 }
5489
5490 fix_delete_line($fixlinenr - 1, $prevrawline);
5491 fix_delete_line($fixlinenr, $rawline);
5492 my $fixedline = $prevrawline;
5493 $fixedline =~ s/"\s*$//;
5494 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5495 fix_insert_line($fixlinenr - 1, $fixedline);
5496 $fixedline = $rawline;
5497 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5498 if ($fixedline !~ /\+\s*$/) {
5499 fix_insert_line($fixlinenr, $fixedline);
5500 }
5501 $last_coalesced_string_linenr = $linenr;
5502 }
5503 }
5504
5505# check for missing a space in a string concatenation
5506 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5507 WARN('MISSING_SPACE',
5508 "break quoted strings at a space character\n" . $hereprev);
5509 }
5510
Joe Perchese4b7d302017-05-08 15:55:51 -07005511# check for an embedded function name in a string when the function is known
5512# This does not work very well for -f --file checking as it depends on patch
5513# context providing the function name or a single line form for in-file
5514# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005515 if ($line =~ /^\+.*$String/ &&
5516 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005517 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5518 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005519 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005520 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005521 }
5522
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005523# check for spaces before a quoted newline
5524 if ($rawline =~ /^.*\".*\s\\n/) {
5525 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5526 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5527 $fix) {
5528 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5529 }
5530
5531 }
5532
Joe Perchesf17dba42014-10-13 15:51:51 -07005533# concatenated string without spaces between elements
Joe Perches79682c02018-08-21 21:57:29 -07005534 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5535 if (CHK("CONCATENATED_STRING",
5536 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5537 $fix) {
5538 while ($line =~ /($String)/g) {
5539 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5540 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5541 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5542 }
5543 }
Joe Perchesf17dba42014-10-13 15:51:51 -07005544 }
5545
Joe Perches90ad30e2014-12-10 15:51:59 -08005546# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005547 if ($line =~ /$String\s*"/) {
Joe Perches79682c02018-08-21 21:57:29 -07005548 if (WARN("STRING_FRAGMENTS",
5549 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5550 $fix) {
5551 while ($line =~ /($String)(?=\s*")/g) {
5552 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5553 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5554 }
5555 }
Joe Perches90ad30e2014-12-10 15:51:59 -08005556 }
5557
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005558# check for non-standard and hex prefixed decimal printf formats
5559 my $show_L = 1; #don't show the same defect twice
5560 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005561 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005562 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005563 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005564 # check for %L
5565 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005566 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005567 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5568 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005569 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005570 # check for %Z
5571 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5572 WARN("PRINTF_Z",
5573 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5574 $show_Z = 0;
5575 }
5576 # check for 0x<decimal>
5577 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5578 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005579 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5580 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005581 }
5582
5583# check for line continuations in quoted strings with odd counts of "
Joe Perches3f7f3352018-02-06 15:38:52 -08005584 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005585 WARN("LINE_CONTINUATIONS",
5586 "Avoid line continuations in quoted strings\n" . $herecurr);
5587 }
5588
Andy Whitcroft00df3442007-06-08 13:47:06 -07005589# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005590 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Prakruthi Deepak Heragu60f89012018-08-21 21:57:57 -07005591 WARN("IF_0",
5592 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5593 }
5594
5595# warn about #if 1
5596 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5597 WARN("IF_1",
5598 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005599 }
5600
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005601# check for needless "if (<foo>) fn(<foo>)" uses
5602 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005603 my $tested = quotemeta($1);
5604 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5605 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5606 my $func = $1;
5607 if (WARN('NEEDLESS_IF',
5608 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5609 $fix) {
5610 my $do_fix = 1;
5611 my $leading_tabs = "";
5612 my $new_leading_tabs = "";
5613 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5614 $leading_tabs = $1;
5615 } else {
5616 $do_fix = 0;
5617 }
5618 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5619 $new_leading_tabs = $1;
5620 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5621 $do_fix = 0;
5622 }
5623 } else {
5624 $do_fix = 0;
5625 }
5626 if ($do_fix) {
5627 fix_delete_line($fixlinenr - 1, $prevrawline);
5628 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5629 }
5630 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005631 }
5632 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005633
Joe Perchesebfdc402014-08-06 16:10:27 -07005634# check for unnecessary "Out of Memory" messages
5635 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5636 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5637 (defined $1 || defined $3) &&
5638 $linenr > 3) {
5639 my $testval = $2;
5640 my $testline = $lines[$linenr - 3];
5641
5642 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5643# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5644
Joe Perchese29a70f2019-03-07 16:28:35 -08005645 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5646 $s !~ /\b__GFP_NOWARN\b/ ) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005647 WARN("OOM_MESSAGE",
5648 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5649 }
5650 }
5651
Joe Perchesf78d98f2014-10-13 15:52:01 -07005652# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005653 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005654 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5655 my $level = $1;
5656 if (WARN("UNNECESSARY_KERN_LEVEL",
5657 "Possible unnecessary $level\n" . $herecurr) &&
5658 $fix) {
5659 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5660 }
5661 }
5662
Joe Perches45c55e92017-02-24 15:01:31 -08005663# check for logging continuations
5664 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5665 WARN("LOGGING_CONTINUATION",
5666 "Avoid logging continuation uses where feasible\n" . $herecurr);
5667 }
5668
Joe Perchesabb08a52014-12-10 15:51:46 -08005669# check for mask then right shift without a parentheses
Joe Perches5b579802018-08-21 21:57:33 -07005670 if ($perl_version_ok &&
Joe Perchesabb08a52014-12-10 15:51:46 -08005671 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5672 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5673 WARN("MASK_THEN_SHIFT",
5674 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5675 }
5676
Joe Perchesb75ac612014-12-10 15:52:02 -08005677# check for pointer comparisons to NULL
Joe Perches5b579802018-08-21 21:57:33 -07005678 if ($perl_version_ok) {
Joe Perchesb75ac612014-12-10 15:52:02 -08005679 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5680 my $val = $1;
5681 my $equal = "!";
5682 $equal = "" if ($4 eq "!=");
5683 if (CHK("COMPARISON_TO_NULL",
5684 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5685 $fix) {
5686 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5687 }
5688 }
5689 }
5690
Joe Perches8716de32013-09-11 14:24:05 -07005691# check for bad placement of section $InitAttribute (e.g.: __initdata)
5692 if ($line =~ /(\b$InitAttribute\b)/) {
5693 my $attr = $1;
5694 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5695 my $ptr = $1;
5696 my $var = $2;
5697 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5698 ERROR("MISPLACED_INIT",
5699 "$attr should be placed after $var\n" . $herecurr)) ||
5700 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5701 WARN("MISPLACED_INIT",
5702 "$attr should be placed after $var\n" . $herecurr))) &&
5703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005704 $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 -07005705 }
5706 }
5707 }
5708
Joe Perchese970b8842013-11-12 15:10:10 -08005709# check for $InitAttributeData (ie: __initdata) with const
5710 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5711 my $attr = $1;
5712 $attr =~ /($InitAttributePrefix)(.*)/;
5713 my $attr_prefix = $1;
5714 my $attr_type = $2;
5715 if (ERROR("INIT_ATTRIBUTE",
5716 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005718 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005719 s/$InitAttributeData/${attr_prefix}initconst/;
5720 }
5721 }
5722
5723# check for $InitAttributeConst (ie: __initconst) without const
5724 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5725 my $attr = $1;
5726 if (ERROR("INIT_ATTRIBUTE",
5727 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5728 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005729 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005730 /(^\+\s*(?:static\s+))/;
5731 $lead = rtrim($1);
5732 $lead = "$lead " if ($lead !~ /^\+$/);
5733 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005734 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005735 }
5736 }
5737
Joe Perchesc17893c2015-04-16 12:44:42 -07005738# check for __read_mostly with const non-pointer (should just be const)
5739 if ($line =~ /\b__read_mostly\b/ &&
5740 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5741 if (ERROR("CONST_READ_MOSTLY",
5742 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5743 $fix) {
5744 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5745 }
5746 }
5747
Joe Perchesfbdb8132014-04-03 14:49:14 -07005748# don't use __constant_<foo> functions outside of include/uapi/
5749 if ($realfile !~ m@^include/uapi/@ &&
5750 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5751 my $constant_func = $1;
5752 my $func = $constant_func;
5753 $func =~ s/^__constant_//;
5754 if (WARN("CONSTANT_CONVERSION",
5755 "$constant_func should be $func\n" . $herecurr) &&
5756 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005757 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005758 }
5759 }
5760
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005761# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005762 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005763 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005764 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005765 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005766 CHK("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005767 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Perches43c1d772014-04-03 14:49:11 -07005768 }
5769 if ($delay > 2000) {
5770 WARN("LONG_UDELAY",
5771 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005772 }
5773 }
5774
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005775# warn about unexpectedly long msleep's
5776 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5777 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005778 WARN("MSLEEP",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03005779 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005780 }
5781 }
5782
Joe Perches36ec1932013-07-03 15:05:25 -07005783# check for comparisons of jiffies
5784 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5785 WARN("JIFFIES_COMPARISON",
5786 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5787 }
5788
Joe Perches9d7a34a2013-07-03 15:05:26 -07005789# check for comparisons of get_jiffies_64()
5790 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5791 WARN("JIFFIES_COMPARISON",
5792 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5793 }
5794
Andy Whitcroft00df3442007-06-08 13:47:06 -07005795# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005796# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005797# print "#ifdef in C files should be avoided\n";
5798# print "$herecurr";
5799# $clean = 0;
5800# }
5801
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005802# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005803 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005804 if (ERROR("SPACING",
5805 "exactly one space required after that #$1\n" . $herecurr) &&
5806 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005807 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005808 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5809 }
5810
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005811 }
5812
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005813# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005814 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5815 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005816 my $which = $1;
5817 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005818 CHK("UNCOMMENTED_DEFINITION",
5819 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005820 }
5821 }
5822# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005823
5824 my $barriers = qr{
5825 mb|
5826 rmb|
5827 wmb|
5828 read_barrier_depends
5829 }x;
5830 my $barrier_stems = qr{
5831 mb__before_atomic|
5832 mb__after_atomic|
5833 store_release|
5834 load_acquire|
5835 store_mb|
5836 (?:$barriers)
5837 }x;
5838 my $all_barriers = qr{
5839 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005840 smp_(?:$barrier_stems)|
5841 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005842 }x;
5843
5844 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005845 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005846 WARN("MEMORY_BARRIER",
5847 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005848 }
5849 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005850
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005851 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5852
5853 if ($realfile !~ m@^include/asm-generic/@ &&
5854 $realfile !~ m@/barrier\.h$@ &&
5855 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5856 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5857 WARN("MEMORY_BARRIER",
5858 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5859 }
5860
Joe Perchescb426e92015-06-25 15:02:46 -07005861# check for waitqueue_active without a comment.
5862 if ($line =~ /\bwaitqueue_active\s*\(/) {
5863 if (!ctx_has_comment($first_line, $linenr)) {
5864 WARN("WAITQUEUE_ACTIVE",
5865 "waitqueue_active without comment\n" . $herecurr);
5866 }
5867 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005868
Paul E. McKenney91db2592017-11-27 09:37:35 -08005869# check for smp_read_barrier_depends and read_barrier_depends
5870 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5871 WARN("READ_BARRIER_DEPENDS",
5872 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5873 }
5874
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005875# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005876 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005877 CHK("ARCH_DEFINES",
5878 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005879 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005880
Joe Perches596ed452017-07-12 14:37:02 -07005881# check that the storage class is not after a type
5882 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005883 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005884 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5885 }
5886# Check that the storage class is at the beginning of a declaration
5887 if ($line =~ /\b$Storage\b/ &&
5888 $line !~ /^.\s*$Storage/ &&
5889 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5890 $1 !~ /[\,\)]\s*$/) {
5891 WARN("STORAGE_CLASS",
5892 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005893 }
5894
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005895# check the location of the inline attribute, that it is between
5896# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005897 if ($line =~ /\b$Type\s+$Inline\b/ ||
5898 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005899 ERROR("INLINE_LOCATION",
5900 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005901 }
5902
Andy Whitcroft8905a672007-11-28 16:21:06 -08005903# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005904 if ($realfile !~ m@\binclude/uapi/@ &&
5905 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005906 if (WARN("INLINE",
5907 "plain inline is preferred over $1\n" . $herecurr) &&
5908 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005909 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005910
5911 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005912 }
5913
Joe Perches3d130fd2011-01-12 17:00:00 -08005914# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005915 if ($realfile !~ m@\binclude/uapi/@ &&
5916 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005917 WARN("PREFER_PACKED",
5918 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005919 }
5920
Joe Perches39b7e282011-07-25 17:13:24 -07005921# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005922 if ($realfile !~ m@\binclude/uapi/@ &&
5923 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005924 WARN("PREFER_ALIGNED",
5925 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005926 }
5927
Joe Perches462811d2019-09-25 16:46:44 -07005928# Check for __attribute__ section, prefer __section
5929 if ($realfile !~ m@\binclude/uapi/@ &&
5930 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5931 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5932 my $new = substr($old, 1, -1);
5933 if (WARN("PREFER_SECTION",
5934 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5935 $fix) {
5936 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5937 }
5938 }
5939
Joe Perches5f14d3b2012-01-10 15:09:52 -08005940# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005941 if ($realfile !~ m@\binclude/uapi/@ &&
5942 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005943 if (WARN("PREFER_PRINTF",
5944 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5945 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005946 $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 -07005947
5948 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005949 }
5950
Joe Perches6061d942012-03-23 15:02:16 -07005951# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005952 if ($realfile !~ m@\binclude/uapi/@ &&
5953 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005954 if (WARN("PREFER_SCANF",
5955 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5956 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005957 $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 -07005958 }
Joe Perches6061d942012-03-23 15:02:16 -07005959 }
5960
Joe Perches619a9082014-12-10 15:51:35 -08005961# Check for __attribute__ weak, or __weak declarations (may have link issues)
Joe Perches5b579802018-08-21 21:57:33 -07005962 if ($perl_version_ok &&
Joe Perches619a9082014-12-10 15:51:35 -08005963 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5964 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5965 $line =~ /\b__weak\b/)) {
5966 ERROR("WEAK_DECLARATION",
5967 "Using weak declarations can have unintended link defects\n" . $herecurr);
5968 }
5969
Tomas Winklerfd39f902016-12-12 16:46:34 -08005970# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005971 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005972 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005973 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5974 my $type = $1;
5975 if ($type =~ /\b($typeC99Typedefs)\b/) {
5976 $type = $1;
5977 my $kernel_type = 'u';
5978 $kernel_type = 's' if ($type =~ /^_*[si]/);
5979 $type =~ /(\d+)/;
5980 $kernel_type .= $1;
5981 if (CHK("PREFER_KERNEL_TYPES",
5982 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5983 $fix) {
5984 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5985 }
5986 }
5987 }
5988
Joe Perches938224b2016-01-20 14:59:15 -08005989# check for cast of C90 native int or longer types constants
5990 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5991 my $cast = $1;
5992 my $const = $2;
5993 if (WARN("TYPECAST_INT_CONSTANT",
5994 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5995 $fix) {
5996 my $suffix = "";
5997 my $newconst = $const;
5998 $newconst =~ s/${Int_type}$//;
5999 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6000 if ($cast =~ /\blong\s+long\b/) {
6001 $suffix .= 'LL';
6002 } elsif ($cast =~ /\blong\b/) {
6003 $suffix .= 'L';
6004 }
6005 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6006 }
6007 }
6008
Joe Perches8f53a9b2010-03-05 13:43:48 -08006009# check for sizeof(&)
6010 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006011 WARN("SIZEOF_ADDRESS",
6012 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08006013 }
6014
Joe Perches66c80b62012-07-30 14:41:22 -07006015# check for sizeof without parenthesis
6016 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006017 if (WARN("SIZEOF_PARENTHESIS",
6018 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6019 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006020 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006021 }
Joe Perches66c80b62012-07-30 14:41:22 -07006022 }
6023
Joe Perches88982fe2012-12-17 16:02:00 -08006024# check for struct spinlock declarations
6025 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6026 WARN("USE_SPINLOCK_T",
6027 "struct spinlock should be spinlock_t\n" . $herecurr);
6028 }
6029
Joe Perchesa6962d72013-04-29 16:18:13 -07006030# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08006031 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07006032 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08006033 $fmt =~ s/%%//g;
6034 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006035 if (WARN("PREFER_SEQ_PUTS",
6036 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6037 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006038 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006039 }
Joe Perchesa6962d72013-04-29 16:18:13 -07006040 }
6041 }
6042
Joe Perches478b1792018-04-10 16:33:34 -07006043# check for vsprintf extension %p<foo> misuses
Joe Perches5b579802018-08-21 21:57:33 -07006044 if ($perl_version_ok &&
Joe Perches0b523762017-05-08 15:55:36 -07006045 defined $stat &&
6046 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6047 $1 !~ /^_*volatile_*$/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006048 my $stat_real;
6049
Joe Perches0b523762017-05-08 15:55:36 -07006050 my $lc = $stat =~ tr@\n@@;
6051 $lc = $lc + $linenr;
6052 for (my $count = $linenr; $count <= $lc; $count++) {
Joe Perchesffe07512018-07-13 16:59:23 -07006053 my $specifier;
6054 my $extension;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006055 my $qualifier;
Joe Perchesffe07512018-07-13 16:59:23 -07006056 my $bad_specifier = "";
Joe Perches0b523762017-05-08 15:55:36 -07006057 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6058 $fmt =~ s/%%//g;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006059
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006060 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006061 $specifier = $1;
6062 $extension = $2;
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006063 $qualifier = $3;
Linus Torvalds361b0d22019-11-26 19:45:12 -08006064 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
Sakari Ailus3bd32d62019-10-03 15:32:18 +03006065 ($extension eq "f" &&
6066 defined $qualifier && $qualifier !~ /^w/)) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006067 $bad_specifier = $specifier;
6068 last;
6069 }
6070 if ($extension eq "x" && !defined($stat_real)) {
6071 if (!defined($stat_real)) {
6072 $stat_real = get_stat_real($linenr, $lc);
6073 }
6074 WARN("VSPRINTF_SPECIFIER_PX",
6075 "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");
6076 }
6077 }
6078 if ($bad_specifier ne "") {
6079 my $stat_real = get_stat_real($linenr, $lc);
6080 my $ext_type = "Invalid";
6081 my $use = "";
6082 if ($bad_specifier =~ /p[Ff]/) {
Tobin C. Hardinge3c6bc92018-04-10 16:33:31 -07006083 $use = " - use %pS instead";
6084 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6085 }
6086
6087 WARN("VSPRINTF_POINTER_EXTENSION",
6088 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6089 }
Joe Perches0b523762017-05-08 15:55:36 -07006090 }
6091 }
6092
Andy Whitcroft554e1652012-01-10 15:09:57 -08006093# Check for misused memsets
Joe Perches5b579802018-08-21 21:57:33 -07006094 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006095 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07006096 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08006097
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006098 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006099 my $ms_val = $7;
6100 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006101
Andy Whitcroft554e1652012-01-10 15:09:57 -08006102 if ($ms_size =~ /^(0x|)0$/i) {
6103 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006104 "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 -08006105 } elsif ($ms_size =~ /^(0x|)1$/i) {
6106 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006107 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6108 }
6109 }
6110
Joe Perches98a9bba2014-01-23 15:54:52 -08006111# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006112# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006113# defined $stat &&
6114# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6115# if (WARN("PREFER_ETHER_ADDR_COPY",
6116# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6117# $fix) {
6118# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6119# }
6120# }
Joe Perches98a9bba2014-01-23 15:54:52 -08006121
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006122# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perches5b579802018-08-21 21:57:33 -07006123# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006124# defined $stat &&
6125# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6126# WARN("PREFER_ETHER_ADDR_EQUAL",
6127# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6128# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07006129
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006130# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6131# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perches5b579802018-08-21 21:57:33 -07006132# if ($perl_version_ok &&
Joe Perchesf333195d2016-10-11 13:51:53 -07006133# defined $stat &&
6134# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6135#
6136# my $ms_val = $7;
6137#
6138# if ($ms_val =~ /^(?:0x|)0+$/i) {
6139# if (WARN("PREFER_ETH_ZERO_ADDR",
6140# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6141# $fix) {
6142# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6143# }
6144# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6145# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6146# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6147# $fix) {
6148# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6149# }
6150# }
6151# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07006152
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006153# typecasts on min/max could be min_t/max_t
Joe Perches5b579802018-08-21 21:57:33 -07006154 if ($perl_version_ok &&
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006155 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006156 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006157 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006158 my $call = $1;
6159 my $cast1 = deparenthesize($2);
6160 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006161 my $cast2 = deparenthesize($7);
6162 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006163 my $cast;
6164
Joe Perchesd1fe9c02012-03-23 15:02:16 -07006165 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08006166 $cast = "$cast1 or $cast2";
6167 } elsif ($cast1 ne "") {
6168 $cast = $cast1;
6169 } else {
6170 $cast = $cast2;
6171 }
6172 WARN("MINMAX",
6173 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08006174 }
6175 }
6176
Joe Perches4a273192012-07-30 14:41:20 -07006177# check usleep_range arguments
Joe Perches5b579802018-08-21 21:57:33 -07006178 if ($perl_version_ok &&
Joe Perches4a273192012-07-30 14:41:20 -07006179 defined $stat &&
6180 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6181 my $min = $1;
6182 my $max = $7;
6183 if ($min eq $max) {
6184 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006185 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006186 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6187 $min > $max) {
6188 WARN("USLEEP_RANGE",
Mauro Carvalho Chehab458f69e2019-06-12 14:53:00 -03006189 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Joe Perches4a273192012-07-30 14:41:20 -07006190 }
6191 }
6192
Joe Perches823b7942013-11-12 15:10:15 -08006193# check for naked sscanf
Joe Perches5b579802018-08-21 21:57:33 -07006194 if ($perl_version_ok &&
Joe Perches823b7942013-11-12 15:10:15 -08006195 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006196 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006197 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6198 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6199 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6200 my $lc = $stat =~ tr@\n@@;
6201 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006202 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches823b7942013-11-12 15:10:15 -08006203 WARN("NAKED_SSCANF",
6204 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6205 }
6206
Joe Perchesafc819a2014-06-04 16:12:08 -07006207# check for simple sscanf that should be kstrto<foo>
Joe Perches5b579802018-08-21 21:57:33 -07006208 if ($perl_version_ok &&
Joe Perchesafc819a2014-06-04 16:12:08 -07006209 defined $stat &&
6210 $line =~ /\bsscanf\b/) {
6211 my $lc = $stat =~ tr@\n@@;
6212 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006213 my $stat_real = get_stat_real($linenr, $lc);
Joe Perchesafc819a2014-06-04 16:12:08 -07006214 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6215 my $format = $6;
6216 my $count = $format =~ tr@%@%@;
6217 if ($count == 1 &&
6218 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6219 WARN("SSCANF_TO_KSTRTO",
6220 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6221 }
6222 }
6223 }
6224
Joe Perches70dc8a42013-09-11 14:23:58 -07006225# check for new externs in .h files.
6226 if ($realfile =~ /\.h$/ &&
6227 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006228 if (CHK("AVOID_EXTERNS",
6229 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006230 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006231 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006232 }
6233 }
6234
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006235# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006236 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006237 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006238 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006239 my $function_name = $1;
6240 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006241
6242 my $s = $stat;
6243 if (defined $cond) {
6244 substr($s, 0, length($cond), '');
6245 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006246 if ($s =~ /^\s*;/ &&
6247 $function_name ne 'uninitialized_var')
6248 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006249 WARN("AVOID_EXTERNS",
6250 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006251 }
6252
6253 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006254 WARN("FUNCTION_ARGUMENTS",
6255 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006256 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006257
6258 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6259 $stat =~ /^.\s*extern\s+/)
6260 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006261 WARN("AVOID_EXTERNS",
6262 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006263 }
6264
Joe Perchesa0ad7592017-07-10 15:52:19 -07006265# check for function declarations that have arguments without identifier names
6266 if (defined $stat &&
Miles Chen25bdda22017-11-17 15:28:34 -08006267 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Joe Perchesca0d8922016-10-11 13:52:16 -07006268 $1 ne "void") {
6269 my $args = trim($1);
6270 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6271 my $arg = trim($1);
6272 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6273 WARN("FUNCTION_ARGUMENTS",
6274 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6275 }
6276 }
6277 }
6278
Joe Perchesa0ad7592017-07-10 15:52:19 -07006279# check for function definitions
Joe Perches5b579802018-08-21 21:57:33 -07006280 if ($perl_version_ok &&
Joe Perchesa0ad7592017-07-10 15:52:19 -07006281 defined $stat &&
6282 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6283 $context_function = $1;
6284
6285# check for multiline function definition with misplaced open brace
6286 my $ok = 0;
6287 my $cnt = statement_rawlines($stat);
6288 my $herectx = $here . "\n";
6289 for (my $n = 0; $n < $cnt; $n++) {
6290 my $rl = raw_line($linenr, $n);
6291 $herectx .= $rl . "\n";
6292 $ok = 1 if ($rl =~ /^[ \+]\{/);
6293 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6294 last if $rl =~ /^[ \+].*\{/;
6295 }
6296 if (!$ok) {
6297 ERROR("OPEN_BRACE",
6298 "open brace '{' following function definitions go on the next line\n" . $herectx);
6299 }
6300 }
6301
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006302# checks for new __setup's
6303 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6304 my $name = $1;
6305
6306 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006307 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02006308 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006309 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006310 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006311
Joe Perchese29a70f2019-03-07 16:28:35 -08006312# check for pointless casting of alloc functions
6313 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006314 WARN("UNNECESSARY_CASTS",
6315 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006316 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006317
Joe Perchesa640d252013-07-03 15:05:21 -07006318# alloc style
6319# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Joe Perches5b579802018-08-21 21:57:33 -07006320 if ($perl_version_ok &&
Joe Perchese29a70f2019-03-07 16:28:35 -08006321 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Joe Perchesa640d252013-07-03 15:05:21 -07006322 CHK("ALLOC_SIZEOF_STRUCT",
6323 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6324 }
6325
Joe Perches60a55362014-06-04 16:12:07 -07006326# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Joe Perches5b579802018-08-21 21:57:33 -07006327 if ($perl_version_ok &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006328 defined $stat &&
6329 $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 -07006330 my $oldfunc = $3;
6331 my $a1 = $4;
6332 my $a2 = $10;
6333 my $newfunc = "kmalloc_array";
6334 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006335 my $r1 = $a1;
6336 my $r2 = $a2;
6337 if ($a1 =~ /^sizeof\s*\S/) {
6338 $r1 = $a2;
6339 $r2 = $a1;
6340 }
6341 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6342 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006343 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006344 my $herectx = get_stat_here($linenr, $cnt, $here);
6345
Joe Perches60a55362014-06-04 16:12:07 -07006346 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006347 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6348 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006349 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006350 $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 -07006351 }
6352 }
6353 }
6354
Joe Perches972fdea2013-04-29 16:18:12 -07006355# check for krealloc arg reuse
Joe Perches5b579802018-08-21 21:57:33 -07006356 if ($perl_version_ok &&
Joe Perches4cab63c2018-08-21 21:57:50 -07006357 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6358 $1 eq $3) {
Joe Perches972fdea2013-04-29 16:18:12 -07006359 WARN("KREALLOC_ARG_REUSE",
6360 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6361 }
6362
Joe Perches5ce59ae2013-02-21 16:44:18 -08006363# check for alloc argument mismatch
6364 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6365 WARN("ALLOC_ARRAY_ARGS",
6366 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6367 }
6368
Joe Perchescaf2a542011-01-12 16:59:56 -08006369# check for multiple semicolons
6370 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006371 if (WARN("ONE_SEMICOLON",
6372 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6373 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006374 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006375 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006376 }
6377
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006378# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6379 if ($realfile !~ m@^include/uapi/@ &&
6380 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006381 my $ull = "";
6382 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6383 if (CHK("BIT_MACRO",
6384 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6385 $fix) {
6386 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6387 }
6388 }
6389
Joe Perches2d632742016-05-20 17:04:00 -07006390# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6391 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6392 my $config = $1;
6393 if (WARN("PREFER_IS_ENABLED",
6394 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6395 $fix) {
6396 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6397 }
6398 }
6399
Joe Perchese81f2392014-08-06 16:11:25 -07006400# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006401 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6402 my $has_break = 0;
6403 my $has_statement = 0;
6404 my $count = 0;
6405 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006406 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006407 $prevline--;
6408 my $rline = $rawlines[$prevline - 1];
6409 my $fline = $lines[$prevline - 1];
6410 last if ($fline =~ /^\@\@/);
6411 next if ($fline =~ /^\-/);
6412 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6413 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6414 next if ($fline =~ /^.[\s$;]*$/);
6415 $has_statement = 1;
6416 $count++;
Heinrich Schuchardt258f79d2017-11-17 15:28:38 -08006417 $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 -08006418 }
6419 if (!$has_break && $has_statement) {
6420 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006421 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006422 }
6423 }
6424
Joe Perchesf36d3eb2020-04-06 20:10:58 -07006425# check for /* fallthrough */ like comment, prefer fallthrough;
6426 my @fallthroughs = (
6427 'fallthrough',
6428 '@fallthrough@',
6429 'lint -fallthrough[ \t]*',
6430 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6431 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6432 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6433 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6434 );
6435 if ($raw_comment ne '') {
6436 foreach my $ft (@fallthroughs) {
6437 if ($raw_comment =~ /$ft/) {
6438 my $msg_level = \&WARN;
6439 $msg_level = \&CHK if ($file);
6440 &{$msg_level}("PREFER_FALLTHROUGH",
6441 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6442 last;
6443 }
6444 }
6445 }
6446
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006447# check for switch/default statements without a break;
Joe Perches5b579802018-08-21 21:57:33 -07006448 if ($perl_version_ok &&
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006449 defined $stat &&
6450 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006451 my $cnt = statement_rawlines($stat);
Tobin C. Hardinge3d95a22018-04-10 16:33:27 -07006452 my $herectx = get_stat_here($linenr, $cnt, $here);
6453
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006454 WARN("DEFAULT_NO_BREAK",
6455 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006456 }
6457
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006458# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006459 if ($line =~ /\b__FUNCTION__\b/) {
6460 if (WARN("USE_FUNC",
6461 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6462 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006463 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006464 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006465 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006466
Joe Perches62ec8182015-02-13 14:38:18 -08006467# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6468 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6469 ERROR("DATE_TIME",
6470 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6471 }
6472
Joe Perches2c924882012-03-23 15:02:20 -07006473# check for use of yield()
6474 if ($line =~ /\byield\s*\(\s*\)/) {
6475 WARN("YIELD",
6476 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6477 }
6478
Joe Perches179f8f42013-07-03 15:05:30 -07006479# check for comparisons against true and false
6480 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6481 my $lead = $1;
6482 my $arg = $2;
6483 my $test = $3;
6484 my $otype = $4;
6485 my $trail = $5;
6486 my $op = "!";
6487
6488 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6489
6490 my $type = lc($otype);
6491 if ($type =~ /^(?:true|false)$/) {
6492 if (("$test" eq "==" && "$type" eq "true") ||
6493 ("$test" eq "!=" && "$type" eq "false")) {
6494 $op = "";
6495 }
6496
6497 CHK("BOOL_COMPARISON",
6498 "Using comparison to $otype is error prone\n" . $herecurr);
6499
6500## maybe suggesting a correct construct would better
6501## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6502
6503 }
6504 }
6505
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006506# check for semaphores initialized locked
6507 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006508 WARN("CONSIDER_COMPLETION",
6509 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006510 }
Joe Perches6712d852012-03-23 15:02:20 -07006511
Joe Perches67d0a072011-10-31 17:13:10 -07006512# recommend kstrto* over simple_strto* and strict_strto*
6513 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006514 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006515 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006516 }
Joe Perches6712d852012-03-23 15:02:20 -07006517
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006518# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006519 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006520 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006521 "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 -07006522 }
Joe Perches6712d852012-03-23 15:02:20 -07006523
Paul E. McKenney3d709ab2018-11-11 10:49:10 -08006524# check for spin_is_locked(), suggest lockdep instead
6525 if ($line =~ /\bspin_is_locked\(/) {
6526 WARN("USE_LOCKDEP",
6527 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6528 }
6529
Joe Perches9189c7e2018-09-07 15:26:18 -07006530# check for deprecated apis
6531 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6532 my $deprecated_api = $1;
6533 my $new_api = $deprecated_apis{$deprecated_api};
6534 WARN("DEPRECATED_API",
6535 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6536 }
6537
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006538# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006539# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006540 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006541 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006542 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006543 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006544 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006545
6546# use of NR_CPUS is usually wrong
6547# ignore definitions of NR_CPUS and usage to define arrays as likely right
6548 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006549 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6550 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006551 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6552 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6553 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006554 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006555 WARN("NR_CPUS",
6556 "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 -07006557 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006558
Joe Perches52ea8502013-11-12 15:10:09 -08006559# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6560 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6561 ERROR("DEFINE_ARCH_HAS",
6562 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6563 }
6564
Joe Perchesacd93622015-02-13 14:38:38 -08006565# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Joe Perches5b579802018-08-21 21:57:33 -07006566 if ($perl_version_ok &&
Joe Perchesacd93622015-02-13 14:38:38 -08006567 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6568 WARN("LIKELY_MISUSE",
6569 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6570 }
6571
Denis Efremovde3f1862019-09-25 16:49:25 -07006572# nested likely/unlikely calls
6573 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6574 WARN("LIKELY_MISUSE",
6575 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6576 }
6577
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006578# whine mightly about in_atomic
6579 if ($line =~ /\bin_atomic\s*\(/) {
6580 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006581 ERROR("IN_ATOMIC",
6582 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006583 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006584 WARN("IN_ATOMIC",
6585 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006586 }
6587 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006588
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006589# check for mutex_trylock_recursive usage
6590 if ($line =~ /mutex_trylock_recursive/) {
6591 ERROR("LOCKING",
6592 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6593 }
6594
Peter Zijlstra1704f472010-03-19 01:37:42 +01006595# check for lockdep_set_novalidate_class
6596 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6597 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6598 if ($realfile !~ m@^kernel/lockdep@ &&
6599 $realfile !~ m@^include/linux/lockdep@ &&
6600 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006601 ERROR("LOCKDEP",
6602 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006603 }
6604 }
Dave Jones88f88312011-01-12 16:59:59 -08006605
Joe Perchesb392c642015-04-16 12:44:16 -07006606 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6607 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006608 WARN("EXPORTED_WORLD_WRITABLE",
6609 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006610 }
Joe Perches24358802014-04-03 14:49:13 -07006611
Joe Perches00180462018-02-06 15:38:55 -08006612# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6613# and whether or not function naming is typical and if
6614# DEVICE_ATTR permissions uses are unusual too
Joe Perches5b579802018-08-21 21:57:33 -07006615 if ($perl_version_ok &&
Joe Perches00180462018-02-06 15:38:55 -08006616 defined $stat &&
6617 $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*\)/) {
6618 my $var = $1;
6619 my $perms = $2;
6620 my $show = $3;
6621 my $store = $4;
6622 my $octal_perms = perms_to_octal($perms);
6623 if ($show =~ /^${var}_show$/ &&
6624 $store =~ /^${var}_store$/ &&
6625 $octal_perms eq "0644") {
6626 if (WARN("DEVICE_ATTR_RW",
6627 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6628 $fix) {
6629 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6630 }
6631 } elsif ($show =~ /^${var}_show$/ &&
6632 $store =~ /^NULL$/ &&
6633 $octal_perms eq "0444") {
6634 if (WARN("DEVICE_ATTR_RO",
6635 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6636 $fix) {
6637 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6638 }
6639 } elsif ($show =~ /^NULL$/ &&
6640 $store =~ /^${var}_store$/ &&
6641 $octal_perms eq "0200") {
6642 if (WARN("DEVICE_ATTR_WO",
6643 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6644 $fix) {
6645 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6646 }
6647 } elsif ($octal_perms eq "0644" ||
6648 $octal_perms eq "0444" ||
6649 $octal_perms eq "0200") {
6650 my $newshow = "$show";
6651 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6652 my $newstore = $store;
6653 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6654 my $rename = "";
6655 if ($show ne $newshow) {
6656 $rename .= " '$show' to '$newshow'";
6657 }
6658 if ($store ne $newstore) {
6659 $rename .= " '$store' to '$newstore'";
6660 }
6661 WARN("DEVICE_ATTR_FUNCTIONS",
6662 "Consider renaming function(s)$rename\n" . $herecurr);
6663 } else {
6664 WARN("DEVICE_ATTR_PERMS",
6665 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6666 }
6667 }
6668
Joe Perches515a2352014-04-03 14:49:24 -07006669# Mode permission misuses where it seems decimal should be octal
6670# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Joe Perches73121532018-02-06 15:38:49 -08006671# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6672# specific definition of not visible in sysfs.
6673# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6674# use the default permissions
Joe Perches5b579802018-08-21 21:57:33 -07006675 if ($perl_version_ok &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006676 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006677 $line =~ /$mode_perms_search/) {
6678 foreach my $entry (@mode_permission_funcs) {
6679 my $func = $entry->[0];
6680 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006681
Joe Perches459cf0a2016-10-11 13:52:19 -07006682 my $lc = $stat =~ tr@\n@@;
6683 $lc = $lc + $linenr;
Tobin C. Harding2a9f9d82018-04-10 16:33:20 -07006684 my $stat_real = get_stat_real($linenr, $lc);
Joe Perches459cf0a2016-10-11 13:52:19 -07006685
Joe Perches515a2352014-04-03 14:49:24 -07006686 my $skip_args = "";
6687 if ($arg_pos > 1) {
6688 $arg_pos--;
6689 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6690 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006691 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006692 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006693 my $val = $1;
6694 $val = $6 if ($skip_args ne "");
Joe Perches73121532018-02-06 15:38:49 -08006695 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6696 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6697 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Joe Perches515a2352014-04-03 14:49:24 -07006698 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006699 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006700 }
6701 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006702 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006703 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006704 }
Joe Perches24358802014-04-03 14:49:13 -07006705 }
6706 }
6707 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006708
Joe Perches459cf0a2016-10-11 13:52:19 -07006709# check for uses of S_<PERMS> that could be octal for readability
Joe Perchesbc22d9a2018-04-10 16:33:53 -07006710 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
Joe Perches00180462018-02-06 15:38:55 -08006711 my $oval = $1;
6712 my $octal = perms_to_octal($oval);
Joe Perches459cf0a2016-10-11 13:52:19 -07006713 if (WARN("SYMBOLIC_PERMS",
6714 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6715 $fix) {
Joe Perches00180462018-02-06 15:38:55 -08006716 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Joe Perches459cf0a2016-10-11 13:52:19 -07006717 }
6718 }
6719
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006720# validate content of MODULE_LICENSE against list from include/linux/module.h
6721 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6722 my $extracted_string = get_quoted_string($line, $rawline);
6723 my $valid_licenses = qr{
6724 GPL|
6725 GPL\ v2|
6726 GPL\ and\ additional\ rights|
6727 Dual\ BSD/GPL|
6728 Dual\ MIT/GPL|
6729 Dual\ MPL/GPL|
6730 Proprietary
6731 }x;
6732 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6733 WARN("MODULE_LICENSE",
6734 "unknown module license " . $extracted_string . "\n" . $herecurr);
6735 }
6736 }
Matteo Croce6a8d76c2019-07-16 16:27:48 -07006737
6738# check for sysctl duplicate constants
6739 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6740 WARN("DUPLICATED_SYSCTL_CONST",
6741 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6742 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006743 }
6744
6745 # If we have no input at all, then there is nothing to report on
6746 # so just keep quiet.
6747 if ($#rawlines == -1) {
6748 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006749 }
6750
Andy Whitcroft8905a672007-11-28 16:21:06 -08006751 # In mailback mode only produce a report in the negative, for
6752 # things that appear to be patches.
6753 if ($mailback && ($clean == 1 || !$is_patch)) {
6754 exit(0);
6755 }
6756
6757 # This is not a patch, and we are are in 'no-patch' mode so
6758 # just keep quiet.
6759 if (!$chk_patch && !$is_patch) {
6760 exit(0);
6761 }
6762
Stafford Hornea08ffbe2017-10-03 16:16:51 -07006763 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006764 ERROR("NOT_UNIFIED_DIFF",
6765 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006766 }
Geert Uytterhoevencd261492018-08-21 21:57:40 -07006767 if ($is_patch && $has_commit_log && $chk_signoff) {
6768 if ($signoff == 0) {
6769 ERROR("MISSING_SIGN_OFF",
6770 "Missing Signed-off-by: line(s)\n");
6771 } elsif (!$authorsignoff) {
6772 WARN("NO_AUTHOR_SIGN_OFF",
6773 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6774 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006775 }
6776
Andy Whitcroft8905a672007-11-28 16:21:06 -08006777 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006778 if ($summary && !($clean == 1 && $quiet == 1)) {
6779 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006780 print "total: $cnt_error errors, $cnt_warn warnings, " .
6781 (($check)? "$cnt_chk checks, " : "") .
6782 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006783 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006784
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006785 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006786 # If there were any defects found and not already fixing them
6787 if (!$clean and !$fix) {
6788 print << "EOM"
6789
6790NOTE: For some of the reported defects, checkpatch may be able to
6791 mechanically convert to the typical style using --fix or --fix-inplace.
6792EOM
6793 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006794 # If there were whitespace errors which cleanpatch can fix
6795 # then suggest that.
6796 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006797 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006798 print << "EOM"
6799
6800NOTE: Whitespace errors detected.
6801 You may wish to use scripts/cleanpatch or scripts/cleanfile
6802EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006803 }
6804 }
6805
Joe Perchesd752fcc2014-08-06 16:11:05 -07006806 if ($clean == 0 && $fix &&
6807 ("@rawlines" ne "@fixed" ||
6808 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006809 my $newfile = $filename;
6810 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006811 my $linecount = 0;
6812 my $f;
6813
Joe Perchesd752fcc2014-08-06 16:11:05 -07006814 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6815
Joe Perches3705ce52013-07-03 15:05:31 -07006816 open($f, '>', $newfile)
6817 or die "$P: Can't open $newfile for write\n";
6818 foreach my $fixed_line (@fixed) {
6819 $linecount++;
6820 if ($file) {
6821 if ($linecount > 3) {
6822 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006823 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006824 }
6825 } else {
6826 print $f $fixed_line . "\n";
6827 }
6828 }
6829 close($f);
6830
6831 if (!$quiet) {
6832 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006833
Joe Perches3705ce52013-07-03 15:05:31 -07006834Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6835
6836Do _NOT_ trust the results written to this file.
6837Do _NOT_ submit these changes without inspecting them for correctness.
6838
6839This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6840No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006841EOM
6842 }
6843 }
6844
Joe Perchesd8469f12015-06-25 15:03:00 -07006845 if ($quiet == 0) {
6846 print "\n";
6847 if ($clean == 1) {
6848 print "$vname has no obvious style problems and is ready for submission.\n";
6849 } else {
6850 print "$vname has style problems, please review.\n";
6851 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006852 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006853 return $clean;
6854}