blob: c141b05c391a1106fca654feb0919355d60ccd92 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
David Keitel72bf3e82016-03-23 20:52:44 -070014use constant BEFORE_SHORTTEXT => 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -080015use constant IN_SHORTTEXT_BLANKLINE => 1;
16use constant IN_SHORTTEXT => 2;
17use constant AFTER_SHORTTEXT => 3;
18use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel72bf3e82016-03-23 20:52:44 -070019use constant SHORTTEXT_LIMIT => 75;
20
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080022my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070023
Joe Perches000d1cc12011-07-25 17:13:25 -070024my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070025
26use Getopt::Long qw(:config no_auto_abbrev);
27
28my $quiet = 0;
29my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080032my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070033my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070036my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070038my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070039my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070040my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070041my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080042my $summary = 1;
43my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070045my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070046my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070047my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080048my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070049my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080050my %debug;
Joe Perches34456862013-07-03 15:05:34 -070051my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070052my %use_type = ();
53my @use = ();
54my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070055my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070056my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070057my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080058my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070059my $ignore_perl_version = 0;
60my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070061my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070062my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070063my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070064my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070065my $conststructsfile = "$D/const_structs.checkpatch";
Joe Perches57230292015-06-25 15:03:03 -070066my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070067my $allow_c99_comments = 1;
Maya Erezec77bd62017-02-26 09:14:43 +020068my $qca_sign_off = 0;
69my $codeaurora_sign_off = 0;
Hannes Eder77f5b102009-09-21 17:04:37 -070070
71sub help {
72 my ($exitcode) = @_;
73
74 print << "EOM";
75Usage: $P [OPTION]... [FILE]...
76Version: $V
77
78Options:
79 -q, --quiet quiet
80 --no-tree run without a kernel tree
81 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080082 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070083 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800103 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700104 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
111 is all off)
112 --test-only=WORD report only warnings/errors containing WORD
113 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
118 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700122 --ignore-perl-version override checking of perl version. expect
123 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700125 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700126 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700127 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Joe Perches3beb42e2016-05-20 17:04:14 -0700136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
155 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
156 push (@types, $_);
157 }
158 @types = sort(uniq(@types));
159 print("#\tMessage type\n\n");
160 foreach my $type (@types) {
161 print(++$count . "\t" . $type . "\n");
162 }
163
164 exit($exitcode);
165}
166
Joe Perches000d1cc12011-07-25 17:13:25 -0700167my $conf = which_conf($configuration_file);
168if (-f $conf) {
169 my @conf_args;
170 open(my $conffile, '<', "$conf")
171 or warn "$P: Can't find a readable $configuration_file file $!\n";
172
173 while (<$conffile>) {
174 my $line = $_;
175
176 $line =~ s/\s*\n?$//g;
177 $line =~ s/^\s*//g;
178 $line =~ s/\s+/ /g;
179
180 next if ($line =~ m/^\s*#/);
181 next if ($line =~ m/^\s*$/);
182
183 my @words = split(" ", $line);
184 foreach my $word (@words) {
185 last if ($word =~ m/^#/);
186 push (@conf_args, $word);
187 }
188 }
189 close($conffile);
190 unshift(@ARGV, @conf_args) if @conf_args;
191}
192
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700193GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700195 'tree!' => \$tree,
196 'signoff!' => \$chk_signoff,
197 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800198 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800200 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700201 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700202 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700203 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 'subjective!' => \$check,
205 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700206 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700207 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700208 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700209 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800210 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700211 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213 'summary!' => \$summary,
214 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800215 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700216 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800217 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700218 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700220 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700221 'codespell!' => \$codespell,
222 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700223 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700224 'h|help' => \$help,
225 'version' => \$help
226) or help(1);
227
228help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700229
Joe Perches3beb42e2016-05-20 17:04:14 -0700230list_types(0) if ($list_types);
231
Joe Perches9624b8d2014-01-23 15:54:44 -0800232$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700233$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800234
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700235my $exit = 0;
236
Dave Hansend62a2012013-09-11 14:23:56 -0700237if ($^V && $^V lt $minimum_perl_version) {
238 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
239 if (!$ignore_perl_version) {
240 exit(1);
241 }
242}
243
Allen Hubbe45107ff2016-08-02 14:04:47 -0700244#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700245if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700246 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700247}
248
Joe Perches91bfe482013-09-11 14:23:59 -0700249sub hash_save_array_words {
250 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700251
Joe Perches91bfe482013-09-11 14:23:59 -0700252 my @array = split(/,/, join(',', @$arrayRef));
253 foreach my $word (@array) {
254 $word =~ s/\s*\n?$//g;
255 $word =~ s/^\s*//g;
256 $word =~ s/\s+/ /g;
257 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700258
Joe Perches91bfe482013-09-11 14:23:59 -0700259 next if ($word =~ m/^\s*#/);
260 next if ($word =~ m/^\s*$/);
261
262 $hashRef->{$word}++;
263 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700264}
265
Joe Perches91bfe482013-09-11 14:23:59 -0700266sub hash_show_words {
267 my ($hashRef, $prefix) = @_;
268
Joe Perches3c816e42015-06-25 15:03:29 -0700269 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700270 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700271 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700272 print " $word";
273 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700274 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700275 }
276}
277
278hash_save_array_words(\%ignore_type, \@ignore);
279hash_save_array_words(\%use_type, \@use);
280
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281my $dbg_values = 0;
282my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700283my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700284my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800285for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 ## no critic
287 eval "\${dbg_$key} = '$debug{$key}';";
288 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800289}
290
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700291my $rpt_cleaners = 0;
292
Andy Whitcroft8905a672007-11-28 16:21:06 -0800293if ($terse) {
294 $emacs = 1;
295 $quiet++;
296}
297
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298if ($tree) {
299 if (defined $root) {
300 if (!top_of_kernel_tree($root)) {
301 die "$P: $root: --root does not point at a valid tree\n";
302 }
303 } else {
304 if (top_of_kernel_tree('.')) {
305 $root = '.';
306 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
307 top_of_kernel_tree($1)) {
308 $root = $1;
309 }
310 }
311
312 if (!defined $root) {
313 print "Must be run from the top-level dir. of a kernel tree\n";
314 exit(2);
315 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700316}
317
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318my $emitted_corrupt = 0;
319
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700320our $Ident = qr{
321 [A-Za-z_][A-Za-z\d_]*
322 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
323 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700324our $Storage = qr{extern|static|asmlinkage};
325our $Sparse = qr{
326 __user|
327 __kernel|
328 __force|
329 __iomem|
330 __must_check|
331 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800332 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700333 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800334 __rcu|
335 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700336 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800337our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
338our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
339our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
340our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
341our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700342
Wolfram Sang52131292010-03-05 13:43:51 -0800343# Notes to $Attribute:
344# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Attribute = qr{
346 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700347 __percpu|
348 __nocast|
349 __safe|
350 __bitwise__|
351 __packed__|
352 __packed2__|
353 __naked|
354 __maybe_unused|
355 __always_unused|
356 __noreturn|
357 __used|
358 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800359 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700360 __noclone|
361 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700362 __read_mostly|
363 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700364 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700365 ____cacheline_aligned|
366 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800367 ____cacheline_internodealigned_in_smp|
368 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700369 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700370our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700371our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
373our $Lval = qr{$Ident(?:$Member)*};
374
Joe Perches95e2c602013-07-03 15:05:20 -0700375our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
376our $Binary = qr{(?i)0b[01]+$Int_type?};
377our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
378our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700379our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800380our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800381our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
382our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
383our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800384our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700385our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800386our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700387our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700388our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389our $Operators = qr{
390 <=|>=|==|!=|
391 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700392 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 }x;
394
Joe Perches91cb5192014-04-03 14:49:32 -0700395our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
396
Joe Perchesab7e23f2015-04-16 12:44:22 -0700397our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700399our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700400our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700402our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700404our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405
Joe Perches15662b32011-10-31 17:13:12 -0700406our $NON_ASCII_UTF8 = qr{
407 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700408 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
409 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
410 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
411 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
412 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
413 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
414}x;
415
Joe Perches15662b32011-10-31 17:13:12 -0700416our $UTF8 = qr{
417 [\x09\x0A\x0D\x20-\x7E] # ASCII
418 | $NON_ASCII_UTF8
419}x;
420
Joe Perchese6176fa2015-06-25 15:02:49 -0700421our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800422our $typeOtherOSTypedefs = qr{(?x:
423 u_(?:char|short|int|long) | # bsd
424 u(?:nchar|short|int|long) # sysv
425)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700426our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700427 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700428 atomic_t
429)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700430our $typeTypedefs = qr{(?x:
431 $typeC99Typedefs\b|
432 $typeOtherOSTypedefs\b|
433 $typeKernelTypedefs\b
434)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700435
Joe Perches6d32f7a2015-11-06 16:31:37 -0800436our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
437
Joe Perches691e6692010-03-05 13:43:51 -0800438our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700439 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700440 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700441 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700442 panic|
Joe Perches06668722013-11-12 15:10:07 -0800443 MODULE_[A-Z_]+|
444 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800445)};
446
Joe Perches20112472011-07-25 17:13:23 -0700447our $signature_tags = qr{(?xi:
448 Signed-off-by:|
449 Acked-by:|
450 Tested-by:|
451 Reviewed-by:|
452 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700453 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700454 To:|
455 Cc:
456)};
457
Joe Perches18130872014-08-06 16:11:22 -0700458our @typeListMisordered = (
459 qr{char\s+(?:un)?signed},
460 qr{int\s+(?:(?:un)?signed\s+)?short\s},
461 qr{int\s+short(?:\s+(?:un)?signed)},
462 qr{short\s+int(?:\s+(?:un)?signed)},
463 qr{(?:un)?signed\s+int\s+short},
464 qr{short\s+(?:un)?signed},
465 qr{long\s+int\s+(?:un)?signed},
466 qr{int\s+long\s+(?:un)?signed},
467 qr{long\s+(?:un)?signed\s+int},
468 qr{int\s+(?:un)?signed\s+long},
469 qr{int\s+(?:un)?signed},
470 qr{int\s+long\s+long\s+(?:un)?signed},
471 qr{long\s+long\s+int\s+(?:un)?signed},
472 qr{long\s+long\s+(?:un)?signed\s+int},
473 qr{long\s+long\s+(?:un)?signed},
474 qr{long\s+(?:un)?signed},
475);
476
Andy Whitcroft8905a672007-11-28 16:21:06 -0800477our @typeList = (
478 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700479 qr{(?:(?:un)?signed\s+)?char},
480 qr{(?:(?:un)?signed\s+)?short\s+int},
481 qr{(?:(?:un)?signed\s+)?short},
482 qr{(?:(?:un)?signed\s+)?int},
483 qr{(?:(?:un)?signed\s+)?long\s+int},
484 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
485 qr{(?:(?:un)?signed\s+)?long\s+long},
486 qr{(?:(?:un)?signed\s+)?long},
487 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488 qr{float},
489 qr{double},
490 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800491 qr{struct\s+$Ident},
492 qr{union\s+$Ident},
493 qr{enum\s+$Ident},
494 qr{${Ident}_t},
495 qr{${Ident}_handler},
496 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700497 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498);
Joe Perches938224b2016-01-20 14:59:15 -0800499
500our $C90_int_types = qr{(?x:
501 long\s+long\s+int\s+(?:un)?signed|
502 long\s+long\s+(?:un)?signed\s+int|
503 long\s+long\s+(?:un)?signed|
504 (?:(?:un)?signed\s+)?long\s+long\s+int|
505 (?:(?:un)?signed\s+)?long\s+long|
506 int\s+long\s+long\s+(?:un)?signed|
507 int\s+(?:(?:un)?signed\s+)?long\s+long|
508
509 long\s+int\s+(?:un)?signed|
510 long\s+(?:un)?signed\s+int|
511 long\s+(?:un)?signed|
512 (?:(?:un)?signed\s+)?long\s+int|
513 (?:(?:un)?signed\s+)?long|
514 int\s+long\s+(?:un)?signed|
515 int\s+(?:(?:un)?signed\s+)?long|
516
517 int\s+(?:un)?signed|
518 (?:(?:un)?signed\s+)?int
519)};
520
Alex Dowad485ff232015-06-25 15:02:52 -0700521our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700522our @typeListWithAttr = (
523 @typeList,
524 qr{struct\s+$InitAttribute\s+$Ident},
525 qr{union\s+$InitAttribute\s+$Ident},
526);
527
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700528our @modifierList = (
529 qr{fastcall},
530);
Alex Dowad485ff232015-06-25 15:02:52 -0700531our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532
Joe Perches24358802014-04-03 14:49:13 -0700533our @mode_permission_funcs = (
534 ["module_param", 3],
535 ["module_param_(?:array|named|string)", 4],
536 ["module_param_array_named", 5],
537 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
538 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700539 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
540 ["IIO_DEV_ATTR_[A-Z_]+", 1],
541 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
542 ["SENSOR_TEMPLATE(?:_2|)", 3],
543 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700544);
545
Joe Perches515a2352014-04-03 14:49:24 -0700546#Create a search pattern for all these functions to speed up a loop below
547our $mode_perms_search = "";
548foreach my $entry (@mode_permission_funcs) {
549 $mode_perms_search .= '|' if ($mode_perms_search ne "");
550 $mode_perms_search .= $entry->[0];
551}
552
Joe Perchesb392c642015-04-16 12:44:16 -0700553our $mode_perms_world_writable = qr{
554 S_IWUGO |
555 S_IWOTH |
556 S_IRWXUGO |
557 S_IALLUGO |
558 0[0-7][0-7][2367]
559}x;
560
Joe Perchesf90774e2016-10-11 13:51:47 -0700561our %mode_permission_string_types = (
562 "S_IRWXU" => 0700,
563 "S_IRUSR" => 0400,
564 "S_IWUSR" => 0200,
565 "S_IXUSR" => 0100,
566 "S_IRWXG" => 0070,
567 "S_IRGRP" => 0040,
568 "S_IWGRP" => 0020,
569 "S_IXGRP" => 0010,
570 "S_IRWXO" => 0007,
571 "S_IROTH" => 0004,
572 "S_IWOTH" => 0002,
573 "S_IXOTH" => 0001,
574 "S_IRWXUGO" => 0777,
575 "S_IRUGO" => 0444,
576 "S_IWUGO" => 0222,
577 "S_IXUGO" => 0111,
578);
579
580#Create a search pattern for all these strings to speed up a loop below
581our $mode_perms_string_search = "";
582foreach my $entry (keys %mode_permission_string_types) {
583 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
584 $mode_perms_string_search .= $entry;
585}
586
Wolfram Sang7840a942010-08-09 17:20:57 -0700587our $allowed_asm_includes = qr{(?x:
588 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700589 memory|
590 time|
591 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700592)};
593# memory.h: ARM has a custom one
594
Kees Cook66b47b42014-10-13 15:51:57 -0700595# Load common spelling mistakes and build regular expression list.
596my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700597my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700598
Joe Perches36061e32014-12-10 15:51:43 -0800599if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800600 while (<$spelling>) {
601 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700602
Joe Perches36061e32014-12-10 15:51:43 -0800603 $line =~ s/\s*\n?$//g;
604 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700605
Joe Perches36061e32014-12-10 15:51:43 -0800606 next if ($line =~ m/^\s*#/);
607 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700608
Joe Perches36061e32014-12-10 15:51:43 -0800609 my ($suspect, $fix) = split(/\|\|/, $line);
610
Joe Perches36061e32014-12-10 15:51:43 -0800611 $spelling_fix{$suspect} = $fix;
612 }
613 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800614} else {
615 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700616}
Kees Cook66b47b42014-10-13 15:51:57 -0700617
Joe Perchesebfd7d62015-04-16 12:44:14 -0700618if ($codespell) {
619 if (open(my $spelling, '<', $codespellfile)) {
620 while (<$spelling>) {
621 my $line = $_;
622
623 $line =~ s/\s*\n?$//g;
624 $line =~ s/^\s*//g;
625
626 next if ($line =~ m/^\s*#/);
627 next if ($line =~ m/^\s*$/);
628 next if ($line =~ m/, disabled/i);
629
630 $line =~ s/,.*$//;
631
632 my ($suspect, $fix) = split(/->/, $line);
633
634 $spelling_fix{$suspect} = $fix;
635 }
636 close($spelling);
637 } else {
638 warn "No codespell typos will be found - file '$codespellfile': $!\n";
639 }
640}
641
642$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
643
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700644my $const_structs = "";
645if (open(my $conststructs, '<', $conststructsfile)) {
646 while (<$conststructs>) {
647 my $line = $_;
648
649 $line =~ s/\s*\n?$//g;
650 $line =~ s/^\s*//g;
651
652 next if ($line =~ m/^\s*#/);
653 next if ($line =~ m/^\s*$/);
654 if ($line =~ /\s/) {
655 print("$conststructsfile: '$line' invalid - ignored\n");
656 next;
657 }
658
659 $const_structs .= '|' if ($const_structs ne "");
660 $const_structs .= $line;
661 }
662 close($conststructsfile);
663} else {
664 warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
665}
666
Andy Whitcroft8905a672007-11-28 16:21:06 -0800667sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700668 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
669 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700670 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700671 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700672 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700673 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700674 (?:$typeTypedefs\b)|
675 (?:${all}\b)
676 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800677 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700678 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800679 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800680 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700681 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700682 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800683 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700684 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800685 }x;
Joe Perches18130872014-08-06 16:11:22 -0700686 $NonptrTypeMisordered = qr{
687 (?:$Modifier\s+|const\s+)*
688 (?:
689 (?:${Misordered}\b)
690 )
691 (?:\s+$Modifier|\s+const)*
692 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700693 $NonptrTypeWithAttr = qr{
694 (?:$Modifier\s+|const\s+)*
695 (?:
696 (?:typeof|__typeof__)\s*\([^\)]*\)|
697 (?:$typeTypedefs\b)|
698 (?:${allWithAttr}\b)
699 )
700 (?:\s+$Modifier|\s+const)*
701 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800702 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700703 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700704 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700705 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800706 }x;
Joe Perches18130872014-08-06 16:11:22 -0700707 $TypeMisordered = qr{
708 $NonptrTypeMisordered
709 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
710 (?:\s+$Inline|\s+$Modifier)*
711 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700712 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700713 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800714}
715build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700716
Joe Perches7d2367a2011-07-25 17:13:22 -0700717our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700718
719# Using $balanced_parens, $LvalOrFunc, or $FuncArg
720# requires at least perl version v5.10.0
721# Any use must be runtime checked with $^V
722
723our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700724our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800725our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700726
Joe Perchesf8422302014-08-06 16:11:31 -0700727our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700728 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700729 (?:$Storage\s+)?LIST_HEAD\s*\(|
730 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
731)};
732
Joe Perches7d2367a2011-07-25 17:13:22 -0700733sub deparenthesize {
734 my ($string) = @_;
735 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700736
737 while ($string =~ /^\s*\(.*\)\s*$/) {
738 $string =~ s@^\s*\(\s*@@;
739 $string =~ s@\s*\)\s*$@@;
740 }
741
Joe Perches7d2367a2011-07-25 17:13:22 -0700742 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700743
Joe Perches7d2367a2011-07-25 17:13:22 -0700744 return $string;
745}
746
Joe Perches34456862013-07-03 15:05:34 -0700747sub seed_camelcase_file {
748 my ($file) = @_;
749
750 return if (!(-f $file));
751
752 local $/;
753
754 open(my $include_file, '<', "$file")
755 or warn "$P: Can't read '$file' $!\n";
756 my $text = <$include_file>;
757 close($include_file);
758
759 my @lines = split('\n', $text);
760
761 foreach my $line (@lines) {
762 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
763 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
764 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800765 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
766 $camelcase{$1} = 1;
767 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700768 $camelcase{$1} = 1;
769 }
770 }
771}
772
Joe Perches85b0ee12016-10-11 13:51:44 -0700773sub is_maintained_obsolete {
774 my ($filename) = @_;
775
776 return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
777
Joe Perches0616efa2016-10-11 13:52:02 -0700778 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700779
780 return $status =~ /obsolete/i;
781}
782
Joe Perches34456862013-07-03 15:05:34 -0700783my $camelcase_seeded = 0;
784sub seed_camelcase_includes {
785 return if ($camelcase_seeded);
786
787 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700788 my $camelcase_cache = "";
789 my @include_files = ();
790
791 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700792
Richard Genoud3645e322014-02-10 14:25:32 -0800793 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700794 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
795 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700796 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700797 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700798 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700799 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700800 @include_files = split('\n', $files);
801 foreach my $file (@include_files) {
802 my $date = POSIX::strftime("%Y%m%d%H%M",
803 localtime((stat $file)[9]));
804 $last_mod_date = $date if ($last_mod_date < $date);
805 }
806 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700807 }
Joe Perchesc707a812013-07-08 16:00:43 -0700808
809 if ($camelcase_cache ne "" && -f $camelcase_cache) {
810 open(my $camelcase_file, '<', "$camelcase_cache")
811 or warn "$P: Can't read '$camelcase_cache' $!\n";
812 while (<$camelcase_file>) {
813 chomp;
814 $camelcase{$_} = 1;
815 }
816 close($camelcase_file);
817
818 return;
819 }
820
Richard Genoud3645e322014-02-10 14:25:32 -0800821 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700822 $files = `git ls-files "include/*.h"`;
823 @include_files = split('\n', $files);
824 }
825
Joe Perches34456862013-07-03 15:05:34 -0700826 foreach my $file (@include_files) {
827 seed_camelcase_file($file);
828 }
Joe Perches351b2a12013-07-03 15:05:36 -0700829
Joe Perchesc707a812013-07-08 16:00:43 -0700830 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700831 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700832 open(my $camelcase_file, '>', "$camelcase_cache")
833 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700834 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
835 print $camelcase_file ("$_\n");
836 }
837 close($camelcase_file);
838 }
Joe Perches34456862013-07-03 15:05:34 -0700839}
840
Joe Perchesd311cd42014-08-06 16:10:57 -0700841sub git_commit_info {
842 my ($commit, $id, $desc) = @_;
843
844 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
845
846 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
847 $output =~ s/^\s*//gm;
848 my @lines = split("\n", $output);
849
Joe Perches0d7835f2015-02-13 14:38:35 -0800850 return ($id, $desc) if ($#lines < 0);
851
Joe Perchesd311cd42014-08-06 16:10:57 -0700852 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
853# Maybe one day convert this block of bash into something that returns
854# all matching commit ids, but it's very slow...
855#
856# echo "checking commits $1..."
857# git rev-list --remotes | grep -i "^$1" |
858# while read line ; do
859# git log --format='%H %s' -1 $line |
860# echo "commit $(cut -c 1-12,41-)"
861# done
862 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
863 } else {
864 $id = substr($lines[0], 0, 12);
865 $desc = substr($lines[0], 41);
866 }
867
868 return ($id, $desc);
869}
870
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700871$chk_signoff = 0 if ($file);
872
Andy Whitcroft00df3442007-06-08 13:47:06 -0700873my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700875my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700876my @fixed_inserted = ();
877my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700878my $fixlinenr = -1;
879
Du, Changbin4a593c32016-05-20 17:04:16 -0700880# If input is git commits, extract all commits from the commit expressions.
881# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
882die "$P: No git repository found\n" if ($git && !-e ".git");
883
884if ($git) {
885 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700886 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700887 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700888 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
889 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700890 } elsif ($commit_expr =~ m/\.\./) {
891 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700892 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700893 $git_range = "-1 $commit_expr";
894 }
895 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
896 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700897 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
898 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700899 my $sha1 = $1;
900 my $subject = $2;
901 unshift(@commits, $sha1);
902 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700903 }
904 }
905 die "$P: no git commits after extraction!\n" if (@commits == 0);
906 @ARGV = @commits;
907}
908
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800909my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700910for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800911 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700912 if ($git) {
913 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
914 die "$P: $filename: git format-patch failed - $!\n";
915 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800916 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700917 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800918 } elsif ($filename eq '-') {
919 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700920 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800921 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700923 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800924 if ($filename eq '-') {
925 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700926 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700927 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 } else {
929 $vname = $filename;
930 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800931 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932 chomp;
933 push(@rawlines, $_);
934 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800935 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700936
937 if ($#ARGV > 0 && $quiet == 0) {
938 print '-' x length($vname) . "\n";
939 print "$vname\n";
940 print '-' x length($vname) . "\n";
941 }
942
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700944 $exit = 1;
945 }
946 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800947 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700948 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700949 @fixed_inserted = ();
950 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700951 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700952 @modifierListFile = ();
953 @typeListFile = ();
954 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700955}
956
Joe Perchesd8469f12015-06-25 15:03:00 -0700957if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700958 hash_show_words(\%use_type, "Used");
959 hash_show_words(\%ignore_type, "Ignored");
960
Joe Perchesd8469f12015-06-25 15:03:00 -0700961 if ($^V lt 5.10.0) {
962 print << "EOM"
963
964NOTE: perl $^V is not modern enough to detect all possible issues.
965 An upgrade to at least perl v5.10.0 is suggested.
966EOM
967 }
968 if ($exit) {
969 print << "EOM"
970
971NOTE: If any of the errors are false positives, please report
972 them to the maintainer, see CHECKPATCH in MAINTAINERS.
973EOM
974 }
975}
976
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700977exit($exit);
978
979sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700980 my ($root) = @_;
981
982 my @tree_check = (
983 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
984 "README", "Documentation", "arch", "include", "drivers",
985 "fs", "init", "ipc", "kernel", "lib", "scripts",
986 );
987
988 foreach my $check (@tree_check) {
989 if (! -e $root . '/' . $check) {
990 return 0;
991 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700992 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700993 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700994}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700995
Joe Perches20112472011-07-25 17:13:23 -0700996sub parse_email {
997 my ($formatted_email) = @_;
998
999 my $name = "";
1000 my $address = "";
1001 my $comment = "";
1002
1003 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1004 $name = $1;
1005 $address = $2;
1006 $comment = $3 if defined $3;
1007 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1008 $address = $1;
1009 $comment = $2 if defined $2;
1010 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1011 $address = $1;
1012 $comment = $2 if defined $2;
1013 $formatted_email =~ s/$address.*$//;
1014 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001015 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001016 $name =~ s/^\"|\"$//g;
1017 # If there's a name left after stripping spaces and
1018 # leading quotes, and the address doesn't have both
1019 # leading and trailing angle brackets, the address
1020 # is invalid. ie:
1021 # "joe smith joe@smith.com" bad
1022 # "joe smith <joe@smith.com" bad
1023 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1024 $name = "";
1025 $address = "";
1026 $comment = "";
1027 }
1028 }
1029
Joe Perches3705ce52013-07-03 15:05:31 -07001030 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001031 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001032 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001033 $address =~ s/^\<|\>$//g;
1034
1035 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1036 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1037 $name = "\"$name\"";
1038 }
1039
1040 return ($name, $address, $comment);
1041}
1042
1043sub format_email {
1044 my ($name, $address) = @_;
1045
1046 my $formatted_email;
1047
Joe Perches3705ce52013-07-03 15:05:31 -07001048 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001049 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001050 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001051
1052 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1053 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1054 $name = "\"$name\"";
1055 }
1056
1057 if ("$name" eq "") {
1058 $formatted_email = "$address";
1059 } else {
1060 $formatted_email = "$name <$address>";
1061 }
1062
1063 return $formatted_email;
1064}
1065
Joe Perchesd311cd42014-08-06 16:10:57 -07001066sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001067 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001068
Joe Perchesbd474ca2014-08-06 16:11:10 -07001069 foreach my $path (split(/:/, $ENV{PATH})) {
1070 if (-e "$path/$bin") {
1071 return "$path/$bin";
1072 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001073 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001074
Joe Perchesbd474ca2014-08-06 16:11:10 -07001075 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001076}
1077
Joe Perches000d1cc12011-07-25 17:13:25 -07001078sub which_conf {
1079 my ($conf) = @_;
1080
1081 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1082 if (-e "$path/$conf") {
1083 return "$path/$conf";
1084 }
1085 }
1086
1087 return "";
1088}
1089
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001090sub expand_tabs {
1091 my ($str) = @_;
1092
1093 my $res = '';
1094 my $n = 0;
1095 for my $c (split(//, $str)) {
1096 if ($c eq "\t") {
1097 $res .= ' ';
1098 $n++;
1099 for (; ($n % 8) != 0; $n++) {
1100 $res .= ' ';
1101 }
1102 next;
1103 }
1104 $res .= $c;
1105 $n++;
1106 }
1107
1108 return $res;
1109}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001112 return $res;
1113}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001114
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001115sub line_stats {
1116 my ($line) = @_;
1117
1118 # Drop the diff line leader and expand tabs
1119 $line =~ s/^.//;
1120 $line = expand_tabs($line);
1121
1122 # Pick the indent from the front of the line.
1123 my ($white) = ($line =~ /^(\s*)/);
1124
1125 return (length($line), length($white));
1126}
1127
Andy Whitcroft773647a2008-03-28 14:15:58 -07001128my $sanitise_quote = '';
1129
1130sub sanitise_line_reset {
1131 my ($in_comment) = @_;
1132
1133 if ($in_comment) {
1134 $sanitise_quote = '*/';
1135 } else {
1136 $sanitise_quote = '';
1137 }
1138}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001139sub sanitise_line {
1140 my ($line) = @_;
1141
1142 my $res = '';
1143 my $l = '';
1144
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001145 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001146 my $off = 0;
1147 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001148
Andy Whitcroft773647a2008-03-28 14:15:58 -07001149 # Always copy over the diff marker.
1150 $res = substr($line, 0, 1);
1151
1152 for ($off = 1; $off < length($line); $off++) {
1153 $c = substr($line, $off, 1);
1154
1155 # Comments we are wacking completly including the begin
1156 # and end, all to $;.
1157 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1158 $sanitise_quote = '*/';
1159
1160 substr($res, $off, 2, "$;$;");
1161 $off++;
1162 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001163 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001164 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001165 $sanitise_quote = '';
1166 substr($res, $off, 2, "$;$;");
1167 $off++;
1168 next;
1169 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001170 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1171 $sanitise_quote = '//';
1172
1173 substr($res, $off, 2, $sanitise_quote);
1174 $off++;
1175 next;
1176 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177
1178 # A \ in a string means ignore the next character.
1179 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1180 $c eq "\\") {
1181 substr($res, $off, 2, 'XX');
1182 $off++;
1183 next;
1184 }
1185 # Regular quotes.
1186 if ($c eq "'" || $c eq '"') {
1187 if ($sanitise_quote eq '') {
1188 $sanitise_quote = $c;
1189
1190 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001191 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 } elsif ($sanitise_quote eq $c) {
1193 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001194 }
1195 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001197 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001198 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1199 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001200 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1201 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001202 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1203 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001204 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001205 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001206 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 }
1208
Daniel Walker113f04a2009-09-21 17:04:35 -07001209 if ($sanitise_quote eq '//') {
1210 $sanitise_quote = '';
1211 }
1212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001214 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 my $clean = 'X' x length($1);
1216 $res =~ s@\<.*\>@<$clean>@;
1217
1218 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001219 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001220 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001221 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001222 }
1223
Joe Perchesdadf6802016-08-02 14:04:33 -07001224 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1225 my $match = $1;
1226 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1227 }
1228
Andy Whitcroft00df3442007-06-08 13:47:06 -07001229 return $res;
1230}
1231
Joe Perchesa6962d72013-04-29 16:18:13 -07001232sub get_quoted_string {
1233 my ($line, $rawline) = @_;
1234
Joe Perches33acb542015-06-25 15:02:54 -07001235 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001236 return substr($rawline, $-[0], $+[0] - $-[0]);
1237}
1238
Andy Whitcroft8905a672007-11-28 16:21:06 -08001239sub ctx_statement_block {
1240 my ($linenr, $remain, $off) = @_;
1241 my $line = $linenr - 1;
1242 my $blk = '';
1243 my $soff = $off;
1244 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001246
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001247 my $loff = 0;
1248
Andy Whitcroft8905a672007-11-28 16:21:06 -08001249 my $type = '';
1250 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001251 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001252 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001253 my $c;
1254 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001255
1256 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001257 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001258 @stack = (['', 0]) if ($#stack == -1);
1259
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 # If we are about to drop off the end, pull in more
1262 # context.
1263 if ($off >= $len) {
1264 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001265 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001267 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001268 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001269 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001270 $len = length($blk);
1271 $line++;
1272 last;
1273 }
1274 # Bail if there is no further context.
1275 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001276 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001277 last;
1278 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001279 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1280 $level++;
1281 $type = '#';
1282 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001283 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001287
Andy Whitcroft773647a2008-03-28 14:15:58 -07001288 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001289
1290 # Handle nested #if/#else.
1291 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1292 push(@stack, [ $type, $level ]);
1293 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1294 ($type, $level) = @{$stack[$#stack - 1]};
1295 } elsif ($remainder =~ /^#\s*endif\b/) {
1296 ($type, $level) = @{pop(@stack)};
1297 }
1298
Andy Whitcroft8905a672007-11-28 16:21:06 -08001299 # Statement ends at the ';' or a close '}' at the
1300 # outermost level.
1301 if ($level == 0 && $c eq ';') {
1302 last;
1303 }
1304
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001305 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 if ($level == 0 && $coff_set == 0 &&
1307 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1308 $remainder =~ /^(else)(?:\s|{)/ &&
1309 $remainder !~ /^else\s+if\b/) {
1310 $coff = $off + length($1) - 1;
1311 $coff_set = 1;
1312 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1313 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001314 }
1315
Andy Whitcroft8905a672007-11-28 16:21:06 -08001316 if (($type eq '' || $type eq '(') && $c eq '(') {
1317 $level++;
1318 $type = '(';
1319 }
1320 if ($type eq '(' && $c eq ')') {
1321 $level--;
1322 $type = ($level != 0)? '(' : '';
1323
1324 if ($level == 0 && $coff < $soff) {
1325 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326 $coff_set = 1;
1327 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001328 }
1329 }
1330 if (($type eq '' || $type eq '{') && $c eq '{') {
1331 $level++;
1332 $type = '{';
1333 }
1334 if ($type eq '{' && $c eq '}') {
1335 $level--;
1336 $type = ($level != 0)? '{' : '';
1337
1338 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001339 if (substr($blk, $off + 1, 1) eq ';') {
1340 $off++;
1341 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001342 last;
1343 }
1344 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001345 # Preprocessor commands end at the newline unless escaped.
1346 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1347 $level--;
1348 $type = '';
1349 $off++;
1350 last;
1351 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 $off++;
1353 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001354 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001356 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001357 $line++;
1358 $remain--;
1359 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001360
1361 my $statement = substr($blk, $soff, $off - $soff + 1);
1362 my $condition = substr($blk, $soff, $coff - $soff + 1);
1363
1364 #warn "STATEMENT<$statement>\n";
1365 #warn "CONDITION<$condition>\n";
1366
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001368
1369 return ($statement, $condition,
1370 $line, $remain + 1, $off - $loff + 1, $level);
1371}
1372
Andy Whitcroftcf655042008-03-04 14:28:20 -08001373sub statement_lines {
1374 my ($stmt) = @_;
1375
1376 # Strip the diff line prefixes and rip blank lines at start and end.
1377 $stmt =~ s/(^|\n)./$1/g;
1378 $stmt =~ s/^\s*//;
1379 $stmt =~ s/\s*$//;
1380
1381 my @stmt_lines = ($stmt =~ /\n/g);
1382
1383 return $#stmt_lines + 2;
1384}
1385
1386sub statement_rawlines {
1387 my ($stmt) = @_;
1388
1389 my @stmt_lines = ($stmt =~ /\n/g);
1390
1391 return $#stmt_lines + 2;
1392}
1393
1394sub statement_block_size {
1395 my ($stmt) = @_;
1396
1397 $stmt =~ s/(^|\n)./$1/g;
1398 $stmt =~ s/^\s*{//;
1399 $stmt =~ s/}\s*$//;
1400 $stmt =~ s/^\s*//;
1401 $stmt =~ s/\s*$//;
1402
1403 my @stmt_lines = ($stmt =~ /\n/g);
1404 my @stmt_statements = ($stmt =~ /;/g);
1405
1406 my $stmt_lines = $#stmt_lines + 2;
1407 my $stmt_statements = $#stmt_statements + 1;
1408
1409 if ($stmt_lines > $stmt_statements) {
1410 return $stmt_lines;
1411 } else {
1412 return $stmt_statements;
1413 }
1414}
1415
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001416sub ctx_statement_full {
1417 my ($linenr, $remain, $off) = @_;
1418 my ($statement, $condition, $level);
1419
1420 my (@chunks);
1421
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001423 ($statement, $condition, $linenr, $remain, $off, $level) =
1424 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001426 push(@chunks, [ $condition, $statement ]);
1427 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1428 return ($level, $linenr, @chunks);
1429 }
1430
1431 # Pull in the following conditional/block pairs and see if they
1432 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001433 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001434 ($statement, $condition, $linenr, $remain, $off, $level) =
1435 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 #print "C: push\n";
1439 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 }
1441
1442 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443}
1444
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001445sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001446 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001447 my $line;
1448 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001449 my $blk = '';
1450 my @o;
1451 my @c;
1452 my @res = ();
1453
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001454 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001455 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001456 for ($line = $start; $remain > 0; $line++) {
1457 next if ($rawlines[$line] =~ /^-/);
1458 $remain--;
1459
1460 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001461
1462 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001463 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001464 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001465 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001466 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001467 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001468 $level = pop(@stack);
1469 }
1470
Andy Whitcroft01464f32010-10-26 14:23:19 -07001471 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001472 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1473 if ($off > 0) {
1474 $off--;
1475 next;
1476 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001478 if ($c eq $close && $level > 0) {
1479 $level--;
1480 last if ($level == 0);
1481 } elsif ($c eq $open) {
1482 $level++;
1483 }
1484 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001485
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001486 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001488 }
1489
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001490 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001491 }
1492
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001493 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494}
1495sub ctx_block_outer {
1496 my ($linenr, $remain) = @_;
1497
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001498 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1499 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001500}
1501sub ctx_block {
1502 my ($linenr, $remain) = @_;
1503
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001504 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1505 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001506}
1507sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001508 my ($linenr, $remain, $off) = @_;
1509
1510 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1511 return @r;
1512}
1513sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001514 my ($linenr, $remain) = @_;
1515
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001516 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001517}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001518sub ctx_statement_level {
1519 my ($linenr, $remain, $off) = @_;
1520
1521 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1522}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001523
1524sub ctx_locate_comment {
1525 my ($first_line, $end_line) = @_;
1526
1527 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001528 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001529 return $current_comment if (defined $current_comment);
1530
1531 # Look through the context and try and figure out if there is a
1532 # comment.
1533 my $in_comment = 0;
1534 $current_comment = '';
1535 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001536 my $line = $rawlines[$linenr - 1];
1537 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001538 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1539 $in_comment = 1;
1540 }
1541 if ($line =~ m@/\*@) {
1542 $in_comment = 1;
1543 }
1544 if (!$in_comment && $current_comment ne '') {
1545 $current_comment = '';
1546 }
1547 $current_comment .= $line . "\n" if ($in_comment);
1548 if ($line =~ m@\*/@) {
1549 $in_comment = 0;
1550 }
1551 }
1552
1553 chomp($current_comment);
1554 return($current_comment);
1555}
1556sub ctx_has_comment {
1557 my ($first_line, $end_line) = @_;
1558 my $cmt = ctx_locate_comment($first_line, $end_line);
1559
Andy Whitcroft00df3442007-06-08 13:47:06 -07001560 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001561 ##print "CMMT: $cmt\n";
1562
1563 return ($cmt ne '');
1564}
1565
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001566sub raw_line {
1567 my ($linenr, $cnt) = @_;
1568
1569 my $offset = $linenr - 1;
1570 $cnt++;
1571
1572 my $line;
1573 while ($cnt) {
1574 $line = $rawlines[$offset++];
1575 next if (defined($line) && $line =~ /^-/);
1576 $cnt--;
1577 }
1578
1579 return $line;
1580}
1581
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001582sub cat_vet {
1583 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001584 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001585
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001586 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001587 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1588 $res .= $1;
1589 if ($2 ne '') {
1590 $coded = sprintf("^%c", unpack('C', $2) + 64);
1591 $res .= $coded;
1592 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001593 }
1594 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001595
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001596 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001597}
1598
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001599my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001601my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001602my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001603
1604sub annotate_reset {
1605 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606 $av_pending = '_';
1607 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001608 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609}
1610
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001611sub annotate_values {
1612 my ($stream, $type) = @_;
1613
1614 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001615 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001616 my $cur = $stream;
1617
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001618 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001622 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001623 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001624 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001625 print "WS($1)\n" if ($dbg_values > 1);
1626 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001627 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629 }
1630
Florian Micklerc023e4732011-01-12 16:59:58 -08001631 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001632 print "CAST($1)\n" if ($dbg_values > 1);
1633 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001634 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001635
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001636 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001637 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001638 $type = 'T';
1639
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001640 } elsif ($cur =~ /^($Modifier)\s*/) {
1641 print "MODIFIER($1)\n" if ($dbg_values > 1);
1642 $type = 'T';
1643
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001644 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001645 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001646 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001647 push(@av_paren_type, $type);
1648 if ($2 ne '') {
1649 $av_pending = 'N';
1650 }
1651 $type = 'E';
1652
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001653 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 print "UNDEF($1)\n" if ($dbg_values > 1);
1655 $av_preprocessor = 1;
1656 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001657
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001658 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001659 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001661
1662 push(@av_paren_type, $type);
1663 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001664 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001666 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001667 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1668 $av_preprocessor = 1;
1669
1670 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1671
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001672 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001674 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001675 print "PRE_END($1)\n" if ($dbg_values > 1);
1676
1677 $av_preprocessor = 1;
1678
1679 # Assume all arms of the conditional end as this
1680 # one does, and continue as if the #endif was not here.
1681 pop(@av_paren_type);
1682 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001683 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001684
1685 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001686 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001687
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001688 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1689 print "ATTR($1)\n" if ($dbg_values > 1);
1690 $av_pending = $type;
1691 $type = 'N';
1692
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001693 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001695 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001696 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001697 }
1698 $type = 'N';
1699
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001700 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001701 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001702 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703 $type = 'N';
1704
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001705 } elsif ($cur =~/^(case)/o) {
1706 print "CASE($1)\n" if ($dbg_values > 1);
1707 $av_pend_colon = 'C';
1708 $type = 'N';
1709
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001710 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001711 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712 $type = 'N';
1713
1714 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001715 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001716 push(@av_paren_type, $av_pending);
1717 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 $type = 'N';
1719
1720 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001721 my $new_type = pop(@av_paren_type);
1722 if ($new_type ne '_') {
1723 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001724 print "PAREN('$1') -> $type\n"
1725 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
1729
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001730 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001731 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001732 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001733 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001735 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1736 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001737 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001738 } elsif ($type eq 'E') {
1739 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001740 }
1741 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1742 $type = 'V';
1743
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001744 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001746 $type = 'V';
1747
1748 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 $type = 'N';
1751
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001754 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001755 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001756
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001757 } elsif ($cur =~/^(,)/) {
1758 print "COMMA($1)\n" if ($dbg_values > 1);
1759 $type = 'C';
1760
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001761 } elsif ($cur =~ /^(\?)/o) {
1762 print "QUESTION($1)\n" if ($dbg_values > 1);
1763 $type = 'N';
1764
1765 } elsif ($cur =~ /^(:)/o) {
1766 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1767
1768 substr($var, length($res), 1, $av_pend_colon);
1769 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1770 $type = 'E';
1771 } else {
1772 $type = 'N';
1773 }
1774 $av_pend_colon = 'O';
1775
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001776 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001777 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001778 $type = 'N';
1779
Andy Whitcroft0d413862008-10-15 22:02:16 -07001780 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001781 my $variant;
1782
1783 print "OPV($1)\n" if ($dbg_values > 1);
1784 if ($type eq 'V') {
1785 $variant = 'B';
1786 } else {
1787 $variant = 'U';
1788 }
1789
1790 substr($var, length($res), 1, $variant);
1791 $type = 'N';
1792
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 if ($1 ne '++' && $1 ne '--') {
1796 $type = 'N';
1797 }
1798
1799 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 }
1802 if (defined $1) {
1803 $cur = substr($cur, length($1));
1804 $res .= $type x length($1);
1805 }
1806 }
1807
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001808 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809}
1810
Andy Whitcroft8905a672007-11-28 16:21:06 -08001811sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001812 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001813 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001814 ^(?:
1815 $Modifier|
1816 $Storage|
1817 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001818 DEFINE_\S+
1819 )$|
1820 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001821 goto|
1822 return|
1823 case|
1824 else|
1825 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001826 do|
1827 \#|
1828 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001829 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001830 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001831 )}x;
1832 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1833 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001834 # Check for modifiers.
1835 $possible =~ s/\s*$Storage\s*//g;
1836 $possible =~ s/\s*$Sparse\s*//g;
1837 if ($possible =~ /^\s*$/) {
1838
1839 } elsif ($possible =~ /\s/) {
1840 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001841 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001842 if ($modifier !~ $notPermitted) {
1843 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001844 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001845 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001846 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001847
1848 } else {
1849 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001850 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001851 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001852 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001853 } else {
1854 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001855 }
1856}
1857
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001858my $prefix = '';
1859
Joe Perches000d1cc12011-07-25 17:13:25 -07001860sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001861 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001862
Joe Perchescbec18a2014-04-03 14:49:19 -07001863 return defined $use_type{$type} if (scalar keys %use_type > 0);
1864
1865 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001866}
1867
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001868sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001869 my ($level, $type, $msg) = @_;
1870
1871 if (!show_type($type) ||
1872 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 return 0;
1874 }
Joe Perches57230292015-06-25 15:03:03 -07001875 my $output = '';
1876 if (-t STDOUT && $color) {
1877 if ($level eq 'ERROR') {
1878 $output .= RED;
1879 } elsif ($level eq 'WARNING') {
1880 $output .= YELLOW;
1881 } else {
1882 $output .= GREEN;
1883 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001884 }
Joe Perches57230292015-06-25 15:03:03 -07001885 $output .= $prefix . $level . ':';
1886 if ($show_types) {
1887 $output .= BLUE if (-t STDOUT && $color);
1888 $output .= "$type:";
1889 }
1890 $output .= RESET if (-t STDOUT && $color);
1891 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001892
1893 if ($showfile) {
1894 my @lines = split("\n", $output, -1);
1895 splice(@lines, 1, 1);
1896 $output = join("\n", @lines);
1897 }
Joe Perches57230292015-06-25 15:03:03 -07001898 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001899
Joe Perches57230292015-06-25 15:03:03 -07001900 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001901
1902 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001903}
Joe Perchescbec18a2014-04-03 14:49:19 -07001904
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001905sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001906 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001907}
Joe Perches000d1cc12011-07-25 17:13:25 -07001908
Joe Perchesd752fcc2014-08-06 16:11:05 -07001909sub fixup_current_range {
1910 my ($lineRef, $offset, $length) = @_;
1911
1912 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1913 my $o = $1;
1914 my $l = $2;
1915 my $no = $o + $offset;
1916 my $nl = $l + $length;
1917 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1918 }
1919}
1920
1921sub fix_inserted_deleted_lines {
1922 my ($linesRef, $insertedRef, $deletedRef) = @_;
1923
1924 my $range_last_linenr = 0;
1925 my $delta_offset = 0;
1926
1927 my $old_linenr = 0;
1928 my $new_linenr = 0;
1929
1930 my $next_insert = 0;
1931 my $next_delete = 0;
1932
1933 my @lines = ();
1934
1935 my $inserted = @{$insertedRef}[$next_insert++];
1936 my $deleted = @{$deletedRef}[$next_delete++];
1937
1938 foreach my $old_line (@{$linesRef}) {
1939 my $save_line = 1;
1940 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001941 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001942 $delta_offset = 0;
1943 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1944 $range_last_linenr = $new_linenr;
1945 fixup_current_range(\$line, $delta_offset, 0);
1946 }
1947
1948 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1949 $deleted = @{$deletedRef}[$next_delete++];
1950 $save_line = 0;
1951 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1952 }
1953
1954 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1955 push(@lines, ${$inserted}{'LINE'});
1956 $inserted = @{$insertedRef}[$next_insert++];
1957 $new_linenr++;
1958 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1959 }
1960
1961 if ($save_line) {
1962 push(@lines, $line);
1963 $new_linenr++;
1964 }
1965
1966 $old_linenr++;
1967 }
1968
1969 return @lines;
1970}
1971
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001972sub fix_insert_line {
1973 my ($linenr, $line) = @_;
1974
1975 my $inserted = {
1976 LINENR => $linenr,
1977 LINE => $line,
1978 };
1979 push(@fixed_inserted, $inserted);
1980}
1981
1982sub fix_delete_line {
1983 my ($linenr, $line) = @_;
1984
1985 my $deleted = {
1986 LINENR => $linenr,
1987 LINE => $line,
1988 };
1989
1990 push(@fixed_deleted, $deleted);
1991}
1992
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001993sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001994 my ($type, $msg) = @_;
1995
1996 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001997 our $clean = 0;
1998 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001999 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 }
Joe Perches3705ce52013-07-03 15:05:31 -07002001 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002002}
2003sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002004 my ($type, $msg) = @_;
2005
2006 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007 our $clean = 0;
2008 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002009 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002010 }
Joe Perches3705ce52013-07-03 15:05:31 -07002011 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002012}
2013sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002014 my ($type, $msg) = @_;
2015
2016 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017 our $clean = 0;
2018 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 }
Joe Perches3705ce52013-07-03 15:05:31 -07002021 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002022}
2023
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002024sub check_absolute_file {
2025 my ($absolute, $herecurr) = @_;
2026 my $file = $absolute;
2027
2028 ##print "absolute<$absolute>\n";
2029
2030 # See if any suffix of this path is a path within the tree.
2031 while ($file =~ s@^[^/]*/@@) {
2032 if (-f "$root/$file") {
2033 ##print "file<$file>\n";
2034 last;
2035 }
2036 }
2037 if (! -f _) {
2038 return 0;
2039 }
2040
2041 # It is, so see if the prefix is acceptable.
2042 my $prefix = $absolute;
2043 substr($prefix, -length($file)) = '';
2044
2045 ##print "prefix<$prefix>\n";
2046 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002047 WARN("USE_RELATIVE_PATH",
2048 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002049 }
2050}
2051
Joe Perches3705ce52013-07-03 15:05:31 -07002052sub trim {
2053 my ($string) = @_;
2054
Joe Perchesb34c6482013-09-11 14:24:01 -07002055 $string =~ s/^\s+|\s+$//g;
2056
2057 return $string;
2058}
2059
2060sub ltrim {
2061 my ($string) = @_;
2062
2063 $string =~ s/^\s+//;
2064
2065 return $string;
2066}
2067
2068sub rtrim {
2069 my ($string) = @_;
2070
2071 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002072
2073 return $string;
2074}
2075
Joe Perches52ea8502013-11-12 15:10:09 -08002076sub string_find_replace {
2077 my ($string, $find, $replace) = @_;
2078
2079 $string =~ s/$find/$replace/g;
2080
2081 return $string;
2082}
2083
Joe Perches3705ce52013-07-03 15:05:31 -07002084sub tabify {
2085 my ($leading) = @_;
2086
2087 my $source_indent = 8;
2088 my $max_spaces_before_tab = $source_indent - 1;
2089 my $spaces_to_tab = " " x $source_indent;
2090
2091 #convert leading spaces to tabs
2092 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2093 #Remove spaces before a tab
2094 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2095
2096 return "$leading";
2097}
2098
Gregory Beanf07523e2011-07-06 15:36:58 -07002099sub cleanup_continuation_headers {
2100 # Collapse any header-continuation lines into a single line so they
2101 # can be parsed meaningfully, as the parser only has one line
2102 # of context to work with.
2103 my $again;
2104 do {
2105 $again = 0;
2106 foreach my $n (0 .. scalar(@rawlines) - 2) {
2107 if ($rawlines[$n]=~/^\s*$/) {
2108 # A blank line means there's no more chance
2109 # of finding headers. Shortcut to done.
2110 return;
2111 }
2112 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2113 $rawlines[$n+1]=~/^\s+/) {
2114 # Continuation header. Collapse it.
2115 my $line = splice @rawlines, $n+1, 1;
2116 $line=~s/^\s+/ /;
2117 $rawlines[$n] .= $line;
2118 # We've 'destabilized' the list, so restart.
2119 $again = 1;
2120 last;
2121 }
2122 }
2123 } while ($again);
2124}
2125
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002126sub pos_last_openparen {
2127 my ($line) = @_;
2128
2129 my $pos = 0;
2130
2131 my $opens = $line =~ tr/\(/\(/;
2132 my $closes = $line =~ tr/\)/\)/;
2133
2134 my $last_openparen = 0;
2135
2136 if (($opens == 0) || ($closes >= $opens)) {
2137 return -1;
2138 }
2139
2140 my $len = length($line);
2141
2142 for ($pos = 0; $pos < $len; $pos++) {
2143 my $string = substr($line, $pos);
2144 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2145 $pos += length($1) - 1;
2146 } elsif (substr($line, $pos, 1) eq '(') {
2147 $last_openparen = $pos;
2148 } elsif (index($string, '(') == -1) {
2149 last;
2150 }
2151 }
2152
Joe Perches91cb5192014-04-03 14:49:32 -07002153 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002154}
2155
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002156sub process {
2157 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002158
2159 my $linenr=0;
2160 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002162 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002163 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002164 my $subjectline="";
2165 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002167 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002168 my $indent;
2169 my $previndent=0;
2170 my $stashindent=0;
2171
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002172 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002173 my $signoff = 0;
2174 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002175 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002176 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002177 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002178 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002179 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002180 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002181 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002182 my $non_utf8_charset = 0;
2183
Joe Perches365dd4e2014-08-06 16:10:42 -07002184 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002185 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002186
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002187 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 our $cnt_lines = 0;
2189 our $cnt_error = 0;
2190 our $cnt_warn = 0;
2191 our $cnt_chk = 0;
2192
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002193 # Trace the real file/line as we go.
2194 my $realfile = '';
2195 my $realline = 0;
2196 my $realcnt = 0;
2197 my $here = '';
2198 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002199 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002200 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002201 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002202
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002203 my $prev_values = 'E';
2204
2205 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002207 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002208 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002209 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002210
Joe Perches7e51f192013-09-11 14:23:57 -07002211 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002214 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002215 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002216 my @setup_docs = ();
2217 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218
Joe Perchesd8b07712013-11-12 15:10:06 -08002219 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002220 my $shorttext = BEFORE_SHORTTEXT;
2221 my $shorttext_exspc = 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002222 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002223
Andy Whitcroft773647a2008-03-28 14:15:58 -07002224 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002225 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002226 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002227
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002228 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 $linenr++;
2230 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002231
Joe Perches3705ce52013-07-03 15:05:31 -07002232 push(@fixed, $rawline) if ($fix);
2233
Andy Whitcroft773647a2008-03-28 14:15:58 -07002234 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002235 $setup_docs = 0;
2236 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2237 $setup_docs = 1;
2238 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002241 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2242 $realline=$1-1;
2243 if (defined $2) {
2244 $realcnt=$3+1;
2245 } else {
2246 $realcnt=1+1;
2247 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002248 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249
2250 # Guestimate if this is a continuing comment. Run
2251 # the context looking for a comment "edge". If this
2252 # edge is a close comment then we must be in a comment
2253 # at context start.
2254 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002255 my $cnt = $realcnt;
2256 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2257 next if (defined $rawlines[$ln - 1] &&
2258 $rawlines[$ln - 1] =~ /^-/);
2259 $cnt--;
2260 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002261 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002262 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2263 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2264 ($edge) = $1;
2265 last;
2266 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 }
2268 if (defined $edge && $edge eq '*/') {
2269 $in_comment = 1;
2270 }
2271
2272 # Guestimate if this is a continuing comment. If this
2273 # is the start of a diff block and this line starts
2274 # ' *' then it is very likely a comment.
2275 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002276 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 {
2278 $in_comment = 1;
2279 }
2280
2281 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2282 sanitise_line_reset($in_comment);
2283
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002286 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 $line = sanitise_line($rawline);
2288 }
2289 push(@lines, $line);
2290
2291 if ($realcnt > 1) {
2292 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2293 } else {
2294 $realcnt = 0;
2295 }
2296
2297 #print "==>$rawline\n";
2298 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299
2300 if ($setup_docs && $line =~ /^\+/) {
2301 push(@setup_docs, $line);
2302 }
2303 }
2304
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002305 $prefix = '';
2306
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 $realcnt = 0;
2308 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002309 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002310 foreach my $line (@lines) {
2311 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002312 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002313 my $sline = $line; #copy of $line
2314 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002316 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002317
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002319 if (!$in_commit_log &&
2320 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002321 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002322 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002323 $realline=$1-1;
2324 if (defined $2) {
2325 $realcnt=$3+1;
2326 } else {
2327 $realcnt=1+1;
2328 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002329 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002330 $prev_values = 'E';
2331
Andy Whitcroft773647a2008-03-28 14:15:58 -07002332 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002333 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002334 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002335 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002336 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002337
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002338# track the line number as we move through the hunk, note that
2339# new versions of GNU diff omit the leading space on completely
2340# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002343 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002344
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002345 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002346 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002347
2348 # Track the previous line.
2349 ($prevline, $stashline) = ($stashline, $line);
2350 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002351 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2352
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002354
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002355 } elsif ($realcnt == 1) {
2356 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002357 }
2358
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002359 my $hunk_line = ($realcnt != 0);
2360
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002361 $here = "#$linenr: " if (!$file);
2362 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002364 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002366 if ($line =~ /^diff --git.*?(\S+)$/) {
2367 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002368 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002369 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002370 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002371 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002372 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002373 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002374 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002375
2376 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002377 if (!$file && $tree && $p1_prefix ne '' &&
2378 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002379 WARN("PATCH_PREFIX",
2380 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002381 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002382
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002383 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002384 ERROR("MODIFIED_INCLUDE_ASM",
2385 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002386 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002387 $found_file = 1;
2388 }
2389
Joe Perches34d88152015-06-25 15:03:05 -07002390#make up the handle for any error we report on this line
2391 if ($showfile) {
2392 $prefix = "$realfile:$realline: "
2393 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002394 if ($file) {
2395 $prefix = "$filename:$realline: ";
2396 } else {
2397 $prefix = "$filename:$linenr: ";
2398 }
Joe Perches34d88152015-06-25 15:03:05 -07002399 }
2400
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002401 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002402 if (is_maintained_obsolete($realfile)) {
2403 WARN("OBSOLETE",
2404 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2405 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002406 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002407 $check = 1;
2408 } else {
2409 $check = $check_orig;
2410 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002411 next;
2412 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002413 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002414
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002415 my $hereline = "$here\n$rawline\n";
2416 my $herecurr = "$here\n$rawline\n";
2417 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002418
David Keitel72bf3e82016-03-23 20:52:44 -07002419 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002420 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2421 # the subject line was just processed,
2422 # a blank line must be next
2423 WARN("NONBLANK_AFTER_SUMMARY",
2424 "non-blank line after summary line\n" . $herecurr);
2425 $shorttext = IN_SHORTTEXT;
2426 # this non-blank line may or may not be commit text -
2427 # a warning has been generated so assume it is commit
2428 # text and move on
2429 $commit_text_present = 1;
2430 # fall through and treat this line as IN_SHORTTEXT
2431 }
David Keitel72bf3e82016-03-23 20:52:44 -07002432 if ($shorttext == IN_SHORTTEXT) {
2433 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002434 if ($commit_text_present == 0) {
2435 WARN("NO_COMMIT_TEXT",
2436 "please add commit text explaining " .
2437 "*why* the change is needed\n" .
2438 $herecurr);
2439 }
David Keitel72bf3e82016-03-23 20:52:44 -07002440 $shorttext = AFTER_SHORTTEXT;
Steve Muckled360fe42012-03-09 11:15:24 -08002441 } elsif ($line=~/^\s*change-id:/i ||
2442 $line=~/^\s*signed-off-by:/i ||
2443 $line=~/^\s*crs-fixed:/i ||
2444 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002445 # this is a tag, there must be commit
2446 # text by now
2447 if ($commit_text_present == 0) {
2448 WARN("NO_COMMIT_TEXT",
2449 "please add commit text explaining " .
2450 "*why* the change is needed\n" .
2451 $herecurr);
2452 # prevent duplicate warnings
2453 $commit_text_present = 1;
2454 }
2455 } elsif ($line=~/\S/) {
2456 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002457 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002458 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2459 # case of non-blank line in this state handled above
2460 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002461 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002462# The Subject line doesn't have to be the last header in the patch.
2463# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2464# Per RFC5322, continuation lines must be folded, so any left-justified
2465# text which looks like a header is definitely a header.
2466 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2467 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002468 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002469 if (length($line) != 0) {
2470 WARN("NONBLANK_AFTER_SUMMARY",
2471 "non-blank line after " .
2472 "summary line\n" .
2473 $sublinenr . $here .
2474 "\n" . $subjectline .
2475 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002476 # this non-blank line may or may not
2477 # be commit text - a warning has been
2478 # generated so assume it is commit
2479 # text and move on
2480 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002481 }
David Keitele288d232016-03-23 20:57:21 -07002482 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002483 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002484 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002485 # This is the subject line. Go to
2486 # CHECK_NEXT_SHORTTEXT to wait for the commit
2487 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002488 $shorttext = CHECK_NEXT_SHORTTEXT;
2489 $subjectline = $line;
2490 $sublinenr = "#$linenr & ";
2491# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002492 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002493 WARN("LONG_SUMMARY_LINE",
2494 "summary line over " .
2495 SHORTTEXT_LIMIT .
2496 " characters\n" . $herecurr);
2497 }
2498 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002499 # Indented format, this must be the summary
2500 # line (i.e. git show). There will be no more
2501 # headers so we are now in the shorttext.
2502 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002503 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002504 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002505 WARN("LONG_SUMMARY_LINE",
2506 "summary line over " .
2507 SHORTTEXT_LIMIT .
2508 " characters\n" . $herecurr);
2509 }
2510 }
2511 }
2512
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002513 $cnt_lines++ if ($realcnt != 0);
2514
Joe Perchese518e9a2015-06-25 15:03:27 -07002515# Check if the commit log has what seems like a diff which can confuse patch
2516 if ($in_commit_log && !$commit_log_has_diff &&
2517 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2518 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2519 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2520 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2521 ERROR("DIFF_IN_COMMIT_MSG",
2522 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2523 $commit_log_has_diff = 1;
2524 }
2525
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002526# Check for incorrect file permissions
2527 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2528 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002529 if ($realfile !~ m@scripts/@ &&
2530 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002531 ERROR("EXECUTE_PERMISSIONS",
2532 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002533 }
2534 }
2535
Joe Perches20112472011-07-25 17:13:23 -07002536# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002537 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002538 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002539 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002540 }
2541
Joe Perchese0d975b2014-12-10 15:51:49 -08002542# Check if MAINTAINERS is being updated. If so, there's probably no need to
2543# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2544 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2545 $reported_maintainer_file = 1;
2546 }
2547
Joe Perches20112472011-07-25 17:13:23 -07002548# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002549 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002550 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002551 my $space_before = $1;
2552 my $sign_off = $2;
2553 my $space_after = $3;
2554 my $email = $4;
2555 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2556
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002557 if ($sign_off !~ /$signature_tags/) {
2558 WARN("BAD_SIGN_OFF",
2559 "Non-standard signature: $sign_off\n" . $herecurr);
2560 }
Joe Perches20112472011-07-25 17:13:23 -07002561 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002562 if (WARN("BAD_SIGN_OFF",
2563 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2564 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002565 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002566 "$ucfirst_sign_off $email";
2567 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002568 }
Joe Perches20112472011-07-25 17:13:23 -07002569 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002570 if (WARN("BAD_SIGN_OFF",
2571 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002573 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002574 "$ucfirst_sign_off $email";
2575 }
2576
Joe Perches20112472011-07-25 17:13:23 -07002577 }
2578 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if (WARN("BAD_SIGN_OFF",
2580 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2581 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002582 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002583 "$ucfirst_sign_off $email";
2584 }
Joe Perches20112472011-07-25 17:13:23 -07002585 }
2586
2587 my ($email_name, $email_address, $comment) = parse_email($email);
2588 my $suggested_email = format_email(($email_name, $email_address));
2589 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002590 ERROR("BAD_SIGN_OFF",
2591 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002592 } else {
2593 my $dequoted = $suggested_email;
2594 $dequoted =~ s/^"//;
2595 $dequoted =~ s/" </ </;
2596 # Don't force email to have quotes
2597 # Allow just an angle bracketed address
2598 if ("$dequoted$comment" ne $email &&
2599 "<$email_address>$comment" ne $email &&
2600 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002601 WARN("BAD_SIGN_OFF",
2602 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002603 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002604 }
Maya Erezec77bd62017-02-26 09:14:43 +02002605 if ($chk_author) {
2606 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2607 $qca_sign_off = 1;
2608 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2609 $codeaurora_sign_off = 1;
2610 } elsif ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
2611 WARN("BAD_SIGN_OFF",
2612 "invalid Signed-off-by identity\n" . $line );
2613 }
2614 }
Joe Perches7e51f192013-09-11 14:23:57 -07002615
2616# Check for duplicate signatures
2617 my $sig_nospace = $line;
2618 $sig_nospace =~ s/\s//g;
2619 $sig_nospace = lc($sig_nospace);
2620 if (defined $signatures{$sig_nospace}) {
Jordan Crouse82e60672018-08-08 13:01:36 -06002621 WARN("DUPLICATE_SIGN_OFF",
Joe Perches7e51f192013-09-11 14:23:57 -07002622 "Duplicate signature\n" . $herecurr);
2623 } else {
2624 $signatures{$sig_nospace} = 1;
2625 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002626 }
2627
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002628# Check email subject for common tools that don't need to be mentioned
2629 if ($in_header_lines &&
2630 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2631 WARN("EMAIL_SUBJECT",
2632 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2633 }
2634
Joe Perches9b3189e2014-06-04 16:12:10 -07002635# Check for old stable address
2636 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2637 ERROR("STABLE_ADDRESS",
2638 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2639 }
2640
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002641# Check for unwanted Gerrit info
2642 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2643 ERROR("GERRIT_CHANGE_ID",
2644 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2645 }
2646
Joe Perches369c8dd2015-11-06 16:31:34 -08002647# Check if the commit log is in a possible stack dump
2648 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2649 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2650 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2651 # timestamp
2652 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2653 # stack dump address
2654 $commit_log_possible_stack_dump = 1;
2655 }
2656
Joe Perches2a076f42015-04-16 12:44:28 -07002657# Check for line lengths > 75 in commit log, warn once
2658 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002659 length($line) > 75 &&
2660 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2661 # file delta changes
2662 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2663 # filename then :
2664 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2665 # A Fixes: or Link: line
2666 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002667 WARN("COMMIT_LOG_LONG_LINE",
2668 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2669 $commit_log_long_line = 1;
2670 }
2671
Joe Perchesbf4daf12015-09-09 15:37:50 -07002672# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002673 if ($in_commit_log && $commit_log_possible_stack_dump &&
2674 $line =~ /^\s*$/) {
2675 $commit_log_possible_stack_dump = 0;
2676 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002677
Joe Perches0d7835f2015-02-13 14:38:35 -08002678# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002679 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002680 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wang5b101522017-04-07 15:22:19 -07002681 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002682 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002683 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002684 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2685 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002686 my $init_char = "c";
2687 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002688 my $short = 1;
2689 my $long = 0;
2690 my $case = 1;
2691 my $space = 1;
2692 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002693 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002694 my $id = '0123456789ab';
2695 my $orig_desc = "commit description";
2696 my $description = "";
2697
Joe Perchesfe043ea2015-09-09 15:37:25 -07002698 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2699 $init_char = $1;
2700 $orig_commit = lc($2);
2701 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2702 $orig_commit = lc($1);
2703 }
2704
Joe Perches0d7835f2015-02-13 14:38:35 -08002705 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2706 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2707 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2708 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2709 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2710 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002711 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002712 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2713 defined $rawlines[$linenr] &&
2714 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2715 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002716 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002717 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2718 defined $rawlines[$linenr] &&
2719 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2720 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2721 $orig_desc = $1;
2722 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2723 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002724 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002725 }
2726
2727 ($id, $description) = git_commit_info($orig_commit,
2728 $id, $orig_desc);
2729
Joe Perches19c146a2015-02-13 14:39:00 -08002730 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002731 ERROR("GIT_COMMIT_ID",
2732 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2733 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002734 }
2735
Joe Perches13f19372014-08-06 16:10:59 -07002736# Check for added, moved or deleted files
2737 if (!$reported_maintainer_file && !$in_commit_log &&
2738 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2739 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2740 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2741 (defined($1) || defined($2))))) {
2742 $reported_maintainer_file = 1;
2743 WARN("FILE_PATH_CHANGES",
2744 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2745 }
2746
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002747#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002748 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
2749 $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002750 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2751 }
2752
Andy Whitcroft00df3442007-06-08 13:47:06 -07002753# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002754 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002755 ERROR("CORRUPTED_PATCH",
2756 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002757 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002758 }
2759
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002760# Check for absolute kernel paths.
2761 if ($tree) {
2762 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2763 my $file = $1;
2764
2765 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2766 check_absolute_file($1, $herecurr)) {
2767 #
2768 } else {
2769 check_absolute_file($file, $herecurr);
2770 }
2771 }
2772 }
2773
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002774# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2775 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002776 $rawline !~ m/^$UTF8*$/) {
2777 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2778
2779 my $blank = copy_spacing($rawline);
2780 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2781 my $hereptr = "$hereline$ptr\n";
2782
Joe Perches34d99212011-07-25 17:13:26 -07002783 CHK("INVALID_UTF8",
2784 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002785 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002786
Joe Perches15662b32011-10-31 17:13:12 -07002787# Check if it's the start of a commit log
2788# (not a header line and we haven't seen the patch filename)
2789 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002790 !($rawline =~ /^\s+\S/ ||
2791 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002792 $in_header_lines = 0;
2793 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002794 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002795 }
2796
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002797# Check if there is UTF-8 in a commit log when a mail header has explicitly
2798# declined it, i.e defined some charset where it is missing.
2799 if ($in_header_lines &&
2800 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2801 $1 !~ /utf-8/i) {
2802 $non_utf8_charset = 1;
2803 }
2804
2805 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002806 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002807 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002808 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2809 }
2810
Kees Cook66b47b42014-10-13 15:51:57 -07002811# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002812 if (defined($misspellings) &&
2813 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002814 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002815 my $typo = $1;
2816 my $typo_fix = $spelling_fix{lc($typo)};
2817 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2818 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2819 my $msg_type = \&WARN;
2820 $msg_type = \&CHK if ($file);
2821 if (&{$msg_type}("TYPO_SPELLING",
2822 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2823 $fix) {
2824 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2825 }
2826 }
2827 }
2828
Andy Whitcroft306708542008-10-15 22:02:28 -07002829# ignore non-hunk lines and lines being removed
2830 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002831
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002832#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002833 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002834 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002835 if (ERROR("DOS_LINE_ENDINGS",
2836 "DOS line endings\n" . $herevet) &&
2837 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002838 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002839 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002840 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2841 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002842 if (ERROR("TRAILING_WHITESPACE",
2843 "trailing whitespace\n" . $herevet) &&
2844 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002845 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002846 }
2847
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002848 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002849 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002850
Josh Triplett4783f892013-11-12 15:10:12 -08002851# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002852 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002853 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2854 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002855 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2856 my $msg_type = \&ERROR;
2857 $msg_type = \&CHK if ($file);
2858 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002859 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Josh Triplett4783f892013-11-12 15:10:12 -08002860 }
2861
Andi Kleen33549572010-05-24 14:33:29 -07002862# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002863# Only applies when adding the entry originally, after that we do not have
2864# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002865 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002866 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002867 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002868 my $cnt = $realcnt;
2869 my $ln = $linenr + 1;
2870 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002871 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002872 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002873 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002874 $f = $lines[$ln - 1];
2875 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2876 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002877
2878 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002879 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002880
Joe Perches8d73e0e2014-08-06 16:10:46 -07002881 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002882 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002883 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002884 $length = -1;
2885 }
2886
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002887 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002888 $f =~ s/#.*//;
2889 $f =~ s/^\s+//;
2890 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002891 if ($f =~ /^\s*config\s/) {
2892 $is_end = 1;
2893 last;
2894 }
Andi Kleen33549572010-05-24 14:33:29 -07002895 $length++;
2896 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002897 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2898 WARN("CONFIG_DESCRIPTION",
2899 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2900 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002901 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002902 }
2903
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002904# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2905 if ($realfile =~ /Kconfig/ &&
2906 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2907 WARN("CONFIG_EXPERIMENTAL",
2908 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2909 }
2910
Christoph Jaeger327953e2015-02-13 14:38:29 -08002911# discourage the use of boolean for type definition attributes of Kconfig options
2912 if ($realfile =~ /Kconfig/ &&
2913 $line =~ /^\+\s*\bboolean\b/) {
2914 WARN("CONFIG_TYPE_BOOLEAN",
2915 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2916 }
2917
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002918 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2919 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2920 my $flag = $1;
2921 my $replacement = {
2922 'EXTRA_AFLAGS' => 'asflags-y',
2923 'EXTRA_CFLAGS' => 'ccflags-y',
2924 'EXTRA_CPPFLAGS' => 'cppflags-y',
2925 'EXTRA_LDFLAGS' => 'ldflags-y',
2926 };
2927
2928 WARN("DEPRECATED_VARIABLE",
2929 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2930 }
2931
Rob Herringbff5da42014-01-23 15:54:51 -08002932# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002933 if (defined $root &&
2934 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2935 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2936
Rob Herringbff5da42014-01-23 15:54:51 -08002937 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2938
Florian Vaussardcc933192014-04-03 14:49:27 -07002939 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2940 my $vp_file = $dt_path . "vendor-prefixes.txt";
2941
Rob Herringbff5da42014-01-23 15:54:51 -08002942 foreach my $compat (@compats) {
2943 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002944 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2945 my $compat3 = $compat;
2946 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2947 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002948 if ( $? >> 8 ) {
2949 WARN("UNDOCUMENTED_DT_STRING",
2950 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2951 }
2952
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002953 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2954 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002955 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002956 if ( $? >> 8 ) {
2957 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002958 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002959 }
2960 }
2961 }
2962
Andy Whitcroft5368df22008-10-15 22:02:27 -07002963# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002964 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002965
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002966# do DT checks:
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002967# * Property names should be lower-case
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002968# * Only newline after };
2969
2970 if ($realfile =~ /\.(dts|dtsi)$/ && $line =~ /^\+/) {
2971 if($line =~ /\};.+$/) { # check for any characters after };
2972 ERROR("DT_STYLE", "newline does not follow immediately after };\n" . $herecurr);
2973 }
Prathyush Katukojwalafe930102017-07-11 15:04:47 -07002974
2975 if($line =~ /[0-9a-zA-Z,\._\+\?#]+\s*=/ && $line !~ /[0-9a-z,\._\+\?#]+\s*=/) { # find property names with uppercase characters
2976 ERROR("DT_PROPERTY_NAME", "property name is not lowercase\n" . $herecurr);
2977 }
Prathyush Katukojwala52034352017-07-11 15:00:01 -07002978 }
2979
Joe Perches47e0c882015-06-25 15:02:57 -07002980# line length limit (with some exclusions)
2981#
2982# There are a few types of lines that may extend beyond $max_line_length:
2983# logging functions like pr_info that end in a string
2984# lines with a single string
2985# #defines that are a single string
2986#
2987# There are 3 different line length message types:
2988# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2989# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2990# LONG_LINE all other lines longer than $max_line_length
2991#
2992# if LONG_LINE is ignored, the other 2 types are also ignored
2993#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002994 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002995 my $msg_type = "LONG_LINE";
2996
2997 # Check the allowed long line types first
2998
2999 # logging functions that end in a string that starts
3000 # before $max_line_length
3001 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3002 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3003 $msg_type = "";
3004
3005 # lines with only strings (w/ possible termination)
3006 # #defines with only strings
3007 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3008 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3009 $msg_type = "";
3010
Joe Perchesd560a5f2016-08-02 14:04:31 -07003011 # EFI_GUID is another special case
3012 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
3013 $msg_type = "";
3014
Bryan Huntsman9b1dda12018-02-12 17:26:10 -08003015 # Long copyright statements are another special case
3016 } elsif ($rawline =~ /^\+.\*.*copyright.*\(c\).*$/i) {
3017 $msg_type = "";
3018
Joe Perches47e0c882015-06-25 15:02:57 -07003019 # Otherwise set the alternate message types
3020
3021 # a comment starts before $max_line_length
3022 } elsif ($line =~ /($;[\s$;]*)$/ &&
3023 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3024 $msg_type = "LONG_LINE_COMMENT"
3025
3026 # a quoted string starts before $max_line_length
3027 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3028 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3029 $msg_type = "LONG_LINE_STRING"
3030 }
3031
3032 if ($msg_type ne "" &&
3033 (show_type("LONG_LINE") || show_type($msg_type))) {
3034 WARN($msg_type,
3035 "line over $max_line_length characters\n" . $herecurr);
3036 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003037 }
3038
Andy Whitcroft8905a672007-11-28 16:21:06 -08003039# check for adding lines without a newline.
3040 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003041 WARN("MISSING_EOF_NEWLINE",
3042 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003043 }
3044
Mike Frysinger42e41c52009-09-21 17:04:40 -07003045# Blackfin: use hi/lo macros
3046 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3047 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3048 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003049 ERROR("LO_MACRO",
3050 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003051 }
3052 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3053 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003054 ERROR("HI_MACRO",
3055 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003056 }
3057 }
3058
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003059# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003060 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003061
3062# at the beginning of a line any tabs must come first and anything
3063# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003064 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3065 $rawline =~ /^\+\s* \s*/) {
3066 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003067 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003068 if (ERROR("CODE_INDENT",
3069 "code indent should use tabs where possible\n" . $herevet) &&
3070 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003071 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003072 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003073 }
3074
Alberto Panizzo08e44362010-03-05 13:43:54 -08003075# check for space before tabs.
3076 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3077 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003078 if (WARN("SPACE_BEFORE_TAB",
3079 "please, no space before tabs\n" . $herevet) &&
3080 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003081 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003082 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003083 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003084 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003085 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003086 }
3087
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003088# check for && or || at the start of a line
3089 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3090 CHK("LOGICAL_CONTINUATIONS",
3091 "Logical continuations should be on the previous line\n" . $hereprev);
3092 }
3093
Joe Perchesa91e8992016-05-20 17:04:05 -07003094# check indentation starts on a tab stop
3095 if ($^V && $^V ge 5.10.0 &&
3096 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3097 my $indent = length($1);
3098 if ($indent % 8) {
3099 if (WARN("TABSTOP",
3100 "Statements should start on a tabstop\n" . $herecurr) &&
3101 $fix) {
3102 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3103 }
3104 }
3105 }
3106
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003107# check multi-line statement indentation matches previous line
3108 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003109 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003110 $prevline =~ /^\+(\t*)(.*)$/;
3111 my $oldindent = $1;
3112 my $rest = $2;
3113
3114 my $pos = pos_last_openparen($rest);
3115 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003116 $line =~ /^(\+| )([ \t]*)/;
3117 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003118
3119 my $goodtabindent = $oldindent .
3120 "\t" x ($pos / 8) .
3121 " " x ($pos % 8);
3122 my $goodspaceindent = $oldindent . " " x $pos;
3123
3124 if ($newindent ne $goodtabindent &&
3125 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003126
3127 if (CHK("PARENTHESIS_ALIGNMENT",
3128 "Alignment should match open parenthesis\n" . $hereprev) &&
3129 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003130 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003131 s/^\+[ \t]*/\+$goodtabindent/;
3132 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003133 }
3134 }
3135 }
3136
Joe Perches6ab3a972015-04-16 12:44:05 -07003137# check for space after cast like "(int) foo" or "(struct foo) bar"
3138# avoid checking a few false positives:
3139# "sizeof(<type>)" or "__alignof__(<type>)"
3140# function pointer declarations like "(*foo)(int) = bar;"
3141# structure definitions like "(struct foo) { 0 };"
3142# multiline macros that define functions
3143# known attributes or the __attribute__ keyword
3144 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3145 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003146 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003147 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003148 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003149 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003150 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003151 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003152 }
3153
Joe Perches86406b12015-09-09 15:37:41 -07003154# Block comment styles
3155# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003156 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003157 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003158 $rawline =~ /^\+[ \t]*\*/ &&
3159 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003160 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3161 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3162 }
3163
Joe Perches86406b12015-09-09 15:37:41 -07003164# Block comments use * on subsequent lines
3165 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3166 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003167 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003168 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003169 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003170 WARN("BLOCK_COMMENT_STYLE",
3171 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003172 }
3173
Joe Perches86406b12015-09-09 15:37:41 -07003174# Block comments use */ on trailing lines
3175 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003176 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3177 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3178 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003179 WARN("BLOCK_COMMENT_STYLE",
3180 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003181 }
3182
Joe Perches08eb9b82016-10-11 13:51:50 -07003183# Block comment * alignment
3184 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003185 $line =~ /^\+[ \t]*$;/ && #leading comment
3186 $rawline =~ /^\+[ \t]*\*/ && #leading *
3187 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003188 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003189 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3190 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003191 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003192 if (defined($1)) {
3193 $oldindent = expand_tabs($1);
3194 } else {
3195 $prevrawline =~ m@^\+(.*/?)\*@;
3196 $oldindent = expand_tabs($1);
3197 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003198 $rawline =~ m@^\+([ \t]*)\*@;
3199 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003200 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003201 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003202 WARN("BLOCK_COMMENT_STYLE",
3203 "Block comments should align the * on each line\n" . $hereprev);
3204 }
3205 }
3206
Joe Perches7f619192014-08-06 16:10:39 -07003207# check for missing blank lines after struct/union declarations
3208# with exceptions for various attributes and macros
3209 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3210 $line =~ /^\+/ &&
3211 !($line =~ /^\+\s*$/ ||
3212 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3213 $line =~ /^\+\s*MODULE_/i ||
3214 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3215 $line =~ /^\+[a-z_]*init/ ||
3216 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3217 $line =~ /^\+\s*DECLARE/ ||
3218 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003219 if (CHK("LINE_SPACING",
3220 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3221 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003222 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003223 }
Joe Perches7f619192014-08-06 16:10:39 -07003224 }
3225
Joe Perches365dd4e2014-08-06 16:10:42 -07003226# check for multiple consecutive blank lines
3227 if ($prevline =~ /^[\+ ]\s*$/ &&
3228 $line =~ /^\+\s*$/ &&
3229 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003230 if (CHK("LINE_SPACING",
3231 "Please don't use multiple blank lines\n" . $hereprev) &&
3232 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003233 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003234 }
3235
Joe Perches365dd4e2014-08-06 16:10:42 -07003236 $last_blank_line = $linenr;
3237 }
3238
Joe Perches3b617e32014-04-03 14:49:28 -07003239# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003240 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3241 # actual declarations
3242 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003243 # function pointer declarations
3244 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003245 # foo bar; where foo is some local typedef or #define
3246 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3247 # known declaration macros
3248 $prevline =~ /^\+\s+$declaration_macros/) &&
3249 # for "else if" which can look like "$Ident $Ident"
3250 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3251 # other possible extensions of declaration lines
3252 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3253 # not starting a section or a macro "\" extended line
3254 $prevline =~ /(?:\{\s*|\\)$/) &&
3255 # looks like a declaration
3256 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003257 # function pointer declarations
3258 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003259 # foo bar; where foo is some local typedef or #define
3260 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3261 # known declaration macros
3262 $sline =~ /^\+\s+$declaration_macros/ ||
3263 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003264 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003265 # start or end of block or continuation of declaration
3266 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3267 # bitfield continuation
3268 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3269 # other possible extensions of declaration lines
3270 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3271 # indentation of previous and current line are the same
3272 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003273 if (WARN("LINE_SPACING",
3274 "Missing a blank line after declarations\n" . $hereprev) &&
3275 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003276 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003277 }
Joe Perches3b617e32014-04-03 14:49:28 -07003278 }
3279
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003280# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003281# Exceptions:
3282# 1) within comments
3283# 2) indented preprocessor commands
3284# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003285 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003286 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003287 if (WARN("LEADING_SPACE",
3288 "please, no spaces at the start of a line\n" . $herevet) &&
3289 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003290 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003291 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003292 }
3293
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003294# check we are in a valid C source file if not then ignore this hunk
3295 next if ($realfile !~ /\.(h|c)$/);
3296
Joe Perches032a4c02014-08-06 16:10:29 -07003297# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003298# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003299# if the previous line is a break or return and is indented 1 tab more...
3300 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3301 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003302 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3303 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3304 defined $lines[$linenr] &&
3305 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003306 WARN("UNNECESSARY_ELSE",
3307 "else is not generally useful after a break or return\n" . $hereprev);
3308 }
3309 }
3310
Joe Perchesc00df192014-08-06 16:11:01 -07003311# check indentation of a line with a break;
3312# if the previous line is a goto or return and is indented the same # of tabs
3313 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3314 my $tabs = $1;
3315 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3316 WARN("UNNECESSARY_BREAK",
3317 "break is not useful after a goto or return\n" . $hereprev);
3318 }
3319 }
3320
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003321# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3322 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3323 WARN("CONFIG_EXPERIMENTAL",
3324 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3325 }
3326
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003327# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003328 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003329 WARN("CVS_KEYWORD",
3330 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003331 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003332
Mike Frysinger42e41c52009-09-21 17:04:40 -07003333# Blackfin: don't use __builtin_bfin_[cs]sync
3334 if ($line =~ /__builtin_bfin_csync/) {
3335 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003336 ERROR("CSYNC",
3337 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003338 }
3339 if ($line =~ /__builtin_bfin_ssync/) {
3340 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003341 ERROR("SSYNC",
3342 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003343 }
3344
Joe Perches56e77d72013-02-21 16:44:14 -08003345# check for old HOTPLUG __dev<foo> section markings
3346 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3347 WARN("HOTPLUG_SECTION",
3348 "Using $1 is unnecessary\n" . $herecurr);
3349 }
3350
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003351# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003352 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3353 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003354#print "LINE<$line>\n";
3355 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003356 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003357 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003358 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003359 $stat =~ s/\n./\n /g;
3360 $cond =~ s/\n./\n /g;
3361
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003362#print "linenr<$linenr> <$stat>\n";
3363 # If this statement has no statement boundaries within
3364 # it there is no point in retrying a statement scan
3365 # until we hit end of it.
3366 my $frag = $stat; $frag =~ s/;+\s*$//;
3367 if ($frag !~ /(?:{|;)/) {
3368#print "skip<$line_nr_next>\n";
3369 $suppress_statement = $line_nr_next;
3370 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003371
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003372 # Find the real next line.
3373 $realline_next = $line_nr_next;
3374 if (defined $realline_next &&
3375 (!defined $lines[$realline_next - 1] ||
3376 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3377 $realline_next++;
3378 }
3379
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003380 my $s = $stat;
3381 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003382
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003383 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003384 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003385
3386 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003387 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003388
Andy Whitcroft463f2862009-09-21 17:04:34 -07003389 } elsif ($s =~ /^.\s*else\b/s) {
3390
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003391 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003392 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003393 my $type = $1;
3394 $type =~ s/\s+/ /g;
3395 possible($type, "A:" . $s);
3396
Andy Whitcroft8905a672007-11-28 16:21:06 -08003397 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003398 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003399 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003400 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003401
3402 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003403 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003404 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003405 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003406
3407 # Check for any sort of function declaration.
3408 # int foo(something bar, other baz);
3409 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003410 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003411 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003412
Andy Whitcroftcf655042008-03-04 14:28:20 -08003413 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003414 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003415 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003416
Andy Whitcroft8905a672007-11-28 16:21:06 -08003417 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003418 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003419
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003420 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003421 }
3422 }
3423 }
3424
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003425 }
3426
Andy Whitcroft653d4872007-06-23 17:16:34 -07003427#
3428# Checks which may be anchored in the context.
3429#
3430
3431# Check for switch () and associated case and default
3432# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003433 if ($line=~/\bswitch\s*\(.*\)/) {
3434 my $err = '';
3435 my $sep = '';
3436 my @ctx = ctx_block_outer($linenr, $realcnt);
3437 shift(@ctx);
3438 for my $ctx (@ctx) {
3439 my ($clen, $cindent) = line_stats($ctx);
3440 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3441 $indent != $cindent) {
3442 $err .= "$sep$ctx\n";
3443 $sep = '';
3444 } else {
3445 $sep = "[...]\n";
3446 }
3447 }
3448 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003449 ERROR("SWITCH_CASE_INDENT_LEVEL",
3450 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003451 }
3452 }
3453
3454# if/while/etc brace do not go on next line, unless defining a do while loop,
3455# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003456 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003457 my $pre_ctx = "$1$2";
3458
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003459 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003460
3461 if ($line =~ /^\+\t{6,}/) {
3462 WARN("DEEP_INDENTATION",
3463 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3464 }
3465
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003466 my $ctx_cnt = $realcnt - $#ctx - 1;
3467 my $ctx = join("\n", @ctx);
3468
Andy Whitcroft548596d2008-07-23 21:29:01 -07003469 my $ctx_ln = $linenr;
3470 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003471
Andy Whitcroft548596d2008-07-23 21:29:01 -07003472 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3473 defined $lines[$ctx_ln - 1] &&
3474 $lines[$ctx_ln - 1] =~ /^-/)) {
3475 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3476 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003477 $ctx_ln++;
3478 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003479
Andy Whitcroft53210162008-07-23 21:29:03 -07003480 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3481 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003482
Joe Perchesd752fcc2014-08-06 16:11:05 -07003483 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003484 ERROR("OPEN_BRACE",
3485 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003486 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003487 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003488 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3489 $ctx =~ /\)\s*\;\s*$/ &&
3490 defined $lines[$ctx_ln - 1])
3491 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003492 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3493 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003494 WARN("TRAILING_SEMICOLON",
3495 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003496 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003497 }
3498 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003499 }
3500
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003501# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003502 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003503 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3504 ctx_statement_block($linenr, $realcnt, 0)
3505 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003506 my ($s, $c) = ($stat, $cond);
3507
3508 substr($s, 0, length($c), '');
3509
Joe Perches9f5af482015-09-09 15:37:30 -07003510 # remove inline comments
3511 $s =~ s/$;/ /g;
3512 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003513
3514 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003515 my @newlines = ($c =~ /\n/gs);
3516 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003517
Joe Perches9f5af482015-09-09 15:37:30 -07003518 # Make sure we remove the line prefixes as we have
3519 # none on the first line, and are going to readd them
3520 # where necessary.
3521 $s =~ s/\n./\n/gs;
3522 while ($s =~ /\n\s+\\\n/) {
3523 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3524 }
3525
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003526 # We want to check the first line inside the block
3527 # starting at the end of the conditional, so remove:
3528 # 1) any blank line termination
3529 # 2) any opening brace { on end of the line
3530 # 3) any do (...) {
3531 my $continuation = 0;
3532 my $check = 0;
3533 $s =~ s/^.*\bdo\b//;
3534 $s =~ s/^\s*{//;
3535 if ($s =~ s/^\s*\\//) {
3536 $continuation = 1;
3537 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003538 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003539 $check = 1;
3540 $cond_lines++;
3541 }
3542
3543 # Also ignore a loop construct at the end of a
3544 # preprocessor statement.
3545 if (($prevline =~ /^.\s*#\s*define\s/ ||
3546 $prevline =~ /\\\s*$/) && $continuation == 0) {
3547 $check = 0;
3548 }
3549
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003550 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003551 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003552 while ($cond_ptr != $cond_lines) {
3553 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003554
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003555 # If we see an #else/#elif then the code
3556 # is not linear.
3557 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3558 $check = 0;
3559 }
3560
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003561 # Ignore:
3562 # 1) blank lines, they should be at 0,
3563 # 2) preprocessor lines, and
3564 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003565 if ($continuation ||
3566 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003567 $s =~ /^\s*#\s*?/ ||
3568 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003569 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003570 if ($s =~ s/^.*?\n//) {
3571 $cond_lines++;
3572 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003573 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003574 }
3575
3576 my (undef, $sindent) = line_stats("+" . $s);
3577 my $stat_real = raw_line($linenr, $cond_lines);
3578
3579 # Check if either of these lines are modified, else
3580 # this is not this patch's fault.
3581 if (!defined($stat_real) ||
3582 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3583 $check = 0;
3584 }
3585 if (defined($stat_real) && $cond_lines > 1) {
3586 $stat_real = "[...]\n$stat_real";
3587 }
3588
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003589 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003590
Joe Perches9f5af482015-09-09 15:37:30 -07003591 if ($check && $s ne '' &&
3592 (($sindent % 8) != 0 ||
3593 ($sindent < $indent) ||
3594 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003595 WARN("SUSPECT_CODE_INDENT",
3596 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003597 }
3598 }
3599
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003600 # Track the 'values' across context and added lines.
3601 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003602 my ($curr_values, $curr_vars) =
3603 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003604 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003605 if ($dbg_values) {
3606 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003607 print "$linenr > .$outline\n";
3608 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003609 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003610 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003611 $prev_values = substr($curr_values, -1);
3612
Andy Whitcroft00df3442007-06-08 13:47:06 -07003613#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003614 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003615
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003616# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003617 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003618 my $type = $1;
3619 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003620 $var = "" if (!defined $var);
3621 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003622 my $sign = $1;
3623 my $pointer = $2;
3624
3625 $pointer = "" if (!defined $pointer);
3626
3627 if (WARN("UNSPECIFIED_INT",
3628 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3629 $fix) {
3630 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003631 my $comp_pointer = $pointer;
3632 $comp_pointer =~ s/\s//g;
3633 $decl .= $comp_pointer;
3634 $decl = rtrim($decl) if ($var eq "");
3635 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003636 }
3637 }
3638 }
3639
Andy Whitcroft653d4872007-06-23 17:16:34 -07003640# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003641 if ($dbg_type) {
3642 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003643 ERROR("TEST_TYPE",
3644 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003645 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003646 ERROR("TEST_NOT_TYPE",
3647 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003648 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003649 next;
3650 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003651# TEST: allow direct testing of the attribute matcher.
3652 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003653 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003654 ERROR("TEST_ATTR",
3655 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003656 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003657 ERROR("TEST_NOT_ATTR",
3658 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003659 }
3660 next;
3661 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003662
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003663# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003664 if ($line =~ /^.\s*{/ &&
3665 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003666 if (ERROR("OPEN_BRACE",
3667 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003668 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3669 fix_delete_line($fixlinenr - 1, $prevrawline);
3670 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003671 my $fixedline = $prevrawline;
3672 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003673 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003674 $fixedline = $line;
Cyril Bur93eae952017-07-10 15:52:21 -07003675 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003676 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003677 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003678 }
3679
Andy Whitcroft653d4872007-06-23 17:16:34 -07003680#
3681# Checks which are anchored on the added line.
3682#
3683
3684# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003685 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003686 my $path = $1;
3687 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003688 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003689 "malformed #include filename\n" . $herecurr);
3690 }
3691 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3692 ERROR("UAPI_INCLUDE",
3693 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003694 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003695 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003696
3697# no C99 // comments
3698 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003699 if (ERROR("C99_COMMENTS",
3700 "do not use C99 // comments\n" . $herecurr) &&
3701 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003702 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003703 if ($line =~ /\/\/(.*)$/) {
3704 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003705 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003706 }
3707 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003708 }
3709 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003710 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003711 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003712
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003713# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3714# the whole statement.
3715#print "APW <$lines[$realline_next - 1]>\n";
3716 if (defined $realline_next &&
3717 exists $lines[$realline_next - 1] &&
3718 !defined $suppress_export{$realline_next} &&
3719 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3720 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003721 # Handle definitions which produce identifiers with
3722 # a prefix:
3723 # XXX(foo);
3724 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003725 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003726 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003727 $name =~ /^${Ident}_$2/) {
3728#print "FOO C name<$name>\n";
3729 $suppress_export{$realline_next} = 1;
3730
3731 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003732 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003733 ^.DEFINE_$Ident\(\Q$name\E\)|
3734 ^.DECLARE_$Ident\(\Q$name\E\)|
3735 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003736 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3737 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003738 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003739#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3740 $suppress_export{$realline_next} = 2;
3741 } else {
3742 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003743 }
3744 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003745 if (!defined $suppress_export{$linenr} &&
3746 $prevline =~ /^.\s*$/ &&
3747 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3748 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3749#print "FOO B <$lines[$linenr - 1]>\n";
3750 $suppress_export{$linenr} = 2;
3751 }
3752 if (defined $suppress_export{$linenr} &&
3753 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003754 WARN("EXPORT_SYMBOL",
3755 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003756 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003757
Joe Eloff5150bda2010-08-09 17:21:00 -07003758# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003759 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003760 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003761 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003762 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003763 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003764 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003765 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003766# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003767 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003768 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003769 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003770 $herecurr) &&
3771 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003772 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003773 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003774 }
3775
Joe Perches18130872014-08-06 16:11:22 -07003776# check for misordered declarations of char/short/int/long with signed/unsigned
3777 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3778 my $tmp = trim($1);
3779 WARN("MISORDERED_TYPE",
3780 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3781 }
3782
Joe Perchescb710ec2010-10-26 14:23:20 -07003783# check for static const char * arrays.
3784 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003785 WARN("STATIC_CONST_CHAR_ARRAY",
3786 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003787 $herecurr);
3788 }
3789
3790# check for static char foo[] = "bar" declarations.
3791 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003792 WARN("STATIC_CONST_CHAR_ARRAY",
3793 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003794 $herecurr);
3795 }
3796
Joe Perchesab7e23f2015-04-16 12:44:22 -07003797# check for const <foo> const where <foo> is not a pointer or array type
3798 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3799 my $found = $1;
3800 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3801 WARN("CONST_CONST",
3802 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3803 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3804 WARN("CONST_CONST",
3805 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3806 }
3807 }
3808
Joe Perches9b0fa602014-04-03 14:49:18 -07003809# check for non-global char *foo[] = {"bar", ...} declarations.
3810 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3811 WARN("STATIC_CONST_CHAR_ARRAY",
3812 "char * array declaration might be better as static const\n" .
3813 $herecurr);
3814 }
3815
Joe Perchesb598b672015-04-16 12:44:36 -07003816# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3817 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3818 my $array = $1;
3819 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3820 my $array_div = $1;
3821 if (WARN("ARRAY_SIZE",
3822 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3823 $fix) {
3824 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3825 }
3826 }
3827 }
3828
Joe Perchesb36190c2014-01-27 17:07:18 -08003829# check for function declarations without arguments like "int foo()"
3830 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3831 if (ERROR("FUNCTION_WITHOUT_ARGS",
3832 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3833 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003834 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003835 }
3836 }
3837
Andy Whitcroft653d4872007-06-23 17:16:34 -07003838# check for new typedefs, only function parameters and sparse annotations
3839# make sense.
3840 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003841 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003842 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003843 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003844 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003845 WARN("NEW_TYPEDEFS",
3846 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003847 }
3848
3849# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003850 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003851 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3852 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003853 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003854
Andy Whitcroft65863862009-01-06 14:41:21 -08003855 # Should start with a space.
3856 $to =~ s/^(\S)/ $1/;
3857 # Should not end with a space.
3858 $to =~ s/\s+$//;
3859 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003860 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003861 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003862
Joe Perches3705ce52013-07-03 15:05:31 -07003863## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003864 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003865 if (ERROR("POINTER_LOCATION",
3866 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3867 $fix) {
3868 my $sub_from = $ident;
3869 my $sub_to = $ident;
3870 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003871 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003872 s@\Q$sub_from\E@$sub_to@;
3873 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003874 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003875 }
3876 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3877 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003878 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003879
Andy Whitcroft65863862009-01-06 14:41:21 -08003880 # Should start with a space.
3881 $to =~ s/^(\S)/ $1/;
3882 # Should not end with a space.
3883 $to =~ s/\s+$//;
3884 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003885 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003886 }
3887 # Modifiers should have spaces.
3888 $to =~ s/(\b$Modifier$)/$1 /;
3889
Joe Perches3705ce52013-07-03 15:05:31 -07003890## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003891 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003892 if (ERROR("POINTER_LOCATION",
3893 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3894 $fix) {
3895
3896 my $sub_from = $match;
3897 my $sub_to = $match;
3898 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003899 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003900 s@\Q$sub_from\E@$sub_to@;
3901 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003902 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003903 }
3904
Joe Perches9d3e3c72015-09-09 15:37:27 -07003905# avoid BUG() or BUG_ON()
3906 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3907 my $msg_type = \&WARN;
3908 $msg_type = \&CHK if ($file);
3909 &{$msg_type}("AVOID_BUG",
3910 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3911 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003912
Joe Perches9d3e3c72015-09-09 15:37:27 -07003913# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003914 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003915 WARN("LINUX_VERSION_CODE",
3916 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003917 }
3918
Joe Perches17441222011-06-15 15:08:17 -07003919# check for uses of printk_ratelimit
3920 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003921 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003922 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003923 }
3924
Andy Whitcroft00df3442007-06-08 13:47:06 -07003925# printk should use KERN_* levels. Note that follow on printk's on the
3926# same line do not need a level, so we use the current block context
3927# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003928# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003929# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003930 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003931 my $ok = 0;
3932 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3933 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003934 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003935 # with "\n" ignore it, else it is to blame
3936 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3937 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3938 $ok = 1;
3939 }
3940 last;
3941 }
3942 }
3943 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003944 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3945 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003946 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003947 }
3948
Joe Perches243f3802012-05-31 16:26:09 -07003949 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3950 my $orig = $1;
3951 my $level = lc($orig);
3952 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003953 my $level2 = $level;
3954 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003955 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003956 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003957 }
3958
3959 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003960 if (WARN("PREFER_PR_LEVEL",
3961 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3962 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003963 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003964 s/\bpr_warning\b/pr_warn/;
3965 }
Joe Perches243f3802012-05-31 16:26:09 -07003966 }
3967
Joe Perchesdc139312013-02-21 16:44:13 -08003968 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3969 my $orig = $1;
3970 my $level = lc($orig);
3971 $level = "warn" if ($level eq "warning");
3972 $level = "dbg" if ($level eq "debug");
3973 WARN("PREFER_DEV_LEVEL",
3974 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3975 }
3976
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003977# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3978# number of false positives, but assembly files are not checked, so at
3979# least the arch entry code will not trigger this warning.
3980 if ($line =~ /\bENOSYS\b/) {
3981 WARN("ENOSYS",
3982 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3983 }
3984
Andy Whitcroft653d4872007-06-23 17:16:34 -07003985# function brace can't be on same line, except for #defines of do while,
3986# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003987 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003988 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003989 if (ERROR("OPEN_BRACE",
3990 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3991 $fix) {
3992 fix_delete_line($fixlinenr, $rawline);
3993 my $fixed_line = $rawline;
3994 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3995 my $line1 = $1;
3996 my $line2 = $2;
3997 fix_insert_line($fixlinenr, ltrim($line1));
3998 fix_insert_line($fixlinenr, "\+{");
3999 if ($line2 !~ /^\s*$/) {
4000 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4001 }
4002 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004003 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004004
Andy Whitcroft8905a672007-11-28 16:21:06 -08004005# open braces for enum, union and struct go on the same line.
4006 if ($line =~ /^.\s*{/ &&
4007 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07004008 if (ERROR("OPEN_BRACE",
4009 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4010 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4011 fix_delete_line($fixlinenr - 1, $prevrawline);
4012 fix_delete_line($fixlinenr, $rawline);
4013 my $fixedline = rtrim($prevrawline) . " {";
4014 fix_insert_line($fixlinenr, $fixedline);
4015 $fixedline = $rawline;
Cyril Bur93eae952017-07-10 15:52:21 -07004016 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07004017 if ($fixedline !~ /^\+\s*$/) {
4018 fix_insert_line($fixlinenr, $fixedline);
4019 }
4020 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004021 }
4022
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004023# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004024 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4025 if (WARN("SPACING",
4026 "missing space after $1 definition\n" . $herecurr) &&
4027 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004028 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004029 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4030 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004031 }
4032
Joe Perches31070b52014-01-23 15:54:49 -08004033# Function pointer declarations
4034# check spacing between type, funcptr, and args
4035# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004036 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004037 my $declare = $1;
4038 my $pre_pointer_space = $2;
4039 my $post_pointer_space = $3;
4040 my $funcname = $4;
4041 my $post_funcname_space = $5;
4042 my $pre_args_space = $6;
4043
Joe Perches91f72e92014-04-03 14:49:12 -07004044# the $Declare variable will capture all spaces after the type
4045# so check it for a missing trailing missing space but pointer return types
4046# don't need a space so don't warn for those.
4047 my $post_declare_space = "";
4048 if ($declare =~ /(\s+)$/) {
4049 $post_declare_space = $1;
4050 $declare = rtrim($declare);
4051 }
4052 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004053 WARN("SPACING",
4054 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004055 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004056 }
4057
4058# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004059# This test is not currently implemented because these declarations are
4060# equivalent to
4061# int foo(int bar, ...)
4062# and this is form shouldn't/doesn't generate a checkpatch warning.
4063#
4064# elsif ($declare =~ /\s{2,}$/) {
4065# WARN("SPACING",
4066# "Multiple spaces after return type\n" . $herecurr);
4067# }
Joe Perches31070b52014-01-23 15:54:49 -08004068
4069# unnecessary space "type ( *funcptr)(args...)"
4070 if (defined $pre_pointer_space &&
4071 $pre_pointer_space =~ /^\s/) {
4072 WARN("SPACING",
4073 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4074 }
4075
4076# unnecessary space "type (* funcptr)(args...)"
4077 if (defined $post_pointer_space &&
4078 $post_pointer_space =~ /^\s/) {
4079 WARN("SPACING",
4080 "Unnecessary space before function pointer name\n" . $herecurr);
4081 }
4082
4083# unnecessary space "type (*funcptr )(args...)"
4084 if (defined $post_funcname_space &&
4085 $post_funcname_space =~ /^\s/) {
4086 WARN("SPACING",
4087 "Unnecessary space after function pointer name\n" . $herecurr);
4088 }
4089
4090# unnecessary space "type (*funcptr) (args...)"
4091 if (defined $pre_args_space &&
4092 $pre_args_space =~ /^\s/) {
4093 WARN("SPACING",
4094 "Unnecessary space before function pointer arguments\n" . $herecurr);
4095 }
4096
4097 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004098 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004099 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004100 }
4101 }
4102
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004103# check for spacing round square brackets; allowed:
4104# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004105# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4106# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004107 while ($line =~ /(.*?\s)\[/g) {
4108 my ($where, $prefix) = ($-[1], $1);
4109 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004110 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004111 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004112 if (ERROR("BRACKET_SPACE",
4113 "space prohibited before open square bracket '['\n" . $herecurr) &&
4114 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004115 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004116 s/^(\+.*?)\s+\[/$1\[/;
4117 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004118 }
4119 }
4120
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004121# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004122 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004123 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004124 my $ctx_before = substr($line, 0, $-[1]);
4125 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004126
4127 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004128 if ($name =~ /^(?:
4129 if|for|while|switch|return|case|
4130 volatile|__volatile__|
4131 __attribute__|format|__extension__|
4132 asm|__asm__)$/x)
4133 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004134 # cpp #define statements have non-optional spaces, ie
4135 # if there is a space between the name and the open
4136 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004137 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004138
4139 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004140 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004141
4142 # If this whole things ends with a type its most
4143 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004144 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004145
4146 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004147 if (WARN("SPACING",
4148 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4149 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004150 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004151 s/\b$name\s+\(/$name\(/;
4152 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004153 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004154 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004155
Andy Whitcroft653d4872007-06-23 17:16:34 -07004156# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004157 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004158 my $fixed_line = "";
4159 my $line_fixed = 0;
4160
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004161 my $ops = qr{
4162 <<=|>>=|<=|>=|==|!=|
4163 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4164 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004165 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004166 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004167 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004168 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004169
4170## print("element count: <" . $#elements . ">\n");
4171## foreach my $el (@elements) {
4172## print("el: <$el>\n");
4173## }
4174
4175 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004176 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004177
Joe Perches3705ce52013-07-03 15:05:31 -07004178 foreach my $el (@elements) {
4179 push(@fix_elements, substr($rawline, $off, length($el)));
4180 $off += length($el);
4181 }
4182
4183 $off = 0;
4184
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004185 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004186 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004187
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004188 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004189
4190 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4191
4192## print("n: <$n> good: <$good>\n");
4193
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004194 $off += length($elements[$n]);
4195
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004196 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004197 my $ca = substr($opline, 0, $off);
4198 my $cc = '';
4199 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4200 $cc = substr($opline, $off + length($elements[$n + 1]));
4201 }
4202 my $cb = "$ca$;$cc";
4203
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004204 my $a = '';
4205 $a = 'V' if ($elements[$n] ne '');
4206 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004207 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004208 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4209 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004210 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004211
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004212 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004213
4214 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004215 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004216 $c = 'V' if ($elements[$n + 2] ne '');
4217 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004218 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004219 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4220 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004221 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004222 } else {
4223 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004224 }
4225
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004226 my $ctx = "${a}x${c}";
4227
4228 my $at = "(ctx:$ctx)";
4229
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004230 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004231 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004232
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004233 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004234 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004235
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004236 # Get the full operator variant.
4237 my $opv = $op . substr($curr_vars, $off, 1);
4238
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004239 # Ignore operators passed as parameters.
4240 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004241 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004242
Andy Whitcroftcf655042008-03-04 14:28:20 -08004243# # Ignore comments
4244# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004245
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004246 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004247 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004248 if ($ctx !~ /.x[WEBC]/ &&
4249 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004250 if (ERROR("SPACING",
4251 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004252 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004253 $line_fixed = 1;
4254 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004255 }
4256
4257 # // is a comment
4258 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004259
Joe Perchesb00e4812014-04-03 14:49:33 -07004260 # : when part of a bitfield
4261 } elsif ($opv eq ':B') {
4262 # skip the bitfield test for now
4263
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004264 # No spaces for:
4265 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004266 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004267 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004268 if (ERROR("SPACING",
4269 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004270 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004271 if (defined $fix_elements[$n + 2]) {
4272 $fix_elements[$n + 2] =~ s/^\s+//;
4273 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004274 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004275 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004276 }
4277
Joe Perches23810972014-12-10 15:51:32 -08004278 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004279 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004280 my $rtrim_before = 0;
4281 my $space_after = 0;
4282 if ($ctx =~ /Wx./) {
4283 if (ERROR("SPACING",
4284 "space prohibited before that '$op' $at\n" . $hereptr)) {
4285 $line_fixed = 1;
4286 $rtrim_before = 1;
4287 }
4288 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004289 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004290 if (ERROR("SPACING",
4291 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004292 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004293 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004294 $space_after = 1;
4295 }
4296 }
4297 if ($rtrim_before || $space_after) {
4298 if ($rtrim_before) {
4299 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4300 } else {
4301 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4302 }
4303 if ($space_after) {
4304 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004305 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004306 }
4307
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004308 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004309 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004310 #warn "'*' is part of type\n";
4311
4312 # unary operators should have a space before and
4313 # none after. May be left adjacent to another
4314 # unary operator, or a cast
4315 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004316 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004317 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004318 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004319 if (ERROR("SPACING",
4320 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004321 if ($n != $last_after + 2) {
4322 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4323 $line_fixed = 1;
4324 }
Joe Perches3705ce52013-07-03 15:05:31 -07004325 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004326 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004327 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004328 # A unary '*' may be const
4329
4330 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004331 if (ERROR("SPACING",
4332 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004333 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004334 if (defined $fix_elements[$n + 2]) {
4335 $fix_elements[$n + 2] =~ s/^\s+//;
4336 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004337 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004338 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004339 }
4340
4341 # unary ++ and unary -- are allowed no space on one side.
4342 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004343 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004344 if (ERROR("SPACING",
4345 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004346 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004347 $line_fixed = 1;
4348 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004349 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004350 if ($ctx =~ /Wx[BE]/ ||
4351 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004352 if (ERROR("SPACING",
4353 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004354 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004355 $line_fixed = 1;
4356 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004357 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004358 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004359 if (ERROR("SPACING",
4360 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004361 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004362 if (defined $fix_elements[$n + 2]) {
4363 $fix_elements[$n + 2] =~ s/^\s+//;
4364 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004365 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004366 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004367 }
4368
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004369 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004370 } elsif ($op eq '<<' or $op eq '>>' or
4371 $op eq '&' or $op eq '^' or $op eq '|' or
4372 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004373 $op eq '*' or $op eq '/' or
4374 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004375 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004376 if ($check) {
4377 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4378 if (CHK("SPACING",
4379 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4380 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4381 $fix_elements[$n + 2] =~ s/^\s+//;
4382 $line_fixed = 1;
4383 }
4384 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4385 if (CHK("SPACING",
4386 "space preferred before that '$op' $at\n" . $hereptr)) {
4387 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4388 $line_fixed = 1;
4389 }
4390 }
4391 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004392 if (ERROR("SPACING",
4393 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004394 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4395 if (defined $fix_elements[$n + 2]) {
4396 $fix_elements[$n + 2] =~ s/^\s+//;
4397 }
Joe Perches3705ce52013-07-03 15:05:31 -07004398 $line_fixed = 1;
4399 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004400 }
4401
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004402 # A colon needs no spaces before when it is
4403 # terminating a case value or a label.
4404 } elsif ($opv eq ':C' || $opv eq ':L') {
4405 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004406 if (ERROR("SPACING",
4407 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004408 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004409 $line_fixed = 1;
4410 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004411 }
4412
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004413 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004414 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004415 my $ok = 0;
4416
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004417 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004418 if (($op eq '<' &&
4419 $cc =~ /^\S+\@\S+>/) ||
4420 ($op eq '>' &&
4421 $ca =~ /<\S+\@\S+$/))
4422 {
4423 $ok = 1;
4424 }
4425
Joe Perchese0df7e12015-04-16 12:44:53 -07004426 # for asm volatile statements
4427 # ignore a colon with another
4428 # colon immediately before or after
4429 if (($op eq ':') &&
4430 ($ca =~ /:$/ || $cc =~ /^:/)) {
4431 $ok = 1;
4432 }
4433
Joe Perches84731622013-11-12 15:10:05 -08004434 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004435 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004436 my $msg_type = \&ERROR;
4437 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4438
4439 if (&{$msg_type}("SPACING",
4440 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004441 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4442 if (defined $fix_elements[$n + 2]) {
4443 $fix_elements[$n + 2] =~ s/^\s+//;
4444 }
Joe Perches3705ce52013-07-03 15:05:31 -07004445 $line_fixed = 1;
4446 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004447 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004448 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004449 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004450
4451## print("n: <$n> GOOD: <$good>\n");
4452
4453 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004454 }
Joe Perches3705ce52013-07-03 15:05:31 -07004455
4456 if (($#elements % 2) == 0) {
4457 $fixed_line = $fixed_line . $fix_elements[$#elements];
4458 }
4459
Joe Perches194f66f2014-08-06 16:11:03 -07004460 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4461 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004462 }
4463
4464
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004465 }
4466
Joe Perches786b6322013-07-03 15:05:32 -07004467# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004468 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004469 if (WARN("SPACING",
4470 "space prohibited before semicolon\n" . $herecurr) &&
4471 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004472 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004473 s/^(\+.*\S)\s+;/$1;/;
4474 }
4475 }
4476
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004477# check for multiple assignments
4478 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004479 CHK("MULTIPLE_ASSIGNMENTS",
4480 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004481 }
4482
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004483## # check for multiple declarations, allowing for a function declaration
4484## # continuation.
4485## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4486## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4487##
4488## # Remove any bracketed sections to ensure we do not
4489## # falsly report the parameters of functions.
4490## my $ln = $line;
4491## while ($ln =~ s/\([^\(\)]*\)//g) {
4492## }
4493## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004494## WARN("MULTIPLE_DECLARATION",
4495## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004496## }
4497## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004498
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004499#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004500 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004501 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004502 if (ERROR("SPACING",
4503 "space required before the open brace '{'\n" . $herecurr) &&
4504 $fix) {
Cyril Bur93eae952017-07-10 15:52:21 -07004505 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004506 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004507 }
4508
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004509## # check for blank lines before declarations
4510## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4511## $prevrawline =~ /^.\s*$/) {
4512## WARN("SPACING",
4513## "No blank lines before declarations\n" . $hereprev);
4514## }
4515##
4516
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004517# closing brace should have a space following it when it has anything
4518# on the line
4519 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004520 if (ERROR("SPACING",
4521 "space required after that close brace '}'\n" . $herecurr) &&
4522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004523 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004524 s/}((?!(?:,|;|\)))\S)/} $1/;
4525 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004526 }
4527
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004528# check spacing on square brackets
4529 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004530 if (ERROR("SPACING",
4531 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004533 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004534 s/\[\s+/\[/;
4535 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004536 }
4537 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004538 if (ERROR("SPACING",
4539 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4540 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004541 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004542 s/\s+\]/\]/;
4543 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004544 }
4545
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004546# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004547 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004548 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004549 if (ERROR("SPACING",
4550 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4551 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004552 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004553 s/\(\s+/\(/;
4554 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004555 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004556 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004557 $line !~ /for\s*\(.*;\s+\)/ &&
4558 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004559 if (ERROR("SPACING",
4560 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4561 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004562 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004563 s/\s+\)/\)/;
4564 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004565 }
4566
Joe Perchese2826fd2014-08-06 16:10:48 -07004567# check unnecessary parentheses around addressof/dereference single $Lvals
4568# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4569
4570 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004571 my $var = $1;
4572 if (CHK("UNNECESSARY_PARENTHESES",
4573 "Unnecessary parentheses around $var\n" . $herecurr) &&
4574 $fix) {
4575 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4576 }
4577 }
4578
4579# check for unnecessary parentheses around function pointer uses
4580# ie: (foo->bar)(); should be foo->bar();
4581# but not "if (foo->bar) (" to avoid some false positives
4582 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4583 my $var = $2;
4584 if (CHK("UNNECESSARY_PARENTHESES",
4585 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4586 $fix) {
4587 my $var2 = deparenthesize($var);
4588 $var2 =~ s/\s//g;
4589 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4590 }
4591 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004592
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004593#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004594 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004595 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004596 if (WARN("INDENTED_LABEL",
4597 "labels should not be indented\n" . $herecurr) &&
4598 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004599 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004600 s/^(.)\s+/$1/;
4601 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004602 }
4603
Joe Perches5b9553a2014-04-03 14:49:21 -07004604# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004605 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004606 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004607 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004608 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4609 my $value = $1;
4610 $value = deparenthesize($value);
4611 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4612 ERROR("RETURN_PARENTHESES",
4613 "return is not a function, parentheses are not required\n" . $herecurr);
4614 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004615 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004616 ERROR("SPACING",
4617 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004618 }
4619 }
Joe Perches507e5142013-11-12 15:10:13 -08004620
Joe Perchesb43ae212014-06-23 13:22:07 -07004621# unnecessary return in a void function
4622# at end-of-function, with the previous line a single leading tab, then return;
4623# and the line before that not a goto label target like "out:"
4624 if ($sline =~ /^[ \+]}\s*$/ &&
4625 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4626 $linenr >= 3 &&
4627 $lines[$linenr - 3] =~ /^[ +]/ &&
4628 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004629 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004630 "void function return statements are not generally useful\n" . $hereprev);
4631 }
Joe Perches9819cf22014-06-04 16:12:09 -07004632
Joe Perches189248d2014-01-23 15:54:47 -08004633# if statements using unnecessary parentheses - ie: if ((foo == bar))
4634 if ($^V && $^V ge 5.10.0 &&
4635 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4636 my $openparens = $1;
4637 my $count = $openparens =~ tr@\(@\(@;
4638 my $msg = "";
4639 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4640 my $comp = $4; #Not $1 because of $LvalOrFunc
4641 $msg = " - maybe == should be = ?" if ($comp eq "==");
4642 WARN("UNNECESSARY_PARENTHESES",
4643 "Unnecessary parentheses$msg\n" . $herecurr);
4644 }
4645 }
4646
Joe Perchesc5595fa2015-09-09 15:37:58 -07004647# comparisons with a constant or upper case identifier on the left
4648# avoid cases like "foo + BAR < baz"
4649# only fix matches surrounded by parentheses to avoid incorrect
4650# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4651 if ($^V && $^V ge 5.10.0 &&
4652 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4653 my $lead = $1;
4654 my $const = $2;
4655 my $comp = $3;
4656 my $to = $4;
4657 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004658 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004659 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4660 WARN("CONSTANT_COMPARISON",
4661 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4662 $fix) {
4663 if ($comp eq "<") {
4664 $newcomp = ">";
4665 } elsif ($comp eq "<=") {
4666 $newcomp = ">=";
4667 } elsif ($comp eq ">") {
4668 $newcomp = "<";
4669 } elsif ($comp eq ">=") {
4670 $newcomp = "<=";
4671 }
4672 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4673 }
4674 }
4675
Joe Perchesf34e4a42015-04-16 12:44:19 -07004676# Return of what appears to be an errno should normally be negative
4677 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004678 my $name = $1;
4679 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004680 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004681 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004682 }
4683 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004684
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004685# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004686 if ($line =~ /\b(if|while|for|switch)\(/) {
4687 if (ERROR("SPACING",
4688 "space required before the open parenthesis '('\n" . $herecurr) &&
4689 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004690 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004691 s/\b(if|while|for|switch)\(/$1 \(/;
4692 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004693 }
4694
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004695# Check for illegal assignment in if conditional -- and check for trailing
4696# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004697 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004698 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4699 ctx_statement_block($linenr, $realcnt, 0)
4700 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004701 my ($stat_next) = ctx_statement_block($line_nr_next,
4702 $remain_next, $off_next);
4703 $stat_next =~ s/\n./\n /g;
4704 ##print "stat<$stat> stat_next<$stat_next>\n";
4705
4706 if ($stat_next =~ /^\s*while\b/) {
4707 # If the statement carries leading newlines,
4708 # then count those as offsets.
4709 my ($whitespace) =
4710 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4711 my $offset =
4712 statement_rawlines($whitespace) - 1;
4713
4714 $suppress_whiletrailers{$line_nr_next +
4715 $offset} = 1;
4716 }
4717 }
4718 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004719 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004720 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004721 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004722
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004723 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004724 ERROR("ASSIGN_IN_IF",
4725 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004726 }
4727
4728 # Find out what is on the end of the line after the
4729 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004730 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004731 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004732 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004733 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4734 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004735 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004736 # Find out how long the conditional actually is.
4737 my @newlines = ($c =~ /\n/gs);
4738 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004739 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004740
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004741 $stat_real = raw_line($linenr, $cond_lines)
4742 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004743 if (defined($stat_real) && $cond_lines > 1) {
4744 $stat_real = "[...]\n$stat_real";
4745 }
4746
Joe Perches000d1cc12011-07-25 17:13:25 -07004747 ERROR("TRAILING_STATEMENTS",
4748 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004749 }
4750 }
4751
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004752# Check for bitwise tests written as boolean
4753 if ($line =~ /
4754 (?:
4755 (?:\[|\(|\&\&|\|\|)
4756 \s*0[xX][0-9]+\s*
4757 (?:\&\&|\|\|)
4758 |
4759 (?:\&\&|\|\|)
4760 \s*0[xX][0-9]+\s*
4761 (?:\&\&|\|\||\)|\])
4762 )/x)
4763 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004764 WARN("HEXADECIMAL_BOOLEAN_TEST",
4765 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004766 }
4767
Andy Whitcroft8905a672007-11-28 16:21:06 -08004768# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004769 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4770 my $s = $1;
4771 $s =~ s/$;//g; # Remove any comments
4772 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004773 ERROR("TRAILING_STATEMENTS",
4774 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004775 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004776 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004777# if should not continue a brace
4778 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004779 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004780 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004781 $herecurr);
4782 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004783# case and default should not have general statements after them
4784 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4785 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004786 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004787 \s*return\s+
4788 )/xg)
4789 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004790 ERROR("TRAILING_STATEMENTS",
4791 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004792 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004793
4794 # Check for }<nl>else {, these must be at the same
4795 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004796 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4797 $previndent == $indent) {
4798 if (ERROR("ELSE_AFTER_BRACE",
4799 "else should follow close brace '}'\n" . $hereprev) &&
4800 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4801 fix_delete_line($fixlinenr - 1, $prevrawline);
4802 fix_delete_line($fixlinenr, $rawline);
4803 my $fixedline = $prevrawline;
4804 $fixedline =~ s/}\s*$//;
4805 if ($fixedline !~ /^\+\s*$/) {
4806 fix_insert_line($fixlinenr, $fixedline);
4807 }
4808 $fixedline = $rawline;
4809 $fixedline =~ s/^(.\s*)else/$1} else/;
4810 fix_insert_line($fixlinenr, $fixedline);
4811 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004812 }
4813
Joe Perches8b8856f2014-08-06 16:11:14 -07004814 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4815 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004816 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4817
4818 # Find out what is on the end of the line after the
4819 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004820 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004821 $s =~ s/\n.*//g;
4822
4823 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004824 if (ERROR("WHILE_AFTER_BRACE",
4825 "while should follow close brace '}'\n" . $hereprev) &&
4826 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4827 fix_delete_line($fixlinenr - 1, $prevrawline);
4828 fix_delete_line($fixlinenr, $rawline);
4829 my $fixedline = $prevrawline;
4830 my $trailing = $rawline;
4831 $trailing =~ s/^\+//;
4832 $trailing = trim($trailing);
4833 $fixedline =~ s/}\s*$/} $trailing/;
4834 fix_insert_line($fixlinenr, $fixedline);
4835 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004836 }
4837 }
4838
Joe Perches95e2c602013-07-03 15:05:20 -07004839#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004840 while ($line =~ m{($Constant|$Lval)}g) {
4841 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004842
4843#gcc binary extension
4844 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004845 if (WARN("GCC_BINARY_CONSTANT",
4846 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4847 $fix) {
4848 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004849 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004850 s/\b$var\b/$hexval/;
4851 }
Joe Perches95e2c602013-07-03 15:05:20 -07004852 }
4853
4854#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004855 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004856 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004857#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004858 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004859#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004860 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4861#Ignore some three character SI units explicitly, like MiB and KHz
4862 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004863 while ($var =~ m{($Ident)}g) {
4864 my $word = $1;
4865 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004866 if ($check) {
4867 seed_camelcase_includes();
4868 if (!$file && !$camelcase_file_seeded) {
4869 seed_camelcase_file($realfile);
4870 $camelcase_file_seeded = 1;
4871 }
4872 }
Joe Perches7e781f62013-09-11 14:23:55 -07004873 if (!defined $camelcase{$word}) {
4874 $camelcase{$word} = 1;
4875 CHK("CAMELCASE",
4876 "Avoid CamelCase: <$word>\n" . $herecurr);
4877 }
Joe Perches34456862013-07-03 15:05:34 -07004878 }
Joe Perches323c1262012-12-17 16:02:07 -08004879 }
4880 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004881
4882#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004883 if ($line =~ /\#\s*define.*\\\s+$/) {
4884 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4885 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4886 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004887 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004888 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004889 }
4890
Fabian Frederick0e212e02015-04-16 12:44:25 -07004891# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4892# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004893 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004894 my $file = "$1.h";
4895 my $checkfile = "include/linux/$file";
4896 if (-f "$root/$checkfile" &&
4897 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004898 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004899 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004900 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4901 if ($asminclude > 0) {
4902 if ($realfile =~ m{^arch/}) {
4903 CHK("ARCH_INCLUDE_LINUX",
4904 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4905 } else {
4906 WARN("INCLUDE_LINUX",
4907 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4908 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004909 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004910 }
4911 }
4912
Andy Whitcroft653d4872007-06-23 17:16:34 -07004913# multi-statement macros should be enclosed in a do while loop, grab the
4914# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004915# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004916 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4917 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004918 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004919 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004920 my ($off, $dstat, $dcond, $rest);
4921 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004922 my $has_flow_statement = 0;
4923 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004924 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004925 ctx_statement_block($linenr, $realcnt, 0);
4926 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004927 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004928 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004929
Joe Perches08a28432014-10-13 15:51:55 -07004930 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004931 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004932
Joe Perchesf59b64b2016-10-11 13:52:08 -07004933 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4934 my $define_args = $1;
4935 my $define_stmt = $dstat;
4936 my @def_args = ();
4937
4938 if (defined $define_args && $define_args ne "") {
4939 $define_args = substr($define_args, 1, length($define_args) - 2);
4940 $define_args =~ s/\s*//g;
4941 @def_args = split(",", $define_args);
4942 }
4943
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004944 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004945 $dstat =~ s/\\\n.//g;
4946 $dstat =~ s/^\s*//s;
4947 $dstat =~ s/\s*$//s;
4948
4949 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004950 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4951 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004952 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004953 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004954 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004955
Gregory Bean02870be2011-05-11 09:11:12 -07004956 # Extremely long macros may fall off the end of the
4957 # available context without closing. Give a dangling
4958 # backslash the benefit of the doubt and allow it
4959 # to gobble any hanging open-parens.
4960 $dstat =~ s/\(.+\\$/1/;
4961
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004962 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004963 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4964 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004965 {
4966 }
4967
Joe Perches42e15292016-03-15 14:58:01 -07004968 # Make asm volatile uses seem like a generic function
4969 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4970
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004971 my $exceptions = qr{
4972 $Declare|
4973 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004974 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004975 DECLARE_PER_CPU|
4976 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004977 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004978 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004979 union|
4980 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004981 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004982 ^\"|\"$|
4983 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004984 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004985 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004986
4987 $ctx =~ s/\n*$//;
4988 my $herectx = $here . "\n";
4989 my $stmt_cnt = statement_rawlines($ctx);
4990
4991 for (my $n = 0; $n < $stmt_cnt; $n++) {
4992 $herectx .= raw_line($linenr, $n) . "\n";
4993 }
4994
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004995 if ($dstat ne '' &&
4996 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4997 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004998 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004999 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005000 $dstat !~ /$exceptions/ &&
5001 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07005002 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08005003 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005004 $dstat !~ /^for\s*$Constant$/ && # for (...)
5005 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5006 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07005007 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07005008 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005009 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005010
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005011 if ($dstat =~ /;/) {
5012 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5013 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5014 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005015 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005016 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005017 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005018
5019 }
Joe Perches52076492016-10-11 13:52:14 -07005020
5021 # Make $define_stmt single line, comment-free, etc
5022 my @stmt_array = split('\n', $define_stmt);
5023 my $first = 1;
5024 $define_stmt = "";
5025 foreach my $l (@stmt_array) {
5026 $l =~ s/\\$//;
5027 if ($first) {
5028 $define_stmt = $l;
5029 $first = 0;
5030 } elsif ($l =~ /^[\+ ]/) {
5031 $define_stmt .= substr($l, 1);
5032 }
5033 }
5034 $define_stmt =~ s/$;//g;
5035 $define_stmt =~ s/\s+/ /g;
5036 $define_stmt = trim($define_stmt);
5037
Joe Perchesf59b64b2016-10-11 13:52:08 -07005038# check if any macro arguments are reused (ignore '...' and 'type')
5039 foreach my $arg (@def_args) {
5040 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005041 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005042 my $tmp = $define_stmt;
5043 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
Joe Perches52076492016-10-11 13:52:14 -07005044 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005045 $tmp =~ s/\b$arg\s*\#\#//g;
5046 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5047 if ($use_cnt > 1) {
5048 CHK("MACRO_ARG_REUSE",
5049 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005050 }
5051# check if any macro arguments may have other precedence issues
5052 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5053 ((defined($1) && $1 ne ',') ||
5054 (defined($2) && $2 ne ','))) {
5055 CHK("MACRO_ARG_PRECEDENCE",
5056 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005057 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005058 }
Joe Perches5023d342012-12-17 16:01:47 -08005059
Joe Perches08a28432014-10-13 15:51:55 -07005060# check for macros with flow control, but without ## concatenation
5061# ## concatenation is commonly a macro that defines a function so ignore those
5062 if ($has_flow_statement && !$has_arg_concat) {
5063 my $herectx = $here . "\n";
5064 my $cnt = statement_rawlines($ctx);
5065
5066 for (my $n = 0; $n < $cnt; $n++) {
5067 $herectx .= raw_line($linenr, $n) . "\n";
5068 }
5069 WARN("MACRO_WITH_FLOW_CONTROL",
5070 "Macros with flow control statements should be avoided\n" . "$herectx");
5071 }
5072
Joe Perches481eb482012-12-17 16:01:56 -08005073# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005074
5075 } else {
5076 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005077 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5078 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005079 $line =~ /^\+.*\\$/) {
5080 WARN("LINE_CONTINUATIONS",
5081 "Avoid unnecessary line continuations\n" . $herecurr);
5082 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005083 }
5084
Joe Perchesb13edf72012-07-30 14:41:24 -07005085# do {} while (0) macro tests:
5086# single-statement macros do not need to be enclosed in do while (0) loop,
5087# macro should not end with a semicolon
5088 if ($^V && $^V ge 5.10.0 &&
5089 $realfile !~ m@/vmlinux.lds.h$@ &&
5090 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5091 my $ln = $linenr;
5092 my $cnt = $realcnt;
5093 my ($off, $dstat, $dcond, $rest);
5094 my $ctx = '';
5095 ($dstat, $dcond, $ln, $cnt, $off) =
5096 ctx_statement_block($linenr, $realcnt, 0);
5097 $ctx = $dstat;
5098
5099 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005100 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005101
5102 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5103 my $stmts = $2;
5104 my $semis = $3;
5105
5106 $ctx =~ s/\n*$//;
5107 my $cnt = statement_rawlines($ctx);
5108 my $herectx = $here . "\n";
5109
5110 for (my $n = 0; $n < $cnt; $n++) {
5111 $herectx .= raw_line($linenr, $n) . "\n";
5112 }
5113
Joe Perchesac8e97f2012-08-21 16:15:53 -07005114 if (($stmts =~ tr/;/;/) == 1 &&
5115 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005116 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5117 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5118 }
5119 if (defined $semis && $semis ne "") {
5120 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5121 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5122 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005123 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5124 $ctx =~ s/\n*$//;
5125 my $cnt = statement_rawlines($ctx);
5126 my $herectx = $here . "\n";
5127
5128 for (my $n = 0; $n < $cnt; $n++) {
5129 $herectx .= raw_line($linenr, $n) . "\n";
5130 }
5131
5132 WARN("TRAILING_SEMICOLON",
5133 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005134 }
5135 }
5136
Mike Frysinger080ba922009-01-06 14:41:25 -08005137# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5138# all assignments may have only one of the following with an assignment:
5139# .
5140# ALIGN(...)
5141# VMLINUX_SYMBOL(...)
5142 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005143 WARN("MISSING_VMLINUX_SYMBOL",
5144 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005145 }
5146
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005147# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005148 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5149 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005150 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005151 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005152 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5153 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005154 my @allowed = ();
5155 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005156 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005157 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005158 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005159 for my $chunk (@chunks) {
5160 my ($cond, $block) = @{$chunk};
5161
Andy Whitcroft773647a2008-03-28 14:15:58 -07005162 # If the condition carries leading newlines, then count those as offsets.
5163 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5164 my $offset = statement_rawlines($whitespace) - 1;
5165
Joe Perchesaad4f612012-03-23 15:02:19 -07005166 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005167 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5168
5169 # We have looked at and allowed this specific line.
5170 $suppress_ifbraces{$ln + $offset} = 1;
5171
5172 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005173 $ln += statement_rawlines($block) - 1;
5174
Andy Whitcroft773647a2008-03-28 14:15:58 -07005175 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005176
5177 $seen++ if ($block =~ /^\s*{/);
5178
Joe Perchesaad4f612012-03-23 15:02:19 -07005179 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005180 if (statement_lines($cond) > 1) {
5181 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005182 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005183 }
5184 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005185 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005186 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005187 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005188 if (statement_block_size($block) > 1) {
5189 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005190 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005191 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005192 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005193 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005194 if ($seen) {
5195 my $sum_allowed = 0;
5196 foreach (@allowed) {
5197 $sum_allowed += $_;
5198 }
5199 if ($sum_allowed == 0) {
5200 WARN("BRACES",
5201 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5202 } elsif ($sum_allowed != $allow &&
5203 $seen != $allow) {
5204 CHK("BRACES",
5205 "braces {} should be used on all arms of this statement\n" . $herectx);
5206 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005207 }
5208 }
5209 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005210 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005211 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005212 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005213
Andy Whitcroftcf655042008-03-04 14:28:20 -08005214 # Check the pre-context.
5215 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5216 #print "APW: ALLOWED: pre<$1>\n";
5217 $allowed = 1;
5218 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005219
5220 my ($level, $endln, @chunks) =
5221 ctx_statement_full($linenr, $realcnt, $-[0]);
5222
Andy Whitcroftcf655042008-03-04 14:28:20 -08005223 # Check the condition.
5224 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005225 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005226 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005227 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005228 }
5229 if (statement_lines($cond) > 1) {
5230 #print "APW: ALLOWED: cond<$cond>\n";
5231 $allowed = 1;
5232 }
5233 if ($block =~/\b(?:if|for|while)\b/) {
5234 #print "APW: ALLOWED: block<$block>\n";
5235 $allowed = 1;
5236 }
5237 if (statement_block_size($block) > 1) {
5238 #print "APW: ALLOWED: lines block<$block>\n";
5239 $allowed = 1;
5240 }
5241 # Check the post-context.
5242 if (defined $chunks[1]) {
5243 my ($cond, $block) = @{$chunks[1]};
5244 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005245 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005246 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005247 if ($block =~ /^\s*\{/) {
5248 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5249 $allowed = 1;
5250 }
5251 }
5252 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005253 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005254 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005255
Andy Whitcroftf0556632008-10-15 22:02:23 -07005256 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005257 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005258 }
5259
Joe Perches000d1cc12011-07-25 17:13:25 -07005260 WARN("BRACES",
5261 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005262 }
5263 }
5264
Joe Perches0979ae62012-12-17 16:01:59 -08005265# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005266 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005267 if (CHK("BRACES",
5268 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5269 $fix && $prevrawline =~ /^\+/) {
5270 fix_delete_line($fixlinenr - 1, $prevrawline);
5271 }
Joe Perches0979ae62012-12-17 16:01:59 -08005272 }
Joe Perches77b9a532013-07-03 15:05:29 -07005273 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005274 if (CHK("BRACES",
5275 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5276 $fix) {
5277 fix_delete_line($fixlinenr, $rawline);
5278 }
Joe Perches0979ae62012-12-17 16:01:59 -08005279 }
5280
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005281# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005282 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5283 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005284 WARN("VOLATILE",
5285 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005286 }
5287
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005288# Check for user-visible strings broken across lines, which breaks the ability
5289# to grep for the string. Make exceptions when the previous string ends in a
5290# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5291# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005292 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005293 $prevline =~ /"\s*$/ &&
5294 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5295 if (WARN("SPLIT_STRING",
5296 "quoted string split across lines\n" . $hereprev) &&
5297 $fix &&
5298 $prevrawline =~ /^\+.*"\s*$/ &&
5299 $last_coalesced_string_linenr != $linenr - 1) {
5300 my $extracted_string = get_quoted_string($line, $rawline);
5301 my $comma_close = "";
5302 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5303 $comma_close = $1;
5304 }
5305
5306 fix_delete_line($fixlinenr - 1, $prevrawline);
5307 fix_delete_line($fixlinenr, $rawline);
5308 my $fixedline = $prevrawline;
5309 $fixedline =~ s/"\s*$//;
5310 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5311 fix_insert_line($fixlinenr - 1, $fixedline);
5312 $fixedline = $rawline;
5313 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5314 if ($fixedline !~ /\+\s*$/) {
5315 fix_insert_line($fixlinenr, $fixedline);
5316 }
5317 $last_coalesced_string_linenr = $linenr;
5318 }
5319 }
5320
5321# check for missing a space in a string concatenation
5322 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5323 WARN('MISSING_SPACE',
5324 "break quoted strings at a space character\n" . $hereprev);
5325 }
5326
5327# check for spaces before a quoted newline
5328 if ($rawline =~ /^.*\".*\s\\n/) {
5329 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5330 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5331 $fix) {
5332 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5333 }
5334
5335 }
5336
Joe Perchesf17dba42014-10-13 15:51:51 -07005337# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005338 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005339 CHK("CONCATENATED_STRING",
5340 "Concatenated strings should use spaces between elements\n" . $herecurr);
5341 }
5342
Joe Perches90ad30e2014-12-10 15:51:59 -08005343# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005344 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005345 WARN("STRING_FRAGMENTS",
5346 "Consecutive strings are generally better as a single string\n" . $herecurr);
5347 }
5348
Joe Perches6e300752015-09-09 15:37:47 -07005349# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005350 my $string;
5351 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5352 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5353 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005354 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005355 WARN("PRINTF_L",
5356 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5357 last;
5358 }
Joe Perches6e300752015-09-09 15:37:47 -07005359 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5360 ERROR("PRINTF_0xDECIMAL",
5361 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5362 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005363 }
5364
5365# check for line continuations in quoted strings with odd counts of "
5366 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5367 WARN("LINE_CONTINUATIONS",
5368 "Avoid line continuations in quoted strings\n" . $herecurr);
5369 }
5370
Gregory Bean6c9c6842011-03-17 14:18:20 -07005371# sys_open/read/write/close are not allowed in the kernel
5372 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5373 ERROR("FILE_OPS",
5374 "$1 is inappropriate in kernel code.\n" .
5375 $herecurr);
5376 }
5377
Gregory Beana7cce0a2011-06-07 08:06:45 -07005378# filp_open is a backdoor for sys_open
5379 if ($line =~ /\b(filp_open)\b/) {
5380 ERROR("FILE_OPS",
5381 "$1 is inappropriate in kernel code.\n" .
5382 $herecurr);
5383 }
5384
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005385# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5386 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5387 ERROR("NON_RELAXED_IO",
5388 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5389 "with appropriate memory barriers instead.\n" .
5390 $herecurr);
5391 }
5392
5393# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5394 if ($line =~ /\b((in|out)([bwl]))\b/) {
5395 my ($all, $pref, $suf) = ($1, $2, $3);
5396 $pref =~ s/in/read/;
5397 $pref =~ s/out/write/;
5398 ERROR("NON_RELAXED_IO",
5399 "Use of $all is deprecated: use " .
5400 "__raw_$pref$suf\n\t" .
5401 "with appropriate memory barriers instead.\n" .
5402 $herecurr);
5403 }
5404
Gregory Bean438bf5f2011-06-15 09:32:38 -07005405# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005406 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005407 WARN("ARM_BARRIER",
5408 "Use of dsb is discouranged: prefer mb.\n" .
5409 $herecurr);
5410 }
5411
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005412# unbounded string functions are overflow risks
5413 my %str_fns = (
5414 "sprintf" => "snprintf",
5415 "strcpy" => "strlcpy",
5416 "strncpy" => "strlcpy",
5417 "strcat" => "strlcat",
5418 "strncat" => "strlcat",
5419 "vsprintf" => "vsnprintf",
5420 "strchr" => "strnchr",
5421 "strstr" => "strnstr",
5422 );
5423 foreach my $k (keys %str_fns) {
5424 if ($line =~ /\b$k\b/) {
5425 ERROR("UNBOUNDED_STRING_FNS",
5426 "Use of $k is deprecated: " .
5427 "use $str_fns{$k} instead.\n" .
5428 $herecurr);
5429 }
5430 }
5431
Andy Whitcroft00df3442007-06-08 13:47:06 -07005432# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005433 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005434 WARN("IF_0",
5435 "if this code is redundant consider removing it\n"
5436 . $herecurr);
5437 }
5438
5439# warn about #if 1
5440 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5441 WARN("IF_1",
5442 "if this code is required consider removing"
5443 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005444 }
5445
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005446# check for needless "if (<foo>) fn(<foo>)" uses
5447 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005448 my $tested = quotemeta($1);
5449 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5450 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5451 my $func = $1;
5452 if (WARN('NEEDLESS_IF',
5453 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5454 $fix) {
5455 my $do_fix = 1;
5456 my $leading_tabs = "";
5457 my $new_leading_tabs = "";
5458 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5459 $leading_tabs = $1;
5460 } else {
5461 $do_fix = 0;
5462 }
5463 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5464 $new_leading_tabs = $1;
5465 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5466 $do_fix = 0;
5467 }
5468 } else {
5469 $do_fix = 0;
5470 }
5471 if ($do_fix) {
5472 fix_delete_line($fixlinenr - 1, $prevrawline);
5473 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5474 }
5475 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005476 }
5477 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005478
Joe Perchesebfdc402014-08-06 16:10:27 -07005479# check for unnecessary "Out of Memory" messages
5480 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5481 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5482 (defined $1 || defined $3) &&
5483 $linenr > 3) {
5484 my $testval = $2;
5485 my $testline = $lines[$linenr - 3];
5486
5487 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5488# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5489
5490 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5491 WARN("OOM_MESSAGE",
5492 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5493 }
5494 }
5495
Joe Perchesf78d98f2014-10-13 15:52:01 -07005496# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005497 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005498 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5499 my $level = $1;
5500 if (WARN("UNNECESSARY_KERN_LEVEL",
5501 "Possible unnecessary $level\n" . $herecurr) &&
5502 $fix) {
5503 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5504 }
5505 }
5506
Joe Perchesabb08a52014-12-10 15:51:46 -08005507# check for mask then right shift without a parentheses
5508 if ($^V && $^V ge 5.10.0 &&
5509 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5510 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5511 WARN("MASK_THEN_SHIFT",
5512 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5513 }
5514
Joe Perchesb75ac612014-12-10 15:52:02 -08005515# check for pointer comparisons to NULL
5516 if ($^V && $^V ge 5.10.0) {
5517 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5518 my $val = $1;
5519 my $equal = "!";
5520 $equal = "" if ($4 eq "!=");
5521 if (CHK("COMPARISON_TO_NULL",
5522 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5523 $fix) {
5524 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5525 }
5526 }
5527 }
5528
Joe Perches8716de32013-09-11 14:24:05 -07005529# check for bad placement of section $InitAttribute (e.g.: __initdata)
5530 if ($line =~ /(\b$InitAttribute\b)/) {
5531 my $attr = $1;
5532 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5533 my $ptr = $1;
5534 my $var = $2;
5535 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5536 ERROR("MISPLACED_INIT",
5537 "$attr should be placed after $var\n" . $herecurr)) ||
5538 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5539 WARN("MISPLACED_INIT",
5540 "$attr should be placed after $var\n" . $herecurr))) &&
5541 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005542 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
Joe Perches8716de32013-09-11 14:24:05 -07005543 }
5544 }
5545 }
5546
Joe Perchese970b882013-11-12 15:10:10 -08005547# check for $InitAttributeData (ie: __initdata) with const
5548 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5549 my $attr = $1;
5550 $attr =~ /($InitAttributePrefix)(.*)/;
5551 my $attr_prefix = $1;
5552 my $attr_type = $2;
5553 if (ERROR("INIT_ATTRIBUTE",
5554 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5555 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005556 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005557 s/$InitAttributeData/${attr_prefix}initconst/;
5558 }
5559 }
5560
5561# check for $InitAttributeConst (ie: __initconst) without const
5562 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5563 my $attr = $1;
5564 if (ERROR("INIT_ATTRIBUTE",
5565 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5566 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005567 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005568 /(^\+\s*(?:static\s+))/;
5569 $lead = rtrim($1);
5570 $lead = "$lead " if ($lead !~ /^\+$/);
5571 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005572 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005573 }
5574 }
5575
Joe Perchesc17893c2015-04-16 12:44:42 -07005576# check for __read_mostly with const non-pointer (should just be const)
5577 if ($line =~ /\b__read_mostly\b/ &&
5578 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5579 if (ERROR("CONST_READ_MOSTLY",
5580 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5581 $fix) {
5582 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5583 }
5584 }
5585
Joe Perchesfbdb8132014-04-03 14:49:14 -07005586# don't use __constant_<foo> functions outside of include/uapi/
5587 if ($realfile !~ m@^include/uapi/@ &&
5588 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5589 my $constant_func = $1;
5590 my $func = $constant_func;
5591 $func =~ s/^__constant_//;
5592 if (WARN("CONSTANT_CONVERSION",
5593 "$constant_func should be $func\n" . $herecurr) &&
5594 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005595 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005596 }
5597 }
5598
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005599# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005600 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005601 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005602 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005603 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005604 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005605 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5606 }
5607 if ($delay > 2000) {
5608 WARN("LONG_UDELAY",
5609 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005610 }
5611 }
5612
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005613# warn about unexpectedly long msleep's
5614 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5615 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005616 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005617 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005618 }
5619 }
5620
Joe Perches36ec1932013-07-03 15:05:25 -07005621# check for comparisons of jiffies
5622 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5623 WARN("JIFFIES_COMPARISON",
5624 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5625 }
5626
Joe Perches9d7a34a2013-07-03 15:05:26 -07005627# check for comparisons of get_jiffies_64()
5628 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5629 WARN("JIFFIES_COMPARISON",
5630 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5631 }
5632
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005633# check the patch for use of mdelay
5634 if ($line =~ /\bmdelay\s*\(/) {
5635 WARN("MDELAY",
5636 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5637 }
5638
Andy Whitcroft00df3442007-06-08 13:47:06 -07005639# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005640# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005641# print "#ifdef in C files should be avoided\n";
5642# print "$herecurr";
5643# $clean = 0;
5644# }
5645
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005646# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005647 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005648 if (ERROR("SPACING",
5649 "exactly one space required after that #$1\n" . $herecurr) &&
5650 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005651 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005652 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5653 }
5654
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005655 }
5656
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005657# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005658 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5659 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005660 my $which = $1;
5661 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005662 CHK("UNCOMMENTED_DEFINITION",
5663 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005664 }
5665 }
5666# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005667
5668 my $barriers = qr{
5669 mb|
5670 rmb|
5671 wmb|
5672 read_barrier_depends
5673 }x;
5674 my $barrier_stems = qr{
5675 mb__before_atomic|
5676 mb__after_atomic|
5677 store_release|
5678 load_acquire|
5679 store_mb|
5680 (?:$barriers)
5681 }x;
5682 my $all_barriers = qr{
5683 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005684 smp_(?:$barrier_stems)|
5685 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005686 }x;
5687
5688 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005689 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005690 WARN("MEMORY_BARRIER",
5691 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005692 }
5693 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005694
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005695 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5696
5697 if ($realfile !~ m@^include/asm-generic/@ &&
5698 $realfile !~ m@/barrier\.h$@ &&
5699 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5700 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5701 WARN("MEMORY_BARRIER",
5702 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5703 }
5704
Joe Perchescb426e92015-06-25 15:02:46 -07005705# check for waitqueue_active without a comment.
5706 if ($line =~ /\bwaitqueue_active\s*\(/) {
5707 if (!ctx_has_comment($first_line, $linenr)) {
5708 WARN("WAITQUEUE_ACTIVE",
5709 "waitqueue_active without comment\n" . $herecurr);
5710 }
5711 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005712
5713# Check for expedited grace periods that interrupt non-idle non-nohz
5714# online CPUs. These expedited can therefore degrade real-time response
5715# if used carelessly, and should be avoided where not absolutely
5716# needed. It is always OK to use synchronize_rcu_expedited() and
5717# synchronize_sched_expedited() at boot time (before real-time applications
5718# start) and in error situations where real-time response is compromised in
5719# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5720# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5721# Of course, nothing comes for free, and srcu_read_lock() and
5722# srcu_read_unlock() do contain full memory barriers in payment for
5723# synchronize_srcu_expedited() non-interruption properties.
5724 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5725 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5726 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5727
5728 }
5729
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005730# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005731 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005732 CHK("ARCH_DEFINES",
5733 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005734 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005735
Tobias Klauserd4977c72010-05-24 14:33:30 -07005736# Check that the storage class is at the beginning of a declaration
5737 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005738 WARN("STORAGE_CLASS",
5739 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005740 }
5741
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005742# check the location of the inline attribute, that it is between
5743# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005744 if ($line =~ /\b$Type\s+$Inline\b/ ||
5745 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005746 ERROR("INLINE_LOCATION",
5747 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005748 }
5749
Andy Whitcroft8905a672007-11-28 16:21:06 -08005750# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005751 if ($realfile !~ m@\binclude/uapi/@ &&
5752 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005753 if (WARN("INLINE",
5754 "plain inline is preferred over $1\n" . $herecurr) &&
5755 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005756 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005757
5758 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005759 }
5760
Joe Perches3d130fd2011-01-12 17:00:00 -08005761# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005762 if ($realfile !~ m@\binclude/uapi/@ &&
5763 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005764 WARN("PREFER_PACKED",
5765 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005766 }
5767
Joe Perches39b7e282011-07-25 17:13:24 -07005768# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005769 if ($realfile !~ m@\binclude/uapi/@ &&
5770 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005771 WARN("PREFER_ALIGNED",
5772 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005773 }
5774
Joe Perches5f14d3b2012-01-10 15:09:52 -08005775# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005776 if ($realfile !~ m@\binclude/uapi/@ &&
5777 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005778 if (WARN("PREFER_PRINTF",
5779 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5780 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005781 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005782
5783 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005784 }
5785
Joe Perches6061d942012-03-23 15:02:16 -07005786# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005787 if ($realfile !~ m@\binclude/uapi/@ &&
5788 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005789 if (WARN("PREFER_SCANF",
5790 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5791 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005792 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005793 }
Joe Perches6061d942012-03-23 15:02:16 -07005794 }
5795
Joe Perches619a9082014-12-10 15:51:35 -08005796# Check for __attribute__ weak, or __weak declarations (may have link issues)
5797 if ($^V && $^V ge 5.10.0 &&
5798 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5799 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5800 $line =~ /\b__weak\b/)) {
5801 ERROR("WEAK_DECLARATION",
5802 "Using weak declarations can have unintended link defects\n" . $herecurr);
5803 }
5804
Joe Perchese6176fa2015-06-25 15:02:49 -07005805# check for c99 types like uint8_t used outside of uapi/
5806 if ($realfile !~ m@\binclude/uapi/@ &&
5807 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5808 my $type = $1;
5809 if ($type =~ /\b($typeC99Typedefs)\b/) {
5810 $type = $1;
5811 my $kernel_type = 'u';
5812 $kernel_type = 's' if ($type =~ /^_*[si]/);
5813 $type =~ /(\d+)/;
5814 $kernel_type .= $1;
5815 if (CHK("PREFER_KERNEL_TYPES",
5816 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5817 $fix) {
5818 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5819 }
5820 }
5821 }
5822
Joe Perches938224b2016-01-20 14:59:15 -08005823# check for cast of C90 native int or longer types constants
5824 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5825 my $cast = $1;
5826 my $const = $2;
5827 if (WARN("TYPECAST_INT_CONSTANT",
5828 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5829 $fix) {
5830 my $suffix = "";
5831 my $newconst = $const;
5832 $newconst =~ s/${Int_type}$//;
5833 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5834 if ($cast =~ /\blong\s+long\b/) {
5835 $suffix .= 'LL';
5836 } elsif ($cast =~ /\blong\b/) {
5837 $suffix .= 'L';
5838 }
5839 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5840 }
5841 }
5842
Joe Perches8f53a9b2010-03-05 13:43:48 -08005843# check for sizeof(&)
5844 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005845 WARN("SIZEOF_ADDRESS",
5846 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005847 }
5848
Joe Perches66c80b62012-07-30 14:41:22 -07005849# check for sizeof without parenthesis
5850 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005851 if (WARN("SIZEOF_PARENTHESIS",
5852 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5853 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005854 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005855 }
Joe Perches66c80b62012-07-30 14:41:22 -07005856 }
5857
Joe Perches88982fe2012-12-17 16:02:00 -08005858# check for struct spinlock declarations
5859 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5860 WARN("USE_SPINLOCK_T",
5861 "struct spinlock should be spinlock_t\n" . $herecurr);
5862 }
5863
Joe Perchesa6962d72013-04-29 16:18:13 -07005864# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005865 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005866 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005867 $fmt =~ s/%%//g;
5868 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005869 if (WARN("PREFER_SEQ_PUTS",
5870 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5871 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005872 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005873 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005874 }
5875 }
5876
Tobin C. Harding5c145bd2017-11-23 10:59:45 +11005877 # check for vsprintf extension %p<foo> misuses
5878 if ($^V && $^V ge 5.10.0 &&
5879 defined $stat &&
5880 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5881 $1 !~ /^_*volatile_*$/) {
5882 my $bad_extension = "";
5883 my $lc = $stat =~ tr@\n@@;
5884 $lc = $lc + $linenr;
5885 for (my $count = $linenr; $count <= $lc; $count++) {
5886 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5887 $fmt =~ s/%%//g;
5888 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNOx]).)/) {
5889 $bad_extension = $1;
5890 last;
5891 }
5892 }
5893 if ($bad_extension ne "") {
5894 my $stat_real = raw_line($linenr, 0);
5895 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5896 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5897 }
5898 WARN("VSPRINTF_POINTER_EXTENSION",
5899 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5900 }
5901 }
5902
Andy Whitcroft554e1652012-01-10 15:09:57 -08005903# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005904 if ($^V && $^V ge 5.10.0 &&
5905 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005906 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005907
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005908 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005909 my $ms_val = $7;
5910 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005911
Andy Whitcroft554e1652012-01-10 15:09:57 -08005912 if ($ms_size =~ /^(0x|)0$/i) {
5913 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005914 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005915 } elsif ($ms_size =~ /^(0x|)1$/i) {
5916 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005917 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5918 }
5919 }
5920
Joe Perches98a9bba2014-01-23 15:54:52 -08005921# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005922# if ($^V && $^V ge 5.10.0 &&
5923# defined $stat &&
5924# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5925# if (WARN("PREFER_ETHER_ADDR_COPY",
5926# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5927# $fix) {
5928# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5929# }
5930# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005931
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005932# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005933# if ($^V && $^V ge 5.10.0 &&
5934# defined $stat &&
5935# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5936# WARN("PREFER_ETHER_ADDR_EQUAL",
5937# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5938# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005939
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005940# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5941# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005942# if ($^V && $^V ge 5.10.0 &&
5943# defined $stat &&
5944# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5945#
5946# my $ms_val = $7;
5947#
5948# if ($ms_val =~ /^(?:0x|)0+$/i) {
5949# if (WARN("PREFER_ETH_ZERO_ADDR",
5950# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5951# $fix) {
5952# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5953# }
5954# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5955# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5956# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5957# $fix) {
5958# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5959# }
5960# }
5961# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005962
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005963# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005964 if ($^V && $^V ge 5.10.0 &&
5965 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005966 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005967 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005968 my $call = $1;
5969 my $cast1 = deparenthesize($2);
5970 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005971 my $cast2 = deparenthesize($7);
5972 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005973 my $cast;
5974
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005975 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005976 $cast = "$cast1 or $cast2";
5977 } elsif ($cast1 ne "") {
5978 $cast = $cast1;
5979 } else {
5980 $cast = $cast2;
5981 }
5982 WARN("MINMAX",
5983 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005984 }
5985 }
5986
Joe Perches4a273192012-07-30 14:41:20 -07005987# check usleep_range arguments
5988 if ($^V && $^V ge 5.10.0 &&
5989 defined $stat &&
5990 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5991 my $min = $1;
5992 my $max = $7;
5993 if ($min eq $max) {
5994 WARN("USLEEP_RANGE",
5995 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5996 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5997 $min > $max) {
5998 WARN("USLEEP_RANGE",
5999 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
6000 }
6001 }
6002
Joe Perches823b7942013-11-12 15:10:15 -08006003# check for naked sscanf
6004 if ($^V && $^V ge 5.10.0 &&
6005 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07006006 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08006007 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6008 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6009 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6010 my $lc = $stat =~ tr@\n@@;
6011 $lc = $lc + $linenr;
6012 my $stat_real = raw_line($linenr, 0);
6013 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6014 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6015 }
6016 WARN("NAKED_SSCANF",
6017 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6018 }
6019
Joe Perchesafc819a2014-06-04 16:12:08 -07006020# check for simple sscanf that should be kstrto<foo>
6021 if ($^V && $^V ge 5.10.0 &&
6022 defined $stat &&
6023 $line =~ /\bsscanf\b/) {
6024 my $lc = $stat =~ tr@\n@@;
6025 $lc = $lc + $linenr;
6026 my $stat_real = raw_line($linenr, 0);
6027 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6028 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6029 }
6030 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6031 my $format = $6;
6032 my $count = $format =~ tr@%@%@;
6033 if ($count == 1 &&
6034 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6035 WARN("SSCANF_TO_KSTRTO",
6036 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6037 }
6038 }
6039 }
6040
Joe Perches70dc8a42013-09-11 14:23:58 -07006041# check for new externs in .h files.
6042 if ($realfile =~ /\.h$/ &&
6043 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006044 if (CHK("AVOID_EXTERNS",
6045 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006046 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006047 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006048 }
6049 }
6050
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006051# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006052 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006053 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006054 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006055 my $function_name = $1;
6056 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006057
6058 my $s = $stat;
6059 if (defined $cond) {
6060 substr($s, 0, length($cond), '');
6061 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006062 if ($s =~ /^\s*;/ &&
6063 $function_name ne 'uninitialized_var')
6064 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006065 WARN("AVOID_EXTERNS",
6066 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006067 }
6068
6069 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006070 WARN("FUNCTION_ARGUMENTS",
6071 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006072 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006073
6074 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6075 $stat =~ /^.\s*extern\s+/)
6076 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006077 WARN("AVOID_EXTERNS",
6078 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006079 }
6080
Joe Perchesca0d8922016-10-11 13:52:16 -07006081 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6082 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6083 $1 ne "void") {
6084 my $args = trim($1);
6085 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6086 my $arg = trim($1);
6087 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6088 WARN("FUNCTION_ARGUMENTS",
6089 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6090 }
6091 }
6092 }
6093
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006094# checks for new __setup's
6095 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6096 my $name = $1;
6097
6098 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006099 CHK("UNDOCUMENTED_SETUP",
6100 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006101 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006102 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006103
6104# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006105 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006106 WARN("UNNECESSARY_CASTS",
6107 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006108 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006109
Joe Perchesa640d252013-07-03 15:05:21 -07006110# alloc style
6111# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6112 if ($^V && $^V ge 5.10.0 &&
6113 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6114 CHK("ALLOC_SIZEOF_STRUCT",
6115 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6116 }
6117
Joe Perches60a55362014-06-04 16:12:07 -07006118# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6119 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006120 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07006121 my $oldfunc = $3;
6122 my $a1 = $4;
6123 my $a2 = $10;
6124 my $newfunc = "kmalloc_array";
6125 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006126 my $r1 = $a1;
6127 my $r2 = $a2;
6128 if ($a1 =~ /^sizeof\s*\S/) {
6129 $r1 = $a2;
6130 $r2 = $a1;
6131 }
6132 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6133 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006134 if (WARN("ALLOC_WITH_MULTIPLY",
6135 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6136 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006137 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07006138
6139 }
6140 }
6141 }
6142
Joe Perches972fdea2013-04-29 16:18:12 -07006143# check for krealloc arg reuse
6144 if ($^V && $^V ge 5.10.0 &&
6145 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6146 WARN("KREALLOC_ARG_REUSE",
6147 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6148 }
6149
Joe Perches5ce59ae2013-02-21 16:44:18 -08006150# check for alloc argument mismatch
6151 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6152 WARN("ALLOC_ARRAY_ARGS",
6153 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6154 }
6155
Joe Perchescaf2a542011-01-12 16:59:56 -08006156# check for multiple semicolons
6157 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006158 if (WARN("ONE_SEMICOLON",
6159 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6160 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006161 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006162 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006163 }
6164
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006165# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6166 if ($realfile !~ m@^include/uapi/@ &&
6167 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006168 my $ull = "";
6169 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6170 if (CHK("BIT_MACRO",
6171 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6172 $fix) {
6173 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6174 }
6175 }
6176
Joe Perches2d632742016-05-20 17:04:00 -07006177# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6178 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6179 my $config = $1;
6180 if (WARN("PREFER_IS_ENABLED",
6181 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6182 $fix) {
6183 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6184 }
6185 }
6186
Joe Perchese81f2392014-08-06 16:11:25 -07006187# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006188 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6189 my $has_break = 0;
6190 my $has_statement = 0;
6191 my $count = 0;
6192 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006193 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006194 $prevline--;
6195 my $rline = $rawlines[$prevline - 1];
6196 my $fline = $lines[$prevline - 1];
6197 last if ($fline =~ /^\@\@/);
6198 next if ($fline =~ /^\-/);
6199 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6200 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6201 next if ($fline =~ /^.[\s$;]*$/);
6202 $has_statement = 1;
6203 $count++;
6204 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6205 }
6206 if (!$has_break && $has_statement) {
6207 WARN("MISSING_BREAK",
6208 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6209 }
6210 }
6211
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006212# check for switch/default statements without a break;
6213 if ($^V && $^V ge 5.10.0 &&
6214 defined $stat &&
6215 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6216 my $ctx = '';
6217 my $herectx = $here . "\n";
6218 my $cnt = statement_rawlines($stat);
6219 for (my $n = 0; $n < $cnt; $n++) {
6220 $herectx .= raw_line($linenr, $n) . "\n";
6221 }
6222 WARN("DEFAULT_NO_BREAK",
6223 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006224 }
6225
Patrick Pannutof370e252010-07-23 13:34:18 -07006226# check for return codes on error paths
6227 if ($line =~ /\breturn\s+-\d+/) {
6228 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006229 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006230 }
6231
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006232# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006233 if ($line =~ /\b__FUNCTION__\b/) {
6234 if (WARN("USE_FUNC",
6235 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6236 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006237 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006238 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006239 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006240
Joe Perches62ec8182015-02-13 14:38:18 -08006241# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6242 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6243 ERROR("DATE_TIME",
6244 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6245 }
6246
Joe Perches2c924882012-03-23 15:02:20 -07006247# check for use of yield()
6248 if ($line =~ /\byield\s*\(\s*\)/) {
6249 WARN("YIELD",
6250 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6251 }
6252
Joe Perches179f8f42013-07-03 15:05:30 -07006253# check for comparisons against true and false
6254 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6255 my $lead = $1;
6256 my $arg = $2;
6257 my $test = $3;
6258 my $otype = $4;
6259 my $trail = $5;
6260 my $op = "!";
6261
6262 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6263
6264 my $type = lc($otype);
6265 if ($type =~ /^(?:true|false)$/) {
6266 if (("$test" eq "==" && "$type" eq "true") ||
6267 ("$test" eq "!=" && "$type" eq "false")) {
6268 $op = "";
6269 }
6270
6271 CHK("BOOL_COMPARISON",
6272 "Using comparison to $otype is error prone\n" . $herecurr);
6273
6274## maybe suggesting a correct construct would better
6275## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6276
6277 }
6278 }
6279
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006280# check for semaphores initialized locked
6281 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006282 WARN("CONSIDER_COMPLETION",
6283 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006284 }
Joe Perches6712d852012-03-23 15:02:20 -07006285
Joe Perches67d0a072011-10-31 17:13:10 -07006286# recommend kstrto* over simple_strto* and strict_strto*
6287 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006288 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006289 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006290 }
Joe Perches6712d852012-03-23 15:02:20 -07006291
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006292# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006293 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006294 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006295 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07006296 }
Joe Perches6712d852012-03-23 15:02:20 -07006297
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006298# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006299 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006300 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006301 WARN("CONST_STRUCT",
6302 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006303 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006304 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006305
6306# use of NR_CPUS is usually wrong
6307# ignore definitions of NR_CPUS and usage to define arrays as likely right
6308 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006309 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6310 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006311 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6312 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6313 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006314 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006315 WARN("NR_CPUS",
6316 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006317 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006318
Joe Perches52ea8502013-11-12 15:10:09 -08006319# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6320 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6321 ERROR("DEFINE_ARCH_HAS",
6322 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6323 }
6324
Joe Perchesacd93622015-02-13 14:38:38 -08006325# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6326 if ($^V && $^V ge 5.10.0 &&
6327 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6328 WARN("LIKELY_MISUSE",
6329 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6330 }
6331
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006332# whine mightly about in_atomic
6333 if ($line =~ /\bin_atomic\s*\(/) {
6334 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006335 ERROR("IN_ATOMIC",
6336 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006337 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006338 WARN("IN_ATOMIC",
6339 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006340 }
6341 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006342
Joe Perches481aea52016-05-20 17:04:08 -07006343# whine about ACCESS_ONCE
6344 if ($^V && $^V ge 5.10.0 &&
6345 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6346 my $par = $1;
6347 my $eq = $2;
6348 my $fun = $3;
6349 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6350 if (defined($eq)) {
6351 if (WARN("PREFER_WRITE_ONCE",
6352 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6353 $fix) {
6354 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6355 }
6356 } else {
6357 if (WARN("PREFER_READ_ONCE",
6358 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6359 $fix) {
6360 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6361 }
6362 }
6363 }
6364
Peter Zijlstra1704f472010-03-19 01:37:42 +01006365# check for lockdep_set_novalidate_class
6366 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6367 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6368 if ($realfile !~ m@^kernel/lockdep@ &&
6369 $realfile !~ m@^include/linux/lockdep@ &&
6370 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006371 ERROR("LOCKDEP",
6372 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006373 }
6374 }
Dave Jones88f88312011-01-12 16:59:59 -08006375
Joe Perchesb392c642015-04-16 12:44:16 -07006376 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6377 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006378 WARN("EXPORTED_WORLD_WRITABLE",
6379 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006380 }
Joe Perches24358802014-04-03 14:49:13 -07006381
Joe Perches515a2352014-04-03 14:49:24 -07006382# Mode permission misuses where it seems decimal should be octal
6383# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6384 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006385 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006386 $line =~ /$mode_perms_search/) {
6387 foreach my $entry (@mode_permission_funcs) {
6388 my $func = $entry->[0];
6389 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006390
Joe Perches459cf0a2016-10-11 13:52:19 -07006391 my $lc = $stat =~ tr@\n@@;
6392 $lc = $lc + $linenr;
6393 my $stat_real = raw_line($linenr, 0);
6394 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6395 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6396 }
6397
Joe Perches515a2352014-04-03 14:49:24 -07006398 my $skip_args = "";
6399 if ($arg_pos > 1) {
6400 $arg_pos--;
6401 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6402 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006403 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006404 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006405 my $val = $1;
6406 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006407 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6408 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006409 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006410 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006411 }
6412 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006413 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006414 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006415 }
Joe Perches24358802014-04-03 14:49:13 -07006416 }
6417 }
6418 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006419
Joe Perches459cf0a2016-10-11 13:52:19 -07006420# check for uses of S_<PERMS> that could be octal for readability
6421 if ($line =~ /\b$mode_perms_string_search\b/) {
6422 my $val = "";
6423 my $oval = "";
6424 my $to = 0;
6425 my $curpos = 0;
6426 my $lastpos = 0;
6427 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6428 $curpos = pos($line);
6429 my $match = $2;
6430 my $omatch = $1;
6431 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6432 $lastpos = $curpos;
6433 $to |= $mode_permission_string_types{$match};
6434 $val .= '\s*\|\s*' if ($val ne "");
6435 $val .= $match;
6436 $oval .= $omatch;
6437 }
6438 $oval =~ s/^\s*\|\s*//;
6439 $oval =~ s/\s*\|\s*$//;
6440 my $octal = sprintf("%04o", $to);
6441 if (WARN("SYMBOLIC_PERMS",
6442 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6443 $fix) {
6444 $fixed[$fixlinenr] =~ s/$val/$octal/;
6445 }
6446 }
6447
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006448# validate content of MODULE_LICENSE against list from include/linux/module.h
6449 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6450 my $extracted_string = get_quoted_string($line, $rawline);
6451 my $valid_licenses = qr{
6452 GPL|
6453 GPL\ v2|
6454 GPL\ and\ additional\ rights|
6455 Dual\ BSD/GPL|
6456 Dual\ MIT/GPL|
6457 Dual\ MPL/GPL|
6458 Proprietary
6459 }x;
6460 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6461 WARN("MODULE_LICENSE",
6462 "unknown module license " . $extracted_string . "\n" . $herecurr);
6463 }
6464 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006465 }
6466
Maya Erezec77bd62017-02-26 09:14:43 +02006467 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6468 WARN("BAD_SIGN_OFF",
6469 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6470 }
6471
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006472 # If we have no input at all, then there is nothing to report on
6473 # so just keep quiet.
6474 if ($#rawlines == -1) {
6475 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006476 }
6477
Andy Whitcroft8905a672007-11-28 16:21:06 -08006478 # In mailback mode only produce a report in the negative, for
6479 # things that appear to be patches.
6480 if ($mailback && ($clean == 1 || !$is_patch)) {
6481 exit(0);
6482 }
6483
6484 # This is not a patch, and we are are in 'no-patch' mode so
6485 # just keep quiet.
6486 if (!$chk_patch && !$is_patch) {
6487 exit(0);
6488 }
6489
Joe Perches06330fc2015-06-25 15:03:11 -07006490 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006491 ERROR("NOT_UNIFIED_DIFF",
6492 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006493 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006494 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006495 ERROR("MISSING_SIGN_OFF",
6496 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006497 }
6498
Andy Whitcroft8905a672007-11-28 16:21:06 -08006499 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006500 if ($summary && !($clean == 1 && $quiet == 1)) {
6501 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006502 print "total: $cnt_error errors, $cnt_warn warnings, " .
6503 (($check)? "$cnt_chk checks, " : "") .
6504 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006505 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006506
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006507 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006508 # If there were any defects found and not already fixing them
6509 if (!$clean and !$fix) {
6510 print << "EOM"
6511
6512NOTE: For some of the reported defects, checkpatch may be able to
6513 mechanically convert to the typical style using --fix or --fix-inplace.
6514EOM
6515 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006516 # If there were whitespace errors which cleanpatch can fix
6517 # then suggest that.
6518 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006519 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006520 print << "EOM"
6521
6522NOTE: Whitespace errors detected.
6523 You may wish to use scripts/cleanpatch or scripts/cleanfile
6524EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006525 }
6526 }
6527
Joe Perchesd752fcc2014-08-06 16:11:05 -07006528 if ($clean == 0 && $fix &&
6529 ("@rawlines" ne "@fixed" ||
6530 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006531 my $newfile = $filename;
6532 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006533 my $linecount = 0;
6534 my $f;
6535
Joe Perchesd752fcc2014-08-06 16:11:05 -07006536 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6537
Joe Perches3705ce52013-07-03 15:05:31 -07006538 open($f, '>', $newfile)
6539 or die "$P: Can't open $newfile for write\n";
6540 foreach my $fixed_line (@fixed) {
6541 $linecount++;
6542 if ($file) {
6543 if ($linecount > 3) {
6544 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006545 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006546 }
6547 } else {
6548 print $f $fixed_line . "\n";
6549 }
6550 }
6551 close($f);
6552
6553 if (!$quiet) {
6554 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006555
Joe Perches3705ce52013-07-03 15:05:31 -07006556Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6557
6558Do _NOT_ trust the results written to this file.
6559Do _NOT_ submit these changes without inspecting them for correctness.
6560
6561This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6562No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006563EOM
6564 }
6565 }
6566
Joe Perchesd8469f12015-06-25 15:03:00 -07006567 if ($quiet == 0) {
6568 print "\n";
6569 if ($clean == 1) {
6570 print "$vname has no obvious style problems and is ready for submission.\n";
6571 } else {
6572 print "$vname has style problems, please review.\n";
6573 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006574 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006575 return $clean;
6576}