blob: f93db0ea4a8dfd6745868762c26221b085bf7a73 [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;
2441 } elsif (length($line) > (SHORTTEXT_LIMIT +
2442 $shorttext_exspc)
2443 && $line !~ /^:([0-7]{6}\s){2}
2444 ([[:xdigit:]]+\.*
2445 \s){2}\w+\s\w+/xms) {
2446 WARN("LONG_COMMIT_TEXT",
2447 "commit text line over " .
2448 SHORTTEXT_LIMIT .
2449 " characters\n" . $herecurr);
Steve Muckled360fe42012-03-09 11:15:24 -08002450 } elsif ($line=~/^\s*change-id:/i ||
2451 $line=~/^\s*signed-off-by:/i ||
2452 $line=~/^\s*crs-fixed:/i ||
2453 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002454 # this is a tag, there must be commit
2455 # text by now
2456 if ($commit_text_present == 0) {
2457 WARN("NO_COMMIT_TEXT",
2458 "please add commit text explaining " .
2459 "*why* the change is needed\n" .
2460 $herecurr);
2461 # prevent duplicate warnings
2462 $commit_text_present = 1;
2463 }
2464 } elsif ($line=~/\S/) {
2465 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002466 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002467 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2468 # case of non-blank line in this state handled above
2469 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002470 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002471# The Subject line doesn't have to be the last header in the patch.
2472# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2473# Per RFC5322, continuation lines must be folded, so any left-justified
2474# text which looks like a header is definitely a header.
2475 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2476 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002477 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002478 if (length($line) != 0) {
2479 WARN("NONBLANK_AFTER_SUMMARY",
2480 "non-blank line after " .
2481 "summary line\n" .
2482 $sublinenr . $here .
2483 "\n" . $subjectline .
2484 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002485 # this non-blank line may or may not
2486 # be commit text - a warning has been
2487 # generated so assume it is commit
2488 # text and move on
2489 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002490 }
David Keitele288d232016-03-23 20:57:21 -07002491 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002492 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002493 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002494 # This is the subject line. Go to
2495 # CHECK_NEXT_SHORTTEXT to wait for the commit
2496 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002497 $shorttext = CHECK_NEXT_SHORTTEXT;
2498 $subjectline = $line;
2499 $sublinenr = "#$linenr & ";
2500# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002501 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002502 WARN("LONG_SUMMARY_LINE",
2503 "summary line over " .
2504 SHORTTEXT_LIMIT .
2505 " characters\n" . $herecurr);
2506 }
2507 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002508 # Indented format, this must be the summary
2509 # line (i.e. git show). There will be no more
2510 # headers so we are now in the shorttext.
2511 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002512 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002513 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002514 WARN("LONG_SUMMARY_LINE",
2515 "summary line over " .
2516 SHORTTEXT_LIMIT .
2517 " characters\n" . $herecurr);
2518 }
2519 }
2520 }
2521
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002522 $cnt_lines++ if ($realcnt != 0);
2523
Joe Perchese518e9a2015-06-25 15:03:27 -07002524# Check if the commit log has what seems like a diff which can confuse patch
2525 if ($in_commit_log && !$commit_log_has_diff &&
2526 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2527 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2528 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2529 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2530 ERROR("DIFF_IN_COMMIT_MSG",
2531 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2532 $commit_log_has_diff = 1;
2533 }
2534
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002535# Check for incorrect file permissions
2536 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2537 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002538 if ($realfile !~ m@scripts/@ &&
2539 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002540 ERROR("EXECUTE_PERMISSIONS",
2541 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002542 }
2543 }
2544
Joe Perches20112472011-07-25 17:13:23 -07002545# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002546 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002547 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002548 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002549 }
2550
Joe Perchese0d975b2014-12-10 15:51:49 -08002551# Check if MAINTAINERS is being updated. If so, there's probably no need to
2552# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2553 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2554 $reported_maintainer_file = 1;
2555 }
2556
Joe Perches20112472011-07-25 17:13:23 -07002557# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002558 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002559 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002560 my $space_before = $1;
2561 my $sign_off = $2;
2562 my $space_after = $3;
2563 my $email = $4;
2564 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2565
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002566 if ($sign_off !~ /$signature_tags/) {
2567 WARN("BAD_SIGN_OFF",
2568 "Non-standard signature: $sign_off\n" . $herecurr);
2569 }
Joe Perches20112472011-07-25 17:13:23 -07002570 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002571 if (WARN("BAD_SIGN_OFF",
2572 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2573 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002574 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002575 "$ucfirst_sign_off $email";
2576 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002577 }
Joe Perches20112472011-07-25 17:13:23 -07002578 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if (WARN("BAD_SIGN_OFF",
2580 "'$ucfirst_sign_off' is the preferred signature form\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 }
2585
Joe Perches20112472011-07-25 17:13:23 -07002586 }
2587 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002588 if (WARN("BAD_SIGN_OFF",
2589 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2590 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002591 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002592 "$ucfirst_sign_off $email";
2593 }
Joe Perches20112472011-07-25 17:13:23 -07002594 }
2595
2596 my ($email_name, $email_address, $comment) = parse_email($email);
2597 my $suggested_email = format_email(($email_name, $email_address));
2598 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002599 ERROR("BAD_SIGN_OFF",
2600 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002601 } else {
2602 my $dequoted = $suggested_email;
2603 $dequoted =~ s/^"//;
2604 $dequoted =~ s/" </ </;
2605 # Don't force email to have quotes
2606 # Allow just an angle bracketed address
2607 if ("$dequoted$comment" ne $email &&
2608 "<$email_address>$comment" ne $email &&
2609 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002610 WARN("BAD_SIGN_OFF",
2611 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002612 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002613 }
Maya Erezec77bd62017-02-26 09:14:43 +02002614 if ($chk_author) {
2615 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2616 $qca_sign_off = 1;
2617 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2618 $codeaurora_sign_off = 1;
2619 } elsif ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
2620 WARN("BAD_SIGN_OFF",
2621 "invalid Signed-off-by identity\n" . $line );
2622 }
2623 }
Joe Perches7e51f192013-09-11 14:23:57 -07002624
2625# Check for duplicate signatures
2626 my $sig_nospace = $line;
2627 $sig_nospace =~ s/\s//g;
2628 $sig_nospace = lc($sig_nospace);
2629 if (defined $signatures{$sig_nospace}) {
2630 WARN("BAD_SIGN_OFF",
2631 "Duplicate signature\n" . $herecurr);
2632 } else {
2633 $signatures{$sig_nospace} = 1;
2634 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002635 }
2636
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002637# Check email subject for common tools that don't need to be mentioned
2638 if ($in_header_lines &&
2639 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2640 WARN("EMAIL_SUBJECT",
2641 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2642 }
2643
Joe Perches9b3189e2014-06-04 16:12:10 -07002644# Check for old stable address
2645 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2646 ERROR("STABLE_ADDRESS",
2647 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2648 }
2649
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002650# Check for unwanted Gerrit info
2651 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2652 ERROR("GERRIT_CHANGE_ID",
2653 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2654 }
2655
Joe Perches369c8dd2015-11-06 16:31:34 -08002656# Check if the commit log is in a possible stack dump
2657 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2658 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2659 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2660 # timestamp
2661 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2662 # stack dump address
2663 $commit_log_possible_stack_dump = 1;
2664 }
2665
Joe Perches2a076f42015-04-16 12:44:28 -07002666# Check for line lengths > 75 in commit log, warn once
2667 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002668 length($line) > 75 &&
2669 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2670 # file delta changes
2671 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2672 # filename then :
2673 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2674 # A Fixes: or Link: line
2675 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002676 WARN("COMMIT_LOG_LONG_LINE",
2677 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2678 $commit_log_long_line = 1;
2679 }
2680
Joe Perchesbf4daf12015-09-09 15:37:50 -07002681# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002682 if ($in_commit_log && $commit_log_possible_stack_dump &&
2683 $line =~ /^\s*$/) {
2684 $commit_log_possible_stack_dump = 0;
2685 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002686
Joe Perches0d7835f2015-02-13 14:38:35 -08002687# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002688 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002689 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wang5b101522017-04-07 15:22:19 -07002690 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002691 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002692 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002693 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2694 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002695 my $init_char = "c";
2696 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002697 my $short = 1;
2698 my $long = 0;
2699 my $case = 1;
2700 my $space = 1;
2701 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002702 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002703 my $id = '0123456789ab';
2704 my $orig_desc = "commit description";
2705 my $description = "";
2706
Joe Perchesfe043ea2015-09-09 15:37:25 -07002707 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2708 $init_char = $1;
2709 $orig_commit = lc($2);
2710 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2711 $orig_commit = lc($1);
2712 }
2713
Joe Perches0d7835f2015-02-13 14:38:35 -08002714 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2715 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2716 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2717 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2718 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2719 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002720 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002721 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2722 defined $rawlines[$linenr] &&
2723 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2724 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002725 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002726 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2727 defined $rawlines[$linenr] &&
2728 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2729 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2730 $orig_desc = $1;
2731 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2732 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002733 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002734 }
2735
2736 ($id, $description) = git_commit_info($orig_commit,
2737 $id, $orig_desc);
2738
Joe Perches19c146a2015-02-13 14:39:00 -08002739 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002740 ERROR("GIT_COMMIT_ID",
2741 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2742 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002743 }
2744
Joe Perches13f19372014-08-06 16:10:59 -07002745# Check for added, moved or deleted files
2746 if (!$reported_maintainer_file && !$in_commit_log &&
2747 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2748 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2749 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2750 (defined($1) || defined($2))))) {
2751 $reported_maintainer_file = 1;
2752 WARN("FILE_PATH_CHANGES",
2753 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2754 }
2755
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002756#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002757 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
2758 $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002759 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2760 }
2761
Andy Whitcroft00df3442007-06-08 13:47:06 -07002762# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002763 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002764 ERROR("CORRUPTED_PATCH",
2765 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002766 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002767 }
2768
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002769# Check for absolute kernel paths.
2770 if ($tree) {
2771 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2772 my $file = $1;
2773
2774 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2775 check_absolute_file($1, $herecurr)) {
2776 #
2777 } else {
2778 check_absolute_file($file, $herecurr);
2779 }
2780 }
2781 }
2782
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002783# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2784 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002785 $rawline !~ m/^$UTF8*$/) {
2786 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2787
2788 my $blank = copy_spacing($rawline);
2789 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2790 my $hereptr = "$hereline$ptr\n";
2791
Joe Perches34d99212011-07-25 17:13:26 -07002792 CHK("INVALID_UTF8",
2793 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002794 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002795
Joe Perches15662b32011-10-31 17:13:12 -07002796# Check if it's the start of a commit log
2797# (not a header line and we haven't seen the patch filename)
2798 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002799 !($rawline =~ /^\s+\S/ ||
2800 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002801 $in_header_lines = 0;
2802 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002803 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002804 }
2805
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002806# Check if there is UTF-8 in a commit log when a mail header has explicitly
2807# declined it, i.e defined some charset where it is missing.
2808 if ($in_header_lines &&
2809 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2810 $1 !~ /utf-8/i) {
2811 $non_utf8_charset = 1;
2812 }
2813
2814 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002815 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002816 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002817 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2818 }
2819
Kees Cook66b47b42014-10-13 15:51:57 -07002820# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002821 if (defined($misspellings) &&
2822 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002823 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002824 my $typo = $1;
2825 my $typo_fix = $spelling_fix{lc($typo)};
2826 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2827 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2828 my $msg_type = \&WARN;
2829 $msg_type = \&CHK if ($file);
2830 if (&{$msg_type}("TYPO_SPELLING",
2831 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2832 $fix) {
2833 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2834 }
2835 }
2836 }
2837
Andy Whitcroft306708542008-10-15 22:02:28 -07002838# ignore non-hunk lines and lines being removed
2839 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002840
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002841#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002842 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002843 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002844 if (ERROR("DOS_LINE_ENDINGS",
2845 "DOS line endings\n" . $herevet) &&
2846 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002847 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002848 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002849 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2850 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002851 if (ERROR("TRAILING_WHITESPACE",
2852 "trailing whitespace\n" . $herevet) &&
2853 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002854 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002855 }
2856
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002857 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002858 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002859
Josh Triplett4783f892013-11-12 15:10:12 -08002860# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002861 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002862 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2863 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002864 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2865 my $msg_type = \&ERROR;
2866 $msg_type = \&CHK if ($file);
2867 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002868 "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 -08002869 }
2870
Andi Kleen33549572010-05-24 14:33:29 -07002871# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002872# Only applies when adding the entry originally, after that we do not have
2873# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002874 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002875 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002876 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002877 my $cnt = $realcnt;
2878 my $ln = $linenr + 1;
2879 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002880 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002881 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002882 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002883 $f = $lines[$ln - 1];
2884 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2885 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002886
2887 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002888 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002889
Joe Perches8d73e0e2014-08-06 16:10:46 -07002890 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002891 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002892 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002893 $length = -1;
2894 }
2895
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002896 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002897 $f =~ s/#.*//;
2898 $f =~ s/^\s+//;
2899 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002900 if ($f =~ /^\s*config\s/) {
2901 $is_end = 1;
2902 last;
2903 }
Andi Kleen33549572010-05-24 14:33:29 -07002904 $length++;
2905 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002906 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2907 WARN("CONFIG_DESCRIPTION",
2908 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2909 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002910 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002911 }
2912
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002913# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2914 if ($realfile =~ /Kconfig/ &&
2915 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2916 WARN("CONFIG_EXPERIMENTAL",
2917 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2918 }
2919
Christoph Jaeger327953e2015-02-13 14:38:29 -08002920# discourage the use of boolean for type definition attributes of Kconfig options
2921 if ($realfile =~ /Kconfig/ &&
2922 $line =~ /^\+\s*\bboolean\b/) {
2923 WARN("CONFIG_TYPE_BOOLEAN",
2924 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2925 }
2926
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002927 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2928 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2929 my $flag = $1;
2930 my $replacement = {
2931 'EXTRA_AFLAGS' => 'asflags-y',
2932 'EXTRA_CFLAGS' => 'ccflags-y',
2933 'EXTRA_CPPFLAGS' => 'cppflags-y',
2934 'EXTRA_LDFLAGS' => 'ldflags-y',
2935 };
2936
2937 WARN("DEPRECATED_VARIABLE",
2938 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2939 }
2940
Rob Herringbff5da42014-01-23 15:54:51 -08002941# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002942 if (defined $root &&
2943 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2944 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2945
Rob Herringbff5da42014-01-23 15:54:51 -08002946 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2947
Florian Vaussardcc933192014-04-03 14:49:27 -07002948 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2949 my $vp_file = $dt_path . "vendor-prefixes.txt";
2950
Rob Herringbff5da42014-01-23 15:54:51 -08002951 foreach my $compat (@compats) {
2952 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002953 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2954 my $compat3 = $compat;
2955 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2956 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002957 if ( $? >> 8 ) {
2958 WARN("UNDOCUMENTED_DT_STRING",
2959 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2960 }
2961
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002962 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2963 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002964 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002965 if ( $? >> 8 ) {
2966 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002967 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002968 }
2969 }
2970 }
2971
Andy Whitcroft5368df22008-10-15 22:02:27 -07002972# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002973 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002974
Joe Perches47e0c882015-06-25 15:02:57 -07002975# line length limit (with some exclusions)
2976#
2977# There are a few types of lines that may extend beyond $max_line_length:
2978# logging functions like pr_info that end in a string
2979# lines with a single string
2980# #defines that are a single string
2981#
2982# There are 3 different line length message types:
2983# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2984# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2985# LONG_LINE all other lines longer than $max_line_length
2986#
2987# if LONG_LINE is ignored, the other 2 types are also ignored
2988#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002989 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002990 my $msg_type = "LONG_LINE";
2991
2992 # Check the allowed long line types first
2993
2994 # logging functions that end in a string that starts
2995 # before $max_line_length
2996 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2997 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2998 $msg_type = "";
2999
3000 # lines with only strings (w/ possible termination)
3001 # #defines with only strings
3002 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3003 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3004 $msg_type = "";
3005
Joe Perchesd560a5f2016-08-02 14:04:31 -07003006 # EFI_GUID is another special case
3007 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
3008 $msg_type = "";
3009
Joe Perches47e0c882015-06-25 15:02:57 -07003010 # Otherwise set the alternate message types
3011
3012 # a comment starts before $max_line_length
3013 } elsif ($line =~ /($;[\s$;]*)$/ &&
3014 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3015 $msg_type = "LONG_LINE_COMMENT"
3016
3017 # a quoted string starts before $max_line_length
3018 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3019 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3020 $msg_type = "LONG_LINE_STRING"
3021 }
3022
3023 if ($msg_type ne "" &&
3024 (show_type("LONG_LINE") || show_type($msg_type))) {
3025 WARN($msg_type,
3026 "line over $max_line_length characters\n" . $herecurr);
3027 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003028 }
3029
Andy Whitcroft8905a672007-11-28 16:21:06 -08003030# check for adding lines without a newline.
3031 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003032 WARN("MISSING_EOF_NEWLINE",
3033 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003034 }
3035
Mike Frysinger42e41c52009-09-21 17:04:40 -07003036# Blackfin: use hi/lo macros
3037 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3038 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3039 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003040 ERROR("LO_MACRO",
3041 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003042 }
3043 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3044 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003045 ERROR("HI_MACRO",
3046 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003047 }
3048 }
3049
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003050# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003051 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003052
3053# at the beginning of a line any tabs must come first and anything
3054# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003055 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3056 $rawline =~ /^\+\s* \s*/) {
3057 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003058 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003059 if (ERROR("CODE_INDENT",
3060 "code indent should use tabs where possible\n" . $herevet) &&
3061 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003062 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003063 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003064 }
3065
Alberto Panizzo08e44362010-03-05 13:43:54 -08003066# check for space before tabs.
3067 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3068 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003069 if (WARN("SPACE_BEFORE_TAB",
3070 "please, no space before tabs\n" . $herevet) &&
3071 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003072 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003073 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003074 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003075 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003076 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003077 }
3078
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003079# check for && or || at the start of a line
3080 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3081 CHK("LOGICAL_CONTINUATIONS",
3082 "Logical continuations should be on the previous line\n" . $hereprev);
3083 }
3084
Joe Perchesa91e8992016-05-20 17:04:05 -07003085# check indentation starts on a tab stop
3086 if ($^V && $^V ge 5.10.0 &&
3087 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3088 my $indent = length($1);
3089 if ($indent % 8) {
3090 if (WARN("TABSTOP",
3091 "Statements should start on a tabstop\n" . $herecurr) &&
3092 $fix) {
3093 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3094 }
3095 }
3096 }
3097
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003098# check multi-line statement indentation matches previous line
3099 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003100 $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 -07003101 $prevline =~ /^\+(\t*)(.*)$/;
3102 my $oldindent = $1;
3103 my $rest = $2;
3104
3105 my $pos = pos_last_openparen($rest);
3106 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003107 $line =~ /^(\+| )([ \t]*)/;
3108 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003109
3110 my $goodtabindent = $oldindent .
3111 "\t" x ($pos / 8) .
3112 " " x ($pos % 8);
3113 my $goodspaceindent = $oldindent . " " x $pos;
3114
3115 if ($newindent ne $goodtabindent &&
3116 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003117
3118 if (CHK("PARENTHESIS_ALIGNMENT",
3119 "Alignment should match open parenthesis\n" . $hereprev) &&
3120 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003121 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003122 s/^\+[ \t]*/\+$goodtabindent/;
3123 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003124 }
3125 }
3126 }
3127
Joe Perches6ab3a972015-04-16 12:44:05 -07003128# check for space after cast like "(int) foo" or "(struct foo) bar"
3129# avoid checking a few false positives:
3130# "sizeof(<type>)" or "__alignof__(<type>)"
3131# function pointer declarations like "(*foo)(int) = bar;"
3132# structure definitions like "(struct foo) { 0 };"
3133# multiline macros that define functions
3134# known attributes or the __attribute__ keyword
3135 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3136 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003137 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003138 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003139 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003140 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003141 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003142 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003143 }
3144
Joe Perches86406b12015-09-09 15:37:41 -07003145# Block comment styles
3146# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003147 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003148 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003149 $rawline =~ /^\+[ \t]*\*/ &&
3150 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003151 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3152 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3153 }
3154
Joe Perches86406b12015-09-09 15:37:41 -07003155# Block comments use * on subsequent lines
3156 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3157 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003158 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003159 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003160 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003161 WARN("BLOCK_COMMENT_STYLE",
3162 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003163 }
3164
Joe Perches86406b12015-09-09 15:37:41 -07003165# Block comments use */ on trailing lines
3166 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003167 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3168 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3169 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003170 WARN("BLOCK_COMMENT_STYLE",
3171 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003172 }
3173
Joe Perches08eb9b82016-10-11 13:51:50 -07003174# Block comment * alignment
3175 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003176 $line =~ /^\+[ \t]*$;/ && #leading comment
3177 $rawline =~ /^\+[ \t]*\*/ && #leading *
3178 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003179 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003180 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3181 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003182 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003183 if (defined($1)) {
3184 $oldindent = expand_tabs($1);
3185 } else {
3186 $prevrawline =~ m@^\+(.*/?)\*@;
3187 $oldindent = expand_tabs($1);
3188 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003189 $rawline =~ m@^\+([ \t]*)\*@;
3190 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003191 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003192 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003193 WARN("BLOCK_COMMENT_STYLE",
3194 "Block comments should align the * on each line\n" . $hereprev);
3195 }
3196 }
3197
Joe Perches7f619192014-08-06 16:10:39 -07003198# check for missing blank lines after struct/union declarations
3199# with exceptions for various attributes and macros
3200 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3201 $line =~ /^\+/ &&
3202 !($line =~ /^\+\s*$/ ||
3203 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3204 $line =~ /^\+\s*MODULE_/i ||
3205 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3206 $line =~ /^\+[a-z_]*init/ ||
3207 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3208 $line =~ /^\+\s*DECLARE/ ||
3209 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003210 if (CHK("LINE_SPACING",
3211 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3212 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003213 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003214 }
Joe Perches7f619192014-08-06 16:10:39 -07003215 }
3216
Joe Perches365dd4e2014-08-06 16:10:42 -07003217# check for multiple consecutive blank lines
3218 if ($prevline =~ /^[\+ ]\s*$/ &&
3219 $line =~ /^\+\s*$/ &&
3220 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003221 if (CHK("LINE_SPACING",
3222 "Please don't use multiple blank lines\n" . $hereprev) &&
3223 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003224 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003225 }
3226
Joe Perches365dd4e2014-08-06 16:10:42 -07003227 $last_blank_line = $linenr;
3228 }
3229
Joe Perches3b617e32014-04-03 14:49:28 -07003230# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003231 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3232 # actual declarations
3233 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003234 # function pointer declarations
3235 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003236 # foo bar; where foo is some local typedef or #define
3237 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3238 # known declaration macros
3239 $prevline =~ /^\+\s+$declaration_macros/) &&
3240 # for "else if" which can look like "$Ident $Ident"
3241 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3242 # other possible extensions of declaration lines
3243 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3244 # not starting a section or a macro "\" extended line
3245 $prevline =~ /(?:\{\s*|\\)$/) &&
3246 # looks like a declaration
3247 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003248 # function pointer declarations
3249 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003250 # foo bar; where foo is some local typedef or #define
3251 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3252 # known declaration macros
3253 $sline =~ /^\+\s+$declaration_macros/ ||
3254 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003255 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003256 # start or end of block or continuation of declaration
3257 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3258 # bitfield continuation
3259 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3260 # other possible extensions of declaration lines
3261 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3262 # indentation of previous and current line are the same
3263 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003264 if (WARN("LINE_SPACING",
3265 "Missing a blank line after declarations\n" . $hereprev) &&
3266 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003267 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003268 }
Joe Perches3b617e32014-04-03 14:49:28 -07003269 }
3270
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003271# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003272# Exceptions:
3273# 1) within comments
3274# 2) indented preprocessor commands
3275# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003276 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003277 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003278 if (WARN("LEADING_SPACE",
3279 "please, no spaces at the start of a line\n" . $herevet) &&
3280 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003281 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003282 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003283 }
3284
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003285# check we are in a valid C source file if not then ignore this hunk
3286 next if ($realfile !~ /\.(h|c)$/);
3287
Joe Perches032a4c02014-08-06 16:10:29 -07003288# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003289# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003290# if the previous line is a break or return and is indented 1 tab more...
3291 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3292 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003293 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3294 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3295 defined $lines[$linenr] &&
3296 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003297 WARN("UNNECESSARY_ELSE",
3298 "else is not generally useful after a break or return\n" . $hereprev);
3299 }
3300 }
3301
Joe Perchesc00df192014-08-06 16:11:01 -07003302# check indentation of a line with a break;
3303# if the previous line is a goto or return and is indented the same # of tabs
3304 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3305 my $tabs = $1;
3306 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3307 WARN("UNNECESSARY_BREAK",
3308 "break is not useful after a goto or return\n" . $hereprev);
3309 }
3310 }
3311
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003312# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3313 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3314 WARN("CONFIG_EXPERIMENTAL",
3315 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3316 }
3317
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003318# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003319 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003320 WARN("CVS_KEYWORD",
3321 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003322 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003323
Mike Frysinger42e41c52009-09-21 17:04:40 -07003324# Blackfin: don't use __builtin_bfin_[cs]sync
3325 if ($line =~ /__builtin_bfin_csync/) {
3326 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003327 ERROR("CSYNC",
3328 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003329 }
3330 if ($line =~ /__builtin_bfin_ssync/) {
3331 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003332 ERROR("SSYNC",
3333 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003334 }
3335
Joe Perches56e77d72013-02-21 16:44:14 -08003336# check for old HOTPLUG __dev<foo> section markings
3337 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3338 WARN("HOTPLUG_SECTION",
3339 "Using $1 is unnecessary\n" . $herecurr);
3340 }
3341
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003342# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003343 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3344 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003345#print "LINE<$line>\n";
3346 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003347 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003348 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003349 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003350 $stat =~ s/\n./\n /g;
3351 $cond =~ s/\n./\n /g;
3352
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003353#print "linenr<$linenr> <$stat>\n";
3354 # If this statement has no statement boundaries within
3355 # it there is no point in retrying a statement scan
3356 # until we hit end of it.
3357 my $frag = $stat; $frag =~ s/;+\s*$//;
3358 if ($frag !~ /(?:{|;)/) {
3359#print "skip<$line_nr_next>\n";
3360 $suppress_statement = $line_nr_next;
3361 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003362
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003363 # Find the real next line.
3364 $realline_next = $line_nr_next;
3365 if (defined $realline_next &&
3366 (!defined $lines[$realline_next - 1] ||
3367 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3368 $realline_next++;
3369 }
3370
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003371 my $s = $stat;
3372 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003373
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003374 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003375 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003376
3377 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003378 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003379
Andy Whitcroft463f2862009-09-21 17:04:34 -07003380 } elsif ($s =~ /^.\s*else\b/s) {
3381
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003382 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003383 } 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 -07003384 my $type = $1;
3385 $type =~ s/\s+/ /g;
3386 possible($type, "A:" . $s);
3387
Andy Whitcroft8905a672007-11-28 16:21:06 -08003388 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003389 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003390 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003391 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003392
3393 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003394 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003395 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003396 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003397
3398 # Check for any sort of function declaration.
3399 # int foo(something bar, other baz);
3400 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003401 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 -08003402 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003403
Andy Whitcroftcf655042008-03-04 14:28:20 -08003404 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003405 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003406 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003407
Andy Whitcroft8905a672007-11-28 16:21:06 -08003408 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003409 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003410
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003411 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003412 }
3413 }
3414 }
3415
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003416 }
3417
Andy Whitcroft653d4872007-06-23 17:16:34 -07003418#
3419# Checks which may be anchored in the context.
3420#
3421
3422# Check for switch () and associated case and default
3423# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003424 if ($line=~/\bswitch\s*\(.*\)/) {
3425 my $err = '';
3426 my $sep = '';
3427 my @ctx = ctx_block_outer($linenr, $realcnt);
3428 shift(@ctx);
3429 for my $ctx (@ctx) {
3430 my ($clen, $cindent) = line_stats($ctx);
3431 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3432 $indent != $cindent) {
3433 $err .= "$sep$ctx\n";
3434 $sep = '';
3435 } else {
3436 $sep = "[...]\n";
3437 }
3438 }
3439 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003440 ERROR("SWITCH_CASE_INDENT_LEVEL",
3441 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003442 }
3443 }
3444
3445# if/while/etc brace do not go on next line, unless defining a do while loop,
3446# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003447 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 -07003448 my $pre_ctx = "$1$2";
3449
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003450 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003451
3452 if ($line =~ /^\+\t{6,}/) {
3453 WARN("DEEP_INDENTATION",
3454 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3455 }
3456
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003457 my $ctx_cnt = $realcnt - $#ctx - 1;
3458 my $ctx = join("\n", @ctx);
3459
Andy Whitcroft548596d2008-07-23 21:29:01 -07003460 my $ctx_ln = $linenr;
3461 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003462
Andy Whitcroft548596d2008-07-23 21:29:01 -07003463 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3464 defined $lines[$ctx_ln - 1] &&
3465 $lines[$ctx_ln - 1] =~ /^-/)) {
3466 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3467 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003468 $ctx_ln++;
3469 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003470
Andy Whitcroft53210162008-07-23 21:29:03 -07003471 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3472 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003473
Joe Perchesd752fcc2014-08-06 16:11:05 -07003474 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003475 ERROR("OPEN_BRACE",
3476 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003477 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003478 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003479 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3480 $ctx =~ /\)\s*\;\s*$/ &&
3481 defined $lines[$ctx_ln - 1])
3482 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003483 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3484 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003485 WARN("TRAILING_SEMICOLON",
3486 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003487 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003488 }
3489 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003490 }
3491
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003492# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003493 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 -08003494 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3495 ctx_statement_block($linenr, $realcnt, 0)
3496 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003497 my ($s, $c) = ($stat, $cond);
3498
3499 substr($s, 0, length($c), '');
3500
Joe Perches9f5af482015-09-09 15:37:30 -07003501 # remove inline comments
3502 $s =~ s/$;/ /g;
3503 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003504
3505 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003506 my @newlines = ($c =~ /\n/gs);
3507 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003508
Joe Perches9f5af482015-09-09 15:37:30 -07003509 # Make sure we remove the line prefixes as we have
3510 # none on the first line, and are going to readd them
3511 # where necessary.
3512 $s =~ s/\n./\n/gs;
3513 while ($s =~ /\n\s+\\\n/) {
3514 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3515 }
3516
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003517 # We want to check the first line inside the block
3518 # starting at the end of the conditional, so remove:
3519 # 1) any blank line termination
3520 # 2) any opening brace { on end of the line
3521 # 3) any do (...) {
3522 my $continuation = 0;
3523 my $check = 0;
3524 $s =~ s/^.*\bdo\b//;
3525 $s =~ s/^\s*{//;
3526 if ($s =~ s/^\s*\\//) {
3527 $continuation = 1;
3528 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003529 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003530 $check = 1;
3531 $cond_lines++;
3532 }
3533
3534 # Also ignore a loop construct at the end of a
3535 # preprocessor statement.
3536 if (($prevline =~ /^.\s*#\s*define\s/ ||
3537 $prevline =~ /\\\s*$/) && $continuation == 0) {
3538 $check = 0;
3539 }
3540
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003541 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003542 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003543 while ($cond_ptr != $cond_lines) {
3544 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003545
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003546 # If we see an #else/#elif then the code
3547 # is not linear.
3548 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3549 $check = 0;
3550 }
3551
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003552 # Ignore:
3553 # 1) blank lines, they should be at 0,
3554 # 2) preprocessor lines, and
3555 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003556 if ($continuation ||
3557 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003558 $s =~ /^\s*#\s*?/ ||
3559 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003560 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003561 if ($s =~ s/^.*?\n//) {
3562 $cond_lines++;
3563 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003564 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003565 }
3566
3567 my (undef, $sindent) = line_stats("+" . $s);
3568 my $stat_real = raw_line($linenr, $cond_lines);
3569
3570 # Check if either of these lines are modified, else
3571 # this is not this patch's fault.
3572 if (!defined($stat_real) ||
3573 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3574 $check = 0;
3575 }
3576 if (defined($stat_real) && $cond_lines > 1) {
3577 $stat_real = "[...]\n$stat_real";
3578 }
3579
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003580 #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 -07003581
Joe Perches9f5af482015-09-09 15:37:30 -07003582 if ($check && $s ne '' &&
3583 (($sindent % 8) != 0 ||
3584 ($sindent < $indent) ||
3585 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003586 WARN("SUSPECT_CODE_INDENT",
3587 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003588 }
3589 }
3590
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003591 # Track the 'values' across context and added lines.
3592 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003593 my ($curr_values, $curr_vars) =
3594 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003595 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003596 if ($dbg_values) {
3597 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003598 print "$linenr > .$outline\n";
3599 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003600 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003601 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003602 $prev_values = substr($curr_values, -1);
3603
Andy Whitcroft00df3442007-06-08 13:47:06 -07003604#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003605 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003606
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003607# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003608 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 -07003609 my $type = $1;
3610 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003611 $var = "" if (!defined $var);
3612 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003613 my $sign = $1;
3614 my $pointer = $2;
3615
3616 $pointer = "" if (!defined $pointer);
3617
3618 if (WARN("UNSPECIFIED_INT",
3619 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3620 $fix) {
3621 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003622 my $comp_pointer = $pointer;
3623 $comp_pointer =~ s/\s//g;
3624 $decl .= $comp_pointer;
3625 $decl = rtrim($decl) if ($var eq "");
3626 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003627 }
3628 }
3629 }
3630
Andy Whitcroft653d4872007-06-23 17:16:34 -07003631# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003632 if ($dbg_type) {
3633 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003634 ERROR("TEST_TYPE",
3635 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003636 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003637 ERROR("TEST_NOT_TYPE",
3638 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003639 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003640 next;
3641 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003642# TEST: allow direct testing of the attribute matcher.
3643 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003644 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003645 ERROR("TEST_ATTR",
3646 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003647 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003648 ERROR("TEST_NOT_ATTR",
3649 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003650 }
3651 next;
3652 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003653
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003654# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003655 if ($line =~ /^.\s*{/ &&
3656 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003657 if (ERROR("OPEN_BRACE",
3658 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003659 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3660 fix_delete_line($fixlinenr - 1, $prevrawline);
3661 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003662 my $fixedline = $prevrawline;
3663 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003664 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003665 $fixedline = $line;
3666 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003667 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003668 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003669 }
3670
Andy Whitcroft653d4872007-06-23 17:16:34 -07003671#
3672# Checks which are anchored on the added line.
3673#
3674
3675# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003676 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003677 my $path = $1;
3678 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003679 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003680 "malformed #include filename\n" . $herecurr);
3681 }
3682 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3683 ERROR("UAPI_INCLUDE",
3684 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003685 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003686 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003687
3688# no C99 // comments
3689 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003690 if (ERROR("C99_COMMENTS",
3691 "do not use C99 // comments\n" . $herecurr) &&
3692 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003693 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003694 if ($line =~ /\/\/(.*)$/) {
3695 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003696 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003697 }
3698 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003699 }
3700 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003701 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003702 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003703
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003704# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3705# the whole statement.
3706#print "APW <$lines[$realline_next - 1]>\n";
3707 if (defined $realline_next &&
3708 exists $lines[$realline_next - 1] &&
3709 !defined $suppress_export{$realline_next} &&
3710 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3711 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003712 # Handle definitions which produce identifiers with
3713 # a prefix:
3714 # XXX(foo);
3715 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003716 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003717 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003718 $name =~ /^${Ident}_$2/) {
3719#print "FOO C name<$name>\n";
3720 $suppress_export{$realline_next} = 1;
3721
3722 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003723 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003724 ^.DEFINE_$Ident\(\Q$name\E\)|
3725 ^.DECLARE_$Ident\(\Q$name\E\)|
3726 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003727 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3728 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003729 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003730#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3731 $suppress_export{$realline_next} = 2;
3732 } else {
3733 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003734 }
3735 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003736 if (!defined $suppress_export{$linenr} &&
3737 $prevline =~ /^.\s*$/ &&
3738 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3739 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3740#print "FOO B <$lines[$linenr - 1]>\n";
3741 $suppress_export{$linenr} = 2;
3742 }
3743 if (defined $suppress_export{$linenr} &&
3744 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003745 WARN("EXPORT_SYMBOL",
3746 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003747 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003748
Joe Eloff5150bda2010-08-09 17:21:00 -07003749# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003750 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003751 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003752 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003753 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003754 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003755 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003756 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003757# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003758 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003759 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003760 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003761 $herecurr) &&
3762 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003763 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003764 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003765 }
3766
Joe Perches18130872014-08-06 16:11:22 -07003767# check for misordered declarations of char/short/int/long with signed/unsigned
3768 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3769 my $tmp = trim($1);
3770 WARN("MISORDERED_TYPE",
3771 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3772 }
3773
Joe Perchescb710ec2010-10-26 14:23:20 -07003774# check for static const char * arrays.
3775 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003776 WARN("STATIC_CONST_CHAR_ARRAY",
3777 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003778 $herecurr);
3779 }
3780
3781# check for static char foo[] = "bar" declarations.
3782 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003783 WARN("STATIC_CONST_CHAR_ARRAY",
3784 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003785 $herecurr);
3786 }
3787
Joe Perchesab7e23f2015-04-16 12:44:22 -07003788# check for const <foo> const where <foo> is not a pointer or array type
3789 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3790 my $found = $1;
3791 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3792 WARN("CONST_CONST",
3793 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3794 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3795 WARN("CONST_CONST",
3796 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3797 }
3798 }
3799
Joe Perches9b0fa602014-04-03 14:49:18 -07003800# check for non-global char *foo[] = {"bar", ...} declarations.
3801 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3802 WARN("STATIC_CONST_CHAR_ARRAY",
3803 "char * array declaration might be better as static const\n" .
3804 $herecurr);
3805 }
3806
Joe Perchesb598b672015-04-16 12:44:36 -07003807# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3808 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3809 my $array = $1;
3810 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3811 my $array_div = $1;
3812 if (WARN("ARRAY_SIZE",
3813 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3814 $fix) {
3815 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3816 }
3817 }
3818 }
3819
Joe Perchesb36190c2014-01-27 17:07:18 -08003820# check for function declarations without arguments like "int foo()"
3821 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3822 if (ERROR("FUNCTION_WITHOUT_ARGS",
3823 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3824 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003825 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003826 }
3827 }
3828
Andy Whitcroft653d4872007-06-23 17:16:34 -07003829# check for new typedefs, only function parameters and sparse annotations
3830# make sense.
3831 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003832 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003833 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003834 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003835 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003836 WARN("NEW_TYPEDEFS",
3837 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003838 }
3839
3840# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003841 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003842 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3843 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003844 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003845
Andy Whitcroft65863862009-01-06 14:41:21 -08003846 # Should start with a space.
3847 $to =~ s/^(\S)/ $1/;
3848 # Should not end with a space.
3849 $to =~ s/\s+$//;
3850 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003851 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003852 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003853
Joe Perches3705ce52013-07-03 15:05:31 -07003854## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003855 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003856 if (ERROR("POINTER_LOCATION",
3857 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3858 $fix) {
3859 my $sub_from = $ident;
3860 my $sub_to = $ident;
3861 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003862 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003863 s@\Q$sub_from\E@$sub_to@;
3864 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003865 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003866 }
3867 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3868 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003869 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003870
Andy Whitcroft65863862009-01-06 14:41:21 -08003871 # Should start with a space.
3872 $to =~ s/^(\S)/ $1/;
3873 # Should not end with a space.
3874 $to =~ s/\s+$//;
3875 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003876 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003877 }
3878 # Modifiers should have spaces.
3879 $to =~ s/(\b$Modifier$)/$1 /;
3880
Joe Perches3705ce52013-07-03 15:05:31 -07003881## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003882 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003883 if (ERROR("POINTER_LOCATION",
3884 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3885 $fix) {
3886
3887 my $sub_from = $match;
3888 my $sub_to = $match;
3889 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003890 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003891 s@\Q$sub_from\E@$sub_to@;
3892 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003893 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003894 }
3895
Joe Perches9d3e3c72015-09-09 15:37:27 -07003896# avoid BUG() or BUG_ON()
3897 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3898 my $msg_type = \&WARN;
3899 $msg_type = \&CHK if ($file);
3900 &{$msg_type}("AVOID_BUG",
3901 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3902 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003903
Joe Perches9d3e3c72015-09-09 15:37:27 -07003904# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003905 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003906 WARN("LINUX_VERSION_CODE",
3907 "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 -08003908 }
3909
Joe Perches17441222011-06-15 15:08:17 -07003910# check for uses of printk_ratelimit
3911 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003912 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003913 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003914 }
3915
Andy Whitcroft00df3442007-06-08 13:47:06 -07003916# printk should use KERN_* levels. Note that follow on printk's on the
3917# same line do not need a level, so we use the current block context
3918# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003919# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003920# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003921 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003922 my $ok = 0;
3923 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3924 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003925 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003926 # with "\n" ignore it, else it is to blame
3927 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3928 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3929 $ok = 1;
3930 }
3931 last;
3932 }
3933 }
3934 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003935 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3936 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003937 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003938 }
3939
Joe Perches243f3802012-05-31 16:26:09 -07003940 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3941 my $orig = $1;
3942 my $level = lc($orig);
3943 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003944 my $level2 = $level;
3945 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003946 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003947 "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 -07003948 }
3949
3950 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003951 if (WARN("PREFER_PR_LEVEL",
3952 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3953 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003954 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003955 s/\bpr_warning\b/pr_warn/;
3956 }
Joe Perches243f3802012-05-31 16:26:09 -07003957 }
3958
Joe Perchesdc139312013-02-21 16:44:13 -08003959 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3960 my $orig = $1;
3961 my $level = lc($orig);
3962 $level = "warn" if ($level eq "warning");
3963 $level = "dbg" if ($level eq "debug");
3964 WARN("PREFER_DEV_LEVEL",
3965 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3966 }
3967
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003968# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3969# number of false positives, but assembly files are not checked, so at
3970# least the arch entry code will not trigger this warning.
3971 if ($line =~ /\bENOSYS\b/) {
3972 WARN("ENOSYS",
3973 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3974 }
3975
Andy Whitcroft653d4872007-06-23 17:16:34 -07003976# function brace can't be on same line, except for #defines of do while,
3977# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003978 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003979 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003980 if (ERROR("OPEN_BRACE",
3981 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3982 $fix) {
3983 fix_delete_line($fixlinenr, $rawline);
3984 my $fixed_line = $rawline;
3985 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3986 my $line1 = $1;
3987 my $line2 = $2;
3988 fix_insert_line($fixlinenr, ltrim($line1));
3989 fix_insert_line($fixlinenr, "\+{");
3990 if ($line2 !~ /^\s*$/) {
3991 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3992 }
3993 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003994 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003995
Andy Whitcroft8905a672007-11-28 16:21:06 -08003996# open braces for enum, union and struct go on the same line.
3997 if ($line =~ /^.\s*{/ &&
3998 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003999 if (ERROR("OPEN_BRACE",
4000 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4001 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4002 fix_delete_line($fixlinenr - 1, $prevrawline);
4003 fix_delete_line($fixlinenr, $rawline);
4004 my $fixedline = rtrim($prevrawline) . " {";
4005 fix_insert_line($fixlinenr, $fixedline);
4006 $fixedline = $rawline;
4007 $fixedline =~ s/^(.\s*){\s*/$1\t/;
4008 if ($fixedline !~ /^\+\s*$/) {
4009 fix_insert_line($fixlinenr, $fixedline);
4010 }
4011 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004012 }
4013
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004014# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004015 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4016 if (WARN("SPACING",
4017 "missing space after $1 definition\n" . $herecurr) &&
4018 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004019 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004020 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4021 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004022 }
4023
Joe Perches31070b52014-01-23 15:54:49 -08004024# Function pointer declarations
4025# check spacing between type, funcptr, and args
4026# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004027 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004028 my $declare = $1;
4029 my $pre_pointer_space = $2;
4030 my $post_pointer_space = $3;
4031 my $funcname = $4;
4032 my $post_funcname_space = $5;
4033 my $pre_args_space = $6;
4034
Joe Perches91f72e92014-04-03 14:49:12 -07004035# the $Declare variable will capture all spaces after the type
4036# so check it for a missing trailing missing space but pointer return types
4037# don't need a space so don't warn for those.
4038 my $post_declare_space = "";
4039 if ($declare =~ /(\s+)$/) {
4040 $post_declare_space = $1;
4041 $declare = rtrim($declare);
4042 }
4043 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004044 WARN("SPACING",
4045 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004046 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004047 }
4048
4049# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004050# This test is not currently implemented because these declarations are
4051# equivalent to
4052# int foo(int bar, ...)
4053# and this is form shouldn't/doesn't generate a checkpatch warning.
4054#
4055# elsif ($declare =~ /\s{2,}$/) {
4056# WARN("SPACING",
4057# "Multiple spaces after return type\n" . $herecurr);
4058# }
Joe Perches31070b52014-01-23 15:54:49 -08004059
4060# unnecessary space "type ( *funcptr)(args...)"
4061 if (defined $pre_pointer_space &&
4062 $pre_pointer_space =~ /^\s/) {
4063 WARN("SPACING",
4064 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4065 }
4066
4067# unnecessary space "type (* funcptr)(args...)"
4068 if (defined $post_pointer_space &&
4069 $post_pointer_space =~ /^\s/) {
4070 WARN("SPACING",
4071 "Unnecessary space before function pointer name\n" . $herecurr);
4072 }
4073
4074# unnecessary space "type (*funcptr )(args...)"
4075 if (defined $post_funcname_space &&
4076 $post_funcname_space =~ /^\s/) {
4077 WARN("SPACING",
4078 "Unnecessary space after function pointer name\n" . $herecurr);
4079 }
4080
4081# unnecessary space "type (*funcptr) (args...)"
4082 if (defined $pre_args_space &&
4083 $pre_args_space =~ /^\s/) {
4084 WARN("SPACING",
4085 "Unnecessary space before function pointer arguments\n" . $herecurr);
4086 }
4087
4088 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004089 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004090 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004091 }
4092 }
4093
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004094# check for spacing round square brackets; allowed:
4095# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004096# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4097# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004098 while ($line =~ /(.*?\s)\[/g) {
4099 my ($where, $prefix) = ($-[1], $1);
4100 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004101 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004102 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004103 if (ERROR("BRACKET_SPACE",
4104 "space prohibited before open square bracket '['\n" . $herecurr) &&
4105 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004106 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004107 s/^(\+.*?)\s+\[/$1\[/;
4108 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004109 }
4110 }
4111
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004112# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004113 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004114 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004115 my $ctx_before = substr($line, 0, $-[1]);
4116 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004117
4118 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004119 if ($name =~ /^(?:
4120 if|for|while|switch|return|case|
4121 volatile|__volatile__|
4122 __attribute__|format|__extension__|
4123 asm|__asm__)$/x)
4124 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004125 # cpp #define statements have non-optional spaces, ie
4126 # if there is a space between the name and the open
4127 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004128 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004129
4130 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004131 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004132
4133 # If this whole things ends with a type its most
4134 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004135 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004136
4137 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004138 if (WARN("SPACING",
4139 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4140 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004141 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004142 s/\b$name\s+\(/$name\(/;
4143 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004144 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004145 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004146
Andy Whitcroft653d4872007-06-23 17:16:34 -07004147# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004148 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004149 my $fixed_line = "";
4150 my $line_fixed = 0;
4151
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004152 my $ops = qr{
4153 <<=|>>=|<=|>=|==|!=|
4154 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4155 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004156 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004157 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004158 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004159 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004160
4161## print("element count: <" . $#elements . ">\n");
4162## foreach my $el (@elements) {
4163## print("el: <$el>\n");
4164## }
4165
4166 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004167 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004168
Joe Perches3705ce52013-07-03 15:05:31 -07004169 foreach my $el (@elements) {
4170 push(@fix_elements, substr($rawline, $off, length($el)));
4171 $off += length($el);
4172 }
4173
4174 $off = 0;
4175
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004176 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004177 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004178
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004179 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004180
4181 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4182
4183## print("n: <$n> good: <$good>\n");
4184
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004185 $off += length($elements[$n]);
4186
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004187 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004188 my $ca = substr($opline, 0, $off);
4189 my $cc = '';
4190 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4191 $cc = substr($opline, $off + length($elements[$n + 1]));
4192 }
4193 my $cb = "$ca$;$cc";
4194
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004195 my $a = '';
4196 $a = 'V' if ($elements[$n] ne '');
4197 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004198 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004199 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4200 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004201 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004202
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004203 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004204
4205 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004206 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004207 $c = 'V' if ($elements[$n + 2] ne '');
4208 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004209 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004210 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4211 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004212 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004213 } else {
4214 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004215 }
4216
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004217 my $ctx = "${a}x${c}";
4218
4219 my $at = "(ctx:$ctx)";
4220
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004221 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004222 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004223
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004224 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004225 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004226
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004227 # Get the full operator variant.
4228 my $opv = $op . substr($curr_vars, $off, 1);
4229
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004230 # Ignore operators passed as parameters.
4231 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004232 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004233
Andy Whitcroftcf655042008-03-04 14:28:20 -08004234# # Ignore comments
4235# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004236
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004237 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004238 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004239 if ($ctx !~ /.x[WEBC]/ &&
4240 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004241 if (ERROR("SPACING",
4242 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004243 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004244 $line_fixed = 1;
4245 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004246 }
4247
4248 # // is a comment
4249 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004250
Joe Perchesb00e4812014-04-03 14:49:33 -07004251 # : when part of a bitfield
4252 } elsif ($opv eq ':B') {
4253 # skip the bitfield test for now
4254
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004255 # No spaces for:
4256 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004257 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004258 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004259 if (ERROR("SPACING",
4260 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004261 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004262 if (defined $fix_elements[$n + 2]) {
4263 $fix_elements[$n + 2] =~ s/^\s+//;
4264 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004265 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004266 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004267 }
4268
Joe Perches23810972014-12-10 15:51:32 -08004269 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004270 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004271 my $rtrim_before = 0;
4272 my $space_after = 0;
4273 if ($ctx =~ /Wx./) {
4274 if (ERROR("SPACING",
4275 "space prohibited before that '$op' $at\n" . $hereptr)) {
4276 $line_fixed = 1;
4277 $rtrim_before = 1;
4278 }
4279 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004280 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004281 if (ERROR("SPACING",
4282 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004283 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004284 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004285 $space_after = 1;
4286 }
4287 }
4288 if ($rtrim_before || $space_after) {
4289 if ($rtrim_before) {
4290 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4291 } else {
4292 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4293 }
4294 if ($space_after) {
4295 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004296 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004297 }
4298
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004299 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004300 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004301 #warn "'*' is part of type\n";
4302
4303 # unary operators should have a space before and
4304 # none after. May be left adjacent to another
4305 # unary operator, or a cast
4306 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004307 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004308 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004309 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004310 if (ERROR("SPACING",
4311 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004312 if ($n != $last_after + 2) {
4313 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4314 $line_fixed = 1;
4315 }
Joe Perches3705ce52013-07-03 15:05:31 -07004316 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004317 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004318 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004319 # A unary '*' may be const
4320
4321 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004322 if (ERROR("SPACING",
4323 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004324 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004325 if (defined $fix_elements[$n + 2]) {
4326 $fix_elements[$n + 2] =~ s/^\s+//;
4327 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004328 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004329 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004330 }
4331
4332 # unary ++ and unary -- are allowed no space on one side.
4333 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004334 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004335 if (ERROR("SPACING",
4336 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004337 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004338 $line_fixed = 1;
4339 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004340 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004341 if ($ctx =~ /Wx[BE]/ ||
4342 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004343 if (ERROR("SPACING",
4344 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004345 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004346 $line_fixed = 1;
4347 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004348 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004349 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004350 if (ERROR("SPACING",
4351 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004352 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004353 if (defined $fix_elements[$n + 2]) {
4354 $fix_elements[$n + 2] =~ s/^\s+//;
4355 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004356 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004357 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004358 }
4359
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004360 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004361 } elsif ($op eq '<<' or $op eq '>>' or
4362 $op eq '&' or $op eq '^' or $op eq '|' or
4363 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004364 $op eq '*' or $op eq '/' or
4365 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004366 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004367 if ($check) {
4368 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4369 if (CHK("SPACING",
4370 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4371 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4372 $fix_elements[$n + 2] =~ s/^\s+//;
4373 $line_fixed = 1;
4374 }
4375 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4376 if (CHK("SPACING",
4377 "space preferred before that '$op' $at\n" . $hereptr)) {
4378 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4379 $line_fixed = 1;
4380 }
4381 }
4382 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004383 if (ERROR("SPACING",
4384 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004385 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4386 if (defined $fix_elements[$n + 2]) {
4387 $fix_elements[$n + 2] =~ s/^\s+//;
4388 }
Joe Perches3705ce52013-07-03 15:05:31 -07004389 $line_fixed = 1;
4390 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004391 }
4392
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004393 # A colon needs no spaces before when it is
4394 # terminating a case value or a label.
4395 } elsif ($opv eq ':C' || $opv eq ':L') {
4396 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004397 if (ERROR("SPACING",
4398 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004399 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004400 $line_fixed = 1;
4401 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004402 }
4403
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004404 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004405 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004406 my $ok = 0;
4407
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004408 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004409 if (($op eq '<' &&
4410 $cc =~ /^\S+\@\S+>/) ||
4411 ($op eq '>' &&
4412 $ca =~ /<\S+\@\S+$/))
4413 {
4414 $ok = 1;
4415 }
4416
Joe Perchese0df7e12015-04-16 12:44:53 -07004417 # for asm volatile statements
4418 # ignore a colon with another
4419 # colon immediately before or after
4420 if (($op eq ':') &&
4421 ($ca =~ /:$/ || $cc =~ /^:/)) {
4422 $ok = 1;
4423 }
4424
Joe Perches84731622013-11-12 15:10:05 -08004425 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004426 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004427 my $msg_type = \&ERROR;
4428 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4429
4430 if (&{$msg_type}("SPACING",
4431 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004432 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4433 if (defined $fix_elements[$n + 2]) {
4434 $fix_elements[$n + 2] =~ s/^\s+//;
4435 }
Joe Perches3705ce52013-07-03 15:05:31 -07004436 $line_fixed = 1;
4437 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004438 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004439 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004440 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004441
4442## print("n: <$n> GOOD: <$good>\n");
4443
4444 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004445 }
Joe Perches3705ce52013-07-03 15:05:31 -07004446
4447 if (($#elements % 2) == 0) {
4448 $fixed_line = $fixed_line . $fix_elements[$#elements];
4449 }
4450
Joe Perches194f66f2014-08-06 16:11:03 -07004451 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4452 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004453 }
4454
4455
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004456 }
4457
Joe Perches786b6322013-07-03 15:05:32 -07004458# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004459 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004460 if (WARN("SPACING",
4461 "space prohibited before semicolon\n" . $herecurr) &&
4462 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004463 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004464 s/^(\+.*\S)\s+;/$1;/;
4465 }
4466 }
4467
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004468# check for multiple assignments
4469 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004470 CHK("MULTIPLE_ASSIGNMENTS",
4471 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004472 }
4473
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004474## # check for multiple declarations, allowing for a function declaration
4475## # continuation.
4476## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4477## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4478##
4479## # Remove any bracketed sections to ensure we do not
4480## # falsly report the parameters of functions.
4481## my $ln = $line;
4482## while ($ln =~ s/\([^\(\)]*\)//g) {
4483## }
4484## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004485## WARN("MULTIPLE_DECLARATION",
4486## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004487## }
4488## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004489
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004490#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004491 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004492 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004493 if (ERROR("SPACING",
4494 "space required before the open brace '{'\n" . $herecurr) &&
4495 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004496 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004497 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004498 }
4499
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004500## # check for blank lines before declarations
4501## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4502## $prevrawline =~ /^.\s*$/) {
4503## WARN("SPACING",
4504## "No blank lines before declarations\n" . $hereprev);
4505## }
4506##
4507
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004508# closing brace should have a space following it when it has anything
4509# on the line
4510 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004511 if (ERROR("SPACING",
4512 "space required after that close brace '}'\n" . $herecurr) &&
4513 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004514 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004515 s/}((?!(?:,|;|\)))\S)/} $1/;
4516 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004517 }
4518
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004519# check spacing on square brackets
4520 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004521 if (ERROR("SPACING",
4522 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4523 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004524 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004525 s/\[\s+/\[/;
4526 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004527 }
4528 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004529 if (ERROR("SPACING",
4530 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4531 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004532 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004533 s/\s+\]/\]/;
4534 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004535 }
4536
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004537# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004538 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004539 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004540 if (ERROR("SPACING",
4541 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4542 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004543 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004544 s/\(\s+/\(/;
4545 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004546 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004547 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004548 $line !~ /for\s*\(.*;\s+\)/ &&
4549 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004550 if (ERROR("SPACING",
4551 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4552 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004553 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004554 s/\s+\)/\)/;
4555 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004556 }
4557
Joe Perchese2826fd2014-08-06 16:10:48 -07004558# check unnecessary parentheses around addressof/dereference single $Lvals
4559# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4560
4561 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004562 my $var = $1;
4563 if (CHK("UNNECESSARY_PARENTHESES",
4564 "Unnecessary parentheses around $var\n" . $herecurr) &&
4565 $fix) {
4566 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4567 }
4568 }
4569
4570# check for unnecessary parentheses around function pointer uses
4571# ie: (foo->bar)(); should be foo->bar();
4572# but not "if (foo->bar) (" to avoid some false positives
4573 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4574 my $var = $2;
4575 if (CHK("UNNECESSARY_PARENTHESES",
4576 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4577 $fix) {
4578 my $var2 = deparenthesize($var);
4579 $var2 =~ s/\s//g;
4580 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4581 }
4582 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004583
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004584#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004585 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004586 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004587 if (WARN("INDENTED_LABEL",
4588 "labels should not be indented\n" . $herecurr) &&
4589 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004590 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004591 s/^(.)\s+/$1/;
4592 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004593 }
4594
Joe Perches5b9553a2014-04-03 14:49:21 -07004595# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004596 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004597 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004598 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004599 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4600 my $value = $1;
4601 $value = deparenthesize($value);
4602 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4603 ERROR("RETURN_PARENTHESES",
4604 "return is not a function, parentheses are not required\n" . $herecurr);
4605 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004606 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004607 ERROR("SPACING",
4608 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004609 }
4610 }
Joe Perches507e5142013-11-12 15:10:13 -08004611
Joe Perchesb43ae212014-06-23 13:22:07 -07004612# unnecessary return in a void function
4613# at end-of-function, with the previous line a single leading tab, then return;
4614# and the line before that not a goto label target like "out:"
4615 if ($sline =~ /^[ \+]}\s*$/ &&
4616 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4617 $linenr >= 3 &&
4618 $lines[$linenr - 3] =~ /^[ +]/ &&
4619 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004620 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004621 "void function return statements are not generally useful\n" . $hereprev);
4622 }
Joe Perches9819cf22014-06-04 16:12:09 -07004623
Joe Perches189248d2014-01-23 15:54:47 -08004624# if statements using unnecessary parentheses - ie: if ((foo == bar))
4625 if ($^V && $^V ge 5.10.0 &&
4626 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4627 my $openparens = $1;
4628 my $count = $openparens =~ tr@\(@\(@;
4629 my $msg = "";
4630 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4631 my $comp = $4; #Not $1 because of $LvalOrFunc
4632 $msg = " - maybe == should be = ?" if ($comp eq "==");
4633 WARN("UNNECESSARY_PARENTHESES",
4634 "Unnecessary parentheses$msg\n" . $herecurr);
4635 }
4636 }
4637
Joe Perchesc5595fa2015-09-09 15:37:58 -07004638# comparisons with a constant or upper case identifier on the left
4639# avoid cases like "foo + BAR < baz"
4640# only fix matches surrounded by parentheses to avoid incorrect
4641# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4642 if ($^V && $^V ge 5.10.0 &&
4643 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4644 my $lead = $1;
4645 my $const = $2;
4646 my $comp = $3;
4647 my $to = $4;
4648 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004649 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004650 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4651 WARN("CONSTANT_COMPARISON",
4652 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4653 $fix) {
4654 if ($comp eq "<") {
4655 $newcomp = ">";
4656 } elsif ($comp eq "<=") {
4657 $newcomp = ">=";
4658 } elsif ($comp eq ">") {
4659 $newcomp = "<";
4660 } elsif ($comp eq ">=") {
4661 $newcomp = "<=";
4662 }
4663 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4664 }
4665 }
4666
Joe Perchesf34e4a42015-04-16 12:44:19 -07004667# Return of what appears to be an errno should normally be negative
4668 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004669 my $name = $1;
4670 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004671 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004672 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004673 }
4674 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004675
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004676# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004677 if ($line =~ /\b(if|while|for|switch)\(/) {
4678 if (ERROR("SPACING",
4679 "space required before the open parenthesis '('\n" . $herecurr) &&
4680 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004681 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004682 s/\b(if|while|for|switch)\(/$1 \(/;
4683 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004684 }
4685
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004686# Check for illegal assignment in if conditional -- and check for trailing
4687# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004688 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004689 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4690 ctx_statement_block($linenr, $realcnt, 0)
4691 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004692 my ($stat_next) = ctx_statement_block($line_nr_next,
4693 $remain_next, $off_next);
4694 $stat_next =~ s/\n./\n /g;
4695 ##print "stat<$stat> stat_next<$stat_next>\n";
4696
4697 if ($stat_next =~ /^\s*while\b/) {
4698 # If the statement carries leading newlines,
4699 # then count those as offsets.
4700 my ($whitespace) =
4701 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4702 my $offset =
4703 statement_rawlines($whitespace) - 1;
4704
4705 $suppress_whiletrailers{$line_nr_next +
4706 $offset} = 1;
4707 }
4708 }
4709 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004710 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004711 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004712 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004713
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004714 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004715 ERROR("ASSIGN_IN_IF",
4716 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004717 }
4718
4719 # Find out what is on the end of the line after the
4720 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004721 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004722 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004723 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004724 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4725 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004726 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004727 # Find out how long the conditional actually is.
4728 my @newlines = ($c =~ /\n/gs);
4729 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004730 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004731
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004732 $stat_real = raw_line($linenr, $cond_lines)
4733 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004734 if (defined($stat_real) && $cond_lines > 1) {
4735 $stat_real = "[...]\n$stat_real";
4736 }
4737
Joe Perches000d1cc12011-07-25 17:13:25 -07004738 ERROR("TRAILING_STATEMENTS",
4739 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004740 }
4741 }
4742
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004743# Check for bitwise tests written as boolean
4744 if ($line =~ /
4745 (?:
4746 (?:\[|\(|\&\&|\|\|)
4747 \s*0[xX][0-9]+\s*
4748 (?:\&\&|\|\|)
4749 |
4750 (?:\&\&|\|\|)
4751 \s*0[xX][0-9]+\s*
4752 (?:\&\&|\|\||\)|\])
4753 )/x)
4754 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004755 WARN("HEXADECIMAL_BOOLEAN_TEST",
4756 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004757 }
4758
Andy Whitcroft8905a672007-11-28 16:21:06 -08004759# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004760 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4761 my $s = $1;
4762 $s =~ s/$;//g; # Remove any comments
4763 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004764 ERROR("TRAILING_STATEMENTS",
4765 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004766 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004767 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004768# if should not continue a brace
4769 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004770 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004771 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004772 $herecurr);
4773 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004774# case and default should not have general statements after them
4775 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4776 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004777 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004778 \s*return\s+
4779 )/xg)
4780 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004781 ERROR("TRAILING_STATEMENTS",
4782 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004783 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004784
4785 # Check for }<nl>else {, these must be at the same
4786 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004787 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4788 $previndent == $indent) {
4789 if (ERROR("ELSE_AFTER_BRACE",
4790 "else should follow close brace '}'\n" . $hereprev) &&
4791 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4792 fix_delete_line($fixlinenr - 1, $prevrawline);
4793 fix_delete_line($fixlinenr, $rawline);
4794 my $fixedline = $prevrawline;
4795 $fixedline =~ s/}\s*$//;
4796 if ($fixedline !~ /^\+\s*$/) {
4797 fix_insert_line($fixlinenr, $fixedline);
4798 }
4799 $fixedline = $rawline;
4800 $fixedline =~ s/^(.\s*)else/$1} else/;
4801 fix_insert_line($fixlinenr, $fixedline);
4802 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004803 }
4804
Joe Perches8b8856f2014-08-06 16:11:14 -07004805 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4806 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004807 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4808
4809 # Find out what is on the end of the line after the
4810 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004811 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004812 $s =~ s/\n.*//g;
4813
4814 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004815 if (ERROR("WHILE_AFTER_BRACE",
4816 "while should follow close brace '}'\n" . $hereprev) &&
4817 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4818 fix_delete_line($fixlinenr - 1, $prevrawline);
4819 fix_delete_line($fixlinenr, $rawline);
4820 my $fixedline = $prevrawline;
4821 my $trailing = $rawline;
4822 $trailing =~ s/^\+//;
4823 $trailing = trim($trailing);
4824 $fixedline =~ s/}\s*$/} $trailing/;
4825 fix_insert_line($fixlinenr, $fixedline);
4826 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004827 }
4828 }
4829
Joe Perches95e2c602013-07-03 15:05:20 -07004830#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004831 while ($line =~ m{($Constant|$Lval)}g) {
4832 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004833
4834#gcc binary extension
4835 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004836 if (WARN("GCC_BINARY_CONSTANT",
4837 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4838 $fix) {
4839 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004840 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004841 s/\b$var\b/$hexval/;
4842 }
Joe Perches95e2c602013-07-03 15:05:20 -07004843 }
4844
4845#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004846 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004847 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004848#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004849 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004850#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004851 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4852#Ignore some three character SI units explicitly, like MiB and KHz
4853 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004854 while ($var =~ m{($Ident)}g) {
4855 my $word = $1;
4856 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004857 if ($check) {
4858 seed_camelcase_includes();
4859 if (!$file && !$camelcase_file_seeded) {
4860 seed_camelcase_file($realfile);
4861 $camelcase_file_seeded = 1;
4862 }
4863 }
Joe Perches7e781f62013-09-11 14:23:55 -07004864 if (!defined $camelcase{$word}) {
4865 $camelcase{$word} = 1;
4866 CHK("CAMELCASE",
4867 "Avoid CamelCase: <$word>\n" . $herecurr);
4868 }
Joe Perches34456862013-07-03 15:05:34 -07004869 }
Joe Perches323c1262012-12-17 16:02:07 -08004870 }
4871 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004872
4873#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004874 if ($line =~ /\#\s*define.*\\\s+$/) {
4875 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4876 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4877 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004878 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004879 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004880 }
4881
Fabian Frederick0e212e02015-04-16 12:44:25 -07004882# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4883# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004884 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004885 my $file = "$1.h";
4886 my $checkfile = "include/linux/$file";
4887 if (-f "$root/$checkfile" &&
4888 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004889 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004890 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004891 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4892 if ($asminclude > 0) {
4893 if ($realfile =~ m{^arch/}) {
4894 CHK("ARCH_INCLUDE_LINUX",
4895 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4896 } else {
4897 WARN("INCLUDE_LINUX",
4898 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4899 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004900 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004901 }
4902 }
4903
Andy Whitcroft653d4872007-06-23 17:16:34 -07004904# multi-statement macros should be enclosed in a do while loop, grab the
4905# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004906# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004907 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4908 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004909 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004910 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911 my ($off, $dstat, $dcond, $rest);
4912 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004913 my $has_flow_statement = 0;
4914 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004915 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004916 ctx_statement_block($linenr, $realcnt, 0);
4917 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004918 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004919 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004920
Joe Perches08a28432014-10-13 15:51:55 -07004921 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004922 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004923
Joe Perchesf59b64b2016-10-11 13:52:08 -07004924 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4925 my $define_args = $1;
4926 my $define_stmt = $dstat;
4927 my @def_args = ();
4928
4929 if (defined $define_args && $define_args ne "") {
4930 $define_args = substr($define_args, 1, length($define_args) - 2);
4931 $define_args =~ s/\s*//g;
4932 @def_args = split(",", $define_args);
4933 }
4934
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004935 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004936 $dstat =~ s/\\\n.//g;
4937 $dstat =~ s/^\s*//s;
4938 $dstat =~ s/\s*$//s;
4939
4940 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004941 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4942 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004943 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004944 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004945 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004946
Gregory Bean02870be2011-05-11 09:11:12 -07004947 # Extremely long macros may fall off the end of the
4948 # available context without closing. Give a dangling
4949 # backslash the benefit of the doubt and allow it
4950 # to gobble any hanging open-parens.
4951 $dstat =~ s/\(.+\\$/1/;
4952
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004953 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004954 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4955 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004956 {
4957 }
4958
Joe Perches42e15292016-03-15 14:58:01 -07004959 # Make asm volatile uses seem like a generic function
4960 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4961
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004962 my $exceptions = qr{
4963 $Declare|
4964 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004965 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004966 DECLARE_PER_CPU|
4967 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004968 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004969 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004970 union|
4971 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004972 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004973 ^\"|\"$|
4974 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004975 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004976 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004977
4978 $ctx =~ s/\n*$//;
4979 my $herectx = $here . "\n";
4980 my $stmt_cnt = statement_rawlines($ctx);
4981
4982 for (my $n = 0; $n < $stmt_cnt; $n++) {
4983 $herectx .= raw_line($linenr, $n) . "\n";
4984 }
4985
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004986 if ($dstat ne '' &&
4987 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4988 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004989 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004990 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004991 $dstat !~ /$exceptions/ &&
4992 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004993 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004994 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004995 $dstat !~ /^for\s*$Constant$/ && # for (...)
4996 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4997 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004998 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004999 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005000 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005001
Andy Whitcroftf74bd192012-01-10 15:09:54 -08005002 if ($dstat =~ /;/) {
5003 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5004 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5005 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07005007 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07005008 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005009
5010 }
Joe Perches52076492016-10-11 13:52:14 -07005011
5012 # Make $define_stmt single line, comment-free, etc
5013 my @stmt_array = split('\n', $define_stmt);
5014 my $first = 1;
5015 $define_stmt = "";
5016 foreach my $l (@stmt_array) {
5017 $l =~ s/\\$//;
5018 if ($first) {
5019 $define_stmt = $l;
5020 $first = 0;
5021 } elsif ($l =~ /^[\+ ]/) {
5022 $define_stmt .= substr($l, 1);
5023 }
5024 }
5025 $define_stmt =~ s/$;//g;
5026 $define_stmt =~ s/\s+/ /g;
5027 $define_stmt = trim($define_stmt);
5028
Joe Perchesf59b64b2016-10-11 13:52:08 -07005029# check if any macro arguments are reused (ignore '...' and 'type')
5030 foreach my $arg (@def_args) {
5031 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005032 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005033 my $tmp = $define_stmt;
5034 $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 -07005035 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005036 $tmp =~ s/\b$arg\s*\#\#//g;
5037 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5038 if ($use_cnt > 1) {
5039 CHK("MACRO_ARG_REUSE",
5040 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005041 }
5042# check if any macro arguments may have other precedence issues
5043 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5044 ((defined($1) && $1 ne ',') ||
5045 (defined($2) && $2 ne ','))) {
5046 CHK("MACRO_ARG_PRECEDENCE",
5047 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005048 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005049 }
Joe Perches5023d342012-12-17 16:01:47 -08005050
Joe Perches08a28432014-10-13 15:51:55 -07005051# check for macros with flow control, but without ## concatenation
5052# ## concatenation is commonly a macro that defines a function so ignore those
5053 if ($has_flow_statement && !$has_arg_concat) {
5054 my $herectx = $here . "\n";
5055 my $cnt = statement_rawlines($ctx);
5056
5057 for (my $n = 0; $n < $cnt; $n++) {
5058 $herectx .= raw_line($linenr, $n) . "\n";
5059 }
5060 WARN("MACRO_WITH_FLOW_CONTROL",
5061 "Macros with flow control statements should be avoided\n" . "$herectx");
5062 }
5063
Joe Perches481eb482012-12-17 16:01:56 -08005064# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005065
5066 } else {
5067 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005068 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5069 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005070 $line =~ /^\+.*\\$/) {
5071 WARN("LINE_CONTINUATIONS",
5072 "Avoid unnecessary line continuations\n" . $herecurr);
5073 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005074 }
5075
Joe Perchesb13edf72012-07-30 14:41:24 -07005076# do {} while (0) macro tests:
5077# single-statement macros do not need to be enclosed in do while (0) loop,
5078# macro should not end with a semicolon
5079 if ($^V && $^V ge 5.10.0 &&
5080 $realfile !~ m@/vmlinux.lds.h$@ &&
5081 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5082 my $ln = $linenr;
5083 my $cnt = $realcnt;
5084 my ($off, $dstat, $dcond, $rest);
5085 my $ctx = '';
5086 ($dstat, $dcond, $ln, $cnt, $off) =
5087 ctx_statement_block($linenr, $realcnt, 0);
5088 $ctx = $dstat;
5089
5090 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005091 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005092
5093 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5094 my $stmts = $2;
5095 my $semis = $3;
5096
5097 $ctx =~ s/\n*$//;
5098 my $cnt = statement_rawlines($ctx);
5099 my $herectx = $here . "\n";
5100
5101 for (my $n = 0; $n < $cnt; $n++) {
5102 $herectx .= raw_line($linenr, $n) . "\n";
5103 }
5104
Joe Perchesac8e97f2012-08-21 16:15:53 -07005105 if (($stmts =~ tr/;/;/) == 1 &&
5106 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005107 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5108 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5109 }
5110 if (defined $semis && $semis ne "") {
5111 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5112 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5113 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005114 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5115 $ctx =~ s/\n*$//;
5116 my $cnt = statement_rawlines($ctx);
5117 my $herectx = $here . "\n";
5118
5119 for (my $n = 0; $n < $cnt; $n++) {
5120 $herectx .= raw_line($linenr, $n) . "\n";
5121 }
5122
5123 WARN("TRAILING_SEMICOLON",
5124 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005125 }
5126 }
5127
Mike Frysinger080ba922009-01-06 14:41:25 -08005128# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5129# all assignments may have only one of the following with an assignment:
5130# .
5131# ALIGN(...)
5132# VMLINUX_SYMBOL(...)
5133 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005134 WARN("MISSING_VMLINUX_SYMBOL",
5135 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005136 }
5137
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005138# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005139 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5140 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005141 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005142 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005143 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5144 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005145 my @allowed = ();
5146 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005147 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005148 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005149 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005150 for my $chunk (@chunks) {
5151 my ($cond, $block) = @{$chunk};
5152
Andy Whitcroft773647a2008-03-28 14:15:58 -07005153 # If the condition carries leading newlines, then count those as offsets.
5154 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5155 my $offset = statement_rawlines($whitespace) - 1;
5156
Joe Perchesaad4f612012-03-23 15:02:19 -07005157 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005158 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5159
5160 # We have looked at and allowed this specific line.
5161 $suppress_ifbraces{$ln + $offset} = 1;
5162
5163 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005164 $ln += statement_rawlines($block) - 1;
5165
Andy Whitcroft773647a2008-03-28 14:15:58 -07005166 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005167
5168 $seen++ if ($block =~ /^\s*{/);
5169
Joe Perchesaad4f612012-03-23 15:02:19 -07005170 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005171 if (statement_lines($cond) > 1) {
5172 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005173 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005174 }
5175 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005176 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005177 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005178 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005179 if (statement_block_size($block) > 1) {
5180 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005181 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005182 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005183 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005184 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005185 if ($seen) {
5186 my $sum_allowed = 0;
5187 foreach (@allowed) {
5188 $sum_allowed += $_;
5189 }
5190 if ($sum_allowed == 0) {
5191 WARN("BRACES",
5192 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5193 } elsif ($sum_allowed != $allow &&
5194 $seen != $allow) {
5195 CHK("BRACES",
5196 "braces {} should be used on all arms of this statement\n" . $herectx);
5197 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005198 }
5199 }
5200 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005201 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005202 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005203 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005204
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205 # Check the pre-context.
5206 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5207 #print "APW: ALLOWED: pre<$1>\n";
5208 $allowed = 1;
5209 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005210
5211 my ($level, $endln, @chunks) =
5212 ctx_statement_full($linenr, $realcnt, $-[0]);
5213
Andy Whitcroftcf655042008-03-04 14:28:20 -08005214 # Check the condition.
5215 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005216 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005217 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005218 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005219 }
5220 if (statement_lines($cond) > 1) {
5221 #print "APW: ALLOWED: cond<$cond>\n";
5222 $allowed = 1;
5223 }
5224 if ($block =~/\b(?:if|for|while)\b/) {
5225 #print "APW: ALLOWED: block<$block>\n";
5226 $allowed = 1;
5227 }
5228 if (statement_block_size($block) > 1) {
5229 #print "APW: ALLOWED: lines block<$block>\n";
5230 $allowed = 1;
5231 }
5232 # Check the post-context.
5233 if (defined $chunks[1]) {
5234 my ($cond, $block) = @{$chunks[1]};
5235 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005236 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005237 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005238 if ($block =~ /^\s*\{/) {
5239 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5240 $allowed = 1;
5241 }
5242 }
5243 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005244 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005245 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005246
Andy Whitcroftf0556632008-10-15 22:02:23 -07005247 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005248 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005249 }
5250
Joe Perches000d1cc12011-07-25 17:13:25 -07005251 WARN("BRACES",
5252 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005253 }
5254 }
5255
Joe Perches0979ae62012-12-17 16:01:59 -08005256# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005257 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005258 if (CHK("BRACES",
5259 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5260 $fix && $prevrawline =~ /^\+/) {
5261 fix_delete_line($fixlinenr - 1, $prevrawline);
5262 }
Joe Perches0979ae62012-12-17 16:01:59 -08005263 }
Joe Perches77b9a532013-07-03 15:05:29 -07005264 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005265 if (CHK("BRACES",
5266 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5267 $fix) {
5268 fix_delete_line($fixlinenr, $rawline);
5269 }
Joe Perches0979ae62012-12-17 16:01:59 -08005270 }
5271
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005272# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005273 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5274 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005275 WARN("VOLATILE",
5276 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005277 }
5278
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005279# Check for user-visible strings broken across lines, which breaks the ability
5280# to grep for the string. Make exceptions when the previous string ends in a
5281# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5282# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005283 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005284 $prevline =~ /"\s*$/ &&
5285 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5286 if (WARN("SPLIT_STRING",
5287 "quoted string split across lines\n" . $hereprev) &&
5288 $fix &&
5289 $prevrawline =~ /^\+.*"\s*$/ &&
5290 $last_coalesced_string_linenr != $linenr - 1) {
5291 my $extracted_string = get_quoted_string($line, $rawline);
5292 my $comma_close = "";
5293 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5294 $comma_close = $1;
5295 }
5296
5297 fix_delete_line($fixlinenr - 1, $prevrawline);
5298 fix_delete_line($fixlinenr, $rawline);
5299 my $fixedline = $prevrawline;
5300 $fixedline =~ s/"\s*$//;
5301 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5302 fix_insert_line($fixlinenr - 1, $fixedline);
5303 $fixedline = $rawline;
5304 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5305 if ($fixedline !~ /\+\s*$/) {
5306 fix_insert_line($fixlinenr, $fixedline);
5307 }
5308 $last_coalesced_string_linenr = $linenr;
5309 }
5310 }
5311
5312# check for missing a space in a string concatenation
5313 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5314 WARN('MISSING_SPACE',
5315 "break quoted strings at a space character\n" . $hereprev);
5316 }
5317
5318# check for spaces before a quoted newline
5319 if ($rawline =~ /^.*\".*\s\\n/) {
5320 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5321 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5322 $fix) {
5323 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5324 }
5325
5326 }
5327
Joe Perchesf17dba42014-10-13 15:51:51 -07005328# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005329 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005330 CHK("CONCATENATED_STRING",
5331 "Concatenated strings should use spaces between elements\n" . $herecurr);
5332 }
5333
Joe Perches90ad30e2014-12-10 15:51:59 -08005334# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005335 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005336 WARN("STRING_FRAGMENTS",
5337 "Consecutive strings are generally better as a single string\n" . $herecurr);
5338 }
5339
Joe Perches6e300752015-09-09 15:37:47 -07005340# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005341 my $string;
5342 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5343 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5344 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005345 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005346 WARN("PRINTF_L",
5347 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5348 last;
5349 }
Joe Perches6e300752015-09-09 15:37:47 -07005350 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5351 ERROR("PRINTF_0xDECIMAL",
5352 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5353 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005354 }
5355
5356# check for line continuations in quoted strings with odd counts of "
5357 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5358 WARN("LINE_CONTINUATIONS",
5359 "Avoid line continuations in quoted strings\n" . $herecurr);
5360 }
5361
Gregory Bean6c9c6842011-03-17 14:18:20 -07005362# sys_open/read/write/close are not allowed in the kernel
5363 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5364 ERROR("FILE_OPS",
5365 "$1 is inappropriate in kernel code.\n" .
5366 $herecurr);
5367 }
5368
Gregory Beana7cce0a2011-06-07 08:06:45 -07005369# filp_open is a backdoor for sys_open
5370 if ($line =~ /\b(filp_open)\b/) {
5371 ERROR("FILE_OPS",
5372 "$1 is inappropriate in kernel code.\n" .
5373 $herecurr);
5374 }
5375
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005376# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5377 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5378 ERROR("NON_RELAXED_IO",
5379 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5380 "with appropriate memory barriers instead.\n" .
5381 $herecurr);
5382 }
5383
5384# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5385 if ($line =~ /\b((in|out)([bwl]))\b/) {
5386 my ($all, $pref, $suf) = ($1, $2, $3);
5387 $pref =~ s/in/read/;
5388 $pref =~ s/out/write/;
5389 ERROR("NON_RELAXED_IO",
5390 "Use of $all is deprecated: use " .
5391 "__raw_$pref$suf\n\t" .
5392 "with appropriate memory barriers instead.\n" .
5393 $herecurr);
5394 }
5395
Gregory Bean438bf5f2011-06-15 09:32:38 -07005396# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005397 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005398 WARN("ARM_BARRIER",
5399 "Use of dsb is discouranged: prefer mb.\n" .
5400 $herecurr);
5401 }
5402
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005403# unbounded string functions are overflow risks
5404 my %str_fns = (
5405 "sprintf" => "snprintf",
5406 "strcpy" => "strlcpy",
5407 "strncpy" => "strlcpy",
5408 "strcat" => "strlcat",
5409 "strncat" => "strlcat",
5410 "vsprintf" => "vsnprintf",
5411 "strchr" => "strnchr",
5412 "strstr" => "strnstr",
5413 );
5414 foreach my $k (keys %str_fns) {
5415 if ($line =~ /\b$k\b/) {
5416 ERROR("UNBOUNDED_STRING_FNS",
5417 "Use of $k is deprecated: " .
5418 "use $str_fns{$k} instead.\n" .
5419 $herecurr);
5420 }
5421 }
5422
Andy Whitcroft00df3442007-06-08 13:47:06 -07005423# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005424 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005425 WARN("IF_0",
5426 "if this code is redundant consider removing it\n"
5427 . $herecurr);
5428 }
5429
5430# warn about #if 1
5431 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5432 WARN("IF_1",
5433 "if this code is required consider removing"
5434 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005435 }
5436
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005437# check for needless "if (<foo>) fn(<foo>)" uses
5438 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005439 my $tested = quotemeta($1);
5440 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5441 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5442 my $func = $1;
5443 if (WARN('NEEDLESS_IF',
5444 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5445 $fix) {
5446 my $do_fix = 1;
5447 my $leading_tabs = "";
5448 my $new_leading_tabs = "";
5449 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5450 $leading_tabs = $1;
5451 } else {
5452 $do_fix = 0;
5453 }
5454 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5455 $new_leading_tabs = $1;
5456 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5457 $do_fix = 0;
5458 }
5459 } else {
5460 $do_fix = 0;
5461 }
5462 if ($do_fix) {
5463 fix_delete_line($fixlinenr - 1, $prevrawline);
5464 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5465 }
5466 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005467 }
5468 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005469
Joe Perchesebfdc402014-08-06 16:10:27 -07005470# check for unnecessary "Out of Memory" messages
5471 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5472 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5473 (defined $1 || defined $3) &&
5474 $linenr > 3) {
5475 my $testval = $2;
5476 my $testline = $lines[$linenr - 3];
5477
5478 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5479# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5480
5481 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5482 WARN("OOM_MESSAGE",
5483 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5484 }
5485 }
5486
Joe Perchesf78d98f2014-10-13 15:52:01 -07005487# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005488 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005489 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5490 my $level = $1;
5491 if (WARN("UNNECESSARY_KERN_LEVEL",
5492 "Possible unnecessary $level\n" . $herecurr) &&
5493 $fix) {
5494 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5495 }
5496 }
5497
Joe Perchesabb08a52014-12-10 15:51:46 -08005498# check for mask then right shift without a parentheses
5499 if ($^V && $^V ge 5.10.0 &&
5500 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5501 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5502 WARN("MASK_THEN_SHIFT",
5503 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5504 }
5505
Joe Perchesb75ac612014-12-10 15:52:02 -08005506# check for pointer comparisons to NULL
5507 if ($^V && $^V ge 5.10.0) {
5508 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5509 my $val = $1;
5510 my $equal = "!";
5511 $equal = "" if ($4 eq "!=");
5512 if (CHK("COMPARISON_TO_NULL",
5513 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5514 $fix) {
5515 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5516 }
5517 }
5518 }
5519
Joe Perches8716de32013-09-11 14:24:05 -07005520# check for bad placement of section $InitAttribute (e.g.: __initdata)
5521 if ($line =~ /(\b$InitAttribute\b)/) {
5522 my $attr = $1;
5523 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5524 my $ptr = $1;
5525 my $var = $2;
5526 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5527 ERROR("MISPLACED_INIT",
5528 "$attr should be placed after $var\n" . $herecurr)) ||
5529 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5530 WARN("MISPLACED_INIT",
5531 "$attr should be placed after $var\n" . $herecurr))) &&
5532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005533 $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 -07005534 }
5535 }
5536 }
5537
Joe Perchese970b882013-11-12 15:10:10 -08005538# check for $InitAttributeData (ie: __initdata) with const
5539 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5540 my $attr = $1;
5541 $attr =~ /($InitAttributePrefix)(.*)/;
5542 my $attr_prefix = $1;
5543 my $attr_type = $2;
5544 if (ERROR("INIT_ATTRIBUTE",
5545 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5546 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005547 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005548 s/$InitAttributeData/${attr_prefix}initconst/;
5549 }
5550 }
5551
5552# check for $InitAttributeConst (ie: __initconst) without const
5553 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5554 my $attr = $1;
5555 if (ERROR("INIT_ATTRIBUTE",
5556 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5557 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005558 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005559 /(^\+\s*(?:static\s+))/;
5560 $lead = rtrim($1);
5561 $lead = "$lead " if ($lead !~ /^\+$/);
5562 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005563 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005564 }
5565 }
5566
Joe Perchesc17893c2015-04-16 12:44:42 -07005567# check for __read_mostly with const non-pointer (should just be const)
5568 if ($line =~ /\b__read_mostly\b/ &&
5569 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5570 if (ERROR("CONST_READ_MOSTLY",
5571 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5572 $fix) {
5573 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5574 }
5575 }
5576
Joe Perchesfbdb8132014-04-03 14:49:14 -07005577# don't use __constant_<foo> functions outside of include/uapi/
5578 if ($realfile !~ m@^include/uapi/@ &&
5579 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5580 my $constant_func = $1;
5581 my $func = $constant_func;
5582 $func =~ s/^__constant_//;
5583 if (WARN("CONSTANT_CONVERSION",
5584 "$constant_func should be $func\n" . $herecurr) &&
5585 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005586 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005587 }
5588 }
5589
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005590# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005591 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005592 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005593 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005594 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005595 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005596 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5597 }
5598 if ($delay > 2000) {
5599 WARN("LONG_UDELAY",
5600 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005601 }
5602 }
5603
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005604# warn about unexpectedly long msleep's
5605 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5606 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005607 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005608 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005609 }
5610 }
5611
Joe Perches36ec1932013-07-03 15:05:25 -07005612# check for comparisons of jiffies
5613 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5614 WARN("JIFFIES_COMPARISON",
5615 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5616 }
5617
Joe Perches9d7a34a2013-07-03 15:05:26 -07005618# check for comparisons of get_jiffies_64()
5619 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5620 WARN("JIFFIES_COMPARISON",
5621 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5622 }
5623
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005624# check the patch for use of mdelay
5625 if ($line =~ /\bmdelay\s*\(/) {
5626 WARN("MDELAY",
5627 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5628 }
5629
Andy Whitcroft00df3442007-06-08 13:47:06 -07005630# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005631# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005632# print "#ifdef in C files should be avoided\n";
5633# print "$herecurr";
5634# $clean = 0;
5635# }
5636
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005637# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005638 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005639 if (ERROR("SPACING",
5640 "exactly one space required after that #$1\n" . $herecurr) &&
5641 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005642 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005643 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5644 }
5645
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005646 }
5647
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005648# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005649 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5650 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005651 my $which = $1;
5652 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005653 CHK("UNCOMMENTED_DEFINITION",
5654 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005655 }
5656 }
5657# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005658
5659 my $barriers = qr{
5660 mb|
5661 rmb|
5662 wmb|
5663 read_barrier_depends
5664 }x;
5665 my $barrier_stems = qr{
5666 mb__before_atomic|
5667 mb__after_atomic|
5668 store_release|
5669 load_acquire|
5670 store_mb|
5671 (?:$barriers)
5672 }x;
5673 my $all_barriers = qr{
5674 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005675 smp_(?:$barrier_stems)|
5676 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005677 }x;
5678
5679 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005680 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005681 WARN("MEMORY_BARRIER",
5682 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005683 }
5684 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005685
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005686 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5687
5688 if ($realfile !~ m@^include/asm-generic/@ &&
5689 $realfile !~ m@/barrier\.h$@ &&
5690 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5691 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5692 WARN("MEMORY_BARRIER",
5693 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5694 }
5695
Joe Perchescb426e92015-06-25 15:02:46 -07005696# check for waitqueue_active without a comment.
5697 if ($line =~ /\bwaitqueue_active\s*\(/) {
5698 if (!ctx_has_comment($first_line, $linenr)) {
5699 WARN("WAITQUEUE_ACTIVE",
5700 "waitqueue_active without comment\n" . $herecurr);
5701 }
5702 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005703
5704# Check for expedited grace periods that interrupt non-idle non-nohz
5705# online CPUs. These expedited can therefore degrade real-time response
5706# if used carelessly, and should be avoided where not absolutely
5707# needed. It is always OK to use synchronize_rcu_expedited() and
5708# synchronize_sched_expedited() at boot time (before real-time applications
5709# start) and in error situations where real-time response is compromised in
5710# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5711# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5712# Of course, nothing comes for free, and srcu_read_lock() and
5713# srcu_read_unlock() do contain full memory barriers in payment for
5714# synchronize_srcu_expedited() non-interruption properties.
5715 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5716 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5717 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5718
5719 }
5720
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005721# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005722 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005723 CHK("ARCH_DEFINES",
5724 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005725 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005726
Tobias Klauserd4977c72010-05-24 14:33:30 -07005727# Check that the storage class is at the beginning of a declaration
5728 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005729 WARN("STORAGE_CLASS",
5730 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005731 }
5732
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005733# check the location of the inline attribute, that it is between
5734# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005735 if ($line =~ /\b$Type\s+$Inline\b/ ||
5736 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005737 ERROR("INLINE_LOCATION",
5738 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005739 }
5740
Andy Whitcroft8905a672007-11-28 16:21:06 -08005741# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005742 if ($realfile !~ m@\binclude/uapi/@ &&
5743 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005744 if (WARN("INLINE",
5745 "plain inline is preferred over $1\n" . $herecurr) &&
5746 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005747 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005748
5749 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005750 }
5751
Joe Perches3d130fd2011-01-12 17:00:00 -08005752# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005753 if ($realfile !~ m@\binclude/uapi/@ &&
5754 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005755 WARN("PREFER_PACKED",
5756 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005757 }
5758
Joe Perches39b7e282011-07-25 17:13:24 -07005759# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005760 if ($realfile !~ m@\binclude/uapi/@ &&
5761 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005762 WARN("PREFER_ALIGNED",
5763 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005764 }
5765
Joe Perches5f14d3b2012-01-10 15:09:52 -08005766# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005767 if ($realfile !~ m@\binclude/uapi/@ &&
5768 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005769 if (WARN("PREFER_PRINTF",
5770 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5771 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005772 $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 -07005773
5774 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005775 }
5776
Joe Perches6061d942012-03-23 15:02:16 -07005777# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005778 if ($realfile !~ m@\binclude/uapi/@ &&
5779 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005780 if (WARN("PREFER_SCANF",
5781 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005783 $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 -07005784 }
Joe Perches6061d942012-03-23 15:02:16 -07005785 }
5786
Joe Perches619a9082014-12-10 15:51:35 -08005787# Check for __attribute__ weak, or __weak declarations (may have link issues)
5788 if ($^V && $^V ge 5.10.0 &&
5789 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5790 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5791 $line =~ /\b__weak\b/)) {
5792 ERROR("WEAK_DECLARATION",
5793 "Using weak declarations can have unintended link defects\n" . $herecurr);
5794 }
5795
Joe Perchese6176fa2015-06-25 15:02:49 -07005796# check for c99 types like uint8_t used outside of uapi/
5797 if ($realfile !~ m@\binclude/uapi/@ &&
5798 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5799 my $type = $1;
5800 if ($type =~ /\b($typeC99Typedefs)\b/) {
5801 $type = $1;
5802 my $kernel_type = 'u';
5803 $kernel_type = 's' if ($type =~ /^_*[si]/);
5804 $type =~ /(\d+)/;
5805 $kernel_type .= $1;
5806 if (CHK("PREFER_KERNEL_TYPES",
5807 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5808 $fix) {
5809 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5810 }
5811 }
5812 }
5813
Joe Perches938224b2016-01-20 14:59:15 -08005814# check for cast of C90 native int or longer types constants
5815 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5816 my $cast = $1;
5817 my $const = $2;
5818 if (WARN("TYPECAST_INT_CONSTANT",
5819 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5820 $fix) {
5821 my $suffix = "";
5822 my $newconst = $const;
5823 $newconst =~ s/${Int_type}$//;
5824 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5825 if ($cast =~ /\blong\s+long\b/) {
5826 $suffix .= 'LL';
5827 } elsif ($cast =~ /\blong\b/) {
5828 $suffix .= 'L';
5829 }
5830 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5831 }
5832 }
5833
Joe Perches8f53a9b2010-03-05 13:43:48 -08005834# check for sizeof(&)
5835 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005836 WARN("SIZEOF_ADDRESS",
5837 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005838 }
5839
Joe Perches66c80b62012-07-30 14:41:22 -07005840# check for sizeof without parenthesis
5841 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005842 if (WARN("SIZEOF_PARENTHESIS",
5843 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5844 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005845 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005846 }
Joe Perches66c80b62012-07-30 14:41:22 -07005847 }
5848
Joe Perches88982fe2012-12-17 16:02:00 -08005849# check for struct spinlock declarations
5850 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5851 WARN("USE_SPINLOCK_T",
5852 "struct spinlock should be spinlock_t\n" . $herecurr);
5853 }
5854
Joe Perchesa6962d72013-04-29 16:18:13 -07005855# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005856 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005857 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005858 $fmt =~ s/%%//g;
5859 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005860 if (WARN("PREFER_SEQ_PUTS",
5861 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5862 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005863 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005864 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005865 }
5866 }
5867
Andy Whitcroft554e1652012-01-10 15:09:57 -08005868# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005869 if ($^V && $^V ge 5.10.0 &&
5870 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005871 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005872
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005873 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005874 my $ms_val = $7;
5875 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005876
Andy Whitcroft554e1652012-01-10 15:09:57 -08005877 if ($ms_size =~ /^(0x|)0$/i) {
5878 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005879 "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 -08005880 } elsif ($ms_size =~ /^(0x|)1$/i) {
5881 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005882 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5883 }
5884 }
5885
Joe Perches98a9bba2014-01-23 15:54:52 -08005886# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005887# if ($^V && $^V ge 5.10.0 &&
5888# defined $stat &&
5889# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5890# if (WARN("PREFER_ETHER_ADDR_COPY",
5891# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5892# $fix) {
5893# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5894# }
5895# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005896
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005897# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005898# if ($^V && $^V ge 5.10.0 &&
5899# defined $stat &&
5900# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5901# WARN("PREFER_ETHER_ADDR_EQUAL",
5902# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5903# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005904
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005905# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5906# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005907# if ($^V && $^V ge 5.10.0 &&
5908# defined $stat &&
5909# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5910#
5911# my $ms_val = $7;
5912#
5913# if ($ms_val =~ /^(?:0x|)0+$/i) {
5914# if (WARN("PREFER_ETH_ZERO_ADDR",
5915# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5916# $fix) {
5917# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5918# }
5919# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5920# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5921# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5922# $fix) {
5923# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5924# }
5925# }
5926# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005927
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005928# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005929 if ($^V && $^V ge 5.10.0 &&
5930 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005931 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005932 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005933 my $call = $1;
5934 my $cast1 = deparenthesize($2);
5935 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005936 my $cast2 = deparenthesize($7);
5937 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005938 my $cast;
5939
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005940 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005941 $cast = "$cast1 or $cast2";
5942 } elsif ($cast1 ne "") {
5943 $cast = $cast1;
5944 } else {
5945 $cast = $cast2;
5946 }
5947 WARN("MINMAX",
5948 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005949 }
5950 }
5951
Joe Perches4a273192012-07-30 14:41:20 -07005952# check usleep_range arguments
5953 if ($^V && $^V ge 5.10.0 &&
5954 defined $stat &&
5955 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5956 my $min = $1;
5957 my $max = $7;
5958 if ($min eq $max) {
5959 WARN("USLEEP_RANGE",
5960 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5961 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5962 $min > $max) {
5963 WARN("USLEEP_RANGE",
5964 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5965 }
5966 }
5967
Joe Perches823b7942013-11-12 15:10:15 -08005968# check for naked sscanf
5969 if ($^V && $^V ge 5.10.0 &&
5970 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005971 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005972 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5973 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5974 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5975 my $lc = $stat =~ tr@\n@@;
5976 $lc = $lc + $linenr;
5977 my $stat_real = raw_line($linenr, 0);
5978 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5979 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5980 }
5981 WARN("NAKED_SSCANF",
5982 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5983 }
5984
Joe Perchesafc819a2014-06-04 16:12:08 -07005985# check for simple sscanf that should be kstrto<foo>
5986 if ($^V && $^V ge 5.10.0 &&
5987 defined $stat &&
5988 $line =~ /\bsscanf\b/) {
5989 my $lc = $stat =~ tr@\n@@;
5990 $lc = $lc + $linenr;
5991 my $stat_real = raw_line($linenr, 0);
5992 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5993 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5994 }
5995 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5996 my $format = $6;
5997 my $count = $format =~ tr@%@%@;
5998 if ($count == 1 &&
5999 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6000 WARN("SSCANF_TO_KSTRTO",
6001 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6002 }
6003 }
6004 }
6005
Joe Perches70dc8a42013-09-11 14:23:58 -07006006# check for new externs in .h files.
6007 if ($realfile =~ /\.h$/ &&
6008 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006009 if (CHK("AVOID_EXTERNS",
6010 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006011 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006012 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006013 }
6014 }
6015
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006016# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006017 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006018 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006019 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006020 my $function_name = $1;
6021 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006022
6023 my $s = $stat;
6024 if (defined $cond) {
6025 substr($s, 0, length($cond), '');
6026 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006027 if ($s =~ /^\s*;/ &&
6028 $function_name ne 'uninitialized_var')
6029 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006030 WARN("AVOID_EXTERNS",
6031 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006032 }
6033
6034 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006035 WARN("FUNCTION_ARGUMENTS",
6036 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006037 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006038
6039 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6040 $stat =~ /^.\s*extern\s+/)
6041 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006042 WARN("AVOID_EXTERNS",
6043 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006044 }
6045
Joe Perchesca0d8922016-10-11 13:52:16 -07006046 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6047 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6048 $1 ne "void") {
6049 my $args = trim($1);
6050 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6051 my $arg = trim($1);
6052 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6053 WARN("FUNCTION_ARGUMENTS",
6054 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6055 }
6056 }
6057 }
6058
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006059# checks for new __setup's
6060 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6061 my $name = $1;
6062
6063 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006064 CHK("UNDOCUMENTED_SETUP",
6065 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006066 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006067 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006068
6069# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006070 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006071 WARN("UNNECESSARY_CASTS",
6072 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006073 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006074
Joe Perchesa640d252013-07-03 15:05:21 -07006075# alloc style
6076# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6077 if ($^V && $^V ge 5.10.0 &&
6078 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6079 CHK("ALLOC_SIZEOF_STRUCT",
6080 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6081 }
6082
Joe Perches60a55362014-06-04 16:12:07 -07006083# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6084 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006085 $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 -07006086 my $oldfunc = $3;
6087 my $a1 = $4;
6088 my $a2 = $10;
6089 my $newfunc = "kmalloc_array";
6090 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006091 my $r1 = $a1;
6092 my $r2 = $a2;
6093 if ($a1 =~ /^sizeof\s*\S/) {
6094 $r1 = $a2;
6095 $r2 = $a1;
6096 }
6097 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6098 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006099 if (WARN("ALLOC_WITH_MULTIPLY",
6100 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6101 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006102 $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 -07006103
6104 }
6105 }
6106 }
6107
Joe Perches972fdea2013-04-29 16:18:12 -07006108# check for krealloc arg reuse
6109 if ($^V && $^V ge 5.10.0 &&
6110 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6111 WARN("KREALLOC_ARG_REUSE",
6112 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6113 }
6114
Joe Perches5ce59ae2013-02-21 16:44:18 -08006115# check for alloc argument mismatch
6116 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6117 WARN("ALLOC_ARRAY_ARGS",
6118 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6119 }
6120
Joe Perchescaf2a542011-01-12 16:59:56 -08006121# check for multiple semicolons
6122 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006123 if (WARN("ONE_SEMICOLON",
6124 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6125 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006126 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006127 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006128 }
6129
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006130# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6131 if ($realfile !~ m@^include/uapi/@ &&
6132 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006133 my $ull = "";
6134 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6135 if (CHK("BIT_MACRO",
6136 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6137 $fix) {
6138 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6139 }
6140 }
6141
Joe Perches2d632742016-05-20 17:04:00 -07006142# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6143 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6144 my $config = $1;
6145 if (WARN("PREFER_IS_ENABLED",
6146 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6147 $fix) {
6148 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6149 }
6150 }
6151
Joe Perchese81f2392014-08-06 16:11:25 -07006152# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006153 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6154 my $has_break = 0;
6155 my $has_statement = 0;
6156 my $count = 0;
6157 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006158 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006159 $prevline--;
6160 my $rline = $rawlines[$prevline - 1];
6161 my $fline = $lines[$prevline - 1];
6162 last if ($fline =~ /^\@\@/);
6163 next if ($fline =~ /^\-/);
6164 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6165 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6166 next if ($fline =~ /^.[\s$;]*$/);
6167 $has_statement = 1;
6168 $count++;
6169 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6170 }
6171 if (!$has_break && $has_statement) {
6172 WARN("MISSING_BREAK",
6173 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6174 }
6175 }
6176
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006177# check for switch/default statements without a break;
6178 if ($^V && $^V ge 5.10.0 &&
6179 defined $stat &&
6180 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6181 my $ctx = '';
6182 my $herectx = $here . "\n";
6183 my $cnt = statement_rawlines($stat);
6184 for (my $n = 0; $n < $cnt; $n++) {
6185 $herectx .= raw_line($linenr, $n) . "\n";
6186 }
6187 WARN("DEFAULT_NO_BREAK",
6188 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006189 }
6190
Patrick Pannutof370e252010-07-23 13:34:18 -07006191# check for return codes on error paths
6192 if ($line =~ /\breturn\s+-\d+/) {
6193 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006194 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006195 }
6196
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006197# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006198 if ($line =~ /\b__FUNCTION__\b/) {
6199 if (WARN("USE_FUNC",
6200 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6201 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006202 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006203 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006204 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006205
Joe Perches62ec8182015-02-13 14:38:18 -08006206# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6207 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6208 ERROR("DATE_TIME",
6209 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6210 }
6211
Joe Perches2c924882012-03-23 15:02:20 -07006212# check for use of yield()
6213 if ($line =~ /\byield\s*\(\s*\)/) {
6214 WARN("YIELD",
6215 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6216 }
6217
Joe Perches179f8f42013-07-03 15:05:30 -07006218# check for comparisons against true and false
6219 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6220 my $lead = $1;
6221 my $arg = $2;
6222 my $test = $3;
6223 my $otype = $4;
6224 my $trail = $5;
6225 my $op = "!";
6226
6227 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6228
6229 my $type = lc($otype);
6230 if ($type =~ /^(?:true|false)$/) {
6231 if (("$test" eq "==" && "$type" eq "true") ||
6232 ("$test" eq "!=" && "$type" eq "false")) {
6233 $op = "";
6234 }
6235
6236 CHK("BOOL_COMPARISON",
6237 "Using comparison to $otype is error prone\n" . $herecurr);
6238
6239## maybe suggesting a correct construct would better
6240## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6241
6242 }
6243 }
6244
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006245# check for semaphores initialized locked
6246 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006247 WARN("CONSIDER_COMPLETION",
6248 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006249 }
Joe Perches6712d852012-03-23 15:02:20 -07006250
Joe Perches67d0a072011-10-31 17:13:10 -07006251# recommend kstrto* over simple_strto* and strict_strto*
6252 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006253 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006254 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006255 }
Joe Perches6712d852012-03-23 15:02:20 -07006256
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006257# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006258 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006259 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006260 "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 -07006261 }
Joe Perches6712d852012-03-23 15:02:20 -07006262
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006263# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006264 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006265 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006266 WARN("CONST_STRUCT",
6267 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006268 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006269 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006270
6271# use of NR_CPUS is usually wrong
6272# ignore definitions of NR_CPUS and usage to define arrays as likely right
6273 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006274 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6275 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006276 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6277 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6278 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006279 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006280 WARN("NR_CPUS",
6281 "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 -07006282 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006283
Joe Perches52ea8502013-11-12 15:10:09 -08006284# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6285 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6286 ERROR("DEFINE_ARCH_HAS",
6287 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6288 }
6289
Joe Perchesacd93622015-02-13 14:38:38 -08006290# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6291 if ($^V && $^V ge 5.10.0 &&
6292 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6293 WARN("LIKELY_MISUSE",
6294 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6295 }
6296
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006297# whine mightly about in_atomic
6298 if ($line =~ /\bin_atomic\s*\(/) {
6299 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006300 ERROR("IN_ATOMIC",
6301 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006302 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006303 WARN("IN_ATOMIC",
6304 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006305 }
6306 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006307
Joe Perches481aea52016-05-20 17:04:08 -07006308# whine about ACCESS_ONCE
6309 if ($^V && $^V ge 5.10.0 &&
6310 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6311 my $par = $1;
6312 my $eq = $2;
6313 my $fun = $3;
6314 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6315 if (defined($eq)) {
6316 if (WARN("PREFER_WRITE_ONCE",
6317 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6318 $fix) {
6319 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6320 }
6321 } else {
6322 if (WARN("PREFER_READ_ONCE",
6323 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6324 $fix) {
6325 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6326 }
6327 }
6328 }
6329
Peter Zijlstra1704f472010-03-19 01:37:42 +01006330# check for lockdep_set_novalidate_class
6331 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6332 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6333 if ($realfile !~ m@^kernel/lockdep@ &&
6334 $realfile !~ m@^include/linux/lockdep@ &&
6335 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006336 ERROR("LOCKDEP",
6337 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006338 }
6339 }
Dave Jones88f88312011-01-12 16:59:59 -08006340
Joe Perchesb392c642015-04-16 12:44:16 -07006341 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6342 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006343 WARN("EXPORTED_WORLD_WRITABLE",
6344 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006345 }
Joe Perches24358802014-04-03 14:49:13 -07006346
Joe Perches515a2352014-04-03 14:49:24 -07006347# Mode permission misuses where it seems decimal should be octal
6348# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6349 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006350 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006351 $line =~ /$mode_perms_search/) {
6352 foreach my $entry (@mode_permission_funcs) {
6353 my $func = $entry->[0];
6354 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006355
Joe Perches459cf0a2016-10-11 13:52:19 -07006356 my $lc = $stat =~ tr@\n@@;
6357 $lc = $lc + $linenr;
6358 my $stat_real = raw_line($linenr, 0);
6359 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6360 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6361 }
6362
Joe Perches515a2352014-04-03 14:49:24 -07006363 my $skip_args = "";
6364 if ($arg_pos > 1) {
6365 $arg_pos--;
6366 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6367 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006368 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006369 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006370 my $val = $1;
6371 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006372 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6373 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006374 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006375 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006376 }
6377 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006378 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006379 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006380 }
Joe Perches24358802014-04-03 14:49:13 -07006381 }
6382 }
6383 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006384
Joe Perches459cf0a2016-10-11 13:52:19 -07006385# check for uses of S_<PERMS> that could be octal for readability
6386 if ($line =~ /\b$mode_perms_string_search\b/) {
6387 my $val = "";
6388 my $oval = "";
6389 my $to = 0;
6390 my $curpos = 0;
6391 my $lastpos = 0;
6392 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6393 $curpos = pos($line);
6394 my $match = $2;
6395 my $omatch = $1;
6396 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6397 $lastpos = $curpos;
6398 $to |= $mode_permission_string_types{$match};
6399 $val .= '\s*\|\s*' if ($val ne "");
6400 $val .= $match;
6401 $oval .= $omatch;
6402 }
6403 $oval =~ s/^\s*\|\s*//;
6404 $oval =~ s/\s*\|\s*$//;
6405 my $octal = sprintf("%04o", $to);
6406 if (WARN("SYMBOLIC_PERMS",
6407 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6408 $fix) {
6409 $fixed[$fixlinenr] =~ s/$val/$octal/;
6410 }
6411 }
6412
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006413# validate content of MODULE_LICENSE against list from include/linux/module.h
6414 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6415 my $extracted_string = get_quoted_string($line, $rawline);
6416 my $valid_licenses = qr{
6417 GPL|
6418 GPL\ v2|
6419 GPL\ and\ additional\ rights|
6420 Dual\ BSD/GPL|
6421 Dual\ MIT/GPL|
6422 Dual\ MPL/GPL|
6423 Proprietary
6424 }x;
6425 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6426 WARN("MODULE_LICENSE",
6427 "unknown module license " . $extracted_string . "\n" . $herecurr);
6428 }
6429 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006430 }
6431
Maya Erezec77bd62017-02-26 09:14:43 +02006432 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6433 WARN("BAD_SIGN_OFF",
6434 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6435 }
6436
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006437 # If we have no input at all, then there is nothing to report on
6438 # so just keep quiet.
6439 if ($#rawlines == -1) {
6440 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006441 }
6442
Andy Whitcroft8905a672007-11-28 16:21:06 -08006443 # In mailback mode only produce a report in the negative, for
6444 # things that appear to be patches.
6445 if ($mailback && ($clean == 1 || !$is_patch)) {
6446 exit(0);
6447 }
6448
6449 # This is not a patch, and we are are in 'no-patch' mode so
6450 # just keep quiet.
6451 if (!$chk_patch && !$is_patch) {
6452 exit(0);
6453 }
6454
Joe Perches06330fc2015-06-25 15:03:11 -07006455 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006456 ERROR("NOT_UNIFIED_DIFF",
6457 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006458 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006459 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006460 ERROR("MISSING_SIGN_OFF",
6461 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006462 }
6463
Andy Whitcroft8905a672007-11-28 16:21:06 -08006464 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006465 if ($summary && !($clean == 1 && $quiet == 1)) {
6466 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006467 print "total: $cnt_error errors, $cnt_warn warnings, " .
6468 (($check)? "$cnt_chk checks, " : "") .
6469 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006470 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006471
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006472 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006473 # If there were any defects found and not already fixing them
6474 if (!$clean and !$fix) {
6475 print << "EOM"
6476
6477NOTE: For some of the reported defects, checkpatch may be able to
6478 mechanically convert to the typical style using --fix or --fix-inplace.
6479EOM
6480 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006481 # If there were whitespace errors which cleanpatch can fix
6482 # then suggest that.
6483 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006484 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006485 print << "EOM"
6486
6487NOTE: Whitespace errors detected.
6488 You may wish to use scripts/cleanpatch or scripts/cleanfile
6489EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006490 }
6491 }
6492
Joe Perchesd752fcc2014-08-06 16:11:05 -07006493 if ($clean == 0 && $fix &&
6494 ("@rawlines" ne "@fixed" ||
6495 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006496 my $newfile = $filename;
6497 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006498 my $linecount = 0;
6499 my $f;
6500
Joe Perchesd752fcc2014-08-06 16:11:05 -07006501 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6502
Joe Perches3705ce52013-07-03 15:05:31 -07006503 open($f, '>', $newfile)
6504 or die "$P: Can't open $newfile for write\n";
6505 foreach my $fixed_line (@fixed) {
6506 $linecount++;
6507 if ($file) {
6508 if ($linecount > 3) {
6509 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006510 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006511 }
6512 } else {
6513 print $f $fixed_line . "\n";
6514 }
6515 }
6516 close($f);
6517
6518 if (!$quiet) {
6519 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006520
Joe Perches3705ce52013-07-03 15:05:31 -07006521Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6522
6523Do _NOT_ trust the results written to this file.
6524Do _NOT_ submit these changes without inspecting them for correctness.
6525
6526This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6527No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006528EOM
6529 }
6530 }
6531
Joe Perchesd8469f12015-06-25 15:03:00 -07006532 if ($quiet == 0) {
6533 print "\n";
6534 if ($clean == 1) {
6535 print "$vname has no obvious style problems and is ready for submission.\n";
6536 } else {
6537 print "$vname has style problems, please review.\n";
6538 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006539 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006540 return $clean;
6541}