blob: ab12e3040abb340b081285924b2e4c34f967ffbd [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
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 Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02009use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070010use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080011use File::Basename;
12use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070013use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070014
15my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080016my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
Joe Perches000d1cc12011-07-25 17:13:25 -070018my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070019
20use Getopt::Long qw(:config no_auto_abbrev);
21
22my $quiet = 0;
23my $tree = 1;
24my $chk_signoff = 1;
25my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070026my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080028my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070029my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070031my $git = 0;
Joe Perches0dea9f1e2016-05-20 17:04:19 -070032my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $check = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -070034my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $summary = 1;
36my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080037my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070038my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070039my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070040my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080041my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070042my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080043my %debug;
Joe Perches34456862013-07-03 15:05:34 -070044my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070045my %use_type = ();
46my @use = ();
47my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070048my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070049my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070050my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080051my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070052my $ignore_perl_version = 0;
53my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070054my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070055my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070056my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070057my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perchesbf1fa1d2016-10-11 13:51:56 -070058my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070059my $typedefsfile = "";
Joe Perches57230292015-06-25 15:03:03 -070060my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070061my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070062
63sub help {
64 my ($exitcode) = @_;
65
66 print << "EOM";
67Usage: $P [OPTION]... [FILE]...
68Version: $V
69
70Options:
71 -q, --quiet quiet
72 --no-tree run without a kernel tree
73 --no-signoff do not check for 'Signed-off-by' line
74 --patch treat FILE as patchfile (default)
75 --emacs emacs compile window format
76 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070077 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070078 -g, --git treat FILE as a single commit or git revision range
79 single git commit with:
80 <rev>
81 <rev>^
82 <rev>~n
83 multiple git commits with:
84 <rev1>..<rev2>
85 <rev1>...<rev2>
86 <rev>-<count>
87 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070088 -f, --file treat FILE as regular source file
89 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070090 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070091 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070092 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080094 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070095 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070096 --root=PATH PATH to the kernel tree root
97 --no-summary suppress the per-file summary
98 --mailback only produce a report in case of warnings/errors
99 --summary-file include the filename in summary
100 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
101 'values', 'possible', 'type', and 'attr' (default
102 is all off)
103 --test-only=WORD report only warnings/errors containing WORD
104 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700105 --fix EXPERIMENTAL - may create horrible results
106 If correctable single-line errors exist, create
107 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
108 with potential errors corrected to the preferred
109 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800110 --fix-inplace EXPERIMENTAL - may create horrible results
111 Is the same as --fix, but overwrites the input
112 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700113 --ignore-perl-version override checking of perl version. expect
114 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700115 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700116 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700117 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700118 --typedefsfile Read additional types from this file
Joe Perches57230292015-06-25 15:03:03 -0700119 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700120 -h, --help, --version display this help and exit
121
122When FILE is - read standard input.
123EOM
124
125 exit($exitcode);
126}
127
Joe Perches3beb42e2016-05-20 17:04:14 -0700128sub uniq {
129 my %seen;
130 return grep { !$seen{$_}++ } @_;
131}
132
133sub list_types {
134 my ($exitcode) = @_;
135
136 my $count = 0;
137
138 local $/ = undef;
139
140 open(my $script, '<', abs_path($P)) or
141 die "$P: Can't read '$P' $!\n";
142
143 my $text = <$script>;
144 close($script);
145
146 my @types = ();
147 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
148 push (@types, $_);
149 }
150 @types = sort(uniq(@types));
151 print("#\tMessage type\n\n");
152 foreach my $type (@types) {
153 print(++$count . "\t" . $type . "\n");
154 }
155
156 exit($exitcode);
157}
158
Joe Perches000d1cc12011-07-25 17:13:25 -0700159my $conf = which_conf($configuration_file);
160if (-f $conf) {
161 my @conf_args;
162 open(my $conffile, '<', "$conf")
163 or warn "$P: Can't find a readable $configuration_file file $!\n";
164
165 while (<$conffile>) {
166 my $line = $_;
167
168 $line =~ s/\s*\n?$//g;
169 $line =~ s/^\s*//g;
170 $line =~ s/\s+/ /g;
171
172 next if ($line =~ m/^\s*#/);
173 next if ($line =~ m/^\s*$/);
174
175 my @words = split(" ", $line);
176 foreach my $word (@words) {
177 last if ($word =~ m/^#/);
178 push (@conf_args, $word);
179 }
180 }
181 close($conffile);
182 unshift(@ARGV, @conf_args) if @conf_args;
183}
184
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700185GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700186 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700187 'tree!' => \$tree,
188 'signoff!' => \$chk_signoff,
189 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700190 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800191 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700192 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700193 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700194 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 'subjective!' => \$check,
196 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700197 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700198 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700199 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700200 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800201 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700202 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800204 'summary!' => \$summary,
205 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800206 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700207 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800208 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700209 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800210 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700211 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700212 'codespell!' => \$codespell,
213 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700214 'typedefsfile=s' => \$typedefsfile,
Joe Perches57230292015-06-25 15:03:03 -0700215 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700216 'h|help' => \$help,
217 'version' => \$help
218) or help(1);
219
220help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700221
Joe Perches3beb42e2016-05-20 17:04:14 -0700222list_types(0) if ($list_types);
223
Joe Perches9624b8d2014-01-23 15:54:44 -0800224$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700225$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800226
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700227my $exit = 0;
228
Dave Hansend62a2012013-09-11 14:23:56 -0700229if ($^V && $^V lt $minimum_perl_version) {
230 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
231 if (!$ignore_perl_version) {
232 exit(1);
233 }
234}
235
Allen Hubbe45107ff2016-08-02 14:04:47 -0700236#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700237if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700238 push(@ARGV, '-');
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700239}
240
Joe Perches91bfe482013-09-11 14:23:59 -0700241sub hash_save_array_words {
242 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700243
Joe Perches91bfe482013-09-11 14:23:59 -0700244 my @array = split(/,/, join(',', @$arrayRef));
245 foreach my $word (@array) {
246 $word =~ s/\s*\n?$//g;
247 $word =~ s/^\s*//g;
248 $word =~ s/\s+/ /g;
249 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700250
Joe Perches91bfe482013-09-11 14:23:59 -0700251 next if ($word =~ m/^\s*#/);
252 next if ($word =~ m/^\s*$/);
253
254 $hashRef->{$word}++;
255 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700256}
257
Joe Perches91bfe482013-09-11 14:23:59 -0700258sub hash_show_words {
259 my ($hashRef, $prefix) = @_;
260
Joe Perches3c816e42015-06-25 15:03:29 -0700261 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700262 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700263 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700264 print " $word";
265 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700266 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700267 }
268}
269
270hash_save_array_words(\%ignore_type, \@ignore);
271hash_save_array_words(\%use_type, \@use);
272
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800273my $dbg_values = 0;
274my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700275my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700276my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800277for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800278 ## no critic
279 eval "\${dbg_$key} = '$debug{$key}';";
280 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281}
282
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700283my $rpt_cleaners = 0;
284
Andy Whitcroft8905a672007-11-28 16:21:06 -0800285if ($terse) {
286 $emacs = 1;
287 $quiet++;
288}
289
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290if ($tree) {
291 if (defined $root) {
292 if (!top_of_kernel_tree($root)) {
293 die "$P: $root: --root does not point at a valid tree\n";
294 }
295 } else {
296 if (top_of_kernel_tree('.')) {
297 $root = '.';
298 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
299 top_of_kernel_tree($1)) {
300 $root = $1;
301 }
302 }
303
304 if (!defined $root) {
305 print "Must be run from the top-level dir. of a kernel tree\n";
306 exit(2);
307 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700308}
309
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310my $emitted_corrupt = 0;
311
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700312our $Ident = qr{
313 [A-Za-z_][A-Za-z\d_]*
314 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
315 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700316our $Storage = qr{extern|static|asmlinkage};
317our $Sparse = qr{
318 __user|
319 __kernel|
320 __force|
321 __iomem|
322 __must_check|
323 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800324 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700325 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800326 __rcu|
327 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700328 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800329our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
330our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
331our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
332our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
333our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700334
Wolfram Sang52131292010-03-05 13:43:51 -0800335# Notes to $Attribute:
336# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700337our $Attribute = qr{
338 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700339 __percpu|
340 __nocast|
341 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200342 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700343 __packed__|
344 __packed2__|
345 __naked|
346 __maybe_unused|
347 __always_unused|
348 __noreturn|
349 __used|
350 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800351 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700352 __noclone|
353 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700354 __read_mostly|
355 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700356 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700357 ____cacheline_aligned|
358 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800359 ____cacheline_internodealigned_in_smp|
360 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700361 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700362our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700363our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700364our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
365our $Lval = qr{$Ident(?:$Member)*};
366
Joe Perches95e2c602013-07-03 15:05:20 -0700367our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
368our $Binary = qr{(?i)0b[01]+$Int_type?};
369our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
370our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700371our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800372our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800373our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
374our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
375our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800376our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700377our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800378our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700379our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700380our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700381our $Operators = qr{
382 <=|>=|==|!=|
383 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700384 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700385 }x;
386
Joe Perches91cb5192014-04-03 14:49:32 -0700387our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
388
Joe Perchesab7e23f2015-04-16 12:44:22 -0700389our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800390our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700391our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700392our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800393our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700394our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700396our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397
Joe Perches15662b32011-10-31 17:13:12 -0700398our $NON_ASCII_UTF8 = qr{
399 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700400 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
401 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
402 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
403 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
404 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
405 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
406}x;
407
Joe Perches15662b32011-10-31 17:13:12 -0700408our $UTF8 = qr{
409 [\x09\x0A\x0D\x20-\x7E] # ASCII
410 | $NON_ASCII_UTF8
411}x;
412
Joe Perchese6176fa2015-06-25 15:02:49 -0700413our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800414our $typeOtherOSTypedefs = qr{(?x:
415 u_(?:char|short|int|long) | # bsd
416 u(?:nchar|short|int|long) # sysv
417)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700418our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700419 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700420 atomic_t
421)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700422our $typeTypedefs = qr{(?x:
423 $typeC99Typedefs\b|
424 $typeOtherOSTypedefs\b|
425 $typeKernelTypedefs\b
426)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700427
Joe Perches6d32f7a2015-11-06 16:31:37 -0800428our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
429
Joe Perches691e6692010-03-05 13:43:51 -0800430our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800431 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700432 (?:[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 -0700433 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700434 panic|
Joe Perches06668722013-11-12 15:10:07 -0800435 MODULE_[A-Z_]+|
436 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800437)};
438
Joe Perches20112472011-07-25 17:13:23 -0700439our $signature_tags = qr{(?xi:
440 Signed-off-by:|
441 Acked-by:|
442 Tested-by:|
443 Reviewed-by:|
444 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700445 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700446 To:|
447 Cc:
448)};
449
Joe Perches18130872014-08-06 16:11:22 -0700450our @typeListMisordered = (
451 qr{char\s+(?:un)?signed},
452 qr{int\s+(?:(?:un)?signed\s+)?short\s},
453 qr{int\s+short(?:\s+(?:un)?signed)},
454 qr{short\s+int(?:\s+(?:un)?signed)},
455 qr{(?:un)?signed\s+int\s+short},
456 qr{short\s+(?:un)?signed},
457 qr{long\s+int\s+(?:un)?signed},
458 qr{int\s+long\s+(?:un)?signed},
459 qr{long\s+(?:un)?signed\s+int},
460 qr{int\s+(?:un)?signed\s+long},
461 qr{int\s+(?:un)?signed},
462 qr{int\s+long\s+long\s+(?:un)?signed},
463 qr{long\s+long\s+int\s+(?:un)?signed},
464 qr{long\s+long\s+(?:un)?signed\s+int},
465 qr{long\s+long\s+(?:un)?signed},
466 qr{long\s+(?:un)?signed},
467);
468
Andy Whitcroft8905a672007-11-28 16:21:06 -0800469our @typeList = (
470 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700471 qr{(?:(?:un)?signed\s+)?char},
472 qr{(?:(?:un)?signed\s+)?short\s+int},
473 qr{(?:(?:un)?signed\s+)?short},
474 qr{(?:(?:un)?signed\s+)?int},
475 qr{(?:(?:un)?signed\s+)?long\s+int},
476 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
477 qr{(?:(?:un)?signed\s+)?long\s+long},
478 qr{(?:(?:un)?signed\s+)?long},
479 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800480 qr{float},
481 qr{double},
482 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800483 qr{struct\s+$Ident},
484 qr{union\s+$Ident},
485 qr{enum\s+$Ident},
486 qr{${Ident}_t},
487 qr{${Ident}_handler},
488 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700489 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800490);
Joe Perches938224b2016-01-20 14:59:15 -0800491
492our $C90_int_types = qr{(?x:
493 long\s+long\s+int\s+(?:un)?signed|
494 long\s+long\s+(?:un)?signed\s+int|
495 long\s+long\s+(?:un)?signed|
496 (?:(?:un)?signed\s+)?long\s+long\s+int|
497 (?:(?:un)?signed\s+)?long\s+long|
498 int\s+long\s+long\s+(?:un)?signed|
499 int\s+(?:(?:un)?signed\s+)?long\s+long|
500
501 long\s+int\s+(?:un)?signed|
502 long\s+(?:un)?signed\s+int|
503 long\s+(?:un)?signed|
504 (?:(?:un)?signed\s+)?long\s+int|
505 (?:(?:un)?signed\s+)?long|
506 int\s+long\s+(?:un)?signed|
507 int\s+(?:(?:un)?signed\s+)?long|
508
509 int\s+(?:un)?signed|
510 (?:(?:un)?signed\s+)?int
511)};
512
Alex Dowad485ff232015-06-25 15:02:52 -0700513our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700514our @typeListWithAttr = (
515 @typeList,
516 qr{struct\s+$InitAttribute\s+$Ident},
517 qr{union\s+$InitAttribute\s+$Ident},
518);
519
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700520our @modifierList = (
521 qr{fastcall},
522);
Alex Dowad485ff232015-06-25 15:02:52 -0700523our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800524
Joe Perches24358802014-04-03 14:49:13 -0700525our @mode_permission_funcs = (
526 ["module_param", 3],
527 ["module_param_(?:array|named|string)", 4],
528 ["module_param_array_named", 5],
529 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
530 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700531 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
532 ["IIO_DEV_ATTR_[A-Z_]+", 1],
533 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
534 ["SENSOR_TEMPLATE(?:_2|)", 3],
535 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700536);
537
Joe Perches515a2352014-04-03 14:49:24 -0700538#Create a search pattern for all these functions to speed up a loop below
539our $mode_perms_search = "";
540foreach my $entry (@mode_permission_funcs) {
541 $mode_perms_search .= '|' if ($mode_perms_search ne "");
542 $mode_perms_search .= $entry->[0];
543}
544
Joe Perchesb392c642015-04-16 12:44:16 -0700545our $mode_perms_world_writable = qr{
546 S_IWUGO |
547 S_IWOTH |
548 S_IRWXUGO |
549 S_IALLUGO |
550 0[0-7][0-7][2367]
551}x;
552
Joe Perchesf90774e2016-10-11 13:51:47 -0700553our %mode_permission_string_types = (
554 "S_IRWXU" => 0700,
555 "S_IRUSR" => 0400,
556 "S_IWUSR" => 0200,
557 "S_IXUSR" => 0100,
558 "S_IRWXG" => 0070,
559 "S_IRGRP" => 0040,
560 "S_IWGRP" => 0020,
561 "S_IXGRP" => 0010,
562 "S_IRWXO" => 0007,
563 "S_IROTH" => 0004,
564 "S_IWOTH" => 0002,
565 "S_IXOTH" => 0001,
566 "S_IRWXUGO" => 0777,
567 "S_IRUGO" => 0444,
568 "S_IWUGO" => 0222,
569 "S_IXUGO" => 0111,
570);
571
572#Create a search pattern for all these strings to speed up a loop below
573our $mode_perms_string_search = "";
574foreach my $entry (keys %mode_permission_string_types) {
575 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
576 $mode_perms_string_search .= $entry;
577}
578
Wolfram Sang7840a942010-08-09 17:20:57 -0700579our $allowed_asm_includes = qr{(?x:
580 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700581 memory|
582 time|
583 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700584)};
585# memory.h: ARM has a custom one
586
Kees Cook66b47b42014-10-13 15:51:57 -0700587# Load common spelling mistakes and build regular expression list.
588my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700589my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700590
Joe Perches36061e32014-12-10 15:51:43 -0800591if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800592 while (<$spelling>) {
593 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700594
Joe Perches36061e32014-12-10 15:51:43 -0800595 $line =~ s/\s*\n?$//g;
596 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700597
Joe Perches36061e32014-12-10 15:51:43 -0800598 next if ($line =~ m/^\s*#/);
599 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700600
Joe Perches36061e32014-12-10 15:51:43 -0800601 my ($suspect, $fix) = split(/\|\|/, $line);
602
Joe Perches36061e32014-12-10 15:51:43 -0800603 $spelling_fix{$suspect} = $fix;
604 }
605 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800606} else {
607 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700608}
Kees Cook66b47b42014-10-13 15:51:57 -0700609
Joe Perchesebfd7d62015-04-16 12:44:14 -0700610if ($codespell) {
611 if (open(my $spelling, '<', $codespellfile)) {
612 while (<$spelling>) {
613 my $line = $_;
614
615 $line =~ s/\s*\n?$//g;
616 $line =~ s/^\s*//g;
617
618 next if ($line =~ m/^\s*#/);
619 next if ($line =~ m/^\s*$/);
620 next if ($line =~ m/, disabled/i);
621
622 $line =~ s/,.*$//;
623
624 my ($suspect, $fix) = split(/->/, $line);
625
626 $spelling_fix{$suspect} = $fix;
627 }
628 close($spelling);
629 } else {
630 warn "No codespell typos will be found - file '$codespellfile': $!\n";
631 }
632}
633
634$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
635
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700636sub read_words {
637 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700638
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700639 if (open(my $words, '<', $file)) {
640 while (<$words>) {
641 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700642
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700643 $line =~ s/\s*\n?$//g;
644 $line =~ s/^\s*//g;
645
646 next if ($line =~ m/^\s*#/);
647 next if ($line =~ m/^\s*$/);
648 if ($line =~ /\s/) {
649 print("$file: '$line' invalid - ignored\n");
650 next;
651 }
652
653 $$wordsRef .= '|' if ($$wordsRef ne "");
654 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700655 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700656 close($file);
657 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700658 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700659
660 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700661}
662
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700663my $const_structs = "";
664read_words(\$const_structs, $conststructsfile)
665 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
666
667my $typeOtherTypedefs = "";
668if (length($typedefsfile)) {
669 read_words(\$typeOtherTypedefs, $typedefsfile)
670 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
671}
672$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
673
Andy Whitcroft8905a672007-11-28 16:21:06 -0800674sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700675 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
676 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700677 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700678 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700679 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700680 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700681 (?:$typeTypedefs\b)|
682 (?:${all}\b)
683 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800684 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700685 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800686 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800687 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700688 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700689 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800690 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700691 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800692 }x;
Joe Perches18130872014-08-06 16:11:22 -0700693 $NonptrTypeMisordered = qr{
694 (?:$Modifier\s+|const\s+)*
695 (?:
696 (?:${Misordered}\b)
697 )
698 (?:\s+$Modifier|\s+const)*
699 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700700 $NonptrTypeWithAttr = qr{
701 (?:$Modifier\s+|const\s+)*
702 (?:
703 (?:typeof|__typeof__)\s*\([^\)]*\)|
704 (?:$typeTypedefs\b)|
705 (?:${allWithAttr}\b)
706 )
707 (?:\s+$Modifier|\s+const)*
708 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800709 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700710 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700711 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700712 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800713 }x;
Joe Perches18130872014-08-06 16:11:22 -0700714 $TypeMisordered = qr{
715 $NonptrTypeMisordered
716 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
717 (?:\s+$Inline|\s+$Modifier)*
718 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700719 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700720 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800721}
722build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723
Joe Perches7d2367a2011-07-25 17:13:22 -0700724our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700725
726# Using $balanced_parens, $LvalOrFunc, or $FuncArg
727# requires at least perl version v5.10.0
728# Any use must be runtime checked with $^V
729
730our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700731our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800732our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700733
Joe Perchesf8422302014-08-06 16:11:31 -0700734our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700735 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700736 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700737 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
738)};
739
Joe Perches7d2367a2011-07-25 17:13:22 -0700740sub deparenthesize {
741 my ($string) = @_;
742 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700743
744 while ($string =~ /^\s*\(.*\)\s*$/) {
745 $string =~ s@^\s*\(\s*@@;
746 $string =~ s@\s*\)\s*$@@;
747 }
748
Joe Perches7d2367a2011-07-25 17:13:22 -0700749 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700750
Joe Perches7d2367a2011-07-25 17:13:22 -0700751 return $string;
752}
753
Joe Perches34456862013-07-03 15:05:34 -0700754sub seed_camelcase_file {
755 my ($file) = @_;
756
757 return if (!(-f $file));
758
759 local $/;
760
761 open(my $include_file, '<', "$file")
762 or warn "$P: Can't read '$file' $!\n";
763 my $text = <$include_file>;
764 close($include_file);
765
766 my @lines = split('\n', $text);
767
768 foreach my $line (@lines) {
769 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
770 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
771 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800772 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
773 $camelcase{$1} = 1;
774 } 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 -0700775 $camelcase{$1} = 1;
776 }
777 }
778}
779
Joe Perches85b0ee12016-10-11 13:51:44 -0700780sub is_maintained_obsolete {
781 my ($filename) = @_;
782
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800783 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700784
Joe Perches0616efa2016-10-11 13:52:02 -0700785 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 -0700786
787 return $status =~ /obsolete/i;
788}
789
Joe Perches34456862013-07-03 15:05:34 -0700790my $camelcase_seeded = 0;
791sub seed_camelcase_includes {
792 return if ($camelcase_seeded);
793
794 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700795 my $camelcase_cache = "";
796 my @include_files = ();
797
798 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700799
Richard Genoud3645e322014-02-10 14:25:32 -0800800 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700801 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
802 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700803 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700804 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700805 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700806 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700807 @include_files = split('\n', $files);
808 foreach my $file (@include_files) {
809 my $date = POSIX::strftime("%Y%m%d%H%M",
810 localtime((stat $file)[9]));
811 $last_mod_date = $date if ($last_mod_date < $date);
812 }
813 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700814 }
Joe Perchesc707a812013-07-08 16:00:43 -0700815
816 if ($camelcase_cache ne "" && -f $camelcase_cache) {
817 open(my $camelcase_file, '<', "$camelcase_cache")
818 or warn "$P: Can't read '$camelcase_cache' $!\n";
819 while (<$camelcase_file>) {
820 chomp;
821 $camelcase{$_} = 1;
822 }
823 close($camelcase_file);
824
825 return;
826 }
827
Richard Genoud3645e322014-02-10 14:25:32 -0800828 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700829 $files = `git ls-files "include/*.h"`;
830 @include_files = split('\n', $files);
831 }
832
Joe Perches34456862013-07-03 15:05:34 -0700833 foreach my $file (@include_files) {
834 seed_camelcase_file($file);
835 }
Joe Perches351b2a12013-07-03 15:05:36 -0700836
Joe Perchesc707a812013-07-08 16:00:43 -0700837 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700838 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700839 open(my $camelcase_file, '>', "$camelcase_cache")
840 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700841 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
842 print $camelcase_file ("$_\n");
843 }
844 close($camelcase_file);
845 }
Joe Perches34456862013-07-03 15:05:34 -0700846}
847
Joe Perchesd311cd42014-08-06 16:10:57 -0700848sub git_commit_info {
849 my ($commit, $id, $desc) = @_;
850
851 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
852
853 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
854 $output =~ s/^\s*//gm;
855 my @lines = split("\n", $output);
856
Joe Perches0d7835f2015-02-13 14:38:35 -0800857 return ($id, $desc) if ($#lines < 0);
858
Joe Perchesd311cd42014-08-06 16:10:57 -0700859 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
860# Maybe one day convert this block of bash into something that returns
861# all matching commit ids, but it's very slow...
862#
863# echo "checking commits $1..."
864# git rev-list --remotes | grep -i "^$1" |
865# while read line ; do
866# git log --format='%H %s' -1 $line |
867# echo "commit $(cut -c 1-12,41-)"
868# done
869 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700870 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700871 } else {
872 $id = substr($lines[0], 0, 12);
873 $desc = substr($lines[0], 41);
874 }
875
876 return ($id, $desc);
877}
878
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700879$chk_signoff = 0 if ($file);
880
Andy Whitcroft00df3442007-06-08 13:47:06 -0700881my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800882my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700883my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700884my @fixed_inserted = ();
885my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700886my $fixlinenr = -1;
887
Du, Changbin4a593c32016-05-20 17:04:16 -0700888# If input is git commits, extract all commits from the commit expressions.
889# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
890die "$P: No git repository found\n" if ($git && !-e ".git");
891
892if ($git) {
893 my @commits = ();
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700894 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700895 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700896 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
897 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700898 } elsif ($commit_expr =~ m/\.\./) {
899 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700900 } else {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700901 $git_range = "-1 $commit_expr";
902 }
903 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
904 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700905 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
906 next if (!defined($1) || !defined($2));
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700907 my $sha1 = $1;
908 my $subject = $2;
909 unshift(@commits, $sha1);
910 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700911 }
912 }
913 die "$P: no git commits after extraction!\n" if (@commits == 0);
914 @ARGV = @commits;
915}
916
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800917my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700918for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800919 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700920 if ($git) {
921 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
922 die "$P: $filename: git format-patch failed - $!\n";
923 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800924 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700925 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800926 } elsif ($filename eq '-') {
927 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700928 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800929 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700930 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700931 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800932 if ($filename eq '-') {
933 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700934 } elsif ($git) {
Joe Perches0dea9f1e2016-05-20 17:04:19 -0700935 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 } else {
937 $vname = $filename;
938 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800939 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700940 chomp;
941 push(@rawlines, $_);
942 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800943 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700944
945 if ($#ARGV > 0 && $quiet == 0) {
946 print '-' x length($vname) . "\n";
947 print "$vname\n";
948 print '-' x length($vname) . "\n";
949 }
950
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800951 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700952 $exit = 1;
953 }
954 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800955 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700956 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700957 @fixed_inserted = ();
958 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700959 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700960 @modifierListFile = ();
961 @typeListFile = ();
962 build_types();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700963}
964
Joe Perchesd8469f12015-06-25 15:03:00 -0700965if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700966 hash_show_words(\%use_type, "Used");
967 hash_show_words(\%ignore_type, "Ignored");
968
Joe Perchesd8469f12015-06-25 15:03:00 -0700969 if ($^V lt 5.10.0) {
970 print << "EOM"
971
972NOTE: perl $^V is not modern enough to detect all possible issues.
973 An upgrade to at least perl v5.10.0 is suggested.
974EOM
975 }
976 if ($exit) {
977 print << "EOM"
978
979NOTE: If any of the errors are false positives, please report
980 them to the maintainer, see CHECKPATCH in MAINTAINERS.
981EOM
982 }
983}
984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700985exit($exit);
986
987sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700988 my ($root) = @_;
989
990 my @tree_check = (
991 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
992 "README", "Documentation", "arch", "include", "drivers",
993 "fs", "init", "ipc", "kernel", "lib", "scripts",
994 );
995
996 foreach my $check (@tree_check) {
997 if (! -e $root . '/' . $check) {
998 return 0;
999 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001000 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001001 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001002}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001003
Joe Perches20112472011-07-25 17:13:23 -07001004sub parse_email {
1005 my ($formatted_email) = @_;
1006
1007 my $name = "";
1008 my $address = "";
1009 my $comment = "";
1010
1011 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1012 $name = $1;
1013 $address = $2;
1014 $comment = $3 if defined $3;
1015 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1016 $address = $1;
1017 $comment = $2 if defined $2;
1018 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1019 $address = $1;
1020 $comment = $2 if defined $2;
1021 $formatted_email =~ s/$address.*$//;
1022 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001023 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001024 $name =~ s/^\"|\"$//g;
1025 # If there's a name left after stripping spaces and
1026 # leading quotes, and the address doesn't have both
1027 # leading and trailing angle brackets, the address
1028 # is invalid. ie:
1029 # "joe smith joe@smith.com" bad
1030 # "joe smith <joe@smith.com" bad
1031 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1032 $name = "";
1033 $address = "";
1034 $comment = "";
1035 }
1036 }
1037
Joe Perches3705ce52013-07-03 15:05:31 -07001038 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001039 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001040 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001041 $address =~ s/^\<|\>$//g;
1042
1043 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1044 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1045 $name = "\"$name\"";
1046 }
1047
1048 return ($name, $address, $comment);
1049}
1050
1051sub format_email {
1052 my ($name, $address) = @_;
1053
1054 my $formatted_email;
1055
Joe Perches3705ce52013-07-03 15:05:31 -07001056 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001057 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001058 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001059
1060 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1061 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1062 $name = "\"$name\"";
1063 }
1064
1065 if ("$name" eq "") {
1066 $formatted_email = "$address";
1067 } else {
1068 $formatted_email = "$name <$address>";
1069 }
1070
1071 return $formatted_email;
1072}
1073
Joe Perchesd311cd42014-08-06 16:10:57 -07001074sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001075 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001076
Joe Perchesbd474ca2014-08-06 16:11:10 -07001077 foreach my $path (split(/:/, $ENV{PATH})) {
1078 if (-e "$path/$bin") {
1079 return "$path/$bin";
1080 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001081 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001082
Joe Perchesbd474ca2014-08-06 16:11:10 -07001083 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001084}
1085
Joe Perches000d1cc12011-07-25 17:13:25 -07001086sub which_conf {
1087 my ($conf) = @_;
1088
1089 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1090 if (-e "$path/$conf") {
1091 return "$path/$conf";
1092 }
1093 }
1094
1095 return "";
1096}
1097
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001098sub expand_tabs {
1099 my ($str) = @_;
1100
1101 my $res = '';
1102 my $n = 0;
1103 for my $c (split(//, $str)) {
1104 if ($c eq "\t") {
1105 $res .= ' ';
1106 $n++;
1107 for (; ($n % 8) != 0; $n++) {
1108 $res .= ' ';
1109 }
1110 next;
1111 }
1112 $res .= $c;
1113 $n++;
1114 }
1115
1116 return $res;
1117}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001118sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001119 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001120 return $res;
1121}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001122
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001123sub line_stats {
1124 my ($line) = @_;
1125
1126 # Drop the diff line leader and expand tabs
1127 $line =~ s/^.//;
1128 $line = expand_tabs($line);
1129
1130 # Pick the indent from the front of the line.
1131 my ($white) = ($line =~ /^(\s*)/);
1132
1133 return (length($line), length($white));
1134}
1135
Andy Whitcroft773647a2008-03-28 14:15:58 -07001136my $sanitise_quote = '';
1137
1138sub sanitise_line_reset {
1139 my ($in_comment) = @_;
1140
1141 if ($in_comment) {
1142 $sanitise_quote = '*/';
1143 } else {
1144 $sanitise_quote = '';
1145 }
1146}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001147sub sanitise_line {
1148 my ($line) = @_;
1149
1150 my $res = '';
1151 my $l = '';
1152
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001154 my $off = 0;
1155 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001156
Andy Whitcroft773647a2008-03-28 14:15:58 -07001157 # Always copy over the diff marker.
1158 $res = substr($line, 0, 1);
1159
1160 for ($off = 1; $off < length($line); $off++) {
1161 $c = substr($line, $off, 1);
1162
1163 # Comments we are wacking completly including the begin
1164 # and end, all to $;.
1165 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1166 $sanitise_quote = '*/';
1167
1168 substr($res, $off, 2, "$;$;");
1169 $off++;
1170 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001171 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001172 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001173 $sanitise_quote = '';
1174 substr($res, $off, 2, "$;$;");
1175 $off++;
1176 next;
1177 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001178 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1179 $sanitise_quote = '//';
1180
1181 substr($res, $off, 2, $sanitise_quote);
1182 $off++;
1183 next;
1184 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001185
1186 # A \ in a string means ignore the next character.
1187 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1188 $c eq "\\") {
1189 substr($res, $off, 2, 'XX');
1190 $off++;
1191 next;
1192 }
1193 # Regular quotes.
1194 if ($c eq "'" || $c eq '"') {
1195 if ($sanitise_quote eq '') {
1196 $sanitise_quote = $c;
1197
1198 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001199 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001200 } elsif ($sanitise_quote eq $c) {
1201 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001202 }
1203 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001204
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001205 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001206 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1207 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001208 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1209 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001210 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1211 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001212 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001213 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001214 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001215 }
1216
Daniel Walker113f04a2009-09-21 17:04:35 -07001217 if ($sanitise_quote eq '//') {
1218 $sanitise_quote = '';
1219 }
1220
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001221 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001222 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001223 my $clean = 'X' x length($1);
1224 $res =~ s@\<.*\>@<$clean>@;
1225
1226 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001227 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001228 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001229 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001230 }
1231
Joe Perchesdadf6802016-08-02 14:04:33 -07001232 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1233 my $match = $1;
1234 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1235 }
1236
Andy Whitcroft00df3442007-06-08 13:47:06 -07001237 return $res;
1238}
1239
Joe Perchesa6962d72013-04-29 16:18:13 -07001240sub get_quoted_string {
1241 my ($line, $rawline) = @_;
1242
Joe Perches33acb542015-06-25 15:02:54 -07001243 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001244 return substr($rawline, $-[0], $+[0] - $-[0]);
1245}
1246
Andy Whitcroft8905a672007-11-28 16:21:06 -08001247sub ctx_statement_block {
1248 my ($linenr, $remain, $off) = @_;
1249 my $line = $linenr - 1;
1250 my $blk = '';
1251 my $soff = $off;
1252 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001253 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001254
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001255 my $loff = 0;
1256
Andy Whitcroft8905a672007-11-28 16:21:06 -08001257 my $type = '';
1258 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001259 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001260 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 my $c;
1262 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001263
1264 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001265 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001266 @stack = (['', 0]) if ($#stack == -1);
1267
Andy Whitcroft773647a2008-03-28 14:15:58 -07001268 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001269 # If we are about to drop off the end, pull in more
1270 # context.
1271 if ($off >= $len) {
1272 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001273 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001274 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001275 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001276 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001277 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001278 $len = length($blk);
1279 $line++;
1280 last;
1281 }
1282 # Bail if there is no further context.
1283 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001284 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 last;
1286 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001287 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1288 $level++;
1289 $type = '#';
1290 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001291 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001292 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001293 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001294 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001295
Andy Whitcroft773647a2008-03-28 14:15:58 -07001296 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001297
1298 # Handle nested #if/#else.
1299 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1300 push(@stack, [ $type, $level ]);
1301 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1302 ($type, $level) = @{$stack[$#stack - 1]};
1303 } elsif ($remainder =~ /^#\s*endif\b/) {
1304 ($type, $level) = @{pop(@stack)};
1305 }
1306
Andy Whitcroft8905a672007-11-28 16:21:06 -08001307 # Statement ends at the ';' or a close '}' at the
1308 # outermost level.
1309 if ($level == 0 && $c eq ';') {
1310 last;
1311 }
1312
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001313 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001314 if ($level == 0 && $coff_set == 0 &&
1315 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1316 $remainder =~ /^(else)(?:\s|{)/ &&
1317 $remainder !~ /^else\s+if\b/) {
1318 $coff = $off + length($1) - 1;
1319 $coff_set = 1;
1320 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1321 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001322 }
1323
Andy Whitcroft8905a672007-11-28 16:21:06 -08001324 if (($type eq '' || $type eq '(') && $c eq '(') {
1325 $level++;
1326 $type = '(';
1327 }
1328 if ($type eq '(' && $c eq ')') {
1329 $level--;
1330 $type = ($level != 0)? '(' : '';
1331
1332 if ($level == 0 && $coff < $soff) {
1333 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001334 $coff_set = 1;
1335 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001336 }
1337 }
1338 if (($type eq '' || $type eq '{') && $c eq '{') {
1339 $level++;
1340 $type = '{';
1341 }
1342 if ($type eq '{' && $c eq '}') {
1343 $level--;
1344 $type = ($level != 0)? '{' : '';
1345
1346 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001347 if (substr($blk, $off + 1, 1) eq ';') {
1348 $off++;
1349 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001350 last;
1351 }
1352 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001353 # Preprocessor commands end at the newline unless escaped.
1354 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1355 $level--;
1356 $type = '';
1357 $off++;
1358 last;
1359 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001360 $off++;
1361 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001362 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001363 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001364 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001365 $line++;
1366 $remain--;
1367 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368
1369 my $statement = substr($blk, $soff, $off - $soff + 1);
1370 my $condition = substr($blk, $soff, $coff - $soff + 1);
1371
1372 #warn "STATEMENT<$statement>\n";
1373 #warn "CONDITION<$condition>\n";
1374
Andy Whitcroft773647a2008-03-28 14:15:58 -07001375 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001376
1377 return ($statement, $condition,
1378 $line, $remain + 1, $off - $loff + 1, $level);
1379}
1380
Andy Whitcroftcf655042008-03-04 14:28:20 -08001381sub statement_lines {
1382 my ($stmt) = @_;
1383
1384 # Strip the diff line prefixes and rip blank lines at start and end.
1385 $stmt =~ s/(^|\n)./$1/g;
1386 $stmt =~ s/^\s*//;
1387 $stmt =~ s/\s*$//;
1388
1389 my @stmt_lines = ($stmt =~ /\n/g);
1390
1391 return $#stmt_lines + 2;
1392}
1393
1394sub statement_rawlines {
1395 my ($stmt) = @_;
1396
1397 my @stmt_lines = ($stmt =~ /\n/g);
1398
1399 return $#stmt_lines + 2;
1400}
1401
1402sub statement_block_size {
1403 my ($stmt) = @_;
1404
1405 $stmt =~ s/(^|\n)./$1/g;
1406 $stmt =~ s/^\s*{//;
1407 $stmt =~ s/}\s*$//;
1408 $stmt =~ s/^\s*//;
1409 $stmt =~ s/\s*$//;
1410
1411 my @stmt_lines = ($stmt =~ /\n/g);
1412 my @stmt_statements = ($stmt =~ /;/g);
1413
1414 my $stmt_lines = $#stmt_lines + 2;
1415 my $stmt_statements = $#stmt_statements + 1;
1416
1417 if ($stmt_lines > $stmt_statements) {
1418 return $stmt_lines;
1419 } else {
1420 return $stmt_statements;
1421 }
1422}
1423
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424sub ctx_statement_full {
1425 my ($linenr, $remain, $off) = @_;
1426 my ($statement, $condition, $level);
1427
1428 my (@chunks);
1429
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001431 ($statement, $condition, $linenr, $remain, $off, $level) =
1432 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001433 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001434 push(@chunks, [ $condition, $statement ]);
1435 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1436 return ($level, $linenr, @chunks);
1437 }
1438
1439 # Pull in the following conditional/block pairs and see if they
1440 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001442 ($statement, $condition, $linenr, $remain, $off, $level) =
1443 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001444 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001445 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001446 #print "C: push\n";
1447 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001448 }
1449
1450 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001451}
1452
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001453sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001454 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001455 my $line;
1456 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001457 my $blk = '';
1458 my @o;
1459 my @c;
1460 my @res = ();
1461
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001462 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001463 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001464 for ($line = $start; $remain > 0; $line++) {
1465 next if ($rawlines[$line] =~ /^-/);
1466 $remain--;
1467
1468 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001469
1470 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001471 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001472 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001473 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001474 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001475 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001476 $level = pop(@stack);
1477 }
1478
Andy Whitcroft01464f32010-10-26 14:23:19 -07001479 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001480 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1481 if ($off > 0) {
1482 $off--;
1483 next;
1484 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001485
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001486 if ($c eq $close && $level > 0) {
1487 $level--;
1488 last if ($level == 0);
1489 } elsif ($c eq $open) {
1490 $level++;
1491 }
1492 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001494 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001495 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001496 }
1497
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001498 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001499 }
1500
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001501 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001502}
1503sub ctx_block_outer {
1504 my ($linenr, $remain) = @_;
1505
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001506 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1507 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001508}
1509sub ctx_block {
1510 my ($linenr, $remain) = @_;
1511
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001512 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1513 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001514}
1515sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001516 my ($linenr, $remain, $off) = @_;
1517
1518 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1519 return @r;
1520}
1521sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001522 my ($linenr, $remain) = @_;
1523
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001524 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001525}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001526sub ctx_statement_level {
1527 my ($linenr, $remain, $off) = @_;
1528
1529 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1530}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001531
1532sub ctx_locate_comment {
1533 my ($first_line, $end_line) = @_;
1534
1535 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001536 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001537 return $current_comment if (defined $current_comment);
1538
1539 # Look through the context and try and figure out if there is a
1540 # comment.
1541 my $in_comment = 0;
1542 $current_comment = '';
1543 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001544 my $line = $rawlines[$linenr - 1];
1545 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001546 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1547 $in_comment = 1;
1548 }
1549 if ($line =~ m@/\*@) {
1550 $in_comment = 1;
1551 }
1552 if (!$in_comment && $current_comment ne '') {
1553 $current_comment = '';
1554 }
1555 $current_comment .= $line . "\n" if ($in_comment);
1556 if ($line =~ m@\*/@) {
1557 $in_comment = 0;
1558 }
1559 }
1560
1561 chomp($current_comment);
1562 return($current_comment);
1563}
1564sub ctx_has_comment {
1565 my ($first_line, $end_line) = @_;
1566 my $cmt = ctx_locate_comment($first_line, $end_line);
1567
Andy Whitcroft00df3442007-06-08 13:47:06 -07001568 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569 ##print "CMMT: $cmt\n";
1570
1571 return ($cmt ne '');
1572}
1573
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001574sub raw_line {
1575 my ($linenr, $cnt) = @_;
1576
1577 my $offset = $linenr - 1;
1578 $cnt++;
1579
1580 my $line;
1581 while ($cnt) {
1582 $line = $rawlines[$offset++];
1583 next if (defined($line) && $line =~ /^-/);
1584 $cnt--;
1585 }
1586
1587 return $line;
1588}
1589
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001590sub cat_vet {
1591 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001592 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001593
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001594 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001595 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1596 $res .= $1;
1597 if ($2 ne '') {
1598 $coded = sprintf("^%c", unpack('C', $2) + 64);
1599 $res .= $coded;
1600 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001601 }
1602 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001603
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001604 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001605}
1606
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001607my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001608my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001609my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001610my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001611
1612sub annotate_reset {
1613 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001614 $av_pending = '_';
1615 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001616 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001617}
1618
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001619sub annotate_values {
1620 my ($stream, $type) = @_;
1621
1622 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001623 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001624 my $cur = $stream;
1625
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001626 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001627
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001628 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001629 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001630 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001631 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001632 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001633 print "WS($1)\n" if ($dbg_values > 1);
1634 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001635 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001636 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001637 }
1638
Florian Micklerc023e4732011-01-12 16:59:58 -08001639 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001640 print "CAST($1)\n" if ($dbg_values > 1);
1641 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001642 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001643
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001644 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001645 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001646 $type = 'T';
1647
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001648 } elsif ($cur =~ /^($Modifier)\s*/) {
1649 print "MODIFIER($1)\n" if ($dbg_values > 1);
1650 $type = 'T';
1651
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001652 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001653 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001654 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001655 push(@av_paren_type, $type);
1656 if ($2 ne '') {
1657 $av_pending = 'N';
1658 }
1659 $type = 'E';
1660
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001661 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001662 print "UNDEF($1)\n" if ($dbg_values > 1);
1663 $av_preprocessor = 1;
1664 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001665
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001666 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001667 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001668 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001669
1670 push(@av_paren_type, $type);
1671 push(@av_paren_type, $type);
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*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001675 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1676 $av_preprocessor = 1;
1677
1678 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1679
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001680 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001681
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001682 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001683 print "PRE_END($1)\n" if ($dbg_values > 1);
1684
1685 $av_preprocessor = 1;
1686
1687 # Assume all arms of the conditional end as this
1688 # one does, and continue as if the #endif was not here.
1689 pop(@av_paren_type);
1690 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001691 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001692
1693 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001694 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001695
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001696 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1697 print "ATTR($1)\n" if ($dbg_values > 1);
1698 $av_pending = $type;
1699 $type = 'N';
1700
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001701 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001702 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001703 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001704 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001705 }
1706 $type = 'N';
1707
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001708 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001709 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001710 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001711 $type = 'N';
1712
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001713 } elsif ($cur =~/^(case)/o) {
1714 print "CASE($1)\n" if ($dbg_values > 1);
1715 $av_pend_colon = 'C';
1716 $type = 'N';
1717
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001718 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001719 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001720 $type = 'N';
1721
1722 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001723 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001724 push(@av_paren_type, $av_pending);
1725 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 $type = 'N';
1727
1728 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001729 my $new_type = pop(@av_paren_type);
1730 if ($new_type ne '_') {
1731 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001732 print "PAREN('$1') -> $type\n"
1733 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001735 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001736 }
1737
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001738 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001739 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001740 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001741 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001743 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1744 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001745 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001746 } elsif ($type eq 'E') {
1747 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001748 }
1749 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1750 $type = 'V';
1751
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001752 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001753 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001754 $type = 'V';
1755
1756 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001757 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001758 $type = 'N';
1759
Andy Whitcroftcf655042008-03-04 14:28:20 -08001760 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001761 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001762 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001763 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001764
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001765 } elsif ($cur =~/^(,)/) {
1766 print "COMMA($1)\n" if ($dbg_values > 1);
1767 $type = 'C';
1768
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001769 } elsif ($cur =~ /^(\?)/o) {
1770 print "QUESTION($1)\n" if ($dbg_values > 1);
1771 $type = 'N';
1772
1773 } elsif ($cur =~ /^(:)/o) {
1774 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1775
1776 substr($var, length($res), 1, $av_pend_colon);
1777 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1778 $type = 'E';
1779 } else {
1780 $type = 'N';
1781 }
1782 $av_pend_colon = 'O';
1783
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001784 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001785 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001786 $type = 'N';
1787
Andy Whitcroft0d413862008-10-15 22:02:16 -07001788 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001789 my $variant;
1790
1791 print "OPV($1)\n" if ($dbg_values > 1);
1792 if ($type eq 'V') {
1793 $variant = 'B';
1794 } else {
1795 $variant = 'U';
1796 }
1797
1798 substr($var, length($res), 1, $variant);
1799 $type = 'N';
1800
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001801 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001803 if ($1 ne '++' && $1 ne '--') {
1804 $type = 'N';
1805 }
1806
1807 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001808 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809 }
1810 if (defined $1) {
1811 $cur = substr($cur, length($1));
1812 $res .= $type x length($1);
1813 }
1814 }
1815
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001816 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001817}
1818
Andy Whitcroft8905a672007-11-28 16:21:06 -08001819sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001820 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001821 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001822 ^(?:
1823 $Modifier|
1824 $Storage|
1825 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001826 DEFINE_\S+
1827 )$|
1828 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001829 goto|
1830 return|
1831 case|
1832 else|
1833 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001834 do|
1835 \#|
1836 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001837 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001838 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001839 )}x;
1840 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1841 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001842 # Check for modifiers.
1843 $possible =~ s/\s*$Storage\s*//g;
1844 $possible =~ s/\s*$Sparse\s*//g;
1845 if ($possible =~ /^\s*$/) {
1846
1847 } elsif ($possible =~ /\s/) {
1848 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001849 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001850 if ($modifier !~ $notPermitted) {
1851 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001852 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001853 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001854 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001855
1856 } else {
1857 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001858 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001859 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001860 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001861 } else {
1862 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001863 }
1864}
1865
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001866my $prefix = '';
1867
Joe Perches000d1cc12011-07-25 17:13:25 -07001868sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001869 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001870
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001871 $type =~ tr/[a-z]/[A-Z]/;
1872
Joe Perchescbec18a2014-04-03 14:49:19 -07001873 return defined $use_type{$type} if (scalar keys %use_type > 0);
1874
1875 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001876}
1877
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001878sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001879 my ($level, $type, $msg) = @_;
1880
1881 if (!show_type($type) ||
1882 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001883 return 0;
1884 }
Joe Perches57230292015-06-25 15:03:03 -07001885 my $output = '';
1886 if (-t STDOUT && $color) {
1887 if ($level eq 'ERROR') {
1888 $output .= RED;
1889 } elsif ($level eq 'WARNING') {
1890 $output .= YELLOW;
1891 } else {
1892 $output .= GREEN;
1893 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001894 }
Joe Perches57230292015-06-25 15:03:03 -07001895 $output .= $prefix . $level . ':';
1896 if ($show_types) {
1897 $output .= BLUE if (-t STDOUT && $color);
1898 $output .= "$type:";
1899 }
1900 $output .= RESET if (-t STDOUT && $color);
1901 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001902
1903 if ($showfile) {
1904 my @lines = split("\n", $output, -1);
1905 splice(@lines, 1, 1);
1906 $output = join("\n", @lines);
1907 }
Joe Perches57230292015-06-25 15:03:03 -07001908 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001909
Joe Perches57230292015-06-25 15:03:03 -07001910 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001911
1912 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001913}
Joe Perchescbec18a2014-04-03 14:49:19 -07001914
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001915sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001916 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001917}
Joe Perches000d1cc12011-07-25 17:13:25 -07001918
Joe Perchesd752fcc2014-08-06 16:11:05 -07001919sub fixup_current_range {
1920 my ($lineRef, $offset, $length) = @_;
1921
1922 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1923 my $o = $1;
1924 my $l = $2;
1925 my $no = $o + $offset;
1926 my $nl = $l + $length;
1927 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1928 }
1929}
1930
1931sub fix_inserted_deleted_lines {
1932 my ($linesRef, $insertedRef, $deletedRef) = @_;
1933
1934 my $range_last_linenr = 0;
1935 my $delta_offset = 0;
1936
1937 my $old_linenr = 0;
1938 my $new_linenr = 0;
1939
1940 my $next_insert = 0;
1941 my $next_delete = 0;
1942
1943 my @lines = ();
1944
1945 my $inserted = @{$insertedRef}[$next_insert++];
1946 my $deleted = @{$deletedRef}[$next_delete++];
1947
1948 foreach my $old_line (@{$linesRef}) {
1949 my $save_line = 1;
1950 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001951 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001952 $delta_offset = 0;
1953 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1954 $range_last_linenr = $new_linenr;
1955 fixup_current_range(\$line, $delta_offset, 0);
1956 }
1957
1958 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1959 $deleted = @{$deletedRef}[$next_delete++];
1960 $save_line = 0;
1961 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1962 }
1963
1964 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1965 push(@lines, ${$inserted}{'LINE'});
1966 $inserted = @{$insertedRef}[$next_insert++];
1967 $new_linenr++;
1968 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1969 }
1970
1971 if ($save_line) {
1972 push(@lines, $line);
1973 $new_linenr++;
1974 }
1975
1976 $old_linenr++;
1977 }
1978
1979 return @lines;
1980}
1981
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001982sub fix_insert_line {
1983 my ($linenr, $line) = @_;
1984
1985 my $inserted = {
1986 LINENR => $linenr,
1987 LINE => $line,
1988 };
1989 push(@fixed_inserted, $inserted);
1990}
1991
1992sub fix_delete_line {
1993 my ($linenr, $line) = @_;
1994
1995 my $deleted = {
1996 LINENR => $linenr,
1997 LINE => $line,
1998 };
1999
2000 push(@fixed_deleted, $deleted);
2001}
2002
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002003sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002004 my ($type, $msg) = @_;
2005
2006 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007 our $clean = 0;
2008 our $cnt_error++;
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 WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002014 my ($type, $msg) = @_;
2015
2016 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002017 our $clean = 0;
2018 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002019 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002020 }
Joe Perches3705ce52013-07-03 15:05:31 -07002021 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002022}
2023sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002024 my ($type, $msg) = @_;
2025
2026 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002027 our $clean = 0;
2028 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002029 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002030 }
Joe Perches3705ce52013-07-03 15:05:31 -07002031 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002032}
2033
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002034sub check_absolute_file {
2035 my ($absolute, $herecurr) = @_;
2036 my $file = $absolute;
2037
2038 ##print "absolute<$absolute>\n";
2039
2040 # See if any suffix of this path is a path within the tree.
2041 while ($file =~ s@^[^/]*/@@) {
2042 if (-f "$root/$file") {
2043 ##print "file<$file>\n";
2044 last;
2045 }
2046 }
2047 if (! -f _) {
2048 return 0;
2049 }
2050
2051 # It is, so see if the prefix is acceptable.
2052 my $prefix = $absolute;
2053 substr($prefix, -length($file)) = '';
2054
2055 ##print "prefix<$prefix>\n";
2056 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002057 WARN("USE_RELATIVE_PATH",
2058 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002059 }
2060}
2061
Joe Perches3705ce52013-07-03 15:05:31 -07002062sub trim {
2063 my ($string) = @_;
2064
Joe Perchesb34c6482013-09-11 14:24:01 -07002065 $string =~ s/^\s+|\s+$//g;
2066
2067 return $string;
2068}
2069
2070sub ltrim {
2071 my ($string) = @_;
2072
2073 $string =~ s/^\s+//;
2074
2075 return $string;
2076}
2077
2078sub rtrim {
2079 my ($string) = @_;
2080
2081 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002082
2083 return $string;
2084}
2085
Joe Perches52ea8502013-11-12 15:10:09 -08002086sub string_find_replace {
2087 my ($string, $find, $replace) = @_;
2088
2089 $string =~ s/$find/$replace/g;
2090
2091 return $string;
2092}
2093
Joe Perches3705ce52013-07-03 15:05:31 -07002094sub tabify {
2095 my ($leading) = @_;
2096
2097 my $source_indent = 8;
2098 my $max_spaces_before_tab = $source_indent - 1;
2099 my $spaces_to_tab = " " x $source_indent;
2100
2101 #convert leading spaces to tabs
2102 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2103 #Remove spaces before a tab
2104 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2105
2106 return "$leading";
2107}
2108
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002109sub pos_last_openparen {
2110 my ($line) = @_;
2111
2112 my $pos = 0;
2113
2114 my $opens = $line =~ tr/\(/\(/;
2115 my $closes = $line =~ tr/\)/\)/;
2116
2117 my $last_openparen = 0;
2118
2119 if (($opens == 0) || ($closes >= $opens)) {
2120 return -1;
2121 }
2122
2123 my $len = length($line);
2124
2125 for ($pos = 0; $pos < $len; $pos++) {
2126 my $string = substr($line, $pos);
2127 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2128 $pos += length($1) - 1;
2129 } elsif (substr($line, $pos, 1) eq '(') {
2130 $last_openparen = $pos;
2131 } elsif (index($string, '(') == -1) {
2132 last;
2133 }
2134 }
2135
Joe Perches91cb5192014-04-03 14:49:32 -07002136 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002137}
2138
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002139sub process {
2140 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002141
2142 my $linenr=0;
2143 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002144 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002145 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002146 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002147
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002148 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002149 my $indent;
2150 my $previndent=0;
2151 my $stashindent=0;
2152
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002153 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002154 my $signoff = 0;
2155 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002156 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002157 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002158 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002159 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002160 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002161 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002162 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002163 my $non_utf8_charset = 0;
2164
Joe Perches365dd4e2014-08-06 16:10:42 -07002165 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002166 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002167
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002168 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002169 our $cnt_lines = 0;
2170 our $cnt_error = 0;
2171 our $cnt_warn = 0;
2172 our $cnt_chk = 0;
2173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002174 # Trace the real file/line as we go.
2175 my $realfile = '';
2176 my $realline = 0;
2177 my $realcnt = 0;
2178 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002179 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002180 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002181 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002182 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002183 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002184
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002185 my $prev_values = 'E';
2186
2187 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002188 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002189 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002190 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002191 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002192
Joe Perches7e51f192013-09-11 14:23:57 -07002193 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002194
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002195 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002197 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002198 my @setup_docs = ();
2199 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002200
Joe Perchesd8b07712013-11-12 15:10:06 -08002201 my $camelcase_file_seeded = 0;
2202
Andy Whitcroft773647a2008-03-28 14:15:58 -07002203 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002204 my $line;
2205 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002206 $linenr++;
2207 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002208
Joe Perches3705ce52013-07-03 15:05:31 -07002209 push(@fixed, $rawline) if ($fix);
2210
Andy Whitcroft773647a2008-03-28 14:15:58 -07002211 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002212 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002213 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002214 $setup_docs = 1;
2215 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002216 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002217 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002218 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 $realline=$1-1;
2220 if (defined $2) {
2221 $realcnt=$3+1;
2222 } else {
2223 $realcnt=1+1;
2224 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002225 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002226
2227 # Guestimate if this is a continuing comment. Run
2228 # the context looking for a comment "edge". If this
2229 # edge is a close comment then we must be in a comment
2230 # at context start.
2231 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002232 my $cnt = $realcnt;
2233 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2234 next if (defined $rawlines[$ln - 1] &&
2235 $rawlines[$ln - 1] =~ /^-/);
2236 $cnt--;
2237 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002238 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002239 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2240 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2241 ($edge) = $1;
2242 last;
2243 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002244 }
2245 if (defined $edge && $edge eq '*/') {
2246 $in_comment = 1;
2247 }
2248
2249 # Guestimate if this is a continuing comment. If this
2250 # is the start of a diff block and this line starts
2251 # ' *' then it is very likely a comment.
2252 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002253 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002254 {
2255 $in_comment = 1;
2256 }
2257
2258 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2259 sanitise_line_reset($in_comment);
2260
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002261 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002262 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002263 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002264 $line = sanitise_line($rawline);
2265 }
2266 push(@lines, $line);
2267
2268 if ($realcnt > 1) {
2269 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2270 } else {
2271 $realcnt = 0;
2272 }
2273
2274 #print "==>$rawline\n";
2275 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002276
2277 if ($setup_docs && $line =~ /^\+/) {
2278 push(@setup_docs, $line);
2279 }
2280 }
2281
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002282 $prefix = '';
2283
Andy Whitcroft773647a2008-03-28 14:15:58 -07002284 $realcnt = 0;
2285 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002286 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002287 foreach my $line (@lines) {
2288 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002289 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002290 my $sline = $line; #copy of $line
2291 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002292
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002293 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002294
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002295#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002296 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002297 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2298 my $context = $4;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002299 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002300 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002301 $realline=$1-1;
2302 if (defined $2) {
2303 $realcnt=$3+1;
2304 } else {
2305 $realcnt=1+1;
2306 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002307 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002308 $prev_values = 'E';
2309
Andy Whitcroft773647a2008-03-28 14:15:58 -07002310 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002311 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002312 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002313 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002314 if ($context =~ /\b(\w+)\s*\(/) {
2315 $context_function = $1;
2316 } else {
2317 undef $context_function;
2318 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002319 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002320
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002321# track the line number as we move through the hunk, note that
2322# new versions of GNU diff omit the leading space on completely
2323# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002324 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002325 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002326 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002327
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002328 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002329 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002330
2331 # Track the previous line.
2332 ($prevline, $stashline) = ($stashline, $line);
2333 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002334 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2335
Andy Whitcroft773647a2008-03-28 14:15:58 -07002336 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002337
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002338 } elsif ($realcnt == 1) {
2339 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002340 }
2341
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002342 my $hunk_line = ($realcnt != 0);
2343
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002344 $here = "#$linenr: " if (!$file);
2345 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002346
Joe Perches2ac73b42014-06-04 16:12:05 -07002347 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002348 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002349 if ($line =~ /^diff --git.*?(\S+)$/) {
2350 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002351 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002352 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002353 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002354 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002355 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002356 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002357 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002358
2359 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002360 if (!$file && $tree && $p1_prefix ne '' &&
2361 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002362 WARN("PATCH_PREFIX",
2363 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002364 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002365
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002366 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002367 ERROR("MODIFIED_INCLUDE_ASM",
2368 "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 -07002369 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002370 $found_file = 1;
2371 }
2372
Joe Perches34d88152015-06-25 15:03:05 -07002373#make up the handle for any error we report on this line
2374 if ($showfile) {
2375 $prefix = "$realfile:$realline: "
2376 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002377 if ($file) {
2378 $prefix = "$filename:$realline: ";
2379 } else {
2380 $prefix = "$filename:$linenr: ";
2381 }
Joe Perches34d88152015-06-25 15:03:05 -07002382 }
2383
Joe Perches2ac73b42014-06-04 16:12:05 -07002384 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002385 if (is_maintained_obsolete($realfile)) {
2386 WARN("OBSOLETE",
2387 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2388 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002389 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002390 $check = 1;
2391 } else {
2392 $check = $check_orig;
2393 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002394 next;
2395 }
2396
Randy Dunlap389834b2007-06-08 13:47:03 -07002397 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002398
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002399 my $hereline = "$here\n$rawline\n";
2400 my $herecurr = "$here\n$rawline\n";
2401 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002402
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002403 $cnt_lines++ if ($realcnt != 0);
2404
Joe Perchese518e9a2015-06-25 15:03:27 -07002405# Check if the commit log has what seems like a diff which can confuse patch
2406 if ($in_commit_log && !$commit_log_has_diff &&
2407 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2408 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2409 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2410 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2411 ERROR("DIFF_IN_COMMIT_MSG",
2412 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2413 $commit_log_has_diff = 1;
2414 }
2415
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002416# Check for incorrect file permissions
2417 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2418 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002419 if ($realfile !~ m@scripts/@ &&
2420 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002421 ERROR("EXECUTE_PERMISSIONS",
2422 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002423 }
2424 }
2425
Joe Perches20112472011-07-25 17:13:23 -07002426# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002427 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002428 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002429 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002430 }
2431
Joe Perchese0d975b2014-12-10 15:51:49 -08002432# Check if MAINTAINERS is being updated. If so, there's probably no need to
2433# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2434 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2435 $reported_maintainer_file = 1;
2436 }
2437
Joe Perches20112472011-07-25 17:13:23 -07002438# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002439 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002440 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002441 my $space_before = $1;
2442 my $sign_off = $2;
2443 my $space_after = $3;
2444 my $email = $4;
2445 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2446
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002447 if ($sign_off !~ /$signature_tags/) {
2448 WARN("BAD_SIGN_OFF",
2449 "Non-standard signature: $sign_off\n" . $herecurr);
2450 }
Joe Perches20112472011-07-25 17:13:23 -07002451 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002452 if (WARN("BAD_SIGN_OFF",
2453 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2454 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002455 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002456 "$ucfirst_sign_off $email";
2457 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002458 }
Joe Perches20112472011-07-25 17:13:23 -07002459 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002460 if (WARN("BAD_SIGN_OFF",
2461 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2462 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002463 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002464 "$ucfirst_sign_off $email";
2465 }
2466
Joe Perches20112472011-07-25 17:13:23 -07002467 }
2468 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002469 if (WARN("BAD_SIGN_OFF",
2470 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2471 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002472 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002473 "$ucfirst_sign_off $email";
2474 }
Joe Perches20112472011-07-25 17:13:23 -07002475 }
2476
2477 my ($email_name, $email_address, $comment) = parse_email($email);
2478 my $suggested_email = format_email(($email_name, $email_address));
2479 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002480 ERROR("BAD_SIGN_OFF",
2481 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002482 } else {
2483 my $dequoted = $suggested_email;
2484 $dequoted =~ s/^"//;
2485 $dequoted =~ s/" </ </;
2486 # Don't force email to have quotes
2487 # Allow just an angle bracketed address
2488 if ("$dequoted$comment" ne $email &&
2489 "<$email_address>$comment" ne $email &&
2490 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002491 WARN("BAD_SIGN_OFF",
2492 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002493 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002494 }
Joe Perches7e51f192013-09-11 14:23:57 -07002495
2496# Check for duplicate signatures
2497 my $sig_nospace = $line;
2498 $sig_nospace =~ s/\s//g;
2499 $sig_nospace = lc($sig_nospace);
2500 if (defined $signatures{$sig_nospace}) {
2501 WARN("BAD_SIGN_OFF",
2502 "Duplicate signature\n" . $herecurr);
2503 } else {
2504 $signatures{$sig_nospace} = 1;
2505 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002506 }
2507
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002508# Check email subject for common tools that don't need to be mentioned
2509 if ($in_header_lines &&
2510 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2511 WARN("EMAIL_SUBJECT",
2512 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2513 }
2514
Joe Perches9b3189e2014-06-04 16:12:10 -07002515# Check for old stable address
2516 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2517 ERROR("STABLE_ADDRESS",
2518 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2519 }
2520
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002521# Check for unwanted Gerrit info
2522 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2523 ERROR("GERRIT_CHANGE_ID",
2524 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2525 }
2526
Joe Perches369c8dd2015-11-06 16:31:34 -08002527# Check if the commit log is in a possible stack dump
2528 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2529 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2530 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2531 # timestamp
2532 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2533 # stack dump address
2534 $commit_log_possible_stack_dump = 1;
2535 }
2536
Joe Perches2a076f42015-04-16 12:44:28 -07002537# Check for line lengths > 75 in commit log, warn once
2538 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002539 length($line) > 75 &&
2540 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2541 # file delta changes
2542 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2543 # filename then :
2544 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2545 # A Fixes: or Link: line
2546 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002547 WARN("COMMIT_LOG_LONG_LINE",
2548 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2549 $commit_log_long_line = 1;
2550 }
2551
Joe Perchesbf4daf12015-09-09 15:37:50 -07002552# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002553 if ($in_commit_log && $commit_log_possible_stack_dump &&
2554 $line =~ /^\s*$/) {
2555 $commit_log_possible_stack_dump = 0;
2556 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002557
Joe Perches0d7835f2015-02-13 14:38:35 -08002558# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002559 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002560 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002561 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002562 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002563 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002564 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2565 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002566 my $init_char = "c";
2567 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002568 my $short = 1;
2569 my $long = 0;
2570 my $case = 1;
2571 my $space = 1;
2572 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002573 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002574 my $id = '0123456789ab';
2575 my $orig_desc = "commit description";
2576 my $description = "";
2577
Joe Perchesfe043ea2015-09-09 15:37:25 -07002578 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2579 $init_char = $1;
2580 $orig_commit = lc($2);
2581 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2582 $orig_commit = lc($1);
2583 }
2584
Joe Perches0d7835f2015-02-13 14:38:35 -08002585 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2586 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2587 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2588 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2589 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2590 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002591 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002592 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2593 defined $rawlines[$linenr] &&
2594 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2595 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002596 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002597 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2598 defined $rawlines[$linenr] &&
2599 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2600 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2601 $orig_desc = $1;
2602 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2603 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002604 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002605 }
2606
2607 ($id, $description) = git_commit_info($orig_commit,
2608 $id, $orig_desc);
2609
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002610 if (defined($id) &&
2611 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002612 ERROR("GIT_COMMIT_ID",
2613 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2614 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002615 }
2616
Joe Perches13f19372014-08-06 16:10:59 -07002617# Check for added, moved or deleted files
2618 if (!$reported_maintainer_file && !$in_commit_log &&
2619 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2620 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2621 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2622 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002623 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002624 $reported_maintainer_file = 1;
2625 WARN("FILE_PATH_CHANGES",
2626 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2627 }
2628
Andy Whitcroft00df3442007-06-08 13:47:06 -07002629# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002630 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002631 ERROR("CORRUPTED_PATCH",
2632 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002633 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002634 }
2635
2636# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2637 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002638 $rawline !~ m/^$UTF8*$/) {
2639 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2640
2641 my $blank = copy_spacing($rawline);
2642 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2643 my $hereptr = "$hereline$ptr\n";
2644
Joe Perches34d99212011-07-25 17:13:26 -07002645 CHK("INVALID_UTF8",
2646 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002647 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002648
Joe Perches15662b32011-10-31 17:13:12 -07002649# Check if it's the start of a commit log
2650# (not a header line and we haven't seen the patch filename)
2651 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002652 !($rawline =~ /^\s+(?:\S|$)/ ||
2653 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002654 $in_header_lines = 0;
2655 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002656 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002657 }
2658
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002659# Check if there is UTF-8 in a commit log when a mail header has explicitly
2660# declined it, i.e defined some charset where it is missing.
2661 if ($in_header_lines &&
2662 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2663 $1 !~ /utf-8/i) {
2664 $non_utf8_charset = 1;
2665 }
2666
2667 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002668 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002669 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002670 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2671 }
2672
Joe Perchesd6430f72016-12-12 16:46:28 -08002673# Check for absolute kernel paths in commit message
2674 if ($tree && $in_commit_log) {
2675 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2676 my $file = $1;
2677
2678 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2679 check_absolute_file($1, $herecurr)) {
2680 #
2681 } else {
2682 check_absolute_file($file, $herecurr);
2683 }
2684 }
2685 }
2686
Kees Cook66b47b42014-10-13 15:51:57 -07002687# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002688 if (defined($misspellings) &&
2689 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002690 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002691 my $typo = $1;
2692 my $typo_fix = $spelling_fix{lc($typo)};
2693 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2694 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2695 my $msg_type = \&WARN;
2696 $msg_type = \&CHK if ($file);
2697 if (&{$msg_type}("TYPO_SPELLING",
2698 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2699 $fix) {
2700 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2701 }
2702 }
2703 }
2704
Andy Whitcroft306708542008-10-15 22:02:28 -07002705# ignore non-hunk lines and lines being removed
2706 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002707
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002708#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002709 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002710 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002711 if (ERROR("DOS_LINE_ENDINGS",
2712 "DOS line endings\n" . $herevet) &&
2713 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002714 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002715 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002716 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2717 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002718 if (ERROR("TRAILING_WHITESPACE",
2719 "trailing whitespace\n" . $herevet) &&
2720 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002721 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002722 }
2723
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002724 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002725 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002726
Josh Triplett4783f892013-11-12 15:10:12 -08002727# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002728 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002729 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002730 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2731 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002732 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2733 my $msg_type = \&ERROR;
2734 $msg_type = \&CHK if ($file);
2735 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002736 "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 -08002737 }
2738
Andi Kleen33549572010-05-24 14:33:29 -07002739# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002740# Only applies when adding the entry originally, after that we do not have
2741# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002742 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002743 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002744 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002745 my $cnt = $realcnt;
2746 my $ln = $linenr + 1;
2747 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002748 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002749 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002750 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002751 $f = $lines[$ln - 1];
2752 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2753 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002754
2755 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002756 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002757
Joe Perches8d73e0e2014-08-06 16:10:46 -07002758 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002759 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002760 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002761 $length = -1;
2762 }
2763
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002764 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002765 $f =~ s/#.*//;
2766 $f =~ s/^\s+//;
2767 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002768 if ($f =~ /^\s*config\s/) {
2769 $is_end = 1;
2770 last;
2771 }
Andi Kleen33549572010-05-24 14:33:29 -07002772 $length++;
2773 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002774 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2775 WARN("CONFIG_DESCRIPTION",
2776 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2777 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002778 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002779 }
2780
Joe Perches628f91a2017-07-10 15:52:07 -07002781# check for MAINTAINERS entries that don't have the right form
2782 if ($realfile =~ /^MAINTAINERS$/ &&
2783 $rawline =~ /^\+[A-Z]:/ &&
2784 $rawline !~ /^\+[A-Z]:\t\S/) {
2785 if (WARN("MAINTAINERS_STYLE",
2786 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2787 $fix) {
2788 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2789 }
2790 }
2791
Christoph Jaeger327953e2015-02-13 14:38:29 -08002792# discourage the use of boolean for type definition attributes of Kconfig options
2793 if ($realfile =~ /Kconfig/ &&
2794 $line =~ /^\+\s*\bboolean\b/) {
2795 WARN("CONFIG_TYPE_BOOLEAN",
2796 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2797 }
2798
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002799 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2800 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2801 my $flag = $1;
2802 my $replacement = {
2803 'EXTRA_AFLAGS' => 'asflags-y',
2804 'EXTRA_CFLAGS' => 'ccflags-y',
2805 'EXTRA_CPPFLAGS' => 'cppflags-y',
2806 'EXTRA_LDFLAGS' => 'ldflags-y',
2807 };
2808
2809 WARN("DEPRECATED_VARIABLE",
2810 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2811 }
2812
Rob Herringbff5da42014-01-23 15:54:51 -08002813# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002814 if (defined $root &&
2815 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2816 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2817
Rob Herringbff5da42014-01-23 15:54:51 -08002818 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2819
Florian Vaussardcc933192014-04-03 14:49:27 -07002820 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2821 my $vp_file = $dt_path . "vendor-prefixes.txt";
2822
Rob Herringbff5da42014-01-23 15:54:51 -08002823 foreach my $compat (@compats) {
2824 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002825 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2826 my $compat3 = $compat;
2827 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2828 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002829 if ( $? >> 8 ) {
2830 WARN("UNDOCUMENTED_DT_STRING",
2831 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2832 }
2833
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002834 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2835 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002836 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002837 if ( $? >> 8 ) {
2838 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002839 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002840 }
2841 }
2842 }
2843
Andy Whitcroft5368df202008-10-15 22:02:27 -07002844# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002845 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002846
Joe Perches47e0c882015-06-25 15:02:57 -07002847# line length limit (with some exclusions)
2848#
2849# There are a few types of lines that may extend beyond $max_line_length:
2850# logging functions like pr_info that end in a string
2851# lines with a single string
2852# #defines that are a single string
2853#
2854# There are 3 different line length message types:
2855# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2856# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2857# LONG_LINE all other lines longer than $max_line_length
2858#
2859# if LONG_LINE is ignored, the other 2 types are also ignored
2860#
2861
Joe Perchesb4749e92015-07-17 16:24:01 -07002862 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07002863 my $msg_type = "LONG_LINE";
2864
2865 # Check the allowed long line types first
2866
2867 # logging functions that end in a string that starts
2868 # before $max_line_length
2869 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2870 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2871 $msg_type = "";
2872
2873 # lines with only strings (w/ possible termination)
2874 # #defines with only strings
2875 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2876 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2877 $msg_type = "";
2878
Joe Perchesd560a5f2016-08-02 14:04:31 -07002879 # EFI_GUID is another special case
2880 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2881 $msg_type = "";
2882
Joe Perches47e0c882015-06-25 15:02:57 -07002883 # Otherwise set the alternate message types
2884
2885 # a comment starts before $max_line_length
2886 } elsif ($line =~ /($;[\s$;]*)$/ &&
2887 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2888 $msg_type = "LONG_LINE_COMMENT"
2889
2890 # a quoted string starts before $max_line_length
2891 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2892 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2893 $msg_type = "LONG_LINE_STRING"
2894 }
2895
2896 if ($msg_type ne "" &&
2897 (show_type("LONG_LINE") || show_type($msg_type))) {
2898 WARN($msg_type,
2899 "line over $max_line_length characters\n" . $herecurr);
2900 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002901 }
2902
Andy Whitcroft8905a672007-11-28 16:21:06 -08002903# check for adding lines without a newline.
2904 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002905 WARN("MISSING_EOF_NEWLINE",
2906 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002907 }
2908
Mike Frysinger42e41c52009-09-21 17:04:40 -07002909# Blackfin: use hi/lo macros
2910 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2911 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2912 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002913 ERROR("LO_MACRO",
2914 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002915 }
2916 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2917 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002918 ERROR("HI_MACRO",
2919 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002920 }
2921 }
2922
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002923# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002924 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002925
2926# at the beginning of a line any tabs must come first and anything
2927# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002928 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2929 $rawline =~ /^\+\s* \s*/) {
2930 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002931 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002932 if (ERROR("CODE_INDENT",
2933 "code indent should use tabs where possible\n" . $herevet) &&
2934 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002935 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002936 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002937 }
2938
Alberto Panizzo08e44362010-03-05 13:43:54 -08002939# check for space before tabs.
2940 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2941 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002942 if (WARN("SPACE_BEFORE_TAB",
2943 "please, no space before tabs\n" . $herevet) &&
2944 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002945 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002946 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002947 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002948 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002949 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002950 }
2951
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002952# check for && or || at the start of a line
2953 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2954 CHK("LOGICAL_CONTINUATIONS",
2955 "Logical continuations should be on the previous line\n" . $hereprev);
2956 }
2957
Joe Perchesa91e8992016-05-20 17:04:05 -07002958# check indentation starts on a tab stop
2959 if ($^V && $^V ge 5.10.0 &&
2960 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2961 my $indent = length($1);
2962 if ($indent % 8) {
2963 if (WARN("TABSTOP",
2964 "Statements should start on a tabstop\n" . $herecurr) &&
2965 $fix) {
2966 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2967 }
2968 }
2969 }
2970
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002971# check multi-line statement indentation matches previous line
2972 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002973 $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 -07002974 $prevline =~ /^\+(\t*)(.*)$/;
2975 my $oldindent = $1;
2976 my $rest = $2;
2977
2978 my $pos = pos_last_openparen($rest);
2979 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002980 $line =~ /^(\+| )([ \t]*)/;
2981 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002982
2983 my $goodtabindent = $oldindent .
2984 "\t" x ($pos / 8) .
2985 " " x ($pos % 8);
2986 my $goodspaceindent = $oldindent . " " x $pos;
2987
2988 if ($newindent ne $goodtabindent &&
2989 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002990
2991 if (CHK("PARENTHESIS_ALIGNMENT",
2992 "Alignment should match open parenthesis\n" . $hereprev) &&
2993 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002994 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002995 s/^\+[ \t]*/\+$goodtabindent/;
2996 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002997 }
2998 }
2999 }
3000
Joe Perches6ab3a972015-04-16 12:44:05 -07003001# check for space after cast like "(int) foo" or "(struct foo) bar"
3002# avoid checking a few false positives:
3003# "sizeof(<type>)" or "__alignof__(<type>)"
3004# function pointer declarations like "(*foo)(int) = bar;"
3005# structure definitions like "(struct foo) { 0 };"
3006# multiline macros that define functions
3007# known attributes or the __attribute__ keyword
3008 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3009 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003010 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003011 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003012 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003013 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003014 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003015 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003016 }
3017
Joe Perches86406b12015-09-09 15:37:41 -07003018# Block comment styles
3019# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003020 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003021 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003022 $rawline =~ /^\+[ \t]*\*/ &&
3023 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003024 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3025 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3026 }
3027
Joe Perches86406b12015-09-09 15:37:41 -07003028# Block comments use * on subsequent lines
3029 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3030 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003031 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003032 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003033 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003034 WARN("BLOCK_COMMENT_STYLE",
3035 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003036 }
3037
Joe Perches86406b12015-09-09 15:37:41 -07003038# Block comments use */ on trailing lines
3039 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003040 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3041 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3042 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003043 WARN("BLOCK_COMMENT_STYLE",
3044 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003045 }
3046
Joe Perches08eb9b82016-10-11 13:51:50 -07003047# Block comment * alignment
3048 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003049 $line =~ /^\+[ \t]*$;/ && #leading comment
3050 $rawline =~ /^\+[ \t]*\*/ && #leading *
3051 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003052 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003053 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3054 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003055 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003056 if (defined($1)) {
3057 $oldindent = expand_tabs($1);
3058 } else {
3059 $prevrawline =~ m@^\+(.*/?)\*@;
3060 $oldindent = expand_tabs($1);
3061 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003062 $rawline =~ m@^\+([ \t]*)\*@;
3063 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003064 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003065 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003066 WARN("BLOCK_COMMENT_STYLE",
3067 "Block comments should align the * on each line\n" . $hereprev);
3068 }
3069 }
3070
Joe Perches7f619192014-08-06 16:10:39 -07003071# check for missing blank lines after struct/union declarations
3072# with exceptions for various attributes and macros
3073 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3074 $line =~ /^\+/ &&
3075 !($line =~ /^\+\s*$/ ||
3076 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3077 $line =~ /^\+\s*MODULE_/i ||
3078 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3079 $line =~ /^\+[a-z_]*init/ ||
3080 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3081 $line =~ /^\+\s*DECLARE/ ||
3082 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003083 if (CHK("LINE_SPACING",
3084 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3085 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003086 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003087 }
Joe Perches7f619192014-08-06 16:10:39 -07003088 }
3089
Joe Perches365dd4e2014-08-06 16:10:42 -07003090# check for multiple consecutive blank lines
3091 if ($prevline =~ /^[\+ ]\s*$/ &&
3092 $line =~ /^\+\s*$/ &&
3093 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003094 if (CHK("LINE_SPACING",
3095 "Please don't use multiple blank lines\n" . $hereprev) &&
3096 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003097 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003098 }
3099
Joe Perches365dd4e2014-08-06 16:10:42 -07003100 $last_blank_line = $linenr;
3101 }
3102
Joe Perches3b617e32014-04-03 14:49:28 -07003103# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003104 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3105 # actual declarations
3106 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003107 # function pointer declarations
3108 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003109 # foo bar; where foo is some local typedef or #define
3110 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3111 # known declaration macros
3112 $prevline =~ /^\+\s+$declaration_macros/) &&
3113 # for "else if" which can look like "$Ident $Ident"
3114 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3115 # other possible extensions of declaration lines
3116 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3117 # not starting a section or a macro "\" extended line
3118 $prevline =~ /(?:\{\s*|\\)$/) &&
3119 # looks like a declaration
3120 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003121 # function pointer declarations
3122 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003123 # foo bar; where foo is some local typedef or #define
3124 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3125 # known declaration macros
3126 $sline =~ /^\+\s+$declaration_macros/ ||
3127 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003128 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003129 # start or end of block or continuation of declaration
3130 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3131 # bitfield continuation
3132 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3133 # other possible extensions of declaration lines
3134 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3135 # indentation of previous and current line are the same
3136 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003137 if (WARN("LINE_SPACING",
3138 "Missing a blank line after declarations\n" . $hereprev) &&
3139 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003140 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003141 }
Joe Perches3b617e32014-04-03 14:49:28 -07003142 }
3143
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003144# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003145# Exceptions:
3146# 1) within comments
3147# 2) indented preprocessor commands
3148# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003149 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003150 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003151 if (WARN("LEADING_SPACE",
3152 "please, no spaces at the start of a line\n" . $herevet) &&
3153 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003154 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003155 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003156 }
3157
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003158# check we are in a valid C source file if not then ignore this hunk
3159 next if ($realfile !~ /\.(h|c)$/);
3160
Joe Perches4dbed762017-05-08 15:55:39 -07003161# check if this appears to be the start function declaration, save the name
3162 if ($sline =~ /^\+\{\s*$/ &&
3163 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3164 $context_function = $1;
3165 }
3166
3167# check if this appears to be the end of function declaration
3168 if ($sline =~ /^\+\}\s*$/) {
3169 undef $context_function;
3170 }
3171
Joe Perches032a4c02014-08-06 16:10:29 -07003172# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003173# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003174# if the previous line is a break or return and is indented 1 tab more...
3175 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3176 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003177 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3178 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3179 defined $lines[$linenr] &&
3180 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003181 WARN("UNNECESSARY_ELSE",
3182 "else is not generally useful after a break or return\n" . $hereprev);
3183 }
3184 }
3185
Joe Perchesc00df192014-08-06 16:11:01 -07003186# check indentation of a line with a break;
3187# if the previous line is a goto or return and is indented the same # of tabs
3188 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3189 my $tabs = $1;
3190 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3191 WARN("UNNECESSARY_BREAK",
3192 "break is not useful after a goto or return\n" . $hereprev);
3193 }
3194 }
3195
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003196# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003197 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003198 WARN("CVS_KEYWORD",
3199 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003200 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003201
Mike Frysinger42e41c52009-09-21 17:04:40 -07003202# Blackfin: don't use __builtin_bfin_[cs]sync
3203 if ($line =~ /__builtin_bfin_csync/) {
3204 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003205 ERROR("CSYNC",
3206 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003207 }
3208 if ($line =~ /__builtin_bfin_ssync/) {
3209 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003210 ERROR("SSYNC",
3211 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003212 }
3213
Joe Perches56e77d72013-02-21 16:44:14 -08003214# check for old HOTPLUG __dev<foo> section markings
3215 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3216 WARN("HOTPLUG_SECTION",
3217 "Using $1 is unnecessary\n" . $herecurr);
3218 }
3219
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003220# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003221 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3222 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003223#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003224 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003225 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003226 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003227 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003228 $stat =~ s/\n./\n /g;
3229 $cond =~ s/\n./\n /g;
3230
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003231#print "linenr<$linenr> <$stat>\n";
3232 # If this statement has no statement boundaries within
3233 # it there is no point in retrying a statement scan
3234 # until we hit end of it.
3235 my $frag = $stat; $frag =~ s/;+\s*$//;
3236 if ($frag !~ /(?:{|;)/) {
3237#print "skip<$line_nr_next>\n";
3238 $suppress_statement = $line_nr_next;
3239 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003240
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003241 # Find the real next line.
3242 $realline_next = $line_nr_next;
3243 if (defined $realline_next &&
3244 (!defined $lines[$realline_next - 1] ||
3245 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3246 $realline_next++;
3247 }
3248
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003249 my $s = $stat;
3250 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003251
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003252 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003253 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003254
3255 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003256 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003257
Andy Whitcroft463f2862009-09-21 17:04:34 -07003258 } elsif ($s =~ /^.\s*else\b/s) {
3259
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003260 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003261 } 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 -07003262 my $type = $1;
3263 $type =~ s/\s+/ /g;
3264 possible($type, "A:" . $s);
3265
Andy Whitcroft8905a672007-11-28 16:21:06 -08003266 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003267 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003268 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003269 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003270
3271 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003272 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003273 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003274 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003275
3276 # Check for any sort of function declaration.
3277 # int foo(something bar, other baz);
3278 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003279 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 -08003280 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003281
Andy Whitcroftcf655042008-03-04 14:28:20 -08003282 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003283 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003284 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003285
Andy Whitcroft8905a672007-11-28 16:21:06 -08003286 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003287 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003288
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003289 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003290 }
3291 }
3292 }
3293
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003294 }
3295
Andy Whitcroft653d4872007-06-23 17:16:34 -07003296#
3297# Checks which may be anchored in the context.
3298#
3299
3300# Check for switch () and associated case and default
3301# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003302 if ($line=~/\bswitch\s*\(.*\)/) {
3303 my $err = '';
3304 my $sep = '';
3305 my @ctx = ctx_block_outer($linenr, $realcnt);
3306 shift(@ctx);
3307 for my $ctx (@ctx) {
3308 my ($clen, $cindent) = line_stats($ctx);
3309 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3310 $indent != $cindent) {
3311 $err .= "$sep$ctx\n";
3312 $sep = '';
3313 } else {
3314 $sep = "[...]\n";
3315 }
3316 }
3317 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003318 ERROR("SWITCH_CASE_INDENT_LEVEL",
3319 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003320 }
3321 }
3322
3323# if/while/etc brace do not go on next line, unless defining a do while loop,
3324# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003325 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 -07003326 my $pre_ctx = "$1$2";
3327
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003328 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003329
3330 if ($line =~ /^\+\t{6,}/) {
3331 WARN("DEEP_INDENTATION",
3332 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3333 }
3334
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003335 my $ctx_cnt = $realcnt - $#ctx - 1;
3336 my $ctx = join("\n", @ctx);
3337
Andy Whitcroft548596d2008-07-23 21:29:01 -07003338 my $ctx_ln = $linenr;
3339 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003340
Andy Whitcroft548596d2008-07-23 21:29:01 -07003341 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3342 defined $lines[$ctx_ln - 1] &&
3343 $lines[$ctx_ln - 1] =~ /^-/)) {
3344 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3345 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003346 $ctx_ln++;
3347 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003348
Andy Whitcroft53210162008-07-23 21:29:03 -07003349 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3350 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003351
Joe Perchesd752fcc2014-08-06 16:11:05 -07003352 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003353 ERROR("OPEN_BRACE",
3354 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003355 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003356 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003357 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3358 $ctx =~ /\)\s*\;\s*$/ &&
3359 defined $lines[$ctx_ln - 1])
3360 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003361 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3362 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003363 WARN("TRAILING_SEMICOLON",
3364 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003365 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003366 }
3367 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003368 }
3369
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003370# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003371 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003372 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3373 ctx_statement_block($linenr, $realcnt, 0)
3374 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003375 my ($s, $c) = ($stat, $cond);
3376
3377 substr($s, 0, length($c), '');
3378
Joe Perches9f5af482015-09-09 15:37:30 -07003379 # remove inline comments
3380 $s =~ s/$;/ /g;
3381 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003382
3383 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003384 my @newlines = ($c =~ /\n/gs);
3385 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003386
Joe Perches9f5af482015-09-09 15:37:30 -07003387 # Make sure we remove the line prefixes as we have
3388 # none on the first line, and are going to readd them
3389 # where necessary.
3390 $s =~ s/\n./\n/gs;
3391 while ($s =~ /\n\s+\\\n/) {
3392 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3393 }
3394
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003395 # We want to check the first line inside the block
3396 # starting at the end of the conditional, so remove:
3397 # 1) any blank line termination
3398 # 2) any opening brace { on end of the line
3399 # 3) any do (...) {
3400 my $continuation = 0;
3401 my $check = 0;
3402 $s =~ s/^.*\bdo\b//;
3403 $s =~ s/^\s*{//;
3404 if ($s =~ s/^\s*\\//) {
3405 $continuation = 1;
3406 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003407 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003408 $check = 1;
3409 $cond_lines++;
3410 }
3411
3412 # Also ignore a loop construct at the end of a
3413 # preprocessor statement.
3414 if (($prevline =~ /^.\s*#\s*define\s/ ||
3415 $prevline =~ /\\\s*$/) && $continuation == 0) {
3416 $check = 0;
3417 }
3418
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003419 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003420 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003421 while ($cond_ptr != $cond_lines) {
3422 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003423
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003424 # If we see an #else/#elif then the code
3425 # is not linear.
3426 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3427 $check = 0;
3428 }
3429
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003430 # Ignore:
3431 # 1) blank lines, they should be at 0,
3432 # 2) preprocessor lines, and
3433 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003434 if ($continuation ||
3435 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003436 $s =~ /^\s*#\s*?/ ||
3437 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003438 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003439 if ($s =~ s/^.*?\n//) {
3440 $cond_lines++;
3441 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003442 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003443 }
3444
3445 my (undef, $sindent) = line_stats("+" . $s);
3446 my $stat_real = raw_line($linenr, $cond_lines);
3447
3448 # Check if either of these lines are modified, else
3449 # this is not this patch's fault.
3450 if (!defined($stat_real) ||
3451 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3452 $check = 0;
3453 }
3454 if (defined($stat_real) && $cond_lines > 1) {
3455 $stat_real = "[...]\n$stat_real";
3456 }
3457
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003458 #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 -07003459
Joe Perches9f5af482015-09-09 15:37:30 -07003460 if ($check && $s ne '' &&
3461 (($sindent % 8) != 0 ||
3462 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003463 ($sindent == $indent &&
3464 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003465 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003466 WARN("SUSPECT_CODE_INDENT",
3467 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003468 }
3469 }
3470
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003471 # Track the 'values' across context and added lines.
3472 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003473 my ($curr_values, $curr_vars) =
3474 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003475 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003476 if ($dbg_values) {
3477 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003478 print "$linenr > .$outline\n";
3479 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003480 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003481 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003482 $prev_values = substr($curr_values, -1);
3483
Andy Whitcroft00df3442007-06-08 13:47:06 -07003484#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003485 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003486
Joe Perches11ca40a2016-12-12 16:46:31 -08003487# check for dereferences that span multiple lines
3488 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3489 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3490 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3491 my $ref = $1;
3492 $line =~ /^.\s*($Lval)/;
3493 $ref .= $1;
3494 $ref =~ s/\s//g;
3495 WARN("MULTILINE_DEREFERENCE",
3496 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3497 }
3498
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003499# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003500 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 -07003501 my $type = $1;
3502 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003503 $var = "" if (!defined $var);
3504 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003505 my $sign = $1;
3506 my $pointer = $2;
3507
3508 $pointer = "" if (!defined $pointer);
3509
3510 if (WARN("UNSPECIFIED_INT",
3511 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3512 $fix) {
3513 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003514 my $comp_pointer = $pointer;
3515 $comp_pointer =~ s/\s//g;
3516 $decl .= $comp_pointer;
3517 $decl = rtrim($decl) if ($var eq "");
3518 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003519 }
3520 }
3521 }
3522
Andy Whitcroft653d4872007-06-23 17:16:34 -07003523# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003524 if ($dbg_type) {
3525 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003526 ERROR("TEST_TYPE",
3527 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003528 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003529 ERROR("TEST_NOT_TYPE",
3530 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003531 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003532 next;
3533 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003534# TEST: allow direct testing of the attribute matcher.
3535 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003536 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003537 ERROR("TEST_ATTR",
3538 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003539 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003540 ERROR("TEST_NOT_ATTR",
3541 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003542 }
3543 next;
3544 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003545
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003546# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003547 if ($line =~ /^.\s*{/ &&
3548 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003549 if (ERROR("OPEN_BRACE",
3550 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003551 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3552 fix_delete_line($fixlinenr - 1, $prevrawline);
3553 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003554 my $fixedline = $prevrawline;
3555 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003556 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003557 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003558 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003559 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003560 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003561 }
3562
Andy Whitcroft653d4872007-06-23 17:16:34 -07003563#
3564# Checks which are anchored on the added line.
3565#
3566
3567# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003568 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003569 my $path = $1;
3570 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003571 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003572 "malformed #include filename\n" . $herecurr);
3573 }
3574 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3575 ERROR("UAPI_INCLUDE",
3576 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003577 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003578 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003579
3580# no C99 // comments
3581 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003582 if (ERROR("C99_COMMENTS",
3583 "do not use C99 // comments\n" . $herecurr) &&
3584 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003585 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003586 if ($line =~ /\/\/(.*)$/) {
3587 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003588 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003589 }
3590 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003591 }
3592 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003593 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003594 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003595
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003596# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3597# the whole statement.
3598#print "APW <$lines[$realline_next - 1]>\n";
3599 if (defined $realline_next &&
3600 exists $lines[$realline_next - 1] &&
3601 !defined $suppress_export{$realline_next} &&
3602 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3603 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003604 # Handle definitions which produce identifiers with
3605 # a prefix:
3606 # XXX(foo);
3607 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003608 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003609 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003610 $name =~ /^${Ident}_$2/) {
3611#print "FOO C name<$name>\n";
3612 $suppress_export{$realline_next} = 1;
3613
3614 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003615 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003616 ^.DEFINE_$Ident\(\Q$name\E\)|
3617 ^.DECLARE_$Ident\(\Q$name\E\)|
3618 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003619 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3620 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003621 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003622#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3623 $suppress_export{$realline_next} = 2;
3624 } else {
3625 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003626 }
3627 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003628 if (!defined $suppress_export{$linenr} &&
3629 $prevline =~ /^.\s*$/ &&
3630 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3631 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3632#print "FOO B <$lines[$linenr - 1]>\n";
3633 $suppress_export{$linenr} = 2;
3634 }
3635 if (defined $suppress_export{$linenr} &&
3636 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003637 WARN("EXPORT_SYMBOL",
3638 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003639 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003640
Joe Eloff5150bda2010-08-09 17:21:00 -07003641# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003642 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003643 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003644 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003645 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003646 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003647 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003648 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003649# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003650 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003651 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003652 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003653 $herecurr) &&
3654 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003655 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003656 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003657 }
3658
Joe Perches18130872014-08-06 16:11:22 -07003659# check for misordered declarations of char/short/int/long with signed/unsigned
3660 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3661 my $tmp = trim($1);
3662 WARN("MISORDERED_TYPE",
3663 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3664 }
3665
Joe Perchescb710ec2010-10-26 14:23:20 -07003666# check for static const char * arrays.
3667 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003668 WARN("STATIC_CONST_CHAR_ARRAY",
3669 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003670 $herecurr);
3671 }
3672
3673# check for static char foo[] = "bar" declarations.
3674 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003675 WARN("STATIC_CONST_CHAR_ARRAY",
3676 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003677 $herecurr);
3678 }
3679
Joe Perchesab7e23f2015-04-16 12:44:22 -07003680# check for const <foo> const where <foo> is not a pointer or array type
3681 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3682 my $found = $1;
3683 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3684 WARN("CONST_CONST",
3685 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3686 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3687 WARN("CONST_CONST",
3688 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3689 }
3690 }
3691
Joe Perches9b0fa602014-04-03 14:49:18 -07003692# check for non-global char *foo[] = {"bar", ...} declarations.
3693 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3694 WARN("STATIC_CONST_CHAR_ARRAY",
3695 "char * array declaration might be better as static const\n" .
3696 $herecurr);
3697 }
3698
Joe Perchesb598b672015-04-16 12:44:36 -07003699# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3700 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3701 my $array = $1;
3702 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3703 my $array_div = $1;
3704 if (WARN("ARRAY_SIZE",
3705 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3706 $fix) {
3707 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3708 }
3709 }
3710 }
3711
Joe Perchesb36190c2014-01-27 17:07:18 -08003712# check for function declarations without arguments like "int foo()"
3713 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3714 if (ERROR("FUNCTION_WITHOUT_ARGS",
3715 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3716 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003717 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003718 }
3719 }
3720
Andy Whitcroft653d4872007-06-23 17:16:34 -07003721# check for new typedefs, only function parameters and sparse annotations
3722# make sense.
3723 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003724 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003725 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003726 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003727 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003728 WARN("NEW_TYPEDEFS",
3729 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003730 }
3731
3732# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003733 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003734 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3735 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003736 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003737
Andy Whitcroft65863862009-01-06 14:41:21 -08003738 # Should start with a space.
3739 $to =~ s/^(\S)/ $1/;
3740 # Should not end with a space.
3741 $to =~ s/\s+$//;
3742 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003743 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003744 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003745
Joe Perches3705ce52013-07-03 15:05:31 -07003746## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003747 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003748 if (ERROR("POINTER_LOCATION",
3749 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3750 $fix) {
3751 my $sub_from = $ident;
3752 my $sub_to = $ident;
3753 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003754 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003755 s@\Q$sub_from\E@$sub_to@;
3756 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003757 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003758 }
3759 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3760 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003761 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003762
Andy Whitcroft65863862009-01-06 14:41:21 -08003763 # Should start with a space.
3764 $to =~ s/^(\S)/ $1/;
3765 # Should not end with a space.
3766 $to =~ s/\s+$//;
3767 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003768 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003769 }
3770 # Modifiers should have spaces.
3771 $to =~ s/(\b$Modifier$)/$1 /;
3772
Joe Perches3705ce52013-07-03 15:05:31 -07003773## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003774 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003775 if (ERROR("POINTER_LOCATION",
3776 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3777 $fix) {
3778
3779 my $sub_from = $match;
3780 my $sub_to = $match;
3781 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003782 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003783 s@\Q$sub_from\E@$sub_to@;
3784 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003785 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003786 }
3787
Joe Perches9d3e3c72015-09-09 15:37:27 -07003788# avoid BUG() or BUG_ON()
3789 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3790 my $msg_type = \&WARN;
3791 $msg_type = \&CHK if ($file);
3792 &{$msg_type}("AVOID_BUG",
3793 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3794 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003795
Joe Perches9d3e3c72015-09-09 15:37:27 -07003796# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003797 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003798 WARN("LINUX_VERSION_CODE",
3799 "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 -08003800 }
3801
Joe Perches17441222011-06-15 15:08:17 -07003802# check for uses of printk_ratelimit
3803 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003804 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003805 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003806 }
3807
Andy Whitcroft00df3442007-06-08 13:47:06 -07003808# printk should use KERN_* levels. Note that follow on printk's on the
3809# same line do not need a level, so we use the current block context
3810# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003811# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003812# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003813 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003814 my $ok = 0;
3815 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3816 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003817 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003818 # with "\n" ignore it, else it is to blame
3819 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3820 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3821 $ok = 1;
3822 }
3823 last;
3824 }
3825 }
3826 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3828 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003829 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003830 }
3831
Joe Perches243f3802012-05-31 16:26:09 -07003832 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3833 my $orig = $1;
3834 my $level = lc($orig);
3835 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003836 my $level2 = $level;
3837 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003838 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003839 "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 -07003840 }
3841
3842 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003843 if (WARN("PREFER_PR_LEVEL",
3844 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003846 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003847 s/\bpr_warning\b/pr_warn/;
3848 }
Joe Perches243f3802012-05-31 16:26:09 -07003849 }
3850
Joe Perchesdc139312013-02-21 16:44:13 -08003851 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3852 my $orig = $1;
3853 my $level = lc($orig);
3854 $level = "warn" if ($level eq "warning");
3855 $level = "dbg" if ($level eq "debug");
3856 WARN("PREFER_DEV_LEVEL",
3857 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3858 }
3859
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003860# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3861# number of false positives, but assembly files are not checked, so at
3862# least the arch entry code will not trigger this warning.
3863 if ($line =~ /\bENOSYS\b/) {
3864 WARN("ENOSYS",
3865 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3866 }
3867
Andy Whitcroft653d4872007-06-23 17:16:34 -07003868# function brace can't be on same line, except for #defines of do while,
3869# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003870 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003871 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003872 if (ERROR("OPEN_BRACE",
3873 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3874 $fix) {
3875 fix_delete_line($fixlinenr, $rawline);
3876 my $fixed_line = $rawline;
3877 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3878 my $line1 = $1;
3879 my $line2 = $2;
3880 fix_insert_line($fixlinenr, ltrim($line1));
3881 fix_insert_line($fixlinenr, "\+{");
3882 if ($line2 !~ /^\s*$/) {
3883 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3884 }
3885 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003886 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003887
Andy Whitcroft8905a672007-11-28 16:21:06 -08003888# open braces for enum, union and struct go on the same line.
3889 if ($line =~ /^.\s*{/ &&
3890 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003891 if (ERROR("OPEN_BRACE",
3892 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3893 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3894 fix_delete_line($fixlinenr - 1, $prevrawline);
3895 fix_delete_line($fixlinenr, $rawline);
3896 my $fixedline = rtrim($prevrawline) . " {";
3897 fix_insert_line($fixlinenr, $fixedline);
3898 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003899 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07003900 if ($fixedline !~ /^\+\s*$/) {
3901 fix_insert_line($fixlinenr, $fixedline);
3902 }
3903 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003904 }
3905
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003906# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003907 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3908 if (WARN("SPACING",
3909 "missing space after $1 definition\n" . $herecurr) &&
3910 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003911 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003912 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3913 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003914 }
3915
Joe Perches31070b52014-01-23 15:54:49 -08003916# Function pointer declarations
3917# check spacing between type, funcptr, and args
3918# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003919 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003920 my $declare = $1;
3921 my $pre_pointer_space = $2;
3922 my $post_pointer_space = $3;
3923 my $funcname = $4;
3924 my $post_funcname_space = $5;
3925 my $pre_args_space = $6;
3926
Joe Perches91f72e92014-04-03 14:49:12 -07003927# the $Declare variable will capture all spaces after the type
3928# so check it for a missing trailing missing space but pointer return types
3929# don't need a space so don't warn for those.
3930 my $post_declare_space = "";
3931 if ($declare =~ /(\s+)$/) {
3932 $post_declare_space = $1;
3933 $declare = rtrim($declare);
3934 }
3935 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003936 WARN("SPACING",
3937 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003938 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003939 }
3940
3941# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003942# This test is not currently implemented because these declarations are
3943# equivalent to
3944# int foo(int bar, ...)
3945# and this is form shouldn't/doesn't generate a checkpatch warning.
3946#
3947# elsif ($declare =~ /\s{2,}$/) {
3948# WARN("SPACING",
3949# "Multiple spaces after return type\n" . $herecurr);
3950# }
Joe Perches31070b52014-01-23 15:54:49 -08003951
3952# unnecessary space "type ( *funcptr)(args...)"
3953 if (defined $pre_pointer_space &&
3954 $pre_pointer_space =~ /^\s/) {
3955 WARN("SPACING",
3956 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3957 }
3958
3959# unnecessary space "type (* funcptr)(args...)"
3960 if (defined $post_pointer_space &&
3961 $post_pointer_space =~ /^\s/) {
3962 WARN("SPACING",
3963 "Unnecessary space before function pointer name\n" . $herecurr);
3964 }
3965
3966# unnecessary space "type (*funcptr )(args...)"
3967 if (defined $post_funcname_space &&
3968 $post_funcname_space =~ /^\s/) {
3969 WARN("SPACING",
3970 "Unnecessary space after function pointer name\n" . $herecurr);
3971 }
3972
3973# unnecessary space "type (*funcptr) (args...)"
3974 if (defined $pre_args_space &&
3975 $pre_args_space =~ /^\s/) {
3976 WARN("SPACING",
3977 "Unnecessary space before function pointer arguments\n" . $herecurr);
3978 }
3979
3980 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003981 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003982 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003983 }
3984 }
3985
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003986# check for spacing round square brackets; allowed:
3987# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003988# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3989# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003990 while ($line =~ /(.*?\s)\[/g) {
3991 my ($where, $prefix) = ($-[1], $1);
3992 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003993 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003994 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003995 if (ERROR("BRACKET_SPACE",
3996 "space prohibited before open square bracket '['\n" . $herecurr) &&
3997 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003998 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003999 s/^(\+.*?)\s+\[/$1\[/;
4000 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004001 }
4002 }
4003
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004004# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004005 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004006 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004007 my $ctx_before = substr($line, 0, $-[1]);
4008 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004009
4010 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004011 if ($name =~ /^(?:
4012 if|for|while|switch|return|case|
4013 volatile|__volatile__|
4014 __attribute__|format|__extension__|
4015 asm|__asm__)$/x)
4016 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004017 # cpp #define statements have non-optional spaces, ie
4018 # if there is a space between the name and the open
4019 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004020 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004021
4022 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004023 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004024
4025 # If this whole things ends with a type its most
4026 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004027 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004028
4029 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004030 if (WARN("SPACING",
4031 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4032 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004033 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004034 s/\b$name\s+\(/$name\(/;
4035 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004036 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004037 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004038
Andy Whitcroft653d4872007-06-23 17:16:34 -07004039# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004040 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004041 my $fixed_line = "";
4042 my $line_fixed = 0;
4043
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004044 my $ops = qr{
4045 <<=|>>=|<=|>=|==|!=|
4046 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4047 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004048 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004049 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004050 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004051 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004052
4053## print("element count: <" . $#elements . ">\n");
4054## foreach my $el (@elements) {
4055## print("el: <$el>\n");
4056## }
4057
4058 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004059 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004060
Joe Perches3705ce52013-07-03 15:05:31 -07004061 foreach my $el (@elements) {
4062 push(@fix_elements, substr($rawline, $off, length($el)));
4063 $off += length($el);
4064 }
4065
4066 $off = 0;
4067
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004068 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004069 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004070
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004071 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004072
4073 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4074
4075## print("n: <$n> good: <$good>\n");
4076
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004077 $off += length($elements[$n]);
4078
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004079 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004080 my $ca = substr($opline, 0, $off);
4081 my $cc = '';
4082 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4083 $cc = substr($opline, $off + length($elements[$n + 1]));
4084 }
4085 my $cb = "$ca$;$cc";
4086
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004087 my $a = '';
4088 $a = 'V' if ($elements[$n] ne '');
4089 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004090 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004091 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4092 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004093 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004094
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004095 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004096
4097 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004098 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004099 $c = 'V' if ($elements[$n + 2] ne '');
4100 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004101 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004102 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4103 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004104 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004105 } else {
4106 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004107 }
4108
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004109 my $ctx = "${a}x${c}";
4110
4111 my $at = "(ctx:$ctx)";
4112
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004113 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004114 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004115
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004116 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004117 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004118
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004119 # Get the full operator variant.
4120 my $opv = $op . substr($curr_vars, $off, 1);
4121
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004122 # Ignore operators passed as parameters.
4123 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004124 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004125
Andy Whitcroftcf655042008-03-04 14:28:20 -08004126# # Ignore comments
4127# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004128
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004129 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004130 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004131 if ($ctx !~ /.x[WEBC]/ &&
4132 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004133 if (ERROR("SPACING",
4134 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004135 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004136 $line_fixed = 1;
4137 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004138 }
4139
4140 # // is a comment
4141 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004142
Joe Perchesb00e4812014-04-03 14:49:33 -07004143 # : when part of a bitfield
4144 } elsif ($opv eq ':B') {
4145 # skip the bitfield test for now
4146
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004147 # No spaces for:
4148 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004149 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004150 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004151 if (ERROR("SPACING",
4152 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004153 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004154 if (defined $fix_elements[$n + 2]) {
4155 $fix_elements[$n + 2] =~ s/^\s+//;
4156 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004157 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004158 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004159 }
4160
Joe Perches23810972014-12-10 15:51:32 -08004161 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004162 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004163 my $rtrim_before = 0;
4164 my $space_after = 0;
4165 if ($ctx =~ /Wx./) {
4166 if (ERROR("SPACING",
4167 "space prohibited before that '$op' $at\n" . $hereptr)) {
4168 $line_fixed = 1;
4169 $rtrim_before = 1;
4170 }
4171 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004172 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004173 if (ERROR("SPACING",
4174 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004175 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004176 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004177 $space_after = 1;
4178 }
4179 }
4180 if ($rtrim_before || $space_after) {
4181 if ($rtrim_before) {
4182 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4183 } else {
4184 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4185 }
4186 if ($space_after) {
4187 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004188 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004189 }
4190
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004191 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004192 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004193 #warn "'*' is part of type\n";
4194
4195 # unary operators should have a space before and
4196 # none after. May be left adjacent to another
4197 # unary operator, or a cast
4198 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004199 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004200 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004201 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004202 if (ERROR("SPACING",
4203 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004204 if ($n != $last_after + 2) {
4205 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4206 $line_fixed = 1;
4207 }
Joe Perches3705ce52013-07-03 15:05:31 -07004208 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004209 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004210 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004211 # A unary '*' may be const
4212
4213 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004214 if (ERROR("SPACING",
4215 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004216 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004217 if (defined $fix_elements[$n + 2]) {
4218 $fix_elements[$n + 2] =~ s/^\s+//;
4219 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004220 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004221 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004222 }
4223
4224 # unary ++ and unary -- are allowed no space on one side.
4225 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004226 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004227 if (ERROR("SPACING",
4228 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004229 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004230 $line_fixed = 1;
4231 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004232 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004233 if ($ctx =~ /Wx[BE]/ ||
4234 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004235 if (ERROR("SPACING",
4236 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004237 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004238 $line_fixed = 1;
4239 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004240 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004241 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004242 if (ERROR("SPACING",
4243 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004244 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004245 if (defined $fix_elements[$n + 2]) {
4246 $fix_elements[$n + 2] =~ s/^\s+//;
4247 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004248 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004249 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004250 }
4251
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004252 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004253 } elsif ($op eq '<<' or $op eq '>>' or
4254 $op eq '&' or $op eq '^' or $op eq '|' or
4255 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004256 $op eq '*' or $op eq '/' or
4257 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004258 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004259 if ($check) {
4260 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4261 if (CHK("SPACING",
4262 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4263 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4264 $fix_elements[$n + 2] =~ s/^\s+//;
4265 $line_fixed = 1;
4266 }
4267 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4268 if (CHK("SPACING",
4269 "space preferred before that '$op' $at\n" . $hereptr)) {
4270 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4271 $line_fixed = 1;
4272 }
4273 }
4274 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004275 if (ERROR("SPACING",
4276 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004277 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4278 if (defined $fix_elements[$n + 2]) {
4279 $fix_elements[$n + 2] =~ s/^\s+//;
4280 }
Joe Perches3705ce52013-07-03 15:05:31 -07004281 $line_fixed = 1;
4282 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004283 }
4284
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004285 # A colon needs no spaces before when it is
4286 # terminating a case value or a label.
4287 } elsif ($opv eq ':C' || $opv eq ':L') {
4288 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004289 if (ERROR("SPACING",
4290 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004291 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004292 $line_fixed = 1;
4293 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004294 }
4295
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004296 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004297 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004298 my $ok = 0;
4299
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004300 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004301 if (($op eq '<' &&
4302 $cc =~ /^\S+\@\S+>/) ||
4303 ($op eq '>' &&
4304 $ca =~ /<\S+\@\S+$/))
4305 {
4306 $ok = 1;
4307 }
4308
Joe Perchese0df7e12015-04-16 12:44:53 -07004309 # for asm volatile statements
4310 # ignore a colon with another
4311 # colon immediately before or after
4312 if (($op eq ':') &&
4313 ($ca =~ /:$/ || $cc =~ /^:/)) {
4314 $ok = 1;
4315 }
4316
Joe Perches84731622013-11-12 15:10:05 -08004317 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004318 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004319 my $msg_type = \&ERROR;
4320 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4321
4322 if (&{$msg_type}("SPACING",
4323 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004324 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4325 if (defined $fix_elements[$n + 2]) {
4326 $fix_elements[$n + 2] =~ s/^\s+//;
4327 }
Joe Perches3705ce52013-07-03 15:05:31 -07004328 $line_fixed = 1;
4329 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004330 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004331 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004332 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004333
4334## print("n: <$n> GOOD: <$good>\n");
4335
4336 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004337 }
Joe Perches3705ce52013-07-03 15:05:31 -07004338
4339 if (($#elements % 2) == 0) {
4340 $fixed_line = $fixed_line . $fix_elements[$#elements];
4341 }
4342
Joe Perches194f66f2014-08-06 16:11:03 -07004343 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4344 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004345 }
4346
4347
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004348 }
4349
Joe Perches786b6322013-07-03 15:05:32 -07004350# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004351 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004352 if (WARN("SPACING",
4353 "space prohibited before semicolon\n" . $herecurr) &&
4354 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004355 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004356 s/^(\+.*\S)\s+;/$1;/;
4357 }
4358 }
4359
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004360# check for multiple assignments
4361 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004362 CHK("MULTIPLE_ASSIGNMENTS",
4363 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004364 }
4365
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004366## # check for multiple declarations, allowing for a function declaration
4367## # continuation.
4368## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4369## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4370##
4371## # Remove any bracketed sections to ensure we do not
4372## # falsly report the parameters of functions.
4373## my $ln = $line;
4374## while ($ln =~ s/\([^\(\)]*\)//g) {
4375## }
4376## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004377## WARN("MULTIPLE_DECLARATION",
4378## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004379## }
4380## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004381
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004382#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004383 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004384 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004385 if (ERROR("SPACING",
4386 "space required before the open brace '{'\n" . $herecurr) &&
4387 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004388 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004389 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004390 }
4391
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004392## # check for blank lines before declarations
4393## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4394## $prevrawline =~ /^.\s*$/) {
4395## WARN("SPACING",
4396## "No blank lines before declarations\n" . $hereprev);
4397## }
4398##
4399
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004400# closing brace should have a space following it when it has anything
4401# on the line
4402 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004403 if (ERROR("SPACING",
4404 "space required after that close brace '}'\n" . $herecurr) &&
4405 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004406 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004407 s/}((?!(?:,|;|\)))\S)/} $1/;
4408 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004409 }
4410
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004411# check spacing on square brackets
4412 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004413 if (ERROR("SPACING",
4414 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4415 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004416 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004417 s/\[\s+/\[/;
4418 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004419 }
4420 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004421 if (ERROR("SPACING",
4422 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4423 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004424 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004425 s/\s+\]/\]/;
4426 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004427 }
4428
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004429# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004430 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4431 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004432 if (ERROR("SPACING",
4433 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4434 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004435 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004436 s/\(\s+/\(/;
4437 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004438 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004439 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004440 $line !~ /for\s*\(.*;\s+\)/ &&
4441 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004442 if (ERROR("SPACING",
4443 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4444 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004445 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004446 s/\s+\)/\)/;
4447 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004448 }
4449
Joe Perchese2826fd2014-08-06 16:10:48 -07004450# check unnecessary parentheses around addressof/dereference single $Lvals
4451# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4452
4453 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004454 my $var = $1;
4455 if (CHK("UNNECESSARY_PARENTHESES",
4456 "Unnecessary parentheses around $var\n" . $herecurr) &&
4457 $fix) {
4458 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4459 }
4460 }
4461
4462# check for unnecessary parentheses around function pointer uses
4463# ie: (foo->bar)(); should be foo->bar();
4464# but not "if (foo->bar) (" to avoid some false positives
4465 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4466 my $var = $2;
4467 if (CHK("UNNECESSARY_PARENTHESES",
4468 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4469 $fix) {
4470 my $var2 = deparenthesize($var);
4471 $var2 =~ s/\s//g;
4472 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4473 }
4474 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004475
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004476#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004477 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004478 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004479 if (WARN("INDENTED_LABEL",
4480 "labels should not be indented\n" . $herecurr) &&
4481 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004482 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004483 s/^(.)\s+/$1/;
4484 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004485 }
4486
Joe Perches5b9553a2014-04-03 14:49:21 -07004487# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004488 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004489 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004490 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004491 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4492 my $value = $1;
4493 $value = deparenthesize($value);
4494 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4495 ERROR("RETURN_PARENTHESES",
4496 "return is not a function, parentheses are not required\n" . $herecurr);
4497 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004498 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004499 ERROR("SPACING",
4500 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004501 }
4502 }
Joe Perches507e5142013-11-12 15:10:13 -08004503
Joe Perchesb43ae212014-06-23 13:22:07 -07004504# unnecessary return in a void function
4505# at end-of-function, with the previous line a single leading tab, then return;
4506# and the line before that not a goto label target like "out:"
4507 if ($sline =~ /^[ \+]}\s*$/ &&
4508 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4509 $linenr >= 3 &&
4510 $lines[$linenr - 3] =~ /^[ +]/ &&
4511 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004512 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004513 "void function return statements are not generally useful\n" . $hereprev);
4514 }
Joe Perches9819cf22014-06-04 16:12:09 -07004515
Joe Perches189248d2014-01-23 15:54:47 -08004516# if statements using unnecessary parentheses - ie: if ((foo == bar))
4517 if ($^V && $^V ge 5.10.0 &&
4518 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4519 my $openparens = $1;
4520 my $count = $openparens =~ tr@\(@\(@;
4521 my $msg = "";
4522 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4523 my $comp = $4; #Not $1 because of $LvalOrFunc
4524 $msg = " - maybe == should be = ?" if ($comp eq "==");
4525 WARN("UNNECESSARY_PARENTHESES",
4526 "Unnecessary parentheses$msg\n" . $herecurr);
4527 }
4528 }
4529
Joe Perchesc5595fa2015-09-09 15:37:58 -07004530# comparisons with a constant or upper case identifier on the left
4531# avoid cases like "foo + BAR < baz"
4532# only fix matches surrounded by parentheses to avoid incorrect
4533# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4534 if ($^V && $^V ge 5.10.0 &&
4535 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4536 my $lead = $1;
4537 my $const = $2;
4538 my $comp = $3;
4539 my $to = $4;
4540 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004541 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004542 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4543 WARN("CONSTANT_COMPARISON",
4544 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4545 $fix) {
4546 if ($comp eq "<") {
4547 $newcomp = ">";
4548 } elsif ($comp eq "<=") {
4549 $newcomp = ">=";
4550 } elsif ($comp eq ">") {
4551 $newcomp = "<";
4552 } elsif ($comp eq ">=") {
4553 $newcomp = "<=";
4554 }
4555 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4556 }
4557 }
4558
Joe Perchesf34e4a42015-04-16 12:44:19 -07004559# Return of what appears to be an errno should normally be negative
4560 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004561 my $name = $1;
4562 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004563 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004564 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004565 }
4566 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004567
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004568# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004569 if ($line =~ /\b(if|while|for|switch)\(/) {
4570 if (ERROR("SPACING",
4571 "space required before the open parenthesis '('\n" . $herecurr) &&
4572 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004573 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004574 s/\b(if|while|for|switch)\(/$1 \(/;
4575 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004576 }
4577
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004578# Check for illegal assignment in if conditional -- and check for trailing
4579# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004580 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004581 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4582 ctx_statement_block($linenr, $realcnt, 0)
4583 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004584 my ($stat_next) = ctx_statement_block($line_nr_next,
4585 $remain_next, $off_next);
4586 $stat_next =~ s/\n./\n /g;
4587 ##print "stat<$stat> stat_next<$stat_next>\n";
4588
4589 if ($stat_next =~ /^\s*while\b/) {
4590 # If the statement carries leading newlines,
4591 # then count those as offsets.
4592 my ($whitespace) =
4593 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4594 my $offset =
4595 statement_rawlines($whitespace) - 1;
4596
4597 $suppress_whiletrailers{$line_nr_next +
4598 $offset} = 1;
4599 }
4600 }
4601 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004602 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004603 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004604 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004605
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004606 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004607 ERROR("ASSIGN_IN_IF",
4608 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004609 }
4610
4611 # Find out what is on the end of the line after the
4612 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004613 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004614 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004615 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004616 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4617 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004618 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004619 # Find out how long the conditional actually is.
4620 my @newlines = ($c =~ /\n/gs);
4621 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004622 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004623
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004624 $stat_real = raw_line($linenr, $cond_lines)
4625 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004626 if (defined($stat_real) && $cond_lines > 1) {
4627 $stat_real = "[...]\n$stat_real";
4628 }
4629
Joe Perches000d1cc12011-07-25 17:13:25 -07004630 ERROR("TRAILING_STATEMENTS",
4631 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004632 }
4633 }
4634
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004635# Check for bitwise tests written as boolean
4636 if ($line =~ /
4637 (?:
4638 (?:\[|\(|\&\&|\|\|)
4639 \s*0[xX][0-9]+\s*
4640 (?:\&\&|\|\|)
4641 |
4642 (?:\&\&|\|\|)
4643 \s*0[xX][0-9]+\s*
4644 (?:\&\&|\|\||\)|\])
4645 )/x)
4646 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004647 WARN("HEXADECIMAL_BOOLEAN_TEST",
4648 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004649 }
4650
Andy Whitcroft8905a672007-11-28 16:21:06 -08004651# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004652 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4653 my $s = $1;
4654 $s =~ s/$;//g; # Remove any comments
4655 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004656 ERROR("TRAILING_STATEMENTS",
4657 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004658 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004659 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004660# if should not continue a brace
4661 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004662 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004663 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004664 $herecurr);
4665 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004666# case and default should not have general statements after them
4667 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4668 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004669 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004670 \s*return\s+
4671 )/xg)
4672 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004673 ERROR("TRAILING_STATEMENTS",
4674 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004675 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004676
4677 # Check for }<nl>else {, these must be at the same
4678 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004679 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4680 $previndent == $indent) {
4681 if (ERROR("ELSE_AFTER_BRACE",
4682 "else should follow close brace '}'\n" . $hereprev) &&
4683 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4684 fix_delete_line($fixlinenr - 1, $prevrawline);
4685 fix_delete_line($fixlinenr, $rawline);
4686 my $fixedline = $prevrawline;
4687 $fixedline =~ s/}\s*$//;
4688 if ($fixedline !~ /^\+\s*$/) {
4689 fix_insert_line($fixlinenr, $fixedline);
4690 }
4691 $fixedline = $rawline;
4692 $fixedline =~ s/^(.\s*)else/$1} else/;
4693 fix_insert_line($fixlinenr, $fixedline);
4694 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004695 }
4696
Joe Perches8b8856f2014-08-06 16:11:14 -07004697 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4698 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004699 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4700
4701 # Find out what is on the end of the line after the
4702 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004703 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004704 $s =~ s/\n.*//g;
4705
4706 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004707 if (ERROR("WHILE_AFTER_BRACE",
4708 "while should follow close brace '}'\n" . $hereprev) &&
4709 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4710 fix_delete_line($fixlinenr - 1, $prevrawline);
4711 fix_delete_line($fixlinenr, $rawline);
4712 my $fixedline = $prevrawline;
4713 my $trailing = $rawline;
4714 $trailing =~ s/^\+//;
4715 $trailing = trim($trailing);
4716 $fixedline =~ s/}\s*$/} $trailing/;
4717 fix_insert_line($fixlinenr, $fixedline);
4718 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004719 }
4720 }
4721
Joe Perches95e2c602013-07-03 15:05:20 -07004722#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004723 while ($line =~ m{($Constant|$Lval)}g) {
4724 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004725
4726#gcc binary extension
4727 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004728 if (WARN("GCC_BINARY_CONSTANT",
4729 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4730 $fix) {
4731 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004732 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004733 s/\b$var\b/$hexval/;
4734 }
Joe Perches95e2c602013-07-03 15:05:20 -07004735 }
4736
4737#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004738 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004739 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004740#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004741 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004742#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004743 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4744#Ignore some three character SI units explicitly, like MiB and KHz
4745 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004746 while ($var =~ m{($Ident)}g) {
4747 my $word = $1;
4748 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004749 if ($check) {
4750 seed_camelcase_includes();
4751 if (!$file && !$camelcase_file_seeded) {
4752 seed_camelcase_file($realfile);
4753 $camelcase_file_seeded = 1;
4754 }
4755 }
Joe Perches7e781f62013-09-11 14:23:55 -07004756 if (!defined $camelcase{$word}) {
4757 $camelcase{$word} = 1;
4758 CHK("CAMELCASE",
4759 "Avoid CamelCase: <$word>\n" . $herecurr);
4760 }
Joe Perches34456862013-07-03 15:05:34 -07004761 }
Joe Perches323c1262012-12-17 16:02:07 -08004762 }
4763 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004764
4765#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004766 if ($line =~ /\#\s*define.*\\\s+$/) {
4767 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4768 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4769 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004770 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004771 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004772 }
4773
Fabian Frederick0e212e02015-04-16 12:44:25 -07004774# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4775# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004776 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004777 my $file = "$1.h";
4778 my $checkfile = "include/linux/$file";
4779 if (-f "$root/$checkfile" &&
4780 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004781 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004782 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004783 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4784 if ($asminclude > 0) {
4785 if ($realfile =~ m{^arch/}) {
4786 CHK("ARCH_INCLUDE_LINUX",
4787 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4788 } else {
4789 WARN("INCLUDE_LINUX",
4790 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4791 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004792 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004793 }
4794 }
4795
Andy Whitcroft653d4872007-06-23 17:16:34 -07004796# multi-statement macros should be enclosed in a do while loop, grab the
4797# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004798# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004799 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4800 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004801 my $ln = $linenr;
4802 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004803 my ($off, $dstat, $dcond, $rest);
4804 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004805 my $has_flow_statement = 0;
4806 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004807 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004808 ctx_statement_block($linenr, $realcnt, 0);
4809 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004810 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004811 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004812
Joe Perches08a28432014-10-13 15:51:55 -07004813 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004814 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004815
Joe Perchesf59b64b2016-10-11 13:52:08 -07004816 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4817 my $define_args = $1;
4818 my $define_stmt = $dstat;
4819 my @def_args = ();
4820
4821 if (defined $define_args && $define_args ne "") {
4822 $define_args = substr($define_args, 1, length($define_args) - 2);
4823 $define_args =~ s/\s*//g;
4824 @def_args = split(",", $define_args);
4825 }
4826
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004827 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004828 $dstat =~ s/\\\n.//g;
4829 $dstat =~ s/^\s*//s;
4830 $dstat =~ s/\s*$//s;
4831
4832 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004833 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4834 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004835 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004836 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004837 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004838
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004839 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004840 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4841 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004842 {
4843 }
4844
Joe Perches42e15292016-03-15 14:58:01 -07004845 # Make asm volatile uses seem like a generic function
4846 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4847
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004848 my $exceptions = qr{
4849 $Declare|
4850 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004851 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004852 DECLARE_PER_CPU|
4853 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004854 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004855 union|
4856 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004857 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004858 ^\"|\"$|
4859 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004860 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004861 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004862
4863 $ctx =~ s/\n*$//;
4864 my $herectx = $here . "\n";
4865 my $stmt_cnt = statement_rawlines($ctx);
4866
4867 for (my $n = 0; $n < $stmt_cnt; $n++) {
4868 $herectx .= raw_line($linenr, $n) . "\n";
4869 }
4870
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004871 if ($dstat ne '' &&
4872 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4873 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004874 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004875 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004876 $dstat !~ /$exceptions/ &&
4877 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004878 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004879 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004880 $dstat !~ /^for\s*$Constant$/ && # for (...)
4881 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4882 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004883 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004884 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004885 {
Joe Perchese7955562017-05-08 15:55:48 -07004886 if ($dstat =~ /^\s*if\b/) {
4887 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4888 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4889 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004890 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4891 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4892 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004893 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004894 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004895 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07004896
4897 }
Joe Perches52076492016-10-11 13:52:14 -07004898
4899 # Make $define_stmt single line, comment-free, etc
4900 my @stmt_array = split('\n', $define_stmt);
4901 my $first = 1;
4902 $define_stmt = "";
4903 foreach my $l (@stmt_array) {
4904 $l =~ s/\\$//;
4905 if ($first) {
4906 $define_stmt = $l;
4907 $first = 0;
4908 } elsif ($l =~ /^[\+ ]/) {
4909 $define_stmt .= substr($l, 1);
4910 }
4911 }
4912 $define_stmt =~ s/$;//g;
4913 $define_stmt =~ s/\s+/ /g;
4914 $define_stmt = trim($define_stmt);
4915
Joe Perchesf59b64b2016-10-11 13:52:08 -07004916# check if any macro arguments are reused (ignore '...' and 'type')
4917 foreach my $arg (@def_args) {
4918 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07004919 next if ($arg =~ /^type$/i);
Joe Perchesf59b64b2016-10-11 13:52:08 -07004920 my $tmp = $define_stmt;
4921 $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 -07004922 $tmp =~ s/\#+\s*$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004923 $tmp =~ s/\b$arg\s*\#\#//g;
4924 my $use_cnt = $tmp =~ s/\b$arg\b//g;
4925 if ($use_cnt > 1) {
4926 CHK("MACRO_ARG_REUSE",
4927 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07004928 }
4929# check if any macro arguments may have other precedence issues
4930 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4931 ((defined($1) && $1 ne ',') ||
4932 (defined($2) && $2 ne ','))) {
4933 CHK("MACRO_ARG_PRECEDENCE",
4934 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07004935 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004936 }
Joe Perches5023d342012-12-17 16:01:47 -08004937
Joe Perches08a28432014-10-13 15:51:55 -07004938# check for macros with flow control, but without ## concatenation
4939# ## concatenation is commonly a macro that defines a function so ignore those
4940 if ($has_flow_statement && !$has_arg_concat) {
4941 my $herectx = $here . "\n";
4942 my $cnt = statement_rawlines($ctx);
4943
4944 for (my $n = 0; $n < $cnt; $n++) {
4945 $herectx .= raw_line($linenr, $n) . "\n";
4946 }
4947 WARN("MACRO_WITH_FLOW_CONTROL",
4948 "Macros with flow control statements should be avoided\n" . "$herectx");
4949 }
4950
Joe Perches481eb482012-12-17 16:01:56 -08004951# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004952
4953 } else {
4954 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004955 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4956 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004957 $line =~ /^\+.*\\$/) {
4958 WARN("LINE_CONTINUATIONS",
4959 "Avoid unnecessary line continuations\n" . $herecurr);
4960 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004961 }
4962
Joe Perchesb13edf72012-07-30 14:41:24 -07004963# do {} while (0) macro tests:
4964# single-statement macros do not need to be enclosed in do while (0) loop,
4965# macro should not end with a semicolon
4966 if ($^V && $^V ge 5.10.0 &&
4967 $realfile !~ m@/vmlinux.lds.h$@ &&
4968 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4969 my $ln = $linenr;
4970 my $cnt = $realcnt;
4971 my ($off, $dstat, $dcond, $rest);
4972 my $ctx = '';
4973 ($dstat, $dcond, $ln, $cnt, $off) =
4974 ctx_statement_block($linenr, $realcnt, 0);
4975 $ctx = $dstat;
4976
4977 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004978 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004979
4980 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4981 my $stmts = $2;
4982 my $semis = $3;
4983
4984 $ctx =~ s/\n*$//;
4985 my $cnt = statement_rawlines($ctx);
4986 my $herectx = $here . "\n";
4987
4988 for (my $n = 0; $n < $cnt; $n++) {
4989 $herectx .= raw_line($linenr, $n) . "\n";
4990 }
4991
Joe Perchesac8e97f2012-08-21 16:15:53 -07004992 if (($stmts =~ tr/;/;/) == 1 &&
4993 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004994 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4995 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4996 }
4997 if (defined $semis && $semis ne "") {
4998 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4999 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5000 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005001 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5002 $ctx =~ s/\n*$//;
5003 my $cnt = statement_rawlines($ctx);
5004 my $herectx = $here . "\n";
5005
5006 for (my $n = 0; $n < $cnt; $n++) {
5007 $herectx .= raw_line($linenr, $n) . "\n";
5008 }
5009
5010 WARN("TRAILING_SEMICOLON",
5011 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005012 }
5013 }
5014
Mike Frysinger080ba922009-01-06 14:41:25 -08005015# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5016# all assignments may have only one of the following with an assignment:
5017# .
5018# ALIGN(...)
5019# VMLINUX_SYMBOL(...)
5020 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005021 WARN("MISSING_VMLINUX_SYMBOL",
5022 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005023 }
5024
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005025# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005026 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5027 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005028 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005029 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005030 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5031 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005032 my @allowed = ();
5033 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005034 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005035 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005036 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005037 for my $chunk (@chunks) {
5038 my ($cond, $block) = @{$chunk};
5039
Andy Whitcroft773647a2008-03-28 14:15:58 -07005040 # If the condition carries leading newlines, then count those as offsets.
5041 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5042 my $offset = statement_rawlines($whitespace) - 1;
5043
Joe Perchesaad4f612012-03-23 15:02:19 -07005044 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005045 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5046
5047 # We have looked at and allowed this specific line.
5048 $suppress_ifbraces{$ln + $offset} = 1;
5049
5050 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005051 $ln += statement_rawlines($block) - 1;
5052
Andy Whitcroft773647a2008-03-28 14:15:58 -07005053 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005054
5055 $seen++ if ($block =~ /^\s*{/);
5056
Joe Perchesaad4f612012-03-23 15:02:19 -07005057 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005058 if (statement_lines($cond) > 1) {
5059 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005060 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005061 }
5062 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005063 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005064 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005065 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005066 if (statement_block_size($block) > 1) {
5067 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005068 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005069 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005070 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005071 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005072 if ($seen) {
5073 my $sum_allowed = 0;
5074 foreach (@allowed) {
5075 $sum_allowed += $_;
5076 }
5077 if ($sum_allowed == 0) {
5078 WARN("BRACES",
5079 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5080 } elsif ($sum_allowed != $allow &&
5081 $seen != $allow) {
5082 CHK("BRACES",
5083 "braces {} should be used on all arms of this statement\n" . $herectx);
5084 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005085 }
5086 }
5087 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005088 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005089 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005090 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005091
Andy Whitcroftcf655042008-03-04 14:28:20 -08005092 # Check the pre-context.
5093 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5094 #print "APW: ALLOWED: pre<$1>\n";
5095 $allowed = 1;
5096 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005097
5098 my ($level, $endln, @chunks) =
5099 ctx_statement_full($linenr, $realcnt, $-[0]);
5100
Andy Whitcroftcf655042008-03-04 14:28:20 -08005101 # Check the condition.
5102 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005103 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005104 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005105 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005106 }
5107 if (statement_lines($cond) > 1) {
5108 #print "APW: ALLOWED: cond<$cond>\n";
5109 $allowed = 1;
5110 }
5111 if ($block =~/\b(?:if|for|while)\b/) {
5112 #print "APW: ALLOWED: block<$block>\n";
5113 $allowed = 1;
5114 }
5115 if (statement_block_size($block) > 1) {
5116 #print "APW: ALLOWED: lines block<$block>\n";
5117 $allowed = 1;
5118 }
5119 # Check the post-context.
5120 if (defined $chunks[1]) {
5121 my ($cond, $block) = @{$chunks[1]};
5122 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005123 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005124 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005125 if ($block =~ /^\s*\{/) {
5126 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5127 $allowed = 1;
5128 }
5129 }
5130 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005131 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005132 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005133
Andy Whitcroftf0556632008-10-15 22:02:23 -07005134 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005135 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005136 }
5137
Joe Perches000d1cc12011-07-25 17:13:25 -07005138 WARN("BRACES",
5139 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005140 }
5141 }
5142
Joe Perchese4c5bab2017-02-24 15:01:41 -08005143# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005144 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5145 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005146 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5147 }
5148
Joe Perches0979ae62012-12-17 16:01:59 -08005149# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005150 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005151 if (CHK("BRACES",
5152 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5153 $fix && $prevrawline =~ /^\+/) {
5154 fix_delete_line($fixlinenr - 1, $prevrawline);
5155 }
Joe Perches0979ae62012-12-17 16:01:59 -08005156 }
Joe Perches77b9a532013-07-03 15:05:29 -07005157 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005158 if (CHK("BRACES",
5159 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5160 $fix) {
5161 fix_delete_line($fixlinenr, $rawline);
5162 }
Joe Perches0979ae62012-12-17 16:01:59 -08005163 }
5164
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005165# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005166 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5167 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005168 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005169 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005170 }
5171
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005172# Check for user-visible strings broken across lines, which breaks the ability
5173# to grep for the string. Make exceptions when the previous string ends in a
5174# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5175# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005176 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005177 $prevline =~ /"\s*$/ &&
5178 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5179 if (WARN("SPLIT_STRING",
5180 "quoted string split across lines\n" . $hereprev) &&
5181 $fix &&
5182 $prevrawline =~ /^\+.*"\s*$/ &&
5183 $last_coalesced_string_linenr != $linenr - 1) {
5184 my $extracted_string = get_quoted_string($line, $rawline);
5185 my $comma_close = "";
5186 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5187 $comma_close = $1;
5188 }
5189
5190 fix_delete_line($fixlinenr - 1, $prevrawline);
5191 fix_delete_line($fixlinenr, $rawline);
5192 my $fixedline = $prevrawline;
5193 $fixedline =~ s/"\s*$//;
5194 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5195 fix_insert_line($fixlinenr - 1, $fixedline);
5196 $fixedline = $rawline;
5197 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5198 if ($fixedline !~ /\+\s*$/) {
5199 fix_insert_line($fixlinenr, $fixedline);
5200 }
5201 $last_coalesced_string_linenr = $linenr;
5202 }
5203 }
5204
5205# check for missing a space in a string concatenation
5206 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5207 WARN('MISSING_SPACE',
5208 "break quoted strings at a space character\n" . $hereprev);
5209 }
5210
Joe Perchese4b7d302017-05-08 15:55:51 -07005211# check for an embedded function name in a string when the function is known
5212# This does not work very well for -f --file checking as it depends on patch
5213# context providing the function name or a single line form for in-file
5214# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005215 if ($line =~ /^\+.*$String/ &&
5216 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005217 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5218 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005219 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005220 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005221 }
5222
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005223# check for spaces before a quoted newline
5224 if ($rawline =~ /^.*\".*\s\\n/) {
5225 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5226 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5227 $fix) {
5228 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5229 }
5230
5231 }
5232
Joe Perchesf17dba42014-10-13 15:51:51 -07005233# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005234 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005235 CHK("CONCATENATED_STRING",
5236 "Concatenated strings should use spaces between elements\n" . $herecurr);
5237 }
5238
Joe Perches90ad30e2014-12-10 15:51:59 -08005239# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005240 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005241 WARN("STRING_FRAGMENTS",
5242 "Consecutive strings are generally better as a single string\n" . $herecurr);
5243 }
5244
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005245# check for non-standard and hex prefixed decimal printf formats
5246 my $show_L = 1; #don't show the same defect twice
5247 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005248 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005249 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005250 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005251 # check for %L
5252 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005253 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005254 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5255 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005256 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005257 # check for %Z
5258 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5259 WARN("PRINTF_Z",
5260 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5261 $show_Z = 0;
5262 }
5263 # check for 0x<decimal>
5264 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5265 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005266 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5267 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005268 }
5269
5270# check for line continuations in quoted strings with odd counts of "
5271 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5272 WARN("LINE_CONTINUATIONS",
5273 "Avoid line continuations in quoted strings\n" . $herecurr);
5274 }
5275
Andy Whitcroft00df3442007-06-08 13:47:06 -07005276# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005277 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005278 CHK("REDUNDANT_CODE",
5279 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005280 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005281 }
5282
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005283# check for needless "if (<foo>) fn(<foo>)" uses
5284 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005285 my $tested = quotemeta($1);
5286 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5287 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5288 my $func = $1;
5289 if (WARN('NEEDLESS_IF',
5290 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5291 $fix) {
5292 my $do_fix = 1;
5293 my $leading_tabs = "";
5294 my $new_leading_tabs = "";
5295 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5296 $leading_tabs = $1;
5297 } else {
5298 $do_fix = 0;
5299 }
5300 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5301 $new_leading_tabs = $1;
5302 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5303 $do_fix = 0;
5304 }
5305 } else {
5306 $do_fix = 0;
5307 }
5308 if ($do_fix) {
5309 fix_delete_line($fixlinenr - 1, $prevrawline);
5310 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5311 }
5312 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005313 }
5314 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005315
Joe Perchesebfdc402014-08-06 16:10:27 -07005316# check for unnecessary "Out of Memory" messages
5317 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5318 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5319 (defined $1 || defined $3) &&
5320 $linenr > 3) {
5321 my $testval = $2;
5322 my $testline = $lines[$linenr - 3];
5323
5324 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5325# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5326
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005327 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005328 WARN("OOM_MESSAGE",
5329 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5330 }
5331 }
5332
Joe Perchesf78d98f2014-10-13 15:52:01 -07005333# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005334 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005335 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5336 my $level = $1;
5337 if (WARN("UNNECESSARY_KERN_LEVEL",
5338 "Possible unnecessary $level\n" . $herecurr) &&
5339 $fix) {
5340 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5341 }
5342 }
5343
Joe Perches45c55e92017-02-24 15:01:31 -08005344# check for logging continuations
5345 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5346 WARN("LOGGING_CONTINUATION",
5347 "Avoid logging continuation uses where feasible\n" . $herecurr);
5348 }
5349
Joe Perchesabb08a52014-12-10 15:51:46 -08005350# check for mask then right shift without a parentheses
5351 if ($^V && $^V ge 5.10.0 &&
5352 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5353 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5354 WARN("MASK_THEN_SHIFT",
5355 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5356 }
5357
Joe Perchesb75ac612014-12-10 15:52:02 -08005358# check for pointer comparisons to NULL
5359 if ($^V && $^V ge 5.10.0) {
5360 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5361 my $val = $1;
5362 my $equal = "!";
5363 $equal = "" if ($4 eq "!=");
5364 if (CHK("COMPARISON_TO_NULL",
5365 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5366 $fix) {
5367 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5368 }
5369 }
5370 }
5371
Joe Perches8716de32013-09-11 14:24:05 -07005372# check for bad placement of section $InitAttribute (e.g.: __initdata)
5373 if ($line =~ /(\b$InitAttribute\b)/) {
5374 my $attr = $1;
5375 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5376 my $ptr = $1;
5377 my $var = $2;
5378 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5379 ERROR("MISPLACED_INIT",
5380 "$attr should be placed after $var\n" . $herecurr)) ||
5381 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5382 WARN("MISPLACED_INIT",
5383 "$attr should be placed after $var\n" . $herecurr))) &&
5384 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005385 $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 -07005386 }
5387 }
5388 }
5389
Joe Perchese970b8842013-11-12 15:10:10 -08005390# check for $InitAttributeData (ie: __initdata) with const
5391 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5392 my $attr = $1;
5393 $attr =~ /($InitAttributePrefix)(.*)/;
5394 my $attr_prefix = $1;
5395 my $attr_type = $2;
5396 if (ERROR("INIT_ATTRIBUTE",
5397 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5398 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005399 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005400 s/$InitAttributeData/${attr_prefix}initconst/;
5401 }
5402 }
5403
5404# check for $InitAttributeConst (ie: __initconst) without const
5405 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5406 my $attr = $1;
5407 if (ERROR("INIT_ATTRIBUTE",
5408 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5409 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005410 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005411 /(^\+\s*(?:static\s+))/;
5412 $lead = rtrim($1);
5413 $lead = "$lead " if ($lead !~ /^\+$/);
5414 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005415 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005416 }
5417 }
5418
Joe Perchesc17893c2015-04-16 12:44:42 -07005419# check for __read_mostly with const non-pointer (should just be const)
5420 if ($line =~ /\b__read_mostly\b/ &&
5421 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5422 if (ERROR("CONST_READ_MOSTLY",
5423 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5424 $fix) {
5425 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5426 }
5427 }
5428
Joe Perchesfbdb8132014-04-03 14:49:14 -07005429# don't use __constant_<foo> functions outside of include/uapi/
5430 if ($realfile !~ m@^include/uapi/@ &&
5431 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5432 my $constant_func = $1;
5433 my $func = $constant_func;
5434 $func =~ s/^__constant_//;
5435 if (WARN("CONSTANT_CONVERSION",
5436 "$constant_func should be $func\n" . $herecurr) &&
5437 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005438 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005439 }
5440 }
5441
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005442# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005443 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005444 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005445 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005446 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005447 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005448 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5449 }
5450 if ($delay > 2000) {
5451 WARN("LONG_UDELAY",
5452 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005453 }
5454 }
5455
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005456# warn about unexpectedly long msleep's
5457 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5458 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005459 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005460 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005461 }
5462 }
5463
Joe Perches36ec1932013-07-03 15:05:25 -07005464# check for comparisons of jiffies
5465 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5466 WARN("JIFFIES_COMPARISON",
5467 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5468 }
5469
Joe Perches9d7a34a2013-07-03 15:05:26 -07005470# check for comparisons of get_jiffies_64()
5471 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5472 WARN("JIFFIES_COMPARISON",
5473 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5474 }
5475
Andy Whitcroft00df3442007-06-08 13:47:06 -07005476# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005477# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005478# print "#ifdef in C files should be avoided\n";
5479# print "$herecurr";
5480# $clean = 0;
5481# }
5482
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005483# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005484 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005485 if (ERROR("SPACING",
5486 "exactly one space required after that #$1\n" . $herecurr) &&
5487 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005488 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005489 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5490 }
5491
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005492 }
5493
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005494# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005495 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5496 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005497 my $which = $1;
5498 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005499 CHK("UNCOMMENTED_DEFINITION",
5500 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005501 }
5502 }
5503# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005504
5505 my $barriers = qr{
5506 mb|
5507 rmb|
5508 wmb|
5509 read_barrier_depends
5510 }x;
5511 my $barrier_stems = qr{
5512 mb__before_atomic|
5513 mb__after_atomic|
5514 store_release|
5515 load_acquire|
5516 store_mb|
5517 (?:$barriers)
5518 }x;
5519 my $all_barriers = qr{
5520 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005521 smp_(?:$barrier_stems)|
5522 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005523 }x;
5524
5525 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005526 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005527 WARN("MEMORY_BARRIER",
5528 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005529 }
5530 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005531
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005532 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5533
5534 if ($realfile !~ m@^include/asm-generic/@ &&
5535 $realfile !~ m@/barrier\.h$@ &&
5536 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5537 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5538 WARN("MEMORY_BARRIER",
5539 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5540 }
5541
Joe Perchescb426e92015-06-25 15:02:46 -07005542# check for waitqueue_active without a comment.
5543 if ($line =~ /\bwaitqueue_active\s*\(/) {
5544 if (!ctx_has_comment($first_line, $linenr)) {
5545 WARN("WAITQUEUE_ACTIVE",
5546 "waitqueue_active without comment\n" . $herecurr);
5547 }
5548 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005549
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005550# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005551 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005552 CHK("ARCH_DEFINES",
5553 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005554 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005555
Tobias Klauserd4977c72010-05-24 14:33:30 -07005556# Check that the storage class is at the beginning of a declaration
5557 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005558 WARN("STORAGE_CLASS",
5559 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005560 }
5561
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005562# check the location of the inline attribute, that it is between
5563# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005564 if ($line =~ /\b$Type\s+$Inline\b/ ||
5565 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005566 ERROR("INLINE_LOCATION",
5567 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005568 }
5569
Andy Whitcroft8905a672007-11-28 16:21:06 -08005570# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005571 if ($realfile !~ m@\binclude/uapi/@ &&
5572 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005573 if (WARN("INLINE",
5574 "plain inline is preferred over $1\n" . $herecurr) &&
5575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005576 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005577
5578 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005579 }
5580
Joe Perches3d130fd2011-01-12 17:00:00 -08005581# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005582 if ($realfile !~ m@\binclude/uapi/@ &&
5583 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005584 WARN("PREFER_PACKED",
5585 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005586 }
5587
Joe Perches39b7e282011-07-25 17:13:24 -07005588# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005589 if ($realfile !~ m@\binclude/uapi/@ &&
5590 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005591 WARN("PREFER_ALIGNED",
5592 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005593 }
5594
Joe Perches5f14d3b2012-01-10 15:09:52 -08005595# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005596 if ($realfile !~ m@\binclude/uapi/@ &&
5597 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005598 if (WARN("PREFER_PRINTF",
5599 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5600 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005601 $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 -07005602
5603 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005604 }
5605
Joe Perches6061d942012-03-23 15:02:16 -07005606# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005607 if ($realfile !~ m@\binclude/uapi/@ &&
5608 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005609 if (WARN("PREFER_SCANF",
5610 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5611 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005612 $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 -07005613 }
Joe Perches6061d942012-03-23 15:02:16 -07005614 }
5615
Joe Perches619a9082014-12-10 15:51:35 -08005616# Check for __attribute__ weak, or __weak declarations (may have link issues)
5617 if ($^V && $^V ge 5.10.0 &&
5618 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5619 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5620 $line =~ /\b__weak\b/)) {
5621 ERROR("WEAK_DECLARATION",
5622 "Using weak declarations can have unintended link defects\n" . $herecurr);
5623 }
5624
Tomas Winklerfd39f902016-12-12 16:46:34 -08005625# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005626 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005627 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005628 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5629 my $type = $1;
5630 if ($type =~ /\b($typeC99Typedefs)\b/) {
5631 $type = $1;
5632 my $kernel_type = 'u';
5633 $kernel_type = 's' if ($type =~ /^_*[si]/);
5634 $type =~ /(\d+)/;
5635 $kernel_type .= $1;
5636 if (CHK("PREFER_KERNEL_TYPES",
5637 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5638 $fix) {
5639 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5640 }
5641 }
5642 }
5643
Joe Perches938224b2016-01-20 14:59:15 -08005644# check for cast of C90 native int or longer types constants
5645 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5646 my $cast = $1;
5647 my $const = $2;
5648 if (WARN("TYPECAST_INT_CONSTANT",
5649 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5650 $fix) {
5651 my $suffix = "";
5652 my $newconst = $const;
5653 $newconst =~ s/${Int_type}$//;
5654 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5655 if ($cast =~ /\blong\s+long\b/) {
5656 $suffix .= 'LL';
5657 } elsif ($cast =~ /\blong\b/) {
5658 $suffix .= 'L';
5659 }
5660 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5661 }
5662 }
5663
Joe Perches8f53a9b2010-03-05 13:43:48 -08005664# check for sizeof(&)
5665 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005666 WARN("SIZEOF_ADDRESS",
5667 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005668 }
5669
Joe Perches66c80b62012-07-30 14:41:22 -07005670# check for sizeof without parenthesis
5671 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005672 if (WARN("SIZEOF_PARENTHESIS",
5673 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5674 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005675 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005676 }
Joe Perches66c80b62012-07-30 14:41:22 -07005677 }
5678
Joe Perches88982fe2012-12-17 16:02:00 -08005679# check for struct spinlock declarations
5680 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5681 WARN("USE_SPINLOCK_T",
5682 "struct spinlock should be spinlock_t\n" . $herecurr);
5683 }
5684
Joe Perchesa6962d72013-04-29 16:18:13 -07005685# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005686 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005687 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005688 $fmt =~ s/%%//g;
5689 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005690 if (WARN("PREFER_SEQ_PUTS",
5691 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5692 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005693 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005694 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005695 }
5696 }
5697
Joe Perches0b523762017-05-08 15:55:36 -07005698 # check for vsprintf extension %p<foo> misuses
5699 if ($^V && $^V ge 5.10.0 &&
5700 defined $stat &&
5701 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5702 $1 !~ /^_*volatile_*$/) {
5703 my $bad_extension = "";
5704 my $lc = $stat =~ tr@\n@@;
5705 $lc = $lc + $linenr;
5706 for (my $count = $linenr; $count <= $lc; $count++) {
5707 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5708 $fmt =~ s/%%//g;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02005709 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) {
Joe Perches0b523762017-05-08 15:55:36 -07005710 $bad_extension = $1;
5711 last;
5712 }
5713 }
5714 if ($bad_extension ne "") {
5715 my $stat_real = raw_line($linenr, 0);
5716 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5717 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5718 }
5719 WARN("VSPRINTF_POINTER_EXTENSION",
5720 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5721 }
5722 }
5723
Andy Whitcroft554e1652012-01-10 15:09:57 -08005724# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005725 if ($^V && $^V ge 5.10.0 &&
5726 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005727 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005728
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005729 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005730 my $ms_val = $7;
5731 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005732
Andy Whitcroft554e1652012-01-10 15:09:57 -08005733 if ($ms_size =~ /^(0x|)0$/i) {
5734 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005735 "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 -08005736 } elsif ($ms_size =~ /^(0x|)1$/i) {
5737 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005738 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5739 }
5740 }
5741
Joe Perches98a9bba2014-01-23 15:54:52 -08005742# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005743# if ($^V && $^V ge 5.10.0 &&
5744# defined $stat &&
5745# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5746# if (WARN("PREFER_ETHER_ADDR_COPY",
5747# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5748# $fix) {
5749# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5750# }
5751# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005752
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005753# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005754# if ($^V && $^V ge 5.10.0 &&
5755# defined $stat &&
5756# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5757# WARN("PREFER_ETHER_ADDR_EQUAL",
5758# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5759# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005760
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005761# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5762# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005763# if ($^V && $^V ge 5.10.0 &&
5764# defined $stat &&
5765# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5766#
5767# my $ms_val = $7;
5768#
5769# if ($ms_val =~ /^(?:0x|)0+$/i) {
5770# if (WARN("PREFER_ETH_ZERO_ADDR",
5771# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5772# $fix) {
5773# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5774# }
5775# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5776# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5777# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5778# $fix) {
5779# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5780# }
5781# }
5782# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005783
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005784# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005785 if ($^V && $^V ge 5.10.0 &&
5786 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005787 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005788 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005789 my $call = $1;
5790 my $cast1 = deparenthesize($2);
5791 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005792 my $cast2 = deparenthesize($7);
5793 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005794 my $cast;
5795
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005796 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005797 $cast = "$cast1 or $cast2";
5798 } elsif ($cast1 ne "") {
5799 $cast = $cast1;
5800 } else {
5801 $cast = $cast2;
5802 }
5803 WARN("MINMAX",
5804 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005805 }
5806 }
5807
Joe Perches4a273192012-07-30 14:41:20 -07005808# check usleep_range arguments
5809 if ($^V && $^V ge 5.10.0 &&
5810 defined $stat &&
5811 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5812 my $min = $1;
5813 my $max = $7;
5814 if ($min eq $max) {
5815 WARN("USLEEP_RANGE",
5816 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5817 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5818 $min > $max) {
5819 WARN("USLEEP_RANGE",
5820 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5821 }
5822 }
5823
Joe Perches823b7942013-11-12 15:10:15 -08005824# check for naked sscanf
5825 if ($^V && $^V ge 5.10.0 &&
5826 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005827 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005828 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5829 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5830 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5831 my $lc = $stat =~ tr@\n@@;
5832 $lc = $lc + $linenr;
5833 my $stat_real = raw_line($linenr, 0);
5834 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5835 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5836 }
5837 WARN("NAKED_SSCANF",
5838 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5839 }
5840
Joe Perchesafc819a2014-06-04 16:12:08 -07005841# check for simple sscanf that should be kstrto<foo>
5842 if ($^V && $^V ge 5.10.0 &&
5843 defined $stat &&
5844 $line =~ /\bsscanf\b/) {
5845 my $lc = $stat =~ tr@\n@@;
5846 $lc = $lc + $linenr;
5847 my $stat_real = raw_line($linenr, 0);
5848 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5849 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5850 }
5851 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5852 my $format = $6;
5853 my $count = $format =~ tr@%@%@;
5854 if ($count == 1 &&
5855 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5856 WARN("SSCANF_TO_KSTRTO",
5857 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5858 }
5859 }
5860 }
5861
Joe Perches70dc8a42013-09-11 14:23:58 -07005862# check for new externs in .h files.
5863 if ($realfile =~ /\.h$/ &&
5864 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005865 if (CHK("AVOID_EXTERNS",
5866 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005867 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005868 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005869 }
5870 }
5871
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005872# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005873 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005874 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005875 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005876 my $function_name = $1;
5877 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005878
5879 my $s = $stat;
5880 if (defined $cond) {
5881 substr($s, 0, length($cond), '');
5882 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005883 if ($s =~ /^\s*;/ &&
5884 $function_name ne 'uninitialized_var')
5885 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005886 WARN("AVOID_EXTERNS",
5887 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005888 }
5889
5890 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005891 WARN("FUNCTION_ARGUMENTS",
5892 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005893 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005894
5895 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5896 $stat =~ /^.\s*extern\s+/)
5897 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005898 WARN("AVOID_EXTERNS",
5899 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005900 }
5901
Joe Perchesa0ad7592017-07-10 15:52:19 -07005902# check for function declarations that have arguments without identifier names
5903 if (defined $stat &&
Joe Perchesca0d8922016-10-11 13:52:16 -07005904 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5905 $1 ne "void") {
5906 my $args = trim($1);
5907 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5908 my $arg = trim($1);
5909 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5910 WARN("FUNCTION_ARGUMENTS",
5911 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5912 }
5913 }
5914 }
5915
Joe Perchesa0ad7592017-07-10 15:52:19 -07005916# check for function definitions
5917 if ($^V && $^V ge 5.10.0 &&
5918 defined $stat &&
5919 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5920 $context_function = $1;
5921
5922# check for multiline function definition with misplaced open brace
5923 my $ok = 0;
5924 my $cnt = statement_rawlines($stat);
5925 my $herectx = $here . "\n";
5926 for (my $n = 0; $n < $cnt; $n++) {
5927 my $rl = raw_line($linenr, $n);
5928 $herectx .= $rl . "\n";
5929 $ok = 1 if ($rl =~ /^[ \+]\{/);
5930 $ok = 1 if ($rl =~ /\{/ && $n == 0);
5931 last if $rl =~ /^[ \+].*\{/;
5932 }
5933 if (!$ok) {
5934 ERROR("OPEN_BRACE",
5935 "open brace '{' following function definitions go on the next line\n" . $herectx);
5936 }
5937 }
5938
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005939# checks for new __setup's
5940 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5941 my $name = $1;
5942
5943 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005944 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005945 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005946 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005947 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005948
5949# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005950 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005951 WARN("UNNECESSARY_CASTS",
5952 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005953 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005954
Joe Perchesa640d252013-07-03 15:05:21 -07005955# alloc style
5956# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5957 if ($^V && $^V ge 5.10.0 &&
5958 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5959 CHK("ALLOC_SIZEOF_STRUCT",
5960 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5961 }
5962
Joe Perches60a55362014-06-04 16:12:07 -07005963# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5964 if ($^V && $^V ge 5.10.0 &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005965 defined $stat &&
5966 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005967 my $oldfunc = $3;
5968 my $a1 = $4;
5969 my $a2 = $10;
5970 my $newfunc = "kmalloc_array";
5971 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005972 my $r1 = $a1;
5973 my $r2 = $a2;
5974 if ($a1 =~ /^sizeof\s*\S/) {
5975 $r1 = $a2;
5976 $r2 = $a1;
5977 }
5978 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5979 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005980 my $ctx = '';
5981 my $herectx = $here . "\n";
5982 my $cnt = statement_rawlines($stat);
5983 for (my $n = 0; $n < $cnt; $n++) {
5984 $herectx .= raw_line($linenr, $n) . "\n";
5985 }
Joe Perches60a55362014-06-04 16:12:07 -07005986 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005987 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
5988 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07005989 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005990 $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 -07005991 }
5992 }
5993 }
5994
Joe Perches972fdea2013-04-29 16:18:12 -07005995# check for krealloc arg reuse
5996 if ($^V && $^V ge 5.10.0 &&
5997 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5998 WARN("KREALLOC_ARG_REUSE",
5999 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6000 }
6001
Joe Perches5ce59ae2013-02-21 16:44:18 -08006002# check for alloc argument mismatch
6003 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6004 WARN("ALLOC_ARRAY_ARGS",
6005 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6006 }
6007
Joe Perchescaf2a542011-01-12 16:59:56 -08006008# check for multiple semicolons
6009 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006010 if (WARN("ONE_SEMICOLON",
6011 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6012 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006013 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006014 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006015 }
6016
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006017# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6018 if ($realfile !~ m@^include/uapi/@ &&
6019 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006020 my $ull = "";
6021 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6022 if (CHK("BIT_MACRO",
6023 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6024 $fix) {
6025 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6026 }
6027 }
6028
Joe Perches2d632742016-05-20 17:04:00 -07006029# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6030 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6031 my $config = $1;
6032 if (WARN("PREFER_IS_ENABLED",
6033 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6034 $fix) {
6035 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6036 }
6037 }
6038
Joe Perchese81f2392014-08-06 16:11:25 -07006039# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006040 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6041 my $has_break = 0;
6042 my $has_statement = 0;
6043 my $count = 0;
6044 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006045 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006046 $prevline--;
6047 my $rline = $rawlines[$prevline - 1];
6048 my $fline = $lines[$prevline - 1];
6049 last if ($fline =~ /^\@\@/);
6050 next if ($fline =~ /^\-/);
6051 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6052 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6053 next if ($fline =~ /^.[\s$;]*$/);
6054 $has_statement = 1;
6055 $count++;
6056 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6057 }
6058 if (!$has_break && $has_statement) {
6059 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006060 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006061 }
6062 }
6063
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006064# check for switch/default statements without a break;
6065 if ($^V && $^V ge 5.10.0 &&
6066 defined $stat &&
6067 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6068 my $ctx = '';
6069 my $herectx = $here . "\n";
6070 my $cnt = statement_rawlines($stat);
6071 for (my $n = 0; $n < $cnt; $n++) {
6072 $herectx .= raw_line($linenr, $n) . "\n";
6073 }
6074 WARN("DEFAULT_NO_BREAK",
6075 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006076 }
6077
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006078# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006079 if ($line =~ /\b__FUNCTION__\b/) {
6080 if (WARN("USE_FUNC",
6081 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6082 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006083 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006084 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006085 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006086
Joe Perches62ec8182015-02-13 14:38:18 -08006087# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6088 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6089 ERROR("DATE_TIME",
6090 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6091 }
6092
Joe Perches2c924882012-03-23 15:02:20 -07006093# check for use of yield()
6094 if ($line =~ /\byield\s*\(\s*\)/) {
6095 WARN("YIELD",
6096 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6097 }
6098
Joe Perches179f8f42013-07-03 15:05:30 -07006099# check for comparisons against true and false
6100 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6101 my $lead = $1;
6102 my $arg = $2;
6103 my $test = $3;
6104 my $otype = $4;
6105 my $trail = $5;
6106 my $op = "!";
6107
6108 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6109
6110 my $type = lc($otype);
6111 if ($type =~ /^(?:true|false)$/) {
6112 if (("$test" eq "==" && "$type" eq "true") ||
6113 ("$test" eq "!=" && "$type" eq "false")) {
6114 $op = "";
6115 }
6116
6117 CHK("BOOL_COMPARISON",
6118 "Using comparison to $otype is error prone\n" . $herecurr);
6119
6120## maybe suggesting a correct construct would better
6121## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6122
6123 }
6124 }
6125
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006126# check for semaphores initialized locked
6127 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006128 WARN("CONSIDER_COMPLETION",
6129 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006130 }
Joe Perches6712d852012-03-23 15:02:20 -07006131
Joe Perches67d0a072011-10-31 17:13:10 -07006132# recommend kstrto* over simple_strto* and strict_strto*
6133 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006134 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006135 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006136 }
Joe Perches6712d852012-03-23 15:02:20 -07006137
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006138# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006139 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006140 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006141 "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 -07006142 }
Joe Perches6712d852012-03-23 15:02:20 -07006143
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006144# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006145# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006146 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006147 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006148 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006149 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006150 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006151
6152# use of NR_CPUS is usually wrong
6153# ignore definitions of NR_CPUS and usage to define arrays as likely right
6154 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006155 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6156 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006157 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6158 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6159 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006160 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006161 WARN("NR_CPUS",
6162 "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 -07006163 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006164
Joe Perches52ea8502013-11-12 15:10:09 -08006165# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6166 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6167 ERROR("DEFINE_ARCH_HAS",
6168 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6169 }
6170
Joe Perchesacd93622015-02-13 14:38:38 -08006171# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6172 if ($^V && $^V ge 5.10.0 &&
6173 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6174 WARN("LIKELY_MISUSE",
6175 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6176 }
6177
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006178# whine mightly about in_atomic
6179 if ($line =~ /\bin_atomic\s*\(/) {
6180 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006181 ERROR("IN_ATOMIC",
6182 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006183 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006184 WARN("IN_ATOMIC",
6185 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006186 }
6187 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006188
Joe Perches481aea52016-05-20 17:04:08 -07006189# whine about ACCESS_ONCE
6190 if ($^V && $^V ge 5.10.0 &&
6191 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6192 my $par = $1;
6193 my $eq = $2;
6194 my $fun = $3;
6195 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6196 if (defined($eq)) {
6197 if (WARN("PREFER_WRITE_ONCE",
6198 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6199 $fix) {
6200 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6201 }
6202 } else {
6203 if (WARN("PREFER_READ_ONCE",
6204 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6205 $fix) {
6206 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6207 }
6208 }
6209 }
6210
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006211# check for mutex_trylock_recursive usage
6212 if ($line =~ /mutex_trylock_recursive/) {
6213 ERROR("LOCKING",
6214 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6215 }
6216
Peter Zijlstra1704f472010-03-19 01:37:42 +01006217# check for lockdep_set_novalidate_class
6218 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6219 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6220 if ($realfile !~ m@^kernel/lockdep@ &&
6221 $realfile !~ m@^include/linux/lockdep@ &&
6222 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006223 ERROR("LOCKDEP",
6224 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006225 }
6226 }
Dave Jones88f88312011-01-12 16:59:59 -08006227
Joe Perchesb392c642015-04-16 12:44:16 -07006228 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6229 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006230 WARN("EXPORTED_WORLD_WRITABLE",
6231 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006232 }
Joe Perches24358802014-04-03 14:49:13 -07006233
Joe Perches515a2352014-04-03 14:49:24 -07006234# Mode permission misuses where it seems decimal should be octal
6235# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6236 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006237 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006238 $line =~ /$mode_perms_search/) {
6239 foreach my $entry (@mode_permission_funcs) {
6240 my $func = $entry->[0];
6241 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006242
Joe Perches459cf0a2016-10-11 13:52:19 -07006243 my $lc = $stat =~ tr@\n@@;
6244 $lc = $lc + $linenr;
6245 my $stat_real = raw_line($linenr, 0);
6246 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6247 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6248 }
6249
Joe Perches515a2352014-04-03 14:49:24 -07006250 my $skip_args = "";
6251 if ($arg_pos > 1) {
6252 $arg_pos--;
6253 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6254 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006255 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006256 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006257 my $val = $1;
6258 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006259 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6260 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006261 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006262 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006263 }
6264 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006265 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006266 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006267 }
Joe Perches24358802014-04-03 14:49:13 -07006268 }
6269 }
6270 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006271
Joe Perches459cf0a2016-10-11 13:52:19 -07006272# check for uses of S_<PERMS> that could be octal for readability
6273 if ($line =~ /\b$mode_perms_string_search\b/) {
6274 my $val = "";
6275 my $oval = "";
6276 my $to = 0;
6277 my $curpos = 0;
6278 my $lastpos = 0;
6279 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6280 $curpos = pos($line);
6281 my $match = $2;
6282 my $omatch = $1;
6283 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6284 $lastpos = $curpos;
6285 $to |= $mode_permission_string_types{$match};
6286 $val .= '\s*\|\s*' if ($val ne "");
6287 $val .= $match;
6288 $oval .= $omatch;
6289 }
6290 $oval =~ s/^\s*\|\s*//;
6291 $oval =~ s/\s*\|\s*$//;
6292 my $octal = sprintf("%04o", $to);
6293 if (WARN("SYMBOLIC_PERMS",
6294 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6295 $fix) {
6296 $fixed[$fixlinenr] =~ s/$val/$octal/;
6297 }
6298 }
6299
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006300# validate content of MODULE_LICENSE against list from include/linux/module.h
6301 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6302 my $extracted_string = get_quoted_string($line, $rawline);
6303 my $valid_licenses = qr{
6304 GPL|
6305 GPL\ v2|
6306 GPL\ and\ additional\ rights|
6307 Dual\ BSD/GPL|
6308 Dual\ MIT/GPL|
6309 Dual\ MPL/GPL|
6310 Proprietary
6311 }x;
6312 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6313 WARN("MODULE_LICENSE",
6314 "unknown module license " . $extracted_string . "\n" . $herecurr);
6315 }
6316 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006317 }
6318
6319 # If we have no input at all, then there is nothing to report on
6320 # so just keep quiet.
6321 if ($#rawlines == -1) {
6322 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006323 }
6324
Andy Whitcroft8905a672007-11-28 16:21:06 -08006325 # In mailback mode only produce a report in the negative, for
6326 # things that appear to be patches.
6327 if ($mailback && ($clean == 1 || !$is_patch)) {
6328 exit(0);
6329 }
6330
6331 # This is not a patch, and we are are in 'no-patch' mode so
6332 # just keep quiet.
6333 if (!$chk_patch && !$is_patch) {
6334 exit(0);
6335 }
6336
Joe Perches06330fc2015-06-25 15:03:11 -07006337 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006338 ERROR("NOT_UNIFIED_DIFF",
6339 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006340 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006341 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006342 ERROR("MISSING_SIGN_OFF",
6343 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006344 }
6345
Andy Whitcroft8905a672007-11-28 16:21:06 -08006346 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006347 if ($summary && !($clean == 1 && $quiet == 1)) {
6348 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006349 print "total: $cnt_error errors, $cnt_warn warnings, " .
6350 (($check)? "$cnt_chk checks, " : "") .
6351 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006352 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006353
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006354 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006355 # If there were any defects found and not already fixing them
6356 if (!$clean and !$fix) {
6357 print << "EOM"
6358
6359NOTE: For some of the reported defects, checkpatch may be able to
6360 mechanically convert to the typical style using --fix or --fix-inplace.
6361EOM
6362 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006363 # If there were whitespace errors which cleanpatch can fix
6364 # then suggest that.
6365 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006366 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006367 print << "EOM"
6368
6369NOTE: Whitespace errors detected.
6370 You may wish to use scripts/cleanpatch or scripts/cleanfile
6371EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006372 }
6373 }
6374
Joe Perchesd752fcc2014-08-06 16:11:05 -07006375 if ($clean == 0 && $fix &&
6376 ("@rawlines" ne "@fixed" ||
6377 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006378 my $newfile = $filename;
6379 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006380 my $linecount = 0;
6381 my $f;
6382
Joe Perchesd752fcc2014-08-06 16:11:05 -07006383 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6384
Joe Perches3705ce52013-07-03 15:05:31 -07006385 open($f, '>', $newfile)
6386 or die "$P: Can't open $newfile for write\n";
6387 foreach my $fixed_line (@fixed) {
6388 $linecount++;
6389 if ($file) {
6390 if ($linecount > 3) {
6391 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006392 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006393 }
6394 } else {
6395 print $f $fixed_line . "\n";
6396 }
6397 }
6398 close($f);
6399
6400 if (!$quiet) {
6401 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006402
Joe Perches3705ce52013-07-03 15:05:31 -07006403Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6404
6405Do _NOT_ trust the results written to this file.
6406Do _NOT_ submit these changes without inspecting them for correctness.
6407
6408This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6409No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006410EOM
6411 }
6412 }
6413
Joe Perchesd8469f12015-06-25 15:03:00 -07006414 if ($quiet == 0) {
6415 print "\n";
6416 if ($clean == 1) {
6417 print "$vname has no obvious style problems and is ready for submission.\n";
6418 } else {
6419 print "$vname has style problems, please review.\n";
6420 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006421 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006422 return $clean;
6423}