blob: 01ab570a168beb5dac696ff688cbd0ccce4cab7d [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";
Tom Rinic57383b2020-06-16 10:29:46 -040065my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
66# git output parsing needs US English output, so first set backtick child process LANGUAGE
67my $git_command ='export LANGUAGE=en_US.UTF-8; git';
68my $tabsize = 8;
Joe Hershberger05622192011-10-18 10:06:59 +000069
70sub help {
71 my ($exitcode) = @_;
72
73 print << "EOM";
74Usage: $P [OPTION]... [FILE]...
75Version: $V
76
77Options:
78 -q, --quiet quiet
79 --no-tree run without a kernel tree
80 --no-signoff do not check for 'Signed-off-by' line
81 --patch treat FILE as patchfile (default)
82 --emacs emacs compile window format
83 --terse one line per report
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020084 --showfile emit diffed file position, not input file position
85 -g, --git treat FILE as a single commit or git revision range
86 single git commit with:
87 <rev>
88 <rev>^
89 <rev>~n
90 multiple git commits with:
91 <rev1>..<rev2>
92 <rev1>...<rev2>
93 <rev>-<count>
94 git merges are ignored
Joe Hershberger05622192011-10-18 10:06:59 +000095 -f, --file treat FILE as regular source file
96 --subjective, --strict enable more subjective tests
Heinrich Schuchardt6305db92017-09-12 09:57:45 +020097 --list-types list the possible message types
Tom Rini6b9709d2014-02-27 08:27:28 -050098 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Hershberger05622192011-10-18 10:06:59 +000099 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200100 --show-types show the specific message type in the output
Simon Glass048a6482020-05-22 16:32:35 -0600101 --max-line-length=n set the maximum line length, (default $max_line_length)
102 if exceeded, warn on patches
103 requires --strict for use with --file
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200104 --min-conf-desc-length=n set the min description length, if shorter, warn
Tom Rinic57383b2020-06-16 10:29:46 -0400105 --tab-size=n set the number of spaces for tab (default $tabsize)
Joe Hershberger05622192011-10-18 10:06:59 +0000106 --root=PATH PATH to the kernel tree root
107 --no-summary suppress the per-file summary
108 --mailback only produce a report in case of warnings/errors
109 --summary-file include the filename in summary
110 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
111 'values', 'possible', 'type', and 'attr' (default
112 is all off)
113 --test-only=WORD report only warnings/errors containing WORD
114 literally
Tom Rini6b9709d2014-02-27 08:27:28 -0500115 --fix EXPERIMENTAL - may create horrible results
116 If correctable single-line errors exist, create
117 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
118 with potential errors corrected to the preferred
119 checkpatch style
120 --fix-inplace EXPERIMENTAL - may create horrible results
121 Is the same as --fix, but overwrites the input
122 file. It's your fault if there's no backup or git
123 --ignore-perl-version override checking of perl version. expect
124 runtime errors.
Dan Murphyc10e0f52017-01-31 14:15:53 -0600125 --codespell Use the codespell dictionary for spelling/typos
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200126 (default:/usr/share/codespell/dictionary.txt)
Dan Murphyc10e0f52017-01-31 14:15:53 -0600127 --codespellfile Use this codespell dictionary
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200128 --typedefsfile Read additional types from this file
129 --color[=WHEN] Use colors 'always', 'never', or only when output
130 is a terminal ('auto'). Default is 'auto'.
Simon Glassb77df592020-05-22 16:32:36 -0600131 --u-boot Run additional checks for U-Boot
Joe Hershberger05622192011-10-18 10:06:59 +0000132 -h, --help, --version display this help and exit
133
134When FILE is - read standard input.
135EOM
136
137 exit($exitcode);
138}
139
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200140sub uniq {
141 my %seen;
142 return grep { !$seen{$_}++ } @_;
143}
144
145sub list_types {
146 my ($exitcode) = @_;
147
148 my $count = 0;
149
150 local $/ = undef;
151
152 open(my $script, '<', abs_path($P)) or
153 die "$P: Can't read '$P' $!\n";
154
155 my $text = <$script>;
156 close($script);
157
158 my @types = ();
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200159 # Also catch when type or level is passed through a variable
160 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200161 push (@types, $_);
162 }
163 @types = sort(uniq(@types));
164 print("#\tMessage type\n\n");
165 foreach my $type (@types) {
166 print(++$count . "\t" . $type . "\n");
167 }
168
169 exit($exitcode);
170}
171
Joe Hershberger05622192011-10-18 10:06:59 +0000172my $conf = which_conf($configuration_file);
173if (-f $conf) {
174 my @conf_args;
175 open(my $conffile, '<', "$conf")
176 or warn "$P: Can't find a readable $configuration_file file $!\n";
177
178 while (<$conffile>) {
179 my $line = $_;
180
181 $line =~ s/\s*\n?$//g;
182 $line =~ s/^\s*//g;
183 $line =~ s/\s+/ /g;
184
185 next if ($line =~ m/^\s*#/);
186 next if ($line =~ m/^\s*$/);
187
188 my @words = split(" ", $line);
189 foreach my $word (@words) {
190 last if ($word =~ m/^#/);
191 push (@conf_args, $word);
192 }
193 }
194 close($conffile);
195 unshift(@ARGV, @conf_args) if @conf_args;
196}
197
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200198# Perl's Getopt::Long allows options to take optional arguments after a space.
199# Prevent --color by itself from consuming other arguments
200foreach (@ARGV) {
201 if ($_ eq "--color" || $_ eq "-color") {
202 $_ = "--color=$color";
203 }
204}
205
Joe Hershberger05622192011-10-18 10:06:59 +0000206GetOptions(
207 'q|quiet+' => \$quiet,
208 'tree!' => \$tree,
209 'signoff!' => \$chk_signoff,
210 'patch!' => \$chk_patch,
211 'emacs!' => \$emacs,
212 'terse!' => \$terse,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200213 'showfile!' => \$showfile,
Joe Hershberger05622192011-10-18 10:06:59 +0000214 'f|file!' => \$file,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200215 'g|git!' => \$git,
Joe Hershberger05622192011-10-18 10:06:59 +0000216 'subjective!' => \$check,
217 'strict!' => \$check,
218 'ignore=s' => \@ignore,
Tom Rini6b9709d2014-02-27 08:27:28 -0500219 'types=s' => \@use,
Joe Hershberger05622192011-10-18 10:06:59 +0000220 'show-types!' => \$show_types,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200221 'list-types!' => \$list_types,
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000222 'max-line-length=i' => \$max_line_length,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200223 'min-conf-desc-length=i' => \$min_conf_desc_length,
Tom Rinic57383b2020-06-16 10:29:46 -0400224 'tab-size=i' => \$tabsize,
Joe Hershberger05622192011-10-18 10:06:59 +0000225 'root=s' => \$root,
226 'summary!' => \$summary,
227 'mailback!' => \$mailback,
228 'summary-file!' => \$summary_file,
Tom Rini6b9709d2014-02-27 08:27:28 -0500229 'fix!' => \$fix,
230 'fix-inplace!' => \$fix_inplace,
231 'ignore-perl-version!' => \$ignore_perl_version,
Joe Hershberger05622192011-10-18 10:06:59 +0000232 'debug=s' => \%debug,
233 'test-only=s' => \$tst_only,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200234 'codespell!' => \$codespell,
235 'codespellfile=s' => \$codespellfile,
236 'typedefsfile=s' => \$typedefsfile,
Simon Glassb77df592020-05-22 16:32:36 -0600237 'u-boot' => \$u_boot,
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200238 'color=s' => \$color,
239 'no-color' => \$color, #keep old behaviors of -nocolor
240 'nocolor' => \$color, #keep old behaviors of -nocolor
Joe Hershberger05622192011-10-18 10:06:59 +0000241 'h|help' => \$help,
242 'version' => \$help
243) or help(1);
244
245help(0) if ($help);
246
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200247list_types(0) if ($list_types);
248
Tom Rini6b9709d2014-02-27 08:27:28 -0500249$fix = 1 if ($fix_inplace);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200250$check_orig = $check;
Tom Rini6b9709d2014-02-27 08:27:28 -0500251
Joe Hershberger05622192011-10-18 10:06:59 +0000252my $exit = 0;
253
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200254my $perl_version_ok = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -0500255if ($^V && $^V lt $minimum_perl_version) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200256 $perl_version_ok = 0;
Tom Rini6b9709d2014-02-27 08:27:28 -0500257 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200258 exit(1) if (!$ignore_perl_version);
Tom Rini6b9709d2014-02-27 08:27:28 -0500259}
260
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200261#if no filenames are given, push '-' to read patch from stdin
Joe Hershberger05622192011-10-18 10:06:59 +0000262if ($#ARGV < 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200263 push(@ARGV, '-');
264}
265
266if ($color =~ /^[01]$/) {
267 $color = !$color;
268} elsif ($color =~ /^always$/i) {
269 $color = 1;
270} elsif ($color =~ /^never$/i) {
271 $color = 0;
272} elsif ($color =~ /^auto$/i) {
273 $color = (-t STDOUT);
274} else {
275 die "Invalid color mode: $color\n";
Joe Hershberger05622192011-10-18 10:06:59 +0000276}
277
Tom Rinic57383b2020-06-16 10:29:46 -0400278# skip TAB size 1 to avoid additional checks on $tabsize - 1
279die "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
280
Tom Rini6b9709d2014-02-27 08:27:28 -0500281sub hash_save_array_words {
282 my ($hashRef, $arrayRef) = @_;
Joe Hershberger05622192011-10-18 10:06:59 +0000283
Tom Rini6b9709d2014-02-27 08:27:28 -0500284 my @array = split(/,/, join(',', @$arrayRef));
285 foreach my $word (@array) {
286 $word =~ s/\s*\n?$//g;
287 $word =~ s/^\s*//g;
288 $word =~ s/\s+/ /g;
289 $word =~ tr/[a-z]/[A-Z]/;
Joe Hershberger05622192011-10-18 10:06:59 +0000290
Tom Rini6b9709d2014-02-27 08:27:28 -0500291 next if ($word =~ m/^\s*#/);
292 next if ($word =~ m/^\s*$/);
293
294 $hashRef->{$word}++;
295 }
Joe Hershberger05622192011-10-18 10:06:59 +0000296}
297
Tom Rini6b9709d2014-02-27 08:27:28 -0500298sub hash_show_words {
299 my ($hashRef, $prefix) = @_;
300
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200301 if (keys %$hashRef) {
302 print "\nNOTE: $prefix message types:";
Tom Rini6b9709d2014-02-27 08:27:28 -0500303 foreach my $word (sort keys %$hashRef) {
304 print " $word";
305 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200306 print "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -0500307 }
308}
309
310hash_save_array_words(\%ignore_type, \@ignore);
311hash_save_array_words(\%use_type, \@use);
312
Joe Hershberger05622192011-10-18 10:06:59 +0000313my $dbg_values = 0;
314my $dbg_possible = 0;
315my $dbg_type = 0;
316my $dbg_attr = 0;
317for my $key (keys %debug) {
318 ## no critic
319 eval "\${dbg_$key} = '$debug{$key}';";
320 die "$@" if ($@);
321}
322
323my $rpt_cleaners = 0;
324
325if ($terse) {
326 $emacs = 1;
327 $quiet++;
328}
329
330if ($tree) {
331 if (defined $root) {
332 if (!top_of_kernel_tree($root)) {
333 die "$P: $root: --root does not point at a valid tree\n";
334 }
335 } else {
336 if (top_of_kernel_tree('.')) {
337 $root = '.';
338 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
339 top_of_kernel_tree($1)) {
340 $root = $1;
341 }
342 }
343
344 if (!defined $root) {
345 print "Must be run from the top-level dir. of a kernel tree\n";
346 exit(2);
347 }
348}
349
350my $emitted_corrupt = 0;
351
352our $Ident = qr{
353 [A-Za-z_][A-Za-z\d_]*
354 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
355 }x;
356our $Storage = qr{extern|static|asmlinkage};
357our $Sparse = qr{
358 __user|
359 __kernel|
360 __force|
361 __iomem|
362 __must_check|
Joe Hershberger05622192011-10-18 10:06:59 +0000363 __kprobes|
364 __ref|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200365 __refconst|
366 __refdata|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200367 __rcu|
368 __private
Joe Hershberger05622192011-10-18 10:06:59 +0000369 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -0500370our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
371our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
372our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
373our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
374our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Hershberger05622192011-10-18 10:06:59 +0000375
376# Notes to $Attribute:
377# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
378our $Attribute = qr{
379 const|
380 __percpu|
381 __nocast|
382 __safe|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200383 __bitwise|
Joe Hershberger05622192011-10-18 10:06:59 +0000384 __packed__|
385 __packed2__|
386 __naked|
387 __maybe_unused|
388 __always_unused|
389 __noreturn|
390 __used|
391 __cold|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200392 __pure|
Joe Hershberger05622192011-10-18 10:06:59 +0000393 __noclone|
394 __deprecated|
395 __read_mostly|
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200396 __ro_after_init|
Joe Hershberger05622192011-10-18 10:06:59 +0000397 __kprobes|
Tom Rini6b9709d2014-02-27 08:27:28 -0500398 $InitAttribute|
Joe Hershberger05622192011-10-18 10:06:59 +0000399 ____cacheline_aligned|
400 ____cacheline_aligned_in_smp|
401 ____cacheline_internodealigned_in_smp|
402 __weak
403 }x;
404our $Modifier;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200405our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Joe Hershberger05622192011-10-18 10:06:59 +0000406our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
407our $Lval = qr{$Ident(?:$Member)*};
408
Tom Rini6b9709d2014-02-27 08:27:28 -0500409our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
410our $Binary = qr{(?i)0b[01]+$Int_type?};
411our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
412our $Int = qr{[0-9]+$Int_type?};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200413our $Octal = qr{0[0-7]+$Int_type?};
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200414our $String = qr{"[X\t]*"};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000415our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
416our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
417our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
418our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200419our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000420our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200421our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Tom Rini6b9709d2014-02-27 08:27:28 -0500422our $Arithmetic = qr{\+|-|\*|\/|%};
Joe Hershberger05622192011-10-18 10:06:59 +0000423our $Operators = qr{
424 <=|>=|==|!=|
425 =>|->|<<|>>|<|>|!|~|
Tom Rini6b9709d2014-02-27 08:27:28 -0500426 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Joe Hershberger05622192011-10-18 10:06:59 +0000427 }x;
428
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200429our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
430
431our $BasicType;
Joe Hershberger05622192011-10-18 10:06:59 +0000432our $NonptrType;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200433our $NonptrTypeMisordered;
Tom Rini6b9709d2014-02-27 08:27:28 -0500434our $NonptrTypeWithAttr;
Joe Hershberger05622192011-10-18 10:06:59 +0000435our $Type;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200436our $TypeMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000437our $Declare;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200438our $DeclareMisordered;
Joe Hershberger05622192011-10-18 10:06:59 +0000439
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000440our $NON_ASCII_UTF8 = qr{
441 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Joe Hershberger05622192011-10-18 10:06:59 +0000442 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
443 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
444 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
445 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
446 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
447 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
448}x;
449
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000450our $UTF8 = qr{
451 [\x09\x0A\x0D\x20-\x7E] # ASCII
452 | $NON_ASCII_UTF8
453}x;
454
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200455our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
456our $typeOtherOSTypedefs = qr{(?x:
457 u_(?:char|short|int|long) | # bsd
458 u(?:nchar|short|int|long) # sysv
459)};
460our $typeKernelTypedefs = qr{(?x:
Joe Hershberger05622192011-10-18 10:06:59 +0000461 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
462 atomic_t
463)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200464our $typeTypedefs = qr{(?x:
465 $typeC99Typedefs\b|
466 $typeOtherOSTypedefs\b|
467 $typeKernelTypedefs\b
468)};
469
470our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
Joe Hershberger05622192011-10-18 10:06:59 +0000471
472our $logFunctions = qr{(?x:
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200473 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Tom Rini6b9709d2014-02-27 08:27:28 -0500474 (?:[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 +0200475 TP_printk|
Joe Hershberger05622192011-10-18 10:06:59 +0000476 WARN(?:_RATELIMIT|_ONCE|)|
477 panic|
James Byrne66b3ccc2019-11-21 14:32:46 +0000478 debug|
479 printf|
Tom Rini6b9709d2014-02-27 08:27:28 -0500480 MODULE_[A-Z_]+|
481 seq_vprintf|seq_printf|seq_puts
Joe Hershberger05622192011-10-18 10:06:59 +0000482)};
483
Tom Rinic57383b2020-06-16 10:29:46 -0400484our $allocFunctions = qr{(?x:
485 (?:(?:devm_)?
486 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
487 kstrdup(?:_const)? |
488 kmemdup(?:_nul)?) |
489 (?:\w+)?alloc_skb(?:_ip_align)? |
490 # dev_alloc_skb/netdev_alloc_skb, et al
491 dma_alloc_coherent
492)};
493
Joe Hershberger05622192011-10-18 10:06:59 +0000494our $signature_tags = qr{(?xi:
495 Signed-off-by:|
Tom Rinic57383b2020-06-16 10:29:46 -0400496 Co-developed-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000497 Acked-by:|
498 Tested-by:|
499 Reviewed-by:|
500 Reported-by:|
Tom Rini6b9709d2014-02-27 08:27:28 -0500501 Suggested-by:|
Joe Hershberger05622192011-10-18 10:06:59 +0000502 To:|
503 Cc:
504)};
505
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200506our @typeListMisordered = (
507 qr{char\s+(?:un)?signed},
508 qr{int\s+(?:(?:un)?signed\s+)?short\s},
509 qr{int\s+short(?:\s+(?:un)?signed)},
510 qr{short\s+int(?:\s+(?:un)?signed)},
511 qr{(?:un)?signed\s+int\s+short},
512 qr{short\s+(?:un)?signed},
513 qr{long\s+int\s+(?:un)?signed},
514 qr{int\s+long\s+(?:un)?signed},
515 qr{long\s+(?:un)?signed\s+int},
516 qr{int\s+(?:un)?signed\s+long},
517 qr{int\s+(?:un)?signed},
518 qr{int\s+long\s+long\s+(?:un)?signed},
519 qr{long\s+long\s+int\s+(?:un)?signed},
520 qr{long\s+long\s+(?:un)?signed\s+int},
521 qr{long\s+long\s+(?:un)?signed},
522 qr{long\s+(?:un)?signed},
523);
524
Joe Hershberger05622192011-10-18 10:06:59 +0000525our @typeList = (
526 qr{void},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200527 qr{(?:(?:un)?signed\s+)?char},
528 qr{(?:(?:un)?signed\s+)?short\s+int},
529 qr{(?:(?:un)?signed\s+)?short},
530 qr{(?:(?:un)?signed\s+)?int},
531 qr{(?:(?:un)?signed\s+)?long\s+int},
532 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
533 qr{(?:(?:un)?signed\s+)?long\s+long},
534 qr{(?:(?:un)?signed\s+)?long},
535 qr{(?:un)?signed},
Joe Hershberger05622192011-10-18 10:06:59 +0000536 qr{float},
537 qr{double},
538 qr{bool},
539 qr{struct\s+$Ident},
540 qr{union\s+$Ident},
541 qr{enum\s+$Ident},
542 qr{${Ident}_t},
543 qr{${Ident}_handler},
544 qr{${Ident}_handler_fn},
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200545 @typeListMisordered,
Joe Hershberger05622192011-10-18 10:06:59 +0000546);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200547
548our $C90_int_types = qr{(?x:
549 long\s+long\s+int\s+(?:un)?signed|
550 long\s+long\s+(?:un)?signed\s+int|
551 long\s+long\s+(?:un)?signed|
552 (?:(?:un)?signed\s+)?long\s+long\s+int|
553 (?:(?:un)?signed\s+)?long\s+long|
554 int\s+long\s+long\s+(?:un)?signed|
555 int\s+(?:(?:un)?signed\s+)?long\s+long|
556
557 long\s+int\s+(?:un)?signed|
558 long\s+(?:un)?signed\s+int|
559 long\s+(?:un)?signed|
560 (?:(?:un)?signed\s+)?long\s+int|
561 (?:(?:un)?signed\s+)?long|
562 int\s+long\s+(?:un)?signed|
563 int\s+(?:(?:un)?signed\s+)?long|
564
565 int\s+(?:un)?signed|
566 (?:(?:un)?signed\s+)?int
567)};
568
569our @typeListFile = ();
Tom Rini6b9709d2014-02-27 08:27:28 -0500570our @typeListWithAttr = (
571 @typeList,
572 qr{struct\s+$InitAttribute\s+$Ident},
573 qr{union\s+$InitAttribute\s+$Ident},
574);
575
Joe Hershberger05622192011-10-18 10:06:59 +0000576our @modifierList = (
577 qr{fastcall},
578);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200579our @modifierListFile = ();
580
581our @mode_permission_funcs = (
582 ["module_param", 3],
583 ["module_param_(?:array|named|string)", 4],
584 ["module_param_array_named", 5],
585 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
586 ["proc_create(?:_data|)", 2],
587 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
588 ["IIO_DEV_ATTR_[A-Z_]+", 1],
589 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
590 ["SENSOR_TEMPLATE(?:_2|)", 3],
591 ["__ATTR", 2],
592);
593
594#Create a search pattern for all these functions to speed up a loop below
595our $mode_perms_search = "";
596foreach my $entry (@mode_permission_funcs) {
597 $mode_perms_search .= '|' if ($mode_perms_search ne "");
598 $mode_perms_search .= $entry->[0];
599}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200600$mode_perms_search = "(?:${mode_perms_search})";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200601
Tom Rinic57383b2020-06-16 10:29:46 -0400602our %deprecated_apis = (
603 "synchronize_rcu_bh" => "synchronize_rcu",
604 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
605 "call_rcu_bh" => "call_rcu",
606 "rcu_barrier_bh" => "rcu_barrier",
607 "synchronize_sched" => "synchronize_rcu",
608 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
609 "call_rcu_sched" => "call_rcu",
610 "rcu_barrier_sched" => "rcu_barrier",
611 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
612 "cond_synchronize_sched" => "cond_synchronize_rcu",
613);
614
615#Create a search pattern for all these strings to speed up a loop below
616our $deprecated_apis_search = "";
617foreach my $entry (keys %deprecated_apis) {
618 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
619 $deprecated_apis_search .= $entry;
620}
621$deprecated_apis_search = "(?:${deprecated_apis_search})";
622
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200623our $mode_perms_world_writable = qr{
624 S_IWUGO |
625 S_IWOTH |
626 S_IRWXUGO |
627 S_IALLUGO |
628 0[0-7][0-7][2367]
629}x;
630
631our %mode_permission_string_types = (
632 "S_IRWXU" => 0700,
633 "S_IRUSR" => 0400,
634 "S_IWUSR" => 0200,
635 "S_IXUSR" => 0100,
636 "S_IRWXG" => 0070,
637 "S_IRGRP" => 0040,
638 "S_IWGRP" => 0020,
639 "S_IXGRP" => 0010,
640 "S_IRWXO" => 0007,
641 "S_IROTH" => 0004,
642 "S_IWOTH" => 0002,
643 "S_IXOTH" => 0001,
644 "S_IRWXUGO" => 0777,
645 "S_IRUGO" => 0444,
646 "S_IWUGO" => 0222,
647 "S_IXUGO" => 0111,
648);
649
650#Create a search pattern for all these strings to speed up a loop below
651our $mode_perms_string_search = "";
652foreach my $entry (keys %mode_permission_string_types) {
653 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
654 $mode_perms_string_search .= $entry;
655}
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200656our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
657our $multi_mode_perms_string_search = qr{
658 ${single_mode_perms_string_search}
659 (?:\s*\|\s*${single_mode_perms_string_search})*
660}x;
661
662sub perms_to_octal {
663 my ($string) = @_;
664
665 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
666
667 my $val = "";
668 my $oval = "";
669 my $to = 0;
670 my $curpos = 0;
671 my $lastpos = 0;
672 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
673 $curpos = pos($string);
674 my $match = $2;
675 my $omatch = $1;
676 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
677 $lastpos = $curpos;
678 $to |= $mode_permission_string_types{$match};
679 $val .= '\s*\|\s*' if ($val ne "");
680 $val .= $match;
681 $oval .= $omatch;
682 }
683 $oval =~ s/^\s*\|\s*//;
684 $oval =~ s/\s*\|\s*$//;
685 return sprintf("%04o", $to);
686}
Joe Hershberger05622192011-10-18 10:06:59 +0000687
688our $allowed_asm_includes = qr{(?x:
689 irq|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200690 memory|
691 time|
692 reboot
Joe Hershberger05622192011-10-18 10:06:59 +0000693)};
694# memory.h: ARM has a custom one
695
Dan Murphyc10e0f52017-01-31 14:15:53 -0600696# Load common spelling mistakes and build regular expression list.
697my $misspellings;
698my %spelling_fix;
699
700if (open(my $spelling, '<', $spelling_file)) {
701 while (<$spelling>) {
702 my $line = $_;
703
704 $line =~ s/\s*\n?$//g;
705 $line =~ s/^\s*//g;
706
707 next if ($line =~ m/^\s*#/);
708 next if ($line =~ m/^\s*$/);
709
710 my ($suspect, $fix) = split(/\|\|/, $line);
711
712 $spelling_fix{$suspect} = $fix;
713 }
714 close($spelling);
715} else {
716 warn "No typos will be found - file '$spelling_file': $!\n";
717}
718
719if ($codespell) {
720 if (open(my $spelling, '<', $codespellfile)) {
721 while (<$spelling>) {
722 my $line = $_;
723
724 $line =~ s/\s*\n?$//g;
725 $line =~ s/^\s*//g;
726
727 next if ($line =~ m/^\s*#/);
728 next if ($line =~ m/^\s*$/);
729 next if ($line =~ m/, disabled/i);
730
731 $line =~ s/,.*$//;
732
733 my ($suspect, $fix) = split(/->/, $line);
734
735 $spelling_fix{$suspect} = $fix;
736 }
737 close($spelling);
738 } else {
739 warn "No codespell typos will be found - file '$codespellfile': $!\n";
740 }
741}
742
743$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
744
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200745sub read_words {
746 my ($wordsRef, $file) = @_;
747
748 if (open(my $words, '<', $file)) {
749 while (<$words>) {
750 my $line = $_;
751
752 $line =~ s/\s*\n?$//g;
753 $line =~ s/^\s*//g;
754
755 next if ($line =~ m/^\s*#/);
756 next if ($line =~ m/^\s*$/);
757 if ($line =~ /\s/) {
758 print("$file: '$line' invalid - ignored\n");
759 next;
760 }
761
762 $$wordsRef .= '|' if ($$wordsRef ne "");
763 $$wordsRef .= $line;
764 }
765 close($file);
766 return 1;
767 }
768
769 return 0;
770}
771
772my $const_structs = "";
773read_words(\$const_structs, $conststructsfile)
774 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
775
776my $typeOtherTypedefs = "";
777if (length($typedefsfile)) {
778 read_words(\$typeOtherTypedefs, $typedefsfile)
779 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
780}
781$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
Dan Murphyc10e0f52017-01-31 14:15:53 -0600782
Joe Hershberger05622192011-10-18 10:06:59 +0000783sub build_types {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200784 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
785 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
786 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Tom Rini6b9709d2014-02-27 08:27:28 -0500787 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Joe Hershberger05622192011-10-18 10:06:59 +0000788 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200789 $BasicType = qr{
790 (?:$typeTypedefs\b)|
791 (?:${all}\b)
792 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000793 $NonptrType = qr{
794 (?:$Modifier\s+|const\s+)*
795 (?:
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000796 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Hershberger05622192011-10-18 10:06:59 +0000797 (?:$typeTypedefs\b)|
798 (?:${all}\b)
799 )
800 (?:\s+$Modifier|\s+const)*
801 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200802 $NonptrTypeMisordered = qr{
803 (?:$Modifier\s+|const\s+)*
804 (?:
805 (?:${Misordered}\b)
806 )
807 (?:\s+$Modifier|\s+const)*
808 }x;
Tom Rini6b9709d2014-02-27 08:27:28 -0500809 $NonptrTypeWithAttr = qr{
810 (?:$Modifier\s+|const\s+)*
811 (?:
812 (?:typeof|__typeof__)\s*\([^\)]*\)|
813 (?:$typeTypedefs\b)|
814 (?:${allWithAttr}\b)
815 )
816 (?:\s+$Modifier|\s+const)*
817 }x;
Joe Hershberger05622192011-10-18 10:06:59 +0000818 $Type = qr{
819 $NonptrType
Tom Rinic57383b2020-06-16 10:29:46 -0400820 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Joe Hershberger05622192011-10-18 10:06:59 +0000821 (?:\s+$Inline|\s+$Modifier)*
822 }x;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200823 $TypeMisordered = qr{
824 $NonptrTypeMisordered
Tom Rinic57383b2020-06-16 10:29:46 -0400825 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200826 (?:\s+$Inline|\s+$Modifier)*
827 }x;
828 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
829 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Joe Hershberger05622192011-10-18 10:06:59 +0000830}
831build_types();
832
Joe Hershberger05622192011-10-18 10:06:59 +0000833our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Kim Phillipsd45a6ae2013-02-28 12:53:52 +0000834
835# Using $balanced_parens, $LvalOrFunc, or $FuncArg
836# requires at least perl version v5.10.0
837# Any use must be runtime checked with $^V
838
839our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200840our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
841our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
842
843our $declaration_macros = qr{(?x:
844 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
845 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +0200846 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
847 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200848)};
Joe Hershberger05622192011-10-18 10:06:59 +0000849
850sub deparenthesize {
851 my ($string) = @_;
852 return "" if (!defined($string));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200853
854 while ($string =~ /^\s*\(.*\)\s*$/) {
855 $string =~ s@^\s*\(\s*@@;
856 $string =~ s@\s*\)\s*$@@;
857 }
858
Joe Hershberger05622192011-10-18 10:06:59 +0000859 $string =~ s@\s+@ @g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200860
Joe Hershberger05622192011-10-18 10:06:59 +0000861 return $string;
862}
863
Tom Rini6b9709d2014-02-27 08:27:28 -0500864sub seed_camelcase_file {
865 my ($file) = @_;
866
867 return if (!(-f $file));
868
869 local $/;
870
871 open(my $include_file, '<', "$file")
872 or warn "$P: Can't read '$file' $!\n";
873 my $text = <$include_file>;
874 close($include_file);
875
876 my @lines = split('\n', $text);
877
878 foreach my $line (@lines) {
879 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
880 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
881 $camelcase{$1} = 1;
882 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
883 $camelcase{$1} = 1;
884 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
885 $camelcase{$1} = 1;
886 }
887 }
888}
889
Tom Rinic57383b2020-06-16 10:29:46 -0400890our %maintained_status = ();
891
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200892sub is_maintained_obsolete {
893 my ($filename) = @_;
894
895 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
896
Tom Rinic57383b2020-06-16 10:29:46 -0400897 if (!exists($maintained_status{$filename})) {
898 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
899 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200900
Tom Rinic57383b2020-06-16 10:29:46 -0400901 return $maintained_status{$filename} =~ /obsolete/i;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200902}
903
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +0200904sub is_SPDX_License_valid {
905 my ($license) = @_;
906
907 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
908
909 my $root_path = abs_path($root);
910 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
911 return 0 if ($status ne "");
912 return 1;
913}
914
Tom Rini6b9709d2014-02-27 08:27:28 -0500915my $camelcase_seeded = 0;
916sub seed_camelcase_includes {
917 return if ($camelcase_seeded);
918
919 my $files;
920 my $camelcase_cache = "";
921 my @include_files = ();
922
923 $camelcase_seeded = 1;
924
925 if (-e ".git") {
Tom Rinic57383b2020-06-16 10:29:46 -0400926 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
Tom Rini6b9709d2014-02-27 08:27:28 -0500927 chomp $git_last_include_commit;
928 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
929 } else {
930 my $last_mod_date = 0;
931 $files = `find $root/include -name "*.h"`;
932 @include_files = split('\n', $files);
933 foreach my $file (@include_files) {
934 my $date = POSIX::strftime("%Y%m%d%H%M",
935 localtime((stat $file)[9]));
936 $last_mod_date = $date if ($last_mod_date < $date);
937 }
938 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
939 }
940
941 if ($camelcase_cache ne "" && -f $camelcase_cache) {
942 open(my $camelcase_file, '<', "$camelcase_cache")
943 or warn "$P: Can't read '$camelcase_cache' $!\n";
944 while (<$camelcase_file>) {
945 chomp;
946 $camelcase{$_} = 1;
947 }
948 close($camelcase_file);
949
950 return;
951 }
952
953 if (-e ".git") {
Tom Rinic57383b2020-06-16 10:29:46 -0400954 $files = `${git_command} ls-files "include/*.h"`;
Tom Rini6b9709d2014-02-27 08:27:28 -0500955 @include_files = split('\n', $files);
956 }
957
958 foreach my $file (@include_files) {
959 seed_camelcase_file($file);
960 }
961
962 if ($camelcase_cache ne "") {
963 unlink glob ".checkpatch-camelcase.*";
964 open(my $camelcase_file, '>', "$camelcase_cache")
965 or warn "$P: Can't write '$camelcase_cache' $!\n";
966 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
967 print $camelcase_file ("$_\n");
968 }
969 close($camelcase_file);
970 }
971}
972
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200973sub git_commit_info {
974 my ($commit, $id, $desc) = @_;
975
976 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
977
Tom Rinic57383b2020-06-16 10:29:46 -0400978 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200979 $output =~ s/^\s*//gm;
980 my @lines = split("\n", $output);
981
982 return ($id, $desc) if ($#lines < 0);
983
Tom Rinic57383b2020-06-16 10:29:46 -0400984 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +0200985# Maybe one day convert this block of bash into something that returns
986# all matching commit ids, but it's very slow...
987#
988# echo "checking commits $1..."
989# git rev-list --remotes | grep -i "^$1" |
990# while read line ; do
991# git log --format='%H %s' -1 $line |
992# echo "commit $(cut -c 1-12,41-)"
993# done
994 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
995 $id = undef;
996 } else {
997 $id = substr($lines[0], 0, 12);
998 $desc = substr($lines[0], 41);
999 }
1000
1001 return ($id, $desc);
1002}
1003
Joe Hershberger05622192011-10-18 10:06:59 +00001004$chk_signoff = 0 if ($file);
1005
Joe Hershberger05622192011-10-18 10:06:59 +00001006my @rawlines = ();
1007my @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001008my @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001009my @fixed_inserted = ();
1010my @fixed_deleted = ();
Dan Murphyc10e0f52017-01-31 14:15:53 -06001011my $fixlinenr = -1;
1012
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001013# If input is git commits, extract all commits from the commit expressions.
1014# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1015die "$P: No git repository found\n" if ($git && !-e ".git");
1016
1017if ($git) {
1018 my @commits = ();
1019 foreach my $commit_expr (@ARGV) {
1020 my $git_range;
1021 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1022 $git_range = "-$2 $1";
1023 } elsif ($commit_expr =~ m/\.\./) {
1024 $git_range = "$commit_expr";
1025 } else {
1026 $git_range = "-1 $commit_expr";
1027 }
Tom Rinic57383b2020-06-16 10:29:46 -04001028 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001029 foreach my $line (split(/\n/, $lines)) {
1030 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1031 next if (!defined($1) || !defined($2));
1032 my $sha1 = $1;
1033 my $subject = $2;
1034 unshift(@commits, $sha1);
1035 $git_commits{$sha1} = $subject;
1036 }
1037 }
1038 die "$P: no git commits after extraction!\n" if (@commits == 0);
1039 @ARGV = @commits;
1040}
1041
1042my $vname;
Tom Rinic57383b2020-06-16 10:29:46 -04001043$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
Joe Hershberger05622192011-10-18 10:06:59 +00001044for my $filename (@ARGV) {
1045 my $FILE;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001046 if ($git) {
1047 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1048 die "$P: $filename: git format-patch failed - $!\n";
1049 } elsif ($file) {
Joe Hershberger05622192011-10-18 10:06:59 +00001050 open($FILE, '-|', "diff -u /dev/null $filename") ||
1051 die "$P: $filename: diff failed - $!\n";
1052 } elsif ($filename eq '-') {
1053 open($FILE, '<&STDIN');
1054 } else {
1055 open($FILE, '<', "$filename") ||
1056 die "$P: $filename: open failed - $!\n";
1057 }
1058 if ($filename eq '-') {
1059 $vname = 'Your patch';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001060 } elsif ($git) {
1061 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Joe Hershberger05622192011-10-18 10:06:59 +00001062 } else {
1063 $vname = $filename;
1064 }
1065 while (<$FILE>) {
1066 chomp;
1067 push(@rawlines, $_);
1068 }
1069 close($FILE);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001070
1071 if ($#ARGV > 0 && $quiet == 0) {
1072 print '-' x length($vname) . "\n";
1073 print "$vname\n";
1074 print '-' x length($vname) . "\n";
1075 }
1076
Joe Hershberger05622192011-10-18 10:06:59 +00001077 if (!process($filename)) {
1078 $exit = 1;
1079 }
1080 @rawlines = ();
1081 @lines = ();
Tom Rini6b9709d2014-02-27 08:27:28 -05001082 @fixed = ();
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001083 @fixed_inserted = ();
1084 @fixed_deleted = ();
1085 $fixlinenr = -1;
1086 @modifierListFile = ();
1087 @typeListFile = ();
1088 build_types();
1089}
1090
1091if (!$quiet) {
1092 hash_show_words(\%use_type, "Used");
1093 hash_show_words(\%ignore_type, "Ignored");
1094
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001095 if (!$perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001096 print << "EOM"
1097
1098NOTE: perl $^V is not modern enough to detect all possible issues.
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02001099 An upgrade to at least perl $minimum_perl_version is suggested.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001100EOM
1101 }
1102 if ($exit) {
1103 print << "EOM"
1104
1105NOTE: If any of the errors are false positives, please report
1106 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1107EOM
1108 }
Joe Hershberger05622192011-10-18 10:06:59 +00001109}
1110
1111exit($exit);
1112
1113sub top_of_kernel_tree {
1114 my ($root) = @_;
1115
1116 my @tree_check = (
Tom Rini6b9709d2014-02-27 08:27:28 -05001117 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
Joe Hershberger05622192011-10-18 10:06:59 +00001118 "README", "Documentation", "arch", "include", "drivers",
1119 "fs", "init", "ipc", "kernel", "lib", "scripts",
1120 );
1121
1122 foreach my $check (@tree_check) {
1123 if (! -e $root . '/' . $check) {
1124 return 0;
1125 }
1126 }
1127 return 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001128}
Joe Hershberger05622192011-10-18 10:06:59 +00001129
1130sub parse_email {
1131 my ($formatted_email) = @_;
1132
1133 my $name = "";
Tom Rinic57383b2020-06-16 10:29:46 -04001134 my $name_comment = "";
Joe Hershberger05622192011-10-18 10:06:59 +00001135 my $address = "";
1136 my $comment = "";
1137
1138 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1139 $name = $1;
1140 $address = $2;
1141 $comment = $3 if defined $3;
1142 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1143 $address = $1;
1144 $comment = $2 if defined $2;
1145 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1146 $address = $1;
1147 $comment = $2 if defined $2;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001148 $formatted_email =~ s/\Q$address\E.*$//;
Joe Hershberger05622192011-10-18 10:06:59 +00001149 $name = $formatted_email;
Tom Rini6b9709d2014-02-27 08:27:28 -05001150 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001151 $name =~ s/^\"|\"$//g;
1152 # If there's a name left after stripping spaces and
1153 # leading quotes, and the address doesn't have both
1154 # leading and trailing angle brackets, the address
1155 # is invalid. ie:
1156 # "joe smith joe@smith.com" bad
1157 # "joe smith <joe@smith.com" bad
1158 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1159 $name = "";
1160 $address = "";
1161 $comment = "";
1162 }
1163 }
1164
Tom Rini6b9709d2014-02-27 08:27:28 -05001165 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001166 $name =~ s/^\"|\"$//g;
Tom Rinic57383b2020-06-16 10:29:46 -04001167 $name =~ s/(\s*\([^\)]+\))\s*//;
1168 if (defined($1)) {
1169 $name_comment = trim($1);
1170 }
Tom Rini6b9709d2014-02-27 08:27:28 -05001171 $address = trim($address);
Joe Hershberger05622192011-10-18 10:06:59 +00001172 $address =~ s/^\<|\>$//g;
1173
1174 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1175 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1176 $name = "\"$name\"";
1177 }
1178
Tom Rinic57383b2020-06-16 10:29:46 -04001179 return ($name, $name_comment, $address, $comment);
Joe Hershberger05622192011-10-18 10:06:59 +00001180}
1181
1182sub format_email {
1183 my ($name, $address) = @_;
1184
1185 my $formatted_email;
1186
Tom Rini6b9709d2014-02-27 08:27:28 -05001187 $name = trim($name);
Joe Hershberger05622192011-10-18 10:06:59 +00001188 $name =~ s/^\"|\"$//g;
Tom Rini6b9709d2014-02-27 08:27:28 -05001189 $address = trim($address);
Joe Hershberger05622192011-10-18 10:06:59 +00001190
1191 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1192 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1193 $name = "\"$name\"";
1194 }
1195
1196 if ("$name" eq "") {
1197 $formatted_email = "$address";
1198 } else {
1199 $formatted_email = "$name <$address>";
1200 }
1201
1202 return $formatted_email;
1203}
1204
Tom Rinic57383b2020-06-16 10:29:46 -04001205sub reformat_email {
1206 my ($email) = @_;
1207
1208 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1209 return format_email($email_name, $email_address);
1210}
1211
1212sub same_email_addresses {
1213 my ($email1, $email2) = @_;
1214
1215 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1216 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1217
1218 return $email1_name eq $email2_name &&
1219 $email1_address eq $email2_address;
1220}
1221
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001222sub which {
1223 my ($bin) = @_;
1224
1225 foreach my $path (split(/:/, $ENV{PATH})) {
1226 if (-e "$path/$bin") {
1227 return "$path/$bin";
1228 }
1229 }
1230
1231 return "";
1232}
1233
Joe Hershberger05622192011-10-18 10:06:59 +00001234sub which_conf {
1235 my ($conf) = @_;
1236
1237 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1238 if (-e "$path/$conf") {
1239 return "$path/$conf";
1240 }
1241 }
1242
1243 return "";
1244}
1245
1246sub expand_tabs {
1247 my ($str) = @_;
1248
1249 my $res = '';
1250 my $n = 0;
1251 for my $c (split(//, $str)) {
1252 if ($c eq "\t") {
1253 $res .= ' ';
1254 $n++;
Tom Rinic57383b2020-06-16 10:29:46 -04001255 for (; ($n % $tabsize) != 0; $n++) {
Joe Hershberger05622192011-10-18 10:06:59 +00001256 $res .= ' ';
1257 }
1258 next;
1259 }
1260 $res .= $c;
1261 $n++;
1262 }
1263
1264 return $res;
1265}
1266sub copy_spacing {
1267 (my $res = shift) =~ tr/\t/ /c;
1268 return $res;
1269}
1270
1271sub line_stats {
1272 my ($line) = @_;
1273
1274 # Drop the diff line leader and expand tabs
1275 $line =~ s/^.//;
1276 $line = expand_tabs($line);
1277
1278 # Pick the indent from the front of the line.
1279 my ($white) = ($line =~ /^(\s*)/);
1280
1281 return (length($line), length($white));
1282}
1283
1284my $sanitise_quote = '';
1285
1286sub sanitise_line_reset {
1287 my ($in_comment) = @_;
1288
1289 if ($in_comment) {
1290 $sanitise_quote = '*/';
1291 } else {
1292 $sanitise_quote = '';
1293 }
1294}
1295sub sanitise_line {
1296 my ($line) = @_;
1297
1298 my $res = '';
1299 my $l = '';
1300
1301 my $qlen = 0;
1302 my $off = 0;
1303 my $c;
1304
1305 # Always copy over the diff marker.
1306 $res = substr($line, 0, 1);
1307
1308 for ($off = 1; $off < length($line); $off++) {
1309 $c = substr($line, $off, 1);
1310
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001311 # Comments we are whacking completely including the begin
Joe Hershberger05622192011-10-18 10:06:59 +00001312 # and end, all to $;.
1313 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1314 $sanitise_quote = '*/';
1315
1316 substr($res, $off, 2, "$;$;");
1317 $off++;
1318 next;
1319 }
1320 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1321 $sanitise_quote = '';
1322 substr($res, $off, 2, "$;$;");
1323 $off++;
1324 next;
1325 }
1326 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1327 $sanitise_quote = '//';
1328
1329 substr($res, $off, 2, $sanitise_quote);
1330 $off++;
1331 next;
1332 }
1333
1334 # A \ in a string means ignore the next character.
1335 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1336 $c eq "\\") {
1337 substr($res, $off, 2, 'XX');
1338 $off++;
1339 next;
1340 }
1341 # Regular quotes.
1342 if ($c eq "'" || $c eq '"') {
1343 if ($sanitise_quote eq '') {
1344 $sanitise_quote = $c;
1345
1346 substr($res, $off, 1, $c);
1347 next;
1348 } elsif ($sanitise_quote eq $c) {
1349 $sanitise_quote = '';
1350 }
1351 }
1352
1353 #print "c<$c> SQ<$sanitise_quote>\n";
1354 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1355 substr($res, $off, 1, $;);
1356 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1357 substr($res, $off, 1, $;);
1358 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1359 substr($res, $off, 1, 'X');
1360 } else {
1361 substr($res, $off, 1, $c);
1362 }
1363 }
1364
1365 if ($sanitise_quote eq '//') {
1366 $sanitise_quote = '';
1367 }
1368
1369 # The pathname on a #include may be surrounded by '<' and '>'.
1370 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1371 my $clean = 'X' x length($1);
1372 $res =~ s@\<.*\>@<$clean>@;
1373
1374 # The whole of a #error is a string.
1375 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1376 my $clean = 'X' x length($1);
1377 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1378 }
1379
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001380 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1381 my $match = $1;
1382 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1383 }
1384
Joe Hershberger05622192011-10-18 10:06:59 +00001385 return $res;
1386}
1387
Tom Rini6b9709d2014-02-27 08:27:28 -05001388sub get_quoted_string {
1389 my ($line, $rawline) = @_;
1390
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001391 return "" if (!defined($line) || !defined($rawline));
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02001392 return "" if ($line !~ m/($String)/g);
Tom Rini6b9709d2014-02-27 08:27:28 -05001393 return substr($rawline, $-[0], $+[0] - $-[0]);
1394}
1395
Joe Hershberger05622192011-10-18 10:06:59 +00001396sub ctx_statement_block {
1397 my ($linenr, $remain, $off) = @_;
1398 my $line = $linenr - 1;
1399 my $blk = '';
1400 my $soff = $off;
1401 my $coff = $off - 1;
1402 my $coff_set = 0;
1403
1404 my $loff = 0;
1405
1406 my $type = '';
1407 my $level = 0;
1408 my @stack = ();
1409 my $p;
1410 my $c;
1411 my $len = 0;
1412
1413 my $remainder;
1414 while (1) {
1415 @stack = (['', 0]) if ($#stack == -1);
1416
1417 #warn "CSB: blk<$blk> remain<$remain>\n";
1418 # If we are about to drop off the end, pull in more
1419 # context.
1420 if ($off >= $len) {
1421 for (; $remain > 0; $line++) {
1422 last if (!defined $lines[$line]);
1423 next if ($lines[$line] =~ /^-/);
1424 $remain--;
1425 $loff = $len;
1426 $blk .= $lines[$line] . "\n";
1427 $len = length($blk);
1428 $line++;
1429 last;
1430 }
1431 # Bail if there is no further context.
1432 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1433 if ($off >= $len) {
1434 last;
1435 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001436 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1437 $level++;
1438 $type = '#';
1439 }
Joe Hershberger05622192011-10-18 10:06:59 +00001440 }
1441 $p = $c;
1442 $c = substr($blk, $off, 1);
1443 $remainder = substr($blk, $off);
1444
1445 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1446
1447 # Handle nested #if/#else.
1448 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1449 push(@stack, [ $type, $level ]);
1450 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1451 ($type, $level) = @{$stack[$#stack - 1]};
1452 } elsif ($remainder =~ /^#\s*endif\b/) {
1453 ($type, $level) = @{pop(@stack)};
1454 }
1455
1456 # Statement ends at the ';' or a close '}' at the
1457 # outermost level.
1458 if ($level == 0 && $c eq ';') {
1459 last;
1460 }
1461
1462 # An else is really a conditional as long as its not else if
1463 if ($level == 0 && $coff_set == 0 &&
1464 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1465 $remainder =~ /^(else)(?:\s|{)/ &&
1466 $remainder !~ /^else\s+if\b/) {
1467 $coff = $off + length($1) - 1;
1468 $coff_set = 1;
1469 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1470 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1471 }
1472
1473 if (($type eq '' || $type eq '(') && $c eq '(') {
1474 $level++;
1475 $type = '(';
1476 }
1477 if ($type eq '(' && $c eq ')') {
1478 $level--;
1479 $type = ($level != 0)? '(' : '';
1480
1481 if ($level == 0 && $coff < $soff) {
1482 $coff = $off;
1483 $coff_set = 1;
1484 #warn "CSB: mark coff<$coff>\n";
1485 }
1486 }
1487 if (($type eq '' || $type eq '{') && $c eq '{') {
1488 $level++;
1489 $type = '{';
1490 }
1491 if ($type eq '{' && $c eq '}') {
1492 $level--;
1493 $type = ($level != 0)? '{' : '';
1494
1495 if ($level == 0) {
1496 if (substr($blk, $off + 1, 1) eq ';') {
1497 $off++;
1498 }
1499 last;
1500 }
1501 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001502 # Preprocessor commands end at the newline unless escaped.
1503 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1504 $level--;
1505 $type = '';
1506 $off++;
1507 last;
1508 }
Joe Hershberger05622192011-10-18 10:06:59 +00001509 $off++;
1510 }
1511 # We are truly at the end, so shuffle to the next line.
1512 if ($off == $len) {
1513 $loff = $len + 1;
1514 $line++;
1515 $remain--;
1516 }
1517
1518 my $statement = substr($blk, $soff, $off - $soff + 1);
1519 my $condition = substr($blk, $soff, $coff - $soff + 1);
1520
1521 #warn "STATEMENT<$statement>\n";
1522 #warn "CONDITION<$condition>\n";
1523
1524 #print "coff<$coff> soff<$off> loff<$loff>\n";
1525
1526 return ($statement, $condition,
1527 $line, $remain + 1, $off - $loff + 1, $level);
1528}
1529
1530sub statement_lines {
1531 my ($stmt) = @_;
1532
1533 # Strip the diff line prefixes and rip blank lines at start and end.
1534 $stmt =~ s/(^|\n)./$1/g;
1535 $stmt =~ s/^\s*//;
1536 $stmt =~ s/\s*$//;
1537
1538 my @stmt_lines = ($stmt =~ /\n/g);
1539
1540 return $#stmt_lines + 2;
1541}
1542
1543sub statement_rawlines {
1544 my ($stmt) = @_;
1545
1546 my @stmt_lines = ($stmt =~ /\n/g);
1547
1548 return $#stmt_lines + 2;
1549}
1550
1551sub statement_block_size {
1552 my ($stmt) = @_;
1553
1554 $stmt =~ s/(^|\n)./$1/g;
1555 $stmt =~ s/^\s*{//;
1556 $stmt =~ s/}\s*$//;
1557 $stmt =~ s/^\s*//;
1558 $stmt =~ s/\s*$//;
1559
1560 my @stmt_lines = ($stmt =~ /\n/g);
1561 my @stmt_statements = ($stmt =~ /;/g);
1562
1563 my $stmt_lines = $#stmt_lines + 2;
1564 my $stmt_statements = $#stmt_statements + 1;
1565
1566 if ($stmt_lines > $stmt_statements) {
1567 return $stmt_lines;
1568 } else {
1569 return $stmt_statements;
1570 }
1571}
1572
1573sub ctx_statement_full {
1574 my ($linenr, $remain, $off) = @_;
1575 my ($statement, $condition, $level);
1576
1577 my (@chunks);
1578
1579 # Grab the first conditional/block pair.
1580 ($statement, $condition, $linenr, $remain, $off, $level) =
1581 ctx_statement_block($linenr, $remain, $off);
1582 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1583 push(@chunks, [ $condition, $statement ]);
1584 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1585 return ($level, $linenr, @chunks);
1586 }
1587
1588 # Pull in the following conditional/block pairs and see if they
1589 # could continue the statement.
1590 for (;;) {
1591 ($statement, $condition, $linenr, $remain, $off, $level) =
1592 ctx_statement_block($linenr, $remain, $off);
1593 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1594 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1595 #print "C: push\n";
1596 push(@chunks, [ $condition, $statement ]);
1597 }
1598
1599 return ($level, $linenr, @chunks);
1600}
1601
1602sub ctx_block_get {
1603 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1604 my $line;
1605 my $start = $linenr - 1;
1606 my $blk = '';
1607 my @o;
1608 my @c;
1609 my @res = ();
1610
1611 my $level = 0;
1612 my @stack = ($level);
1613 for ($line = $start; $remain > 0; $line++) {
1614 next if ($rawlines[$line] =~ /^-/);
1615 $remain--;
1616
1617 $blk .= $rawlines[$line];
1618
1619 # Handle nested #if/#else.
1620 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1621 push(@stack, $level);
1622 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1623 $level = $stack[$#stack - 1];
1624 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1625 $level = pop(@stack);
1626 }
1627
1628 foreach my $c (split(//, $lines[$line])) {
1629 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1630 if ($off > 0) {
1631 $off--;
1632 next;
1633 }
1634
1635 if ($c eq $close && $level > 0) {
1636 $level--;
1637 last if ($level == 0);
1638 } elsif ($c eq $open) {
1639 $level++;
1640 }
1641 }
1642
1643 if (!$outer || $level <= 1) {
1644 push(@res, $rawlines[$line]);
1645 }
1646
1647 last if ($level == 0);
1648 }
1649
1650 return ($level, @res);
1651}
1652sub ctx_block_outer {
1653 my ($linenr, $remain) = @_;
1654
1655 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1656 return @r;
1657}
1658sub ctx_block {
1659 my ($linenr, $remain) = @_;
1660
1661 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1662 return @r;
1663}
1664sub ctx_statement {
1665 my ($linenr, $remain, $off) = @_;
1666
1667 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1668 return @r;
1669}
1670sub ctx_block_level {
1671 my ($linenr, $remain) = @_;
1672
1673 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1674}
1675sub ctx_statement_level {
1676 my ($linenr, $remain, $off) = @_;
1677
1678 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1679}
1680
1681sub ctx_locate_comment {
1682 my ($first_line, $end_line) = @_;
1683
1684 # Catch a comment on the end of the line itself.
1685 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1686 return $current_comment if (defined $current_comment);
1687
1688 # Look through the context and try and figure out if there is a
1689 # comment.
1690 my $in_comment = 0;
1691 $current_comment = '';
1692 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1693 my $line = $rawlines[$linenr - 1];
1694 #warn " $line\n";
1695 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1696 $in_comment = 1;
1697 }
1698 if ($line =~ m@/\*@) {
1699 $in_comment = 1;
1700 }
1701 if (!$in_comment && $current_comment ne '') {
1702 $current_comment = '';
1703 }
1704 $current_comment .= $line . "\n" if ($in_comment);
1705 if ($line =~ m@\*/@) {
1706 $in_comment = 0;
1707 }
1708 }
1709
1710 chomp($current_comment);
1711 return($current_comment);
1712}
1713sub ctx_has_comment {
1714 my ($first_line, $end_line) = @_;
1715 my $cmt = ctx_locate_comment($first_line, $end_line);
1716
1717 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1718 ##print "CMMT: $cmt\n";
1719
1720 return ($cmt ne '');
1721}
1722
1723sub raw_line {
1724 my ($linenr, $cnt) = @_;
1725
1726 my $offset = $linenr - 1;
1727 $cnt++;
1728
1729 my $line;
1730 while ($cnt) {
1731 $line = $rawlines[$offset++];
1732 next if (defined($line) && $line =~ /^-/);
1733 $cnt--;
1734 }
1735
1736 return $line;
1737}
1738
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02001739sub get_stat_real {
1740 my ($linenr, $lc) = @_;
1741
1742 my $stat_real = raw_line($linenr, 0);
1743 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1744 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1745 }
1746
1747 return $stat_real;
1748}
1749
1750sub get_stat_here {
1751 my ($linenr, $cnt, $here) = @_;
1752
1753 my $herectx = $here . "\n";
1754 for (my $n = 0; $n < $cnt; $n++) {
1755 $herectx .= raw_line($linenr, $n) . "\n";
1756 }
1757
1758 return $herectx;
1759}
1760
Joe Hershberger05622192011-10-18 10:06:59 +00001761sub cat_vet {
1762 my ($vet) = @_;
1763 my ($res, $coded);
1764
1765 $res = '';
1766 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1767 $res .= $1;
1768 if ($2 ne '') {
1769 $coded = sprintf("^%c", unpack('C', $2) + 64);
1770 $res .= $coded;
1771 }
1772 }
1773 $res =~ s/$/\$/;
1774
1775 return $res;
1776}
1777
1778my $av_preprocessor = 0;
1779my $av_pending;
1780my @av_paren_type;
1781my $av_pend_colon;
1782
1783sub annotate_reset {
1784 $av_preprocessor = 0;
1785 $av_pending = '_';
1786 @av_paren_type = ('E');
1787 $av_pend_colon = 'O';
1788}
1789
1790sub annotate_values {
1791 my ($stream, $type) = @_;
1792
1793 my $res;
1794 my $var = '_' x length($stream);
1795 my $cur = $stream;
1796
1797 print "$stream\n" if ($dbg_values > 1);
1798
1799 while (length($cur)) {
1800 @av_paren_type = ('E') if ($#av_paren_type < 0);
1801 print " <" . join('', @av_paren_type) .
1802 "> <$type> <$av_pending>" if ($dbg_values > 1);
1803 if ($cur =~ /^(\s+)/o) {
1804 print "WS($1)\n" if ($dbg_values > 1);
1805 if ($1 =~ /\n/ && $av_preprocessor) {
1806 $type = pop(@av_paren_type);
1807 $av_preprocessor = 0;
1808 }
1809
1810 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1811 print "CAST($1)\n" if ($dbg_values > 1);
1812 push(@av_paren_type, $type);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00001813 $type = 'c';
Joe Hershberger05622192011-10-18 10:06:59 +00001814
1815 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1816 print "DECLARE($1)\n" if ($dbg_values > 1);
1817 $type = 'T';
1818
1819 } elsif ($cur =~ /^($Modifier)\s*/) {
1820 print "MODIFIER($1)\n" if ($dbg_values > 1);
1821 $type = 'T';
1822
1823 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1824 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1825 $av_preprocessor = 1;
1826 push(@av_paren_type, $type);
1827 if ($2 ne '') {
1828 $av_pending = 'N';
1829 }
1830 $type = 'E';
1831
1832 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1833 print "UNDEF($1)\n" if ($dbg_values > 1);
1834 $av_preprocessor = 1;
1835 push(@av_paren_type, $type);
1836
1837 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1838 print "PRE_START($1)\n" if ($dbg_values > 1);
1839 $av_preprocessor = 1;
1840
1841 push(@av_paren_type, $type);
1842 push(@av_paren_type, $type);
1843 $type = 'E';
1844
1845 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1846 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1847 $av_preprocessor = 1;
1848
1849 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1850
1851 $type = 'E';
1852
1853 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1854 print "PRE_END($1)\n" if ($dbg_values > 1);
1855
1856 $av_preprocessor = 1;
1857
1858 # Assume all arms of the conditional end as this
1859 # one does, and continue as if the #endif was not here.
1860 pop(@av_paren_type);
1861 push(@av_paren_type, $type);
1862 $type = 'E';
1863
1864 } elsif ($cur =~ /^(\\\n)/o) {
1865 print "PRECONT($1)\n" if ($dbg_values > 1);
1866
1867 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1868 print "ATTR($1)\n" if ($dbg_values > 1);
1869 $av_pending = $type;
1870 $type = 'N';
1871
1872 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1873 print "SIZEOF($1)\n" if ($dbg_values > 1);
1874 if (defined $2) {
1875 $av_pending = 'V';
1876 }
1877 $type = 'N';
1878
1879 } elsif ($cur =~ /^(if|while|for)\b/o) {
1880 print "COND($1)\n" if ($dbg_values > 1);
1881 $av_pending = 'E';
1882 $type = 'N';
1883
1884 } elsif ($cur =~/^(case)/o) {
1885 print "CASE($1)\n" if ($dbg_values > 1);
1886 $av_pend_colon = 'C';
1887 $type = 'N';
1888
1889 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1890 print "KEYWORD($1)\n" if ($dbg_values > 1);
1891 $type = 'N';
1892
1893 } elsif ($cur =~ /^(\()/o) {
1894 print "PAREN('$1')\n" if ($dbg_values > 1);
1895 push(@av_paren_type, $av_pending);
1896 $av_pending = '_';
1897 $type = 'N';
1898
1899 } elsif ($cur =~ /^(\))/o) {
1900 my $new_type = pop(@av_paren_type);
1901 if ($new_type ne '_') {
1902 $type = $new_type;
1903 print "PAREN('$1') -> $type\n"
1904 if ($dbg_values > 1);
1905 } else {
1906 print "PAREN('$1')\n" if ($dbg_values > 1);
1907 }
1908
1909 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1910 print "FUNC($1)\n" if ($dbg_values > 1);
1911 $type = 'V';
1912 $av_pending = 'V';
1913
1914 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1915 if (defined $2 && $type eq 'C' || $type eq 'T') {
1916 $av_pend_colon = 'B';
1917 } elsif ($type eq 'E') {
1918 $av_pend_colon = 'L';
1919 }
1920 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1921 $type = 'V';
1922
1923 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1924 print "IDENT($1)\n" if ($dbg_values > 1);
1925 $type = 'V';
1926
1927 } elsif ($cur =~ /^($Assignment)/o) {
1928 print "ASSIGN($1)\n" if ($dbg_values > 1);
1929 $type = 'N';
1930
1931 } elsif ($cur =~/^(;|{|})/) {
1932 print "END($1)\n" if ($dbg_values > 1);
1933 $type = 'E';
1934 $av_pend_colon = 'O';
1935
1936 } elsif ($cur =~/^(,)/) {
1937 print "COMMA($1)\n" if ($dbg_values > 1);
1938 $type = 'C';
1939
1940 } elsif ($cur =~ /^(\?)/o) {
1941 print "QUESTION($1)\n" if ($dbg_values > 1);
1942 $type = 'N';
1943
1944 } elsif ($cur =~ /^(:)/o) {
1945 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1946
1947 substr($var, length($res), 1, $av_pend_colon);
1948 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1949 $type = 'E';
1950 } else {
1951 $type = 'N';
1952 }
1953 $av_pend_colon = 'O';
1954
1955 } elsif ($cur =~ /^(\[)/o) {
1956 print "CLOSE($1)\n" if ($dbg_values > 1);
1957 $type = 'N';
1958
1959 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1960 my $variant;
1961
1962 print "OPV($1)\n" if ($dbg_values > 1);
1963 if ($type eq 'V') {
1964 $variant = 'B';
1965 } else {
1966 $variant = 'U';
1967 }
1968
1969 substr($var, length($res), 1, $variant);
1970 $type = 'N';
1971
1972 } elsif ($cur =~ /^($Operators)/o) {
1973 print "OP($1)\n" if ($dbg_values > 1);
1974 if ($1 ne '++' && $1 ne '--') {
1975 $type = 'N';
1976 }
1977
1978 } elsif ($cur =~ /(^.)/o) {
1979 print "C($1)\n" if ($dbg_values > 1);
1980 }
1981 if (defined $1) {
1982 $cur = substr($cur, length($1));
1983 $res .= $type x length($1);
1984 }
1985 }
1986
1987 return ($res, $var);
1988}
1989
1990sub possible {
1991 my ($possible, $line) = @_;
1992 my $notPermitted = qr{(?:
1993 ^(?:
1994 $Modifier|
1995 $Storage|
1996 $Type|
1997 DEFINE_\S+
1998 )$|
1999 ^(?:
2000 goto|
2001 return|
2002 case|
2003 else|
2004 asm|__asm__|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002005 do|
2006 \#|
2007 \#\#|
Joe Hershberger05622192011-10-18 10:06:59 +00002008 )(?:\s|$)|
2009 ^(?:typedef|struct|enum)\b
2010 )}x;
2011 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2012 if ($possible !~ $notPermitted) {
2013 # Check for modifiers.
2014 $possible =~ s/\s*$Storage\s*//g;
2015 $possible =~ s/\s*$Sparse\s*//g;
2016 if ($possible =~ /^\s*$/) {
2017
2018 } elsif ($possible =~ /\s/) {
2019 $possible =~ s/\s*$Type\s*//g;
2020 for my $modifier (split(' ', $possible)) {
2021 if ($modifier !~ $notPermitted) {
2022 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002023 push(@modifierListFile, $modifier);
Joe Hershberger05622192011-10-18 10:06:59 +00002024 }
2025 }
2026
2027 } else {
2028 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002029 push(@typeListFile, $possible);
Joe Hershberger05622192011-10-18 10:06:59 +00002030 }
2031 build_types();
2032 } else {
2033 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2034 }
2035}
2036
2037my $prefix = '';
2038
2039sub show_type {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002040 my ($type) = @_;
Tom Rini6b9709d2014-02-27 08:27:28 -05002041
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002042 $type =~ tr/[a-z]/[A-Z]/;
2043
2044 return defined $use_type{$type} if (scalar keys %use_type > 0);
2045
2046 return !defined $ignore_type{$type};
Joe Hershberger05622192011-10-18 10:06:59 +00002047}
2048
2049sub report {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002050 my ($level, $type, $msg) = @_;
2051
2052 if (!show_type($type) ||
2053 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002054 return 0;
2055 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002056 my $output = '';
2057 if ($color) {
2058 if ($level eq 'ERROR') {
2059 $output .= RED;
2060 } elsif ($level eq 'WARNING') {
2061 $output .= YELLOW;
2062 } else {
2063 $output .= GREEN;
2064 }
Joe Hershberger05622192011-10-18 10:06:59 +00002065 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002066 $output .= $prefix . $level . ':';
2067 if ($show_types) {
2068 $output .= BLUE if ($color);
2069 $output .= "$type:";
2070 }
2071 $output .= RESET if ($color);
2072 $output .= ' ' . $msg . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00002073
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002074 if ($showfile) {
2075 my @lines = split("\n", $output, -1);
2076 splice(@lines, 1, 1);
2077 $output = join("\n", @lines);
2078 }
2079 $output = (split('\n', $output))[0] . "\n" if ($terse);
2080
2081 push(our @report, $output);
Joe Hershberger05622192011-10-18 10:06:59 +00002082
2083 return 1;
2084}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002085
Joe Hershberger05622192011-10-18 10:06:59 +00002086sub report_dump {
2087 our @report;
2088}
2089
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002090sub fixup_current_range {
2091 my ($lineRef, $offset, $length) = @_;
2092
2093 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2094 my $o = $1;
2095 my $l = $2;
2096 my $no = $o + $offset;
2097 my $nl = $l + $length;
2098 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2099 }
2100}
2101
2102sub fix_inserted_deleted_lines {
2103 my ($linesRef, $insertedRef, $deletedRef) = @_;
2104
2105 my $range_last_linenr = 0;
2106 my $delta_offset = 0;
2107
2108 my $old_linenr = 0;
2109 my $new_linenr = 0;
2110
2111 my $next_insert = 0;
2112 my $next_delete = 0;
2113
2114 my @lines = ();
2115
2116 my $inserted = @{$insertedRef}[$next_insert++];
2117 my $deleted = @{$deletedRef}[$next_delete++];
2118
2119 foreach my $old_line (@{$linesRef}) {
2120 my $save_line = 1;
2121 my $line = $old_line; #don't modify the array
2122 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2123 $delta_offset = 0;
2124 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2125 $range_last_linenr = $new_linenr;
2126 fixup_current_range(\$line, $delta_offset, 0);
2127 }
2128
2129 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2130 $deleted = @{$deletedRef}[$next_delete++];
2131 $save_line = 0;
2132 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2133 }
2134
2135 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2136 push(@lines, ${$inserted}{'LINE'});
2137 $inserted = @{$insertedRef}[$next_insert++];
2138 $new_linenr++;
2139 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2140 }
2141
2142 if ($save_line) {
2143 push(@lines, $line);
2144 $new_linenr++;
2145 }
2146
2147 $old_linenr++;
2148 }
2149
2150 return @lines;
2151}
2152
2153sub fix_insert_line {
2154 my ($linenr, $line) = @_;
2155
2156 my $inserted = {
2157 LINENR => $linenr,
2158 LINE => $line,
2159 };
2160 push(@fixed_inserted, $inserted);
2161}
2162
2163sub fix_delete_line {
2164 my ($linenr, $line) = @_;
2165
2166 my $deleted = {
2167 LINENR => $linenr,
2168 LINE => $line,
2169 };
2170
2171 push(@fixed_deleted, $deleted);
2172}
2173
Joe Hershberger05622192011-10-18 10:06:59 +00002174sub ERROR {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002175 my ($type, $msg) = @_;
2176
2177 if (report("ERROR", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002178 our $clean = 0;
2179 our $cnt_error++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002180 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002181 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002182 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002183}
2184sub WARN {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002185 my ($type, $msg) = @_;
2186
2187 if (report("WARNING", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002188 our $clean = 0;
2189 our $cnt_warn++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002190 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002191 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002192 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002193}
2194sub CHK {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002195 my ($type, $msg) = @_;
2196
2197 if ($check && report("CHECK", $type, $msg)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002198 our $clean = 0;
2199 our $cnt_chk++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002200 return 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002201 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002202 return 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002203}
2204
2205sub check_absolute_file {
2206 my ($absolute, $herecurr) = @_;
2207 my $file = $absolute;
2208
2209 ##print "absolute<$absolute>\n";
2210
2211 # See if any suffix of this path is a path within the tree.
2212 while ($file =~ s@^[^/]*/@@) {
2213 if (-f "$root/$file") {
2214 ##print "file<$file>\n";
2215 last;
2216 }
2217 }
2218 if (! -f _) {
2219 return 0;
2220 }
2221
2222 # It is, so see if the prefix is acceptable.
2223 my $prefix = $absolute;
2224 substr($prefix, -length($file)) = '';
2225
2226 ##print "prefix<$prefix>\n";
2227 if ($prefix ne ".../") {
2228 WARN("USE_RELATIVE_PATH",
2229 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2230 }
2231}
2232
Tom Rini6b9709d2014-02-27 08:27:28 -05002233sub trim {
2234 my ($string) = @_;
2235
2236 $string =~ s/^\s+|\s+$//g;
2237
2238 return $string;
2239}
2240
2241sub ltrim {
2242 my ($string) = @_;
2243
2244 $string =~ s/^\s+//;
2245
2246 return $string;
2247}
2248
2249sub rtrim {
2250 my ($string) = @_;
2251
2252 $string =~ s/\s+$//;
2253
2254 return $string;
2255}
2256
2257sub string_find_replace {
2258 my ($string, $find, $replace) = @_;
2259
2260 $string =~ s/$find/$replace/g;
2261
2262 return $string;
2263}
2264
2265sub tabify {
2266 my ($leading) = @_;
2267
Tom Rinic57383b2020-06-16 10:29:46 -04002268 my $source_indent = $tabsize;
Tom Rini6b9709d2014-02-27 08:27:28 -05002269 my $max_spaces_before_tab = $source_indent - 1;
2270 my $spaces_to_tab = " " x $source_indent;
2271
2272 #convert leading spaces to tabs
2273 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2274 #Remove spaces before a tab
2275 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2276
2277 return "$leading";
2278}
2279
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002280sub pos_last_openparen {
2281 my ($line) = @_;
2282
2283 my $pos = 0;
2284
2285 my $opens = $line =~ tr/\(/\(/;
2286 my $closes = $line =~ tr/\)/\)/;
2287
2288 my $last_openparen = 0;
2289
2290 if (($opens == 0) || ($closes >= $opens)) {
2291 return -1;
2292 }
2293
2294 my $len = length($line);
2295
2296 for ($pos = 0; $pos < $len; $pos++) {
2297 my $string = substr($line, $pos);
2298 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2299 $pos += length($1) - 1;
2300 } elsif (substr($line, $pos, 1) eq '(') {
2301 $last_openparen = $pos;
2302 } elsif (index($string, '(') == -1) {
2303 last;
2304 }
2305 }
2306
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002307 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002308}
2309
Tom Rinic57383b2020-06-16 10:29:46 -04002310sub get_raw_comment {
2311 my ($line, $rawline) = @_;
2312 my $comment = '';
2313
2314 for my $i (0 .. (length($line) - 1)) {
2315 if (substr($line, $i, 1) eq "$;") {
2316 $comment .= substr($rawline, $i, 1);
2317 }
2318 }
2319
2320 return $comment;
2321}
2322
Simon Glassb7bbd552020-12-03 16:55:24 -07002323# Args:
2324# line: Patch line to check
2325# auto: Auto variable name, e.g. "per_child_auto"
2326# suffix: Suffix to expect on member, e.g. "_priv"
2327# warning: Warning name, e.g. "PRIV_AUTO"
2328sub u_boot_struct_name {
2329 my ($line, $auto, $suffix, $warning) = @_;
2330
2331 # Use _priv as a suffix for the device-private data struct
2332 if ($line =~ /^\+\s*\.${auto}\s*=\s*sizeof\(struct\((\w+)\).*/) {
2333 my $struct_name = $1;
2334 if ($struct_name !~ /^\w+${suffix}/) {
2335 WARN($warning, "struct \'$struct_name\' should have a ${suffix} suffix");
2336 }
2337 }
2338}
2339
Simon Glassb77df592020-05-22 16:32:36 -06002340# Checks specific to U-Boot
2341sub u_boot_line {
Simon Glass23552ba2020-07-19 10:16:01 -06002342 my ($realfile, $line, $rawline, $herecurr) = @_;
Simon Glass281236c2020-05-22 16:32:37 -06002343
2344 # ask for a test if a new uclass ID is added
2345 if ($realfile =~ /uclass-id.h/ && $line =~ /^\+/) {
2346 WARN("NEW_UCLASS",
2347 "Possible new uclass - make sure to add a sandbox driver, plus a test in test/dm/<name>.c\n" . $herecurr);
2348 }
Simon Glass7fc7d242020-05-22 16:32:38 -06002349
2350 # try to get people to use the livetree API
2351 if ($line =~ /^\+.*fdtdec_/) {
2352 WARN("LIVETREE",
2353 "Use the livetree API (dev_read_...)\n" . $herecurr);
2354 }
Simon Glass58978112020-05-22 16:32:39 -06002355
2356 # add tests for new commands
2357 if ($line =~ /^\+.*do_($Ident)\(struct cmd_tbl.*/) {
2358 WARN("CMD_TEST",
2359 "Possible new command - make sure you add a test\n" . $herecurr);
2360 }
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002361
2362 # use if instead of #if
Simon Glass8af45b12020-06-14 10:54:08 -06002363 if ($realfile =~ /\.c$/ && $line =~ /^\+#if.*CONFIG.*/) {
Simon Glassdd5b0fa2020-05-22 16:32:40 -06002364 WARN("PREFER_IF",
2365 "Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible\n" . $herecurr);
2366 }
Tom Rinif3e2ebe2020-05-26 14:29:02 -04002367
2368 # use defconfig to manage CONFIG_CMD options
2369 if ($line =~ /\+\s*#\s*(define|undef)\s+(CONFIG_CMD\w*)\b/) {
2370 ERROR("DEFINE_CONFIG_CMD",
2371 "All commands are managed by Kconfig\n" . $herecurr);
2372 }
Simon Glass23552ba2020-07-19 10:16:01 -06002373
2374 # Don't put common.h and dm.h in header files
2375 if ($realfile =~ /\.h$/ && $rawline =~ /^\+#include\s*<(common|dm)\.h>*/) {
2376 ERROR("BARRED_INCLUDE_IN_HDR",
2377 "Avoid including common.h and dm.h in header files\n" . $herecurr);
2378 }
Tom Rini12178b52020-08-20 08:37:49 -04002379
2380 # Do not disable fdt / initrd relocation
2381 if ($rawline =~ /.*(fdt|initrd)_high=0xffffffff/) {
2382 ERROR("DISABLE_FDT_OR_INITRD_RELOC",
2383 "fdt or initrd relocation disabled at boot time\n" . $herecurr);
2384 }
Alper Nebi Yasakb9cca2c2020-10-05 09:57:30 +03002385
2386 # Do not use CONFIG_ prefix in CONFIG_IS_ENABLED() calls
2387 if ($line =~ /^\+.*CONFIG_IS_ENABLED\(CONFIG_\w*\).*/) {
2388 ERROR("CONFIG_IS_ENABLED_CONFIG",
2389 "CONFIG_IS_ENABLED() takes values without the CONFIG_ prefix\n" . $herecurr);
2390 }
Simon Glassb7bbd552020-12-03 16:55:24 -07002391
2392 # Use _priv as a suffix for the device-private data struct
2393 if ($line =~ /^\+\s*\.priv_auto\s*=\s*sizeof\(struct\((\w+)\).*/) {
2394 my $struct_name = $1;
2395 if ($struct_name !~ /^\w+_priv/) {
2396 WARN("PRIV_AUTO", "struct \'$struct_name\' should have a _priv suffix");
2397 }
2398 }
2399
2400 # Check struct names for the 'auto' members of struct driver
2401 u_boot_struct_name($line, "priv_auto", "_priv", "PRIV_AUTO");
2402 u_boot_struct_name($line, "plat_auto", "_plat", "PLAT_AUTO");
2403 u_boot_struct_name($line, "per_child_auto", "_priv", "CHILD_PRIV_AUTO");
2404 u_boot_struct_name($line, "per_child_plat_auto", "_plat",
2405 "CHILD_PLAT_AUTO");
2406
2407 # Now the ones for struct uclass, skipping those in common with above
2408 u_boot_struct_name($line, "per_device_auto", "_priv",
2409 "DEVICE_PRIV_AUTO");
2410 u_boot_struct_name($line, "per_device_plat_auto", "_plat",
2411 "DEVICE_PLAT_AUTO");
Simon Glassb77df592020-05-22 16:32:36 -06002412}
2413
Joe Hershberger05622192011-10-18 10:06:59 +00002414sub process {
2415 my $filename = shift;
2416
2417 my $linenr=0;
2418 my $prevline="";
2419 my $prevrawline="";
2420 my $stashline="";
2421 my $stashrawline="";
2422
2423 my $length;
2424 my $indent;
2425 my $previndent=0;
2426 my $stashindent=0;
2427
2428 our $clean = 1;
2429 my $signoff = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002430 my $author = '';
2431 my $authorsignoff = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002432 my $is_patch = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002433 my $is_binding_patch = -1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002434 my $in_header_lines = $file ? 0 : 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002435 my $in_commit_log = 0; #Scanning lines before patch
Tom Rinic57383b2020-06-16 10:29:46 -04002436 my $has_patch_separator = 0; #Found a --- line
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002437 my $has_commit_log = 0; #Encountered lines before patch
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002438 my $commit_log_lines = 0; #Number of commit log lines
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002439 my $commit_log_possible_stack_dump = 0;
2440 my $commit_log_long_line = 0;
2441 my $commit_log_has_diff = 0;
2442 my $reported_maintainer_file = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002443 my $non_utf8_charset = 0;
2444
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002445 my $last_blank_line = 0;
2446 my $last_coalesced_string_linenr = -1;
2447
Joe Hershberger05622192011-10-18 10:06:59 +00002448 our @report = ();
2449 our $cnt_lines = 0;
2450 our $cnt_error = 0;
2451 our $cnt_warn = 0;
2452 our $cnt_chk = 0;
2453
2454 # Trace the real file/line as we go.
2455 my $realfile = '';
2456 my $realline = 0;
2457 my $realcnt = 0;
2458 my $here = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002459 my $context_function; #undef'd unless there's a known function
Joe Hershberger05622192011-10-18 10:06:59 +00002460 my $in_comment = 0;
2461 my $comment_edge = 0;
2462 my $first_line = 0;
2463 my $p1_prefix = '';
2464
2465 my $prev_values = 'E';
2466
2467 # suppression flags
2468 my %suppress_ifbraces;
2469 my %suppress_whiletrailers;
2470 my %suppress_export;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002471 my $suppress_statement = 0;
2472
Tom Rini6b9709d2014-02-27 08:27:28 -05002473 my %signatures = ();
Joe Hershberger05622192011-10-18 10:06:59 +00002474
2475 # Pre-scan the patch sanitizing the lines.
2476 # Pre-scan the patch looking for any __setup documentation.
2477 #
2478 my @setup_docs = ();
2479 my $setup_docs = 0;
2480
Tom Rini6b9709d2014-02-27 08:27:28 -05002481 my $camelcase_file_seeded = 0;
2482
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002483 my $checklicenseline = 1;
2484
Joe Hershberger05622192011-10-18 10:06:59 +00002485 sanitise_line_reset();
2486 my $line;
2487 foreach my $rawline (@rawlines) {
2488 $linenr++;
2489 $line = $rawline;
2490
Tom Rini6b9709d2014-02-27 08:27:28 -05002491 push(@fixed, $rawline) if ($fix);
2492
Joe Hershberger05622192011-10-18 10:06:59 +00002493 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2494 $setup_docs = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002495 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Joe Hershberger05622192011-10-18 10:06:59 +00002496 $setup_docs = 1;
2497 }
2498 #next;
2499 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002500 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002501 $realline=$1-1;
2502 if (defined $2) {
2503 $realcnt=$3+1;
2504 } else {
2505 $realcnt=1+1;
2506 }
2507 $in_comment = 0;
2508
2509 # Guestimate if this is a continuing comment. Run
2510 # the context looking for a comment "edge". If this
2511 # edge is a close comment then we must be in a comment
2512 # at context start.
2513 my $edge;
2514 my $cnt = $realcnt;
2515 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2516 next if (defined $rawlines[$ln - 1] &&
2517 $rawlines[$ln - 1] =~ /^-/);
2518 $cnt--;
2519 #print "RAW<$rawlines[$ln - 1]>\n";
2520 last if (!defined $rawlines[$ln - 1]);
2521 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2522 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2523 ($edge) = $1;
2524 last;
2525 }
2526 }
2527 if (defined $edge && $edge eq '*/') {
2528 $in_comment = 1;
2529 }
2530
2531 # Guestimate if this is a continuing comment. If this
2532 # is the start of a diff block and this line starts
2533 # ' *' then it is very likely a comment.
2534 if (!defined $edge &&
2535 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2536 {
2537 $in_comment = 1;
2538 }
2539
2540 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2541 sanitise_line_reset($in_comment);
2542
2543 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2544 # Standardise the strings and chars within the input to
2545 # simplify matching -- only bother with positive lines.
2546 $line = sanitise_line($rawline);
2547 }
2548 push(@lines, $line);
2549
2550 if ($realcnt > 1) {
2551 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2552 } else {
2553 $realcnt = 0;
2554 }
2555
2556 #print "==>$rawline\n";
2557 #print "-->$line\n";
2558
2559 if ($setup_docs && $line =~ /^\+/) {
2560 push(@setup_docs, $line);
2561 }
2562 }
2563
2564 $prefix = '';
2565
2566 $realcnt = 0;
2567 $linenr = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002568 $fixlinenr = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00002569 foreach my $line (@lines) {
2570 $linenr++;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002571 $fixlinenr++;
Tom Rini6b9709d2014-02-27 08:27:28 -05002572 my $sline = $line; #copy of $line
2573 $sline =~ s/$;/ /g; #with comments as spaces
Joe Hershberger05622192011-10-18 10:06:59 +00002574
2575 my $rawline = $rawlines[$linenr - 1];
Tom Rinic57383b2020-06-16 10:29:46 -04002576 my $raw_comment = get_raw_comment($line, $rawline);
Joe Hershberger05622192011-10-18 10:06:59 +00002577
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002578# check if it's a mode change, rename or start of a patch
2579 if (!$in_commit_log &&
2580 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2581 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2582 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2583 $is_patch = 1;
2584 }
2585
Joe Hershberger05622192011-10-18 10:06:59 +00002586#extract the line range in the file after the patch is applied
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002587 if (!$in_commit_log &&
2588 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2589 my $context = $4;
Joe Hershberger05622192011-10-18 10:06:59 +00002590 $is_patch = 1;
2591 $first_line = $linenr + 1;
2592 $realline=$1-1;
2593 if (defined $2) {
2594 $realcnt=$3+1;
2595 } else {
2596 $realcnt=1+1;
2597 }
2598 annotate_reset();
2599 $prev_values = 'E';
2600
2601 %suppress_ifbraces = ();
2602 %suppress_whiletrailers = ();
2603 %suppress_export = ();
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002604 $suppress_statement = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002605 if ($context =~ /\b(\w+)\s*\(/) {
2606 $context_function = $1;
2607 } else {
2608 undef $context_function;
2609 }
Joe Hershberger05622192011-10-18 10:06:59 +00002610 next;
2611
2612# track the line number as we move through the hunk, note that
2613# new versions of GNU diff omit the leading space on completely
2614# blank context lines so we need to count that too.
2615 } elsif ($line =~ /^( |\+|$)/) {
2616 $realline++;
2617 $realcnt-- if ($realcnt != 0);
2618
2619 # Measure the line length and indent.
2620 ($length, $indent) = line_stats($rawline);
2621
2622 # Track the previous line.
2623 ($prevline, $stashline) = ($stashline, $line);
2624 ($previndent, $stashindent) = ($stashindent, $indent);
2625 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2626
2627 #warn "line<$line>\n";
2628
2629 } elsif ($realcnt == 1) {
2630 $realcnt--;
2631 }
2632
2633 my $hunk_line = ($realcnt != 0);
2634
Joe Hershberger05622192011-10-18 10:06:59 +00002635 $here = "#$linenr: " if (!$file);
2636 $here = "#$realline: " if ($file);
2637
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002638 my $found_file = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002639 # extract the filename as it passes
2640 if ($line =~ /^diff --git.*?(\S+)$/) {
2641 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002642 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002643 $in_commit_log = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002644 $found_file = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00002645 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2646 $realfile = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05002647 $realfile =~ s@^([^/]*)/@@ if (!$file);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002648 $in_commit_log = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00002649
2650 $p1_prefix = $1;
2651 if (!$file && $tree && $p1_prefix ne '' &&
2652 -e "$root/$p1_prefix") {
2653 WARN("PATCH_PREFIX",
2654 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2655 }
2656
2657 if ($realfile =~ m@^include/asm/@) {
2658 ERROR("MODIFIED_INCLUDE_ASM",
2659 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2660 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002661 $found_file = 1;
2662 }
2663
2664#make up the handle for any error we report on this line
2665 if ($showfile) {
2666 $prefix = "$realfile:$realline: "
2667 } elsif ($emacs) {
2668 if ($file) {
2669 $prefix = "$filename:$realline: ";
2670 } else {
2671 $prefix = "$filename:$linenr: ";
2672 }
2673 }
2674
2675 if ($found_file) {
2676 if (is_maintained_obsolete($realfile)) {
2677 WARN("OBSOLETE",
2678 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2679 }
2680 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2681 $check = 1;
2682 } else {
2683 $check = $check_orig;
2684 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02002685 $checklicenseline = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002686
2687 if ($realfile !~ /^MAINTAINERS/) {
2688 my $last_binding_patch = $is_binding_patch;
2689
2690 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2691
2692 if (($last_binding_patch != -1) &&
2693 ($last_binding_patch ^ $is_binding_patch)) {
2694 WARN("DT_SPLIT_BINDING_PATCH",
2695 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2696 }
2697 }
2698
Joe Hershberger05622192011-10-18 10:06:59 +00002699 next;
2700 }
2701
2702 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2703
2704 my $hereline = "$here\n$rawline\n";
2705 my $herecurr = "$here\n$rawline\n";
2706 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2707
2708 $cnt_lines++ if ($realcnt != 0);
2709
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002710# Verify the existence of a commit log if appropriate
2711# 2 is used because a $signature is counted in $commit_log_lines
2712 if ($in_commit_log) {
2713 if ($line !~ /^\s*$/) {
2714 $commit_log_lines++; #could be a $signature
2715 }
2716 } elsif ($has_commit_log && $commit_log_lines < 2) {
2717 WARN("COMMIT_MESSAGE",
2718 "Missing commit description - Add an appropriate one\n");
2719 $commit_log_lines = 2; #warn only once
2720 }
2721
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002722# Check if the commit log has what seems like a diff which can confuse patch
2723 if ($in_commit_log && !$commit_log_has_diff &&
2724 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2725 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2726 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2727 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2728 ERROR("DIFF_IN_COMMIT_MSG",
2729 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2730 $commit_log_has_diff = 1;
2731 }
2732
Joe Hershberger05622192011-10-18 10:06:59 +00002733# Check for incorrect file permissions
2734 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2735 my $permhere = $here . "FILE: $realfile\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05002736 if ($realfile !~ m@scripts/@ &&
2737 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00002738 ERROR("EXECUTE_PERMISSIONS",
2739 "do not set execute permissions for source files\n" . $permhere);
2740 }
2741 }
2742
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002743# Check the patch for a From:
2744 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2745 $author = $1;
2746 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2747 $author =~ s/"//g;
Tom Rinic57383b2020-06-16 10:29:46 -04002748 $author = reformat_email($author);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002749 }
2750
Joe Hershberger05622192011-10-18 10:06:59 +00002751# Check the patch for a signoff:
Tom Rinic57383b2020-06-16 10:29:46 -04002752 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00002753 $signoff++;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002754 $in_commit_log = 0;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002755 if ($author ne '') {
Tom Rinic57383b2020-06-16 10:29:46 -04002756 if (same_email_addresses($1, $author)) {
2757 $authorsignoff = 1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02002758 }
2759 }
Joe Hershberger05622192011-10-18 10:06:59 +00002760 }
2761
Tom Rinic57383b2020-06-16 10:29:46 -04002762# Check for patch separator
2763 if ($line =~ /^---$/) {
2764 $has_patch_separator = 1;
2765 $in_commit_log = 0;
2766 }
2767
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002768# Check if MAINTAINERS is being updated. If so, there's probably no need to
2769# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2770 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2771 $reported_maintainer_file = 1;
2772 }
2773
Joe Hershberger05622192011-10-18 10:06:59 +00002774# Check signature styles
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002775 if (!$in_header_lines &&
2776 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Hershberger05622192011-10-18 10:06:59 +00002777 my $space_before = $1;
2778 my $sign_off = $2;
2779 my $space_after = $3;
2780 my $email = $4;
2781 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2782
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00002783 if ($sign_off !~ /$signature_tags/) {
2784 WARN("BAD_SIGN_OFF",
2785 "Non-standard signature: $sign_off\n" . $herecurr);
2786 }
Joe Hershberger05622192011-10-18 10:06:59 +00002787 if (defined $space_before && $space_before ne "") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002788 if (WARN("BAD_SIGN_OFF",
2789 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2790 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002791 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002792 "$ucfirst_sign_off $email";
2793 }
Joe Hershberger05622192011-10-18 10:06:59 +00002794 }
2795 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Tom Rini6b9709d2014-02-27 08:27:28 -05002796 if (WARN("BAD_SIGN_OFF",
2797 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2798 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002799 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002800 "$ucfirst_sign_off $email";
2801 }
2802
Joe Hershberger05622192011-10-18 10:06:59 +00002803 }
2804 if (!defined $space_after || $space_after ne " ") {
Tom Rini6b9709d2014-02-27 08:27:28 -05002805 if (WARN("BAD_SIGN_OFF",
2806 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2807 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002808 $fixed[$fixlinenr] =
Tom Rini6b9709d2014-02-27 08:27:28 -05002809 "$ucfirst_sign_off $email";
2810 }
Joe Hershberger05622192011-10-18 10:06:59 +00002811 }
2812
Tom Rinic57383b2020-06-16 10:29:46 -04002813 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
Joe Hershberger05622192011-10-18 10:06:59 +00002814 my $suggested_email = format_email(($email_name, $email_address));
2815 if ($suggested_email eq "") {
2816 ERROR("BAD_SIGN_OFF",
2817 "Unrecognized email address: '$email'\n" . $herecurr);
2818 } else {
2819 my $dequoted = $suggested_email;
2820 $dequoted =~ s/^"//;
2821 $dequoted =~ s/" </ </;
2822 # Don't force email to have quotes
2823 # Allow just an angle bracketed address
Tom Rinic57383b2020-06-16 10:29:46 -04002824 if (!same_email_addresses($email, $suggested_email)) {
Joe Hershberger05622192011-10-18 10:06:59 +00002825 WARN("BAD_SIGN_OFF",
2826 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2827 }
2828 }
Tom Rini6b9709d2014-02-27 08:27:28 -05002829
2830# Check for duplicate signatures
2831 my $sig_nospace = $line;
2832 $sig_nospace =~ s/\s//g;
2833 $sig_nospace = lc($sig_nospace);
2834 if (defined $signatures{$sig_nospace}) {
2835 WARN("BAD_SIGN_OFF",
2836 "Duplicate signature\n" . $herecurr);
2837 } else {
2838 $signatures{$sig_nospace} = 1;
2839 }
Tom Rinic57383b2020-06-16 10:29:46 -04002840
2841# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2842 if ($sign_off =~ /^co-developed-by:$/i) {
2843 if ($email eq $author) {
2844 WARN("BAD_SIGN_OFF",
2845 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2846 }
2847 if (!defined $lines[$linenr]) {
2848 WARN("BAD_SIGN_OFF",
2849 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2850 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2851 WARN("BAD_SIGN_OFF",
2852 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2853 } elsif ($1 ne $email) {
2854 WARN("BAD_SIGN_OFF",
2855 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2856 }
2857 }
Joe Hershberger05622192011-10-18 10:06:59 +00002858 }
2859
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002860# Check email subject for common tools that don't need to be mentioned
2861 if ($in_header_lines &&
2862 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2863 WARN("EMAIL_SUBJECT",
2864 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2865 }
2866
Tom Rinic57383b2020-06-16 10:29:46 -04002867# Check for Gerrit Change-Ids not in any patch context
2868 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002869 ERROR("GERRIT_CHANGE_ID",
Tom Rinic57383b2020-06-16 10:29:46 -04002870 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002871 }
2872
2873# Check if the commit log is in a possible stack dump
2874 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2875 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2876 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2877 # timestamp
Tom Rinic57383b2020-06-16 10:29:46 -04002878 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2879 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2880 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2881 # stack dump address styles
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002882 $commit_log_possible_stack_dump = 1;
2883 }
2884
2885# Check for line lengths > 75 in commit log, warn once
2886 if ($in_commit_log && !$commit_log_long_line &&
2887 length($line) > 75 &&
2888 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2889 # file delta changes
2890 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2891 # filename then :
2892 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2893 # A Fixes: or Link: line
2894 $commit_log_possible_stack_dump)) {
2895 WARN("COMMIT_LOG_LONG_LINE",
2896 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2897 $commit_log_long_line = 1;
2898 }
2899
2900# Reset possible stack dump if a blank line is found
2901 if ($in_commit_log && $commit_log_possible_stack_dump &&
2902 $line =~ /^\s*$/) {
2903 $commit_log_possible_stack_dump = 0;
2904 }
2905
2906# Check for git id commit length and improperly formed commit descriptions
2907 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Tom Rinic57383b2020-06-16 10:29:46 -04002908 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02002909 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2910 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2911 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2912 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2913 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2914 my $init_char = "c";
2915 my $orig_commit = "";
2916 my $short = 1;
2917 my $long = 0;
2918 my $case = 1;
2919 my $space = 1;
2920 my $hasdesc = 0;
2921 my $hasparens = 0;
2922 my $id = '0123456789ab';
2923 my $orig_desc = "commit description";
2924 my $description = "";
2925
2926 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2927 $init_char = $1;
2928 $orig_commit = lc($2);
2929 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2930 $orig_commit = lc($1);
2931 }
2932
2933 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2934 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2935 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2936 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2937 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2938 $orig_desc = $1;
2939 $hasparens = 1;
2940 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2941 defined $rawlines[$linenr] &&
2942 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2943 $orig_desc = $1;
2944 $hasparens = 1;
2945 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2946 defined $rawlines[$linenr] &&
2947 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2948 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2949 $orig_desc = $1;
2950 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2951 $orig_desc .= " " . $1;
2952 $hasparens = 1;
2953 }
2954
2955 ($id, $description) = git_commit_info($orig_commit,
2956 $id, $orig_desc);
2957
2958 if (defined($id) &&
2959 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2960 ERROR("GIT_COMMIT_ID",
2961 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2962 }
2963 }
2964
2965# Check for added, moved or deleted files
2966 if (!$reported_maintainer_file && !$in_commit_log &&
2967 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2968 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2969 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2970 (defined($1) || defined($2))))) {
2971 $is_patch = 1;
2972 $reported_maintainer_file = 1;
2973 WARN("FILE_PATH_CHANGES",
2974 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2975 }
2976
Tom Rinic57383b2020-06-16 10:29:46 -04002977# Check for adding new DT bindings not in schema format
2978 if (!$in_commit_log &&
2979 ($line =~ /^new file mode\s*\d+\s*$/) &&
2980 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2981 WARN("DT_SCHEMA_BINDING_PATCH",
2982 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2983 }
2984
Joe Hershberger05622192011-10-18 10:06:59 +00002985# Check for wrappage within a valid hunk of the file
2986 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2987 ERROR("CORRUPTED_PATCH",
2988 "patch seems to be corrupt (line wrapped?)\n" .
2989 $herecurr) if (!$emitted_corrupt++);
2990 }
2991
Joe Hershberger05622192011-10-18 10:06:59 +00002992# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2993 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2994 $rawline !~ m/^$UTF8*$/) {
2995 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2996
2997 my $blank = copy_spacing($rawline);
2998 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2999 my $hereptr = "$hereline$ptr\n";
3000
3001 CHK("INVALID_UTF8",
3002 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3003 }
3004
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003005# Check if it's the start of a commit log
3006# (not a header line and we haven't seen the patch filename)
3007 if ($in_header_lines && $realfile =~ /^$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003008 !($rawline =~ /^\s+(?:\S|$)/ ||
3009 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003010 $in_header_lines = 0;
3011 $in_commit_log = 1;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003012 $has_commit_log = 1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003013 }
3014
3015# Check if there is UTF-8 in a commit log when a mail header has explicitly
3016# declined it, i.e defined some charset where it is missing.
3017 if ($in_header_lines &&
3018 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3019 $1 !~ /utf-8/i) {
3020 $non_utf8_charset = 1;
3021 }
3022
3023 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3024 $rawline =~ /$NON_ASCII_UTF8/) {
3025 WARN("UTF8_BEFORE_PATCH",
3026 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3027 }
3028
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003029# Check for absolute kernel paths in commit message
3030 if ($tree && $in_commit_log) {
3031 while ($line =~ m{(?:^|\s)(/\S*)}g) {
3032 my $file = $1;
3033
3034 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3035 check_absolute_file($1, $herecurr)) {
3036 #
3037 } else {
3038 check_absolute_file($file, $herecurr);
3039 }
3040 }
3041 }
3042
Dan Murphyc10e0f52017-01-31 14:15:53 -06003043# Check for various typo / spelling mistakes
3044 if (defined($misspellings) &&
3045 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3046 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
3047 my $typo = $1;
3048 my $typo_fix = $spelling_fix{lc($typo)};
3049 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3050 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003051 my $msg_level = \&WARN;
3052 $msg_level = \&CHK if ($file);
3053 if (&{$msg_level}("TYPO_SPELLING",
3054 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
Dan Murphyc10e0f52017-01-31 14:15:53 -06003055 $fix) {
3056 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3057 }
3058 }
3059 }
3060
Tom Rinic57383b2020-06-16 10:29:46 -04003061# check for invalid commit id
3062 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3063 my $id;
3064 my $description;
3065 ($id, $description) = git_commit_info($2, undef, undef);
3066 if (!defined($id)) {
3067 WARN("UNKNOWN_COMMIT_ID",
3068 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3069 }
3070 }
3071
Joe Hershberger05622192011-10-18 10:06:59 +00003072# ignore non-hunk lines and lines being removed
3073 next if (!$hunk_line || $line =~ /^-/);
3074
3075#trailing whitespace
3076 if ($line =~ /^\+.*\015/) {
3077 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003078 if (ERROR("DOS_LINE_ENDINGS",
3079 "DOS line endings\n" . $herevet) &&
3080 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003081 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003082 }
Joe Hershberger05622192011-10-18 10:06:59 +00003083 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3084 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003085 if (ERROR("TRAILING_WHITESPACE",
3086 "trailing whitespace\n" . $herevet) &&
3087 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003088 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05003089 }
3090
Joe Hershberger05622192011-10-18 10:06:59 +00003091 $rpt_cleaners = 1;
3092 }
3093
Tom Rini6b9709d2014-02-27 08:27:28 -05003094# Check for FSF mailing addresses.
3095 if ($rawline =~ /\bwrite to the Free/i ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003096 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Tom Rini6b9709d2014-02-27 08:27:28 -05003097 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3098 $rawline =~ /\b51\s+Franklin\s+St/i) {
3099 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003100 my $msg_level = \&ERROR;
3101 $msg_level = \&CHK if ($file);
3102 &{$msg_level}("FSF_MAILING_ADDRESS",
3103 "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 -05003104 }
3105
Joe Hershberger05622192011-10-18 10:06:59 +00003106# check for Kconfig help text having a real description
3107# Only applies when adding the entry originally, after that we do not have
3108# sufficient context to determine whether it is indeed long enough.
3109 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003110 # 'choice' is usually the last thing on the line (though
3111 # Kconfig supports named choices), so use a word boundary
3112 # (\b) rather than a whitespace character (\s)
3113 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003114 my $length = 0;
3115 my $cnt = $realcnt;
3116 my $ln = $linenr + 1;
3117 my $f;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003118 my $is_start = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00003119 my $is_end = 0;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003120 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Joe Hershberger05622192011-10-18 10:06:59 +00003121 $f = $lines[$ln - 1];
3122 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3123 $is_end = $lines[$ln - 1] =~ /^\+/;
Joe Hershberger05622192011-10-18 10:06:59 +00003124
3125 next if ($f =~ /^-/);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003126 last if (!$file && $f =~ /^\@\@/);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003127
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003128 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003129 $is_start = 1;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003130 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3131 if ($lines[$ln - 1] =~ "---help---") {
3132 WARN("CONFIG_DESCRIPTION",
3133 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3134 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003135 $length = -1;
3136 }
3137
Joe Hershberger05622192011-10-18 10:06:59 +00003138 $f =~ s/^.//;
3139 $f =~ s/#.*//;
3140 $f =~ s/^\s+//;
3141 next if ($f =~ /^$/);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003142
3143 # This only checks context lines in the patch
3144 # and so hopefully shouldn't trigger false
3145 # positives, even though some of these are
3146 # common words in help texts
3147 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3148 if|endif|menu|endmenu|source)\b/x) {
Joe Hershberger05622192011-10-18 10:06:59 +00003149 $is_end = 1;
3150 last;
3151 }
3152 $length++;
3153 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003154 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3155 WARN("CONFIG_DESCRIPTION",
3156 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3157 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003158 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3159 }
3160
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003161# check for MAINTAINERS entries that don't have the right form
3162 if ($realfile =~ /^MAINTAINERS$/ &&
3163 $rawline =~ /^\+[A-Z]:/ &&
3164 $rawline !~ /^\+[A-Z]:\t\S/) {
3165 if (WARN("MAINTAINERS_STYLE",
3166 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3167 $fix) {
3168 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3169 }
3170 }
3171
3172# discourage the use of boolean for type definition attributes of Kconfig options
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003173 if ($realfile =~ /Kconfig/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003174 $line =~ /^\+\s*\bboolean\b/) {
3175 WARN("CONFIG_TYPE_BOOLEAN",
3176 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003177 }
3178
3179 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3180 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3181 my $flag = $1;
3182 my $replacement = {
3183 'EXTRA_AFLAGS' => 'asflags-y',
3184 'EXTRA_CFLAGS' => 'ccflags-y',
3185 'EXTRA_CPPFLAGS' => 'cppflags-y',
3186 'EXTRA_LDFLAGS' => 'ldflags-y',
3187 };
3188
3189 WARN("DEPRECATED_VARIABLE",
3190 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
Joe Hershberger05622192011-10-18 10:06:59 +00003191 }
3192
Tom Rini6b9709d2014-02-27 08:27:28 -05003193# check for DT compatible documentation
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003194 if (defined $root &&
3195 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3196 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3197
Tom Rini6b9709d2014-02-27 08:27:28 -05003198 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3199
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003200 my $dt_path = $root . "/Documentation/devicetree/bindings/";
Tom Rinic57383b2020-06-16 10:29:46 -04003201 my $vp_file = $dt_path . "vendor-prefixes.yaml";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003202
Tom Rini6b9709d2014-02-27 08:27:28 -05003203 foreach my $compat (@compats) {
3204 my $compat2 = $compat;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003205 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3206 my $compat3 = $compat;
3207 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3208 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003209 if ( $? >> 8 ) {
3210 WARN("UNDOCUMENTED_DT_STRING",
3211 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3212 }
3213
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003214 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3215 my $vendor = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04003216 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
Tom Rini6b9709d2014-02-27 08:27:28 -05003217 if ( $? >> 8 ) {
3218 WARN("UNDOCUMENTED_DT_STRING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003219 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05003220 }
3221 }
3222 }
3223
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003224# check for using SPDX license tag at beginning of files
3225 if ($realline == $checklicenseline) {
3226 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3227 $checklicenseline = 2;
3228 } elsif ($rawline =~ /^\+/) {
3229 my $comment = "";
3230 if ($realfile =~ /\.(h|s|S)$/) {
3231 $comment = '/*';
3232 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3233 $comment = '//';
Tom Rinic57383b2020-06-16 10:29:46 -04003234 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003235 $comment = '#';
3236 } elsif ($realfile =~ /\.rst$/) {
3237 $comment = '..';
3238 }
3239
Tom Rinic57383b2020-06-16 10:29:46 -04003240# check SPDX comment style for .[chsS] files
3241 if ($realfile =~ /\.[chsS]$/ &&
3242 $rawline =~ /SPDX-License-Identifier:/ &&
3243 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3244 WARN("SPDX_LICENSE_TAG",
3245 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3246 }
3247
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003248 if ($comment !~ /^$/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04003249 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3250 WARN("SPDX_LICENSE_TAG",
3251 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003252 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
Tom Rinic57383b2020-06-16 10:29:46 -04003253 my $spdx_license = $1;
3254 if (!is_SPDX_License_valid($spdx_license)) {
3255 WARN("SPDX_LICENSE_TAG",
3256 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3257 }
3258 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3259 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3260 my $msg_level = \&WARN;
3261 $msg_level = \&CHK if ($file);
3262 if (&{$msg_level}("SPDX_LICENSE_TAG",
3263
3264 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3265 $fix) {
3266 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3267 }
3268 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003269 }
3270 }
3271 }
3272
Joe Hershberger05622192011-10-18 10:06:59 +00003273# check we are in a valid source file if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003274 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003275
Tom Rinic57383b2020-06-16 10:29:46 -04003276# check for using SPDX-License-Identifier on the wrong line number
3277 if ($realline != $checklicenseline &&
3278 $rawline =~ /\bSPDX-License-Identifier:/ &&
3279 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3280 WARN("SPDX_LICENSE_TAG",
3281 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3282 }
3283
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003284# line length limit (with some exclusions)
3285#
3286# There are a few types of lines that may extend beyond $max_line_length:
3287# logging functions like pr_info that end in a string
3288# lines with a single string
3289# #defines that are a single string
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003290# lines with an RFC3986 like URL
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003291#
3292# There are 3 different line length message types:
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003293# LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003294# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3295# LONG_LINE all other lines longer than $max_line_length
3296#
3297# if LONG_LINE is ignored, the other 2 types are also ignored
3298#
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003299
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003300 if ($line =~ /^\+/ && $length > $max_line_length) {
3301 my $msg_type = "LONG_LINE";
Joe Hershberger05622192011-10-18 10:06:59 +00003302
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003303 # Check the allowed long line types first
3304
3305 # logging functions that end in a string that starts
3306 # before $max_line_length
3307 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3308 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3309 $msg_type = "";
3310
3311 # lines with only strings (w/ possible termination)
3312 # #defines with only strings
3313 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3314 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3315 $msg_type = "";
3316
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003317 # More special cases
3318 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3319 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3320 $msg_type = "";
3321
3322 # URL ($rawline is used in case the URL is in a comment)
3323 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003324 $msg_type = "";
3325
3326 # Otherwise set the alternate message types
3327
3328 # a comment starts before $max_line_length
3329 } elsif ($line =~ /($;[\s$;]*)$/ &&
3330 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3331 $msg_type = "LONG_LINE_COMMENT"
3332
3333 # a quoted string starts before $max_line_length
3334 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3335 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3336 $msg_type = "LONG_LINE_STRING"
Tom Rini6b9709d2014-02-27 08:27:28 -05003337 }
3338
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003339 if ($msg_type ne "" &&
3340 (show_type("LONG_LINE") || show_type($msg_type))) {
Simon Glass048a6482020-05-22 16:32:35 -06003341 my $msg_level = \&WARN;
3342 $msg_level = \&CHK if ($file);
3343 &{$msg_level}($msg_type,
3344 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003345 }
Joe Hershberger05622192011-10-18 10:06:59 +00003346 }
3347
3348# check for adding lines without a newline.
3349 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3350 WARN("MISSING_EOF_NEWLINE",
3351 "adding a line without newline at end of file\n" . $herecurr);
3352 }
3353
Simon Glassb77df592020-05-22 16:32:36 -06003354 if ($u_boot) {
Simon Glass23552ba2020-07-19 10:16:01 -06003355 u_boot_line($realfile, $line, $rawline, $herecurr);
Simon Glassb77df592020-05-22 16:32:36 -06003356 }
3357
Joe Hershberger05622192011-10-18 10:06:59 +00003358# check we are in a valid source file C or perl if not then ignore this hunk
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003359 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Joe Hershberger05622192011-10-18 10:06:59 +00003360
3361# at the beginning of a line any tabs must come first and anything
Tom Rinic57383b2020-06-16 10:29:46 -04003362# more than $tabsize must use tabs.
Joe Hershberger05622192011-10-18 10:06:59 +00003363 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3364 $rawline =~ /^\+\s* \s*/) {
3365 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Hershberger05622192011-10-18 10:06:59 +00003366 $rpt_cleaners = 1;
Tom Rini6b9709d2014-02-27 08:27:28 -05003367 if (ERROR("CODE_INDENT",
3368 "code indent should use tabs where possible\n" . $herevet) &&
3369 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003370 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003371 }
Joe Hershberger05622192011-10-18 10:06:59 +00003372 }
3373
3374# check for space before tabs.
3375 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3376 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003377 if (WARN("SPACE_BEFORE_TAB",
3378 "please, no space before tabs\n" . $herevet) &&
3379 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003380 while ($fixed[$fixlinenr] =~
Tom Rinic57383b2020-06-16 10:29:46 -04003381 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003382 while ($fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003383 s/(^\+.*) +\t/$1\t/) {}
3384 }
Joe Hershberger05622192011-10-18 10:06:59 +00003385 }
3386
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003387# check for assignments on the start of a line
3388 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3389 CHK("ASSIGNMENT_CONTINUATIONS",
3390 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3391 }
3392
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003393# check for && or || at the start of a line
3394 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3395 CHK("LOGICAL_CONTINUATIONS",
3396 "Logical continuations should be on the previous line\n" . $hereprev);
3397 }
3398
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003399# check indentation starts on a tab stop
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003400 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003401 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003402 my $indent = length($1);
Tom Rinic57383b2020-06-16 10:29:46 -04003403 if ($indent % $tabsize) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003404 if (WARN("TABSTOP",
3405 "Statements should start on a tabstop\n" . $herecurr) &&
3406 $fix) {
Tom Rinic57383b2020-06-16 10:29:46 -04003407 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003408 }
3409 }
3410 }
3411
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003412# check multi-line statement indentation matches previous line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003413 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003414 $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 +00003415 $prevline =~ /^\+(\t*)(.*)$/;
3416 my $oldindent = $1;
3417 my $rest = $2;
3418
3419 my $pos = pos_last_openparen($rest);
3420 if ($pos >= 0) {
3421 $line =~ /^(\+| )([ \t]*)/;
3422 my $newindent = $2;
3423
3424 my $goodtabindent = $oldindent .
Tom Rinic57383b2020-06-16 10:29:46 -04003425 "\t" x ($pos / $tabsize) .
3426 " " x ($pos % $tabsize);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003427 my $goodspaceindent = $oldindent . " " x $pos;
3428
3429 if ($newindent ne $goodtabindent &&
3430 $newindent ne $goodspaceindent) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003431
3432 if (CHK("PARENTHESIS_ALIGNMENT",
3433 "Alignment should match open parenthesis\n" . $hereprev) &&
3434 $fix && $line =~ /^\+/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003435 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05003436 s/^\+[ \t]*/\+$goodtabindent/;
3437 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003438 }
3439 }
3440 }
3441
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003442# check for space after cast like "(int) foo" or "(struct foo) bar"
3443# avoid checking a few false positives:
3444# "sizeof(<type>)" or "__alignof__(<type>)"
3445# function pointer declarations like "(*foo)(int) = bar;"
3446# structure definitions like "(struct foo) { 0 };"
3447# multiline macros that define functions
3448# known attributes or the __attribute__ keyword
3449 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3450 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05003451 if (CHK("SPACING",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003452 "No space is necessary after a cast\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003453 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003454 $fixed[$fixlinenr] =~
3455 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Tom Rini6b9709d2014-02-27 08:27:28 -05003456 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003457 }
3458
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003459# Block comment styles
3460# Networking with an initial /*
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003461 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003462 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003463 $rawline =~ /^\+[ \t]*\*/ &&
3464 $realline > 2) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003465 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3466 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3467 }
3468
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003469# Block comments use * on subsequent lines
3470 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3471 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Tom Rini6b9709d2014-02-27 08:27:28 -05003472 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3473 $rawline =~ /^\+/ && #line is new
3474 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003475 WARN("BLOCK_COMMENT_STYLE",
3476 "Block comments use * on subsequent lines\n" . $hereprev);
Tom Rini6b9709d2014-02-27 08:27:28 -05003477 }
3478
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003479# Block comments use */ on trailing lines
3480 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003481 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3482 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3483 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003484 WARN("BLOCK_COMMENT_STYLE",
3485 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3486 }
3487
3488# Block comment * alignment
3489 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3490 $line =~ /^\+[ \t]*$;/ && #leading comment
3491 $rawline =~ /^\+[ \t]*\*/ && #leading *
3492 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3493 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3494 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3495 my $oldindent;
3496 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3497 if (defined($1)) {
3498 $oldindent = expand_tabs($1);
3499 } else {
3500 $prevrawline =~ m@^\+(.*/?)\*@;
3501 $oldindent = expand_tabs($1);
3502 }
3503 $rawline =~ m@^\+([ \t]*)\*@;
3504 my $newindent = $1;
3505 $newindent = expand_tabs($newindent);
3506 if (length($oldindent) ne length($newindent)) {
3507 WARN("BLOCK_COMMENT_STYLE",
3508 "Block comments should align the * on each line\n" . $hereprev);
3509 }
3510 }
3511
3512# check for missing blank lines after struct/union declarations
3513# with exceptions for various attributes and macros
3514 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3515 $line =~ /^\+/ &&
3516 !($line =~ /^\+\s*$/ ||
3517 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3518 $line =~ /^\+\s*MODULE_/i ||
3519 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3520 $line =~ /^\+[a-z_]*init/ ||
3521 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3522 $line =~ /^\+\s*DECLARE/ ||
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003523 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003524 $line =~ /^\+\s*__setup/)) {
3525 if (CHK("LINE_SPACING",
3526 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3527 $fix) {
3528 fix_insert_line($fixlinenr, "\+");
3529 }
3530 }
3531
3532# check for multiple consecutive blank lines
3533 if ($prevline =~ /^[\+ ]\s*$/ &&
3534 $line =~ /^\+\s*$/ &&
3535 $last_blank_line != ($linenr - 1)) {
3536 if (CHK("LINE_SPACING",
3537 "Please don't use multiple blank lines\n" . $hereprev) &&
3538 $fix) {
3539 fix_delete_line($fixlinenr, $rawline);
3540 }
3541
3542 $last_blank_line = $linenr;
3543 }
3544
3545# check for missing blank lines after declarations
3546 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3547 # actual declarations
3548 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3549 # function pointer declarations
3550 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3551 # foo bar; where foo is some local typedef or #define
3552 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3553 # known declaration macros
3554 $prevline =~ /^\+\s+$declaration_macros/) &&
3555 # for "else if" which can look like "$Ident $Ident"
3556 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3557 # other possible extensions of declaration lines
3558 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3559 # not starting a section or a macro "\" extended line
3560 $prevline =~ /(?:\{\s*|\\)$/) &&
3561 # looks like a declaration
3562 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3563 # function pointer declarations
3564 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3565 # foo bar; where foo is some local typedef or #define
3566 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3567 # known declaration macros
3568 $sline =~ /^\+\s+$declaration_macros/ ||
3569 # start of struct or union or enum
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02003570 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003571 # start or end of block or continuation of declaration
3572 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3573 # bitfield continuation
3574 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3575 # other possible extensions of declaration lines
3576 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3577 # indentation of previous and current line are the same
3578 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3579 if (WARN("LINE_SPACING",
3580 "Missing a blank line after declarations\n" . $hereprev) &&
3581 $fix) {
3582 fix_insert_line($fixlinenr, "\+");
3583 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003584 }
3585
Joe Hershberger05622192011-10-18 10:06:59 +00003586# check for spaces at the beginning of a line.
3587# Exceptions:
3588# 1) within comments
3589# 2) indented preprocessor commands
3590# 3) hanging labels
Tom Rini6b9709d2014-02-27 08:27:28 -05003591 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003592 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05003593 if (WARN("LEADING_SPACE",
3594 "please, no spaces at the start of a line\n" . $herevet) &&
3595 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003596 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Tom Rini6b9709d2014-02-27 08:27:28 -05003597 }
Joe Hershberger05622192011-10-18 10:06:59 +00003598 }
3599
3600# check we are in a valid C source file if not then ignore this hunk
3601 next if ($realfile !~ /\.(h|c)$/);
3602
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02003603# check for unusual line ending [ or (
3604 if ($line =~ /^\+.*([\[\(])\s*$/) {
3605 CHK("OPEN_ENDED_LINE",
3606 "Lines should not end with a '$1'\n" . $herecurr);
3607 }
3608
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003609# check if this appears to be the start function declaration, save the name
3610 if ($sline =~ /^\+\{\s*$/ &&
3611 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3612 $context_function = $1;
3613 }
3614
3615# check if this appears to be the end of function declaration
3616 if ($sline =~ /^\+\}\s*$/) {
3617 undef $context_function;
3618 }
3619
3620# check indentation of any line with a bare else
3621# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3622# if the previous line is a break or return and is indented 1 tab more...
3623 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3624 my $tabs = length($1) + 1;
3625 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3626 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3627 defined $lines[$linenr] &&
3628 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3629 WARN("UNNECESSARY_ELSE",
3630 "else is not generally useful after a break or return\n" . $hereprev);
3631 }
3632 }
3633
3634# check indentation of a line with a break;
3635# if the previous line is a goto or return and is indented the same # of tabs
3636 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3637 my $tabs = $1;
3638 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3639 WARN("UNNECESSARY_BREAK",
3640 "break is not useful after a goto or return\n" . $hereprev);
3641 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003642 }
3643
Joe Hershberger05622192011-10-18 10:06:59 +00003644# check for RCS/CVS revision markers
3645 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3646 WARN("CVS_KEYWORD",
3647 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3648 }
3649
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003650# check for old HOTPLUG __dev<foo> section markings
3651 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3652 WARN("HOTPLUG_SECTION",
3653 "Using $1 is unnecessary\n" . $herecurr);
3654 }
3655
Joe Hershberger05622192011-10-18 10:06:59 +00003656# Check for potential 'bare' types
3657 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3658 $realline_next);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003659#print "LINE<$line>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003660 if ($linenr > $suppress_statement &&
Tom Rini6b9709d2014-02-27 08:27:28 -05003661 $realcnt && $sline =~ /.\s*\S/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003662 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3663 ctx_statement_block($linenr, $realcnt, 0);
3664 $stat =~ s/\n./\n /g;
3665 $cond =~ s/\n./\n /g;
3666
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003667#print "linenr<$linenr> <$stat>\n";
3668 # If this statement has no statement boundaries within
3669 # it there is no point in retrying a statement scan
3670 # until we hit end of it.
3671 my $frag = $stat; $frag =~ s/;+\s*$//;
3672 if ($frag !~ /(?:{|;)/) {
3673#print "skip<$line_nr_next>\n";
3674 $suppress_statement = $line_nr_next;
3675 }
3676
Joe Hershberger05622192011-10-18 10:06:59 +00003677 # Find the real next line.
3678 $realline_next = $line_nr_next;
3679 if (defined $realline_next &&
3680 (!defined $lines[$realline_next - 1] ||
3681 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3682 $realline_next++;
3683 }
3684
3685 my $s = $stat;
3686 $s =~ s/{.*$//s;
3687
3688 # Ignore goto labels.
3689 if ($s =~ /$Ident:\*$/s) {
3690
3691 # Ignore functions being called
3692 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3693
3694 } elsif ($s =~ /^.\s*else\b/s) {
3695
3696 # declarations always start with types
3697 } 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) {
3698 my $type = $1;
3699 $type =~ s/\s+/ /g;
3700 possible($type, "A:" . $s);
3701
3702 # definitions in global scope can only start with types
3703 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3704 possible($1, "B:" . $s);
3705 }
3706
3707 # any (foo ... *) is a pointer cast, and foo is a type
3708 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3709 possible($1, "C:" . $s);
3710 }
3711
3712 # Check for any sort of function declaration.
3713 # int foo(something bar, other baz);
3714 # void (*store_gdt)(x86_descr_ptr *);
3715 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3716 my ($name_len) = length($1);
3717
3718 my $ctx = $s;
3719 substr($ctx, 0, $name_len + 1, '');
3720 $ctx =~ s/\)[^\)]*$//;
3721
3722 for my $arg (split(/\s*,\s*/, $ctx)) {
3723 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3724
3725 possible($1, "D:" . $s);
3726 }
3727 }
3728 }
3729
3730 }
3731
3732#
3733# Checks which may be anchored in the context.
3734#
3735
3736# Check for switch () and associated case and default
3737# statements should be at the same indent.
3738 if ($line=~/\bswitch\s*\(.*\)/) {
3739 my $err = '';
3740 my $sep = '';
3741 my @ctx = ctx_block_outer($linenr, $realcnt);
3742 shift(@ctx);
3743 for my $ctx (@ctx) {
3744 my ($clen, $cindent) = line_stats($ctx);
3745 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3746 $indent != $cindent) {
3747 $err .= "$sep$ctx\n";
3748 $sep = '';
3749 } else {
3750 $sep = "[...]\n";
3751 }
3752 }
3753 if ($err ne '') {
3754 ERROR("SWITCH_CASE_INDENT_LEVEL",
3755 "switch and case should be at the same indent\n$hereline$err");
3756 }
3757 }
3758
3759# if/while/etc brace do not go on next line, unless defining a do while loop,
3760# or if that brace on the next line is for something else
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003761 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 +00003762 my $pre_ctx = "$1$2";
3763
3764 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00003765
3766 if ($line =~ /^\+\t{6,}/) {
3767 WARN("DEEP_INDENTATION",
3768 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3769 }
3770
Joe Hershberger05622192011-10-18 10:06:59 +00003771 my $ctx_cnt = $realcnt - $#ctx - 1;
3772 my $ctx = join("\n", @ctx);
3773
3774 my $ctx_ln = $linenr;
3775 my $ctx_skip = $realcnt;
3776
3777 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3778 defined $lines[$ctx_ln - 1] &&
3779 $lines[$ctx_ln - 1] =~ /^-/)) {
3780 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3781 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3782 $ctx_ln++;
3783 }
3784
3785 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3786 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3787
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003788 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Hershberger05622192011-10-18 10:06:59 +00003789 ERROR("OPEN_BRACE",
3790 "that open brace { should be on the previous line\n" .
3791 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3792 }
3793 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3794 $ctx =~ /\)\s*\;\s*$/ &&
3795 defined $lines[$ctx_ln - 1])
3796 {
3797 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3798 if ($nindent > $indent) {
3799 WARN("TRAILING_SEMICOLON",
3800 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3801 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3802 }
3803 }
3804 }
3805
3806# Check relative indent for conditionals and blocks.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003807 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 +00003808 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3809 ctx_statement_block($linenr, $realcnt, 0)
3810 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00003811 my ($s, $c) = ($stat, $cond);
3812
3813 substr($s, 0, length($c), '');
3814
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003815 # remove inline comments
3816 $s =~ s/$;/ /g;
3817 $c =~ s/$;/ /g;
Joe Hershberger05622192011-10-18 10:06:59 +00003818
3819 # Find out how long the conditional actually is.
3820 my @newlines = ($c =~ /\n/gs);
3821 my $cond_lines = 1 + $#newlines;
3822
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003823 # Make sure we remove the line prefixes as we have
3824 # none on the first line, and are going to readd them
3825 # where necessary.
3826 $s =~ s/\n./\n/gs;
3827 while ($s =~ /\n\s+\\\n/) {
3828 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3829 }
3830
Joe Hershberger05622192011-10-18 10:06:59 +00003831 # We want to check the first line inside the block
3832 # starting at the end of the conditional, so remove:
3833 # 1) any blank line termination
3834 # 2) any opening brace { on end of the line
3835 # 3) any do (...) {
3836 my $continuation = 0;
3837 my $check = 0;
3838 $s =~ s/^.*\bdo\b//;
3839 $s =~ s/^\s*{//;
3840 if ($s =~ s/^\s*\\//) {
3841 $continuation = 1;
3842 }
3843 if ($s =~ s/^\s*?\n//) {
3844 $check = 1;
3845 $cond_lines++;
3846 }
3847
3848 # Also ignore a loop construct at the end of a
3849 # preprocessor statement.
3850 if (($prevline =~ /^.\s*#\s*define\s/ ||
3851 $prevline =~ /\\\s*$/) && $continuation == 0) {
3852 $check = 0;
3853 }
3854
3855 my $cond_ptr = -1;
3856 $continuation = 0;
3857 while ($cond_ptr != $cond_lines) {
3858 $cond_ptr = $cond_lines;
3859
3860 # If we see an #else/#elif then the code
3861 # is not linear.
3862 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3863 $check = 0;
3864 }
3865
3866 # Ignore:
3867 # 1) blank lines, they should be at 0,
3868 # 2) preprocessor lines, and
3869 # 3) labels.
3870 if ($continuation ||
3871 $s =~ /^\s*?\n/ ||
3872 $s =~ /^\s*#\s*?/ ||
3873 $s =~ /^\s*$Ident\s*:/) {
3874 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3875 if ($s =~ s/^.*?\n//) {
3876 $cond_lines++;
3877 }
3878 }
3879 }
3880
3881 my (undef, $sindent) = line_stats("+" . $s);
3882 my $stat_real = raw_line($linenr, $cond_lines);
3883
3884 # Check if either of these lines are modified, else
3885 # this is not this patch's fault.
3886 if (!defined($stat_real) ||
3887 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3888 $check = 0;
3889 }
3890 if (defined($stat_real) && $cond_lines > 1) {
3891 $stat_real = "[...]\n$stat_real";
3892 }
3893
3894 #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";
3895
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003896 if ($check && $s ne '' &&
Tom Rinic57383b2020-06-16 10:29:46 -04003897 (($sindent % $tabsize) != 0 ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003898 ($sindent < $indent) ||
3899 ($sindent == $indent &&
3900 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Tom Rinic57383b2020-06-16 10:29:46 -04003901 ($sindent > $indent + $tabsize))) {
Joe Hershberger05622192011-10-18 10:06:59 +00003902 WARN("SUSPECT_CODE_INDENT",
3903 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3904 }
3905 }
3906
3907 # Track the 'values' across context and added lines.
3908 my $opline = $line; $opline =~ s/^./ /;
3909 my ($curr_values, $curr_vars) =
3910 annotate_values($opline . "\n", $prev_values);
3911 $curr_values = $prev_values . $curr_values;
3912 if ($dbg_values) {
3913 my $outline = $opline; $outline =~ s/\t/ /g;
3914 print "$linenr > .$outline\n";
3915 print "$linenr > $curr_values\n";
3916 print "$linenr > $curr_vars\n";
3917 }
3918 $prev_values = substr($curr_values, -1);
3919
3920#ignore lines not being added
Tom Rini6b9709d2014-02-27 08:27:28 -05003921 next if ($line =~ /^[^\+]/);
Joe Hershberger05622192011-10-18 10:06:59 +00003922
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003923# check for dereferences that span multiple lines
3924 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3925 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3926 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3927 my $ref = $1;
3928 $line =~ /^.\s*($Lval)/;
3929 $ref .= $1;
3930 $ref =~ s/\s//g;
3931 WARN("MULTILINE_DEREFERENCE",
3932 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3933 }
3934
3935# check for declarations of signed or unsigned without int
3936 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3937 my $type = $1;
3938 my $var = $2;
3939 $var = "" if (!defined $var);
3940 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3941 my $sign = $1;
3942 my $pointer = $2;
3943
3944 $pointer = "" if (!defined $pointer);
3945
3946 if (WARN("UNSPECIFIED_INT",
3947 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3948 $fix) {
3949 my $decl = trim($sign) . " int ";
3950 my $comp_pointer = $pointer;
3951 $comp_pointer =~ s/\s//g;
3952 $decl .= $comp_pointer;
3953 $decl = rtrim($decl) if ($var eq "");
3954 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3955 }
3956 }
3957 }
3958
Joe Hershberger05622192011-10-18 10:06:59 +00003959# TEST: allow direct testing of the type matcher.
3960 if ($dbg_type) {
3961 if ($line =~ /^.\s*$Declare\s*$/) {
3962 ERROR("TEST_TYPE",
3963 "TEST: is type\n" . $herecurr);
3964 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3965 ERROR("TEST_NOT_TYPE",
3966 "TEST: is not type ($1 is)\n". $herecurr);
3967 }
3968 next;
3969 }
3970# TEST: allow direct testing of the attribute matcher.
3971 if ($dbg_attr) {
3972 if ($line =~ /^.\s*$Modifier\s*$/) {
3973 ERROR("TEST_ATTR",
3974 "TEST: is attr\n" . $herecurr);
3975 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3976 ERROR("TEST_NOT_ATTR",
3977 "TEST: is not attr ($1 is)\n". $herecurr);
3978 }
3979 next;
3980 }
3981
3982# check for initialisation to aggregates open brace on the next line
3983 if ($line =~ /^.\s*{/ &&
3984 $prevline =~ /(?:^|[^=])=\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02003985 if (ERROR("OPEN_BRACE",
3986 "that open brace { should be on the previous line\n" . $hereprev) &&
3987 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3988 fix_delete_line($fixlinenr - 1, $prevrawline);
3989 fix_delete_line($fixlinenr, $rawline);
3990 my $fixedline = $prevrawline;
3991 $fixedline =~ s/\s*=\s*$/ = {/;
3992 fix_insert_line($fixlinenr, $fixedline);
3993 $fixedline = $line;
3994 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3995 fix_insert_line($fixlinenr, $fixedline);
3996 }
Joe Hershberger05622192011-10-18 10:06:59 +00003997 }
3998
3999#
4000# Checks which are anchored on the added line.
4001#
4002
4003# check for malformed paths in #include statements (uses RAW line)
4004 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4005 my $path = $1;
4006 if ($path =~ m{//}) {
4007 ERROR("MALFORMED_INCLUDE",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004008 "malformed #include filename\n" . $herecurr);
4009 }
4010 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4011 ERROR("UAPI_INCLUDE",
4012 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004013 }
4014 }
4015
4016# no C99 // comments
4017 if ($line =~ m{//}) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004018 if (ERROR("C99_COMMENTS",
4019 "do not use C99 // comments\n" . $herecurr) &&
4020 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004021 my $line = $fixed[$fixlinenr];
Tom Rini6b9709d2014-02-27 08:27:28 -05004022 if ($line =~ /\/\/(.*)$/) {
4023 my $comment = trim($1);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004024 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Tom Rini6b9709d2014-02-27 08:27:28 -05004025 }
4026 }
Joe Hershberger05622192011-10-18 10:06:59 +00004027 }
4028 # Remove C99 comments.
4029 $line =~ s@//.*@@;
4030 $opline =~ s@//.*@@;
4031
4032# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4033# the whole statement.
4034#print "APW <$lines[$realline_next - 1]>\n";
4035 if (defined $realline_next &&
4036 exists $lines[$realline_next - 1] &&
4037 !defined $suppress_export{$realline_next} &&
4038 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4039 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4040 # Handle definitions which produce identifiers with
4041 # a prefix:
4042 # XXX(foo);
4043 # EXPORT_SYMBOL(something_foo);
4044 my $name = $1;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004045 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Joe Hershberger05622192011-10-18 10:06:59 +00004046 $name =~ /^${Ident}_$2/) {
4047#print "FOO C name<$name>\n";
4048 $suppress_export{$realline_next} = 1;
4049
4050 } elsif ($stat !~ /(?:
4051 \n.}\s*$|
4052 ^.DEFINE_$Ident\(\Q$name\E\)|
4053 ^.DECLARE_$Ident\(\Q$name\E\)|
4054 ^.LIST_HEAD\(\Q$name\E\)|
4055 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4056 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4057 )/x) {
4058#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4059 $suppress_export{$realline_next} = 2;
4060 } else {
4061 $suppress_export{$realline_next} = 1;
4062 }
4063 }
4064 if (!defined $suppress_export{$linenr} &&
4065 $prevline =~ /^.\s*$/ &&
4066 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4067 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4068#print "FOO B <$lines[$linenr - 1]>\n";
4069 $suppress_export{$linenr} = 2;
4070 }
4071 if (defined $suppress_export{$linenr} &&
4072 $suppress_export{$linenr} == 2) {
4073 WARN("EXPORT_SYMBOL",
4074 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4075 }
4076
4077# check for global initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004078 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004079 if (ERROR("GLOBAL_INITIALISERS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004080 "do not initialise globals to $1\n" . $herecurr) &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004081 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004082 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004083 }
Joe Hershberger05622192011-10-18 10:06:59 +00004084 }
4085# check for static initialisers.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004086 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004087 if (ERROR("INITIALISED_STATIC",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004088 "do not initialise statics to $1\n" .
Tom Rini6b9709d2014-02-27 08:27:28 -05004089 $herecurr) &&
4090 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004091 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004092 }
Joe Hershberger05622192011-10-18 10:06:59 +00004093 }
4094
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004095# check for misordered declarations of char/short/int/long with signed/unsigned
4096 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4097 my $tmp = trim($1);
4098 WARN("MISORDERED_TYPE",
4099 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4100 }
4101
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004102# check for unnecessary <signed> int declarations of short/long/long long
4103 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4104 my $type = trim($1);
4105 next if ($type !~ /\bint\b/);
4106 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4107 my $new_type = $type;
4108 $new_type =~ s/\b\s*int\s*\b/ /;
4109 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4110 $new_type =~ s/^const\s+//;
4111 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4112 $new_type = "const $new_type" if ($type =~ /^const\b/);
4113 $new_type =~ s/\s+/ /g;
4114 $new_type = trim($new_type);
4115 if (WARN("UNNECESSARY_INT",
4116 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4117 $fix) {
4118 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4119 }
4120 }
4121
Joe Hershberger05622192011-10-18 10:06:59 +00004122# check for static const char * arrays.
4123 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4124 WARN("STATIC_CONST_CHAR_ARRAY",
4125 "static const char * array should probably be static const char * const\n" .
4126 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004127 }
4128
4129# check for initialized const char arrays that should be static const
4130 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4131 if (WARN("STATIC_CONST_CHAR_ARRAY",
4132 "const array should probably be static const\n" . $herecurr) &&
4133 $fix) {
4134 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4135 }
4136 }
Joe Hershberger05622192011-10-18 10:06:59 +00004137
4138# check for static char foo[] = "bar" declarations.
4139 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4140 WARN("STATIC_CONST_CHAR_ARRAY",
4141 "static char array declaration should probably be static const char\n" .
4142 $herecurr);
Tom Rinic57383b2020-06-16 10:29:46 -04004143 }
Joe Hershberger05622192011-10-18 10:06:59 +00004144
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004145# check for const <foo> const where <foo> is not a pointer or array type
4146 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4147 my $found = $1;
4148 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4149 WARN("CONST_CONST",
4150 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4151 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4152 WARN("CONST_CONST",
4153 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4154 }
4155 }
4156
4157# check for non-global char *foo[] = {"bar", ...} declarations.
4158 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4159 WARN("STATIC_CONST_CHAR_ARRAY",
4160 "char * array declaration might be better as static const\n" .
4161 $herecurr);
4162 }
4163
4164# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4165 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4166 my $array = $1;
4167 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4168 my $array_div = $1;
4169 if (WARN("ARRAY_SIZE",
4170 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4171 $fix) {
4172 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4173 }
4174 }
4175 }
4176
Tom Rini6b9709d2014-02-27 08:27:28 -05004177# check for function declarations without arguments like "int foo()"
Tom Rinic57383b2020-06-16 10:29:46 -04004178 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004179 if (ERROR("FUNCTION_WITHOUT_ARGS",
4180 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4181 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004182 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004183 }
Joe Hershberger05622192011-10-18 10:06:59 +00004184 }
4185
4186# check for new typedefs, only function parameters and sparse annotations
4187# make sense.
4188 if ($line =~ /\btypedef\s/ &&
4189 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4190 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4191 $line !~ /\b$typeTypedefs\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004192 $line !~ /\b__bitwise\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004193 WARN("NEW_TYPEDEFS",
4194 "do not add new typedefs\n" . $herecurr);
4195 }
4196
4197# * goes on variable not on type
4198 # (char*[ const])
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004199 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4200 #print "AA<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004201 my ($ident, $from, $to) = ($1, $2, $2);
Joe Hershberger05622192011-10-18 10:06:59 +00004202
4203 # Should start with a space.
4204 $to =~ s/^(\S)/ $1/;
4205 # Should not end with a space.
4206 $to =~ s/\s+$//;
4207 # '*'s should not have spaces between.
4208 while ($to =~ s/\*\s+\*/\*\*/) {
4209 }
4210
Tom Rini6b9709d2014-02-27 08:27:28 -05004211## print "1: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004212 if ($from ne $to) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004213 if (ERROR("POINTER_LOCATION",
4214 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4215 $fix) {
4216 my $sub_from = $ident;
4217 my $sub_to = $ident;
4218 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004219 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004220 s@\Q$sub_from\E@$sub_to@;
4221 }
Joe Hershberger05622192011-10-18 10:06:59 +00004222 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004223 }
4224 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4225 #print "BB<$1>\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05004226 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Joe Hershberger05622192011-10-18 10:06:59 +00004227
4228 # Should start with a space.
4229 $to =~ s/^(\S)/ $1/;
4230 # Should not end with a space.
4231 $to =~ s/\s+$//;
4232 # '*'s should not have spaces between.
4233 while ($to =~ s/\*\s+\*/\*\*/) {
4234 }
4235 # Modifiers should have spaces.
4236 $to =~ s/(\b$Modifier$)/$1 /;
4237
Tom Rini6b9709d2014-02-27 08:27:28 -05004238## print "2: from<$from> to<$to> ident<$ident>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00004239 if ($from ne $to && $ident !~ /^$Modifier$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004240 if (ERROR("POINTER_LOCATION",
4241 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4242 $fix) {
4243
4244 my $sub_from = $match;
4245 my $sub_to = $match;
4246 $sub_to =~ s/\Q$from\E/$to/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004247 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004248 s@\Q$sub_from\E@$sub_to@;
4249 }
Joe Hershberger05622192011-10-18 10:06:59 +00004250 }
4251 }
4252
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004253# avoid BUG() or BUG_ON()
4254 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004255 my $msg_level = \&WARN;
4256 $msg_level = \&CHK if ($file);
4257 &{$msg_level}("AVOID_BUG",
4258 "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 +02004259 }
Joe Hershberger05622192011-10-18 10:06:59 +00004260
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004261# avoid LINUX_VERSION_CODE
Joe Hershberger05622192011-10-18 10:06:59 +00004262 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4263 WARN("LINUX_VERSION_CODE",
4264 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4265 }
4266
4267# check for uses of printk_ratelimit
4268 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4269 WARN("PRINTK_RATELIMITED",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004270 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004271 }
4272
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004273# printk should use KERN_* levels
4274 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4275 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4276 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00004277 }
4278
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004279 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4280 my $orig = $1;
4281 my $level = lc($orig);
4282 $level = "warn" if ($level eq "warning");
4283 my $level2 = $level;
4284 $level2 = "dbg" if ($level eq "debug");
4285 WARN("PREFER_PR_LEVEL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004286 "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 +00004287 }
4288
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004289 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4290 my $orig = $1;
4291 my $level = lc($orig);
4292 $level = "warn" if ($level eq "warning");
4293 $level = "dbg" if ($level eq "debug");
4294 WARN("PREFER_DEV_LEVEL",
4295 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4296 }
4297
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004298# ENOSYS means "bad syscall nr" and nothing else. This will have a small
4299# number of false positives, but assembly files are not checked, so at
4300# least the arch entry code will not trigger this warning.
4301 if ($line =~ /\bENOSYS\b/) {
4302 WARN("ENOSYS",
4303 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4304 }
4305
Joe Hershberger05622192011-10-18 10:06:59 +00004306# function brace can't be on same line, except for #defines of do while,
4307# or if closed on same line
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004308 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004309 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4310 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4311 $sline !~ /}/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004312 if (ERROR("OPEN_BRACE",
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004313 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004314 $fix) {
4315 fix_delete_line($fixlinenr, $rawline);
4316 my $fixed_line = $rawline;
4317 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4318 my $line1 = $1;
4319 my $line2 = $2;
4320 fix_insert_line($fixlinenr, ltrim($line1));
4321 fix_insert_line($fixlinenr, "\+{");
4322 if ($line2 !~ /^\s*$/) {
4323 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4324 }
4325 }
Joe Hershberger05622192011-10-18 10:06:59 +00004326 }
4327
4328# open braces for enum, union and struct go on the same line.
4329 if ($line =~ /^.\s*{/ &&
4330 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004331 if (ERROR("OPEN_BRACE",
4332 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4333 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4334 fix_delete_line($fixlinenr - 1, $prevrawline);
4335 fix_delete_line($fixlinenr, $rawline);
4336 my $fixedline = rtrim($prevrawline) . " {";
4337 fix_insert_line($fixlinenr, $fixedline);
4338 $fixedline = $rawline;
4339 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4340 if ($fixedline !~ /^\+\s*$/) {
4341 fix_insert_line($fixlinenr, $fixedline);
4342 }
4343 }
Joe Hershberger05622192011-10-18 10:06:59 +00004344 }
4345
4346# missing space after union, struct or enum definition
Tom Rini6b9709d2014-02-27 08:27:28 -05004347 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4348 if (WARN("SPACING",
4349 "missing space after $1 definition\n" . $herecurr) &&
4350 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004351 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004352 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4353 }
4354 }
4355
4356# Function pointer declarations
4357# check spacing between type, funcptr, and args
4358# canonical declaration is "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004359 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004360 my $declare = $1;
4361 my $pre_pointer_space = $2;
4362 my $post_pointer_space = $3;
4363 my $funcname = $4;
4364 my $post_funcname_space = $5;
4365 my $pre_args_space = $6;
4366
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004367# the $Declare variable will capture all spaces after the type
4368# so check it for a missing trailing missing space but pointer return types
4369# don't need a space so don't warn for those.
4370 my $post_declare_space = "";
4371 if ($declare =~ /(\s+)$/) {
4372 $post_declare_space = $1;
4373 $declare = rtrim($declare);
4374 }
4375 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004376 WARN("SPACING",
4377 "missing space after return type\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004378 $post_declare_space = " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004379 }
4380
4381# unnecessary space "type (*funcptr)(args...)"
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004382# This test is not currently implemented because these declarations are
4383# equivalent to
4384# int foo(int bar, ...)
4385# and this is form shouldn't/doesn't generate a checkpatch warning.
4386#
4387# elsif ($declare =~ /\s{2,}$/) {
4388# WARN("SPACING",
4389# "Multiple spaces after return type\n" . $herecurr);
4390# }
Tom Rini6b9709d2014-02-27 08:27:28 -05004391
4392# unnecessary space "type ( *funcptr)(args...)"
4393 if (defined $pre_pointer_space &&
4394 $pre_pointer_space =~ /^\s/) {
4395 WARN("SPACING",
4396 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4397 }
4398
4399# unnecessary space "type (* funcptr)(args...)"
4400 if (defined $post_pointer_space &&
4401 $post_pointer_space =~ /^\s/) {
4402 WARN("SPACING",
4403 "Unnecessary space before function pointer name\n" . $herecurr);
4404 }
4405
4406# unnecessary space "type (*funcptr )(args...)"
4407 if (defined $post_funcname_space &&
4408 $post_funcname_space =~ /^\s/) {
4409 WARN("SPACING",
4410 "Unnecessary space after function pointer name\n" . $herecurr);
4411 }
4412
4413# unnecessary space "type (*funcptr) (args...)"
4414 if (defined $pre_args_space &&
4415 $pre_args_space =~ /^\s/) {
4416 WARN("SPACING",
4417 "Unnecessary space before function pointer arguments\n" . $herecurr);
4418 }
4419
4420 if (show_type("SPACING") && $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004421 $fixed[$fixlinenr] =~
4422 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05004423 }
Joe Hershberger05622192011-10-18 10:06:59 +00004424 }
4425
4426# check for spacing round square brackets; allowed:
4427# 1. with a type on the left -- int [] a;
4428# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4429# 3. inside a curly brace -- = { [0...10] = 5 }
4430 while ($line =~ /(.*?\s)\[/g) {
4431 my ($where, $prefix) = ($-[1], $1);
4432 if ($prefix !~ /$Type\s+$/ &&
4433 ($where != 0 || $prefix !~ /^.\s+$/) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004434 $prefix !~ /[{,:]\s+$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004435 if (ERROR("BRACKET_SPACE",
4436 "space prohibited before open square bracket '['\n" . $herecurr) &&
4437 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004438 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004439 s/^(\+.*?)\s+\[/$1\[/;
4440 }
Joe Hershberger05622192011-10-18 10:06:59 +00004441 }
4442 }
4443
4444# check for spaces between functions and their parentheses.
4445 while ($line =~ /($Ident)\s+\(/g) {
4446 my $name = $1;
4447 my $ctx_before = substr($line, 0, $-[1]);
4448 my $ctx = "$ctx_before$name";
4449
4450 # Ignore those directives where spaces _are_ permitted.
4451 if ($name =~ /^(?:
4452 if|for|while|switch|return|case|
4453 volatile|__volatile__|
4454 __attribute__|format|__extension__|
4455 asm|__asm__)$/x)
4456 {
Joe Hershberger05622192011-10-18 10:06:59 +00004457 # cpp #define statements have non-optional spaces, ie
4458 # if there is a space between the name and the open
4459 # parenthesis it is simply not a parameter group.
4460 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4461
4462 # cpp #elif statement condition may start with a (
4463 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4464
4465 # If this whole things ends with a type its most
4466 # likely a typedef for a function.
4467 } elsif ($ctx =~ /$Type$/) {
4468
4469 } else {
Tom Rini6b9709d2014-02-27 08:27:28 -05004470 if (WARN("SPACING",
4471 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4472 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004473 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004474 s/\b$name\s+\(/$name\(/;
4475 }
Joe Hershberger05622192011-10-18 10:06:59 +00004476 }
4477 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00004478
Joe Hershberger05622192011-10-18 10:06:59 +00004479# Check operator spacing.
4480 if (!($line=~/\#\s*include/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004481 my $fixed_line = "";
4482 my $line_fixed = 0;
4483
Joe Hershberger05622192011-10-18 10:06:59 +00004484 my $ops = qr{
4485 <<=|>>=|<=|>=|==|!=|
4486 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4487 =>|->|<<|>>|<|>|=|!|~|
4488 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tom Rini6b9709d2014-02-27 08:27:28 -05004489 \?:|\?|:
Joe Hershberger05622192011-10-18 10:06:59 +00004490 }x;
4491 my @elements = split(/($ops|;)/, $opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004492
4493## print("element count: <" . $#elements . ">\n");
4494## foreach my $el (@elements) {
4495## print("el: <$el>\n");
4496## }
4497
4498 my @fix_elements = ();
Joe Hershberger05622192011-10-18 10:06:59 +00004499 my $off = 0;
4500
Tom Rini6b9709d2014-02-27 08:27:28 -05004501 foreach my $el (@elements) {
4502 push(@fix_elements, substr($rawline, $off, length($el)));
4503 $off += length($el);
4504 }
4505
4506 $off = 0;
4507
Joe Hershberger05622192011-10-18 10:06:59 +00004508 my $blank = copy_spacing($opline);
Tom Rini6b9709d2014-02-27 08:27:28 -05004509 my $last_after = -1;
Joe Hershberger05622192011-10-18 10:06:59 +00004510
4511 for (my $n = 0; $n < $#elements; $n += 2) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004512
4513 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4514
4515## print("n: <$n> good: <$good>\n");
4516
Joe Hershberger05622192011-10-18 10:06:59 +00004517 $off += length($elements[$n]);
4518
4519 # Pick up the preceding and succeeding characters.
4520 my $ca = substr($opline, 0, $off);
4521 my $cc = '';
4522 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4523 $cc = substr($opline, $off + length($elements[$n + 1]));
4524 }
4525 my $cb = "$ca$;$cc";
4526
4527 my $a = '';
4528 $a = 'V' if ($elements[$n] ne '');
4529 $a = 'W' if ($elements[$n] =~ /\s$/);
4530 $a = 'C' if ($elements[$n] =~ /$;$/);
4531 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4532 $a = 'O' if ($elements[$n] eq '');
4533 $a = 'E' if ($ca =~ /^\s*$/);
4534
4535 my $op = $elements[$n + 1];
4536
4537 my $c = '';
4538 if (defined $elements[$n + 2]) {
4539 $c = 'V' if ($elements[$n + 2] ne '');
4540 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4541 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4542 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4543 $c = 'O' if ($elements[$n + 2] eq '');
4544 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4545 } else {
4546 $c = 'E';
4547 }
4548
4549 my $ctx = "${a}x${c}";
4550
4551 my $at = "(ctx:$ctx)";
4552
4553 my $ptr = substr($blank, 0, $off) . "^";
4554 my $hereptr = "$hereline$ptr\n";
4555
4556 # Pull out the value of this operator.
4557 my $op_type = substr($curr_values, $off + 1, 1);
4558
4559 # Get the full operator variant.
4560 my $opv = $op . substr($curr_vars, $off, 1);
4561
4562 # Ignore operators passed as parameters.
4563 if ($op_type ne 'V' &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004564 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00004565
4566# # Ignore comments
4567# } elsif ($op =~ /^$;+$/) {
4568
4569 # ; should have either the end of line or a space or \ after it
4570 } elsif ($op eq ';') {
4571 if ($ctx !~ /.x[WEBC]/ &&
4572 $cc !~ /^\\/ && $cc !~ /^;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004573 if (ERROR("SPACING",
4574 "space required after that '$op' $at\n" . $hereptr)) {
4575 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4576 $line_fixed = 1;
4577 }
Joe Hershberger05622192011-10-18 10:06:59 +00004578 }
4579
4580 # // is a comment
4581 } elsif ($op eq '//') {
4582
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004583 # : when part of a bitfield
4584 } elsif ($opv eq ':B') {
4585 # skip the bitfield test for now
4586
Joe Hershberger05622192011-10-18 10:06:59 +00004587 # No spaces for:
4588 # ->
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004589 } elsif ($op eq '->') {
Joe Hershberger05622192011-10-18 10:06:59 +00004590 if ($ctx =~ /Wx.|.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004591 if (ERROR("SPACING",
4592 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4593 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4594 if (defined $fix_elements[$n + 2]) {
4595 $fix_elements[$n + 2] =~ s/^\s+//;
4596 }
4597 $line_fixed = 1;
4598 }
Joe Hershberger05622192011-10-18 10:06:59 +00004599 }
4600
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004601 # , must not have a space before and must have a space on the right.
Joe Hershberger05622192011-10-18 10:06:59 +00004602 } elsif ($op eq ',') {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004603 my $rtrim_before = 0;
4604 my $space_after = 0;
4605 if ($ctx =~ /Wx./) {
4606 if (ERROR("SPACING",
4607 "space prohibited before that '$op' $at\n" . $hereptr)) {
4608 $line_fixed = 1;
4609 $rtrim_before = 1;
4610 }
4611 }
Joe Hershberger05622192011-10-18 10:06:59 +00004612 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004613 if (ERROR("SPACING",
4614 "space required after that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004615 $line_fixed = 1;
4616 $last_after = $n;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004617 $space_after = 1;
4618 }
4619 }
4620 if ($rtrim_before || $space_after) {
4621 if ($rtrim_before) {
4622 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4623 } else {
4624 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4625 }
4626 if ($space_after) {
4627 $good .= " ";
Tom Rini6b9709d2014-02-27 08:27:28 -05004628 }
Joe Hershberger05622192011-10-18 10:06:59 +00004629 }
4630
4631 # '*' as part of a type definition -- reported already.
4632 } elsif ($opv eq '*_') {
4633 #warn "'*' is part of type\n";
4634
4635 # unary operators should have a space before and
4636 # none after. May be left adjacent to another
4637 # unary operator, or a cast
4638 } elsif ($op eq '!' || $op eq '~' ||
4639 $opv eq '*U' || $opv eq '-U' ||
4640 $opv eq '&U' || $opv eq '&&U') {
4641 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004642 if (ERROR("SPACING",
4643 "space required before that '$op' $at\n" . $hereptr)) {
4644 if ($n != $last_after + 2) {
4645 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4646 $line_fixed = 1;
4647 }
4648 }
Joe Hershberger05622192011-10-18 10:06:59 +00004649 }
4650 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4651 # A unary '*' may be const
4652
4653 } elsif ($ctx =~ /.xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004654 if (ERROR("SPACING",
4655 "space prohibited after that '$op' $at\n" . $hereptr)) {
4656 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4657 if (defined $fix_elements[$n + 2]) {
4658 $fix_elements[$n + 2] =~ s/^\s+//;
4659 }
4660 $line_fixed = 1;
4661 }
Joe Hershberger05622192011-10-18 10:06:59 +00004662 }
4663
4664 # unary ++ and unary -- are allowed no space on one side.
4665 } elsif ($op eq '++' or $op eq '--') {
4666 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004667 if (ERROR("SPACING",
4668 "space required one side of that '$op' $at\n" . $hereptr)) {
4669 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4670 $line_fixed = 1;
4671 }
Joe Hershberger05622192011-10-18 10:06:59 +00004672 }
4673 if ($ctx =~ /Wx[BE]/ ||
4674 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004675 if (ERROR("SPACING",
4676 "space prohibited before that '$op' $at\n" . $hereptr)) {
4677 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4678 $line_fixed = 1;
4679 }
Joe Hershberger05622192011-10-18 10:06:59 +00004680 }
4681 if ($ctx =~ /ExW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004682 if (ERROR("SPACING",
4683 "space prohibited after that '$op' $at\n" . $hereptr)) {
4684 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4685 if (defined $fix_elements[$n + 2]) {
4686 $fix_elements[$n + 2] =~ s/^\s+//;
4687 }
4688 $line_fixed = 1;
4689 }
Joe Hershberger05622192011-10-18 10:06:59 +00004690 }
4691
Joe Hershberger05622192011-10-18 10:06:59 +00004692 # << and >> may either have or not have spaces both sides
4693 } elsif ($op eq '<<' or $op eq '>>' or
4694 $op eq '&' or $op eq '^' or $op eq '|' or
4695 $op eq '+' or $op eq '-' or
4696 $op eq '*' or $op eq '/' or
4697 $op eq '%')
4698 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004699 if ($check) {
4700 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4701 if (CHK("SPACING",
4702 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4703 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4704 $fix_elements[$n + 2] =~ s/^\s+//;
4705 $line_fixed = 1;
4706 }
4707 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4708 if (CHK("SPACING",
4709 "space preferred before that '$op' $at\n" . $hereptr)) {
4710 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4711 $line_fixed = 1;
4712 }
4713 }
4714 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004715 if (ERROR("SPACING",
4716 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4717 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4718 if (defined $fix_elements[$n + 2]) {
4719 $fix_elements[$n + 2] =~ s/^\s+//;
4720 }
4721 $line_fixed = 1;
4722 }
Joe Hershberger05622192011-10-18 10:06:59 +00004723 }
4724
4725 # A colon needs no spaces before when it is
4726 # terminating a case value or a label.
4727 } elsif ($opv eq ':C' || $opv eq ':L') {
4728 if ($ctx =~ /Wx./) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004729 if (ERROR("SPACING",
4730 "space prohibited before that '$op' $at\n" . $hereptr)) {
4731 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4732 $line_fixed = 1;
4733 }
Joe Hershberger05622192011-10-18 10:06:59 +00004734 }
4735
4736 # All the others need spaces both sides.
4737 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4738 my $ok = 0;
4739
4740 # Ignore email addresses <foo@bar>
4741 if (($op eq '<' &&
4742 $cc =~ /^\S+\@\S+>/) ||
4743 ($op eq '>' &&
4744 $ca =~ /<\S+\@\S+$/))
4745 {
Tom Rinic57383b2020-06-16 10:29:46 -04004746 $ok = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00004747 }
4748
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004749 # for asm volatile statements
4750 # ignore a colon with another
4751 # colon immediately before or after
4752 if (($op eq ':') &&
4753 ($ca =~ /:$/ || $cc =~ /^:/)) {
4754 $ok = 1;
4755 }
4756
Tom Rini6b9709d2014-02-27 08:27:28 -05004757 # messages are ERROR, but ?: are CHK
Joe Hershberger05622192011-10-18 10:06:59 +00004758 if ($ok == 0) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004759 my $msg_level = \&ERROR;
4760 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
Tom Rini6b9709d2014-02-27 08:27:28 -05004761
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004762 if (&{$msg_level}("SPACING",
4763 "spaces required around that '$op' $at\n" . $hereptr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004764 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4765 if (defined $fix_elements[$n + 2]) {
4766 $fix_elements[$n + 2] =~ s/^\s+//;
4767 }
4768 $line_fixed = 1;
4769 }
Joe Hershberger05622192011-10-18 10:06:59 +00004770 }
4771 }
4772 $off += length($elements[$n + 1]);
Tom Rini6b9709d2014-02-27 08:27:28 -05004773
4774## print("n: <$n> GOOD: <$good>\n");
4775
4776 $fixed_line = $fixed_line . $good;
4777 }
4778
4779 if (($#elements % 2) == 0) {
4780 $fixed_line = $fixed_line . $fix_elements[$#elements];
4781 }
4782
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004783 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4784 $fixed[$fixlinenr] = $fixed_line;
Tom Rini6b9709d2014-02-27 08:27:28 -05004785 }
4786
4787
4788 }
4789
4790# check for whitespace before a non-naked semicolon
4791 if ($line =~ /^\+.*\S\s+;\s*$/) {
4792 if (WARN("SPACING",
4793 "space prohibited before semicolon\n" . $herecurr) &&
4794 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004795 1 while $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004796 s/^(\+.*\S)\s+;/$1;/;
Joe Hershberger05622192011-10-18 10:06:59 +00004797 }
4798 }
4799
4800# check for multiple assignments
4801 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4802 CHK("MULTIPLE_ASSIGNMENTS",
4803 "multiple assignments should be avoided\n" . $herecurr);
4804 }
4805
4806## # check for multiple declarations, allowing for a function declaration
4807## # continuation.
4808## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4809## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4810##
4811## # Remove any bracketed sections to ensure we do not
4812## # falsly report the parameters of functions.
4813## my $ln = $line;
4814## while ($ln =~ s/\([^\(\)]*\)//g) {
4815## }
4816## if ($ln =~ /,/) {
4817## WARN("MULTIPLE_DECLARATION",
4818## "declaring multiple variables together should be avoided\n" . $herecurr);
4819## }
4820## }
4821
4822#need space before brace following if, while, etc
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004823 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004824 $line =~ /\b(?:else|do)\{/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004825 if (ERROR("SPACING",
4826 "space required before the open brace '{'\n" . $herecurr) &&
4827 $fix) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004828 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
Tom Rini6b9709d2014-02-27 08:27:28 -05004829 }
Joe Hershberger05622192011-10-18 10:06:59 +00004830 }
4831
Tom Rini6b9709d2014-02-27 08:27:28 -05004832## # check for blank lines before declarations
4833## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4834## $prevrawline =~ /^.\s*$/) {
4835## WARN("SPACING",
4836## "No blank lines before declarations\n" . $hereprev);
4837## }
4838##
4839
Joe Hershberger05622192011-10-18 10:06:59 +00004840# closing brace should have a space following it when it has anything
4841# on the line
Tom Rinic57383b2020-06-16 10:29:46 -04004842 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004843 if (ERROR("SPACING",
4844 "space required after that close brace '}'\n" . $herecurr) &&
4845 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004846 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004847 s/}((?!(?:,|;|\)))\S)/} $1/;
4848 }
Joe Hershberger05622192011-10-18 10:06:59 +00004849 }
4850
4851# check spacing on square brackets
4852 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004853 if (ERROR("SPACING",
4854 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4855 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004856 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004857 s/\[\s+/\[/;
4858 }
Joe Hershberger05622192011-10-18 10:06:59 +00004859 }
4860 if ($line =~ /\s\]/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004861 if (ERROR("SPACING",
4862 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4863 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004864 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004865 s/\s+\]/\]/;
4866 }
Joe Hershberger05622192011-10-18 10:06:59 +00004867 }
4868
4869# check spacing on parentheses
4870 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4871 $line !~ /for\s*\(\s+;/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004872 if (ERROR("SPACING",
4873 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4874 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004875 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004876 s/\(\s+/\(/;
4877 }
Joe Hershberger05622192011-10-18 10:06:59 +00004878 }
4879 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4880 $line !~ /for\s*\(.*;\s+\)/ &&
4881 $line !~ /:\s+\)/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004882 if (ERROR("SPACING",
4883 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4884 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004885 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004886 s/\s+\)/\)/;
4887 }
Joe Hershberger05622192011-10-18 10:06:59 +00004888 }
4889
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004890# check unnecessary parentheses around addressof/dereference single $Lvals
4891# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4892
4893 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4894 my $var = $1;
4895 if (CHK("UNNECESSARY_PARENTHESES",
4896 "Unnecessary parentheses around $var\n" . $herecurr) &&
4897 $fix) {
4898 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4899 }
4900 }
4901
4902# check for unnecessary parentheses around function pointer uses
4903# ie: (foo->bar)(); should be foo->bar();
4904# but not "if (foo->bar) (" to avoid some false positives
4905 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4906 my $var = $2;
4907 if (CHK("UNNECESSARY_PARENTHESES",
4908 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4909 $fix) {
4910 my $var2 = deparenthesize($var);
4911 $var2 =~ s/\s//g;
4912 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4913 }
4914 }
4915
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004916# check for unnecessary parentheses around comparisons in if uses
4917# when !drivers/staging or command-line uses --strict
4918 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004919 $perl_version_ok && defined($stat) &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02004920 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4921 my $if_stat = $1;
4922 my $test = substr($2, 1, -1);
4923 my $herectx;
4924 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4925 my $match = $1;
4926 # avoid parentheses around potential macro args
4927 next if ($match =~ /^\s*\w+\s*$/);
4928 if (!defined($herectx)) {
4929 $herectx = $here . "\n";
4930 my $cnt = statement_rawlines($if_stat);
4931 for (my $n = 0; $n < $cnt; $n++) {
4932 my $rl = raw_line($linenr, $n);
4933 $herectx .= $rl . "\n";
4934 last if $rl =~ /^[ \+].*\{/;
4935 }
4936 }
4937 CHK("UNNECESSARY_PARENTHESES",
4938 "Unnecessary parentheses around '$match'\n" . $herectx);
4939 }
4940 }
4941
Joe Hershberger05622192011-10-18 10:06:59 +00004942#goto labels aren't indented, allow a single space however
4943 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4944 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05004945 if (WARN("INDENTED_LABEL",
4946 "labels should not be indented\n" . $herecurr) &&
4947 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004948 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05004949 s/^(.)\s+/$1/;
4950 }
Joe Hershberger05622192011-10-18 10:06:59 +00004951 }
4952
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004953# return is not a function
Tom Rini6b9709d2014-02-27 08:27:28 -05004954 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Joe Hershberger05622192011-10-18 10:06:59 +00004955 my $spacing = $1;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004956 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004957 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4958 my $value = $1;
4959 $value = deparenthesize($value);
4960 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4961 ERROR("RETURN_PARENTHESES",
4962 "return is not a function, parentheses are not required\n" . $herecurr);
4963 }
Joe Hershberger05622192011-10-18 10:06:59 +00004964 } elsif ($spacing !~ /\s+/) {
4965 ERROR("SPACING",
4966 "space required before the open parenthesis '('\n" . $herecurr);
4967 }
4968 }
Tom Rini6b9709d2014-02-27 08:27:28 -05004969
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004970# unnecessary return in a void function
4971# at end-of-function, with the previous line a single leading tab, then return;
4972# and the line before that not a goto label target like "out:"
4973 if ($sline =~ /^[ \+]}\s*$/ &&
4974 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4975 $linenr >= 3 &&
4976 $lines[$linenr - 3] =~ /^[ +]/ &&
4977 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4978 WARN("RETURN_VOID",
4979 "void function return statements are not generally useful\n" . $hereprev);
4980 }
4981
Tom Rini6b9709d2014-02-27 08:27:28 -05004982# if statements using unnecessary parentheses - ie: if ((foo == bar))
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02004983 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05004984 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4985 my $openparens = $1;
4986 my $count = $openparens =~ tr@\(@\(@;
4987 my $msg = "";
4988 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4989 my $comp = $4; #Not $1 because of $LvalOrFunc
4990 $msg = " - maybe == should be = ?" if ($comp eq "==");
4991 WARN("UNNECESSARY_PARENTHESES",
4992 "Unnecessary parentheses$msg\n" . $herecurr);
4993 }
4994 }
4995
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02004996# comparisons with a constant or upper case identifier on the left
4997# avoid cases like "foo + BAR < baz"
4998# only fix matches surrounded by parentheses to avoid incorrect
4999# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005000 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005001 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5002 my $lead = $1;
5003 my $const = $2;
5004 my $comp = $3;
5005 my $to = $4;
5006 my $newcomp = $comp;
5007 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5008 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5009 WARN("CONSTANT_COMPARISON",
5010 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5011 $fix) {
5012 if ($comp eq "<") {
5013 $newcomp = ">";
5014 } elsif ($comp eq "<=") {
5015 $newcomp = ">=";
5016 } elsif ($comp eq ">") {
5017 $newcomp = "<";
5018 } elsif ($comp eq ">=") {
5019 $newcomp = "<=";
5020 }
5021 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5022 }
5023 }
5024
5025# Return of what appears to be an errno should normally be negative
5026 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005027 my $name = $1;
5028 if ($name ne 'EOF' && $name ne 'ERROR') {
5029 WARN("USE_NEGATIVE_ERRNO",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005030 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005031 }
5032 }
5033
Joe Hershberger05622192011-10-18 10:06:59 +00005034# Need a space before open parenthesis after if, while etc
Tom Rini6b9709d2014-02-27 08:27:28 -05005035 if ($line =~ /\b(if|while|for|switch)\(/) {
5036 if (ERROR("SPACING",
5037 "space required before the open parenthesis '('\n" . $herecurr) &&
5038 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005039 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005040 s/\b(if|while|for|switch)\(/$1 \(/;
5041 }
Joe Hershberger05622192011-10-18 10:06:59 +00005042 }
5043
5044# Check for illegal assignment in if conditional -- and check for trailing
5045# statements after the conditional.
5046 if ($line =~ /do\s*(?!{)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005047 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5048 ctx_statement_block($linenr, $realcnt, 0)
5049 if (!defined $stat);
Joe Hershberger05622192011-10-18 10:06:59 +00005050 my ($stat_next) = ctx_statement_block($line_nr_next,
5051 $remain_next, $off_next);
5052 $stat_next =~ s/\n./\n /g;
5053 ##print "stat<$stat> stat_next<$stat_next>\n";
5054
5055 if ($stat_next =~ /^\s*while\b/) {
5056 # If the statement carries leading newlines,
5057 # then count those as offsets.
5058 my ($whitespace) =
5059 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5060 my $offset =
5061 statement_rawlines($whitespace) - 1;
5062
5063 $suppress_whiletrailers{$line_nr_next +
5064 $offset} = 1;
5065 }
5066 }
5067 if (!defined $suppress_whiletrailers{$linenr} &&
Tom Rini6b9709d2014-02-27 08:27:28 -05005068 defined($stat) && defined($cond) &&
Joe Hershberger05622192011-10-18 10:06:59 +00005069 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5070 my ($s, $c) = ($stat, $cond);
5071
5072 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5073 ERROR("ASSIGN_IN_IF",
5074 "do not use assignment in if condition\n" . $herecurr);
5075 }
5076
5077 # Find out what is on the end of the line after the
5078 # conditional.
5079 substr($s, 0, length($c), '');
5080 $s =~ s/\n.*//g;
Tom Rinic57383b2020-06-16 10:29:46 -04005081 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005082 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5083 $c !~ /}\s*while\s*/)
5084 {
5085 # Find out how long the conditional actually is.
5086 my @newlines = ($c =~ /\n/gs);
5087 my $cond_lines = 1 + $#newlines;
5088 my $stat_real = '';
5089
5090 $stat_real = raw_line($linenr, $cond_lines)
5091 . "\n" if ($cond_lines);
5092 if (defined($stat_real) && $cond_lines > 1) {
5093 $stat_real = "[...]\n$stat_real";
5094 }
5095
5096 ERROR("TRAILING_STATEMENTS",
5097 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5098 }
5099 }
5100
5101# Check for bitwise tests written as boolean
5102 if ($line =~ /
5103 (?:
5104 (?:\[|\(|\&\&|\|\|)
5105 \s*0[xX][0-9]+\s*
5106 (?:\&\&|\|\|)
5107 |
5108 (?:\&\&|\|\|)
5109 \s*0[xX][0-9]+\s*
5110 (?:\&\&|\|\||\)|\])
5111 )/x)
5112 {
5113 WARN("HEXADECIMAL_BOOLEAN_TEST",
5114 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5115 }
5116
5117# if and else should not have general statements after it
5118 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5119 my $s = $1;
Tom Rinic57383b2020-06-16 10:29:46 -04005120 $s =~ s/$;//g; # Remove any comments
Joe Hershberger05622192011-10-18 10:06:59 +00005121 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5122 ERROR("TRAILING_STATEMENTS",
5123 "trailing statements should be on next line\n" . $herecurr);
5124 }
5125 }
5126# if should not continue a brace
5127 if ($line =~ /}\s*if\b/) {
5128 ERROR("TRAILING_STATEMENTS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005129 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Joe Hershberger05622192011-10-18 10:06:59 +00005130 $herecurr);
5131 }
5132# case and default should not have general statements after them
5133 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5134 $line !~ /\G(?:
5135 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5136 \s*return\s+
5137 )/xg)
5138 {
5139 ERROR("TRAILING_STATEMENTS",
5140 "trailing statements should be on next line\n" . $herecurr);
5141 }
5142
5143 # Check for }<nl>else {, these must be at the same
5144 # indent level to be relevant to each other.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005145 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5146 $previndent == $indent) {
5147 if (ERROR("ELSE_AFTER_BRACE",
5148 "else should follow close brace '}'\n" . $hereprev) &&
5149 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5150 fix_delete_line($fixlinenr - 1, $prevrawline);
5151 fix_delete_line($fixlinenr, $rawline);
5152 my $fixedline = $prevrawline;
5153 $fixedline =~ s/}\s*$//;
5154 if ($fixedline !~ /^\+\s*$/) {
5155 fix_insert_line($fixlinenr, $fixedline);
5156 }
5157 $fixedline = $rawline;
5158 $fixedline =~ s/^(.\s*)else/$1} else/;
5159 fix_insert_line($fixlinenr, $fixedline);
5160 }
Joe Hershberger05622192011-10-18 10:06:59 +00005161 }
5162
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005163 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5164 $previndent == $indent) {
Joe Hershberger05622192011-10-18 10:06:59 +00005165 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5166
5167 # Find out what is on the end of the line after the
5168 # conditional.
5169 substr($s, 0, length($c), '');
5170 $s =~ s/\n.*//g;
5171
5172 if ($s =~ /^\s*;/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005173 if (ERROR("WHILE_AFTER_BRACE",
5174 "while should follow close brace '}'\n" . $hereprev) &&
5175 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5176 fix_delete_line($fixlinenr - 1, $prevrawline);
5177 fix_delete_line($fixlinenr, $rawline);
5178 my $fixedline = $prevrawline;
5179 my $trailing = $rawline;
5180 $trailing =~ s/^\+//;
5181 $trailing = trim($trailing);
5182 $fixedline =~ s/}\s*$/} $trailing/;
5183 fix_insert_line($fixlinenr, $fixedline);
5184 }
Joe Hershberger05622192011-10-18 10:06:59 +00005185 }
5186 }
5187
Tom Rini6b9709d2014-02-27 08:27:28 -05005188#Specific variable tests
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005189 while ($line =~ m{($Constant|$Lval)}g) {
5190 my $var = $1;
Tom Rini6b9709d2014-02-27 08:27:28 -05005191
Tom Rini6b9709d2014-02-27 08:27:28 -05005192#CamelCase
5193 if ($var !~ /^$Constant$/ &&
5194 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5195#Ignore Page<foo> variants
5196 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Tom Rinic57383b2020-06-16 10:29:46 -04005197#Ignore SI style variants like nS, mV and dB
5198#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5199 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005200#Ignore some three character SI units explicitly, like MiB and KHz
5201 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005202 while ($var =~ m{($Ident)}g) {
5203 my $word = $1;
5204 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5205 if ($check) {
5206 seed_camelcase_includes();
5207 if (!$file && !$camelcase_file_seeded) {
5208 seed_camelcase_file($realfile);
5209 $camelcase_file_seeded = 1;
5210 }
5211 }
5212 if (!defined $camelcase{$word}) {
5213 $camelcase{$word} = 1;
5214 CHK("CAMELCASE",
5215 "Avoid CamelCase: <$word>\n" . $herecurr);
5216 }
5217 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005218 }
5219 }
Joe Hershberger05622192011-10-18 10:06:59 +00005220
5221#no spaces allowed after \ in define
Tom Rini6b9709d2014-02-27 08:27:28 -05005222 if ($line =~ /\#\s*define.*\\\s+$/) {
5223 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5224 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5225 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005226 $fixed[$fixlinenr] =~ s/\s+$//;
Tom Rini6b9709d2014-02-27 08:27:28 -05005227 }
Joe Hershberger05622192011-10-18 10:06:59 +00005228 }
5229
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005230# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5231# itself <asm/foo.h> (uses RAW line)
Joe Hershberger05622192011-10-18 10:06:59 +00005232 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5233 my $file = "$1.h";
5234 my $checkfile = "include/linux/$file";
5235 if (-f "$root/$checkfile" &&
5236 $realfile ne $checkfile &&
5237 $1 !~ /$allowed_asm_includes/)
5238 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005239 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5240 if ($asminclude > 0) {
5241 if ($realfile =~ m{^arch/}) {
5242 CHK("ARCH_INCLUDE_LINUX",
5243 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5244 } else {
5245 WARN("INCLUDE_LINUX",
5246 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5247 }
Joe Hershberger05622192011-10-18 10:06:59 +00005248 }
5249 }
5250 }
5251
5252# multi-statement macros should be enclosed in a do while loop, grab the
5253# first statement and ensure its the whole macro if its not enclosed
5254# in a known good container
5255 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5256 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5257 my $ln = $linenr;
5258 my $cnt = $realcnt;
5259 my ($off, $dstat, $dcond, $rest);
5260 my $ctx = '';
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005261 my $has_flow_statement = 0;
5262 my $has_arg_concat = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005263 ($dstat, $dcond, $ln, $cnt, $off) =
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005264 ctx_statement_block($linenr, $realcnt, 0);
5265 $ctx = $dstat;
Joe Hershberger05622192011-10-18 10:06:59 +00005266 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5267 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5268
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005269 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5270 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5271
5272 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5273 my $define_args = $1;
5274 my $define_stmt = $dstat;
5275 my @def_args = ();
5276
5277 if (defined $define_args && $define_args ne "") {
5278 $define_args = substr($define_args, 1, length($define_args) - 2);
5279 $define_args =~ s/\s*//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005280 $define_args =~ s/\\\+?//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005281 @def_args = split(",", $define_args);
5282 }
5283
Joe Hershberger05622192011-10-18 10:06:59 +00005284 $dstat =~ s/$;//g;
5285 $dstat =~ s/\\\n.//g;
5286 $dstat =~ s/^\s*//s;
5287 $dstat =~ s/\s*$//s;
5288
5289 # Flatten any parentheses and braces
5290 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5291 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005292 $dstat =~ s/.\[[^\[\]]*\]/1/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005293 {
5294 }
5295
Tom Rinic57383b2020-06-16 10:29:46 -04005296 # Flatten any obvious string concatenation.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005297 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5298 $dstat =~ s/$Ident\s*($String)/$1/)
Joe Hershberger05622192011-10-18 10:06:59 +00005299 {
5300 }
5301
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005302 # Make asm volatile uses seem like a generic function
5303 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5304
Joe Hershberger05622192011-10-18 10:06:59 +00005305 my $exceptions = qr{
5306 $Declare|
5307 module_param_named|
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005308 MODULE_PARM_DESC|
Joe Hershberger05622192011-10-18 10:06:59 +00005309 DECLARE_PER_CPU|
5310 DEFINE_PER_CPU|
5311 __typeof__\(|
5312 union|
5313 struct|
5314 \.$Ident\s*=\s*|
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005315 ^\"|\"$|
5316 ^\[
Joe Hershberger05622192011-10-18 10:06:59 +00005317 }x;
5318 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005319
5320 $ctx =~ s/\n*$//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005321 my $stmt_cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005322 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005323
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005324 if ($dstat ne '' &&
5325 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5326 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Tom Rini6b9709d2014-02-27 08:27:28 -05005327 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005328 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005329 $dstat !~ /$exceptions/ &&
5330 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Tom Rini6b9709d2014-02-27 08:27:28 -05005331 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005332 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5333 $dstat !~ /^for\s*$Constant$/ && # for (...)
5334 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5335 $dstat !~ /^do\s*{/ && # do {...
Heiko Schocherd8a1a302016-01-04 09:17:19 +01005336 $dstat !~ /^\(\{/ && # ({...
Tom Rini6b9709d2014-02-27 08:27:28 -05005337 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005338 {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005339 if ($dstat =~ /^\s*if\b/) {
5340 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5341 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5342 } elsif ($dstat =~ /;/) {
5343 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5344 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5345 } else {
5346 ERROR("COMPLEX_MACRO",
5347 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5348 }
5349
5350 }
5351
5352 # Make $define_stmt single line, comment-free, etc
5353 my @stmt_array = split('\n', $define_stmt);
5354 my $first = 1;
5355 $define_stmt = "";
5356 foreach my $l (@stmt_array) {
5357 $l =~ s/\\$//;
5358 if ($first) {
5359 $define_stmt = $l;
5360 $first = 0;
5361 } elsif ($l =~ /^[\+ ]/) {
5362 $define_stmt .= substr($l, 1);
5363 }
5364 }
5365 $define_stmt =~ s/$;//g;
5366 $define_stmt =~ s/\s+/ /g;
5367 $define_stmt = trim($define_stmt);
5368
5369# check if any macro arguments are reused (ignore '...' and 'type')
5370 foreach my $arg (@def_args) {
5371 next if ($arg =~ /\.\.\./);
5372 next if ($arg =~ /^type$/i);
5373 my $tmp_stmt = $define_stmt;
Tom Rinic57383b2020-06-16 10:29:46 -04005374 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005375 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5376 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005377 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005378 if ($use_cnt > 1) {
5379 CHK("MACRO_ARG_REUSE",
5380 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5381 }
5382# check if any macro arguments may have other precedence issues
5383 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5384 ((defined($1) && $1 ne ',') ||
5385 (defined($2) && $2 ne ','))) {
5386 CHK("MACRO_ARG_PRECEDENCE",
5387 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5388 }
5389 }
5390
5391# check for macros with flow control, but without ## concatenation
5392# ## concatenation is commonly a macro that defines a function so ignore those
5393 if ($has_flow_statement && !$has_arg_concat) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005394 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005395 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005396
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005397 WARN("MACRO_WITH_FLOW_CONTROL",
5398 "Macros with flow control statements should be avoided\n" . "$herectx");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005399 }
5400
5401# check for line continuations outside of #defines, preprocessor #, and asm
5402
5403 } else {
5404 if ($prevline !~ /^..*\\$/ &&
5405 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5406 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5407 $line =~ /^\+.*\\$/) {
5408 WARN("LINE_CONTINUATIONS",
5409 "Avoid unnecessary line continuations\n" . $herecurr);
5410 }
5411 }
5412
5413# do {} while (0) macro tests:
5414# single-statement macros do not need to be enclosed in do while (0) loop,
5415# macro should not end with a semicolon
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005416 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005417 $realfile !~ m@/vmlinux.lds.h$@ &&
5418 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5419 my $ln = $linenr;
5420 my $cnt = $realcnt;
5421 my ($off, $dstat, $dcond, $rest);
5422 my $ctx = '';
5423 ($dstat, $dcond, $ln, $cnt, $off) =
5424 ctx_statement_block($linenr, $realcnt, 0);
5425 $ctx = $dstat;
5426
5427 $dstat =~ s/\\\n.//g;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005428 $dstat =~ s/$;/ /g;
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005429
5430 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5431 my $stmts = $2;
5432 my $semis = $3;
5433
5434 $ctx =~ s/\n*$//;
5435 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005436 my $herectx = get_stat_here($linenr, $cnt, $here);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005437
5438 if (($stmts =~ tr/;/;/) == 1 &&
5439 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5440 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5441 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5442 }
5443 if (defined $semis && $semis ne "") {
5444 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5445 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005446 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005447 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5448 $ctx =~ s/\n*$//;
5449 my $cnt = statement_rawlines($ctx);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005450 my $herectx = get_stat_here($linenr, $cnt, $here);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005451
5452 WARN("TRAILING_SEMICOLON",
5453 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Hershberger05622192011-10-18 10:06:59 +00005454 }
5455 }
5456
Joe Hershberger05622192011-10-18 10:06:59 +00005457# check for redundant bracing round if etc
5458 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5459 my ($level, $endln, @chunks) =
5460 ctx_statement_full($linenr, $realcnt, 1);
5461 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5462 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5463 if ($#chunks > 0 && $level == 0) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005464 my @allowed = ();
5465 my $allow = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005466 my $seen = 0;
5467 my $herectx = $here . "\n";
5468 my $ln = $linenr - 1;
5469 for my $chunk (@chunks) {
5470 my ($cond, $block) = @{$chunk};
5471
5472 # If the condition carries leading newlines, then count those as offsets.
5473 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5474 my $offset = statement_rawlines($whitespace) - 1;
5475
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005476 $allowed[$allow] = 0;
Joe Hershberger05622192011-10-18 10:06:59 +00005477 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5478
5479 # We have looked at and allowed this specific line.
5480 $suppress_ifbraces{$ln + $offset} = 1;
5481
5482 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5483 $ln += statement_rawlines($block) - 1;
5484
5485 substr($block, 0, length($cond), '');
5486
5487 $seen++ if ($block =~ /^\s*{/);
5488
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005489 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Joe Hershberger05622192011-10-18 10:06:59 +00005490 if (statement_lines($cond) > 1) {
5491 #print "APW: ALLOWED: cond<$cond>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005492 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005493 }
5494 if ($block =~/\b(?:if|for|while)\b/) {
5495 #print "APW: ALLOWED: block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005496 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005497 }
5498 if (statement_block_size($block) > 1) {
5499 #print "APW: ALLOWED: lines block<$block>\n";
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005500 $allowed[$allow] = 1;
Joe Hershberger05622192011-10-18 10:06:59 +00005501 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005502 $allow++;
Joe Hershberger05622192011-10-18 10:06:59 +00005503 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005504 if ($seen) {
5505 my $sum_allowed = 0;
5506 foreach (@allowed) {
5507 $sum_allowed += $_;
5508 }
5509 if ($sum_allowed == 0) {
5510 WARN("BRACES",
5511 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5512 } elsif ($sum_allowed != $allow &&
5513 $seen != $allow) {
5514 CHK("BRACES",
5515 "braces {} should be used on all arms of this statement\n" . $herectx);
5516 }
Joe Hershberger05622192011-10-18 10:06:59 +00005517 }
5518 }
5519 }
5520 if (!defined $suppress_ifbraces{$linenr - 1} &&
5521 $line =~ /\b(if|while|for|else)\b/) {
5522 my $allowed = 0;
5523
5524 # Check the pre-context.
5525 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5526 #print "APW: ALLOWED: pre<$1>\n";
5527 $allowed = 1;
5528 }
5529
5530 my ($level, $endln, @chunks) =
5531 ctx_statement_full($linenr, $realcnt, $-[0]);
5532
5533 # Check the condition.
5534 my ($cond, $block) = @{$chunks[0]};
5535 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5536 if (defined $cond) {
5537 substr($block, 0, length($cond), '');
5538 }
5539 if (statement_lines($cond) > 1) {
5540 #print "APW: ALLOWED: cond<$cond>\n";
5541 $allowed = 1;
5542 }
5543 if ($block =~/\b(?:if|for|while)\b/) {
5544 #print "APW: ALLOWED: block<$block>\n";
5545 $allowed = 1;
5546 }
5547 if (statement_block_size($block) > 1) {
5548 #print "APW: ALLOWED: lines block<$block>\n";
5549 $allowed = 1;
5550 }
5551 # Check the post-context.
5552 if (defined $chunks[1]) {
5553 my ($cond, $block) = @{$chunks[1]};
5554 if (defined $cond) {
5555 substr($block, 0, length($cond), '');
5556 }
5557 if ($block =~ /^\s*\{/) {
5558 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5559 $allowed = 1;
5560 }
5561 }
5562 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Joe Hershberger05622192011-10-18 10:06:59 +00005563 my $cnt = statement_rawlines($block);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005564 my $herectx = get_stat_here($linenr, $cnt, $here);
Joe Hershberger05622192011-10-18 10:06:59 +00005565
5566 WARN("BRACES",
5567 "braces {} are not necessary for single statement blocks\n" . $herectx);
5568 }
5569 }
5570
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005571# check for single line unbalanced braces
5572 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5573 $sline =~ /^.\s*else\s*\{\s*$/) {
5574 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5575 }
5576
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005577# check for unnecessary blank lines around braces
Tom Rini6b9709d2014-02-27 08:27:28 -05005578 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005579 if (CHK("BRACES",
5580 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5581 $fix && $prevrawline =~ /^\+/) {
5582 fix_delete_line($fixlinenr - 1, $prevrawline);
5583 }
Joe Hershberger05622192011-10-18 10:06:59 +00005584 }
Tom Rini6b9709d2014-02-27 08:27:28 -05005585 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005586 if (CHK("BRACES",
5587 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5588 $fix) {
5589 fix_delete_line($fixlinenr, $rawline);
5590 }
Joe Hershberger05622192011-10-18 10:06:59 +00005591 }
5592
5593# no volatiles please
5594 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5595 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5596 WARN("VOLATILE",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005597 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5598 }
5599
5600# Check for user-visible strings broken across lines, which breaks the ability
5601# to grep for the string. Make exceptions when the previous string ends in a
5602# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5603# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5604 if ($line =~ /^\+\s*$String/ &&
5605 $prevline =~ /"\s*$/ &&
5606 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5607 if (WARN("SPLIT_STRING",
5608 "quoted string split across lines\n" . $hereprev) &&
5609 $fix &&
5610 $prevrawline =~ /^\+.*"\s*$/ &&
5611 $last_coalesced_string_linenr != $linenr - 1) {
5612 my $extracted_string = get_quoted_string($line, $rawline);
5613 my $comma_close = "";
5614 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5615 $comma_close = $1;
5616 }
5617
5618 fix_delete_line($fixlinenr - 1, $prevrawline);
5619 fix_delete_line($fixlinenr, $rawline);
5620 my $fixedline = $prevrawline;
5621 $fixedline =~ s/"\s*$//;
5622 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5623 fix_insert_line($fixlinenr - 1, $fixedline);
5624 $fixedline = $rawline;
5625 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5626 if ($fixedline !~ /\+\s*$/) {
5627 fix_insert_line($fixlinenr, $fixedline);
5628 }
5629 $last_coalesced_string_linenr = $linenr;
5630 }
5631 }
5632
5633# check for missing a space in a string concatenation
5634 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5635 WARN('MISSING_SPACE',
5636 "break quoted strings at a space character\n" . $hereprev);
5637 }
5638
5639# check for an embedded function name in a string when the function is known
5640# This does not work very well for -f --file checking as it depends on patch
5641# context providing the function name or a single line form for in-file
5642# function declarations
5643 if ($line =~ /^\+.*$String/ &&
5644 defined($context_function) &&
5645 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5646 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5647 WARN("EMBEDDED_FUNCTION_NAME",
5648 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5649 }
5650
5651# check for spaces before a quoted newline
5652 if ($rawline =~ /^.*\".*\s\\n/) {
5653 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5654 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5655 $fix) {
5656 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5657 }
5658
5659 }
5660
5661# concatenated string without spaces between elements
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005662 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5663 if (CHK("CONCATENATED_STRING",
5664 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5665 $fix) {
5666 while ($line =~ /($String)/g) {
5667 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5668 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5669 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5670 }
5671 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005672 }
5673
5674# uncoalesced string fragments
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005675 if ($line =~ /$String\s*"/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005676 if (WARN("STRING_FRAGMENTS",
5677 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5678 $fix) {
5679 while ($line =~ /($String)(?=\s*")/g) {
5680 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5681 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5682 }
5683 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005684 }
5685
5686# check for non-standard and hex prefixed decimal printf formats
5687 my $show_L = 1; #don't show the same defect twice
5688 my $show_Z = 1;
5689 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5690 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5691 $string =~ s/%%/__/g;
5692 # check for %L
5693 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5694 WARN("PRINTF_L",
5695 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5696 $show_L = 0;
5697 }
5698 # check for %Z
5699 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5700 WARN("PRINTF_Z",
5701 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5702 $show_Z = 0;
5703 }
5704 # check for 0x<decimal>
5705 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5706 ERROR("PRINTF_0XDECIMAL",
5707 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5708 }
5709 }
5710
5711# check for line continuations in quoted strings with odd counts of "
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005712 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005713 WARN("LINE_CONTINUATIONS",
5714 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005715 }
5716
5717# warn about #if 0
5718 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005719 WARN("IF_0",
5720 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5721 }
5722
5723# warn about #if 1
5724 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5725 WARN("IF_1",
5726 "Consider removing the #if 1 and its #endif\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005727 }
5728
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005729# check for needless "if (<foo>) fn(<foo>)" uses
5730 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005731 my $tested = quotemeta($1);
5732 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5733 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5734 my $func = $1;
5735 if (WARN('NEEDLESS_IF',
5736 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5737 $fix) {
5738 my $do_fix = 1;
5739 my $leading_tabs = "";
5740 my $new_leading_tabs = "";
5741 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5742 $leading_tabs = $1;
5743 } else {
5744 $do_fix = 0;
5745 }
5746 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5747 $new_leading_tabs = $1;
5748 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5749 $do_fix = 0;
5750 }
5751 } else {
5752 $do_fix = 0;
5753 }
5754 if ($do_fix) {
5755 fix_delete_line($fixlinenr - 1, $prevrawline);
5756 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5757 }
5758 }
5759 }
5760 }
5761
5762# check for unnecessary "Out of Memory" messages
5763 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5764 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5765 (defined $1 || defined $3) &&
5766 $linenr > 3) {
5767 my $testval = $2;
5768 my $testline = $lines[$linenr - 3];
5769
5770 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5771# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5772
Tom Rinic57383b2020-06-16 10:29:46 -04005773 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5774 $s !~ /\b__GFP_NOWARN\b/ ) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005775 WARN("OOM_MESSAGE",
5776 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5777 }
5778 }
5779
5780# check for logging functions with KERN_<LEVEL>
5781 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5782 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5783 my $level = $1;
5784 if (WARN("UNNECESSARY_KERN_LEVEL",
5785 "Possible unnecessary $level\n" . $herecurr) &&
5786 $fix) {
5787 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5788 }
5789 }
5790
5791# check for logging continuations
5792 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5793 WARN("LOGGING_CONTINUATION",
5794 "Avoid logging continuation uses where feasible\n" . $herecurr);
5795 }
5796
5797# check for mask then right shift without a parentheses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005798 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005799 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5800 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5801 WARN("MASK_THEN_SHIFT",
5802 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5803 }
5804
5805# check for pointer comparisons to NULL
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02005806 if ($perl_version_ok) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005807 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5808 my $val = $1;
5809 my $equal = "!";
5810 $equal = "" if ($4 eq "!=");
5811 if (CHK("COMPARISON_TO_NULL",
5812 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5813 $fix) {
5814 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5815 }
Joe Hershberger05622192011-10-18 10:06:59 +00005816 }
5817 }
5818
Tom Rini6b9709d2014-02-27 08:27:28 -05005819# check for bad placement of section $InitAttribute (e.g.: __initdata)
5820 if ($line =~ /(\b$InitAttribute\b)/) {
5821 my $attr = $1;
5822 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5823 my $ptr = $1;
5824 my $var = $2;
5825 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5826 ERROR("MISPLACED_INIT",
5827 "$attr should be placed after $var\n" . $herecurr)) ||
5828 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5829 WARN("MISPLACED_INIT",
5830 "$attr should be placed after $var\n" . $herecurr))) &&
5831 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005832 $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 -05005833 }
5834 }
5835 }
5836
5837# check for $InitAttributeData (ie: __initdata) with const
5838 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5839 my $attr = $1;
5840 $attr =~ /($InitAttributePrefix)(.*)/;
5841 my $attr_prefix = $1;
5842 my $attr_type = $2;
5843 if (ERROR("INIT_ATTRIBUTE",
5844 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5845 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005846 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005847 s/$InitAttributeData/${attr_prefix}initconst/;
5848 }
5849 }
5850
5851# check for $InitAttributeConst (ie: __initconst) without const
5852 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5853 my $attr = $1;
5854 if (ERROR("INIT_ATTRIBUTE",
5855 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5856 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005857 my $lead = $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005858 /(^\+\s*(?:static\s+))/;
5859 $lead = rtrim($1);
5860 $lead = "$lead " if ($lead !~ /^\+$/);
5861 $lead = "${lead}const ";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005862 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5863 }
5864 }
5865
5866# check for __read_mostly with const non-pointer (should just be const)
5867 if ($line =~ /\b__read_mostly\b/ &&
5868 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5869 if (ERROR("CONST_READ_MOSTLY",
5870 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5871 $fix) {
5872 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5873 }
5874 }
5875
5876# don't use __constant_<foo> functions outside of include/uapi/
5877 if ($realfile !~ m@^include/uapi/@ &&
5878 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5879 my $constant_func = $1;
5880 my $func = $constant_func;
5881 $func =~ s/^__constant_//;
5882 if (WARN("CONSTANT_CONVERSION",
5883 "$constant_func should be $func\n" . $herecurr) &&
5884 $fix) {
5885 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05005886 }
5887 }
5888
Joe Hershberger05622192011-10-18 10:06:59 +00005889# prefer usleep_range over udelay
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00005890 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005891 my $delay = $1;
Joe Hershberger05622192011-10-18 10:06:59 +00005892 # ignore udelay's < 10, however
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005893 if (! ($delay < 10) ) {
Joe Hershberger05622192011-10-18 10:06:59 +00005894 CHK("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04005895 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005896 }
5897 if ($delay > 2000) {
5898 WARN("LONG_UDELAY",
5899 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005900 }
5901 }
5902
5903# warn about unexpectedly long msleep's
5904 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5905 if ($1 < 20) {
5906 WARN("MSLEEP",
Tom Rinic57383b2020-06-16 10:29:46 -04005907 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005908 }
5909 }
5910
Tom Rini6b9709d2014-02-27 08:27:28 -05005911# check for comparisons of jiffies
5912 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5913 WARN("JIFFIES_COMPARISON",
5914 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5915 }
5916
5917# check for comparisons of get_jiffies_64()
5918 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5919 WARN("JIFFIES_COMPARISON",
5920 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5921 }
5922
Joe Hershberger05622192011-10-18 10:06:59 +00005923# warn about #ifdefs in C files
5924# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5925# print "#ifdef in C files should be avoided\n";
5926# print "$herecurr";
5927# $clean = 0;
5928# }
5929
5930# warn about spacing in #ifdefs
5931 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005932 if (ERROR("SPACING",
5933 "exactly one space required after that #$1\n" . $herecurr) &&
5934 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005935 $fixed[$fixlinenr] =~
Tom Rini6b9709d2014-02-27 08:27:28 -05005936 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5937 }
5938
Joe Hershberger05622192011-10-18 10:06:59 +00005939 }
5940
5941# check for spinlock_t definitions without a comment.
5942 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5943 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5944 my $which = $1;
5945 if (!ctx_has_comment($first_line, $linenr)) {
5946 CHK("UNCOMMENTED_DEFINITION",
5947 "$1 definition without comment\n" . $herecurr);
5948 }
5949 }
5950# check for memory barriers without a comment.
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005951
5952 my $barriers = qr{
5953 mb|
5954 rmb|
5955 wmb|
5956 read_barrier_depends
5957 }x;
5958 my $barrier_stems = qr{
5959 mb__before_atomic|
5960 mb__after_atomic|
5961 store_release|
5962 load_acquire|
5963 store_mb|
5964 (?:$barriers)
5965 }x;
5966 my $all_barriers = qr{
5967 (?:$barriers)|
5968 smp_(?:$barrier_stems)|
5969 virt_(?:$barrier_stems)
5970 }x;
5971
5972 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00005973 if (!ctx_has_comment($first_line, $linenr)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05005974 WARN("MEMORY_BARRIER",
5975 "memory barrier without comment\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00005976 }
5977 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02005978
5979 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5980
5981 if ($realfile !~ m@^include/asm-generic/@ &&
5982 $realfile !~ m@/barrier\.h$@ &&
5983 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5984 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5985 WARN("MEMORY_BARRIER",
5986 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5987 }
5988
5989# check for waitqueue_active without a comment.
5990 if ($line =~ /\bwaitqueue_active\s*\(/) {
5991 if (!ctx_has_comment($first_line, $linenr)) {
5992 WARN("WAITQUEUE_ACTIVE",
5993 "waitqueue_active without comment\n" . $herecurr);
5994 }
5995 }
5996
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02005997# check for smp_read_barrier_depends and read_barrier_depends
5998 if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
5999 WARN("READ_BARRIER_DEPENDS",
6000 "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
6001 }
6002
Joe Hershberger05622192011-10-18 10:06:59 +00006003# check of hardware specific defines
6004 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6005 CHK("ARCH_DEFINES",
6006 "architecture specific defines should be avoided\n" . $herecurr);
6007 }
6008
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006009# check that the storage class is not after a type
6010 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006011 WARN("STORAGE_CLASS",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006012 "storage class '$2' should be located before type '$1'\n" . $herecurr);
6013 }
6014# Check that the storage class is at the beginning of a declaration
6015 if ($line =~ /\b$Storage\b/ &&
6016 $line !~ /^.\s*$Storage/ &&
6017 $line =~ /^.\s*(.+?)\$Storage\s/ &&
6018 $1 !~ /[\,\)]\s*$/) {
6019 WARN("STORAGE_CLASS",
6020 "storage class should be at the beginning of the declaration\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006021 }
6022
6023# check the location of the inline attribute, that it is between
6024# storage class and type.
6025 if ($line =~ /\b$Type\s+$Inline\b/ ||
6026 $line =~ /\b$Inline\s+$Storage\b/) {
6027 ERROR("INLINE_LOCATION",
6028 "inline keyword should sit between storage class and type\n" . $herecurr);
6029 }
6030
6031# Check for __inline__ and __inline, prefer inline
Tom Rini6b9709d2014-02-27 08:27:28 -05006032 if ($realfile !~ m@\binclude/uapi/@ &&
6033 $line =~ /\b(__inline__|__inline)\b/) {
6034 if (WARN("INLINE",
6035 "plain inline is preferred over $1\n" . $herecurr) &&
6036 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006037 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006038
6039 }
Joe Hershberger05622192011-10-18 10:06:59 +00006040 }
6041
6042# Check for __attribute__ packed, prefer __packed
Tom Rini6b9709d2014-02-27 08:27:28 -05006043 if ($realfile !~ m@\binclude/uapi/@ &&
6044 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006045 WARN("PREFER_PACKED",
6046 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
6047 }
6048
6049# Check for __attribute__ aligned, prefer __aligned
Tom Rini6b9709d2014-02-27 08:27:28 -05006050 if ($realfile !~ m@\binclude/uapi/@ &&
6051 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006052 WARN("PREFER_ALIGNED",
6053 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
6054 }
6055
Tom Rinic57383b2020-06-16 10:29:46 -04006056# Check for __attribute__ section, prefer __section
6057 if ($realfile !~ m@\binclude/uapi/@ &&
6058 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6059 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6060 my $new = substr($old, 1, -1);
6061 if (WARN("PREFER_SECTION",
6062 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6063 $fix) {
6064 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6065 }
6066 }
6067
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006068# Check for __attribute__ format(printf, prefer __printf
Tom Rini6b9709d2014-02-27 08:27:28 -05006069 if ($realfile !~ m@\binclude/uapi/@ &&
6070 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6071 if (WARN("PREFER_PRINTF",
6072 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6073 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006074 $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 -05006075
6076 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006077 }
6078
6079# Check for __attribute__ format(scanf, prefer __scanf
Tom Rini6b9709d2014-02-27 08:27:28 -05006080 if ($realfile !~ m@\binclude/uapi/@ &&
6081 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6082 if (WARN("PREFER_SCANF",
6083 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6084 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006085 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6086 }
6087 }
6088
6089# Check for __attribute__ weak, or __weak declarations (may have link issues)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006090 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006091 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6092 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6093 $line =~ /\b__weak\b/)) {
6094 ERROR("WEAK_DECLARATION",
6095 "Using weak declarations can have unintended link defects\n" . $herecurr);
6096 }
6097
6098# check for c99 types like uint8_t used outside of uapi/ and tools/
6099 if ($realfile !~ m@\binclude/uapi/@ &&
6100 $realfile !~ m@\btools/@ &&
6101 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6102 my $type = $1;
6103 if ($type =~ /\b($typeC99Typedefs)\b/) {
6104 $type = $1;
6105 my $kernel_type = 'u';
6106 $kernel_type = 's' if ($type =~ /^_*[si]/);
6107 $type =~ /(\d+)/;
6108 $kernel_type .= $1;
6109 if (CHK("PREFER_KERNEL_TYPES",
6110 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6111 $fix) {
6112 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6113 }
6114 }
6115 }
6116
6117# check for cast of C90 native int or longer types constants
6118 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6119 my $cast = $1;
6120 my $const = $2;
6121 if (WARN("TYPECAST_INT_CONSTANT",
6122 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6123 $fix) {
6124 my $suffix = "";
6125 my $newconst = $const;
6126 $newconst =~ s/${Int_type}$//;
6127 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6128 if ($cast =~ /\blong\s+long\b/) {
6129 $suffix .= 'LL';
6130 } elsif ($cast =~ /\blong\b/) {
6131 $suffix .= 'L';
6132 }
6133 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006134 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006135 }
6136
Joe Hershberger05622192011-10-18 10:06:59 +00006137# check for sizeof(&)
6138 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6139 WARN("SIZEOF_ADDRESS",
6140 "sizeof(& should be avoided\n" . $herecurr);
6141 }
6142
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006143# check for sizeof without parenthesis
6144 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006145 if (WARN("SIZEOF_PARENTHESIS",
6146 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6147 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006148 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Tom Rini6b9709d2014-02-27 08:27:28 -05006149 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006150 }
6151
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006152# check for struct spinlock declarations
6153 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6154 WARN("USE_SPINLOCK_T",
6155 "struct spinlock should be spinlock_t\n" . $herecurr);
6156 }
6157
Tom Rini6b9709d2014-02-27 08:27:28 -05006158# check for seq_printf uses that could be seq_puts
6159 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6160 my $fmt = get_quoted_string($line, $rawline);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006161 $fmt =~ s/%%//g;
6162 if ($fmt !~ /%/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006163 if (WARN("PREFER_SEQ_PUTS",
6164 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6165 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006166 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006167 }
6168 }
6169 }
6170
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006171# check for vsprintf extension %p<foo> misuses
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006172 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006173 defined $stat &&
6174 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6175 $1 !~ /^_*volatile_*$/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006176 my $stat_real;
6177
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006178 my $lc = $stat =~ tr@\n@@;
6179 $lc = $lc + $linenr;
6180 for (my $count = $linenr; $count <= $lc; $count++) {
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006181 my $specifier;
6182 my $extension;
Tom Rinic57383b2020-06-16 10:29:46 -04006183 my $qualifier;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006184 my $bad_specifier = "";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006185 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6186 $fmt =~ s/%%//g;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006187
Tom Rinic57383b2020-06-16 10:29:46 -04006188 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006189 $specifier = $1;
6190 $extension = $2;
Tom Rinic57383b2020-06-16 10:29:46 -04006191 $qualifier = $3;
6192 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6193 ($extension eq "f" &&
6194 defined $qualifier && $qualifier !~ /^w/)) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006195 $bad_specifier = $specifier;
6196 last;
6197 }
6198 if ($extension eq "x" && !defined($stat_real)) {
6199 if (!defined($stat_real)) {
6200 $stat_real = get_stat_real($linenr, $lc);
6201 }
6202 WARN("VSPRINTF_SPECIFIER_PX",
6203 "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");
6204 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006205 }
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006206 if ($bad_specifier ne "") {
6207 my $stat_real = get_stat_real($linenr, $lc);
6208 my $ext_type = "Invalid";
6209 my $use = "";
6210 if ($bad_specifier =~ /p[Ff]/) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006211 $use = " - use %pS instead";
6212 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6213 }
6214
6215 WARN("VSPRINTF_POINTER_EXTENSION",
6216 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006217 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006218 }
6219 }
6220
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006221# Check for misused memsets
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006222 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006223 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006224 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006225
6226 my $ms_addr = $2;
6227 my $ms_val = $7;
6228 my $ms_size = $12;
6229
6230 if ($ms_size =~ /^(0x|)0$/i) {
6231 ERROR("MEMSET",
6232 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6233 } elsif ($ms_size =~ /^(0x|)1$/i) {
6234 WARN("MEMSET",
6235 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6236 }
6237 }
6238
Tom Rini6b9709d2014-02-27 08:27:28 -05006239# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006240# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006241# defined $stat &&
6242# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6243# if (WARN("PREFER_ETHER_ADDR_COPY",
6244# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6245# $fix) {
6246# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6247# }
6248# }
6249
6250# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006251# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006252# defined $stat &&
6253# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6254# WARN("PREFER_ETHER_ADDR_EQUAL",
6255# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6256# }
6257
6258# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6259# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006260# if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006261# defined $stat &&
6262# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6263#
6264# my $ms_val = $7;
6265#
6266# if ($ms_val =~ /^(?:0x|)0+$/i) {
6267# if (WARN("PREFER_ETH_ZERO_ADDR",
6268# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6269# $fix) {
6270# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6271# }
6272# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6273# if (WARN("PREFER_ETH_BROADCAST_ADDR",
6274# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6275# $fix) {
6276# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6277# }
6278# }
6279# }
Tom Rini6b9709d2014-02-27 08:27:28 -05006280
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006281# typecasts on min/max could be min_t/max_t
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006282 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006283 defined $stat &&
6284 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6285 if (defined $2 || defined $7) {
6286 my $call = $1;
6287 my $cast1 = deparenthesize($2);
6288 my $arg1 = $3;
6289 my $cast2 = deparenthesize($7);
6290 my $arg2 = $8;
6291 my $cast;
6292
6293 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6294 $cast = "$cast1 or $cast2";
6295 } elsif ($cast1 ne "") {
6296 $cast = $cast1;
6297 } else {
6298 $cast = $cast2;
6299 }
6300 WARN("MINMAX",
6301 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6302 }
6303 }
6304
6305# check usleep_range arguments
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006306 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006307 defined $stat &&
6308 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6309 my $min = $1;
6310 my $max = $7;
6311 if ($min eq $max) {
6312 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006313 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006314 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6315 $min > $max) {
6316 WARN("USLEEP_RANGE",
Tom Rinic57383b2020-06-16 10:29:46 -04006317 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006318 }
6319 }
6320
Tom Rini6b9709d2014-02-27 08:27:28 -05006321# check for naked sscanf
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006322 if ($perl_version_ok &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006323 defined $stat &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006324 $line =~ /\bsscanf\b/ &&
Tom Rini6b9709d2014-02-27 08:27:28 -05006325 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6326 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6327 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6328 my $lc = $stat =~ tr@\n@@;
6329 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006330 my $stat_real = get_stat_real($linenr, $lc);
Tom Rini6b9709d2014-02-27 08:27:28 -05006331 WARN("NAKED_SSCANF",
6332 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6333 }
6334
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006335# check for simple sscanf that should be kstrto<foo>
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006336 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006337 defined $stat &&
6338 $line =~ /\bsscanf\b/) {
6339 my $lc = $stat =~ tr@\n@@;
6340 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006341 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006342 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6343 my $format = $6;
6344 my $count = $format =~ tr@%@%@;
6345 if ($count == 1 &&
6346 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6347 WARN("SSCANF_TO_KSTRTO",
6348 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6349 }
6350 }
6351 }
6352
Tom Rini6b9709d2014-02-27 08:27:28 -05006353# check for new externs in .h files.
6354 if ($realfile =~ /\.h$/ &&
6355 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6356 if (CHK("AVOID_EXTERNS",
6357 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6358 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006359 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Tom Rini6b9709d2014-02-27 08:27:28 -05006360 }
6361 }
6362
Joe Hershberger05622192011-10-18 10:06:59 +00006363# check for new externs in .c files.
6364 if ($realfile =~ /\.c$/ && defined $stat &&
6365 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6366 {
6367 my $function_name = $1;
6368 my $paren_space = $2;
6369
6370 my $s = $stat;
6371 if (defined $cond) {
6372 substr($s, 0, length($cond), '');
6373 }
6374 if ($s =~ /^\s*;/ &&
6375 $function_name ne 'uninitialized_var')
6376 {
6377 WARN("AVOID_EXTERNS",
6378 "externs should be avoided in .c files\n" . $herecurr);
6379 }
6380
6381 if ($paren_space =~ /\n/) {
6382 WARN("FUNCTION_ARGUMENTS",
6383 "arguments for function declarations should follow identifier\n" . $herecurr);
6384 }
6385
6386 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6387 $stat =~ /^.\s*extern\s+/)
6388 {
6389 WARN("AVOID_EXTERNS",
6390 "externs should be avoided in .c files\n" . $herecurr);
6391 }
6392
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006393# check for function declarations that have arguments without identifier names
Tom Rinic57383b2020-06-16 10:29:46 -04006394# while avoiding uninitialized_var(x)
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006395 if (defined $stat &&
Tom Rinic57383b2020-06-16 10:29:46 -04006396 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:($Ident)|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6397 (!defined($1) ||
6398 (defined($1) && $1 ne "uninitialized_var")) &&
6399 $2 ne "void") {
6400 my $args = trim($2);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006401 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6402 my $arg = trim($1);
Tom Rinic57383b2020-06-16 10:29:46 -04006403 if ($arg =~ /^$Type$/ &&
6404 $arg !~ /enum\s+$Ident$/) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006405 WARN("FUNCTION_ARGUMENTS",
6406 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6407 }
6408 }
6409 }
6410
6411# check for function definitions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006412 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006413 defined $stat &&
6414 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6415 $context_function = $1;
6416
6417# check for multiline function definition with misplaced open brace
6418 my $ok = 0;
6419 my $cnt = statement_rawlines($stat);
6420 my $herectx = $here . "\n";
6421 for (my $n = 0; $n < $cnt; $n++) {
6422 my $rl = raw_line($linenr, $n);
6423 $herectx .= $rl . "\n";
6424 $ok = 1 if ($rl =~ /^[ \+]\{/);
6425 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6426 last if $rl =~ /^[ \+].*\{/;
6427 }
6428 if (!$ok) {
6429 ERROR("OPEN_BRACE",
6430 "open brace '{' following function definitions go on the next line\n" . $herectx);
6431 }
6432 }
6433
Joe Hershberger05622192011-10-18 10:06:59 +00006434# checks for new __setup's
6435 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6436 my $name = $1;
6437
6438 if (!grep(/$name/, @setup_docs)) {
6439 CHK("UNDOCUMENTED_SETUP",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006440 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006441 }
6442 }
6443
Tom Rinic57383b2020-06-16 10:29:46 -04006444# check for pointless casting of alloc functions
6445 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006446 WARN("UNNECESSARY_CASTS",
6447 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6448 }
6449
Tom Rini6b9709d2014-02-27 08:27:28 -05006450# alloc style
6451# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006452 if ($perl_version_ok &&
Tom Rinic57383b2020-06-16 10:29:46 -04006453 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006454 CHK("ALLOC_SIZEOF_STRUCT",
6455 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6456 }
6457
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006458# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006459 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006460 defined $stat &&
6461 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6462 my $oldfunc = $3;
6463 my $a1 = $4;
6464 my $a2 = $10;
6465 my $newfunc = "kmalloc_array";
6466 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6467 my $r1 = $a1;
6468 my $r2 = $a2;
6469 if ($a1 =~ /^sizeof\s*\S/) {
6470 $r1 = $a2;
6471 $r2 = $a1;
6472 }
6473 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6474 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006475 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006476 my $herectx = get_stat_here($linenr, $cnt, $here);
6477
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006478 if (WARN("ALLOC_WITH_MULTIPLY",
6479 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6480 $cnt == 1 &&
6481 $fix) {
6482 $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;
6483 }
6484 }
6485 }
6486
Tom Rini6b9709d2014-02-27 08:27:28 -05006487# check for krealloc arg reuse
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006488 if ($perl_version_ok &&
6489 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6490 $1 eq $3) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006491 WARN("KREALLOC_ARG_REUSE",
6492 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6493 }
6494
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006495# check for alloc argument mismatch
6496 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6497 WARN("ALLOC_ARRAY_ARGS",
6498 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006499 }
6500
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006501# check for multiple semicolons
6502 if ($line =~ /;\s*;\s*$/) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006503 if (WARN("ONE_SEMICOLON",
6504 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6505 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006506 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6507 }
6508 }
6509
6510# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6511 if ($realfile !~ m@^include/uapi/@ &&
6512 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6513 my $ull = "";
6514 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6515 if (CHK("BIT_MACRO",
6516 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6517 $fix) {
6518 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6519 }
6520 }
6521
6522# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6523 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6524 my $config = $1;
6525 if (WARN("PREFER_IS_ENABLED",
6526 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6527 $fix) {
6528 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
Tom Rini6b9709d2014-02-27 08:27:28 -05006529 }
6530 }
6531
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006532# check for case / default statements not preceded by break/fallthrough/switch
Tom Rini6b9709d2014-02-27 08:27:28 -05006533 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6534 my $has_break = 0;
6535 my $has_statement = 0;
6536 my $count = 0;
6537 my $prevline = $linenr;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006538 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006539 $prevline--;
6540 my $rline = $rawlines[$prevline - 1];
6541 my $fline = $lines[$prevline - 1];
6542 last if ($fline =~ /^\@\@/);
6543 next if ($fline =~ /^\-/);
6544 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6545 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6546 next if ($fline =~ /^.[\s$;]*$/);
6547 $has_statement = 1;
6548 $count++;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006549 $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 -05006550 }
6551 if (!$has_break && $has_statement) {
6552 WARN("MISSING_BREAK",
Robert P. J. Dayfc0b5942016-09-07 14:27:59 -04006553 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Tom Rini6b9709d2014-02-27 08:27:28 -05006554 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006555 }
6556
Tom Rinic57383b2020-06-16 10:29:46 -04006557# check for /* fallthrough */ like comment, prefer fallthrough;
6558 my @fallthroughs = (
6559 'fallthrough',
6560 '@fallthrough@',
6561 'lint -fallthrough[ \t]*',
6562 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6563 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6564 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6565 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6566 );
6567 if ($raw_comment ne '') {
6568 foreach my $ft (@fallthroughs) {
6569 if ($raw_comment =~ /$ft/) {
6570 my $msg_level = \&WARN;
6571 $msg_level = \&CHK if ($file);
6572 &{$msg_level}("PREFER_FALLTHROUGH",
6573 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6574 last;
6575 }
6576 }
6577 }
6578
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006579# check for switch/default statements without a break;
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006580 if ($perl_version_ok &&
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006581 defined $stat &&
6582 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006583 my $cnt = statement_rawlines($stat);
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006584 my $herectx = get_stat_here($linenr, $cnt, $here);
6585
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006586 WARN("DEFAULT_NO_BREAK",
6587 "switch default: should use break\n" . $herectx);
Eric Nelson172a3a82012-05-02 20:32:18 -07006588 }
6589
Joe Hershberger05622192011-10-18 10:06:59 +00006590# check for gcc specific __FUNCTION__
Tom Rini6b9709d2014-02-27 08:27:28 -05006591 if ($line =~ /\b__FUNCTION__\b/) {
6592 if (WARN("USE_FUNC",
6593 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6594 $fix) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006595 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Tom Rini6b9709d2014-02-27 08:27:28 -05006596 }
Joe Hershberger05622192011-10-18 10:06:59 +00006597 }
6598
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006599# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6600 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6601 ERROR("DATE_TIME",
6602 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6603 }
6604
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006605# check for use of yield()
6606 if ($line =~ /\byield\s*\(\s*\)/) {
6607 WARN("YIELD",
6608 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6609 }
6610
Tom Rini6b9709d2014-02-27 08:27:28 -05006611# check for comparisons against true and false
6612 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6613 my $lead = $1;
6614 my $arg = $2;
6615 my $test = $3;
6616 my $otype = $4;
6617 my $trail = $5;
6618 my $op = "!";
6619
6620 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6621
6622 my $type = lc($otype);
6623 if ($type =~ /^(?:true|false)$/) {
6624 if (("$test" eq "==" && "$type" eq "true") ||
6625 ("$test" eq "!=" && "$type" eq "false")) {
6626 $op = "";
6627 }
6628
6629 CHK("BOOL_COMPARISON",
6630 "Using comparison to $otype is error prone\n" . $herecurr);
6631
6632## maybe suggesting a correct construct would better
6633## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6634
6635 }
6636 }
6637
Joe Hershberger05622192011-10-18 10:06:59 +00006638# check for semaphores initialized locked
6639 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6640 WARN("CONSIDER_COMPLETION",
6641 "consider using a completion\n" . $herecurr);
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006642 }
Joe Hershberger05622192011-10-18 10:06:59 +00006643
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006644# recommend kstrto* over simple_strto* and strict_strto*
6645 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006646 WARN("CONSIDER_KSTRTO",
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006647 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006648 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006649
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006650# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Joe Hershberger05622192011-10-18 10:06:59 +00006651 if ($line =~ /^.\s*__initcall\s*\(/) {
6652 WARN("USE_DEVICE_INITCALL",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006653 "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 +00006654 }
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006655
Tom Rinic57383b2020-06-16 10:29:46 -04006656# check for spin_is_locked(), suggest lockdep instead
6657 if ($line =~ /\bspin_is_locked\(/) {
6658 WARN("USE_LOCKDEP",
6659 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6660 }
6661
6662# check for deprecated apis
6663 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6664 my $deprecated_api = $1;
6665 my $new_api = $deprecated_apis{$deprecated_api};
6666 WARN("DEPRECATED_API",
6667 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6668 }
6669
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006670# check for various structs that are normally const (ops, kgdb, device_tree)
6671# and avoid what seem like struct definitions 'struct foo {'
Joe Hershberger05622192011-10-18 10:06:59 +00006672 if ($line !~ /\bconst\b/ &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006673 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006674 WARN("CONST_STRUCT",
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006675 "struct $1 should normally be const\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006676 }
6677
6678# use of NR_CPUS is usually wrong
6679# ignore definitions of NR_CPUS and usage to define arrays as likely right
6680 if ($line =~ /\bNR_CPUS\b/ &&
6681 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6682 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6683 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6684 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6685 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6686 {
6687 WARN("NR_CPUS",
6688 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6689 }
6690
Tom Rini6b9709d2014-02-27 08:27:28 -05006691# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6692 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6693 ERROR("DEFINE_ARCH_HAS",
6694 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6695 }
6696
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006697# likely/unlikely comparisons similar to "(likely(foo) > 0)"
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006698 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006699 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6700 WARN("LIKELY_MISUSE",
6701 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
Joe Hershberger05622192011-10-18 10:06:59 +00006702 }
6703
Tom Rinic57383b2020-06-16 10:29:46 -04006704# nested likely/unlikely calls
6705 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6706 WARN("LIKELY_MISUSE",
6707 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6708 }
6709
Joe Hershberger05622192011-10-18 10:06:59 +00006710# whine mightly about in_atomic
6711 if ($line =~ /\bin_atomic\s*\(/) {
6712 if ($realfile =~ m@^drivers/@) {
6713 ERROR("IN_ATOMIC",
6714 "do not use in_atomic in drivers\n" . $herecurr);
6715 } elsif ($realfile !~ m@^kernel/@) {
6716 WARN("IN_ATOMIC",
6717 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6718 }
6719 }
6720
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006721# check for mutex_trylock_recursive usage
6722 if ($line =~ /mutex_trylock_recursive/) {
6723 ERROR("LOCKING",
6724 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6725 }
6726
Joe Hershberger05622192011-10-18 10:06:59 +00006727# check for lockdep_set_novalidate_class
6728 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6729 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6730 if ($realfile !~ m@^kernel/lockdep@ &&
6731 $realfile !~ m@^include/linux/lockdep@ &&
6732 $realfile !~ m@^drivers/base/core@) {
6733 ERROR("LOCKDEP",
6734 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6735 }
6736 }
6737
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006738 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6739 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006740 WARN("EXPORTED_WORLD_WRITABLE",
6741 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6742 }
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006743
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006744# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6745# and whether or not function naming is typical and if
6746# DEVICE_ATTR permissions uses are unusual too
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006747 if ($perl_version_ok &&
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006748 defined $stat &&
6749 $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*\)/) {
6750 my $var = $1;
6751 my $perms = $2;
6752 my $show = $3;
6753 my $store = $4;
6754 my $octal_perms = perms_to_octal($perms);
6755 if ($show =~ /^${var}_show$/ &&
6756 $store =~ /^${var}_store$/ &&
6757 $octal_perms eq "0644") {
6758 if (WARN("DEVICE_ATTR_RW",
6759 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6760 $fix) {
6761 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6762 }
6763 } elsif ($show =~ /^${var}_show$/ &&
6764 $store =~ /^NULL$/ &&
6765 $octal_perms eq "0444") {
6766 if (WARN("DEVICE_ATTR_RO",
6767 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6768 $fix) {
6769 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6770 }
6771 } elsif ($show =~ /^NULL$/ &&
6772 $store =~ /^${var}_store$/ &&
6773 $octal_perms eq "0200") {
6774 if (WARN("DEVICE_ATTR_WO",
6775 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6776 $fix) {
6777 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6778 }
6779 } elsif ($octal_perms eq "0644" ||
6780 $octal_perms eq "0444" ||
6781 $octal_perms eq "0200") {
6782 my $newshow = "$show";
6783 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6784 my $newstore = $store;
6785 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6786 my $rename = "";
6787 if ($show ne $newshow) {
6788 $rename .= " '$show' to '$newshow'";
6789 }
6790 if ($store ne $newstore) {
6791 $rename .= " '$store' to '$newstore'";
6792 }
6793 WARN("DEVICE_ATTR_FUNCTIONS",
6794 "Consider renaming function(s)$rename\n" . $herecurr);
6795 } else {
6796 WARN("DEVICE_ATTR_PERMS",
6797 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6798 }
6799 }
6800
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006801# Mode permission misuses where it seems decimal should be octal
6802# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006803# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6804# specific definition of not visible in sysfs.
6805# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6806# use the default permissions
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006807 if ($perl_version_ok &&
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006808 defined $stat &&
6809 $line =~ /$mode_perms_search/) {
6810 foreach my $entry (@mode_permission_funcs) {
6811 my $func = $entry->[0];
6812 my $arg_pos = $entry->[1];
6813
6814 my $lc = $stat =~ tr@\n@@;
6815 $lc = $lc + $linenr;
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006816 my $stat_real = get_stat_real($linenr, $lc);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006817
6818 my $skip_args = "";
6819 if ($arg_pos > 1) {
6820 $arg_pos--;
6821 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6822 }
6823 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6824 if ($stat =~ /$test/) {
6825 my $val = $1;
6826 $val = $6 if ($skip_args ne "");
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006827 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6828 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6829 ($val =~ /^$Octal$/ && length($val) ne 4))) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006830 ERROR("NON_OCTAL_PERMISSIONS",
6831 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6832 }
6833 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6834 ERROR("EXPORTED_WORLD_WRITABLE",
6835 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6836 }
6837 }
6838 }
6839 }
6840
6841# check for uses of S_<PERMS> that could be octal for readability
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006842 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6843 my $oval = $1;
6844 my $octal = perms_to_octal($oval);
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006845 if (WARN("SYMBOLIC_PERMS",
6846 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6847 $fix) {
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006848 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006849 }
6850 }
6851
6852# validate content of MODULE_LICENSE against list from include/linux/module.h
6853 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6854 my $extracted_string = get_quoted_string($line, $rawline);
6855 my $valid_licenses = qr{
6856 GPL|
6857 GPL\ v2|
6858 GPL\ and\ additional\ rights|
6859 Dual\ BSD/GPL|
6860 Dual\ MIT/GPL|
6861 Dual\ MPL/GPL|
6862 Proprietary
6863 }x;
6864 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6865 WARN("MODULE_LICENSE",
6866 "unknown module license " . $extracted_string . "\n" . $herecurr);
6867 }
6868 }
Tom Rinic57383b2020-06-16 10:29:46 -04006869
6870# check for sysctl duplicate constants
6871 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6872 WARN("DUPLICATED_SYSCTL_CONST",
6873 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6874 }
Joe Hershberger05622192011-10-18 10:06:59 +00006875 }
6876
6877 # If we have no input at all, then there is nothing to report on
6878 # so just keep quiet.
6879 if ($#rawlines == -1) {
6880 exit(0);
6881 }
6882
6883 # In mailback mode only produce a report in the negative, for
6884 # things that appear to be patches.
6885 if ($mailback && ($clean == 1 || !$is_patch)) {
6886 exit(0);
6887 }
6888
6889 # This is not a patch, and we are are in 'no-patch' mode so
6890 # just keep quiet.
6891 if (!$chk_patch && !$is_patch) {
6892 exit(0);
6893 }
6894
Heinrich Schuchardtc398f2d2018-04-04 15:39:20 +02006895 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
Joe Hershberger05622192011-10-18 10:06:59 +00006896 ERROR("NOT_UNIFIED_DIFF",
6897 "Does not appear to be a unified-diff format patch\n");
6898 }
Heinrich Schuchardtc261fef2019-10-19 09:06:38 +02006899 if ($is_patch && $has_commit_log && $chk_signoff) {
6900 if ($signoff == 0) {
6901 ERROR("MISSING_SIGN_OFF",
6902 "Missing Signed-off-by: line(s)\n");
6903 } elsif (!$authorsignoff) {
6904 WARN("NO_AUTHOR_SIGN_OFF",
6905 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6906 }
Joe Hershberger05622192011-10-18 10:06:59 +00006907 }
6908
6909 print report_dump();
6910 if ($summary && !($clean == 1 && $quiet == 1)) {
6911 print "$filename " if ($summary_file);
6912 print "total: $cnt_error errors, $cnt_warn warnings, " .
6913 (($check)? "$cnt_chk checks, " : "") .
6914 "$cnt_lines lines checked\n";
Joe Hershberger05622192011-10-18 10:06:59 +00006915 }
6916
6917 if ($quiet == 0) {
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006918 # If there were any defects found and not already fixing them
6919 if (!$clean and !$fix) {
6920 print << "EOM"
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006921
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006922NOTE: For some of the reported defects, checkpatch may be able to
6923 mechanically convert to the typical style using --fix or --fix-inplace.
6924EOM
Kim Phillipsd45a6ae2013-02-28 12:53:52 +00006925 }
Joe Hershberger05622192011-10-18 10:06:59 +00006926 # If there were whitespace errors which cleanpatch can fix
6927 # then suggest that.
6928 if ($rpt_cleaners) {
Joe Hershberger05622192011-10-18 10:06:59 +00006929 $rpt_cleaners = 0;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006930 print << "EOM"
6931
6932NOTE: Whitespace errors detected.
6933 You may wish to use scripts/cleanpatch or scripts/cleanfile
6934EOM
Joe Hershberger05622192011-10-18 10:06:59 +00006935 }
6936 }
6937
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006938 if ($clean == 0 && $fix &&
6939 ("@rawlines" ne "@fixed" ||
6940 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Tom Rini6b9709d2014-02-27 08:27:28 -05006941 my $newfile = $filename;
6942 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6943 my $linecount = 0;
6944 my $f;
6945
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006946 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6947
Tom Rini6b9709d2014-02-27 08:27:28 -05006948 open($f, '>', $newfile)
6949 or die "$P: Can't open $newfile for write\n";
6950 foreach my $fixed_line (@fixed) {
6951 $linecount++;
6952 if ($file) {
6953 if ($linecount > 3) {
6954 $fixed_line =~ s/^\+//;
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006955 print $f $fixed_line . "\n";
Tom Rini6b9709d2014-02-27 08:27:28 -05006956 }
6957 } else {
6958 print $f $fixed_line . "\n";
6959 }
6960 }
6961 close($f);
6962
6963 if (!$quiet) {
6964 print << "EOM";
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006965
Tom Rini6b9709d2014-02-27 08:27:28 -05006966Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6967
6968Do _NOT_ trust the results written to this file.
6969Do _NOT_ submit these changes without inspecting them for correctness.
6970
6971This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6972No warranties, expressed or implied...
Tom Rini6b9709d2014-02-27 08:27:28 -05006973EOM
6974 }
Joe Hershberger05622192011-10-18 10:06:59 +00006975 }
6976
Heinrich Schuchardt6305db92017-09-12 09:57:45 +02006977 if ($quiet == 0) {
6978 print "\n";
6979 if ($clean == 1) {
6980 print "$vname has no obvious style problems and is ready for submission.\n";
6981 } else {
6982 print "$vname has style problems, please review.\n";
6983 }
Joe Hershberger05622192011-10-18 10:06:59 +00006984 }
Joe Hershberger05622192011-10-18 10:06:59 +00006985 return $clean;
6986}