blob: 1053d29ce94e225c5236ad17acd0dea53c546209 [file] [log] [blame]
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001#!/usr/bin/env perl
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002# SPDX-License-Identifier: GPL-2.0
3#
Joe Hershberger05622192011-10-18 10:06:59 +00004# (c) 2001, Dave Jones. (the file handling bit)
5# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02008# (c) 2010-2018 Joe Perches <joe@perches.com>
Joe Hershberger05622192011-10-18 10:06:59 +00009
10use strict;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020011use warnings;
Tom Rini6b9709d2014-02-27 08:27:28 -050012use POSIX;
Dan Murphyc10e0f52017-01-31 14:15:53 -060013use File::Basename;
14use Cwd 'abs_path';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020015use Term::ANSIColor qw(:constants);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +020016use Encode qw(decode encode);
Joe Hershberger05622192011-10-18 10:06:59 +000017
18my $P = $0;
Dan Murphyc10e0f52017-01-31 14:15:53 -060019my $D = dirname(abs_path($P));
Joe Hershberger05622192011-10-18 10:06:59 +000020
21my $V = '0.32';
22
23use Getopt::Long qw(:config no_auto_abbrev);
24
25my $quiet = 0;
26my $tree = 1;
27my $chk_signoff = 1;
28my $chk_patch = 1;
29my $tst_only;
30my $emacs = 0;
31my $terse = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020032my $showfile = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000033my $file = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020034my $git = 0;
35my %git_commits = ();
Joe Hershberger05622192011-10-18 10:06:59 +000036my $check = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020037my $check_orig = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000038my $summary = 1;
39my $mailback = 0;
40my $summary_file = 0;
41my $show_types = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020042my $list_types = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -050043my $fix = 0;
44my $fix_inplace = 0;
Joe Hershberger05622192011-10-18 10:06:59 +000045my $root;
46my %debug;
Tom Rini6b9709d2014-02-27 08:27:28 -050047my %camelcase = ();
48my %use_type = ();
49my @use = ();
Joe Hershberger05622192011-10-18 10:06:59 +000050my %ignore_type = ();
51my @ignore = ();
52my $help = 0;
53my $configuration_file = ".checkpatch.conf";
Simon Glass048a6482020-05-22 16:32:35 -060054my $max_line_length = 100;
Tom Rini6b9709d2014-02-27 08:27:28 -050055my $ignore_perl_version = 0;
56my $minimum_perl_version = 5.10.0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020057my $min_conf_desc_length = 4;
Dan Murphyc10e0f52017-01-31 14:15:53 -060058my $spelling_file = "$D/spelling.txt";
59my $codespell = 0;
60my $codespellfile = "/usr/share/codespell/dictionary.txt";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020061my $conststructsfile = "$D/const_structs.checkpatch";
62my $typedefsfile = "";
Simon Glassb77df592020-05-22 16:32:36 -060063my $u_boot = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020064my $color = "auto";
Simon Glass048a6482020-05-22 16:32:35 -060065my $allow_c99_comments = 1;
Joe Hershberger05622192011-10-18 10:06:59 +000066
67sub help {
68 my ($exitcode) = @_;
69
70 print << "EOM";
71Usage: $P [OPTION]... [FILE]...
72Version: $V
73
74Options:
75 -q, --quiet quiet
76 --no-tree run without a kernel tree
77 --no-signoff do not check for 'Signed-off-by' line
78 --patch treat FILE as patchfile (default)
79 --emacs emacs compile window format
80 --terse one line per report
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020081 --showfile emit diffed file position, not input file position
82 -g, --git treat FILE as a single commit or git revision range
83 single git commit with:
84 <rev>
85 <rev>^
86 <rev>~n
87 multiple git commits with:
88 <rev1>..<rev2>
89 <rev1>...<rev2>
90 <rev>-<count>
91 git merges are ignored
Joe Hershberger05622192011-10-18 10:06:59 +000092 -f, --file treat FILE as regular source file
93 --subjective, --strict enable more subjective tests
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020094 --list-types list the possible message types
Tom Rini6b9709d2014-02-27 08:27:28 -050095 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershberger05622192011-10-18 10:06:59 +000096 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020097 --show-types show the specific message type in the output
Simon Glass048a6482020-05-22 16:32:35 -060098 --max-line-length=n set the maximum line length, (default $max_line_length)
99 if exceeded, warn on patches
100 requires --strict for use with --file
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200101 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Hershberger05622192011-10-18 10:06:59 +0000102 --root=PATH PATH to the kernel tree root
103 --no-summary suppress the per-file summary
104 --mailback only produce a report in case of warnings/errors
105 --summary-file include the filename in summary
106 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
107 'values', 'possible', 'type', and 'attr' (default
108 is all off)
109 --test-only=WORD report only warnings/errors containing WORD
110 literally
Tom Rini6b9709d2014-02-27 08:27:28 -0500111 --fix EXPERIMENTAL - may create horrible results
112 If correctable single-line errors exist, create
113 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
114 with potential errors corrected to the preferred
115 checkpatch style
116 --fix-inplace EXPERIMENTAL - may create horrible results
117 Is the same as --fix, but overwrites the input
118 file. It's your fault if there's no backup or git
119 --ignore-perl-version override checking of perl version. expect
120 runtime errors.
Dan Murphyc10e0f52017-01-31 14:15:53 -0600121 --codespell Use the codespell dictionary for spelling/typos
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200122 (default:/usr/share/codespell/dictionary.txt)
Dan Murphyc10e0f52017-01-31 14:15:53 -0600123 --codespellfile Use this codespell dictionary
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200124 --typedefsfile Read additional types from this file
125 --color[=WHEN] Use colors 'always', 'never', or only when output
126 is a terminal ('auto'). Default is 'auto'.
Simon Glassb77df592020-05-22 16:32:36 -0600127 --u-boot Run additional checks for U-Boot
Joe Hershberger05622192011-10-18 10:06:59 +0000128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200155 # Also catch when type or level is passed through a variable
156 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200157 push (@types, $_);
158 }
159 @types = sort(uniq(@types));
160 print("#\tMessage type\n\n");
161 foreach my $type (@types) {
162 print(++$count . "\t" . $type . "\n");
163 }
164
165 exit($exitcode);
166}
167
Joe Hershberger05622192011-10-18 10:06:59 +0000168my $conf = which_conf($configuration_file);
169if (-f $conf) {
170 my @conf_args;
171 open(my $conffile, '<', "$conf")
172 or warn "$P: Can't find a readable $configuration_file file $!\n";
173
174 while (<$conffile>) {
175 my $line = $_;
176
177 $line =~ s/\s*\n?$//g;
178 $line =~ s/^\s*//g;
179 $line =~ s/\s+/ /g;
180
181 next if ($line =~ m/^\s*#/);
182 next if ($line =~ m/^\s*$/);
183
184 my @words = split(" ", $line);
185 foreach my $word (@words) {
186 last if ($word =~ m/^#/);
187 push (@conf_args, $word);
188 }
189 }
190 close($conffile);
191 unshift(@ARGV, @conf_args) if @conf_args;
192}
193
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200194# Perl's Getopt::Long allows options to take optional arguments after a space.
195# Prevent --color by itself from consuming other arguments
196foreach (@ARGV) {
197 if ($_ eq "--color" || $_ eq "-color") {
198 $_ = "--color=$color";
199 }
200}
201
Joe Hershberger05622192011-10-18 10:06:59 +0000202GetOptions(
203 'q|quiet+' => \$quiet,
204 'tree!' => \$tree,
205 'signoff!' => \$chk_signoff,
206 'patch!' => \$chk_patch,
207 'emacs!' => \$emacs,
208 'terse!' => \$terse,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200209 'showfile!' => \$showfile,
Joe Hershberger05622192011-10-18 10:06:59 +0000210 'f|file!' => \$file,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200211 'g|git!' => \$git,
Joe Hershberger05622192011-10-18 10:06:59 +0000212 'subjective!' => \$check,
213 'strict!' => \$check,
214 'ignore=s' => \@ignore,
Tom Rini6b9709d2014-02-27 08:27:28 -0500215 'types=s' => \@use,
Joe Hershberger05622192011-10-18 10:06:59 +0000216 'show-types!' => \$show_types,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200217 'list-types!' => \$list_types,
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000218 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200219 'min-conf-desc-length=i' => \$min_conf_desc_length,
Joe Hershberger05622192011-10-18 10:06:59 +0000220 'root=s' => \$root,
221 'summary!' => \$summary,
222 'mailback!' => \$mailback,
223 'summary-file!' => \$summary_file,
Tom Rini6b9709d2014-02-27 08:27:28 -0500224 'fix!' => \$fix,
225 'fix-inplace!' => \$fix_inplace,
226 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershberger05622192011-10-18 10:06:59 +0000227 'debug=s' => \%debug,
228 'test-only=s' => \$tst_only,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200229 'codespell!' => \$codespell,
230 'codespellfile=s' => \$codespellfile,
231 'typedefsfile=s' => \$typedefsfile,
Simon Glassb77df592020-05-22 16:32:36 -0600232 'u-boot' => \$u_boot,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200233 'color=s' => \$color,
234 'no-color' => \$color, #keep old behaviors of -nocolor
235 'nocolor' => \$color, #keep old behaviors of -nocolor
Joe Hershberger05622192011-10-18 10:06:59 +0000236 'h|help' => \$help,
237 'version' => \$help
238) or help(1);
239
240help(0) if ($help);
241
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200242list_types(0) if ($list_types);
243
Tom Rini6b9709d2014-02-27 08:27:28 -0500244$fix = 1 if ($fix_inplace);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200245$check_orig = $check;
Tom Rini6b9709d2014-02-27 08:27:28 -0500246
Joe Hershberger05622192011-10-18 10:06:59 +0000247my $exit = 0;
248
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200249my $perl_version_ok = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -0500250if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200251 $perl_version_ok = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -0500252 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200253 exit(1) if (!$ignore_perl_version);
Tom Rini6b9709d2014-02-27 08:27:28 -0500254}
255
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200256#if no filenames are given, push '-' to read patch from stdin
Joe Hershberger05622192011-10-18 10:06:59 +0000257if ($#ARGV < 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200258 push(@ARGV, '-');
259}
260
261if ($color =~ /^[01]$/) {
262 $color = !$color;
263} elsif ($color =~ /^always$/i) {
264 $color = 1;
265} elsif ($color =~ /^never$/i) {
266 $color = 0;
267} elsif ($color =~ /^auto$/i) {
268 $color = (-t STDOUT);
269} else {
270 die "Invalid color mode: $color\n";
Joe Hershberger05622192011-10-18 10:06:59 +0000271}
272
Tom Rini6b9709d2014-02-27 08:27:28 -0500273sub hash_save_array_words {
274 my ($hashRef, $arrayRef) = @_;
Joe Hershberger05622192011-10-18 10:06:59 +0000275
Tom Rini6b9709d2014-02-27 08:27:28 -0500276 my @array = split(/,/, join(',', @$arrayRef));
277 foreach my $word (@array) {
278 $word =~ s/\s*\n?$//g;
279 $word =~ s/^\s*//g;
280 $word =~ s/\s+/ /g;
281 $word =~ tr/[a-z]/[A-Z]/;
Joe Hershberger05622192011-10-18 10:06:59 +0000282
Tom Rini6b9709d2014-02-27 08:27:28 -0500283 next if ($word =~ m/^\s*#/);
284 next if ($word =~ m/^\s*$/);
285
286 $hashRef->{$word}++;
287 }
Joe Hershberger05622192011-10-18 10:06:59 +0000288}
289
Tom Rini6b9709d2014-02-27 08:27:28 -0500290sub hash_show_words {
291 my ($hashRef, $prefix) = @_;
292
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200293 if (keys %$hashRef) {
294 print "\nNOTE: $prefix message types:";
Tom Rini6b9709d2014-02-27 08:27:28 -0500295 foreach my $word (sort keys %$hashRef) {
296 print " $word";
297 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200298 print "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -0500299 }
300}
301
302hash_save_array_words(\%ignore_type, \@ignore);
303hash_save_array_words(\%use_type, \@use);
304
Joe Hershberger05622192011-10-18 10:06:59 +0000305my $dbg_values = 0;
306my $dbg_possible = 0;
307my $dbg_type = 0;
308my $dbg_attr = 0;
309for my $key (keys %debug) {
310 ## no critic
311 eval "\${dbg_$key} = '$debug{$key}';";
312 die "$@" if ($@);
313}
314
315my $rpt_cleaners = 0;
316
317if ($terse) {
318 $emacs = 1;
319 $quiet++;
320}
321
322if ($tree) {
323 if (defined $root) {
324 if (!top_of_kernel_tree($root)) {
325 die "$P: $root: --root does not point at a valid tree\n";
326 }
327 } else {
328 if (top_of_kernel_tree('.')) {
329 $root = '.';
330 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
331 top_of_kernel_tree($1)) {
332 $root = $1;
333 }
334 }
335
336 if (!defined $root) {
337 print "Must be run from the top-level dir. of a kernel tree\n";
338 exit(2);
339 }
340}
341
342my $emitted_corrupt = 0;
343
344our $Ident = qr{
345 [A-Za-z_][A-Za-z\d_]*
346 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
347 }x;
348our $Storage = qr{extern|static|asmlinkage};
349our $Sparse = qr{
350 __user|
351 __kernel|
352 __force|
353 __iomem|
354 __must_check|
Joe Hershberger05622192011-10-18 10:06:59 +0000355 __kprobes|
356 __ref|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200357 __refconst|
358 __refdata|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200359 __rcu|
360 __private
Joe Hershberger05622192011-10-18 10:06:59 +0000361 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -0500362our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
363our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
364our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
365our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
366our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Hershberger05622192011-10-18 10:06:59 +0000367
368# Notes to $Attribute:
369# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
370our $Attribute = qr{
371 const|
372 __percpu|
373 __nocast|
374 __safe|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200375 __bitwise|
Joe Hershberger05622192011-10-18 10:06:59 +0000376 __packed__|
377 __packed2__|
378 __naked|
379 __maybe_unused|
380 __always_unused|
381 __noreturn|
382 __used|
383 __cold|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200384 __pure|
Joe Hershberger05622192011-10-18 10:06:59 +0000385 __noclone|
386 __deprecated|
387 __read_mostly|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200388 __ro_after_init|
Joe Hershberger05622192011-10-18 10:06:59 +0000389 __kprobes|
Tom Rini6b9709d2014-02-27 08:27:28 -0500390 $InitAttribute|
Joe Hershberger05622192011-10-18 10:06:59 +0000391 ____cacheline_aligned|
392 ____cacheline_aligned_in_smp|
393 ____cacheline_internodealigned_in_smp|
394 __weak
395 }x;
396our $Modifier;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200397our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershberger05622192011-10-18 10:06:59 +0000398our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
399our $Lval = qr{$Ident(?:$Member)*};
400
Tom Rini6b9709d2014-02-27 08:27:28 -0500401our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
402our $Binary = qr{(?i)0b[01]+$Int_type?};
403our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
404our $Int = qr{[0-9]+$Int_type?};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200405our $Octal = qr{0[0-7]+$Int_type?};
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200406our $String = qr{"[X\t]*"};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000407our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
408our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
409our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
410our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200411our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000412our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200413our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rini6b9709d2014-02-27 08:27:28 -0500414our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershberger05622192011-10-18 10:06:59 +0000415our $Operators = qr{
416 <=|>=|==|!=|
417 =>|->|<<|>>|<|>|!|~|
Tom Rini6b9709d2014-02-27 08:27:28 -0500418 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershberger05622192011-10-18 10:06:59 +0000419 }x;
420
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200421our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
422
423our $BasicType;
Joe Hershberger05622192011-10-18 10:06:59 +0000424our $NonptrType;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200425our $NonptrTypeMisordered;
Tom Rini6b9709d2014-02-27 08:27:28 -0500426our $NonptrTypeWithAttr;
Joe Hershberger05622192011-10-18 10:06:59 +0000427our $Type;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200428our $TypeMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000429our $Declare;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200430our $DeclareMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000431
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000432our $NON_ASCII_UTF8 = qr{
433 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershberger05622192011-10-18 10:06:59 +0000434 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
435 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
436 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
437 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
438 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
439 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
440}x;
441
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000442our $UTF8 = qr{
443 [\x09\x0A\x0D\x20-\x7E] # ASCII
444 | $NON_ASCII_UTF8
445}x;
446
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200447our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
448our $typeOtherOSTypedefs = qr{(?x:
449 u_(?:char|short|int|long) | # bsd
450 u(?:nchar|short|int|long) # sysv
451)};
452our $typeKernelTypedefs = qr{(?x:
Joe Hershberger05622192011-10-18 10:06:59 +0000453 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
454 atomic_t
455)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200456our $typeTypedefs = qr{(?x:
457 $typeC99Typedefs\b|
458 $typeOtherOSTypedefs\b|
459 $typeKernelTypedefs\b
460)};
461
462our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Joe Hershberger05622192011-10-18 10:06:59 +0000463
464our $logFunctions = qr{(?x:
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200465 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rini6b9709d2014-02-27 08:27:28 -0500466 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200467 TP_printk|
Joe Hershberger05622192011-10-18 10:06:59 +0000468 WARN(?:_RATELIMIT|_ONCE|)|
469 panic|
James Byrne66b3ccc2019-11-21 14:32:46 +0000470 debug|
471 printf|
Tom Rini6b9709d2014-02-27 08:27:28 -0500472 MODULE_[A-Z_]+|
473 seq_vprintf|seq_printf|seq_puts
Joe Hershberger05622192011-10-18 10:06:59 +0000474)};
475
476our $signature_tags = qr{(?xi:
477 Signed-off-by:|
478 Acked-by:|
479 Tested-by:|
480 Reviewed-by:|
481 Reported-by:|
Tom Rini6b9709d2014-02-27 08:27:28 -0500482 Suggested-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000483 To:|
484 Cc:
485)};
486
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200487our @typeListMisordered = (
488 qr{char\s+(?:un)?signed},
489 qr{int\s+(?:(?:un)?signed\s+)?short\s},
490 qr{int\s+short(?:\s+(?:un)?signed)},
491 qr{short\s+int(?:\s+(?:un)?signed)},
492 qr{(?:un)?signed\s+int\s+short},
493 qr{short\s+(?:un)?signed},
494 qr{long\s+int\s+(?:un)?signed},
495 qr{int\s+long\s+(?:un)?signed},
496 qr{long\s+(?:un)?signed\s+int},
497 qr{int\s+(?:un)?signed\s+long},
498 qr{int\s+(?:un)?signed},
499 qr{int\s+long\s+long\s+(?:un)?signed},
500 qr{long\s+long\s+int\s+(?:un)?signed},
501 qr{long\s+long\s+(?:un)?signed\s+int},
502 qr{long\s+long\s+(?:un)?signed},
503 qr{long\s+(?:un)?signed},
504);
505
Joe Hershberger05622192011-10-18 10:06:59 +0000506our @typeList = (
507 qr{void},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200508 qr{(?:(?:un)?signed\s+)?char},
509 qr{(?:(?:un)?signed\s+)?short\s+int},
510 qr{(?:(?:un)?signed\s+)?short},
511 qr{(?:(?:un)?signed\s+)?int},
512 qr{(?:(?:un)?signed\s+)?long\s+int},
513 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
514 qr{(?:(?:un)?signed\s+)?long\s+long},
515 qr{(?:(?:un)?signed\s+)?long},
516 qr{(?:un)?signed},
Joe Hershberger05622192011-10-18 10:06:59 +0000517 qr{float},
518 qr{double},
519 qr{bool},
520 qr{struct\s+$Ident},
521 qr{union\s+$Ident},
522 qr{enum\s+$Ident},
523 qr{${Ident}_t},
524 qr{${Ident}_handler},
525 qr{${Ident}_handler_fn},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200526 @typeListMisordered,
Joe Hershberger05622192011-10-18 10:06:59 +0000527);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200528
529our $C90_int_types = qr{(?x:
530 long\s+long\s+int\s+(?:un)?signed|
531 long\s+long\s+(?:un)?signed\s+int|
532 long\s+long\s+(?:un)?signed|
533 (?:(?:un)?signed\s+)?long\s+long\s+int|
534 (?:(?:un)?signed\s+)?long\s+long|
535 int\s+long\s+long\s+(?:un)?signed|
536 int\s+(?:(?:un)?signed\s+)?long\s+long|
537
538 long\s+int\s+(?:un)?signed|
539 long\s+(?:un)?signed\s+int|
540 long\s+(?:un)?signed|
541 (?:(?:un)?signed\s+)?long\s+int|
542 (?:(?:un)?signed\s+)?long|
543 int\s+long\s+(?:un)?signed|
544 int\s+(?:(?:un)?signed\s+)?long|
545
546 int\s+(?:un)?signed|
547 (?:(?:un)?signed\s+)?int
548)};
549
550our @typeListFile = ();
Tom Rini6b9709d2014-02-27 08:27:28 -0500551our @typeListWithAttr = (
552 @typeList,
553 qr{struct\s+$InitAttribute\s+$Ident},
554 qr{union\s+$InitAttribute\s+$Ident},
555);
556
Joe Hershberger05622192011-10-18 10:06:59 +0000557our @modifierList = (
558 qr{fastcall},
559);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200560our @modifierListFile = ();
561
562our @mode_permission_funcs = (
563 ["module_param", 3],
564 ["module_param_(?:array|named|string)", 4],
565 ["module_param_array_named", 5],
566 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
567 ["proc_create(?:_data|)", 2],
568 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
569 ["IIO_DEV_ATTR_[A-Z_]+", 1],
570 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
571 ["SENSOR_TEMPLATE(?:_2|)", 3],
572 ["__ATTR", 2],
573);
574
575#Create a search pattern for all these functions to speed up a loop below
576our $mode_perms_search = "";
577foreach my $entry (@mode_permission_funcs) {
578 $mode_perms_search .= '|' if ($mode_perms_search ne "");
579 $mode_perms_search .= $entry->[0];
580}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200581$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200582
583our $mode_perms_world_writable = qr{
584 S_IWUGO |
585 S_IWOTH |
586 S_IRWXUGO |
587 S_IALLUGO |
588 0[0-7][0-7][2367]
589}x;
590
591our %mode_permission_string_types = (
592 "S_IRWXU" => 0700,
593 "S_IRUSR" => 0400,
594 "S_IWUSR" => 0200,
595 "S_IXUSR" => 0100,
596 "S_IRWXG" => 0070,
597 "S_IRGRP" => 0040,
598 "S_IWGRP" => 0020,
599 "S_IXGRP" => 0010,
600 "S_IRWXO" => 0007,
601 "S_IROTH" => 0004,
602 "S_IWOTH" => 0002,
603 "S_IXOTH" => 0001,
604 "S_IRWXUGO" => 0777,
605 "S_IRUGO" => 0444,
606 "S_IWUGO" => 0222,
607 "S_IXUGO" => 0111,
608);
609
610#Create a search pattern for all these strings to speed up a loop below
611our $mode_perms_string_search = "";
612foreach my $entry (keys %mode_permission_string_types) {
613 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
614 $mode_perms_string_search .= $entry;
615}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200616our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
617our $multi_mode_perms_string_search = qr{
618 ${single_mode_perms_string_search}
619 (?:\s*\|\s*${single_mode_perms_string_search})*
620}x;
621
622sub perms_to_octal {
623 my ($string) = @_;
624
625 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
626
627 my $val = "";
628 my $oval = "";
629 my $to = 0;
630 my $curpos = 0;
631 my $lastpos = 0;
632 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
633 $curpos = pos($string);
634 my $match = $2;
635 my $omatch = $1;
636 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
637 $lastpos = $curpos;
638 $to |= $mode_permission_string_types{$match};
639 $val .= '\s*\|\s*' if ($val ne "");
640 $val .= $match;
641 $oval .= $omatch;
642 }
643 $oval =~ s/^\s*\|\s*//;
644 $oval =~ s/\s*\|\s*$//;
645 return sprintf("%04o", $to);
646}
Joe Hershberger05622192011-10-18 10:06:59 +0000647
648our $allowed_asm_includes = qr{(?x:
649 irq|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200650 memory|
651 time|
652 reboot
Joe Hershberger05622192011-10-18 10:06:59 +0000653)};
654# memory.h: ARM has a custom one
655
Dan Murphyc10e0f52017-01-31 14:15:53 -0600656# Load common spelling mistakes and build regular expression list.
657my $misspellings;
658my %spelling_fix;
659
660if (open(my $spelling, '<', $spelling_file)) {
661 while (<$spelling>) {
662 my $line = $_;
663
664 $line =~ s/\s*\n?$//g;
665 $line =~ s/^\s*//g;
666
667 next if ($line =~ m/^\s*#/);
668 next if ($line =~ m/^\s*$/);
669
670 my ($suspect, $fix) = split(/\|\|/, $line);
671
672 $spelling_fix{$suspect} = $fix;
673 }
674 close($spelling);
675} else {
676 warn "No typos will be found - file '$spelling_file': $!\n";
677}
678
679if ($codespell) {
680 if (open(my $spelling, '<', $codespellfile)) {
681 while (<$spelling>) {
682 my $line = $_;
683
684 $line =~ s/\s*\n?$//g;
685 $line =~ s/^\s*//g;
686
687 next if ($line =~ m/^\s*#/);
688 next if ($line =~ m/^\s*$/);
689 next if ($line =~ m/, disabled/i);
690
691 $line =~ s/,.*$//;
692
693 my ($suspect, $fix) = split(/->/, $line);
694
695 $spelling_fix{$suspect} = $fix;
696 }
697 close($spelling);
698 } else {
699 warn "No codespell typos will be found - file '$codespellfile': $!\n";
700 }
701}
702
703$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
704
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200705sub read_words {
706 my ($wordsRef, $file) = @_;
707
708 if (open(my $words, '<', $file)) {
709 while (<$words>) {
710 my $line = $_;
711
712 $line =~ s/\s*\n?$//g;
713 $line =~ s/^\s*//g;
714
715 next if ($line =~ m/^\s*#/);
716 next if ($line =~ m/^\s*$/);
717 if ($line =~ /\s/) {
718 print("$file: '$line' invalid - ignored\n");
719 next;
720 }
721
722 $$wordsRef .= '|' if ($$wordsRef ne "");
723 $$wordsRef .= $line;
724 }
725 close($file);
726 return 1;
727 }
728
729 return 0;
730}
731
732my $const_structs = "";
733read_words(\$const_structs, $conststructsfile)
734 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
735
736my $typeOtherTypedefs = "";
737if (length($typedefsfile)) {
738 read_words(\$typeOtherTypedefs, $typedefsfile)
739 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
740}
741$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
Dan Murphyc10e0f52017-01-31 14:15:53 -0600742
Joe Hershberger05622192011-10-18 10:06:59 +0000743sub build_types {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200744 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
745 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
746 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Tom Rini6b9709d2014-02-27 08:27:28 -0500747 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershberger05622192011-10-18 10:06:59 +0000748 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200749 $BasicType = qr{
750 (?:$typeTypedefs\b)|
751 (?:${all}\b)
752 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000753 $NonptrType = qr{
754 (?:$Modifier\s+|const\s+)*
755 (?:
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000756 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershberger05622192011-10-18 10:06:59 +0000757 (?:$typeTypedefs\b)|
758 (?:${all}\b)
759 )
760 (?:\s+$Modifier|\s+const)*
761 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200762 $NonptrTypeMisordered = qr{
763 (?:$Modifier\s+|const\s+)*
764 (?:
765 (?:${Misordered}\b)
766 )
767 (?:\s+$Modifier|\s+const)*
768 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -0500769 $NonptrTypeWithAttr = qr{
770 (?:$Modifier\s+|const\s+)*
771 (?:
772 (?:typeof|__typeof__)\s*\([^\)]*\)|
773 (?:$typeTypedefs\b)|
774 (?:${allWithAttr}\b)
775 )
776 (?:\s+$Modifier|\s+const)*
777 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000778 $Type = qr{
779 $NonptrType
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200780 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Joe Hershberger05622192011-10-18 10:06:59 +0000781 (?:\s+$Inline|\s+$Modifier)*
782 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200783 $TypeMisordered = qr{
784 $NonptrTypeMisordered
785 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
786 (?:\s+$Inline|\s+$Modifier)*
787 }x;
788 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
789 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Joe Hershberger05622192011-10-18 10:06:59 +0000790}
791build_types();
792
Joe Hershberger05622192011-10-18 10:06:59 +0000793our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000794
795# Using $balanced_parens, $LvalOrFunc, or $FuncArg
796# requires at least perl version v5.10.0
797# Any use must be runtime checked with $^V
798
799our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200800our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
801our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
802
803our $declaration_macros = qr{(?x:
804 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
805 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200806 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
807 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200808)};
Joe Hershberger05622192011-10-18 10:06:59 +0000809
810sub deparenthesize {
811 my ($string) = @_;
812 return "" if (!defined($string));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200813
814 while ($string =~ /^\s*\(.*\)\s*$/) {
815 $string =~ s@^\s*\(\s*@@;
816 $string =~ s@\s*\)\s*$@@;
817 }
818
Joe Hershberger05622192011-10-18 10:06:59 +0000819 $string =~ s@\s+@ @g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200820
Joe Hershberger05622192011-10-18 10:06:59 +0000821 return $string;
822}
823
Tom Rini6b9709d2014-02-27 08:27:28 -0500824sub seed_camelcase_file {
825 my ($file) = @_;
826
827 return if (!(-f $file));
828
829 local $/;
830
831 open(my $include_file, '<', "$file")
832 or warn "$P: Can't read '$file' $!\n";
833 my $text = <$include_file>;
834 close($include_file);
835
836 my @lines = split('\n', $text);
837
838 foreach my $line (@lines) {
839 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
840 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
841 $camelcase{$1} = 1;
842 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
843 $camelcase{$1} = 1;
844 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
845 $camelcase{$1} = 1;
846 }
847 }
848}
849
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200850sub is_maintained_obsolete {
851 my ($filename) = @_;
852
853 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
854
855 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
856
857 return $status =~ /obsolete/i;
858}
859
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200860sub is_SPDX_License_valid {
861 my ($license) = @_;
862
863 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
864
865 my $root_path = abs_path($root);
866 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
867 return 0 if ($status ne "");
868 return 1;
869}
870
Tom Rini6b9709d2014-02-27 08:27:28 -0500871my $camelcase_seeded = 0;
872sub seed_camelcase_includes {
873 return if ($camelcase_seeded);
874
875 my $files;
876 my $camelcase_cache = "";
877 my @include_files = ();
878
879 $camelcase_seeded = 1;
880
881 if (-e ".git") {
Simon Glass048a6482020-05-22 16:32:35 -0600882 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rini6b9709d2014-02-27 08:27:28 -0500883 chomp $git_last_include_commit;
884 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
885 } else {
886 my $last_mod_date = 0;
887 $files = `find $root/include -name "*.h"`;
888 @include_files = split('\n', $files);
889 foreach my $file (@include_files) {
890 my $date = POSIX::strftime("%Y%m%d%H%M",
891 localtime((stat $file)[9]));
892 $last_mod_date = $date if ($last_mod_date < $date);
893 }
894 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
895 }
896
897 if ($camelcase_cache ne "" && -f $camelcase_cache) {
898 open(my $camelcase_file, '<', "$camelcase_cache")
899 or warn "$P: Can't read '$camelcase_cache' $!\n";
900 while (<$camelcase_file>) {
901 chomp;
902 $camelcase{$_} = 1;
903 }
904 close($camelcase_file);
905
906 return;
907 }
908
909 if (-e ".git") {
Simon Glass048a6482020-05-22 16:32:35 -0600910 $files = `git ls-files "include/*.h"`;
Tom Rini6b9709d2014-02-27 08:27:28 -0500911 @include_files = split('\n', $files);
912 }
913
914 foreach my $file (@include_files) {
915 seed_camelcase_file($file);
916 }
917
918 if ($camelcase_cache ne "") {
919 unlink glob ".checkpatch-camelcase.*";
920 open(my $camelcase_file, '>', "$camelcase_cache")
921 or warn "$P: Can't write '$camelcase_cache' $!\n";
922 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
923 print $camelcase_file ("$_\n");
924 }
925 close($camelcase_file);
926 }
927}
928
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200929sub git_commit_info {
930 my ($commit, $id, $desc) = @_;
931
932 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
933
Simon Glass048a6482020-05-22 16:32:35 -0600934 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200935 $output =~ s/^\s*//gm;
936 my @lines = split("\n", $output);
937
938 return ($id, $desc) if ($#lines < 0);
939
Simon Glass048a6482020-05-22 16:32:35 -0600940 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200941# Maybe one day convert this block of bash into something that returns
942# all matching commit ids, but it's very slow...
943#
944# echo "checking commits $1..."
945# git rev-list --remotes | grep -i "^$1" |
946# while read line ; do
947# git log --format='%H %s' -1 $line |
948# echo "commit $(cut -c 1-12,41-)"
949# done
950 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
951 $id = undef;
952 } else {
953 $id = substr($lines[0], 0, 12);
954 $desc = substr($lines[0], 41);
955 }
956
957 return ($id, $desc);
958}
959
Joe Hershberger05622192011-10-18 10:06:59 +0000960$chk_signoff = 0 if ($file);
961
Joe Hershberger05622192011-10-18 10:06:59 +0000962my @rawlines = ();
963my @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -0500964my @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200965my @fixed_inserted = ();
966my @fixed_deleted = ();
Dan Murphyc10e0f52017-01-31 14:15:53 -0600967my $fixlinenr = -1;
968
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200969# If input is git commits, extract all commits from the commit expressions.
970# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
971die "$P: No git repository found\n" if ($git && !-e ".git");
972
973if ($git) {
974 my @commits = ();
975 foreach my $commit_expr (@ARGV) {
976 my $git_range;
977 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
978 $git_range = "-$2 $1";
979 } elsif ($commit_expr =~ m/\.\./) {
980 $git_range = "$commit_expr";
981 } else {
982 $git_range = "-1 $commit_expr";
983 }
Simon Glass048a6482020-05-22 16:32:35 -0600984 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200985 foreach my $line (split(/\n/, $lines)) {
986 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
987 next if (!defined($1) || !defined($2));
988 my $sha1 = $1;
989 my $subject = $2;
990 unshift(@commits, $sha1);
991 $git_commits{$sha1} = $subject;
992 }
993 }
994 die "$P: no git commits after extraction!\n" if (@commits == 0);
995 @ARGV = @commits;
996}
997
998my $vname;
Joe Hershberger05622192011-10-18 10:06:59 +0000999for my $filename (@ARGV) {
1000 my $FILE;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001001 if ($git) {
1002 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1003 die "$P: $filename: git format-patch failed - $!\n";
1004 } elsif ($file) {
Joe Hershberger05622192011-10-18 10:06:59 +00001005 open($FILE, '-|', "diff -u /dev/null $filename") ||
1006 die "$P: $filename: diff failed - $!\n";
1007 } elsif ($filename eq '-') {
1008 open($FILE, '<&STDIN');
1009 } else {
1010 open($FILE, '<', "$filename") ||
1011 die "$P: $filename: open failed - $!\n";
1012 }
1013 if ($filename eq '-') {
1014 $vname = 'Your patch';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001015 } elsif ($git) {
1016 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershberger05622192011-10-18 10:06:59 +00001017 } else {
1018 $vname = $filename;
1019 }
1020 while (<$FILE>) {
1021 chomp;
1022 push(@rawlines, $_);
1023 }
1024 close($FILE);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001025
1026 if ($#ARGV > 0 && $quiet == 0) {
1027 print '-' x length($vname) . "\n";
1028 print "$vname\n";
1029 print '-' x length($vname) . "\n";
1030 }
1031
Joe Hershberger05622192011-10-18 10:06:59 +00001032 if (!process($filename)) {
1033 $exit = 1;
1034 }
1035 @rawlines = ();
1036 @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001037 @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001038 @fixed_inserted = ();
1039 @fixed_deleted = ();
1040 $fixlinenr = -1;
1041 @modifierListFile = ();
1042 @typeListFile = ();
1043 build_types();
1044}
1045
1046if (!$quiet) {
1047 hash_show_words(\%use_type, "Used");
1048 hash_show_words(\%ignore_type, "Ignored");
1049
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001050 if (!$perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001051 print << "EOM"
1052
1053NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001054 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001055EOM
1056 }
1057 if ($exit) {
1058 print << "EOM"
1059
1060NOTE: If any of the errors are false positives, please report
1061 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1062EOM
1063 }
Joe Hershberger05622192011-10-18 10:06:59 +00001064}
1065
1066exit($exit);
1067
1068sub top_of_kernel_tree {
1069 my ($root) = @_;
1070
1071 my @tree_check = (
Tom Rini6b9709d2014-02-27 08:27:28 -05001072 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershberger05622192011-10-18 10:06:59 +00001073 "README", "Documentation", "arch", "include", "drivers",
1074 "fs", "init", "ipc", "kernel", "lib", "scripts",
1075 );
1076
1077 foreach my $check (@tree_check) {
1078 if (! -e $root . '/' . $check) {
1079 return 0;
1080 }
1081 }
1082 return 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001083}
Joe Hershberger05622192011-10-18 10:06:59 +00001084
1085sub parse_email {
1086 my ($formatted_email) = @_;
1087
1088 my $name = "";
1089 my $address = "";
1090 my $comment = "";
1091
1092 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1093 $name = $1;
1094 $address = $2;
1095 $comment = $3 if defined $3;
1096 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1097 $address = $1;
1098 $comment = $2 if defined $2;
1099 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1100 $address = $1;
1101 $comment = $2 if defined $2;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001102 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershberger05622192011-10-18 10:06:59 +00001103 $name = $formatted_email;
Tom Rini6b9709d2014-02-27 08:27:28 -05001104 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001105 $name =~ s/^\"|\"$//g;
1106 # If there's a name left after stripping spaces and
1107 # leading quotes, and the address doesn't have both
1108 # leading and trailing angle brackets, the address
1109 # is invalid. ie:
1110 # "joe smith joe@smith.com" bad
1111 # "joe smith <joe@smith.com" bad
1112 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1113 $name = "";
1114 $address = "";
1115 $comment = "";
1116 }
1117 }
1118
Tom Rini6b9709d2014-02-27 08:27:28 -05001119 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001120 $name =~ s/^\"|\"$//g;
Tom Rini6b9709d2014-02-27 08:27:28 -05001121 $address = trim($address);
Joe Hershberger05622192011-10-18 10:06:59 +00001122 $address =~ s/^\<|\>$//g;
1123
1124 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1125 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1126 $name = "\"$name\"";
1127 }
1128
1129 return ($name, $address, $comment);
1130}
1131
1132sub format_email {
1133 my ($name, $address) = @_;
1134
1135 my $formatted_email;
1136
Tom Rini6b9709d2014-02-27 08:27:28 -05001137 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001138 $name =~ s/^\"|\"$//g;
Tom Rini6b9709d2014-02-27 08:27:28 -05001139 $address = trim($address);
Joe Hershberger05622192011-10-18 10:06:59 +00001140
1141 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1142 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1143 $name = "\"$name\"";
1144 }
1145
1146 if ("$name" eq "") {
1147 $formatted_email = "$address";
1148 } else {
1149 $formatted_email = "$name <$address>";
1150 }
1151
1152 return $formatted_email;
1153}
1154
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001155sub which {
1156 my ($bin) = @_;
1157
1158 foreach my $path (split(/:/, $ENV{PATH})) {
1159 if (-e "$path/$bin") {
1160 return "$path/$bin";
1161 }
1162 }
1163
1164 return "";
1165}
1166
Joe Hershberger05622192011-10-18 10:06:59 +00001167sub which_conf {
1168 my ($conf) = @_;
1169
1170 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1171 if (-e "$path/$conf") {
1172 return "$path/$conf";
1173 }
1174 }
1175
1176 return "";
1177}
1178
1179sub expand_tabs {
1180 my ($str) = @_;
1181
1182 my $res = '';
1183 my $n = 0;
1184 for my $c (split(//, $str)) {
1185 if ($c eq "\t") {
1186 $res .= ' ';
1187 $n++;
1188 for (; ($n % 8) != 0; $n++) {
1189 $res .= ' ';
1190 }
1191 next;
1192 }
1193 $res .= $c;
1194 $n++;
1195 }
1196
1197 return $res;
1198}
1199sub copy_spacing {
1200 (my $res = shift) =~ tr/\t/ /c;
1201 return $res;
1202}
1203
1204sub line_stats {
1205 my ($line) = @_;
1206
1207 # Drop the diff line leader and expand tabs
1208 $line =~ s/^.//;
1209 $line = expand_tabs($line);
1210
1211 # Pick the indent from the front of the line.
1212 my ($white) = ($line =~ /^(\s*)/);
1213
1214 return (length($line), length($white));
1215}
1216
1217my $sanitise_quote = '';
1218
1219sub sanitise_line_reset {
1220 my ($in_comment) = @_;
1221
1222 if ($in_comment) {
1223 $sanitise_quote = '*/';
1224 } else {
1225 $sanitise_quote = '';
1226 }
1227}
1228sub sanitise_line {
1229 my ($line) = @_;
1230
1231 my $res = '';
1232 my $l = '';
1233
1234 my $qlen = 0;
1235 my $off = 0;
1236 my $c;
1237
1238 # Always copy over the diff marker.
1239 $res = substr($line, 0, 1);
1240
1241 for ($off = 1; $off < length($line); $off++) {
1242 $c = substr($line, $off, 1);
1243
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001244 # Comments we are whacking completely including the begin
Joe Hershberger05622192011-10-18 10:06:59 +00001245 # and end, all to $;.
1246 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1247 $sanitise_quote = '*/';
1248
1249 substr($res, $off, 2, "$;$;");
1250 $off++;
1251 next;
1252 }
1253 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1254 $sanitise_quote = '';
1255 substr($res, $off, 2, "$;$;");
1256 $off++;
1257 next;
1258 }
1259 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1260 $sanitise_quote = '//';
1261
1262 substr($res, $off, 2, $sanitise_quote);
1263 $off++;
1264 next;
1265 }
1266
1267 # A \ in a string means ignore the next character.
1268 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1269 $c eq "\\") {
1270 substr($res, $off, 2, 'XX');
1271 $off++;
1272 next;
1273 }
1274 # Regular quotes.
1275 if ($c eq "'" || $c eq '"') {
1276 if ($sanitise_quote eq '') {
1277 $sanitise_quote = $c;
1278
1279 substr($res, $off, 1, $c);
1280 next;
1281 } elsif ($sanitise_quote eq $c) {
1282 $sanitise_quote = '';
1283 }
1284 }
1285
1286 #print "c<$c> SQ<$sanitise_quote>\n";
1287 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1288 substr($res, $off, 1, $;);
1289 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1290 substr($res, $off, 1, $;);
1291 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1292 substr($res, $off, 1, 'X');
1293 } else {
1294 substr($res, $off, 1, $c);
1295 }
1296 }
1297
1298 if ($sanitise_quote eq '//') {
1299 $sanitise_quote = '';
1300 }
1301
1302 # The pathname on a #include may be surrounded by '<' and '>'.
1303 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1304 my $clean = 'X' x length($1);
1305 $res =~ s@\<.*\>@<$clean>@;
1306
1307 # The whole of a #error is a string.
1308 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1309 my $clean = 'X' x length($1);
1310 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1311 }
1312
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001313 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1314 my $match = $1;
1315 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1316 }
1317
Joe Hershberger05622192011-10-18 10:06:59 +00001318 return $res;
1319}
1320
Tom Rini6b9709d2014-02-27 08:27:28 -05001321sub get_quoted_string {
1322 my ($line, $rawline) = @_;
1323
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001324 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001325 return "" if ($line !~ m/($String)/g);
Tom Rini6b9709d2014-02-27 08:27:28 -05001326 return substr($rawline, $-[0], $+[0] - $-[0]);
1327}
1328
Joe Hershberger05622192011-10-18 10:06:59 +00001329sub ctx_statement_block {
1330 my ($linenr, $remain, $off) = @_;
1331 my $line = $linenr - 1;
1332 my $blk = '';
1333 my $soff = $off;
1334 my $coff = $off - 1;
1335 my $coff_set = 0;
1336
1337 my $loff = 0;
1338
1339 my $type = '';
1340 my $level = 0;
1341 my @stack = ();
1342 my $p;
1343 my $c;
1344 my $len = 0;
1345
1346 my $remainder;
1347 while (1) {
1348 @stack = (['', 0]) if ($#stack == -1);
1349
1350 #warn "CSB: blk<$blk> remain<$remain>\n";
1351 # If we are about to drop off the end, pull in more
1352 # context.
1353 if ($off >= $len) {
1354 for (; $remain > 0; $line++) {
1355 last if (!defined $lines[$line]);
1356 next if ($lines[$line] =~ /^-/);
1357 $remain--;
1358 $loff = $len;
1359 $blk .= $lines[$line] . "\n";
1360 $len = length($blk);
1361 $line++;
1362 last;
1363 }
1364 # Bail if there is no further context.
1365 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1366 if ($off >= $len) {
1367 last;
1368 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001369 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1370 $level++;
1371 $type = '#';
1372 }
Joe Hershberger05622192011-10-18 10:06:59 +00001373 }
1374 $p = $c;
1375 $c = substr($blk, $off, 1);
1376 $remainder = substr($blk, $off);
1377
1378 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1379
1380 # Handle nested #if/#else.
1381 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1382 push(@stack, [ $type, $level ]);
1383 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1384 ($type, $level) = @{$stack[$#stack - 1]};
1385 } elsif ($remainder =~ /^#\s*endif\b/) {
1386 ($type, $level) = @{pop(@stack)};
1387 }
1388
1389 # Statement ends at the ';' or a close '}' at the
1390 # outermost level.
1391 if ($level == 0 && $c eq ';') {
1392 last;
1393 }
1394
1395 # An else is really a conditional as long as its not else if
1396 if ($level == 0 && $coff_set == 0 &&
1397 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1398 $remainder =~ /^(else)(?:\s|{)/ &&
1399 $remainder !~ /^else\s+if\b/) {
1400 $coff = $off + length($1) - 1;
1401 $coff_set = 1;
1402 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1403 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1404 }
1405
1406 if (($type eq '' || $type eq '(') && $c eq '(') {
1407 $level++;
1408 $type = '(';
1409 }
1410 if ($type eq '(' && $c eq ')') {
1411 $level--;
1412 $type = ($level != 0)? '(' : '';
1413
1414 if ($level == 0 && $coff < $soff) {
1415 $coff = $off;
1416 $coff_set = 1;
1417 #warn "CSB: mark coff<$coff>\n";
1418 }
1419 }
1420 if (($type eq '' || $type eq '{') && $c eq '{') {
1421 $level++;
1422 $type = '{';
1423 }
1424 if ($type eq '{' && $c eq '}') {
1425 $level--;
1426 $type = ($level != 0)? '{' : '';
1427
1428 if ($level == 0) {
1429 if (substr($blk, $off + 1, 1) eq ';') {
1430 $off++;
1431 }
1432 last;
1433 }
1434 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001435 # Preprocessor commands end at the newline unless escaped.
1436 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1437 $level--;
1438 $type = '';
1439 $off++;
1440 last;
1441 }
Joe Hershberger05622192011-10-18 10:06:59 +00001442 $off++;
1443 }
1444 # We are truly at the end, so shuffle to the next line.
1445 if ($off == $len) {
1446 $loff = $len + 1;
1447 $line++;
1448 $remain--;
1449 }
1450
1451 my $statement = substr($blk, $soff, $off - $soff + 1);
1452 my $condition = substr($blk, $soff, $coff - $soff + 1);
1453
1454 #warn "STATEMENT<$statement>\n";
1455 #warn "CONDITION<$condition>\n";
1456
1457 #print "coff<$coff> soff<$off> loff<$loff>\n";
1458
1459 return ($statement, $condition,
1460 $line, $remain + 1, $off - $loff + 1, $level);
1461}
1462
1463sub statement_lines {
1464 my ($stmt) = @_;
1465
1466 # Strip the diff line prefixes and rip blank lines at start and end.
1467 $stmt =~ s/(^|\n)./$1/g;
1468 $stmt =~ s/^\s*//;
1469 $stmt =~ s/\s*$//;
1470
1471 my @stmt_lines = ($stmt =~ /\n/g);
1472
1473 return $#stmt_lines + 2;
1474}
1475
1476sub statement_rawlines {
1477 my ($stmt) = @_;
1478
1479 my @stmt_lines = ($stmt =~ /\n/g);
1480
1481 return $#stmt_lines + 2;
1482}
1483
1484sub statement_block_size {
1485 my ($stmt) = @_;
1486
1487 $stmt =~ s/(^|\n)./$1/g;
1488 $stmt =~ s/^\s*{//;
1489 $stmt =~ s/}\s*$//;
1490 $stmt =~ s/^\s*//;
1491 $stmt =~ s/\s*$//;
1492
1493 my @stmt_lines = ($stmt =~ /\n/g);
1494 my @stmt_statements = ($stmt =~ /;/g);
1495
1496 my $stmt_lines = $#stmt_lines + 2;
1497 my $stmt_statements = $#stmt_statements + 1;
1498
1499 if ($stmt_lines > $stmt_statements) {
1500 return $stmt_lines;
1501 } else {
1502 return $stmt_statements;
1503 }
1504}
1505
1506sub ctx_statement_full {
1507 my ($linenr, $remain, $off) = @_;
1508 my ($statement, $condition, $level);
1509
1510 my (@chunks);
1511
1512 # Grab the first conditional/block pair.
1513 ($statement, $condition, $linenr, $remain, $off, $level) =
1514 ctx_statement_block($linenr, $remain, $off);
1515 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1516 push(@chunks, [ $condition, $statement ]);
1517 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1518 return ($level, $linenr, @chunks);
1519 }
1520
1521 # Pull in the following conditional/block pairs and see if they
1522 # could continue the statement.
1523 for (;;) {
1524 ($statement, $condition, $linenr, $remain, $off, $level) =
1525 ctx_statement_block($linenr, $remain, $off);
1526 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1527 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1528 #print "C: push\n";
1529 push(@chunks, [ $condition, $statement ]);
1530 }
1531
1532 return ($level, $linenr, @chunks);
1533}
1534
1535sub ctx_block_get {
1536 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1537 my $line;
1538 my $start = $linenr - 1;
1539 my $blk = '';
1540 my @o;
1541 my @c;
1542 my @res = ();
1543
1544 my $level = 0;
1545 my @stack = ($level);
1546 for ($line = $start; $remain > 0; $line++) {
1547 next if ($rawlines[$line] =~ /^-/);
1548 $remain--;
1549
1550 $blk .= $rawlines[$line];
1551
1552 # Handle nested #if/#else.
1553 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1554 push(@stack, $level);
1555 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1556 $level = $stack[$#stack - 1];
1557 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1558 $level = pop(@stack);
1559 }
1560
1561 foreach my $c (split(//, $lines[$line])) {
1562 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1563 if ($off > 0) {
1564 $off--;
1565 next;
1566 }
1567
1568 if ($c eq $close && $level > 0) {
1569 $level--;
1570 last if ($level == 0);
1571 } elsif ($c eq $open) {
1572 $level++;
1573 }
1574 }
1575
1576 if (!$outer || $level <= 1) {
1577 push(@res, $rawlines[$line]);
1578 }
1579
1580 last if ($level == 0);
1581 }
1582
1583 return ($level, @res);
1584}
1585sub ctx_block_outer {
1586 my ($linenr, $remain) = @_;
1587
1588 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1589 return @r;
1590}
1591sub ctx_block {
1592 my ($linenr, $remain) = @_;
1593
1594 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1595 return @r;
1596}
1597sub ctx_statement {
1598 my ($linenr, $remain, $off) = @_;
1599
1600 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1601 return @r;
1602}
1603sub ctx_block_level {
1604 my ($linenr, $remain) = @_;
1605
1606 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1607}
1608sub ctx_statement_level {
1609 my ($linenr, $remain, $off) = @_;
1610
1611 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1612}
1613
1614sub ctx_locate_comment {
1615 my ($first_line, $end_line) = @_;
1616
1617 # Catch a comment on the end of the line itself.
1618 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1619 return $current_comment if (defined $current_comment);
1620
1621 # Look through the context and try and figure out if there is a
1622 # comment.
1623 my $in_comment = 0;
1624 $current_comment = '';
1625 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1626 my $line = $rawlines[$linenr - 1];
1627 #warn " $line\n";
1628 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1629 $in_comment = 1;
1630 }
1631 if ($line =~ m@/\*@) {
1632 $in_comment = 1;
1633 }
1634 if (!$in_comment && $current_comment ne '') {
1635 $current_comment = '';
1636 }
1637 $current_comment .= $line . "\n" if ($in_comment);
1638 if ($line =~ m@\*/@) {
1639 $in_comment = 0;
1640 }
1641 }
1642
1643 chomp($current_comment);
1644 return($current_comment);
1645}
1646sub ctx_has_comment {
1647 my ($first_line, $end_line) = @_;
1648 my $cmt = ctx_locate_comment($first_line, $end_line);
1649
1650 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1651 ##print "CMMT: $cmt\n";
1652
1653 return ($cmt ne '');
1654}
1655
1656sub raw_line {
1657 my ($linenr, $cnt) = @_;
1658
1659 my $offset = $linenr - 1;
1660 $cnt++;
1661
1662 my $line;
1663 while ($cnt) {
1664 $line = $rawlines[$offset++];
1665 next if (defined($line) && $line =~ /^-/);
1666 $cnt--;
1667 }
1668
1669 return $line;
1670}
1671
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001672sub get_stat_real {
1673 my ($linenr, $lc) = @_;
1674
1675 my $stat_real = raw_line($linenr, 0);
1676 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1677 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1678 }
1679
1680 return $stat_real;
1681}
1682
1683sub get_stat_here {
1684 my ($linenr, $cnt, $here) = @_;
1685
1686 my $herectx = $here . "\n";
1687 for (my $n = 0; $n < $cnt; $n++) {
1688 $herectx .= raw_line($linenr, $n) . "\n";
1689 }
1690
1691 return $herectx;
1692}
1693
Joe Hershberger05622192011-10-18 10:06:59 +00001694sub cat_vet {
1695 my ($vet) = @_;
1696 my ($res, $coded);
1697
1698 $res = '';
1699 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1700 $res .= $1;
1701 if ($2 ne '') {
1702 $coded = sprintf("^%c", unpack('C', $2) + 64);
1703 $res .= $coded;
1704 }
1705 }
1706 $res =~ s/$/\$/;
1707
1708 return $res;
1709}
1710
1711my $av_preprocessor = 0;
1712my $av_pending;
1713my @av_paren_type;
1714my $av_pend_colon;
1715
1716sub annotate_reset {
1717 $av_preprocessor = 0;
1718 $av_pending = '_';
1719 @av_paren_type = ('E');
1720 $av_pend_colon = 'O';
1721}
1722
1723sub annotate_values {
1724 my ($stream, $type) = @_;
1725
1726 my $res;
1727 my $var = '_' x length($stream);
1728 my $cur = $stream;
1729
1730 print "$stream\n" if ($dbg_values > 1);
1731
1732 while (length($cur)) {
1733 @av_paren_type = ('E') if ($#av_paren_type < 0);
1734 print " <" . join('', @av_paren_type) .
1735 "> <$type> <$av_pending>" if ($dbg_values > 1);
1736 if ($cur =~ /^(\s+)/o) {
1737 print "WS($1)\n" if ($dbg_values > 1);
1738 if ($1 =~ /\n/ && $av_preprocessor) {
1739 $type = pop(@av_paren_type);
1740 $av_preprocessor = 0;
1741 }
1742
1743 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1744 print "CAST($1)\n" if ($dbg_values > 1);
1745 push(@av_paren_type, $type);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001746 $type = 'c';
Joe Hershberger05622192011-10-18 10:06:59 +00001747
1748 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1749 print "DECLARE($1)\n" if ($dbg_values > 1);
1750 $type = 'T';
1751
1752 } elsif ($cur =~ /^($Modifier)\s*/) {
1753 print "MODIFIER($1)\n" if ($dbg_values > 1);
1754 $type = 'T';
1755
1756 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1757 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1758 $av_preprocessor = 1;
1759 push(@av_paren_type, $type);
1760 if ($2 ne '') {
1761 $av_pending = 'N';
1762 }
1763 $type = 'E';
1764
1765 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1766 print "UNDEF($1)\n" if ($dbg_values > 1);
1767 $av_preprocessor = 1;
1768 push(@av_paren_type, $type);
1769
1770 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1771 print "PRE_START($1)\n" if ($dbg_values > 1);
1772 $av_preprocessor = 1;
1773
1774 push(@av_paren_type, $type);
1775 push(@av_paren_type, $type);
1776 $type = 'E';
1777
1778 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1779 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1780 $av_preprocessor = 1;
1781
1782 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1783
1784 $type = 'E';
1785
1786 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1787 print "PRE_END($1)\n" if ($dbg_values > 1);
1788
1789 $av_preprocessor = 1;
1790
1791 # Assume all arms of the conditional end as this
1792 # one does, and continue as if the #endif was not here.
1793 pop(@av_paren_type);
1794 push(@av_paren_type, $type);
1795 $type = 'E';
1796
1797 } elsif ($cur =~ /^(\\\n)/o) {
1798 print "PRECONT($1)\n" if ($dbg_values > 1);
1799
1800 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1801 print "ATTR($1)\n" if ($dbg_values > 1);
1802 $av_pending = $type;
1803 $type = 'N';
1804
1805 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1806 print "SIZEOF($1)\n" if ($dbg_values > 1);
1807 if (defined $2) {
1808 $av_pending = 'V';
1809 }
1810 $type = 'N';
1811
1812 } elsif ($cur =~ /^(if|while|for)\b/o) {
1813 print "COND($1)\n" if ($dbg_values > 1);
1814 $av_pending = 'E';
1815 $type = 'N';
1816
1817 } elsif ($cur =~/^(case)/o) {
1818 print "CASE($1)\n" if ($dbg_values > 1);
1819 $av_pend_colon = 'C';
1820 $type = 'N';
1821
1822 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1823 print "KEYWORD($1)\n" if ($dbg_values > 1);
1824 $type = 'N';
1825
1826 } elsif ($cur =~ /^(\()/o) {
1827 print "PAREN('$1')\n" if ($dbg_values > 1);
1828 push(@av_paren_type, $av_pending);
1829 $av_pending = '_';
1830 $type = 'N';
1831
1832 } elsif ($cur =~ /^(\))/o) {
1833 my $new_type = pop(@av_paren_type);
1834 if ($new_type ne '_') {
1835 $type = $new_type;
1836 print "PAREN('$1') -> $type\n"
1837 if ($dbg_values > 1);
1838 } else {
1839 print "PAREN('$1')\n" if ($dbg_values > 1);
1840 }
1841
1842 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1843 print "FUNC($1)\n" if ($dbg_values > 1);
1844 $type = 'V';
1845 $av_pending = 'V';
1846
1847 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1848 if (defined $2 && $type eq 'C' || $type eq 'T') {
1849 $av_pend_colon = 'B';
1850 } elsif ($type eq 'E') {
1851 $av_pend_colon = 'L';
1852 }
1853 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1854 $type = 'V';
1855
1856 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1857 print "IDENT($1)\n" if ($dbg_values > 1);
1858 $type = 'V';
1859
1860 } elsif ($cur =~ /^($Assignment)/o) {
1861 print "ASSIGN($1)\n" if ($dbg_values > 1);
1862 $type = 'N';
1863
1864 } elsif ($cur =~/^(;|{|})/) {
1865 print "END($1)\n" if ($dbg_values > 1);
1866 $type = 'E';
1867 $av_pend_colon = 'O';
1868
1869 } elsif ($cur =~/^(,)/) {
1870 print "COMMA($1)\n" if ($dbg_values > 1);
1871 $type = 'C';
1872
1873 } elsif ($cur =~ /^(\?)/o) {
1874 print "QUESTION($1)\n" if ($dbg_values > 1);
1875 $type = 'N';
1876
1877 } elsif ($cur =~ /^(:)/o) {
1878 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1879
1880 substr($var, length($res), 1, $av_pend_colon);
1881 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1882 $type = 'E';
1883 } else {
1884 $type = 'N';
1885 }
1886 $av_pend_colon = 'O';
1887
1888 } elsif ($cur =~ /^(\[)/o) {
1889 print "CLOSE($1)\n" if ($dbg_values > 1);
1890 $type = 'N';
1891
1892 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1893 my $variant;
1894
1895 print "OPV($1)\n" if ($dbg_values > 1);
1896 if ($type eq 'V') {
1897 $variant = 'B';
1898 } else {
1899 $variant = 'U';
1900 }
1901
1902 substr($var, length($res), 1, $variant);
1903 $type = 'N';
1904
1905 } elsif ($cur =~ /^($Operators)/o) {
1906 print "OP($1)\n" if ($dbg_values > 1);
1907 if ($1 ne '++' && $1 ne '--') {
1908 $type = 'N';
1909 }
1910
1911 } elsif ($cur =~ /(^.)/o) {
1912 print "C($1)\n" if ($dbg_values > 1);
1913 }
1914 if (defined $1) {
1915 $cur = substr($cur, length($1));
1916 $res .= $type x length($1);
1917 }
1918 }
1919
1920 return ($res, $var);
1921}
1922
1923sub possible {
1924 my ($possible, $line) = @_;
1925 my $notPermitted = qr{(?:
1926 ^(?:
1927 $Modifier|
1928 $Storage|
1929 $Type|
1930 DEFINE_\S+
1931 )$|
1932 ^(?:
1933 goto|
1934 return|
1935 case|
1936 else|
1937 asm|__asm__|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001938 do|
1939 \#|
1940 \#\#|
Joe Hershberger05622192011-10-18 10:06:59 +00001941 )(?:\s|$)|
1942 ^(?:typedef|struct|enum)\b
1943 )}x;
1944 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1945 if ($possible !~ $notPermitted) {
1946 # Check for modifiers.
1947 $possible =~ s/\s*$Storage\s*//g;
1948 $possible =~ s/\s*$Sparse\s*//g;
1949 if ($possible =~ /^\s*$/) {
1950
1951 } elsif ($possible =~ /\s/) {
1952 $possible =~ s/\s*$Type\s*//g;
1953 for my $modifier (split(' ', $possible)) {
1954 if ($modifier !~ $notPermitted) {
1955 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001956 push(@modifierListFile, $modifier);
Joe Hershberger05622192011-10-18 10:06:59 +00001957 }
1958 }
1959
1960 } else {
1961 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001962 push(@typeListFile, $possible);
Joe Hershberger05622192011-10-18 10:06:59 +00001963 }
1964 build_types();
1965 } else {
1966 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1967 }
1968}
1969
1970my $prefix = '';
1971
1972sub show_type {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001973 my ($type) = @_;
Tom Rini6b9709d2014-02-27 08:27:28 -05001974
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001975 $type =~ tr/[a-z]/[A-Z]/;
1976
1977 return defined $use_type{$type} if (scalar keys %use_type > 0);
1978
1979 return !defined $ignore_type{$type};
Joe Hershberger05622192011-10-18 10:06:59 +00001980}
1981
1982sub report {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001983 my ($level, $type, $msg) = @_;
1984
1985 if (!show_type($type) ||
1986 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Joe Hershberger05622192011-10-18 10:06:59 +00001987 return 0;
1988 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001989 my $output = '';
1990 if ($color) {
1991 if ($level eq 'ERROR') {
1992 $output .= RED;
1993 } elsif ($level eq 'WARNING') {
1994 $output .= YELLOW;
1995 } else {
1996 $output .= GREEN;
1997 }
Joe Hershberger05622192011-10-18 10:06:59 +00001998 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001999 $output .= $prefix . $level . ':';
2000 if ($show_types) {
2001 $output .= BLUE if ($color);
2002 $output .= "$type:";
2003 }
2004 $output .= RESET if ($color);
2005 $output .= ' ' . $msg . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00002006
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002007 if ($showfile) {
2008 my @lines = split("\n", $output, -1);
2009 splice(@lines, 1, 1);
2010 $output = join("\n", @lines);
2011 }
2012 $output = (split('\n', $output))[0] . "\n" if ($terse);
2013
2014 push(our @report, $output);
Joe Hershberger05622192011-10-18 10:06:59 +00002015
2016 return 1;
2017}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002018
Joe Hershberger05622192011-10-18 10:06:59 +00002019sub report_dump {
2020 our @report;
2021}
2022
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002023sub fixup_current_range {
2024 my ($lineRef, $offset, $length) = @_;
2025
2026 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2027 my $o = $1;
2028 my $l = $2;
2029 my $no = $o + $offset;
2030 my $nl = $l + $length;
2031 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2032 }
2033}
2034
2035sub fix_inserted_deleted_lines {
2036 my ($linesRef, $insertedRef, $deletedRef) = @_;
2037
2038 my $range_last_linenr = 0;
2039 my $delta_offset = 0;
2040
2041 my $old_linenr = 0;
2042 my $new_linenr = 0;
2043
2044 my $next_insert = 0;
2045 my $next_delete = 0;
2046
2047 my @lines = ();
2048
2049 my $inserted = @{$insertedRef}[$next_insert++];
2050 my $deleted = @{$deletedRef}[$next_delete++];
2051
2052 foreach my $old_line (@{$linesRef}) {
2053 my $save_line = 1;
2054 my $line = $old_line; #don't modify the array
2055 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2056 $delta_offset = 0;
2057 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2058 $range_last_linenr = $new_linenr;
2059 fixup_current_range(\$line, $delta_offset, 0);
2060 }
2061
2062 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2063 $deleted = @{$deletedRef}[$next_delete++];
2064 $save_line = 0;
2065 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2066 }
2067
2068 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2069 push(@lines, ${$inserted}{'LINE'});
2070 $inserted = @{$insertedRef}[$next_insert++];
2071 $new_linenr++;
2072 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2073 }
2074
2075 if ($save_line) {
2076 push(@lines, $line);
2077 $new_linenr++;
2078 }
2079
2080 $old_linenr++;
2081 }
2082
2083 return @lines;
2084}
2085
2086sub fix_insert_line {
2087 my ($linenr, $line) = @_;
2088
2089 my $inserted = {
2090 LINENR => $linenr,
2091 LINE => $line,
2092 };
2093 push(@fixed_inserted, $inserted);
2094}
2095
2096sub fix_delete_line {
2097 my ($linenr, $line) = @_;
2098
2099 my $deleted = {
2100 LINENR => $linenr,
2101 LINE => $line,
2102 };
2103
2104 push(@fixed_deleted, $deleted);
2105}
2106
Joe Hershberger05622192011-10-18 10:06:59 +00002107sub ERROR {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002108 my ($type, $msg) = @_;
2109
2110 if (report("ERROR", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002111 our $clean = 0;
2112 our $cnt_error++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002113 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002114 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002115 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002116}
2117sub WARN {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002118 my ($type, $msg) = @_;
2119
2120 if (report("WARNING", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002121 our $clean = 0;
2122 our $cnt_warn++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002123 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002124 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002125 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002126}
2127sub CHK {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002128 my ($type, $msg) = @_;
2129
2130 if ($check && report("CHECK", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002131 our $clean = 0;
2132 our $cnt_chk++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002133 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002134 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002135 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002136}
2137
2138sub check_absolute_file {
2139 my ($absolute, $herecurr) = @_;
2140 my $file = $absolute;
2141
2142 ##print "absolute<$absolute>\n";
2143
2144 # See if any suffix of this path is a path within the tree.
2145 while ($file =~ s@^[^/]*/@@) {
2146 if (-f "$root/$file") {
2147 ##print "file<$file>\n";
2148 last;
2149 }
2150 }
2151 if (! -f _) {
2152 return 0;
2153 }
2154
2155 # It is, so see if the prefix is acceptable.
2156 my $prefix = $absolute;
2157 substr($prefix, -length($file)) = '';
2158
2159 ##print "prefix<$prefix>\n";
2160 if ($prefix ne ".../") {
2161 WARN("USE_RELATIVE_PATH",
2162 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2163 }
2164}
2165
Tom Rini6b9709d2014-02-27 08:27:28 -05002166sub trim {
2167 my ($string) = @_;
2168
2169 $string =~ s/^\s+|\s+$//g;
2170
2171 return $string;
2172}
2173
2174sub ltrim {
2175 my ($string) = @_;
2176
2177 $string =~ s/^\s+//;
2178
2179 return $string;
2180}
2181
2182sub rtrim {
2183 my ($string) = @_;
2184
2185 $string =~ s/\s+$//;
2186
2187 return $string;
2188}
2189
2190sub string_find_replace {
2191 my ($string, $find, $replace) = @_;
2192
2193 $string =~ s/$find/$replace/g;
2194
2195 return $string;
2196}
2197
2198sub tabify {
2199 my ($leading) = @_;
2200
2201 my $source_indent = 8;
2202 my $max_spaces_before_tab = $source_indent - 1;
2203 my $spaces_to_tab = " " x $source_indent;
2204
2205 #convert leading spaces to tabs
2206 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2207 #Remove spaces before a tab
2208 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2209
2210 return "$leading";
2211}
2212
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002213sub pos_last_openparen {
2214 my ($line) = @_;
2215
2216 my $pos = 0;
2217
2218 my $opens = $line =~ tr/\(/\(/;
2219 my $closes = $line =~ tr/\)/\)/;
2220
2221 my $last_openparen = 0;
2222
2223 if (($opens == 0) || ($closes >= $opens)) {
2224 return -1;
2225 }
2226
2227 my $len = length($line);
2228
2229 for ($pos = 0; $pos < $len; $pos++) {
2230 my $string = substr($line, $pos);
2231 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2232 $pos += length($1) - 1;
2233 } elsif (substr($line, $pos, 1) eq '(') {
2234 $last_openparen = $pos;
2235 } elsif (index($string, '(') == -1) {
2236 last;
2237 }
2238 }
2239
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002240 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002241}
2242
Simon Glassb77df592020-05-22 16:32:36 -06002243# Checks specific to U-Boot
2244sub u_boot_line {
2245 my ($realfile, $line, $herecurr) = @_;
Simon Glass281236c2020-05-22 16:32:37 -06002246
2247 # ask for a test if a new uclass ID is added
2248 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2249 WARN("NEW_UCLASS",
2250 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2251 }
Simon Glassb77df592020-05-22 16:32:36 -06002252}
2253
Joe Hershberger05622192011-10-18 10:06:59 +00002254sub process {
2255 my $filename = shift;
2256
2257 my $linenr=0;
2258 my $prevline="";
2259 my $prevrawline="";
2260 my $stashline="";
2261 my $stashrawline="";
2262
2263 my $length;
2264 my $indent;
2265 my $previndent=0;
2266 my $stashindent=0;
2267
2268 our $clean = 1;
2269 my $signoff = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002270 my $author = '';
2271 my $authorsignoff = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002272 my $is_patch = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002273 my $is_binding_patch = -1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002274 my $in_header_lines = $file ? 0 : 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002275 my $in_commit_log = 0; #Scanning lines before patch
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002276 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002277 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002278 my $commit_log_possible_stack_dump = 0;
2279 my $commit_log_long_line = 0;
2280 my $commit_log_has_diff = 0;
2281 my $reported_maintainer_file = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002282 my $non_utf8_charset = 0;
2283
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002284 my $last_blank_line = 0;
2285 my $last_coalesced_string_linenr = -1;
2286
Joe Hershberger05622192011-10-18 10:06:59 +00002287 our @report = ();
2288 our $cnt_lines = 0;
2289 our $cnt_error = 0;
2290 our $cnt_warn = 0;
2291 our $cnt_chk = 0;
2292
2293 # Trace the real file/line as we go.
2294 my $realfile = '';
2295 my $realline = 0;
2296 my $realcnt = 0;
2297 my $here = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002298 my $context_function; #undef'd unless there's a known function
Joe Hershberger05622192011-10-18 10:06:59 +00002299 my $in_comment = 0;
2300 my $comment_edge = 0;
2301 my $first_line = 0;
2302 my $p1_prefix = '';
2303
2304 my $prev_values = 'E';
2305
2306 # suppression flags
2307 my %suppress_ifbraces;
2308 my %suppress_whiletrailers;
2309 my %suppress_export;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002310 my $suppress_statement = 0;
2311
Tom Rini6b9709d2014-02-27 08:27:28 -05002312 my %signatures = ();
Joe Hershberger05622192011-10-18 10:06:59 +00002313
2314 # Pre-scan the patch sanitizing the lines.
2315 # Pre-scan the patch looking for any __setup documentation.
2316 #
2317 my @setup_docs = ();
2318 my $setup_docs = 0;
2319
Tom Rini6b9709d2014-02-27 08:27:28 -05002320 my $camelcase_file_seeded = 0;
2321
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002322 my $checklicenseline = 1;
2323
Joe Hershberger05622192011-10-18 10:06:59 +00002324 sanitise_line_reset();
2325 my $line;
2326 foreach my $rawline (@rawlines) {
2327 $linenr++;
2328 $line = $rawline;
2329
Tom Rini6b9709d2014-02-27 08:27:28 -05002330 push(@fixed, $rawline) if ($fix);
2331
Joe Hershberger05622192011-10-18 10:06:59 +00002332 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2333 $setup_docs = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002334 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Joe Hershberger05622192011-10-18 10:06:59 +00002335 $setup_docs = 1;
2336 }
2337 #next;
2338 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002339 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002340 $realline=$1-1;
2341 if (defined $2) {
2342 $realcnt=$3+1;
2343 } else {
2344 $realcnt=1+1;
2345 }
2346 $in_comment = 0;
2347
2348 # Guestimate if this is a continuing comment. Run
2349 # the context looking for a comment "edge". If this
2350 # edge is a close comment then we must be in a comment
2351 # at context start.
2352 my $edge;
2353 my $cnt = $realcnt;
2354 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2355 next if (defined $rawlines[$ln - 1] &&
2356 $rawlines[$ln - 1] =~ /^-/);
2357 $cnt--;
2358 #print "RAW<$rawlines[$ln - 1]>\n";
2359 last if (!defined $rawlines[$ln - 1]);
2360 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2361 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2362 ($edge) = $1;
2363 last;
2364 }
2365 }
2366 if (defined $edge && $edge eq '*/') {
2367 $in_comment = 1;
2368 }
2369
2370 # Guestimate if this is a continuing comment. If this
2371 # is the start of a diff block and this line starts
2372 # ' *' then it is very likely a comment.
2373 if (!defined $edge &&
2374 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2375 {
2376 $in_comment = 1;
2377 }
2378
2379 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2380 sanitise_line_reset($in_comment);
2381
2382 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2383 # Standardise the strings and chars within the input to
2384 # simplify matching -- only bother with positive lines.
2385 $line = sanitise_line($rawline);
2386 }
2387 push(@lines, $line);
2388
2389 if ($realcnt > 1) {
2390 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2391 } else {
2392 $realcnt = 0;
2393 }
2394
2395 #print "==>$rawline\n";
2396 #print "-->$line\n";
2397
2398 if ($setup_docs && $line =~ /^\+/) {
2399 push(@setup_docs, $line);
2400 }
2401 }
2402
2403 $prefix = '';
2404
2405 $realcnt = 0;
2406 $linenr = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002407 $fixlinenr = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00002408 foreach my $line (@lines) {
2409 $linenr++;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002410 $fixlinenr++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002411 my $sline = $line; #copy of $line
2412 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershberger05622192011-10-18 10:06:59 +00002413
2414 my $rawline = $rawlines[$linenr - 1];
2415
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002416# check if it's a mode change, rename or start of a patch
2417 if (!$in_commit_log &&
2418 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2419 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2420 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2421 $is_patch = 1;
2422 }
2423
Joe Hershberger05622192011-10-18 10:06:59 +00002424#extract the line range in the file after the patch is applied
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002425 if (!$in_commit_log &&
2426 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2427 my $context = $4;
Joe Hershberger05622192011-10-18 10:06:59 +00002428 $is_patch = 1;
2429 $first_line = $linenr + 1;
2430 $realline=$1-1;
2431 if (defined $2) {
2432 $realcnt=$3+1;
2433 } else {
2434 $realcnt=1+1;
2435 }
2436 annotate_reset();
2437 $prev_values = 'E';
2438
2439 %suppress_ifbraces = ();
2440 %suppress_whiletrailers = ();
2441 %suppress_export = ();
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002442 $suppress_statement = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002443 if ($context =~ /\b(\w+)\s*\(/) {
2444 $context_function = $1;
2445 } else {
2446 undef $context_function;
2447 }
Joe Hershberger05622192011-10-18 10:06:59 +00002448 next;
2449
2450# track the line number as we move through the hunk, note that
2451# new versions of GNU diff omit the leading space on completely
2452# blank context lines so we need to count that too.
2453 } elsif ($line =~ /^( |\+|$)/) {
2454 $realline++;
2455 $realcnt-- if ($realcnt != 0);
2456
2457 # Measure the line length and indent.
2458 ($length, $indent) = line_stats($rawline);
2459
2460 # Track the previous line.
2461 ($prevline, $stashline) = ($stashline, $line);
2462 ($previndent, $stashindent) = ($stashindent, $indent);
2463 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2464
2465 #warn "line<$line>\n";
2466
2467 } elsif ($realcnt == 1) {
2468 $realcnt--;
2469 }
2470
2471 my $hunk_line = ($realcnt != 0);
2472
Joe Hershberger05622192011-10-18 10:06:59 +00002473 $here = "#$linenr: " if (!$file);
2474 $here = "#$realline: " if ($file);
2475
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002476 my $found_file = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002477 # extract the filename as it passes
2478 if ($line =~ /^diff --git.*?(\S+)$/) {
2479 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002480 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002481 $in_commit_log = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002482 $found_file = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002483 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2484 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002485 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002486 $in_commit_log = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002487
2488 $p1_prefix = $1;
2489 if (!$file && $tree && $p1_prefix ne '' &&
2490 -e "$root/$p1_prefix") {
2491 WARN("PATCH_PREFIX",
2492 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2493 }
2494
2495 if ($realfile =~ m@^include/asm/@) {
2496 ERROR("MODIFIED_INCLUDE_ASM",
2497 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2498 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002499 $found_file = 1;
2500 }
2501
2502#make up the handle for any error we report on this line
2503 if ($showfile) {
2504 $prefix = "$realfile:$realline: "
2505 } elsif ($emacs) {
2506 if ($file) {
2507 $prefix = "$filename:$realline: ";
2508 } else {
2509 $prefix = "$filename:$linenr: ";
2510 }
2511 }
2512
2513 if ($found_file) {
2514 if (is_maintained_obsolete($realfile)) {
2515 WARN("OBSOLETE",
2516 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2517 }
2518 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2519 $check = 1;
2520 } else {
2521 $check = $check_orig;
2522 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002523 $checklicenseline = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002524
2525 if ($realfile !~ /^MAINTAINERS/) {
2526 my $last_binding_patch = $is_binding_patch;
2527
2528 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2529
2530 if (($last_binding_patch != -1) &&
2531 ($last_binding_patch ^ $is_binding_patch)) {
2532 WARN("DT_SPLIT_BINDING_PATCH",
2533 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2534 }
2535 }
2536
Joe Hershberger05622192011-10-18 10:06:59 +00002537 next;
2538 }
2539
2540 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2541
2542 my $hereline = "$here\n$rawline\n";
2543 my $herecurr = "$here\n$rawline\n";
2544 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2545
2546 $cnt_lines++ if ($realcnt != 0);
2547
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002548# Verify the existence of a commit log if appropriate
2549# 2 is used because a $signature is counted in $commit_log_lines
2550 if ($in_commit_log) {
2551 if ($line !~ /^\s*$/) {
2552 $commit_log_lines++; #could be a $signature
2553 }
2554 } elsif ($has_commit_log && $commit_log_lines < 2) {
2555 WARN("COMMIT_MESSAGE",
2556 "Missing commit description - Add an appropriate one\n");
2557 $commit_log_lines = 2; #warn only once
2558 }
2559
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002560# Check if the commit log has what seems like a diff which can confuse patch
2561 if ($in_commit_log && !$commit_log_has_diff &&
2562 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2563 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2564 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2565 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2566 ERROR("DIFF_IN_COMMIT_MSG",
2567 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2568 $commit_log_has_diff = 1;
2569 }
2570
Joe Hershberger05622192011-10-18 10:06:59 +00002571# Check for incorrect file permissions
2572 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2573 my $permhere = $here . "FILE: $realfile\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05002574 if ($realfile !~ m@scripts/@ &&
2575 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002576 ERROR("EXECUTE_PERMISSIONS",
2577 "do not set execute permissions for source files\n" . $permhere);
2578 }
2579 }
2580
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002581# Check the patch for a From:
2582 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2583 $author = $1;
2584 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2585 $author =~ s/"//g;
2586 }
2587
Joe Hershberger05622192011-10-18 10:06:59 +00002588# Check the patch for a signoff:
2589 if ($line =~ /^\s*signed-off-by:/i) {
2590 $signoff++;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002591 $in_commit_log = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002592 if ($author ne '') {
2593 my $l = $line;
2594 $l =~ s/"//g;
2595 if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2596 $authorsignoff = 1;
2597 }
2598 }
Joe Hershberger05622192011-10-18 10:06:59 +00002599 }
2600
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002601# Check if MAINTAINERS is being updated. If so, there's probably no need to
2602# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2603 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2604 $reported_maintainer_file = 1;
2605 }
2606
Joe Hershberger05622192011-10-18 10:06:59 +00002607# Check signature styles
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002608 if (!$in_header_lines &&
2609 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00002610 my $space_before = $1;
2611 my $sign_off = $2;
2612 my $space_after = $3;
2613 my $email = $4;
2614 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2615
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002616 if ($sign_off !~ /$signature_tags/) {
2617 WARN("BAD_SIGN_OFF",
2618 "Non-standard signature: $sign_off\n" . $herecurr);
2619 }
Joe Hershberger05622192011-10-18 10:06:59 +00002620 if (defined $space_before && $space_before ne "") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002621 if (WARN("BAD_SIGN_OFF",
2622 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2623 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002624 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002625 "$ucfirst_sign_off $email";
2626 }
Joe Hershberger05622192011-10-18 10:06:59 +00002627 }
2628 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rini6b9709d2014-02-27 08:27:28 -05002629 if (WARN("BAD_SIGN_OFF",
2630 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2631 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002632 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002633 "$ucfirst_sign_off $email";
2634 }
2635
Joe Hershberger05622192011-10-18 10:06:59 +00002636 }
2637 if (!defined $space_after || $space_after ne " ") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002638 if (WARN("BAD_SIGN_OFF",
2639 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2640 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002641 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002642 "$ucfirst_sign_off $email";
2643 }
Joe Hershberger05622192011-10-18 10:06:59 +00002644 }
2645
2646 my ($email_name, $email_address, $comment) = parse_email($email);
2647 my $suggested_email = format_email(($email_name, $email_address));
2648 if ($suggested_email eq "") {
2649 ERROR("BAD_SIGN_OFF",
2650 "Unrecognized email address: '$email'\n" . $herecurr);
2651 } else {
2652 my $dequoted = $suggested_email;
2653 $dequoted =~ s/^"//;
2654 $dequoted =~ s/" </ </;
2655 # Don't force email to have quotes
2656 # Allow just an angle bracketed address
2657 if ("$dequoted$comment" ne $email &&
2658 "<$email_address>$comment" ne $email &&
2659 "$suggested_email$comment" ne $email) {
2660 WARN("BAD_SIGN_OFF",
2661 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2662 }
2663 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002664
2665# Check for duplicate signatures
2666 my $sig_nospace = $line;
2667 $sig_nospace =~ s/\s//g;
2668 $sig_nospace = lc($sig_nospace);
2669 if (defined $signatures{$sig_nospace}) {
2670 WARN("BAD_SIGN_OFF",
2671 "Duplicate signature\n" . $herecurr);
2672 } else {
2673 $signatures{$sig_nospace} = 1;
2674 }
Joe Hershberger05622192011-10-18 10:06:59 +00002675 }
2676
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002677# Check email subject for common tools that don't need to be mentioned
2678 if ($in_header_lines &&
2679 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2680 WARN("EMAIL_SUBJECT",
2681 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2682 }
2683
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002684# Check for unwanted Gerrit info
2685 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2686 ERROR("GERRIT_CHANGE_ID",
2687 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2688 }
2689
2690# Check if the commit log is in a possible stack dump
2691 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2692 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2693 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2694 # timestamp
Simon Glass048a6482020-05-22 16:32:35 -06002695 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2696 # stack dump address
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002697 $commit_log_possible_stack_dump = 1;
2698 }
2699
2700# Check for line lengths > 75 in commit log, warn once
2701 if ($in_commit_log && !$commit_log_long_line &&
2702 length($line) > 75 &&
2703 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2704 # file delta changes
2705 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2706 # filename then :
2707 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2708 # A Fixes: or Link: line
2709 $commit_log_possible_stack_dump)) {
2710 WARN("COMMIT_LOG_LONG_LINE",
2711 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2712 $commit_log_long_line = 1;
2713 }
2714
2715# Reset possible stack dump if a blank line is found
2716 if ($in_commit_log && $commit_log_possible_stack_dump &&
2717 $line =~ /^\s*$/) {
2718 $commit_log_possible_stack_dump = 0;
2719 }
2720
2721# Check for git id commit length and improperly formed commit descriptions
2722 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2723 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2724 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2725 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2726 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2727 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2728 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2729 my $init_char = "c";
2730 my $orig_commit = "";
2731 my $short = 1;
2732 my $long = 0;
2733 my $case = 1;
2734 my $space = 1;
2735 my $hasdesc = 0;
2736 my $hasparens = 0;
2737 my $id = '0123456789ab';
2738 my $orig_desc = "commit description";
2739 my $description = "";
2740
2741 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2742 $init_char = $1;
2743 $orig_commit = lc($2);
2744 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2745 $orig_commit = lc($1);
2746 }
2747
2748 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2749 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2750 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2751 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2752 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2753 $orig_desc = $1;
2754 $hasparens = 1;
2755 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2756 defined $rawlines[$linenr] &&
2757 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2758 $orig_desc = $1;
2759 $hasparens = 1;
2760 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2761 defined $rawlines[$linenr] &&
2762 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2763 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2764 $orig_desc = $1;
2765 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2766 $orig_desc .= " " . $1;
2767 $hasparens = 1;
2768 }
2769
2770 ($id, $description) = git_commit_info($orig_commit,
2771 $id, $orig_desc);
2772
2773 if (defined($id) &&
2774 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2775 ERROR("GIT_COMMIT_ID",
2776 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2777 }
2778 }
2779
2780# Check for added, moved or deleted files
2781 if (!$reported_maintainer_file && !$in_commit_log &&
2782 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2783 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2784 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2785 (defined($1) || defined($2))))) {
2786 $is_patch = 1;
2787 $reported_maintainer_file = 1;
2788 WARN("FILE_PATH_CHANGES",
2789 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2790 }
2791
Joe Hershberger05622192011-10-18 10:06:59 +00002792# Check for wrappage within a valid hunk of the file
2793 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2794 ERROR("CORRUPTED_PATCH",
2795 "patch seems to be corrupt (line wrapped?)\n" .
2796 $herecurr) if (!$emitted_corrupt++);
2797 }
2798
Joe Hershberger05622192011-10-18 10:06:59 +00002799# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2800 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2801 $rawline !~ m/^$UTF8*$/) {
2802 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2803
2804 my $blank = copy_spacing($rawline);
2805 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2806 my $hereptr = "$hereline$ptr\n";
2807
2808 CHK("INVALID_UTF8",
2809 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2810 }
2811
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002812# Check if it's the start of a commit log
2813# (not a header line and we haven't seen the patch filename)
2814 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002815 !($rawline =~ /^\s+(?:\S|$)/ ||
2816 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002817 $in_header_lines = 0;
2818 $in_commit_log = 1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002819 $has_commit_log = 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002820 }
2821
2822# Check if there is UTF-8 in a commit log when a mail header has explicitly
2823# declined it, i.e defined some charset where it is missing.
2824 if ($in_header_lines &&
2825 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2826 $1 !~ /utf-8/i) {
2827 $non_utf8_charset = 1;
2828 }
2829
2830 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2831 $rawline =~ /$NON_ASCII_UTF8/) {
2832 WARN("UTF8_BEFORE_PATCH",
2833 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2834 }
2835
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002836# Check for absolute kernel paths in commit message
2837 if ($tree && $in_commit_log) {
2838 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2839 my $file = $1;
2840
2841 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2842 check_absolute_file($1, $herecurr)) {
2843 #
2844 } else {
2845 check_absolute_file($file, $herecurr);
2846 }
2847 }
2848 }
2849
Dan Murphyc10e0f52017-01-31 14:15:53 -06002850# Check for various typo / spelling mistakes
2851 if (defined($misspellings) &&
2852 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2853 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2854 my $typo = $1;
2855 my $typo_fix = $spelling_fix{lc($typo)};
2856 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2857 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002858 my $msg_level = \&WARN;
2859 $msg_level = \&CHK if ($file);
2860 if (&{$msg_level}("TYPO_SPELLING",
2861 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Dan Murphyc10e0f52017-01-31 14:15:53 -06002862 $fix) {
2863 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2864 }
2865 }
2866 }
2867
Joe Hershberger05622192011-10-18 10:06:59 +00002868# ignore non-hunk lines and lines being removed
2869 next if (!$hunk_line || $line =~ /^-/);
2870
2871#trailing whitespace
2872 if ($line =~ /^\+.*\015/) {
2873 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05002874 if (ERROR("DOS_LINE_ENDINGS",
2875 "DOS line endings\n" . $herevet) &&
2876 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002877 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05002878 }
Joe Hershberger05622192011-10-18 10:06:59 +00002879 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2880 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05002881 if (ERROR("TRAILING_WHITESPACE",
2882 "trailing whitespace\n" . $herevet) &&
2883 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002884 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05002885 }
2886
Joe Hershberger05622192011-10-18 10:06:59 +00002887 $rpt_cleaners = 1;
2888 }
2889
Tom Rini6b9709d2014-02-27 08:27:28 -05002890# Check for FSF mailing addresses.
2891 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002892 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rini6b9709d2014-02-27 08:27:28 -05002893 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2894 $rawline =~ /\b51\s+Franklin\s+St/i) {
2895 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002896 my $msg_level = \&ERROR;
2897 $msg_level = \&CHK if ($file);
2898 &{$msg_level}("FSF_MAILING_ADDRESS",
2899 "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)
Tom Rini6b9709d2014-02-27 08:27:28 -05002900 }
2901
Joe Hershberger05622192011-10-18 10:06:59 +00002902# check for Kconfig help text having a real description
2903# Only applies when adding the entry originally, after that we do not have
2904# sufficient context to determine whether it is indeed long enough.
2905 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002906 # 'choice' is usually the last thing on the line (though
2907 # Kconfig supports named choices), so use a word boundary
2908 # (\b) rather than a whitespace character (\s)
2909 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002910 my $length = 0;
2911 my $cnt = $realcnt;
2912 my $ln = $linenr + 1;
2913 my $f;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002914 my $is_start = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002915 my $is_end = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002916 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershberger05622192011-10-18 10:06:59 +00002917 $f = $lines[$ln - 1];
2918 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2919 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershberger05622192011-10-18 10:06:59 +00002920
2921 next if ($f =~ /^-/);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002922 last if (!$file && $f =~ /^\@\@/);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002923
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002924 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002925 $is_start = 1;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002926 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
2927 if ($lines[$ln - 1] =~ "---help---") {
2928 WARN("CONFIG_DESCRIPTION",
2929 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
2930 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002931 $length = -1;
2932 }
2933
Joe Hershberger05622192011-10-18 10:06:59 +00002934 $f =~ s/^.//;
2935 $f =~ s/#.*//;
2936 $f =~ s/^\s+//;
2937 next if ($f =~ /^$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002938
2939 # This only checks context lines in the patch
2940 # and so hopefully shouldn't trigger false
2941 # positives, even though some of these are
2942 # common words in help texts
2943 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2944 if|endif|menu|endmenu|source)\b/x) {
Joe Hershberger05622192011-10-18 10:06:59 +00002945 $is_end = 1;
2946 last;
2947 }
2948 $length++;
2949 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002950 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2951 WARN("CONFIG_DESCRIPTION",
2952 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2953 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002954 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2955 }
2956
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002957# check for MAINTAINERS entries that don't have the right form
2958 if ($realfile =~ /^MAINTAINERS$/ &&
2959 $rawline =~ /^\+[A-Z]:/ &&
2960 $rawline !~ /^\+[A-Z]:\t\S/) {
2961 if (WARN("MAINTAINERS_STYLE",
2962 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2963 $fix) {
2964 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2965 }
2966 }
2967
2968# discourage the use of boolean for type definition attributes of Kconfig options
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002969 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002970 $line =~ /^\+\s*\bboolean\b/) {
2971 WARN("CONFIG_TYPE_BOOLEAN",
2972 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002973 }
2974
2975 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2976 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2977 my $flag = $1;
2978 my $replacement = {
2979 'EXTRA_AFLAGS' => 'asflags-y',
2980 'EXTRA_CFLAGS' => 'ccflags-y',
2981 'EXTRA_CPPFLAGS' => 'cppflags-y',
2982 'EXTRA_LDFLAGS' => 'ldflags-y',
2983 };
2984
2985 WARN("DEPRECATED_VARIABLE",
2986 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
Joe Hershberger05622192011-10-18 10:06:59 +00002987 }
2988
Tom Rini6b9709d2014-02-27 08:27:28 -05002989# check for DT compatible documentation
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002990 if (defined $root &&
2991 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2992 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2993
Tom Rini6b9709d2014-02-27 08:27:28 -05002994 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2995
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002996 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Simon Glass048a6482020-05-22 16:32:35 -06002997 my $vp_file = $dt_path . "vendor-prefixes.txt";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002998
Tom Rini6b9709d2014-02-27 08:27:28 -05002999 foreach my $compat (@compats) {
3000 my $compat2 = $compat;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003001 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3002 my $compat3 = $compat;
3003 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3004 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003005 if ( $? >> 8 ) {
3006 WARN("UNDOCUMENTED_DT_STRING",
3007 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3008 }
3009
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003010 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3011 my $vendor = $1;
Simon Glass048a6482020-05-22 16:32:35 -06003012 `grep -Eq "^$vendor\\b" $vp_file`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003013 if ( $? >> 8 ) {
3014 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003015 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05003016 }
3017 }
3018 }
3019
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003020# check for using SPDX license tag at beginning of files
3021 if ($realline == $checklicenseline) {
3022 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3023 $checklicenseline = 2;
3024 } elsif ($rawline =~ /^\+/) {
3025 my $comment = "";
3026 if ($realfile =~ /\.(h|s|S)$/) {
3027 $comment = '/*';
3028 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3029 $comment = '//';
3030 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
3031 $comment = '#';
3032 } elsif ($realfile =~ /\.rst$/) {
3033 $comment = '..';
3034 }
3035
3036 if ($comment !~ /^$/ &&
Simon Glass048a6482020-05-22 16:32:35 -06003037 $rawline !~ /^\+\Q$comment\E SPDX-License-Identifier: /) {
3038 WARN("SPDX_LICENSE_TAG",
3039 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003040 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Simon Glass048a6482020-05-22 16:32:35 -06003041 my $spdx_license = $1;
3042 if (!is_SPDX_License_valid($spdx_license)) {
3043 WARN("SPDX_LICENSE_TAG",
3044 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3045 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003046 }
3047 }
3048 }
3049
Joe Hershberger05622192011-10-18 10:06:59 +00003050# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003051 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003052
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003053# line length limit (with some exclusions)
3054#
3055# There are a few types of lines that may extend beyond $max_line_length:
3056# logging functions like pr_info that end in a string
3057# lines with a single string
3058# #defines that are a single string
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003059# lines with an RFC3986 like URL
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003060#
3061# There are 3 different line length message types:
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003062# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003063# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3064# LONG_LINE all other lines longer than $max_line_length
3065#
3066# if LONG_LINE is ignored, the other 2 types are also ignored
3067#
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003068
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003069 if ($line =~ /^\+/ && $length > $max_line_length) {
3070 my $msg_type = "LONG_LINE";
Joe Hershberger05622192011-10-18 10:06:59 +00003071
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003072 # Check the allowed long line types first
3073
3074 # logging functions that end in a string that starts
3075 # before $max_line_length
3076 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3077 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3078 $msg_type = "";
3079
3080 # lines with only strings (w/ possible termination)
3081 # #defines with only strings
3082 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3083 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3084 $msg_type = "";
3085
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003086 # More special cases
3087 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3088 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3089 $msg_type = "";
3090
3091 # URL ($rawline is used in case the URL is in a comment)
3092 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003093 $msg_type = "";
3094
3095 # Otherwise set the alternate message types
3096
3097 # a comment starts before $max_line_length
3098 } elsif ($line =~ /($;[\s$;]*)$/ &&
3099 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3100 $msg_type = "LONG_LINE_COMMENT"
3101
3102 # a quoted string starts before $max_line_length
3103 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3104 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3105 $msg_type = "LONG_LINE_STRING"
Tom Rini6b9709d2014-02-27 08:27:28 -05003106 }
3107
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003108 if ($msg_type ne "" &&
3109 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glass048a6482020-05-22 16:32:35 -06003110 my $msg_level = \&WARN;
3111 $msg_level = \&CHK if ($file);
3112 &{$msg_level}($msg_type,
3113 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003114 }
Joe Hershberger05622192011-10-18 10:06:59 +00003115 }
3116
3117# check for adding lines without a newline.
3118 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3119 WARN("MISSING_EOF_NEWLINE",
3120 "adding a line without newline at end of file\n" . $herecurr);
3121 }
3122
Simon Glassb77df592020-05-22 16:32:36 -06003123 if ($u_boot) {
3124 u_boot_line($realfile, $line, $herecurr);
3125 }
3126
Joe Hershberger05622192011-10-18 10:06:59 +00003127# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003128 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003129
3130# at the beginning of a line any tabs must come first and anything
3131# more than 8 must use tabs.
3132 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3133 $rawline =~ /^\+\s* \s*/) {
3134 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003135 $rpt_cleaners = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -05003136 if (ERROR("CODE_INDENT",
3137 "code indent should use tabs where possible\n" . $herevet) &&
3138 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003139 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003140 }
Joe Hershberger05622192011-10-18 10:06:59 +00003141 }
3142
3143# check for space before tabs.
3144 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3145 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003146 if (WARN("SPACE_BEFORE_TAB",
3147 "please, no space before tabs\n" . $herevet) &&
3148 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003149 while ($fixed[$fixlinenr] =~
Joe Perchese3a4fac2014-09-23 12:41:02 +02003150 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003151 while ($fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003152 s/(^\+.*) +\t/$1\t/) {}
3153 }
Joe Hershberger05622192011-10-18 10:06:59 +00003154 }
3155
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003156# check for assignments on the start of a line
3157 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3158 CHK("ASSIGNMENT_CONTINUATIONS",
3159 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3160 }
3161
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003162# check for && or || at the start of a line
3163 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3164 CHK("LOGICAL_CONTINUATIONS",
3165 "Logical continuations should be on the previous line\n" . $hereprev);
3166 }
3167
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003168# check indentation starts on a tab stop
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003169 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003170 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003171 my $indent = length($1);
3172 if ($indent % 8) {
3173 if (WARN("TABSTOP",
3174 "Statements should start on a tabstop\n" . $herecurr) &&
3175 $fix) {
3176 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3177 }
3178 }
3179 }
3180
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003181# check multi-line statement indentation matches previous line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003182 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003183 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003184 $prevline =~ /^\+(\t*)(.*)$/;
3185 my $oldindent = $1;
3186 my $rest = $2;
3187
3188 my $pos = pos_last_openparen($rest);
3189 if ($pos >= 0) {
3190 $line =~ /^(\+| )([ \t]*)/;
3191 my $newindent = $2;
3192
3193 my $goodtabindent = $oldindent .
3194 "\t" x ($pos / 8) .
3195 " " x ($pos % 8);
3196 my $goodspaceindent = $oldindent . " " x $pos;
3197
3198 if ($newindent ne $goodtabindent &&
3199 $newindent ne $goodspaceindent) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003200
3201 if (CHK("PARENTHESIS_ALIGNMENT",
3202 "Alignment should match open parenthesis\n" . $hereprev) &&
3203 $fix && $line =~ /^\+/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003204 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003205 s/^\+[ \t]*/\+$goodtabindent/;
3206 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003207 }
3208 }
3209 }
3210
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003211# check for space after cast like "(int) foo" or "(struct foo) bar"
3212# avoid checking a few false positives:
3213# "sizeof(<type>)" or "__alignof__(<type>)"
3214# function pointer declarations like "(*foo)(int) = bar;"
3215# structure definitions like "(struct foo) { 0 };"
3216# multiline macros that define functions
3217# known attributes or the __attribute__ keyword
3218 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3219 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003220 if (CHK("SPACING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003221 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003222 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003223 $fixed[$fixlinenr] =~
3224 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003225 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003226 }
3227
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003228# Block comment styles
3229# Networking with an initial /*
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003230 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003231 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003232 $rawline =~ /^\+[ \t]*\*/ &&
3233 $realline > 2) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003234 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3235 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3236 }
3237
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003238# Block comments use * on subsequent lines
3239 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3240 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rini6b9709d2014-02-27 08:27:28 -05003241 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3242 $rawline =~ /^\+/ && #line is new
3243 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003244 WARN("BLOCK_COMMENT_STYLE",
3245 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rini6b9709d2014-02-27 08:27:28 -05003246 }
3247
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003248# Block comments use */ on trailing lines
3249 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003250 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3251 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3252 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003253 WARN("BLOCK_COMMENT_STYLE",
3254 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3255 }
3256
3257# Block comment * alignment
3258 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3259 $line =~ /^\+[ \t]*$;/ && #leading comment
3260 $rawline =~ /^\+[ \t]*\*/ && #leading *
3261 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3262 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3263 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3264 my $oldindent;
3265 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3266 if (defined($1)) {
3267 $oldindent = expand_tabs($1);
3268 } else {
3269 $prevrawline =~ m@^\+(.*/?)\*@;
3270 $oldindent = expand_tabs($1);
3271 }
3272 $rawline =~ m@^\+([ \t]*)\*@;
3273 my $newindent = $1;
3274 $newindent = expand_tabs($newindent);
3275 if (length($oldindent) ne length($newindent)) {
3276 WARN("BLOCK_COMMENT_STYLE",
3277 "Block comments should align the * on each line\n" . $hereprev);
3278 }
3279 }
3280
3281# check for missing blank lines after struct/union declarations
3282# with exceptions for various attributes and macros
3283 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3284 $line =~ /^\+/ &&
3285 !($line =~ /^\+\s*$/ ||
3286 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3287 $line =~ /^\+\s*MODULE_/i ||
3288 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3289 $line =~ /^\+[a-z_]*init/ ||
3290 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3291 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003292 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003293 $line =~ /^\+\s*__setup/)) {
3294 if (CHK("LINE_SPACING",
3295 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3296 $fix) {
3297 fix_insert_line($fixlinenr, "\+");
3298 }
3299 }
3300
3301# check for multiple consecutive blank lines
3302 if ($prevline =~ /^[\+ ]\s*$/ &&
3303 $line =~ /^\+\s*$/ &&
3304 $last_blank_line != ($linenr - 1)) {
3305 if (CHK("LINE_SPACING",
3306 "Please don't use multiple blank lines\n" . $hereprev) &&
3307 $fix) {
3308 fix_delete_line($fixlinenr, $rawline);
3309 }
3310
3311 $last_blank_line = $linenr;
3312 }
3313
3314# check for missing blank lines after declarations
3315 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3316 # actual declarations
3317 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3318 # function pointer declarations
3319 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3320 # foo bar; where foo is some local typedef or #define
3321 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3322 # known declaration macros
3323 $prevline =~ /^\+\s+$declaration_macros/) &&
3324 # for "else if" which can look like "$Ident $Ident"
3325 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3326 # other possible extensions of declaration lines
3327 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3328 # not starting a section or a macro "\" extended line
3329 $prevline =~ /(?:\{\s*|\\)$/) &&
3330 # looks like a declaration
3331 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3332 # function pointer declarations
3333 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3334 # foo bar; where foo is some local typedef or #define
3335 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3336 # known declaration macros
3337 $sline =~ /^\+\s+$declaration_macros/ ||
3338 # start of struct or union or enum
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003339 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003340 # start or end of block or continuation of declaration
3341 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3342 # bitfield continuation
3343 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3344 # other possible extensions of declaration lines
3345 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3346 # indentation of previous and current line are the same
3347 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3348 if (WARN("LINE_SPACING",
3349 "Missing a blank line after declarations\n" . $hereprev) &&
3350 $fix) {
3351 fix_insert_line($fixlinenr, "\+");
3352 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003353 }
3354
Joe Hershberger05622192011-10-18 10:06:59 +00003355# check for spaces at the beginning of a line.
3356# Exceptions:
3357# 1) within comments
3358# 2) indented preprocessor commands
3359# 3) hanging labels
Tom Rini6b9709d2014-02-27 08:27:28 -05003360 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003361 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003362 if (WARN("LEADING_SPACE",
3363 "please, no spaces at the start of a line\n" . $herevet) &&
3364 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003365 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003366 }
Joe Hershberger05622192011-10-18 10:06:59 +00003367 }
3368
3369# check we are in a valid C source file if not then ignore this hunk
3370 next if ($realfile !~ /\.(h|c)$/);
3371
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003372# check for unusual line ending [ or (
3373 if ($line =~ /^\+.*([\[\(])\s*$/) {
3374 CHK("OPEN_ENDED_LINE",
3375 "Lines should not end with a '$1'\n" . $herecurr);
3376 }
3377
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003378# check if this appears to be the start function declaration, save the name
3379 if ($sline =~ /^\+\{\s*$/ &&
3380 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3381 $context_function = $1;
3382 }
3383
3384# check if this appears to be the end of function declaration
3385 if ($sline =~ /^\+\}\s*$/) {
3386 undef $context_function;
3387 }
3388
3389# check indentation of any line with a bare else
3390# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3391# if the previous line is a break or return and is indented 1 tab more...
3392 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3393 my $tabs = length($1) + 1;
3394 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3395 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3396 defined $lines[$linenr] &&
3397 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3398 WARN("UNNECESSARY_ELSE",
3399 "else is not generally useful after a break or return\n" . $hereprev);
3400 }
3401 }
3402
3403# check indentation of a line with a break;
3404# if the previous line is a goto or return and is indented the same # of tabs
3405 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3406 my $tabs = $1;
3407 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3408 WARN("UNNECESSARY_BREAK",
3409 "break is not useful after a goto or return\n" . $hereprev);
3410 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003411 }
3412
Joe Hershberger05622192011-10-18 10:06:59 +00003413# check for RCS/CVS revision markers
3414 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3415 WARN("CVS_KEYWORD",
3416 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3417 }
3418
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003419# check for old HOTPLUG __dev<foo> section markings
3420 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3421 WARN("HOTPLUG_SECTION",
3422 "Using $1 is unnecessary\n" . $herecurr);
3423 }
3424
Joe Hershberger05622192011-10-18 10:06:59 +00003425# Check for potential 'bare' types
3426 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3427 $realline_next);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003428#print "LINE<$line>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003429 if ($linenr > $suppress_statement &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003430 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003431 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3432 ctx_statement_block($linenr, $realcnt, 0);
3433 $stat =~ s/\n./\n /g;
3434 $cond =~ s/\n./\n /g;
3435
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003436#print "linenr<$linenr> <$stat>\n";
3437 # If this statement has no statement boundaries within
3438 # it there is no point in retrying a statement scan
3439 # until we hit end of it.
3440 my $frag = $stat; $frag =~ s/;+\s*$//;
3441 if ($frag !~ /(?:{|;)/) {
3442#print "skip<$line_nr_next>\n";
3443 $suppress_statement = $line_nr_next;
3444 }
3445
Joe Hershberger05622192011-10-18 10:06:59 +00003446 # Find the real next line.
3447 $realline_next = $line_nr_next;
3448 if (defined $realline_next &&
3449 (!defined $lines[$realline_next - 1] ||
3450 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3451 $realline_next++;
3452 }
3453
3454 my $s = $stat;
3455 $s =~ s/{.*$//s;
3456
3457 # Ignore goto labels.
3458 if ($s =~ /$Ident:\*$/s) {
3459
3460 # Ignore functions being called
3461 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3462
3463 } elsif ($s =~ /^.\s*else\b/s) {
3464
3465 # declarations always start with types
3466 } 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) {
3467 my $type = $1;
3468 $type =~ s/\s+/ /g;
3469 possible($type, "A:" . $s);
3470
3471 # definitions in global scope can only start with types
3472 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3473 possible($1, "B:" . $s);
3474 }
3475
3476 # any (foo ... *) is a pointer cast, and foo is a type
3477 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3478 possible($1, "C:" . $s);
3479 }
3480
3481 # Check for any sort of function declaration.
3482 # int foo(something bar, other baz);
3483 # void (*store_gdt)(x86_descr_ptr *);
3484 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3485 my ($name_len) = length($1);
3486
3487 my $ctx = $s;
3488 substr($ctx, 0, $name_len + 1, '');
3489 $ctx =~ s/\)[^\)]*$//;
3490
3491 for my $arg (split(/\s*,\s*/, $ctx)) {
3492 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3493
3494 possible($1, "D:" . $s);
3495 }
3496 }
3497 }
3498
3499 }
3500
3501#
3502# Checks which may be anchored in the context.
3503#
3504
3505# Check for switch () and associated case and default
3506# statements should be at the same indent.
3507 if ($line=~/\bswitch\s*\(.*\)/) {
3508 my $err = '';
3509 my $sep = '';
3510 my @ctx = ctx_block_outer($linenr, $realcnt);
3511 shift(@ctx);
3512 for my $ctx (@ctx) {
3513 my ($clen, $cindent) = line_stats($ctx);
3514 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3515 $indent != $cindent) {
3516 $err .= "$sep$ctx\n";
3517 $sep = '';
3518 } else {
3519 $sep = "[...]\n";
3520 }
3521 }
3522 if ($err ne '') {
3523 ERROR("SWITCH_CASE_INDENT_LEVEL",
3524 "switch and case should be at the same indent\n$hereline$err");
3525 }
3526 }
3527
3528# if/while/etc brace do not go on next line, unless defining a do while loop,
3529# or if that brace on the next line is for something else
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003530 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003531 my $pre_ctx = "$1$2";
3532
3533 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003534
3535 if ($line =~ /^\+\t{6,}/) {
3536 WARN("DEEP_INDENTATION",
3537 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3538 }
3539
Joe Hershberger05622192011-10-18 10:06:59 +00003540 my $ctx_cnt = $realcnt - $#ctx - 1;
3541 my $ctx = join("\n", @ctx);
3542
3543 my $ctx_ln = $linenr;
3544 my $ctx_skip = $realcnt;
3545
3546 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3547 defined $lines[$ctx_ln - 1] &&
3548 $lines[$ctx_ln - 1] =~ /^-/)) {
3549 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3550 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3551 $ctx_ln++;
3552 }
3553
3554 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3555 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3556
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003557 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003558 ERROR("OPEN_BRACE",
3559 "that open brace { should be on the previous line\n" .
3560 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3561 }
3562 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3563 $ctx =~ /\)\s*\;\s*$/ &&
3564 defined $lines[$ctx_ln - 1])
3565 {
3566 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3567 if ($nindent > $indent) {
3568 WARN("TRAILING_SEMICOLON",
3569 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3570 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3571 }
3572 }
3573 }
3574
3575# Check relative indent for conditionals and blocks.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003576 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003577 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3578 ctx_statement_block($linenr, $realcnt, 0)
3579 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00003580 my ($s, $c) = ($stat, $cond);
3581
3582 substr($s, 0, length($c), '');
3583
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003584 # remove inline comments
3585 $s =~ s/$;/ /g;
3586 $c =~ s/$;/ /g;
Joe Hershberger05622192011-10-18 10:06:59 +00003587
3588 # Find out how long the conditional actually is.
3589 my @newlines = ($c =~ /\n/gs);
3590 my $cond_lines = 1 + $#newlines;
3591
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003592 # Make sure we remove the line prefixes as we have
3593 # none on the first line, and are going to readd them
3594 # where necessary.
3595 $s =~ s/\n./\n/gs;
3596 while ($s =~ /\n\s+\\\n/) {
3597 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3598 }
3599
Joe Hershberger05622192011-10-18 10:06:59 +00003600 # We want to check the first line inside the block
3601 # starting at the end of the conditional, so remove:
3602 # 1) any blank line termination
3603 # 2) any opening brace { on end of the line
3604 # 3) any do (...) {
3605 my $continuation = 0;
3606 my $check = 0;
3607 $s =~ s/^.*\bdo\b//;
3608 $s =~ s/^\s*{//;
3609 if ($s =~ s/^\s*\\//) {
3610 $continuation = 1;
3611 }
3612 if ($s =~ s/^\s*?\n//) {
3613 $check = 1;
3614 $cond_lines++;
3615 }
3616
3617 # Also ignore a loop construct at the end of a
3618 # preprocessor statement.
3619 if (($prevline =~ /^.\s*#\s*define\s/ ||
3620 $prevline =~ /\\\s*$/) && $continuation == 0) {
3621 $check = 0;
3622 }
3623
3624 my $cond_ptr = -1;
3625 $continuation = 0;
3626 while ($cond_ptr != $cond_lines) {
3627 $cond_ptr = $cond_lines;
3628
3629 # If we see an #else/#elif then the code
3630 # is not linear.
3631 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3632 $check = 0;
3633 }
3634
3635 # Ignore:
3636 # 1) blank lines, they should be at 0,
3637 # 2) preprocessor lines, and
3638 # 3) labels.
3639 if ($continuation ||
3640 $s =~ /^\s*?\n/ ||
3641 $s =~ /^\s*#\s*?/ ||
3642 $s =~ /^\s*$Ident\s*:/) {
3643 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3644 if ($s =~ s/^.*?\n//) {
3645 $cond_lines++;
3646 }
3647 }
3648 }
3649
3650 my (undef, $sindent) = line_stats("+" . $s);
3651 my $stat_real = raw_line($linenr, $cond_lines);
3652
3653 # Check if either of these lines are modified, else
3654 # this is not this patch's fault.
3655 if (!defined($stat_real) ||
3656 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3657 $check = 0;
3658 }
3659 if (defined($stat_real) && $cond_lines > 1) {
3660 $stat_real = "[...]\n$stat_real";
3661 }
3662
3663 #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";
3664
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003665 if ($check && $s ne '' &&
3666 (($sindent % 8) != 0 ||
3667 ($sindent < $indent) ||
3668 ($sindent == $indent &&
3669 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3670 ($sindent > $indent + 8))) {
Joe Hershberger05622192011-10-18 10:06:59 +00003671 WARN("SUSPECT_CODE_INDENT",
3672 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3673 }
3674 }
3675
3676 # Track the 'values' across context and added lines.
3677 my $opline = $line; $opline =~ s/^./ /;
3678 my ($curr_values, $curr_vars) =
3679 annotate_values($opline . "\n", $prev_values);
3680 $curr_values = $prev_values . $curr_values;
3681 if ($dbg_values) {
3682 my $outline = $opline; $outline =~ s/\t/ /g;
3683 print "$linenr > .$outline\n";
3684 print "$linenr > $curr_values\n";
3685 print "$linenr > $curr_vars\n";
3686 }
3687 $prev_values = substr($curr_values, -1);
3688
3689#ignore lines not being added
Tom Rini6b9709d2014-02-27 08:27:28 -05003690 next if ($line =~ /^[^\+]/);
Joe Hershberger05622192011-10-18 10:06:59 +00003691
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003692# check for dereferences that span multiple lines
3693 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3694 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3695 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3696 my $ref = $1;
3697 $line =~ /^.\s*($Lval)/;
3698 $ref .= $1;
3699 $ref =~ s/\s//g;
3700 WARN("MULTILINE_DEREFERENCE",
3701 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3702 }
3703
3704# check for declarations of signed or unsigned without int
3705 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3706 my $type = $1;
3707 my $var = $2;
3708 $var = "" if (!defined $var);
3709 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3710 my $sign = $1;
3711 my $pointer = $2;
3712
3713 $pointer = "" if (!defined $pointer);
3714
3715 if (WARN("UNSPECIFIED_INT",
3716 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3717 $fix) {
3718 my $decl = trim($sign) . " int ";
3719 my $comp_pointer = $pointer;
3720 $comp_pointer =~ s/\s//g;
3721 $decl .= $comp_pointer;
3722 $decl = rtrim($decl) if ($var eq "");
3723 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3724 }
3725 }
3726 }
3727
Joe Hershberger05622192011-10-18 10:06:59 +00003728# TEST: allow direct testing of the type matcher.
3729 if ($dbg_type) {
3730 if ($line =~ /^.\s*$Declare\s*$/) {
3731 ERROR("TEST_TYPE",
3732 "TEST: is type\n" . $herecurr);
3733 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3734 ERROR("TEST_NOT_TYPE",
3735 "TEST: is not type ($1 is)\n". $herecurr);
3736 }
3737 next;
3738 }
3739# TEST: allow direct testing of the attribute matcher.
3740 if ($dbg_attr) {
3741 if ($line =~ /^.\s*$Modifier\s*$/) {
3742 ERROR("TEST_ATTR",
3743 "TEST: is attr\n" . $herecurr);
3744 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3745 ERROR("TEST_NOT_ATTR",
3746 "TEST: is not attr ($1 is)\n". $herecurr);
3747 }
3748 next;
3749 }
3750
3751# check for initialisation to aggregates open brace on the next line
3752 if ($line =~ /^.\s*{/ &&
3753 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003754 if (ERROR("OPEN_BRACE",
3755 "that open brace { should be on the previous line\n" . $hereprev) &&
3756 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3757 fix_delete_line($fixlinenr - 1, $prevrawline);
3758 fix_delete_line($fixlinenr, $rawline);
3759 my $fixedline = $prevrawline;
3760 $fixedline =~ s/\s*=\s*$/ = {/;
3761 fix_insert_line($fixlinenr, $fixedline);
3762 $fixedline = $line;
3763 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3764 fix_insert_line($fixlinenr, $fixedline);
3765 }
Joe Hershberger05622192011-10-18 10:06:59 +00003766 }
3767
3768#
3769# Checks which are anchored on the added line.
3770#
3771
3772# check for malformed paths in #include statements (uses RAW line)
3773 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3774 my $path = $1;
3775 if ($path =~ m{//}) {
3776 ERROR("MALFORMED_INCLUDE",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003777 "malformed #include filename\n" . $herecurr);
3778 }
3779 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3780 ERROR("UAPI_INCLUDE",
3781 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00003782 }
3783 }
3784
3785# no C99 // comments
3786 if ($line =~ m{//}) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003787 if (ERROR("C99_COMMENTS",
3788 "do not use C99 // comments\n" . $herecurr) &&
3789 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003790 my $line = $fixed[$fixlinenr];
Tom Rini6b9709d2014-02-27 08:27:28 -05003791 if ($line =~ /\/\/(.*)$/) {
3792 my $comment = trim($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003793 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rini6b9709d2014-02-27 08:27:28 -05003794 }
3795 }
Joe Hershberger05622192011-10-18 10:06:59 +00003796 }
3797 # Remove C99 comments.
3798 $line =~ s@//.*@@;
3799 $opline =~ s@//.*@@;
3800
3801# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3802# the whole statement.
3803#print "APW <$lines[$realline_next - 1]>\n";
3804 if (defined $realline_next &&
3805 exists $lines[$realline_next - 1] &&
3806 !defined $suppress_export{$realline_next} &&
3807 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3808 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3809 # Handle definitions which produce identifiers with
3810 # a prefix:
3811 # XXX(foo);
3812 # EXPORT_SYMBOL(something_foo);
3813 my $name = $1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003814 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershberger05622192011-10-18 10:06:59 +00003815 $name =~ /^${Ident}_$2/) {
3816#print "FOO C name<$name>\n";
3817 $suppress_export{$realline_next} = 1;
3818
3819 } elsif ($stat !~ /(?:
3820 \n.}\s*$|
3821 ^.DEFINE_$Ident\(\Q$name\E\)|
3822 ^.DECLARE_$Ident\(\Q$name\E\)|
3823 ^.LIST_HEAD\(\Q$name\E\)|
3824 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3825 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3826 )/x) {
3827#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3828 $suppress_export{$realline_next} = 2;
3829 } else {
3830 $suppress_export{$realline_next} = 1;
3831 }
3832 }
3833 if (!defined $suppress_export{$linenr} &&
3834 $prevline =~ /^.\s*$/ &&
3835 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3836 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3837#print "FOO B <$lines[$linenr - 1]>\n";
3838 $suppress_export{$linenr} = 2;
3839 }
3840 if (defined $suppress_export{$linenr} &&
3841 $suppress_export{$linenr} == 2) {
3842 WARN("EXPORT_SYMBOL",
3843 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3844 }
3845
3846# check for global initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003847 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003848 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003849 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003850 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003851 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003852 }
Joe Hershberger05622192011-10-18 10:06:59 +00003853 }
3854# check for static initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003855 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003856 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003857 "do not initialise statics to $1\n" .
Tom Rini6b9709d2014-02-27 08:27:28 -05003858 $herecurr) &&
3859 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003860 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003861 }
Joe Hershberger05622192011-10-18 10:06:59 +00003862 }
3863
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003864# check for misordered declarations of char/short/int/long with signed/unsigned
3865 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3866 my $tmp = trim($1);
3867 WARN("MISORDERED_TYPE",
3868 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3869 }
3870
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003871# check for unnecessary <signed> int declarations of short/long/long long
3872 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3873 my $type = trim($1);
3874 next if ($type !~ /\bint\b/);
3875 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3876 my $new_type = $type;
3877 $new_type =~ s/\b\s*int\s*\b/ /;
3878 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3879 $new_type =~ s/^const\s+//;
3880 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3881 $new_type = "const $new_type" if ($type =~ /^const\b/);
3882 $new_type =~ s/\s+/ /g;
3883 $new_type = trim($new_type);
3884 if (WARN("UNNECESSARY_INT",
3885 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3886 $fix) {
3887 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3888 }
3889 }
3890
Joe Hershberger05622192011-10-18 10:06:59 +00003891# check for static const char * arrays.
3892 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3893 WARN("STATIC_CONST_CHAR_ARRAY",
3894 "static const char * array should probably be static const char * const\n" .
3895 $herecurr);
Simon Glass048a6482020-05-22 16:32:35 -06003896 }
Joe Hershberger05622192011-10-18 10:06:59 +00003897
3898# check for static char foo[] = "bar" declarations.
3899 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3900 WARN("STATIC_CONST_CHAR_ARRAY",
3901 "static char array declaration should probably be static const char\n" .
3902 $herecurr);
Simon Glass048a6482020-05-22 16:32:35 -06003903 }
Joe Hershberger05622192011-10-18 10:06:59 +00003904
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003905# check for const <foo> const where <foo> is not a pointer or array type
3906 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3907 my $found = $1;
3908 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3909 WARN("CONST_CONST",
3910 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3911 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3912 WARN("CONST_CONST",
3913 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3914 }
3915 }
3916
3917# check for non-global char *foo[] = {"bar", ...} declarations.
3918 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3919 WARN("STATIC_CONST_CHAR_ARRAY",
3920 "char * array declaration might be better as static const\n" .
3921 $herecurr);
3922 }
3923
3924# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3925 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3926 my $array = $1;
3927 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3928 my $array_div = $1;
3929 if (WARN("ARRAY_SIZE",
3930 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3931 $fix) {
3932 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3933 }
3934 }
3935 }
3936
Tom Rini6b9709d2014-02-27 08:27:28 -05003937# check for function declarations without arguments like "int foo()"
3938 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3939 if (ERROR("FUNCTION_WITHOUT_ARGS",
3940 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3941 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003942 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003943 }
Joe Hershberger05622192011-10-18 10:06:59 +00003944 }
3945
3946# check for new typedefs, only function parameters and sparse annotations
3947# make sense.
3948 if ($line =~ /\btypedef\s/ &&
3949 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3950 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3951 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003952 $line !~ /\b__bitwise\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003953 WARN("NEW_TYPEDEFS",
3954 "do not add new typedefs\n" . $herecurr);
3955 }
3956
3957# * goes on variable not on type
3958 # (char*[ const])
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003959 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3960 #print "AA<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003961 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershberger05622192011-10-18 10:06:59 +00003962
3963 # Should start with a space.
3964 $to =~ s/^(\S)/ $1/;
3965 # Should not end with a space.
3966 $to =~ s/\s+$//;
3967 # '*'s should not have spaces between.
3968 while ($to =~ s/\*\s+\*/\*\*/) {
3969 }
3970
Tom Rini6b9709d2014-02-27 08:27:28 -05003971## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003972 if ($from ne $to) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003973 if (ERROR("POINTER_LOCATION",
3974 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3975 $fix) {
3976 my $sub_from = $ident;
3977 my $sub_to = $ident;
3978 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003979 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003980 s@\Q$sub_from\E@$sub_to@;
3981 }
Joe Hershberger05622192011-10-18 10:06:59 +00003982 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003983 }
3984 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3985 #print "BB<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003986 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershberger05622192011-10-18 10:06:59 +00003987
3988 # Should start with a space.
3989 $to =~ s/^(\S)/ $1/;
3990 # Should not end with a space.
3991 $to =~ s/\s+$//;
3992 # '*'s should not have spaces between.
3993 while ($to =~ s/\*\s+\*/\*\*/) {
3994 }
3995 # Modifiers should have spaces.
3996 $to =~ s/(\b$Modifier$)/$1 /;
3997
Tom Rini6b9709d2014-02-27 08:27:28 -05003998## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003999 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004000 if (ERROR("POINTER_LOCATION",
4001 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4002 $fix) {
4003
4004 my $sub_from = $match;
4005 my $sub_to = $match;
4006 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004007 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004008 s@\Q$sub_from\E@$sub_to@;
4009 }
Joe Hershberger05622192011-10-18 10:06:59 +00004010 }
4011 }
4012
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004013# avoid BUG() or BUG_ON()
4014 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004015 my $msg_level = \&WARN;
4016 $msg_level = \&CHK if ($file);
4017 &{$msg_level}("AVOID_BUG",
4018 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004019 }
Joe Hershberger05622192011-10-18 10:06:59 +00004020
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004021# avoid LINUX_VERSION_CODE
Joe Hershberger05622192011-10-18 10:06:59 +00004022 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4023 WARN("LINUX_VERSION_CODE",
4024 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4025 }
4026
4027# check for uses of printk_ratelimit
4028 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4029 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004030 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004031 }
4032
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004033# printk should use KERN_* levels
4034 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4035 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4036 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004037 }
4038
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004039 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4040 my $orig = $1;
4041 my $level = lc($orig);
4042 $level = "warn" if ($level eq "warning");
4043 my $level2 = $level;
4044 $level2 = "dbg" if ($level eq "debug");
4045 WARN("PREFER_PR_LEVEL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004046 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004047 }
4048
4049 if ($line =~ /\bpr_warning\s*\(/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004050 if (WARN("PREFER_PR_LEVEL",
4051 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4052 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004053 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004054 s/\bpr_warning\b/pr_warn/;
4055 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004056 }
4057
4058 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4059 my $orig = $1;
4060 my $level = lc($orig);
4061 $level = "warn" if ($level eq "warning");
4062 $level = "dbg" if ($level eq "debug");
4063 WARN("PREFER_DEV_LEVEL",
4064 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4065 }
4066
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004067# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4068# number of false positives, but assembly files are not checked, so at
4069# least the arch entry code will not trigger this warning.
4070 if ($line =~ /\bENOSYS\b/) {
4071 WARN("ENOSYS",
4072 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4073 }
4074
Joe Hershberger05622192011-10-18 10:06:59 +00004075# function brace can't be on same line, except for #defines of do while,
4076# or if closed on same line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004077 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004078 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4079 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4080 $sline !~ /}/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004081 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004082 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004083 $fix) {
4084 fix_delete_line($fixlinenr, $rawline);
4085 my $fixed_line = $rawline;
4086 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4087 my $line1 = $1;
4088 my $line2 = $2;
4089 fix_insert_line($fixlinenr, ltrim($line1));
4090 fix_insert_line($fixlinenr, "\+{");
4091 if ($line2 !~ /^\s*$/) {
4092 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4093 }
4094 }
Joe Hershberger05622192011-10-18 10:06:59 +00004095 }
4096
4097# open braces for enum, union and struct go on the same line.
4098 if ($line =~ /^.\s*{/ &&
4099 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004100 if (ERROR("OPEN_BRACE",
4101 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4102 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4103 fix_delete_line($fixlinenr - 1, $prevrawline);
4104 fix_delete_line($fixlinenr, $rawline);
4105 my $fixedline = rtrim($prevrawline) . " {";
4106 fix_insert_line($fixlinenr, $fixedline);
4107 $fixedline = $rawline;
4108 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4109 if ($fixedline !~ /^\+\s*$/) {
4110 fix_insert_line($fixlinenr, $fixedline);
4111 }
4112 }
Joe Hershberger05622192011-10-18 10:06:59 +00004113 }
4114
4115# missing space after union, struct or enum definition
Tom Rini6b9709d2014-02-27 08:27:28 -05004116 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4117 if (WARN("SPACING",
4118 "missing space after $1 definition\n" . $herecurr) &&
4119 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004120 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004121 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4122 }
4123 }
4124
4125# Function pointer declarations
4126# check spacing between type, funcptr, and args
4127# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004128 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004129 my $declare = $1;
4130 my $pre_pointer_space = $2;
4131 my $post_pointer_space = $3;
4132 my $funcname = $4;
4133 my $post_funcname_space = $5;
4134 my $pre_args_space = $6;
4135
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004136# the $Declare variable will capture all spaces after the type
4137# so check it for a missing trailing missing space but pointer return types
4138# don't need a space so don't warn for those.
4139 my $post_declare_space = "";
4140 if ($declare =~ /(\s+)$/) {
4141 $post_declare_space = $1;
4142 $declare = rtrim($declare);
4143 }
4144 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004145 WARN("SPACING",
4146 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004147 $post_declare_space = " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004148 }
4149
4150# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004151# This test is not currently implemented because these declarations are
4152# equivalent to
4153# int foo(int bar, ...)
4154# and this is form shouldn't/doesn't generate a checkpatch warning.
4155#
4156# elsif ($declare =~ /\s{2,}$/) {
4157# WARN("SPACING",
4158# "Multiple spaces after return type\n" . $herecurr);
4159# }
Tom Rini6b9709d2014-02-27 08:27:28 -05004160
4161# unnecessary space "type ( *funcptr)(args...)"
4162 if (defined $pre_pointer_space &&
4163 $pre_pointer_space =~ /^\s/) {
4164 WARN("SPACING",
4165 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4166 }
4167
4168# unnecessary space "type (* funcptr)(args...)"
4169 if (defined $post_pointer_space &&
4170 $post_pointer_space =~ /^\s/) {
4171 WARN("SPACING",
4172 "Unnecessary space before function pointer name\n" . $herecurr);
4173 }
4174
4175# unnecessary space "type (*funcptr )(args...)"
4176 if (defined $post_funcname_space &&
4177 $post_funcname_space =~ /^\s/) {
4178 WARN("SPACING",
4179 "Unnecessary space after function pointer name\n" . $herecurr);
4180 }
4181
4182# unnecessary space "type (*funcptr) (args...)"
4183 if (defined $pre_args_space &&
4184 $pre_args_space =~ /^\s/) {
4185 WARN("SPACING",
4186 "Unnecessary space before function pointer arguments\n" . $herecurr);
4187 }
4188
4189 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004190 $fixed[$fixlinenr] =~
4191 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05004192 }
Joe Hershberger05622192011-10-18 10:06:59 +00004193 }
4194
4195# check for spacing round square brackets; allowed:
4196# 1. with a type on the left -- int [] a;
4197# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4198# 3. inside a curly brace -- = { [0...10] = 5 }
4199 while ($line =~ /(.*?\s)\[/g) {
4200 my ($where, $prefix) = ($-[1], $1);
4201 if ($prefix !~ /$Type\s+$/ &&
4202 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004203 $prefix !~ /[{,:]\s+$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004204 if (ERROR("BRACKET_SPACE",
4205 "space prohibited before open square bracket '['\n" . $herecurr) &&
4206 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004207 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004208 s/^(\+.*?)\s+\[/$1\[/;
4209 }
Joe Hershberger05622192011-10-18 10:06:59 +00004210 }
4211 }
4212
4213# check for spaces between functions and their parentheses.
4214 while ($line =~ /($Ident)\s+\(/g) {
4215 my $name = $1;
4216 my $ctx_before = substr($line, 0, $-[1]);
4217 my $ctx = "$ctx_before$name";
4218
4219 # Ignore those directives where spaces _are_ permitted.
4220 if ($name =~ /^(?:
4221 if|for|while|switch|return|case|
4222 volatile|__volatile__|
4223 __attribute__|format|__extension__|
4224 asm|__asm__)$/x)
4225 {
Joe Hershberger05622192011-10-18 10:06:59 +00004226 # cpp #define statements have non-optional spaces, ie
4227 # if there is a space between the name and the open
4228 # parenthesis it is simply not a parameter group.
4229 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4230
4231 # cpp #elif statement condition may start with a (
4232 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4233
4234 # If this whole things ends with a type its most
4235 # likely a typedef for a function.
4236 } elsif ($ctx =~ /$Type$/) {
4237
4238 } else {
Tom Rini6b9709d2014-02-27 08:27:28 -05004239 if (WARN("SPACING",
4240 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4241 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004242 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004243 s/\b$name\s+\(/$name\(/;
4244 }
Joe Hershberger05622192011-10-18 10:06:59 +00004245 }
4246 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004247
Joe Hershberger05622192011-10-18 10:06:59 +00004248# Check operator spacing.
4249 if (!($line=~/\#\s*include/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004250 my $fixed_line = "";
4251 my $line_fixed = 0;
4252
Joe Hershberger05622192011-10-18 10:06:59 +00004253 my $ops = qr{
4254 <<=|>>=|<=|>=|==|!=|
4255 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4256 =>|->|<<|>>|<|>|=|!|~|
4257 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rini6b9709d2014-02-27 08:27:28 -05004258 \?:|\?|:
Joe Hershberger05622192011-10-18 10:06:59 +00004259 }x;
4260 my @elements = split(/($ops|;)/, $opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004261
4262## print("element count: <" . $#elements . ">\n");
4263## foreach my $el (@elements) {
4264## print("el: <$el>\n");
4265## }
4266
4267 my @fix_elements = ();
Joe Hershberger05622192011-10-18 10:06:59 +00004268 my $off = 0;
4269
Tom Rini6b9709d2014-02-27 08:27:28 -05004270 foreach my $el (@elements) {
4271 push(@fix_elements, substr($rawline, $off, length($el)));
4272 $off += length($el);
4273 }
4274
4275 $off = 0;
4276
Joe Hershberger05622192011-10-18 10:06:59 +00004277 my $blank = copy_spacing($opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004278 my $last_after = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00004279
4280 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004281
4282 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4283
4284## print("n: <$n> good: <$good>\n");
4285
Joe Hershberger05622192011-10-18 10:06:59 +00004286 $off += length($elements[$n]);
4287
4288 # Pick up the preceding and succeeding characters.
4289 my $ca = substr($opline, 0, $off);
4290 my $cc = '';
4291 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4292 $cc = substr($opline, $off + length($elements[$n + 1]));
4293 }
4294 my $cb = "$ca$;$cc";
4295
4296 my $a = '';
4297 $a = 'V' if ($elements[$n] ne '');
4298 $a = 'W' if ($elements[$n] =~ /\s$/);
4299 $a = 'C' if ($elements[$n] =~ /$;$/);
4300 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4301 $a = 'O' if ($elements[$n] eq '');
4302 $a = 'E' if ($ca =~ /^\s*$/);
4303
4304 my $op = $elements[$n + 1];
4305
4306 my $c = '';
4307 if (defined $elements[$n + 2]) {
4308 $c = 'V' if ($elements[$n + 2] ne '');
4309 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4310 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4311 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4312 $c = 'O' if ($elements[$n + 2] eq '');
4313 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4314 } else {
4315 $c = 'E';
4316 }
4317
4318 my $ctx = "${a}x${c}";
4319
4320 my $at = "(ctx:$ctx)";
4321
4322 my $ptr = substr($blank, 0, $off) . "^";
4323 my $hereptr = "$hereline$ptr\n";
4324
4325 # Pull out the value of this operator.
4326 my $op_type = substr($curr_values, $off + 1, 1);
4327
4328 # Get the full operator variant.
4329 my $opv = $op . substr($curr_vars, $off, 1);
4330
4331 # Ignore operators passed as parameters.
4332 if ($op_type ne 'V' &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004333 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004334
4335# # Ignore comments
4336# } elsif ($op =~ /^$;+$/) {
4337
4338 # ; should have either the end of line or a space or \ after it
4339 } elsif ($op eq ';') {
4340 if ($ctx !~ /.x[WEBC]/ &&
4341 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004342 if (ERROR("SPACING",
4343 "space required after that '$op' $at\n" . $hereptr)) {
4344 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4345 $line_fixed = 1;
4346 }
Joe Hershberger05622192011-10-18 10:06:59 +00004347 }
4348
4349 # // is a comment
4350 } elsif ($op eq '//') {
4351
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004352 # : when part of a bitfield
4353 } elsif ($opv eq ':B') {
4354 # skip the bitfield test for now
4355
Joe Hershberger05622192011-10-18 10:06:59 +00004356 # No spaces for:
4357 # ->
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004358 } elsif ($op eq '->') {
Joe Hershberger05622192011-10-18 10:06:59 +00004359 if ($ctx =~ /Wx.|.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004360 if (ERROR("SPACING",
4361 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4362 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4363 if (defined $fix_elements[$n + 2]) {
4364 $fix_elements[$n + 2] =~ s/^\s+//;
4365 }
4366 $line_fixed = 1;
4367 }
Joe Hershberger05622192011-10-18 10:06:59 +00004368 }
4369
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004370 # , must not have a space before and must have a space on the right.
Joe Hershberger05622192011-10-18 10:06:59 +00004371 } elsif ($op eq ',') {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004372 my $rtrim_before = 0;
4373 my $space_after = 0;
4374 if ($ctx =~ /Wx./) {
4375 if (ERROR("SPACING",
4376 "space prohibited before that '$op' $at\n" . $hereptr)) {
4377 $line_fixed = 1;
4378 $rtrim_before = 1;
4379 }
4380 }
Joe Hershberger05622192011-10-18 10:06:59 +00004381 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004382 if (ERROR("SPACING",
4383 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004384 $line_fixed = 1;
4385 $last_after = $n;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004386 $space_after = 1;
4387 }
4388 }
4389 if ($rtrim_before || $space_after) {
4390 if ($rtrim_before) {
4391 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4392 } else {
4393 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4394 }
4395 if ($space_after) {
4396 $good .= " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004397 }
Joe Hershberger05622192011-10-18 10:06:59 +00004398 }
4399
4400 # '*' as part of a type definition -- reported already.
4401 } elsif ($opv eq '*_') {
4402 #warn "'*' is part of type\n";
4403
4404 # unary operators should have a space before and
4405 # none after. May be left adjacent to another
4406 # unary operator, or a cast
4407 } elsif ($op eq '!' || $op eq '~' ||
4408 $opv eq '*U' || $opv eq '-U' ||
4409 $opv eq '&U' || $opv eq '&&U') {
4410 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004411 if (ERROR("SPACING",
4412 "space required before that '$op' $at\n" . $hereptr)) {
4413 if ($n != $last_after + 2) {
4414 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4415 $line_fixed = 1;
4416 }
4417 }
Joe Hershberger05622192011-10-18 10:06:59 +00004418 }
4419 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4420 # A unary '*' may be const
4421
4422 } elsif ($ctx =~ /.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004423 if (ERROR("SPACING",
4424 "space prohibited after that '$op' $at\n" . $hereptr)) {
4425 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4426 if (defined $fix_elements[$n + 2]) {
4427 $fix_elements[$n + 2] =~ s/^\s+//;
4428 }
4429 $line_fixed = 1;
4430 }
Joe Hershberger05622192011-10-18 10:06:59 +00004431 }
4432
4433 # unary ++ and unary -- are allowed no space on one side.
4434 } elsif ($op eq '++' or $op eq '--') {
4435 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004436 if (ERROR("SPACING",
4437 "space required one side of that '$op' $at\n" . $hereptr)) {
4438 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4439 $line_fixed = 1;
4440 }
Joe Hershberger05622192011-10-18 10:06:59 +00004441 }
4442 if ($ctx =~ /Wx[BE]/ ||
4443 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004444 if (ERROR("SPACING",
4445 "space prohibited before that '$op' $at\n" . $hereptr)) {
4446 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4447 $line_fixed = 1;
4448 }
Joe Hershberger05622192011-10-18 10:06:59 +00004449 }
4450 if ($ctx =~ /ExW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004451 if (ERROR("SPACING",
4452 "space prohibited after that '$op' $at\n" . $hereptr)) {
4453 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4454 if (defined $fix_elements[$n + 2]) {
4455 $fix_elements[$n + 2] =~ s/^\s+//;
4456 }
4457 $line_fixed = 1;
4458 }
Joe Hershberger05622192011-10-18 10:06:59 +00004459 }
4460
Joe Hershberger05622192011-10-18 10:06:59 +00004461 # << and >> may either have or not have spaces both sides
4462 } elsif ($op eq '<<' or $op eq '>>' or
4463 $op eq '&' or $op eq '^' or $op eq '|' or
4464 $op eq '+' or $op eq '-' or
4465 $op eq '*' or $op eq '/' or
4466 $op eq '%')
4467 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004468 if ($check) {
4469 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4470 if (CHK("SPACING",
4471 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4472 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4473 $fix_elements[$n + 2] =~ s/^\s+//;
4474 $line_fixed = 1;
4475 }
4476 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4477 if (CHK("SPACING",
4478 "space preferred before that '$op' $at\n" . $hereptr)) {
4479 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4480 $line_fixed = 1;
4481 }
4482 }
4483 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004484 if (ERROR("SPACING",
4485 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4486 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4487 if (defined $fix_elements[$n + 2]) {
4488 $fix_elements[$n + 2] =~ s/^\s+//;
4489 }
4490 $line_fixed = 1;
4491 }
Joe Hershberger05622192011-10-18 10:06:59 +00004492 }
4493
4494 # A colon needs no spaces before when it is
4495 # terminating a case value or a label.
4496 } elsif ($opv eq ':C' || $opv eq ':L') {
4497 if ($ctx =~ /Wx./) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004498 if (ERROR("SPACING",
4499 "space prohibited before that '$op' $at\n" . $hereptr)) {
4500 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4501 $line_fixed = 1;
4502 }
Joe Hershberger05622192011-10-18 10:06:59 +00004503 }
4504
4505 # All the others need spaces both sides.
4506 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4507 my $ok = 0;
4508
4509 # Ignore email addresses <foo@bar>
4510 if (($op eq '<' &&
4511 $cc =~ /^\S+\@\S+>/) ||
4512 ($op eq '>' &&
4513 $ca =~ /<\S+\@\S+$/))
4514 {
Tom Rini6b9709d2014-02-27 08:27:28 -05004515 $ok = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00004516 }
4517
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004518 # for asm volatile statements
4519 # ignore a colon with another
4520 # colon immediately before or after
4521 if (($op eq ':') &&
4522 ($ca =~ /:$/ || $cc =~ /^:/)) {
4523 $ok = 1;
4524 }
4525
Tom Rini6b9709d2014-02-27 08:27:28 -05004526 # messages are ERROR, but ?: are CHK
Joe Hershberger05622192011-10-18 10:06:59 +00004527 if ($ok == 0) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004528 my $msg_level = \&ERROR;
4529 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rini6b9709d2014-02-27 08:27:28 -05004530
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004531 if (&{$msg_level}("SPACING",
4532 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004533 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4534 if (defined $fix_elements[$n + 2]) {
4535 $fix_elements[$n + 2] =~ s/^\s+//;
4536 }
4537 $line_fixed = 1;
4538 }
Joe Hershberger05622192011-10-18 10:06:59 +00004539 }
4540 }
4541 $off += length($elements[$n + 1]);
Tom Rini6b9709d2014-02-27 08:27:28 -05004542
4543## print("n: <$n> GOOD: <$good>\n");
4544
4545 $fixed_line = $fixed_line . $good;
4546 }
4547
4548 if (($#elements % 2) == 0) {
4549 $fixed_line = $fixed_line . $fix_elements[$#elements];
4550 }
4551
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004552 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4553 $fixed[$fixlinenr] = $fixed_line;
Tom Rini6b9709d2014-02-27 08:27:28 -05004554 }
4555
4556
4557 }
4558
4559# check for whitespace before a non-naked semicolon
4560 if ($line =~ /^\+.*\S\s+;\s*$/) {
4561 if (WARN("SPACING",
4562 "space prohibited before semicolon\n" . $herecurr) &&
4563 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004564 1 while $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004565 s/^(\+.*\S)\s+;/$1;/;
Joe Hershberger05622192011-10-18 10:06:59 +00004566 }
4567 }
4568
4569# check for multiple assignments
4570 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4571 CHK("MULTIPLE_ASSIGNMENTS",
4572 "multiple assignments should be avoided\n" . $herecurr);
4573 }
4574
4575## # check for multiple declarations, allowing for a function declaration
4576## # continuation.
4577## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4578## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4579##
4580## # Remove any bracketed sections to ensure we do not
4581## # falsly report the parameters of functions.
4582## my $ln = $line;
4583## while ($ln =~ s/\([^\(\)]*\)//g) {
4584## }
4585## if ($ln =~ /,/) {
4586## WARN("MULTIPLE_DECLARATION",
4587## "declaring multiple variables together should be avoided\n" . $herecurr);
4588## }
4589## }
4590
4591#need space before brace following if, while, etc
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004592 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004593 $line =~ /\b(?:else|do)\{/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004594 if (ERROR("SPACING",
4595 "space required before the open brace '{'\n" . $herecurr) &&
4596 $fix) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004597 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004598 }
Joe Hershberger05622192011-10-18 10:06:59 +00004599 }
4600
Tom Rini6b9709d2014-02-27 08:27:28 -05004601## # check for blank lines before declarations
4602## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4603## $prevrawline =~ /^.\s*$/) {
4604## WARN("SPACING",
4605## "No blank lines before declarations\n" . $hereprev);
4606## }
4607##
4608
Joe Hershberger05622192011-10-18 10:06:59 +00004609# closing brace should have a space following it when it has anything
4610# on the line
Simon Glass048a6482020-05-22 16:32:35 -06004611 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004612 if (ERROR("SPACING",
4613 "space required after that close brace '}'\n" . $herecurr) &&
4614 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004615 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004616 s/}((?!(?:,|;|\)))\S)/} $1/;
4617 }
Joe Hershberger05622192011-10-18 10:06:59 +00004618 }
4619
4620# check spacing on square brackets
4621 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004622 if (ERROR("SPACING",
4623 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4624 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004625 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004626 s/\[\s+/\[/;
4627 }
Joe Hershberger05622192011-10-18 10:06:59 +00004628 }
4629 if ($line =~ /\s\]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004630 if (ERROR("SPACING",
4631 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4632 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004633 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004634 s/\s+\]/\]/;
4635 }
Joe Hershberger05622192011-10-18 10:06:59 +00004636 }
4637
4638# check spacing on parentheses
4639 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4640 $line !~ /for\s*\(\s+;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004641 if (ERROR("SPACING",
4642 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4643 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004644 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004645 s/\(\s+/\(/;
4646 }
Joe Hershberger05622192011-10-18 10:06:59 +00004647 }
4648 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4649 $line !~ /for\s*\(.*;\s+\)/ &&
4650 $line !~ /:\s+\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004651 if (ERROR("SPACING",
4652 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4653 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004654 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004655 s/\s+\)/\)/;
4656 }
Joe Hershberger05622192011-10-18 10:06:59 +00004657 }
4658
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004659# check unnecessary parentheses around addressof/dereference single $Lvals
4660# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4661
4662 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4663 my $var = $1;
4664 if (CHK("UNNECESSARY_PARENTHESES",
4665 "Unnecessary parentheses around $var\n" . $herecurr) &&
4666 $fix) {
4667 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4668 }
4669 }
4670
4671# check for unnecessary parentheses around function pointer uses
4672# ie: (foo->bar)(); should be foo->bar();
4673# but not "if (foo->bar) (" to avoid some false positives
4674 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4675 my $var = $2;
4676 if (CHK("UNNECESSARY_PARENTHESES",
4677 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4678 $fix) {
4679 my $var2 = deparenthesize($var);
4680 $var2 =~ s/\s//g;
4681 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4682 }
4683 }
4684
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004685# check for unnecessary parentheses around comparisons in if uses
4686# when !drivers/staging or command-line uses --strict
4687 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004688 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004689 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4690 my $if_stat = $1;
4691 my $test = substr($2, 1, -1);
4692 my $herectx;
4693 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4694 my $match = $1;
4695 # avoid parentheses around potential macro args
4696 next if ($match =~ /^\s*\w+\s*$/);
4697 if (!defined($herectx)) {
4698 $herectx = $here . "\n";
4699 my $cnt = statement_rawlines($if_stat);
4700 for (my $n = 0; $n < $cnt; $n++) {
4701 my $rl = raw_line($linenr, $n);
4702 $herectx .= $rl . "\n";
4703 last if $rl =~ /^[ \+].*\{/;
4704 }
4705 }
4706 CHK("UNNECESSARY_PARENTHESES",
4707 "Unnecessary parentheses around '$match'\n" . $herectx);
4708 }
4709 }
4710
Joe Hershberger05622192011-10-18 10:06:59 +00004711#goto labels aren't indented, allow a single space however
4712 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4713 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004714 if (WARN("INDENTED_LABEL",
4715 "labels should not be indented\n" . $herecurr) &&
4716 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004717 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004718 s/^(.)\s+/$1/;
4719 }
Joe Hershberger05622192011-10-18 10:06:59 +00004720 }
4721
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004722# return is not a function
Tom Rini6b9709d2014-02-27 08:27:28 -05004723 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershberger05622192011-10-18 10:06:59 +00004724 my $spacing = $1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004725 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004726 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4727 my $value = $1;
4728 $value = deparenthesize($value);
4729 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4730 ERROR("RETURN_PARENTHESES",
4731 "return is not a function, parentheses are not required\n" . $herecurr);
4732 }
Joe Hershberger05622192011-10-18 10:06:59 +00004733 } elsif ($spacing !~ /\s+/) {
4734 ERROR("SPACING",
4735 "space required before the open parenthesis '('\n" . $herecurr);
4736 }
4737 }
Tom Rini6b9709d2014-02-27 08:27:28 -05004738
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004739# unnecessary return in a void function
4740# at end-of-function, with the previous line a single leading tab, then return;
4741# and the line before that not a goto label target like "out:"
4742 if ($sline =~ /^[ \+]}\s*$/ &&
4743 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4744 $linenr >= 3 &&
4745 $lines[$linenr - 3] =~ /^[ +]/ &&
4746 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4747 WARN("RETURN_VOID",
4748 "void function return statements are not generally useful\n" . $hereprev);
4749 }
4750
Tom Rini6b9709d2014-02-27 08:27:28 -05004751# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004752 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004753 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4754 my $openparens = $1;
4755 my $count = $openparens =~ tr@\(@\(@;
4756 my $msg = "";
4757 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4758 my $comp = $4; #Not $1 because of $LvalOrFunc
4759 $msg = " - maybe == should be = ?" if ($comp eq "==");
4760 WARN("UNNECESSARY_PARENTHESES",
4761 "Unnecessary parentheses$msg\n" . $herecurr);
4762 }
4763 }
4764
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004765# comparisons with a constant or upper case identifier on the left
4766# avoid cases like "foo + BAR < baz"
4767# only fix matches surrounded by parentheses to avoid incorrect
4768# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004769 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004770 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4771 my $lead = $1;
4772 my $const = $2;
4773 my $comp = $3;
4774 my $to = $4;
4775 my $newcomp = $comp;
4776 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4777 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4778 WARN("CONSTANT_COMPARISON",
4779 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4780 $fix) {
4781 if ($comp eq "<") {
4782 $newcomp = ">";
4783 } elsif ($comp eq "<=") {
4784 $newcomp = ">=";
4785 } elsif ($comp eq ">") {
4786 $newcomp = "<";
4787 } elsif ($comp eq ">=") {
4788 $newcomp = "<=";
4789 }
4790 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4791 }
4792 }
4793
4794# Return of what appears to be an errno should normally be negative
4795 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004796 my $name = $1;
4797 if ($name ne 'EOF' && $name ne 'ERROR') {
4798 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004799 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004800 }
4801 }
4802
Joe Hershberger05622192011-10-18 10:06:59 +00004803# Need a space before open parenthesis after if, while etc
Tom Rini6b9709d2014-02-27 08:27:28 -05004804 if ($line =~ /\b(if|while|for|switch)\(/) {
4805 if (ERROR("SPACING",
4806 "space required before the open parenthesis '('\n" . $herecurr) &&
4807 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004808 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004809 s/\b(if|while|for|switch)\(/$1 \(/;
4810 }
Joe Hershberger05622192011-10-18 10:06:59 +00004811 }
4812
4813# Check for illegal assignment in if conditional -- and check for trailing
4814# statements after the conditional.
4815 if ($line =~ /do\s*(?!{)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004816 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4817 ctx_statement_block($linenr, $realcnt, 0)
4818 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00004819 my ($stat_next) = ctx_statement_block($line_nr_next,
4820 $remain_next, $off_next);
4821 $stat_next =~ s/\n./\n /g;
4822 ##print "stat<$stat> stat_next<$stat_next>\n";
4823
4824 if ($stat_next =~ /^\s*while\b/) {
4825 # If the statement carries leading newlines,
4826 # then count those as offsets.
4827 my ($whitespace) =
4828 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4829 my $offset =
4830 statement_rawlines($whitespace) - 1;
4831
4832 $suppress_whiletrailers{$line_nr_next +
4833 $offset} = 1;
4834 }
4835 }
4836 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004837 defined($stat) && defined($cond) &&
Joe Hershberger05622192011-10-18 10:06:59 +00004838 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4839 my ($s, $c) = ($stat, $cond);
4840
4841 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4842 ERROR("ASSIGN_IN_IF",
4843 "do not use assignment in if condition\n" . $herecurr);
4844 }
4845
4846 # Find out what is on the end of the line after the
4847 # conditional.
4848 substr($s, 0, length($c), '');
4849 $s =~ s/\n.*//g;
4850 $s =~ s/$;//g; # Remove any comments
4851 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4852 $c !~ /}\s*while\s*/)
4853 {
4854 # Find out how long the conditional actually is.
4855 my @newlines = ($c =~ /\n/gs);
4856 my $cond_lines = 1 + $#newlines;
4857 my $stat_real = '';
4858
4859 $stat_real = raw_line($linenr, $cond_lines)
4860 . "\n" if ($cond_lines);
4861 if (defined($stat_real) && $cond_lines > 1) {
4862 $stat_real = "[...]\n$stat_real";
4863 }
4864
4865 ERROR("TRAILING_STATEMENTS",
4866 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4867 }
4868 }
4869
4870# Check for bitwise tests written as boolean
4871 if ($line =~ /
4872 (?:
4873 (?:\[|\(|\&\&|\|\|)
4874 \s*0[xX][0-9]+\s*
4875 (?:\&\&|\|\|)
4876 |
4877 (?:\&\&|\|\|)
4878 \s*0[xX][0-9]+\s*
4879 (?:\&\&|\|\||\)|\])
4880 )/x)
4881 {
4882 WARN("HEXADECIMAL_BOOLEAN_TEST",
4883 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4884 }
4885
4886# if and else should not have general statements after it
4887 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4888 my $s = $1;
4889 $s =~ s/$;//g; # Remove any comments
4890 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4891 ERROR("TRAILING_STATEMENTS",
4892 "trailing statements should be on next line\n" . $herecurr);
4893 }
4894 }
4895# if should not continue a brace
4896 if ($line =~ /}\s*if\b/) {
4897 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004898 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershberger05622192011-10-18 10:06:59 +00004899 $herecurr);
4900 }
4901# case and default should not have general statements after them
4902 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4903 $line !~ /\G(?:
4904 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4905 \s*return\s+
4906 )/xg)
4907 {
4908 ERROR("TRAILING_STATEMENTS",
4909 "trailing statements should be on next line\n" . $herecurr);
4910 }
4911
4912 # Check for }<nl>else {, these must be at the same
4913 # indent level to be relevant to each other.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004914 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4915 $previndent == $indent) {
4916 if (ERROR("ELSE_AFTER_BRACE",
4917 "else should follow close brace '}'\n" . $hereprev) &&
4918 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4919 fix_delete_line($fixlinenr - 1, $prevrawline);
4920 fix_delete_line($fixlinenr, $rawline);
4921 my $fixedline = $prevrawline;
4922 $fixedline =~ s/}\s*$//;
4923 if ($fixedline !~ /^\+\s*$/) {
4924 fix_insert_line($fixlinenr, $fixedline);
4925 }
4926 $fixedline = $rawline;
4927 $fixedline =~ s/^(.\s*)else/$1} else/;
4928 fix_insert_line($fixlinenr, $fixedline);
4929 }
Joe Hershberger05622192011-10-18 10:06:59 +00004930 }
4931
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004932 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4933 $previndent == $indent) {
Joe Hershberger05622192011-10-18 10:06:59 +00004934 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4935
4936 # Find out what is on the end of the line after the
4937 # conditional.
4938 substr($s, 0, length($c), '');
4939 $s =~ s/\n.*//g;
4940
4941 if ($s =~ /^\s*;/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004942 if (ERROR("WHILE_AFTER_BRACE",
4943 "while should follow close brace '}'\n" . $hereprev) &&
4944 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4945 fix_delete_line($fixlinenr - 1, $prevrawline);
4946 fix_delete_line($fixlinenr, $rawline);
4947 my $fixedline = $prevrawline;
4948 my $trailing = $rawline;
4949 $trailing =~ s/^\+//;
4950 $trailing = trim($trailing);
4951 $fixedline =~ s/}\s*$/} $trailing/;
4952 fix_insert_line($fixlinenr, $fixedline);
4953 }
Joe Hershberger05622192011-10-18 10:06:59 +00004954 }
4955 }
4956
Tom Rini6b9709d2014-02-27 08:27:28 -05004957#Specific variable tests
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004958 while ($line =~ m{($Constant|$Lval)}g) {
4959 my $var = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05004960
Simon Glass048a6482020-05-22 16:32:35 -06004961#gcc binary extension
4962 if ($var =~ /^$Binary$/) {
4963 if (WARN("GCC_BINARY_CONSTANT",
4964 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4965 $fix) {
4966 my $hexval = sprintf("0x%x", oct($var));
4967 $fixed[$fixlinenr] =~
4968 s/\b$var\b/$hexval/;
4969 }
4970 }
4971
Tom Rini6b9709d2014-02-27 08:27:28 -05004972#CamelCase
4973 if ($var !~ /^$Constant$/ &&
4974 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4975#Ignore Page<foo> variants
4976 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4977#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004978 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4979#Ignore some three character SI units explicitly, like MiB and KHz
4980 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004981 while ($var =~ m{($Ident)}g) {
4982 my $word = $1;
4983 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4984 if ($check) {
4985 seed_camelcase_includes();
4986 if (!$file && !$camelcase_file_seeded) {
4987 seed_camelcase_file($realfile);
4988 $camelcase_file_seeded = 1;
4989 }
4990 }
4991 if (!defined $camelcase{$word}) {
4992 $camelcase{$word} = 1;
4993 CHK("CAMELCASE",
4994 "Avoid CamelCase: <$word>\n" . $herecurr);
4995 }
4996 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004997 }
4998 }
Joe Hershberger05622192011-10-18 10:06:59 +00004999
5000#no spaces allowed after \ in define
Tom Rini6b9709d2014-02-27 08:27:28 -05005001 if ($line =~ /\#\s*define.*\\\s+$/) {
5002 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5003 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5004 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005005 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05005006 }
Joe Hershberger05622192011-10-18 10:06:59 +00005007 }
5008
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005009# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5010# itself <asm/foo.h> (uses RAW line)
Joe Hershberger05622192011-10-18 10:06:59 +00005011 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5012 my $file = "$1.h";
5013 my $checkfile = "include/linux/$file";
5014 if (-f "$root/$checkfile" &&
5015 $realfile ne $checkfile &&
5016 $1 !~ /$allowed_asm_includes/)
5017 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005018 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5019 if ($asminclude > 0) {
5020 if ($realfile =~ m{^arch/}) {
5021 CHK("ARCH_INCLUDE_LINUX",
5022 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5023 } else {
5024 WARN("INCLUDE_LINUX",
5025 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5026 }
Joe Hershberger05622192011-10-18 10:06:59 +00005027 }
5028 }
5029 }
5030
5031# multi-statement macros should be enclosed in a do while loop, grab the
5032# first statement and ensure its the whole macro if its not enclosed
5033# in a known good container
5034 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5035 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5036 my $ln = $linenr;
5037 my $cnt = $realcnt;
5038 my ($off, $dstat, $dcond, $rest);
5039 my $ctx = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005040 my $has_flow_statement = 0;
5041 my $has_arg_concat = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005042 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005043 ctx_statement_block($linenr, $realcnt, 0);
5044 $ctx = $dstat;
Joe Hershberger05622192011-10-18 10:06:59 +00005045 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5046 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5047
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005048 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5049 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5050
5051 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5052 my $define_args = $1;
5053 my $define_stmt = $dstat;
5054 my @def_args = ();
5055
5056 if (defined $define_args && $define_args ne "") {
5057 $define_args = substr($define_args, 1, length($define_args) - 2);
5058 $define_args =~ s/\s*//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005059 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005060 @def_args = split(",", $define_args);
5061 }
5062
Joe Hershberger05622192011-10-18 10:06:59 +00005063 $dstat =~ s/$;//g;
5064 $dstat =~ s/\\\n.//g;
5065 $dstat =~ s/^\s*//s;
5066 $dstat =~ s/\s*$//s;
5067
5068 # Flatten any parentheses and braces
5069 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5070 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005071 $dstat =~ s/.\[[^\[\]]*\]/1/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005072 {
5073 }
5074
5075 # Flatten any obvious string concatentation.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005076 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5077 $dstat =~ s/$Ident\s*($String)/$1/)
Joe Hershberger05622192011-10-18 10:06:59 +00005078 {
5079 }
5080
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005081 # Make asm volatile uses seem like a generic function
5082 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5083
Joe Hershberger05622192011-10-18 10:06:59 +00005084 my $exceptions = qr{
5085 $Declare|
5086 module_param_named|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005087 MODULE_PARM_DESC|
Joe Hershberger05622192011-10-18 10:06:59 +00005088 DECLARE_PER_CPU|
5089 DEFINE_PER_CPU|
5090 __typeof__\(|
5091 union|
5092 struct|
5093 \.$Ident\s*=\s*|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005094 ^\"|\"$|
5095 ^\[
Joe Hershberger05622192011-10-18 10:06:59 +00005096 }x;
5097 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005098
5099 $ctx =~ s/\n*$//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005100 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005101 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005102
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005103 if ($dstat ne '' &&
5104 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5105 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rini6b9709d2014-02-27 08:27:28 -05005106 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005107 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005108 $dstat !~ /$exceptions/ &&
5109 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rini6b9709d2014-02-27 08:27:28 -05005110 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005111 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5112 $dstat !~ /^for\s*$Constant$/ && # for (...)
5113 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5114 $dstat !~ /^do\s*{/ && # do {...
Heiko Schocherd8a1a302016-01-04 09:17:19 +01005115 $dstat !~ /^\(\{/ && # ({...
Tom Rini6b9709d2014-02-27 08:27:28 -05005116 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005117 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005118 if ($dstat =~ /^\s*if\b/) {
5119 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5120 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5121 } elsif ($dstat =~ /;/) {
5122 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5123 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5124 } else {
5125 ERROR("COMPLEX_MACRO",
5126 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5127 }
5128
5129 }
5130
5131 # Make $define_stmt single line, comment-free, etc
5132 my @stmt_array = split('\n', $define_stmt);
5133 my $first = 1;
5134 $define_stmt = "";
5135 foreach my $l (@stmt_array) {
5136 $l =~ s/\\$//;
5137 if ($first) {
5138 $define_stmt = $l;
5139 $first = 0;
5140 } elsif ($l =~ /^[\+ ]/) {
5141 $define_stmt .= substr($l, 1);
5142 }
5143 }
5144 $define_stmt =~ s/$;//g;
5145 $define_stmt =~ s/\s+/ /g;
5146 $define_stmt = trim($define_stmt);
5147
5148# check if any macro arguments are reused (ignore '...' and 'type')
5149 foreach my $arg (@def_args) {
5150 next if ($arg =~ /\.\.\./);
5151 next if ($arg =~ /^type$/i);
5152 my $tmp_stmt = $define_stmt;
Simon Glass048a6482020-05-22 16:32:35 -06005153 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005154 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5155 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005156 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005157 if ($use_cnt > 1) {
5158 CHK("MACRO_ARG_REUSE",
5159 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5160 }
5161# check if any macro arguments may have other precedence issues
5162 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5163 ((defined($1) && $1 ne ',') ||
5164 (defined($2) && $2 ne ','))) {
5165 CHK("MACRO_ARG_PRECEDENCE",
5166 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5167 }
5168 }
5169
5170# check for macros with flow control, but without ## concatenation
5171# ## concatenation is commonly a macro that defines a function so ignore those
5172 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005173 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005174 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005175
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005176 WARN("MACRO_WITH_FLOW_CONTROL",
5177 "Macros with flow control statements should be avoided\n" . "$herectx");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005178 }
5179
5180# check for line continuations outside of #defines, preprocessor #, and asm
5181
5182 } else {
5183 if ($prevline !~ /^..*\\$/ &&
5184 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5185 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5186 $line =~ /^\+.*\\$/) {
5187 WARN("LINE_CONTINUATIONS",
5188 "Avoid unnecessary line continuations\n" . $herecurr);
5189 }
5190 }
5191
5192# do {} while (0) macro tests:
5193# single-statement macros do not need to be enclosed in do while (0) loop,
5194# macro should not end with a semicolon
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005195 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005196 $realfile !~ m@/vmlinux.lds.h$@ &&
5197 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5198 my $ln = $linenr;
5199 my $cnt = $realcnt;
5200 my ($off, $dstat, $dcond, $rest);
5201 my $ctx = '';
5202 ($dstat, $dcond, $ln, $cnt, $off) =
5203 ctx_statement_block($linenr, $realcnt, 0);
5204 $ctx = $dstat;
5205
5206 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005207 $dstat =~ s/$;/ /g;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005208
5209 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5210 my $stmts = $2;
5211 my $semis = $3;
5212
5213 $ctx =~ s/\n*$//;
5214 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005215 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005216
5217 if (($stmts =~ tr/;/;/) == 1 &&
5218 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5219 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5220 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5221 }
5222 if (defined $semis && $semis ne "") {
5223 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5224 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005225 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005226 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5227 $ctx =~ s/\n*$//;
5228 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005229 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005230
5231 WARN("TRAILING_SEMICOLON",
5232 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005233 }
5234 }
5235
Joe Hershberger05622192011-10-18 10:06:59 +00005236# check for redundant bracing round if etc
5237 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5238 my ($level, $endln, @chunks) =
5239 ctx_statement_full($linenr, $realcnt, 1);
5240 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5241 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5242 if ($#chunks > 0 && $level == 0) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005243 my @allowed = ();
5244 my $allow = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005245 my $seen = 0;
5246 my $herectx = $here . "\n";
5247 my $ln = $linenr - 1;
5248 for my $chunk (@chunks) {
5249 my ($cond, $block) = @{$chunk};
5250
5251 # If the condition carries leading newlines, then count those as offsets.
5252 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5253 my $offset = statement_rawlines($whitespace) - 1;
5254
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005255 $allowed[$allow] = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005256 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5257
5258 # We have looked at and allowed this specific line.
5259 $suppress_ifbraces{$ln + $offset} = 1;
5260
5261 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5262 $ln += statement_rawlines($block) - 1;
5263
5264 substr($block, 0, length($cond), '');
5265
5266 $seen++ if ($block =~ /^\s*{/);
5267
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005268 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00005269 if (statement_lines($cond) > 1) {
5270 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005271 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005272 }
5273 if ($block =~/\b(?:if|for|while)\b/) {
5274 #print "APW: ALLOWED: block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005275 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005276 }
5277 if (statement_block_size($block) > 1) {
5278 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005279 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005280 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005281 $allow++;
Joe Hershberger05622192011-10-18 10:06:59 +00005282 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005283 if ($seen) {
5284 my $sum_allowed = 0;
5285 foreach (@allowed) {
5286 $sum_allowed += $_;
5287 }
5288 if ($sum_allowed == 0) {
5289 WARN("BRACES",
5290 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5291 } elsif ($sum_allowed != $allow &&
5292 $seen != $allow) {
5293 CHK("BRACES",
5294 "braces {} should be used on all arms of this statement\n" . $herectx);
5295 }
Joe Hershberger05622192011-10-18 10:06:59 +00005296 }
5297 }
5298 }
5299 if (!defined $suppress_ifbraces{$linenr - 1} &&
5300 $line =~ /\b(if|while|for|else)\b/) {
5301 my $allowed = 0;
5302
5303 # Check the pre-context.
5304 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5305 #print "APW: ALLOWED: pre<$1>\n";
5306 $allowed = 1;
5307 }
5308
5309 my ($level, $endln, @chunks) =
5310 ctx_statement_full($linenr, $realcnt, $-[0]);
5311
5312 # Check the condition.
5313 my ($cond, $block) = @{$chunks[0]};
5314 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5315 if (defined $cond) {
5316 substr($block, 0, length($cond), '');
5317 }
5318 if (statement_lines($cond) > 1) {
5319 #print "APW: ALLOWED: cond<$cond>\n";
5320 $allowed = 1;
5321 }
5322 if ($block =~/\b(?:if|for|while)\b/) {
5323 #print "APW: ALLOWED: block<$block>\n";
5324 $allowed = 1;
5325 }
5326 if (statement_block_size($block) > 1) {
5327 #print "APW: ALLOWED: lines block<$block>\n";
5328 $allowed = 1;
5329 }
5330 # Check the post-context.
5331 if (defined $chunks[1]) {
5332 my ($cond, $block) = @{$chunks[1]};
5333 if (defined $cond) {
5334 substr($block, 0, length($cond), '');
5335 }
5336 if ($block =~ /^\s*\{/) {
5337 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5338 $allowed = 1;
5339 }
5340 }
5341 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershberger05622192011-10-18 10:06:59 +00005342 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005343 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershberger05622192011-10-18 10:06:59 +00005344
5345 WARN("BRACES",
5346 "braces {} are not necessary for single statement blocks\n" . $herectx);
5347 }
5348 }
5349
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005350# check for single line unbalanced braces
5351 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5352 $sline =~ /^.\s*else\s*\{\s*$/) {
5353 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5354 }
5355
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005356# check for unnecessary blank lines around braces
Tom Rini6b9709d2014-02-27 08:27:28 -05005357 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005358 if (CHK("BRACES",
5359 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5360 $fix && $prevrawline =~ /^\+/) {
5361 fix_delete_line($fixlinenr - 1, $prevrawline);
5362 }
Joe Hershberger05622192011-10-18 10:06:59 +00005363 }
Tom Rini6b9709d2014-02-27 08:27:28 -05005364 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005365 if (CHK("BRACES",
5366 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5367 $fix) {
5368 fix_delete_line($fixlinenr, $rawline);
5369 }
Joe Hershberger05622192011-10-18 10:06:59 +00005370 }
5371
5372# no volatiles please
5373 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5374 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5375 WARN("VOLATILE",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005376 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5377 }
5378
5379# Check for user-visible strings broken across lines, which breaks the ability
5380# to grep for the string. Make exceptions when the previous string ends in a
5381# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5382# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5383 if ($line =~ /^\+\s*$String/ &&
5384 $prevline =~ /"\s*$/ &&
5385 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5386 if (WARN("SPLIT_STRING",
5387 "quoted string split across lines\n" . $hereprev) &&
5388 $fix &&
5389 $prevrawline =~ /^\+.*"\s*$/ &&
5390 $last_coalesced_string_linenr != $linenr - 1) {
5391 my $extracted_string = get_quoted_string($line, $rawline);
5392 my $comma_close = "";
5393 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5394 $comma_close = $1;
5395 }
5396
5397 fix_delete_line($fixlinenr - 1, $prevrawline);
5398 fix_delete_line($fixlinenr, $rawline);
5399 my $fixedline = $prevrawline;
5400 $fixedline =~ s/"\s*$//;
5401 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5402 fix_insert_line($fixlinenr - 1, $fixedline);
5403 $fixedline = $rawline;
5404 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5405 if ($fixedline !~ /\+\s*$/) {
5406 fix_insert_line($fixlinenr, $fixedline);
5407 }
5408 $last_coalesced_string_linenr = $linenr;
5409 }
5410 }
5411
5412# check for missing a space in a string concatenation
5413 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5414 WARN('MISSING_SPACE',
5415 "break quoted strings at a space character\n" . $hereprev);
5416 }
5417
5418# check for an embedded function name in a string when the function is known
5419# This does not work very well for -f --file checking as it depends on patch
5420# context providing the function name or a single line form for in-file
5421# function declarations
5422 if ($line =~ /^\+.*$String/ &&
5423 defined($context_function) &&
5424 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5425 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5426 WARN("EMBEDDED_FUNCTION_NAME",
5427 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5428 }
5429
5430# check for spaces before a quoted newline
5431 if ($rawline =~ /^.*\".*\s\\n/) {
5432 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5433 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5434 $fix) {
5435 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5436 }
5437
5438 }
5439
5440# concatenated string without spaces between elements
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005441 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5442 if (CHK("CONCATENATED_STRING",
5443 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5444 $fix) {
5445 while ($line =~ /($String)/g) {
5446 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5447 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5448 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5449 }
5450 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005451 }
5452
5453# uncoalesced string fragments
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005454 if ($line =~ /$String\s*"/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005455 if (WARN("STRING_FRAGMENTS",
5456 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5457 $fix) {
5458 while ($line =~ /($String)(?=\s*")/g) {
5459 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5460 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5461 }
5462 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005463 }
5464
5465# check for non-standard and hex prefixed decimal printf formats
5466 my $show_L = 1; #don't show the same defect twice
5467 my $show_Z = 1;
5468 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5469 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5470 $string =~ s/%%/__/g;
5471 # check for %L
5472 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5473 WARN("PRINTF_L",
5474 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5475 $show_L = 0;
5476 }
5477 # check for %Z
5478 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5479 WARN("PRINTF_Z",
5480 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5481 $show_Z = 0;
5482 }
5483 # check for 0x<decimal>
5484 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5485 ERROR("PRINTF_0XDECIMAL",
5486 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5487 }
5488 }
5489
5490# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005491 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005492 WARN("LINE_CONTINUATIONS",
5493 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005494 }
5495
5496# warn about #if 0
5497 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005498 WARN("IF_0",
5499 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5500 }
5501
5502# warn about #if 1
5503 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5504 WARN("IF_1",
5505 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005506 }
5507
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005508# check for needless "if (<foo>) fn(<foo>)" uses
5509 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005510 my $tested = quotemeta($1);
5511 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5512 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5513 my $func = $1;
5514 if (WARN('NEEDLESS_IF',
5515 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5516 $fix) {
5517 my $do_fix = 1;
5518 my $leading_tabs = "";
5519 my $new_leading_tabs = "";
5520 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5521 $leading_tabs = $1;
5522 } else {
5523 $do_fix = 0;
5524 }
5525 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5526 $new_leading_tabs = $1;
5527 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5528 $do_fix = 0;
5529 }
5530 } else {
5531 $do_fix = 0;
5532 }
5533 if ($do_fix) {
5534 fix_delete_line($fixlinenr - 1, $prevrawline);
5535 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5536 }
5537 }
5538 }
5539 }
5540
5541# check for unnecessary "Out of Memory" messages
5542 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5543 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5544 (defined $1 || defined $3) &&
5545 $linenr > 3) {
5546 my $testval = $2;
5547 my $testline = $lines[$linenr - 3];
5548
5549 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5550# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5551
Simon Glass048a6482020-05-22 16:32:35 -06005552 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005553 WARN("OOM_MESSAGE",
5554 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5555 }
5556 }
5557
5558# check for logging functions with KERN_<LEVEL>
5559 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5560 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5561 my $level = $1;
5562 if (WARN("UNNECESSARY_KERN_LEVEL",
5563 "Possible unnecessary $level\n" . $herecurr) &&
5564 $fix) {
5565 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5566 }
5567 }
5568
5569# check for logging continuations
5570 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5571 WARN("LOGGING_CONTINUATION",
5572 "Avoid logging continuation uses where feasible\n" . $herecurr);
5573 }
5574
5575# check for mask then right shift without a parentheses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005576 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005577 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5578 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5579 WARN("MASK_THEN_SHIFT",
5580 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5581 }
5582
5583# check for pointer comparisons to NULL
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005584 if ($perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005585 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5586 my $val = $1;
5587 my $equal = "!";
5588 $equal = "" if ($4 eq "!=");
5589 if (CHK("COMPARISON_TO_NULL",
5590 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5591 $fix) {
5592 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5593 }
Joe Hershberger05622192011-10-18 10:06:59 +00005594 }
5595 }
5596
Tom Rini6b9709d2014-02-27 08:27:28 -05005597# check for bad placement of section $InitAttribute (e.g.: __initdata)
5598 if ($line =~ /(\b$InitAttribute\b)/) {
5599 my $attr = $1;
5600 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5601 my $ptr = $1;
5602 my $var = $2;
5603 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5604 ERROR("MISPLACED_INIT",
5605 "$attr should be placed after $var\n" . $herecurr)) ||
5606 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5607 WARN("MISPLACED_INIT",
5608 "$attr should be placed after $var\n" . $herecurr))) &&
5609 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005610 $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;
Tom Rini6b9709d2014-02-27 08:27:28 -05005611 }
5612 }
5613 }
5614
5615# check for $InitAttributeData (ie: __initdata) with const
5616 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5617 my $attr = $1;
5618 $attr =~ /($InitAttributePrefix)(.*)/;
5619 my $attr_prefix = $1;
5620 my $attr_type = $2;
5621 if (ERROR("INIT_ATTRIBUTE",
5622 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5623 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005624 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005625 s/$InitAttributeData/${attr_prefix}initconst/;
5626 }
5627 }
5628
5629# check for $InitAttributeConst (ie: __initconst) without const
5630 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5631 my $attr = $1;
5632 if (ERROR("INIT_ATTRIBUTE",
5633 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5634 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005635 my $lead = $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005636 /(^\+\s*(?:static\s+))/;
5637 $lead = rtrim($1);
5638 $lead = "$lead " if ($lead !~ /^\+$/);
5639 $lead = "${lead}const ";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005640 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5641 }
5642 }
5643
5644# check for __read_mostly with const non-pointer (should just be const)
5645 if ($line =~ /\b__read_mostly\b/ &&
5646 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5647 if (ERROR("CONST_READ_MOSTLY",
5648 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5649 $fix) {
5650 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5651 }
5652 }
5653
5654# don't use __constant_<foo> functions outside of include/uapi/
5655 if ($realfile !~ m@^include/uapi/@ &&
5656 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5657 my $constant_func = $1;
5658 my $func = $constant_func;
5659 $func =~ s/^__constant_//;
5660 if (WARN("CONSTANT_CONVERSION",
5661 "$constant_func should be $func\n" . $herecurr) &&
5662 $fix) {
5663 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05005664 }
5665 }
5666
Joe Hershberger05622192011-10-18 10:06:59 +00005667# prefer usleep_range over udelay
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005668 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005669 my $delay = $1;
Joe Hershberger05622192011-10-18 10:06:59 +00005670 # ignore udelay's < 10, however
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005671 if (! ($delay < 10) ) {
Joe Hershberger05622192011-10-18 10:06:59 +00005672 CHK("USLEEP_RANGE",
Simon Glass048a6482020-05-22 16:32:35 -06005673 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005674 }
5675 if ($delay > 2000) {
5676 WARN("LONG_UDELAY",
5677 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005678 }
5679 }
5680
5681# warn about unexpectedly long msleep's
5682 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5683 if ($1 < 20) {
5684 WARN("MSLEEP",
Simon Glass048a6482020-05-22 16:32:35 -06005685 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005686 }
5687 }
5688
Tom Rini6b9709d2014-02-27 08:27:28 -05005689# check for comparisons of jiffies
5690 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5691 WARN("JIFFIES_COMPARISON",
5692 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5693 }
5694
5695# check for comparisons of get_jiffies_64()
5696 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5697 WARN("JIFFIES_COMPARISON",
5698 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5699 }
5700
Joe Hershberger05622192011-10-18 10:06:59 +00005701# warn about #ifdefs in C files
5702# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5703# print "#ifdef in C files should be avoided\n";
5704# print "$herecurr";
5705# $clean = 0;
5706# }
5707
5708# warn about spacing in #ifdefs
5709 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005710 if (ERROR("SPACING",
5711 "exactly one space required after that #$1\n" . $herecurr) &&
5712 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005713 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005714 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5715 }
5716
Joe Hershberger05622192011-10-18 10:06:59 +00005717 }
5718
5719# check for spinlock_t definitions without a comment.
5720 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5721 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5722 my $which = $1;
5723 if (!ctx_has_comment($first_line, $linenr)) {
5724 CHK("UNCOMMENTED_DEFINITION",
5725 "$1 definition without comment\n" . $herecurr);
5726 }
5727 }
5728# check for memory barriers without a comment.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005729
5730 my $barriers = qr{
5731 mb|
5732 rmb|
5733 wmb|
5734 read_barrier_depends
5735 }x;
5736 my $barrier_stems = qr{
5737 mb__before_atomic|
5738 mb__after_atomic|
5739 store_release|
5740 load_acquire|
5741 store_mb|
5742 (?:$barriers)
5743 }x;
5744 my $all_barriers = qr{
5745 (?:$barriers)|
5746 smp_(?:$barrier_stems)|
5747 virt_(?:$barrier_stems)
5748 }x;
5749
5750 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005751 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005752 WARN("MEMORY_BARRIER",
5753 "memory barrier without comment\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005754 }
5755 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005756
5757 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5758
5759 if ($realfile !~ m@^include/asm-generic/@ &&
5760 $realfile !~ m@/barrier\.h$@ &&
5761 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5762 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5763 WARN("MEMORY_BARRIER",
5764 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5765 }
5766
5767# check for waitqueue_active without a comment.
5768 if ($line =~ /\bwaitqueue_active\s*\(/) {
5769 if (!ctx_has_comment($first_line, $linenr)) {
5770 WARN("WAITQUEUE_ACTIVE",
5771 "waitqueue_active without comment\n" . $herecurr);
5772 }
5773 }
5774
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005775# check for smp_read_barrier_depends and read_barrier_depends
5776 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5777 WARN("READ_BARRIER_DEPENDS",
5778 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
5779 }
5780
Joe Hershberger05622192011-10-18 10:06:59 +00005781# check of hardware specific defines
5782 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5783 CHK("ARCH_DEFINES",
5784 "architecture specific defines should be avoided\n" . $herecurr);
5785 }
5786
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005787# check that the storage class is not after a type
5788 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005789 WARN("STORAGE_CLASS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005790 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5791 }
5792# Check that the storage class is at the beginning of a declaration
5793 if ($line =~ /\b$Storage\b/ &&
5794 $line !~ /^.\s*$Storage/ &&
5795 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5796 $1 !~ /[\,\)]\s*$/) {
5797 WARN("STORAGE_CLASS",
5798 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005799 }
5800
5801# check the location of the inline attribute, that it is between
5802# storage class and type.
5803 if ($line =~ /\b$Type\s+$Inline\b/ ||
5804 $line =~ /\b$Inline\s+$Storage\b/) {
5805 ERROR("INLINE_LOCATION",
5806 "inline keyword should sit between storage class and type\n" . $herecurr);
5807 }
5808
5809# Check for __inline__ and __inline, prefer inline
Tom Rini6b9709d2014-02-27 08:27:28 -05005810 if ($realfile !~ m@\binclude/uapi/@ &&
5811 $line =~ /\b(__inline__|__inline)\b/) {
5812 if (WARN("INLINE",
5813 "plain inline is preferred over $1\n" . $herecurr) &&
5814 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005815 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rini6b9709d2014-02-27 08:27:28 -05005816
5817 }
Joe Hershberger05622192011-10-18 10:06:59 +00005818 }
5819
5820# Check for __attribute__ packed, prefer __packed
Tom Rini6b9709d2014-02-27 08:27:28 -05005821 if ($realfile !~ m@\binclude/uapi/@ &&
5822 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005823 WARN("PREFER_PACKED",
5824 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5825 }
5826
5827# Check for __attribute__ aligned, prefer __aligned
Tom Rini6b9709d2014-02-27 08:27:28 -05005828 if ($realfile !~ m@\binclude/uapi/@ &&
5829 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005830 WARN("PREFER_ALIGNED",
5831 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5832 }
5833
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005834# Check for __attribute__ format(printf, prefer __printf
Tom Rini6b9709d2014-02-27 08:27:28 -05005835 if ($realfile !~ m@\binclude/uapi/@ &&
5836 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5837 if (WARN("PREFER_PRINTF",
5838 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5839 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005840 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05005841
5842 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005843 }
5844
5845# Check for __attribute__ format(scanf, prefer __scanf
Tom Rini6b9709d2014-02-27 08:27:28 -05005846 if ($realfile !~ m@\binclude/uapi/@ &&
5847 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5848 if (WARN("PREFER_SCANF",
5849 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5850 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005851 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5852 }
5853 }
5854
5855# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005856 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005857 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5858 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5859 $line =~ /\b__weak\b/)) {
5860 ERROR("WEAK_DECLARATION",
5861 "Using weak declarations can have unintended link defects\n" . $herecurr);
5862 }
5863
5864# check for c99 types like uint8_t used outside of uapi/ and tools/
5865 if ($realfile !~ m@\binclude/uapi/@ &&
5866 $realfile !~ m@\btools/@ &&
5867 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5868 my $type = $1;
5869 if ($type =~ /\b($typeC99Typedefs)\b/) {
5870 $type = $1;
5871 my $kernel_type = 'u';
5872 $kernel_type = 's' if ($type =~ /^_*[si]/);
5873 $type =~ /(\d+)/;
5874 $kernel_type .= $1;
5875 if (CHK("PREFER_KERNEL_TYPES",
5876 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5877 $fix) {
5878 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5879 }
5880 }
5881 }
5882
5883# check for cast of C90 native int or longer types constants
5884 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5885 my $cast = $1;
5886 my $const = $2;
5887 if (WARN("TYPECAST_INT_CONSTANT",
5888 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5889 $fix) {
5890 my $suffix = "";
5891 my $newconst = $const;
5892 $newconst =~ s/${Int_type}$//;
5893 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5894 if ($cast =~ /\blong\s+long\b/) {
5895 $suffix .= 'LL';
5896 } elsif ($cast =~ /\blong\b/) {
5897 $suffix .= 'L';
5898 }
5899 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rini6b9709d2014-02-27 08:27:28 -05005900 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005901 }
5902
Joe Hershberger05622192011-10-18 10:06:59 +00005903# check for sizeof(&)
5904 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5905 WARN("SIZEOF_ADDRESS",
5906 "sizeof(& should be avoided\n" . $herecurr);
5907 }
5908
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005909# check for sizeof without parenthesis
5910 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005911 if (WARN("SIZEOF_PARENTHESIS",
5912 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5913 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005914 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05005915 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005916 }
5917
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005918# check for struct spinlock declarations
5919 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5920 WARN("USE_SPINLOCK_T",
5921 "struct spinlock should be spinlock_t\n" . $herecurr);
5922 }
5923
Tom Rini6b9709d2014-02-27 08:27:28 -05005924# check for seq_printf uses that could be seq_puts
5925 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5926 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005927 $fmt =~ s/%%//g;
5928 if ($fmt !~ /%/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005929 if (WARN("PREFER_SEQ_PUTS",
5930 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5931 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005932 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Tom Rini6b9709d2014-02-27 08:27:28 -05005933 }
5934 }
5935 }
5936
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005937# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005938 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005939 defined $stat &&
5940 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5941 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005942 my $stat_real;
5943
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005944 my $lc = $stat =~ tr@\n@@;
5945 $lc = $lc + $linenr;
5946 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005947 my $specifier;
5948 my $extension;
5949 my $bad_specifier = "";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005950 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5951 $fmt =~ s/%%//g;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005952
5953 while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5954 $specifier = $1;
5955 $extension = $2;
Simon Glass048a6482020-05-22 16:32:35 -06005956 if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOx]/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005957 $bad_specifier = $specifier;
5958 last;
5959 }
5960 if ($extension eq "x" && !defined($stat_real)) {
5961 if (!defined($stat_real)) {
5962 $stat_real = get_stat_real($linenr, $lc);
5963 }
5964 WARN("VSPRINTF_SPECIFIER_PX",
5965 "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");
5966 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005967 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005968 if ($bad_specifier ne "") {
5969 my $stat_real = get_stat_real($linenr, $lc);
5970 my $ext_type = "Invalid";
5971 my $use = "";
5972 if ($bad_specifier =~ /p[Ff]/) {
5973 $ext_type = "Deprecated";
5974 $use = " - use %pS instead";
5975 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
5976 }
5977
5978 WARN("VSPRINTF_POINTER_EXTENSION",
5979 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005980 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005981 }
5982 }
5983
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005984# Check for misused memsets
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005985 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005986 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005987 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005988
5989 my $ms_addr = $2;
5990 my $ms_val = $7;
5991 my $ms_size = $12;
5992
5993 if ($ms_size =~ /^(0x|)0$/i) {
5994 ERROR("MEMSET",
5995 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5996 } elsif ($ms_size =~ /^(0x|)1$/i) {
5997 WARN("MEMSET",
5998 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5999 }
6000 }
6001
Tom Rini6b9709d2014-02-27 08:27:28 -05006002# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006003# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006004# defined $stat &&
6005# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6006# if (WARN("PREFER_ETHER_ADDR_COPY",
6007# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6008# $fix) {
6009# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6010# }
6011# }
6012
6013# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006014# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006015# defined $stat &&
6016# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6017# WARN("PREFER_ETHER_ADDR_EQUAL",
6018# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6019# }
6020
6021# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6022# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006023# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006024# defined $stat &&
6025# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6026#
6027# my $ms_val = $7;
6028#
6029# if ($ms_val =~ /^(?:0x|)0+$/i) {
6030# if (WARN("PREFER_ETH_ZERO_ADDR",
6031# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6032# $fix) {
6033# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6034# }
6035# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6036# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6037# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6038# $fix) {
6039# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6040# }
6041# }
6042# }
Tom Rini6b9709d2014-02-27 08:27:28 -05006043
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006044# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006045 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006046 defined $stat &&
6047 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6048 if (defined $2 || defined $7) {
6049 my $call = $1;
6050 my $cast1 = deparenthesize($2);
6051 my $arg1 = $3;
6052 my $cast2 = deparenthesize($7);
6053 my $arg2 = $8;
6054 my $cast;
6055
6056 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6057 $cast = "$cast1 or $cast2";
6058 } elsif ($cast1 ne "") {
6059 $cast = $cast1;
6060 } else {
6061 $cast = $cast2;
6062 }
6063 WARN("MINMAX",
6064 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6065 }
6066 }
6067
6068# check usleep_range arguments
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006069 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006070 defined $stat &&
6071 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6072 my $min = $1;
6073 my $max = $7;
6074 if ($min eq $max) {
6075 WARN("USLEEP_RANGE",
Simon Glass048a6482020-05-22 16:32:35 -06006076 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006077 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6078 $min > $max) {
6079 WARN("USLEEP_RANGE",
Simon Glass048a6482020-05-22 16:32:35 -06006080 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006081 }
6082 }
6083
Tom Rini6b9709d2014-02-27 08:27:28 -05006084# check for naked sscanf
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006085 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006086 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006087 $line =~ /\bsscanf\b/ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006088 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6089 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6090 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6091 my $lc = $stat =~ tr@\n@@;
6092 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006093 my $stat_real = get_stat_real($linenr, $lc);
Tom Rini6b9709d2014-02-27 08:27:28 -05006094 WARN("NAKED_SSCANF",
6095 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6096 }
6097
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006098# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006099 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006100 defined $stat &&
6101 $line =~ /\bsscanf\b/) {
6102 my $lc = $stat =~ tr@\n@@;
6103 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006104 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006105 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6106 my $format = $6;
6107 my $count = $format =~ tr@%@%@;
6108 if ($count == 1 &&
6109 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6110 WARN("SSCANF_TO_KSTRTO",
6111 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6112 }
6113 }
6114 }
6115
Tom Rini6b9709d2014-02-27 08:27:28 -05006116# check for new externs in .h files.
6117 if ($realfile =~ /\.h$/ &&
6118 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6119 if (CHK("AVOID_EXTERNS",
6120 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6121 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006122 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006123 }
6124 }
6125
Joe Hershberger05622192011-10-18 10:06:59 +00006126# check for new externs in .c files.
6127 if ($realfile =~ /\.c$/ && defined $stat &&
6128 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6129 {
6130 my $function_name = $1;
6131 my $paren_space = $2;
6132
6133 my $s = $stat;
6134 if (defined $cond) {
6135 substr($s, 0, length($cond), '');
6136 }
6137 if ($s =~ /^\s*;/ &&
6138 $function_name ne 'uninitialized_var')
6139 {
6140 WARN("AVOID_EXTERNS",
6141 "externs should be avoided in .c files\n" . $herecurr);
6142 }
6143
6144 if ($paren_space =~ /\n/) {
6145 WARN("FUNCTION_ARGUMENTS",
6146 "arguments for function declarations should follow identifier\n" . $herecurr);
6147 }
6148
6149 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6150 $stat =~ /^.\s*extern\s+/)
6151 {
6152 WARN("AVOID_EXTERNS",
6153 "externs should be avoided in .c files\n" . $herecurr);
6154 }
6155
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006156# check for function declarations that have arguments without identifier names
6157 if (defined $stat &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006158 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006159 $1 ne "void") {
6160 my $args = trim($1);
6161 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6162 my $arg = trim($1);
6163 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6164 WARN("FUNCTION_ARGUMENTS",
6165 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6166 }
6167 }
6168 }
6169
6170# check for function definitions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006171 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006172 defined $stat &&
6173 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6174 $context_function = $1;
6175
6176# check for multiline function definition with misplaced open brace
6177 my $ok = 0;
6178 my $cnt = statement_rawlines($stat);
6179 my $herectx = $here . "\n";
6180 for (my $n = 0; $n < $cnt; $n++) {
6181 my $rl = raw_line($linenr, $n);
6182 $herectx .= $rl . "\n";
6183 $ok = 1 if ($rl =~ /^[ \+]\{/);
6184 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6185 last if $rl =~ /^[ \+].*\{/;
6186 }
6187 if (!$ok) {
6188 ERROR("OPEN_BRACE",
6189 "open brace '{' following function definitions go on the next line\n" . $herectx);
6190 }
6191 }
6192
Joe Hershberger05622192011-10-18 10:06:59 +00006193# checks for new __setup's
6194 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6195 my $name = $1;
6196
6197 if (!grep(/$name/, @setup_docs)) {
6198 CHK("UNDOCUMENTED_SETUP",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006199 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006200 }
6201 }
6202
Simon Glass048a6482020-05-22 16:32:35 -06006203# check for pointless casting of kmalloc return
6204 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006205 WARN("UNNECESSARY_CASTS",
6206 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6207 }
6208
Tom Rini6b9709d2014-02-27 08:27:28 -05006209# alloc style
6210# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006211 if ($perl_version_ok &&
Simon Glass048a6482020-05-22 16:32:35 -06006212 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006213 CHK("ALLOC_SIZEOF_STRUCT",
6214 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6215 }
6216
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006217# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006218 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006219 defined $stat &&
6220 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6221 my $oldfunc = $3;
6222 my $a1 = $4;
6223 my $a2 = $10;
6224 my $newfunc = "kmalloc_array";
6225 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6226 my $r1 = $a1;
6227 my $r2 = $a2;
6228 if ($a1 =~ /^sizeof\s*\S/) {
6229 $r1 = $a2;
6230 $r2 = $a1;
6231 }
6232 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6233 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006234 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006235 my $herectx = get_stat_here($linenr, $cnt, $here);
6236
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006237 if (WARN("ALLOC_WITH_MULTIPLY",
6238 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6239 $cnt == 1 &&
6240 $fix) {
6241 $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;
6242 }
6243 }
6244 }
6245
Tom Rini6b9709d2014-02-27 08:27:28 -05006246# check for krealloc arg reuse
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006247 if ($perl_version_ok &&
6248 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6249 $1 eq $3) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006250 WARN("KREALLOC_ARG_REUSE",
6251 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6252 }
6253
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006254# check for alloc argument mismatch
6255 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6256 WARN("ALLOC_ARRAY_ARGS",
6257 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006258 }
6259
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006260# check for multiple semicolons
6261 if ($line =~ /;\s*;\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006262 if (WARN("ONE_SEMICOLON",
6263 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6264 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006265 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6266 }
6267 }
6268
6269# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6270 if ($realfile !~ m@^include/uapi/@ &&
6271 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6272 my $ull = "";
6273 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6274 if (CHK("BIT_MACRO",
6275 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6276 $fix) {
6277 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6278 }
6279 }
6280
6281# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6282 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6283 my $config = $1;
6284 if (WARN("PREFER_IS_ENABLED",
6285 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6286 $fix) {
6287 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
Tom Rini6b9709d2014-02-27 08:27:28 -05006288 }
6289 }
6290
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006291# check for case / default statements not preceded by break/fallthrough/switch
Tom Rini6b9709d2014-02-27 08:27:28 -05006292 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6293 my $has_break = 0;
6294 my $has_statement = 0;
6295 my $count = 0;
6296 my $prevline = $linenr;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006297 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006298 $prevline--;
6299 my $rline = $rawlines[$prevline - 1];
6300 my $fline = $lines[$prevline - 1];
6301 last if ($fline =~ /^\@\@/);
6302 next if ($fline =~ /^\-/);
6303 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6304 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6305 next if ($fline =~ /^.[\s$;]*$/);
6306 $has_statement = 1;
6307 $count++;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006308 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
Tom Rini6b9709d2014-02-27 08:27:28 -05006309 }
6310 if (!$has_break && $has_statement) {
6311 WARN("MISSING_BREAK",
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006312 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05006313 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006314 }
6315
6316# check for switch/default statements without a break;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006317 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006318 defined $stat &&
6319 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006320 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006321 my $herectx = get_stat_here($linenr, $cnt, $here);
6322
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006323 WARN("DEFAULT_NO_BREAK",
6324 "switch default: should use break\n" . $herectx);
Eric Nelson172a3a82012-05-02 20:32:18 -07006325 }
6326
Joe Hershberger05622192011-10-18 10:06:59 +00006327# check for gcc specific __FUNCTION__
Tom Rini6b9709d2014-02-27 08:27:28 -05006328 if ($line =~ /\b__FUNCTION__\b/) {
6329 if (WARN("USE_FUNC",
6330 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6331 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006332 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05006333 }
Joe Hershberger05622192011-10-18 10:06:59 +00006334 }
6335
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006336# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6337 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6338 ERROR("DATE_TIME",
6339 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6340 }
6341
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006342# check for use of yield()
6343 if ($line =~ /\byield\s*\(\s*\)/) {
6344 WARN("YIELD",
6345 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6346 }
6347
Tom Rini6b9709d2014-02-27 08:27:28 -05006348# check for comparisons against true and false
6349 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6350 my $lead = $1;
6351 my $arg = $2;
6352 my $test = $3;
6353 my $otype = $4;
6354 my $trail = $5;
6355 my $op = "!";
6356
6357 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6358
6359 my $type = lc($otype);
6360 if ($type =~ /^(?:true|false)$/) {
6361 if (("$test" eq "==" && "$type" eq "true") ||
6362 ("$test" eq "!=" && "$type" eq "false")) {
6363 $op = "";
6364 }
6365
6366 CHK("BOOL_COMPARISON",
6367 "Using comparison to $otype is error prone\n" . $herecurr);
6368
6369## maybe suggesting a correct construct would better
6370## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6371
6372 }
6373 }
6374
Simon Glass048a6482020-05-22 16:32:35 -06006375# check for bool bitfields
6376 if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6377 WARN("BOOL_BITFIELD",
6378 "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6379 }
6380
6381# check for bool use in .h files
6382 if ($realfile =~ /\.h$/ &&
6383 $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6384 CHK("BOOL_MEMBER",
6385 "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6386 }
6387
Joe Hershberger05622192011-10-18 10:06:59 +00006388# check for semaphores initialized locked
6389 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6390 WARN("CONSIDER_COMPLETION",
6391 "consider using a completion\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006392 }
Joe Hershberger05622192011-10-18 10:06:59 +00006393
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006394# recommend kstrto* over simple_strto* and strict_strto*
6395 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006396 WARN("CONSIDER_KSTRTO",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006397 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006398 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006399
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006400# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershberger05622192011-10-18 10:06:59 +00006401 if ($line =~ /^.\s*__initcall\s*\(/) {
6402 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006403 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006404 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006405
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006406# check for various structs that are normally const (ops, kgdb, device_tree)
6407# and avoid what seem like struct definitions 'struct foo {'
Joe Hershberger05622192011-10-18 10:06:59 +00006408 if ($line !~ /\bconst\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006409 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006410 WARN("CONST_STRUCT",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006411 "struct $1 should normally be const\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006412 }
6413
6414# use of NR_CPUS is usually wrong
6415# ignore definitions of NR_CPUS and usage to define arrays as likely right
6416 if ($line =~ /\bNR_CPUS\b/ &&
6417 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6418 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6419 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6420 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6421 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6422 {
6423 WARN("NR_CPUS",
6424 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6425 }
6426
Tom Rini6b9709d2014-02-27 08:27:28 -05006427# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6428 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6429 ERROR("DEFINE_ARCH_HAS",
6430 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6431 }
6432
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006433# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006434 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006435 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6436 WARN("LIKELY_MISUSE",
6437 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006438 }
6439
6440# whine mightly about in_atomic
6441 if ($line =~ /\bin_atomic\s*\(/) {
6442 if ($realfile =~ m@^drivers/@) {
6443 ERROR("IN_ATOMIC",
6444 "do not use in_atomic in drivers\n" . $herecurr);
6445 } elsif ($realfile !~ m@^kernel/@) {
6446 WARN("IN_ATOMIC",
6447 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6448 }
6449 }
6450
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006451# check for mutex_trylock_recursive usage
6452 if ($line =~ /mutex_trylock_recursive/) {
6453 ERROR("LOCKING",
6454 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6455 }
6456
Joe Hershberger05622192011-10-18 10:06:59 +00006457# check for lockdep_set_novalidate_class
6458 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6459 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6460 if ($realfile !~ m@^kernel/lockdep@ &&
6461 $realfile !~ m@^include/linux/lockdep@ &&
6462 $realfile !~ m@^drivers/base/core@) {
6463 ERROR("LOCKDEP",
6464 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6465 }
6466 }
6467
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006468 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6469 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006470 WARN("EXPORTED_WORLD_WRITABLE",
6471 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6472 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006473
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006474# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6475# and whether or not function naming is typical and if
6476# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006477 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006478 defined $stat &&
6479 $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*\)/) {
6480 my $var = $1;
6481 my $perms = $2;
6482 my $show = $3;
6483 my $store = $4;
6484 my $octal_perms = perms_to_octal($perms);
6485 if ($show =~ /^${var}_show$/ &&
6486 $store =~ /^${var}_store$/ &&
6487 $octal_perms eq "0644") {
6488 if (WARN("DEVICE_ATTR_RW",
6489 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6490 $fix) {
6491 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6492 }
6493 } elsif ($show =~ /^${var}_show$/ &&
6494 $store =~ /^NULL$/ &&
6495 $octal_perms eq "0444") {
6496 if (WARN("DEVICE_ATTR_RO",
6497 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6498 $fix) {
6499 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6500 }
6501 } elsif ($show =~ /^NULL$/ &&
6502 $store =~ /^${var}_store$/ &&
6503 $octal_perms eq "0200") {
6504 if (WARN("DEVICE_ATTR_WO",
6505 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6506 $fix) {
6507 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6508 }
6509 } elsif ($octal_perms eq "0644" ||
6510 $octal_perms eq "0444" ||
6511 $octal_perms eq "0200") {
6512 my $newshow = "$show";
6513 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6514 my $newstore = $store;
6515 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6516 my $rename = "";
6517 if ($show ne $newshow) {
6518 $rename .= " '$show' to '$newshow'";
6519 }
6520 if ($store ne $newstore) {
6521 $rename .= " '$store' to '$newstore'";
6522 }
6523 WARN("DEVICE_ATTR_FUNCTIONS",
6524 "Consider renaming function(s)$rename\n" . $herecurr);
6525 } else {
6526 WARN("DEVICE_ATTR_PERMS",
6527 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6528 }
6529 }
6530
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006531# Mode permission misuses where it seems decimal should be octal
6532# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006533# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6534# specific definition of not visible in sysfs.
6535# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6536# use the default permissions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006537 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006538 defined $stat &&
6539 $line =~ /$mode_perms_search/) {
6540 foreach my $entry (@mode_permission_funcs) {
6541 my $func = $entry->[0];
6542 my $arg_pos = $entry->[1];
6543
6544 my $lc = $stat =~ tr@\n@@;
6545 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006546 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006547
6548 my $skip_args = "";
6549 if ($arg_pos > 1) {
6550 $arg_pos--;
6551 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6552 }
6553 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6554 if ($stat =~ /$test/) {
6555 my $val = $1;
6556 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006557 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6558 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6559 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006560 ERROR("NON_OCTAL_PERMISSIONS",
6561 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6562 }
6563 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6564 ERROR("EXPORTED_WORLD_WRITABLE",
6565 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6566 }
6567 }
6568 }
6569 }
6570
6571# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006572 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6573 my $oval = $1;
6574 my $octal = perms_to_octal($oval);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006575 if (WARN("SYMBOLIC_PERMS",
6576 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6577 $fix) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006578 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006579 }
6580 }
6581
6582# validate content of MODULE_LICENSE against list from include/linux/module.h
6583 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6584 my $extracted_string = get_quoted_string($line, $rawline);
6585 my $valid_licenses = qr{
6586 GPL|
6587 GPL\ v2|
6588 GPL\ and\ additional\ rights|
6589 Dual\ BSD/GPL|
6590 Dual\ MIT/GPL|
6591 Dual\ MPL/GPL|
6592 Proprietary
6593 }x;
6594 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6595 WARN("MODULE_LICENSE",
6596 "unknown module license " . $extracted_string . "\n" . $herecurr);
6597 }
6598 }
Joe Hershberger05622192011-10-18 10:06:59 +00006599 }
6600
6601 # If we have no input at all, then there is nothing to report on
6602 # so just keep quiet.
6603 if ($#rawlines == -1) {
6604 exit(0);
6605 }
6606
6607 # In mailback mode only produce a report in the negative, for
6608 # things that appear to be patches.
6609 if ($mailback && ($clean == 1 || !$is_patch)) {
6610 exit(0);
6611 }
6612
6613 # This is not a patch, and we are are in 'no-patch' mode so
6614 # just keep quiet.
6615 if (!$chk_patch && !$is_patch) {
6616 exit(0);
6617 }
6618
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006619 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006620 ERROR("NOT_UNIFIED_DIFF",
6621 "Does not appear to be a unified-diff format patch\n");
6622 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006623 if ($is_patch && $has_commit_log && $chk_signoff) {
6624 if ($signoff == 0) {
6625 ERROR("MISSING_SIGN_OFF",
6626 "Missing Signed-off-by: line(s)\n");
6627 } elsif (!$authorsignoff) {
6628 WARN("NO_AUTHOR_SIGN_OFF",
6629 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6630 }
Joe Hershberger05622192011-10-18 10:06:59 +00006631 }
6632
6633 print report_dump();
6634 if ($summary && !($clean == 1 && $quiet == 1)) {
6635 print "$filename " if ($summary_file);
6636 print "total: $cnt_error errors, $cnt_warn warnings, " .
6637 (($check)? "$cnt_chk checks, " : "") .
6638 "$cnt_lines lines checked\n";
Joe Hershberger05622192011-10-18 10:06:59 +00006639 }
6640
6641 if ($quiet == 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006642 # If there were any defects found and not already fixing them
6643 if (!$clean and !$fix) {
6644 print << "EOM"
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006645
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006646NOTE: For some of the reported defects, checkpatch may be able to
6647 mechanically convert to the typical style using --fix or --fix-inplace.
6648EOM
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006649 }
Joe Hershberger05622192011-10-18 10:06:59 +00006650 # If there were whitespace errors which cleanpatch can fix
6651 # then suggest that.
6652 if ($rpt_cleaners) {
Joe Hershberger05622192011-10-18 10:06:59 +00006653 $rpt_cleaners = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006654 print << "EOM"
6655
6656NOTE: Whitespace errors detected.
6657 You may wish to use scripts/cleanpatch or scripts/cleanfile
6658EOM
Joe Hershberger05622192011-10-18 10:06:59 +00006659 }
6660 }
6661
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006662 if ($clean == 0 && $fix &&
6663 ("@rawlines" ne "@fixed" ||
6664 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006665 my $newfile = $filename;
6666 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6667 my $linecount = 0;
6668 my $f;
6669
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006670 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6671
Tom Rini6b9709d2014-02-27 08:27:28 -05006672 open($f, '>', $newfile)
6673 or die "$P: Can't open $newfile for write\n";
6674 foreach my $fixed_line (@fixed) {
6675 $linecount++;
6676 if ($file) {
6677 if ($linecount > 3) {
6678 $fixed_line =~ s/^\+//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006679 print $f $fixed_line . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05006680 }
6681 } else {
6682 print $f $fixed_line . "\n";
6683 }
6684 }
6685 close($f);
6686
6687 if (!$quiet) {
6688 print << "EOM";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006689
Tom Rini6b9709d2014-02-27 08:27:28 -05006690Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6691
6692Do _NOT_ trust the results written to this file.
6693Do _NOT_ submit these changes without inspecting them for correctness.
6694
6695This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6696No warranties, expressed or implied...
Tom Rini6b9709d2014-02-27 08:27:28 -05006697EOM
6698 }
Joe Hershberger05622192011-10-18 10:06:59 +00006699 }
6700
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006701 if ($quiet == 0) {
6702 print "\n";
6703 if ($clean == 1) {
6704 print "$vname has no obvious style problems and is ready for submission.\n";
6705 } else {
6706 print "$vname has style problems, please review.\n";
6707 }
Joe Hershberger05622192011-10-18 10:06:59 +00006708 }
Joe Hershberger05622192011-10-18 10:06:59 +00006709 return $clean;
6710}