blob: b6d29778bd7b662e3c1519140670fcb41997c001 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
David Keitel72bf3e82016-03-23 20:52:44 -070014use constant BEFORE_SHORTTEXT => 0;
15use constant IN_SHORTTEXT => 1;
16use constant AFTER_SHORTTEXT => 2;
David Keitele288d232016-03-23 20:57:21 -070017use constant CHECK_NEXT_SHORTTEXT => 3;
David Keitel72bf3e82016-03-23 20:52:44 -070018use constant SHORTTEXT_LIMIT => 75;
19
Andy Whitcroft0a920b52007-06-01 00:46:48 -070020my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080021my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070022
Joe Perches000d1cc12011-07-25 17:13:25 -070023my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070024
25use Getopt::Long qw(:config no_auto_abbrev);
26
27my $quiet = 0;
28my $tree = 1;
29my $chk_signoff = 1;
30my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080031my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070032my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080034my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070035my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070037my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070038my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070039my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070040my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080041my $summary = 1;
42my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080043my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070045my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070046my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080047my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070048my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080049my %debug;
Joe Perches34456862013-07-03 15:05:34 -070050my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070051my %use_type = ();
52my @use = ();
53my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070054my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070055my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070056my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080057my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070058my $ignore_perl_version = 0;
59my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070060my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070061my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070062my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070063my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perches57230292015-06-25 15:03:03 -070064my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070065my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070066
67sub help {
68 my ($exitcode) = @_;
69
70 print << "EOM";
71Usage: $P [OPTION]... [FILE]...
72Version: $V
73
74Options:
75 -q, --quiet quiet
76 --no-tree run without a kernel tree
77 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080078 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070079 --patch treat FILE as patchfile (default)
80 --emacs emacs compile window format
81 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070082 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070083 -g, --git treat FILE as a single commit or git revision range
84 single git commit with:
85 <rev>
86 <rev>^
87 <rev>~n
88 multiple git commits with:
89 <rev1>..<rev2>
90 <rev1>...<rev2>
91 <rev>-<count>
92 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070093 -f, --file treat FILE as regular source file
94 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070095 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070096 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070097 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070098 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080099 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -0700100 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -0700101 --root=PATH PATH to the kernel tree root
102 --no-summary suppress the per-file summary
103 --mailback only produce a report in case of warnings/errors
104 --summary-file include the filename in summary
105 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
106 'values', 'possible', 'type', and 'attr' (default
107 is all off)
108 --test-only=WORD report only warnings/errors containing WORD
109 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700110 --fix EXPERIMENTAL - may create horrible results
111 If correctable single-line errors exist, create
112 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
113 with potential errors corrected to the preferred
114 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800115 --fix-inplace EXPERIMENTAL - may create horrible results
116 Is the same as --fix, but overwrites the input
117 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700118 --ignore-perl-version override checking of perl version. expect
119 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700120 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700121 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700122 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700123 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700124 -h, --help, --version display this help and exit
125
126When FILE is - read standard input.
127EOM
128
129 exit($exitcode);
130}
131
Joe Perches3beb42e2016-05-20 17:04:14 -0700132sub uniq {
133 my %seen;
134 return grep { !$seen{$_}++ } @_;
135}
136
137sub list_types {
138 my ($exitcode) = @_;
139
140 my $count = 0;
141
142 local $/ = undef;
143
144 open(my $script, '<', abs_path($P)) or
145 die "$P: Can't read '$P' $!\n";
146
147 my $text = <$script>;
148 close($script);
149
150 my @types = ();
151 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
152 push (@types, $_);
153 }
154 @types = sort(uniq(@types));
155 print("#\tMessage type\n\n");
156 foreach my $type (@types) {
157 print(++$count . "\t" . $type . "\n");
158 }
159
160 exit($exitcode);
161}
162
Joe Perches000d1cc12011-07-25 17:13:25 -0700163my $conf = which_conf($configuration_file);
164if (-f $conf) {
165 my @conf_args;
166 open(my $conffile, '<', "$conf")
167 or warn "$P: Can't find a readable $configuration_file file $!\n";
168
169 while (<$conffile>) {
170 my $line = $_;
171
172 $line =~ s/\s*\n?$//g;
173 $line =~ s/^\s*//g;
174 $line =~ s/\s+/ /g;
175
176 next if ($line =~ m/^\s*#/);
177 next if ($line =~ m/^\s*$/);
178
179 my @words = split(" ", $line);
180 foreach my $word (@words) {
181 last if ($word =~ m/^#/);
182 push (@conf_args, $word);
183 }
184 }
185 close($conffile);
186 unshift(@ARGV, @conf_args) if @conf_args;
187}
188
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700189GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700190 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700191 'tree!' => \$tree,
192 'signoff!' => \$chk_signoff,
193 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800194 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800196 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700197 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700198 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700199 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700200 'subjective!' => \$check,
201 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700202 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700203 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700204 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700205 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800206 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700207 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700208 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800209 'summary!' => \$summary,
210 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800211 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700212 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800213 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700214 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800215 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700216 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700217 'codespell!' => \$codespell,
218 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700219 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700220 'h|help' => \$help,
221 'version' => \$help
222) or help(1);
223
224help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700225
Joe Perches3beb42e2016-05-20 17:04:14 -0700226list_types(0) if ($list_types);
227
Joe Perches9624b8d2014-01-23 15:54:44 -0800228$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700229$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800230
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700231my $exit = 0;
232
Dave Hansend62a2012013-09-11 14:23:56 -0700233if ($^V && $^V lt $minimum_perl_version) {
234 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
235 if (!$ignore_perl_version) {
236 exit(1);
237 }
238}
239
Allen Hubbe45107ff2016-08-02 14:04:47 -0700240#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700241if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700242 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243}
244
Joe Perches91bfe482013-09-11 14:23:59 -0700245sub hash_save_array_words {
246 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700247
Joe Perches91bfe482013-09-11 14:23:59 -0700248 my @array = split(/,/, join(',', @$arrayRef));
249 foreach my $word (@array) {
250 $word =~ s/\s*\n?$//g;
251 $word =~ s/^\s*//g;
252 $word =~ s/\s+/ /g;
253 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700254
Joe Perches91bfe482013-09-11 14:23:59 -0700255 next if ($word =~ m/^\s*#/);
256 next if ($word =~ m/^\s*$/);
257
258 $hashRef->{$word}++;
259 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700260}
261
Joe Perches91bfe482013-09-11 14:23:59 -0700262sub hash_show_words {
263 my ($hashRef, $prefix) = @_;
264
Joe Perches3c816e42015-06-25 15:03:29 -0700265 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700266 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700267 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700268 print " $word";
269 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700270 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700271 }
272}
273
274hash_save_array_words(\%ignore_type, \@ignore);
275hash_save_array_words(\%use_type, \@use);
276
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800277my $dbg_values = 0;
278my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700279my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700280my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800281for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800282 ## no critic
283 eval "\${dbg_$key} = '$debug{$key}';";
284 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800285}
286
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700287my $rpt_cleaners = 0;
288
Andy Whitcroft8905a672007-11-28 16:21:06 -0800289if ($terse) {
290 $emacs = 1;
291 $quiet++;
292}
293
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294if ($tree) {
295 if (defined $root) {
296 if (!top_of_kernel_tree($root)) {
297 die "$P: $root: --root does not point at a valid tree\n";
298 }
299 } else {
300 if (top_of_kernel_tree('.')) {
301 $root = '.';
302 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
303 top_of_kernel_tree($1)) {
304 $root = $1;
305 }
306 }
307
308 if (!defined $root) {
309 print "Must be run from the top-level dir. of a kernel tree\n";
310 exit(2);
311 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700312}
313
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314my $emitted_corrupt = 0;
315
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700316our $Ident = qr{
317 [A-Za-z_][A-Za-z\d_]*
318 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
319 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700320our $Storage = qr{extern|static|asmlinkage};
321our $Sparse = qr{
322 __user|
323 __kernel|
324 __force|
325 __iomem|
326 __must_check|
327 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800328 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700329 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800330 __rcu|
331 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700332 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800333our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
334our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
335our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
336our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
337our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700338
Wolfram Sang52131292010-03-05 13:43:51 -0800339# Notes to $Attribute:
340# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700341our $Attribute = qr{
342 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700343 __percpu|
344 __nocast|
345 __safe|
346 __bitwise__|
347 __packed__|
348 __packed2__|
349 __naked|
350 __maybe_unused|
351 __always_unused|
352 __noreturn|
353 __used|
354 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800355 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700356 __noclone|
357 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700358 __read_mostly|
359 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700360 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700361 ____cacheline_aligned|
362 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800363 ____cacheline_internodealigned_in_smp|
364 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700365 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700366our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700367our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700368our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
369our $Lval = qr{$Ident(?:$Member)*};
370
Joe Perches95e2c602013-07-03 15:05:20 -0700371our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
372our $Binary = qr{(?i)0b[01]+$Int_type?};
373our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
374our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700375our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800376our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800377our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
378our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
379our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800380our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700381our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800382our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700383our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700384our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700385our $Operators = qr{
386 <=|>=|==|!=|
387 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700388 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700389 }x;
390
Joe Perches91cb5192014-04-03 14:49:32 -0700391our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
392
Joe Perchesab7e23f2015-04-16 12:44:22 -0700393our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800394our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700395our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700396our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700398our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700400our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401
Joe Perches15662b32011-10-31 17:13:12 -0700402our $NON_ASCII_UTF8 = qr{
403 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700404 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
405 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
406 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
407 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
408 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
409 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
410}x;
411
Joe Perches15662b32011-10-31 17:13:12 -0700412our $UTF8 = qr{
413 [\x09\x0A\x0D\x20-\x7E] # ASCII
414 | $NON_ASCII_UTF8
415}x;
416
Joe Perchese6176fa2015-06-25 15:02:49 -0700417our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800418our $typeOtherOSTypedefs = qr{(?x:
419 u_(?:char|short|int|long) | # bsd
420 u(?:nchar|short|int|long) # sysv
421)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700422our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700423 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700424 atomic_t
425)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700426our $typeTypedefs = qr{(?x:
427 $typeC99Typedefs\b|
428 $typeOtherOSTypedefs\b|
429 $typeKernelTypedefs\b
430)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700431
Joe Perches6d32f7a2015-11-06 16:31:37 -0800432our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
433
Joe Perches691e6692010-03-05 13:43:51 -0800434our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700435 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700436 (?:[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 -0700437 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700438 panic|
Joe Perches06668722013-11-12 15:10:07 -0800439 MODULE_[A-Z_]+|
440 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800441)};
442
Joe Perches20112472011-07-25 17:13:23 -0700443our $signature_tags = qr{(?xi:
444 Signed-off-by:|
445 Acked-by:|
446 Tested-by:|
447 Reviewed-by:|
448 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700449 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700450 To:|
451 Cc:
452)};
453
Joe Perches18130872014-08-06 16:11:22 -0700454our @typeListMisordered = (
455 qr{char\s+(?:un)?signed},
456 qr{int\s+(?:(?:un)?signed\s+)?short\s},
457 qr{int\s+short(?:\s+(?:un)?signed)},
458 qr{short\s+int(?:\s+(?:un)?signed)},
459 qr{(?:un)?signed\s+int\s+short},
460 qr{short\s+(?:un)?signed},
461 qr{long\s+int\s+(?:un)?signed},
462 qr{int\s+long\s+(?:un)?signed},
463 qr{long\s+(?:un)?signed\s+int},
464 qr{int\s+(?:un)?signed\s+long},
465 qr{int\s+(?:un)?signed},
466 qr{int\s+long\s+long\s+(?:un)?signed},
467 qr{long\s+long\s+int\s+(?:un)?signed},
468 qr{long\s+long\s+(?:un)?signed\s+int},
469 qr{long\s+long\s+(?:un)?signed},
470 qr{long\s+(?:un)?signed},
471);
472
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473our @typeList = (
474 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700475 qr{(?:(?:un)?signed\s+)?char},
476 qr{(?:(?:un)?signed\s+)?short\s+int},
477 qr{(?:(?:un)?signed\s+)?short},
478 qr{(?:(?:un)?signed\s+)?int},
479 qr{(?:(?:un)?signed\s+)?long\s+int},
480 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
481 qr{(?:(?:un)?signed\s+)?long\s+long},
482 qr{(?:(?:un)?signed\s+)?long},
483 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800484 qr{float},
485 qr{double},
486 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800487 qr{struct\s+$Ident},
488 qr{union\s+$Ident},
489 qr{enum\s+$Ident},
490 qr{${Ident}_t},
491 qr{${Ident}_handler},
492 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700493 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800494);
Joe Perches938224b2016-01-20 14:59:15 -0800495
496our $C90_int_types = qr{(?x:
497 long\s+long\s+int\s+(?:un)?signed|
498 long\s+long\s+(?:un)?signed\s+int|
499 long\s+long\s+(?:un)?signed|
500 (?:(?:un)?signed\s+)?long\s+long\s+int|
501 (?:(?:un)?signed\s+)?long\s+long|
502 int\s+long\s+long\s+(?:un)?signed|
503 int\s+(?:(?:un)?signed\s+)?long\s+long|
504
505 long\s+int\s+(?:un)?signed|
506 long\s+(?:un)?signed\s+int|
507 long\s+(?:un)?signed|
508 (?:(?:un)?signed\s+)?long\s+int|
509 (?:(?:un)?signed\s+)?long|
510 int\s+long\s+(?:un)?signed|
511 int\s+(?:(?:un)?signed\s+)?long|
512
513 int\s+(?:un)?signed|
514 (?:(?:un)?signed\s+)?int
515)};
516
Alex Dowad485ff232015-06-25 15:02:52 -0700517our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700518our @typeListWithAttr = (
519 @typeList,
520 qr{struct\s+$InitAttribute\s+$Ident},
521 qr{union\s+$InitAttribute\s+$Ident},
522);
523
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700524our @modifierList = (
525 qr{fastcall},
526);
Alex Dowad485ff232015-06-25 15:02:52 -0700527our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800528
Joe Perches24358802014-04-03 14:49:13 -0700529our @mode_permission_funcs = (
530 ["module_param", 3],
531 ["module_param_(?:array|named|string)", 4],
532 ["module_param_array_named", 5],
533 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
534 ["proc_create(?:_data|)", 2],
535 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
536);
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
Wolfram Sang7840a942010-08-09 17:20:57 -0700553our $allowed_asm_includes = qr{(?x:
554 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700555 memory|
556 time|
557 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700558)};
559# memory.h: ARM has a custom one
560
Kees Cook66b47b42014-10-13 15:51:57 -0700561# Load common spelling mistakes and build regular expression list.
562my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700563my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700564
Joe Perches36061e32014-12-10 15:51:43 -0800565if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800566 while (<$spelling>) {
567 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700568
Joe Perches36061e32014-12-10 15:51:43 -0800569 $line =~ s/\s*\n?$//g;
570 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700571
Joe Perches36061e32014-12-10 15:51:43 -0800572 next if ($line =~ m/^\s*#/);
573 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700574
Joe Perches36061e32014-12-10 15:51:43 -0800575 my ($suspect, $fix) = split(/\|\|/, $line);
576
Joe Perches36061e32014-12-10 15:51:43 -0800577 $spelling_fix{$suspect} = $fix;
578 }
579 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800580} else {
581 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700582}
Kees Cook66b47b42014-10-13 15:51:57 -0700583
Joe Perchesebfd7d62015-04-16 12:44:14 -0700584if ($codespell) {
585 if (open(my $spelling, '<', $codespellfile)) {
586 while (<$spelling>) {
587 my $line = $_;
588
589 $line =~ s/\s*\n?$//g;
590 $line =~ s/^\s*//g;
591
592 next if ($line =~ m/^\s*#/);
593 next if ($line =~ m/^\s*$/);
594 next if ($line =~ m/, disabled/i);
595
596 $line =~ s/,.*$//;
597
598 my ($suspect, $fix) = split(/->/, $line);
599
600 $spelling_fix{$suspect} = $fix;
601 }
602 close($spelling);
603 } else {
604 warn "No codespell typos will be found - file '$codespellfile': $!\n";
605 }
606}
607
608$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
609
Andy Whitcroft8905a672007-11-28 16:21:06 -0800610sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700611 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
612 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700613 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700614 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700615 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700616 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700617 (?:$typeTypedefs\b)|
618 (?:${all}\b)
619 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800620 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700621 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800622 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800623 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700624 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700625 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800626 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700627 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800628 }x;
Joe Perches18130872014-08-06 16:11:22 -0700629 $NonptrTypeMisordered = qr{
630 (?:$Modifier\s+|const\s+)*
631 (?:
632 (?:${Misordered}\b)
633 )
634 (?:\s+$Modifier|\s+const)*
635 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700636 $NonptrTypeWithAttr = qr{
637 (?:$Modifier\s+|const\s+)*
638 (?:
639 (?:typeof|__typeof__)\s*\([^\)]*\)|
640 (?:$typeTypedefs\b)|
641 (?:${allWithAttr}\b)
642 )
643 (?:\s+$Modifier|\s+const)*
644 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800645 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700646 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700647 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700648 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800649 }x;
Joe Perches18130872014-08-06 16:11:22 -0700650 $TypeMisordered = qr{
651 $NonptrTypeMisordered
652 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
653 (?:\s+$Inline|\s+$Modifier)*
654 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700655 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700656 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800657}
658build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700659
Joe Perches7d2367a2011-07-25 17:13:22 -0700660our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700661
662# Using $balanced_parens, $LvalOrFunc, or $FuncArg
663# requires at least perl version v5.10.0
664# Any use must be runtime checked with $^V
665
666our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700667our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800668our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700669
Joe Perchesf8422302014-08-06 16:11:31 -0700670our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700671 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700672 (?:$Storage\s+)?LIST_HEAD\s*\(|
673 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
674)};
675
Joe Perches7d2367a2011-07-25 17:13:22 -0700676sub deparenthesize {
677 my ($string) = @_;
678 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700679
680 while ($string =~ /^\s*\(.*\)\s*$/) {
681 $string =~ s@^\s*\(\s*@@;
682 $string =~ s@\s*\)\s*$@@;
683 }
684
Joe Perches7d2367a2011-07-25 17:13:22 -0700685 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700686
Joe Perches7d2367a2011-07-25 17:13:22 -0700687 return $string;
688}
689
Joe Perches34456862013-07-03 15:05:34 -0700690sub seed_camelcase_file {
691 my ($file) = @_;
692
693 return if (!(-f $file));
694
695 local $/;
696
697 open(my $include_file, '<', "$file")
698 or warn "$P: Can't read '$file' $!\n";
699 my $text = <$include_file>;
700 close($include_file);
701
702 my @lines = split('\n', $text);
703
704 foreach my $line (@lines) {
705 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
706 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
707 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800708 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
709 $camelcase{$1} = 1;
710 } 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 -0700711 $camelcase{$1} = 1;
712 }
713 }
714}
715
716my $camelcase_seeded = 0;
717sub seed_camelcase_includes {
718 return if ($camelcase_seeded);
719
720 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700721 my $camelcase_cache = "";
722 my @include_files = ();
723
724 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700725
Richard Genoud3645e322014-02-10 14:25:32 -0800726 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700727 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
728 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700729 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700730 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700731 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700732 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700733 @include_files = split('\n', $files);
734 foreach my $file (@include_files) {
735 my $date = POSIX::strftime("%Y%m%d%H%M",
736 localtime((stat $file)[9]));
737 $last_mod_date = $date if ($last_mod_date < $date);
738 }
739 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700740 }
Joe Perchesc707a812013-07-08 16:00:43 -0700741
742 if ($camelcase_cache ne "" && -f $camelcase_cache) {
743 open(my $camelcase_file, '<', "$camelcase_cache")
744 or warn "$P: Can't read '$camelcase_cache' $!\n";
745 while (<$camelcase_file>) {
746 chomp;
747 $camelcase{$_} = 1;
748 }
749 close($camelcase_file);
750
751 return;
752 }
753
Richard Genoud3645e322014-02-10 14:25:32 -0800754 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700755 $files = `git ls-files "include/*.h"`;
756 @include_files = split('\n', $files);
757 }
758
Joe Perches34456862013-07-03 15:05:34 -0700759 foreach my $file (@include_files) {
760 seed_camelcase_file($file);
761 }
Joe Perches351b2a12013-07-03 15:05:36 -0700762
Joe Perchesc707a812013-07-08 16:00:43 -0700763 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700764 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700765 open(my $camelcase_file, '>', "$camelcase_cache")
766 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700767 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
768 print $camelcase_file ("$_\n");
769 }
770 close($camelcase_file);
771 }
Joe Perches34456862013-07-03 15:05:34 -0700772}
773
Joe Perchesd311cd42014-08-06 16:10:57 -0700774sub git_commit_info {
775 my ($commit, $id, $desc) = @_;
776
777 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
778
779 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
780 $output =~ s/^\s*//gm;
781 my @lines = split("\n", $output);
782
Joe Perches0d7835f2015-02-13 14:38:35 -0800783 return ($id, $desc) if ($#lines < 0);
784
Joe Perchesd311cd42014-08-06 16:10:57 -0700785 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
786# Maybe one day convert this block of bash into something that returns
787# all matching commit ids, but it's very slow...
788#
789# echo "checking commits $1..."
790# git rev-list --remotes | grep -i "^$1" |
791# while read line ; do
792# git log --format='%H %s' -1 $line |
793# echo "commit $(cut -c 1-12,41-)"
794# done
795 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
796 } else {
797 $id = substr($lines[0], 0, 12);
798 $desc = substr($lines[0], 41);
799 }
800
801 return ($id, $desc);
802}
803
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700804$chk_signoff = 0 if ($file);
805
Andy Whitcroft00df3442007-06-08 13:47:06 -0700806my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800807my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700808my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700809my @fixed_inserted = ();
810my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700811my $fixlinenr = -1;
812
Du, Changbin4a593c32016-05-20 17:04:16 -0700813# If input is git commits, extract all commits from the commit expressions.
814# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
815die "$P: No git repository found\n" if ($git && !-e ".git");
816
817if ($git) {
818 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700819 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700820 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700821 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
822 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700823 } elsif ($commit_expr =~ m/\.\./) {
824 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700825 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700826 $git_range = "-1 $commit_expr";
827 }
828 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
829 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700830 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
831 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700832 my $sha1 = $1;
833 my $subject = $2;
834 unshift(@commits, $sha1);
835 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700836 }
837 }
838 die "$P: no git commits after extraction!\n" if (@commits == 0);
839 @ARGV = @commits;
840}
841
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800842my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700843for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800844 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700845 if ($git) {
846 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
847 die "$P: $filename: git format-patch failed - $!\n";
848 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800849 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700850 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800851 } elsif ($filename eq '-') {
852 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800854 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700855 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700856 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800857 if ($filename eq '-') {
858 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700859 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700860 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800861 } else {
862 $vname = $filename;
863 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800864 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700865 chomp;
866 push(@rawlines, $_);
867 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800868 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700869
870 if ($#ARGV > 0 && $quiet == 0) {
871 print '-' x length($vname) . "\n";
872 print "$vname\n";
873 print '-' x length($vname) . "\n";
874 }
875
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800876 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700877 $exit = 1;
878 }
879 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800880 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700881 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700882 @fixed_inserted = ();
883 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700884 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700885 @modifierListFile = ();
886 @typeListFile = ();
887 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700888}
889
Joe Perchesd8469f12015-06-25 15:03:00 -0700890if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700891 hash_show_words(\%use_type, "Used");
892 hash_show_words(\%ignore_type, "Ignored");
893
Joe Perchesd8469f12015-06-25 15:03:00 -0700894 if ($^V lt 5.10.0) {
895 print << "EOM"
896
897NOTE: perl $^V is not modern enough to detect all possible issues.
898 An upgrade to at least perl v5.10.0 is suggested.
899EOM
900 }
901 if ($exit) {
902 print << "EOM"
903
904NOTE: If any of the errors are false positives, please report
905 them to the maintainer, see CHECKPATCH in MAINTAINERS.
906EOM
907 }
908}
909
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700910exit($exit);
911
912sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700913 my ($root) = @_;
914
915 my @tree_check = (
916 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
917 "README", "Documentation", "arch", "include", "drivers",
918 "fs", "init", "ipc", "kernel", "lib", "scripts",
919 );
920
921 foreach my $check (@tree_check) {
922 if (! -e $root . '/' . $check) {
923 return 0;
924 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700925 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700926 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700927}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700928
Joe Perches20112472011-07-25 17:13:23 -0700929sub parse_email {
930 my ($formatted_email) = @_;
931
932 my $name = "";
933 my $address = "";
934 my $comment = "";
935
936 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
937 $name = $1;
938 $address = $2;
939 $comment = $3 if defined $3;
940 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
941 $address = $1;
942 $comment = $2 if defined $2;
943 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
944 $address = $1;
945 $comment = $2 if defined $2;
946 $formatted_email =~ s/$address.*$//;
947 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700948 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700949 $name =~ s/^\"|\"$//g;
950 # If there's a name left after stripping spaces and
951 # leading quotes, and the address doesn't have both
952 # leading and trailing angle brackets, the address
953 # is invalid. ie:
954 # "joe smith joe@smith.com" bad
955 # "joe smith <joe@smith.com" bad
956 if ($name ne "" && $address !~ /^<[^>]+>$/) {
957 $name = "";
958 $address = "";
959 $comment = "";
960 }
961 }
962
Joe Perches3705ce52013-07-03 15:05:31 -0700963 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700964 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700965 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700966 $address =~ s/^\<|\>$//g;
967
968 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
969 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
970 $name = "\"$name\"";
971 }
972
973 return ($name, $address, $comment);
974}
975
976sub format_email {
977 my ($name, $address) = @_;
978
979 my $formatted_email;
980
Joe Perches3705ce52013-07-03 15:05:31 -0700981 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700982 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700983 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700984
985 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
986 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
987 $name = "\"$name\"";
988 }
989
990 if ("$name" eq "") {
991 $formatted_email = "$address";
992 } else {
993 $formatted_email = "$name <$address>";
994 }
995
996 return $formatted_email;
997}
998
Joe Perchesd311cd42014-08-06 16:10:57 -0700999sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001000 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001001
Joe Perchesbd474ca2014-08-06 16:11:10 -07001002 foreach my $path (split(/:/, $ENV{PATH})) {
1003 if (-e "$path/$bin") {
1004 return "$path/$bin";
1005 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001006 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001007
Joe Perchesbd474ca2014-08-06 16:11:10 -07001008 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001009}
1010
Joe Perches000d1cc12011-07-25 17:13:25 -07001011sub which_conf {
1012 my ($conf) = @_;
1013
1014 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1015 if (-e "$path/$conf") {
1016 return "$path/$conf";
1017 }
1018 }
1019
1020 return "";
1021}
1022
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001023sub expand_tabs {
1024 my ($str) = @_;
1025
1026 my $res = '';
1027 my $n = 0;
1028 for my $c (split(//, $str)) {
1029 if ($c eq "\t") {
1030 $res .= ' ';
1031 $n++;
1032 for (; ($n % 8) != 0; $n++) {
1033 $res .= ' ';
1034 }
1035 next;
1036 }
1037 $res .= $c;
1038 $n++;
1039 }
1040
1041 return $res;
1042}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001044 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001045 return $res;
1046}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001047
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001048sub line_stats {
1049 my ($line) = @_;
1050
1051 # Drop the diff line leader and expand tabs
1052 $line =~ s/^.//;
1053 $line = expand_tabs($line);
1054
1055 # Pick the indent from the front of the line.
1056 my ($white) = ($line =~ /^(\s*)/);
1057
1058 return (length($line), length($white));
1059}
1060
Andy Whitcroft773647a2008-03-28 14:15:58 -07001061my $sanitise_quote = '';
1062
1063sub sanitise_line_reset {
1064 my ($in_comment) = @_;
1065
1066 if ($in_comment) {
1067 $sanitise_quote = '*/';
1068 } else {
1069 $sanitise_quote = '';
1070 }
1071}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001072sub sanitise_line {
1073 my ($line) = @_;
1074
1075 my $res = '';
1076 my $l = '';
1077
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001078 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001079 my $off = 0;
1080 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001081
Andy Whitcroft773647a2008-03-28 14:15:58 -07001082 # Always copy over the diff marker.
1083 $res = substr($line, 0, 1);
1084
1085 for ($off = 1; $off < length($line); $off++) {
1086 $c = substr($line, $off, 1);
1087
1088 # Comments we are wacking completly including the begin
1089 # and end, all to $;.
1090 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1091 $sanitise_quote = '*/';
1092
1093 substr($res, $off, 2, "$;$;");
1094 $off++;
1095 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001096 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001097 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001098 $sanitise_quote = '';
1099 substr($res, $off, 2, "$;$;");
1100 $off++;
1101 next;
1102 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001103 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1104 $sanitise_quote = '//';
1105
1106 substr($res, $off, 2, $sanitise_quote);
1107 $off++;
1108 next;
1109 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001110
1111 # A \ in a string means ignore the next character.
1112 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1113 $c eq "\\") {
1114 substr($res, $off, 2, 'XX');
1115 $off++;
1116 next;
1117 }
1118 # Regular quotes.
1119 if ($c eq "'" || $c eq '"') {
1120 if ($sanitise_quote eq '') {
1121 $sanitise_quote = $c;
1122
1123 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001124 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001125 } elsif ($sanitise_quote eq $c) {
1126 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001127 }
1128 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001129
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001130 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001131 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1132 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001133 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1134 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001135 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1136 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001137 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001138 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001139 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 }
1141
Daniel Walker113f04a2009-09-21 17:04:35 -07001142 if ($sanitise_quote eq '//') {
1143 $sanitise_quote = '';
1144 }
1145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001146 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001147 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001148 my $clean = 'X' x length($1);
1149 $res =~ s@\<.*\>@<$clean>@;
1150
1151 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001152 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001153 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001154 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001155 }
1156
Joe Perchesdadf6802016-08-02 14:04:33 -07001157 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1158 my $match = $1;
1159 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1160 }
1161
Andy Whitcroft00df3442007-06-08 13:47:06 -07001162 return $res;
1163}
1164
Joe Perchesa6962d72013-04-29 16:18:13 -07001165sub get_quoted_string {
1166 my ($line, $rawline) = @_;
1167
Joe Perches33acb542015-06-25 15:02:54 -07001168 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001169 return substr($rawline, $-[0], $+[0] - $-[0]);
1170}
1171
Andy Whitcroft8905a672007-11-28 16:21:06 -08001172sub ctx_statement_block {
1173 my ($linenr, $remain, $off) = @_;
1174 my $line = $linenr - 1;
1175 my $blk = '';
1176 my $soff = $off;
1177 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001178 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001179
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001180 my $loff = 0;
1181
Andy Whitcroft8905a672007-11-28 16:21:06 -08001182 my $type = '';
1183 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001184 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001185 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001186 my $c;
1187 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001188
1189 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001190 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001191 @stack = (['', 0]) if ($#stack == -1);
1192
Andy Whitcroft773647a2008-03-28 14:15:58 -07001193 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001194 # If we are about to drop off the end, pull in more
1195 # context.
1196 if ($off >= $len) {
1197 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001198 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001199 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001200 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001201 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001202 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001203 $len = length($blk);
1204 $line++;
1205 last;
1206 }
1207 # Bail if there is no further context.
1208 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001209 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001210 last;
1211 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001212 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1213 $level++;
1214 $type = '#';
1215 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001216 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001217 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001218 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001219 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001220
Andy Whitcroft773647a2008-03-28 14:15:58 -07001221 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001222
1223 # Handle nested #if/#else.
1224 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1225 push(@stack, [ $type, $level ]);
1226 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1227 ($type, $level) = @{$stack[$#stack - 1]};
1228 } elsif ($remainder =~ /^#\s*endif\b/) {
1229 ($type, $level) = @{pop(@stack)};
1230 }
1231
Andy Whitcroft8905a672007-11-28 16:21:06 -08001232 # Statement ends at the ';' or a close '}' at the
1233 # outermost level.
1234 if ($level == 0 && $c eq ';') {
1235 last;
1236 }
1237
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001238 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001239 if ($level == 0 && $coff_set == 0 &&
1240 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1241 $remainder =~ /^(else)(?:\s|{)/ &&
1242 $remainder !~ /^else\s+if\b/) {
1243 $coff = $off + length($1) - 1;
1244 $coff_set = 1;
1245 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1246 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001247 }
1248
Andy Whitcroft8905a672007-11-28 16:21:06 -08001249 if (($type eq '' || $type eq '(') && $c eq '(') {
1250 $level++;
1251 $type = '(';
1252 }
1253 if ($type eq '(' && $c eq ')') {
1254 $level--;
1255 $type = ($level != 0)? '(' : '';
1256
1257 if ($level == 0 && $coff < $soff) {
1258 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001259 $coff_set = 1;
1260 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001261 }
1262 }
1263 if (($type eq '' || $type eq '{') && $c eq '{') {
1264 $level++;
1265 $type = '{';
1266 }
1267 if ($type eq '{' && $c eq '}') {
1268 $level--;
1269 $type = ($level != 0)? '{' : '';
1270
1271 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001272 if (substr($blk, $off + 1, 1) eq ';') {
1273 $off++;
1274 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001275 last;
1276 }
1277 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001278 # Preprocessor commands end at the newline unless escaped.
1279 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1280 $level--;
1281 $type = '';
1282 $off++;
1283 last;
1284 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 $off++;
1286 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001287 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001288 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001289 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001290 $line++;
1291 $remain--;
1292 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001293
1294 my $statement = substr($blk, $soff, $off - $soff + 1);
1295 my $condition = substr($blk, $soff, $coff - $soff + 1);
1296
1297 #warn "STATEMENT<$statement>\n";
1298 #warn "CONDITION<$condition>\n";
1299
Andy Whitcroft773647a2008-03-28 14:15:58 -07001300 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001301
1302 return ($statement, $condition,
1303 $line, $remain + 1, $off - $loff + 1, $level);
1304}
1305
Andy Whitcroftcf655042008-03-04 14:28:20 -08001306sub statement_lines {
1307 my ($stmt) = @_;
1308
1309 # Strip the diff line prefixes and rip blank lines at start and end.
1310 $stmt =~ s/(^|\n)./$1/g;
1311 $stmt =~ s/^\s*//;
1312 $stmt =~ s/\s*$//;
1313
1314 my @stmt_lines = ($stmt =~ /\n/g);
1315
1316 return $#stmt_lines + 2;
1317}
1318
1319sub statement_rawlines {
1320 my ($stmt) = @_;
1321
1322 my @stmt_lines = ($stmt =~ /\n/g);
1323
1324 return $#stmt_lines + 2;
1325}
1326
1327sub statement_block_size {
1328 my ($stmt) = @_;
1329
1330 $stmt =~ s/(^|\n)./$1/g;
1331 $stmt =~ s/^\s*{//;
1332 $stmt =~ s/}\s*$//;
1333 $stmt =~ s/^\s*//;
1334 $stmt =~ s/\s*$//;
1335
1336 my @stmt_lines = ($stmt =~ /\n/g);
1337 my @stmt_statements = ($stmt =~ /;/g);
1338
1339 my $stmt_lines = $#stmt_lines + 2;
1340 my $stmt_statements = $#stmt_statements + 1;
1341
1342 if ($stmt_lines > $stmt_statements) {
1343 return $stmt_lines;
1344 } else {
1345 return $stmt_statements;
1346 }
1347}
1348
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001349sub ctx_statement_full {
1350 my ($linenr, $remain, $off) = @_;
1351 my ($statement, $condition, $level);
1352
1353 my (@chunks);
1354
Andy Whitcroftcf655042008-03-04 14:28:20 -08001355 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001356 ($statement, $condition, $linenr, $remain, $off, $level) =
1357 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001358 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001359 push(@chunks, [ $condition, $statement ]);
1360 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1361 return ($level, $linenr, @chunks);
1362 }
1363
1364 # Pull in the following conditional/block pairs and see if they
1365 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001366 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001367 ($statement, $condition, $linenr, $remain, $off, $level) =
1368 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001369 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001370 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001371 #print "C: push\n";
1372 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001373 }
1374
1375 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001376}
1377
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001378sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001379 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001380 my $line;
1381 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001382 my $blk = '';
1383 my @o;
1384 my @c;
1385 my @res = ();
1386
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001387 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001388 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 for ($line = $start; $remain > 0; $line++) {
1390 next if ($rawlines[$line] =~ /^-/);
1391 $remain--;
1392
1393 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001394
1395 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001396 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001397 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001398 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001399 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001400 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001401 $level = pop(@stack);
1402 }
1403
Andy Whitcroft01464f32010-10-26 14:23:19 -07001404 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001405 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1406 if ($off > 0) {
1407 $off--;
1408 next;
1409 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001410
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001411 if ($c eq $close && $level > 0) {
1412 $level--;
1413 last if ($level == 0);
1414 } elsif ($c eq $open) {
1415 $level++;
1416 }
1417 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001418
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001419 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001420 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001421 }
1422
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001423 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001424 }
1425
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001426 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001427}
1428sub ctx_block_outer {
1429 my ($linenr, $remain) = @_;
1430
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001431 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1432 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001433}
1434sub ctx_block {
1435 my ($linenr, $remain) = @_;
1436
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001437 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1438 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001439}
1440sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001441 my ($linenr, $remain, $off) = @_;
1442
1443 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1444 return @r;
1445}
1446sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001447 my ($linenr, $remain) = @_;
1448
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001449 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001450}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001451sub ctx_statement_level {
1452 my ($linenr, $remain, $off) = @_;
1453
1454 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1455}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001456
1457sub ctx_locate_comment {
1458 my ($first_line, $end_line) = @_;
1459
1460 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001461 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001462 return $current_comment if (defined $current_comment);
1463
1464 # Look through the context and try and figure out if there is a
1465 # comment.
1466 my $in_comment = 0;
1467 $current_comment = '';
1468 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001469 my $line = $rawlines[$linenr - 1];
1470 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001471 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1472 $in_comment = 1;
1473 }
1474 if ($line =~ m@/\*@) {
1475 $in_comment = 1;
1476 }
1477 if (!$in_comment && $current_comment ne '') {
1478 $current_comment = '';
1479 }
1480 $current_comment .= $line . "\n" if ($in_comment);
1481 if ($line =~ m@\*/@) {
1482 $in_comment = 0;
1483 }
1484 }
1485
1486 chomp($current_comment);
1487 return($current_comment);
1488}
1489sub ctx_has_comment {
1490 my ($first_line, $end_line) = @_;
1491 my $cmt = ctx_locate_comment($first_line, $end_line);
1492
Andy Whitcroft00df3442007-06-08 13:47:06 -07001493 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001494 ##print "CMMT: $cmt\n";
1495
1496 return ($cmt ne '');
1497}
1498
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001499sub raw_line {
1500 my ($linenr, $cnt) = @_;
1501
1502 my $offset = $linenr - 1;
1503 $cnt++;
1504
1505 my $line;
1506 while ($cnt) {
1507 $line = $rawlines[$offset++];
1508 next if (defined($line) && $line =~ /^-/);
1509 $cnt--;
1510 }
1511
1512 return $line;
1513}
1514
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001515sub cat_vet {
1516 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001517 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001518
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001519 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001520 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1521 $res .= $1;
1522 if ($2 ne '') {
1523 $coded = sprintf("^%c", unpack('C', $2) + 64);
1524 $res .= $coded;
1525 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001526 }
1527 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001528
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001529 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001530}
1531
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001532my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001533my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001534my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001535my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001536
1537sub annotate_reset {
1538 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001539 $av_pending = '_';
1540 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001541 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001542}
1543
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001544sub annotate_values {
1545 my ($stream, $type) = @_;
1546
1547 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001548 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001549 my $cur = $stream;
1550
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001551 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001552
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001553 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001554 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001555 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001556 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001557 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001558 print "WS($1)\n" if ($dbg_values > 1);
1559 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001560 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001561 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001562 }
1563
Florian Micklerc023e4732011-01-12 16:59:58 -08001564 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001565 print "CAST($1)\n" if ($dbg_values > 1);
1566 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001567 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001568
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001569 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001570 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001571 $type = 'T';
1572
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001573 } elsif ($cur =~ /^($Modifier)\s*/) {
1574 print "MODIFIER($1)\n" if ($dbg_values > 1);
1575 $type = 'T';
1576
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001577 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001578 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001579 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001580 push(@av_paren_type, $type);
1581 if ($2 ne '') {
1582 $av_pending = 'N';
1583 }
1584 $type = 'E';
1585
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001586 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001587 print "UNDEF($1)\n" if ($dbg_values > 1);
1588 $av_preprocessor = 1;
1589 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001590
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001591 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001592 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001593 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001594
1595 push(@av_paren_type, $type);
1596 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001597 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001598
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001599 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1601 $av_preprocessor = 1;
1602
1603 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1604
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001605 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001606
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001607 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001608 print "PRE_END($1)\n" if ($dbg_values > 1);
1609
1610 $av_preprocessor = 1;
1611
1612 # Assume all arms of the conditional end as this
1613 # one does, and continue as if the #endif was not here.
1614 pop(@av_paren_type);
1615 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001616 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001617
1618 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001619 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001621 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1622 print "ATTR($1)\n" if ($dbg_values > 1);
1623 $av_pending = $type;
1624 $type = 'N';
1625
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001626 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001627 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001628 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001629 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001630 }
1631 $type = 'N';
1632
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001633 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001634 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001635 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001636 $type = 'N';
1637
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001638 } elsif ($cur =~/^(case)/o) {
1639 print "CASE($1)\n" if ($dbg_values > 1);
1640 $av_pend_colon = 'C';
1641 $type = 'N';
1642
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001643 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001644 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001645 $type = 'N';
1646
1647 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001648 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001649 push(@av_paren_type, $av_pending);
1650 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001651 $type = 'N';
1652
1653 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001654 my $new_type = pop(@av_paren_type);
1655 if ($new_type ne '_') {
1656 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001657 print "PAREN('$1') -> $type\n"
1658 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001659 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001660 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001661 }
1662
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001663 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001664 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001665 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001666 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001667
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001668 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1669 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001670 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001671 } elsif ($type eq 'E') {
1672 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001673 }
1674 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1675 $type = 'V';
1676
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001677 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001678 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001679 $type = 'V';
1680
1681 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001682 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001683 $type = 'N';
1684
Andy Whitcroftcf655042008-03-04 14:28:20 -08001685 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001686 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001687 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001688 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001689
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001690 } elsif ($cur =~/^(,)/) {
1691 print "COMMA($1)\n" if ($dbg_values > 1);
1692 $type = 'C';
1693
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001694 } elsif ($cur =~ /^(\?)/o) {
1695 print "QUESTION($1)\n" if ($dbg_values > 1);
1696 $type = 'N';
1697
1698 } elsif ($cur =~ /^(:)/o) {
1699 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1700
1701 substr($var, length($res), 1, $av_pend_colon);
1702 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1703 $type = 'E';
1704 } else {
1705 $type = 'N';
1706 }
1707 $av_pend_colon = 'O';
1708
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001709 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001710 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001711 $type = 'N';
1712
Andy Whitcroft0d413862008-10-15 22:02:16 -07001713 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001714 my $variant;
1715
1716 print "OPV($1)\n" if ($dbg_values > 1);
1717 if ($type eq 'V') {
1718 $variant = 'B';
1719 } else {
1720 $variant = 'U';
1721 }
1722
1723 substr($var, length($res), 1, $variant);
1724 $type = 'N';
1725
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 if ($1 ne '++' && $1 ne '--') {
1729 $type = 'N';
1730 }
1731
1732 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001733 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734 }
1735 if (defined $1) {
1736 $cur = substr($cur, length($1));
1737 $res .= $type x length($1);
1738 }
1739 }
1740
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001741 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001742}
1743
Andy Whitcroft8905a672007-11-28 16:21:06 -08001744sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001745 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001746 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001747 ^(?:
1748 $Modifier|
1749 $Storage|
1750 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001751 DEFINE_\S+
1752 )$|
1753 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001754 goto|
1755 return|
1756 case|
1757 else|
1758 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001759 do|
1760 \#|
1761 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001762 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001763 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001764 )}x;
1765 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1766 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001767 # Check for modifiers.
1768 $possible =~ s/\s*$Storage\s*//g;
1769 $possible =~ s/\s*$Sparse\s*//g;
1770 if ($possible =~ /^\s*$/) {
1771
1772 } elsif ($possible =~ /\s/) {
1773 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001774 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001775 if ($modifier !~ $notPermitted) {
1776 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001777 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001778 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001779 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001780
1781 } else {
1782 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001783 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001784 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001785 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001786 } else {
1787 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001788 }
1789}
1790
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001791my $prefix = '';
1792
Joe Perches000d1cc12011-07-25 17:13:25 -07001793sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001794 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001795
Joe Perchescbec18a2014-04-03 14:49:19 -07001796 return defined $use_type{$type} if (scalar keys %use_type > 0);
1797
1798 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001799}
1800
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001801sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001802 my ($level, $type, $msg) = @_;
1803
1804 if (!show_type($type) ||
1805 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001806 return 0;
1807 }
Joe Perches57230292015-06-25 15:03:03 -07001808 my $output = '';
1809 if (-t STDOUT && $color) {
1810 if ($level eq 'ERROR') {
1811 $output .= RED;
1812 } elsif ($level eq 'WARNING') {
1813 $output .= YELLOW;
1814 } else {
1815 $output .= GREEN;
1816 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001817 }
Joe Perches57230292015-06-25 15:03:03 -07001818 $output .= $prefix . $level . ':';
1819 if ($show_types) {
1820 $output .= BLUE if (-t STDOUT && $color);
1821 $output .= "$type:";
1822 }
1823 $output .= RESET if (-t STDOUT && $color);
1824 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001825
1826 if ($showfile) {
1827 my @lines = split("\n", $output, -1);
1828 splice(@lines, 1, 1);
1829 $output = join("\n", @lines);
1830 }
Joe Perches57230292015-06-25 15:03:03 -07001831 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001832
Joe Perches57230292015-06-25 15:03:03 -07001833 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001834
1835 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001836}
Joe Perchescbec18a2014-04-03 14:49:19 -07001837
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001838sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001839 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001840}
Joe Perches000d1cc12011-07-25 17:13:25 -07001841
Joe Perchesd752fcc2014-08-06 16:11:05 -07001842sub fixup_current_range {
1843 my ($lineRef, $offset, $length) = @_;
1844
1845 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1846 my $o = $1;
1847 my $l = $2;
1848 my $no = $o + $offset;
1849 my $nl = $l + $length;
1850 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1851 }
1852}
1853
1854sub fix_inserted_deleted_lines {
1855 my ($linesRef, $insertedRef, $deletedRef) = @_;
1856
1857 my $range_last_linenr = 0;
1858 my $delta_offset = 0;
1859
1860 my $old_linenr = 0;
1861 my $new_linenr = 0;
1862
1863 my $next_insert = 0;
1864 my $next_delete = 0;
1865
1866 my @lines = ();
1867
1868 my $inserted = @{$insertedRef}[$next_insert++];
1869 my $deleted = @{$deletedRef}[$next_delete++];
1870
1871 foreach my $old_line (@{$linesRef}) {
1872 my $save_line = 1;
1873 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001874 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001875 $delta_offset = 0;
1876 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1877 $range_last_linenr = $new_linenr;
1878 fixup_current_range(\$line, $delta_offset, 0);
1879 }
1880
1881 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1882 $deleted = @{$deletedRef}[$next_delete++];
1883 $save_line = 0;
1884 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1885 }
1886
1887 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1888 push(@lines, ${$inserted}{'LINE'});
1889 $inserted = @{$insertedRef}[$next_insert++];
1890 $new_linenr++;
1891 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1892 }
1893
1894 if ($save_line) {
1895 push(@lines, $line);
1896 $new_linenr++;
1897 }
1898
1899 $old_linenr++;
1900 }
1901
1902 return @lines;
1903}
1904
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001905sub fix_insert_line {
1906 my ($linenr, $line) = @_;
1907
1908 my $inserted = {
1909 LINENR => $linenr,
1910 LINE => $line,
1911 };
1912 push(@fixed_inserted, $inserted);
1913}
1914
1915sub fix_delete_line {
1916 my ($linenr, $line) = @_;
1917
1918 my $deleted = {
1919 LINENR => $linenr,
1920 LINE => $line,
1921 };
1922
1923 push(@fixed_deleted, $deleted);
1924}
1925
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001926sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001927 my ($type, $msg) = @_;
1928
1929 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001930 our $clean = 0;
1931 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001932 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001933 }
Joe Perches3705ce52013-07-03 15:05:31 -07001934 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001935}
1936sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001937 my ($type, $msg) = @_;
1938
1939 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940 our $clean = 0;
1941 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001942 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001943 }
Joe Perches3705ce52013-07-03 15:05:31 -07001944 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001945}
1946sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001947 my ($type, $msg) = @_;
1948
1949 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001950 our $clean = 0;
1951 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001952 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001953 }
Joe Perches3705ce52013-07-03 15:05:31 -07001954 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001955}
1956
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001957sub check_absolute_file {
1958 my ($absolute, $herecurr) = @_;
1959 my $file = $absolute;
1960
1961 ##print "absolute<$absolute>\n";
1962
1963 # See if any suffix of this path is a path within the tree.
1964 while ($file =~ s@^[^/]*/@@) {
1965 if (-f "$root/$file") {
1966 ##print "file<$file>\n";
1967 last;
1968 }
1969 }
1970 if (! -f _) {
1971 return 0;
1972 }
1973
1974 # It is, so see if the prefix is acceptable.
1975 my $prefix = $absolute;
1976 substr($prefix, -length($file)) = '';
1977
1978 ##print "prefix<$prefix>\n";
1979 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001980 WARN("USE_RELATIVE_PATH",
1981 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001982 }
1983}
1984
Joe Perches3705ce52013-07-03 15:05:31 -07001985sub trim {
1986 my ($string) = @_;
1987
Joe Perchesb34c6482013-09-11 14:24:01 -07001988 $string =~ s/^\s+|\s+$//g;
1989
1990 return $string;
1991}
1992
1993sub ltrim {
1994 my ($string) = @_;
1995
1996 $string =~ s/^\s+//;
1997
1998 return $string;
1999}
2000
2001sub rtrim {
2002 my ($string) = @_;
2003
2004 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002005
2006 return $string;
2007}
2008
Joe Perches52ea8502013-11-12 15:10:09 -08002009sub string_find_replace {
2010 my ($string, $find, $replace) = @_;
2011
2012 $string =~ s/$find/$replace/g;
2013
2014 return $string;
2015}
2016
Joe Perches3705ce52013-07-03 15:05:31 -07002017sub tabify {
2018 my ($leading) = @_;
2019
2020 my $source_indent = 8;
2021 my $max_spaces_before_tab = $source_indent - 1;
2022 my $spaces_to_tab = " " x $source_indent;
2023
2024 #convert leading spaces to tabs
2025 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2026 #Remove spaces before a tab
2027 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2028
2029 return "$leading";
2030}
2031
Gregory Beanf07523e2011-07-06 15:36:58 -07002032sub cleanup_continuation_headers {
2033 # Collapse any header-continuation lines into a single line so they
2034 # can be parsed meaningfully, as the parser only has one line
2035 # of context to work with.
2036 my $again;
2037 do {
2038 $again = 0;
2039 foreach my $n (0 .. scalar(@rawlines) - 2) {
2040 if ($rawlines[$n]=~/^\s*$/) {
2041 # A blank line means there's no more chance
2042 # of finding headers. Shortcut to done.
2043 return;
2044 }
2045 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2046 $rawlines[$n+1]=~/^\s+/) {
2047 # Continuation header. Collapse it.
2048 my $line = splice @rawlines, $n+1, 1;
2049 $line=~s/^\s+/ /;
2050 $rawlines[$n] .= $line;
2051 # We've 'destabilized' the list, so restart.
2052 $again = 1;
2053 last;
2054 }
2055 }
2056 } while ($again);
2057}
2058
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002059sub pos_last_openparen {
2060 my ($line) = @_;
2061
2062 my $pos = 0;
2063
2064 my $opens = $line =~ tr/\(/\(/;
2065 my $closes = $line =~ tr/\)/\)/;
2066
2067 my $last_openparen = 0;
2068
2069 if (($opens == 0) || ($closes >= $opens)) {
2070 return -1;
2071 }
2072
2073 my $len = length($line);
2074
2075 for ($pos = 0; $pos < $len; $pos++) {
2076 my $string = substr($line, $pos);
2077 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2078 $pos += length($1) - 1;
2079 } elsif (substr($line, $pos, 1) eq '(') {
2080 $last_openparen = $pos;
2081 } elsif (index($string, '(') == -1) {
2082 last;
2083 }
2084 }
2085
Joe Perches91cb5192014-04-03 14:49:32 -07002086 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002087}
2088
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002089sub process {
2090 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091
2092 my $linenr=0;
2093 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002094 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002095 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002096 my $stashrawline="";
David Keitele288d232016-03-23 20:57:21 -07002097 my $subjectline="";
2098 my $sublinenr="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002099
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002100 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002101 my $indent;
2102 my $previndent=0;
2103 my $stashindent=0;
2104
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002105 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002106 my $signoff = 0;
2107 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002108 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002109 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002110 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002111 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002112 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002113 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002114 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002115 my $non_utf8_charset = 0;
2116
Joe Perches365dd4e2014-08-06 16:10:42 -07002117 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002118 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002119
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002120 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002121 our $cnt_lines = 0;
2122 our $cnt_error = 0;
2123 our $cnt_warn = 0;
2124 our $cnt_chk = 0;
2125
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002126 # Trace the real file/line as we go.
2127 my $realfile = '';
2128 my $realline = 0;
2129 my $realcnt = 0;
2130 my $here = '';
2131 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002132 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002133 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002134 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002135
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002136 my $prev_values = 'E';
2137
2138 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002139 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002140 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002141 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002142 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002143
Joe Perches7e51f192013-09-11 14:23:57 -07002144 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002145
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002146 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002147 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002148 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002149 my @setup_docs = ();
2150 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151
Joe Perchesd8b07712013-11-12 15:10:06 -08002152 my $camelcase_file_seeded = 0;
David Keitel72bf3e82016-03-23 20:52:44 -07002153 my $shorttext = BEFORE_SHORTTEXT;
2154 my $shorttext_exspc = 0;
Joe Perchesd8b07712013-11-12 15:10:06 -08002155
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002157 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002158 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002159
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002160 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002161 $linenr++;
2162 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002163
Joe Perches3705ce52013-07-03 15:05:31 -07002164 push(@fixed, $rawline) if ($fix);
2165
Andy Whitcroft773647a2008-03-28 14:15:58 -07002166 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002167 $setup_docs = 0;
2168 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2169 $setup_docs = 1;
2170 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002171 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002172 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002173 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2174 $realline=$1-1;
2175 if (defined $2) {
2176 $realcnt=$3+1;
2177 } else {
2178 $realcnt=1+1;
2179 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002180 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002181
2182 # Guestimate if this is a continuing comment. Run
2183 # the context looking for a comment "edge". If this
2184 # edge is a close comment then we must be in a comment
2185 # at context start.
2186 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002187 my $cnt = $realcnt;
2188 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2189 next if (defined $rawlines[$ln - 1] &&
2190 $rawlines[$ln - 1] =~ /^-/);
2191 $cnt--;
2192 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002193 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002194 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2195 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2196 ($edge) = $1;
2197 last;
2198 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002199 }
2200 if (defined $edge && $edge eq '*/') {
2201 $in_comment = 1;
2202 }
2203
2204 # Guestimate if this is a continuing comment. If this
2205 # is the start of a diff block and this line starts
2206 # ' *' then it is very likely a comment.
2207 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002208 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002209 {
2210 $in_comment = 1;
2211 }
2212
2213 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2214 sanitise_line_reset($in_comment);
2215
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002216 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002217 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002218 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002219 $line = sanitise_line($rawline);
2220 }
2221 push(@lines, $line);
2222
2223 if ($realcnt > 1) {
2224 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2225 } else {
2226 $realcnt = 0;
2227 }
2228
2229 #print "==>$rawline\n";
2230 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002231
2232 if ($setup_docs && $line =~ /^\+/) {
2233 push(@setup_docs, $line);
2234 }
2235 }
2236
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002237 $prefix = '';
2238
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 $realcnt = 0;
2240 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002241 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002242 foreach my $line (@lines) {
2243 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002244 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002245 my $sline = $line; #copy of $line
2246 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002247
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002248 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002249
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002250#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002251 if (!$in_commit_log &&
2252 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002253 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002254 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002255 $realline=$1-1;
2256 if (defined $2) {
2257 $realcnt=$3+1;
2258 } else {
2259 $realcnt=1+1;
2260 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002261 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002262 $prev_values = 'E';
2263
Andy Whitcroft773647a2008-03-28 14:15:58 -07002264 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002265 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002266 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002267 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002268 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002269
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002270# track the line number as we move through the hunk, note that
2271# new versions of GNU diff omit the leading space on completely
2272# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002273 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002274 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002275 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002276
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002277 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002278 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002279
2280 # Track the previous line.
2281 ($prevline, $stashline) = ($stashline, $line);
2282 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002283 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2284
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002286
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002287 } elsif ($realcnt == 1) {
2288 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002289 }
2290
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002291 my $hunk_line = ($realcnt != 0);
2292
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002293 $here = "#$linenr: " if (!$file);
2294 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002295
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002296 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002297 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002298 if ($line =~ /^diff --git.*?(\S+)$/) {
2299 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002300 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002301 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002302 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002303 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002304 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002305 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002306 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002307
2308 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002309 if (!$file && $tree && $p1_prefix ne '' &&
2310 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002311 WARN("PATCH_PREFIX",
2312 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002313 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002314
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002315 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002316 ERROR("MODIFIED_INCLUDE_ASM",
2317 "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 -07002318 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002319 $found_file = 1;
2320 }
2321
Joe Perches34d88152015-06-25 15:03:05 -07002322#make up the handle for any error we report on this line
2323 if ($showfile) {
2324 $prefix = "$realfile:$realline: "
2325 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002326 if ($file) {
2327 $prefix = "$filename:$realline: ";
2328 } else {
2329 $prefix = "$filename:$linenr: ";
2330 }
Joe Perches34d88152015-06-25 15:03:05 -07002331 }
2332
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002333 if ($found_file) {
Joe Perches7bd7e482015-09-09 15:37:44 -07002334 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002335 $check = 1;
2336 } else {
2337 $check = $check_orig;
2338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002339 next;
2340 }
Randy Dunlap389834b2007-06-08 13:47:03 -07002341 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002343 my $hereline = "$here\n$rawline\n";
2344 my $herecurr = "$here\n$rawline\n";
2345 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002346
David Keitel72bf3e82016-03-23 20:52:44 -07002347 if ($shorttext != AFTER_SHORTTEXT) {
2348 if ($shorttext == IN_SHORTTEXT) {
2349 if ($line=~/^---/ || $line=~/^diff.*/) {
2350 $shorttext = AFTER_SHORTTEXT;
2351 } elsif (length($line) > (SHORTTEXT_LIMIT +
2352 $shorttext_exspc)
2353 && $line !~ /^:([0-7]{6}\s){2}
2354 ([[:xdigit:]]+\.*
2355 \s){2}\w+\s\w+/xms) {
2356 WARN("LONG_COMMIT_TEXT",
2357 "commit text line over " .
2358 SHORTTEXT_LIMIT .
2359 " characters\n" . $herecurr);
2360 }
David Keitele288d232016-03-23 20:57:21 -07002361 } elsif ($shorttext == CHECK_NEXT_SHORTTEXT) {
Gregory Beana386ec22011-03-03 13:46:41 -08002362# The Subject line doesn't have to be the last header in the patch.
2363# Avoid moving to the IN_SHORTTEXT state until clear of all headers.
2364# Per RFC5322, continuation lines must be folded, so any left-justified
2365# text which looks like a header is definitely a header.
2366 if ($line!~/^[\x21-\x39\x3b-\x7e]+:/) {
2367 $shorttext = IN_SHORTTEXT;
David Keitele288d232016-03-23 20:57:21 -07002368# Check for Subject line followed by a blank line.
Gregory Beana386ec22011-03-03 13:46:41 -08002369 if (length($line) != 0) {
2370 WARN("NONBLANK_AFTER_SUMMARY",
2371 "non-blank line after " .
2372 "summary line\n" .
2373 $sublinenr . $here .
2374 "\n" . $subjectline .
2375 "\n" . $line . "\n");
2376 }
David Keitele288d232016-03-23 20:57:21 -07002377 }
2378 } elsif ($line=~/^Subject: \[[^\]]*\] (.*)/) {
2379 $shorttext = CHECK_NEXT_SHORTTEXT;
2380 $subjectline = $line;
2381 $sublinenr = "#$linenr & ";
2382# Check for Subject line less than line limit
David Keitel72bf3e82016-03-23 20:52:44 -07002383 if (length($1) > SHORTTEXT_LIMIT) {
2384 WARN("LONG_SUMMARY_LINE",
2385 "summary line over " .
2386 SHORTTEXT_LIMIT .
2387 " characters\n" . $herecurr);
2388 }
2389 } elsif ($line=~/^ (.*)/) {
2390 $shorttext = IN_SHORTTEXT;
2391 $shorttext_exspc = 4;
2392 if (length($1) > SHORTTEXT_LIMIT) {
2393 WARN("LONG_SUMMARY_LINE",
2394 "summary line over " .
2395 SHORTTEXT_LIMIT .
2396 " characters\n" . $herecurr);
2397 }
2398 }
2399 }
2400
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002401 $cnt_lines++ if ($realcnt != 0);
2402
Joe Perchese518e9a2015-06-25 15:03:27 -07002403# Check if the commit log has what seems like a diff which can confuse patch
2404 if ($in_commit_log && !$commit_log_has_diff &&
2405 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2406 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2407 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2408 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2409 ERROR("DIFF_IN_COMMIT_MSG",
2410 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2411 $commit_log_has_diff = 1;
2412 }
2413
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002414# Check for incorrect file permissions
2415 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2416 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002417 if ($realfile !~ m@scripts/@ &&
2418 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002419 ERROR("EXECUTE_PERMISSIONS",
2420 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002421 }
2422 }
2423
Joe Perches20112472011-07-25 17:13:23 -07002424# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002425 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002426 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002427 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002428 }
2429
Joe Perchese0d975b2014-12-10 15:51:49 -08002430# Check if MAINTAINERS is being updated. If so, there's probably no need to
2431# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2432 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2433 $reported_maintainer_file = 1;
2434 }
2435
Joe Perches20112472011-07-25 17:13:23 -07002436# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002437 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002438 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002439 my $space_before = $1;
2440 my $sign_off = $2;
2441 my $space_after = $3;
2442 my $email = $4;
2443 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2444
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002445 if ($sign_off !~ /$signature_tags/) {
2446 WARN("BAD_SIGN_OFF",
2447 "Non-standard signature: $sign_off\n" . $herecurr);
2448 }
Joe Perches20112472011-07-25 17:13:23 -07002449 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002450 if (WARN("BAD_SIGN_OFF",
2451 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2452 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002453 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002454 "$ucfirst_sign_off $email";
2455 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002456 }
Joe Perches20112472011-07-25 17:13:23 -07002457 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002458 if (WARN("BAD_SIGN_OFF",
2459 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2460 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002461 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002462 "$ucfirst_sign_off $email";
2463 }
2464
Joe Perches20112472011-07-25 17:13:23 -07002465 }
2466 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002467 if (WARN("BAD_SIGN_OFF",
2468 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2469 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002470 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002471 "$ucfirst_sign_off $email";
2472 }
Joe Perches20112472011-07-25 17:13:23 -07002473 }
2474
2475 my ($email_name, $email_address, $comment) = parse_email($email);
2476 my $suggested_email = format_email(($email_name, $email_address));
2477 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002478 ERROR("BAD_SIGN_OFF",
2479 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002480 } else {
2481 my $dequoted = $suggested_email;
2482 $dequoted =~ s/^"//;
2483 $dequoted =~ s/" </ </;
2484 # Don't force email to have quotes
2485 # Allow just an angle bracketed address
2486 if ("$dequoted$comment" ne $email &&
2487 "<$email_address>$comment" ne $email &&
2488 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002489 WARN("BAD_SIGN_OFF",
2490 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002491 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002492 }
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002493 if ($chk_author && $line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002494 WARN("BAD_SIGN_OFF",
2495 "invalid Signed-off-by identity\n" . $line );
2496 }
Joe Perches7e51f192013-09-11 14:23:57 -07002497
2498# Check for duplicate signatures
2499 my $sig_nospace = $line;
2500 $sig_nospace =~ s/\s//g;
2501 $sig_nospace = lc($sig_nospace);
2502 if (defined $signatures{$sig_nospace}) {
2503 WARN("BAD_SIGN_OFF",
2504 "Duplicate signature\n" . $herecurr);
2505 } else {
2506 $signatures{$sig_nospace} = 1;
2507 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002508 }
2509
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002510# Check email subject for common tools that don't need to be mentioned
2511 if ($in_header_lines &&
2512 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2513 WARN("EMAIL_SUBJECT",
2514 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2515 }
2516
Joe Perches9b3189e2014-06-04 16:12:10 -07002517# Check for old stable address
2518 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2519 ERROR("STABLE_ADDRESS",
2520 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2521 }
2522
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002523# Check for unwanted Gerrit info
2524 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2525 ERROR("GERRIT_CHANGE_ID",
2526 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2527 }
2528
Joe Perches369c8dd2015-11-06 16:31:34 -08002529# Check if the commit log is in a possible stack dump
2530 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2531 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2532 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2533 # timestamp
2534 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2535 # stack dump address
2536 $commit_log_possible_stack_dump = 1;
2537 }
2538
Joe Perches2a076f42015-04-16 12:44:28 -07002539# Check for line lengths > 75 in commit log, warn once
2540 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002541 length($line) > 75 &&
2542 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2543 # file delta changes
2544 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2545 # filename then :
2546 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2547 # A Fixes: or Link: line
2548 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002549 WARN("COMMIT_LOG_LONG_LINE",
2550 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2551 $commit_log_long_line = 1;
2552 }
2553
Joe Perchesbf4daf12015-09-09 15:37:50 -07002554# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002555 if ($in_commit_log && $commit_log_possible_stack_dump &&
2556 $line =~ /^\s*$/) {
2557 $commit_log_possible_stack_dump = 0;
2558 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002559
Joe Perches0d7835f2015-02-13 14:38:35 -08002560# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002561 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002562 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002563 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002564 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002565 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2566 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002567 my $init_char = "c";
2568 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002569 my $short = 1;
2570 my $long = 0;
2571 my $case = 1;
2572 my $space = 1;
2573 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002574 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002575 my $id = '0123456789ab';
2576 my $orig_desc = "commit description";
2577 my $description = "";
2578
Joe Perchesfe043ea2015-09-09 15:37:25 -07002579 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2580 $init_char = $1;
2581 $orig_commit = lc($2);
2582 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2583 $orig_commit = lc($1);
2584 }
2585
Joe Perches0d7835f2015-02-13 14:38:35 -08002586 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2587 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2588 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2589 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2590 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2591 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002592 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002593 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2594 defined $rawlines[$linenr] &&
2595 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2596 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002597 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002598 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2599 defined $rawlines[$linenr] &&
2600 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2601 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2602 $orig_desc = $1;
2603 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2604 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002605 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002606 }
2607
2608 ($id, $description) = git_commit_info($orig_commit,
2609 $id, $orig_desc);
2610
Joe Perches19c146a2015-02-13 14:39:00 -08002611 if ($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))))) {
2623 $reported_maintainer_file = 1;
2624 WARN("FILE_PATH_CHANGES",
2625 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2626 }
2627
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002628#check the patch for invalid author credentials
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002629 if ($chk_author && $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002630 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2631 }
2632
Andy Whitcroft00df3442007-06-08 13:47:06 -07002633# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002634 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002635 ERROR("CORRUPTED_PATCH",
2636 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002637 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002638 }
2639
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002640# Check for absolute kernel paths.
2641 if ($tree) {
2642 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2643 my $file = $1;
2644
2645 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2646 check_absolute_file($1, $herecurr)) {
2647 #
2648 } else {
2649 check_absolute_file($file, $herecurr);
2650 }
2651 }
2652 }
2653
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002654# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2655 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002656 $rawline !~ m/^$UTF8*$/) {
2657 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2658
2659 my $blank = copy_spacing($rawline);
2660 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2661 my $hereptr = "$hereline$ptr\n";
2662
Joe Perches34d99212011-07-25 17:13:26 -07002663 CHK("INVALID_UTF8",
2664 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002665 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002666
Joe Perches15662b32011-10-31 17:13:12 -07002667# Check if it's the start of a commit log
2668# (not a header line and we haven't seen the patch filename)
2669 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002670 !($rawline =~ /^\s+\S/ ||
2671 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002672 $in_header_lines = 0;
2673 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002674 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002675 }
2676
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002677# Check if there is UTF-8 in a commit log when a mail header has explicitly
2678# declined it, i.e defined some charset where it is missing.
2679 if ($in_header_lines &&
2680 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2681 $1 !~ /utf-8/i) {
2682 $non_utf8_charset = 1;
2683 }
2684
2685 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002686 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002687 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002688 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2689 }
2690
Kees Cook66b47b42014-10-13 15:51:57 -07002691# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002692 if (defined($misspellings) &&
2693 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002694 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002695 my $typo = $1;
2696 my $typo_fix = $spelling_fix{lc($typo)};
2697 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2698 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2699 my $msg_type = \&WARN;
2700 $msg_type = \&CHK if ($file);
2701 if (&{$msg_type}("TYPO_SPELLING",
2702 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2703 $fix) {
2704 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2705 }
2706 }
2707 }
2708
Andy Whitcroft306708542008-10-15 22:02:28 -07002709# ignore non-hunk lines and lines being removed
2710 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002711
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002712#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002713 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002714 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002715 if (ERROR("DOS_LINE_ENDINGS",
2716 "DOS line endings\n" . $herevet) &&
2717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002718 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002719 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002720 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2721 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002722 if (ERROR("TRAILING_WHITESPACE",
2723 "trailing whitespace\n" . $herevet) &&
2724 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002725 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002726 }
2727
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002728 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002729 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002730
Josh Triplett4783f892013-11-12 15:10:12 -08002731# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002732 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002733 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2734 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002735 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2736 my $msg_type = \&ERROR;
2737 $msg_type = \&CHK if ($file);
2738 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002739 "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 -08002740 }
2741
Andi Kleen33549572010-05-24 14:33:29 -07002742# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002743# Only applies when adding the entry originally, after that we do not have
2744# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002745 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002746 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002747 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002748 my $cnt = $realcnt;
2749 my $ln = $linenr + 1;
2750 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002751 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002752 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002753 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002754 $f = $lines[$ln - 1];
2755 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2756 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002757
2758 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002759 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002760
Joe Perches8d73e0e2014-08-06 16:10:46 -07002761 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002762 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002763 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002764 $length = -1;
2765 }
2766
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002767 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002768 $f =~ s/#.*//;
2769 $f =~ s/^\s+//;
2770 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002771 if ($f =~ /^\s*config\s/) {
2772 $is_end = 1;
2773 last;
2774 }
Andi Kleen33549572010-05-24 14:33:29 -07002775 $length++;
2776 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002777 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2778 WARN("CONFIG_DESCRIPTION",
2779 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2780 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002781 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002782 }
2783
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002784# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2785 if ($realfile =~ /Kconfig/ &&
2786 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2787 WARN("CONFIG_EXPERIMENTAL",
2788 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2789 }
2790
Christoph Jaeger327953e2015-02-13 14:38:29 -08002791# discourage the use of boolean for type definition attributes of Kconfig options
2792 if ($realfile =~ /Kconfig/ &&
2793 $line =~ /^\+\s*\bboolean\b/) {
2794 WARN("CONFIG_TYPE_BOOLEAN",
2795 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2796 }
2797
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002798 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2799 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2800 my $flag = $1;
2801 my $replacement = {
2802 'EXTRA_AFLAGS' => 'asflags-y',
2803 'EXTRA_CFLAGS' => 'ccflags-y',
2804 'EXTRA_CPPFLAGS' => 'cppflags-y',
2805 'EXTRA_LDFLAGS' => 'ldflags-y',
2806 };
2807
2808 WARN("DEPRECATED_VARIABLE",
2809 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2810 }
2811
Rob Herringbff5da42014-01-23 15:54:51 -08002812# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002813 if (defined $root &&
2814 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2815 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2816
Rob Herringbff5da42014-01-23 15:54:51 -08002817 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2818
Florian Vaussardcc933192014-04-03 14:49:27 -07002819 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2820 my $vp_file = $dt_path . "vendor-prefixes.txt";
2821
Rob Herringbff5da42014-01-23 15:54:51 -08002822 foreach my $compat (@compats) {
2823 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002824 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2825 my $compat3 = $compat;
2826 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2827 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002828 if ( $? >> 8 ) {
2829 WARN("UNDOCUMENTED_DT_STRING",
2830 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2831 }
2832
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002833 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2834 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002835 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002836 if ( $? >> 8 ) {
2837 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002838 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002839 }
2840 }
2841 }
2842
Andy Whitcroft5368df22008-10-15 22:02:27 -07002843# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002844 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002845
Joe Perches47e0c882015-06-25 15:02:57 -07002846# line length limit (with some exclusions)
2847#
2848# There are a few types of lines that may extend beyond $max_line_length:
2849# logging functions like pr_info that end in a string
2850# lines with a single string
2851# #defines that are a single string
2852#
2853# There are 3 different line length message types:
2854# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2855# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2856# LONG_LINE all other lines longer than $max_line_length
2857#
2858# if LONG_LINE is ignored, the other 2 types are also ignored
2859#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002860 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002861 my $msg_type = "LONG_LINE";
2862
2863 # Check the allowed long line types first
2864
2865 # logging functions that end in a string that starts
2866 # before $max_line_length
2867 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2868 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2869 $msg_type = "";
2870
2871 # lines with only strings (w/ possible termination)
2872 # #defines with only strings
2873 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2874 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2875 $msg_type = "";
2876
Joe Perchesd560a5f2016-08-02 14:04:31 -07002877 # EFI_GUID is another special case
2878 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2879 $msg_type = "";
2880
Joe Perches47e0c882015-06-25 15:02:57 -07002881 # Otherwise set the alternate message types
2882
2883 # a comment starts before $max_line_length
2884 } elsif ($line =~ /($;[\s$;]*)$/ &&
2885 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2886 $msg_type = "LONG_LINE_COMMENT"
2887
2888 # a quoted string starts before $max_line_length
2889 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2890 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2891 $msg_type = "LONG_LINE_STRING"
2892 }
2893
2894 if ($msg_type ne "" &&
2895 (show_type("LONG_LINE") || show_type($msg_type))) {
2896 WARN($msg_type,
2897 "line over $max_line_length characters\n" . $herecurr);
2898 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002899 }
2900
Andy Whitcroft8905a672007-11-28 16:21:06 -08002901# check for adding lines without a newline.
2902 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002903 WARN("MISSING_EOF_NEWLINE",
2904 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002905 }
2906
Mike Frysinger42e41c52009-09-21 17:04:40 -07002907# Blackfin: use hi/lo macros
2908 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2909 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2910 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002911 ERROR("LO_MACRO",
2912 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002913 }
2914 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2915 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002916 ERROR("HI_MACRO",
2917 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002918 }
2919 }
2920
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002921# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002922 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002923
2924# at the beginning of a line any tabs must come first and anything
2925# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002926 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2927 $rawline =~ /^\+\s* \s*/) {
2928 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002929 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002930 if (ERROR("CODE_INDENT",
2931 "code indent should use tabs where possible\n" . $herevet) &&
2932 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002933 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002934 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002935 }
2936
Alberto Panizzo08e44362010-03-05 13:43:54 -08002937# check for space before tabs.
2938 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2939 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002940 if (WARN("SPACE_BEFORE_TAB",
2941 "please, no space before tabs\n" . $herevet) &&
2942 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002943 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002944 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002945 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002946 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002947 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002948 }
2949
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002950# check for && or || at the start of a line
2951 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2952 CHK("LOGICAL_CONTINUATIONS",
2953 "Logical continuations should be on the previous line\n" . $hereprev);
2954 }
2955
Joe Perchesa91e8992016-05-20 17:04:05 -07002956# check indentation starts on a tab stop
2957 if ($^V && $^V ge 5.10.0 &&
2958 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2959 my $indent = length($1);
2960 if ($indent % 8) {
2961 if (WARN("TABSTOP",
2962 "Statements should start on a tabstop\n" . $herecurr) &&
2963 $fix) {
2964 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2965 }
2966 }
2967 }
2968
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002969# check multi-line statement indentation matches previous line
2970 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002971 $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 -07002972 $prevline =~ /^\+(\t*)(.*)$/;
2973 my $oldindent = $1;
2974 my $rest = $2;
2975
2976 my $pos = pos_last_openparen($rest);
2977 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002978 $line =~ /^(\+| )([ \t]*)/;
2979 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002980
2981 my $goodtabindent = $oldindent .
2982 "\t" x ($pos / 8) .
2983 " " x ($pos % 8);
2984 my $goodspaceindent = $oldindent . " " x $pos;
2985
2986 if ($newindent ne $goodtabindent &&
2987 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002988
2989 if (CHK("PARENTHESIS_ALIGNMENT",
2990 "Alignment should match open parenthesis\n" . $hereprev) &&
2991 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002992 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002993 s/^\+[ \t]*/\+$goodtabindent/;
2994 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002995 }
2996 }
2997 }
2998
Joe Perches6ab3a972015-04-16 12:44:05 -07002999# check for space after cast like "(int) foo" or "(struct foo) bar"
3000# avoid checking a few false positives:
3001# "sizeof(<type>)" or "__alignof__(<type>)"
3002# function pointer declarations like "(*foo)(int) = bar;"
3003# structure definitions like "(struct foo) { 0 };"
3004# multiline macros that define functions
3005# known attributes or the __attribute__ keyword
3006 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3007 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003008 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003009 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003010 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003011 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003012 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003013 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003014 }
3015
Joe Perches86406b12015-09-09 15:37:41 -07003016# Block comment styles
3017# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003018 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003019 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003020 $rawline =~ /^\+[ \t]*\*/ &&
3021 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003022 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3023 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3024 }
3025
Joe Perches86406b12015-09-09 15:37:41 -07003026# Block comments use * on subsequent lines
3027 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3028 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003029 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003030 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003031 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003032 WARN("BLOCK_COMMENT_STYLE",
3033 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003034 }
3035
Joe Perches86406b12015-09-09 15:37:41 -07003036# Block comments use */ on trailing lines
3037 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003038 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3039 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3040 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003041 WARN("BLOCK_COMMENT_STYLE",
3042 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003043 }
3044
Joe Perches7f619192014-08-06 16:10:39 -07003045# check for missing blank lines after struct/union declarations
3046# with exceptions for various attributes and macros
3047 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3048 $line =~ /^\+/ &&
3049 !($line =~ /^\+\s*$/ ||
3050 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3051 $line =~ /^\+\s*MODULE_/i ||
3052 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3053 $line =~ /^\+[a-z_]*init/ ||
3054 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3055 $line =~ /^\+\s*DECLARE/ ||
3056 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003057 if (CHK("LINE_SPACING",
3058 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3059 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003060 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003061 }
Joe Perches7f619192014-08-06 16:10:39 -07003062 }
3063
Joe Perches365dd4e2014-08-06 16:10:42 -07003064# check for multiple consecutive blank lines
3065 if ($prevline =~ /^[\+ ]\s*$/ &&
3066 $line =~ /^\+\s*$/ &&
3067 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003068 if (CHK("LINE_SPACING",
3069 "Please don't use multiple blank lines\n" . $hereprev) &&
3070 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003071 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003072 }
3073
Joe Perches365dd4e2014-08-06 16:10:42 -07003074 $last_blank_line = $linenr;
3075 }
3076
Joe Perches3b617e32014-04-03 14:49:28 -07003077# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003078 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3079 # actual declarations
3080 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003081 # function pointer declarations
3082 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003083 # foo bar; where foo is some local typedef or #define
3084 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3085 # known declaration macros
3086 $prevline =~ /^\+\s+$declaration_macros/) &&
3087 # for "else if" which can look like "$Ident $Ident"
3088 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3089 # other possible extensions of declaration lines
3090 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3091 # not starting a section or a macro "\" extended line
3092 $prevline =~ /(?:\{\s*|\\)$/) &&
3093 # looks like a declaration
3094 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003095 # function pointer declarations
3096 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003097 # foo bar; where foo is some local typedef or #define
3098 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3099 # known declaration macros
3100 $sline =~ /^\+\s+$declaration_macros/ ||
3101 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003102 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003103 # start or end of block or continuation of declaration
3104 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3105 # bitfield continuation
3106 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3107 # other possible extensions of declaration lines
3108 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3109 # indentation of previous and current line are the same
3110 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003111 if (WARN("LINE_SPACING",
3112 "Missing a blank line after declarations\n" . $hereprev) &&
3113 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003114 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003115 }
Joe Perches3b617e32014-04-03 14:49:28 -07003116 }
3117
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003118# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003119# Exceptions:
3120# 1) within comments
3121# 2) indented preprocessor commands
3122# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003123 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003124 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003125 if (WARN("LEADING_SPACE",
3126 "please, no spaces at the start of a line\n" . $herevet) &&
3127 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003128 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003129 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003130 }
3131
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003132# check we are in a valid C source file if not then ignore this hunk
3133 next if ($realfile !~ /\.(h|c)$/);
3134
Joe Perches032a4c02014-08-06 16:10:29 -07003135# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003136# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003137# if the previous line is a break or return and is indented 1 tab more...
3138 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3139 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003140 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3141 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3142 defined $lines[$linenr] &&
3143 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003144 WARN("UNNECESSARY_ELSE",
3145 "else is not generally useful after a break or return\n" . $hereprev);
3146 }
3147 }
3148
Joe Perchesc00df192014-08-06 16:11:01 -07003149# check indentation of a line with a break;
3150# if the previous line is a goto or return and is indented the same # of tabs
3151 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3152 my $tabs = $1;
3153 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3154 WARN("UNNECESSARY_BREAK",
3155 "break is not useful after a goto or return\n" . $hereprev);
3156 }
3157 }
3158
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003159# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3160 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3161 WARN("CONFIG_EXPERIMENTAL",
3162 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3163 }
3164
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003165# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003166 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003167 WARN("CVS_KEYWORD",
3168 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003169 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003170
Mike Frysinger42e41c52009-09-21 17:04:40 -07003171# Blackfin: don't use __builtin_bfin_[cs]sync
3172 if ($line =~ /__builtin_bfin_csync/) {
3173 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003174 ERROR("CSYNC",
3175 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003176 }
3177 if ($line =~ /__builtin_bfin_ssync/) {
3178 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003179 ERROR("SSYNC",
3180 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003181 }
3182
Joe Perches56e77d72013-02-21 16:44:14 -08003183# check for old HOTPLUG __dev<foo> section markings
3184 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3185 WARN("HOTPLUG_SECTION",
3186 "Using $1 is unnecessary\n" . $herecurr);
3187 }
3188
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003189# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003190 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3191 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003192#print "LINE<$line>\n";
3193 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003194 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003195 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003196 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003197 $stat =~ s/\n./\n /g;
3198 $cond =~ s/\n./\n /g;
3199
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003200#print "linenr<$linenr> <$stat>\n";
3201 # If this statement has no statement boundaries within
3202 # it there is no point in retrying a statement scan
3203 # until we hit end of it.
3204 my $frag = $stat; $frag =~ s/;+\s*$//;
3205 if ($frag !~ /(?:{|;)/) {
3206#print "skip<$line_nr_next>\n";
3207 $suppress_statement = $line_nr_next;
3208 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003209
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003210 # Find the real next line.
3211 $realline_next = $line_nr_next;
3212 if (defined $realline_next &&
3213 (!defined $lines[$realline_next - 1] ||
3214 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3215 $realline_next++;
3216 }
3217
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003218 my $s = $stat;
3219 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003220
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003221 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003222 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003223
3224 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003225 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003226
Andy Whitcroft463f2862009-09-21 17:04:34 -07003227 } elsif ($s =~ /^.\s*else\b/s) {
3228
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003229 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003230 } 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 -07003231 my $type = $1;
3232 $type =~ s/\s+/ /g;
3233 possible($type, "A:" . $s);
3234
Andy Whitcroft8905a672007-11-28 16:21:06 -08003235 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003236 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003237 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003238 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003239
3240 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003241 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003242 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003243 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003244
3245 # Check for any sort of function declaration.
3246 # int foo(something bar, other baz);
3247 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003248 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 -08003249 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003250
Andy Whitcroftcf655042008-03-04 14:28:20 -08003251 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003252 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003253 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003254
Andy Whitcroft8905a672007-11-28 16:21:06 -08003255 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003256 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003257
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003258 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003259 }
3260 }
3261 }
3262
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003263 }
3264
Andy Whitcroft653d4872007-06-23 17:16:34 -07003265#
3266# Checks which may be anchored in the context.
3267#
3268
3269# Check for switch () and associated case and default
3270# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003271 if ($line=~/\bswitch\s*\(.*\)/) {
3272 my $err = '';
3273 my $sep = '';
3274 my @ctx = ctx_block_outer($linenr, $realcnt);
3275 shift(@ctx);
3276 for my $ctx (@ctx) {
3277 my ($clen, $cindent) = line_stats($ctx);
3278 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3279 $indent != $cindent) {
3280 $err .= "$sep$ctx\n";
3281 $sep = '';
3282 } else {
3283 $sep = "[...]\n";
3284 }
3285 }
3286 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003287 ERROR("SWITCH_CASE_INDENT_LEVEL",
3288 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003289 }
3290 }
3291
3292# if/while/etc brace do not go on next line, unless defining a do while loop,
3293# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003294 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 -07003295 my $pre_ctx = "$1$2";
3296
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003297 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003298
3299 if ($line =~ /^\+\t{6,}/) {
3300 WARN("DEEP_INDENTATION",
3301 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3302 }
3303
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003304 my $ctx_cnt = $realcnt - $#ctx - 1;
3305 my $ctx = join("\n", @ctx);
3306
Andy Whitcroft548596d2008-07-23 21:29:01 -07003307 my $ctx_ln = $linenr;
3308 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003309
Andy Whitcroft548596d2008-07-23 21:29:01 -07003310 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3311 defined $lines[$ctx_ln - 1] &&
3312 $lines[$ctx_ln - 1] =~ /^-/)) {
3313 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3314 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003315 $ctx_ln++;
3316 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003317
Andy Whitcroft53210162008-07-23 21:29:03 -07003318 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3319 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003320
Joe Perchesd752fcc2014-08-06 16:11:05 -07003321 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003322 ERROR("OPEN_BRACE",
3323 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003324 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003325 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003326 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3327 $ctx =~ /\)\s*\;\s*$/ &&
3328 defined $lines[$ctx_ln - 1])
3329 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003330 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3331 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003332 WARN("TRAILING_SEMICOLON",
3333 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003334 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003335 }
3336 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003337 }
3338
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003339# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003340 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003341 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3342 ctx_statement_block($linenr, $realcnt, 0)
3343 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003344 my ($s, $c) = ($stat, $cond);
3345
3346 substr($s, 0, length($c), '');
3347
Joe Perches9f5af482015-09-09 15:37:30 -07003348 # remove inline comments
3349 $s =~ s/$;/ /g;
3350 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003351
3352 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003353 my @newlines = ($c =~ /\n/gs);
3354 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003355
Joe Perches9f5af482015-09-09 15:37:30 -07003356 # Make sure we remove the line prefixes as we have
3357 # none on the first line, and are going to readd them
3358 # where necessary.
3359 $s =~ s/\n./\n/gs;
3360 while ($s =~ /\n\s+\\\n/) {
3361 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3362 }
3363
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003364 # We want to check the first line inside the block
3365 # starting at the end of the conditional, so remove:
3366 # 1) any blank line termination
3367 # 2) any opening brace { on end of the line
3368 # 3) any do (...) {
3369 my $continuation = 0;
3370 my $check = 0;
3371 $s =~ s/^.*\bdo\b//;
3372 $s =~ s/^\s*{//;
3373 if ($s =~ s/^\s*\\//) {
3374 $continuation = 1;
3375 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003376 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003377 $check = 1;
3378 $cond_lines++;
3379 }
3380
3381 # Also ignore a loop construct at the end of a
3382 # preprocessor statement.
3383 if (($prevline =~ /^.\s*#\s*define\s/ ||
3384 $prevline =~ /\\\s*$/) && $continuation == 0) {
3385 $check = 0;
3386 }
3387
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003388 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003389 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003390 while ($cond_ptr != $cond_lines) {
3391 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003392
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003393 # If we see an #else/#elif then the code
3394 # is not linear.
3395 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3396 $check = 0;
3397 }
3398
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003399 # Ignore:
3400 # 1) blank lines, they should be at 0,
3401 # 2) preprocessor lines, and
3402 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003403 if ($continuation ||
3404 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003405 $s =~ /^\s*#\s*?/ ||
3406 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003407 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003408 if ($s =~ s/^.*?\n//) {
3409 $cond_lines++;
3410 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003411 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003412 }
3413
3414 my (undef, $sindent) = line_stats("+" . $s);
3415 my $stat_real = raw_line($linenr, $cond_lines);
3416
3417 # Check if either of these lines are modified, else
3418 # this is not this patch's fault.
3419 if (!defined($stat_real) ||
3420 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3421 $check = 0;
3422 }
3423 if (defined($stat_real) && $cond_lines > 1) {
3424 $stat_real = "[...]\n$stat_real";
3425 }
3426
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003427 #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 -07003428
Joe Perches9f5af482015-09-09 15:37:30 -07003429 if ($check && $s ne '' &&
3430 (($sindent % 8) != 0 ||
3431 ($sindent < $indent) ||
3432 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003433 WARN("SUSPECT_CODE_INDENT",
3434 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003435 }
3436 }
3437
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003438 # Track the 'values' across context and added lines.
3439 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003440 my ($curr_values, $curr_vars) =
3441 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003442 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003443 if ($dbg_values) {
3444 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003445 print "$linenr > .$outline\n";
3446 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003447 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003448 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003449 $prev_values = substr($curr_values, -1);
3450
Andy Whitcroft00df3442007-06-08 13:47:06 -07003451#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003452 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003453
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003454# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003455 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 -07003456 my $type = $1;
3457 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003458 $var = "" if (!defined $var);
3459 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003460 my $sign = $1;
3461 my $pointer = $2;
3462
3463 $pointer = "" if (!defined $pointer);
3464
3465 if (WARN("UNSPECIFIED_INT",
3466 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3467 $fix) {
3468 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003469 my $comp_pointer = $pointer;
3470 $comp_pointer =~ s/\s//g;
3471 $decl .= $comp_pointer;
3472 $decl = rtrim($decl) if ($var eq "");
3473 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003474 }
3475 }
3476 }
3477
Andy Whitcroft653d4872007-06-23 17:16:34 -07003478# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003479 if ($dbg_type) {
3480 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003481 ERROR("TEST_TYPE",
3482 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003483 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003484 ERROR("TEST_NOT_TYPE",
3485 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003486 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003487 next;
3488 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003489# TEST: allow direct testing of the attribute matcher.
3490 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003491 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003492 ERROR("TEST_ATTR",
3493 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003494 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003495 ERROR("TEST_NOT_ATTR",
3496 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003497 }
3498 next;
3499 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003500
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003501# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003502 if ($line =~ /^.\s*{/ &&
3503 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003504 if (ERROR("OPEN_BRACE",
3505 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003506 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3507 fix_delete_line($fixlinenr - 1, $prevrawline);
3508 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003509 my $fixedline = $prevrawline;
3510 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003511 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003512 $fixedline = $line;
3513 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003514 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003515 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003516 }
3517
Andy Whitcroft653d4872007-06-23 17:16:34 -07003518#
3519# Checks which are anchored on the added line.
3520#
3521
3522# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003523 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003524 my $path = $1;
3525 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003526 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003527 "malformed #include filename\n" . $herecurr);
3528 }
3529 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3530 ERROR("UAPI_INCLUDE",
3531 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003532 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003533 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003534
3535# no C99 // comments
3536 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003537 if (ERROR("C99_COMMENTS",
3538 "do not use C99 // comments\n" . $herecurr) &&
3539 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003540 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003541 if ($line =~ /\/\/(.*)$/) {
3542 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003543 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003544 }
3545 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003546 }
3547 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003548 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003549 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003550
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003551# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3552# the whole statement.
3553#print "APW <$lines[$realline_next - 1]>\n";
3554 if (defined $realline_next &&
3555 exists $lines[$realline_next - 1] &&
3556 !defined $suppress_export{$realline_next} &&
3557 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3558 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003559 # Handle definitions which produce identifiers with
3560 # a prefix:
3561 # XXX(foo);
3562 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003563 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003564 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003565 $name =~ /^${Ident}_$2/) {
3566#print "FOO C name<$name>\n";
3567 $suppress_export{$realline_next} = 1;
3568
3569 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003570 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003571 ^.DEFINE_$Ident\(\Q$name\E\)|
3572 ^.DECLARE_$Ident\(\Q$name\E\)|
3573 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003574 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3575 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003576 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003577#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3578 $suppress_export{$realline_next} = 2;
3579 } else {
3580 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003581 }
3582 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003583 if (!defined $suppress_export{$linenr} &&
3584 $prevline =~ /^.\s*$/ &&
3585 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3586 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3587#print "FOO B <$lines[$linenr - 1]>\n";
3588 $suppress_export{$linenr} = 2;
3589 }
3590 if (defined $suppress_export{$linenr} &&
3591 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003592 WARN("EXPORT_SYMBOL",
3593 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003594 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003595
Joe Eloff5150bda2010-08-09 17:21:00 -07003596# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003597 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003598 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003599 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003600 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003601 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003602 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003603 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003604# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003605 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003606 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003607 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003608 $herecurr) &&
3609 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003610 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003611 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003612 }
3613
Joe Perches18130872014-08-06 16:11:22 -07003614# check for misordered declarations of char/short/int/long with signed/unsigned
3615 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3616 my $tmp = trim($1);
3617 WARN("MISORDERED_TYPE",
3618 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3619 }
3620
Joe Perchescb710ec2010-10-26 14:23:20 -07003621# check for static const char * arrays.
3622 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003623 WARN("STATIC_CONST_CHAR_ARRAY",
3624 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003625 $herecurr);
3626 }
3627
3628# check for static char foo[] = "bar" declarations.
3629 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003630 WARN("STATIC_CONST_CHAR_ARRAY",
3631 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003632 $herecurr);
3633 }
3634
Joe Perchesab7e23f2015-04-16 12:44:22 -07003635# check for const <foo> const where <foo> is not a pointer or array type
3636 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3637 my $found = $1;
3638 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3639 WARN("CONST_CONST",
3640 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3641 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3642 WARN("CONST_CONST",
3643 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3644 }
3645 }
3646
Joe Perches9b0fa602014-04-03 14:49:18 -07003647# check for non-global char *foo[] = {"bar", ...} declarations.
3648 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3649 WARN("STATIC_CONST_CHAR_ARRAY",
3650 "char * array declaration might be better as static const\n" .
3651 $herecurr);
3652 }
3653
Joe Perchesb598b672015-04-16 12:44:36 -07003654# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3655 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3656 my $array = $1;
3657 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3658 my $array_div = $1;
3659 if (WARN("ARRAY_SIZE",
3660 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3661 $fix) {
3662 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3663 }
3664 }
3665 }
3666
Joe Perchesb36190c2014-01-27 17:07:18 -08003667# check for function declarations without arguments like "int foo()"
3668 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3669 if (ERROR("FUNCTION_WITHOUT_ARGS",
3670 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3671 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003672 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003673 }
3674 }
3675
Joe Perches92e112f2013-12-13 11:36:22 -07003676# check for uses of DEFINE_PCI_DEVICE_TABLE
3677 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3678 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3679 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3680 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003681 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
Joe Perches92e112f2013-12-13 11:36:22 -07003682 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003683 }
3684
Andy Whitcroft653d4872007-06-23 17:16:34 -07003685# check for new typedefs, only function parameters and sparse annotations
3686# make sense.
3687 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003688 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003689 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003690 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003691 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003692 WARN("NEW_TYPEDEFS",
3693 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003694 }
3695
3696# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003697 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003698 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3699 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003700 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003701
Andy Whitcroft65863862009-01-06 14:41:21 -08003702 # Should start with a space.
3703 $to =~ s/^(\S)/ $1/;
3704 # Should not end with a space.
3705 $to =~ s/\s+$//;
3706 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003707 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003708 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003709
Joe Perches3705ce52013-07-03 15:05:31 -07003710## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003711 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003712 if (ERROR("POINTER_LOCATION",
3713 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3714 $fix) {
3715 my $sub_from = $ident;
3716 my $sub_to = $ident;
3717 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003718 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003719 s@\Q$sub_from\E@$sub_to@;
3720 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003721 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003722 }
3723 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3724 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003725 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003726
Andy Whitcroft65863862009-01-06 14:41:21 -08003727 # Should start with a space.
3728 $to =~ s/^(\S)/ $1/;
3729 # Should not end with a space.
3730 $to =~ s/\s+$//;
3731 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003732 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003733 }
3734 # Modifiers should have spaces.
3735 $to =~ s/(\b$Modifier$)/$1 /;
3736
Joe Perches3705ce52013-07-03 15:05:31 -07003737## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003738 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003739 if (ERROR("POINTER_LOCATION",
3740 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3741 $fix) {
3742
3743 my $sub_from = $match;
3744 my $sub_to = $match;
3745 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003746 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003747 s@\Q$sub_from\E@$sub_to@;
3748 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003749 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003750 }
3751
Joe Perches9d3e3c72015-09-09 15:37:27 -07003752# avoid BUG() or BUG_ON()
3753 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3754 my $msg_type = \&WARN;
3755 $msg_type = \&CHK if ($file);
3756 &{$msg_type}("AVOID_BUG",
3757 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3758 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003759
Joe Perches9d3e3c72015-09-09 15:37:27 -07003760# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003761 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003762 WARN("LINUX_VERSION_CODE",
3763 "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 -08003764 }
3765
Joe Perches17441222011-06-15 15:08:17 -07003766# check for uses of printk_ratelimit
3767 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003768 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003769 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003770 }
3771
Andy Whitcroft00df3442007-06-08 13:47:06 -07003772# printk should use KERN_* levels. Note that follow on printk's on the
3773# same line do not need a level, so we use the current block context
3774# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003775# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003776# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003777 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003778 my $ok = 0;
3779 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3780 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003781 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003782 # with "\n" ignore it, else it is to blame
3783 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3784 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3785 $ok = 1;
3786 }
3787 last;
3788 }
3789 }
3790 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003791 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3792 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003793 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003794 }
3795
Joe Perches243f3802012-05-31 16:26:09 -07003796 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3797 my $orig = $1;
3798 my $level = lc($orig);
3799 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003800 my $level2 = $level;
3801 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003802 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003803 "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 -07003804 }
3805
3806 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003807 if (WARN("PREFER_PR_LEVEL",
3808 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3809 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003810 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003811 s/\bpr_warning\b/pr_warn/;
3812 }
Joe Perches243f3802012-05-31 16:26:09 -07003813 }
3814
Joe Perchesdc139312013-02-21 16:44:13 -08003815 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3816 my $orig = $1;
3817 my $level = lc($orig);
3818 $level = "warn" if ($level eq "warning");
3819 $level = "dbg" if ($level eq "debug");
3820 WARN("PREFER_DEV_LEVEL",
3821 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3822 }
3823
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003824# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3825# number of false positives, but assembly files are not checked, so at
3826# least the arch entry code will not trigger this warning.
3827 if ($line =~ /\bENOSYS\b/) {
3828 WARN("ENOSYS",
3829 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3830 }
3831
Andy Whitcroft653d4872007-06-23 17:16:34 -07003832# function brace can't be on same line, except for #defines of do while,
3833# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003834 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003835 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003836 if (ERROR("OPEN_BRACE",
3837 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3838 $fix) {
3839 fix_delete_line($fixlinenr, $rawline);
3840 my $fixed_line = $rawline;
3841 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3842 my $line1 = $1;
3843 my $line2 = $2;
3844 fix_insert_line($fixlinenr, ltrim($line1));
3845 fix_insert_line($fixlinenr, "\+{");
3846 if ($line2 !~ /^\s*$/) {
3847 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3848 }
3849 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003850 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003851
Andy Whitcroft8905a672007-11-28 16:21:06 -08003852# open braces for enum, union and struct go on the same line.
3853 if ($line =~ /^.\s*{/ &&
3854 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003855 if (ERROR("OPEN_BRACE",
3856 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3857 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3858 fix_delete_line($fixlinenr - 1, $prevrawline);
3859 fix_delete_line($fixlinenr, $rawline);
3860 my $fixedline = rtrim($prevrawline) . " {";
3861 fix_insert_line($fixlinenr, $fixedline);
3862 $fixedline = $rawline;
3863 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3864 if ($fixedline !~ /^\+\s*$/) {
3865 fix_insert_line($fixlinenr, $fixedline);
3866 }
3867 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003868 }
3869
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003870# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003871 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3872 if (WARN("SPACING",
3873 "missing space after $1 definition\n" . $herecurr) &&
3874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003875 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003876 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3877 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003878 }
3879
Joe Perches31070b52014-01-23 15:54:49 -08003880# Function pointer declarations
3881# check spacing between type, funcptr, and args
3882# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003883 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003884 my $declare = $1;
3885 my $pre_pointer_space = $2;
3886 my $post_pointer_space = $3;
3887 my $funcname = $4;
3888 my $post_funcname_space = $5;
3889 my $pre_args_space = $6;
3890
Joe Perches91f72e92014-04-03 14:49:12 -07003891# the $Declare variable will capture all spaces after the type
3892# so check it for a missing trailing missing space but pointer return types
3893# don't need a space so don't warn for those.
3894 my $post_declare_space = "";
3895 if ($declare =~ /(\s+)$/) {
3896 $post_declare_space = $1;
3897 $declare = rtrim($declare);
3898 }
3899 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003900 WARN("SPACING",
3901 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003902 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003903 }
3904
3905# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003906# This test is not currently implemented because these declarations are
3907# equivalent to
3908# int foo(int bar, ...)
3909# and this is form shouldn't/doesn't generate a checkpatch warning.
3910#
3911# elsif ($declare =~ /\s{2,}$/) {
3912# WARN("SPACING",
3913# "Multiple spaces after return type\n" . $herecurr);
3914# }
Joe Perches31070b52014-01-23 15:54:49 -08003915
3916# unnecessary space "type ( *funcptr)(args...)"
3917 if (defined $pre_pointer_space &&
3918 $pre_pointer_space =~ /^\s/) {
3919 WARN("SPACING",
3920 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3921 }
3922
3923# unnecessary space "type (* funcptr)(args...)"
3924 if (defined $post_pointer_space &&
3925 $post_pointer_space =~ /^\s/) {
3926 WARN("SPACING",
3927 "Unnecessary space before function pointer name\n" . $herecurr);
3928 }
3929
3930# unnecessary space "type (*funcptr )(args...)"
3931 if (defined $post_funcname_space &&
3932 $post_funcname_space =~ /^\s/) {
3933 WARN("SPACING",
3934 "Unnecessary space after function pointer name\n" . $herecurr);
3935 }
3936
3937# unnecessary space "type (*funcptr) (args...)"
3938 if (defined $pre_args_space &&
3939 $pre_args_space =~ /^\s/) {
3940 WARN("SPACING",
3941 "Unnecessary space before function pointer arguments\n" . $herecurr);
3942 }
3943
3944 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003945 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003946 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003947 }
3948 }
3949
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003950# check for spacing round square brackets; allowed:
3951# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003952# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3953# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003954 while ($line =~ /(.*?\s)\[/g) {
3955 my ($where, $prefix) = ($-[1], $1);
3956 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003957 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003958 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003959 if (ERROR("BRACKET_SPACE",
3960 "space prohibited before open square bracket '['\n" . $herecurr) &&
3961 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003962 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003963 s/^(\+.*?)\s+\[/$1\[/;
3964 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003965 }
3966 }
3967
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003968# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003969 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003970 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003971 my $ctx_before = substr($line, 0, $-[1]);
3972 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003973
3974 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003975 if ($name =~ /^(?:
3976 if|for|while|switch|return|case|
3977 volatile|__volatile__|
3978 __attribute__|format|__extension__|
3979 asm|__asm__)$/x)
3980 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003981 # cpp #define statements have non-optional spaces, ie
3982 # if there is a space between the name and the open
3983 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003984 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003985
3986 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003987 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003988
3989 # If this whole things ends with a type its most
3990 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003991 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003992
3993 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003994 if (WARN("SPACING",
3995 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3996 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003997 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003998 s/\b$name\s+\(/$name\(/;
3999 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004000 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004001 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004002
Andy Whitcroft653d4872007-06-23 17:16:34 -07004003# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004004 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004005 my $fixed_line = "";
4006 my $line_fixed = 0;
4007
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004008 my $ops = qr{
4009 <<=|>>=|<=|>=|==|!=|
4010 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4011 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004012 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004013 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004014 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004015 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004016
4017## print("element count: <" . $#elements . ">\n");
4018## foreach my $el (@elements) {
4019## print("el: <$el>\n");
4020## }
4021
4022 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004023 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004024
Joe Perches3705ce52013-07-03 15:05:31 -07004025 foreach my $el (@elements) {
4026 push(@fix_elements, substr($rawline, $off, length($el)));
4027 $off += length($el);
4028 }
4029
4030 $off = 0;
4031
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004032 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004033 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004034
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004035 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004036
4037 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4038
4039## print("n: <$n> good: <$good>\n");
4040
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004041 $off += length($elements[$n]);
4042
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004043 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004044 my $ca = substr($opline, 0, $off);
4045 my $cc = '';
4046 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4047 $cc = substr($opline, $off + length($elements[$n + 1]));
4048 }
4049 my $cb = "$ca$;$cc";
4050
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004051 my $a = '';
4052 $a = 'V' if ($elements[$n] ne '');
4053 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004054 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004055 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4056 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004057 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004058
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004059 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004060
4061 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004062 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004063 $c = 'V' if ($elements[$n + 2] ne '');
4064 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004065 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004066 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4067 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004068 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004069 } else {
4070 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004071 }
4072
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004073 my $ctx = "${a}x${c}";
4074
4075 my $at = "(ctx:$ctx)";
4076
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004077 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004078 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004079
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004080 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004081 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004082
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004083 # Get the full operator variant.
4084 my $opv = $op . substr($curr_vars, $off, 1);
4085
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004086 # Ignore operators passed as parameters.
4087 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004088 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004089
Andy Whitcroftcf655042008-03-04 14:28:20 -08004090# # Ignore comments
4091# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004092
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004093 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004094 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004095 if ($ctx !~ /.x[WEBC]/ &&
4096 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004097 if (ERROR("SPACING",
4098 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004099 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004100 $line_fixed = 1;
4101 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004102 }
4103
4104 # // is a comment
4105 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004106
Joe Perchesb00e4812014-04-03 14:49:33 -07004107 # : when part of a bitfield
4108 } elsif ($opv eq ':B') {
4109 # skip the bitfield test for now
4110
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004111 # No spaces for:
4112 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004113 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004114 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004115 if (ERROR("SPACING",
4116 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004117 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004118 if (defined $fix_elements[$n + 2]) {
4119 $fix_elements[$n + 2] =~ s/^\s+//;
4120 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004121 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004122 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004123 }
4124
Joe Perches23810972014-12-10 15:51:32 -08004125 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004126 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004127 my $rtrim_before = 0;
4128 my $space_after = 0;
4129 if ($ctx =~ /Wx./) {
4130 if (ERROR("SPACING",
4131 "space prohibited before that '$op' $at\n" . $hereptr)) {
4132 $line_fixed = 1;
4133 $rtrim_before = 1;
4134 }
4135 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004136 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004137 if (ERROR("SPACING",
4138 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004139 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004140 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004141 $space_after = 1;
4142 }
4143 }
4144 if ($rtrim_before || $space_after) {
4145 if ($rtrim_before) {
4146 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4147 } else {
4148 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4149 }
4150 if ($space_after) {
4151 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004152 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004153 }
4154
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004155 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004156 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004157 #warn "'*' is part of type\n";
4158
4159 # unary operators should have a space before and
4160 # none after. May be left adjacent to another
4161 # unary operator, or a cast
4162 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004163 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004164 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004165 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004166 if (ERROR("SPACING",
4167 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004168 if ($n != $last_after + 2) {
4169 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4170 $line_fixed = 1;
4171 }
Joe Perches3705ce52013-07-03 15:05:31 -07004172 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004173 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004174 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004175 # A unary '*' may be const
4176
4177 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004178 if (ERROR("SPACING",
4179 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004180 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004181 if (defined $fix_elements[$n + 2]) {
4182 $fix_elements[$n + 2] =~ s/^\s+//;
4183 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004184 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004185 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004186 }
4187
4188 # unary ++ and unary -- are allowed no space on one side.
4189 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004190 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004191 if (ERROR("SPACING",
4192 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004193 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004194 $line_fixed = 1;
4195 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004196 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004197 if ($ctx =~ /Wx[BE]/ ||
4198 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004199 if (ERROR("SPACING",
4200 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004201 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004202 $line_fixed = 1;
4203 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004204 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004205 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004206 if (ERROR("SPACING",
4207 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004208 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004209 if (defined $fix_elements[$n + 2]) {
4210 $fix_elements[$n + 2] =~ s/^\s+//;
4211 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004212 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004213 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004214 }
4215
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004216 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004217 } elsif ($op eq '<<' or $op eq '>>' or
4218 $op eq '&' or $op eq '^' or $op eq '|' or
4219 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004220 $op eq '*' or $op eq '/' or
4221 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004222 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004223 if ($check) {
4224 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4225 if (CHK("SPACING",
4226 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4227 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4228 $fix_elements[$n + 2] =~ s/^\s+//;
4229 $line_fixed = 1;
4230 }
4231 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4232 if (CHK("SPACING",
4233 "space preferred before that '$op' $at\n" . $hereptr)) {
4234 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4235 $line_fixed = 1;
4236 }
4237 }
4238 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004239 if (ERROR("SPACING",
4240 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004241 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4242 if (defined $fix_elements[$n + 2]) {
4243 $fix_elements[$n + 2] =~ s/^\s+//;
4244 }
Joe Perches3705ce52013-07-03 15:05:31 -07004245 $line_fixed = 1;
4246 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004247 }
4248
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004249 # A colon needs no spaces before when it is
4250 # terminating a case value or a label.
4251 } elsif ($opv eq ':C' || $opv eq ':L') {
4252 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004253 if (ERROR("SPACING",
4254 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004255 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004256 $line_fixed = 1;
4257 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004258 }
4259
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004260 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004261 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004262 my $ok = 0;
4263
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004264 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004265 if (($op eq '<' &&
4266 $cc =~ /^\S+\@\S+>/) ||
4267 ($op eq '>' &&
4268 $ca =~ /<\S+\@\S+$/))
4269 {
4270 $ok = 1;
4271 }
4272
Joe Perchese0df7e12015-04-16 12:44:53 -07004273 # for asm volatile statements
4274 # ignore a colon with another
4275 # colon immediately before or after
4276 if (($op eq ':') &&
4277 ($ca =~ /:$/ || $cc =~ /^:/)) {
4278 $ok = 1;
4279 }
4280
Joe Perches84731622013-11-12 15:10:05 -08004281 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004282 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004283 my $msg_type = \&ERROR;
4284 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4285
4286 if (&{$msg_type}("SPACING",
4287 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004288 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4289 if (defined $fix_elements[$n + 2]) {
4290 $fix_elements[$n + 2] =~ s/^\s+//;
4291 }
Joe Perches3705ce52013-07-03 15:05:31 -07004292 $line_fixed = 1;
4293 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004294 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004295 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004296 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004297
4298## print("n: <$n> GOOD: <$good>\n");
4299
4300 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004301 }
Joe Perches3705ce52013-07-03 15:05:31 -07004302
4303 if (($#elements % 2) == 0) {
4304 $fixed_line = $fixed_line . $fix_elements[$#elements];
4305 }
4306
Joe Perches194f66f2014-08-06 16:11:03 -07004307 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4308 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004309 }
4310
4311
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004312 }
4313
Joe Perches786b6322013-07-03 15:05:32 -07004314# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004315 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004316 if (WARN("SPACING",
4317 "space prohibited before semicolon\n" . $herecurr) &&
4318 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004319 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004320 s/^(\+.*\S)\s+;/$1;/;
4321 }
4322 }
4323
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004324# check for multiple assignments
4325 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004326 CHK("MULTIPLE_ASSIGNMENTS",
4327 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004328 }
4329
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004330## # check for multiple declarations, allowing for a function declaration
4331## # continuation.
4332## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4333## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4334##
4335## # Remove any bracketed sections to ensure we do not
4336## # falsly report the parameters of functions.
4337## my $ln = $line;
4338## while ($ln =~ s/\([^\(\)]*\)//g) {
4339## }
4340## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004341## WARN("MULTIPLE_DECLARATION",
4342## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004343## }
4344## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004345
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004346#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004347 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004348 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004349 if (ERROR("SPACING",
4350 "space required before the open brace '{'\n" . $herecurr) &&
4351 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004352 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004353 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004354 }
4355
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004356## # check for blank lines before declarations
4357## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4358## $prevrawline =~ /^.\s*$/) {
4359## WARN("SPACING",
4360## "No blank lines before declarations\n" . $hereprev);
4361## }
4362##
4363
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004364# closing brace should have a space following it when it has anything
4365# on the line
4366 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004367 if (ERROR("SPACING",
4368 "space required after that close brace '}'\n" . $herecurr) &&
4369 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004370 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004371 s/}((?!(?:,|;|\)))\S)/} $1/;
4372 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004373 }
4374
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004375# check spacing on square brackets
4376 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004377 if (ERROR("SPACING",
4378 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4379 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004380 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004381 s/\[\s+/\[/;
4382 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004383 }
4384 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004385 if (ERROR("SPACING",
4386 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4387 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004388 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004389 s/\s+\]/\]/;
4390 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004391 }
4392
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004393# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004394 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004395 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004396 if (ERROR("SPACING",
4397 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4398 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004399 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004400 s/\(\s+/\(/;
4401 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004402 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004403 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004404 $line !~ /for\s*\(.*;\s+\)/ &&
4405 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004406 if (ERROR("SPACING",
4407 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4408 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004409 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004410 s/\s+\)/\)/;
4411 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004412 }
4413
Joe Perchese2826fd2014-08-06 16:10:48 -07004414# check unnecessary parentheses around addressof/dereference single $Lvals
4415# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4416
4417 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004418 my $var = $1;
4419 if (CHK("UNNECESSARY_PARENTHESES",
4420 "Unnecessary parentheses around $var\n" . $herecurr) &&
4421 $fix) {
4422 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4423 }
4424 }
4425
4426# check for unnecessary parentheses around function pointer uses
4427# ie: (foo->bar)(); should be foo->bar();
4428# but not "if (foo->bar) (" to avoid some false positives
4429 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4430 my $var = $2;
4431 if (CHK("UNNECESSARY_PARENTHESES",
4432 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4433 $fix) {
4434 my $var2 = deparenthesize($var);
4435 $var2 =~ s/\s//g;
4436 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4437 }
4438 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004439
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004440#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004441 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004442 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004443 if (WARN("INDENTED_LABEL",
4444 "labels should not be indented\n" . $herecurr) &&
4445 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004446 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004447 s/^(.)\s+/$1/;
4448 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004449 }
4450
Joe Perches5b9553a2014-04-03 14:49:21 -07004451# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004452 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004453 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004454 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004455 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4456 my $value = $1;
4457 $value = deparenthesize($value);
4458 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4459 ERROR("RETURN_PARENTHESES",
4460 "return is not a function, parentheses are not required\n" . $herecurr);
4461 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004462 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004463 ERROR("SPACING",
4464 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004465 }
4466 }
Joe Perches507e5142013-11-12 15:10:13 -08004467
Joe Perchesb43ae212014-06-23 13:22:07 -07004468# unnecessary return in a void function
4469# at end-of-function, with the previous line a single leading tab, then return;
4470# and the line before that not a goto label target like "out:"
4471 if ($sline =~ /^[ \+]}\s*$/ &&
4472 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4473 $linenr >= 3 &&
4474 $lines[$linenr - 3] =~ /^[ +]/ &&
4475 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004476 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004477 "void function return statements are not generally useful\n" . $hereprev);
4478 }
Joe Perches9819cf22014-06-04 16:12:09 -07004479
Joe Perches189248d2014-01-23 15:54:47 -08004480# if statements using unnecessary parentheses - ie: if ((foo == bar))
4481 if ($^V && $^V ge 5.10.0 &&
4482 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4483 my $openparens = $1;
4484 my $count = $openparens =~ tr@\(@\(@;
4485 my $msg = "";
4486 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4487 my $comp = $4; #Not $1 because of $LvalOrFunc
4488 $msg = " - maybe == should be = ?" if ($comp eq "==");
4489 WARN("UNNECESSARY_PARENTHESES",
4490 "Unnecessary parentheses$msg\n" . $herecurr);
4491 }
4492 }
4493
Joe Perchesc5595fa2015-09-09 15:37:58 -07004494# comparisons with a constant or upper case identifier on the left
4495# avoid cases like "foo + BAR < baz"
4496# only fix matches surrounded by parentheses to avoid incorrect
4497# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4498 if ($^V && $^V ge 5.10.0 &&
4499 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4500 my $lead = $1;
4501 my $const = $2;
4502 my $comp = $3;
4503 my $to = $4;
4504 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004505 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004506 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4507 WARN("CONSTANT_COMPARISON",
4508 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4509 $fix) {
4510 if ($comp eq "<") {
4511 $newcomp = ">";
4512 } elsif ($comp eq "<=") {
4513 $newcomp = ">=";
4514 } elsif ($comp eq ">") {
4515 $newcomp = "<";
4516 } elsif ($comp eq ">=") {
4517 $newcomp = "<=";
4518 }
4519 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4520 }
4521 }
4522
Joe Perchesf34e4a42015-04-16 12:44:19 -07004523# Return of what appears to be an errno should normally be negative
4524 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004525 my $name = $1;
4526 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004527 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004528 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004529 }
4530 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004531
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004532# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004533 if ($line =~ /\b(if|while|for|switch)\(/) {
4534 if (ERROR("SPACING",
4535 "space required before the open parenthesis '('\n" . $herecurr) &&
4536 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004537 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004538 s/\b(if|while|for|switch)\(/$1 \(/;
4539 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004540 }
4541
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004542# Check for illegal assignment in if conditional -- and check for trailing
4543# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004544 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004545 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4546 ctx_statement_block($linenr, $realcnt, 0)
4547 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004548 my ($stat_next) = ctx_statement_block($line_nr_next,
4549 $remain_next, $off_next);
4550 $stat_next =~ s/\n./\n /g;
4551 ##print "stat<$stat> stat_next<$stat_next>\n";
4552
4553 if ($stat_next =~ /^\s*while\b/) {
4554 # If the statement carries leading newlines,
4555 # then count those as offsets.
4556 my ($whitespace) =
4557 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4558 my $offset =
4559 statement_rawlines($whitespace) - 1;
4560
4561 $suppress_whiletrailers{$line_nr_next +
4562 $offset} = 1;
4563 }
4564 }
4565 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004566 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004567 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004568 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004569
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004570 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004571 ERROR("ASSIGN_IN_IF",
4572 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004573 }
4574
4575 # Find out what is on the end of the line after the
4576 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004577 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004578 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004579 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004580 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4581 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004582 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004583 # Find out how long the conditional actually is.
4584 my @newlines = ($c =~ /\n/gs);
4585 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004586 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004587
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004588 $stat_real = raw_line($linenr, $cond_lines)
4589 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004590 if (defined($stat_real) && $cond_lines > 1) {
4591 $stat_real = "[...]\n$stat_real";
4592 }
4593
Joe Perches000d1cc12011-07-25 17:13:25 -07004594 ERROR("TRAILING_STATEMENTS",
4595 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004596 }
4597 }
4598
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004599# Check for bitwise tests written as boolean
4600 if ($line =~ /
4601 (?:
4602 (?:\[|\(|\&\&|\|\|)
4603 \s*0[xX][0-9]+\s*
4604 (?:\&\&|\|\|)
4605 |
4606 (?:\&\&|\|\|)
4607 \s*0[xX][0-9]+\s*
4608 (?:\&\&|\|\||\)|\])
4609 )/x)
4610 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004611 WARN("HEXADECIMAL_BOOLEAN_TEST",
4612 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004613 }
4614
Andy Whitcroft8905a672007-11-28 16:21:06 -08004615# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004616 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4617 my $s = $1;
4618 $s =~ s/$;//g; # Remove any comments
4619 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004620 ERROR("TRAILING_STATEMENTS",
4621 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004622 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004623 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004624# if should not continue a brace
4625 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004626 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004627 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004628 $herecurr);
4629 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004630# case and default should not have general statements after them
4631 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4632 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004633 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004634 \s*return\s+
4635 )/xg)
4636 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004637 ERROR("TRAILING_STATEMENTS",
4638 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004639 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004640
4641 # Check for }<nl>else {, these must be at the same
4642 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004643 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4644 $previndent == $indent) {
4645 if (ERROR("ELSE_AFTER_BRACE",
4646 "else should follow close brace '}'\n" . $hereprev) &&
4647 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4648 fix_delete_line($fixlinenr - 1, $prevrawline);
4649 fix_delete_line($fixlinenr, $rawline);
4650 my $fixedline = $prevrawline;
4651 $fixedline =~ s/}\s*$//;
4652 if ($fixedline !~ /^\+\s*$/) {
4653 fix_insert_line($fixlinenr, $fixedline);
4654 }
4655 $fixedline = $rawline;
4656 $fixedline =~ s/^(.\s*)else/$1} else/;
4657 fix_insert_line($fixlinenr, $fixedline);
4658 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004659 }
4660
Joe Perches8b8856f2014-08-06 16:11:14 -07004661 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4662 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004663 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4664
4665 # Find out what is on the end of the line after the
4666 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004667 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004668 $s =~ s/\n.*//g;
4669
4670 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004671 if (ERROR("WHILE_AFTER_BRACE",
4672 "while should follow close brace '}'\n" . $hereprev) &&
4673 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4674 fix_delete_line($fixlinenr - 1, $prevrawline);
4675 fix_delete_line($fixlinenr, $rawline);
4676 my $fixedline = $prevrawline;
4677 my $trailing = $rawline;
4678 $trailing =~ s/^\+//;
4679 $trailing = trim($trailing);
4680 $fixedline =~ s/}\s*$/} $trailing/;
4681 fix_insert_line($fixlinenr, $fixedline);
4682 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004683 }
4684 }
4685
Joe Perches95e2c602013-07-03 15:05:20 -07004686#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004687 while ($line =~ m{($Constant|$Lval)}g) {
4688 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004689
4690#gcc binary extension
4691 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004692 if (WARN("GCC_BINARY_CONSTANT",
4693 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4694 $fix) {
4695 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004696 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004697 s/\b$var\b/$hexval/;
4698 }
Joe Perches95e2c602013-07-03 15:05:20 -07004699 }
4700
4701#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004702 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004703 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004704#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004705 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004706#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004707 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4708#Ignore some three character SI units explicitly, like MiB and KHz
4709 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004710 while ($var =~ m{($Ident)}g) {
4711 my $word = $1;
4712 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004713 if ($check) {
4714 seed_camelcase_includes();
4715 if (!$file && !$camelcase_file_seeded) {
4716 seed_camelcase_file($realfile);
4717 $camelcase_file_seeded = 1;
4718 }
4719 }
Joe Perches7e781f62013-09-11 14:23:55 -07004720 if (!defined $camelcase{$word}) {
4721 $camelcase{$word} = 1;
4722 CHK("CAMELCASE",
4723 "Avoid CamelCase: <$word>\n" . $herecurr);
4724 }
Joe Perches34456862013-07-03 15:05:34 -07004725 }
Joe Perches323c1262012-12-17 16:02:07 -08004726 }
4727 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004728
4729#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004730 if ($line =~ /\#\s*define.*\\\s+$/) {
4731 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4732 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4733 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004734 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004735 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004736 }
4737
Fabian Frederick0e212e02015-04-16 12:44:25 -07004738# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4739# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004740 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004741 my $file = "$1.h";
4742 my $checkfile = "include/linux/$file";
4743 if (-f "$root/$checkfile" &&
4744 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004745 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004746 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004747 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4748 if ($asminclude > 0) {
4749 if ($realfile =~ m{^arch/}) {
4750 CHK("ARCH_INCLUDE_LINUX",
4751 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4752 } else {
4753 WARN("INCLUDE_LINUX",
4754 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4755 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004756 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004757 }
4758 }
4759
Andy Whitcroft653d4872007-06-23 17:16:34 -07004760# multi-statement macros should be enclosed in a do while loop, grab the
4761# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004762# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004763 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4764 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004765 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004766 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004767 my ($off, $dstat, $dcond, $rest);
4768 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004769 my $has_flow_statement = 0;
4770 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004771 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004772 ctx_statement_block($linenr, $realcnt, 0);
4773 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004774 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004775 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004776
Joe Perches08a28432014-10-13 15:51:55 -07004777 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004778 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004779
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004780 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004781 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004782 $dstat =~ s/\\\n.//g;
4783 $dstat =~ s/^\s*//s;
4784 $dstat =~ s/\s*$//s;
4785
4786 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004787 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4788 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004789 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004790 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004791 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004792
Gregory Bean02870be2011-05-11 09:11:12 -07004793 # Extremely long macros may fall off the end of the
4794 # available context without closing. Give a dangling
4795 # backslash the benefit of the doubt and allow it
4796 # to gobble any hanging open-parens.
4797 $dstat =~ s/\(.+\\$/1/;
4798
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004799 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004800 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4801 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004802 {
4803 }
4804
Joe Perches42e15292016-03-15 14:58:01 -07004805 # Make asm volatile uses seem like a generic function
4806 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4807
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004808 my $exceptions = qr{
4809 $Declare|
4810 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004811 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004812 DECLARE_PER_CPU|
4813 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004814 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004815 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004816 union|
4817 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004818 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004819 ^\"|\"$|
4820 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004821 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004822 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004823 if ($dstat ne '' &&
4824 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4825 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004826 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004827 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004828 $dstat !~ /$exceptions/ &&
4829 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004830 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004831 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004832 $dstat !~ /^for\s*$Constant$/ && # for (...)
4833 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4834 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004835 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004836 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004837 {
4838 $ctx =~ s/\n*$//;
4839 my $herectx = $here . "\n";
4840 my $cnt = statement_rawlines($ctx);
4841
4842 for (my $n = 0; $n < $cnt; $n++) {
4843 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004844 }
4845
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004846 if ($dstat =~ /;/) {
4847 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4848 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4849 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004850 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004851 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004852 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004853 }
Joe Perches5023d342012-12-17 16:01:47 -08004854
Joe Perches08a28432014-10-13 15:51:55 -07004855# check for macros with flow control, but without ## concatenation
4856# ## concatenation is commonly a macro that defines a function so ignore those
4857 if ($has_flow_statement && !$has_arg_concat) {
4858 my $herectx = $here . "\n";
4859 my $cnt = statement_rawlines($ctx);
4860
4861 for (my $n = 0; $n < $cnt; $n++) {
4862 $herectx .= raw_line($linenr, $n) . "\n";
4863 }
4864 WARN("MACRO_WITH_FLOW_CONTROL",
4865 "Macros with flow control statements should be avoided\n" . "$herectx");
4866 }
4867
Joe Perches481eb482012-12-17 16:01:56 -08004868# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004869
4870 } else {
4871 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004872 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4873 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004874 $line =~ /^\+.*\\$/) {
4875 WARN("LINE_CONTINUATIONS",
4876 "Avoid unnecessary line continuations\n" . $herecurr);
4877 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004878 }
4879
Joe Perchesb13edf72012-07-30 14:41:24 -07004880# do {} while (0) macro tests:
4881# single-statement macros do not need to be enclosed in do while (0) loop,
4882# macro should not end with a semicolon
4883 if ($^V && $^V ge 5.10.0 &&
4884 $realfile !~ m@/vmlinux.lds.h$@ &&
4885 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4886 my $ln = $linenr;
4887 my $cnt = $realcnt;
4888 my ($off, $dstat, $dcond, $rest);
4889 my $ctx = '';
4890 ($dstat, $dcond, $ln, $cnt, $off) =
4891 ctx_statement_block($linenr, $realcnt, 0);
4892 $ctx = $dstat;
4893
4894 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004895 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004896
4897 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4898 my $stmts = $2;
4899 my $semis = $3;
4900
4901 $ctx =~ s/\n*$//;
4902 my $cnt = statement_rawlines($ctx);
4903 my $herectx = $here . "\n";
4904
4905 for (my $n = 0; $n < $cnt; $n++) {
4906 $herectx .= raw_line($linenr, $n) . "\n";
4907 }
4908
Joe Perchesac8e97f2012-08-21 16:15:53 -07004909 if (($stmts =~ tr/;/;/) == 1 &&
4910 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004911 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4912 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4913 }
4914 if (defined $semis && $semis ne "") {
4915 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4916 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4917 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004918 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4919 $ctx =~ s/\n*$//;
4920 my $cnt = statement_rawlines($ctx);
4921 my $herectx = $here . "\n";
4922
4923 for (my $n = 0; $n < $cnt; $n++) {
4924 $herectx .= raw_line($linenr, $n) . "\n";
4925 }
4926
4927 WARN("TRAILING_SEMICOLON",
4928 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004929 }
4930 }
4931
Mike Frysinger080ba922009-01-06 14:41:25 -08004932# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4933# all assignments may have only one of the following with an assignment:
4934# .
4935# ALIGN(...)
4936# VMLINUX_SYMBOL(...)
4937 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004938 WARN("MISSING_VMLINUX_SYMBOL",
4939 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004940 }
4941
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004942# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004943 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4944 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004945 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004946 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004947 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4948 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004949 my @allowed = ();
4950 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004951 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004952 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004953 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004954 for my $chunk (@chunks) {
4955 my ($cond, $block) = @{$chunk};
4956
Andy Whitcroft773647a2008-03-28 14:15:58 -07004957 # If the condition carries leading newlines, then count those as offsets.
4958 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4959 my $offset = statement_rawlines($whitespace) - 1;
4960
Joe Perchesaad4f612012-03-23 15:02:19 -07004961 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004962 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4963
4964 # We have looked at and allowed this specific line.
4965 $suppress_ifbraces{$ln + $offset} = 1;
4966
4967 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004968 $ln += statement_rawlines($block) - 1;
4969
Andy Whitcroft773647a2008-03-28 14:15:58 -07004970 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004971
4972 $seen++ if ($block =~ /^\s*{/);
4973
Joe Perchesaad4f612012-03-23 15:02:19 -07004974 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004975 if (statement_lines($cond) > 1) {
4976 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004977 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004978 }
4979 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004980 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004981 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004982 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004983 if (statement_block_size($block) > 1) {
4984 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004985 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004986 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004987 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004988 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004989 if ($seen) {
4990 my $sum_allowed = 0;
4991 foreach (@allowed) {
4992 $sum_allowed += $_;
4993 }
4994 if ($sum_allowed == 0) {
4995 WARN("BRACES",
4996 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4997 } elsif ($sum_allowed != $allow &&
4998 $seen != $allow) {
4999 CHK("BRACES",
5000 "braces {} should be used on all arms of this statement\n" . $herectx);
5001 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005002 }
5003 }
5004 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005005 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005006 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005007 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005008
Andy Whitcroftcf655042008-03-04 14:28:20 -08005009 # Check the pre-context.
5010 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5011 #print "APW: ALLOWED: pre<$1>\n";
5012 $allowed = 1;
5013 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005014
5015 my ($level, $endln, @chunks) =
5016 ctx_statement_full($linenr, $realcnt, $-[0]);
5017
Andy Whitcroftcf655042008-03-04 14:28:20 -08005018 # Check the condition.
5019 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005020 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005021 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005022 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005023 }
5024 if (statement_lines($cond) > 1) {
5025 #print "APW: ALLOWED: cond<$cond>\n";
5026 $allowed = 1;
5027 }
5028 if ($block =~/\b(?:if|for|while)\b/) {
5029 #print "APW: ALLOWED: block<$block>\n";
5030 $allowed = 1;
5031 }
5032 if (statement_block_size($block) > 1) {
5033 #print "APW: ALLOWED: lines block<$block>\n";
5034 $allowed = 1;
5035 }
5036 # Check the post-context.
5037 if (defined $chunks[1]) {
5038 my ($cond, $block) = @{$chunks[1]};
5039 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005040 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005041 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005042 if ($block =~ /^\s*\{/) {
5043 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5044 $allowed = 1;
5045 }
5046 }
5047 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005048 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005049 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005050
Andy Whitcroftf0556632008-10-15 22:02:23 -07005051 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005052 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005053 }
5054
Joe Perches000d1cc12011-07-25 17:13:25 -07005055 WARN("BRACES",
5056 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005057 }
5058 }
5059
Joe Perches0979ae62012-12-17 16:01:59 -08005060# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005061 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005062 if (CHK("BRACES",
5063 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5064 $fix && $prevrawline =~ /^\+/) {
5065 fix_delete_line($fixlinenr - 1, $prevrawline);
5066 }
Joe Perches0979ae62012-12-17 16:01:59 -08005067 }
Joe Perches77b9a532013-07-03 15:05:29 -07005068 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005069 if (CHK("BRACES",
5070 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5071 $fix) {
5072 fix_delete_line($fixlinenr, $rawline);
5073 }
Joe Perches0979ae62012-12-17 16:01:59 -08005074 }
5075
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005076# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005077 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5078 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005079 WARN("VOLATILE",
5080 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005081 }
5082
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005083# Check for user-visible strings broken across lines, which breaks the ability
5084# to grep for the string. Make exceptions when the previous string ends in a
5085# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5086# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005087 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005088 $prevline =~ /"\s*$/ &&
5089 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5090 if (WARN("SPLIT_STRING",
5091 "quoted string split across lines\n" . $hereprev) &&
5092 $fix &&
5093 $prevrawline =~ /^\+.*"\s*$/ &&
5094 $last_coalesced_string_linenr != $linenr - 1) {
5095 my $extracted_string = get_quoted_string($line, $rawline);
5096 my $comma_close = "";
5097 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5098 $comma_close = $1;
5099 }
5100
5101 fix_delete_line($fixlinenr - 1, $prevrawline);
5102 fix_delete_line($fixlinenr, $rawline);
5103 my $fixedline = $prevrawline;
5104 $fixedline =~ s/"\s*$//;
5105 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5106 fix_insert_line($fixlinenr - 1, $fixedline);
5107 $fixedline = $rawline;
5108 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5109 if ($fixedline !~ /\+\s*$/) {
5110 fix_insert_line($fixlinenr, $fixedline);
5111 }
5112 $last_coalesced_string_linenr = $linenr;
5113 }
5114 }
5115
5116# check for missing a space in a string concatenation
5117 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5118 WARN('MISSING_SPACE',
5119 "break quoted strings at a space character\n" . $hereprev);
5120 }
5121
5122# check for spaces before a quoted newline
5123 if ($rawline =~ /^.*\".*\s\\n/) {
5124 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5125 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5126 $fix) {
5127 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5128 }
5129
5130 }
5131
Joe Perchesf17dba42014-10-13 15:51:51 -07005132# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005133 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005134 CHK("CONCATENATED_STRING",
5135 "Concatenated strings should use spaces between elements\n" . $herecurr);
5136 }
5137
Joe Perches90ad30e2014-12-10 15:51:59 -08005138# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005139 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005140 WARN("STRING_FRAGMENTS",
5141 "Consecutive strings are generally better as a single string\n" . $herecurr);
5142 }
5143
Joe Perches6e300752015-09-09 15:37:47 -07005144# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005145 my $string;
5146 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5147 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5148 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005149 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005150 WARN("PRINTF_L",
5151 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5152 last;
5153 }
Joe Perches6e300752015-09-09 15:37:47 -07005154 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5155 ERROR("PRINTF_0xDECIMAL",
5156 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5157 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005158 }
5159
5160# check for line continuations in quoted strings with odd counts of "
5161 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5162 WARN("LINE_CONTINUATIONS",
5163 "Avoid line continuations in quoted strings\n" . $herecurr);
5164 }
5165
Gregory Bean6c9c6842011-03-17 14:18:20 -07005166# sys_open/read/write/close are not allowed in the kernel
5167 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5168 ERROR("FILE_OPS",
5169 "$1 is inappropriate in kernel code.\n" .
5170 $herecurr);
5171 }
5172
Gregory Beana7cce0a2011-06-07 08:06:45 -07005173# filp_open is a backdoor for sys_open
5174 if ($line =~ /\b(filp_open)\b/) {
5175 ERROR("FILE_OPS",
5176 "$1 is inappropriate in kernel code.\n" .
5177 $herecurr);
5178 }
5179
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005180# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5181 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5182 ERROR("NON_RELAXED_IO",
5183 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5184 "with appropriate memory barriers instead.\n" .
5185 $herecurr);
5186 }
5187
5188# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5189 if ($line =~ /\b((in|out)([bwl]))\b/) {
5190 my ($all, $pref, $suf) = ($1, $2, $3);
5191 $pref =~ s/in/read/;
5192 $pref =~ s/out/write/;
5193 ERROR("NON_RELAXED_IO",
5194 "Use of $all is deprecated: use " .
5195 "__raw_$pref$suf\n\t" .
5196 "with appropriate memory barriers instead.\n" .
5197 $herecurr);
5198 }
5199
Gregory Bean438bf5f2011-06-15 09:32:38 -07005200# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005201 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005202 WARN("ARM_BARRIER",
5203 "Use of dsb is discouranged: prefer mb.\n" .
5204 $herecurr);
5205 }
5206
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005207# unbounded string functions are overflow risks
5208 my %str_fns = (
5209 "sprintf" => "snprintf",
5210 "strcpy" => "strlcpy",
5211 "strncpy" => "strlcpy",
5212 "strcat" => "strlcat",
5213 "strncat" => "strlcat",
5214 "vsprintf" => "vsnprintf",
5215 "strchr" => "strnchr",
5216 "strstr" => "strnstr",
5217 );
5218 foreach my $k (keys %str_fns) {
5219 if ($line =~ /\b$k\b/) {
5220 ERROR("UNBOUNDED_STRING_FNS",
5221 "Use of $k is deprecated: " .
5222 "use $str_fns{$k} instead.\n" .
5223 $herecurr);
5224 }
5225 }
5226
Andy Whitcroft00df3442007-06-08 13:47:06 -07005227# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005228 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005229 WARN("IF_0",
5230 "if this code is redundant consider removing it\n"
5231 . $herecurr);
5232 }
5233
5234# warn about #if 1
5235 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5236 WARN("IF_1",
5237 "if this code is required consider removing"
5238 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005239 }
5240
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005241# check for needless "if (<foo>) fn(<foo>)" uses
5242 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005243 my $tested = quotemeta($1);
5244 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5245 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5246 my $func = $1;
5247 if (WARN('NEEDLESS_IF',
5248 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5249 $fix) {
5250 my $do_fix = 1;
5251 my $leading_tabs = "";
5252 my $new_leading_tabs = "";
5253 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5254 $leading_tabs = $1;
5255 } else {
5256 $do_fix = 0;
5257 }
5258 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5259 $new_leading_tabs = $1;
5260 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5261 $do_fix = 0;
5262 }
5263 } else {
5264 $do_fix = 0;
5265 }
5266 if ($do_fix) {
5267 fix_delete_line($fixlinenr - 1, $prevrawline);
5268 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5269 }
5270 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005271 }
5272 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005273
Joe Perchesebfdc402014-08-06 16:10:27 -07005274# check for unnecessary "Out of Memory" messages
5275 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5276 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5277 (defined $1 || defined $3) &&
5278 $linenr > 3) {
5279 my $testval = $2;
5280 my $testline = $lines[$linenr - 3];
5281
5282 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5283# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5284
5285 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5286 WARN("OOM_MESSAGE",
5287 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5288 }
5289 }
5290
Joe Perchesf78d98f2014-10-13 15:52:01 -07005291# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005292 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005293 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5294 my $level = $1;
5295 if (WARN("UNNECESSARY_KERN_LEVEL",
5296 "Possible unnecessary $level\n" . $herecurr) &&
5297 $fix) {
5298 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5299 }
5300 }
5301
Joe Perchesabb08a52014-12-10 15:51:46 -08005302# check for mask then right shift without a parentheses
5303 if ($^V && $^V ge 5.10.0 &&
5304 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5305 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5306 WARN("MASK_THEN_SHIFT",
5307 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5308 }
5309
Joe Perchesb75ac612014-12-10 15:52:02 -08005310# check for pointer comparisons to NULL
5311 if ($^V && $^V ge 5.10.0) {
5312 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5313 my $val = $1;
5314 my $equal = "!";
5315 $equal = "" if ($4 eq "!=");
5316 if (CHK("COMPARISON_TO_NULL",
5317 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5318 $fix) {
5319 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5320 }
5321 }
5322 }
5323
Joe Perches8716de32013-09-11 14:24:05 -07005324# check for bad placement of section $InitAttribute (e.g.: __initdata)
5325 if ($line =~ /(\b$InitAttribute\b)/) {
5326 my $attr = $1;
5327 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5328 my $ptr = $1;
5329 my $var = $2;
5330 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5331 ERROR("MISPLACED_INIT",
5332 "$attr should be placed after $var\n" . $herecurr)) ||
5333 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5334 WARN("MISPLACED_INIT",
5335 "$attr should be placed after $var\n" . $herecurr))) &&
5336 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005337 $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 -07005338 }
5339 }
5340 }
5341
Joe Perchese970b882013-11-12 15:10:10 -08005342# check for $InitAttributeData (ie: __initdata) with const
5343 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5344 my $attr = $1;
5345 $attr =~ /($InitAttributePrefix)(.*)/;
5346 my $attr_prefix = $1;
5347 my $attr_type = $2;
5348 if (ERROR("INIT_ATTRIBUTE",
5349 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5350 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005351 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005352 s/$InitAttributeData/${attr_prefix}initconst/;
5353 }
5354 }
5355
5356# check for $InitAttributeConst (ie: __initconst) without const
5357 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5358 my $attr = $1;
5359 if (ERROR("INIT_ATTRIBUTE",
5360 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5361 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005362 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005363 /(^\+\s*(?:static\s+))/;
5364 $lead = rtrim($1);
5365 $lead = "$lead " if ($lead !~ /^\+$/);
5366 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005367 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005368 }
5369 }
5370
Joe Perchesc17893c2015-04-16 12:44:42 -07005371# check for __read_mostly with const non-pointer (should just be const)
5372 if ($line =~ /\b__read_mostly\b/ &&
5373 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5374 if (ERROR("CONST_READ_MOSTLY",
5375 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5376 $fix) {
5377 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5378 }
5379 }
5380
Joe Perchesfbdb8132014-04-03 14:49:14 -07005381# don't use __constant_<foo> functions outside of include/uapi/
5382 if ($realfile !~ m@^include/uapi/@ &&
5383 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5384 my $constant_func = $1;
5385 my $func = $constant_func;
5386 $func =~ s/^__constant_//;
5387 if (WARN("CONSTANT_CONVERSION",
5388 "$constant_func should be $func\n" . $herecurr) &&
5389 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005390 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005391 }
5392 }
5393
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005394# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005395 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005396 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005397 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005398 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005399 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005400 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5401 }
5402 if ($delay > 2000) {
5403 WARN("LONG_UDELAY",
5404 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005405 }
5406 }
5407
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005408# warn about unexpectedly long msleep's
5409 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5410 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005411 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005412 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005413 }
5414 }
5415
Joe Perches36ec1932013-07-03 15:05:25 -07005416# check for comparisons of jiffies
5417 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5418 WARN("JIFFIES_COMPARISON",
5419 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5420 }
5421
Joe Perches9d7a34a2013-07-03 15:05:26 -07005422# check for comparisons of get_jiffies_64()
5423 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5424 WARN("JIFFIES_COMPARISON",
5425 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5426 }
5427
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005428# check the patch for use of mdelay
5429 if ($line =~ /\bmdelay\s*\(/) {
5430 WARN("MDELAY",
5431 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5432 }
5433
Andy Whitcroft00df3442007-06-08 13:47:06 -07005434# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005435# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005436# print "#ifdef in C files should be avoided\n";
5437# print "$herecurr";
5438# $clean = 0;
5439# }
5440
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005441# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005442 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005443 if (ERROR("SPACING",
5444 "exactly one space required after that #$1\n" . $herecurr) &&
5445 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005446 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005447 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5448 }
5449
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005450 }
5451
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005452# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005453 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5454 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005455 my $which = $1;
5456 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005457 CHK("UNCOMMENTED_DEFINITION",
5458 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005459 }
5460 }
5461# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005462
5463 my $barriers = qr{
5464 mb|
5465 rmb|
5466 wmb|
5467 read_barrier_depends
5468 }x;
5469 my $barrier_stems = qr{
5470 mb__before_atomic|
5471 mb__after_atomic|
5472 store_release|
5473 load_acquire|
5474 store_mb|
5475 (?:$barriers)
5476 }x;
5477 my $all_barriers = qr{
5478 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005479 smp_(?:$barrier_stems)|
5480 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005481 }x;
5482
5483 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005484 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005485 WARN("MEMORY_BARRIER",
5486 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005487 }
5488 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005489
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005490 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5491
5492 if ($realfile !~ m@^include/asm-generic/@ &&
5493 $realfile !~ m@/barrier\.h$@ &&
5494 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5495 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5496 WARN("MEMORY_BARRIER",
5497 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5498 }
5499
Joe Perchescb426e92015-06-25 15:02:46 -07005500# check for waitqueue_active without a comment.
5501 if ($line =~ /\bwaitqueue_active\s*\(/) {
5502 if (!ctx_has_comment($first_line, $linenr)) {
5503 WARN("WAITQUEUE_ACTIVE",
5504 "waitqueue_active without comment\n" . $herecurr);
5505 }
5506 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005507
5508# Check for expedited grace periods that interrupt non-idle non-nohz
5509# online CPUs. These expedited can therefore degrade real-time response
5510# if used carelessly, and should be avoided where not absolutely
5511# needed. It is always OK to use synchronize_rcu_expedited() and
5512# synchronize_sched_expedited() at boot time (before real-time applications
5513# start) and in error situations where real-time response is compromised in
5514# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5515# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5516# Of course, nothing comes for free, and srcu_read_lock() and
5517# srcu_read_unlock() do contain full memory barriers in payment for
5518# synchronize_srcu_expedited() non-interruption properties.
5519 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5520 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5521 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5522
5523 }
5524
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005525# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005526 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005527 CHK("ARCH_DEFINES",
5528 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005529 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005530
Tobias Klauserd4977c72010-05-24 14:33:30 -07005531# Check that the storage class is at the beginning of a declaration
5532 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005533 WARN("STORAGE_CLASS",
5534 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005535 }
5536
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005537# check the location of the inline attribute, that it is between
5538# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005539 if ($line =~ /\b$Type\s+$Inline\b/ ||
5540 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005541 ERROR("INLINE_LOCATION",
5542 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005543 }
5544
Andy Whitcroft8905a672007-11-28 16:21:06 -08005545# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005546 if ($realfile !~ m@\binclude/uapi/@ &&
5547 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005548 if (WARN("INLINE",
5549 "plain inline is preferred over $1\n" . $herecurr) &&
5550 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005551 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005552
5553 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005554 }
5555
Joe Perches3d130fd2011-01-12 17:00:00 -08005556# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005557 if ($realfile !~ m@\binclude/uapi/@ &&
5558 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005559 WARN("PREFER_PACKED",
5560 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005561 }
5562
Joe Perches39b7e282011-07-25 17:13:24 -07005563# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005564 if ($realfile !~ m@\binclude/uapi/@ &&
5565 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005566 WARN("PREFER_ALIGNED",
5567 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005568 }
5569
Joe Perches5f14d3b2012-01-10 15:09:52 -08005570# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005571 if ($realfile !~ m@\binclude/uapi/@ &&
5572 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005573 if (WARN("PREFER_PRINTF",
5574 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005576 $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 -07005577
5578 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005579 }
5580
Joe Perches6061d942012-03-23 15:02:16 -07005581# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005582 if ($realfile !~ m@\binclude/uapi/@ &&
5583 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005584 if (WARN("PREFER_SCANF",
5585 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5586 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005587 $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 -07005588 }
Joe Perches6061d942012-03-23 15:02:16 -07005589 }
5590
Joe Perches619a9082014-12-10 15:51:35 -08005591# Check for __attribute__ weak, or __weak declarations (may have link issues)
5592 if ($^V && $^V ge 5.10.0 &&
5593 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5594 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5595 $line =~ /\b__weak\b/)) {
5596 ERROR("WEAK_DECLARATION",
5597 "Using weak declarations can have unintended link defects\n" . $herecurr);
5598 }
5599
Joe Perchese6176fa2015-06-25 15:02:49 -07005600# check for c99 types like uint8_t used outside of uapi/
5601 if ($realfile !~ m@\binclude/uapi/@ &&
5602 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5603 my $type = $1;
5604 if ($type =~ /\b($typeC99Typedefs)\b/) {
5605 $type = $1;
5606 my $kernel_type = 'u';
5607 $kernel_type = 's' if ($type =~ /^_*[si]/);
5608 $type =~ /(\d+)/;
5609 $kernel_type .= $1;
5610 if (CHK("PREFER_KERNEL_TYPES",
5611 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5612 $fix) {
5613 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5614 }
5615 }
5616 }
5617
Joe Perches938224b2016-01-20 14:59:15 -08005618# check for cast of C90 native int or longer types constants
5619 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5620 my $cast = $1;
5621 my $const = $2;
5622 if (WARN("TYPECAST_INT_CONSTANT",
5623 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5624 $fix) {
5625 my $suffix = "";
5626 my $newconst = $const;
5627 $newconst =~ s/${Int_type}$//;
5628 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5629 if ($cast =~ /\blong\s+long\b/) {
5630 $suffix .= 'LL';
5631 } elsif ($cast =~ /\blong\b/) {
5632 $suffix .= 'L';
5633 }
5634 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5635 }
5636 }
5637
Joe Perches8f53a9b2010-03-05 13:43:48 -08005638# check for sizeof(&)
5639 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005640 WARN("SIZEOF_ADDRESS",
5641 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005642 }
5643
Joe Perches66c80b62012-07-30 14:41:22 -07005644# check for sizeof without parenthesis
5645 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005646 if (WARN("SIZEOF_PARENTHESIS",
5647 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5648 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005649 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005650 }
Joe Perches66c80b62012-07-30 14:41:22 -07005651 }
5652
Joe Perches88982fe2012-12-17 16:02:00 -08005653# check for struct spinlock declarations
5654 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5655 WARN("USE_SPINLOCK_T",
5656 "struct spinlock should be spinlock_t\n" . $herecurr);
5657 }
5658
Joe Perchesa6962d72013-04-29 16:18:13 -07005659# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005660 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005661 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005662 $fmt =~ s/%%//g;
5663 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005664 if (WARN("PREFER_SEQ_PUTS",
5665 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5666 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005667 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005668 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005669 }
5670 }
5671
Andy Whitcroft554e1652012-01-10 15:09:57 -08005672# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005673 if ($^V && $^V ge 5.10.0 &&
5674 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005675 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005676
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005677 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005678 my $ms_val = $7;
5679 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005680
Andy Whitcroft554e1652012-01-10 15:09:57 -08005681 if ($ms_size =~ /^(0x|)0$/i) {
5682 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005683 "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 -08005684 } elsif ($ms_size =~ /^(0x|)1$/i) {
5685 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005686 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5687 }
5688 }
5689
Joe Perches98a9bba2014-01-23 15:54:52 -08005690# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5691 if ($^V && $^V ge 5.10.0 &&
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005692 defined $stat &&
5693 $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
Joe Perches98a9bba2014-01-23 15:54:52 -08005694 if (WARN("PREFER_ETHER_ADDR_COPY",
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005695 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
Joe Perches98a9bba2014-01-23 15:54:52 -08005696 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005697 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
Joe Perches98a9bba2014-01-23 15:54:52 -08005698 }
5699 }
5700
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005701# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5702 if ($^V && $^V ge 5.10.0 &&
5703 defined $stat &&
5704 $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5705 WARN("PREFER_ETHER_ADDR_EQUAL",
5706 "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5707 }
5708
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005709# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5710# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5711 if ($^V && $^V ge 5.10.0 &&
5712 defined $stat &&
5713 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5714
5715 my $ms_val = $7;
5716
5717 if ($ms_val =~ /^(?:0x|)0+$/i) {
5718 if (WARN("PREFER_ETH_ZERO_ADDR",
5719 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5720 $fix) {
5721 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5722 }
5723 } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5724 if (WARN("PREFER_ETH_BROADCAST_ADDR",
5725 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5726 $fix) {
5727 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5728 }
5729 }
5730 }
5731
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005732# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005733 if ($^V && $^V ge 5.10.0 &&
5734 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005735 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005736 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005737 my $call = $1;
5738 my $cast1 = deparenthesize($2);
5739 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005740 my $cast2 = deparenthesize($7);
5741 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005742 my $cast;
5743
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005744 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005745 $cast = "$cast1 or $cast2";
5746 } elsif ($cast1 ne "") {
5747 $cast = $cast1;
5748 } else {
5749 $cast = $cast2;
5750 }
5751 WARN("MINMAX",
5752 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005753 }
5754 }
5755
Joe Perches4a273192012-07-30 14:41:20 -07005756# check usleep_range arguments
5757 if ($^V && $^V ge 5.10.0 &&
5758 defined $stat &&
5759 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5760 my $min = $1;
5761 my $max = $7;
5762 if ($min eq $max) {
5763 WARN("USLEEP_RANGE",
5764 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5765 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5766 $min > $max) {
5767 WARN("USLEEP_RANGE",
5768 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5769 }
5770 }
5771
Joe Perches823b7942013-11-12 15:10:15 -08005772# check for naked sscanf
5773 if ($^V && $^V ge 5.10.0 &&
5774 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005775 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005776 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5777 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5778 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5779 my $lc = $stat =~ tr@\n@@;
5780 $lc = $lc + $linenr;
5781 my $stat_real = raw_line($linenr, 0);
5782 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5783 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5784 }
5785 WARN("NAKED_SSCANF",
5786 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5787 }
5788
Joe Perchesafc819a2014-06-04 16:12:08 -07005789# check for simple sscanf that should be kstrto<foo>
5790 if ($^V && $^V ge 5.10.0 &&
5791 defined $stat &&
5792 $line =~ /\bsscanf\b/) {
5793 my $lc = $stat =~ tr@\n@@;
5794 $lc = $lc + $linenr;
5795 my $stat_real = raw_line($linenr, 0);
5796 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5797 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5798 }
5799 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5800 my $format = $6;
5801 my $count = $format =~ tr@%@%@;
5802 if ($count == 1 &&
5803 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5804 WARN("SSCANF_TO_KSTRTO",
5805 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5806 }
5807 }
5808 }
5809
Joe Perches70dc8a42013-09-11 14:23:58 -07005810# check for new externs in .h files.
5811 if ($realfile =~ /\.h$/ &&
5812 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005813 if (CHK("AVOID_EXTERNS",
5814 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005815 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005816 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005817 }
5818 }
5819
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005820# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005821 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005822 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005823 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005824 my $function_name = $1;
5825 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005826
5827 my $s = $stat;
5828 if (defined $cond) {
5829 substr($s, 0, length($cond), '');
5830 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005831 if ($s =~ /^\s*;/ &&
5832 $function_name ne 'uninitialized_var')
5833 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005834 WARN("AVOID_EXTERNS",
5835 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005836 }
5837
5838 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005839 WARN("FUNCTION_ARGUMENTS",
5840 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005841 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005842
5843 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5844 $stat =~ /^.\s*extern\s+/)
5845 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005846 WARN("AVOID_EXTERNS",
5847 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005848 }
5849
5850# checks for new __setup's
5851 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5852 my $name = $1;
5853
5854 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005855 CHK("UNDOCUMENTED_SETUP",
5856 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005857 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005858 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005859
5860# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005861 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005862 WARN("UNNECESSARY_CASTS",
5863 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005864 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005865
Joe Perchesa640d252013-07-03 15:05:21 -07005866# alloc style
5867# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5868 if ($^V && $^V ge 5.10.0 &&
5869 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5870 CHK("ALLOC_SIZEOF_STRUCT",
5871 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5872 }
5873
Joe Perches60a55362014-06-04 16:12:07 -07005874# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5875 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005876 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005877 my $oldfunc = $3;
5878 my $a1 = $4;
5879 my $a2 = $10;
5880 my $newfunc = "kmalloc_array";
5881 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005882 my $r1 = $a1;
5883 my $r2 = $a2;
5884 if ($a1 =~ /^sizeof\s*\S/) {
5885 $r1 = $a2;
5886 $r2 = $a1;
5887 }
5888 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5889 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005890 if (WARN("ALLOC_WITH_MULTIPLY",
5891 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5892 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005893 $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 -07005894
5895 }
5896 }
5897 }
5898
Joe Perches972fdea2013-04-29 16:18:12 -07005899# check for krealloc arg reuse
5900 if ($^V && $^V ge 5.10.0 &&
5901 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5902 WARN("KREALLOC_ARG_REUSE",
5903 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5904 }
5905
Joe Perches5ce59ae2013-02-21 16:44:18 -08005906# check for alloc argument mismatch
5907 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5908 WARN("ALLOC_ARRAY_ARGS",
5909 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5910 }
5911
Joe Perchescaf2a542011-01-12 16:59:56 -08005912# check for multiple semicolons
5913 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005914 if (WARN("ONE_SEMICOLON",
5915 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5916 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005917 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005918 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005919 }
5920
Tomas Winklercec3aaa2016-08-02 14:04:39 -07005921# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5922 if ($realfile !~ m@^include/uapi/@ &&
5923 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08005924 my $ull = "";
5925 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5926 if (CHK("BIT_MACRO",
5927 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5928 $fix) {
5929 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5930 }
5931 }
5932
Joe Perches2d632742016-05-20 17:04:00 -07005933# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
5934 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
5935 my $config = $1;
5936 if (WARN("PREFER_IS_ENABLED",
5937 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
5938 $fix) {
5939 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
5940 }
5941 }
5942
Joe Perchese81f2392014-08-06 16:11:25 -07005943# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005944 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5945 my $has_break = 0;
5946 my $has_statement = 0;
5947 my $count = 0;
5948 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005949 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005950 $prevline--;
5951 my $rline = $rawlines[$prevline - 1];
5952 my $fline = $lines[$prevline - 1];
5953 last if ($fline =~ /^\@\@/);
5954 next if ($fline =~ /^\-/);
5955 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5956 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5957 next if ($fline =~ /^.[\s$;]*$/);
5958 $has_statement = 1;
5959 $count++;
5960 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5961 }
5962 if (!$has_break && $has_statement) {
5963 WARN("MISSING_BREAK",
5964 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5965 }
5966 }
5967
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005968# check for switch/default statements without a break;
5969 if ($^V && $^V ge 5.10.0 &&
5970 defined $stat &&
5971 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5972 my $ctx = '';
5973 my $herectx = $here . "\n";
5974 my $cnt = statement_rawlines($stat);
5975 for (my $n = 0; $n < $cnt; $n++) {
5976 $herectx .= raw_line($linenr, $n) . "\n";
5977 }
5978 WARN("DEFAULT_NO_BREAK",
5979 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005980 }
5981
Patrick Pannutof370e252010-07-23 13:34:18 -07005982# check for return codes on error paths
5983 if ($line =~ /\breturn\s+-\d+/) {
5984 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07005985 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07005986 }
5987
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005988# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005989 if ($line =~ /\b__FUNCTION__\b/) {
5990 if (WARN("USE_FUNC",
5991 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5992 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005993 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005994 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005995 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005996
Joe Perches62ec8182015-02-13 14:38:18 -08005997# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5998 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5999 ERROR("DATE_TIME",
6000 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6001 }
6002
Joe Perches2c924882012-03-23 15:02:20 -07006003# check for use of yield()
6004 if ($line =~ /\byield\s*\(\s*\)/) {
6005 WARN("YIELD",
6006 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6007 }
6008
Joe Perches179f8f42013-07-03 15:05:30 -07006009# check for comparisons against true and false
6010 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6011 my $lead = $1;
6012 my $arg = $2;
6013 my $test = $3;
6014 my $otype = $4;
6015 my $trail = $5;
6016 my $op = "!";
6017
6018 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6019
6020 my $type = lc($otype);
6021 if ($type =~ /^(?:true|false)$/) {
6022 if (("$test" eq "==" && "$type" eq "true") ||
6023 ("$test" eq "!=" && "$type" eq "false")) {
6024 $op = "";
6025 }
6026
6027 CHK("BOOL_COMPARISON",
6028 "Using comparison to $otype is error prone\n" . $herecurr);
6029
6030## maybe suggesting a correct construct would better
6031## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6032
6033 }
6034 }
6035
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006036# check for semaphores initialized locked
6037 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006038 WARN("CONSIDER_COMPLETION",
6039 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006040 }
Joe Perches6712d852012-03-23 15:02:20 -07006041
Joe Perches67d0a072011-10-31 17:13:10 -07006042# recommend kstrto* over simple_strto* and strict_strto*
6043 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006044 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006045 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006046 }
Joe Perches6712d852012-03-23 15:02:20 -07006047
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006048# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006049 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006050 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006051 "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 -07006052 }
Joe Perches6712d852012-03-23 15:02:20 -07006053
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006054# check for various structs that are normally const (ops, kgdb, device_tree)
6055 my $const_structs = qr{
6056 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006057 address_space_operations|
6058 backlight_ops|
6059 block_device_operations|
6060 dentry_operations|
6061 dev_pm_ops|
6062 dma_map_ops|
6063 extent_io_ops|
6064 file_lock_operations|
6065 file_operations|
6066 hv_ops|
6067 ide_dma_ops|
6068 intel_dvo_dev_ops|
6069 item_operations|
6070 iwl_ops|
6071 kgdb_arch|
6072 kgdb_io|
6073 kset_uevent_ops|
6074 lock_manager_operations|
6075 microcode_ops|
6076 mtrr_ops|
6077 neigh_ops|
6078 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006079 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08006080 pci_raw_ops|
6081 pipe_buf_operations|
6082 platform_hibernation_ops|
6083 platform_suspend_ops|
6084 proto_ops|
6085 rpc_pipe_ops|
6086 seq_operations|
6087 snd_ac97_build_ops|
6088 soc_pcmcia_socket_ops|
6089 stacktrace_ops|
6090 sysfs_ops|
6091 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07006092 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006093 usb_mon_operations|
6094 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006095 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006096 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006097 WARN("CONST_STRUCT",
6098 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006099 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006100 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006101
6102# use of NR_CPUS is usually wrong
6103# ignore definitions of NR_CPUS and usage to define arrays as likely right
6104 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006105 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6106 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006107 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6108 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6109 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006110 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006111 WARN("NR_CPUS",
6112 "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 -07006113 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006114
Joe Perches52ea8502013-11-12 15:10:09 -08006115# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6116 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6117 ERROR("DEFINE_ARCH_HAS",
6118 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6119 }
6120
Joe Perchesacd93622015-02-13 14:38:38 -08006121# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6122 if ($^V && $^V ge 5.10.0 &&
6123 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6124 WARN("LIKELY_MISUSE",
6125 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6126 }
6127
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006128# whine mightly about in_atomic
6129 if ($line =~ /\bin_atomic\s*\(/) {
6130 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006131 ERROR("IN_ATOMIC",
6132 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006133 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006134 WARN("IN_ATOMIC",
6135 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006136 }
6137 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006138
Joe Perches481aea52016-05-20 17:04:08 -07006139# whine about ACCESS_ONCE
6140 if ($^V && $^V ge 5.10.0 &&
6141 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6142 my $par = $1;
6143 my $eq = $2;
6144 my $fun = $3;
6145 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6146 if (defined($eq)) {
6147 if (WARN("PREFER_WRITE_ONCE",
6148 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6149 $fix) {
6150 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6151 }
6152 } else {
6153 if (WARN("PREFER_READ_ONCE",
6154 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6155 $fix) {
6156 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6157 }
6158 }
6159 }
6160
Peter Zijlstra1704f472010-03-19 01:37:42 +01006161# check for lockdep_set_novalidate_class
6162 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6163 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6164 if ($realfile !~ m@^kernel/lockdep@ &&
6165 $realfile !~ m@^include/linux/lockdep@ &&
6166 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006167 ERROR("LOCKDEP",
6168 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006169 }
6170 }
Dave Jones88f88312011-01-12 16:59:59 -08006171
Joe Perchesb392c642015-04-16 12:44:16 -07006172 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6173 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006174 WARN("EXPORTED_WORLD_WRITABLE",
6175 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006176 }
Joe Perches24358802014-04-03 14:49:13 -07006177
Joe Perches515a2352014-04-03 14:49:24 -07006178# Mode permission misuses where it seems decimal should be octal
6179# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6180 if ($^V && $^V ge 5.10.0 &&
6181 $line =~ /$mode_perms_search/) {
6182 foreach my $entry (@mode_permission_funcs) {
6183 my $func = $entry->[0];
6184 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006185
Joe Perches515a2352014-04-03 14:49:24 -07006186 my $skip_args = "";
6187 if ($arg_pos > 1) {
6188 $arg_pos--;
6189 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6190 }
6191 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
6192 if ($line =~ /$test/) {
6193 my $val = $1;
6194 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07006195
Joe Perches515a2352014-04-03 14:49:24 -07006196 if ($val !~ /^0$/ &&
6197 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6198 length($val) ne 4)) {
6199 ERROR("NON_OCTAL_PERMISSIONS",
6200 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08006201 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6202 ERROR("EXPORTED_WORLD_WRITABLE",
6203 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07006204 }
Joe Perches24358802014-04-03 14:49:13 -07006205 }
6206 }
6207 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006208
6209# validate content of MODULE_LICENSE against list from include/linux/module.h
6210 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6211 my $extracted_string = get_quoted_string($line, $rawline);
6212 my $valid_licenses = qr{
6213 GPL|
6214 GPL\ v2|
6215 GPL\ and\ additional\ rights|
6216 Dual\ BSD/GPL|
6217 Dual\ MIT/GPL|
6218 Dual\ MPL/GPL|
6219 Proprietary
6220 }x;
6221 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6222 WARN("MODULE_LICENSE",
6223 "unknown module license " . $extracted_string . "\n" . $herecurr);
6224 }
6225 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006226 }
6227
6228 # If we have no input at all, then there is nothing to report on
6229 # so just keep quiet.
6230 if ($#rawlines == -1) {
6231 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006232 }
6233
Andy Whitcroft8905a672007-11-28 16:21:06 -08006234 # In mailback mode only produce a report in the negative, for
6235 # things that appear to be patches.
6236 if ($mailback && ($clean == 1 || !$is_patch)) {
6237 exit(0);
6238 }
6239
6240 # This is not a patch, and we are are in 'no-patch' mode so
6241 # just keep quiet.
6242 if (!$chk_patch && !$is_patch) {
6243 exit(0);
6244 }
6245
Joe Perches06330fc2015-06-25 15:03:11 -07006246 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006247 ERROR("NOT_UNIFIED_DIFF",
6248 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006249 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006250 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006251 ERROR("MISSING_SIGN_OFF",
6252 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006253 }
6254
Andy Whitcroft8905a672007-11-28 16:21:06 -08006255 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006256 if ($summary && !($clean == 1 && $quiet == 1)) {
6257 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006258 print "total: $cnt_error errors, $cnt_warn warnings, " .
6259 (($check)? "$cnt_chk checks, " : "") .
6260 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006261 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006262
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006263 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006264 # If there were any defects found and not already fixing them
6265 if (!$clean and !$fix) {
6266 print << "EOM"
6267
6268NOTE: For some of the reported defects, checkpatch may be able to
6269 mechanically convert to the typical style using --fix or --fix-inplace.
6270EOM
6271 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006272 # If there were whitespace errors which cleanpatch can fix
6273 # then suggest that.
6274 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006275 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006276 print << "EOM"
6277
6278NOTE: Whitespace errors detected.
6279 You may wish to use scripts/cleanpatch or scripts/cleanfile
6280EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006281 }
6282 }
6283
Joe Perchesd752fcc2014-08-06 16:11:05 -07006284 if ($clean == 0 && $fix &&
6285 ("@rawlines" ne "@fixed" ||
6286 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006287 my $newfile = $filename;
6288 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006289 my $linecount = 0;
6290 my $f;
6291
Joe Perchesd752fcc2014-08-06 16:11:05 -07006292 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6293
Joe Perches3705ce52013-07-03 15:05:31 -07006294 open($f, '>', $newfile)
6295 or die "$P: Can't open $newfile for write\n";
6296 foreach my $fixed_line (@fixed) {
6297 $linecount++;
6298 if ($file) {
6299 if ($linecount > 3) {
6300 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006301 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006302 }
6303 } else {
6304 print $f $fixed_line . "\n";
6305 }
6306 }
6307 close($f);
6308
6309 if (!$quiet) {
6310 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006311
Joe Perches3705ce52013-07-03 15:05:31 -07006312Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6313
6314Do _NOT_ trust the results written to this file.
6315Do _NOT_ submit these changes without inspecting them for correctness.
6316
6317This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6318No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006319EOM
6320 }
6321 }
6322
Joe Perchesd8469f12015-06-25 15:03:00 -07006323 if ($quiet == 0) {
6324 print "\n";
6325 if ($clean == 1) {
6326 print "$vname has no obvious style problems and is ready for submission.\n";
6327 } else {
6328 print "$vname has style problems, please review.\n";
6329 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006330 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006331 return $clean;
6332}