blob: 31a731e06f5022afd7c040d5688840491b228e9d [file] [log] [blame]
Andy Whitcroft0a920b5b2007-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 Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830be2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070010
11my $P = $0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -070012$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070013
Joe Perches000d1cc12011-07-25 17:13:25 -070014my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
21my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070022my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080024my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $file = 0;
26my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070027my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080028my $summary = 1;
29my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080030my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070031my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070032my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080033my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070034my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080035my %debug;
Joe Perches34456862013-07-03 15:05:34 -070036my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070037my %use_type = ();
38my @use = ();
39my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070040my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070041my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070042my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080043my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070044my $ignore_perl_version = 0;
45my $minimum_perl_version = 5.10.0;
Hannes Eder77f5b102009-09-21 17:04:37 -070046
47sub help {
48 my ($exitcode) = @_;
49
50 print << "EOM";
51Usage: $P [OPTION]... [FILE]...
52Version: $V
53
54Options:
55 -q, --quiet quiet
56 --no-tree run without a kernel tree
57 --no-signoff do not check for 'Signed-off-by' line
58 --patch treat FILE as patchfile (default)
59 --emacs emacs compile window format
60 --terse one line per report
61 -f, --file treat FILE as regular source file
62 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070063 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070064 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080065 --max-line-length=n set the maximum line length, if exceeded, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070066 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070067 --root=PATH PATH to the kernel tree root
68 --no-summary suppress the per-file summary
69 --mailback only produce a report in case of warnings/errors
70 --summary-file include the filename in summary
71 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
72 'values', 'possible', 'type', and 'attr' (default
73 is all off)
74 --test-only=WORD report only warnings/errors containing WORD
75 literally
Joe Perches3705ce52013-07-03 15:05:31 -070076 --fix EXPERIMENTAL - may create horrible results
77 If correctable single-line errors exist, create
78 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
79 with potential errors corrected to the preferred
80 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080081 --fix-inplace EXPERIMENTAL - may create horrible results
82 Is the same as --fix, but overwrites the input
83 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070084 --ignore-perl-version override checking of perl version. expect
85 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070086 -h, --help, --version display this help and exit
87
88When FILE is - read standard input.
89EOM
90
91 exit($exitcode);
92}
93
Joe Perches000d1cc12011-07-25 17:13:25 -070094my $conf = which_conf($configuration_file);
95if (-f $conf) {
96 my @conf_args;
97 open(my $conffile, '<', "$conf")
98 or warn "$P: Can't find a readable $configuration_file file $!\n";
99
100 while (<$conffile>) {
101 my $line = $_;
102
103 $line =~ s/\s*\n?$//g;
104 $line =~ s/^\s*//g;
105 $line =~ s/\s+/ /g;
106
107 next if ($line =~ m/^\s*#/);
108 next if ($line =~ m/^\s*$/);
109
110 my @words = split(" ", $line);
111 foreach my $word (@words) {
112 last if ($word =~ m/^#/);
113 push (@conf_args, $word);
114 }
115 }
116 close($conffile);
117 unshift(@ARGV, @conf_args) if @conf_args;
118}
119
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700120GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700121 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700122 'tree!' => \$tree,
123 'signoff!' => \$chk_signoff,
124 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700125 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800126 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700127 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700128 'subjective!' => \$check,
129 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700130 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700131 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700132 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800133 'max-line-length=i' => \$max_line_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700134 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800135 'summary!' => \$summary,
136 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800137 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700138 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800139 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700140 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800141 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700142 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700143 'h|help' => \$help,
144 'version' => \$help
145) or help(1);
146
147help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700148
Joe Perches9624b8d2014-01-23 15:54:44 -0800149$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700150$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800151
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700152my $exit = 0;
153
Dave Hansend62a2012013-09-11 14:23:56 -0700154if ($^V && $^V lt $minimum_perl_version) {
155 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
156 if (!$ignore_perl_version) {
157 exit(1);
158 }
159}
160
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700161if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700162 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700163 exit(1);
164}
165
Joe Perches91bfe482013-09-11 14:23:59 -0700166sub hash_save_array_words {
167 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700168
Joe Perches91bfe482013-09-11 14:23:59 -0700169 my @array = split(/,/, join(',', @$arrayRef));
170 foreach my $word (@array) {
171 $word =~ s/\s*\n?$//g;
172 $word =~ s/^\s*//g;
173 $word =~ s/\s+/ /g;
174 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700175
Joe Perches91bfe482013-09-11 14:23:59 -0700176 next if ($word =~ m/^\s*#/);
177 next if ($word =~ m/^\s*$/);
178
179 $hashRef->{$word}++;
180 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700181}
182
Joe Perches91bfe482013-09-11 14:23:59 -0700183sub hash_show_words {
184 my ($hashRef, $prefix) = @_;
185
Joe Perches58cb3cf2013-09-11 14:24:04 -0700186 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700187 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700188 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700189 print " $word";
190 }
191 print "\n\n";
192 }
193}
194
195hash_save_array_words(\%ignore_type, \@ignore);
196hash_save_array_words(\%use_type, \@use);
197
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800198my $dbg_values = 0;
199my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700200my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700201my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800202for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800203 ## no critic
204 eval "\${dbg_$key} = '$debug{$key}';";
205 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800206}
207
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700208my $rpt_cleaners = 0;
209
Andy Whitcroft8905a672007-11-28 16:21:06 -0800210if ($terse) {
211 $emacs = 1;
212 $quiet++;
213}
214
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700215if ($tree) {
216 if (defined $root) {
217 if (!top_of_kernel_tree($root)) {
218 die "$P: $root: --root does not point at a valid tree\n";
219 }
220 } else {
221 if (top_of_kernel_tree('.')) {
222 $root = '.';
223 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
224 top_of_kernel_tree($1)) {
225 $root = $1;
226 }
227 }
228
229 if (!defined $root) {
230 print "Must be run from the top-level dir. of a kernel tree\n";
231 exit(2);
232 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700233}
234
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700235my $emitted_corrupt = 0;
236
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700237our $Ident = qr{
238 [A-Za-z_][A-Za-z\d_]*
239 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
240 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241our $Storage = qr{extern|static|asmlinkage};
242our $Sparse = qr{
243 __user|
244 __kernel|
245 __force|
246 __iomem|
247 __must_check|
248 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800249 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700250 __ref|
251 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700252 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800253our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
254our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
255our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
256our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
257our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700258
Wolfram Sang52131292010-03-05 13:43:51 -0800259# Notes to $Attribute:
260# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700261our $Attribute = qr{
262 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700263 __percpu|
264 __nocast|
265 __safe|
266 __bitwise__|
267 __packed__|
268 __packed2__|
269 __naked|
270 __maybe_unused|
271 __always_unused|
272 __noreturn|
273 __used|
274 __cold|
275 __noclone|
276 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700277 __read_mostly|
278 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700279 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700280 ____cacheline_aligned|
281 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800282 ____cacheline_internodealigned_in_smp|
283 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700285our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700286our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700287our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
288our $Lval = qr{$Ident(?:$Member)*};
289
Joe Perches95e2c602013-07-03 15:05:20 -0700290our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
291our $Binary = qr{(?i)0b[01]+$Int_type?};
292our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
293our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700294our $Octal = qr{0[0-7]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800295our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
296our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
297our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800298our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700299our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800300our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700301our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700302our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700303our $Operators = qr{
304 <=|>=|==|!=|
305 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700306 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700307 }x;
308
Joe Perches91cb5192014-04-03 14:49:32 -0700309our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
310
Andy Whitcroft8905a672007-11-28 16:21:06 -0800311our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700312our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700313our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700315our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800316our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700317our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800318
Joe Perches15662b32011-10-31 17:13:12 -0700319our $NON_ASCII_UTF8 = qr{
320 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700321 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
322 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
323 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
324 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
325 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
326 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
327}x;
328
Joe Perches15662b32011-10-31 17:13:12 -0700329our $UTF8 = qr{
330 [\x09\x0A\x0D\x20-\x7E] # ASCII
331 | $NON_ASCII_UTF8
332}x;
333
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700334our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700335 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700336 atomic_t
337)};
338
Joe Perches691e6692010-03-05 13:43:51 -0800339our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700340 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700341 (?:[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 -0700342 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700343 panic|
Joe Perches06668722013-11-12 15:10:07 -0800344 MODULE_[A-Z_]+|
345 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800346)};
347
Joe Perches20112472011-07-25 17:13:23 -0700348our $signature_tags = qr{(?xi:
349 Signed-off-by:|
350 Acked-by:|
351 Tested-by:|
352 Reviewed-by:|
353 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700354 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700355 To:|
356 Cc:
357)};
358
Joe Perches18130872014-08-06 16:11:22 -0700359our @typeListMisordered = (
360 qr{char\s+(?:un)?signed},
361 qr{int\s+(?:(?:un)?signed\s+)?short\s},
362 qr{int\s+short(?:\s+(?:un)?signed)},
363 qr{short\s+int(?:\s+(?:un)?signed)},
364 qr{(?:un)?signed\s+int\s+short},
365 qr{short\s+(?:un)?signed},
366 qr{long\s+int\s+(?:un)?signed},
367 qr{int\s+long\s+(?:un)?signed},
368 qr{long\s+(?:un)?signed\s+int},
369 qr{int\s+(?:un)?signed\s+long},
370 qr{int\s+(?:un)?signed},
371 qr{int\s+long\s+long\s+(?:un)?signed},
372 qr{long\s+long\s+int\s+(?:un)?signed},
373 qr{long\s+long\s+(?:un)?signed\s+int},
374 qr{long\s+long\s+(?:un)?signed},
375 qr{long\s+(?:un)?signed},
376);
377
Andy Whitcroft8905a672007-11-28 16:21:06 -0800378our @typeList = (
379 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700380 qr{(?:(?:un)?signed\s+)?char},
381 qr{(?:(?:un)?signed\s+)?short\s+int},
382 qr{(?:(?:un)?signed\s+)?short},
383 qr{(?:(?:un)?signed\s+)?int},
384 qr{(?:(?:un)?signed\s+)?long\s+int},
385 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
386 qr{(?:(?:un)?signed\s+)?long\s+long},
387 qr{(?:(?:un)?signed\s+)?long},
388 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389 qr{float},
390 qr{double},
391 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392 qr{struct\s+$Ident},
393 qr{union\s+$Ident},
394 qr{enum\s+$Ident},
395 qr{${Ident}_t},
396 qr{${Ident}_handler},
397 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700398 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399);
Joe Perches8716de32013-09-11 14:24:05 -0700400our @typeListWithAttr = (
401 @typeList,
402 qr{struct\s+$InitAttribute\s+$Ident},
403 qr{union\s+$InitAttribute\s+$Ident},
404);
405
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700406our @modifierList = (
407 qr{fastcall},
408);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409
Joe Perches24358802014-04-03 14:49:13 -0700410our @mode_permission_funcs = (
411 ["module_param", 3],
412 ["module_param_(?:array|named|string)", 4],
413 ["module_param_array_named", 5],
414 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
415 ["proc_create(?:_data|)", 2],
416 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
417);
418
Joe Perches515a2352014-04-03 14:49:24 -0700419#Create a search pattern for all these functions to speed up a loop below
420our $mode_perms_search = "";
421foreach my $entry (@mode_permission_funcs) {
422 $mode_perms_search .= '|' if ($mode_perms_search ne "");
423 $mode_perms_search .= $entry->[0];
424}
425
Wolfram Sang7840a942010-08-09 17:20:57 -0700426our $allowed_asm_includes = qr{(?x:
427 irq|
428 memory
429)};
430# memory.h: ARM has a custom one
431
Andy Whitcroft8905a672007-11-28 16:21:06 -0800432sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700433 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
434 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700435 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700436 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700437 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800438 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700439 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800440 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800441 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700442 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700443 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800444 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700445 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800446 }x;
Joe Perches18130872014-08-06 16:11:22 -0700447 $NonptrTypeMisordered = qr{
448 (?:$Modifier\s+|const\s+)*
449 (?:
450 (?:${Misordered}\b)
451 )
452 (?:\s+$Modifier|\s+const)*
453 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700454 $NonptrTypeWithAttr = qr{
455 (?:$Modifier\s+|const\s+)*
456 (?:
457 (?:typeof|__typeof__)\s*\([^\)]*\)|
458 (?:$typeTypedefs\b)|
459 (?:${allWithAttr}\b)
460 )
461 (?:\s+$Modifier|\s+const)*
462 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800463 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700464 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700465 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700466 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467 }x;
Joe Perches18130872014-08-06 16:11:22 -0700468 $TypeMisordered = qr{
469 $NonptrTypeMisordered
470 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
471 (?:\s+$Inline|\s+$Modifier)*
472 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700473 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700474 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800475}
476build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700477
Joe Perches7d2367a2011-07-25 17:13:22 -0700478our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700479
480# Using $balanced_parens, $LvalOrFunc, or $FuncArg
481# requires at least perl version v5.10.0
482# Any use must be runtime checked with $^V
483
484our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700485our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800486our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700487
Joe Perchesf8422302014-08-06 16:11:31 -0700488our $declaration_macros = qr{(?x:
489 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
490 (?:$Storage\s+)?LIST_HEAD\s*\(|
491 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
492)};
493
Joe Perches7d2367a2011-07-25 17:13:22 -0700494sub deparenthesize {
495 my ($string) = @_;
496 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700497
498 while ($string =~ /^\s*\(.*\)\s*$/) {
499 $string =~ s@^\s*\(\s*@@;
500 $string =~ s@\s*\)\s*$@@;
501 }
502
Joe Perches7d2367a2011-07-25 17:13:22 -0700503 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700504
Joe Perches7d2367a2011-07-25 17:13:22 -0700505 return $string;
506}
507
Joe Perches34456862013-07-03 15:05:34 -0700508sub seed_camelcase_file {
509 my ($file) = @_;
510
511 return if (!(-f $file));
512
513 local $/;
514
515 open(my $include_file, '<', "$file")
516 or warn "$P: Can't read '$file' $!\n";
517 my $text = <$include_file>;
518 close($include_file);
519
520 my @lines = split('\n', $text);
521
522 foreach my $line (@lines) {
523 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
524 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
525 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800526 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
527 $camelcase{$1} = 1;
528 } 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 -0700529 $camelcase{$1} = 1;
530 }
531 }
532}
533
534my $camelcase_seeded = 0;
535sub seed_camelcase_includes {
536 return if ($camelcase_seeded);
537
538 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700539 my $camelcase_cache = "";
540 my @include_files = ();
541
542 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700543
Richard Genoud3645e322014-02-10 14:25:32 -0800544 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700545 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
546 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700547 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700548 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700549 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700550 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700551 @include_files = split('\n', $files);
552 foreach my $file (@include_files) {
553 my $date = POSIX::strftime("%Y%m%d%H%M",
554 localtime((stat $file)[9]));
555 $last_mod_date = $date if ($last_mod_date < $date);
556 }
557 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700558 }
Joe Perchesc707a812013-07-08 16:00:43 -0700559
560 if ($camelcase_cache ne "" && -f $camelcase_cache) {
561 open(my $camelcase_file, '<', "$camelcase_cache")
562 or warn "$P: Can't read '$camelcase_cache' $!\n";
563 while (<$camelcase_file>) {
564 chomp;
565 $camelcase{$_} = 1;
566 }
567 close($camelcase_file);
568
569 return;
570 }
571
Richard Genoud3645e322014-02-10 14:25:32 -0800572 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700573 $files = `git ls-files "include/*.h"`;
574 @include_files = split('\n', $files);
575 }
576
Joe Perches34456862013-07-03 15:05:34 -0700577 foreach my $file (@include_files) {
578 seed_camelcase_file($file);
579 }
Joe Perches351b2a12013-07-03 15:05:36 -0700580
Joe Perchesc707a812013-07-08 16:00:43 -0700581 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700582 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700583 open(my $camelcase_file, '>', "$camelcase_cache")
584 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700585 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
586 print $camelcase_file ("$_\n");
587 }
588 close($camelcase_file);
589 }
Joe Perches34456862013-07-03 15:05:34 -0700590}
591
Joe Perchesd311cd42014-08-06 16:10:57 -0700592sub git_commit_info {
593 my ($commit, $id, $desc) = @_;
594
595 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
596
597 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
598 $output =~ s/^\s*//gm;
599 my @lines = split("\n", $output);
600
601 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
602# Maybe one day convert this block of bash into something that returns
603# all matching commit ids, but it's very slow...
604#
605# echo "checking commits $1..."
606# git rev-list --remotes | grep -i "^$1" |
607# while read line ; do
608# git log --format='%H %s' -1 $line |
609# echo "commit $(cut -c 1-12,41-)"
610# done
611 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
612 } else {
613 $id = substr($lines[0], 0, 12);
614 $desc = substr($lines[0], 41);
615 }
616
617 return ($id, $desc);
618}
619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700620$chk_signoff = 0 if ($file);
621
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700622my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800623my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700624my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700625my @fixed_inserted = ();
626my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700627my $fixlinenr = -1;
628
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800629my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700630for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800631 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700632 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800633 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700634 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800635 } elsif ($filename eq '-') {
636 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700637 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800638 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700639 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700640 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800641 if ($filename eq '-') {
642 $vname = 'Your patch';
643 } else {
644 $vname = $filename;
645 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800646 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700647 chomp;
648 push(@rawlines, $_);
649 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800650 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800651 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700652 $exit = 1;
653 }
654 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800655 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700656 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700657 @fixed_inserted = ();
658 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700659 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700660}
661
662exit($exit);
663
664sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700665 my ($root) = @_;
666
667 my @tree_check = (
668 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
669 "README", "Documentation", "arch", "include", "drivers",
670 "fs", "init", "ipc", "kernel", "lib", "scripts",
671 );
672
673 foreach my $check (@tree_check) {
674 if (! -e $root . '/' . $check) {
675 return 0;
676 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700677 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700678 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700679}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700680
Joe Perches20112472011-07-25 17:13:23 -0700681sub parse_email {
682 my ($formatted_email) = @_;
683
684 my $name = "";
685 my $address = "";
686 my $comment = "";
687
688 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
689 $name = $1;
690 $address = $2;
691 $comment = $3 if defined $3;
692 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
693 $address = $1;
694 $comment = $2 if defined $2;
695 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
696 $address = $1;
697 $comment = $2 if defined $2;
698 $formatted_email =~ s/$address.*$//;
699 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700700 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700701 $name =~ s/^\"|\"$//g;
702 # If there's a name left after stripping spaces and
703 # leading quotes, and the address doesn't have both
704 # leading and trailing angle brackets, the address
705 # is invalid. ie:
706 # "joe smith joe@smith.com" bad
707 # "joe smith <joe@smith.com" bad
708 if ($name ne "" && $address !~ /^<[^>]+>$/) {
709 $name = "";
710 $address = "";
711 $comment = "";
712 }
713 }
714
Joe Perches3705ce52013-07-03 15:05:31 -0700715 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700716 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700717 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700718 $address =~ s/^\<|\>$//g;
719
720 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
721 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
722 $name = "\"$name\"";
723 }
724
725 return ($name, $address, $comment);
726}
727
728sub format_email {
729 my ($name, $address) = @_;
730
731 my $formatted_email;
732
Joe Perches3705ce52013-07-03 15:05:31 -0700733 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700734 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700735 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700736
737 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
738 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
739 $name = "\"$name\"";
740 }
741
742 if ("$name" eq "") {
743 $formatted_email = "$address";
744 } else {
745 $formatted_email = "$name <$address>";
746 }
747
748 return $formatted_email;
749}
750
Joe Perchesd311cd42014-08-06 16:10:57 -0700751sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700752 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700753
Joe Perchesbd474ca2014-08-06 16:11:10 -0700754 foreach my $path (split(/:/, $ENV{PATH})) {
755 if (-e "$path/$bin") {
756 return "$path/$bin";
757 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700758 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700759
Joe Perchesbd474ca2014-08-06 16:11:10 -0700760 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700761}
762
Joe Perches000d1cc12011-07-25 17:13:25 -0700763sub which_conf {
764 my ($conf) = @_;
765
766 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
767 if (-e "$path/$conf") {
768 return "$path/$conf";
769 }
770 }
771
772 return "";
773}
774
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700775sub expand_tabs {
776 my ($str) = @_;
777
778 my $res = '';
779 my $n = 0;
780 for my $c (split(//, $str)) {
781 if ($c eq "\t") {
782 $res .= ' ';
783 $n++;
784 for (; ($n % 8) != 0; $n++) {
785 $res .= ' ';
786 }
787 next;
788 }
789 $res .= $c;
790 $n++;
791 }
792
793 return $res;
794}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700795sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700796 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700797 return $res;
798}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700799
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700800sub line_stats {
801 my ($line) = @_;
802
803 # Drop the diff line leader and expand tabs
804 $line =~ s/^.//;
805 $line = expand_tabs($line);
806
807 # Pick the indent from the front of the line.
808 my ($white) = ($line =~ /^(\s*)/);
809
810 return (length($line), length($white));
811}
812
Andy Whitcroft773647a2008-03-28 14:15:58 -0700813my $sanitise_quote = '';
814
815sub sanitise_line_reset {
816 my ($in_comment) = @_;
817
818 if ($in_comment) {
819 $sanitise_quote = '*/';
820 } else {
821 $sanitise_quote = '';
822 }
823}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700824sub sanitise_line {
825 my ($line) = @_;
826
827 my $res = '';
828 my $l = '';
829
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800830 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700831 my $off = 0;
832 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700833
Andy Whitcroft773647a2008-03-28 14:15:58 -0700834 # Always copy over the diff marker.
835 $res = substr($line, 0, 1);
836
837 for ($off = 1; $off < length($line); $off++) {
838 $c = substr($line, $off, 1);
839
840 # Comments we are wacking completly including the begin
841 # and end, all to $;.
842 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
843 $sanitise_quote = '*/';
844
845 substr($res, $off, 2, "$;$;");
846 $off++;
847 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800848 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700849 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700850 $sanitise_quote = '';
851 substr($res, $off, 2, "$;$;");
852 $off++;
853 next;
854 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700855 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
856 $sanitise_quote = '//';
857
858 substr($res, $off, 2, $sanitise_quote);
859 $off++;
860 next;
861 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700862
863 # A \ in a string means ignore the next character.
864 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
865 $c eq "\\") {
866 substr($res, $off, 2, 'XX');
867 $off++;
868 next;
869 }
870 # Regular quotes.
871 if ($c eq "'" || $c eq '"') {
872 if ($sanitise_quote eq '') {
873 $sanitise_quote = $c;
874
875 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700876 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700877 } elsif ($sanitise_quote eq $c) {
878 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700879 }
880 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700881
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800882 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700883 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
884 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700885 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
886 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700887 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
888 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700889 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700890 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700891 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800892 }
893
Daniel Walker113f04a2009-09-21 17:04:35 -0700894 if ($sanitise_quote eq '//') {
895 $sanitise_quote = '';
896 }
897
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800898 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700899 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800900 my $clean = 'X' x length($1);
901 $res =~ s@\<.*\>@<$clean>@;
902
903 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700904 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700906 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800907 }
908
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700909 return $res;
910}
911
Joe Perchesa6962d72013-04-29 16:18:13 -0700912sub get_quoted_string {
913 my ($line, $rawline) = @_;
914
915 return "" if ($line !~ m/(\"[X]+\")/g);
916 return substr($rawline, $-[0], $+[0] - $-[0]);
917}
918
Andy Whitcroft8905a672007-11-28 16:21:06 -0800919sub ctx_statement_block {
920 my ($linenr, $remain, $off) = @_;
921 my $line = $linenr - 1;
922 my $blk = '';
923 my $soff = $off;
924 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800926
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800927 my $loff = 0;
928
Andy Whitcroft8905a672007-11-28 16:21:06 -0800929 my $type = '';
930 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800931 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800932 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800933 my $c;
934 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800935
936 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800937 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800938 @stack = (['', 0]) if ($#stack == -1);
939
Andy Whitcroft773647a2008-03-28 14:15:58 -0700940 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800941 # If we are about to drop off the end, pull in more
942 # context.
943 if ($off >= $len) {
944 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700945 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800946 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800947 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800948 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800949 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800950 $len = length($blk);
951 $line++;
952 last;
953 }
954 # Bail if there is no further context.
955 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800956 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800957 last;
958 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800959 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
960 $level++;
961 $type = '#';
962 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800963 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800964 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800965 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800966 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800967
Andy Whitcroft773647a2008-03-28 14:15:58 -0700968 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800969
970 # Handle nested #if/#else.
971 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
972 push(@stack, [ $type, $level ]);
973 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
974 ($type, $level) = @{$stack[$#stack - 1]};
975 } elsif ($remainder =~ /^#\s*endif\b/) {
976 ($type, $level) = @{pop(@stack)};
977 }
978
Andy Whitcroft8905a672007-11-28 16:21:06 -0800979 # Statement ends at the ';' or a close '}' at the
980 # outermost level.
981 if ($level == 0 && $c eq ';') {
982 last;
983 }
984
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800985 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700986 if ($level == 0 && $coff_set == 0 &&
987 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
988 $remainder =~ /^(else)(?:\s|{)/ &&
989 $remainder !~ /^else\s+if\b/) {
990 $coff = $off + length($1) - 1;
991 $coff_set = 1;
992 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
993 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800994 }
995
Andy Whitcroft8905a672007-11-28 16:21:06 -0800996 if (($type eq '' || $type eq '(') && $c eq '(') {
997 $level++;
998 $type = '(';
999 }
1000 if ($type eq '(' && $c eq ')') {
1001 $level--;
1002 $type = ($level != 0)? '(' : '';
1003
1004 if ($level == 0 && $coff < $soff) {
1005 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001006 $coff_set = 1;
1007 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001008 }
1009 }
1010 if (($type eq '' || $type eq '{') && $c eq '{') {
1011 $level++;
1012 $type = '{';
1013 }
1014 if ($type eq '{' && $c eq '}') {
1015 $level--;
1016 $type = ($level != 0)? '{' : '';
1017
1018 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001019 if (substr($blk, $off + 1, 1) eq ';') {
1020 $off++;
1021 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001022 last;
1023 }
1024 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001025 # Preprocessor commands end at the newline unless escaped.
1026 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1027 $level--;
1028 $type = '';
1029 $off++;
1030 last;
1031 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001032 $off++;
1033 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001034 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001035 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001036 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001037 $line++;
1038 $remain--;
1039 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001040
1041 my $statement = substr($blk, $soff, $off - $soff + 1);
1042 my $condition = substr($blk, $soff, $coff - $soff + 1);
1043
1044 #warn "STATEMENT<$statement>\n";
1045 #warn "CONDITION<$condition>\n";
1046
Andy Whitcroft773647a2008-03-28 14:15:58 -07001047 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001048
1049 return ($statement, $condition,
1050 $line, $remain + 1, $off - $loff + 1, $level);
1051}
1052
Andy Whitcroftcf655042008-03-04 14:28:20 -08001053sub statement_lines {
1054 my ($stmt) = @_;
1055
1056 # Strip the diff line prefixes and rip blank lines at start and end.
1057 $stmt =~ s/(^|\n)./$1/g;
1058 $stmt =~ s/^\s*//;
1059 $stmt =~ s/\s*$//;
1060
1061 my @stmt_lines = ($stmt =~ /\n/g);
1062
1063 return $#stmt_lines + 2;
1064}
1065
1066sub statement_rawlines {
1067 my ($stmt) = @_;
1068
1069 my @stmt_lines = ($stmt =~ /\n/g);
1070
1071 return $#stmt_lines + 2;
1072}
1073
1074sub statement_block_size {
1075 my ($stmt) = @_;
1076
1077 $stmt =~ s/(^|\n)./$1/g;
1078 $stmt =~ s/^\s*{//;
1079 $stmt =~ s/}\s*$//;
1080 $stmt =~ s/^\s*//;
1081 $stmt =~ s/\s*$//;
1082
1083 my @stmt_lines = ($stmt =~ /\n/g);
1084 my @stmt_statements = ($stmt =~ /;/g);
1085
1086 my $stmt_lines = $#stmt_lines + 2;
1087 my $stmt_statements = $#stmt_statements + 1;
1088
1089 if ($stmt_lines > $stmt_statements) {
1090 return $stmt_lines;
1091 } else {
1092 return $stmt_statements;
1093 }
1094}
1095
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001096sub ctx_statement_full {
1097 my ($linenr, $remain, $off) = @_;
1098 my ($statement, $condition, $level);
1099
1100 my (@chunks);
1101
Andy Whitcroftcf655042008-03-04 14:28:20 -08001102 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001103 ($statement, $condition, $linenr, $remain, $off, $level) =
1104 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001105 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001106 push(@chunks, [ $condition, $statement ]);
1107 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1108 return ($level, $linenr, @chunks);
1109 }
1110
1111 # Pull in the following conditional/block pairs and see if they
1112 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001113 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001114 ($statement, $condition, $linenr, $remain, $off, $level) =
1115 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001116 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001117 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001118 #print "C: push\n";
1119 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001120 }
1121
1122 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001123}
1124
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001125sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001126 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001127 my $line;
1128 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001129 my $blk = '';
1130 my @o;
1131 my @c;
1132 my @res = ();
1133
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001134 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001135 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001136 for ($line = $start; $remain > 0; $line++) {
1137 next if ($rawlines[$line] =~ /^-/);
1138 $remain--;
1139
1140 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001141
1142 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001143 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001144 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001145 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001146 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001147 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001148 $level = pop(@stack);
1149 }
1150
Andy Whitcroft01464f32010-10-26 14:23:19 -07001151 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001152 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1153 if ($off > 0) {
1154 $off--;
1155 next;
1156 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001157
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001158 if ($c eq $close && $level > 0) {
1159 $level--;
1160 last if ($level == 0);
1161 } elsif ($c eq $open) {
1162 $level++;
1163 }
1164 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001165
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001166 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001167 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001168 }
1169
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001170 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001171 }
1172
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001173 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001174}
1175sub ctx_block_outer {
1176 my ($linenr, $remain) = @_;
1177
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001178 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1179 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001180}
1181sub ctx_block {
1182 my ($linenr, $remain) = @_;
1183
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001184 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1185 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001186}
1187sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001188 my ($linenr, $remain, $off) = @_;
1189
1190 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1191 return @r;
1192}
1193sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001194 my ($linenr, $remain) = @_;
1195
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001196 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001197}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001198sub ctx_statement_level {
1199 my ($linenr, $remain, $off) = @_;
1200
1201 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1202}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001203
1204sub ctx_locate_comment {
1205 my ($first_line, $end_line) = @_;
1206
1207 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001208 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001209 return $current_comment if (defined $current_comment);
1210
1211 # Look through the context and try and figure out if there is a
1212 # comment.
1213 my $in_comment = 0;
1214 $current_comment = '';
1215 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001216 my $line = $rawlines[$linenr - 1];
1217 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001218 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1219 $in_comment = 1;
1220 }
1221 if ($line =~ m@/\*@) {
1222 $in_comment = 1;
1223 }
1224 if (!$in_comment && $current_comment ne '') {
1225 $current_comment = '';
1226 }
1227 $current_comment .= $line . "\n" if ($in_comment);
1228 if ($line =~ m@\*/@) {
1229 $in_comment = 0;
1230 }
1231 }
1232
1233 chomp($current_comment);
1234 return($current_comment);
1235}
1236sub ctx_has_comment {
1237 my ($first_line, $end_line) = @_;
1238 my $cmt = ctx_locate_comment($first_line, $end_line);
1239
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001240 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001241 ##print "CMMT: $cmt\n";
1242
1243 return ($cmt ne '');
1244}
1245
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001246sub raw_line {
1247 my ($linenr, $cnt) = @_;
1248
1249 my $offset = $linenr - 1;
1250 $cnt++;
1251
1252 my $line;
1253 while ($cnt) {
1254 $line = $rawlines[$offset++];
1255 next if (defined($line) && $line =~ /^-/);
1256 $cnt--;
1257 }
1258
1259 return $line;
1260}
1261
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001262sub cat_vet {
1263 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001264 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001265
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001266 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001267 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1268 $res .= $1;
1269 if ($2 ne '') {
1270 $coded = sprintf("^%c", unpack('C', $2) + 64);
1271 $res .= $coded;
1272 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001273 }
1274 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001275
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001276 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001277}
1278
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001279my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001280my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001281my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001282my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001283
1284sub annotate_reset {
1285 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001286 $av_pending = '_';
1287 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001288 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001289}
1290
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001291sub annotate_values {
1292 my ($stream, $type) = @_;
1293
1294 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001295 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001296 my $cur = $stream;
1297
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001298 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001299
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001300 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001301 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001302 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001303 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001305 print "WS($1)\n" if ($dbg_values > 1);
1306 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001307 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001308 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001309 }
1310
Florian Micklerc023e4732011-01-12 16:59:58 -08001311 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001312 print "CAST($1)\n" if ($dbg_values > 1);
1313 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001314 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001315
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001316 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001318 $type = 'T';
1319
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001320 } elsif ($cur =~ /^($Modifier)\s*/) {
1321 print "MODIFIER($1)\n" if ($dbg_values > 1);
1322 $type = 'T';
1323
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001324 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001325 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001327 push(@av_paren_type, $type);
1328 if ($2 ne '') {
1329 $av_pending = 'N';
1330 }
1331 $type = 'E';
1332
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001333 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001334 print "UNDEF($1)\n" if ($dbg_values > 1);
1335 $av_preprocessor = 1;
1336 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001337
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001338 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001339 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001340 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001341
1342 push(@av_paren_type, $type);
1343 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001344 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001345
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001346 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001347 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1348 $av_preprocessor = 1;
1349
1350 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1351
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001352 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001353
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001354 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001355 print "PRE_END($1)\n" if ($dbg_values > 1);
1356
1357 $av_preprocessor = 1;
1358
1359 # Assume all arms of the conditional end as this
1360 # one does, and continue as if the #endif was not here.
1361 pop(@av_paren_type);
1362 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001363 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001364
1365 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001366 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001367
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001368 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1369 print "ATTR($1)\n" if ($dbg_values > 1);
1370 $av_pending = $type;
1371 $type = 'N';
1372
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001373 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001375 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001376 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001377 }
1378 $type = 'N';
1379
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001380 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001381 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001382 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001383 $type = 'N';
1384
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001385 } elsif ($cur =~/^(case)/o) {
1386 print "CASE($1)\n" if ($dbg_values > 1);
1387 $av_pend_colon = 'C';
1388 $type = 'N';
1389
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001390 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001391 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001392 $type = 'N';
1393
1394 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001395 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001396 push(@av_paren_type, $av_pending);
1397 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001398 $type = 'N';
1399
1400 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001401 my $new_type = pop(@av_paren_type);
1402 if ($new_type ne '_') {
1403 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 print "PAREN('$1') -> $type\n"
1405 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001406 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001408 }
1409
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001410 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001412 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001413 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001414
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001415 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1416 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001417 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001418 } elsif ($type eq 'E') {
1419 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001420 }
1421 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1422 $type = 'V';
1423
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001424 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001425 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001426 $type = 'V';
1427
1428 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001429 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001430 $type = 'N';
1431
Andy Whitcroftcf655042008-03-04 14:28:20 -08001432 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001433 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001434 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001435 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001436
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001437 } elsif ($cur =~/^(,)/) {
1438 print "COMMA($1)\n" if ($dbg_values > 1);
1439 $type = 'C';
1440
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001441 } elsif ($cur =~ /^(\?)/o) {
1442 print "QUESTION($1)\n" if ($dbg_values > 1);
1443 $type = 'N';
1444
1445 } elsif ($cur =~ /^(:)/o) {
1446 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1447
1448 substr($var, length($res), 1, $av_pend_colon);
1449 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1450 $type = 'E';
1451 } else {
1452 $type = 'N';
1453 }
1454 $av_pend_colon = 'O';
1455
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001456 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001457 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458 $type = 'N';
1459
Andy Whitcroft0d413862008-10-15 22:02:16 -07001460 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001461 my $variant;
1462
1463 print "OPV($1)\n" if ($dbg_values > 1);
1464 if ($type eq 'V') {
1465 $variant = 'B';
1466 } else {
1467 $variant = 'U';
1468 }
1469
1470 substr($var, length($res), 1, $variant);
1471 $type = 'N';
1472
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001473 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001474 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001475 if ($1 ne '++' && $1 ne '--') {
1476 $type = 'N';
1477 }
1478
1479 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001480 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001481 }
1482 if (defined $1) {
1483 $cur = substr($cur, length($1));
1484 $res .= $type x length($1);
1485 }
1486 }
1487
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001488 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001489}
1490
Andy Whitcroft8905a672007-11-28 16:21:06 -08001491sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001492 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001493 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001494 ^(?:
1495 $Modifier|
1496 $Storage|
1497 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001498 DEFINE_\S+
1499 )$|
1500 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001501 goto|
1502 return|
1503 case|
1504 else|
1505 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001506 do|
1507 \#|
1508 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001509 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001510 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001511 )}x;
1512 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1513 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001514 # Check for modifiers.
1515 $possible =~ s/\s*$Storage\s*//g;
1516 $possible =~ s/\s*$Sparse\s*//g;
1517 if ($possible =~ /^\s*$/) {
1518
1519 } elsif ($possible =~ /\s/) {
1520 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001521 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001522 if ($modifier !~ $notPermitted) {
1523 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1524 push(@modifierList, $modifier);
1525 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001526 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001527
1528 } else {
1529 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1530 push(@typeList, $possible);
1531 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001532 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001533 } else {
1534 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001535 }
1536}
1537
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001538my $prefix = '';
1539
Joe Perches000d1cc12011-07-25 17:13:25 -07001540sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001541 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001542
Joe Perchescbec18a2014-04-03 14:49:19 -07001543 return defined $use_type{$type} if (scalar keys %use_type > 0);
1544
1545 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001546}
1547
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001548sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001549 my ($level, $type, $msg) = @_;
1550
1551 if (!show_type($type) ||
1552 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001553 return 0;
1554 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001555 my $line;
1556 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001557 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001558 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001559 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001560 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001561 $line = (split('\n', $line))[0] . "\n" if ($terse);
1562
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001563 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001564
1565 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001566}
Joe Perchescbec18a2014-04-03 14:49:19 -07001567
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001568sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001569 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001570}
Joe Perches000d1cc12011-07-25 17:13:25 -07001571
Joe Perchesd752fcc2014-08-06 16:11:05 -07001572sub fixup_current_range {
1573 my ($lineRef, $offset, $length) = @_;
1574
1575 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1576 my $o = $1;
1577 my $l = $2;
1578 my $no = $o + $offset;
1579 my $nl = $l + $length;
1580 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1581 }
1582}
1583
1584sub fix_inserted_deleted_lines {
1585 my ($linesRef, $insertedRef, $deletedRef) = @_;
1586
1587 my $range_last_linenr = 0;
1588 my $delta_offset = 0;
1589
1590 my $old_linenr = 0;
1591 my $new_linenr = 0;
1592
1593 my $next_insert = 0;
1594 my $next_delete = 0;
1595
1596 my @lines = ();
1597
1598 my $inserted = @{$insertedRef}[$next_insert++];
1599 my $deleted = @{$deletedRef}[$next_delete++];
1600
1601 foreach my $old_line (@{$linesRef}) {
1602 my $save_line = 1;
1603 my $line = $old_line; #don't modify the array
1604 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1605 $delta_offset = 0;
1606 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1607 $range_last_linenr = $new_linenr;
1608 fixup_current_range(\$line, $delta_offset, 0);
1609 }
1610
1611 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1612 $deleted = @{$deletedRef}[$next_delete++];
1613 $save_line = 0;
1614 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1615 }
1616
1617 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1618 push(@lines, ${$inserted}{'LINE'});
1619 $inserted = @{$insertedRef}[$next_insert++];
1620 $new_linenr++;
1621 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1622 }
1623
1624 if ($save_line) {
1625 push(@lines, $line);
1626 $new_linenr++;
1627 }
1628
1629 $old_linenr++;
1630 }
1631
1632 return @lines;
1633}
1634
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001635sub fix_insert_line {
1636 my ($linenr, $line) = @_;
1637
1638 my $inserted = {
1639 LINENR => $linenr,
1640 LINE => $line,
1641 };
1642 push(@fixed_inserted, $inserted);
1643}
1644
1645sub fix_delete_line {
1646 my ($linenr, $line) = @_;
1647
1648 my $deleted = {
1649 LINENR => $linenr,
1650 LINE => $line,
1651 };
1652
1653 push(@fixed_deleted, $deleted);
1654}
1655
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001656sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001657 my ($type, $msg) = @_;
1658
1659 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001660 our $clean = 0;
1661 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001662 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001663 }
Joe Perches3705ce52013-07-03 15:05:31 -07001664 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001665}
1666sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001667 my ($type, $msg) = @_;
1668
1669 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001670 our $clean = 0;
1671 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001672 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001673 }
Joe Perches3705ce52013-07-03 15:05:31 -07001674 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001675}
1676sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001677 my ($type, $msg) = @_;
1678
1679 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001680 our $clean = 0;
1681 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001682 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001683 }
Joe Perches3705ce52013-07-03 15:05:31 -07001684 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001685}
1686
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001687sub check_absolute_file {
1688 my ($absolute, $herecurr) = @_;
1689 my $file = $absolute;
1690
1691 ##print "absolute<$absolute>\n";
1692
1693 # See if any suffix of this path is a path within the tree.
1694 while ($file =~ s@^[^/]*/@@) {
1695 if (-f "$root/$file") {
1696 ##print "file<$file>\n";
1697 last;
1698 }
1699 }
1700 if (! -f _) {
1701 return 0;
1702 }
1703
1704 # It is, so see if the prefix is acceptable.
1705 my $prefix = $absolute;
1706 substr($prefix, -length($file)) = '';
1707
1708 ##print "prefix<$prefix>\n";
1709 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001710 WARN("USE_RELATIVE_PATH",
1711 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001712 }
1713}
1714
Joe Perches3705ce52013-07-03 15:05:31 -07001715sub trim {
1716 my ($string) = @_;
1717
Joe Perchesb34c6482013-09-11 14:24:01 -07001718 $string =~ s/^\s+|\s+$//g;
1719
1720 return $string;
1721}
1722
1723sub ltrim {
1724 my ($string) = @_;
1725
1726 $string =~ s/^\s+//;
1727
1728 return $string;
1729}
1730
1731sub rtrim {
1732 my ($string) = @_;
1733
1734 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001735
1736 return $string;
1737}
1738
Joe Perches52ea8502013-11-12 15:10:09 -08001739sub string_find_replace {
1740 my ($string, $find, $replace) = @_;
1741
1742 $string =~ s/$find/$replace/g;
1743
1744 return $string;
1745}
1746
Joe Perches3705ce52013-07-03 15:05:31 -07001747sub tabify {
1748 my ($leading) = @_;
1749
1750 my $source_indent = 8;
1751 my $max_spaces_before_tab = $source_indent - 1;
1752 my $spaces_to_tab = " " x $source_indent;
1753
1754 #convert leading spaces to tabs
1755 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1756 #Remove spaces before a tab
1757 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1758
1759 return "$leading";
1760}
1761
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001762sub pos_last_openparen {
1763 my ($line) = @_;
1764
1765 my $pos = 0;
1766
1767 my $opens = $line =~ tr/\(/\(/;
1768 my $closes = $line =~ tr/\)/\)/;
1769
1770 my $last_openparen = 0;
1771
1772 if (($opens == 0) || ($closes >= $opens)) {
1773 return -1;
1774 }
1775
1776 my $len = length($line);
1777
1778 for ($pos = 0; $pos < $len; $pos++) {
1779 my $string = substr($line, $pos);
1780 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1781 $pos += length($1) - 1;
1782 } elsif (substr($line, $pos, 1) eq '(') {
1783 $last_openparen = $pos;
1784 } elsif (index($string, '(') == -1) {
1785 last;
1786 }
1787 }
1788
Joe Perches91cb5192014-04-03 14:49:32 -07001789 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001790}
1791
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001792sub process {
1793 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001794
1795 my $linenr=0;
1796 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001797 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001798 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001799 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001800
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001801 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001802 my $indent;
1803 my $previndent=0;
1804 my $stashindent=0;
1805
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001806 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001807 my $signoff = 0;
1808 my $is_patch = 0;
1809
Joe Perches29ee1b02014-08-06 16:10:35 -07001810 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001811 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001812 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001813 my $non_utf8_charset = 0;
1814
Joe Perches365dd4e2014-08-06 16:10:42 -07001815 my $last_blank_line = 0;
1816
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001817 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001818 our $cnt_lines = 0;
1819 our $cnt_error = 0;
1820 our $cnt_warn = 0;
1821 our $cnt_chk = 0;
1822
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001823 # Trace the real file/line as we go.
1824 my $realfile = '';
1825 my $realline = 0;
1826 my $realcnt = 0;
1827 my $here = '';
1828 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001829 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001830 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001831 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001832
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001833 my $prev_values = 'E';
1834
1835 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001836 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001837 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001838 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001839 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001840
Joe Perches7e51f192013-09-11 14:23:57 -07001841 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001842
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001843 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001844 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001845 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001846 my @setup_docs = ();
1847 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001848
Joe Perchesd8b07712013-11-12 15:10:06 -08001849 my $camelcase_file_seeded = 0;
1850
Andy Whitcroft773647a2008-03-28 14:15:58 -07001851 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001852 my $line;
1853 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001854 $linenr++;
1855 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001856
Joe Perches3705ce52013-07-03 15:05:31 -07001857 push(@fixed, $rawline) if ($fix);
1858
Andy Whitcroft773647a2008-03-28 14:15:58 -07001859 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001860 $setup_docs = 0;
1861 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1862 $setup_docs = 1;
1863 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001864 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001865 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001866 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1867 $realline=$1-1;
1868 if (defined $2) {
1869 $realcnt=$3+1;
1870 } else {
1871 $realcnt=1+1;
1872 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001873 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001874
1875 # Guestimate if this is a continuing comment. Run
1876 # the context looking for a comment "edge". If this
1877 # edge is a close comment then we must be in a comment
1878 # at context start.
1879 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001880 my $cnt = $realcnt;
1881 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1882 next if (defined $rawlines[$ln - 1] &&
1883 $rawlines[$ln - 1] =~ /^-/);
1884 $cnt--;
1885 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001886 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001887 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1888 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1889 ($edge) = $1;
1890 last;
1891 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001892 }
1893 if (defined $edge && $edge eq '*/') {
1894 $in_comment = 1;
1895 }
1896
1897 # Guestimate if this is a continuing comment. If this
1898 # is the start of a diff block and this line starts
1899 # ' *' then it is very likely a comment.
1900 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001901 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001902 {
1903 $in_comment = 1;
1904 }
1905
1906 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1907 sanitise_line_reset($in_comment);
1908
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001909 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001910 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001911 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001912 $line = sanitise_line($rawline);
1913 }
1914 push(@lines, $line);
1915
1916 if ($realcnt > 1) {
1917 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1918 } else {
1919 $realcnt = 0;
1920 }
1921
1922 #print "==>$rawline\n";
1923 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001924
1925 if ($setup_docs && $line =~ /^\+/) {
1926 push(@setup_docs, $line);
1927 }
1928 }
1929
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001930 $prefix = '';
1931
Andy Whitcroft773647a2008-03-28 14:15:58 -07001932 $realcnt = 0;
1933 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001934 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001935 foreach my $line (@lines) {
1936 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001937 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001938 my $sline = $line; #copy of $line
1939 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001940
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001941 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001942
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001943#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001945 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001946 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001947 $realline=$1-1;
1948 if (defined $2) {
1949 $realcnt=$3+1;
1950 } else {
1951 $realcnt=1+1;
1952 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001953 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001954 $prev_values = 'E';
1955
Andy Whitcroft773647a2008-03-28 14:15:58 -07001956 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001957 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001958 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001959 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001960 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001961
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001962# track the line number as we move through the hunk, note that
1963# new versions of GNU diff omit the leading space on completely
1964# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001965 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001966 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001967 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001968
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001969 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001970 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001971
1972 # Track the previous line.
1973 ($prevline, $stashline) = ($stashline, $line);
1974 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001975 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1976
Andy Whitcroft773647a2008-03-28 14:15:58 -07001977 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001978
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001979 } elsif ($realcnt == 1) {
1980 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001981 }
1982
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001983 my $hunk_line = ($realcnt != 0);
1984
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001985#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001986 $prefix = "$filename:$realline: " if ($emacs && $file);
1987 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1988
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001989 $here = "#$linenr: " if (!$file);
1990 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001991
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001992 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001993 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001994 if ($line =~ /^diff --git.*?(\S+)$/) {
1995 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08001996 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08001997 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001998 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001999 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002000 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002001 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002002 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002003
2004 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002005 if (!$file && $tree && $p1_prefix ne '' &&
2006 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002007 WARN("PATCH_PREFIX",
2008 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002009 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002010
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002011 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002012 ERROR("MODIFIED_INCLUDE_ASM",
2013 "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 -07002014 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002015 $found_file = 1;
2016 }
2017
2018 if ($found_file) {
2019 if ($realfile =~ m@^(drivers/net/|net/)@) {
2020 $check = 1;
2021 } else {
2022 $check = $check_orig;
2023 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002024 next;
2025 }
2026
Randy Dunlap389834b2007-06-08 13:47:03 -07002027 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002028
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002029 my $hereline = "$here\n$rawline\n";
2030 my $herecurr = "$here\n$rawline\n";
2031 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002032
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002033 $cnt_lines++ if ($realcnt != 0);
2034
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002035# Check for incorrect file permissions
2036 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2037 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002038 if ($realfile !~ m@scripts/@ &&
2039 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002040 ERROR("EXECUTE_PERMISSIONS",
2041 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002042 }
2043 }
2044
Joe Perches20112472011-07-25 17:13:23 -07002045# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002046 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002047 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002048 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002049 }
2050
2051# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002052 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002053 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002054 my $space_before = $1;
2055 my $sign_off = $2;
2056 my $space_after = $3;
2057 my $email = $4;
2058 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2059
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002060 if ($sign_off !~ /$signature_tags/) {
2061 WARN("BAD_SIGN_OFF",
2062 "Non-standard signature: $sign_off\n" . $herecurr);
2063 }
Joe Perches20112472011-07-25 17:13:23 -07002064 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002065 if (WARN("BAD_SIGN_OFF",
2066 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2067 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002068 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002069 "$ucfirst_sign_off $email";
2070 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002071 }
Joe Perches20112472011-07-25 17:13:23 -07002072 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002073 if (WARN("BAD_SIGN_OFF",
2074 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2075 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002076 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002077 "$ucfirst_sign_off $email";
2078 }
2079
Joe Perches20112472011-07-25 17:13:23 -07002080 }
2081 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002082 if (WARN("BAD_SIGN_OFF",
2083 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2084 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002085 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002086 "$ucfirst_sign_off $email";
2087 }
Joe Perches20112472011-07-25 17:13:23 -07002088 }
2089
2090 my ($email_name, $email_address, $comment) = parse_email($email);
2091 my $suggested_email = format_email(($email_name, $email_address));
2092 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002093 ERROR("BAD_SIGN_OFF",
2094 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002095 } else {
2096 my $dequoted = $suggested_email;
2097 $dequoted =~ s/^"//;
2098 $dequoted =~ s/" </ </;
2099 # Don't force email to have quotes
2100 # Allow just an angle bracketed address
2101 if ("$dequoted$comment" ne $email &&
2102 "<$email_address>$comment" ne $email &&
2103 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002104 WARN("BAD_SIGN_OFF",
2105 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002106 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002107 }
Joe Perches7e51f192013-09-11 14:23:57 -07002108
2109# Check for duplicate signatures
2110 my $sig_nospace = $line;
2111 $sig_nospace =~ s/\s//g;
2112 $sig_nospace = lc($sig_nospace);
2113 if (defined $signatures{$sig_nospace}) {
2114 WARN("BAD_SIGN_OFF",
2115 "Duplicate signature\n" . $herecurr);
2116 } else {
2117 $signatures{$sig_nospace} = 1;
2118 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002119 }
2120
Joe Perches9b3189eb2014-06-04 16:12:10 -07002121# Check for old stable address
2122 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2123 ERROR("STABLE_ADDRESS",
2124 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2125 }
2126
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002127# Check for unwanted Gerrit info
2128 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2129 ERROR("GERRIT_CHANGE_ID",
2130 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2131 }
2132
Joe Perchesd311cd42014-08-06 16:10:57 -07002133# Check for improperly formed commit descriptions
2134 if ($in_commit_log &&
2135 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2136 $line !~ /\b[Cc]ommit [0-9a-f]{12,16} \("/) {
2137 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2138 my $init_char = $1;
2139 my $orig_commit = lc($2);
2140 my $id = '01234567890ab';
2141 my $desc = 'commit description';
2142 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2143 ERROR("GIT_COMMIT_ID",
2144 "Please use 12 to 16 chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2145 }
2146
Joe Perches13f19372014-08-06 16:10:59 -07002147# Check for added, moved or deleted files
2148 if (!$reported_maintainer_file && !$in_commit_log &&
2149 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2150 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2151 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2152 (defined($1) || defined($2))))) {
2153 $reported_maintainer_file = 1;
2154 WARN("FILE_PATH_CHANGES",
2155 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2156 }
2157
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002158# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002159 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002160 ERROR("CORRUPTED_PATCH",
2161 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002162 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002163 }
2164
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002165# Check for absolute kernel paths.
2166 if ($tree) {
2167 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2168 my $file = $1;
2169
2170 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2171 check_absolute_file($1, $herecurr)) {
2172 #
2173 } else {
2174 check_absolute_file($file, $herecurr);
2175 }
2176 }
2177 }
2178
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002179# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2180 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002181 $rawline !~ m/^$UTF8*$/) {
2182 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2183
2184 my $blank = copy_spacing($rawline);
2185 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2186 my $hereptr = "$hereline$ptr\n";
2187
Joe Perches34d99212011-07-25 17:13:26 -07002188 CHK("INVALID_UTF8",
2189 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002190 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002191
Joe Perches15662b32011-10-31 17:13:12 -07002192# Check if it's the start of a commit log
2193# (not a header line and we haven't seen the patch filename)
2194 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002195 !($rawline =~ /^\s+\S/ ||
2196 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002197 $in_header_lines = 0;
2198 $in_commit_log = 1;
2199 }
2200
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002201# Check if there is UTF-8 in a commit log when a mail header has explicitly
2202# declined it, i.e defined some charset where it is missing.
2203 if ($in_header_lines &&
2204 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2205 $1 !~ /utf-8/i) {
2206 $non_utf8_charset = 1;
2207 }
2208
2209 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002210 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002211 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002212 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2213 }
2214
Andy Whitcroft306708542008-10-15 22:02:28 -07002215# ignore non-hunk lines and lines being removed
2216 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002217
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002218#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002219 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002220 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002221 if (ERROR("DOS_LINE_ENDINGS",
2222 "DOS line endings\n" . $herevet) &&
2223 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002224 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002225 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002226 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2227 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002228 if (ERROR("TRAILING_WHITESPACE",
2229 "trailing whitespace\n" . $herevet) &&
2230 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002231 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002232 }
2233
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002234 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002235 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002236
Josh Triplett4783f892013-11-12 15:10:12 -08002237# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002238 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002239 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2240 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002241 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2242 my $msg_type = \&ERROR;
2243 $msg_type = \&CHK if ($file);
2244 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002245 "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 -08002246 }
2247
Andi Kleen33549572010-05-24 14:33:29 -07002248# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002249# Only applies when adding the entry originally, after that we do not have
2250# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002251 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002252 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002253 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002254 my $cnt = $realcnt;
2255 my $ln = $linenr + 1;
2256 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002257 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002258 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002259 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002260 $f = $lines[$ln - 1];
2261 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2262 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002263
2264 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002265 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002266
Joe Perches8d73e0e2014-08-06 16:10:46 -07002267 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002268 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002269 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002270 $length = -1;
2271 }
2272
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002273 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002274 $f =~ s/#.*//;
2275 $f =~ s/^\s+//;
2276 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002277 if ($f =~ /^\s*config\s/) {
2278 $is_end = 1;
2279 last;
2280 }
Andi Kleen33549572010-05-24 14:33:29 -07002281 $length++;
2282 }
Joe Perches000d1cc12011-07-25 17:13:25 -07002283 WARN("CONFIG_DESCRIPTION",
Andy Whitcrofta1385802012-01-10 15:10:03 -08002284 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
2285 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002286 }
2287
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002288# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2289 if ($realfile =~ /Kconfig/ &&
2290 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2291 WARN("CONFIG_EXPERIMENTAL",
2292 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2293 }
2294
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002295 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2296 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2297 my $flag = $1;
2298 my $replacement = {
2299 'EXTRA_AFLAGS' => 'asflags-y',
2300 'EXTRA_CFLAGS' => 'ccflags-y',
2301 'EXTRA_CPPFLAGS' => 'cppflags-y',
2302 'EXTRA_LDFLAGS' => 'ldflags-y',
2303 };
2304
2305 WARN("DEPRECATED_VARIABLE",
2306 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2307 }
2308
Rob Herringbff5da42014-01-23 15:54:51 -08002309# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002310 if (defined $root &&
2311 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2312 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2313
Rob Herringbff5da42014-01-23 15:54:51 -08002314 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2315
Florian Vaussardcc933192014-04-03 14:49:27 -07002316 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2317 my $vp_file = $dt_path . "vendor-prefixes.txt";
2318
Rob Herringbff5da42014-01-23 15:54:51 -08002319 foreach my $compat (@compats) {
2320 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002321 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2322 my $compat3 = $compat;
2323 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2324 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002325 if ( $? >> 8 ) {
2326 WARN("UNDOCUMENTED_DT_STRING",
2327 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2328 }
2329
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002330 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2331 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002332 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002333 if ( $? >> 8 ) {
2334 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002335 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002336 }
2337 }
2338 }
2339
Andy Whitcroft5368df202008-10-15 22:02:27 -07002340# check we are in a valid source file if not then ignore this hunk
2341 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
2342
Joe Perches6cd7f382012-12-17 16:01:54 -08002343#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002344 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002345 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002346 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002347 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002348 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002349 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002350 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002351 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002352 }
2353
Josh Triplettca56dc02012-03-23 15:02:21 -07002354# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002355# to grep for the string. Make exceptions when the previous string ends in a
2356# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2357# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002358 if ($line =~ /^\+\s*"/ &&
2359 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002360 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002361 WARN("SPLIT_STRING",
2362 "quoted string split across lines\n" . $hereprev);
2363 }
2364
Dan Carpenterece96592014-08-06 16:11:29 -07002365# check for missing a space in a string concatination
2366 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2367 WARN('MISSING_SPACE',
2368 "break quoted strings at a space character\n" . $hereprev);
2369 }
2370
Joe Perches5e79d962010-03-05 13:43:55 -08002371# check for spaces before a quoted newline
2372 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002373 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2374 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2375 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002376 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002377 }
2378
Joe Perches5e79d962010-03-05 13:43:55 -08002379 }
2380
Andy Whitcroft8905a672007-11-28 16:21:06 -08002381# check for adding lines without a newline.
2382 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002383 WARN("MISSING_EOF_NEWLINE",
2384 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002385 }
2386
Mike Frysinger42e41c52009-09-21 17:04:40 -07002387# Blackfin: use hi/lo macros
2388 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2389 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2390 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002391 ERROR("LO_MACRO",
2392 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002393 }
2394 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2395 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002396 ERROR("HI_MACRO",
2397 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002398 }
2399 }
2400
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002401# check we are in a valid source file C or perl if not then ignore this hunk
2402 next if ($realfile !~ /\.(h|c|pl)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002403
2404# at the beginning of a line any tabs must come first and anything
2405# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002406 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2407 $rawline =~ /^\+\s* \s*/) {
2408 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002409 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002410 if (ERROR("CODE_INDENT",
2411 "code indent should use tabs where possible\n" . $herevet) &&
2412 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002413 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002414 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002415 }
2416
Alberto Panizzo08e44362010-03-05 13:43:54 -08002417# check for space before tabs.
2418 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002420 if (WARN("SPACE_BEFORE_TAB",
2421 "please, no space before tabs\n" . $herevet) &&
2422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002423 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002424 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002425 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002426 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002427 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002428 }
2429
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002430# check for && or || at the start of a line
2431 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2432 CHK("LOGICAL_CONTINUATIONS",
2433 "Logical continuations should be on the previous line\n" . $hereprev);
2434 }
2435
2436# check multi-line statement indentation matches previous line
2437 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002438 $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 -07002439 $prevline =~ /^\+(\t*)(.*)$/;
2440 my $oldindent = $1;
2441 my $rest = $2;
2442
2443 my $pos = pos_last_openparen($rest);
2444 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002445 $line =~ /^(\+| )([ \t]*)/;
2446 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002447
2448 my $goodtabindent = $oldindent .
2449 "\t" x ($pos / 8) .
2450 " " x ($pos % 8);
2451 my $goodspaceindent = $oldindent . " " x $pos;
2452
2453 if ($newindent ne $goodtabindent &&
2454 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002455
2456 if (CHK("PARENTHESIS_ALIGNMENT",
2457 "Alignment should match open parenthesis\n" . $hereprev) &&
2458 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002459 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002460 s/^\+[ \t]*/\+$goodtabindent/;
2461 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002462 }
2463 }
2464 }
2465
Joe Perches308cc8d2014-08-06 16:11:27 -07002466 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002467 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002468 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002469 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002470 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002471 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002472 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002473 }
2474
Joe Perches05880602012-10-04 17:13:35 -07002475 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002476 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002477 $rawline =~ /^\+[ \t]*\*/ &&
2478 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002479 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2480 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2481 }
2482
2483 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002484 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2485 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002486 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002487 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2488 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2489 "networking block comments start with * on subsequent lines\n" . $hereprev);
2490 }
2491
2492 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002493 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2494 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2495 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2496 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002497 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2498 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2499 }
2500
Joe Perches7f619192014-08-06 16:10:39 -07002501# check for missing blank lines after struct/union declarations
2502# with exceptions for various attributes and macros
2503 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2504 $line =~ /^\+/ &&
2505 !($line =~ /^\+\s*$/ ||
2506 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2507 $line =~ /^\+\s*MODULE_/i ||
2508 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2509 $line =~ /^\+[a-z_]*init/ ||
2510 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2511 $line =~ /^\+\s*DECLARE/ ||
2512 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002513 if (CHK("LINE_SPACING",
2514 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2515 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002516 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002517 }
Joe Perches7f619192014-08-06 16:10:39 -07002518 }
2519
Joe Perches365dd4e2014-08-06 16:10:42 -07002520# check for multiple consecutive blank lines
2521 if ($prevline =~ /^[\+ ]\s*$/ &&
2522 $line =~ /^\+\s*$/ &&
2523 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002524 if (CHK("LINE_SPACING",
2525 "Please don't use multiple blank lines\n" . $hereprev) &&
2526 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002527 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002528 }
2529
Joe Perches365dd4e2014-08-06 16:10:42 -07002530 $last_blank_line = $linenr;
2531 }
2532
Joe Perches3b617e32014-04-03 14:49:28 -07002533# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002534 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2535 # actual declarations
2536 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002537 # function pointer declarations
2538 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002539 # foo bar; where foo is some local typedef or #define
2540 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2541 # known declaration macros
2542 $prevline =~ /^\+\s+$declaration_macros/) &&
2543 # for "else if" which can look like "$Ident $Ident"
2544 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2545 # other possible extensions of declaration lines
2546 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2547 # not starting a section or a macro "\" extended line
2548 $prevline =~ /(?:\{\s*|\\)$/) &&
2549 # looks like a declaration
2550 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002551 # function pointer declarations
2552 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002553 # foo bar; where foo is some local typedef or #define
2554 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2555 # known declaration macros
2556 $sline =~ /^\+\s+$declaration_macros/ ||
2557 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002558 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002559 # start or end of block or continuation of declaration
2560 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2561 # bitfield continuation
2562 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2563 # other possible extensions of declaration lines
2564 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2565 # indentation of previous and current line are the same
2566 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002567 if (WARN("LINE_SPACING",
2568 "Missing a blank line after declarations\n" . $hereprev) &&
2569 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002570 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002571 }
Joe Perches3b617e32014-04-03 14:49:28 -07002572 }
2573
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002574# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002575# Exceptions:
2576# 1) within comments
2577# 2) indented preprocessor commands
2578# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002579 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002580 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002581 if (WARN("LEADING_SPACE",
2582 "please, no spaces at the start of a line\n" . $herevet) &&
2583 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002584 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002585 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002586 }
2587
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002588# check we are in a valid C source file if not then ignore this hunk
2589 next if ($realfile !~ /\.(h|c)$/);
2590
Joe Perches032a4c02014-08-06 16:10:29 -07002591# check indentation of any line with a bare else
2592# if the previous line is a break or return and is indented 1 tab more...
2593 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2594 my $tabs = length($1) + 1;
2595 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2596 WARN("UNNECESSARY_ELSE",
2597 "else is not generally useful after a break or return\n" . $hereprev);
2598 }
2599 }
2600
Joe Perchesc00df192014-08-06 16:11:01 -07002601# check indentation of a line with a break;
2602# if the previous line is a goto or return and is indented the same # of tabs
2603 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2604 my $tabs = $1;
2605 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2606 WARN("UNNECESSARY_BREAK",
2607 "break is not useful after a goto or return\n" . $hereprev);
2608 }
2609 }
2610
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002611# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2612 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2613 WARN("CONFIG_EXPERIMENTAL",
2614 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2615 }
2616
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002617# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002618 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002619 WARN("CVS_KEYWORD",
2620 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002621 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002622
Mike Frysinger42e41c52009-09-21 17:04:40 -07002623# Blackfin: don't use __builtin_bfin_[cs]sync
2624 if ($line =~ /__builtin_bfin_csync/) {
2625 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002626 ERROR("CSYNC",
2627 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002628 }
2629 if ($line =~ /__builtin_bfin_ssync/) {
2630 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002631 ERROR("SSYNC",
2632 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002633 }
2634
Joe Perches56e77d72013-02-21 16:44:14 -08002635# check for old HOTPLUG __dev<foo> section markings
2636 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2637 WARN("HOTPLUG_SECTION",
2638 "Using $1 is unnecessary\n" . $herecurr);
2639 }
2640
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002641# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002642 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2643 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002644#print "LINE<$line>\n";
2645 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002646 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002647 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002648 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002649 $stat =~ s/\n./\n /g;
2650 $cond =~ s/\n./\n /g;
2651
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002652#print "linenr<$linenr> <$stat>\n";
2653 # If this statement has no statement boundaries within
2654 # it there is no point in retrying a statement scan
2655 # until we hit end of it.
2656 my $frag = $stat; $frag =~ s/;+\s*$//;
2657 if ($frag !~ /(?:{|;)/) {
2658#print "skip<$line_nr_next>\n";
2659 $suppress_statement = $line_nr_next;
2660 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002661
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002662 # Find the real next line.
2663 $realline_next = $line_nr_next;
2664 if (defined $realline_next &&
2665 (!defined $lines[$realline_next - 1] ||
2666 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2667 $realline_next++;
2668 }
2669
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002670 my $s = $stat;
2671 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002672
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002673 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002674 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002675
2676 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002677 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002678
Andy Whitcroft463f2862009-09-21 17:04:34 -07002679 } elsif ($s =~ /^.\s*else\b/s) {
2680
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002681 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002682 } 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 -07002683 my $type = $1;
2684 $type =~ s/\s+/ /g;
2685 possible($type, "A:" . $s);
2686
Andy Whitcroft8905a672007-11-28 16:21:06 -08002687 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002688 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002689 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002690 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002691
2692 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002693 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002694 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002695 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002696
2697 # Check for any sort of function declaration.
2698 # int foo(something bar, other baz);
2699 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002700 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 -08002701 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002702
Andy Whitcroftcf655042008-03-04 14:28:20 -08002703 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002704 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002705 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002706
Andy Whitcroft8905a672007-11-28 16:21:06 -08002707 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002708 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002709
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002710 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002711 }
2712 }
2713 }
2714
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002715 }
2716
Andy Whitcroft653d4872007-06-23 17:16:34 -07002717#
2718# Checks which may be anchored in the context.
2719#
2720
2721# Check for switch () and associated case and default
2722# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002723 if ($line=~/\bswitch\s*\(.*\)/) {
2724 my $err = '';
2725 my $sep = '';
2726 my @ctx = ctx_block_outer($linenr, $realcnt);
2727 shift(@ctx);
2728 for my $ctx (@ctx) {
2729 my ($clen, $cindent) = line_stats($ctx);
2730 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2731 $indent != $cindent) {
2732 $err .= "$sep$ctx\n";
2733 $sep = '';
2734 } else {
2735 $sep = "[...]\n";
2736 }
2737 }
2738 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002739 ERROR("SWITCH_CASE_INDENT_LEVEL",
2740 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002741 }
2742 }
2743
2744# if/while/etc brace do not go on next line, unless defining a do while loop,
2745# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002746 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 -07002747 my $pre_ctx = "$1$2";
2748
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002749 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002750
2751 if ($line =~ /^\+\t{6,}/) {
2752 WARN("DEEP_INDENTATION",
2753 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2754 }
2755
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002756 my $ctx_cnt = $realcnt - $#ctx - 1;
2757 my $ctx = join("\n", @ctx);
2758
Andy Whitcroft548596d2008-07-23 21:29:01 -07002759 my $ctx_ln = $linenr;
2760 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002761
Andy Whitcroft548596d2008-07-23 21:29:01 -07002762 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2763 defined $lines[$ctx_ln - 1] &&
2764 $lines[$ctx_ln - 1] =~ /^-/)) {
2765 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2766 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002767 $ctx_ln++;
2768 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002769
Andy Whitcroft53210162008-07-23 21:29:03 -07002770 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2771 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002772
Joe Perchesd752fcc2014-08-06 16:11:05 -07002773 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002774 ERROR("OPEN_BRACE",
2775 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002776 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002777 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002778 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2779 $ctx =~ /\)\s*\;\s*$/ &&
2780 defined $lines[$ctx_ln - 1])
2781 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002782 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2783 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002784 WARN("TRAILING_SEMICOLON",
2785 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002786 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002787 }
2788 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002789 }
2790
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002791# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002792 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 -08002793 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2794 ctx_statement_block($linenr, $realcnt, 0)
2795 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002796 my ($s, $c) = ($stat, $cond);
2797
2798 substr($s, 0, length($c), '');
2799
2800 # Make sure we remove the line prefixes as we have
2801 # none on the first line, and are going to readd them
2802 # where necessary.
2803 $s =~ s/\n./\n/gs;
2804
2805 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002806 my @newlines = ($c =~ /\n/gs);
2807 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002808
2809 # We want to check the first line inside the block
2810 # starting at the end of the conditional, so remove:
2811 # 1) any blank line termination
2812 # 2) any opening brace { on end of the line
2813 # 3) any do (...) {
2814 my $continuation = 0;
2815 my $check = 0;
2816 $s =~ s/^.*\bdo\b//;
2817 $s =~ s/^\s*{//;
2818 if ($s =~ s/^\s*\\//) {
2819 $continuation = 1;
2820 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002821 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002822 $check = 1;
2823 $cond_lines++;
2824 }
2825
2826 # Also ignore a loop construct at the end of a
2827 # preprocessor statement.
2828 if (($prevline =~ /^.\s*#\s*define\s/ ||
2829 $prevline =~ /\\\s*$/) && $continuation == 0) {
2830 $check = 0;
2831 }
2832
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002833 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002834 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002835 while ($cond_ptr != $cond_lines) {
2836 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002837
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002838 # If we see an #else/#elif then the code
2839 # is not linear.
2840 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2841 $check = 0;
2842 }
2843
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002844 # Ignore:
2845 # 1) blank lines, they should be at 0,
2846 # 2) preprocessor lines, and
2847 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002848 if ($continuation ||
2849 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002850 $s =~ /^\s*#\s*?/ ||
2851 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002852 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002853 if ($s =~ s/^.*?\n//) {
2854 $cond_lines++;
2855 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002856 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002857 }
2858
2859 my (undef, $sindent) = line_stats("+" . $s);
2860 my $stat_real = raw_line($linenr, $cond_lines);
2861
2862 # Check if either of these lines are modified, else
2863 # this is not this patch's fault.
2864 if (!defined($stat_real) ||
2865 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2866 $check = 0;
2867 }
2868 if (defined($stat_real) && $cond_lines > 1) {
2869 $stat_real = "[...]\n$stat_real";
2870 }
2871
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002872 #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 -07002873
2874 if ($check && (($sindent % 8) != 0 ||
2875 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002876 WARN("SUSPECT_CODE_INDENT",
2877 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002878 }
2879 }
2880
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002881 # Track the 'values' across context and added lines.
2882 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002883 my ($curr_values, $curr_vars) =
2884 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002885 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002886 if ($dbg_values) {
2887 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002888 print "$linenr > .$outline\n";
2889 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002890 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002891 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002892 $prev_values = substr($curr_values, -1);
2893
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002894#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002895 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002896
Andy Whitcroft653d4872007-06-23 17:16:34 -07002897# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002898 if ($dbg_type) {
2899 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002900 ERROR("TEST_TYPE",
2901 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002902 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002903 ERROR("TEST_NOT_TYPE",
2904 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002905 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002906 next;
2907 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002908# TEST: allow direct testing of the attribute matcher.
2909 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002910 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002911 ERROR("TEST_ATTR",
2912 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002913 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002914 ERROR("TEST_NOT_ATTR",
2915 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002916 }
2917 next;
2918 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002919
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002920# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002921 if ($line =~ /^.\s*{/ &&
2922 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002923 if (ERROR("OPEN_BRACE",
2924 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002925 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2926 fix_delete_line($fixlinenr - 1, $prevrawline);
2927 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002928 my $fixedline = $prevrawline;
2929 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002930 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002931 $fixedline = $line;
2932 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002933 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002934 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002935 }
2936
Andy Whitcroft653d4872007-06-23 17:16:34 -07002937#
2938# Checks which are anchored on the added line.
2939#
2940
2941# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002942 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002943 my $path = $1;
2944 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002945 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002946 "malformed #include filename\n" . $herecurr);
2947 }
2948 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2949 ERROR("UAPI_INCLUDE",
2950 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002951 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002952 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002953
2954# no C99 // comments
2955 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002956 if (ERROR("C99_COMMENTS",
2957 "do not use C99 // comments\n" . $herecurr) &&
2958 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002959 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07002960 if ($line =~ /\/\/(.*)$/) {
2961 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07002962 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07002963 }
2964 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002965 }
2966 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002967 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002968 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002969
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002970# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2971# the whole statement.
2972#print "APW <$lines[$realline_next - 1]>\n";
2973 if (defined $realline_next &&
2974 exists $lines[$realline_next - 1] &&
2975 !defined $suppress_export{$realline_next} &&
2976 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2977 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002978 # Handle definitions which produce identifiers with
2979 # a prefix:
2980 # XXX(foo);
2981 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002982 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002983 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002984 $name =~ /^${Ident}_$2/) {
2985#print "FOO C name<$name>\n";
2986 $suppress_export{$realline_next} = 1;
2987
2988 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002989 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07002990 ^.DEFINE_$Ident\(\Q$name\E\)|
2991 ^.DECLARE_$Ident\(\Q$name\E\)|
2992 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002993 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2994 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07002995 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002996#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2997 $suppress_export{$realline_next} = 2;
2998 } else {
2999 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003000 }
3001 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003002 if (!defined $suppress_export{$linenr} &&
3003 $prevline =~ /^.\s*$/ &&
3004 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3005 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3006#print "FOO B <$lines[$linenr - 1]>\n";
3007 $suppress_export{$linenr} = 2;
3008 }
3009 if (defined $suppress_export{$linenr} &&
3010 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003011 WARN("EXPORT_SYMBOL",
3012 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003013 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003014
Joe Eloff5150bda2010-08-09 17:21:00 -07003015# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003016 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3017 if (ERROR("GLOBAL_INITIALISERS",
3018 "do not initialise globals to 0 or NULL\n" .
3019 $herecurr) &&
3020 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003021 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003022 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003023 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003024# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003025 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3026 if (ERROR("INITIALISED_STATIC",
3027 "do not initialise statics to 0 or NULL\n" .
3028 $herecurr) &&
3029 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003030 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003031 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003032 }
3033
Joe Perches18130872014-08-06 16:11:22 -07003034# check for misordered declarations of char/short/int/long with signed/unsigned
3035 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3036 my $tmp = trim($1);
3037 WARN("MISORDERED_TYPE",
3038 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3039 }
3040
Joe Perchescb710ec2010-10-26 14:23:20 -07003041# check for static const char * arrays.
3042 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003043 WARN("STATIC_CONST_CHAR_ARRAY",
3044 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003045 $herecurr);
3046 }
3047
3048# check for static char foo[] = "bar" declarations.
3049 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003050 WARN("STATIC_CONST_CHAR_ARRAY",
3051 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003052 $herecurr);
3053 }
3054
Joe Perches9b0fa602014-04-03 14:49:18 -07003055# check for non-global char *foo[] = {"bar", ...} declarations.
3056 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3057 WARN("STATIC_CONST_CHAR_ARRAY",
3058 "char * array declaration might be better as static const\n" .
3059 $herecurr);
3060 }
3061
Joe Perchesb36190c2014-01-27 17:07:18 -08003062# check for function declarations without arguments like "int foo()"
3063 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3064 if (ERROR("FUNCTION_WITHOUT_ARGS",
3065 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3066 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003067 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003068 }
3069 }
3070
Joe Perches92e112f2013-12-13 11:36:22 -07003071# check for uses of DEFINE_PCI_DEVICE_TABLE
3072 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3073 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3074 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3075 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003076 $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 -07003077 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003078 }
3079
Andy Whitcroft653d4872007-06-23 17:16:34 -07003080# check for new typedefs, only function parameters and sparse annotations
3081# make sense.
3082 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003083 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003084 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003085 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003086 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003087 WARN("NEW_TYPEDEFS",
3088 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003089 }
3090
3091# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003092 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003093 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3094 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003095 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003096
Andy Whitcroft65863862009-01-06 14:41:21 -08003097 # Should start with a space.
3098 $to =~ s/^(\S)/ $1/;
3099 # Should not end with a space.
3100 $to =~ s/\s+$//;
3101 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003102 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003103 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003104
Joe Perches3705ce52013-07-03 15:05:31 -07003105## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003106 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003107 if (ERROR("POINTER_LOCATION",
3108 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3109 $fix) {
3110 my $sub_from = $ident;
3111 my $sub_to = $ident;
3112 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003113 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003114 s@\Q$sub_from\E@$sub_to@;
3115 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003116 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003117 }
3118 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3119 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003120 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003121
Andy Whitcroft65863862009-01-06 14:41:21 -08003122 # Should start with a space.
3123 $to =~ s/^(\S)/ $1/;
3124 # Should not end with a space.
3125 $to =~ s/\s+$//;
3126 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003127 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003128 }
3129 # Modifiers should have spaces.
3130 $to =~ s/(\b$Modifier$)/$1 /;
3131
Joe Perches3705ce52013-07-03 15:05:31 -07003132## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003133 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003134 if (ERROR("POINTER_LOCATION",
3135 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3136 $fix) {
3137
3138 my $sub_from = $match;
3139 my $sub_to = $match;
3140 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003141 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003142 s@\Q$sub_from\E@$sub_to@;
3143 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003144 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003145 }
3146
3147# # no BUG() or BUG_ON()
3148# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3149# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3150# print "$herecurr";
3151# $clean = 0;
3152# }
3153
Andy Whitcroft8905a672007-11-28 16:21:06 -08003154 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003155 WARN("LINUX_VERSION_CODE",
3156 "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 -08003157 }
3158
Joe Perches17441222011-06-15 15:08:17 -07003159# check for uses of printk_ratelimit
3160 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003161 WARN("PRINTK_RATELIMITED",
3162"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003163 }
3164
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003165# printk should use KERN_* levels. Note that follow on printk's on the
3166# same line do not need a level, so we use the current block context
3167# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003168# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003169# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003170 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003171 my $ok = 0;
3172 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3173 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003174 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003175 # with "\n" ignore it, else it is to blame
3176 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3177 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3178 $ok = 1;
3179 }
3180 last;
3181 }
3182 }
3183 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003184 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3185 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003187 }
3188
Joe Perches243f3802012-05-31 16:26:09 -07003189 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3190 my $orig = $1;
3191 my $level = lc($orig);
3192 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003193 my $level2 = $level;
3194 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003195 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003196 "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 -07003197 }
3198
3199 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003200 if (WARN("PREFER_PR_LEVEL",
3201 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3202 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003203 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003204 s/\bpr_warning\b/pr_warn/;
3205 }
Joe Perches243f3802012-05-31 16:26:09 -07003206 }
3207
Joe Perchesdc139312013-02-21 16:44:13 -08003208 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3209 my $orig = $1;
3210 my $level = lc($orig);
3211 $level = "warn" if ($level eq "warning");
3212 $level = "dbg" if ($level eq "debug");
3213 WARN("PREFER_DEV_LEVEL",
3214 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3215 }
3216
Andy Whitcroft653d4872007-06-23 17:16:34 -07003217# function brace can't be on same line, except for #defines of do while,
3218# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003219 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003220 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003221 if (ERROR("OPEN_BRACE",
3222 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3223 $fix) {
3224 fix_delete_line($fixlinenr, $rawline);
3225 my $fixed_line = $rawline;
3226 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3227 my $line1 = $1;
3228 my $line2 = $2;
3229 fix_insert_line($fixlinenr, ltrim($line1));
3230 fix_insert_line($fixlinenr, "\+{");
3231 if ($line2 !~ /^\s*$/) {
3232 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3233 }
3234 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003235 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003236
Andy Whitcroft8905a672007-11-28 16:21:06 -08003237# open braces for enum, union and struct go on the same line.
3238 if ($line =~ /^.\s*{/ &&
3239 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003240 if (ERROR("OPEN_BRACE",
3241 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3242 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3243 fix_delete_line($fixlinenr - 1, $prevrawline);
3244 fix_delete_line($fixlinenr, $rawline);
3245 my $fixedline = rtrim($prevrawline) . " {";
3246 fix_insert_line($fixlinenr, $fixedline);
3247 $fixedline = $rawline;
3248 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3249 if ($fixedline !~ /^\+\s*$/) {
3250 fix_insert_line($fixlinenr, $fixedline);
3251 }
3252 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003253 }
3254
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003255# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003256 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3257 if (WARN("SPACING",
3258 "missing space after $1 definition\n" . $herecurr) &&
3259 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003260 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003261 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3262 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003263 }
3264
Joe Perches31070b52014-01-23 15:54:49 -08003265# Function pointer declarations
3266# check spacing between type, funcptr, and args
3267# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003268 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003269 my $declare = $1;
3270 my $pre_pointer_space = $2;
3271 my $post_pointer_space = $3;
3272 my $funcname = $4;
3273 my $post_funcname_space = $5;
3274 my $pre_args_space = $6;
3275
Joe Perches91f72e92014-04-03 14:49:12 -07003276# the $Declare variable will capture all spaces after the type
3277# so check it for a missing trailing missing space but pointer return types
3278# don't need a space so don't warn for those.
3279 my $post_declare_space = "";
3280 if ($declare =~ /(\s+)$/) {
3281 $post_declare_space = $1;
3282 $declare = rtrim($declare);
3283 }
3284 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003285 WARN("SPACING",
3286 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003287 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003288 }
3289
3290# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003291# This test is not currently implemented because these declarations are
3292# equivalent to
3293# int foo(int bar, ...)
3294# and this is form shouldn't/doesn't generate a checkpatch warning.
3295#
3296# elsif ($declare =~ /\s{2,}$/) {
3297# WARN("SPACING",
3298# "Multiple spaces after return type\n" . $herecurr);
3299# }
Joe Perches31070b52014-01-23 15:54:49 -08003300
3301# unnecessary space "type ( *funcptr)(args...)"
3302 if (defined $pre_pointer_space &&
3303 $pre_pointer_space =~ /^\s/) {
3304 WARN("SPACING",
3305 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3306 }
3307
3308# unnecessary space "type (* funcptr)(args...)"
3309 if (defined $post_pointer_space &&
3310 $post_pointer_space =~ /^\s/) {
3311 WARN("SPACING",
3312 "Unnecessary space before function pointer name\n" . $herecurr);
3313 }
3314
3315# unnecessary space "type (*funcptr )(args...)"
3316 if (defined $post_funcname_space &&
3317 $post_funcname_space =~ /^\s/) {
3318 WARN("SPACING",
3319 "Unnecessary space after function pointer name\n" . $herecurr);
3320 }
3321
3322# unnecessary space "type (*funcptr) (args...)"
3323 if (defined $pre_args_space &&
3324 $pre_args_space =~ /^\s/) {
3325 WARN("SPACING",
3326 "Unnecessary space before function pointer arguments\n" . $herecurr);
3327 }
3328
3329 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003330 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003331 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003332 }
3333 }
3334
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003335# check for spacing round square brackets; allowed:
3336# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003337# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3338# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003339 while ($line =~ /(.*?\s)\[/g) {
3340 my ($where, $prefix) = ($-[1], $1);
3341 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003342 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003343 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003344 if (ERROR("BRACKET_SPACE",
3345 "space prohibited before open square bracket '['\n" . $herecurr) &&
3346 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003347 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003348 s/^(\+.*?)\s+\[/$1\[/;
3349 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003350 }
3351 }
3352
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003353# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003354 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003355 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003356 my $ctx_before = substr($line, 0, $-[1]);
3357 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003358
3359 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003360 if ($name =~ /^(?:
3361 if|for|while|switch|return|case|
3362 volatile|__volatile__|
3363 __attribute__|format|__extension__|
3364 asm|__asm__)$/x)
3365 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003366 # cpp #define statements have non-optional spaces, ie
3367 # if there is a space between the name and the open
3368 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003369 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003370
3371 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003372 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003373
3374 # If this whole things ends with a type its most
3375 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003376 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003377
3378 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003379 if (WARN("SPACING",
3380 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3381 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003382 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003383 s/\b$name\s+\(/$name\(/;
3384 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003385 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003386 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003387
Andy Whitcroft653d4872007-06-23 17:16:34 -07003388# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003389 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003390 my $fixed_line = "";
3391 my $line_fixed = 0;
3392
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003393 my $ops = qr{
3394 <<=|>>=|<=|>=|==|!=|
3395 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3396 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003397 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003398 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003399 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003400 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003401
3402## print("element count: <" . $#elements . ">\n");
3403## foreach my $el (@elements) {
3404## print("el: <$el>\n");
3405## }
3406
3407 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003408 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003409
Joe Perches3705ce52013-07-03 15:05:31 -07003410 foreach my $el (@elements) {
3411 push(@fix_elements, substr($rawline, $off, length($el)));
3412 $off += length($el);
3413 }
3414
3415 $off = 0;
3416
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003417 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003418 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003419
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003420 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003421
3422 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3423
3424## print("n: <$n> good: <$good>\n");
3425
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003426 $off += length($elements[$n]);
3427
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003428 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003429 my $ca = substr($opline, 0, $off);
3430 my $cc = '';
3431 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3432 $cc = substr($opline, $off + length($elements[$n + 1]));
3433 }
3434 my $cb = "$ca$;$cc";
3435
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003436 my $a = '';
3437 $a = 'V' if ($elements[$n] ne '');
3438 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003439 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003440 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3441 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003442 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003443
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003444 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003445
3446 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003447 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003448 $c = 'V' if ($elements[$n + 2] ne '');
3449 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003450 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003451 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3452 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003453 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003454 } else {
3455 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003456 }
3457
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003458 my $ctx = "${a}x${c}";
3459
3460 my $at = "(ctx:$ctx)";
3461
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003462 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003463 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003464
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003465 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003466 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003467
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003468 # Get the full operator variant.
3469 my $opv = $op . substr($curr_vars, $off, 1);
3470
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003471 # Ignore operators passed as parameters.
3472 if ($op_type ne 'V' &&
3473 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3474
Andy Whitcroftcf655042008-03-04 14:28:20 -08003475# # Ignore comments
3476# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003477
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003478 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003479 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003480 if ($ctx !~ /.x[WEBC]/ &&
3481 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003482 if (ERROR("SPACING",
3483 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003484 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003485 $line_fixed = 1;
3486 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003487 }
3488
3489 # // is a comment
3490 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003491
Joe Perchesb00e4812014-04-03 14:49:33 -07003492 # : when part of a bitfield
3493 } elsif ($opv eq ':B') {
3494 # skip the bitfield test for now
3495
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003496 # No spaces for:
3497 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003498 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003499 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003500 if (ERROR("SPACING",
3501 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003502 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003503 if (defined $fix_elements[$n + 2]) {
3504 $fix_elements[$n + 2] =~ s/^\s+//;
3505 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003506 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003507 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003508 }
3509
3510 # , must have a space on the right.
3511 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003512 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003513 if (ERROR("SPACING",
3514 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003515 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003516 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003517 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07003518 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003519 }
3520
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003521 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003522 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003523 #warn "'*' is part of type\n";
3524
3525 # unary operators should have a space before and
3526 # none after. May be left adjacent to another
3527 # unary operator, or a cast
3528 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003529 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003530 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003531 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003532 if (ERROR("SPACING",
3533 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003534 if ($n != $last_after + 2) {
3535 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3536 $line_fixed = 1;
3537 }
Joe Perches3705ce52013-07-03 15:05:31 -07003538 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003539 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003540 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003541 # A unary '*' may be const
3542
3543 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003544 if (ERROR("SPACING",
3545 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003546 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003547 if (defined $fix_elements[$n + 2]) {
3548 $fix_elements[$n + 2] =~ s/^\s+//;
3549 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003550 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003551 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003552 }
3553
3554 # unary ++ and unary -- are allowed no space on one side.
3555 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003556 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003557 if (ERROR("SPACING",
3558 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003559 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003560 $line_fixed = 1;
3561 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003562 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003563 if ($ctx =~ /Wx[BE]/ ||
3564 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003565 if (ERROR("SPACING",
3566 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003567 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003568 $line_fixed = 1;
3569 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003570 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003571 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003572 if (ERROR("SPACING",
3573 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003574 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003575 if (defined $fix_elements[$n + 2]) {
3576 $fix_elements[$n + 2] =~ s/^\s+//;
3577 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003578 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003579 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003580 }
3581
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003582 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003583 } elsif ($op eq '<<' or $op eq '>>' or
3584 $op eq '&' or $op eq '^' or $op eq '|' or
3585 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003586 $op eq '*' or $op eq '/' or
3587 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003588 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003589 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003590 if (ERROR("SPACING",
3591 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003592 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3593 if (defined $fix_elements[$n + 2]) {
3594 $fix_elements[$n + 2] =~ s/^\s+//;
3595 }
Joe Perches3705ce52013-07-03 15:05:31 -07003596 $line_fixed = 1;
3597 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003598 }
3599
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003600 # A colon needs no spaces before when it is
3601 # terminating a case value or a label.
3602 } elsif ($opv eq ':C' || $opv eq ':L') {
3603 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003604 if (ERROR("SPACING",
3605 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003606 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003607 $line_fixed = 1;
3608 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003609 }
3610
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003611 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003612 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003613 my $ok = 0;
3614
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003615 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003616 if (($op eq '<' &&
3617 $cc =~ /^\S+\@\S+>/) ||
3618 ($op eq '>' &&
3619 $ca =~ /<\S+\@\S+$/))
3620 {
3621 $ok = 1;
3622 }
3623
Joe Perches84731622013-11-12 15:10:05 -08003624 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003625 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003626 my $msg_type = \&ERROR;
3627 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3628
3629 if (&{$msg_type}("SPACING",
3630 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003631 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3632 if (defined $fix_elements[$n + 2]) {
3633 $fix_elements[$n + 2] =~ s/^\s+//;
3634 }
Joe Perches3705ce52013-07-03 15:05:31 -07003635 $line_fixed = 1;
3636 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003637 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003638 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003639 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003640
3641## print("n: <$n> GOOD: <$good>\n");
3642
3643 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003644 }
Joe Perches3705ce52013-07-03 15:05:31 -07003645
3646 if (($#elements % 2) == 0) {
3647 $fixed_line = $fixed_line . $fix_elements[$#elements];
3648 }
3649
Joe Perches194f66f2014-08-06 16:11:03 -07003650 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3651 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003652 }
3653
3654
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003655 }
3656
Joe Perches786b6322013-07-03 15:05:32 -07003657# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003658 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003659 if (WARN("SPACING",
3660 "space prohibited before semicolon\n" . $herecurr) &&
3661 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003662 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003663 s/^(\+.*\S)\s+;/$1;/;
3664 }
3665 }
3666
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003667# check for multiple assignments
3668 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003669 CHK("MULTIPLE_ASSIGNMENTS",
3670 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003671 }
3672
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003673## # check for multiple declarations, allowing for a function declaration
3674## # continuation.
3675## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3676## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3677##
3678## # Remove any bracketed sections to ensure we do not
3679## # falsly report the parameters of functions.
3680## my $ln = $line;
3681## while ($ln =~ s/\([^\(\)]*\)//g) {
3682## }
3683## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003684## WARN("MULTIPLE_DECLARATION",
3685## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003686## }
3687## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003688
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003689#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003690 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3691 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003692 if (ERROR("SPACING",
3693 "space required before the open brace '{'\n" . $herecurr) &&
3694 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003695 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003696 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003697 }
3698
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003699## # check for blank lines before declarations
3700## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3701## $prevrawline =~ /^.\s*$/) {
3702## WARN("SPACING",
3703## "No blank lines before declarations\n" . $hereprev);
3704## }
3705##
3706
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003707# closing brace should have a space following it when it has anything
3708# on the line
3709 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003710 if (ERROR("SPACING",
3711 "space required after that close brace '}'\n" . $herecurr) &&
3712 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003713 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003714 s/}((?!(?:,|;|\)))\S)/} $1/;
3715 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003716 }
3717
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003718# check spacing on square brackets
3719 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003720 if (ERROR("SPACING",
3721 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003723 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003724 s/\[\s+/\[/;
3725 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003726 }
3727 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003728 if (ERROR("SPACING",
3729 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3730 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003731 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003732 s/\s+\]/\]/;
3733 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003734 }
3735
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003736# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003737 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3738 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003739 if (ERROR("SPACING",
3740 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3741 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003742 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003743 s/\(\s+/\(/;
3744 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003745 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003746 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003747 $line !~ /for\s*\(.*;\s+\)/ &&
3748 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003749 if (ERROR("SPACING",
3750 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3751 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003752 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3753 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003754 s/\s+\)/\)/;
3755 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003756 }
3757
Joe Perchese2826fd2014-08-06 16:10:48 -07003758# check unnecessary parentheses around addressof/dereference single $Lvals
3759# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3760
3761 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3762 CHK("UNNECESSARY_PARENTHESES",
3763 "Unnecessary parentheses around $1\n" . $herecurr);
3764 }
3765
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003766#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003767 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003768 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003769 if (WARN("INDENTED_LABEL",
3770 "labels should not be indented\n" . $herecurr) &&
3771 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003772 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003773 s/^(.)\s+/$1/;
3774 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003775 }
3776
Joe Perches5b9553a2014-04-03 14:49:21 -07003777# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003778 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003779 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003780 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003781 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3782 my $value = $1;
3783 $value = deparenthesize($value);
3784 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3785 ERROR("RETURN_PARENTHESES",
3786 "return is not a function, parentheses are not required\n" . $herecurr);
3787 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003788 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003789 ERROR("SPACING",
3790 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003791 }
3792 }
Joe Perches507e5142013-11-12 15:10:13 -08003793
Joe Perchesb43ae212014-06-23 13:22:07 -07003794# unnecessary return in a void function
3795# at end-of-function, with the previous line a single leading tab, then return;
3796# and the line before that not a goto label target like "out:"
3797 if ($sline =~ /^[ \+]}\s*$/ &&
3798 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3799 $linenr >= 3 &&
3800 $lines[$linenr - 3] =~ /^[ +]/ &&
3801 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003802 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003803 "void function return statements are not generally useful\n" . $hereprev);
3804 }
Joe Perches9819cf22014-06-04 16:12:09 -07003805
Joe Perches189248d2014-01-23 15:54:47 -08003806# if statements using unnecessary parentheses - ie: if ((foo == bar))
3807 if ($^V && $^V ge 5.10.0 &&
3808 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3809 my $openparens = $1;
3810 my $count = $openparens =~ tr@\(@\(@;
3811 my $msg = "";
3812 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3813 my $comp = $4; #Not $1 because of $LvalOrFunc
3814 $msg = " - maybe == should be = ?" if ($comp eq "==");
3815 WARN("UNNECESSARY_PARENTHESES",
3816 "Unnecessary parentheses$msg\n" . $herecurr);
3817 }
3818 }
3819
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003820# Return of what appears to be an errno should normally be -'ve
3821 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3822 my $name = $1;
3823 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003824 WARN("USE_NEGATIVE_ERRNO",
3825 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003826 }
3827 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003828
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003829# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003830 if ($line =~ /\b(if|while|for|switch)\(/) {
3831 if (ERROR("SPACING",
3832 "space required before the open parenthesis '('\n" . $herecurr) &&
3833 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003834 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003835 s/\b(if|while|for|switch)\(/$1 \(/;
3836 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003837 }
3838
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003839# Check for illegal assignment in if conditional -- and check for trailing
3840# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003841 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003842 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3843 ctx_statement_block($linenr, $realcnt, 0)
3844 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003845 my ($stat_next) = ctx_statement_block($line_nr_next,
3846 $remain_next, $off_next);
3847 $stat_next =~ s/\n./\n /g;
3848 ##print "stat<$stat> stat_next<$stat_next>\n";
3849
3850 if ($stat_next =~ /^\s*while\b/) {
3851 # If the statement carries leading newlines,
3852 # then count those as offsets.
3853 my ($whitespace) =
3854 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3855 my $offset =
3856 statement_rawlines($whitespace) - 1;
3857
3858 $suppress_whiletrailers{$line_nr_next +
3859 $offset} = 1;
3860 }
3861 }
3862 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003863 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003864 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003865 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003866
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003867 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003868 ERROR("ASSIGN_IN_IF",
3869 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003870 }
3871
3872 # Find out what is on the end of the line after the
3873 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003874 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003875 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003876 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003877 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3878 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003879 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003880 # Find out how long the conditional actually is.
3881 my @newlines = ($c =~ /\n/gs);
3882 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003883 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003884
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003885 $stat_real = raw_line($linenr, $cond_lines)
3886 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003887 if (defined($stat_real) && $cond_lines > 1) {
3888 $stat_real = "[...]\n$stat_real";
3889 }
3890
Joe Perches000d1cc12011-07-25 17:13:25 -07003891 ERROR("TRAILING_STATEMENTS",
3892 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003893 }
3894 }
3895
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003896# Check for bitwise tests written as boolean
3897 if ($line =~ /
3898 (?:
3899 (?:\[|\(|\&\&|\|\|)
3900 \s*0[xX][0-9]+\s*
3901 (?:\&\&|\|\|)
3902 |
3903 (?:\&\&|\|\|)
3904 \s*0[xX][0-9]+\s*
3905 (?:\&\&|\|\||\)|\])
3906 )/x)
3907 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003908 WARN("HEXADECIMAL_BOOLEAN_TEST",
3909 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003910 }
3911
Andy Whitcroft8905a672007-11-28 16:21:06 -08003912# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003913 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3914 my $s = $1;
3915 $s =~ s/$;//g; # Remove any comments
3916 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917 ERROR("TRAILING_STATEMENTS",
3918 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003919 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003920 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003921# if should not continue a brace
3922 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003923 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07003924 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003925 $herecurr);
3926 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003927# case and default should not have general statements after them
3928 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3929 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003930 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003931 \s*return\s+
3932 )/xg)
3933 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003934 ERROR("TRAILING_STATEMENTS",
3935 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003936 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003937
3938 # Check for }<nl>else {, these must be at the same
3939 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07003940 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3941 $previndent == $indent) {
3942 if (ERROR("ELSE_AFTER_BRACE",
3943 "else should follow close brace '}'\n" . $hereprev) &&
3944 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3945 fix_delete_line($fixlinenr - 1, $prevrawline);
3946 fix_delete_line($fixlinenr, $rawline);
3947 my $fixedline = $prevrawline;
3948 $fixedline =~ s/}\s*$//;
3949 if ($fixedline !~ /^\+\s*$/) {
3950 fix_insert_line($fixlinenr, $fixedline);
3951 }
3952 $fixedline = $rawline;
3953 $fixedline =~ s/^(.\s*)else/$1} else/;
3954 fix_insert_line($fixlinenr, $fixedline);
3955 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003956 }
3957
Joe Perches8b8856f2014-08-06 16:11:14 -07003958 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3959 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003960 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3961
3962 # Find out what is on the end of the line after the
3963 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003964 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003965 $s =~ s/\n.*//g;
3966
3967 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07003968 if (ERROR("WHILE_AFTER_BRACE",
3969 "while should follow close brace '}'\n" . $hereprev) &&
3970 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3971 fix_delete_line($fixlinenr - 1, $prevrawline);
3972 fix_delete_line($fixlinenr, $rawline);
3973 my $fixedline = $prevrawline;
3974 my $trailing = $rawline;
3975 $trailing =~ s/^\+//;
3976 $trailing = trim($trailing);
3977 $fixedline =~ s/}\s*$/} $trailing/;
3978 fix_insert_line($fixlinenr, $fixedline);
3979 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003980 }
3981 }
3982
Joe Perches95e2c602013-07-03 15:05:20 -07003983#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003984 while ($line =~ m{($Constant|$Lval)}g) {
3985 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003986
3987#gcc binary extension
3988 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003989 if (WARN("GCC_BINARY_CONSTANT",
3990 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3991 $fix) {
3992 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07003993 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003994 s/\b$var\b/$hexval/;
3995 }
Joe Perches95e2c602013-07-03 15:05:20 -07003996 }
3997
3998#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07003999 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004000 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004001#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004002 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004003#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07004004 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004005 while ($var =~ m{($Ident)}g) {
4006 my $word = $1;
4007 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004008 if ($check) {
4009 seed_camelcase_includes();
4010 if (!$file && !$camelcase_file_seeded) {
4011 seed_camelcase_file($realfile);
4012 $camelcase_file_seeded = 1;
4013 }
4014 }
Joe Perches7e781f62013-09-11 14:23:55 -07004015 if (!defined $camelcase{$word}) {
4016 $camelcase{$word} = 1;
4017 CHK("CAMELCASE",
4018 "Avoid CamelCase: <$word>\n" . $herecurr);
4019 }
Joe Perches34456862013-07-03 15:05:34 -07004020 }
Joe Perches323c1262012-12-17 16:02:07 -08004021 }
4022 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004023
4024#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004025 if ($line =~ /\#\s*define.*\\\s+$/) {
4026 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4027 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4028 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004029 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004030 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004031 }
4032
Andy Whitcroft653d4872007-06-23 17:16:34 -07004033#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004034 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004035 my $file = "$1.h";
4036 my $checkfile = "include/linux/$file";
4037 if (-f "$root/$checkfile" &&
4038 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004039 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004040 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004041 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004042 CHK("ARCH_INCLUDE_LINUX",
4043 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004044 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004045 WARN("INCLUDE_LINUX",
4046 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004047 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004048 }
4049 }
4050
Andy Whitcroft653d4872007-06-23 17:16:34 -07004051# multi-statement macros should be enclosed in a do while loop, grab the
4052# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004053# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004054 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4055 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004056 my $ln = $linenr;
4057 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004058 my ($off, $dstat, $dcond, $rest);
4059 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004060 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004061 ctx_statement_block($linenr, $realcnt, 0);
4062 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004063 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004064 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004065
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004066 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004067 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004068 $dstat =~ s/\\\n.//g;
4069 $dstat =~ s/^\s*//s;
4070 $dstat =~ s/\s*$//s;
4071
4072 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004073 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4074 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004075 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004076 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004077 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004078
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004079 # Flatten any obvious string concatentation.
4080 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4081 $dstat =~ s/$Ident\s*("X*")/$1/)
4082 {
4083 }
4084
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004085 my $exceptions = qr{
4086 $Declare|
4087 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004088 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004089 DECLARE_PER_CPU|
4090 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004091 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004092 union|
4093 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004094 \.$Ident\s*=\s*|
4095 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004096 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004097 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004098 if ($dstat ne '' &&
4099 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4100 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004101 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004102 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004103 $dstat !~ /$exceptions/ &&
4104 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004105 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004106 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004107 $dstat !~ /^for\s*$Constant$/ && # for (...)
4108 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4109 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004110 $dstat !~ /^\({/ && # ({...
4111 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004112 {
4113 $ctx =~ s/\n*$//;
4114 my $herectx = $here . "\n";
4115 my $cnt = statement_rawlines($ctx);
4116
4117 for (my $n = 0; $n < $cnt; $n++) {
4118 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004119 }
4120
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004121 if ($dstat =~ /;/) {
4122 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4123 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4124 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004125 ERROR("COMPLEX_MACRO",
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004126 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004127 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004128 }
Joe Perches5023d342012-12-17 16:01:47 -08004129
Joe Perches481eb482012-12-17 16:01:56 -08004130# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004131
4132 } else {
4133 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004134 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4135 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004136 $line =~ /^\+.*\\$/) {
4137 WARN("LINE_CONTINUATIONS",
4138 "Avoid unnecessary line continuations\n" . $herecurr);
4139 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004140 }
4141
Joe Perchesb13edf72012-07-30 14:41:24 -07004142# do {} while (0) macro tests:
4143# single-statement macros do not need to be enclosed in do while (0) loop,
4144# macro should not end with a semicolon
4145 if ($^V && $^V ge 5.10.0 &&
4146 $realfile !~ m@/vmlinux.lds.h$@ &&
4147 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4148 my $ln = $linenr;
4149 my $cnt = $realcnt;
4150 my ($off, $dstat, $dcond, $rest);
4151 my $ctx = '';
4152 ($dstat, $dcond, $ln, $cnt, $off) =
4153 ctx_statement_block($linenr, $realcnt, 0);
4154 $ctx = $dstat;
4155
4156 $dstat =~ s/\\\n.//g;
4157
4158 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4159 my $stmts = $2;
4160 my $semis = $3;
4161
4162 $ctx =~ s/\n*$//;
4163 my $cnt = statement_rawlines($ctx);
4164 my $herectx = $here . "\n";
4165
4166 for (my $n = 0; $n < $cnt; $n++) {
4167 $herectx .= raw_line($linenr, $n) . "\n";
4168 }
4169
Joe Perchesac8e97f2012-08-21 16:15:53 -07004170 if (($stmts =~ tr/;/;/) == 1 &&
4171 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004172 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4173 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4174 }
4175 if (defined $semis && $semis ne "") {
4176 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4177 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4178 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004179 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4180 $ctx =~ s/\n*$//;
4181 my $cnt = statement_rawlines($ctx);
4182 my $herectx = $here . "\n";
4183
4184 for (my $n = 0; $n < $cnt; $n++) {
4185 $herectx .= raw_line($linenr, $n) . "\n";
4186 }
4187
4188 WARN("TRAILING_SEMICOLON",
4189 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004190 }
4191 }
4192
Mike Frysinger080ba922009-01-06 14:41:25 -08004193# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4194# all assignments may have only one of the following with an assignment:
4195# .
4196# ALIGN(...)
4197# VMLINUX_SYMBOL(...)
4198 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004199 WARN("MISSING_VMLINUX_SYMBOL",
4200 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004201 }
4202
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004203# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004204 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4205 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004206 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004207 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004208 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4209 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004210 my @allowed = ();
4211 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004212 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004213 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004214 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004215 for my $chunk (@chunks) {
4216 my ($cond, $block) = @{$chunk};
4217
Andy Whitcroft773647a2008-03-28 14:15:58 -07004218 # If the condition carries leading newlines, then count those as offsets.
4219 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4220 my $offset = statement_rawlines($whitespace) - 1;
4221
Joe Perchesaad4f612012-03-23 15:02:19 -07004222 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004223 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4224
4225 # We have looked at and allowed this specific line.
4226 $suppress_ifbraces{$ln + $offset} = 1;
4227
4228 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004229 $ln += statement_rawlines($block) - 1;
4230
Andy Whitcroft773647a2008-03-28 14:15:58 -07004231 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004232
4233 $seen++ if ($block =~ /^\s*{/);
4234
Joe Perchesaad4f612012-03-23 15:02:19 -07004235 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004236 if (statement_lines($cond) > 1) {
4237 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004238 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004239 }
4240 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004241 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004242 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004243 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004244 if (statement_block_size($block) > 1) {
4245 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004246 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004247 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004248 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004249 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004250 if ($seen) {
4251 my $sum_allowed = 0;
4252 foreach (@allowed) {
4253 $sum_allowed += $_;
4254 }
4255 if ($sum_allowed == 0) {
4256 WARN("BRACES",
4257 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4258 } elsif ($sum_allowed != $allow &&
4259 $seen != $allow) {
4260 CHK("BRACES",
4261 "braces {} should be used on all arms of this statement\n" . $herectx);
4262 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004263 }
4264 }
4265 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004266 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004267 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004268 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004269
Andy Whitcroftcf655042008-03-04 14:28:20 -08004270 # Check the pre-context.
4271 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4272 #print "APW: ALLOWED: pre<$1>\n";
4273 $allowed = 1;
4274 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004275
4276 my ($level, $endln, @chunks) =
4277 ctx_statement_full($linenr, $realcnt, $-[0]);
4278
Andy Whitcroftcf655042008-03-04 14:28:20 -08004279 # Check the condition.
4280 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004281 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004282 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004283 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004284 }
4285 if (statement_lines($cond) > 1) {
4286 #print "APW: ALLOWED: cond<$cond>\n";
4287 $allowed = 1;
4288 }
4289 if ($block =~/\b(?:if|for|while)\b/) {
4290 #print "APW: ALLOWED: block<$block>\n";
4291 $allowed = 1;
4292 }
4293 if (statement_block_size($block) > 1) {
4294 #print "APW: ALLOWED: lines block<$block>\n";
4295 $allowed = 1;
4296 }
4297 # Check the post-context.
4298 if (defined $chunks[1]) {
4299 my ($cond, $block) = @{$chunks[1]};
4300 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004301 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004302 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004303 if ($block =~ /^\s*\{/) {
4304 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4305 $allowed = 1;
4306 }
4307 }
4308 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004309 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004310 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004311
Andy Whitcroftf0556632008-10-15 22:02:23 -07004312 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004313 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004314 }
4315
Joe Perches000d1cc12011-07-25 17:13:25 -07004316 WARN("BRACES",
4317 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004318 }
4319 }
4320
Joe Perches0979ae62012-12-17 16:01:59 -08004321# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004322 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004323 CHK("BRACES",
4324 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4325 }
Joe Perches77b9a532013-07-03 15:05:29 -07004326 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004327 CHK("BRACES",
4328 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4329 }
4330
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004331# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004332 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4333 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004334 WARN("VOLATILE",
4335 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004336 }
4337
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004338# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004339 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004340 CHK("REDUNDANT_CODE",
4341 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004342 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004343 }
4344
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004345# check for needless "if (<foo>) fn(<foo>)" uses
4346 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4347 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4348 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4349 WARN('NEEDLESS_IF',
4350 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004351 }
4352 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004353
Joe Perchesebfdc402014-08-06 16:10:27 -07004354# check for unnecessary "Out of Memory" messages
4355 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4356 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4357 (defined $1 || defined $3) &&
4358 $linenr > 3) {
4359 my $testval = $2;
4360 my $testline = $lines[$linenr - 3];
4361
4362 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4363# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4364
4365 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4366 WARN("OOM_MESSAGE",
4367 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4368 }
4369 }
4370
Joe Perches8716de32013-09-11 14:24:05 -07004371# check for bad placement of section $InitAttribute (e.g.: __initdata)
4372 if ($line =~ /(\b$InitAttribute\b)/) {
4373 my $attr = $1;
4374 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4375 my $ptr = $1;
4376 my $var = $2;
4377 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4378 ERROR("MISPLACED_INIT",
4379 "$attr should be placed after $var\n" . $herecurr)) ||
4380 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4381 WARN("MISPLACED_INIT",
4382 "$attr should be placed after $var\n" . $herecurr))) &&
4383 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004384 $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 -07004385 }
4386 }
4387 }
4388
Joe Perchese970b8842013-11-12 15:10:10 -08004389# check for $InitAttributeData (ie: __initdata) with const
4390 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4391 my $attr = $1;
4392 $attr =~ /($InitAttributePrefix)(.*)/;
4393 my $attr_prefix = $1;
4394 my $attr_type = $2;
4395 if (ERROR("INIT_ATTRIBUTE",
4396 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4397 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004398 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004399 s/$InitAttributeData/${attr_prefix}initconst/;
4400 }
4401 }
4402
4403# check for $InitAttributeConst (ie: __initconst) without const
4404 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4405 my $attr = $1;
4406 if (ERROR("INIT_ATTRIBUTE",
4407 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4408 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004409 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004410 /(^\+\s*(?:static\s+))/;
4411 $lead = rtrim($1);
4412 $lead = "$lead " if ($lead !~ /^\+$/);
4413 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004414 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004415 }
4416 }
4417
Joe Perchesfbdb8132014-04-03 14:49:14 -07004418# don't use __constant_<foo> functions outside of include/uapi/
4419 if ($realfile !~ m@^include/uapi/@ &&
4420 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4421 my $constant_func = $1;
4422 my $func = $constant_func;
4423 $func =~ s/^__constant_//;
4424 if (WARN("CONSTANT_CONVERSION",
4425 "$constant_func should be $func\n" . $herecurr) &&
4426 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004427 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004428 }
4429 }
4430
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004431# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004432 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004433 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004434 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004435 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004436 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004437 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4438 }
4439 if ($delay > 2000) {
4440 WARN("LONG_UDELAY",
4441 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004442 }
4443 }
4444
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004445# warn about unexpectedly long msleep's
4446 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4447 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004448 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004449 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004450 }
4451 }
4452
Joe Perches36ec1932013-07-03 15:05:25 -07004453# check for comparisons of jiffies
4454 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4455 WARN("JIFFIES_COMPARISON",
4456 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4457 }
4458
Joe Perches9d7a34a2013-07-03 15:05:26 -07004459# check for comparisons of get_jiffies_64()
4460 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4461 WARN("JIFFIES_COMPARISON",
4462 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4463 }
4464
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004465# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004466# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004467# print "#ifdef in C files should be avoided\n";
4468# print "$herecurr";
4469# $clean = 0;
4470# }
4471
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004472# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004473 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004474 if (ERROR("SPACING",
4475 "exactly one space required after that #$1\n" . $herecurr) &&
4476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004477 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004478 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4479 }
4480
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004481 }
4482
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004483# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004484 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4485 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004486 my $which = $1;
4487 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004488 CHK("UNCOMMENTED_DEFINITION",
4489 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004490 }
4491 }
4492# check for memory barriers without a comment.
4493 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4494 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004495 WARN("MEMORY_BARRIER",
4496 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004497 }
4498 }
4499# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004500 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004501 CHK("ARCH_DEFINES",
4502 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004503 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004504
Tobias Klauserd4977c72010-05-24 14:33:30 -07004505# Check that the storage class is at the beginning of a declaration
4506 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004507 WARN("STORAGE_CLASS",
4508 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004509 }
4510
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004511# check the location of the inline attribute, that it is between
4512# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004513 if ($line =~ /\b$Type\s+$Inline\b/ ||
4514 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004515 ERROR("INLINE_LOCATION",
4516 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004517 }
4518
Andy Whitcroft8905a672007-11-28 16:21:06 -08004519# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004520 if ($realfile !~ m@\binclude/uapi/@ &&
4521 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004522 if (WARN("INLINE",
4523 "plain inline is preferred over $1\n" . $herecurr) &&
4524 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004525 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004526
4527 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004528 }
4529
Joe Perches3d130fd2011-01-12 17:00:00 -08004530# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004531 if ($realfile !~ m@\binclude/uapi/@ &&
4532 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004533 WARN("PREFER_PACKED",
4534 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004535 }
4536
Joe Perches39b7e282011-07-25 17:13:24 -07004537# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004538 if ($realfile !~ m@\binclude/uapi/@ &&
4539 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004540 WARN("PREFER_ALIGNED",
4541 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004542 }
4543
Joe Perches5f14d3b2012-01-10 15:09:52 -08004544# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004545 if ($realfile !~ m@\binclude/uapi/@ &&
4546 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004547 if (WARN("PREFER_PRINTF",
4548 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4549 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004550 $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 -07004551
4552 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004553 }
4554
Joe Perches6061d942012-03-23 15:02:16 -07004555# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004556 if ($realfile !~ m@\binclude/uapi/@ &&
4557 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004558 if (WARN("PREFER_SCANF",
4559 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4560 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004561 $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 -07004562 }
Joe Perches6061d942012-03-23 15:02:16 -07004563 }
4564
Joe Perches8f53a9b2010-03-05 13:43:48 -08004565# check for sizeof(&)
4566 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004567 WARN("SIZEOF_ADDRESS",
4568 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004569 }
4570
Joe Perches66c80b62012-07-30 14:41:22 -07004571# check for sizeof without parenthesis
4572 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004573 if (WARN("SIZEOF_PARENTHESIS",
4574 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004576 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004577 }
Joe Perches66c80b62012-07-30 14:41:22 -07004578 }
4579
Joe Perches428e2fd2011-05-24 17:13:39 -07004580# check for line continuations in quoted strings with odd counts of "
4581 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004582 WARN("LINE_CONTINUATIONS",
4583 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004584 }
4585
Joe Perches88982fe2012-12-17 16:02:00 -08004586# check for struct spinlock declarations
4587 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4588 WARN("USE_SPINLOCK_T",
4589 "struct spinlock should be spinlock_t\n" . $herecurr);
4590 }
4591
Joe Perchesa6962d72013-04-29 16:18:13 -07004592# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004593 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004594 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004595 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004596 if (WARN("PREFER_SEQ_PUTS",
4597 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4598 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004599 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004600 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004601 }
4602 }
4603
Andy Whitcroft554e1652012-01-10 15:09:57 -08004604# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004605 if ($^V && $^V ge 5.10.0 &&
4606 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004607 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004608
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004609 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004610 my $ms_val = $7;
4611 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004612
Andy Whitcroft554e1652012-01-10 15:09:57 -08004613 if ($ms_size =~ /^(0x|)0$/i) {
4614 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004615 "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 -08004616 } elsif ($ms_size =~ /^(0x|)1$/i) {
4617 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004618 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4619 }
4620 }
4621
Joe Perches98a9bba2014-01-23 15:54:52 -08004622# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4623 if ($^V && $^V ge 5.10.0 &&
4624 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4625 if (WARN("PREFER_ETHER_ADDR_COPY",
4626 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4627 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004628 $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 -08004629 }
4630 }
4631
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004632# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004633 if ($^V && $^V ge 5.10.0 &&
4634 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004635 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004636 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004637 my $call = $1;
4638 my $cast1 = deparenthesize($2);
4639 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004640 my $cast2 = deparenthesize($7);
4641 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004642 my $cast;
4643
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004644 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004645 $cast = "$cast1 or $cast2";
4646 } elsif ($cast1 ne "") {
4647 $cast = $cast1;
4648 } else {
4649 $cast = $cast2;
4650 }
4651 WARN("MINMAX",
4652 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004653 }
4654 }
4655
Joe Perches4a273192012-07-30 14:41:20 -07004656# check usleep_range arguments
4657 if ($^V && $^V ge 5.10.0 &&
4658 defined $stat &&
4659 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4660 my $min = $1;
4661 my $max = $7;
4662 if ($min eq $max) {
4663 WARN("USLEEP_RANGE",
4664 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4665 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4666 $min > $max) {
4667 WARN("USLEEP_RANGE",
4668 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4669 }
4670 }
4671
Joe Perches823b7942013-11-12 15:10:15 -08004672# check for naked sscanf
4673 if ($^V && $^V ge 5.10.0 &&
4674 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004675 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004676 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4677 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4678 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4679 my $lc = $stat =~ tr@\n@@;
4680 $lc = $lc + $linenr;
4681 my $stat_real = raw_line($linenr, 0);
4682 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4683 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4684 }
4685 WARN("NAKED_SSCANF",
4686 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4687 }
4688
Joe Perchesafc819a2014-06-04 16:12:08 -07004689# check for simple sscanf that should be kstrto<foo>
4690 if ($^V && $^V ge 5.10.0 &&
4691 defined $stat &&
4692 $line =~ /\bsscanf\b/) {
4693 my $lc = $stat =~ tr@\n@@;
4694 $lc = $lc + $linenr;
4695 my $stat_real = raw_line($linenr, 0);
4696 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4697 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4698 }
4699 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4700 my $format = $6;
4701 my $count = $format =~ tr@%@%@;
4702 if ($count == 1 &&
4703 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4704 WARN("SSCANF_TO_KSTRTO",
4705 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4706 }
4707 }
4708 }
4709
Joe Perches70dc8a42013-09-11 14:23:58 -07004710# check for new externs in .h files.
4711 if ($realfile =~ /\.h$/ &&
4712 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004713 if (CHK("AVOID_EXTERNS",
4714 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004715 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004716 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004717 }
4718 }
4719
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004720# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004721 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004722 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004723 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004724 my $function_name = $1;
4725 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004726
4727 my $s = $stat;
4728 if (defined $cond) {
4729 substr($s, 0, length($cond), '');
4730 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004731 if ($s =~ /^\s*;/ &&
4732 $function_name ne 'uninitialized_var')
4733 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004734 WARN("AVOID_EXTERNS",
4735 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004736 }
4737
4738 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004739 WARN("FUNCTION_ARGUMENTS",
4740 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004741 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004742
4743 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4744 $stat =~ /^.\s*extern\s+/)
4745 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004746 WARN("AVOID_EXTERNS",
4747 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004748 }
4749
4750# checks for new __setup's
4751 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4752 my $name = $1;
4753
4754 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004755 CHK("UNDOCUMENTED_SETUP",
4756 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004757 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004758 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004759
4760# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004761 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004762 WARN("UNNECESSARY_CASTS",
4763 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004764 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004765
Joe Perchesa640d252013-07-03 15:05:21 -07004766# alloc style
4767# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4768 if ($^V && $^V ge 5.10.0 &&
4769 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4770 CHK("ALLOC_SIZEOF_STRUCT",
4771 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4772 }
4773
Joe Perches60a55362014-06-04 16:12:07 -07004774# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4775 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004776 $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 -07004777 my $oldfunc = $3;
4778 my $a1 = $4;
4779 my $a2 = $10;
4780 my $newfunc = "kmalloc_array";
4781 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004782 my $r1 = $a1;
4783 my $r2 = $a2;
4784 if ($a1 =~ /^sizeof\s*\S/) {
4785 $r1 = $a2;
4786 $r2 = $a1;
4787 }
4788 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4789 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004790 if (WARN("ALLOC_WITH_MULTIPLY",
4791 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004793 $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 -07004794
4795 }
4796 }
4797 }
4798
Joe Perches972fdea2013-04-29 16:18:12 -07004799# check for krealloc arg reuse
4800 if ($^V && $^V ge 5.10.0 &&
4801 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4802 WARN("KREALLOC_ARG_REUSE",
4803 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4804 }
4805
Joe Perches5ce59ae2013-02-21 16:44:18 -08004806# check for alloc argument mismatch
4807 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4808 WARN("ALLOC_ARRAY_ARGS",
4809 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4810 }
4811
Joe Perchescaf2a542011-01-12 16:59:56 -08004812# check for multiple semicolons
4813 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004814 if (WARN("ONE_SEMICOLON",
4815 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4816 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004817 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004818 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004819 }
4820
Joe Perchese81f2392014-08-06 16:11:25 -07004821# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08004822 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4823 my $has_break = 0;
4824 my $has_statement = 0;
4825 my $count = 0;
4826 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07004827 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08004828 $prevline--;
4829 my $rline = $rawlines[$prevline - 1];
4830 my $fline = $lines[$prevline - 1];
4831 last if ($fline =~ /^\@\@/);
4832 next if ($fline =~ /^\-/);
4833 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4834 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4835 next if ($fline =~ /^.[\s$;]*$/);
4836 $has_statement = 1;
4837 $count++;
4838 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4839 }
4840 if (!$has_break && $has_statement) {
4841 WARN("MISSING_BREAK",
4842 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4843 }
4844 }
4845
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004846# check for switch/default statements without a break;
4847 if ($^V && $^V ge 5.10.0 &&
4848 defined $stat &&
4849 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4850 my $ctx = '';
4851 my $herectx = $here . "\n";
4852 my $cnt = statement_rawlines($stat);
4853 for (my $n = 0; $n < $cnt; $n++) {
4854 $herectx .= raw_line($linenr, $n) . "\n";
4855 }
4856 WARN("DEFAULT_NO_BREAK",
4857 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004858 }
4859
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004860# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004861 if ($line =~ /\b__FUNCTION__\b/) {
4862 if (WARN("USE_FUNC",
4863 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4864 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004865 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004866 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004867 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004868
Joe Perches2c924882012-03-23 15:02:20 -07004869# check for use of yield()
4870 if ($line =~ /\byield\s*\(\s*\)/) {
4871 WARN("YIELD",
4872 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4873 }
4874
Joe Perches179f8f42013-07-03 15:05:30 -07004875# check for comparisons against true and false
4876 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4877 my $lead = $1;
4878 my $arg = $2;
4879 my $test = $3;
4880 my $otype = $4;
4881 my $trail = $5;
4882 my $op = "!";
4883
4884 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4885
4886 my $type = lc($otype);
4887 if ($type =~ /^(?:true|false)$/) {
4888 if (("$test" eq "==" && "$type" eq "true") ||
4889 ("$test" eq "!=" && "$type" eq "false")) {
4890 $op = "";
4891 }
4892
4893 CHK("BOOL_COMPARISON",
4894 "Using comparison to $otype is error prone\n" . $herecurr);
4895
4896## maybe suggesting a correct construct would better
4897## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4898
4899 }
4900 }
4901
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004902# check for semaphores initialized locked
4903 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004904 WARN("CONSIDER_COMPLETION",
4905 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004906 }
Joe Perches6712d852012-03-23 15:02:20 -07004907
Joe Perches67d0a072011-10-31 17:13:10 -07004908# recommend kstrto* over simple_strto* and strict_strto*
4909 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004910 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004911 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004912 }
Joe Perches6712d852012-03-23 15:02:20 -07004913
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004914# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07004915 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004916 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004917 "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 -07004918 }
Joe Perches6712d852012-03-23 15:02:20 -07004919
Emese Revfy79404842010-03-05 13:43:53 -08004920# check for various ops structs, ensure they are const.
4921 my $struct_ops = qr{acpi_dock_ops|
4922 address_space_operations|
4923 backlight_ops|
4924 block_device_operations|
4925 dentry_operations|
4926 dev_pm_ops|
4927 dma_map_ops|
4928 extent_io_ops|
4929 file_lock_operations|
4930 file_operations|
4931 hv_ops|
4932 ide_dma_ops|
4933 intel_dvo_dev_ops|
4934 item_operations|
4935 iwl_ops|
4936 kgdb_arch|
4937 kgdb_io|
4938 kset_uevent_ops|
4939 lock_manager_operations|
4940 microcode_ops|
4941 mtrr_ops|
4942 neigh_ops|
4943 nlmsvc_binding|
4944 pci_raw_ops|
4945 pipe_buf_operations|
4946 platform_hibernation_ops|
4947 platform_suspend_ops|
4948 proto_ops|
4949 rpc_pipe_ops|
4950 seq_operations|
4951 snd_ac97_build_ops|
4952 soc_pcmcia_socket_ops|
4953 stacktrace_ops|
4954 sysfs_ops|
4955 tty_operations|
4956 usb_mon_operations|
4957 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004958 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004959 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004960 WARN("CONST_STRUCT",
4961 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004962 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004963 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004964
4965# use of NR_CPUS is usually wrong
4966# ignore definitions of NR_CPUS and usage to define arrays as likely right
4967 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004968 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4969 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004970 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4971 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4972 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004973 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004974 WARN("NR_CPUS",
4975 "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 -07004976 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004977
Joe Perches52ea8502013-11-12 15:10:09 -08004978# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4979 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4980 ERROR("DEFINE_ARCH_HAS",
4981 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4982 }
4983
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004984# check for %L{u,d,i} in strings
4985 my $string;
4986 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4987 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07004988 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004989 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004990 WARN("PRINTF_L",
4991 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004992 last;
4993 }
4994 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08004995
4996# whine mightly about in_atomic
4997 if ($line =~ /\bin_atomic\s*\(/) {
4998 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004999 ERROR("IN_ATOMIC",
5000 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005001 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005002 WARN("IN_ATOMIC",
5003 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005004 }
5005 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005006
5007# check for lockdep_set_novalidate_class
5008 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5009 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5010 if ($realfile !~ m@^kernel/lockdep@ &&
5011 $realfile !~ m@^include/linux/lockdep@ &&
5012 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005013 ERROR("LOCKDEP",
5014 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005015 }
5016 }
Dave Jones88f88312011-01-12 16:59:59 -08005017
5018 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5019 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005020 WARN("EXPORTED_WORLD_WRITABLE",
5021 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005022 }
Joe Perches24358802014-04-03 14:49:13 -07005023
Joe Perches515a2352014-04-03 14:49:24 -07005024# Mode permission misuses where it seems decimal should be octal
5025# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5026 if ($^V && $^V ge 5.10.0 &&
5027 $line =~ /$mode_perms_search/) {
5028 foreach my $entry (@mode_permission_funcs) {
5029 my $func = $entry->[0];
5030 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005031
Joe Perches515a2352014-04-03 14:49:24 -07005032 my $skip_args = "";
5033 if ($arg_pos > 1) {
5034 $arg_pos--;
5035 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5036 }
5037 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5038 if ($line =~ /$test/) {
5039 my $val = $1;
5040 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005041
Joe Perches515a2352014-04-03 14:49:24 -07005042 if ($val !~ /^0$/ &&
5043 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5044 length($val) ne 4)) {
5045 ERROR("NON_OCTAL_PERMISSIONS",
5046 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5047 }
Joe Perches24358802014-04-03 14:49:13 -07005048 }
5049 }
5050 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005051 }
5052
5053 # If we have no input at all, then there is nothing to report on
5054 # so just keep quiet.
5055 if ($#rawlines == -1) {
5056 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005057 }
5058
Andy Whitcroft8905a672007-11-28 16:21:06 -08005059 # In mailback mode only produce a report in the negative, for
5060 # things that appear to be patches.
5061 if ($mailback && ($clean == 1 || !$is_patch)) {
5062 exit(0);
5063 }
5064
5065 # This is not a patch, and we are are in 'no-patch' mode so
5066 # just keep quiet.
5067 if (!$chk_patch && !$is_patch) {
5068 exit(0);
5069 }
5070
5071 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005072 ERROR("NOT_UNIFIED_DIFF",
5073 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005074 }
5075 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005076 ERROR("MISSING_SIGN_OFF",
5077 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005078 }
5079
Andy Whitcroft8905a672007-11-28 16:21:06 -08005080 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005081 if ($summary && !($clean == 1 && $quiet == 1)) {
5082 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005083 print "total: $cnt_error errors, $cnt_warn warnings, " .
5084 (($check)? "$cnt_chk checks, " : "") .
5085 "$cnt_lines lines checked\n";
5086 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005087 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005088
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005089 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005090
5091 if ($^V lt 5.10.0) {
5092 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5093 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5094 }
5095
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005096 # If there were whitespace errors which cleanpatch can fix
5097 # then suggest that.
5098 if ($rpt_cleaners) {
5099 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5100 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005101 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005102 }
5103 }
5104
Joe Perches91bfe482013-09-11 14:23:59 -07005105 hash_show_words(\%use_type, "Used");
5106 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005107
Joe Perchesd752fcc2014-08-06 16:11:05 -07005108 if ($clean == 0 && $fix &&
5109 ("@rawlines" ne "@fixed" ||
5110 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005111 my $newfile = $filename;
5112 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005113 my $linecount = 0;
5114 my $f;
5115
Joe Perchesd752fcc2014-08-06 16:11:05 -07005116 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5117
Joe Perches3705ce52013-07-03 15:05:31 -07005118 open($f, '>', $newfile)
5119 or die "$P: Can't open $newfile for write\n";
5120 foreach my $fixed_line (@fixed) {
5121 $linecount++;
5122 if ($file) {
5123 if ($linecount > 3) {
5124 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005125 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005126 }
5127 } else {
5128 print $f $fixed_line . "\n";
5129 }
5130 }
5131 close($f);
5132
5133 if (!$quiet) {
5134 print << "EOM";
5135Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5136
5137Do _NOT_ trust the results written to this file.
5138Do _NOT_ submit these changes without inspecting them for correctness.
5139
5140This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5141No warranties, expressed or implied...
5142
5143EOM
5144 }
5145 }
5146
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005147 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005148 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005149 }
5150 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005151 print << "EOM";
5152$vname has style problems, please review.
5153
5154If any of these errors are false positives, please report
5155them to the maintainer, see CHECKPATCH in MAINTAINERS.
5156EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005157 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005158
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005159 return $clean;
5160}