blob: 374abf44363615004537cb895d3b55bc410ae99b [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;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070010
11my $P = $0;
Kees Cook66b47b42014-10-13 15:51:57 -070012$P =~ s@(.*)/@@g;
13my $D = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070014
Joe Perches000d1cc12011-07-25 17:13:25 -070015my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016
17use Getopt::Long qw(:config no_auto_abbrev);
18
19my $quiet = 0;
20my $tree = 1;
21my $chk_signoff = 1;
22my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070023my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080025my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070026my $file = 0;
27my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070028my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080029my $summary = 1;
30my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080031my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070032my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070033my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080034my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080036my %debug;
Joe Perches34456862013-07-03 15:05:34 -070037my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070038my %use_type = ();
39my @use = ();
40my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070041my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070042my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070043my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080044my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070045my $ignore_perl_version = 0;
46my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070047my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070048my $spelling_file = "$D/spelling.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070049
50sub help {
51 my ($exitcode) = @_;
52
53 print << "EOM";
54Usage: $P [OPTION]... [FILE]...
55Version: $V
56
57Options:
58 -q, --quiet quiet
59 --no-tree run without a kernel tree
60 --no-signoff do not check for 'Signed-off-by' line
61 --patch treat FILE as patchfile (default)
62 --emacs emacs compile window format
63 --terse one line per report
64 -f, --file treat FILE as regular source file
65 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070066 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070067 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080068 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070069 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070070 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070071 --root=PATH PATH to the kernel tree root
72 --no-summary suppress the per-file summary
73 --mailback only produce a report in case of warnings/errors
74 --summary-file include the filename in summary
75 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
76 'values', 'possible', 'type', and 'attr' (default
77 is all off)
78 --test-only=WORD report only warnings/errors containing WORD
79 literally
Joe Perches3705ce52013-07-03 15:05:31 -070080 --fix EXPERIMENTAL - may create horrible results
81 If correctable single-line errors exist, create
82 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
83 with potential errors corrected to the preferred
84 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080085 --fix-inplace EXPERIMENTAL - may create horrible results
86 Is the same as --fix, but overwrites the input
87 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070088 --ignore-perl-version override checking of perl version. expect
89 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070090 -h, --help, --version display this help and exit
91
92When FILE is - read standard input.
93EOM
94
95 exit($exitcode);
96}
97
Joe Perches000d1cc12011-07-25 17:13:25 -070098my $conf = which_conf($configuration_file);
99if (-f $conf) {
100 my @conf_args;
101 open(my $conffile, '<', "$conf")
102 or warn "$P: Can't find a readable $configuration_file file $!\n";
103
104 while (<$conffile>) {
105 my $line = $_;
106
107 $line =~ s/\s*\n?$//g;
108 $line =~ s/^\s*//g;
109 $line =~ s/\s+/ /g;
110
111 next if ($line =~ m/^\s*#/);
112 next if ($line =~ m/^\s*$/);
113
114 my @words = split(" ", $line);
115 foreach my $word (@words) {
116 last if ($word =~ m/^#/);
117 push (@conf_args, $word);
118 }
119 }
120 close($conffile);
121 unshift(@ARGV, @conf_args) if @conf_args;
122}
123
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700124GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700126 'tree!' => \$tree,
127 'signoff!' => \$chk_signoff,
128 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700129 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800130 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700131 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700132 'subjective!' => \$check,
133 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700134 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700135 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700136 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800137 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700138 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700139 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800140 'summary!' => \$summary,
141 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800142 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700143 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800144 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700145 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800146 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700147 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700148 'h|help' => \$help,
149 'version' => \$help
150) or help(1);
151
152help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700153
Joe Perches9624b8d2014-01-23 15:54:44 -0800154$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700155$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800156
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700157my $exit = 0;
158
Dave Hansend62a2012013-09-11 14:23:56 -0700159if ($^V && $^V lt $minimum_perl_version) {
160 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
161 if (!$ignore_perl_version) {
162 exit(1);
163 }
164}
165
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700166if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700167 print "$P: no input files\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700168 exit(1);
169}
170
Joe Perches91bfe482013-09-11 14:23:59 -0700171sub hash_save_array_words {
172 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700173
Joe Perches91bfe482013-09-11 14:23:59 -0700174 my @array = split(/,/, join(',', @$arrayRef));
175 foreach my $word (@array) {
176 $word =~ s/\s*\n?$//g;
177 $word =~ s/^\s*//g;
178 $word =~ s/\s+/ /g;
179 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700180
Joe Perches91bfe482013-09-11 14:23:59 -0700181 next if ($word =~ m/^\s*#/);
182 next if ($word =~ m/^\s*$/);
183
184 $hashRef->{$word}++;
185 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700186}
187
Joe Perches91bfe482013-09-11 14:23:59 -0700188sub hash_show_words {
189 my ($hashRef, $prefix) = @_;
190
Joe Perches58cb3cf2013-09-11 14:24:04 -0700191 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700192 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700193 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700194 print " $word";
195 }
196 print "\n\n";
197 }
198}
199
200hash_save_array_words(\%ignore_type, \@ignore);
201hash_save_array_words(\%use_type, \@use);
202
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800203my $dbg_values = 0;
204my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700205my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700206my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800207for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800208 ## no critic
209 eval "\${dbg_$key} = '$debug{$key}';";
210 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800211}
212
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700213my $rpt_cleaners = 0;
214
Andy Whitcroft8905a672007-11-28 16:21:06 -0800215if ($terse) {
216 $emacs = 1;
217 $quiet++;
218}
219
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700220if ($tree) {
221 if (defined $root) {
222 if (!top_of_kernel_tree($root)) {
223 die "$P: $root: --root does not point at a valid tree\n";
224 }
225 } else {
226 if (top_of_kernel_tree('.')) {
227 $root = '.';
228 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
229 top_of_kernel_tree($1)) {
230 $root = $1;
231 }
232 }
233
234 if (!defined $root) {
235 print "Must be run from the top-level dir. of a kernel tree\n";
236 exit(2);
237 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700238}
239
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700240my $emitted_corrupt = 0;
241
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700242our $Ident = qr{
243 [A-Za-z_][A-Za-z\d_]*
244 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
245 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700246our $Storage = qr{extern|static|asmlinkage};
247our $Sparse = qr{
248 __user|
249 __kernel|
250 __force|
251 __iomem|
252 __must_check|
253 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800254 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700255 __ref|
256 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700257 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800258our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
259our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
260our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
261our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
262our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700263
Wolfram Sang52131292010-03-05 13:43:51 -0800264# Notes to $Attribute:
265# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700266our $Attribute = qr{
267 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700268 __percpu|
269 __nocast|
270 __safe|
271 __bitwise__|
272 __packed__|
273 __packed2__|
274 __naked|
275 __maybe_unused|
276 __always_unused|
277 __noreturn|
278 __used|
279 __cold|
280 __noclone|
281 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700282 __read_mostly|
283 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700284 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700285 ____cacheline_aligned|
286 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800287 ____cacheline_internodealigned_in_smp|
288 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700289 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700290our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700291our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700292our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
293our $Lval = qr{$Ident(?:$Member)*};
294
Joe Perches95e2c602013-07-03 15:05:20 -0700295our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
296our $Binary = qr{(?i)0b[01]+$Int_type?};
297our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
298our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700299our $Octal = qr{0[0-7]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800300our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
301our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
302our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800303our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700304our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800305our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700306our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700307our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700308our $Operators = qr{
309 <=|>=|==|!=|
310 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700311 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700312 }x;
313
Joe Perches91cb5192014-04-03 14:49:32 -0700314our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
315
Andy Whitcroft8905a672007-11-28 16:21:06 -0800316our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700317our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700318our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800319our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700320our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800321our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700322our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800323
Joe Perches15662b32011-10-31 17:13:12 -0700324our $NON_ASCII_UTF8 = qr{
325 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700326 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
327 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
328 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
329 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
330 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
331 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
332}x;
333
Joe Perches15662b32011-10-31 17:13:12 -0700334our $UTF8 = qr{
335 [\x09\x0A\x0D\x20-\x7E] # ASCII
336 | $NON_ASCII_UTF8
337}x;
338
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700339our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700340 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700341 atomic_t
342)};
343
Joe Perches691e6692010-03-05 13:43:51 -0800344our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700345 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700346 (?:[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 -0700347 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700348 panic|
Joe Perches06668722013-11-12 15:10:07 -0800349 MODULE_[A-Z_]+|
350 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800351)};
352
Joe Perches20112472011-07-25 17:13:23 -0700353our $signature_tags = qr{(?xi:
354 Signed-off-by:|
355 Acked-by:|
356 Tested-by:|
357 Reviewed-by:|
358 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700359 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700360 To:|
361 Cc:
362)};
363
Joe Perches18130872014-08-06 16:11:22 -0700364our @typeListMisordered = (
365 qr{char\s+(?:un)?signed},
366 qr{int\s+(?:(?:un)?signed\s+)?short\s},
367 qr{int\s+short(?:\s+(?:un)?signed)},
368 qr{short\s+int(?:\s+(?:un)?signed)},
369 qr{(?:un)?signed\s+int\s+short},
370 qr{short\s+(?:un)?signed},
371 qr{long\s+int\s+(?:un)?signed},
372 qr{int\s+long\s+(?:un)?signed},
373 qr{long\s+(?:un)?signed\s+int},
374 qr{int\s+(?:un)?signed\s+long},
375 qr{int\s+(?:un)?signed},
376 qr{int\s+long\s+long\s+(?:un)?signed},
377 qr{long\s+long\s+int\s+(?:un)?signed},
378 qr{long\s+long\s+(?:un)?signed\s+int},
379 qr{long\s+long\s+(?:un)?signed},
380 qr{long\s+(?:un)?signed},
381);
382
Andy Whitcroft8905a672007-11-28 16:21:06 -0800383our @typeList = (
384 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700385 qr{(?:(?:un)?signed\s+)?char},
386 qr{(?:(?:un)?signed\s+)?short\s+int},
387 qr{(?:(?:un)?signed\s+)?short},
388 qr{(?:(?:un)?signed\s+)?int},
389 qr{(?:(?:un)?signed\s+)?long\s+int},
390 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
391 qr{(?:(?:un)?signed\s+)?long\s+long},
392 qr{(?:(?:un)?signed\s+)?long},
393 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800394 qr{float},
395 qr{double},
396 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397 qr{struct\s+$Ident},
398 qr{union\s+$Ident},
399 qr{enum\s+$Ident},
400 qr{${Ident}_t},
401 qr{${Ident}_handler},
402 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700403 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800404);
Joe Perches8716de32013-09-11 14:24:05 -0700405our @typeListWithAttr = (
406 @typeList,
407 qr{struct\s+$InitAttribute\s+$Ident},
408 qr{union\s+$InitAttribute\s+$Ident},
409);
410
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700411our @modifierList = (
412 qr{fastcall},
413);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800414
Joe Perches24358802014-04-03 14:49:13 -0700415our @mode_permission_funcs = (
416 ["module_param", 3],
417 ["module_param_(?:array|named|string)", 4],
418 ["module_param_array_named", 5],
419 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
420 ["proc_create(?:_data|)", 2],
421 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
422);
423
Joe Perches515a2352014-04-03 14:49:24 -0700424#Create a search pattern for all these functions to speed up a loop below
425our $mode_perms_search = "";
426foreach my $entry (@mode_permission_funcs) {
427 $mode_perms_search .= '|' if ($mode_perms_search ne "");
428 $mode_perms_search .= $entry->[0];
429}
430
Wolfram Sang7840a942010-08-09 17:20:57 -0700431our $allowed_asm_includes = qr{(?x:
432 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700433 memory|
434 time|
435 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700436)};
437# memory.h: ARM has a custom one
438
Kees Cook66b47b42014-10-13 15:51:57 -0700439# Load common spelling mistakes and build regular expression list.
440my $misspellings;
441my @spelling_list;
442my %spelling_fix;
443open(my $spelling, '<', $spelling_file)
444 or die "$P: Can't open $spelling_file for reading: $!\n";
445while (<$spelling>) {
446 my $line = $_;
447
448 $line =~ s/\s*\n?$//g;
449 $line =~ s/^\s*//g;
450
451 next if ($line =~ m/^\s*#/);
452 next if ($line =~ m/^\s*$/);
453
454 my ($suspect, $fix) = split(/\|\|/, $line);
455
456 push(@spelling_list, $suspect);
457 $spelling_fix{$suspect} = $fix;
458}
459close($spelling);
460$misspellings = join("|", @spelling_list);
461
Andy Whitcroft8905a672007-11-28 16:21:06 -0800462sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700463 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
464 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700465 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700466 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700467 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700469 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800470 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800471 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700472 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700473 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800474 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700475 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800476 }x;
Joe Perches18130872014-08-06 16:11:22 -0700477 $NonptrTypeMisordered = qr{
478 (?:$Modifier\s+|const\s+)*
479 (?:
480 (?:${Misordered}\b)
481 )
482 (?:\s+$Modifier|\s+const)*
483 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700484 $NonptrTypeWithAttr = qr{
485 (?:$Modifier\s+|const\s+)*
486 (?:
487 (?:typeof|__typeof__)\s*\([^\)]*\)|
488 (?:$typeTypedefs\b)|
489 (?:${allWithAttr}\b)
490 )
491 (?:\s+$Modifier|\s+const)*
492 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800493 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700494 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700495 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700496 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800497 }x;
Joe Perches18130872014-08-06 16:11:22 -0700498 $TypeMisordered = qr{
499 $NonptrTypeMisordered
500 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
501 (?:\s+$Inline|\s+$Modifier)*
502 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700503 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700504 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800505}
506build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700507
Joe Perches7d2367a2011-07-25 17:13:22 -0700508our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700509
510# Using $balanced_parens, $LvalOrFunc, or $FuncArg
511# requires at least perl version v5.10.0
512# Any use must be runtime checked with $^V
513
514our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700515our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800516our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700517
Joe Perchesf8422302014-08-06 16:11:31 -0700518our $declaration_macros = qr{(?x:
519 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
520 (?:$Storage\s+)?LIST_HEAD\s*\(|
521 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
522)};
523
Joe Perches7d2367a2011-07-25 17:13:22 -0700524sub deparenthesize {
525 my ($string) = @_;
526 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700527
528 while ($string =~ /^\s*\(.*\)\s*$/) {
529 $string =~ s@^\s*\(\s*@@;
530 $string =~ s@\s*\)\s*$@@;
531 }
532
Joe Perches7d2367a2011-07-25 17:13:22 -0700533 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700534
Joe Perches7d2367a2011-07-25 17:13:22 -0700535 return $string;
536}
537
Joe Perches34456862013-07-03 15:05:34 -0700538sub seed_camelcase_file {
539 my ($file) = @_;
540
541 return if (!(-f $file));
542
543 local $/;
544
545 open(my $include_file, '<', "$file")
546 or warn "$P: Can't read '$file' $!\n";
547 my $text = <$include_file>;
548 close($include_file);
549
550 my @lines = split('\n', $text);
551
552 foreach my $line (@lines) {
553 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
554 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
555 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800556 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
557 $camelcase{$1} = 1;
558 } 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 -0700559 $camelcase{$1} = 1;
560 }
561 }
562}
563
564my $camelcase_seeded = 0;
565sub seed_camelcase_includes {
566 return if ($camelcase_seeded);
567
568 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700569 my $camelcase_cache = "";
570 my @include_files = ();
571
572 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700573
Richard Genoud3645e322014-02-10 14:25:32 -0800574 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700575 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
576 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700577 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700578 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700579 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700580 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700581 @include_files = split('\n', $files);
582 foreach my $file (@include_files) {
583 my $date = POSIX::strftime("%Y%m%d%H%M",
584 localtime((stat $file)[9]));
585 $last_mod_date = $date if ($last_mod_date < $date);
586 }
587 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700588 }
Joe Perchesc707a812013-07-08 16:00:43 -0700589
590 if ($camelcase_cache ne "" && -f $camelcase_cache) {
591 open(my $camelcase_file, '<', "$camelcase_cache")
592 or warn "$P: Can't read '$camelcase_cache' $!\n";
593 while (<$camelcase_file>) {
594 chomp;
595 $camelcase{$_} = 1;
596 }
597 close($camelcase_file);
598
599 return;
600 }
601
Richard Genoud3645e322014-02-10 14:25:32 -0800602 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700603 $files = `git ls-files "include/*.h"`;
604 @include_files = split('\n', $files);
605 }
606
Joe Perches34456862013-07-03 15:05:34 -0700607 foreach my $file (@include_files) {
608 seed_camelcase_file($file);
609 }
Joe Perches351b2a12013-07-03 15:05:36 -0700610
Joe Perchesc707a812013-07-08 16:00:43 -0700611 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700612 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700613 open(my $camelcase_file, '>', "$camelcase_cache")
614 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700615 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
616 print $camelcase_file ("$_\n");
617 }
618 close($camelcase_file);
619 }
Joe Perches34456862013-07-03 15:05:34 -0700620}
621
Joe Perchesd311cd42014-08-06 16:10:57 -0700622sub git_commit_info {
623 my ($commit, $id, $desc) = @_;
624
625 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
626
627 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
628 $output =~ s/^\s*//gm;
629 my @lines = split("\n", $output);
630
631 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
632# Maybe one day convert this block of bash into something that returns
633# all matching commit ids, but it's very slow...
634#
635# echo "checking commits $1..."
636# git rev-list --remotes | grep -i "^$1" |
637# while read line ; do
638# git log --format='%H %s' -1 $line |
639# echo "commit $(cut -c 1-12,41-)"
640# done
641 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
642 } else {
643 $id = substr($lines[0], 0, 12);
644 $desc = substr($lines[0], 41);
645 }
646
647 return ($id, $desc);
648}
649
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700650$chk_signoff = 0 if ($file);
651
Andy Whitcroft00df3442007-06-08 13:47:06 -0700652my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800653my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700654my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700655my @fixed_inserted = ();
656my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700657my $fixlinenr = -1;
658
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800659my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700660for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800661 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700662 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800663 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700664 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800665 } elsif ($filename eq '-') {
666 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700667 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800668 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700669 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700670 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800671 if ($filename eq '-') {
672 $vname = 'Your patch';
673 } else {
674 $vname = $filename;
675 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800676 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700677 chomp;
678 push(@rawlines, $_);
679 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800680 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800681 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700682 $exit = 1;
683 }
684 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800685 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700686 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700687 @fixed_inserted = ();
688 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700689 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700690}
691
692exit($exit);
693
694sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700695 my ($root) = @_;
696
697 my @tree_check = (
698 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
699 "README", "Documentation", "arch", "include", "drivers",
700 "fs", "init", "ipc", "kernel", "lib", "scripts",
701 );
702
703 foreach my $check (@tree_check) {
704 if (! -e $root . '/' . $check) {
705 return 0;
706 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700707 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700708 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700709}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700710
Joe Perches20112472011-07-25 17:13:23 -0700711sub parse_email {
712 my ($formatted_email) = @_;
713
714 my $name = "";
715 my $address = "";
716 my $comment = "";
717
718 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
719 $name = $1;
720 $address = $2;
721 $comment = $3 if defined $3;
722 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
723 $address = $1;
724 $comment = $2 if defined $2;
725 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
726 $address = $1;
727 $comment = $2 if defined $2;
728 $formatted_email =~ s/$address.*$//;
729 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700730 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700731 $name =~ s/^\"|\"$//g;
732 # If there's a name left after stripping spaces and
733 # leading quotes, and the address doesn't have both
734 # leading and trailing angle brackets, the address
735 # is invalid. ie:
736 # "joe smith joe@smith.com" bad
737 # "joe smith <joe@smith.com" bad
738 if ($name ne "" && $address !~ /^<[^>]+>$/) {
739 $name = "";
740 $address = "";
741 $comment = "";
742 }
743 }
744
Joe Perches3705ce52013-07-03 15:05:31 -0700745 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700746 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700747 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700748 $address =~ s/^\<|\>$//g;
749
750 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
751 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
752 $name = "\"$name\"";
753 }
754
755 return ($name, $address, $comment);
756}
757
758sub format_email {
759 my ($name, $address) = @_;
760
761 my $formatted_email;
762
Joe Perches3705ce52013-07-03 15:05:31 -0700763 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700764 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700765 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700766
767 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
768 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
769 $name = "\"$name\"";
770 }
771
772 if ("$name" eq "") {
773 $formatted_email = "$address";
774 } else {
775 $formatted_email = "$name <$address>";
776 }
777
778 return $formatted_email;
779}
780
Joe Perchesd311cd42014-08-06 16:10:57 -0700781sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700782 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700783
Joe Perchesbd474ca2014-08-06 16:11:10 -0700784 foreach my $path (split(/:/, $ENV{PATH})) {
785 if (-e "$path/$bin") {
786 return "$path/$bin";
787 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700788 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700789
Joe Perchesbd474ca2014-08-06 16:11:10 -0700790 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700791}
792
Joe Perches000d1cc12011-07-25 17:13:25 -0700793sub which_conf {
794 my ($conf) = @_;
795
796 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
797 if (-e "$path/$conf") {
798 return "$path/$conf";
799 }
800 }
801
802 return "";
803}
804
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700805sub expand_tabs {
806 my ($str) = @_;
807
808 my $res = '';
809 my $n = 0;
810 for my $c (split(//, $str)) {
811 if ($c eq "\t") {
812 $res .= ' ';
813 $n++;
814 for (; ($n % 8) != 0; $n++) {
815 $res .= ' ';
816 }
817 next;
818 }
819 $res .= $c;
820 $n++;
821 }
822
823 return $res;
824}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700825sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700826 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700827 return $res;
828}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700829
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700830sub line_stats {
831 my ($line) = @_;
832
833 # Drop the diff line leader and expand tabs
834 $line =~ s/^.//;
835 $line = expand_tabs($line);
836
837 # Pick the indent from the front of the line.
838 my ($white) = ($line =~ /^(\s*)/);
839
840 return (length($line), length($white));
841}
842
Andy Whitcroft773647a2008-03-28 14:15:58 -0700843my $sanitise_quote = '';
844
845sub sanitise_line_reset {
846 my ($in_comment) = @_;
847
848 if ($in_comment) {
849 $sanitise_quote = '*/';
850 } else {
851 $sanitise_quote = '';
852 }
853}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700854sub sanitise_line {
855 my ($line) = @_;
856
857 my $res = '';
858 my $l = '';
859
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800860 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700861 my $off = 0;
862 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700863
Andy Whitcroft773647a2008-03-28 14:15:58 -0700864 # Always copy over the diff marker.
865 $res = substr($line, 0, 1);
866
867 for ($off = 1; $off < length($line); $off++) {
868 $c = substr($line, $off, 1);
869
870 # Comments we are wacking completly including the begin
871 # and end, all to $;.
872 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
873 $sanitise_quote = '*/';
874
875 substr($res, $off, 2, "$;$;");
876 $off++;
877 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800878 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700879 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700880 $sanitise_quote = '';
881 substr($res, $off, 2, "$;$;");
882 $off++;
883 next;
884 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700885 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
886 $sanitise_quote = '//';
887
888 substr($res, $off, 2, $sanitise_quote);
889 $off++;
890 next;
891 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700892
893 # A \ in a string means ignore the next character.
894 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
895 $c eq "\\") {
896 substr($res, $off, 2, 'XX');
897 $off++;
898 next;
899 }
900 # Regular quotes.
901 if ($c eq "'" || $c eq '"') {
902 if ($sanitise_quote eq '') {
903 $sanitise_quote = $c;
904
905 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700906 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700907 } elsif ($sanitise_quote eq $c) {
908 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700909 }
910 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700911
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800912 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700913 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
914 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700915 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
916 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700917 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
918 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700919 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700920 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700921 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800922 }
923
Daniel Walker113f04a2009-09-21 17:04:35 -0700924 if ($sanitise_quote eq '//') {
925 $sanitise_quote = '';
926 }
927
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700929 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800930 my $clean = 'X' x length($1);
931 $res =~ s@\<.*\>@<$clean>@;
932
933 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700934 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800935 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700936 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800937 }
938
Andy Whitcroft00df3442007-06-08 13:47:06 -0700939 return $res;
940}
941
Joe Perchesa6962d72013-04-29 16:18:13 -0700942sub get_quoted_string {
943 my ($line, $rawline) = @_;
944
945 return "" if ($line !~ m/(\"[X]+\")/g);
946 return substr($rawline, $-[0], $+[0] - $-[0]);
947}
948
Andy Whitcroft8905a672007-11-28 16:21:06 -0800949sub ctx_statement_block {
950 my ($linenr, $remain, $off) = @_;
951 my $line = $linenr - 1;
952 my $blk = '';
953 my $soff = $off;
954 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700955 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800956
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800957 my $loff = 0;
958
Andy Whitcroft8905a672007-11-28 16:21:06 -0800959 my $type = '';
960 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800961 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800962 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800963 my $c;
964 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800965
966 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800967 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800968 @stack = (['', 0]) if ($#stack == -1);
969
Andy Whitcroft773647a2008-03-28 14:15:58 -0700970 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800971 # If we are about to drop off the end, pull in more
972 # context.
973 if ($off >= $len) {
974 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700975 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800976 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800977 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800978 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800979 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800980 $len = length($blk);
981 $line++;
982 last;
983 }
984 # Bail if there is no further context.
985 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800986 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800987 last;
988 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800989 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
990 $level++;
991 $type = '#';
992 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800993 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800994 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800995 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800996 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800997
Andy Whitcroft773647a2008-03-28 14:15:58 -0700998 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800999
1000 # Handle nested #if/#else.
1001 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1002 push(@stack, [ $type, $level ]);
1003 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1004 ($type, $level) = @{$stack[$#stack - 1]};
1005 } elsif ($remainder =~ /^#\s*endif\b/) {
1006 ($type, $level) = @{pop(@stack)};
1007 }
1008
Andy Whitcroft8905a672007-11-28 16:21:06 -08001009 # Statement ends at the ';' or a close '}' at the
1010 # outermost level.
1011 if ($level == 0 && $c eq ';') {
1012 last;
1013 }
1014
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001015 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001016 if ($level == 0 && $coff_set == 0 &&
1017 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1018 $remainder =~ /^(else)(?:\s|{)/ &&
1019 $remainder !~ /^else\s+if\b/) {
1020 $coff = $off + length($1) - 1;
1021 $coff_set = 1;
1022 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1023 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001024 }
1025
Andy Whitcroft8905a672007-11-28 16:21:06 -08001026 if (($type eq '' || $type eq '(') && $c eq '(') {
1027 $level++;
1028 $type = '(';
1029 }
1030 if ($type eq '(' && $c eq ')') {
1031 $level--;
1032 $type = ($level != 0)? '(' : '';
1033
1034 if ($level == 0 && $coff < $soff) {
1035 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001036 $coff_set = 1;
1037 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001038 }
1039 }
1040 if (($type eq '' || $type eq '{') && $c eq '{') {
1041 $level++;
1042 $type = '{';
1043 }
1044 if ($type eq '{' && $c eq '}') {
1045 $level--;
1046 $type = ($level != 0)? '{' : '';
1047
1048 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001049 if (substr($blk, $off + 1, 1) eq ';') {
1050 $off++;
1051 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001052 last;
1053 }
1054 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001055 # Preprocessor commands end at the newline unless escaped.
1056 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1057 $level--;
1058 $type = '';
1059 $off++;
1060 last;
1061 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001062 $off++;
1063 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001064 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001065 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001066 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001067 $line++;
1068 $remain--;
1069 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001070
1071 my $statement = substr($blk, $soff, $off - $soff + 1);
1072 my $condition = substr($blk, $soff, $coff - $soff + 1);
1073
1074 #warn "STATEMENT<$statement>\n";
1075 #warn "CONDITION<$condition>\n";
1076
Andy Whitcroft773647a2008-03-28 14:15:58 -07001077 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001078
1079 return ($statement, $condition,
1080 $line, $remain + 1, $off - $loff + 1, $level);
1081}
1082
Andy Whitcroftcf655042008-03-04 14:28:20 -08001083sub statement_lines {
1084 my ($stmt) = @_;
1085
1086 # Strip the diff line prefixes and rip blank lines at start and end.
1087 $stmt =~ s/(^|\n)./$1/g;
1088 $stmt =~ s/^\s*//;
1089 $stmt =~ s/\s*$//;
1090
1091 my @stmt_lines = ($stmt =~ /\n/g);
1092
1093 return $#stmt_lines + 2;
1094}
1095
1096sub statement_rawlines {
1097 my ($stmt) = @_;
1098
1099 my @stmt_lines = ($stmt =~ /\n/g);
1100
1101 return $#stmt_lines + 2;
1102}
1103
1104sub statement_block_size {
1105 my ($stmt) = @_;
1106
1107 $stmt =~ s/(^|\n)./$1/g;
1108 $stmt =~ s/^\s*{//;
1109 $stmt =~ s/}\s*$//;
1110 $stmt =~ s/^\s*//;
1111 $stmt =~ s/\s*$//;
1112
1113 my @stmt_lines = ($stmt =~ /\n/g);
1114 my @stmt_statements = ($stmt =~ /;/g);
1115
1116 my $stmt_lines = $#stmt_lines + 2;
1117 my $stmt_statements = $#stmt_statements + 1;
1118
1119 if ($stmt_lines > $stmt_statements) {
1120 return $stmt_lines;
1121 } else {
1122 return $stmt_statements;
1123 }
1124}
1125
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001126sub ctx_statement_full {
1127 my ($linenr, $remain, $off) = @_;
1128 my ($statement, $condition, $level);
1129
1130 my (@chunks);
1131
Andy Whitcroftcf655042008-03-04 14:28:20 -08001132 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001133 ($statement, $condition, $linenr, $remain, $off, $level) =
1134 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001135 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001136 push(@chunks, [ $condition, $statement ]);
1137 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1138 return ($level, $linenr, @chunks);
1139 }
1140
1141 # Pull in the following conditional/block pairs and see if they
1142 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001143 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001144 ($statement, $condition, $linenr, $remain, $off, $level) =
1145 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001146 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001147 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001148 #print "C: push\n";
1149 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001150 }
1151
1152 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001153}
1154
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001155sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001156 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001157 my $line;
1158 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001159 my $blk = '';
1160 my @o;
1161 my @c;
1162 my @res = ();
1163
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001164 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001165 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001166 for ($line = $start; $remain > 0; $line++) {
1167 next if ($rawlines[$line] =~ /^-/);
1168 $remain--;
1169
1170 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001171
1172 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001173 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001174 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001175 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001176 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001177 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001178 $level = pop(@stack);
1179 }
1180
Andy Whitcroft01464f32010-10-26 14:23:19 -07001181 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001182 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1183 if ($off > 0) {
1184 $off--;
1185 next;
1186 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001187
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001188 if ($c eq $close && $level > 0) {
1189 $level--;
1190 last if ($level == 0);
1191 } elsif ($c eq $open) {
1192 $level++;
1193 }
1194 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001195
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001196 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001197 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001198 }
1199
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001200 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001201 }
1202
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001203 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001204}
1205sub ctx_block_outer {
1206 my ($linenr, $remain) = @_;
1207
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001208 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1209 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001210}
1211sub ctx_block {
1212 my ($linenr, $remain) = @_;
1213
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001214 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1215 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001216}
1217sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001218 my ($linenr, $remain, $off) = @_;
1219
1220 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1221 return @r;
1222}
1223sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001224 my ($linenr, $remain) = @_;
1225
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001226 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001227}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001228sub ctx_statement_level {
1229 my ($linenr, $remain, $off) = @_;
1230
1231 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1232}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001233
1234sub ctx_locate_comment {
1235 my ($first_line, $end_line) = @_;
1236
1237 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001238 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001239 return $current_comment if (defined $current_comment);
1240
1241 # Look through the context and try and figure out if there is a
1242 # comment.
1243 my $in_comment = 0;
1244 $current_comment = '';
1245 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001246 my $line = $rawlines[$linenr - 1];
1247 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001248 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1249 $in_comment = 1;
1250 }
1251 if ($line =~ m@/\*@) {
1252 $in_comment = 1;
1253 }
1254 if (!$in_comment && $current_comment ne '') {
1255 $current_comment = '';
1256 }
1257 $current_comment .= $line . "\n" if ($in_comment);
1258 if ($line =~ m@\*/@) {
1259 $in_comment = 0;
1260 }
1261 }
1262
1263 chomp($current_comment);
1264 return($current_comment);
1265}
1266sub ctx_has_comment {
1267 my ($first_line, $end_line) = @_;
1268 my $cmt = ctx_locate_comment($first_line, $end_line);
1269
Andy Whitcroft00df3442007-06-08 13:47:06 -07001270 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001271 ##print "CMMT: $cmt\n";
1272
1273 return ($cmt ne '');
1274}
1275
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001276sub raw_line {
1277 my ($linenr, $cnt) = @_;
1278
1279 my $offset = $linenr - 1;
1280 $cnt++;
1281
1282 my $line;
1283 while ($cnt) {
1284 $line = $rawlines[$offset++];
1285 next if (defined($line) && $line =~ /^-/);
1286 $cnt--;
1287 }
1288
1289 return $line;
1290}
1291
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001292sub cat_vet {
1293 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001294 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001295
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001296 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001297 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1298 $res .= $1;
1299 if ($2 ne '') {
1300 $coded = sprintf("^%c", unpack('C', $2) + 64);
1301 $res .= $coded;
1302 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001303 }
1304 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001305
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001306 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001307}
1308
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001309my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001310my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001311my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001312my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001313
1314sub annotate_reset {
1315 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001316 $av_pending = '_';
1317 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001318 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001319}
1320
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001321sub annotate_values {
1322 my ($stream, $type) = @_;
1323
1324 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001325 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326 my $cur = $stream;
1327
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001328 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001329
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001330 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001331 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001332 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001333 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001335 print "WS($1)\n" if ($dbg_values > 1);
1336 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001337 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001338 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001339 }
1340
Florian Micklerc023e4732011-01-12 16:59:58 -08001341 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001342 print "CAST($1)\n" if ($dbg_values > 1);
1343 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001344 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001345
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001346 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001347 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001348 $type = 'T';
1349
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001350 } elsif ($cur =~ /^($Modifier)\s*/) {
1351 print "MODIFIER($1)\n" if ($dbg_values > 1);
1352 $type = 'T';
1353
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001354 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001355 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001356 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001357 push(@av_paren_type, $type);
1358 if ($2 ne '') {
1359 $av_pending = 'N';
1360 }
1361 $type = 'E';
1362
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001363 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001364 print "UNDEF($1)\n" if ($dbg_values > 1);
1365 $av_preprocessor = 1;
1366 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001367
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001368 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001369 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001370 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001371
1372 push(@av_paren_type, $type);
1373 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001374 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001376 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001377 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1378 $av_preprocessor = 1;
1379
1380 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1381
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001382 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001383
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001384 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001385 print "PRE_END($1)\n" if ($dbg_values > 1);
1386
1387 $av_preprocessor = 1;
1388
1389 # Assume all arms of the conditional end as this
1390 # one does, and continue as if the #endif was not here.
1391 pop(@av_paren_type);
1392 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001393 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001394
1395 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001396 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001397
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001398 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1399 print "ATTR($1)\n" if ($dbg_values > 1);
1400 $av_pending = $type;
1401 $type = 'N';
1402
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001403 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001405 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001406 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001407 }
1408 $type = 'N';
1409
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001410 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001412 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413 $type = 'N';
1414
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001415 } elsif ($cur =~/^(case)/o) {
1416 print "CASE($1)\n" if ($dbg_values > 1);
1417 $av_pend_colon = 'C';
1418 $type = 'N';
1419
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001420 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001421 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001422 $type = 'N';
1423
1424 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001425 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001426 push(@av_paren_type, $av_pending);
1427 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428 $type = 'N';
1429
1430 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001431 my $new_type = pop(@av_paren_type);
1432 if ($new_type ne '_') {
1433 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001434 print "PAREN('$1') -> $type\n"
1435 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001436 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001437 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001438 }
1439
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001440 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001441 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001442 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001443 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001444
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001445 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1446 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001447 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001448 } elsif ($type eq 'E') {
1449 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001450 }
1451 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1452 $type = 'V';
1453
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001454 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001455 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001456 $type = 'V';
1457
1458 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001459 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 $type = 'N';
1461
Andy Whitcroftcf655042008-03-04 14:28:20 -08001462 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001464 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001465 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001466
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001467 } elsif ($cur =~/^(,)/) {
1468 print "COMMA($1)\n" if ($dbg_values > 1);
1469 $type = 'C';
1470
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001471 } elsif ($cur =~ /^(\?)/o) {
1472 print "QUESTION($1)\n" if ($dbg_values > 1);
1473 $type = 'N';
1474
1475 } elsif ($cur =~ /^(:)/o) {
1476 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1477
1478 substr($var, length($res), 1, $av_pend_colon);
1479 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1480 $type = 'E';
1481 } else {
1482 $type = 'N';
1483 }
1484 $av_pend_colon = 'O';
1485
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001486 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001487 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488 $type = 'N';
1489
Andy Whitcroft0d413862008-10-15 22:02:16 -07001490 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001491 my $variant;
1492
1493 print "OPV($1)\n" if ($dbg_values > 1);
1494 if ($type eq 'V') {
1495 $variant = 'B';
1496 } else {
1497 $variant = 'U';
1498 }
1499
1500 substr($var, length($res), 1, $variant);
1501 $type = 'N';
1502
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001504 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001505 if ($1 ne '++' && $1 ne '--') {
1506 $type = 'N';
1507 }
1508
1509 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001510 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001511 }
1512 if (defined $1) {
1513 $cur = substr($cur, length($1));
1514 $res .= $type x length($1);
1515 }
1516 }
1517
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001518 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001519}
1520
Andy Whitcroft8905a672007-11-28 16:21:06 -08001521sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001522 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001523 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001524 ^(?:
1525 $Modifier|
1526 $Storage|
1527 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001528 DEFINE_\S+
1529 )$|
1530 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001531 goto|
1532 return|
1533 case|
1534 else|
1535 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001536 do|
1537 \#|
1538 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001539 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001540 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001541 )}x;
1542 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1543 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001544 # Check for modifiers.
1545 $possible =~ s/\s*$Storage\s*//g;
1546 $possible =~ s/\s*$Sparse\s*//g;
1547 if ($possible =~ /^\s*$/) {
1548
1549 } elsif ($possible =~ /\s/) {
1550 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001551 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001552 if ($modifier !~ $notPermitted) {
1553 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1554 push(@modifierList, $modifier);
1555 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001556 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001557
1558 } else {
1559 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1560 push(@typeList, $possible);
1561 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001562 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001563 } else {
1564 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001565 }
1566}
1567
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001568my $prefix = '';
1569
Joe Perches000d1cc12011-07-25 17:13:25 -07001570sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001571 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001572
Joe Perchescbec18a2014-04-03 14:49:19 -07001573 return defined $use_type{$type} if (scalar keys %use_type > 0);
1574
1575 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001576}
1577
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001578sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001579 my ($level, $type, $msg) = @_;
1580
1581 if (!show_type($type) ||
1582 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001583 return 0;
1584 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001585 my $line;
1586 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001587 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001588 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001589 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001590 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001591 $line = (split('\n', $line))[0] . "\n" if ($terse);
1592
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001593 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001594
1595 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001596}
Joe Perchescbec18a2014-04-03 14:49:19 -07001597
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001598sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001599 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001600}
Joe Perches000d1cc12011-07-25 17:13:25 -07001601
Joe Perchesd752fcc2014-08-06 16:11:05 -07001602sub fixup_current_range {
1603 my ($lineRef, $offset, $length) = @_;
1604
1605 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1606 my $o = $1;
1607 my $l = $2;
1608 my $no = $o + $offset;
1609 my $nl = $l + $length;
1610 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1611 }
1612}
1613
1614sub fix_inserted_deleted_lines {
1615 my ($linesRef, $insertedRef, $deletedRef) = @_;
1616
1617 my $range_last_linenr = 0;
1618 my $delta_offset = 0;
1619
1620 my $old_linenr = 0;
1621 my $new_linenr = 0;
1622
1623 my $next_insert = 0;
1624 my $next_delete = 0;
1625
1626 my @lines = ();
1627
1628 my $inserted = @{$insertedRef}[$next_insert++];
1629 my $deleted = @{$deletedRef}[$next_delete++];
1630
1631 foreach my $old_line (@{$linesRef}) {
1632 my $save_line = 1;
1633 my $line = $old_line; #don't modify the array
1634 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1635 $delta_offset = 0;
1636 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1637 $range_last_linenr = $new_linenr;
1638 fixup_current_range(\$line, $delta_offset, 0);
1639 }
1640
1641 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1642 $deleted = @{$deletedRef}[$next_delete++];
1643 $save_line = 0;
1644 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1645 }
1646
1647 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1648 push(@lines, ${$inserted}{'LINE'});
1649 $inserted = @{$insertedRef}[$next_insert++];
1650 $new_linenr++;
1651 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1652 }
1653
1654 if ($save_line) {
1655 push(@lines, $line);
1656 $new_linenr++;
1657 }
1658
1659 $old_linenr++;
1660 }
1661
1662 return @lines;
1663}
1664
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001665sub fix_insert_line {
1666 my ($linenr, $line) = @_;
1667
1668 my $inserted = {
1669 LINENR => $linenr,
1670 LINE => $line,
1671 };
1672 push(@fixed_inserted, $inserted);
1673}
1674
1675sub fix_delete_line {
1676 my ($linenr, $line) = @_;
1677
1678 my $deleted = {
1679 LINENR => $linenr,
1680 LINE => $line,
1681 };
1682
1683 push(@fixed_deleted, $deleted);
1684}
1685
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001686sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001687 my ($type, $msg) = @_;
1688
1689 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001690 our $clean = 0;
1691 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001692 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001693 }
Joe Perches3705ce52013-07-03 15:05:31 -07001694 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001695}
1696sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001697 my ($type, $msg) = @_;
1698
1699 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001700 our $clean = 0;
1701 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001702 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001703 }
Joe Perches3705ce52013-07-03 15:05:31 -07001704 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001705}
1706sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001707 my ($type, $msg) = @_;
1708
1709 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001710 our $clean = 0;
1711 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001712 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001713 }
Joe Perches3705ce52013-07-03 15:05:31 -07001714 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001715}
1716
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001717sub check_absolute_file {
1718 my ($absolute, $herecurr) = @_;
1719 my $file = $absolute;
1720
1721 ##print "absolute<$absolute>\n";
1722
1723 # See if any suffix of this path is a path within the tree.
1724 while ($file =~ s@^[^/]*/@@) {
1725 if (-f "$root/$file") {
1726 ##print "file<$file>\n";
1727 last;
1728 }
1729 }
1730 if (! -f _) {
1731 return 0;
1732 }
1733
1734 # It is, so see if the prefix is acceptable.
1735 my $prefix = $absolute;
1736 substr($prefix, -length($file)) = '';
1737
1738 ##print "prefix<$prefix>\n";
1739 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001740 WARN("USE_RELATIVE_PATH",
1741 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001742 }
1743}
1744
Joe Perches3705ce52013-07-03 15:05:31 -07001745sub trim {
1746 my ($string) = @_;
1747
Joe Perchesb34c6482013-09-11 14:24:01 -07001748 $string =~ s/^\s+|\s+$//g;
1749
1750 return $string;
1751}
1752
1753sub ltrim {
1754 my ($string) = @_;
1755
1756 $string =~ s/^\s+//;
1757
1758 return $string;
1759}
1760
1761sub rtrim {
1762 my ($string) = @_;
1763
1764 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001765
1766 return $string;
1767}
1768
Joe Perches52ea8502013-11-12 15:10:09 -08001769sub string_find_replace {
1770 my ($string, $find, $replace) = @_;
1771
1772 $string =~ s/$find/$replace/g;
1773
1774 return $string;
1775}
1776
Joe Perches3705ce52013-07-03 15:05:31 -07001777sub tabify {
1778 my ($leading) = @_;
1779
1780 my $source_indent = 8;
1781 my $max_spaces_before_tab = $source_indent - 1;
1782 my $spaces_to_tab = " " x $source_indent;
1783
1784 #convert leading spaces to tabs
1785 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1786 #Remove spaces before a tab
1787 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1788
1789 return "$leading";
1790}
1791
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001792sub pos_last_openparen {
1793 my ($line) = @_;
1794
1795 my $pos = 0;
1796
1797 my $opens = $line =~ tr/\(/\(/;
1798 my $closes = $line =~ tr/\)/\)/;
1799
1800 my $last_openparen = 0;
1801
1802 if (($opens == 0) || ($closes >= $opens)) {
1803 return -1;
1804 }
1805
1806 my $len = length($line);
1807
1808 for ($pos = 0; $pos < $len; $pos++) {
1809 my $string = substr($line, $pos);
1810 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1811 $pos += length($1) - 1;
1812 } elsif (substr($line, $pos, 1) eq '(') {
1813 $last_openparen = $pos;
1814 } elsif (index($string, '(') == -1) {
1815 last;
1816 }
1817 }
1818
Joe Perches91cb5192014-04-03 14:49:32 -07001819 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001820}
1821
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001822sub process {
1823 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001824
1825 my $linenr=0;
1826 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001827 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001828 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001829 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001830
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001831 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001832 my $indent;
1833 my $previndent=0;
1834 my $stashindent=0;
1835
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001836 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001837 my $signoff = 0;
1838 my $is_patch = 0;
1839
Joe Perches29ee1b02014-08-06 16:10:35 -07001840 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001841 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001842 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001843 my $non_utf8_charset = 0;
1844
Joe Perches365dd4e2014-08-06 16:10:42 -07001845 my $last_blank_line = 0;
1846
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001847 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001848 our $cnt_lines = 0;
1849 our $cnt_error = 0;
1850 our $cnt_warn = 0;
1851 our $cnt_chk = 0;
1852
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001853 # Trace the real file/line as we go.
1854 my $realfile = '';
1855 my $realline = 0;
1856 my $realcnt = 0;
1857 my $here = '';
1858 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001859 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001860 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001861 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001862
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001863 my $prev_values = 'E';
1864
1865 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001866 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001867 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001868 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001869 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001870
Joe Perches7e51f192013-09-11 14:23:57 -07001871 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001872
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001873 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001874 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001876 my @setup_docs = ();
1877 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001878
Joe Perchesd8b07712013-11-12 15:10:06 -08001879 my $camelcase_file_seeded = 0;
1880
Andy Whitcroft773647a2008-03-28 14:15:58 -07001881 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 my $line;
1883 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001884 $linenr++;
1885 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886
Joe Perches3705ce52013-07-03 15:05:31 -07001887 push(@fixed, $rawline) if ($fix);
1888
Andy Whitcroft773647a2008-03-28 14:15:58 -07001889 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001890 $setup_docs = 0;
1891 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1892 $setup_docs = 1;
1893 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001894 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001895 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001896 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1897 $realline=$1-1;
1898 if (defined $2) {
1899 $realcnt=$3+1;
1900 } else {
1901 $realcnt=1+1;
1902 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001903 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001904
1905 # Guestimate if this is a continuing comment. Run
1906 # the context looking for a comment "edge". If this
1907 # edge is a close comment then we must be in a comment
1908 # at context start.
1909 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001910 my $cnt = $realcnt;
1911 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1912 next if (defined $rawlines[$ln - 1] &&
1913 $rawlines[$ln - 1] =~ /^-/);
1914 $cnt--;
1915 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001916 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001917 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1918 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1919 ($edge) = $1;
1920 last;
1921 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001922 }
1923 if (defined $edge && $edge eq '*/') {
1924 $in_comment = 1;
1925 }
1926
1927 # Guestimate if this is a continuing comment. If this
1928 # is the start of a diff block and this line starts
1929 # ' *' then it is very likely a comment.
1930 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001931 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001932 {
1933 $in_comment = 1;
1934 }
1935
1936 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1937 sanitise_line_reset($in_comment);
1938
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001939 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001940 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001941 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001942 $line = sanitise_line($rawline);
1943 }
1944 push(@lines, $line);
1945
1946 if ($realcnt > 1) {
1947 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1948 } else {
1949 $realcnt = 0;
1950 }
1951
1952 #print "==>$rawline\n";
1953 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001954
1955 if ($setup_docs && $line =~ /^\+/) {
1956 push(@setup_docs, $line);
1957 }
1958 }
1959
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001960 $prefix = '';
1961
Andy Whitcroft773647a2008-03-28 14:15:58 -07001962 $realcnt = 0;
1963 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001964 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001965 foreach my $line (@lines) {
1966 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001967 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001968 my $sline = $line; #copy of $line
1969 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001970
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001971 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001972
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001973#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001974 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001975 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001976 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001977 $realline=$1-1;
1978 if (defined $2) {
1979 $realcnt=$3+1;
1980 } else {
1981 $realcnt=1+1;
1982 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001983 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001984 $prev_values = 'E';
1985
Andy Whitcroft773647a2008-03-28 14:15:58 -07001986 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001987 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001988 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001989 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001990 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001991
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001992# track the line number as we move through the hunk, note that
1993# new versions of GNU diff omit the leading space on completely
1994# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001996 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001997 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001998
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001999 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002000 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002001
2002 # Track the previous line.
2003 ($prevline, $stashline) = ($stashline, $line);
2004 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002005 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2006
Andy Whitcroft773647a2008-03-28 14:15:58 -07002007 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002008
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002009 } elsif ($realcnt == 1) {
2010 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002011 }
2012
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002013 my $hunk_line = ($realcnt != 0);
2014
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002015#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016 $prefix = "$filename:$realline: " if ($emacs && $file);
2017 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2018
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002019 $here = "#$linenr: " if (!$file);
2020 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002021
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002022 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002024 if ($line =~ /^diff --git.*?(\S+)$/) {
2025 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002026 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002027 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002028 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002029 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002031 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002032 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002033
2034 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002035 if (!$file && $tree && $p1_prefix ne '' &&
2036 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002037 WARN("PATCH_PREFIX",
2038 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002039 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002041 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002042 ERROR("MODIFIED_INCLUDE_ASM",
2043 "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 -07002044 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002045 $found_file = 1;
2046 }
2047
2048 if ($found_file) {
2049 if ($realfile =~ m@^(drivers/net/|net/)@) {
2050 $check = 1;
2051 } else {
2052 $check = $check_orig;
2053 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002054 next;
2055 }
2056
Randy Dunlap389834b2007-06-08 13:47:03 -07002057 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002058
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002059 my $hereline = "$here\n$rawline\n";
2060 my $herecurr = "$here\n$rawline\n";
2061 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002062
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002063 $cnt_lines++ if ($realcnt != 0);
2064
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002065# Check for incorrect file permissions
2066 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2067 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002068 if ($realfile !~ m@scripts/@ &&
2069 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002070 ERROR("EXECUTE_PERMISSIONS",
2071 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002072 }
2073 }
2074
Joe Perches20112472011-07-25 17:13:23 -07002075# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002076 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002077 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002078 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002079 }
2080
2081# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002082 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002083 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002084 my $space_before = $1;
2085 my $sign_off = $2;
2086 my $space_after = $3;
2087 my $email = $4;
2088 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2089
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002090 if ($sign_off !~ /$signature_tags/) {
2091 WARN("BAD_SIGN_OFF",
2092 "Non-standard signature: $sign_off\n" . $herecurr);
2093 }
Joe Perches20112472011-07-25 17:13:23 -07002094 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002095 if (WARN("BAD_SIGN_OFF",
2096 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2097 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002098 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002099 "$ucfirst_sign_off $email";
2100 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002101 }
Joe Perches20112472011-07-25 17:13:23 -07002102 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002103 if (WARN("BAD_SIGN_OFF",
2104 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2105 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002106 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002107 "$ucfirst_sign_off $email";
2108 }
2109
Joe Perches20112472011-07-25 17:13:23 -07002110 }
2111 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002112 if (WARN("BAD_SIGN_OFF",
2113 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2114 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002115 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002116 "$ucfirst_sign_off $email";
2117 }
Joe Perches20112472011-07-25 17:13:23 -07002118 }
2119
2120 my ($email_name, $email_address, $comment) = parse_email($email);
2121 my $suggested_email = format_email(($email_name, $email_address));
2122 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002123 ERROR("BAD_SIGN_OFF",
2124 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002125 } else {
2126 my $dequoted = $suggested_email;
2127 $dequoted =~ s/^"//;
2128 $dequoted =~ s/" </ </;
2129 # Don't force email to have quotes
2130 # Allow just an angle bracketed address
2131 if ("$dequoted$comment" ne $email &&
2132 "<$email_address>$comment" ne $email &&
2133 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002134 WARN("BAD_SIGN_OFF",
2135 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002136 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002137 }
Joe Perches7e51f192013-09-11 14:23:57 -07002138
2139# Check for duplicate signatures
2140 my $sig_nospace = $line;
2141 $sig_nospace =~ s/\s//g;
2142 $sig_nospace = lc($sig_nospace);
2143 if (defined $signatures{$sig_nospace}) {
2144 WARN("BAD_SIGN_OFF",
2145 "Duplicate signature\n" . $herecurr);
2146 } else {
2147 $signatures{$sig_nospace} = 1;
2148 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002149 }
2150
Joe Perches9b3189e2014-06-04 16:12:10 -07002151# Check for old stable address
2152 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2153 ERROR("STABLE_ADDRESS",
2154 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2155 }
2156
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002157# Check for unwanted Gerrit info
2158 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2159 ERROR("GERRIT_CHANGE_ID",
2160 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2161 }
2162
Joe Perchesd311cd42014-08-06 16:10:57 -07002163# Check for improperly formed commit descriptions
2164 if ($in_commit_log &&
2165 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
Joe Perches66881732014-09-09 14:50:55 -07002166 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2167 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2168 defined $rawlines[$linenr] &&
2169 $rawlines[$linenr] =~ /^\s*\("/))) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002170 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2171 my $init_char = $1;
2172 my $orig_commit = lc($2);
2173 my $id = '01234567890ab';
2174 my $desc = 'commit description';
2175 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2176 ERROR("GIT_COMMIT_ID",
Joe Perches3f6316b2014-08-29 15:18:26 -07002177 "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
Joe Perchesd311cd42014-08-06 16:10:57 -07002178 }
2179
Joe Perches13f19372014-08-06 16:10:59 -07002180# Check for added, moved or deleted files
2181 if (!$reported_maintainer_file && !$in_commit_log &&
2182 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2183 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2184 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2185 (defined($1) || defined($2))))) {
2186 $reported_maintainer_file = 1;
2187 WARN("FILE_PATH_CHANGES",
2188 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2189 }
2190
Andy Whitcroft00df3442007-06-08 13:47:06 -07002191# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002192 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002193 ERROR("CORRUPTED_PATCH",
2194 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002195 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002196 }
2197
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002198# Check for absolute kernel paths.
2199 if ($tree) {
2200 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2201 my $file = $1;
2202
2203 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2204 check_absolute_file($1, $herecurr)) {
2205 #
2206 } else {
2207 check_absolute_file($file, $herecurr);
2208 }
2209 }
2210 }
2211
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002212# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2213 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002214 $rawline !~ m/^$UTF8*$/) {
2215 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2216
2217 my $blank = copy_spacing($rawline);
2218 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2219 my $hereptr = "$hereline$ptr\n";
2220
Joe Perches34d99212011-07-25 17:13:26 -07002221 CHK("INVALID_UTF8",
2222 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002223 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002224
Joe Perches15662b32011-10-31 17:13:12 -07002225# Check if it's the start of a commit log
2226# (not a header line and we haven't seen the patch filename)
2227 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002228 !($rawline =~ /^\s+\S/ ||
2229 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002230 $in_header_lines = 0;
2231 $in_commit_log = 1;
2232 }
2233
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002234# Check if there is UTF-8 in a commit log when a mail header has explicitly
2235# declined it, i.e defined some charset where it is missing.
2236 if ($in_header_lines &&
2237 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2238 $1 !~ /utf-8/i) {
2239 $non_utf8_charset = 1;
2240 }
2241
2242 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002243 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002244 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002245 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2246 }
2247
Kees Cook66b47b42014-10-13 15:51:57 -07002248# Check for various typo / spelling mistakes
2249 if ($in_commit_log || $line =~ /^\+/) {
2250 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2251 my $typo = $1;
2252 my $typo_fix = $spelling_fix{lc($typo)};
2253 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2254 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2255 my $msg_type = \&WARN;
2256 $msg_type = \&CHK if ($file);
2257 if (&{$msg_type}("TYPO_SPELLING",
2258 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2259 $fix) {
2260 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2261 }
2262 }
2263 }
2264
Andy Whitcroft306708542008-10-15 22:02:28 -07002265# ignore non-hunk lines and lines being removed
2266 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002267
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002268#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002269 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002270 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002271 if (ERROR("DOS_LINE_ENDINGS",
2272 "DOS line endings\n" . $herevet) &&
2273 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002274 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002275 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002276 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2277 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002278 if (ERROR("TRAILING_WHITESPACE",
2279 "trailing whitespace\n" . $herevet) &&
2280 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002281 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002282 }
2283
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002284 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002285 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002286
Josh Triplett4783f892013-11-12 15:10:12 -08002287# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002288 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002289 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2290 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002291 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2292 my $msg_type = \&ERROR;
2293 $msg_type = \&CHK if ($file);
2294 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002295 "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 -08002296 }
2297
Andi Kleen33549572010-05-24 14:33:29 -07002298# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002299# Only applies when adding the entry originally, after that we do not have
2300# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002301 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002302 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002303 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002304 my $cnt = $realcnt;
2305 my $ln = $linenr + 1;
2306 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002307 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002308 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002309 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002310 $f = $lines[$ln - 1];
2311 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2312 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002313
2314 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002315 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002316
Joe Perches8d73e0e2014-08-06 16:10:46 -07002317 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002318 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002319 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002320 $length = -1;
2321 }
2322
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002323 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002324 $f =~ s/#.*//;
2325 $f =~ s/^\s+//;
2326 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002327 if ($f =~ /^\s*config\s/) {
2328 $is_end = 1;
2329 last;
2330 }
Andi Kleen33549572010-05-24 14:33:29 -07002331 $length++;
2332 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002333 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2334 WARN("CONFIG_DESCRIPTION",
2335 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2336 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002337 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002338 }
2339
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002340# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2341 if ($realfile =~ /Kconfig/ &&
2342 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2343 WARN("CONFIG_EXPERIMENTAL",
2344 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2345 }
2346
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002347 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2348 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2349 my $flag = $1;
2350 my $replacement = {
2351 'EXTRA_AFLAGS' => 'asflags-y',
2352 'EXTRA_CFLAGS' => 'ccflags-y',
2353 'EXTRA_CPPFLAGS' => 'cppflags-y',
2354 'EXTRA_LDFLAGS' => 'ldflags-y',
2355 };
2356
2357 WARN("DEPRECATED_VARIABLE",
2358 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2359 }
2360
Rob Herringbff5da42014-01-23 15:54:51 -08002361# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002362 if (defined $root &&
2363 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2364 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2365
Rob Herringbff5da42014-01-23 15:54:51 -08002366 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2367
Florian Vaussardcc933192014-04-03 14:49:27 -07002368 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2369 my $vp_file = $dt_path . "vendor-prefixes.txt";
2370
Rob Herringbff5da42014-01-23 15:54:51 -08002371 foreach my $compat (@compats) {
2372 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002373 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2374 my $compat3 = $compat;
2375 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2376 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002377 if ( $? >> 8 ) {
2378 WARN("UNDOCUMENTED_DT_STRING",
2379 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2380 }
2381
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002382 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2383 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002384 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002385 if ( $? >> 8 ) {
2386 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002387 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002388 }
2389 }
2390 }
2391
Andy Whitcroft5368df22008-10-15 22:02:27 -07002392# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002393 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002394
Joe Perches6cd7f382012-12-17 16:01:54 -08002395#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002396 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002397 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002398 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002399 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002400 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002401 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002402 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002403 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002404 }
2405
Josh Triplettca56dc02012-03-23 15:02:21 -07002406# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002407# to grep for the string. Make exceptions when the previous string ends in a
2408# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2409# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002410 if ($line =~ /^\+\s*"/ &&
2411 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002412 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002413 WARN("SPLIT_STRING",
2414 "quoted string split across lines\n" . $hereprev);
2415 }
2416
Dan Carpenterece96592014-08-06 16:11:29 -07002417# check for missing a space in a string concatination
2418 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2419 WARN('MISSING_SPACE',
2420 "break quoted strings at a space character\n" . $hereprev);
2421 }
2422
Joe Perches5e79d962010-03-05 13:43:55 -08002423# check for spaces before a quoted newline
2424 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002425 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2426 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2427 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002428 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002429 }
2430
Joe Perches5e79d962010-03-05 13:43:55 -08002431 }
2432
Andy Whitcroft8905a672007-11-28 16:21:06 -08002433# check for adding lines without a newline.
2434 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002435 WARN("MISSING_EOF_NEWLINE",
2436 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002437 }
2438
Mike Frysinger42e41c52009-09-21 17:04:40 -07002439# Blackfin: use hi/lo macros
2440 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2441 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2442 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002443 ERROR("LO_MACRO",
2444 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002445 }
2446 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2447 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002448 ERROR("HI_MACRO",
2449 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002450 }
2451 }
2452
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002453# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002454 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002455
2456# at the beginning of a line any tabs must come first and anything
2457# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002458 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2459 $rawline =~ /^\+\s* \s*/) {
2460 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002461 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002462 if (ERROR("CODE_INDENT",
2463 "code indent should use tabs where possible\n" . $herevet) &&
2464 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002465 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002466 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002467 }
2468
Alberto Panizzo08e44362010-03-05 13:43:54 -08002469# check for space before tabs.
2470 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2471 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002472 if (WARN("SPACE_BEFORE_TAB",
2473 "please, no space before tabs\n" . $herevet) &&
2474 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002475 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002476 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002477 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002478 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002479 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002480 }
2481
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002482# check for && or || at the start of a line
2483 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2484 CHK("LOGICAL_CONTINUATIONS",
2485 "Logical continuations should be on the previous line\n" . $hereprev);
2486 }
2487
2488# check multi-line statement indentation matches previous line
2489 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002490 $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 -07002491 $prevline =~ /^\+(\t*)(.*)$/;
2492 my $oldindent = $1;
2493 my $rest = $2;
2494
2495 my $pos = pos_last_openparen($rest);
2496 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002497 $line =~ /^(\+| )([ \t]*)/;
2498 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002499
2500 my $goodtabindent = $oldindent .
2501 "\t" x ($pos / 8) .
2502 " " x ($pos % 8);
2503 my $goodspaceindent = $oldindent . " " x $pos;
2504
2505 if ($newindent ne $goodtabindent &&
2506 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002507
2508 if (CHK("PARENTHESIS_ALIGNMENT",
2509 "Alignment should match open parenthesis\n" . $hereprev) &&
2510 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002511 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002512 s/^\+[ \t]*/\+$goodtabindent/;
2513 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002514 }
2515 }
2516 }
2517
Joe Perches308cc8d2014-08-06 16:11:27 -07002518 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002519 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002520 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002521 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002522 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002523 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002524 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002525 }
2526
Joe Perches05880602012-10-04 17:13:35 -07002527 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002528 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002529 $rawline =~ /^\+[ \t]*\*/ &&
2530 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002531 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2532 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2533 }
2534
2535 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002536 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2537 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002538 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002539 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2540 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2541 "networking block comments start with * on subsequent lines\n" . $hereprev);
2542 }
2543
2544 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002545 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2546 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2547 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2548 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002549 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2550 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2551 }
2552
Joe Perches7f619192014-08-06 16:10:39 -07002553# check for missing blank lines after struct/union declarations
2554# with exceptions for various attributes and macros
2555 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2556 $line =~ /^\+/ &&
2557 !($line =~ /^\+\s*$/ ||
2558 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2559 $line =~ /^\+\s*MODULE_/i ||
2560 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2561 $line =~ /^\+[a-z_]*init/ ||
2562 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2563 $line =~ /^\+\s*DECLARE/ ||
2564 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002565 if (CHK("LINE_SPACING",
2566 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2567 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002568 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002569 }
Joe Perches7f619192014-08-06 16:10:39 -07002570 }
2571
Joe Perches365dd4e2014-08-06 16:10:42 -07002572# check for multiple consecutive blank lines
2573 if ($prevline =~ /^[\+ ]\s*$/ &&
2574 $line =~ /^\+\s*$/ &&
2575 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002576 if (CHK("LINE_SPACING",
2577 "Please don't use multiple blank lines\n" . $hereprev) &&
2578 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002579 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002580 }
2581
Joe Perches365dd4e2014-08-06 16:10:42 -07002582 $last_blank_line = $linenr;
2583 }
2584
Joe Perches3b617e32014-04-03 14:49:28 -07002585# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002586 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2587 # actual declarations
2588 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002589 # function pointer declarations
2590 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002591 # foo bar; where foo is some local typedef or #define
2592 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2593 # known declaration macros
2594 $prevline =~ /^\+\s+$declaration_macros/) &&
2595 # for "else if" which can look like "$Ident $Ident"
2596 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2597 # other possible extensions of declaration lines
2598 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2599 # not starting a section or a macro "\" extended line
2600 $prevline =~ /(?:\{\s*|\\)$/) &&
2601 # looks like a declaration
2602 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002603 # function pointer declarations
2604 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002605 # foo bar; where foo is some local typedef or #define
2606 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2607 # known declaration macros
2608 $sline =~ /^\+\s+$declaration_macros/ ||
2609 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002610 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002611 # start or end of block or continuation of declaration
2612 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2613 # bitfield continuation
2614 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2615 # other possible extensions of declaration lines
2616 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2617 # indentation of previous and current line are the same
2618 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002619 if (WARN("LINE_SPACING",
2620 "Missing a blank line after declarations\n" . $hereprev) &&
2621 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002622 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002623 }
Joe Perches3b617e32014-04-03 14:49:28 -07002624 }
2625
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002626# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002627# Exceptions:
2628# 1) within comments
2629# 2) indented preprocessor commands
2630# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002631 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002632 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002633 if (WARN("LEADING_SPACE",
2634 "please, no spaces at the start of a line\n" . $herevet) &&
2635 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002636 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002637 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002638 }
2639
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002640# check we are in a valid C source file if not then ignore this hunk
2641 next if ($realfile !~ /\.(h|c)$/);
2642
Joe Perches032a4c02014-08-06 16:10:29 -07002643# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002644# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002645# if the previous line is a break or return and is indented 1 tab more...
2646 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2647 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002648 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2649 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2650 defined $lines[$linenr] &&
2651 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002652 WARN("UNNECESSARY_ELSE",
2653 "else is not generally useful after a break or return\n" . $hereprev);
2654 }
2655 }
2656
Joe Perchesc00df192014-08-06 16:11:01 -07002657# check indentation of a line with a break;
2658# if the previous line is a goto or return and is indented the same # of tabs
2659 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2660 my $tabs = $1;
2661 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2662 WARN("UNNECESSARY_BREAK",
2663 "break is not useful after a goto or return\n" . $hereprev);
2664 }
2665 }
2666
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002667# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2668 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2669 WARN("CONFIG_EXPERIMENTAL",
2670 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2671 }
2672
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002673# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002674 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002675 WARN("CVS_KEYWORD",
2676 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002677 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002678
Mike Frysinger42e41c52009-09-21 17:04:40 -07002679# Blackfin: don't use __builtin_bfin_[cs]sync
2680 if ($line =~ /__builtin_bfin_csync/) {
2681 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002682 ERROR("CSYNC",
2683 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002684 }
2685 if ($line =~ /__builtin_bfin_ssync/) {
2686 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002687 ERROR("SSYNC",
2688 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002689 }
2690
Joe Perches56e77d72013-02-21 16:44:14 -08002691# check for old HOTPLUG __dev<foo> section markings
2692 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2693 WARN("HOTPLUG_SECTION",
2694 "Using $1 is unnecessary\n" . $herecurr);
2695 }
2696
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002697# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002698 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2699 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002700#print "LINE<$line>\n";
2701 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002702 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002703 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002704 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002705 $stat =~ s/\n./\n /g;
2706 $cond =~ s/\n./\n /g;
2707
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002708#print "linenr<$linenr> <$stat>\n";
2709 # If this statement has no statement boundaries within
2710 # it there is no point in retrying a statement scan
2711 # until we hit end of it.
2712 my $frag = $stat; $frag =~ s/;+\s*$//;
2713 if ($frag !~ /(?:{|;)/) {
2714#print "skip<$line_nr_next>\n";
2715 $suppress_statement = $line_nr_next;
2716 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002717
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002718 # Find the real next line.
2719 $realline_next = $line_nr_next;
2720 if (defined $realline_next &&
2721 (!defined $lines[$realline_next - 1] ||
2722 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2723 $realline_next++;
2724 }
2725
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002726 my $s = $stat;
2727 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002728
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002729 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002730 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002731
2732 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002733 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002734
Andy Whitcroft463f2862009-09-21 17:04:34 -07002735 } elsif ($s =~ /^.\s*else\b/s) {
2736
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002737 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002738 } 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 -07002739 my $type = $1;
2740 $type =~ s/\s+/ /g;
2741 possible($type, "A:" . $s);
2742
Andy Whitcroft8905a672007-11-28 16:21:06 -08002743 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002744 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002745 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002746 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002747
2748 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002749 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002750 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002751 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002752
2753 # Check for any sort of function declaration.
2754 # int foo(something bar, other baz);
2755 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002756 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 -08002757 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002758
Andy Whitcroftcf655042008-03-04 14:28:20 -08002759 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002760 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002761 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002762
Andy Whitcroft8905a672007-11-28 16:21:06 -08002763 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002764 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002765
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002766 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002767 }
2768 }
2769 }
2770
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002771 }
2772
Andy Whitcroft653d4872007-06-23 17:16:34 -07002773#
2774# Checks which may be anchored in the context.
2775#
2776
2777# Check for switch () and associated case and default
2778# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002779 if ($line=~/\bswitch\s*\(.*\)/) {
2780 my $err = '';
2781 my $sep = '';
2782 my @ctx = ctx_block_outer($linenr, $realcnt);
2783 shift(@ctx);
2784 for my $ctx (@ctx) {
2785 my ($clen, $cindent) = line_stats($ctx);
2786 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2787 $indent != $cindent) {
2788 $err .= "$sep$ctx\n";
2789 $sep = '';
2790 } else {
2791 $sep = "[...]\n";
2792 }
2793 }
2794 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002795 ERROR("SWITCH_CASE_INDENT_LEVEL",
2796 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002797 }
2798 }
2799
2800# if/while/etc brace do not go on next line, unless defining a do while loop,
2801# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002802 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 -07002803 my $pre_ctx = "$1$2";
2804
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002805 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002806
2807 if ($line =~ /^\+\t{6,}/) {
2808 WARN("DEEP_INDENTATION",
2809 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2810 }
2811
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002812 my $ctx_cnt = $realcnt - $#ctx - 1;
2813 my $ctx = join("\n", @ctx);
2814
Andy Whitcroft548596d2008-07-23 21:29:01 -07002815 my $ctx_ln = $linenr;
2816 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002817
Andy Whitcroft548596d2008-07-23 21:29:01 -07002818 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2819 defined $lines[$ctx_ln - 1] &&
2820 $lines[$ctx_ln - 1] =~ /^-/)) {
2821 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2822 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002823 $ctx_ln++;
2824 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002825
Andy Whitcroft53210162008-07-23 21:29:03 -07002826 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2827 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002828
Joe Perchesd752fcc2014-08-06 16:11:05 -07002829 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002830 ERROR("OPEN_BRACE",
2831 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002832 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002833 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002834 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2835 $ctx =~ /\)\s*\;\s*$/ &&
2836 defined $lines[$ctx_ln - 1])
2837 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002838 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2839 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002840 WARN("TRAILING_SEMICOLON",
2841 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002842 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002843 }
2844 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002845 }
2846
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002847# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002848 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 -08002849 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2850 ctx_statement_block($linenr, $realcnt, 0)
2851 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002852 my ($s, $c) = ($stat, $cond);
2853
2854 substr($s, 0, length($c), '');
2855
2856 # Make sure we remove the line prefixes as we have
2857 # none on the first line, and are going to readd them
2858 # where necessary.
2859 $s =~ s/\n./\n/gs;
2860
2861 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002862 my @newlines = ($c =~ /\n/gs);
2863 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002864
2865 # We want to check the first line inside the block
2866 # starting at the end of the conditional, so remove:
2867 # 1) any blank line termination
2868 # 2) any opening brace { on end of the line
2869 # 3) any do (...) {
2870 my $continuation = 0;
2871 my $check = 0;
2872 $s =~ s/^.*\bdo\b//;
2873 $s =~ s/^\s*{//;
2874 if ($s =~ s/^\s*\\//) {
2875 $continuation = 1;
2876 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002877 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002878 $check = 1;
2879 $cond_lines++;
2880 }
2881
2882 # Also ignore a loop construct at the end of a
2883 # preprocessor statement.
2884 if (($prevline =~ /^.\s*#\s*define\s/ ||
2885 $prevline =~ /\\\s*$/) && $continuation == 0) {
2886 $check = 0;
2887 }
2888
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002889 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002890 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002891 while ($cond_ptr != $cond_lines) {
2892 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002893
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002894 # If we see an #else/#elif then the code
2895 # is not linear.
2896 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2897 $check = 0;
2898 }
2899
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002900 # Ignore:
2901 # 1) blank lines, they should be at 0,
2902 # 2) preprocessor lines, and
2903 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002904 if ($continuation ||
2905 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002906 $s =~ /^\s*#\s*?/ ||
2907 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002908 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002909 if ($s =~ s/^.*?\n//) {
2910 $cond_lines++;
2911 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002912 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002913 }
2914
2915 my (undef, $sindent) = line_stats("+" . $s);
2916 my $stat_real = raw_line($linenr, $cond_lines);
2917
2918 # Check if either of these lines are modified, else
2919 # this is not this patch's fault.
2920 if (!defined($stat_real) ||
2921 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2922 $check = 0;
2923 }
2924 if (defined($stat_real) && $cond_lines > 1) {
2925 $stat_real = "[...]\n$stat_real";
2926 }
2927
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002928 #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 -07002929
2930 if ($check && (($sindent % 8) != 0 ||
2931 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002932 WARN("SUSPECT_CODE_INDENT",
2933 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002934 }
2935 }
2936
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002937 # Track the 'values' across context and added lines.
2938 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002939 my ($curr_values, $curr_vars) =
2940 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002941 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002942 if ($dbg_values) {
2943 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002944 print "$linenr > .$outline\n";
2945 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002946 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002947 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002948 $prev_values = substr($curr_values, -1);
2949
Andy Whitcroft00df3442007-06-08 13:47:06 -07002950#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002951 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002952
Andy Whitcroft653d4872007-06-23 17:16:34 -07002953# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002954 if ($dbg_type) {
2955 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002956 ERROR("TEST_TYPE",
2957 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002958 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002959 ERROR("TEST_NOT_TYPE",
2960 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002961 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002962 next;
2963 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002964# TEST: allow direct testing of the attribute matcher.
2965 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002966 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002967 ERROR("TEST_ATTR",
2968 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002969 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002970 ERROR("TEST_NOT_ATTR",
2971 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002972 }
2973 next;
2974 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002975
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002976# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002977 if ($line =~ /^.\s*{/ &&
2978 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002979 if (ERROR("OPEN_BRACE",
2980 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002981 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2982 fix_delete_line($fixlinenr - 1, $prevrawline);
2983 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002984 my $fixedline = $prevrawline;
2985 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002986 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002987 $fixedline = $line;
2988 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002989 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002990 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002991 }
2992
Andy Whitcroft653d4872007-06-23 17:16:34 -07002993#
2994# Checks which are anchored on the added line.
2995#
2996
2997# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002998 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002999 my $path = $1;
3000 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003001 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003002 "malformed #include filename\n" . $herecurr);
3003 }
3004 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3005 ERROR("UAPI_INCLUDE",
3006 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003007 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003008 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003009
3010# no C99 // comments
3011 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003012 if (ERROR("C99_COMMENTS",
3013 "do not use C99 // comments\n" . $herecurr) &&
3014 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003015 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003016 if ($line =~ /\/\/(.*)$/) {
3017 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003018 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003019 }
3020 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003021 }
3022 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003023 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003024 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003025
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003026# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3027# the whole statement.
3028#print "APW <$lines[$realline_next - 1]>\n";
3029 if (defined $realline_next &&
3030 exists $lines[$realline_next - 1] &&
3031 !defined $suppress_export{$realline_next} &&
3032 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3033 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003034 # Handle definitions which produce identifiers with
3035 # a prefix:
3036 # XXX(foo);
3037 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003038 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003039 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003040 $name =~ /^${Ident}_$2/) {
3041#print "FOO C name<$name>\n";
3042 $suppress_export{$realline_next} = 1;
3043
3044 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003045 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003046 ^.DEFINE_$Ident\(\Q$name\E\)|
3047 ^.DECLARE_$Ident\(\Q$name\E\)|
3048 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003049 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3050 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003051 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003052#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3053 $suppress_export{$realline_next} = 2;
3054 } else {
3055 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003056 }
3057 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003058 if (!defined $suppress_export{$linenr} &&
3059 $prevline =~ /^.\s*$/ &&
3060 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3061 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3062#print "FOO B <$lines[$linenr - 1]>\n";
3063 $suppress_export{$linenr} = 2;
3064 }
3065 if (defined $suppress_export{$linenr} &&
3066 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003067 WARN("EXPORT_SYMBOL",
3068 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003069 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003070
Joe Eloff5150bda2010-08-09 17:21:00 -07003071# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003072 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3073 if (ERROR("GLOBAL_INITIALISERS",
3074 "do not initialise globals to 0 or NULL\n" .
3075 $herecurr) &&
3076 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003077 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003078 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003079 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003080# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003081 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3082 if (ERROR("INITIALISED_STATIC",
3083 "do not initialise statics to 0 or NULL\n" .
3084 $herecurr) &&
3085 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003086 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003087 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003088 }
3089
Joe Perches18130872014-08-06 16:11:22 -07003090# check for misordered declarations of char/short/int/long with signed/unsigned
3091 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3092 my $tmp = trim($1);
3093 WARN("MISORDERED_TYPE",
3094 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3095 }
3096
Joe Perchescb710ec2010-10-26 14:23:20 -07003097# check for static const char * arrays.
3098 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003099 WARN("STATIC_CONST_CHAR_ARRAY",
3100 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003101 $herecurr);
3102 }
3103
3104# check for static char foo[] = "bar" declarations.
3105 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003106 WARN("STATIC_CONST_CHAR_ARRAY",
3107 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003108 $herecurr);
3109 }
3110
Joe Perches9b0fa602014-04-03 14:49:18 -07003111# check for non-global char *foo[] = {"bar", ...} declarations.
3112 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3113 WARN("STATIC_CONST_CHAR_ARRAY",
3114 "char * array declaration might be better as static const\n" .
3115 $herecurr);
3116 }
3117
Joe Perchesb36190c2014-01-27 17:07:18 -08003118# check for function declarations without arguments like "int foo()"
3119 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3120 if (ERROR("FUNCTION_WITHOUT_ARGS",
3121 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3122 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003123 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003124 }
3125 }
3126
Joe Perches92e112f2013-12-13 11:36:22 -07003127# check for uses of DEFINE_PCI_DEVICE_TABLE
3128 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3129 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3130 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3131 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003132 $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 -07003133 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003134 }
3135
Andy Whitcroft653d4872007-06-23 17:16:34 -07003136# check for new typedefs, only function parameters and sparse annotations
3137# make sense.
3138 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003139 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003140 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003141 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003142 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003143 WARN("NEW_TYPEDEFS",
3144 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003145 }
3146
3147# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003148 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003149 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3150 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003151 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003152
Andy Whitcroft65863862009-01-06 14:41:21 -08003153 # Should start with a space.
3154 $to =~ s/^(\S)/ $1/;
3155 # Should not end with a space.
3156 $to =~ s/\s+$//;
3157 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003158 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003159 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003160
Joe Perches3705ce52013-07-03 15:05:31 -07003161## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003162 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003163 if (ERROR("POINTER_LOCATION",
3164 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3165 $fix) {
3166 my $sub_from = $ident;
3167 my $sub_to = $ident;
3168 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003169 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003170 s@\Q$sub_from\E@$sub_to@;
3171 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003172 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003173 }
3174 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3175 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003176 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003177
Andy Whitcroft65863862009-01-06 14:41:21 -08003178 # Should start with a space.
3179 $to =~ s/^(\S)/ $1/;
3180 # Should not end with a space.
3181 $to =~ s/\s+$//;
3182 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003183 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003184 }
3185 # Modifiers should have spaces.
3186 $to =~ s/(\b$Modifier$)/$1 /;
3187
Joe Perches3705ce52013-07-03 15:05:31 -07003188## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003189 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003190 if (ERROR("POINTER_LOCATION",
3191 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3192 $fix) {
3193
3194 my $sub_from = $match;
3195 my $sub_to = $match;
3196 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003197 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003198 s@\Q$sub_from\E@$sub_to@;
3199 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003200 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003201 }
3202
3203# # no BUG() or BUG_ON()
3204# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3205# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3206# print "$herecurr";
3207# $clean = 0;
3208# }
3209
Andy Whitcroft8905a672007-11-28 16:21:06 -08003210 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003211 WARN("LINUX_VERSION_CODE",
3212 "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 -08003213 }
3214
Joe Perches17441222011-06-15 15:08:17 -07003215# check for uses of printk_ratelimit
3216 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003217 WARN("PRINTK_RATELIMITED",
3218"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003219 }
3220
Andy Whitcroft00df3442007-06-08 13:47:06 -07003221# printk should use KERN_* levels. Note that follow on printk's on the
3222# same line do not need a level, so we use the current block context
3223# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003224# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003225# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003226 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003227 my $ok = 0;
3228 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3229 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003230 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003231 # with "\n" ignore it, else it is to blame
3232 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3233 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3234 $ok = 1;
3235 }
3236 last;
3237 }
3238 }
3239 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003240 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3241 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003242 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003243 }
3244
Joe Perches243f3802012-05-31 16:26:09 -07003245 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3246 my $orig = $1;
3247 my $level = lc($orig);
3248 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003249 my $level2 = $level;
3250 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003251 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003252 "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 -07003253 }
3254
3255 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003256 if (WARN("PREFER_PR_LEVEL",
3257 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3258 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003259 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003260 s/\bpr_warning\b/pr_warn/;
3261 }
Joe Perches243f3802012-05-31 16:26:09 -07003262 }
3263
Joe Perchesdc139312013-02-21 16:44:13 -08003264 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3265 my $orig = $1;
3266 my $level = lc($orig);
3267 $level = "warn" if ($level eq "warning");
3268 $level = "dbg" if ($level eq "debug");
3269 WARN("PREFER_DEV_LEVEL",
3270 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3271 }
3272
Andy Whitcroft653d4872007-06-23 17:16:34 -07003273# function brace can't be on same line, except for #defines of do while,
3274# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003275 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003276 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003277 if (ERROR("OPEN_BRACE",
3278 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3279 $fix) {
3280 fix_delete_line($fixlinenr, $rawline);
3281 my $fixed_line = $rawline;
3282 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3283 my $line1 = $1;
3284 my $line2 = $2;
3285 fix_insert_line($fixlinenr, ltrim($line1));
3286 fix_insert_line($fixlinenr, "\+{");
3287 if ($line2 !~ /^\s*$/) {
3288 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3289 }
3290 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003291 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003292
Andy Whitcroft8905a672007-11-28 16:21:06 -08003293# open braces for enum, union and struct go on the same line.
3294 if ($line =~ /^.\s*{/ &&
3295 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003296 if (ERROR("OPEN_BRACE",
3297 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3298 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3299 fix_delete_line($fixlinenr - 1, $prevrawline);
3300 fix_delete_line($fixlinenr, $rawline);
3301 my $fixedline = rtrim($prevrawline) . " {";
3302 fix_insert_line($fixlinenr, $fixedline);
3303 $fixedline = $rawline;
3304 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3305 if ($fixedline !~ /^\+\s*$/) {
3306 fix_insert_line($fixlinenr, $fixedline);
3307 }
3308 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003309 }
3310
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003311# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003312 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3313 if (WARN("SPACING",
3314 "missing space after $1 definition\n" . $herecurr) &&
3315 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003316 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003317 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3318 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003319 }
3320
Joe Perches31070b52014-01-23 15:54:49 -08003321# Function pointer declarations
3322# check spacing between type, funcptr, and args
3323# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003324 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003325 my $declare = $1;
3326 my $pre_pointer_space = $2;
3327 my $post_pointer_space = $3;
3328 my $funcname = $4;
3329 my $post_funcname_space = $5;
3330 my $pre_args_space = $6;
3331
Joe Perches91f72e92014-04-03 14:49:12 -07003332# the $Declare variable will capture all spaces after the type
3333# so check it for a missing trailing missing space but pointer return types
3334# don't need a space so don't warn for those.
3335 my $post_declare_space = "";
3336 if ($declare =~ /(\s+)$/) {
3337 $post_declare_space = $1;
3338 $declare = rtrim($declare);
3339 }
3340 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003341 WARN("SPACING",
3342 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003343 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003344 }
3345
3346# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003347# This test is not currently implemented because these declarations are
3348# equivalent to
3349# int foo(int bar, ...)
3350# and this is form shouldn't/doesn't generate a checkpatch warning.
3351#
3352# elsif ($declare =~ /\s{2,}$/) {
3353# WARN("SPACING",
3354# "Multiple spaces after return type\n" . $herecurr);
3355# }
Joe Perches31070b52014-01-23 15:54:49 -08003356
3357# unnecessary space "type ( *funcptr)(args...)"
3358 if (defined $pre_pointer_space &&
3359 $pre_pointer_space =~ /^\s/) {
3360 WARN("SPACING",
3361 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3362 }
3363
3364# unnecessary space "type (* funcptr)(args...)"
3365 if (defined $post_pointer_space &&
3366 $post_pointer_space =~ /^\s/) {
3367 WARN("SPACING",
3368 "Unnecessary space before function pointer name\n" . $herecurr);
3369 }
3370
3371# unnecessary space "type (*funcptr )(args...)"
3372 if (defined $post_funcname_space &&
3373 $post_funcname_space =~ /^\s/) {
3374 WARN("SPACING",
3375 "Unnecessary space after function pointer name\n" . $herecurr);
3376 }
3377
3378# unnecessary space "type (*funcptr) (args...)"
3379 if (defined $pre_args_space &&
3380 $pre_args_space =~ /^\s/) {
3381 WARN("SPACING",
3382 "Unnecessary space before function pointer arguments\n" . $herecurr);
3383 }
3384
3385 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003386 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003387 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003388 }
3389 }
3390
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003391# check for spacing round square brackets; allowed:
3392# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003393# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3394# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003395 while ($line =~ /(.*?\s)\[/g) {
3396 my ($where, $prefix) = ($-[1], $1);
3397 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003398 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003399 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003400 if (ERROR("BRACKET_SPACE",
3401 "space prohibited before open square bracket '['\n" . $herecurr) &&
3402 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003403 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003404 s/^(\+.*?)\s+\[/$1\[/;
3405 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003406 }
3407 }
3408
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003409# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003410 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003411 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003412 my $ctx_before = substr($line, 0, $-[1]);
3413 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003414
3415 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003416 if ($name =~ /^(?:
3417 if|for|while|switch|return|case|
3418 volatile|__volatile__|
3419 __attribute__|format|__extension__|
3420 asm|__asm__)$/x)
3421 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003422 # cpp #define statements have non-optional spaces, ie
3423 # if there is a space between the name and the open
3424 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003425 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003426
3427 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003428 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003429
3430 # If this whole things ends with a type its most
3431 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003432 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003433
3434 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003435 if (WARN("SPACING",
3436 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3437 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003438 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003439 s/\b$name\s+\(/$name\(/;
3440 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003441 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003442 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003443
Andy Whitcroft653d4872007-06-23 17:16:34 -07003444# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003445 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003446 my $fixed_line = "";
3447 my $line_fixed = 0;
3448
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003449 my $ops = qr{
3450 <<=|>>=|<=|>=|==|!=|
3451 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3452 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003453 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003454 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003455 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003456 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003457
3458## print("element count: <" . $#elements . ">\n");
3459## foreach my $el (@elements) {
3460## print("el: <$el>\n");
3461## }
3462
3463 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003464 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003465
Joe Perches3705ce52013-07-03 15:05:31 -07003466 foreach my $el (@elements) {
3467 push(@fix_elements, substr($rawline, $off, length($el)));
3468 $off += length($el);
3469 }
3470
3471 $off = 0;
3472
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003473 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003474 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003475
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003476 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003477
3478 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3479
3480## print("n: <$n> good: <$good>\n");
3481
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003482 $off += length($elements[$n]);
3483
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003484 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003485 my $ca = substr($opline, 0, $off);
3486 my $cc = '';
3487 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3488 $cc = substr($opline, $off + length($elements[$n + 1]));
3489 }
3490 my $cb = "$ca$;$cc";
3491
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003492 my $a = '';
3493 $a = 'V' if ($elements[$n] ne '');
3494 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003495 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003496 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3497 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003498 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003499
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003500 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003501
3502 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003503 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003504 $c = 'V' if ($elements[$n + 2] ne '');
3505 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003506 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003507 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3508 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003509 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003510 } else {
3511 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003512 }
3513
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003514 my $ctx = "${a}x${c}";
3515
3516 my $at = "(ctx:$ctx)";
3517
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003518 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003519 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003520
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003521 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003522 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003523
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003524 # Get the full operator variant.
3525 my $opv = $op . substr($curr_vars, $off, 1);
3526
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003527 # Ignore operators passed as parameters.
3528 if ($op_type ne 'V' &&
3529 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3530
Andy Whitcroftcf655042008-03-04 14:28:20 -08003531# # Ignore comments
3532# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003533
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003534 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003535 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003536 if ($ctx !~ /.x[WEBC]/ &&
3537 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003538 if (ERROR("SPACING",
3539 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003540 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003541 $line_fixed = 1;
3542 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003543 }
3544
3545 # // is a comment
3546 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003547
Joe Perchesb00e4812014-04-03 14:49:33 -07003548 # : when part of a bitfield
3549 } elsif ($opv eq ':B') {
3550 # skip the bitfield test for now
3551
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003552 # No spaces for:
3553 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003554 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003555 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003556 if (ERROR("SPACING",
3557 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003558 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003559 if (defined $fix_elements[$n + 2]) {
3560 $fix_elements[$n + 2] =~ s/^\s+//;
3561 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003562 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003563 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003564 }
3565
3566 # , must have a space on the right.
3567 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003568 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003569 if (ERROR("SPACING",
3570 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003571 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003572 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003573 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07003574 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003575 }
3576
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003577 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003578 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003579 #warn "'*' is part of type\n";
3580
3581 # unary operators should have a space before and
3582 # none after. May be left adjacent to another
3583 # unary operator, or a cast
3584 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003585 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003586 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003587 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003588 if (ERROR("SPACING",
3589 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003590 if ($n != $last_after + 2) {
3591 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3592 $line_fixed = 1;
3593 }
Joe Perches3705ce52013-07-03 15:05:31 -07003594 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003595 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003596 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003597 # A unary '*' may be const
3598
3599 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003600 if (ERROR("SPACING",
3601 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003602 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003603 if (defined $fix_elements[$n + 2]) {
3604 $fix_elements[$n + 2] =~ s/^\s+//;
3605 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003606 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003607 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003608 }
3609
3610 # unary ++ and unary -- are allowed no space on one side.
3611 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003612 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003613 if (ERROR("SPACING",
3614 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003615 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003616 $line_fixed = 1;
3617 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003618 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003619 if ($ctx =~ /Wx[BE]/ ||
3620 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003621 if (ERROR("SPACING",
3622 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003623 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003624 $line_fixed = 1;
3625 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003626 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003627 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003628 if (ERROR("SPACING",
3629 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003630 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003631 if (defined $fix_elements[$n + 2]) {
3632 $fix_elements[$n + 2] =~ s/^\s+//;
3633 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003634 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003635 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003636 }
3637
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003638 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003639 } elsif ($op eq '<<' or $op eq '>>' or
3640 $op eq '&' or $op eq '^' or $op eq '|' or
3641 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003642 $op eq '*' or $op eq '/' or
3643 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003644 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003645 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003646 if (ERROR("SPACING",
3647 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003648 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3649 if (defined $fix_elements[$n + 2]) {
3650 $fix_elements[$n + 2] =~ s/^\s+//;
3651 }
Joe Perches3705ce52013-07-03 15:05:31 -07003652 $line_fixed = 1;
3653 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003654 }
3655
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003656 # A colon needs no spaces before when it is
3657 # terminating a case value or a label.
3658 } elsif ($opv eq ':C' || $opv eq ':L') {
3659 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003660 if (ERROR("SPACING",
3661 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003662 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003663 $line_fixed = 1;
3664 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003665 }
3666
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003667 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003668 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003669 my $ok = 0;
3670
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003671 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003672 if (($op eq '<' &&
3673 $cc =~ /^\S+\@\S+>/) ||
3674 ($op eq '>' &&
3675 $ca =~ /<\S+\@\S+$/))
3676 {
3677 $ok = 1;
3678 }
3679
Joe Perches84731622013-11-12 15:10:05 -08003680 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003681 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003682 my $msg_type = \&ERROR;
3683 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3684
3685 if (&{$msg_type}("SPACING",
3686 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003687 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3688 if (defined $fix_elements[$n + 2]) {
3689 $fix_elements[$n + 2] =~ s/^\s+//;
3690 }
Joe Perches3705ce52013-07-03 15:05:31 -07003691 $line_fixed = 1;
3692 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003693 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003694 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003695 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003696
3697## print("n: <$n> GOOD: <$good>\n");
3698
3699 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003700 }
Joe Perches3705ce52013-07-03 15:05:31 -07003701
3702 if (($#elements % 2) == 0) {
3703 $fixed_line = $fixed_line . $fix_elements[$#elements];
3704 }
3705
Joe Perches194f66f2014-08-06 16:11:03 -07003706 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3707 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003708 }
3709
3710
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003711 }
3712
Joe Perches786b6322013-07-03 15:05:32 -07003713# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003714 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003715 if (WARN("SPACING",
3716 "space prohibited before semicolon\n" . $herecurr) &&
3717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003718 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003719 s/^(\+.*\S)\s+;/$1;/;
3720 }
3721 }
3722
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003723# check for multiple assignments
3724 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003725 CHK("MULTIPLE_ASSIGNMENTS",
3726 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003727 }
3728
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003729## # check for multiple declarations, allowing for a function declaration
3730## # continuation.
3731## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3732## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3733##
3734## # Remove any bracketed sections to ensure we do not
3735## # falsly report the parameters of functions.
3736## my $ln = $line;
3737## while ($ln =~ s/\([^\(\)]*\)//g) {
3738## }
3739## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003740## WARN("MULTIPLE_DECLARATION",
3741## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003742## }
3743## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003744
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003745#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003746 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3747 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003748 if (ERROR("SPACING",
3749 "space required before the open brace '{'\n" . $herecurr) &&
3750 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003751 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003752 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003753 }
3754
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003755## # check for blank lines before declarations
3756## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3757## $prevrawline =~ /^.\s*$/) {
3758## WARN("SPACING",
3759## "No blank lines before declarations\n" . $hereprev);
3760## }
3761##
3762
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003763# closing brace should have a space following it when it has anything
3764# on the line
3765 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003766 if (ERROR("SPACING",
3767 "space required after that close brace '}'\n" . $herecurr) &&
3768 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003769 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003770 s/}((?!(?:,|;|\)))\S)/} $1/;
3771 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003772 }
3773
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003774# check spacing on square brackets
3775 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003776 if (ERROR("SPACING",
3777 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3778 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003779 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003780 s/\[\s+/\[/;
3781 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003782 }
3783 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003784 if (ERROR("SPACING",
3785 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3786 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003787 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003788 s/\s+\]/\]/;
3789 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003790 }
3791
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003792# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003793 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3794 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003795 if (ERROR("SPACING",
3796 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3797 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003798 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003799 s/\(\s+/\(/;
3800 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003801 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003802 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003803 $line !~ /for\s*\(.*;\s+\)/ &&
3804 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003805 if (ERROR("SPACING",
3806 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3807 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003808 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003809 s/\s+\)/\)/;
3810 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003811 }
3812
Joe Perchese2826fd2014-08-06 16:10:48 -07003813# check unnecessary parentheses around addressof/dereference single $Lvals
3814# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3815
3816 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3817 CHK("UNNECESSARY_PARENTHESES",
3818 "Unnecessary parentheses around $1\n" . $herecurr);
3819 }
3820
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003821#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003822 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003823 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003824 if (WARN("INDENTED_LABEL",
3825 "labels should not be indented\n" . $herecurr) &&
3826 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003827 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003828 s/^(.)\s+/$1/;
3829 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003830 }
3831
Joe Perches5b9553a2014-04-03 14:49:21 -07003832# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003833 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003834 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003835 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003836 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3837 my $value = $1;
3838 $value = deparenthesize($value);
3839 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3840 ERROR("RETURN_PARENTHESES",
3841 "return is not a function, parentheses are not required\n" . $herecurr);
3842 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003843 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003844 ERROR("SPACING",
3845 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003846 }
3847 }
Joe Perches507e5142013-11-12 15:10:13 -08003848
Joe Perchesb43ae212014-06-23 13:22:07 -07003849# unnecessary return in a void function
3850# at end-of-function, with the previous line a single leading tab, then return;
3851# and the line before that not a goto label target like "out:"
3852 if ($sline =~ /^[ \+]}\s*$/ &&
3853 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3854 $linenr >= 3 &&
3855 $lines[$linenr - 3] =~ /^[ +]/ &&
3856 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003857 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003858 "void function return statements are not generally useful\n" . $hereprev);
3859 }
Joe Perches9819cf22014-06-04 16:12:09 -07003860
Joe Perches189248d2014-01-23 15:54:47 -08003861# if statements using unnecessary parentheses - ie: if ((foo == bar))
3862 if ($^V && $^V ge 5.10.0 &&
3863 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3864 my $openparens = $1;
3865 my $count = $openparens =~ tr@\(@\(@;
3866 my $msg = "";
3867 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3868 my $comp = $4; #Not $1 because of $LvalOrFunc
3869 $msg = " - maybe == should be = ?" if ($comp eq "==");
3870 WARN("UNNECESSARY_PARENTHESES",
3871 "Unnecessary parentheses$msg\n" . $herecurr);
3872 }
3873 }
3874
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003875# Return of what appears to be an errno should normally be -'ve
3876 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3877 my $name = $1;
3878 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003879 WARN("USE_NEGATIVE_ERRNO",
3880 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003881 }
3882 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003883
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003884# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003885 if ($line =~ /\b(if|while|for|switch)\(/) {
3886 if (ERROR("SPACING",
3887 "space required before the open parenthesis '('\n" . $herecurr) &&
3888 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003889 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003890 s/\b(if|while|for|switch)\(/$1 \(/;
3891 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003892 }
3893
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003894# Check for illegal assignment in if conditional -- and check for trailing
3895# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003896 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003897 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3898 ctx_statement_block($linenr, $realcnt, 0)
3899 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003900 my ($stat_next) = ctx_statement_block($line_nr_next,
3901 $remain_next, $off_next);
3902 $stat_next =~ s/\n./\n /g;
3903 ##print "stat<$stat> stat_next<$stat_next>\n";
3904
3905 if ($stat_next =~ /^\s*while\b/) {
3906 # If the statement carries leading newlines,
3907 # then count those as offsets.
3908 my ($whitespace) =
3909 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3910 my $offset =
3911 statement_rawlines($whitespace) - 1;
3912
3913 $suppress_whiletrailers{$line_nr_next +
3914 $offset} = 1;
3915 }
3916 }
3917 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003918 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003919 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003920 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003921
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003922 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003923 ERROR("ASSIGN_IN_IF",
3924 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003925 }
3926
3927 # Find out what is on the end of the line after the
3928 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003929 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003930 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003931 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003932 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3933 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003934 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003935 # Find out how long the conditional actually is.
3936 my @newlines = ($c =~ /\n/gs);
3937 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003938 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003939
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003940 $stat_real = raw_line($linenr, $cond_lines)
3941 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003942 if (defined($stat_real) && $cond_lines > 1) {
3943 $stat_real = "[...]\n$stat_real";
3944 }
3945
Joe Perches000d1cc12011-07-25 17:13:25 -07003946 ERROR("TRAILING_STATEMENTS",
3947 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003948 }
3949 }
3950
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003951# Check for bitwise tests written as boolean
3952 if ($line =~ /
3953 (?:
3954 (?:\[|\(|\&\&|\|\|)
3955 \s*0[xX][0-9]+\s*
3956 (?:\&\&|\|\|)
3957 |
3958 (?:\&\&|\|\|)
3959 \s*0[xX][0-9]+\s*
3960 (?:\&\&|\|\||\)|\])
3961 )/x)
3962 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003963 WARN("HEXADECIMAL_BOOLEAN_TEST",
3964 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003965 }
3966
Andy Whitcroft8905a672007-11-28 16:21:06 -08003967# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003968 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3969 my $s = $1;
3970 $s =~ s/$;//g; # Remove any comments
3971 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003972 ERROR("TRAILING_STATEMENTS",
3973 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003974 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003975 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003976# if should not continue a brace
3977 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003978 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07003979 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003980 $herecurr);
3981 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003982# case and default should not have general statements after them
3983 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3984 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003985 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003986 \s*return\s+
3987 )/xg)
3988 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003989 ERROR("TRAILING_STATEMENTS",
3990 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003991 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003992
3993 # Check for }<nl>else {, these must be at the same
3994 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07003995 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3996 $previndent == $indent) {
3997 if (ERROR("ELSE_AFTER_BRACE",
3998 "else should follow close brace '}'\n" . $hereprev) &&
3999 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4000 fix_delete_line($fixlinenr - 1, $prevrawline);
4001 fix_delete_line($fixlinenr, $rawline);
4002 my $fixedline = $prevrawline;
4003 $fixedline =~ s/}\s*$//;
4004 if ($fixedline !~ /^\+\s*$/) {
4005 fix_insert_line($fixlinenr, $fixedline);
4006 }
4007 $fixedline = $rawline;
4008 $fixedline =~ s/^(.\s*)else/$1} else/;
4009 fix_insert_line($fixlinenr, $fixedline);
4010 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004011 }
4012
Joe Perches8b8856f2014-08-06 16:11:14 -07004013 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4014 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004015 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4016
4017 # Find out what is on the end of the line after the
4018 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004019 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004020 $s =~ s/\n.*//g;
4021
4022 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004023 if (ERROR("WHILE_AFTER_BRACE",
4024 "while should follow close brace '}'\n" . $hereprev) &&
4025 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4026 fix_delete_line($fixlinenr - 1, $prevrawline);
4027 fix_delete_line($fixlinenr, $rawline);
4028 my $fixedline = $prevrawline;
4029 my $trailing = $rawline;
4030 $trailing =~ s/^\+//;
4031 $trailing = trim($trailing);
4032 $fixedline =~ s/}\s*$/} $trailing/;
4033 fix_insert_line($fixlinenr, $fixedline);
4034 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004035 }
4036 }
4037
Joe Perches95e2c602013-07-03 15:05:20 -07004038#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004039 while ($line =~ m{($Constant|$Lval)}g) {
4040 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004041
4042#gcc binary extension
4043 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004044 if (WARN("GCC_BINARY_CONSTANT",
4045 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4046 $fix) {
4047 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004048 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004049 s/\b$var\b/$hexval/;
4050 }
Joe Perches95e2c602013-07-03 15:05:20 -07004051 }
4052
4053#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004054 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004055 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004056#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004057 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004058#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07004059 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004060 while ($var =~ m{($Ident)}g) {
4061 my $word = $1;
4062 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004063 if ($check) {
4064 seed_camelcase_includes();
4065 if (!$file && !$camelcase_file_seeded) {
4066 seed_camelcase_file($realfile);
4067 $camelcase_file_seeded = 1;
4068 }
4069 }
Joe Perches7e781f62013-09-11 14:23:55 -07004070 if (!defined $camelcase{$word}) {
4071 $camelcase{$word} = 1;
4072 CHK("CAMELCASE",
4073 "Avoid CamelCase: <$word>\n" . $herecurr);
4074 }
Joe Perches34456862013-07-03 15:05:34 -07004075 }
Joe Perches323c1262012-12-17 16:02:07 -08004076 }
4077 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004078
4079#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004080 if ($line =~ /\#\s*define.*\\\s+$/) {
4081 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4082 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4083 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004084 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004085 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004086 }
4087
Andy Whitcroft653d4872007-06-23 17:16:34 -07004088#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004089 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004090 my $file = "$1.h";
4091 my $checkfile = "include/linux/$file";
4092 if (-f "$root/$checkfile" &&
4093 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004094 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004095 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004096 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004097 CHK("ARCH_INCLUDE_LINUX",
4098 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004099 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004100 WARN("INCLUDE_LINUX",
4101 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004102 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004103 }
4104 }
4105
Andy Whitcroft653d4872007-06-23 17:16:34 -07004106# multi-statement macros should be enclosed in a do while loop, grab the
4107# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004108# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004109 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4110 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004111 my $ln = $linenr;
4112 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004113 my ($off, $dstat, $dcond, $rest);
4114 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004115 my $has_flow_statement = 0;
4116 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004117 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004118 ctx_statement_block($linenr, $realcnt, 0);
4119 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004120 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004121 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004122
Joe Perches08a28432014-10-13 15:51:55 -07004123 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4124 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4125
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004126 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004127 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004128 $dstat =~ s/\\\n.//g;
4129 $dstat =~ s/^\s*//s;
4130 $dstat =~ s/\s*$//s;
4131
4132 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004133 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4134 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004135 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004136 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004137 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004138
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004139 # Flatten any obvious string concatentation.
4140 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4141 $dstat =~ s/$Ident\s*("X*")/$1/)
4142 {
4143 }
4144
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004145 my $exceptions = qr{
4146 $Declare|
4147 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004148 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004149 DECLARE_PER_CPU|
4150 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004151 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004152 union|
4153 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004154 \.$Ident\s*=\s*|
4155 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004156 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004157 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004158 if ($dstat ne '' &&
4159 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4160 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004161 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004162 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004163 $dstat !~ /$exceptions/ &&
4164 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004165 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004166 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004167 $dstat !~ /^for\s*$Constant$/ && # for (...)
4168 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4169 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004170 $dstat !~ /^\({/ && # ({...
4171 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004172 {
4173 $ctx =~ s/\n*$//;
4174 my $herectx = $here . "\n";
4175 my $cnt = statement_rawlines($ctx);
4176
4177 for (my $n = 0; $n < $cnt; $n++) {
4178 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004179 }
4180
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004181 if ($dstat =~ /;/) {
4182 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4183 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4184 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004185 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004186 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004187 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004188 }
Joe Perches5023d342012-12-17 16:01:47 -08004189
Joe Perches08a28432014-10-13 15:51:55 -07004190# check for macros with flow control, but without ## concatenation
4191# ## concatenation is commonly a macro that defines a function so ignore those
4192 if ($has_flow_statement && !$has_arg_concat) {
4193 my $herectx = $here . "\n";
4194 my $cnt = statement_rawlines($ctx);
4195
4196 for (my $n = 0; $n < $cnt; $n++) {
4197 $herectx .= raw_line($linenr, $n) . "\n";
4198 }
4199 WARN("MACRO_WITH_FLOW_CONTROL",
4200 "Macros with flow control statements should be avoided\n" . "$herectx");
4201 }
4202
Joe Perches481eb482012-12-17 16:01:56 -08004203# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004204
4205 } else {
4206 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004207 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4208 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004209 $line =~ /^\+.*\\$/) {
4210 WARN("LINE_CONTINUATIONS",
4211 "Avoid unnecessary line continuations\n" . $herecurr);
4212 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004213 }
4214
Joe Perchesb13edf72012-07-30 14:41:24 -07004215# do {} while (0) macro tests:
4216# single-statement macros do not need to be enclosed in do while (0) loop,
4217# macro should not end with a semicolon
4218 if ($^V && $^V ge 5.10.0 &&
4219 $realfile !~ m@/vmlinux.lds.h$@ &&
4220 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4221 my $ln = $linenr;
4222 my $cnt = $realcnt;
4223 my ($off, $dstat, $dcond, $rest);
4224 my $ctx = '';
4225 ($dstat, $dcond, $ln, $cnt, $off) =
4226 ctx_statement_block($linenr, $realcnt, 0);
4227 $ctx = $dstat;
4228
4229 $dstat =~ s/\\\n.//g;
4230
4231 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4232 my $stmts = $2;
4233 my $semis = $3;
4234
4235 $ctx =~ s/\n*$//;
4236 my $cnt = statement_rawlines($ctx);
4237 my $herectx = $here . "\n";
4238
4239 for (my $n = 0; $n < $cnt; $n++) {
4240 $herectx .= raw_line($linenr, $n) . "\n";
4241 }
4242
Joe Perchesac8e97f2012-08-21 16:15:53 -07004243 if (($stmts =~ tr/;/;/) == 1 &&
4244 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004245 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4246 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4247 }
4248 if (defined $semis && $semis ne "") {
4249 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4250 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4251 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004252 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4253 $ctx =~ s/\n*$//;
4254 my $cnt = statement_rawlines($ctx);
4255 my $herectx = $here . "\n";
4256
4257 for (my $n = 0; $n < $cnt; $n++) {
4258 $herectx .= raw_line($linenr, $n) . "\n";
4259 }
4260
4261 WARN("TRAILING_SEMICOLON",
4262 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004263 }
4264 }
4265
Mike Frysinger080ba922009-01-06 14:41:25 -08004266# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4267# all assignments may have only one of the following with an assignment:
4268# .
4269# ALIGN(...)
4270# VMLINUX_SYMBOL(...)
4271 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004272 WARN("MISSING_VMLINUX_SYMBOL",
4273 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004274 }
4275
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004276# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004277 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4278 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004279 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004280 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004281 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4282 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004283 my @allowed = ();
4284 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004285 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004286 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004287 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004288 for my $chunk (@chunks) {
4289 my ($cond, $block) = @{$chunk};
4290
Andy Whitcroft773647a2008-03-28 14:15:58 -07004291 # If the condition carries leading newlines, then count those as offsets.
4292 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4293 my $offset = statement_rawlines($whitespace) - 1;
4294
Joe Perchesaad4f612012-03-23 15:02:19 -07004295 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004296 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4297
4298 # We have looked at and allowed this specific line.
4299 $suppress_ifbraces{$ln + $offset} = 1;
4300
4301 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004302 $ln += statement_rawlines($block) - 1;
4303
Andy Whitcroft773647a2008-03-28 14:15:58 -07004304 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004305
4306 $seen++ if ($block =~ /^\s*{/);
4307
Joe Perchesaad4f612012-03-23 15:02:19 -07004308 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004309 if (statement_lines($cond) > 1) {
4310 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004311 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004312 }
4313 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004314 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004315 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004316 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004317 if (statement_block_size($block) > 1) {
4318 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004319 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004320 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004321 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004322 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004323 if ($seen) {
4324 my $sum_allowed = 0;
4325 foreach (@allowed) {
4326 $sum_allowed += $_;
4327 }
4328 if ($sum_allowed == 0) {
4329 WARN("BRACES",
4330 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4331 } elsif ($sum_allowed != $allow &&
4332 $seen != $allow) {
4333 CHK("BRACES",
4334 "braces {} should be used on all arms of this statement\n" . $herectx);
4335 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004336 }
4337 }
4338 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004340 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004341 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004342
Andy Whitcroftcf655042008-03-04 14:28:20 -08004343 # Check the pre-context.
4344 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4345 #print "APW: ALLOWED: pre<$1>\n";
4346 $allowed = 1;
4347 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004348
4349 my ($level, $endln, @chunks) =
4350 ctx_statement_full($linenr, $realcnt, $-[0]);
4351
Andy Whitcroftcf655042008-03-04 14:28:20 -08004352 # Check the condition.
4353 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004354 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004355 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004356 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004357 }
4358 if (statement_lines($cond) > 1) {
4359 #print "APW: ALLOWED: cond<$cond>\n";
4360 $allowed = 1;
4361 }
4362 if ($block =~/\b(?:if|for|while)\b/) {
4363 #print "APW: ALLOWED: block<$block>\n";
4364 $allowed = 1;
4365 }
4366 if (statement_block_size($block) > 1) {
4367 #print "APW: ALLOWED: lines block<$block>\n";
4368 $allowed = 1;
4369 }
4370 # Check the post-context.
4371 if (defined $chunks[1]) {
4372 my ($cond, $block) = @{$chunks[1]};
4373 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004374 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004375 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004376 if ($block =~ /^\s*\{/) {
4377 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4378 $allowed = 1;
4379 }
4380 }
4381 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004382 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004383 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004384
Andy Whitcroftf0556632008-10-15 22:02:23 -07004385 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004386 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004387 }
4388
Joe Perches000d1cc12011-07-25 17:13:25 -07004389 WARN("BRACES",
4390 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004391 }
4392 }
4393
Joe Perches0979ae62012-12-17 16:01:59 -08004394# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004395 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004396 CHK("BRACES",
4397 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4398 }
Joe Perches77b9a532013-07-03 15:05:29 -07004399 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004400 CHK("BRACES",
4401 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4402 }
4403
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004404# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004405 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4406 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004407 WARN("VOLATILE",
4408 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004409 }
4410
Joe Perchesf17dba42014-10-13 15:51:51 -07004411# concatenated string without spaces between elements
4412 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4413 CHK("CONCATENATED_STRING",
4414 "Concatenated strings should use spaces between elements\n" . $herecurr);
4415 }
4416
Andy Whitcroft00df3442007-06-08 13:47:06 -07004417# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004418 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004419 CHK("REDUNDANT_CODE",
4420 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004421 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004422 }
4423
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004424# check for needless "if (<foo>) fn(<foo>)" uses
4425 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4426 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4427 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4428 WARN('NEEDLESS_IF',
4429 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004430 }
4431 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004432
Joe Perchesebfdc402014-08-06 16:10:27 -07004433# check for unnecessary "Out of Memory" messages
4434 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4435 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4436 (defined $1 || defined $3) &&
4437 $linenr > 3) {
4438 my $testval = $2;
4439 my $testline = $lines[$linenr - 3];
4440
4441 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4442# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4443
4444 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4445 WARN("OOM_MESSAGE",
4446 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4447 }
4448 }
4449
Joe Perchesf78d98f2014-10-13 15:52:01 -07004450# check for logging functions with KERN_<LEVEL>
4451 if ($line !~ /printk\s*\(/ &&
4452 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4453 my $level = $1;
4454 if (WARN("UNNECESSARY_KERN_LEVEL",
4455 "Possible unnecessary $level\n" . $herecurr) &&
4456 $fix) {
4457 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4458 }
4459 }
4460
Joe Perches8716de32013-09-11 14:24:05 -07004461# check for bad placement of section $InitAttribute (e.g.: __initdata)
4462 if ($line =~ /(\b$InitAttribute\b)/) {
4463 my $attr = $1;
4464 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4465 my $ptr = $1;
4466 my $var = $2;
4467 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4468 ERROR("MISPLACED_INIT",
4469 "$attr should be placed after $var\n" . $herecurr)) ||
4470 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4471 WARN("MISPLACED_INIT",
4472 "$attr should be placed after $var\n" . $herecurr))) &&
4473 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004474 $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 -07004475 }
4476 }
4477 }
4478
Joe Perchese970b882013-11-12 15:10:10 -08004479# check for $InitAttributeData (ie: __initdata) with const
4480 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4481 my $attr = $1;
4482 $attr =~ /($InitAttributePrefix)(.*)/;
4483 my $attr_prefix = $1;
4484 my $attr_type = $2;
4485 if (ERROR("INIT_ATTRIBUTE",
4486 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4487 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004488 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004489 s/$InitAttributeData/${attr_prefix}initconst/;
4490 }
4491 }
4492
4493# check for $InitAttributeConst (ie: __initconst) without const
4494 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4495 my $attr = $1;
4496 if (ERROR("INIT_ATTRIBUTE",
4497 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4498 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004499 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004500 /(^\+\s*(?:static\s+))/;
4501 $lead = rtrim($1);
4502 $lead = "$lead " if ($lead !~ /^\+$/);
4503 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004504 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004505 }
4506 }
4507
Joe Perchesfbdb8132014-04-03 14:49:14 -07004508# don't use __constant_<foo> functions outside of include/uapi/
4509 if ($realfile !~ m@^include/uapi/@ &&
4510 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4511 my $constant_func = $1;
4512 my $func = $constant_func;
4513 $func =~ s/^__constant_//;
4514 if (WARN("CONSTANT_CONVERSION",
4515 "$constant_func should be $func\n" . $herecurr) &&
4516 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004517 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004518 }
4519 }
4520
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004521# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004522 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004523 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004524 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004525 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004526 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004527 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4528 }
4529 if ($delay > 2000) {
4530 WARN("LONG_UDELAY",
4531 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004532 }
4533 }
4534
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004535# warn about unexpectedly long msleep's
4536 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4537 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004538 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004539 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004540 }
4541 }
4542
Joe Perches36ec1932013-07-03 15:05:25 -07004543# check for comparisons of jiffies
4544 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4545 WARN("JIFFIES_COMPARISON",
4546 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4547 }
4548
Joe Perches9d7a34a2013-07-03 15:05:26 -07004549# check for comparisons of get_jiffies_64()
4550 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4551 WARN("JIFFIES_COMPARISON",
4552 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4553 }
4554
Andy Whitcroft00df3442007-06-08 13:47:06 -07004555# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004556# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004557# print "#ifdef in C files should be avoided\n";
4558# print "$herecurr";
4559# $clean = 0;
4560# }
4561
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004562# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004563 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004564 if (ERROR("SPACING",
4565 "exactly one space required after that #$1\n" . $herecurr) &&
4566 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004567 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004568 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4569 }
4570
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004571 }
4572
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004573# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004574 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4575 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004576 my $which = $1;
4577 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004578 CHK("UNCOMMENTED_DEFINITION",
4579 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004580 }
4581 }
4582# check for memory barriers without a comment.
4583 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4584 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004585 WARN("MEMORY_BARRIER",
4586 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004587 }
4588 }
4589# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004590 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004591 CHK("ARCH_DEFINES",
4592 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004593 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004594
Tobias Klauserd4977c72010-05-24 14:33:30 -07004595# Check that the storage class is at the beginning of a declaration
4596 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004597 WARN("STORAGE_CLASS",
4598 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004599 }
4600
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004601# check the location of the inline attribute, that it is between
4602# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004603 if ($line =~ /\b$Type\s+$Inline\b/ ||
4604 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004605 ERROR("INLINE_LOCATION",
4606 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004607 }
4608
Andy Whitcroft8905a672007-11-28 16:21:06 -08004609# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004610 if ($realfile !~ m@\binclude/uapi/@ &&
4611 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004612 if (WARN("INLINE",
4613 "plain inline is preferred over $1\n" . $herecurr) &&
4614 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004615 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004616
4617 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004618 }
4619
Joe Perches3d130fd2011-01-12 17:00:00 -08004620# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004621 if ($realfile !~ m@\binclude/uapi/@ &&
4622 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004623 WARN("PREFER_PACKED",
4624 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004625 }
4626
Joe Perches39b7e282011-07-25 17:13:24 -07004627# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004628 if ($realfile !~ m@\binclude/uapi/@ &&
4629 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004630 WARN("PREFER_ALIGNED",
4631 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004632 }
4633
Joe Perches5f14d3b2012-01-10 15:09:52 -08004634# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004635 if ($realfile !~ m@\binclude/uapi/@ &&
4636 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004637 if (WARN("PREFER_PRINTF",
4638 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4639 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004640 $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 -07004641
4642 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004643 }
4644
Joe Perches6061d942012-03-23 15:02:16 -07004645# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004646 if ($realfile !~ m@\binclude/uapi/@ &&
4647 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004648 if (WARN("PREFER_SCANF",
4649 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4650 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004651 $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 -07004652 }
Joe Perches6061d942012-03-23 15:02:16 -07004653 }
4654
Joe Perches8f53a9b2010-03-05 13:43:48 -08004655# check for sizeof(&)
4656 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004657 WARN("SIZEOF_ADDRESS",
4658 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004659 }
4660
Joe Perches66c80b62012-07-30 14:41:22 -07004661# check for sizeof without parenthesis
4662 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004663 if (WARN("SIZEOF_PARENTHESIS",
4664 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4665 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004666 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004667 }
Joe Perches66c80b62012-07-30 14:41:22 -07004668 }
4669
Joe Perches428e2fd2011-05-24 17:13:39 -07004670# check for line continuations in quoted strings with odd counts of "
4671 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004672 WARN("LINE_CONTINUATIONS",
4673 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004674 }
4675
Joe Perches88982fe2012-12-17 16:02:00 -08004676# check for struct spinlock declarations
4677 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4678 WARN("USE_SPINLOCK_T",
4679 "struct spinlock should be spinlock_t\n" . $herecurr);
4680 }
4681
Joe Perchesa6962d72013-04-29 16:18:13 -07004682# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004683 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004684 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004685 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004686 if (WARN("PREFER_SEQ_PUTS",
4687 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4688 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004689 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004690 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004691 }
4692 }
4693
Andy Whitcroft554e1652012-01-10 15:09:57 -08004694# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004695 if ($^V && $^V ge 5.10.0 &&
4696 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004697 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004698
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004699 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004700 my $ms_val = $7;
4701 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004702
Andy Whitcroft554e1652012-01-10 15:09:57 -08004703 if ($ms_size =~ /^(0x|)0$/i) {
4704 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004705 "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 -08004706 } elsif ($ms_size =~ /^(0x|)1$/i) {
4707 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004708 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4709 }
4710 }
4711
Joe Perches98a9bba2014-01-23 15:54:52 -08004712# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4713 if ($^V && $^V ge 5.10.0 &&
4714 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4715 if (WARN("PREFER_ETHER_ADDR_COPY",
4716 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4717 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004718 $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 -08004719 }
4720 }
4721
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004722# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004723 if ($^V && $^V ge 5.10.0 &&
4724 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004725 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004726 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004727 my $call = $1;
4728 my $cast1 = deparenthesize($2);
4729 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004730 my $cast2 = deparenthesize($7);
4731 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004732 my $cast;
4733
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004734 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004735 $cast = "$cast1 or $cast2";
4736 } elsif ($cast1 ne "") {
4737 $cast = $cast1;
4738 } else {
4739 $cast = $cast2;
4740 }
4741 WARN("MINMAX",
4742 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004743 }
4744 }
4745
Joe Perches4a273192012-07-30 14:41:20 -07004746# check usleep_range arguments
4747 if ($^V && $^V ge 5.10.0 &&
4748 defined $stat &&
4749 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4750 my $min = $1;
4751 my $max = $7;
4752 if ($min eq $max) {
4753 WARN("USLEEP_RANGE",
4754 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4755 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4756 $min > $max) {
4757 WARN("USLEEP_RANGE",
4758 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4759 }
4760 }
4761
Joe Perches823b7942013-11-12 15:10:15 -08004762# check for naked sscanf
4763 if ($^V && $^V ge 5.10.0 &&
4764 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004765 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004766 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4767 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4768 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4769 my $lc = $stat =~ tr@\n@@;
4770 $lc = $lc + $linenr;
4771 my $stat_real = raw_line($linenr, 0);
4772 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4773 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4774 }
4775 WARN("NAKED_SSCANF",
4776 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4777 }
4778
Joe Perchesafc819a2014-06-04 16:12:08 -07004779# check for simple sscanf that should be kstrto<foo>
4780 if ($^V && $^V ge 5.10.0 &&
4781 defined $stat &&
4782 $line =~ /\bsscanf\b/) {
4783 my $lc = $stat =~ tr@\n@@;
4784 $lc = $lc + $linenr;
4785 my $stat_real = raw_line($linenr, 0);
4786 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4787 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4788 }
4789 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4790 my $format = $6;
4791 my $count = $format =~ tr@%@%@;
4792 if ($count == 1 &&
4793 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4794 WARN("SSCANF_TO_KSTRTO",
4795 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4796 }
4797 }
4798 }
4799
Joe Perches70dc8a42013-09-11 14:23:58 -07004800# check for new externs in .h files.
4801 if ($realfile =~ /\.h$/ &&
4802 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004803 if (CHK("AVOID_EXTERNS",
4804 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004805 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004806 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004807 }
4808 }
4809
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004810# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004811 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004812 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004813 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004814 my $function_name = $1;
4815 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004816
4817 my $s = $stat;
4818 if (defined $cond) {
4819 substr($s, 0, length($cond), '');
4820 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004821 if ($s =~ /^\s*;/ &&
4822 $function_name ne 'uninitialized_var')
4823 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004824 WARN("AVOID_EXTERNS",
4825 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004826 }
4827
4828 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004829 WARN("FUNCTION_ARGUMENTS",
4830 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004831 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004832
4833 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4834 $stat =~ /^.\s*extern\s+/)
4835 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004836 WARN("AVOID_EXTERNS",
4837 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004838 }
4839
4840# checks for new __setup's
4841 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4842 my $name = $1;
4843
4844 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004845 CHK("UNDOCUMENTED_SETUP",
4846 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004847 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004848 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004849
4850# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004851 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004852 WARN("UNNECESSARY_CASTS",
4853 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004854 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004855
Joe Perchesa640d252013-07-03 15:05:21 -07004856# alloc style
4857# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4858 if ($^V && $^V ge 5.10.0 &&
4859 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4860 CHK("ALLOC_SIZEOF_STRUCT",
4861 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4862 }
4863
Joe Perches60a55362014-06-04 16:12:07 -07004864# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4865 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004866 $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 -07004867 my $oldfunc = $3;
4868 my $a1 = $4;
4869 my $a2 = $10;
4870 my $newfunc = "kmalloc_array";
4871 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004872 my $r1 = $a1;
4873 my $r2 = $a2;
4874 if ($a1 =~ /^sizeof\s*\S/) {
4875 $r1 = $a2;
4876 $r2 = $a1;
4877 }
4878 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4879 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004880 if (WARN("ALLOC_WITH_MULTIPLY",
4881 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4882 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004883 $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 -07004884
4885 }
4886 }
4887 }
4888
Joe Perches972fdea2013-04-29 16:18:12 -07004889# check for krealloc arg reuse
4890 if ($^V && $^V ge 5.10.0 &&
4891 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4892 WARN("KREALLOC_ARG_REUSE",
4893 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4894 }
4895
Joe Perches5ce59ae2013-02-21 16:44:18 -08004896# check for alloc argument mismatch
4897 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4898 WARN("ALLOC_ARRAY_ARGS",
4899 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4900 }
4901
Joe Perchescaf2a542011-01-12 16:59:56 -08004902# check for multiple semicolons
4903 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004904 if (WARN("ONE_SEMICOLON",
4905 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4906 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004907 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004908 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004909 }
4910
Joe Perchese81f2392014-08-06 16:11:25 -07004911# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08004912 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4913 my $has_break = 0;
4914 my $has_statement = 0;
4915 my $count = 0;
4916 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07004917 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08004918 $prevline--;
4919 my $rline = $rawlines[$prevline - 1];
4920 my $fline = $lines[$prevline - 1];
4921 last if ($fline =~ /^\@\@/);
4922 next if ($fline =~ /^\-/);
4923 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4924 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4925 next if ($fline =~ /^.[\s$;]*$/);
4926 $has_statement = 1;
4927 $count++;
4928 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4929 }
4930 if (!$has_break && $has_statement) {
4931 WARN("MISSING_BREAK",
4932 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4933 }
4934 }
4935
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004936# check for switch/default statements without a break;
4937 if ($^V && $^V ge 5.10.0 &&
4938 defined $stat &&
4939 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4940 my $ctx = '';
4941 my $herectx = $here . "\n";
4942 my $cnt = statement_rawlines($stat);
4943 for (my $n = 0; $n < $cnt; $n++) {
4944 $herectx .= raw_line($linenr, $n) . "\n";
4945 }
4946 WARN("DEFAULT_NO_BREAK",
4947 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004948 }
4949
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004950# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004951 if ($line =~ /\b__FUNCTION__\b/) {
4952 if (WARN("USE_FUNC",
4953 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4954 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004955 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004956 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004957 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004958
Joe Perches2c924882012-03-23 15:02:20 -07004959# check for use of yield()
4960 if ($line =~ /\byield\s*\(\s*\)/) {
4961 WARN("YIELD",
4962 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4963 }
4964
Joe Perches179f8f42013-07-03 15:05:30 -07004965# check for comparisons against true and false
4966 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4967 my $lead = $1;
4968 my $arg = $2;
4969 my $test = $3;
4970 my $otype = $4;
4971 my $trail = $5;
4972 my $op = "!";
4973
4974 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4975
4976 my $type = lc($otype);
4977 if ($type =~ /^(?:true|false)$/) {
4978 if (("$test" eq "==" && "$type" eq "true") ||
4979 ("$test" eq "!=" && "$type" eq "false")) {
4980 $op = "";
4981 }
4982
4983 CHK("BOOL_COMPARISON",
4984 "Using comparison to $otype is error prone\n" . $herecurr);
4985
4986## maybe suggesting a correct construct would better
4987## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4988
4989 }
4990 }
4991
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004992# check for semaphores initialized locked
4993 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004994 WARN("CONSIDER_COMPLETION",
4995 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004996 }
Joe Perches6712d852012-03-23 15:02:20 -07004997
Joe Perches67d0a072011-10-31 17:13:10 -07004998# recommend kstrto* over simple_strto* and strict_strto*
4999 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005000 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005001 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005002 }
Joe Perches6712d852012-03-23 15:02:20 -07005003
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005004# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005005 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005006 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005007 "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 -07005008 }
Joe Perches6712d852012-03-23 15:02:20 -07005009
Emese Revfy79404842010-03-05 13:43:53 -08005010# check for various ops structs, ensure they are const.
5011 my $struct_ops = qr{acpi_dock_ops|
5012 address_space_operations|
5013 backlight_ops|
5014 block_device_operations|
5015 dentry_operations|
5016 dev_pm_ops|
5017 dma_map_ops|
5018 extent_io_ops|
5019 file_lock_operations|
5020 file_operations|
5021 hv_ops|
5022 ide_dma_ops|
5023 intel_dvo_dev_ops|
5024 item_operations|
5025 iwl_ops|
5026 kgdb_arch|
5027 kgdb_io|
5028 kset_uevent_ops|
5029 lock_manager_operations|
5030 microcode_ops|
5031 mtrr_ops|
5032 neigh_ops|
5033 nlmsvc_binding|
5034 pci_raw_ops|
5035 pipe_buf_operations|
5036 platform_hibernation_ops|
5037 platform_suspend_ops|
5038 proto_ops|
5039 rpc_pipe_ops|
5040 seq_operations|
5041 snd_ac97_build_ops|
5042 soc_pcmcia_socket_ops|
5043 stacktrace_ops|
5044 sysfs_ops|
5045 tty_operations|
5046 usb_mon_operations|
5047 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005048 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08005049 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005050 WARN("CONST_STRUCT",
5051 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005052 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005053 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005054
5055# use of NR_CPUS is usually wrong
5056# ignore definitions of NR_CPUS and usage to define arrays as likely right
5057 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005058 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5059 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005060 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5061 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5062 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005063 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005064 WARN("NR_CPUS",
5065 "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 -07005066 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005067
Joe Perches52ea8502013-11-12 15:10:09 -08005068# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5069 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5070 ERROR("DEFINE_ARCH_HAS",
5071 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5072 }
5073
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005074# check for %L{u,d,i} in strings
5075 my $string;
5076 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5077 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07005078 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005079 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005080 WARN("PRINTF_L",
5081 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005082 last;
5083 }
5084 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005085
5086# whine mightly about in_atomic
5087 if ($line =~ /\bin_atomic\s*\(/) {
5088 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005089 ERROR("IN_ATOMIC",
5090 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005091 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005092 WARN("IN_ATOMIC",
5093 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005094 }
5095 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005096
5097# check for lockdep_set_novalidate_class
5098 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5099 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5100 if ($realfile !~ m@^kernel/lockdep@ &&
5101 $realfile !~ m@^include/linux/lockdep@ &&
5102 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005103 ERROR("LOCKDEP",
5104 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005105 }
5106 }
Dave Jones88f88312011-01-12 16:59:59 -08005107
5108 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5109 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005110 WARN("EXPORTED_WORLD_WRITABLE",
5111 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005112 }
Joe Perches24358802014-04-03 14:49:13 -07005113
Joe Perches515a2352014-04-03 14:49:24 -07005114# Mode permission misuses where it seems decimal should be octal
5115# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5116 if ($^V && $^V ge 5.10.0 &&
5117 $line =~ /$mode_perms_search/) {
5118 foreach my $entry (@mode_permission_funcs) {
5119 my $func = $entry->[0];
5120 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005121
Joe Perches515a2352014-04-03 14:49:24 -07005122 my $skip_args = "";
5123 if ($arg_pos > 1) {
5124 $arg_pos--;
5125 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5126 }
5127 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5128 if ($line =~ /$test/) {
5129 my $val = $1;
5130 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005131
Joe Perches515a2352014-04-03 14:49:24 -07005132 if ($val !~ /^0$/ &&
5133 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5134 length($val) ne 4)) {
5135 ERROR("NON_OCTAL_PERMISSIONS",
5136 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5137 }
Joe Perches24358802014-04-03 14:49:13 -07005138 }
5139 }
5140 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005141 }
5142
5143 # If we have no input at all, then there is nothing to report on
5144 # so just keep quiet.
5145 if ($#rawlines == -1) {
5146 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005147 }
5148
Andy Whitcroft8905a672007-11-28 16:21:06 -08005149 # In mailback mode only produce a report in the negative, for
5150 # things that appear to be patches.
5151 if ($mailback && ($clean == 1 || !$is_patch)) {
5152 exit(0);
5153 }
5154
5155 # This is not a patch, and we are are in 'no-patch' mode so
5156 # just keep quiet.
5157 if (!$chk_patch && !$is_patch) {
5158 exit(0);
5159 }
5160
5161 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005162 ERROR("NOT_UNIFIED_DIFF",
5163 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005164 }
5165 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005166 ERROR("MISSING_SIGN_OFF",
5167 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005168 }
5169
Andy Whitcroft8905a672007-11-28 16:21:06 -08005170 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005171 if ($summary && !($clean == 1 && $quiet == 1)) {
5172 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005173 print "total: $cnt_error errors, $cnt_warn warnings, " .
5174 (($check)? "$cnt_chk checks, " : "") .
5175 "$cnt_lines lines checked\n";
5176 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005177 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005178
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005179 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005180
5181 if ($^V lt 5.10.0) {
5182 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5183 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5184 }
5185
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005186 # If there were whitespace errors which cleanpatch can fix
5187 # then suggest that.
5188 if ($rpt_cleaners) {
5189 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5190 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005191 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005192 }
5193 }
5194
Joe Perches91bfe482013-09-11 14:23:59 -07005195 hash_show_words(\%use_type, "Used");
5196 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005197
Joe Perchesd752fcc2014-08-06 16:11:05 -07005198 if ($clean == 0 && $fix &&
5199 ("@rawlines" ne "@fixed" ||
5200 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005201 my $newfile = $filename;
5202 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005203 my $linecount = 0;
5204 my $f;
5205
Joe Perchesd752fcc2014-08-06 16:11:05 -07005206 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5207
Joe Perches3705ce52013-07-03 15:05:31 -07005208 open($f, '>', $newfile)
5209 or die "$P: Can't open $newfile for write\n";
5210 foreach my $fixed_line (@fixed) {
5211 $linecount++;
5212 if ($file) {
5213 if ($linecount > 3) {
5214 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005215 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005216 }
5217 } else {
5218 print $f $fixed_line . "\n";
5219 }
5220 }
5221 close($f);
5222
5223 if (!$quiet) {
5224 print << "EOM";
5225Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5226
5227Do _NOT_ trust the results written to this file.
5228Do _NOT_ submit these changes without inspecting them for correctness.
5229
5230This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5231No warranties, expressed or implied...
5232
5233EOM
5234 }
5235 }
5236
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005237 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005238 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005239 }
5240 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005241 print << "EOM";
5242$vname has style problems, please review.
5243
5244If any of these errors are false positives, please report
5245them to the maintainer, see CHECKPATCH in MAINTAINERS.
5246EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005247 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005248
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005249 return $clean;
5250}