blob: a0d45ef93030cd0e5e92620a74134f1f38f525fe [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
David Keitel72bf3e82016-03-23 20:52:44 -070014use constant BEFORE_SHORTTEXT => 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -080015use constant IN_SHORTTEXT_BLANKLINE => 1;
16use constant IN_SHORTTEXT => 2;
17use constant AFTER_SHORTTEXT => 3;
18use constant CHECK_NEXT_SHORTTEXT => 4;
David Keitel72bf3e82016-03-23 20:52:44 -070019use constant SHORTTEXT_LIMIT => 75;
20
Andy Whitcroft0a920b52007-06-01 00:46:48 -070021my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080022my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070023
Joe Perches000d1cc12011-07-25 17:13:25 -070024my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070025
26use Getopt::Long qw(:config no_auto_abbrev);
27
28my $quiet = 0;
29my $tree = 1;
30my $chk_signoff = 1;
31my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080032my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070033my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070036my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070038my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070039my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070040my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070041my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080042my $summary = 1;
43my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070045my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070046my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070047my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080048my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070049my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080050my %debug;
Joe Perches34456862013-07-03 15:05:34 -070051my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070052my %use_type = ();
53my @use = ();
54my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070055my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070056my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070057my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080058my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070059my $ignore_perl_version = 0;
60my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070061my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070062my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070063my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070064my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070065my $conststructsfile = "$D/const_structs.checkpatch";
Joe Perches57230292015-06-25 15:03:03 -070066my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070067my $allow_c99_comments = 1;
Maya Erezec77bd62017-02-26 09:14:43 +020068my $qca_sign_off = 0;
69my $codeaurora_sign_off = 0;
Hannes Eder77f5b102009-09-21 17:04:37 -070070
71sub help {
72 my ($exitcode) = @_;
73
74 print << "EOM";
75Usage: $P [OPTION]... [FILE]...
76Version: $V
77
78Options:
79 -q, --quiet quiet
80 --no-tree run without a kernel tree
81 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080082 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070083 --patch treat FILE as patchfile (default)
84 --emacs emacs compile window format
85 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070086 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070087 -g, --git treat FILE as a single commit or git revision range
88 single git commit with:
89 <rev>
90 <rev>^
91 <rev>~n
92 multiple git commits with:
93 <rev1>..<rev2>
94 <rev1>...<rev2>
95 <rev>-<count>
96 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070097 -f, --file treat FILE as regular source file
98 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070099 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -0700100 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -0700101 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -0700102 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -0800103 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700104 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
111 is all off)
112 --test-only=WORD report only warnings/errors containing WORD
113 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
118 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700122 --ignore-perl-version override checking of perl version. expect
123 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700124 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700125 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700126 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700127 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700128 -h, --help, --version display this help and exit
129
130When FILE is - read standard input.
131EOM
132
133 exit($exitcode);
134}
135
Joe Perches3beb42e2016-05-20 17:04:14 -0700136sub uniq {
137 my %seen;
138 return grep { !$seen{$_}++ } @_;
139}
140
141sub list_types {
142 my ($exitcode) = @_;
143
144 my $count = 0;
145
146 local $/ = undef;
147
148 open(my $script, '<', abs_path($P)) or
149 die "$P: Can't read '$P' $!\n";
150
151 my $text = <$script>;
152 close($script);
153
154 my @types = ();
155 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
156 push (@types, $_);
157 }
158 @types = sort(uniq(@types));
159 print("#\tMessage type\n\n");
160 foreach my $type (@types) {
161 print(++$count . "\t" . $type . "\n");
162 }
163
164 exit($exitcode);
165}
166
Joe Perches000d1cc12011-07-25 17:13:25 -0700167my $conf = which_conf($configuration_file);
168if (-f $conf) {
169 my @conf_args;
170 open(my $conffile, '<', "$conf")
171 or warn "$P: Can't find a readable $configuration_file file $!\n";
172
173 while (<$conffile>) {
174 my $line = $_;
175
176 $line =~ s/\s*\n?$//g;
177 $line =~ s/^\s*//g;
178 $line =~ s/\s+/ /g;
179
180 next if ($line =~ m/^\s*#/);
181 next if ($line =~ m/^\s*$/);
182
183 my @words = split(" ", $line);
184 foreach my $word (@words) {
185 last if ($word =~ m/^#/);
186 push (@conf_args, $word);
187 }
188 }
189 close($conffile);
190 unshift(@ARGV, @conf_args) if @conf_args;
191}
192
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700193GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700195 'tree!' => \$tree,
196 'signoff!' => \$chk_signoff,
197 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800198 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800200 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700201 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700202 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700203 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 'subjective!' => \$check,
205 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700206 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700207 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700208 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700209 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800210 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700211 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213 'summary!' => \$summary,
214 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800215 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700216 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800217 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700218 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700220 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700221 'codespell!' => \$codespell,
222 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700223 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700224 'h|help' => \$help,
225 'version' => \$help
226) or help(1);
227
228help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700229
Joe Perches3beb42e2016-05-20 17:04:14 -0700230list_types(0) if ($list_types);
231
Joe Perches9624b8d2014-01-23 15:54:44 -0800232$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700233$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800234
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700235my $exit = 0;
236
Dave Hansend62a2012013-09-11 14:23:56 -0700237if ($^V && $^V lt $minimum_perl_version) {
238 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
239 if (!$ignore_perl_version) {
240 exit(1);
241 }
242}
243
Allen Hubbe45107ff2016-08-02 14:04:47 -0700244#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700245if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700246 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700247}
248
Joe Perches91bfe482013-09-11 14:23:59 -0700249sub hash_save_array_words {
250 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700251
Joe Perches91bfe482013-09-11 14:23:59 -0700252 my @array = split(/,/, join(',', @$arrayRef));
253 foreach my $word (@array) {
254 $word =~ s/\s*\n?$//g;
255 $word =~ s/^\s*//g;
256 $word =~ s/\s+/ /g;
257 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700258
Joe Perches91bfe482013-09-11 14:23:59 -0700259 next if ($word =~ m/^\s*#/);
260 next if ($word =~ m/^\s*$/);
261
262 $hashRef->{$word}++;
263 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700264}
265
Joe Perches91bfe482013-09-11 14:23:59 -0700266sub hash_show_words {
267 my ($hashRef, $prefix) = @_;
268
Joe Perches3c816e42015-06-25 15:03:29 -0700269 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700270 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700271 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700272 print " $word";
273 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700274 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700275 }
276}
277
278hash_save_array_words(\%ignore_type, \@ignore);
279hash_save_array_words(\%use_type, \@use);
280
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281my $dbg_values = 0;
282my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700283my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700284my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800285for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800286 ## no critic
287 eval "\${dbg_$key} = '$debug{$key}';";
288 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800289}
290
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700291my $rpt_cleaners = 0;
292
Andy Whitcroft8905a672007-11-28 16:21:06 -0800293if ($terse) {
294 $emacs = 1;
295 $quiet++;
296}
297
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700298if ($tree) {
299 if (defined $root) {
300 if (!top_of_kernel_tree($root)) {
301 die "$P: $root: --root does not point at a valid tree\n";
302 }
303 } else {
304 if (top_of_kernel_tree('.')) {
305 $root = '.';
306 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
307 top_of_kernel_tree($1)) {
308 $root = $1;
309 }
310 }
311
312 if (!defined $root) {
313 print "Must be run from the top-level dir. of a kernel tree\n";
314 exit(2);
315 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700316}
317
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700318my $emitted_corrupt = 0;
319
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700320our $Ident = qr{
321 [A-Za-z_][A-Za-z\d_]*
322 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
323 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700324our $Storage = qr{extern|static|asmlinkage};
325our $Sparse = qr{
326 __user|
327 __kernel|
328 __force|
329 __iomem|
330 __must_check|
331 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800332 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700333 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800334 __rcu|
335 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700336 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800337our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
338our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
339our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
340our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
341our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700342
Wolfram Sang52131292010-03-05 13:43:51 -0800343# Notes to $Attribute:
344# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700345our $Attribute = qr{
346 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700347 __percpu|
348 __nocast|
349 __safe|
350 __bitwise__|
351 __packed__|
352 __packed2__|
353 __naked|
354 __maybe_unused|
355 __always_unused|
356 __noreturn|
357 __used|
358 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800359 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700360 __noclone|
361 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700362 __read_mostly|
363 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700364 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700365 ____cacheline_aligned|
366 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800367 ____cacheline_internodealigned_in_smp|
368 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700369 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700370our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700371our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700372our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
373our $Lval = qr{$Ident(?:$Member)*};
374
Joe Perches95e2c602013-07-03 15:05:20 -0700375our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
376our $Binary = qr{(?i)0b[01]+$Int_type?};
377our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
378our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700379our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800380our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800381our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
382our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
383our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800384our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700385our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800386our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700387our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700388our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389our $Operators = qr{
390 <=|>=|==|!=|
391 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700392 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700393 }x;
394
Joe Perches91cb5192014-04-03 14:49:32 -0700395our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
396
Joe Perchesab7e23f2015-04-16 12:44:22 -0700397our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700399our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700400our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700402our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800403our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700404our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405
Joe Perches15662b32011-10-31 17:13:12 -0700406our $NON_ASCII_UTF8 = qr{
407 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700408 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
409 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
410 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
411 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
412 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
413 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
414}x;
415
Joe Perches15662b32011-10-31 17:13:12 -0700416our $UTF8 = qr{
417 [\x09\x0A\x0D\x20-\x7E] # ASCII
418 | $NON_ASCII_UTF8
419}x;
420
Joe Perchese6176fa2015-06-25 15:02:49 -0700421our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800422our $typeOtherOSTypedefs = qr{(?x:
423 u_(?:char|short|int|long) | # bsd
424 u(?:nchar|short|int|long) # sysv
425)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700426our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700427 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700428 atomic_t
429)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700430our $typeTypedefs = qr{(?x:
431 $typeC99Typedefs\b|
432 $typeOtherOSTypedefs\b|
433 $typeKernelTypedefs\b
434)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700435
Joe Perches6d32f7a2015-11-06 16:31:37 -0800436our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
437
Joe Perches691e6692010-03-05 13:43:51 -0800438our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700439 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700440 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700441 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700442 panic|
Joe Perches06668722013-11-12 15:10:07 -0800443 MODULE_[A-Z_]+|
444 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800445)};
446
Joe Perches20112472011-07-25 17:13:23 -0700447our $signature_tags = qr{(?xi:
448 Signed-off-by:|
449 Acked-by:|
450 Tested-by:|
451 Reviewed-by:|
452 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700453 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700454 To:|
455 Cc:
456)};
457
Joe Perches18130872014-08-06 16:11:22 -0700458our @typeListMisordered = (
459 qr{char\s+(?:un)?signed},
460 qr{int\s+(?:(?:un)?signed\s+)?short\s},
461 qr{int\s+short(?:\s+(?:un)?signed)},
462 qr{short\s+int(?:\s+(?:un)?signed)},
463 qr{(?:un)?signed\s+int\s+short},
464 qr{short\s+(?:un)?signed},
465 qr{long\s+int\s+(?:un)?signed},
466 qr{int\s+long\s+(?:un)?signed},
467 qr{long\s+(?:un)?signed\s+int},
468 qr{int\s+(?:un)?signed\s+long},
469 qr{int\s+(?:un)?signed},
470 qr{int\s+long\s+long\s+(?:un)?signed},
471 qr{long\s+long\s+int\s+(?:un)?signed},
472 qr{long\s+long\s+(?:un)?signed\s+int},
473 qr{long\s+long\s+(?:un)?signed},
474 qr{long\s+(?:un)?signed},
475);
476
Andy Whitcroft8905a672007-11-28 16:21:06 -0800477our @typeList = (
478 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700479 qr{(?:(?:un)?signed\s+)?char},
480 qr{(?:(?:un)?signed\s+)?short\s+int},
481 qr{(?:(?:un)?signed\s+)?short},
482 qr{(?:(?:un)?signed\s+)?int},
483 qr{(?:(?:un)?signed\s+)?long\s+int},
484 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
485 qr{(?:(?:un)?signed\s+)?long\s+long},
486 qr{(?:(?:un)?signed\s+)?long},
487 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488 qr{float},
489 qr{double},
490 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800491 qr{struct\s+$Ident},
492 qr{union\s+$Ident},
493 qr{enum\s+$Ident},
494 qr{${Ident}_t},
495 qr{${Ident}_handler},
496 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700497 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800498);
Joe Perches938224b2016-01-20 14:59:15 -0800499
500our $C90_int_types = qr{(?x:
501 long\s+long\s+int\s+(?:un)?signed|
502 long\s+long\s+(?:un)?signed\s+int|
503 long\s+long\s+(?:un)?signed|
504 (?:(?:un)?signed\s+)?long\s+long\s+int|
505 (?:(?:un)?signed\s+)?long\s+long|
506 int\s+long\s+long\s+(?:un)?signed|
507 int\s+(?:(?:un)?signed\s+)?long\s+long|
508
509 long\s+int\s+(?:un)?signed|
510 long\s+(?:un)?signed\s+int|
511 long\s+(?:un)?signed|
512 (?:(?:un)?signed\s+)?long\s+int|
513 (?:(?:un)?signed\s+)?long|
514 int\s+long\s+(?:un)?signed|
515 int\s+(?:(?:un)?signed\s+)?long|
516
517 int\s+(?:un)?signed|
518 (?:(?:un)?signed\s+)?int
519)};
520
Alex Dowad485ff232015-06-25 15:02:52 -0700521our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700522our @typeListWithAttr = (
523 @typeList,
524 qr{struct\s+$InitAttribute\s+$Ident},
525 qr{union\s+$InitAttribute\s+$Ident},
526);
527
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700528our @modifierList = (
529 qr{fastcall},
530);
Alex Dowad485ff232015-06-25 15:02:52 -0700531our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800532
Joe Perches24358802014-04-03 14:49:13 -0700533our @mode_permission_funcs = (
534 ["module_param", 3],
535 ["module_param_(?:array|named|string)", 4],
536 ["module_param_array_named", 5],
537 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
538 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700539 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
540 ["IIO_DEV_ATTR_[A-Z_]+", 1],
541 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
542 ["SENSOR_TEMPLATE(?:_2|)", 3],
543 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700544);
545
Joe Perches515a2352014-04-03 14:49:24 -0700546#Create a search pattern for all these functions to speed up a loop below
547our $mode_perms_search = "";
548foreach my $entry (@mode_permission_funcs) {
549 $mode_perms_search .= '|' if ($mode_perms_search ne "");
550 $mode_perms_search .= $entry->[0];
551}
552
Joe Perchesb392c642015-04-16 12:44:16 -0700553our $mode_perms_world_writable = qr{
554 S_IWUGO |
555 S_IWOTH |
556 S_IRWXUGO |
557 S_IALLUGO |
558 0[0-7][0-7][2367]
559}x;
560
Joe Perchesf90774e2016-10-11 13:51:47 -0700561our %mode_permission_string_types = (
562 "S_IRWXU" => 0700,
563 "S_IRUSR" => 0400,
564 "S_IWUSR" => 0200,
565 "S_IXUSR" => 0100,
566 "S_IRWXG" => 0070,
567 "S_IRGRP" => 0040,
568 "S_IWGRP" => 0020,
569 "S_IXGRP" => 0010,
570 "S_IRWXO" => 0007,
571 "S_IROTH" => 0004,
572 "S_IWOTH" => 0002,
573 "S_IXOTH" => 0001,
574 "S_IRWXUGO" => 0777,
575 "S_IRUGO" => 0444,
576 "S_IWUGO" => 0222,
577 "S_IXUGO" => 0111,
578);
579
580#Create a search pattern for all these strings to speed up a loop below
581our $mode_perms_string_search = "";
582foreach my $entry (keys %mode_permission_string_types) {
583 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
584 $mode_perms_string_search .= $entry;
585}
586
Wolfram Sang7840a942010-08-09 17:20:57 -0700587our $allowed_asm_includes = qr{(?x:
588 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700589 memory|
590 time|
591 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700592)};
593# memory.h: ARM has a custom one
594
Kees Cook66b47b42014-10-13 15:51:57 -0700595# Load common spelling mistakes and build regular expression list.
596my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700597my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700598
Joe Perches36061e32014-12-10 15:51:43 -0800599if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800600 while (<$spelling>) {
601 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700602
Joe Perches36061e32014-12-10 15:51:43 -0800603 $line =~ s/\s*\n?$//g;
604 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700605
Joe Perches36061e32014-12-10 15:51:43 -0800606 next if ($line =~ m/^\s*#/);
607 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700608
Joe Perches36061e32014-12-10 15:51:43 -0800609 my ($suspect, $fix) = split(/\|\|/, $line);
610
Joe Perches36061e32014-12-10 15:51:43 -0800611 $spelling_fix{$suspect} = $fix;
612 }
613 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800614} else {
615 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700616}
Kees Cook66b47b42014-10-13 15:51:57 -0700617
Joe Perchesebfd7d62015-04-16 12:44:14 -0700618if ($codespell) {
619 if (open(my $spelling, '<', $codespellfile)) {
620 while (<$spelling>) {
621 my $line = $_;
622
623 $line =~ s/\s*\n?$//g;
624 $line =~ s/^\s*//g;
625
626 next if ($line =~ m/^\s*#/);
627 next if ($line =~ m/^\s*$/);
628 next if ($line =~ m/, disabled/i);
629
630 $line =~ s/,.*$//;
631
632 my ($suspect, $fix) = split(/->/, $line);
633
634 $spelling_fix{$suspect} = $fix;
635 }
636 close($spelling);
637 } else {
638 warn "No codespell typos will be found - file '$codespellfile': $!\n";
639 }
640}
641
642$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
643
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700644my $const_structs = "";
645if (open(my $conststructs, '<', $conststructsfile)) {
646 while (<$conststructs>) {
647 my $line = $_;
648
649 $line =~ s/\s*\n?$//g;
650 $line =~ s/^\s*//g;
651
652 next if ($line =~ m/^\s*#/);
653 next if ($line =~ m/^\s*$/);
654 if ($line =~ /\s/) {
655 print("$conststructsfile: '$line' invalid - ignored\n");
656 next;
657 }
658
659 $const_structs .= '|' if ($const_structs ne "");
660 $const_structs .= $line;
661 }
662 close($conststructsfile);
663} else {
664 warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
665}
666
Andy Whitcroft8905a672007-11-28 16:21:06 -0800667sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700668 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
669 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700670 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700671 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700672 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700673 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700674 (?:$typeTypedefs\b)|
675 (?:${all}\b)
676 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800677 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700678 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800679 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800680 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700681 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700682 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800683 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700684 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800685 }x;
Joe Perches18130872014-08-06 16:11:22 -0700686 $NonptrTypeMisordered = qr{
687 (?:$Modifier\s+|const\s+)*
688 (?:
689 (?:${Misordered}\b)
690 )
691 (?:\s+$Modifier|\s+const)*
692 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700693 $NonptrTypeWithAttr = qr{
694 (?:$Modifier\s+|const\s+)*
695 (?:
696 (?:typeof|__typeof__)\s*\([^\)]*\)|
697 (?:$typeTypedefs\b)|
698 (?:${allWithAttr}\b)
699 )
700 (?:\s+$Modifier|\s+const)*
701 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800702 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700703 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700704 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700705 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800706 }x;
Joe Perches18130872014-08-06 16:11:22 -0700707 $TypeMisordered = qr{
708 $NonptrTypeMisordered
709 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
710 (?:\s+$Inline|\s+$Modifier)*
711 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700712 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700713 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800714}
715build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700716
Joe Perches7d2367a2011-07-25 17:13:22 -0700717our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700718
719# Using $balanced_parens, $LvalOrFunc, or $FuncArg
720# requires at least perl version v5.10.0
721# Any use must be runtime checked with $^V
722
723our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700724our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800725our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700726
Joe Perchesf8422302014-08-06 16:11:31 -0700727our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700728 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700729 (?:$Storage\s+)?LIST_HEAD\s*\(|
730 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
731)};
732
Joe Perches7d2367a2011-07-25 17:13:22 -0700733sub deparenthesize {
734 my ($string) = @_;
735 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700736
737 while ($string =~ /^\s*\(.*\)\s*$/) {
738 $string =~ s@^\s*\(\s*@@;
739 $string =~ s@\s*\)\s*$@@;
740 }
741
Joe Perches7d2367a2011-07-25 17:13:22 -0700742 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700743
Joe Perches7d2367a2011-07-25 17:13:22 -0700744 return $string;
745}
746
Joe Perches34456862013-07-03 15:05:34 -0700747sub seed_camelcase_file {
748 my ($file) = @_;
749
750 return if (!(-f $file));
751
752 local $/;
753
754 open(my $include_file, '<', "$file")
755 or warn "$P: Can't read '$file' $!\n";
756 my $text = <$include_file>;
757 close($include_file);
758
759 my @lines = split('\n', $text);
760
761 foreach my $line (@lines) {
762 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
763 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
764 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800765 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
766 $camelcase{$1} = 1;
767 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700768 $camelcase{$1} = 1;
769 }
770 }
771}
772
Joe Perches85b0ee12016-10-11 13:51:44 -0700773sub is_maintained_obsolete {
774 my ($filename) = @_;
775
776 return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
777
Joe Perches0616efa2016-10-11 13:52:02 -0700778 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700779
780 return $status =~ /obsolete/i;
781}
782
Joe Perches34456862013-07-03 15:05:34 -0700783my $camelcase_seeded = 0;
784sub seed_camelcase_includes {
785 return if ($camelcase_seeded);
786
787 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700788 my $camelcase_cache = "";
789 my @include_files = ();
790
791 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700792
Richard Genoud3645e322014-02-10 14:25:32 -0800793 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700794 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
795 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700796 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700797 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700798 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700799 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700800 @include_files = split('\n', $files);
801 foreach my $file (@include_files) {
802 my $date = POSIX::strftime("%Y%m%d%H%M",
803 localtime((stat $file)[9]));
804 $last_mod_date = $date if ($last_mod_date < $date);
805 }
806 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700807 }
Joe Perchesc707a812013-07-08 16:00:43 -0700808
809 if ($camelcase_cache ne "" && -f $camelcase_cache) {
810 open(my $camelcase_file, '<', "$camelcase_cache")
811 or warn "$P: Can't read '$camelcase_cache' $!\n";
812 while (<$camelcase_file>) {
813 chomp;
814 $camelcase{$_} = 1;
815 }
816 close($camelcase_file);
817
818 return;
819 }
820
Richard Genoud3645e322014-02-10 14:25:32 -0800821 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700822 $files = `git ls-files "include/*.h"`;
823 @include_files = split('\n', $files);
824 }
825
Joe Perches34456862013-07-03 15:05:34 -0700826 foreach my $file (@include_files) {
827 seed_camelcase_file($file);
828 }
Joe Perches351b2a12013-07-03 15:05:36 -0700829
Joe Perchesc707a812013-07-08 16:00:43 -0700830 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700831 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700832 open(my $camelcase_file, '>', "$camelcase_cache")
833 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700834 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
835 print $camelcase_file ("$_\n");
836 }
837 close($camelcase_file);
838 }
Joe Perches34456862013-07-03 15:05:34 -0700839}
840
Joe Perchesd311cd42014-08-06 16:10:57 -0700841sub git_commit_info {
842 my ($commit, $id, $desc) = @_;
843
844 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
845
846 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
847 $output =~ s/^\s*//gm;
848 my @lines = split("\n", $output);
849
Joe Perches0d7835f2015-02-13 14:38:35 -0800850 return ($id, $desc) if ($#lines < 0);
851
Joe Perchesd311cd42014-08-06 16:10:57 -0700852 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
853# Maybe one day convert this block of bash into something that returns
854# all matching commit ids, but it's very slow...
855#
856# echo "checking commits $1..."
857# git rev-list --remotes | grep -i "^$1" |
858# while read line ; do
859# git log --format='%H %s' -1 $line |
860# echo "commit $(cut -c 1-12,41-)"
861# done
862 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
863 } else {
864 $id = substr($lines[0], 0, 12);
865 $desc = substr($lines[0], 41);
866 }
867
868 return ($id, $desc);
869}
870
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700871$chk_signoff = 0 if ($file);
872
Andy Whitcroft00df3442007-06-08 13:47:06 -0700873my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700875my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700876my @fixed_inserted = ();
877my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700878my $fixlinenr = -1;
879
Du, Changbin4a593c32016-05-20 17:04:16 -0700880# If input is git commits, extract all commits from the commit expressions.
881# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
882die "$P: No git repository found\n" if ($git && !-e ".git");
883
884if ($git) {
885 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700886 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700887 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700888 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
889 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700890 } elsif ($commit_expr =~ m/\.\./) {
891 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700892 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700893 $git_range = "-1 $commit_expr";
894 }
895 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
896 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700897 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
898 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700899 my $sha1 = $1;
900 my $subject = $2;
901 unshift(@commits, $sha1);
902 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700903 }
904 }
905 die "$P: no git commits after extraction!\n" if (@commits == 0);
906 @ARGV = @commits;
907}
908
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800909my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700910for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800911 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700912 if ($git) {
913 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
914 die "$P: $filename: git format-patch failed - $!\n";
915 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800916 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700917 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800918 } elsif ($filename eq '-') {
919 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700920 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800921 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700922 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700923 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800924 if ($filename eq '-') {
925 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700926 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700927 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 } else {
929 $vname = $filename;
930 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800931 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700932 chomp;
933 push(@rawlines, $_);
934 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800935 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700936
937 if ($#ARGV > 0 && $quiet == 0) {
938 print '-' x length($vname) . "\n";
939 print "$vname\n";
940 print '-' x length($vname) . "\n";
941 }
942
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700944 $exit = 1;
945 }
946 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800947 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700948 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700949 @fixed_inserted = ();
950 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700951 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700952 @modifierListFile = ();
953 @typeListFile = ();
954 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700955}
956
Joe Perchesd8469f12015-06-25 15:03:00 -0700957if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700958 hash_show_words(\%use_type, "Used");
959 hash_show_words(\%ignore_type, "Ignored");
960
Joe Perchesd8469f12015-06-25 15:03:00 -0700961 if ($^V lt 5.10.0) {
962 print << "EOM"
963
964NOTE: perl $^V is not modern enough to detect all possible issues.
965 An upgrade to at least perl v5.10.0 is suggested.
966EOM
967 }
968 if ($exit) {
969 print << "EOM"
970
971NOTE: If any of the errors are false positives, please report
972 them to the maintainer, see CHECKPATCH in MAINTAINERS.
973EOM
974 }
975}
976
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700977exit($exit);
978
979sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700980 my ($root) = @_;
981
982 my @tree_check = (
983 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
984 "README", "Documentation", "arch", "include", "drivers",
985 "fs", "init", "ipc", "kernel", "lib", "scripts",
986 );
987
988 foreach my $check (@tree_check) {
989 if (! -e $root . '/' . $check) {
990 return 0;
991 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700992 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700993 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700994}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700995
Joe Perches20112472011-07-25 17:13:23 -0700996sub parse_email {
997 my ($formatted_email) = @_;
998
999 my $name = "";
1000 my $address = "";
1001 my $comment = "";
1002
1003 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1004 $name = $1;
1005 $address = $2;
1006 $comment = $3 if defined $3;
1007 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1008 $address = $1;
1009 $comment = $2 if defined $2;
1010 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1011 $address = $1;
1012 $comment = $2 if defined $2;
1013 $formatted_email =~ s/$address.*$//;
1014 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001015 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001016 $name =~ s/^\"|\"$//g;
1017 # If there's a name left after stripping spaces and
1018 # leading quotes, and the address doesn't have both
1019 # leading and trailing angle brackets, the address
1020 # is invalid. ie:
1021 # "joe smith joe@smith.com" bad
1022 # "joe smith <joe@smith.com" bad
1023 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1024 $name = "";
1025 $address = "";
1026 $comment = "";
1027 }
1028 }
1029
Joe Perches3705ce52013-07-03 15:05:31 -07001030 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001031 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001032 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001033 $address =~ s/^\<|\>$//g;
1034
1035 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1036 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1037 $name = "\"$name\"";
1038 }
1039
1040 return ($name, $address, $comment);
1041}
1042
1043sub format_email {
1044 my ($name, $address) = @_;
1045
1046 my $formatted_email;
1047
Joe Perches3705ce52013-07-03 15:05:31 -07001048 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001049 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001050 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001051
1052 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1053 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1054 $name = "\"$name\"";
1055 }
1056
1057 if ("$name" eq "") {
1058 $formatted_email = "$address";
1059 } else {
1060 $formatted_email = "$name <$address>";
1061 }
1062
1063 return $formatted_email;
1064}
1065
Joe Perchesd311cd42014-08-06 16:10:57 -07001066sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001067 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001068
Joe Perchesbd474ca2014-08-06 16:11:10 -07001069 foreach my $path (split(/:/, $ENV{PATH})) {
1070 if (-e "$path/$bin") {
1071 return "$path/$bin";
1072 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001073 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001074
Joe Perchesbd474ca2014-08-06 16:11:10 -07001075 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001076}
1077
Joe Perches000d1cc12011-07-25 17:13:25 -07001078sub which_conf {
1079 my ($conf) = @_;
1080
1081 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1082 if (-e "$path/$conf") {
1083 return "$path/$conf";
1084 }
1085 }
1086
1087 return "";
1088}
1089
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001090sub expand_tabs {
1091 my ($str) = @_;
1092
1093 my $res = '';
1094 my $n = 0;
1095 for my $c (split(//, $str)) {
1096 if ($c eq "\t") {
1097 $res .= ' ';
1098 $n++;
1099 for (; ($n % 8) != 0; $n++) {
1100 $res .= ' ';
1101 }
1102 next;
1103 }
1104 $res .= $c;
1105 $n++;
1106 }
1107
1108 return $res;
1109}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001110sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001111 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001112 return $res;
1113}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001114
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001115sub line_stats {
1116 my ($line) = @_;
1117
1118 # Drop the diff line leader and expand tabs
1119 $line =~ s/^.//;
1120 $line = expand_tabs($line);
1121
1122 # Pick the indent from the front of the line.
1123 my ($white) = ($line =~ /^(\s*)/);
1124
1125 return (length($line), length($white));
1126}
1127
Andy Whitcroft773647a2008-03-28 14:15:58 -07001128my $sanitise_quote = '';
1129
1130sub sanitise_line_reset {
1131 my ($in_comment) = @_;
1132
1133 if ($in_comment) {
1134 $sanitise_quote = '*/';
1135 } else {
1136 $sanitise_quote = '';
1137 }
1138}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001139sub sanitise_line {
1140 my ($line) = @_;
1141
1142 my $res = '';
1143 my $l = '';
1144
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001145 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001146 my $off = 0;
1147 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001148
Andy Whitcroft773647a2008-03-28 14:15:58 -07001149 # Always copy over the diff marker.
1150 $res = substr($line, 0, 1);
1151
1152 for ($off = 1; $off < length($line); $off++) {
1153 $c = substr($line, $off, 1);
1154
1155 # Comments we are wacking completly including the begin
1156 # and end, all to $;.
1157 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1158 $sanitise_quote = '*/';
1159
1160 substr($res, $off, 2, "$;$;");
1161 $off++;
1162 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001163 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001164 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001165 $sanitise_quote = '';
1166 substr($res, $off, 2, "$;$;");
1167 $off++;
1168 next;
1169 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001170 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1171 $sanitise_quote = '//';
1172
1173 substr($res, $off, 2, $sanitise_quote);
1174 $off++;
1175 next;
1176 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177
1178 # A \ in a string means ignore the next character.
1179 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1180 $c eq "\\") {
1181 substr($res, $off, 2, 'XX');
1182 $off++;
1183 next;
1184 }
1185 # Regular quotes.
1186 if ($c eq "'" || $c eq '"') {
1187 if ($sanitise_quote eq '') {
1188 $sanitise_quote = $c;
1189
1190 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001191 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001192 } elsif ($sanitise_quote eq $c) {
1193 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001194 }
1195 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001197 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001198 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1199 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001200 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1201 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001202 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1203 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001204 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001205 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001206 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001207 }
1208
Daniel Walker113f04a2009-09-21 17:04:35 -07001209 if ($sanitise_quote eq '//') {
1210 $sanitise_quote = '';
1211 }
1212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001214 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 my $clean = 'X' x length($1);
1216 $res =~ s@\<.*\>@<$clean>@;
1217
1218 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001219 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001220 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001221 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001222 }
1223
Joe Perchesdadf6802016-08-02 14:04:33 -07001224 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1225 my $match = $1;
1226 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1227 }
1228
Andy Whitcroft00df3442007-06-08 13:47:06 -07001229 return $res;
1230}
1231
Joe Perchesa6962d72013-04-29 16:18:13 -07001232sub get_quoted_string {
1233 my ($line, $rawline) = @_;
1234
Joe Perches33acb542015-06-25 15:02:54 -07001235 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001236 return substr($rawline, $-[0], $+[0] - $-[0]);
1237}
1238
Andy Whitcroft8905a672007-11-28 16:21:06 -08001239sub ctx_statement_block {
1240 my ($linenr, $remain, $off) = @_;
1241 my $line = $linenr - 1;
1242 my $blk = '';
1243 my $soff = $off;
1244 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001245 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001246
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001247 my $loff = 0;
1248
Andy Whitcroft8905a672007-11-28 16:21:06 -08001249 my $type = '';
1250 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001251 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001252 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001253 my $c;
1254 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001255
1256 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001257 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001258 @stack = (['', 0]) if ($#stack == -1);
1259
Andy Whitcroft773647a2008-03-28 14:15:58 -07001260 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 # If we are about to drop off the end, pull in more
1262 # context.
1263 if ($off >= $len) {
1264 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001265 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001266 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001267 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001268 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001269 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001270 $len = length($blk);
1271 $line++;
1272 last;
1273 }
1274 # Bail if there is no further context.
1275 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001276 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001277 last;
1278 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001279 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1280 $level++;
1281 $type = '#';
1282 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001283 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001284 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001287
Andy Whitcroft773647a2008-03-28 14:15:58 -07001288 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001289
1290 # Handle nested #if/#else.
1291 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1292 push(@stack, [ $type, $level ]);
1293 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1294 ($type, $level) = @{$stack[$#stack - 1]};
1295 } elsif ($remainder =~ /^#\s*endif\b/) {
1296 ($type, $level) = @{pop(@stack)};
1297 }
1298
Andy Whitcroft8905a672007-11-28 16:21:06 -08001299 # Statement ends at the ';' or a close '}' at the
1300 # outermost level.
1301 if ($level == 0 && $c eq ';') {
1302 last;
1303 }
1304
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001305 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 if ($level == 0 && $coff_set == 0 &&
1307 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1308 $remainder =~ /^(else)(?:\s|{)/ &&
1309 $remainder !~ /^else\s+if\b/) {
1310 $coff = $off + length($1) - 1;
1311 $coff_set = 1;
1312 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1313 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001314 }
1315
Andy Whitcroft8905a672007-11-28 16:21:06 -08001316 if (($type eq '' || $type eq '(') && $c eq '(') {
1317 $level++;
1318 $type = '(';
1319 }
1320 if ($type eq '(' && $c eq ')') {
1321 $level--;
1322 $type = ($level != 0)? '(' : '';
1323
1324 if ($level == 0 && $coff < $soff) {
1325 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001326 $coff_set = 1;
1327 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001328 }
1329 }
1330 if (($type eq '' || $type eq '{') && $c eq '{') {
1331 $level++;
1332 $type = '{';
1333 }
1334 if ($type eq '{' && $c eq '}') {
1335 $level--;
1336 $type = ($level != 0)? '{' : '';
1337
1338 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001339 if (substr($blk, $off + 1, 1) eq ';') {
1340 $off++;
1341 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001342 last;
1343 }
1344 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001345 # Preprocessor commands end at the newline unless escaped.
1346 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1347 $level--;
1348 $type = '';
1349 $off++;
1350 last;
1351 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001352 $off++;
1353 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001354 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001355 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001356 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001357 $line++;
1358 $remain--;
1359 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001360
1361 my $statement = substr($blk, $soff, $off - $soff + 1);
1362 my $condition = substr($blk, $soff, $coff - $soff + 1);
1363
1364 #warn "STATEMENT<$statement>\n";
1365 #warn "CONDITION<$condition>\n";
1366
Andy Whitcroft773647a2008-03-28 14:15:58 -07001367 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001368
1369 return ($statement, $condition,
1370 $line, $remain + 1, $off - $loff + 1, $level);
1371}
1372
Andy Whitcroftcf655042008-03-04 14:28:20 -08001373sub statement_lines {
1374 my ($stmt) = @_;
1375
1376 # Strip the diff line prefixes and rip blank lines at start and end.
1377 $stmt =~ s/(^|\n)./$1/g;
1378 $stmt =~ s/^\s*//;
1379 $stmt =~ s/\s*$//;
1380
1381 my @stmt_lines = ($stmt =~ /\n/g);
1382
1383 return $#stmt_lines + 2;
1384}
1385
1386sub statement_rawlines {
1387 my ($stmt) = @_;
1388
1389 my @stmt_lines = ($stmt =~ /\n/g);
1390
1391 return $#stmt_lines + 2;
1392}
1393
1394sub statement_block_size {
1395 my ($stmt) = @_;
1396
1397 $stmt =~ s/(^|\n)./$1/g;
1398 $stmt =~ s/^\s*{//;
1399 $stmt =~ s/}\s*$//;
1400 $stmt =~ s/^\s*//;
1401 $stmt =~ s/\s*$//;
1402
1403 my @stmt_lines = ($stmt =~ /\n/g);
1404 my @stmt_statements = ($stmt =~ /;/g);
1405
1406 my $stmt_lines = $#stmt_lines + 2;
1407 my $stmt_statements = $#stmt_statements + 1;
1408
1409 if ($stmt_lines > $stmt_statements) {
1410 return $stmt_lines;
1411 } else {
1412 return $stmt_statements;
1413 }
1414}
1415
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001416sub ctx_statement_full {
1417 my ($linenr, $remain, $off) = @_;
1418 my ($statement, $condition, $level);
1419
1420 my (@chunks);
1421
Andy Whitcroftcf655042008-03-04 14:28:20 -08001422 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001423 ($statement, $condition, $linenr, $remain, $off, $level) =
1424 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001425 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001426 push(@chunks, [ $condition, $statement ]);
1427 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1428 return ($level, $linenr, @chunks);
1429 }
1430
1431 # Pull in the following conditional/block pairs and see if they
1432 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001433 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001434 ($statement, $condition, $linenr, $remain, $off, $level) =
1435 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001436 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001437 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001438 #print "C: push\n";
1439 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001440 }
1441
1442 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001443}
1444
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001445sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001446 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001447 my $line;
1448 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001449 my $blk = '';
1450 my @o;
1451 my @c;
1452 my @res = ();
1453
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001454 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001455 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001456 for ($line = $start; $remain > 0; $line++) {
1457 next if ($rawlines[$line] =~ /^-/);
1458 $remain--;
1459
1460 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001461
1462 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001463 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001464 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001465 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001466 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001467 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001468 $level = pop(@stack);
1469 }
1470
Andy Whitcroft01464f32010-10-26 14:23:19 -07001471 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001472 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1473 if ($off > 0) {
1474 $off--;
1475 next;
1476 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001477
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001478 if ($c eq $close && $level > 0) {
1479 $level--;
1480 last if ($level == 0);
1481 } elsif ($c eq $open) {
1482 $level++;
1483 }
1484 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001485
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001486 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001488 }
1489
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001490 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001491 }
1492
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001493 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494}
1495sub ctx_block_outer {
1496 my ($linenr, $remain) = @_;
1497
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001498 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1499 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001500}
1501sub ctx_block {
1502 my ($linenr, $remain) = @_;
1503
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001504 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1505 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001506}
1507sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001508 my ($linenr, $remain, $off) = @_;
1509
1510 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1511 return @r;
1512}
1513sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001514 my ($linenr, $remain) = @_;
1515
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001516 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001517}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001518sub ctx_statement_level {
1519 my ($linenr, $remain, $off) = @_;
1520
1521 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1522}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001523
1524sub ctx_locate_comment {
1525 my ($first_line, $end_line) = @_;
1526
1527 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001528 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001529 return $current_comment if (defined $current_comment);
1530
1531 # Look through the context and try and figure out if there is a
1532 # comment.
1533 my $in_comment = 0;
1534 $current_comment = '';
1535 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001536 my $line = $rawlines[$linenr - 1];
1537 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001538 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1539 $in_comment = 1;
1540 }
1541 if ($line =~ m@/\*@) {
1542 $in_comment = 1;
1543 }
1544 if (!$in_comment && $current_comment ne '') {
1545 $current_comment = '';
1546 }
1547 $current_comment .= $line . "\n" if ($in_comment);
1548 if ($line =~ m@\*/@) {
1549 $in_comment = 0;
1550 }
1551 }
1552
1553 chomp($current_comment);
1554 return($current_comment);
1555}
1556sub ctx_has_comment {
1557 my ($first_line, $end_line) = @_;
1558 my $cmt = ctx_locate_comment($first_line, $end_line);
1559
Andy Whitcroft00df3442007-06-08 13:47:06 -07001560 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001561 ##print "CMMT: $cmt\n";
1562
1563 return ($cmt ne '');
1564}
1565
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001566sub raw_line {
1567 my ($linenr, $cnt) = @_;
1568
1569 my $offset = $linenr - 1;
1570 $cnt++;
1571
1572 my $line;
1573 while ($cnt) {
1574 $line = $rawlines[$offset++];
1575 next if (defined($line) && $line =~ /^-/);
1576 $cnt--;
1577 }
1578
1579 return $line;
1580}
1581
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001582sub cat_vet {
1583 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001584 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001585
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001586 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001587 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1588 $res .= $1;
1589 if ($2 ne '') {
1590 $coded = sprintf("^%c", unpack('C', $2) + 64);
1591 $res .= $coded;
1592 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001593 }
1594 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001595
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001596 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001597}
1598
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001599my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001601my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001602my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001603
1604sub annotate_reset {
1605 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606 $av_pending = '_';
1607 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001608 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609}
1610
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001611sub annotate_values {
1612 my ($stream, $type) = @_;
1613
1614 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001615 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001616 my $cur = $stream;
1617
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001618 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001621 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001622 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001623 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001624 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001625 print "WS($1)\n" if ($dbg_values > 1);
1626 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001627 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001629 }
1630
Florian Micklerc023e4732011-01-12 16:59:58 -08001631 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001632 print "CAST($1)\n" if ($dbg_values > 1);
1633 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001634 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001635
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001636 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001637 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001638 $type = 'T';
1639
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001640 } elsif ($cur =~ /^($Modifier)\s*/) {
1641 print "MODIFIER($1)\n" if ($dbg_values > 1);
1642 $type = 'T';
1643
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001644 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001645 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001646 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001647 push(@av_paren_type, $type);
1648 if ($2 ne '') {
1649 $av_pending = 'N';
1650 }
1651 $type = 'E';
1652
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001653 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 print "UNDEF($1)\n" if ($dbg_values > 1);
1655 $av_preprocessor = 1;
1656 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001657
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001658 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001659 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001661
1662 push(@av_paren_type, $type);
1663 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001664 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001665
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001666 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001667 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1668 $av_preprocessor = 1;
1669
1670 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1671
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001672 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001673
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001674 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001675 print "PRE_END($1)\n" if ($dbg_values > 1);
1676
1677 $av_preprocessor = 1;
1678
1679 # Assume all arms of the conditional end as this
1680 # one does, and continue as if the #endif was not here.
1681 pop(@av_paren_type);
1682 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001683 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001684
1685 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001686 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001687
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001688 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1689 print "ATTR($1)\n" if ($dbg_values > 1);
1690 $av_pending = $type;
1691 $type = 'N';
1692
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001693 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001695 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001696 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001697 }
1698 $type = 'N';
1699
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001700 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001701 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001702 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703 $type = 'N';
1704
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001705 } elsif ($cur =~/^(case)/o) {
1706 print "CASE($1)\n" if ($dbg_values > 1);
1707 $av_pend_colon = 'C';
1708 $type = 'N';
1709
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001710 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001711 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001712 $type = 'N';
1713
1714 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001715 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001716 push(@av_paren_type, $av_pending);
1717 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 $type = 'N';
1719
1720 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001721 my $new_type = pop(@av_paren_type);
1722 if ($new_type ne '_') {
1723 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001724 print "PAREN('$1') -> $type\n"
1725 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
1729
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001730 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001731 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001732 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001733 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001735 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1736 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001737 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001738 } elsif ($type eq 'E') {
1739 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001740 }
1741 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1742 $type = 'V';
1743
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001744 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001745 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001746 $type = 'V';
1747
1748 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001749 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001750 $type = 'N';
1751
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001754 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001755 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001756
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001757 } elsif ($cur =~/^(,)/) {
1758 print "COMMA($1)\n" if ($dbg_values > 1);
1759 $type = 'C';
1760
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001761 } elsif ($cur =~ /^(\?)/o) {
1762 print "QUESTION($1)\n" if ($dbg_values > 1);
1763 $type = 'N';
1764
1765 } elsif ($cur =~ /^(:)/o) {
1766 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1767
1768 substr($var, length($res), 1, $av_pend_colon);
1769 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1770 $type = 'E';
1771 } else {
1772 $type = 'N';
1773 }
1774 $av_pend_colon = 'O';
1775
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001776 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001777 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001778 $type = 'N';
1779
Andy Whitcroft0d413862008-10-15 22:02:16 -07001780 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001781 my $variant;
1782
1783 print "OPV($1)\n" if ($dbg_values > 1);
1784 if ($type eq 'V') {
1785 $variant = 'B';
1786 } else {
1787 $variant = 'U';
1788 }
1789
1790 substr($var, length($res), 1, $variant);
1791 $type = 'N';
1792
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001793 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001794 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001795 if ($1 ne '++' && $1 ne '--') {
1796 $type = 'N';
1797 }
1798
1799 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001800 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 }
1802 if (defined $1) {
1803 $cur = substr($cur, length($1));
1804 $res .= $type x length($1);
1805 }
1806 }
1807
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001808 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809}
1810
Andy Whitcroft8905a672007-11-28 16:21:06 -08001811sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001812 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001813 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001814 ^(?:
1815 $Modifier|
1816 $Storage|
1817 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001818 DEFINE_\S+
1819 )$|
1820 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001821 goto|
1822 return|
1823 case|
1824 else|
1825 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001826 do|
1827 \#|
1828 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001829 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001830 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001831 )}x;
1832 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1833 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001834 # Check for modifiers.
1835 $possible =~ s/\s*$Storage\s*//g;
1836 $possible =~ s/\s*$Sparse\s*//g;
1837 if ($possible =~ /^\s*$/) {
1838
1839 } elsif ($possible =~ /\s/) {
1840 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001841 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001842 if ($modifier !~ $notPermitted) {
1843 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001844 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001845 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001846 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001847
1848 } else {
1849 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001850 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001851 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001852 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001853 } else {
1854 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001855 }
1856}
1857
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001858my $prefix = '';
1859
Joe Perches000d1cc12011-07-25 17:13:25 -07001860sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001861 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001862
Joe Perchescbec18a2014-04-03 14:49:19 -07001863 return defined $use_type{$type} if (scalar keys %use_type > 0);
1864
1865 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001866}
1867
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001868sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001869 my ($level, $type, $msg) = @_;
1870
1871 if (!show_type($type) ||
1872 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001873 return 0;
1874 }
Joe Perches57230292015-06-25 15:03:03 -07001875 my $output = '';
1876 if (-t STDOUT && $color) {
1877 if ($level eq 'ERROR') {
1878 $output .= RED;
1879 } elsif ($level eq 'WARNING') {
1880 $output .= YELLOW;
1881 } else {
1882 $output .= GREEN;
1883 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001884 }
Joe Perches57230292015-06-25 15:03:03 -07001885 $output .= $prefix . $level . ':';
1886 if ($show_types) {
1887 $output .= BLUE if (-t STDOUT && $color);
1888 $output .= "$type:";
1889 }
1890 $output .= RESET if (-t STDOUT && $color);
1891 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001892
1893 if ($showfile) {
1894 my @lines = split("\n", $output, -1);
1895 splice(@lines, 1, 1);
1896 $output = join("\n", @lines);
1897 }
Joe Perches57230292015-06-25 15:03:03 -07001898 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001899
Joe Perches57230292015-06-25 15:03:03 -07001900 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001901
1902 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001903}
Joe Perchescbec18a2014-04-03 14:49:19 -07001904
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001905sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001906 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001907}
Joe Perches000d1cc12011-07-25 17:13:25 -07001908
Joe Perchesd752fcc2014-08-06 16:11:05 -07001909sub fixup_current_range {
1910 my ($lineRef, $offset, $length) = @_;
1911
1912 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1913 my $o = $1;
1914 my $l = $2;
1915 my $no = $o + $offset;
1916 my $nl = $l + $length;
1917 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1918 }
1919}
1920
1921sub fix_inserted_deleted_lines {
1922 my ($linesRef, $insertedRef, $deletedRef) = @_;
1923
1924 my $range_last_linenr = 0;
1925 my $delta_offset = 0;
1926
1927 my $old_linenr = 0;
1928 my $new_linenr = 0;
1929
1930 my $next_insert = 0;
1931 my $next_delete = 0;
1932
1933 my @lines = ();
1934
1935 my $inserted = @{$insertedRef}[$next_insert++];
1936 my $deleted = @{$deletedRef}[$next_delete++];
1937
1938 foreach my $old_line (@{$linesRef}) {
1939 my $save_line = 1;
1940 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001941 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001942 $delta_offset = 0;
1943 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1944 $range_last_linenr = $new_linenr;
1945 fixup_current_range(\$line, $delta_offset, 0);
1946 }
1947
1948 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1949 $deleted = @{$deletedRef}[$next_delete++];
1950 $save_line = 0;
1951 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1952 }
1953
1954 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1955 push(@lines, ${$inserted}{'LINE'});
1956 $inserted = @{$insertedRef}[$next_insert++];
1957 $new_linenr++;
1958 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1959 }
1960
1961 if ($save_line) {
1962 push(@lines, $line);
1963 $new_linenr++;
1964 }
1965
1966 $old_linenr++;
1967 }
1968
1969 return @lines;
1970}
1971
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001972sub fix_insert_line {
1973 my ($linenr, $line) = @_;
1974
1975 my $inserted = {
1976 LINENR => $linenr,
1977 LINE => $line,
1978 };
1979 push(@fixed_inserted, $inserted);
1980}
1981
1982sub fix_delete_line {
1983 my ($linenr, $line) = @_;
1984
1985 my $deleted = {
1986 LINENR => $linenr,
1987 LINE => $line,
1988 };
1989
1990 push(@fixed_deleted, $deleted);
1991}
1992
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001993sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001994 my ($type, $msg) = @_;
1995
1996 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001997 our $clean = 0;
1998 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001999 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 }
Joe Perches3705ce52013-07-03 15:05:31 -07002001 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002002}
2003sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002004 my ($type, $msg) = @_;
2005
2006 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007 our $clean = 0;
2008 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002009 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002010 }
Joe Perches3705ce52013-07-03 15:05:31 -07002011 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002012}
2013sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002014 my ($type, $msg) = @_;
2015
2016 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017 our $clean = 0;
2018 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002020 }
Joe Perches3705ce52013-07-03 15:05:31 -07002021 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002022}
2023
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002024sub check_absolute_file {
2025 my ($absolute, $herecurr) = @_;
2026 my $file = $absolute;
2027
2028 ##print "absolute<$absolute>\n";
2029
2030 # See if any suffix of this path is a path within the tree.
2031 while ($file =~ s@^[^/]*/@@) {
2032 if (-f "$root/$file") {
2033 ##print "file<$file>\n";
2034 last;
2035 }
2036 }
2037 if (! -f _) {
2038 return 0;
2039 }
2040
2041 # It is, so see if the prefix is acceptable.
2042 my $prefix = $absolute;
2043 substr($prefix, -length($file)) = '';
2044
2045 ##print "prefix<$prefix>\n";
2046 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002047 WARN("USE_RELATIVE_PATH",
2048 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002049 }
2050}
2051
Joe Perches3705ce52013-07-03 15:05:31 -07002052sub trim {
2053 my ($string) = @_;
2054
Joe Perchesb34c6482013-09-11 14:24:01 -07002055 $string =~ s/^\s+|\s+$//g;
2056
2057 return $string;
2058}
2059
2060sub ltrim {
2061 my ($string) = @_;
2062
2063 $string =~ s/^\s+//;
2064
2065 return $string;
2066}
2067
2068sub rtrim {
2069 my ($string) = @_;
2070
2071 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002072
2073 return $string;
2074}
2075
Joe Perches52ea8502013-11-12 15:10:09 -08002076sub string_find_replace {
2077 my ($string, $find, $replace) = @_;
2078
2079 $string =~ s/$find/$replace/g;
2080
2081 return $string;
2082}
2083
Joe Perches3705ce52013-07-03 15:05:31 -07002084sub tabify {
2085 my ($leading) = @_;
2086
2087 my $source_indent = 8;
2088 my $max_spaces_before_tab = $source_indent - 1;
2089 my $spaces_to_tab = " " x $source_indent;
2090
2091 #convert leading spaces to tabs
2092 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2093 #Remove spaces before a tab
2094 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2095
2096 return "$leading";
2097}
2098
Gregory Beanf07523e2011-07-06 15:36:58 -07002099sub cleanup_continuation_headers {
2100 # Collapse any header-continuation lines into a single line so they
2101 # can be parsed meaningfully, as the parser only has one line
2102 # of context to work with.
2103 my $again;
2104 do {
2105 $again = 0;
2106 foreach my $n (0 .. scalar(@rawlines) - 2) {
2107 if ($rawlines[$n]=~/^\s*$/) {
2108 # A blank line means there's no more chance
2109 # of finding headers. Shortcut to done.
2110 return;
2111 }
2112 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2113 $rawlines[$n+1]=~/^\s+/) {
2114 # Continuation header. Collapse it.
2115 my $line = splice @rawlines, $n+1, 1;
2116 $line=~s/^\s+/ /;
2117 $rawlines[$n] .= $line;
2118 # We've 'destabilized' the list, so restart.
2119 $again = 1;
2120 last;
2121 }
2122 }
2123 } while ($again);
2124}
2125
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002126sub pos_last_openparen {
2127 my ($line) = @_;
2128
2129 my $pos = 0;
2130
2131 my $opens = $line =~ tr/\(/\(/;
2132 my $closes = $line =~ tr/\)/\)/;
2133
2134 my $last_openparen = 0;
2135
2136 if (($opens == 0) || ($closes >= $opens)) {
2137 return -1;
2138 }
2139
2140 my $len = length($line);
2141
2142 for ($pos = 0; $pos < $len; $pos++) {
2143 my $string = substr($line, $pos);
2144 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2145 $pos += length($1) - 1;
2146 } elsif (substr($line, $pos, 1) eq '(') {
2147 $last_openparen = $pos;
2148 } elsif (index($string, '(') == -1) {
2149 last;
2150 }
2151 }
2152
Joe Perches91cb5192014-04-03 14:49:32 -07002153 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002154}
2155
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002156sub process {
2157 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002158
2159 my $linenr=0;
2160 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002161 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002162 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002163 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002164 my $subjectline="";
2165 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002166
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002167 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002168 my $indent;
2169 my $previndent=0;
2170 my $stashindent=0;
2171
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002172 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002173 my $signoff = 0;
2174 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002175 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002176 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002177 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002178 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002179 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002180 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002181 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002182 my $non_utf8_charset = 0;
2183
Joe Perches365dd4e2014-08-06 16:10:42 -07002184 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002185 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002186
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002187 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002188 our $cnt_lines = 0;
2189 our $cnt_error = 0;
2190 our $cnt_warn = 0;
2191 our $cnt_chk = 0;
2192
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002193 # Trace the real file/line as we go.
2194 my $realfile = '';
2195 my $realline = 0;
2196 my $realcnt = 0;
2197 my $here = '';
2198 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002199 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002200 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002201 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002202
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002203 my $prev_values = 'E';
2204
2205 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002207 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002208 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002209 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002210
Joe Perches7e51f192013-09-11 14:23:57 -07002211 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002212
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002213 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002214 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002215 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002216 my @setup_docs = ();
2217 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002218
Joe Perchesd8b07712013-11-12 15:10:06 -08002219 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002220 my $shorttext = BEFORE_SHORTTEXT;
2221 my $shorttext_exspc = 0;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002222 my $commit_text_present = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002223
Andy Whitcroft773647a2008-03-28 14:15:58 -07002224 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002225 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002226 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002227
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002228 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 $linenr++;
2230 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002231
Joe Perches3705ce52013-07-03 15:05:31 -07002232 push(@fixed, $rawline) if ($fix);
2233
Andy Whitcroft773647a2008-03-28 14:15:58 -07002234 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002235 $setup_docs = 0;
2236 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2237 $setup_docs = 1;
2238 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002241 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2242 $realline=$1-1;
2243 if (defined $2) {
2244 $realcnt=$3+1;
2245 } else {
2246 $realcnt=1+1;
2247 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002248 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249
2250 # Guestimate if this is a continuing comment. Run
2251 # the context looking for a comment "edge". If this
2252 # edge is a close comment then we must be in a comment
2253 # at context start.
2254 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002255 my $cnt = $realcnt;
2256 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2257 next if (defined $rawlines[$ln - 1] &&
2258 $rawlines[$ln - 1] =~ /^-/);
2259 $cnt--;
2260 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002261 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002262 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2263 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2264 ($edge) = $1;
2265 last;
2266 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 }
2268 if (defined $edge && $edge eq '*/') {
2269 $in_comment = 1;
2270 }
2271
2272 # Guestimate if this is a continuing comment. If this
2273 # is the start of a diff block and this line starts
2274 # ' *' then it is very likely a comment.
2275 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002276 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 {
2278 $in_comment = 1;
2279 }
2280
2281 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2282 sanitise_line_reset($in_comment);
2283
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002286 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 $line = sanitise_line($rawline);
2288 }
2289 push(@lines, $line);
2290
2291 if ($realcnt > 1) {
2292 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2293 } else {
2294 $realcnt = 0;
2295 }
2296
2297 #print "==>$rawline\n";
2298 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299
2300 if ($setup_docs && $line =~ /^\+/) {
2301 push(@setup_docs, $line);
2302 }
2303 }
2304
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002305 $prefix = '';
2306
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 $realcnt = 0;
2308 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002309 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002310 foreach my $line (@lines) {
2311 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002312 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002313 my $sline = $line; #copy of $line
2314 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002316 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002317
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002319 if (!$in_commit_log &&
2320 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002321 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002322 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002323 $realline=$1-1;
2324 if (defined $2) {
2325 $realcnt=$3+1;
2326 } else {
2327 $realcnt=1+1;
2328 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002329 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002330 $prev_values = 'E';
2331
Andy Whitcroft773647a2008-03-28 14:15:58 -07002332 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002333 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002334 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002335 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002336 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002337
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002338# track the line number as we move through the hunk, note that
2339# new versions of GNU diff omit the leading space on completely
2340# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002341 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002343 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002344
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002345 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002346 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002347
2348 # Track the previous line.
2349 ($prevline, $stashline) = ($stashline, $line);
2350 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002351 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2352
Andy Whitcroft773647a2008-03-28 14:15:58 -07002353 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002354
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002355 } elsif ($realcnt == 1) {
2356 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002357 }
2358
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002359 my $hunk_line = ($realcnt != 0);
2360
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002361 $here = "#$linenr: " if (!$file);
2362 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002363
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002364 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002366 if ($line =~ /^diff --git.*?(\S+)$/) {
2367 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002368 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002369 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002370 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002371 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002372 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002373 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002374 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002375
2376 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002377 if (!$file && $tree && $p1_prefix ne '' &&
2378 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002379 WARN("PATCH_PREFIX",
2380 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002381 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002382
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002383 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002384 ERROR("MODIFIED_INCLUDE_ASM",
2385 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002386 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002387 $found_file = 1;
2388 }
2389
Joe Perches34d88152015-06-25 15:03:05 -07002390#make up the handle for any error we report on this line
2391 if ($showfile) {
2392 $prefix = "$realfile:$realline: "
2393 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002394 if ($file) {
2395 $prefix = "$filename:$realline: ";
2396 } else {
2397 $prefix = "$filename:$linenr: ";
2398 }
Joe Perches34d88152015-06-25 15:03:05 -07002399 }
2400
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002401 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002402 if (is_maintained_obsolete($realfile)) {
2403 WARN("OBSOLETE",
2404 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2405 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002406 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002407 $check = 1;
2408 } else {
2409 $check = $check_orig;
2410 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002411 next;
2412 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002413 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002414
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002415 my $hereline = "$here\n$rawline\n";
2416 my $herecurr = "$here\n$rawline\n";
2417 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002418
David Keitel72bf3e82016-03-23 20:52:44 -07002419 if ($shorttext != AFTER_SHORTTEXT) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002420 if ($shorttext == IN_SHORTTEXT_BLANKLINE && $line=~/\S/) {
2421 # the subject line was just processed,
2422 # a blank line must be next
2423 WARN("NONBLANK_AFTER_SUMMARY",
2424 "non-blank line after summary line\n" . $herecurr);
2425 $shorttext = IN_SHORTTEXT;
2426 # this non-blank line may or may not be commit text -
2427 # a warning has been generated so assume it is commit
2428 # text and move on
2429 $commit_text_present = 1;
2430 # fall through and treat this line as IN_SHORTTEXT
2431 }
David Keitel72bf3e82016-03-23 20:52:44 -07002432 if ($shorttext == IN_SHORTTEXT) {
2433 if ($line=~/^---/ || $line=~/^diff.*/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002434 if ($commit_text_present == 0) {
2435 WARN("NO_COMMIT_TEXT",
2436 "please add commit text explaining " .
2437 "*why* the change is needed\n" .
2438 $herecurr);
2439 }
David Keitel72bf3e82016-03-23 20:52:44 -07002440 $shorttext = AFTER_SHORTTEXT;
Steve Muckled360fe42012-03-09 11:15:24 -08002441 } elsif ($line=~/^\s*change-id:/i ||
2442 $line=~/^\s*signed-off-by:/i ||
2443 $line=~/^\s*crs-fixed:/i ||
2444 $line=~/^\s*acked-by:/i) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002445 # this is a tag, there must be commit
2446 # text by now
2447 if ($commit_text_present == 0) {
2448 WARN("NO_COMMIT_TEXT",
2449 "please add commit text explaining " .
2450 "*why* the change is needed\n" .
2451 $herecurr);
2452 # prevent duplicate warnings
2453 $commit_text_present = 1;
2454 }
2455 } elsif ($line=~/\S/) {
2456 $commit_text_present = 1;
David Keitel72bf3e82016-03-23 20:52:44 -07002457 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002458 } elsif ($shorttext == IN_SHORTTEXT_BLANKLINE) {
2459 # case of non-blank line in this state handled above
2460 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002461 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002462# The Subject line doesn't have to be the last header in the patch.
2463# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2464# Per RFC5322, continuation lines must be folded, so any left-justified
2465# text which looks like a header is definitely a header.
2466 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2467 $shorttext = IN_SHORTTEXT;
Steve Muckle9ed561d2012-03-05 10:12:04 -08002468 # Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002469 if (length($line) != 0) {
2470 WARN("NONBLANK_AFTER_SUMMARY",
2471 "non-blank line after " .
2472 "summary line\n" .
2473 $sublinenr . $here .
2474 "\n" . $subjectline .
2475 "\n" . $line . "\n");
Steve Muckle9ed561d2012-03-05 10:12:04 -08002476 # this non-blank line may or may not
2477 # be commit text - a warning has been
2478 # generated so assume it is commit
2479 # text and move on
2480 $commit_text_present = 1;
Gregory Beana386ec22011-03-03 13:46:41 -08002481 }
David Keitele288d232016-03-23 20:57:21 -07002482 }
Steve Muckle9ed561d2012-03-05 10:12:04 -08002483 # The next two cases are BEFORE_SHORTTEXT.
David Keitele288d232016-03-23 20:57:21 -07002484 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002485 # This is the subject line. Go to
2486 # CHECK_NEXT_SHORTTEXT to wait for the commit
2487 # text to show up.
David Keitele288d232016-03-23 20:57:21 -07002488 $shorttext = CHECK_NEXT_SHORTTEXT;
2489 $subjectline = $line;
2490 $sublinenr = "#$linenr & ";
2491# Check for Subject line less than line limit
Matt Wagantalle324fc92012-08-18 11:21:00 -07002492 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002493 WARN("LONG_SUMMARY_LINE",
2494 "summary line over " .
2495 SHORTTEXT_LIMIT .
2496 " characters\n" . $herecurr);
2497 }
2498 } elsif ($line=~/^ (.*)/) {
Steve Muckle9ed561d2012-03-05 10:12:04 -08002499 # Indented format, this must be the summary
2500 # line (i.e. git show). There will be no more
2501 # headers so we are now in the shorttext.
2502 $shorttext = IN_SHORTTEXT_BLANKLINE;
David Keitel72bf3e82016-03-23 20:52:44 -07002503 $shorttext_exspc = 4;
Matt Wagantalle324fc92012-08-18 11:21:00 -07002504 if (length($1) > SHORTTEXT_LIMIT && !($1 =~ m/Revert\ \"/)) {
David Keitel72bf3e82016-03-23 20:52:44 -07002505 WARN("LONG_SUMMARY_LINE",
2506 "summary line over " .
2507 SHORTTEXT_LIMIT .
2508 " characters\n" . $herecurr);
2509 }
2510 }
2511 }
2512
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002513 $cnt_lines++ if ($realcnt != 0);
2514
Joe Perchese518e9a2015-06-25 15:03:27 -07002515# Check if the commit log has what seems like a diff which can confuse patch
2516 if ($in_commit_log && !$commit_log_has_diff &&
2517 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2518 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2519 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2520 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2521 ERROR("DIFF_IN_COMMIT_MSG",
2522 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2523 $commit_log_has_diff = 1;
2524 }
2525
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002526# Check for incorrect file permissions
2527 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2528 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002529 if ($realfile !~ m@scripts/@ &&
2530 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002531 ERROR("EXECUTE_PERMISSIONS",
2532 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002533 }
2534 }
2535
Joe Perches20112472011-07-25 17:13:23 -07002536# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002537 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002538 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002539 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002540 }
2541
Joe Perchese0d975b2014-12-10 15:51:49 -08002542# Check if MAINTAINERS is being updated. If so, there's probably no need to
2543# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2544 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2545 $reported_maintainer_file = 1;
2546 }
2547
Joe Perches20112472011-07-25 17:13:23 -07002548# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002549 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002550 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002551 my $space_before = $1;
2552 my $sign_off = $2;
2553 my $space_after = $3;
2554 my $email = $4;
2555 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2556
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002557 if ($sign_off !~ /$signature_tags/) {
2558 WARN("BAD_SIGN_OFF",
2559 "Non-standard signature: $sign_off\n" . $herecurr);
2560 }
Joe Perches20112472011-07-25 17:13:23 -07002561 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002562 if (WARN("BAD_SIGN_OFF",
2563 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2564 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002565 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002566 "$ucfirst_sign_off $email";
2567 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002568 }
Joe Perches20112472011-07-25 17:13:23 -07002569 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002570 if (WARN("BAD_SIGN_OFF",
2571 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002573 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002574 "$ucfirst_sign_off $email";
2575 }
2576
Joe Perches20112472011-07-25 17:13:23 -07002577 }
2578 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if (WARN("BAD_SIGN_OFF",
2580 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2581 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002582 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002583 "$ucfirst_sign_off $email";
2584 }
Joe Perches20112472011-07-25 17:13:23 -07002585 }
2586
2587 my ($email_name, $email_address, $comment) = parse_email($email);
2588 my $suggested_email = format_email(($email_name, $email_address));
2589 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002590 ERROR("BAD_SIGN_OFF",
2591 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002592 } else {
2593 my $dequoted = $suggested_email;
2594 $dequoted =~ s/^"//;
2595 $dequoted =~ s/" </ </;
2596 # Don't force email to have quotes
2597 # Allow just an angle bracketed address
2598 if ("$dequoted$comment" ne $email &&
2599 "<$email_address>$comment" ne $email &&
2600 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002601 WARN("BAD_SIGN_OFF",
2602 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002603 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002604 }
Maya Erezec77bd62017-02-26 09:14:43 +02002605 if ($chk_author) {
2606 if ($line =~ /^\s*signed-off-by:.*qca\.qualcomm\.com/i) {
2607 $qca_sign_off = 1;
2608 } elsif ($line =~ /^\s*signed-off-by:.*codeaurora\.org/i) {
2609 $codeaurora_sign_off = 1;
2610 } elsif ($line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
2611 WARN("BAD_SIGN_OFF",
2612 "invalid Signed-off-by identity\n" . $line );
2613 }
2614 }
Joe Perches7e51f192013-09-11 14:23:57 -07002615
2616# Check for duplicate signatures
2617 my $sig_nospace = $line;
2618 $sig_nospace =~ s/\s//g;
2619 $sig_nospace = lc($sig_nospace);
2620 if (defined $signatures{$sig_nospace}) {
2621 WARN("BAD_SIGN_OFF",
2622 "Duplicate signature\n" . $herecurr);
2623 } else {
2624 $signatures{$sig_nospace} = 1;
2625 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002626 }
2627
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002628# Check email subject for common tools that don't need to be mentioned
2629 if ($in_header_lines &&
2630 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2631 WARN("EMAIL_SUBJECT",
2632 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2633 }
2634
Joe Perches9b3189e2014-06-04 16:12:10 -07002635# Check for old stable address
2636 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2637 ERROR("STABLE_ADDRESS",
2638 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2639 }
2640
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002641# Check for unwanted Gerrit info
2642 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2643 ERROR("GERRIT_CHANGE_ID",
2644 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2645 }
2646
Joe Perches369c8dd2015-11-06 16:31:34 -08002647# Check if the commit log is in a possible stack dump
2648 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2649 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2650 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2651 # timestamp
2652 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2653 # stack dump address
2654 $commit_log_possible_stack_dump = 1;
2655 }
2656
Joe Perches2a076f42015-04-16 12:44:28 -07002657# Check for line lengths > 75 in commit log, warn once
2658 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002659 length($line) > 75 &&
2660 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2661 # file delta changes
2662 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2663 # filename then :
2664 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2665 # A Fixes: or Link: line
2666 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002667 WARN("COMMIT_LOG_LONG_LINE",
2668 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2669 $commit_log_long_line = 1;
2670 }
2671
Joe Perchesbf4daf12015-09-09 15:37:50 -07002672# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002673 if ($in_commit_log && $commit_log_possible_stack_dump &&
2674 $line =~ /^\s*$/) {
2675 $commit_log_possible_stack_dump = 0;
2676 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002677
Joe Perches0d7835f2015-02-13 14:38:35 -08002678# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002679 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002680 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wang5b101522017-04-07 15:22:19 -07002681 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002682 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002683 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002684 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2685 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002686 my $init_char = "c";
2687 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002688 my $short = 1;
2689 my $long = 0;
2690 my $case = 1;
2691 my $space = 1;
2692 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002693 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002694 my $id = '0123456789ab';
2695 my $orig_desc = "commit description";
2696 my $description = "";
2697
Joe Perchesfe043ea2015-09-09 15:37:25 -07002698 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2699 $init_char = $1;
2700 $orig_commit = lc($2);
2701 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2702 $orig_commit = lc($1);
2703 }
2704
Joe Perches0d7835f2015-02-13 14:38:35 -08002705 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2706 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2707 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2708 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2709 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2710 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002711 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002712 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2713 defined $rawlines[$linenr] &&
2714 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2715 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002716 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002717 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2718 defined $rawlines[$linenr] &&
2719 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2720 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2721 $orig_desc = $1;
2722 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2723 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002724 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002725 }
2726
2727 ($id, $description) = git_commit_info($orig_commit,
2728 $id, $orig_desc);
2729
Joe Perches19c146a2015-02-13 14:39:00 -08002730 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002731 ERROR("GIT_COMMIT_ID",
2732 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2733 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002734 }
2735
Joe Perches13f19372014-08-06 16:10:59 -07002736# Check for added, moved or deleted files
2737 if (!$reported_maintainer_file && !$in_commit_log &&
2738 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2739 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2740 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2741 (defined($1) || defined($2))))) {
2742 $reported_maintainer_file = 1;
2743 WARN("FILE_PATH_CHANGES",
2744 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2745 }
2746
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002747#check the patch for invalid author credentials
Maya Erezec77bd62017-02-26 09:14:43 +02002748 if ($chk_author && !($line =~ /^From:.*qca\.qualcomm\.com/) &&
2749 $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002750 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2751 }
2752
Andy Whitcroft00df3442007-06-08 13:47:06 -07002753# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002754 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002755 ERROR("CORRUPTED_PATCH",
2756 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002757 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002758 }
2759
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002760# Check for absolute kernel paths.
2761 if ($tree) {
2762 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2763 my $file = $1;
2764
2765 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2766 check_absolute_file($1, $herecurr)) {
2767 #
2768 } else {
2769 check_absolute_file($file, $herecurr);
2770 }
2771 }
2772 }
2773
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002774# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2775 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002776 $rawline !~ m/^$UTF8*$/) {
2777 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2778
2779 my $blank = copy_spacing($rawline);
2780 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2781 my $hereptr = "$hereline$ptr\n";
2782
Joe Perches34d99212011-07-25 17:13:26 -07002783 CHK("INVALID_UTF8",
2784 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002785 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002786
Joe Perches15662b32011-10-31 17:13:12 -07002787# Check if it's the start of a commit log
2788# (not a header line and we haven't seen the patch filename)
2789 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002790 !($rawline =~ /^\s+\S/ ||
2791 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002792 $in_header_lines = 0;
2793 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002794 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002795 }
2796
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002797# Check if there is UTF-8 in a commit log when a mail header has explicitly
2798# declined it, i.e defined some charset where it is missing.
2799 if ($in_header_lines &&
2800 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2801 $1 !~ /utf-8/i) {
2802 $non_utf8_charset = 1;
2803 }
2804
2805 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002806 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002807 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002808 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2809 }
2810
Kees Cook66b47b42014-10-13 15:51:57 -07002811# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002812 if (defined($misspellings) &&
2813 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002814 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002815 my $typo = $1;
2816 my $typo_fix = $spelling_fix{lc($typo)};
2817 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2818 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2819 my $msg_type = \&WARN;
2820 $msg_type = \&CHK if ($file);
2821 if (&{$msg_type}("TYPO_SPELLING",
2822 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2823 $fix) {
2824 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2825 }
2826 }
2827 }
2828
Andy Whitcroft306708542008-10-15 22:02:28 -07002829# ignore non-hunk lines and lines being removed
2830 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002831
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002832#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002833 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002834 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002835 if (ERROR("DOS_LINE_ENDINGS",
2836 "DOS line endings\n" . $herevet) &&
2837 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002838 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002839 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002840 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2841 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002842 if (ERROR("TRAILING_WHITESPACE",
2843 "trailing whitespace\n" . $herevet) &&
2844 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002845 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002846 }
2847
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002848 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002849 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002850
Josh Triplett4783f892013-11-12 15:10:12 -08002851# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002852 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002853 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2854 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002855 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2856 my $msg_type = \&ERROR;
2857 $msg_type = \&CHK if ($file);
2858 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002859 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
Josh Triplett4783f892013-11-12 15:10:12 -08002860 }
2861
Andi Kleen33549572010-05-24 14:33:29 -07002862# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002863# Only applies when adding the entry originally, after that we do not have
2864# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002865 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002866 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002867 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002868 my $cnt = $realcnt;
2869 my $ln = $linenr + 1;
2870 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002871 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002872 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002873 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002874 $f = $lines[$ln - 1];
2875 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2876 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002877
2878 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002879 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002880
Joe Perches8d73e0e2014-08-06 16:10:46 -07002881 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002882 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002883 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002884 $length = -1;
2885 }
2886
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002887 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002888 $f =~ s/#.*//;
2889 $f =~ s/^\s+//;
2890 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002891 if ($f =~ /^\s*config\s/) {
2892 $is_end = 1;
2893 last;
2894 }
Andi Kleen33549572010-05-24 14:33:29 -07002895 $length++;
2896 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002897 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2898 WARN("CONFIG_DESCRIPTION",
2899 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2900 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002901 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002902 }
2903
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002904# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2905 if ($realfile =~ /Kconfig/ &&
2906 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2907 WARN("CONFIG_EXPERIMENTAL",
2908 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2909 }
2910
Christoph Jaeger327953e2015-02-13 14:38:29 -08002911# discourage the use of boolean for type definition attributes of Kconfig options
2912 if ($realfile =~ /Kconfig/ &&
2913 $line =~ /^\+\s*\bboolean\b/) {
2914 WARN("CONFIG_TYPE_BOOLEAN",
2915 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2916 }
2917
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002918 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2919 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2920 my $flag = $1;
2921 my $replacement = {
2922 'EXTRA_AFLAGS' => 'asflags-y',
2923 'EXTRA_CFLAGS' => 'ccflags-y',
2924 'EXTRA_CPPFLAGS' => 'cppflags-y',
2925 'EXTRA_LDFLAGS' => 'ldflags-y',
2926 };
2927
2928 WARN("DEPRECATED_VARIABLE",
2929 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2930 }
2931
Rob Herringbff5da42014-01-23 15:54:51 -08002932# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002933 if (defined $root &&
2934 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2935 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2936
Rob Herringbff5da42014-01-23 15:54:51 -08002937 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2938
Florian Vaussardcc933192014-04-03 14:49:27 -07002939 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2940 my $vp_file = $dt_path . "vendor-prefixes.txt";
2941
Rob Herringbff5da42014-01-23 15:54:51 -08002942 foreach my $compat (@compats) {
2943 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002944 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2945 my $compat3 = $compat;
2946 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2947 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002948 if ( $? >> 8 ) {
2949 WARN("UNDOCUMENTED_DT_STRING",
2950 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2951 }
2952
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002953 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2954 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002955 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002956 if ( $? >> 8 ) {
2957 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002958 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002959 }
2960 }
2961 }
2962
Andy Whitcroft5368df22008-10-15 22:02:27 -07002963# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002964 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002965
Joe Perches47e0c882015-06-25 15:02:57 -07002966# line length limit (with some exclusions)
2967#
2968# There are a few types of lines that may extend beyond $max_line_length:
2969# logging functions like pr_info that end in a string
2970# lines with a single string
2971# #defines that are a single string
2972#
2973# There are 3 different line length message types:
2974# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2975# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2976# LONG_LINE all other lines longer than $max_line_length
2977#
2978# if LONG_LINE is ignored, the other 2 types are also ignored
2979#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002980 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002981 my $msg_type = "LONG_LINE";
2982
2983 # Check the allowed long line types first
2984
2985 # logging functions that end in a string that starts
2986 # before $max_line_length
2987 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2988 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2989 $msg_type = "";
2990
2991 # lines with only strings (w/ possible termination)
2992 # #defines with only strings
2993 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2994 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2995 $msg_type = "";
2996
Joe Perchesd560a5f2016-08-02 14:04:31 -07002997 # EFI_GUID is another special case
2998 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2999 $msg_type = "";
3000
Joe Perches47e0c882015-06-25 15:02:57 -07003001 # Otherwise set the alternate message types
3002
3003 # a comment starts before $max_line_length
3004 } elsif ($line =~ /($;[\s$;]*)$/ &&
3005 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3006 $msg_type = "LONG_LINE_COMMENT"
3007
3008 # a quoted string starts before $max_line_length
3009 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3010 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3011 $msg_type = "LONG_LINE_STRING"
3012 }
3013
3014 if ($msg_type ne "" &&
3015 (show_type("LONG_LINE") || show_type($msg_type))) {
3016 WARN($msg_type,
3017 "line over $max_line_length characters\n" . $herecurr);
3018 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003019 }
3020
Andy Whitcroft8905a672007-11-28 16:21:06 -08003021# check for adding lines without a newline.
3022 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003023 WARN("MISSING_EOF_NEWLINE",
3024 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003025 }
3026
Mike Frysinger42e41c52009-09-21 17:04:40 -07003027# Blackfin: use hi/lo macros
3028 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
3029 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
3030 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003031 ERROR("LO_MACRO",
3032 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003033 }
3034 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
3035 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003036 ERROR("HI_MACRO",
3037 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003038 }
3039 }
3040
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003041# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07003042 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003043
3044# at the beginning of a line any tabs must come first and anything
3045# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003046 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3047 $rawline =~ /^\+\s* \s*/) {
3048 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07003049 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003050 if (ERROR("CODE_INDENT",
3051 "code indent should use tabs where possible\n" . $herevet) &&
3052 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003053 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003054 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003055 }
3056
Alberto Panizzo08e44362010-03-05 13:43:54 -08003057# check for space before tabs.
3058 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3059 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003060 if (WARN("SPACE_BEFORE_TAB",
3061 "please, no space before tabs\n" . $herevet) &&
3062 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003063 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07003064 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07003065 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08003066 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07003067 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08003068 }
3069
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003070# check for && or || at the start of a line
3071 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3072 CHK("LOGICAL_CONTINUATIONS",
3073 "Logical continuations should be on the previous line\n" . $hereprev);
3074 }
3075
Joe Perchesa91e8992016-05-20 17:04:05 -07003076# check indentation starts on a tab stop
3077 if ($^V && $^V ge 5.10.0 &&
3078 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
3079 my $indent = length($1);
3080 if ($indent % 8) {
3081 if (WARN("TABSTOP",
3082 "Statements should start on a tabstop\n" . $herecurr) &&
3083 $fix) {
3084 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3085 }
3086 }
3087 }
3088
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003089# check multi-line statement indentation matches previous line
3090 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07003091 $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 -07003092 $prevline =~ /^\+(\t*)(.*)$/;
3093 my $oldindent = $1;
3094 my $rest = $2;
3095
3096 my $pos = pos_last_openparen($rest);
3097 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003098 $line =~ /^(\+| )([ \t]*)/;
3099 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003100
3101 my $goodtabindent = $oldindent .
3102 "\t" x ($pos / 8) .
3103 " " x ($pos % 8);
3104 my $goodspaceindent = $oldindent . " " x $pos;
3105
3106 if ($newindent ne $goodtabindent &&
3107 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003108
3109 if (CHK("PARENTHESIS_ALIGNMENT",
3110 "Alignment should match open parenthesis\n" . $hereprev) &&
3111 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003112 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003113 s/^\+[ \t]*/\+$goodtabindent/;
3114 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003115 }
3116 }
3117 }
3118
Joe Perches6ab3a972015-04-16 12:44:05 -07003119# check for space after cast like "(int) foo" or "(struct foo) bar"
3120# avoid checking a few false positives:
3121# "sizeof(<type>)" or "__alignof__(<type>)"
3122# function pointer declarations like "(*foo)(int) = bar;"
3123# structure definitions like "(struct foo) { 0 };"
3124# multiline macros that define functions
3125# known attributes or the __attribute__ keyword
3126 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3127 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003128 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003129 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003130 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003131 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003132 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003133 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003134 }
3135
Joe Perches86406b12015-09-09 15:37:41 -07003136# Block comment styles
3137# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003138 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003139 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003140 $rawline =~ /^\+[ \t]*\*/ &&
3141 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003142 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3143 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3144 }
3145
Joe Perches86406b12015-09-09 15:37:41 -07003146# Block comments use * on subsequent lines
3147 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3148 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003149 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003150 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003151 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003152 WARN("BLOCK_COMMENT_STYLE",
3153 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003154 }
3155
Joe Perches86406b12015-09-09 15:37:41 -07003156# Block comments use */ on trailing lines
3157 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003158 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3159 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3160 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003161 WARN("BLOCK_COMMENT_STYLE",
3162 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003163 }
3164
Joe Perches08eb9b82016-10-11 13:51:50 -07003165# Block comment * alignment
3166 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003167 $line =~ /^\+[ \t]*$;/ && #leading comment
3168 $rawline =~ /^\+[ \t]*\*/ && #leading *
3169 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003170 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003171 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3172 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003173 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003174 if (defined($1)) {
3175 $oldindent = expand_tabs($1);
3176 } else {
3177 $prevrawline =~ m@^\+(.*/?)\*@;
3178 $oldindent = expand_tabs($1);
3179 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003180 $rawline =~ m@^\+([ \t]*)\*@;
3181 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003182 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003183 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003184 WARN("BLOCK_COMMENT_STYLE",
3185 "Block comments should align the * on each line\n" . $hereprev);
3186 }
3187 }
3188
Joe Perches7f619192014-08-06 16:10:39 -07003189# check for missing blank lines after struct/union declarations
3190# with exceptions for various attributes and macros
3191 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3192 $line =~ /^\+/ &&
3193 !($line =~ /^\+\s*$/ ||
3194 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3195 $line =~ /^\+\s*MODULE_/i ||
3196 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3197 $line =~ /^\+[a-z_]*init/ ||
3198 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3199 $line =~ /^\+\s*DECLARE/ ||
3200 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003201 if (CHK("LINE_SPACING",
3202 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3203 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003204 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003205 }
Joe Perches7f619192014-08-06 16:10:39 -07003206 }
3207
Joe Perches365dd4e2014-08-06 16:10:42 -07003208# check for multiple consecutive blank lines
3209 if ($prevline =~ /^[\+ ]\s*$/ &&
3210 $line =~ /^\+\s*$/ &&
3211 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003212 if (CHK("LINE_SPACING",
3213 "Please don't use multiple blank lines\n" . $hereprev) &&
3214 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003215 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003216 }
3217
Joe Perches365dd4e2014-08-06 16:10:42 -07003218 $last_blank_line = $linenr;
3219 }
3220
Joe Perches3b617e32014-04-03 14:49:28 -07003221# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003222 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3223 # actual declarations
3224 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003225 # function pointer declarations
3226 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003227 # foo bar; where foo is some local typedef or #define
3228 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3229 # known declaration macros
3230 $prevline =~ /^\+\s+$declaration_macros/) &&
3231 # for "else if" which can look like "$Ident $Ident"
3232 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3233 # other possible extensions of declaration lines
3234 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3235 # not starting a section or a macro "\" extended line
3236 $prevline =~ /(?:\{\s*|\\)$/) &&
3237 # looks like a declaration
3238 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003239 # function pointer declarations
3240 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003241 # foo bar; where foo is some local typedef or #define
3242 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3243 # known declaration macros
3244 $sline =~ /^\+\s+$declaration_macros/ ||
3245 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003246 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003247 # start or end of block or continuation of declaration
3248 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3249 # bitfield continuation
3250 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3251 # other possible extensions of declaration lines
3252 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3253 # indentation of previous and current line are the same
3254 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003255 if (WARN("LINE_SPACING",
3256 "Missing a blank line after declarations\n" . $hereprev) &&
3257 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003258 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003259 }
Joe Perches3b617e32014-04-03 14:49:28 -07003260 }
3261
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003262# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003263# Exceptions:
3264# 1) within comments
3265# 2) indented preprocessor commands
3266# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003267 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003268 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003269 if (WARN("LEADING_SPACE",
3270 "please, no spaces at the start of a line\n" . $herevet) &&
3271 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003272 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003273 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003274 }
3275
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003276# check we are in a valid C source file if not then ignore this hunk
3277 next if ($realfile !~ /\.(h|c)$/);
3278
Joe Perches032a4c02014-08-06 16:10:29 -07003279# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003280# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003281# if the previous line is a break or return and is indented 1 tab more...
3282 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3283 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003284 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3285 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3286 defined $lines[$linenr] &&
3287 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003288 WARN("UNNECESSARY_ELSE",
3289 "else is not generally useful after a break or return\n" . $hereprev);
3290 }
3291 }
3292
Joe Perchesc00df192014-08-06 16:11:01 -07003293# check indentation of a line with a break;
3294# if the previous line is a goto or return and is indented the same # of tabs
3295 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3296 my $tabs = $1;
3297 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3298 WARN("UNNECESSARY_BREAK",
3299 "break is not useful after a goto or return\n" . $hereprev);
3300 }
3301 }
3302
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003303# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3304 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3305 WARN("CONFIG_EXPERIMENTAL",
3306 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3307 }
3308
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003309# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003310 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003311 WARN("CVS_KEYWORD",
3312 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003313 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003314
Mike Frysinger42e41c52009-09-21 17:04:40 -07003315# Blackfin: don't use __builtin_bfin_[cs]sync
3316 if ($line =~ /__builtin_bfin_csync/) {
3317 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003318 ERROR("CSYNC",
3319 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003320 }
3321 if ($line =~ /__builtin_bfin_ssync/) {
3322 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003323 ERROR("SSYNC",
3324 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003325 }
3326
Joe Perches56e77d72013-02-21 16:44:14 -08003327# check for old HOTPLUG __dev<foo> section markings
3328 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3329 WARN("HOTPLUG_SECTION",
3330 "Using $1 is unnecessary\n" . $herecurr);
3331 }
3332
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003333# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003334 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3335 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003336#print "LINE<$line>\n";
3337 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003338 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003339 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003340 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003341 $stat =~ s/\n./\n /g;
3342 $cond =~ s/\n./\n /g;
3343
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003344#print "linenr<$linenr> <$stat>\n";
3345 # If this statement has no statement boundaries within
3346 # it there is no point in retrying a statement scan
3347 # until we hit end of it.
3348 my $frag = $stat; $frag =~ s/;+\s*$//;
3349 if ($frag !~ /(?:{|;)/) {
3350#print "skip<$line_nr_next>\n";
3351 $suppress_statement = $line_nr_next;
3352 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003353
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003354 # Find the real next line.
3355 $realline_next = $line_nr_next;
3356 if (defined $realline_next &&
3357 (!defined $lines[$realline_next - 1] ||
3358 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3359 $realline_next++;
3360 }
3361
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003362 my $s = $stat;
3363 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003364
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003365 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003366 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003367
3368 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003369 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003370
Andy Whitcroft463f2862009-09-21 17:04:34 -07003371 } elsif ($s =~ /^.\s*else\b/s) {
3372
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003373 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003374 } 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 -07003375 my $type = $1;
3376 $type =~ s/\s+/ /g;
3377 possible($type, "A:" . $s);
3378
Andy Whitcroft8905a672007-11-28 16:21:06 -08003379 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003380 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003381 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003382 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003383
3384 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003385 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003386 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003387 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003388
3389 # Check for any sort of function declaration.
3390 # int foo(something bar, other baz);
3391 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003392 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 -08003393 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003394
Andy Whitcroftcf655042008-03-04 14:28:20 -08003395 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003396 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003397 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003398
Andy Whitcroft8905a672007-11-28 16:21:06 -08003399 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003400 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003401
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003402 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003403 }
3404 }
3405 }
3406
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003407 }
3408
Andy Whitcroft653d4872007-06-23 17:16:34 -07003409#
3410# Checks which may be anchored in the context.
3411#
3412
3413# Check for switch () and associated case and default
3414# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003415 if ($line=~/\bswitch\s*\(.*\)/) {
3416 my $err = '';
3417 my $sep = '';
3418 my @ctx = ctx_block_outer($linenr, $realcnt);
3419 shift(@ctx);
3420 for my $ctx (@ctx) {
3421 my ($clen, $cindent) = line_stats($ctx);
3422 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3423 $indent != $cindent) {
3424 $err .= "$sep$ctx\n";
3425 $sep = '';
3426 } else {
3427 $sep = "[...]\n";
3428 }
3429 }
3430 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003431 ERROR("SWITCH_CASE_INDENT_LEVEL",
3432 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003433 }
3434 }
3435
3436# if/while/etc brace do not go on next line, unless defining a do while loop,
3437# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003438 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 -07003439 my $pre_ctx = "$1$2";
3440
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003441 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003442
3443 if ($line =~ /^\+\t{6,}/) {
3444 WARN("DEEP_INDENTATION",
3445 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3446 }
3447
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003448 my $ctx_cnt = $realcnt - $#ctx - 1;
3449 my $ctx = join("\n", @ctx);
3450
Andy Whitcroft548596d2008-07-23 21:29:01 -07003451 my $ctx_ln = $linenr;
3452 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003453
Andy Whitcroft548596d2008-07-23 21:29:01 -07003454 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3455 defined $lines[$ctx_ln - 1] &&
3456 $lines[$ctx_ln - 1] =~ /^-/)) {
3457 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3458 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003459 $ctx_ln++;
3460 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003461
Andy Whitcroft53210162008-07-23 21:29:03 -07003462 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3463 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003464
Joe Perchesd752fcc2014-08-06 16:11:05 -07003465 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003466 ERROR("OPEN_BRACE",
3467 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003468 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003469 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003470 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3471 $ctx =~ /\)\s*\;\s*$/ &&
3472 defined $lines[$ctx_ln - 1])
3473 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003474 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3475 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003476 WARN("TRAILING_SEMICOLON",
3477 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003478 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003479 }
3480 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003481 }
3482
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003483# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003484 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 -08003485 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3486 ctx_statement_block($linenr, $realcnt, 0)
3487 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003488 my ($s, $c) = ($stat, $cond);
3489
3490 substr($s, 0, length($c), '');
3491
Joe Perches9f5af482015-09-09 15:37:30 -07003492 # remove inline comments
3493 $s =~ s/$;/ /g;
3494 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003495
3496 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003497 my @newlines = ($c =~ /\n/gs);
3498 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003499
Joe Perches9f5af482015-09-09 15:37:30 -07003500 # Make sure we remove the line prefixes as we have
3501 # none on the first line, and are going to readd them
3502 # where necessary.
3503 $s =~ s/\n./\n/gs;
3504 while ($s =~ /\n\s+\\\n/) {
3505 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3506 }
3507
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003508 # We want to check the first line inside the block
3509 # starting at the end of the conditional, so remove:
3510 # 1) any blank line termination
3511 # 2) any opening brace { on end of the line
3512 # 3) any do (...) {
3513 my $continuation = 0;
3514 my $check = 0;
3515 $s =~ s/^.*\bdo\b//;
3516 $s =~ s/^\s*{//;
3517 if ($s =~ s/^\s*\\//) {
3518 $continuation = 1;
3519 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003520 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003521 $check = 1;
3522 $cond_lines++;
3523 }
3524
3525 # Also ignore a loop construct at the end of a
3526 # preprocessor statement.
3527 if (($prevline =~ /^.\s*#\s*define\s/ ||
3528 $prevline =~ /\\\s*$/) && $continuation == 0) {
3529 $check = 0;
3530 }
3531
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003532 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003533 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003534 while ($cond_ptr != $cond_lines) {
3535 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003536
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003537 # If we see an #else/#elif then the code
3538 # is not linear.
3539 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3540 $check = 0;
3541 }
3542
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003543 # Ignore:
3544 # 1) blank lines, they should be at 0,
3545 # 2) preprocessor lines, and
3546 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003547 if ($continuation ||
3548 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003549 $s =~ /^\s*#\s*?/ ||
3550 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003551 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003552 if ($s =~ s/^.*?\n//) {
3553 $cond_lines++;
3554 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003555 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003556 }
3557
3558 my (undef, $sindent) = line_stats("+" . $s);
3559 my $stat_real = raw_line($linenr, $cond_lines);
3560
3561 # Check if either of these lines are modified, else
3562 # this is not this patch's fault.
3563 if (!defined($stat_real) ||
3564 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3565 $check = 0;
3566 }
3567 if (defined($stat_real) && $cond_lines > 1) {
3568 $stat_real = "[...]\n$stat_real";
3569 }
3570
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003571 #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 -07003572
Joe Perches9f5af482015-09-09 15:37:30 -07003573 if ($check && $s ne '' &&
3574 (($sindent % 8) != 0 ||
3575 ($sindent < $indent) ||
3576 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003577 WARN("SUSPECT_CODE_INDENT",
3578 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003579 }
3580 }
3581
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003582 # Track the 'values' across context and added lines.
3583 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003584 my ($curr_values, $curr_vars) =
3585 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003586 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003587 if ($dbg_values) {
3588 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003589 print "$linenr > .$outline\n";
3590 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003591 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003592 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003593 $prev_values = substr($curr_values, -1);
3594
Andy Whitcroft00df3442007-06-08 13:47:06 -07003595#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003596 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003597
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003598# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003599 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 -07003600 my $type = $1;
3601 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003602 $var = "" if (!defined $var);
3603 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003604 my $sign = $1;
3605 my $pointer = $2;
3606
3607 $pointer = "" if (!defined $pointer);
3608
3609 if (WARN("UNSPECIFIED_INT",
3610 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3611 $fix) {
3612 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003613 my $comp_pointer = $pointer;
3614 $comp_pointer =~ s/\s//g;
3615 $decl .= $comp_pointer;
3616 $decl = rtrim($decl) if ($var eq "");
3617 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003618 }
3619 }
3620 }
3621
Andy Whitcroft653d4872007-06-23 17:16:34 -07003622# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003623 if ($dbg_type) {
3624 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003625 ERROR("TEST_TYPE",
3626 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003627 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003628 ERROR("TEST_NOT_TYPE",
3629 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003630 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003631 next;
3632 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003633# TEST: allow direct testing of the attribute matcher.
3634 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003635 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003636 ERROR("TEST_ATTR",
3637 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003638 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003639 ERROR("TEST_NOT_ATTR",
3640 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003641 }
3642 next;
3643 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003644
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003645# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003646 if ($line =~ /^.\s*{/ &&
3647 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003648 if (ERROR("OPEN_BRACE",
3649 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003650 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3651 fix_delete_line($fixlinenr - 1, $prevrawline);
3652 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003653 my $fixedline = $prevrawline;
3654 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003655 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003656 $fixedline = $line;
3657 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003658 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003659 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003660 }
3661
Andy Whitcroft653d4872007-06-23 17:16:34 -07003662#
3663# Checks which are anchored on the added line.
3664#
3665
3666# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003667 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003668 my $path = $1;
3669 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003670 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003671 "malformed #include filename\n" . $herecurr);
3672 }
3673 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3674 ERROR("UAPI_INCLUDE",
3675 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003676 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003677 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003678
3679# no C99 // comments
3680 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003681 if (ERROR("C99_COMMENTS",
3682 "do not use C99 // comments\n" . $herecurr) &&
3683 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003684 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003685 if ($line =~ /\/\/(.*)$/) {
3686 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003687 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003688 }
3689 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003690 }
3691 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003692 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003693 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003694
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003695# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3696# the whole statement.
3697#print "APW <$lines[$realline_next - 1]>\n";
3698 if (defined $realline_next &&
3699 exists $lines[$realline_next - 1] &&
3700 !defined $suppress_export{$realline_next} &&
3701 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3702 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003703 # Handle definitions which produce identifiers with
3704 # a prefix:
3705 # XXX(foo);
3706 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003707 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003708 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003709 $name =~ /^${Ident}_$2/) {
3710#print "FOO C name<$name>\n";
3711 $suppress_export{$realline_next} = 1;
3712
3713 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003714 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003715 ^.DEFINE_$Ident\(\Q$name\E\)|
3716 ^.DECLARE_$Ident\(\Q$name\E\)|
3717 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003718 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3719 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003720 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003721#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3722 $suppress_export{$realline_next} = 2;
3723 } else {
3724 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003725 }
3726 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003727 if (!defined $suppress_export{$linenr} &&
3728 $prevline =~ /^.\s*$/ &&
3729 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3730 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3731#print "FOO B <$lines[$linenr - 1]>\n";
3732 $suppress_export{$linenr} = 2;
3733 }
3734 if (defined $suppress_export{$linenr} &&
3735 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003736 WARN("EXPORT_SYMBOL",
3737 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003738 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003739
Joe Eloff5150bda2010-08-09 17:21:00 -07003740# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003741 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003742 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003743 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003744 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003745 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003746 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003747 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003748# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003749 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003750 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003751 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003752 $herecurr) &&
3753 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003754 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003755 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003756 }
3757
Joe Perches18130872014-08-06 16:11:22 -07003758# check for misordered declarations of char/short/int/long with signed/unsigned
3759 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3760 my $tmp = trim($1);
3761 WARN("MISORDERED_TYPE",
3762 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3763 }
3764
Joe Perchescb710ec2010-10-26 14:23:20 -07003765# check for static const char * arrays.
3766 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003767 WARN("STATIC_CONST_CHAR_ARRAY",
3768 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003769 $herecurr);
3770 }
3771
3772# check for static char foo[] = "bar" declarations.
3773 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003774 WARN("STATIC_CONST_CHAR_ARRAY",
3775 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003776 $herecurr);
3777 }
3778
Joe Perchesab7e23f2015-04-16 12:44:22 -07003779# check for const <foo> const where <foo> is not a pointer or array type
3780 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3781 my $found = $1;
3782 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3783 WARN("CONST_CONST",
3784 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3785 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3786 WARN("CONST_CONST",
3787 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3788 }
3789 }
3790
Joe Perches9b0fa602014-04-03 14:49:18 -07003791# check for non-global char *foo[] = {"bar", ...} declarations.
3792 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3793 WARN("STATIC_CONST_CHAR_ARRAY",
3794 "char * array declaration might be better as static const\n" .
3795 $herecurr);
3796 }
3797
Joe Perchesb598b672015-04-16 12:44:36 -07003798# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3799 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3800 my $array = $1;
3801 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3802 my $array_div = $1;
3803 if (WARN("ARRAY_SIZE",
3804 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3805 $fix) {
3806 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3807 }
3808 }
3809 }
3810
Joe Perchesb36190c2014-01-27 17:07:18 -08003811# check for function declarations without arguments like "int foo()"
3812 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3813 if (ERROR("FUNCTION_WITHOUT_ARGS",
3814 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3815 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003816 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003817 }
3818 }
3819
Andy Whitcroft653d4872007-06-23 17:16:34 -07003820# check for new typedefs, only function parameters and sparse annotations
3821# make sense.
3822 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003823 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003824 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003825 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003826 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 WARN("NEW_TYPEDEFS",
3828 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003829 }
3830
3831# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003832 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003833 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3834 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003835 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003836
Andy Whitcroft65863862009-01-06 14:41:21 -08003837 # Should start with a space.
3838 $to =~ s/^(\S)/ $1/;
3839 # Should not end with a space.
3840 $to =~ s/\s+$//;
3841 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003842 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003843 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003844
Joe Perches3705ce52013-07-03 15:05:31 -07003845## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003846 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003847 if (ERROR("POINTER_LOCATION",
3848 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3849 $fix) {
3850 my $sub_from = $ident;
3851 my $sub_to = $ident;
3852 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003853 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003854 s@\Q$sub_from\E@$sub_to@;
3855 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003856 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003857 }
3858 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3859 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003860 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003861
Andy Whitcroft65863862009-01-06 14:41:21 -08003862 # Should start with a space.
3863 $to =~ s/^(\S)/ $1/;
3864 # Should not end with a space.
3865 $to =~ s/\s+$//;
3866 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003867 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003868 }
3869 # Modifiers should have spaces.
3870 $to =~ s/(\b$Modifier$)/$1 /;
3871
Joe Perches3705ce52013-07-03 15:05:31 -07003872## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003873 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003874 if (ERROR("POINTER_LOCATION",
3875 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3876 $fix) {
3877
3878 my $sub_from = $match;
3879 my $sub_to = $match;
3880 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003881 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003882 s@\Q$sub_from\E@$sub_to@;
3883 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003884 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003885 }
3886
Joe Perches9d3e3c72015-09-09 15:37:27 -07003887# avoid BUG() or BUG_ON()
3888 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3889 my $msg_type = \&WARN;
3890 $msg_type = \&CHK if ($file);
3891 &{$msg_type}("AVOID_BUG",
3892 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3893 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003894
Joe Perches9d3e3c72015-09-09 15:37:27 -07003895# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003896 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003897 WARN("LINUX_VERSION_CODE",
3898 "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 -08003899 }
3900
Joe Perches17441222011-06-15 15:08:17 -07003901# check for uses of printk_ratelimit
3902 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003903 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003904 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003905 }
3906
Andy Whitcroft00df3442007-06-08 13:47:06 -07003907# printk should use KERN_* levels. Note that follow on printk's on the
3908# same line do not need a level, so we use the current block context
3909# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003910# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003911# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003912 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003913 my $ok = 0;
3914 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3915 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003916 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003917 # with "\n" ignore it, else it is to blame
3918 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3919 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3920 $ok = 1;
3921 }
3922 last;
3923 }
3924 }
3925 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003926 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3927 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003928 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003929 }
3930
Joe Perches243f3802012-05-31 16:26:09 -07003931 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3932 my $orig = $1;
3933 my $level = lc($orig);
3934 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003935 my $level2 = $level;
3936 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003937 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003938 "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 -07003939 }
3940
3941 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003942 if (WARN("PREFER_PR_LEVEL",
3943 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3944 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003945 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003946 s/\bpr_warning\b/pr_warn/;
3947 }
Joe Perches243f3802012-05-31 16:26:09 -07003948 }
3949
Joe Perchesdc139312013-02-21 16:44:13 -08003950 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3951 my $orig = $1;
3952 my $level = lc($orig);
3953 $level = "warn" if ($level eq "warning");
3954 $level = "dbg" if ($level eq "debug");
3955 WARN("PREFER_DEV_LEVEL",
3956 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3957 }
3958
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003959# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3960# number of false positives, but assembly files are not checked, so at
3961# least the arch entry code will not trigger this warning.
3962 if ($line =~ /\bENOSYS\b/) {
3963 WARN("ENOSYS",
3964 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3965 }
3966
Andy Whitcroft653d4872007-06-23 17:16:34 -07003967# function brace can't be on same line, except for #defines of do while,
3968# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003969 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003970 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003971 if (ERROR("OPEN_BRACE",
3972 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3973 $fix) {
3974 fix_delete_line($fixlinenr, $rawline);
3975 my $fixed_line = $rawline;
3976 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3977 my $line1 = $1;
3978 my $line2 = $2;
3979 fix_insert_line($fixlinenr, ltrim($line1));
3980 fix_insert_line($fixlinenr, "\+{");
3981 if ($line2 !~ /^\s*$/) {
3982 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3983 }
3984 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003985 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003986
Andy Whitcroft8905a672007-11-28 16:21:06 -08003987# open braces for enum, union and struct go on the same line.
3988 if ($line =~ /^.\s*{/ &&
3989 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003990 if (ERROR("OPEN_BRACE",
3991 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3992 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3993 fix_delete_line($fixlinenr - 1, $prevrawline);
3994 fix_delete_line($fixlinenr, $rawline);
3995 my $fixedline = rtrim($prevrawline) . " {";
3996 fix_insert_line($fixlinenr, $fixedline);
3997 $fixedline = $rawline;
3998 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3999 if ($fixedline !~ /^\+\s*$/) {
4000 fix_insert_line($fixlinenr, $fixedline);
4001 }
4002 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004003 }
4004
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004005# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07004006 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4007 if (WARN("SPACING",
4008 "missing space after $1 definition\n" . $herecurr) &&
4009 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004010 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004011 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4012 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07004013 }
4014
Joe Perches31070b52014-01-23 15:54:49 -08004015# Function pointer declarations
4016# check spacing between type, funcptr, and args
4017# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004018 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08004019 my $declare = $1;
4020 my $pre_pointer_space = $2;
4021 my $post_pointer_space = $3;
4022 my $funcname = $4;
4023 my $post_funcname_space = $5;
4024 my $pre_args_space = $6;
4025
Joe Perches91f72e92014-04-03 14:49:12 -07004026# the $Declare variable will capture all spaces after the type
4027# so check it for a missing trailing missing space but pointer return types
4028# don't need a space so don't warn for those.
4029 my $post_declare_space = "";
4030 if ($declare =~ /(\s+)$/) {
4031 $post_declare_space = $1;
4032 $declare = rtrim($declare);
4033 }
4034 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08004035 WARN("SPACING",
4036 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07004037 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08004038 }
4039
4040# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07004041# This test is not currently implemented because these declarations are
4042# equivalent to
4043# int foo(int bar, ...)
4044# and this is form shouldn't/doesn't generate a checkpatch warning.
4045#
4046# elsif ($declare =~ /\s{2,}$/) {
4047# WARN("SPACING",
4048# "Multiple spaces after return type\n" . $herecurr);
4049# }
Joe Perches31070b52014-01-23 15:54:49 -08004050
4051# unnecessary space "type ( *funcptr)(args...)"
4052 if (defined $pre_pointer_space &&
4053 $pre_pointer_space =~ /^\s/) {
4054 WARN("SPACING",
4055 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4056 }
4057
4058# unnecessary space "type (* funcptr)(args...)"
4059 if (defined $post_pointer_space &&
4060 $post_pointer_space =~ /^\s/) {
4061 WARN("SPACING",
4062 "Unnecessary space before function pointer name\n" . $herecurr);
4063 }
4064
4065# unnecessary space "type (*funcptr )(args...)"
4066 if (defined $post_funcname_space &&
4067 $post_funcname_space =~ /^\s/) {
4068 WARN("SPACING",
4069 "Unnecessary space after function pointer name\n" . $herecurr);
4070 }
4071
4072# unnecessary space "type (*funcptr) (args...)"
4073 if (defined $pre_args_space &&
4074 $pre_args_space =~ /^\s/) {
4075 WARN("SPACING",
4076 "Unnecessary space before function pointer arguments\n" . $herecurr);
4077 }
4078
4079 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004080 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004081 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004082 }
4083 }
4084
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004085# check for spacing round square brackets; allowed:
4086# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004087# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4088# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004089 while ($line =~ /(.*?\s)\[/g) {
4090 my ($where, $prefix) = ($-[1], $1);
4091 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004092 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004093 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004094 if (ERROR("BRACKET_SPACE",
4095 "space prohibited before open square bracket '['\n" . $herecurr) &&
4096 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004097 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004098 s/^(\+.*?)\s+\[/$1\[/;
4099 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004100 }
4101 }
4102
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004103# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004104 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004105 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004106 my $ctx_before = substr($line, 0, $-[1]);
4107 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004108
4109 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004110 if ($name =~ /^(?:
4111 if|for|while|switch|return|case|
4112 volatile|__volatile__|
4113 __attribute__|format|__extension__|
4114 asm|__asm__)$/x)
4115 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004116 # cpp #define statements have non-optional spaces, ie
4117 # if there is a space between the name and the open
4118 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004119 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004120
4121 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004122 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004123
4124 # If this whole things ends with a type its most
4125 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004126 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004127
4128 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004129 if (WARN("SPACING",
4130 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4131 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004132 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004133 s/\b$name\s+\(/$name\(/;
4134 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004135 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004136 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004137
Andy Whitcroft653d4872007-06-23 17:16:34 -07004138# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004139 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004140 my $fixed_line = "";
4141 my $line_fixed = 0;
4142
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004143 my $ops = qr{
4144 <<=|>>=|<=|>=|==|!=|
4145 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4146 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004147 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004148 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004149 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004150 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004151
4152## print("element count: <" . $#elements . ">\n");
4153## foreach my $el (@elements) {
4154## print("el: <$el>\n");
4155## }
4156
4157 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004158 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004159
Joe Perches3705ce52013-07-03 15:05:31 -07004160 foreach my $el (@elements) {
4161 push(@fix_elements, substr($rawline, $off, length($el)));
4162 $off += length($el);
4163 }
4164
4165 $off = 0;
4166
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004167 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004168 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004169
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004170 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004171
4172 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4173
4174## print("n: <$n> good: <$good>\n");
4175
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004176 $off += length($elements[$n]);
4177
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004178 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004179 my $ca = substr($opline, 0, $off);
4180 my $cc = '';
4181 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4182 $cc = substr($opline, $off + length($elements[$n + 1]));
4183 }
4184 my $cb = "$ca$;$cc";
4185
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004186 my $a = '';
4187 $a = 'V' if ($elements[$n] ne '');
4188 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004189 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004190 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4191 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004192 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004193
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004194 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004195
4196 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004197 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004198 $c = 'V' if ($elements[$n + 2] ne '');
4199 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004200 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004201 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4202 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004203 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004204 } else {
4205 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004206 }
4207
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004208 my $ctx = "${a}x${c}";
4209
4210 my $at = "(ctx:$ctx)";
4211
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004212 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004213 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004214
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004215 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004216 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004217
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004218 # Get the full operator variant.
4219 my $opv = $op . substr($curr_vars, $off, 1);
4220
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004221 # Ignore operators passed as parameters.
4222 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004223 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004224
Andy Whitcroftcf655042008-03-04 14:28:20 -08004225# # Ignore comments
4226# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004227
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004228 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004229 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004230 if ($ctx !~ /.x[WEBC]/ &&
4231 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004232 if (ERROR("SPACING",
4233 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004234 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004235 $line_fixed = 1;
4236 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004237 }
4238
4239 # // is a comment
4240 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004241
Joe Perchesb00e4812014-04-03 14:49:33 -07004242 # : when part of a bitfield
4243 } elsif ($opv eq ':B') {
4244 # skip the bitfield test for now
4245
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004246 # No spaces for:
4247 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004248 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004249 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004250 if (ERROR("SPACING",
4251 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004252 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004253 if (defined $fix_elements[$n + 2]) {
4254 $fix_elements[$n + 2] =~ s/^\s+//;
4255 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004256 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004257 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004258 }
4259
Joe Perches23810972014-12-10 15:51:32 -08004260 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004261 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004262 my $rtrim_before = 0;
4263 my $space_after = 0;
4264 if ($ctx =~ /Wx./) {
4265 if (ERROR("SPACING",
4266 "space prohibited before that '$op' $at\n" . $hereptr)) {
4267 $line_fixed = 1;
4268 $rtrim_before = 1;
4269 }
4270 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004271 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004272 if (ERROR("SPACING",
4273 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004274 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004275 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004276 $space_after = 1;
4277 }
4278 }
4279 if ($rtrim_before || $space_after) {
4280 if ($rtrim_before) {
4281 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4282 } else {
4283 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4284 }
4285 if ($space_after) {
4286 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004287 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004288 }
4289
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004290 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004291 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004292 #warn "'*' is part of type\n";
4293
4294 # unary operators should have a space before and
4295 # none after. May be left adjacent to another
4296 # unary operator, or a cast
4297 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004298 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004299 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004300 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004301 if (ERROR("SPACING",
4302 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004303 if ($n != $last_after + 2) {
4304 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4305 $line_fixed = 1;
4306 }
Joe Perches3705ce52013-07-03 15:05:31 -07004307 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004308 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004309 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004310 # A unary '*' may be const
4311
4312 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004313 if (ERROR("SPACING",
4314 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004315 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004316 if (defined $fix_elements[$n + 2]) {
4317 $fix_elements[$n + 2] =~ s/^\s+//;
4318 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004319 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004320 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004321 }
4322
4323 # unary ++ and unary -- are allowed no space on one side.
4324 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004325 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004326 if (ERROR("SPACING",
4327 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004328 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004329 $line_fixed = 1;
4330 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004331 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004332 if ($ctx =~ /Wx[BE]/ ||
4333 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004334 if (ERROR("SPACING",
4335 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004336 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004337 $line_fixed = 1;
4338 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004339 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004340 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004341 if (ERROR("SPACING",
4342 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004343 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004344 if (defined $fix_elements[$n + 2]) {
4345 $fix_elements[$n + 2] =~ s/^\s+//;
4346 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004347 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004348 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004349 }
4350
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004351 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004352 } elsif ($op eq '<<' or $op eq '>>' or
4353 $op eq '&' or $op eq '^' or $op eq '|' or
4354 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004355 $op eq '*' or $op eq '/' or
4356 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004357 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004358 if ($check) {
4359 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4360 if (CHK("SPACING",
4361 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4362 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4363 $fix_elements[$n + 2] =~ s/^\s+//;
4364 $line_fixed = 1;
4365 }
4366 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4367 if (CHK("SPACING",
4368 "space preferred before that '$op' $at\n" . $hereptr)) {
4369 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4370 $line_fixed = 1;
4371 }
4372 }
4373 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004374 if (ERROR("SPACING",
4375 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004376 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4377 if (defined $fix_elements[$n + 2]) {
4378 $fix_elements[$n + 2] =~ s/^\s+//;
4379 }
Joe Perches3705ce52013-07-03 15:05:31 -07004380 $line_fixed = 1;
4381 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004382 }
4383
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004384 # A colon needs no spaces before when it is
4385 # terminating a case value or a label.
4386 } elsif ($opv eq ':C' || $opv eq ':L') {
4387 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004388 if (ERROR("SPACING",
4389 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004390 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004391 $line_fixed = 1;
4392 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004393 }
4394
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004395 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004396 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004397 my $ok = 0;
4398
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004399 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004400 if (($op eq '<' &&
4401 $cc =~ /^\S+\@\S+>/) ||
4402 ($op eq '>' &&
4403 $ca =~ /<\S+\@\S+$/))
4404 {
4405 $ok = 1;
4406 }
4407
Joe Perchese0df7e12015-04-16 12:44:53 -07004408 # for asm volatile statements
4409 # ignore a colon with another
4410 # colon immediately before or after
4411 if (($op eq ':') &&
4412 ($ca =~ /:$/ || $cc =~ /^:/)) {
4413 $ok = 1;
4414 }
4415
Joe Perches84731622013-11-12 15:10:05 -08004416 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004417 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004418 my $msg_type = \&ERROR;
4419 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4420
4421 if (&{$msg_type}("SPACING",
4422 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004423 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4424 if (defined $fix_elements[$n + 2]) {
4425 $fix_elements[$n + 2] =~ s/^\s+//;
4426 }
Joe Perches3705ce52013-07-03 15:05:31 -07004427 $line_fixed = 1;
4428 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004429 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004430 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004431 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004432
4433## print("n: <$n> GOOD: <$good>\n");
4434
4435 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004436 }
Joe Perches3705ce52013-07-03 15:05:31 -07004437
4438 if (($#elements % 2) == 0) {
4439 $fixed_line = $fixed_line . $fix_elements[$#elements];
4440 }
4441
Joe Perches194f66f2014-08-06 16:11:03 -07004442 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4443 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004444 }
4445
4446
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004447 }
4448
Joe Perches786b6322013-07-03 15:05:32 -07004449# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004450 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004451 if (WARN("SPACING",
4452 "space prohibited before semicolon\n" . $herecurr) &&
4453 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004454 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004455 s/^(\+.*\S)\s+;/$1;/;
4456 }
4457 }
4458
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004459# check for multiple assignments
4460 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004461 CHK("MULTIPLE_ASSIGNMENTS",
4462 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004463 }
4464
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004465## # check for multiple declarations, allowing for a function declaration
4466## # continuation.
4467## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4468## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4469##
4470## # Remove any bracketed sections to ensure we do not
4471## # falsly report the parameters of functions.
4472## my $ln = $line;
4473## while ($ln =~ s/\([^\(\)]*\)//g) {
4474## }
4475## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004476## WARN("MULTIPLE_DECLARATION",
4477## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004478## }
4479## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004480
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004481#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004482 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004483 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004484 if (ERROR("SPACING",
4485 "space required before the open brace '{'\n" . $herecurr) &&
4486 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004487 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004488 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004489 }
4490
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004491## # check for blank lines before declarations
4492## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4493## $prevrawline =~ /^.\s*$/) {
4494## WARN("SPACING",
4495## "No blank lines before declarations\n" . $hereprev);
4496## }
4497##
4498
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004499# closing brace should have a space following it when it has anything
4500# on the line
4501 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004502 if (ERROR("SPACING",
4503 "space required after that close brace '}'\n" . $herecurr) &&
4504 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004505 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004506 s/}((?!(?:,|;|\)))\S)/} $1/;
4507 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004508 }
4509
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004510# check spacing on square brackets
4511 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004512 if (ERROR("SPACING",
4513 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4514 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004515 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004516 s/\[\s+/\[/;
4517 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004518 }
4519 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004520 if (ERROR("SPACING",
4521 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4522 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004523 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004524 s/\s+\]/\]/;
4525 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004526 }
4527
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004528# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004529 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004530 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004531 if (ERROR("SPACING",
4532 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4533 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004534 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004535 s/\(\s+/\(/;
4536 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004537 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004538 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004539 $line !~ /for\s*\(.*;\s+\)/ &&
4540 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004541 if (ERROR("SPACING",
4542 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4543 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004544 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004545 s/\s+\)/\)/;
4546 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004547 }
4548
Joe Perchese2826fd2014-08-06 16:10:48 -07004549# check unnecessary parentheses around addressof/dereference single $Lvals
4550# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4551
4552 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004553 my $var = $1;
4554 if (CHK("UNNECESSARY_PARENTHESES",
4555 "Unnecessary parentheses around $var\n" . $herecurr) &&
4556 $fix) {
4557 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4558 }
4559 }
4560
4561# check for unnecessary parentheses around function pointer uses
4562# ie: (foo->bar)(); should be foo->bar();
4563# but not "if (foo->bar) (" to avoid some false positives
4564 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4565 my $var = $2;
4566 if (CHK("UNNECESSARY_PARENTHESES",
4567 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4568 $fix) {
4569 my $var2 = deparenthesize($var);
4570 $var2 =~ s/\s//g;
4571 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4572 }
4573 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004574
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004575#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004576 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004577 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004578 if (WARN("INDENTED_LABEL",
4579 "labels should not be indented\n" . $herecurr) &&
4580 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004581 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004582 s/^(.)\s+/$1/;
4583 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004584 }
4585
Joe Perches5b9553a2014-04-03 14:49:21 -07004586# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004587 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004588 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004589 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004590 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4591 my $value = $1;
4592 $value = deparenthesize($value);
4593 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4594 ERROR("RETURN_PARENTHESES",
4595 "return is not a function, parentheses are not required\n" . $herecurr);
4596 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004597 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004598 ERROR("SPACING",
4599 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004600 }
4601 }
Joe Perches507e5142013-11-12 15:10:13 -08004602
Joe Perchesb43ae212014-06-23 13:22:07 -07004603# unnecessary return in a void function
4604# at end-of-function, with the previous line a single leading tab, then return;
4605# and the line before that not a goto label target like "out:"
4606 if ($sline =~ /^[ \+]}\s*$/ &&
4607 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4608 $linenr >= 3 &&
4609 $lines[$linenr - 3] =~ /^[ +]/ &&
4610 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004611 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004612 "void function return statements are not generally useful\n" . $hereprev);
4613 }
Joe Perches9819cf22014-06-04 16:12:09 -07004614
Joe Perches189248d2014-01-23 15:54:47 -08004615# if statements using unnecessary parentheses - ie: if ((foo == bar))
4616 if ($^V && $^V ge 5.10.0 &&
4617 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4618 my $openparens = $1;
4619 my $count = $openparens =~ tr@\(@\(@;
4620 my $msg = "";
4621 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4622 my $comp = $4; #Not $1 because of $LvalOrFunc
4623 $msg = " - maybe == should be = ?" if ($comp eq "==");
4624 WARN("UNNECESSARY_PARENTHESES",
4625 "Unnecessary parentheses$msg\n" . $herecurr);
4626 }
4627 }
4628
Joe Perchesc5595fa2015-09-09 15:37:58 -07004629# comparisons with a constant or upper case identifier on the left
4630# avoid cases like "foo + BAR < baz"
4631# only fix matches surrounded by parentheses to avoid incorrect
4632# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4633 if ($^V && $^V ge 5.10.0 &&
4634 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4635 my $lead = $1;
4636 my $const = $2;
4637 my $comp = $3;
4638 my $to = $4;
4639 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004640 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004641 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4642 WARN("CONSTANT_COMPARISON",
4643 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4644 $fix) {
4645 if ($comp eq "<") {
4646 $newcomp = ">";
4647 } elsif ($comp eq "<=") {
4648 $newcomp = ">=";
4649 } elsif ($comp eq ">") {
4650 $newcomp = "<";
4651 } elsif ($comp eq ">=") {
4652 $newcomp = "<=";
4653 }
4654 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4655 }
4656 }
4657
Joe Perchesf34e4a42015-04-16 12:44:19 -07004658# Return of what appears to be an errno should normally be negative
4659 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004660 my $name = $1;
4661 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004662 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004663 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004664 }
4665 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004666
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004667# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004668 if ($line =~ /\b(if|while|for|switch)\(/) {
4669 if (ERROR("SPACING",
4670 "space required before the open parenthesis '('\n" . $herecurr) &&
4671 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004672 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004673 s/\b(if|while|for|switch)\(/$1 \(/;
4674 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004675 }
4676
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004677# Check for illegal assignment in if conditional -- and check for trailing
4678# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004679 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004680 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4681 ctx_statement_block($linenr, $realcnt, 0)
4682 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004683 my ($stat_next) = ctx_statement_block($line_nr_next,
4684 $remain_next, $off_next);
4685 $stat_next =~ s/\n./\n /g;
4686 ##print "stat<$stat> stat_next<$stat_next>\n";
4687
4688 if ($stat_next =~ /^\s*while\b/) {
4689 # If the statement carries leading newlines,
4690 # then count those as offsets.
4691 my ($whitespace) =
4692 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4693 my $offset =
4694 statement_rawlines($whitespace) - 1;
4695
4696 $suppress_whiletrailers{$line_nr_next +
4697 $offset} = 1;
4698 }
4699 }
4700 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004701 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004702 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004703 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004704
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004705 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004706 ERROR("ASSIGN_IN_IF",
4707 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004708 }
4709
4710 # Find out what is on the end of the line after the
4711 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004712 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004713 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004714 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004715 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4716 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004717 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004718 # Find out how long the conditional actually is.
4719 my @newlines = ($c =~ /\n/gs);
4720 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004721 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004722
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004723 $stat_real = raw_line($linenr, $cond_lines)
4724 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004725 if (defined($stat_real) && $cond_lines > 1) {
4726 $stat_real = "[...]\n$stat_real";
4727 }
4728
Joe Perches000d1cc12011-07-25 17:13:25 -07004729 ERROR("TRAILING_STATEMENTS",
4730 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004731 }
4732 }
4733
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004734# Check for bitwise tests written as boolean
4735 if ($line =~ /
4736 (?:
4737 (?:\[|\(|\&\&|\|\|)
4738 \s*0[xX][0-9]+\s*
4739 (?:\&\&|\|\|)
4740 |
4741 (?:\&\&|\|\|)
4742 \s*0[xX][0-9]+\s*
4743 (?:\&\&|\|\||\)|\])
4744 )/x)
4745 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004746 WARN("HEXADECIMAL_BOOLEAN_TEST",
4747 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004748 }
4749
Andy Whitcroft8905a672007-11-28 16:21:06 -08004750# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004751 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4752 my $s = $1;
4753 $s =~ s/$;//g; # Remove any comments
4754 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004755 ERROR("TRAILING_STATEMENTS",
4756 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004757 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004758 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004759# if should not continue a brace
4760 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004761 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004762 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004763 $herecurr);
4764 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004765# case and default should not have general statements after them
4766 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4767 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004768 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004769 \s*return\s+
4770 )/xg)
4771 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004772 ERROR("TRAILING_STATEMENTS",
4773 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004774 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004775
4776 # Check for }<nl>else {, these must be at the same
4777 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004778 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4779 $previndent == $indent) {
4780 if (ERROR("ELSE_AFTER_BRACE",
4781 "else should follow close brace '}'\n" . $hereprev) &&
4782 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4783 fix_delete_line($fixlinenr - 1, $prevrawline);
4784 fix_delete_line($fixlinenr, $rawline);
4785 my $fixedline = $prevrawline;
4786 $fixedline =~ s/}\s*$//;
4787 if ($fixedline !~ /^\+\s*$/) {
4788 fix_insert_line($fixlinenr, $fixedline);
4789 }
4790 $fixedline = $rawline;
4791 $fixedline =~ s/^(.\s*)else/$1} else/;
4792 fix_insert_line($fixlinenr, $fixedline);
4793 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004794 }
4795
Joe Perches8b8856f2014-08-06 16:11:14 -07004796 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4797 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004798 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4799
4800 # Find out what is on the end of the line after the
4801 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004802 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004803 $s =~ s/\n.*//g;
4804
4805 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004806 if (ERROR("WHILE_AFTER_BRACE",
4807 "while should follow close brace '}'\n" . $hereprev) &&
4808 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4809 fix_delete_line($fixlinenr - 1, $prevrawline);
4810 fix_delete_line($fixlinenr, $rawline);
4811 my $fixedline = $prevrawline;
4812 my $trailing = $rawline;
4813 $trailing =~ s/^\+//;
4814 $trailing = trim($trailing);
4815 $fixedline =~ s/}\s*$/} $trailing/;
4816 fix_insert_line($fixlinenr, $fixedline);
4817 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004818 }
4819 }
4820
Joe Perches95e2c602013-07-03 15:05:20 -07004821#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004822 while ($line =~ m{($Constant|$Lval)}g) {
4823 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004824
4825#gcc binary extension
4826 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004827 if (WARN("GCC_BINARY_CONSTANT",
4828 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4829 $fix) {
4830 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004831 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004832 s/\b$var\b/$hexval/;
4833 }
Joe Perches95e2c602013-07-03 15:05:20 -07004834 }
4835
4836#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004837 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004838 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004839#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004840 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004841#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004842 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4843#Ignore some three character SI units explicitly, like MiB and KHz
4844 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004845 while ($var =~ m{($Ident)}g) {
4846 my $word = $1;
4847 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004848 if ($check) {
4849 seed_camelcase_includes();
4850 if (!$file && !$camelcase_file_seeded) {
4851 seed_camelcase_file($realfile);
4852 $camelcase_file_seeded = 1;
4853 }
4854 }
Joe Perches7e781f62013-09-11 14:23:55 -07004855 if (!defined $camelcase{$word}) {
4856 $camelcase{$word} = 1;
4857 CHK("CAMELCASE",
4858 "Avoid CamelCase: <$word>\n" . $herecurr);
4859 }
Joe Perches34456862013-07-03 15:05:34 -07004860 }
Joe Perches323c1262012-12-17 16:02:07 -08004861 }
4862 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004863
4864#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004865 if ($line =~ /\#\s*define.*\\\s+$/) {
4866 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4867 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4868 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004869 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004870 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004871 }
4872
Fabian Frederick0e212e02015-04-16 12:44:25 -07004873# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4874# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004875 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004876 my $file = "$1.h";
4877 my $checkfile = "include/linux/$file";
4878 if (-f "$root/$checkfile" &&
4879 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004880 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004881 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004882 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4883 if ($asminclude > 0) {
4884 if ($realfile =~ m{^arch/}) {
4885 CHK("ARCH_INCLUDE_LINUX",
4886 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4887 } else {
4888 WARN("INCLUDE_LINUX",
4889 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4890 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004891 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004892 }
4893 }
4894
Andy Whitcroft653d4872007-06-23 17:16:34 -07004895# multi-statement macros should be enclosed in a do while loop, grab the
4896# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004897# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004898 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4899 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004900 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004901 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004902 my ($off, $dstat, $dcond, $rest);
4903 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004904 my $has_flow_statement = 0;
4905 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004906 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004907 ctx_statement_block($linenr, $realcnt, 0);
4908 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004909 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004910 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004911
Joe Perches08a28432014-10-13 15:51:55 -07004912 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004913 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004914
Joe Perchesf59b64b2016-10-11 13:52:08 -07004915 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4916 my $define_args = $1;
4917 my $define_stmt = $dstat;
4918 my @def_args = ();
4919
4920 if (defined $define_args && $define_args ne "") {
4921 $define_args = substr($define_args, 1, length($define_args) - 2);
4922 $define_args =~ s/\s*//g;
4923 @def_args = split(",", $define_args);
4924 }
4925
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004926 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004927 $dstat =~ s/\\\n.//g;
4928 $dstat =~ s/^\s*//s;
4929 $dstat =~ s/\s*$//s;
4930
4931 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004932 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4933 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004934 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004935 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004936 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004937
Gregory Bean02870be2011-05-11 09:11:12 -07004938 # Extremely long macros may fall off the end of the
4939 # available context without closing. Give a dangling
4940 # backslash the benefit of the doubt and allow it
4941 # to gobble any hanging open-parens.
4942 $dstat =~ s/\(.+\\$/1/;
4943
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004944 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004945 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4946 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004947 {
4948 }
4949
Joe Perches42e15292016-03-15 14:58:01 -07004950 # Make asm volatile uses seem like a generic function
4951 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4952
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004953 my $exceptions = qr{
4954 $Declare|
4955 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004956 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004957 DECLARE_PER_CPU|
4958 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004959 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004960 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004961 union|
4962 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004963 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004964 ^\"|\"$|
4965 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004966 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004967 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004968
4969 $ctx =~ s/\n*$//;
4970 my $herectx = $here . "\n";
4971 my $stmt_cnt = statement_rawlines($ctx);
4972
4973 for (my $n = 0; $n < $stmt_cnt; $n++) {
4974 $herectx .= raw_line($linenr, $n) . "\n";
4975 }
4976
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004977 if ($dstat ne '' &&
4978 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4979 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004980 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004981 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004982 $dstat !~ /$exceptions/ &&
4983 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004984 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004985 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004986 $dstat !~ /^for\s*$Constant$/ && # for (...)
4987 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4988 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004989 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004990 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004991 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004992
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004993 if ($dstat =~ /;/) {
4994 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4995 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4996 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004997 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004998 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004999 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07005000
5001 }
Joe Perches52076492016-10-11 13:52:14 -07005002
5003 # Make $define_stmt single line, comment-free, etc
5004 my @stmt_array = split('\n', $define_stmt);
5005 my $first = 1;
5006 $define_stmt = "";
5007 foreach my $l (@stmt_array) {
5008 $l =~ s/\\$//;
5009 if ($first) {
5010 $define_stmt = $l;
5011 $first = 0;
5012 } elsif ($l =~ /^[\+ ]/) {
5013 $define_stmt .= substr($l, 1);
5014 }
5015 }
5016 $define_stmt =~ s/$;//g;
5017 $define_stmt =~ s/\s+/ /g;
5018 $define_stmt = trim($define_stmt);
5019
Joe Perchesf59b64b2016-10-11 13:52:08 -07005020# check if any macro arguments are reused (ignore '...' and 'type')
5021 foreach my $arg (@def_args) {
5022 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07005023 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07005024 my $tmp = $define_stmt;
5025 $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 -07005026 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07005027 $tmp =~ s/\b$arg\s*\#\#//g;
5028 my $use_cnt = $tmp =~ s/\b$arg\b//g;
5029 if ($use_cnt > 1) {
5030 CHK("MACRO_ARG_REUSE",
5031 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07005032 }
5033# check if any macro arguments may have other precedence issues
5034 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5035 ((defined($1) && $1 ne ',') ||
5036 (defined($2) && $2 ne ','))) {
5037 CHK("MACRO_ARG_PRECEDENCE",
5038 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07005039 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005040 }
Joe Perches5023d342012-12-17 16:01:47 -08005041
Joe Perches08a28432014-10-13 15:51:55 -07005042# check for macros with flow control, but without ## concatenation
5043# ## concatenation is commonly a macro that defines a function so ignore those
5044 if ($has_flow_statement && !$has_arg_concat) {
5045 my $herectx = $here . "\n";
5046 my $cnt = statement_rawlines($ctx);
5047
5048 for (my $n = 0; $n < $cnt; $n++) {
5049 $herectx .= raw_line($linenr, $n) . "\n";
5050 }
5051 WARN("MACRO_WITH_FLOW_CONTROL",
5052 "Macros with flow control statements should be avoided\n" . "$herectx");
5053 }
5054
Joe Perches481eb482012-12-17 16:01:56 -08005055# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08005056
5057 } else {
5058 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08005059 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5060 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08005061 $line =~ /^\+.*\\$/) {
5062 WARN("LINE_CONTINUATIONS",
5063 "Avoid unnecessary line continuations\n" . $herecurr);
5064 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005065 }
5066
Joe Perchesb13edf72012-07-30 14:41:24 -07005067# do {} while (0) macro tests:
5068# single-statement macros do not need to be enclosed in do while (0) loop,
5069# macro should not end with a semicolon
5070 if ($^V && $^V ge 5.10.0 &&
5071 $realfile !~ m@/vmlinux.lds.h$@ &&
5072 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5073 my $ln = $linenr;
5074 my $cnt = $realcnt;
5075 my ($off, $dstat, $dcond, $rest);
5076 my $ctx = '';
5077 ($dstat, $dcond, $ln, $cnt, $off) =
5078 ctx_statement_block($linenr, $realcnt, 0);
5079 $ctx = $dstat;
5080
5081 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005082 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005083
5084 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5085 my $stmts = $2;
5086 my $semis = $3;
5087
5088 $ctx =~ s/\n*$//;
5089 my $cnt = statement_rawlines($ctx);
5090 my $herectx = $here . "\n";
5091
5092 for (my $n = 0; $n < $cnt; $n++) {
5093 $herectx .= raw_line($linenr, $n) . "\n";
5094 }
5095
Joe Perchesac8e97f2012-08-21 16:15:53 -07005096 if (($stmts =~ tr/;/;/) == 1 &&
5097 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005098 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5099 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5100 }
5101 if (defined $semis && $semis ne "") {
5102 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5103 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5104 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005105 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5106 $ctx =~ s/\n*$//;
5107 my $cnt = statement_rawlines($ctx);
5108 my $herectx = $here . "\n";
5109
5110 for (my $n = 0; $n < $cnt; $n++) {
5111 $herectx .= raw_line($linenr, $n) . "\n";
5112 }
5113
5114 WARN("TRAILING_SEMICOLON",
5115 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005116 }
5117 }
5118
Mike Frysinger080ba922009-01-06 14:41:25 -08005119# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5120# all assignments may have only one of the following with an assignment:
5121# .
5122# ALIGN(...)
5123# VMLINUX_SYMBOL(...)
5124 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005125 WARN("MISSING_VMLINUX_SYMBOL",
5126 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005127 }
5128
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005129# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005130 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5131 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005132 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005133 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005134 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5135 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005136 my @allowed = ();
5137 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005138 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005139 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005140 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005141 for my $chunk (@chunks) {
5142 my ($cond, $block) = @{$chunk};
5143
Andy Whitcroft773647a2008-03-28 14:15:58 -07005144 # If the condition carries leading newlines, then count those as offsets.
5145 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5146 my $offset = statement_rawlines($whitespace) - 1;
5147
Joe Perchesaad4f612012-03-23 15:02:19 -07005148 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005149 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5150
5151 # We have looked at and allowed this specific line.
5152 $suppress_ifbraces{$ln + $offset} = 1;
5153
5154 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005155 $ln += statement_rawlines($block) - 1;
5156
Andy Whitcroft773647a2008-03-28 14:15:58 -07005157 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005158
5159 $seen++ if ($block =~ /^\s*{/);
5160
Joe Perchesaad4f612012-03-23 15:02:19 -07005161 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005162 if (statement_lines($cond) > 1) {
5163 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005164 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005165 }
5166 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005167 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005168 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005169 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005170 if (statement_block_size($block) > 1) {
5171 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005172 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005173 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005174 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005175 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005176 if ($seen) {
5177 my $sum_allowed = 0;
5178 foreach (@allowed) {
5179 $sum_allowed += $_;
5180 }
5181 if ($sum_allowed == 0) {
5182 WARN("BRACES",
5183 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5184 } elsif ($sum_allowed != $allow &&
5185 $seen != $allow) {
5186 CHK("BRACES",
5187 "braces {} should be used on all arms of this statement\n" . $herectx);
5188 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005189 }
5190 }
5191 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005192 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005193 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005194 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005195
Andy Whitcroftcf655042008-03-04 14:28:20 -08005196 # Check the pre-context.
5197 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5198 #print "APW: ALLOWED: pre<$1>\n";
5199 $allowed = 1;
5200 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005201
5202 my ($level, $endln, @chunks) =
5203 ctx_statement_full($linenr, $realcnt, $-[0]);
5204
Andy Whitcroftcf655042008-03-04 14:28:20 -08005205 # Check the condition.
5206 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005207 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005208 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005209 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005210 }
5211 if (statement_lines($cond) > 1) {
5212 #print "APW: ALLOWED: cond<$cond>\n";
5213 $allowed = 1;
5214 }
5215 if ($block =~/\b(?:if|for|while)\b/) {
5216 #print "APW: ALLOWED: block<$block>\n";
5217 $allowed = 1;
5218 }
5219 if (statement_block_size($block) > 1) {
5220 #print "APW: ALLOWED: lines block<$block>\n";
5221 $allowed = 1;
5222 }
5223 # Check the post-context.
5224 if (defined $chunks[1]) {
5225 my ($cond, $block) = @{$chunks[1]};
5226 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005227 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005228 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005229 if ($block =~ /^\s*\{/) {
5230 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5231 $allowed = 1;
5232 }
5233 }
5234 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005235 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005236 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005237
Andy Whitcroftf0556632008-10-15 22:02:23 -07005238 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005239 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005240 }
5241
Joe Perches000d1cc12011-07-25 17:13:25 -07005242 WARN("BRACES",
5243 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005244 }
5245 }
5246
Joe Perches0979ae62012-12-17 16:01:59 -08005247# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005248 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005249 if (CHK("BRACES",
5250 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5251 $fix && $prevrawline =~ /^\+/) {
5252 fix_delete_line($fixlinenr - 1, $prevrawline);
5253 }
Joe Perches0979ae62012-12-17 16:01:59 -08005254 }
Joe Perches77b9a532013-07-03 15:05:29 -07005255 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005256 if (CHK("BRACES",
5257 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5258 $fix) {
5259 fix_delete_line($fixlinenr, $rawline);
5260 }
Joe Perches0979ae62012-12-17 16:01:59 -08005261 }
5262
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005263# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005264 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5265 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005266 WARN("VOLATILE",
5267 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005268 }
5269
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005270# Check for user-visible strings broken across lines, which breaks the ability
5271# to grep for the string. Make exceptions when the previous string ends in a
5272# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5273# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005274 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005275 $prevline =~ /"\s*$/ &&
5276 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5277 if (WARN("SPLIT_STRING",
5278 "quoted string split across lines\n" . $hereprev) &&
5279 $fix &&
5280 $prevrawline =~ /^\+.*"\s*$/ &&
5281 $last_coalesced_string_linenr != $linenr - 1) {
5282 my $extracted_string = get_quoted_string($line, $rawline);
5283 my $comma_close = "";
5284 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5285 $comma_close = $1;
5286 }
5287
5288 fix_delete_line($fixlinenr - 1, $prevrawline);
5289 fix_delete_line($fixlinenr, $rawline);
5290 my $fixedline = $prevrawline;
5291 $fixedline =~ s/"\s*$//;
5292 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5293 fix_insert_line($fixlinenr - 1, $fixedline);
5294 $fixedline = $rawline;
5295 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5296 if ($fixedline !~ /\+\s*$/) {
5297 fix_insert_line($fixlinenr, $fixedline);
5298 }
5299 $last_coalesced_string_linenr = $linenr;
5300 }
5301 }
5302
5303# check for missing a space in a string concatenation
5304 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5305 WARN('MISSING_SPACE',
5306 "break quoted strings at a space character\n" . $hereprev);
5307 }
5308
5309# check for spaces before a quoted newline
5310 if ($rawline =~ /^.*\".*\s\\n/) {
5311 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5312 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5313 $fix) {
5314 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5315 }
5316
5317 }
5318
Joe Perchesf17dba42014-10-13 15:51:51 -07005319# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005320 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005321 CHK("CONCATENATED_STRING",
5322 "Concatenated strings should use spaces between elements\n" . $herecurr);
5323 }
5324
Joe Perches90ad30e2014-12-10 15:51:59 -08005325# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005326 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005327 WARN("STRING_FRAGMENTS",
5328 "Consecutive strings are generally better as a single string\n" . $herecurr);
5329 }
5330
Joe Perches6e300752015-09-09 15:37:47 -07005331# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005332 my $string;
5333 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5334 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5335 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005336 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005337 WARN("PRINTF_L",
5338 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5339 last;
5340 }
Joe Perches6e300752015-09-09 15:37:47 -07005341 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5342 ERROR("PRINTF_0xDECIMAL",
5343 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5344 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005345 }
5346
5347# check for line continuations in quoted strings with odd counts of "
5348 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5349 WARN("LINE_CONTINUATIONS",
5350 "Avoid line continuations in quoted strings\n" . $herecurr);
5351 }
5352
Gregory Bean6c9c6842011-03-17 14:18:20 -07005353# sys_open/read/write/close are not allowed in the kernel
5354 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5355 ERROR("FILE_OPS",
5356 "$1 is inappropriate in kernel code.\n" .
5357 $herecurr);
5358 }
5359
Gregory Beana7cce0a2011-06-07 08:06:45 -07005360# filp_open is a backdoor for sys_open
5361 if ($line =~ /\b(filp_open)\b/) {
5362 ERROR("FILE_OPS",
5363 "$1 is inappropriate in kernel code.\n" .
5364 $herecurr);
5365 }
5366
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005367# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5368 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5369 ERROR("NON_RELAXED_IO",
5370 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5371 "with appropriate memory barriers instead.\n" .
5372 $herecurr);
5373 }
5374
5375# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5376 if ($line =~ /\b((in|out)([bwl]))\b/) {
5377 my ($all, $pref, $suf) = ($1, $2, $3);
5378 $pref =~ s/in/read/;
5379 $pref =~ s/out/write/;
5380 ERROR("NON_RELAXED_IO",
5381 "Use of $all is deprecated: use " .
5382 "__raw_$pref$suf\n\t" .
5383 "with appropriate memory barriers instead.\n" .
5384 $herecurr);
5385 }
5386
Gregory Bean438bf5f2011-06-15 09:32:38 -07005387# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005388 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005389 WARN("ARM_BARRIER",
5390 "Use of dsb is discouranged: prefer mb.\n" .
5391 $herecurr);
5392 }
5393
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005394# unbounded string functions are overflow risks
5395 my %str_fns = (
5396 "sprintf" => "snprintf",
5397 "strcpy" => "strlcpy",
5398 "strncpy" => "strlcpy",
5399 "strcat" => "strlcat",
5400 "strncat" => "strlcat",
5401 "vsprintf" => "vsnprintf",
5402 "strchr" => "strnchr",
5403 "strstr" => "strnstr",
5404 );
5405 foreach my $k (keys %str_fns) {
5406 if ($line =~ /\b$k\b/) {
5407 ERROR("UNBOUNDED_STRING_FNS",
5408 "Use of $k is deprecated: " .
5409 "use $str_fns{$k} instead.\n" .
5410 $herecurr);
5411 }
5412 }
5413
Andy Whitcroft00df3442007-06-08 13:47:06 -07005414# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005415 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005416 WARN("IF_0",
5417 "if this code is redundant consider removing it\n"
5418 . $herecurr);
5419 }
5420
5421# warn about #if 1
5422 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5423 WARN("IF_1",
5424 "if this code is required consider removing"
5425 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005426 }
5427
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005428# check for needless "if (<foo>) fn(<foo>)" uses
5429 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005430 my $tested = quotemeta($1);
5431 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5432 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5433 my $func = $1;
5434 if (WARN('NEEDLESS_IF',
5435 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5436 $fix) {
5437 my $do_fix = 1;
5438 my $leading_tabs = "";
5439 my $new_leading_tabs = "";
5440 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5441 $leading_tabs = $1;
5442 } else {
5443 $do_fix = 0;
5444 }
5445 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5446 $new_leading_tabs = $1;
5447 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5448 $do_fix = 0;
5449 }
5450 } else {
5451 $do_fix = 0;
5452 }
5453 if ($do_fix) {
5454 fix_delete_line($fixlinenr - 1, $prevrawline);
5455 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5456 }
5457 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005458 }
5459 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005460
Joe Perchesebfdc402014-08-06 16:10:27 -07005461# check for unnecessary "Out of Memory" messages
5462 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5463 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5464 (defined $1 || defined $3) &&
5465 $linenr > 3) {
5466 my $testval = $2;
5467 my $testline = $lines[$linenr - 3];
5468
5469 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5470# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5471
5472 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5473 WARN("OOM_MESSAGE",
5474 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5475 }
5476 }
5477
Joe Perchesf78d98f2014-10-13 15:52:01 -07005478# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005479 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005480 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5481 my $level = $1;
5482 if (WARN("UNNECESSARY_KERN_LEVEL",
5483 "Possible unnecessary $level\n" . $herecurr) &&
5484 $fix) {
5485 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5486 }
5487 }
5488
Joe Perchesabb08a52014-12-10 15:51:46 -08005489# check for mask then right shift without a parentheses
5490 if ($^V && $^V ge 5.10.0 &&
5491 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5492 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5493 WARN("MASK_THEN_SHIFT",
5494 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5495 }
5496
Joe Perchesb75ac612014-12-10 15:52:02 -08005497# check for pointer comparisons to NULL
5498 if ($^V && $^V ge 5.10.0) {
5499 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5500 my $val = $1;
5501 my $equal = "!";
5502 $equal = "" if ($4 eq "!=");
5503 if (CHK("COMPARISON_TO_NULL",
5504 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5505 $fix) {
5506 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5507 }
5508 }
5509 }
5510
Joe Perches8716de32013-09-11 14:24:05 -07005511# check for bad placement of section $InitAttribute (e.g.: __initdata)
5512 if ($line =~ /(\b$InitAttribute\b)/) {
5513 my $attr = $1;
5514 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5515 my $ptr = $1;
5516 my $var = $2;
5517 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5518 ERROR("MISPLACED_INIT",
5519 "$attr should be placed after $var\n" . $herecurr)) ||
5520 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5521 WARN("MISPLACED_INIT",
5522 "$attr should be placed after $var\n" . $herecurr))) &&
5523 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005524 $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 -07005525 }
5526 }
5527 }
5528
Joe Perchese970b882013-11-12 15:10:10 -08005529# check for $InitAttributeData (ie: __initdata) with const
5530 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5531 my $attr = $1;
5532 $attr =~ /($InitAttributePrefix)(.*)/;
5533 my $attr_prefix = $1;
5534 my $attr_type = $2;
5535 if (ERROR("INIT_ATTRIBUTE",
5536 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5537 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005538 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005539 s/$InitAttributeData/${attr_prefix}initconst/;
5540 }
5541 }
5542
5543# check for $InitAttributeConst (ie: __initconst) without const
5544 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5545 my $attr = $1;
5546 if (ERROR("INIT_ATTRIBUTE",
5547 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5548 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005549 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005550 /(^\+\s*(?:static\s+))/;
5551 $lead = rtrim($1);
5552 $lead = "$lead " if ($lead !~ /^\+$/);
5553 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005554 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005555 }
5556 }
5557
Joe Perchesc17893c2015-04-16 12:44:42 -07005558# check for __read_mostly with const non-pointer (should just be const)
5559 if ($line =~ /\b__read_mostly\b/ &&
5560 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5561 if (ERROR("CONST_READ_MOSTLY",
5562 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5563 $fix) {
5564 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5565 }
5566 }
5567
Joe Perchesfbdb8132014-04-03 14:49:14 -07005568# don't use __constant_<foo> functions outside of include/uapi/
5569 if ($realfile !~ m@^include/uapi/@ &&
5570 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5571 my $constant_func = $1;
5572 my $func = $constant_func;
5573 $func =~ s/^__constant_//;
5574 if (WARN("CONSTANT_CONVERSION",
5575 "$constant_func should be $func\n" . $herecurr) &&
5576 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005577 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005578 }
5579 }
5580
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005581# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005582 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005583 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005584 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005585 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005586 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005587 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5588 }
5589 if ($delay > 2000) {
5590 WARN("LONG_UDELAY",
5591 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005592 }
5593 }
5594
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005595# warn about unexpectedly long msleep's
5596 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5597 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005598 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005599 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005600 }
5601 }
5602
Joe Perches36ec1932013-07-03 15:05:25 -07005603# check for comparisons of jiffies
5604 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5605 WARN("JIFFIES_COMPARISON",
5606 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5607 }
5608
Joe Perches9d7a34a2013-07-03 15:05:26 -07005609# check for comparisons of get_jiffies_64()
5610 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5611 WARN("JIFFIES_COMPARISON",
5612 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5613 }
5614
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005615# check the patch for use of mdelay
5616 if ($line =~ /\bmdelay\s*\(/) {
5617 WARN("MDELAY",
5618 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5619 }
5620
Andy Whitcroft00df3442007-06-08 13:47:06 -07005621# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005622# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005623# print "#ifdef in C files should be avoided\n";
5624# print "$herecurr";
5625# $clean = 0;
5626# }
5627
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005628# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005629 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005630 if (ERROR("SPACING",
5631 "exactly one space required after that #$1\n" . $herecurr) &&
5632 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005633 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005634 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5635 }
5636
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005637 }
5638
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005639# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005640 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5641 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005642 my $which = $1;
5643 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005644 CHK("UNCOMMENTED_DEFINITION",
5645 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005646 }
5647 }
5648# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005649
5650 my $barriers = qr{
5651 mb|
5652 rmb|
5653 wmb|
5654 read_barrier_depends
5655 }x;
5656 my $barrier_stems = qr{
5657 mb__before_atomic|
5658 mb__after_atomic|
5659 store_release|
5660 load_acquire|
5661 store_mb|
5662 (?:$barriers)
5663 }x;
5664 my $all_barriers = qr{
5665 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005666 smp_(?:$barrier_stems)|
5667 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005668 }x;
5669
5670 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005671 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005672 WARN("MEMORY_BARRIER",
5673 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005674 }
5675 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005676
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005677 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5678
5679 if ($realfile !~ m@^include/asm-generic/@ &&
5680 $realfile !~ m@/barrier\.h$@ &&
5681 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5682 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5683 WARN("MEMORY_BARRIER",
5684 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5685 }
5686
Joe Perchescb426e92015-06-25 15:02:46 -07005687# check for waitqueue_active without a comment.
5688 if ($line =~ /\bwaitqueue_active\s*\(/) {
5689 if (!ctx_has_comment($first_line, $linenr)) {
5690 WARN("WAITQUEUE_ACTIVE",
5691 "waitqueue_active without comment\n" . $herecurr);
5692 }
5693 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005694
5695# Check for expedited grace periods that interrupt non-idle non-nohz
5696# online CPUs. These expedited can therefore degrade real-time response
5697# if used carelessly, and should be avoided where not absolutely
5698# needed. It is always OK to use synchronize_rcu_expedited() and
5699# synchronize_sched_expedited() at boot time (before real-time applications
5700# start) and in error situations where real-time response is compromised in
5701# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5702# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5703# Of course, nothing comes for free, and srcu_read_lock() and
5704# srcu_read_unlock() do contain full memory barriers in payment for
5705# synchronize_srcu_expedited() non-interruption properties.
5706 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5707 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5708 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5709
5710 }
5711
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005712# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005713 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005714 CHK("ARCH_DEFINES",
5715 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005716 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005717
Tobias Klauserd4977c72010-05-24 14:33:30 -07005718# Check that the storage class is at the beginning of a declaration
5719 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005720 WARN("STORAGE_CLASS",
5721 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005722 }
5723
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005724# check the location of the inline attribute, that it is between
5725# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005726 if ($line =~ /\b$Type\s+$Inline\b/ ||
5727 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005728 ERROR("INLINE_LOCATION",
5729 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005730 }
5731
Andy Whitcroft8905a672007-11-28 16:21:06 -08005732# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005733 if ($realfile !~ m@\binclude/uapi/@ &&
5734 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005735 if (WARN("INLINE",
5736 "plain inline is preferred over $1\n" . $herecurr) &&
5737 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005738 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005739
5740 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005741 }
5742
Joe Perches3d130fd2011-01-12 17:00:00 -08005743# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005744 if ($realfile !~ m@\binclude/uapi/@ &&
5745 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005746 WARN("PREFER_PACKED",
5747 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005748 }
5749
Joe Perches39b7e282011-07-25 17:13:24 -07005750# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005751 if ($realfile !~ m@\binclude/uapi/@ &&
5752 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005753 WARN("PREFER_ALIGNED",
5754 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005755 }
5756
Joe Perches5f14d3b2012-01-10 15:09:52 -08005757# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005758 if ($realfile !~ m@\binclude/uapi/@ &&
5759 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005760 if (WARN("PREFER_PRINTF",
5761 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5762 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005763 $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 -07005764
5765 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005766 }
5767
Joe Perches6061d942012-03-23 15:02:16 -07005768# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005769 if ($realfile !~ m@\binclude/uapi/@ &&
5770 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005771 if (WARN("PREFER_SCANF",
5772 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5773 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005774 $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 -07005775 }
Joe Perches6061d942012-03-23 15:02:16 -07005776 }
5777
Joe Perches619a9082014-12-10 15:51:35 -08005778# Check for __attribute__ weak, or __weak declarations (may have link issues)
5779 if ($^V && $^V ge 5.10.0 &&
5780 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5781 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5782 $line =~ /\b__weak\b/)) {
5783 ERROR("WEAK_DECLARATION",
5784 "Using weak declarations can have unintended link defects\n" . $herecurr);
5785 }
5786
Joe Perchese6176fa2015-06-25 15:02:49 -07005787# check for c99 types like uint8_t used outside of uapi/
5788 if ($realfile !~ m@\binclude/uapi/@ &&
5789 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5790 my $type = $1;
5791 if ($type =~ /\b($typeC99Typedefs)\b/) {
5792 $type = $1;
5793 my $kernel_type = 'u';
5794 $kernel_type = 's' if ($type =~ /^_*[si]/);
5795 $type =~ /(\d+)/;
5796 $kernel_type .= $1;
5797 if (CHK("PREFER_KERNEL_TYPES",
5798 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5799 $fix) {
5800 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5801 }
5802 }
5803 }
5804
Joe Perches938224b2016-01-20 14:59:15 -08005805# check for cast of C90 native int or longer types constants
5806 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5807 my $cast = $1;
5808 my $const = $2;
5809 if (WARN("TYPECAST_INT_CONSTANT",
5810 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5811 $fix) {
5812 my $suffix = "";
5813 my $newconst = $const;
5814 $newconst =~ s/${Int_type}$//;
5815 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5816 if ($cast =~ /\blong\s+long\b/) {
5817 $suffix .= 'LL';
5818 } elsif ($cast =~ /\blong\b/) {
5819 $suffix .= 'L';
5820 }
5821 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5822 }
5823 }
5824
Joe Perches8f53a9b2010-03-05 13:43:48 -08005825# check for sizeof(&)
5826 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005827 WARN("SIZEOF_ADDRESS",
5828 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005829 }
5830
Joe Perches66c80b62012-07-30 14:41:22 -07005831# check for sizeof without parenthesis
5832 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005833 if (WARN("SIZEOF_PARENTHESIS",
5834 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5835 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005836 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005837 }
Joe Perches66c80b62012-07-30 14:41:22 -07005838 }
5839
Joe Perches88982fe2012-12-17 16:02:00 -08005840# check for struct spinlock declarations
5841 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5842 WARN("USE_SPINLOCK_T",
5843 "struct spinlock should be spinlock_t\n" . $herecurr);
5844 }
5845
Joe Perchesa6962d72013-04-29 16:18:13 -07005846# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005847 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005848 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005849 $fmt =~ s/%%//g;
5850 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005851 if (WARN("PREFER_SEQ_PUTS",
5852 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5853 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005854 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005855 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005856 }
5857 }
5858
Andy Whitcroft554e1652012-01-10 15:09:57 -08005859# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005860 if ($^V && $^V ge 5.10.0 &&
5861 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005862 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005863
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005864 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005865 my $ms_val = $7;
5866 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005867
Andy Whitcroft554e1652012-01-10 15:09:57 -08005868 if ($ms_size =~ /^(0x|)0$/i) {
5869 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005870 "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 -08005871 } elsif ($ms_size =~ /^(0x|)1$/i) {
5872 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005873 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5874 }
5875 }
5876
Joe Perches98a9bba2014-01-23 15:54:52 -08005877# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005878# if ($^V && $^V ge 5.10.0 &&
5879# defined $stat &&
5880# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5881# if (WARN("PREFER_ETHER_ADDR_COPY",
5882# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5883# $fix) {
5884# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5885# }
5886# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005887
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005888# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005889# if ($^V && $^V ge 5.10.0 &&
5890# defined $stat &&
5891# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5892# WARN("PREFER_ETHER_ADDR_EQUAL",
5893# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5894# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005895
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005896# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5897# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005898# if ($^V && $^V ge 5.10.0 &&
5899# defined $stat &&
5900# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5901#
5902# my $ms_val = $7;
5903#
5904# if ($ms_val =~ /^(?:0x|)0+$/i) {
5905# if (WARN("PREFER_ETH_ZERO_ADDR",
5906# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5907# $fix) {
5908# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5909# }
5910# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5911# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5912# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5913# $fix) {
5914# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5915# }
5916# }
5917# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005918
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005919# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005920 if ($^V && $^V ge 5.10.0 &&
5921 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005922 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005923 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005924 my $call = $1;
5925 my $cast1 = deparenthesize($2);
5926 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005927 my $cast2 = deparenthesize($7);
5928 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005929 my $cast;
5930
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005931 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005932 $cast = "$cast1 or $cast2";
5933 } elsif ($cast1 ne "") {
5934 $cast = $cast1;
5935 } else {
5936 $cast = $cast2;
5937 }
5938 WARN("MINMAX",
5939 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005940 }
5941 }
5942
Joe Perches4a273192012-07-30 14:41:20 -07005943# check usleep_range arguments
5944 if ($^V && $^V ge 5.10.0 &&
5945 defined $stat &&
5946 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5947 my $min = $1;
5948 my $max = $7;
5949 if ($min eq $max) {
5950 WARN("USLEEP_RANGE",
5951 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5952 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5953 $min > $max) {
5954 WARN("USLEEP_RANGE",
5955 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5956 }
5957 }
5958
Joe Perches823b7942013-11-12 15:10:15 -08005959# check for naked sscanf
5960 if ($^V && $^V ge 5.10.0 &&
5961 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005962 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005963 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5964 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5965 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5966 my $lc = $stat =~ tr@\n@@;
5967 $lc = $lc + $linenr;
5968 my $stat_real = raw_line($linenr, 0);
5969 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5970 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5971 }
5972 WARN("NAKED_SSCANF",
5973 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5974 }
5975
Joe Perchesafc819a2014-06-04 16:12:08 -07005976# check for simple sscanf that should be kstrto<foo>
5977 if ($^V && $^V ge 5.10.0 &&
5978 defined $stat &&
5979 $line =~ /\bsscanf\b/) {
5980 my $lc = $stat =~ tr@\n@@;
5981 $lc = $lc + $linenr;
5982 my $stat_real = raw_line($linenr, 0);
5983 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5984 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5985 }
5986 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5987 my $format = $6;
5988 my $count = $format =~ tr@%@%@;
5989 if ($count == 1 &&
5990 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5991 WARN("SSCANF_TO_KSTRTO",
5992 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5993 }
5994 }
5995 }
5996
Joe Perches70dc8a42013-09-11 14:23:58 -07005997# check for new externs in .h files.
5998 if ($realfile =~ /\.h$/ &&
5999 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07006000 if (CHK("AVOID_EXTERNS",
6001 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07006002 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006003 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07006004 }
6005 }
6006
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006007# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006008 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006009 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006010 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006011 my $function_name = $1;
6012 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006013
6014 my $s = $stat;
6015 if (defined $cond) {
6016 substr($s, 0, length($cond), '');
6017 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006018 if ($s =~ /^\s*;/ &&
6019 $function_name ne 'uninitialized_var')
6020 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006021 WARN("AVOID_EXTERNS",
6022 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006023 }
6024
6025 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006026 WARN("FUNCTION_ARGUMENTS",
6027 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006028 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006029
6030 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6031 $stat =~ /^.\s*extern\s+/)
6032 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006033 WARN("AVOID_EXTERNS",
6034 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006035 }
6036
Joe Perchesca0d8922016-10-11 13:52:16 -07006037 if ($realfile =~ /\.[ch]$/ && defined $stat &&
6038 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
6039 $1 ne "void") {
6040 my $args = trim($1);
6041 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6042 my $arg = trim($1);
6043 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6044 WARN("FUNCTION_ARGUMENTS",
6045 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6046 }
6047 }
6048 }
6049
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006050# checks for new __setup's
6051 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6052 my $name = $1;
6053
6054 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006055 CHK("UNDOCUMENTED_SETUP",
6056 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07006057 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07006058 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006059
6060# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08006061 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006062 WARN("UNNECESSARY_CASTS",
6063 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07006064 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006065
Joe Perchesa640d252013-07-03 15:05:21 -07006066# alloc style
6067# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6068 if ($^V && $^V ge 5.10.0 &&
6069 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6070 CHK("ALLOC_SIZEOF_STRUCT",
6071 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6072 }
6073
Joe Perches60a55362014-06-04 16:12:07 -07006074# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6075 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07006076 $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 -07006077 my $oldfunc = $3;
6078 my $a1 = $4;
6079 my $a2 = $10;
6080 my $newfunc = "kmalloc_array";
6081 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006082 my $r1 = $a1;
6083 my $r2 = $a2;
6084 if ($a1 =~ /^sizeof\s*\S/) {
6085 $r1 = $a2;
6086 $r2 = $a1;
6087 }
6088 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6089 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07006090 if (WARN("ALLOC_WITH_MULTIPLY",
6091 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
6092 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006093 $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 -07006094
6095 }
6096 }
6097 }
6098
Joe Perches972fdea2013-04-29 16:18:12 -07006099# check for krealloc arg reuse
6100 if ($^V && $^V ge 5.10.0 &&
6101 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6102 WARN("KREALLOC_ARG_REUSE",
6103 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6104 }
6105
Joe Perches5ce59ae2013-02-21 16:44:18 -08006106# check for alloc argument mismatch
6107 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6108 WARN("ALLOC_ARRAY_ARGS",
6109 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6110 }
6111
Joe Perchescaf2a542011-01-12 16:59:56 -08006112# check for multiple semicolons
6113 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006114 if (WARN("ONE_SEMICOLON",
6115 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6116 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006117 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006118 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006119 }
6120
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006121# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6122 if ($realfile !~ m@^include/uapi/@ &&
6123 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006124 my $ull = "";
6125 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6126 if (CHK("BIT_MACRO",
6127 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6128 $fix) {
6129 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6130 }
6131 }
6132
Joe Perches2d632742016-05-20 17:04:00 -07006133# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6134 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6135 my $config = $1;
6136 if (WARN("PREFER_IS_ENABLED",
6137 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6138 $fix) {
6139 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6140 }
6141 }
6142
Joe Perchese81f2392014-08-06 16:11:25 -07006143# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006144 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6145 my $has_break = 0;
6146 my $has_statement = 0;
6147 my $count = 0;
6148 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006149 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006150 $prevline--;
6151 my $rline = $rawlines[$prevline - 1];
6152 my $fline = $lines[$prevline - 1];
6153 last if ($fline =~ /^\@\@/);
6154 next if ($fline =~ /^\-/);
6155 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6156 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6157 next if ($fline =~ /^.[\s$;]*$/);
6158 $has_statement = 1;
6159 $count++;
6160 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6161 }
6162 if (!$has_break && $has_statement) {
6163 WARN("MISSING_BREAK",
6164 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
6165 }
6166 }
6167
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006168# check for switch/default statements without a break;
6169 if ($^V && $^V ge 5.10.0 &&
6170 defined $stat &&
6171 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6172 my $ctx = '';
6173 my $herectx = $here . "\n";
6174 my $cnt = statement_rawlines($stat);
6175 for (my $n = 0; $n < $cnt; $n++) {
6176 $herectx .= raw_line($linenr, $n) . "\n";
6177 }
6178 WARN("DEFAULT_NO_BREAK",
6179 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006180 }
6181
Patrick Pannutof370e252010-07-23 13:34:18 -07006182# check for return codes on error paths
6183 if ($line =~ /\breturn\s+-\d+/) {
6184 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07006185 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07006186 }
6187
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006188# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006189 if ($line =~ /\b__FUNCTION__\b/) {
6190 if (WARN("USE_FUNC",
6191 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6192 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006193 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006194 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006195 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006196
Joe Perches62ec8182015-02-13 14:38:18 -08006197# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6198 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6199 ERROR("DATE_TIME",
6200 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6201 }
6202
Joe Perches2c924882012-03-23 15:02:20 -07006203# check for use of yield()
6204 if ($line =~ /\byield\s*\(\s*\)/) {
6205 WARN("YIELD",
6206 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6207 }
6208
Joe Perches179f8f42013-07-03 15:05:30 -07006209# check for comparisons against true and false
6210 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6211 my $lead = $1;
6212 my $arg = $2;
6213 my $test = $3;
6214 my $otype = $4;
6215 my $trail = $5;
6216 my $op = "!";
6217
6218 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6219
6220 my $type = lc($otype);
6221 if ($type =~ /^(?:true|false)$/) {
6222 if (("$test" eq "==" && "$type" eq "true") ||
6223 ("$test" eq "!=" && "$type" eq "false")) {
6224 $op = "";
6225 }
6226
6227 CHK("BOOL_COMPARISON",
6228 "Using comparison to $otype is error prone\n" . $herecurr);
6229
6230## maybe suggesting a correct construct would better
6231## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6232
6233 }
6234 }
6235
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006236# check for semaphores initialized locked
6237 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006238 WARN("CONSIDER_COMPLETION",
6239 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006240 }
Joe Perches6712d852012-03-23 15:02:20 -07006241
Joe Perches67d0a072011-10-31 17:13:10 -07006242# recommend kstrto* over simple_strto* and strict_strto*
6243 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006244 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006245 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006246 }
Joe Perches6712d852012-03-23 15:02:20 -07006247
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006248# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006249 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006250 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006251 "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 -07006252 }
Joe Perches6712d852012-03-23 15:02:20 -07006253
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006254# check for various structs that are normally const (ops, kgdb, device_tree)
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006255 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006256 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006257 WARN("CONST_STRUCT",
6258 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006259 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006260 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006261
6262# use of NR_CPUS is usually wrong
6263# ignore definitions of NR_CPUS and usage to define arrays as likely right
6264 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006265 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6266 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006267 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6268 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6269 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006270 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006271 WARN("NR_CPUS",
6272 "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 -07006273 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006274
Joe Perches52ea8502013-11-12 15:10:09 -08006275# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6276 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6277 ERROR("DEFINE_ARCH_HAS",
6278 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6279 }
6280
Joe Perchesacd93622015-02-13 14:38:38 -08006281# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6282 if ($^V && $^V ge 5.10.0 &&
6283 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6284 WARN("LIKELY_MISUSE",
6285 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6286 }
6287
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006288# whine mightly about in_atomic
6289 if ($line =~ /\bin_atomic\s*\(/) {
6290 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006291 ERROR("IN_ATOMIC",
6292 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006293 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006294 WARN("IN_ATOMIC",
6295 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006296 }
6297 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006298
Joe Perches481aea52016-05-20 17:04:08 -07006299# whine about ACCESS_ONCE
6300 if ($^V && $^V ge 5.10.0 &&
6301 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6302 my $par = $1;
6303 my $eq = $2;
6304 my $fun = $3;
6305 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6306 if (defined($eq)) {
6307 if (WARN("PREFER_WRITE_ONCE",
6308 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6309 $fix) {
6310 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6311 }
6312 } else {
6313 if (WARN("PREFER_READ_ONCE",
6314 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6315 $fix) {
6316 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6317 }
6318 }
6319 }
6320
Peter Zijlstra1704f472010-03-19 01:37:42 +01006321# check for lockdep_set_novalidate_class
6322 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6323 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6324 if ($realfile !~ m@^kernel/lockdep@ &&
6325 $realfile !~ m@^include/linux/lockdep@ &&
6326 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006327 ERROR("LOCKDEP",
6328 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006329 }
6330 }
Dave Jones88f88312011-01-12 16:59:59 -08006331
Joe Perchesb392c642015-04-16 12:44:16 -07006332 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6333 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006334 WARN("EXPORTED_WORLD_WRITABLE",
6335 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006336 }
Joe Perches24358802014-04-03 14:49:13 -07006337
Joe Perches515a2352014-04-03 14:49:24 -07006338# Mode permission misuses where it seems decimal should be octal
6339# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6340 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006341 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006342 $line =~ /$mode_perms_search/) {
6343 foreach my $entry (@mode_permission_funcs) {
6344 my $func = $entry->[0];
6345 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006346
Joe Perches459cf0a2016-10-11 13:52:19 -07006347 my $lc = $stat =~ tr@\n@@;
6348 $lc = $lc + $linenr;
6349 my $stat_real = raw_line($linenr, 0);
6350 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6351 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6352 }
6353
Joe Perches515a2352014-04-03 14:49:24 -07006354 my $skip_args = "";
6355 if ($arg_pos > 1) {
6356 $arg_pos--;
6357 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6358 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006359 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006360 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006361 my $val = $1;
6362 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006363 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6364 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006365 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006366 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006367 }
6368 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006369 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006370 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perches515a2352014-04-03 14:49:24 -07006371 }
Joe Perches24358802014-04-03 14:49:13 -07006372 }
6373 }
6374 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006375
Joe Perches459cf0a2016-10-11 13:52:19 -07006376# check for uses of S_<PERMS> that could be octal for readability
6377 if ($line =~ /\b$mode_perms_string_search\b/) {
6378 my $val = "";
6379 my $oval = "";
6380 my $to = 0;
6381 my $curpos = 0;
6382 my $lastpos = 0;
6383 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6384 $curpos = pos($line);
6385 my $match = $2;
6386 my $omatch = $1;
6387 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6388 $lastpos = $curpos;
6389 $to |= $mode_permission_string_types{$match};
6390 $val .= '\s*\|\s*' if ($val ne "");
6391 $val .= $match;
6392 $oval .= $omatch;
6393 }
6394 $oval =~ s/^\s*\|\s*//;
6395 $oval =~ s/\s*\|\s*$//;
6396 my $octal = sprintf("%04o", $to);
6397 if (WARN("SYMBOLIC_PERMS",
6398 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6399 $fix) {
6400 $fixed[$fixlinenr] =~ s/$val/$octal/;
6401 }
6402 }
6403
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006404# validate content of MODULE_LICENSE against list from include/linux/module.h
6405 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6406 my $extracted_string = get_quoted_string($line, $rawline);
6407 my $valid_licenses = qr{
6408 GPL|
6409 GPL\ v2|
6410 GPL\ and\ additional\ rights|
6411 Dual\ BSD/GPL|
6412 Dual\ MIT/GPL|
6413 Dual\ MPL/GPL|
6414 Proprietary
6415 }x;
6416 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6417 WARN("MODULE_LICENSE",
6418 "unknown module license " . $extracted_string . "\n" . $herecurr);
6419 }
6420 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006421 }
6422
Maya Erezec77bd62017-02-26 09:14:43 +02006423 if ($chk_author && $qca_sign_off && !$codeaurora_sign_off) {
6424 WARN("BAD_SIGN_OFF",
6425 "QCA Signed-off-by requires CODEAURORA Signed-off-by\n" . $line );
6426 }
6427
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006428 # If we have no input at all, then there is nothing to report on
6429 # so just keep quiet.
6430 if ($#rawlines == -1) {
6431 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006432 }
6433
Andy Whitcroft8905a672007-11-28 16:21:06 -08006434 # In mailback mode only produce a report in the negative, for
6435 # things that appear to be patches.
6436 if ($mailback && ($clean == 1 || !$is_patch)) {
6437 exit(0);
6438 }
6439
6440 # This is not a patch, and we are are in 'no-patch' mode so
6441 # just keep quiet.
6442 if (!$chk_patch && !$is_patch) {
6443 exit(0);
6444 }
6445
Joe Perches06330fc2015-06-25 15:03:11 -07006446 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006447 ERROR("NOT_UNIFIED_DIFF",
6448 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006449 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006450 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006451 ERROR("MISSING_SIGN_OFF",
6452 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006453 }
6454
Andy Whitcroft8905a672007-11-28 16:21:06 -08006455 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006456 if ($summary && !($clean == 1 && $quiet == 1)) {
6457 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006458 print "total: $cnt_error errors, $cnt_warn warnings, " .
6459 (($check)? "$cnt_chk checks, " : "") .
6460 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006461 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006462
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006463 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006464 # If there were any defects found and not already fixing them
6465 if (!$clean and !$fix) {
6466 print << "EOM"
6467
6468NOTE: For some of the reported defects, checkpatch may be able to
6469 mechanically convert to the typical style using --fix or --fix-inplace.
6470EOM
6471 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006472 # If there were whitespace errors which cleanpatch can fix
6473 # then suggest that.
6474 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006475 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006476 print << "EOM"
6477
6478NOTE: Whitespace errors detected.
6479 You may wish to use scripts/cleanpatch or scripts/cleanfile
6480EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006481 }
6482 }
6483
Joe Perchesd752fcc2014-08-06 16:11:05 -07006484 if ($clean == 0 && $fix &&
6485 ("@rawlines" ne "@fixed" ||
6486 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006487 my $newfile = $filename;
6488 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006489 my $linecount = 0;
6490 my $f;
6491
Joe Perchesd752fcc2014-08-06 16:11:05 -07006492 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6493
Joe Perches3705ce52013-07-03 15:05:31 -07006494 open($f, '>', $newfile)
6495 or die "$P: Can't open $newfile for write\n";
6496 foreach my $fixed_line (@fixed) {
6497 $linecount++;
6498 if ($file) {
6499 if ($linecount > 3) {
6500 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006501 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006502 }
6503 } else {
6504 print $f $fixed_line . "\n";
6505 }
6506 }
6507 close($f);
6508
6509 if (!$quiet) {
6510 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006511
Joe Perches3705ce52013-07-03 15:05:31 -07006512Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6513
6514Do _NOT_ trust the results written to this file.
6515Do _NOT_ submit these changes without inspecting them for correctness.
6516
6517This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6518No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006519EOM
6520 }
6521 }
6522
Joe Perchesd8469f12015-06-25 15:03:00 -07006523 if ($quiet == 0) {
6524 print "\n";
6525 if ($clean == 1) {
6526 print "$vname has no obvious style problems and is ready for submission.\n";
6527 } else {
6528 print "$vname has style problems, please review.\n";
6529 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006530 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006531 return $clean;
6532}