blob: 9846ddeafdf090cb034e691af9ca5b3b70a1754a [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;
Vadim Bendebury56193272014-10-13 15:51:48 -070046my $min_conf_desc_length = 4;
Hannes Eder77f5b102009-09-21 17:04:37 -070047
48sub help {
49 my ($exitcode) = @_;
50
51 print << "EOM";
52Usage: $P [OPTION]... [FILE]...
53Version: $V
54
55Options:
56 -q, --quiet quiet
57 --no-tree run without a kernel tree
58 --no-signoff do not check for 'Signed-off-by' line
59 --patch treat FILE as patchfile (default)
60 --emacs emacs compile window format
61 --terse one line per report
62 -f, --file treat FILE as regular source file
63 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070064 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070065 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080066 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070067 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070068 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070069 --root=PATH PATH to the kernel tree root
70 --no-summary suppress the per-file summary
71 --mailback only produce a report in case of warnings/errors
72 --summary-file include the filename in summary
73 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
74 'values', 'possible', 'type', and 'attr' (default
75 is all off)
76 --test-only=WORD report only warnings/errors containing WORD
77 literally
Joe Perches3705ce52013-07-03 15:05:31 -070078 --fix EXPERIMENTAL - may create horrible results
79 If correctable single-line errors exist, create
80 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
81 with potential errors corrected to the preferred
82 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080083 --fix-inplace EXPERIMENTAL - may create horrible results
84 Is the same as --fix, but overwrites the input
85 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070086 --ignore-perl-version override checking of perl version. expect
87 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070088 -h, --help, --version display this help and exit
89
90When FILE is - read standard input.
91EOM
92
93 exit($exitcode);
94}
95
Joe Perches000d1cc12011-07-25 17:13:25 -070096my $conf = which_conf($configuration_file);
97if (-f $conf) {
98 my @conf_args;
99 open(my $conffile, '<', "$conf")
100 or warn "$P: Can't find a readable $configuration_file file $!\n";
101
102 while (<$conffile>) {
103 my $line = $_;
104
105 $line =~ s/\s*\n?$//g;
106 $line =~ s/^\s*//g;
107 $line =~ s/\s+/ /g;
108
109 next if ($line =~ m/^\s*#/);
110 next if ($line =~ m/^\s*$/);
111
112 my @words = split(" ", $line);
113 foreach my $word (@words) {
114 last if ($word =~ m/^#/);
115 push (@conf_args, $word);
116 }
117 }
118 close($conffile);
119 unshift(@ARGV, @conf_args) if @conf_args;
120}
121
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700122GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700123 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700124 'tree!' => \$tree,
125 'signoff!' => \$chk_signoff,
126 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700127 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800128 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700129 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700130 'subjective!' => \$check,
131 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700132 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700133 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700134 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800135 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700136 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700137 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800138 'summary!' => \$summary,
139 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800140 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700141 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800142 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700143 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800144 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700145 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700146 'h|help' => \$help,
147 'version' => \$help
148) or help(1);
149
150help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700151
Joe Perches9624b8d2014-01-23 15:54:44 -0800152$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700153$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800154
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700155my $exit = 0;
156
Dave Hansend62a2012013-09-11 14:23:56 -0700157if ($^V && $^V lt $minimum_perl_version) {
158 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
159 if (!$ignore_perl_version) {
160 exit(1);
161 }
162}
163
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700164if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700165 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700166 exit(1);
167}
168
Joe Perches91bfe482013-09-11 14:23:59 -0700169sub hash_save_array_words {
170 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700171
Joe Perches91bfe482013-09-11 14:23:59 -0700172 my @array = split(/,/, join(',', @$arrayRef));
173 foreach my $word (@array) {
174 $word =~ s/\s*\n?$//g;
175 $word =~ s/^\s*//g;
176 $word =~ s/\s+/ /g;
177 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700178
Joe Perches91bfe482013-09-11 14:23:59 -0700179 next if ($word =~ m/^\s*#/);
180 next if ($word =~ m/^\s*$/);
181
182 $hashRef->{$word}++;
183 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700184}
185
Joe Perches91bfe482013-09-11 14:23:59 -0700186sub hash_show_words {
187 my ($hashRef, $prefix) = @_;
188
Joe Perches58cb3cf2013-09-11 14:24:04 -0700189 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700190 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700191 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700192 print " $word";
193 }
194 print "\n\n";
195 }
196}
197
198hash_save_array_words(\%ignore_type, \@ignore);
199hash_save_array_words(\%use_type, \@use);
200
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800201my $dbg_values = 0;
202my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700203my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700204my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800205for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800206 ## no critic
207 eval "\${dbg_$key} = '$debug{$key}';";
208 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800209}
210
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700211my $rpt_cleaners = 0;
212
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213if ($terse) {
214 $emacs = 1;
215 $quiet++;
216}
217
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700218if ($tree) {
219 if (defined $root) {
220 if (!top_of_kernel_tree($root)) {
221 die "$P: $root: --root does not point at a valid tree\n";
222 }
223 } else {
224 if (top_of_kernel_tree('.')) {
225 $root = '.';
226 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
227 top_of_kernel_tree($1)) {
228 $root = $1;
229 }
230 }
231
232 if (!defined $root) {
233 print "Must be run from the top-level dir. of a kernel tree\n";
234 exit(2);
235 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700236}
237
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700238my $emitted_corrupt = 0;
239
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700240our $Ident = qr{
241 [A-Za-z_][A-Za-z\d_]*
242 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
243 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700244our $Storage = qr{extern|static|asmlinkage};
245our $Sparse = qr{
246 __user|
247 __kernel|
248 __force|
249 __iomem|
250 __must_check|
251 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800252 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700253 __ref|
254 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700255 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800256our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
257our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
258our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
259our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
260our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700261
Wolfram Sang52131292010-03-05 13:43:51 -0800262# Notes to $Attribute:
263# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700264our $Attribute = qr{
265 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700266 __percpu|
267 __nocast|
268 __safe|
269 __bitwise__|
270 __packed__|
271 __packed2__|
272 __naked|
273 __maybe_unused|
274 __always_unused|
275 __noreturn|
276 __used|
277 __cold|
278 __noclone|
279 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700280 __read_mostly|
281 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700282 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700283 ____cacheline_aligned|
284 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800285 ____cacheline_internodealigned_in_smp|
286 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700287 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700288our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700289our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
291our $Lval = qr{$Ident(?:$Member)*};
292
Joe Perches95e2c602013-07-03 15:05:20 -0700293our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
294our $Binary = qr{(?i)0b[01]+$Int_type?};
295our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
296our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700297our $Octal = qr{0[0-7]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800298our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
299our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
300our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800301our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700302our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800303our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700304our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700305our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700306our $Operators = qr{
307 <=|>=|==|!=|
308 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700309 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700310 }x;
311
Joe Perches91cb5192014-04-03 14:49:32 -0700312our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
313
Andy Whitcroft8905a672007-11-28 16:21:06 -0800314our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700315our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700316our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800317our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700318our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800319our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700320our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800321
Joe Perches15662b32011-10-31 17:13:12 -0700322our $NON_ASCII_UTF8 = qr{
323 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700324 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
325 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
326 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
327 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
328 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
329 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
330}x;
331
Joe Perches15662b32011-10-31 17:13:12 -0700332our $UTF8 = qr{
333 [\x09\x0A\x0D\x20-\x7E] # ASCII
334 | $NON_ASCII_UTF8
335}x;
336
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700337our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700338 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700339 atomic_t
340)};
341
Joe Perches691e6692010-03-05 13:43:51 -0800342our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700343 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700344 (?:[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 -0700345 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700346 panic|
Joe Perches06668722013-11-12 15:10:07 -0800347 MODULE_[A-Z_]+|
348 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800349)};
350
Joe Perches20112472011-07-25 17:13:23 -0700351our $signature_tags = qr{(?xi:
352 Signed-off-by:|
353 Acked-by:|
354 Tested-by:|
355 Reviewed-by:|
356 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700357 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700358 To:|
359 Cc:
360)};
361
Joe Perches18130872014-08-06 16:11:22 -0700362our @typeListMisordered = (
363 qr{char\s+(?:un)?signed},
364 qr{int\s+(?:(?:un)?signed\s+)?short\s},
365 qr{int\s+short(?:\s+(?:un)?signed)},
366 qr{short\s+int(?:\s+(?:un)?signed)},
367 qr{(?:un)?signed\s+int\s+short},
368 qr{short\s+(?:un)?signed},
369 qr{long\s+int\s+(?:un)?signed},
370 qr{int\s+long\s+(?:un)?signed},
371 qr{long\s+(?:un)?signed\s+int},
372 qr{int\s+(?:un)?signed\s+long},
373 qr{int\s+(?:un)?signed},
374 qr{int\s+long\s+long\s+(?:un)?signed},
375 qr{long\s+long\s+int\s+(?:un)?signed},
376 qr{long\s+long\s+(?:un)?signed\s+int},
377 qr{long\s+long\s+(?:un)?signed},
378 qr{long\s+(?:un)?signed},
379);
380
Andy Whitcroft8905a672007-11-28 16:21:06 -0800381our @typeList = (
382 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700383 qr{(?:(?:un)?signed\s+)?char},
384 qr{(?:(?:un)?signed\s+)?short\s+int},
385 qr{(?:(?:un)?signed\s+)?short},
386 qr{(?:(?:un)?signed\s+)?int},
387 qr{(?:(?:un)?signed\s+)?long\s+int},
388 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
389 qr{(?:(?:un)?signed\s+)?long\s+long},
390 qr{(?:(?:un)?signed\s+)?long},
391 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392 qr{float},
393 qr{double},
394 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395 qr{struct\s+$Ident},
396 qr{union\s+$Ident},
397 qr{enum\s+$Ident},
398 qr{${Ident}_t},
399 qr{${Ident}_handler},
400 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700401 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800402);
Joe Perches8716de32013-09-11 14:24:05 -0700403our @typeListWithAttr = (
404 @typeList,
405 qr{struct\s+$InitAttribute\s+$Ident},
406 qr{union\s+$InitAttribute\s+$Ident},
407);
408
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700409our @modifierList = (
410 qr{fastcall},
411);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800412
Joe Perches24358802014-04-03 14:49:13 -0700413our @mode_permission_funcs = (
414 ["module_param", 3],
415 ["module_param_(?:array|named|string)", 4],
416 ["module_param_array_named", 5],
417 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
418 ["proc_create(?:_data|)", 2],
419 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
420);
421
Joe Perches515a2352014-04-03 14:49:24 -0700422#Create a search pattern for all these functions to speed up a loop below
423our $mode_perms_search = "";
424foreach my $entry (@mode_permission_funcs) {
425 $mode_perms_search .= '|' if ($mode_perms_search ne "");
426 $mode_perms_search .= $entry->[0];
427}
428
Wolfram Sang7840a942010-08-09 17:20:57 -0700429our $allowed_asm_includes = qr{(?x:
430 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700431 memory|
432 time|
433 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700434)};
435# memory.h: ARM has a custom one
436
Andy Whitcroft8905a672007-11-28 16:21:06 -0800437sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700438 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
439 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700440 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700441 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700442 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800443 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700444 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800445 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800446 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700447 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700448 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800449 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700450 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800451 }x;
Joe Perches18130872014-08-06 16:11:22 -0700452 $NonptrTypeMisordered = qr{
453 (?:$Modifier\s+|const\s+)*
454 (?:
455 (?:${Misordered}\b)
456 )
457 (?:\s+$Modifier|\s+const)*
458 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700459 $NonptrTypeWithAttr = qr{
460 (?:$Modifier\s+|const\s+)*
461 (?:
462 (?:typeof|__typeof__)\s*\([^\)]*\)|
463 (?:$typeTypedefs\b)|
464 (?:${allWithAttr}\b)
465 )
466 (?:\s+$Modifier|\s+const)*
467 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700469 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700470 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700471 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800472 }x;
Joe Perches18130872014-08-06 16:11:22 -0700473 $TypeMisordered = qr{
474 $NonptrTypeMisordered
475 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
476 (?:\s+$Inline|\s+$Modifier)*
477 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700478 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700479 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800480}
481build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700482
Joe Perches7d2367a2011-07-25 17:13:22 -0700483our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700484
485# Using $balanced_parens, $LvalOrFunc, or $FuncArg
486# requires at least perl version v5.10.0
487# Any use must be runtime checked with $^V
488
489our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700490our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800491our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700492
Joe Perchesf8422302014-08-06 16:11:31 -0700493our $declaration_macros = qr{(?x:
494 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
495 (?:$Storage\s+)?LIST_HEAD\s*\(|
496 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
497)};
498
Joe Perches7d2367a2011-07-25 17:13:22 -0700499sub deparenthesize {
500 my ($string) = @_;
501 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700502
503 while ($string =~ /^\s*\(.*\)\s*$/) {
504 $string =~ s@^\s*\(\s*@@;
505 $string =~ s@\s*\)\s*$@@;
506 }
507
Joe Perches7d2367a2011-07-25 17:13:22 -0700508 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700509
Joe Perches7d2367a2011-07-25 17:13:22 -0700510 return $string;
511}
512
Joe Perches34456862013-07-03 15:05:34 -0700513sub seed_camelcase_file {
514 my ($file) = @_;
515
516 return if (!(-f $file));
517
518 local $/;
519
520 open(my $include_file, '<', "$file")
521 or warn "$P: Can't read '$file' $!\n";
522 my $text = <$include_file>;
523 close($include_file);
524
525 my @lines = split('\n', $text);
526
527 foreach my $line (@lines) {
528 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
529 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
530 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800531 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
532 $camelcase{$1} = 1;
533 } 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 -0700534 $camelcase{$1} = 1;
535 }
536 }
537}
538
539my $camelcase_seeded = 0;
540sub seed_camelcase_includes {
541 return if ($camelcase_seeded);
542
543 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700544 my $camelcase_cache = "";
545 my @include_files = ();
546
547 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700548
Richard Genoud3645e322014-02-10 14:25:32 -0800549 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700550 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
551 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700552 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700553 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700554 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700555 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700556 @include_files = split('\n', $files);
557 foreach my $file (@include_files) {
558 my $date = POSIX::strftime("%Y%m%d%H%M",
559 localtime((stat $file)[9]));
560 $last_mod_date = $date if ($last_mod_date < $date);
561 }
562 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700563 }
Joe Perchesc707a812013-07-08 16:00:43 -0700564
565 if ($camelcase_cache ne "" && -f $camelcase_cache) {
566 open(my $camelcase_file, '<', "$camelcase_cache")
567 or warn "$P: Can't read '$camelcase_cache' $!\n";
568 while (<$camelcase_file>) {
569 chomp;
570 $camelcase{$_} = 1;
571 }
572 close($camelcase_file);
573
574 return;
575 }
576
Richard Genoud3645e322014-02-10 14:25:32 -0800577 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700578 $files = `git ls-files "include/*.h"`;
579 @include_files = split('\n', $files);
580 }
581
Joe Perches34456862013-07-03 15:05:34 -0700582 foreach my $file (@include_files) {
583 seed_camelcase_file($file);
584 }
Joe Perches351b2a12013-07-03 15:05:36 -0700585
Joe Perchesc707a812013-07-08 16:00:43 -0700586 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700587 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700588 open(my $camelcase_file, '>', "$camelcase_cache")
589 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700590 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
591 print $camelcase_file ("$_\n");
592 }
593 close($camelcase_file);
594 }
Joe Perches34456862013-07-03 15:05:34 -0700595}
596
Joe Perchesd311cd42014-08-06 16:10:57 -0700597sub git_commit_info {
598 my ($commit, $id, $desc) = @_;
599
600 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
601
602 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
603 $output =~ s/^\s*//gm;
604 my @lines = split("\n", $output);
605
606 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
607# Maybe one day convert this block of bash into something that returns
608# all matching commit ids, but it's very slow...
609#
610# echo "checking commits $1..."
611# git rev-list --remotes | grep -i "^$1" |
612# while read line ; do
613# git log --format='%H %s' -1 $line |
614# echo "commit $(cut -c 1-12,41-)"
615# done
616 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
617 } else {
618 $id = substr($lines[0], 0, 12);
619 $desc = substr($lines[0], 41);
620 }
621
622 return ($id, $desc);
623}
624
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700625$chk_signoff = 0 if ($file);
626
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700627my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800628my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700629my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700630my @fixed_inserted = ();
631my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700632my $fixlinenr = -1;
633
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800634my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700635for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800636 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700637 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800638 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700639 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800640 } elsif ($filename eq '-') {
641 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700642 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800643 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700644 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700645 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800646 if ($filename eq '-') {
647 $vname = 'Your patch';
648 } else {
649 $vname = $filename;
650 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800651 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700652 chomp;
653 push(@rawlines, $_);
654 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800655 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800656 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700657 $exit = 1;
658 }
659 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800660 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700661 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700662 @fixed_inserted = ();
663 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700664 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700665}
666
667exit($exit);
668
669sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700670 my ($root) = @_;
671
672 my @tree_check = (
673 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
674 "README", "Documentation", "arch", "include", "drivers",
675 "fs", "init", "ipc", "kernel", "lib", "scripts",
676 );
677
678 foreach my $check (@tree_check) {
679 if (! -e $root . '/' . $check) {
680 return 0;
681 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700682 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700683 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700684}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700685
Joe Perches20112472011-07-25 17:13:23 -0700686sub parse_email {
687 my ($formatted_email) = @_;
688
689 my $name = "";
690 my $address = "";
691 my $comment = "";
692
693 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
694 $name = $1;
695 $address = $2;
696 $comment = $3 if defined $3;
697 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
698 $address = $1;
699 $comment = $2 if defined $2;
700 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
701 $address = $1;
702 $comment = $2 if defined $2;
703 $formatted_email =~ s/$address.*$//;
704 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700705 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700706 $name =~ s/^\"|\"$//g;
707 # If there's a name left after stripping spaces and
708 # leading quotes, and the address doesn't have both
709 # leading and trailing angle brackets, the address
710 # is invalid. ie:
711 # "joe smith joe@smith.com" bad
712 # "joe smith <joe@smith.com" bad
713 if ($name ne "" && $address !~ /^<[^>]+>$/) {
714 $name = "";
715 $address = "";
716 $comment = "";
717 }
718 }
719
Joe Perches3705ce52013-07-03 15:05:31 -0700720 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700721 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700722 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700723 $address =~ s/^\<|\>$//g;
724
725 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
726 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
727 $name = "\"$name\"";
728 }
729
730 return ($name, $address, $comment);
731}
732
733sub format_email {
734 my ($name, $address) = @_;
735
736 my $formatted_email;
737
Joe Perches3705ce52013-07-03 15:05:31 -0700738 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700739 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700740 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700741
742 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
743 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
744 $name = "\"$name\"";
745 }
746
747 if ("$name" eq "") {
748 $formatted_email = "$address";
749 } else {
750 $formatted_email = "$name <$address>";
751 }
752
753 return $formatted_email;
754}
755
Joe Perchesd311cd42014-08-06 16:10:57 -0700756sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700757 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700758
Joe Perchesbd474ca2014-08-06 16:11:10 -0700759 foreach my $path (split(/:/, $ENV{PATH})) {
760 if (-e "$path/$bin") {
761 return "$path/$bin";
762 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700763 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700764
Joe Perchesbd474ca2014-08-06 16:11:10 -0700765 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700766}
767
Joe Perches000d1cc12011-07-25 17:13:25 -0700768sub which_conf {
769 my ($conf) = @_;
770
771 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
772 if (-e "$path/$conf") {
773 return "$path/$conf";
774 }
775 }
776
777 return "";
778}
779
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700780sub expand_tabs {
781 my ($str) = @_;
782
783 my $res = '';
784 my $n = 0;
785 for my $c (split(//, $str)) {
786 if ($c eq "\t") {
787 $res .= ' ';
788 $n++;
789 for (; ($n % 8) != 0; $n++) {
790 $res .= ' ';
791 }
792 next;
793 }
794 $res .= $c;
795 $n++;
796 }
797
798 return $res;
799}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700800sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700801 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700802 return $res;
803}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700804
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700805sub line_stats {
806 my ($line) = @_;
807
808 # Drop the diff line leader and expand tabs
809 $line =~ s/^.//;
810 $line = expand_tabs($line);
811
812 # Pick the indent from the front of the line.
813 my ($white) = ($line =~ /^(\s*)/);
814
815 return (length($line), length($white));
816}
817
Andy Whitcroft773647a2008-03-28 14:15:58 -0700818my $sanitise_quote = '';
819
820sub sanitise_line_reset {
821 my ($in_comment) = @_;
822
823 if ($in_comment) {
824 $sanitise_quote = '*/';
825 } else {
826 $sanitise_quote = '';
827 }
828}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700829sub sanitise_line {
830 my ($line) = @_;
831
832 my $res = '';
833 my $l = '';
834
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800835 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700836 my $off = 0;
837 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700838
Andy Whitcroft773647a2008-03-28 14:15:58 -0700839 # Always copy over the diff marker.
840 $res = substr($line, 0, 1);
841
842 for ($off = 1; $off < length($line); $off++) {
843 $c = substr($line, $off, 1);
844
845 # Comments we are wacking completly including the begin
846 # and end, all to $;.
847 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
848 $sanitise_quote = '*/';
849
850 substr($res, $off, 2, "$;$;");
851 $off++;
852 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800853 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700854 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700855 $sanitise_quote = '';
856 substr($res, $off, 2, "$;$;");
857 $off++;
858 next;
859 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700860 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
861 $sanitise_quote = '//';
862
863 substr($res, $off, 2, $sanitise_quote);
864 $off++;
865 next;
866 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700867
868 # A \ in a string means ignore the next character.
869 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
870 $c eq "\\") {
871 substr($res, $off, 2, 'XX');
872 $off++;
873 next;
874 }
875 # Regular quotes.
876 if ($c eq "'" || $c eq '"') {
877 if ($sanitise_quote eq '') {
878 $sanitise_quote = $c;
879
880 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700881 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700882 } elsif ($sanitise_quote eq $c) {
883 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700884 }
885 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700886
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800887 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700888 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
889 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700890 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
891 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700892 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
893 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700894 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700895 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700896 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800897 }
898
Daniel Walker113f04a2009-09-21 17:04:35 -0700899 if ($sanitise_quote eq '//') {
900 $sanitise_quote = '';
901 }
902
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800903 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700904 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905 my $clean = 'X' x length($1);
906 $res =~ s@\<.*\>@<$clean>@;
907
908 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700909 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800910 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700911 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800912 }
913
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700914 return $res;
915}
916
Joe Perchesa6962d72013-04-29 16:18:13 -0700917sub get_quoted_string {
918 my ($line, $rawline) = @_;
919
920 return "" if ($line !~ m/(\"[X]+\")/g);
921 return substr($rawline, $-[0], $+[0] - $-[0]);
922}
923
Andy Whitcroft8905a672007-11-28 16:21:06 -0800924sub ctx_statement_block {
925 my ($linenr, $remain, $off) = @_;
926 my $line = $linenr - 1;
927 my $blk = '';
928 my $soff = $off;
929 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700930 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800931
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800932 my $loff = 0;
933
Andy Whitcroft8905a672007-11-28 16:21:06 -0800934 my $type = '';
935 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800936 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800937 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800938 my $c;
939 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800940
941 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800942 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800943 @stack = (['', 0]) if ($#stack == -1);
944
Andy Whitcroft773647a2008-03-28 14:15:58 -0700945 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800946 # If we are about to drop off the end, pull in more
947 # context.
948 if ($off >= $len) {
949 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700950 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800951 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800952 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800953 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800954 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800955 $len = length($blk);
956 $line++;
957 last;
958 }
959 # Bail if there is no further context.
960 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800961 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800962 last;
963 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800964 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
965 $level++;
966 $type = '#';
967 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800968 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800969 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800970 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800971 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800972
Andy Whitcroft773647a2008-03-28 14:15:58 -0700973 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -0800974
975 # Handle nested #if/#else.
976 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
977 push(@stack, [ $type, $level ]);
978 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
979 ($type, $level) = @{$stack[$#stack - 1]};
980 } elsif ($remainder =~ /^#\s*endif\b/) {
981 ($type, $level) = @{pop(@stack)};
982 }
983
Andy Whitcroft8905a672007-11-28 16:21:06 -0800984 # Statement ends at the ';' or a close '}' at the
985 # outermost level.
986 if ($level == 0 && $c eq ';') {
987 last;
988 }
989
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800990 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700991 if ($level == 0 && $coff_set == 0 &&
992 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
993 $remainder =~ /^(else)(?:\s|{)/ &&
994 $remainder !~ /^else\s+if\b/) {
995 $coff = $off + length($1) - 1;
996 $coff_set = 1;
997 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
998 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800999 }
1000
Andy Whitcroft8905a672007-11-28 16:21:06 -08001001 if (($type eq '' || $type eq '(') && $c eq '(') {
1002 $level++;
1003 $type = '(';
1004 }
1005 if ($type eq '(' && $c eq ')') {
1006 $level--;
1007 $type = ($level != 0)? '(' : '';
1008
1009 if ($level == 0 && $coff < $soff) {
1010 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001011 $coff_set = 1;
1012 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001013 }
1014 }
1015 if (($type eq '' || $type eq '{') && $c eq '{') {
1016 $level++;
1017 $type = '{';
1018 }
1019 if ($type eq '{' && $c eq '}') {
1020 $level--;
1021 $type = ($level != 0)? '{' : '';
1022
1023 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001024 if (substr($blk, $off + 1, 1) eq ';') {
1025 $off++;
1026 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001027 last;
1028 }
1029 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001030 # Preprocessor commands end at the newline unless escaped.
1031 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1032 $level--;
1033 $type = '';
1034 $off++;
1035 last;
1036 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001037 $off++;
1038 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001039 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001040 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001041 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001042 $line++;
1043 $remain--;
1044 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001045
1046 my $statement = substr($blk, $soff, $off - $soff + 1);
1047 my $condition = substr($blk, $soff, $coff - $soff + 1);
1048
1049 #warn "STATEMENT<$statement>\n";
1050 #warn "CONDITION<$condition>\n";
1051
Andy Whitcroft773647a2008-03-28 14:15:58 -07001052 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001053
1054 return ($statement, $condition,
1055 $line, $remain + 1, $off - $loff + 1, $level);
1056}
1057
Andy Whitcroftcf655042008-03-04 14:28:20 -08001058sub statement_lines {
1059 my ($stmt) = @_;
1060
1061 # Strip the diff line prefixes and rip blank lines at start and end.
1062 $stmt =~ s/(^|\n)./$1/g;
1063 $stmt =~ s/^\s*//;
1064 $stmt =~ s/\s*$//;
1065
1066 my @stmt_lines = ($stmt =~ /\n/g);
1067
1068 return $#stmt_lines + 2;
1069}
1070
1071sub statement_rawlines {
1072 my ($stmt) = @_;
1073
1074 my @stmt_lines = ($stmt =~ /\n/g);
1075
1076 return $#stmt_lines + 2;
1077}
1078
1079sub statement_block_size {
1080 my ($stmt) = @_;
1081
1082 $stmt =~ s/(^|\n)./$1/g;
1083 $stmt =~ s/^\s*{//;
1084 $stmt =~ s/}\s*$//;
1085 $stmt =~ s/^\s*//;
1086 $stmt =~ s/\s*$//;
1087
1088 my @stmt_lines = ($stmt =~ /\n/g);
1089 my @stmt_statements = ($stmt =~ /;/g);
1090
1091 my $stmt_lines = $#stmt_lines + 2;
1092 my $stmt_statements = $#stmt_statements + 1;
1093
1094 if ($stmt_lines > $stmt_statements) {
1095 return $stmt_lines;
1096 } else {
1097 return $stmt_statements;
1098 }
1099}
1100
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001101sub ctx_statement_full {
1102 my ($linenr, $remain, $off) = @_;
1103 my ($statement, $condition, $level);
1104
1105 my (@chunks);
1106
Andy Whitcroftcf655042008-03-04 14:28:20 -08001107 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001108 ($statement, $condition, $linenr, $remain, $off, $level) =
1109 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001110 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001111 push(@chunks, [ $condition, $statement ]);
1112 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1113 return ($level, $linenr, @chunks);
1114 }
1115
1116 # Pull in the following conditional/block pairs and see if they
1117 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001118 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001119 ($statement, $condition, $linenr, $remain, $off, $level) =
1120 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001121 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001122 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001123 #print "C: push\n";
1124 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001125 }
1126
1127 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001128}
1129
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001130sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001131 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001132 my $line;
1133 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001134 my $blk = '';
1135 my @o;
1136 my @c;
1137 my @res = ();
1138
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001139 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001140 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001141 for ($line = $start; $remain > 0; $line++) {
1142 next if ($rawlines[$line] =~ /^-/);
1143 $remain--;
1144
1145 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001146
1147 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001148 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001149 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001150 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001151 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001152 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001153 $level = pop(@stack);
1154 }
1155
Andy Whitcroft01464f32010-10-26 14:23:19 -07001156 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001157 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1158 if ($off > 0) {
1159 $off--;
1160 next;
1161 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001162
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001163 if ($c eq $close && $level > 0) {
1164 $level--;
1165 last if ($level == 0);
1166 } elsif ($c eq $open) {
1167 $level++;
1168 }
1169 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001170
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001171 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001172 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001173 }
1174
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001175 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001176 }
1177
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001178 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001179}
1180sub ctx_block_outer {
1181 my ($linenr, $remain) = @_;
1182
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001183 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1184 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001185}
1186sub ctx_block {
1187 my ($linenr, $remain) = @_;
1188
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001189 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1190 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001191}
1192sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001193 my ($linenr, $remain, $off) = @_;
1194
1195 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1196 return @r;
1197}
1198sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001199 my ($linenr, $remain) = @_;
1200
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001201 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001202}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001203sub ctx_statement_level {
1204 my ($linenr, $remain, $off) = @_;
1205
1206 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1207}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001208
1209sub ctx_locate_comment {
1210 my ($first_line, $end_line) = @_;
1211
1212 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001213 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001214 return $current_comment if (defined $current_comment);
1215
1216 # Look through the context and try and figure out if there is a
1217 # comment.
1218 my $in_comment = 0;
1219 $current_comment = '';
1220 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001221 my $line = $rawlines[$linenr - 1];
1222 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001223 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1224 $in_comment = 1;
1225 }
1226 if ($line =~ m@/\*@) {
1227 $in_comment = 1;
1228 }
1229 if (!$in_comment && $current_comment ne '') {
1230 $current_comment = '';
1231 }
1232 $current_comment .= $line . "\n" if ($in_comment);
1233 if ($line =~ m@\*/@) {
1234 $in_comment = 0;
1235 }
1236 }
1237
1238 chomp($current_comment);
1239 return($current_comment);
1240}
1241sub ctx_has_comment {
1242 my ($first_line, $end_line) = @_;
1243 my $cmt = ctx_locate_comment($first_line, $end_line);
1244
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001245 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001246 ##print "CMMT: $cmt\n";
1247
1248 return ($cmt ne '');
1249}
1250
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001251sub raw_line {
1252 my ($linenr, $cnt) = @_;
1253
1254 my $offset = $linenr - 1;
1255 $cnt++;
1256
1257 my $line;
1258 while ($cnt) {
1259 $line = $rawlines[$offset++];
1260 next if (defined($line) && $line =~ /^-/);
1261 $cnt--;
1262 }
1263
1264 return $line;
1265}
1266
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001267sub cat_vet {
1268 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001269 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001270
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001271 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001272 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1273 $res .= $1;
1274 if ($2 ne '') {
1275 $coded = sprintf("^%c", unpack('C', $2) + 64);
1276 $res .= $coded;
1277 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001278 }
1279 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001280
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001281 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001282}
1283
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001284my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001285my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001286my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001287my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001288
1289sub annotate_reset {
1290 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001291 $av_pending = '_';
1292 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001293 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001294}
1295
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001296sub annotate_values {
1297 my ($stream, $type) = @_;
1298
1299 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001300 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001301 my $cur = $stream;
1302
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001303 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001304
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001305 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001306 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001307 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001308 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001309 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001310 print "WS($1)\n" if ($dbg_values > 1);
1311 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001312 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001313 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001314 }
1315
Florian Micklerc023e4732011-01-12 16:59:58 -08001316 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001317 print "CAST($1)\n" if ($dbg_values > 1);
1318 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001319 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001320
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001321 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001322 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001323 $type = 'T';
1324
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001325 } elsif ($cur =~ /^($Modifier)\s*/) {
1326 print "MODIFIER($1)\n" if ($dbg_values > 1);
1327 $type = 'T';
1328
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001329 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001330 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001331 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001332 push(@av_paren_type, $type);
1333 if ($2 ne '') {
1334 $av_pending = 'N';
1335 }
1336 $type = 'E';
1337
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001338 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001339 print "UNDEF($1)\n" if ($dbg_values > 1);
1340 $av_preprocessor = 1;
1341 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001342
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001343 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001344 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001345 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001346
1347 push(@av_paren_type, $type);
1348 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001349 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001350
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001351 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001352 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1353 $av_preprocessor = 1;
1354
1355 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1356
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001357 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001358
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001359 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001360 print "PRE_END($1)\n" if ($dbg_values > 1);
1361
1362 $av_preprocessor = 1;
1363
1364 # Assume all arms of the conditional end as this
1365 # one does, and continue as if the #endif was not here.
1366 pop(@av_paren_type);
1367 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001368 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001369
1370 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001372
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001373 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1374 print "ATTR($1)\n" if ($dbg_values > 1);
1375 $av_pending = $type;
1376 $type = 'N';
1377
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001378 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001379 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001380 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001381 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001382 }
1383 $type = 'N';
1384
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001385 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001386 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001387 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001388 $type = 'N';
1389
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001390 } elsif ($cur =~/^(case)/o) {
1391 print "CASE($1)\n" if ($dbg_values > 1);
1392 $av_pend_colon = 'C';
1393 $type = 'N';
1394
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001395 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001396 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001397 $type = 'N';
1398
1399 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001400 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001401 push(@av_paren_type, $av_pending);
1402 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001403 $type = 'N';
1404
1405 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001406 my $new_type = pop(@av_paren_type);
1407 if ($new_type ne '_') {
1408 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001409 print "PAREN('$1') -> $type\n"
1410 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001412 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413 }
1414
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001415 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001416 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001417 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001418 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001419
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001420 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1421 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001422 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001423 } elsif ($type eq 'E') {
1424 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001425 }
1426 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1427 $type = 'V';
1428
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001429 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001430 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001431 $type = 'V';
1432
1433 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001434 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001435 $type = 'N';
1436
Andy Whitcroftcf655042008-03-04 14:28:20 -08001437 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001438 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001439 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001440 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001441
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001442 } elsif ($cur =~/^(,)/) {
1443 print "COMMA($1)\n" if ($dbg_values > 1);
1444 $type = 'C';
1445
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001446 } elsif ($cur =~ /^(\?)/o) {
1447 print "QUESTION($1)\n" if ($dbg_values > 1);
1448 $type = 'N';
1449
1450 } elsif ($cur =~ /^(:)/o) {
1451 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1452
1453 substr($var, length($res), 1, $av_pend_colon);
1454 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1455 $type = 'E';
1456 } else {
1457 $type = 'N';
1458 }
1459 $av_pend_colon = 'O';
1460
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001461 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001462 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001463 $type = 'N';
1464
Andy Whitcroft0d413862008-10-15 22:02:16 -07001465 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001466 my $variant;
1467
1468 print "OPV($1)\n" if ($dbg_values > 1);
1469 if ($type eq 'V') {
1470 $variant = 'B';
1471 } else {
1472 $variant = 'U';
1473 }
1474
1475 substr($var, length($res), 1, $variant);
1476 $type = 'N';
1477
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001478 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001479 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001480 if ($1 ne '++' && $1 ne '--') {
1481 $type = 'N';
1482 }
1483
1484 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001485 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001486 }
1487 if (defined $1) {
1488 $cur = substr($cur, length($1));
1489 $res .= $type x length($1);
1490 }
1491 }
1492
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001493 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001494}
1495
Andy Whitcroft8905a672007-11-28 16:21:06 -08001496sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001497 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001498 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001499 ^(?:
1500 $Modifier|
1501 $Storage|
1502 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001503 DEFINE_\S+
1504 )$|
1505 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001506 goto|
1507 return|
1508 case|
1509 else|
1510 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001511 do|
1512 \#|
1513 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001514 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001515 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001516 )}x;
1517 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1518 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001519 # Check for modifiers.
1520 $possible =~ s/\s*$Storage\s*//g;
1521 $possible =~ s/\s*$Sparse\s*//g;
1522 if ($possible =~ /^\s*$/) {
1523
1524 } elsif ($possible =~ /\s/) {
1525 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001526 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001527 if ($modifier !~ $notPermitted) {
1528 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1529 push(@modifierList, $modifier);
1530 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001531 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001532
1533 } else {
1534 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1535 push(@typeList, $possible);
1536 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001537 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001538 } else {
1539 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001540 }
1541}
1542
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001543my $prefix = '';
1544
Joe Perches000d1cc12011-07-25 17:13:25 -07001545sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001546 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001547
Joe Perchescbec18a2014-04-03 14:49:19 -07001548 return defined $use_type{$type} if (scalar keys %use_type > 0);
1549
1550 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001551}
1552
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001553sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001554 my ($level, $type, $msg) = @_;
1555
1556 if (!show_type($type) ||
1557 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001558 return 0;
1559 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001560 my $line;
1561 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001562 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001563 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001564 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001565 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001566 $line = (split('\n', $line))[0] . "\n" if ($terse);
1567
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001568 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001569
1570 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001571}
Joe Perchescbec18a2014-04-03 14:49:19 -07001572
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001573sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001574 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001575}
Joe Perches000d1cc12011-07-25 17:13:25 -07001576
Joe Perchesd752fcc2014-08-06 16:11:05 -07001577sub fixup_current_range {
1578 my ($lineRef, $offset, $length) = @_;
1579
1580 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1581 my $o = $1;
1582 my $l = $2;
1583 my $no = $o + $offset;
1584 my $nl = $l + $length;
1585 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1586 }
1587}
1588
1589sub fix_inserted_deleted_lines {
1590 my ($linesRef, $insertedRef, $deletedRef) = @_;
1591
1592 my $range_last_linenr = 0;
1593 my $delta_offset = 0;
1594
1595 my $old_linenr = 0;
1596 my $new_linenr = 0;
1597
1598 my $next_insert = 0;
1599 my $next_delete = 0;
1600
1601 my @lines = ();
1602
1603 my $inserted = @{$insertedRef}[$next_insert++];
1604 my $deleted = @{$deletedRef}[$next_delete++];
1605
1606 foreach my $old_line (@{$linesRef}) {
1607 my $save_line = 1;
1608 my $line = $old_line; #don't modify the array
1609 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1610 $delta_offset = 0;
1611 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1612 $range_last_linenr = $new_linenr;
1613 fixup_current_range(\$line, $delta_offset, 0);
1614 }
1615
1616 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1617 $deleted = @{$deletedRef}[$next_delete++];
1618 $save_line = 0;
1619 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1620 }
1621
1622 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1623 push(@lines, ${$inserted}{'LINE'});
1624 $inserted = @{$insertedRef}[$next_insert++];
1625 $new_linenr++;
1626 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1627 }
1628
1629 if ($save_line) {
1630 push(@lines, $line);
1631 $new_linenr++;
1632 }
1633
1634 $old_linenr++;
1635 }
1636
1637 return @lines;
1638}
1639
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001640sub fix_insert_line {
1641 my ($linenr, $line) = @_;
1642
1643 my $inserted = {
1644 LINENR => $linenr,
1645 LINE => $line,
1646 };
1647 push(@fixed_inserted, $inserted);
1648}
1649
1650sub fix_delete_line {
1651 my ($linenr, $line) = @_;
1652
1653 my $deleted = {
1654 LINENR => $linenr,
1655 LINE => $line,
1656 };
1657
1658 push(@fixed_deleted, $deleted);
1659}
1660
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001661sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001662 my ($type, $msg) = @_;
1663
1664 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001665 our $clean = 0;
1666 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001667 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001668 }
Joe Perches3705ce52013-07-03 15:05:31 -07001669 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001670}
1671sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001672 my ($type, $msg) = @_;
1673
1674 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001675 our $clean = 0;
1676 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001677 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001678 }
Joe Perches3705ce52013-07-03 15:05:31 -07001679 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001680}
1681sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001682 my ($type, $msg) = @_;
1683
1684 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001685 our $clean = 0;
1686 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001687 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001688 }
Joe Perches3705ce52013-07-03 15:05:31 -07001689 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001690}
1691
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001692sub check_absolute_file {
1693 my ($absolute, $herecurr) = @_;
1694 my $file = $absolute;
1695
1696 ##print "absolute<$absolute>\n";
1697
1698 # See if any suffix of this path is a path within the tree.
1699 while ($file =~ s@^[^/]*/@@) {
1700 if (-f "$root/$file") {
1701 ##print "file<$file>\n";
1702 last;
1703 }
1704 }
1705 if (! -f _) {
1706 return 0;
1707 }
1708
1709 # It is, so see if the prefix is acceptable.
1710 my $prefix = $absolute;
1711 substr($prefix, -length($file)) = '';
1712
1713 ##print "prefix<$prefix>\n";
1714 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001715 WARN("USE_RELATIVE_PATH",
1716 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001717 }
1718}
1719
Joe Perches3705ce52013-07-03 15:05:31 -07001720sub trim {
1721 my ($string) = @_;
1722
Joe Perchesb34c6482013-09-11 14:24:01 -07001723 $string =~ s/^\s+|\s+$//g;
1724
1725 return $string;
1726}
1727
1728sub ltrim {
1729 my ($string) = @_;
1730
1731 $string =~ s/^\s+//;
1732
1733 return $string;
1734}
1735
1736sub rtrim {
1737 my ($string) = @_;
1738
1739 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001740
1741 return $string;
1742}
1743
Joe Perches52ea8502013-11-12 15:10:09 -08001744sub string_find_replace {
1745 my ($string, $find, $replace) = @_;
1746
1747 $string =~ s/$find/$replace/g;
1748
1749 return $string;
1750}
1751
Joe Perches3705ce52013-07-03 15:05:31 -07001752sub tabify {
1753 my ($leading) = @_;
1754
1755 my $source_indent = 8;
1756 my $max_spaces_before_tab = $source_indent - 1;
1757 my $spaces_to_tab = " " x $source_indent;
1758
1759 #convert leading spaces to tabs
1760 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1761 #Remove spaces before a tab
1762 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1763
1764 return "$leading";
1765}
1766
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001767sub pos_last_openparen {
1768 my ($line) = @_;
1769
1770 my $pos = 0;
1771
1772 my $opens = $line =~ tr/\(/\(/;
1773 my $closes = $line =~ tr/\)/\)/;
1774
1775 my $last_openparen = 0;
1776
1777 if (($opens == 0) || ($closes >= $opens)) {
1778 return -1;
1779 }
1780
1781 my $len = length($line);
1782
1783 for ($pos = 0; $pos < $len; $pos++) {
1784 my $string = substr($line, $pos);
1785 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1786 $pos += length($1) - 1;
1787 } elsif (substr($line, $pos, 1) eq '(') {
1788 $last_openparen = $pos;
1789 } elsif (index($string, '(') == -1) {
1790 last;
1791 }
1792 }
1793
Joe Perches91cb5192014-04-03 14:49:32 -07001794 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001795}
1796
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001797sub process {
1798 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001799
1800 my $linenr=0;
1801 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001802 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001803 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001804 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001805
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001806 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001807 my $indent;
1808 my $previndent=0;
1809 my $stashindent=0;
1810
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001811 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001812 my $signoff = 0;
1813 my $is_patch = 0;
1814
Joe Perches29ee1b02014-08-06 16:10:35 -07001815 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001816 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001817 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001818 my $non_utf8_charset = 0;
1819
Joe Perches365dd4e2014-08-06 16:10:42 -07001820 my $last_blank_line = 0;
1821
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001822 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001823 our $cnt_lines = 0;
1824 our $cnt_error = 0;
1825 our $cnt_warn = 0;
1826 our $cnt_chk = 0;
1827
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001828 # Trace the real file/line as we go.
1829 my $realfile = '';
1830 my $realline = 0;
1831 my $realcnt = 0;
1832 my $here = '';
1833 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001834 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001835 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001836 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001837
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001838 my $prev_values = 'E';
1839
1840 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001841 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001842 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001843 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001844 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001845
Joe Perches7e51f192013-09-11 14:23:57 -07001846 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001847
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001848 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001849 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001850 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001851 my @setup_docs = ();
1852 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001853
Joe Perchesd8b07712013-11-12 15:10:06 -08001854 my $camelcase_file_seeded = 0;
1855
Andy Whitcroft773647a2008-03-28 14:15:58 -07001856 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001857 my $line;
1858 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001859 $linenr++;
1860 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001861
Joe Perches3705ce52013-07-03 15:05:31 -07001862 push(@fixed, $rawline) if ($fix);
1863
Andy Whitcroft773647a2008-03-28 14:15:58 -07001864 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001865 $setup_docs = 0;
1866 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1867 $setup_docs = 1;
1868 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001869 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001870 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001871 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1872 $realline=$1-1;
1873 if (defined $2) {
1874 $realcnt=$3+1;
1875 } else {
1876 $realcnt=1+1;
1877 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001878 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001879
1880 # Guestimate if this is a continuing comment. Run
1881 # the context looking for a comment "edge". If this
1882 # edge is a close comment then we must be in a comment
1883 # at context start.
1884 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001885 my $cnt = $realcnt;
1886 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1887 next if (defined $rawlines[$ln - 1] &&
1888 $rawlines[$ln - 1] =~ /^-/);
1889 $cnt--;
1890 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001891 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001892 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1893 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1894 ($edge) = $1;
1895 last;
1896 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001897 }
1898 if (defined $edge && $edge eq '*/') {
1899 $in_comment = 1;
1900 }
1901
1902 # Guestimate if this is a continuing comment. If this
1903 # is the start of a diff block and this line starts
1904 # ' *' then it is very likely a comment.
1905 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001906 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001907 {
1908 $in_comment = 1;
1909 }
1910
1911 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1912 sanitise_line_reset($in_comment);
1913
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001914 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001915 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001916 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001917 $line = sanitise_line($rawline);
1918 }
1919 push(@lines, $line);
1920
1921 if ($realcnt > 1) {
1922 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1923 } else {
1924 $realcnt = 0;
1925 }
1926
1927 #print "==>$rawline\n";
1928 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001929
1930 if ($setup_docs && $line =~ /^\+/) {
1931 push(@setup_docs, $line);
1932 }
1933 }
1934
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001935 $prefix = '';
1936
Andy Whitcroft773647a2008-03-28 14:15:58 -07001937 $realcnt = 0;
1938 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001939 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001940 foreach my $line (@lines) {
1941 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001942 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001943 my $sline = $line; #copy of $line
1944 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001945
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001946 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001947
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001948#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001949 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001950 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001951 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001952 $realline=$1-1;
1953 if (defined $2) {
1954 $realcnt=$3+1;
1955 } else {
1956 $realcnt=1+1;
1957 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001958 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001959 $prev_values = 'E';
1960
Andy Whitcroft773647a2008-03-28 14:15:58 -07001961 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001962 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001963 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001964 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001965 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001966
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001967# track the line number as we move through the hunk, note that
1968# new versions of GNU diff omit the leading space on completely
1969# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001970 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001971 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001972 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001973
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001974 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001975 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001976
1977 # Track the previous line.
1978 ($prevline, $stashline) = ($stashline, $line);
1979 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001980 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1981
Andy Whitcroft773647a2008-03-28 14:15:58 -07001982 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001983
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001984 } elsif ($realcnt == 1) {
1985 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001986 }
1987
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07001988 my $hunk_line = ($realcnt != 0);
1989
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001990#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001991 $prefix = "$filename:$realline: " if ($emacs && $file);
1992 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1993
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001994 $here = "#$linenr: " if (!$file);
1995 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001996
Joe Perches2ac73b4f2014-06-04 16:12:05 -07001997 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001998 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07001999 if ($line =~ /^diff --git.*?(\S+)$/) {
2000 $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;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002003 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002004 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002005 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002006 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002007 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002008
2009 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002010 if (!$file && $tree && $p1_prefix ne '' &&
2011 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002012 WARN("PATCH_PREFIX",
2013 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002014 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002015
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002016 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002017 ERROR("MODIFIED_INCLUDE_ASM",
2018 "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 -07002019 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002020 $found_file = 1;
2021 }
2022
2023 if ($found_file) {
2024 if ($realfile =~ m@^(drivers/net/|net/)@) {
2025 $check = 1;
2026 } else {
2027 $check = $check_orig;
2028 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002029 next;
2030 }
2031
Randy Dunlap389834b2007-06-08 13:47:03 -07002032 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002033
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002034 my $hereline = "$here\n$rawline\n";
2035 my $herecurr = "$here\n$rawline\n";
2036 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002037
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002038 $cnt_lines++ if ($realcnt != 0);
2039
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002040# Check for incorrect file permissions
2041 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2042 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002043 if ($realfile !~ m@scripts/@ &&
2044 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002045 ERROR("EXECUTE_PERMISSIONS",
2046 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002047 }
2048 }
2049
Joe Perches20112472011-07-25 17:13:23 -07002050# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002051 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002052 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002053 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002054 }
2055
2056# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002057 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002058 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002059 my $space_before = $1;
2060 my $sign_off = $2;
2061 my $space_after = $3;
2062 my $email = $4;
2063 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2064
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002065 if ($sign_off !~ /$signature_tags/) {
2066 WARN("BAD_SIGN_OFF",
2067 "Non-standard signature: $sign_off\n" . $herecurr);
2068 }
Joe Perches20112472011-07-25 17:13:23 -07002069 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002070 if (WARN("BAD_SIGN_OFF",
2071 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2072 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002073 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002074 "$ucfirst_sign_off $email";
2075 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002076 }
Joe Perches20112472011-07-25 17:13:23 -07002077 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002078 if (WARN("BAD_SIGN_OFF",
2079 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2080 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002081 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002082 "$ucfirst_sign_off $email";
2083 }
2084
Joe Perches20112472011-07-25 17:13:23 -07002085 }
2086 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002087 if (WARN("BAD_SIGN_OFF",
2088 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2089 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002090 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002091 "$ucfirst_sign_off $email";
2092 }
Joe Perches20112472011-07-25 17:13:23 -07002093 }
2094
2095 my ($email_name, $email_address, $comment) = parse_email($email);
2096 my $suggested_email = format_email(($email_name, $email_address));
2097 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002098 ERROR("BAD_SIGN_OFF",
2099 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002100 } else {
2101 my $dequoted = $suggested_email;
2102 $dequoted =~ s/^"//;
2103 $dequoted =~ s/" </ </;
2104 # Don't force email to have quotes
2105 # Allow just an angle bracketed address
2106 if ("$dequoted$comment" ne $email &&
2107 "<$email_address>$comment" ne $email &&
2108 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002109 WARN("BAD_SIGN_OFF",
2110 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002111 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002112 }
Joe Perches7e51f192013-09-11 14:23:57 -07002113
2114# Check for duplicate signatures
2115 my $sig_nospace = $line;
2116 $sig_nospace =~ s/\s//g;
2117 $sig_nospace = lc($sig_nospace);
2118 if (defined $signatures{$sig_nospace}) {
2119 WARN("BAD_SIGN_OFF",
2120 "Duplicate signature\n" . $herecurr);
2121 } else {
2122 $signatures{$sig_nospace} = 1;
2123 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002124 }
2125
Joe Perches9b3189eb2014-06-04 16:12:10 -07002126# Check for old stable address
2127 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2128 ERROR("STABLE_ADDRESS",
2129 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2130 }
2131
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002132# Check for unwanted Gerrit info
2133 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2134 ERROR("GERRIT_CHANGE_ID",
2135 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2136 }
2137
Joe Perchesd311cd42014-08-06 16:10:57 -07002138# Check for improperly formed commit descriptions
2139 if ($in_commit_log &&
2140 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
Joe Perches66881732014-09-09 14:50:55 -07002141 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2142 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2143 defined $rawlines[$linenr] &&
2144 $rawlines[$linenr] =~ /^\s*\("/))) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002145 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2146 my $init_char = $1;
2147 my $orig_commit = lc($2);
2148 my $id = '01234567890ab';
2149 my $desc = 'commit description';
2150 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2151 ERROR("GIT_COMMIT_ID",
Joe Perches3f6316b42014-08-29 15:18:26 -07002152 "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
Joe Perchesd311cd42014-08-06 16:10:57 -07002153 }
2154
Joe Perches13f19372014-08-06 16:10:59 -07002155# Check for added, moved or deleted files
2156 if (!$reported_maintainer_file && !$in_commit_log &&
2157 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2158 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2159 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2160 (defined($1) || defined($2))))) {
2161 $reported_maintainer_file = 1;
2162 WARN("FILE_PATH_CHANGES",
2163 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2164 }
2165
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002166# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002167 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002168 ERROR("CORRUPTED_PATCH",
2169 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002170 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002171 }
2172
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002173# Check for absolute kernel paths.
2174 if ($tree) {
2175 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2176 my $file = $1;
2177
2178 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2179 check_absolute_file($1, $herecurr)) {
2180 #
2181 } else {
2182 check_absolute_file($file, $herecurr);
2183 }
2184 }
2185 }
2186
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002187# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2188 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002189 $rawline !~ m/^$UTF8*$/) {
2190 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2191
2192 my $blank = copy_spacing($rawline);
2193 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2194 my $hereptr = "$hereline$ptr\n";
2195
Joe Perches34d99212011-07-25 17:13:26 -07002196 CHK("INVALID_UTF8",
2197 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002198 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002199
Joe Perches15662b32011-10-31 17:13:12 -07002200# Check if it's the start of a commit log
2201# (not a header line and we haven't seen the patch filename)
2202 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002203 !($rawline =~ /^\s+\S/ ||
2204 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002205 $in_header_lines = 0;
2206 $in_commit_log = 1;
2207 }
2208
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002209# Check if there is UTF-8 in a commit log when a mail header has explicitly
2210# declined it, i.e defined some charset where it is missing.
2211 if ($in_header_lines &&
2212 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2213 $1 !~ /utf-8/i) {
2214 $non_utf8_charset = 1;
2215 }
2216
2217 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002218 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002219 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002220 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2221 }
2222
Andy Whitcroft306708542008-10-15 22:02:28 -07002223# ignore non-hunk lines and lines being removed
2224 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002225
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002226#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002227 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002228 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002229 if (ERROR("DOS_LINE_ENDINGS",
2230 "DOS line endings\n" . $herevet) &&
2231 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002232 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002233 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002234 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2235 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002236 if (ERROR("TRAILING_WHITESPACE",
2237 "trailing whitespace\n" . $herevet) &&
2238 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002239 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002240 }
2241
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002242 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002243 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002244
Josh Triplett4783f892013-11-12 15:10:12 -08002245# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002246 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002247 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2248 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002249 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2250 my $msg_type = \&ERROR;
2251 $msg_type = \&CHK if ($file);
2252 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002253 "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 -08002254 }
2255
Andi Kleen33549572010-05-24 14:33:29 -07002256# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002257# Only applies when adding the entry originally, after that we do not have
2258# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002259 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002260 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002261 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002262 my $cnt = $realcnt;
2263 my $ln = $linenr + 1;
2264 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002265 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002266 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002267 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002268 $f = $lines[$ln - 1];
2269 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2270 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002271
2272 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002273 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002274
Joe Perches8d73e0e2014-08-06 16:10:46 -07002275 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002276 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002277 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002278 $length = -1;
2279 }
2280
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002281 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002282 $f =~ s/#.*//;
2283 $f =~ s/^\s+//;
2284 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002285 if ($f =~ /^\s*config\s/) {
2286 $is_end = 1;
2287 last;
2288 }
Andi Kleen33549572010-05-24 14:33:29 -07002289 $length++;
2290 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002291 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2292 WARN("CONFIG_DESCRIPTION",
2293 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2294 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002295 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002296 }
2297
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002298# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2299 if ($realfile =~ /Kconfig/ &&
2300 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2301 WARN("CONFIG_EXPERIMENTAL",
2302 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2303 }
2304
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002305 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2306 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2307 my $flag = $1;
2308 my $replacement = {
2309 'EXTRA_AFLAGS' => 'asflags-y',
2310 'EXTRA_CFLAGS' => 'ccflags-y',
2311 'EXTRA_CPPFLAGS' => 'cppflags-y',
2312 'EXTRA_LDFLAGS' => 'ldflags-y',
2313 };
2314
2315 WARN("DEPRECATED_VARIABLE",
2316 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2317 }
2318
Rob Herringbff5da42014-01-23 15:54:51 -08002319# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002320 if (defined $root &&
2321 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2322 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2323
Rob Herringbff5da42014-01-23 15:54:51 -08002324 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2325
Florian Vaussardcc933192014-04-03 14:49:27 -07002326 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2327 my $vp_file = $dt_path . "vendor-prefixes.txt";
2328
Rob Herringbff5da42014-01-23 15:54:51 -08002329 foreach my $compat (@compats) {
2330 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002331 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2332 my $compat3 = $compat;
2333 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2334 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002335 if ( $? >> 8 ) {
2336 WARN("UNDOCUMENTED_DT_STRING",
2337 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2338 }
2339
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002340 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2341 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002342 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002343 if ( $? >> 8 ) {
2344 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002345 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002346 }
2347 }
2348 }
2349
Andy Whitcroft5368df202008-10-15 22:02:27 -07002350# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002351 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002352
Joe Perches6cd7f382012-12-17 16:01:54 -08002353#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002354 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002355 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002356 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002357 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002358 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002359 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002360 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002361 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002362 }
2363
Josh Triplettca56dc02012-03-23 15:02:21 -07002364# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002365# to grep for the string. Make exceptions when the previous string ends in a
2366# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2367# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002368 if ($line =~ /^\+\s*"/ &&
2369 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002370 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002371 WARN("SPLIT_STRING",
2372 "quoted string split across lines\n" . $hereprev);
2373 }
2374
Dan Carpenterece96592014-08-06 16:11:29 -07002375# check for missing a space in a string concatination
2376 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2377 WARN('MISSING_SPACE',
2378 "break quoted strings at a space character\n" . $hereprev);
2379 }
2380
Joe Perches5e79d962010-03-05 13:43:55 -08002381# check for spaces before a quoted newline
2382 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002383 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2384 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2385 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002386 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002387 }
2388
Joe Perches5e79d962010-03-05 13:43:55 -08002389 }
2390
Andy Whitcroft8905a672007-11-28 16:21:06 -08002391# check for adding lines without a newline.
2392 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002393 WARN("MISSING_EOF_NEWLINE",
2394 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002395 }
2396
Mike Frysinger42e41c52009-09-21 17:04:40 -07002397# Blackfin: use hi/lo macros
2398 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2399 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2400 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002401 ERROR("LO_MACRO",
2402 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002403 }
2404 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2405 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002406 ERROR("HI_MACRO",
2407 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002408 }
2409 }
2410
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002411# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002412 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002413
2414# at the beginning of a line any tabs must come first and anything
2415# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002416 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2417 $rawline =~ /^\+\s* \s*/) {
2418 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002419 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002420 if (ERROR("CODE_INDENT",
2421 "code indent should use tabs where possible\n" . $herevet) &&
2422 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002423 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002424 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002425 }
2426
Alberto Panizzo08e44362010-03-05 13:43:54 -08002427# check for space before tabs.
2428 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2429 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002430 if (WARN("SPACE_BEFORE_TAB",
2431 "please, no space before tabs\n" . $herevet) &&
2432 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002433 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002434 s/(^\+.*) {8,8}+\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002435 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002436 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002437 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002438 }
2439
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002440# check for && or || at the start of a line
2441 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2442 CHK("LOGICAL_CONTINUATIONS",
2443 "Logical continuations should be on the previous line\n" . $hereprev);
2444 }
2445
2446# check multi-line statement indentation matches previous line
2447 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002448 $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 -07002449 $prevline =~ /^\+(\t*)(.*)$/;
2450 my $oldindent = $1;
2451 my $rest = $2;
2452
2453 my $pos = pos_last_openparen($rest);
2454 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002455 $line =~ /^(\+| )([ \t]*)/;
2456 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002457
2458 my $goodtabindent = $oldindent .
2459 "\t" x ($pos / 8) .
2460 " " x ($pos % 8);
2461 my $goodspaceindent = $oldindent . " " x $pos;
2462
2463 if ($newindent ne $goodtabindent &&
2464 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002465
2466 if (CHK("PARENTHESIS_ALIGNMENT",
2467 "Alignment should match open parenthesis\n" . $hereprev) &&
2468 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002469 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002470 s/^\+[ \t]*/\+$goodtabindent/;
2471 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002472 }
2473 }
2474 }
2475
Joe Perches308cc8d2014-08-06 16:11:27 -07002476 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002477 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002478 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002479 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002480 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002481 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002482 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002483 }
2484
Joe Perches05880602012-10-04 17:13:35 -07002485 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002486 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002487 $rawline =~ /^\+[ \t]*\*/ &&
2488 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002489 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2490 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2491 }
2492
2493 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002494 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2495 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002496 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002497 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2498 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2499 "networking block comments start with * on subsequent lines\n" . $hereprev);
2500 }
2501
2502 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002503 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2504 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2505 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2506 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002507 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2508 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2509 }
2510
Joe Perches7f619192014-08-06 16:10:39 -07002511# check for missing blank lines after struct/union declarations
2512# with exceptions for various attributes and macros
2513 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2514 $line =~ /^\+/ &&
2515 !($line =~ /^\+\s*$/ ||
2516 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2517 $line =~ /^\+\s*MODULE_/i ||
2518 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2519 $line =~ /^\+[a-z_]*init/ ||
2520 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2521 $line =~ /^\+\s*DECLARE/ ||
2522 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002523 if (CHK("LINE_SPACING",
2524 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2525 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002526 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002527 }
Joe Perches7f619192014-08-06 16:10:39 -07002528 }
2529
Joe Perches365dd4e2014-08-06 16:10:42 -07002530# check for multiple consecutive blank lines
2531 if ($prevline =~ /^[\+ ]\s*$/ &&
2532 $line =~ /^\+\s*$/ &&
2533 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002534 if (CHK("LINE_SPACING",
2535 "Please don't use multiple blank lines\n" . $hereprev) &&
2536 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002537 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002538 }
2539
Joe Perches365dd4e2014-08-06 16:10:42 -07002540 $last_blank_line = $linenr;
2541 }
2542
Joe Perches3b617e32014-04-03 14:49:28 -07002543# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002544 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2545 # actual declarations
2546 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002547 # function pointer declarations
2548 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002549 # foo bar; where foo is some local typedef or #define
2550 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2551 # known declaration macros
2552 $prevline =~ /^\+\s+$declaration_macros/) &&
2553 # for "else if" which can look like "$Ident $Ident"
2554 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2555 # other possible extensions of declaration lines
2556 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2557 # not starting a section or a macro "\" extended line
2558 $prevline =~ /(?:\{\s*|\\)$/) &&
2559 # looks like a declaration
2560 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002561 # function pointer declarations
2562 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002563 # foo bar; where foo is some local typedef or #define
2564 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2565 # known declaration macros
2566 $sline =~ /^\+\s+$declaration_macros/ ||
2567 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002568 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002569 # start or end of block or continuation of declaration
2570 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2571 # bitfield continuation
2572 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2573 # other possible extensions of declaration lines
2574 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2575 # indentation of previous and current line are the same
2576 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002577 if (WARN("LINE_SPACING",
2578 "Missing a blank line after declarations\n" . $hereprev) &&
2579 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002580 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002581 }
Joe Perches3b617e32014-04-03 14:49:28 -07002582 }
2583
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002584# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002585# Exceptions:
2586# 1) within comments
2587# 2) indented preprocessor commands
2588# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002589 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002590 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002591 if (WARN("LEADING_SPACE",
2592 "please, no spaces at the start of a line\n" . $herevet) &&
2593 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002594 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002595 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002596 }
2597
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002598# check we are in a valid C source file if not then ignore this hunk
2599 next if ($realfile !~ /\.(h|c)$/);
2600
Joe Perches032a4c02014-08-06 16:10:29 -07002601# check indentation of any line with a bare else
2602# if the previous line is a break or return and is indented 1 tab more...
2603 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2604 my $tabs = length($1) + 1;
2605 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) {
2606 WARN("UNNECESSARY_ELSE",
2607 "else is not generally useful after a break or return\n" . $hereprev);
2608 }
2609 }
2610
Joe Perchesc00df192014-08-06 16:11:01 -07002611# check indentation of a line with a break;
2612# if the previous line is a goto or return and is indented the same # of tabs
2613 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2614 my $tabs = $1;
2615 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2616 WARN("UNNECESSARY_BREAK",
2617 "break is not useful after a goto or return\n" . $hereprev);
2618 }
2619 }
2620
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002621# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2622 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2623 WARN("CONFIG_EXPERIMENTAL",
2624 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2625 }
2626
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002627# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002628 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002629 WARN("CVS_KEYWORD",
2630 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002631 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002632
Mike Frysinger42e41c52009-09-21 17:04:40 -07002633# Blackfin: don't use __builtin_bfin_[cs]sync
2634 if ($line =~ /__builtin_bfin_csync/) {
2635 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002636 ERROR("CSYNC",
2637 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002638 }
2639 if ($line =~ /__builtin_bfin_ssync/) {
2640 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002641 ERROR("SSYNC",
2642 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002643 }
2644
Joe Perches56e77d72013-02-21 16:44:14 -08002645# check for old HOTPLUG __dev<foo> section markings
2646 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2647 WARN("HOTPLUG_SECTION",
2648 "Using $1 is unnecessary\n" . $herecurr);
2649 }
2650
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002651# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002652 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2653 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002654#print "LINE<$line>\n";
2655 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002656 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002657 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002658 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002659 $stat =~ s/\n./\n /g;
2660 $cond =~ s/\n./\n /g;
2661
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002662#print "linenr<$linenr> <$stat>\n";
2663 # If this statement has no statement boundaries within
2664 # it there is no point in retrying a statement scan
2665 # until we hit end of it.
2666 my $frag = $stat; $frag =~ s/;+\s*$//;
2667 if ($frag !~ /(?:{|;)/) {
2668#print "skip<$line_nr_next>\n";
2669 $suppress_statement = $line_nr_next;
2670 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002671
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002672 # Find the real next line.
2673 $realline_next = $line_nr_next;
2674 if (defined $realline_next &&
2675 (!defined $lines[$realline_next - 1] ||
2676 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2677 $realline_next++;
2678 }
2679
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002680 my $s = $stat;
2681 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002682
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002683 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002684 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002685
2686 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002687 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002688
Andy Whitcroft463f2862009-09-21 17:04:34 -07002689 } elsif ($s =~ /^.\s*else\b/s) {
2690
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002691 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002692 } 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 -07002693 my $type = $1;
2694 $type =~ s/\s+/ /g;
2695 possible($type, "A:" . $s);
2696
Andy Whitcroft8905a672007-11-28 16:21:06 -08002697 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002698 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002699 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002700 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002701
2702 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002703 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002704 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002705 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002706
2707 # Check for any sort of function declaration.
2708 # int foo(something bar, other baz);
2709 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002710 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 -08002711 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002712
Andy Whitcroftcf655042008-03-04 14:28:20 -08002713 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002714 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002715 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002716
Andy Whitcroft8905a672007-11-28 16:21:06 -08002717 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002718 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002719
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002720 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002721 }
2722 }
2723 }
2724
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002725 }
2726
Andy Whitcroft653d4872007-06-23 17:16:34 -07002727#
2728# Checks which may be anchored in the context.
2729#
2730
2731# Check for switch () and associated case and default
2732# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002733 if ($line=~/\bswitch\s*\(.*\)/) {
2734 my $err = '';
2735 my $sep = '';
2736 my @ctx = ctx_block_outer($linenr, $realcnt);
2737 shift(@ctx);
2738 for my $ctx (@ctx) {
2739 my ($clen, $cindent) = line_stats($ctx);
2740 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2741 $indent != $cindent) {
2742 $err .= "$sep$ctx\n";
2743 $sep = '';
2744 } else {
2745 $sep = "[...]\n";
2746 }
2747 }
2748 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002749 ERROR("SWITCH_CASE_INDENT_LEVEL",
2750 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002751 }
2752 }
2753
2754# if/while/etc brace do not go on next line, unless defining a do while loop,
2755# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002756 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 -07002757 my $pre_ctx = "$1$2";
2758
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002759 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002760
2761 if ($line =~ /^\+\t{6,}/) {
2762 WARN("DEEP_INDENTATION",
2763 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2764 }
2765
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002766 my $ctx_cnt = $realcnt - $#ctx - 1;
2767 my $ctx = join("\n", @ctx);
2768
Andy Whitcroft548596d2008-07-23 21:29:01 -07002769 my $ctx_ln = $linenr;
2770 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002771
Andy Whitcroft548596d2008-07-23 21:29:01 -07002772 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2773 defined $lines[$ctx_ln - 1] &&
2774 $lines[$ctx_ln - 1] =~ /^-/)) {
2775 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2776 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002777 $ctx_ln++;
2778 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002779
Andy Whitcroft53210162008-07-23 21:29:03 -07002780 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2781 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002782
Joe Perchesd752fcc2014-08-06 16:11:05 -07002783 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002784 ERROR("OPEN_BRACE",
2785 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002786 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002787 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002788 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2789 $ctx =~ /\)\s*\;\s*$/ &&
2790 defined $lines[$ctx_ln - 1])
2791 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002792 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2793 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002794 WARN("TRAILING_SEMICOLON",
2795 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002796 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002797 }
2798 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002799 }
2800
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002801# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002802 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 -08002803 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2804 ctx_statement_block($linenr, $realcnt, 0)
2805 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002806 my ($s, $c) = ($stat, $cond);
2807
2808 substr($s, 0, length($c), '');
2809
2810 # Make sure we remove the line prefixes as we have
2811 # none on the first line, and are going to readd them
2812 # where necessary.
2813 $s =~ s/\n./\n/gs;
2814
2815 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002816 my @newlines = ($c =~ /\n/gs);
2817 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002818
2819 # We want to check the first line inside the block
2820 # starting at the end of the conditional, so remove:
2821 # 1) any blank line termination
2822 # 2) any opening brace { on end of the line
2823 # 3) any do (...) {
2824 my $continuation = 0;
2825 my $check = 0;
2826 $s =~ s/^.*\bdo\b//;
2827 $s =~ s/^\s*{//;
2828 if ($s =~ s/^\s*\\//) {
2829 $continuation = 1;
2830 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002831 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002832 $check = 1;
2833 $cond_lines++;
2834 }
2835
2836 # Also ignore a loop construct at the end of a
2837 # preprocessor statement.
2838 if (($prevline =~ /^.\s*#\s*define\s/ ||
2839 $prevline =~ /\\\s*$/) && $continuation == 0) {
2840 $check = 0;
2841 }
2842
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002843 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002844 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002845 while ($cond_ptr != $cond_lines) {
2846 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002847
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002848 # If we see an #else/#elif then the code
2849 # is not linear.
2850 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2851 $check = 0;
2852 }
2853
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002854 # Ignore:
2855 # 1) blank lines, they should be at 0,
2856 # 2) preprocessor lines, and
2857 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002858 if ($continuation ||
2859 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002860 $s =~ /^\s*#\s*?/ ||
2861 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002862 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002863 if ($s =~ s/^.*?\n//) {
2864 $cond_lines++;
2865 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002866 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002867 }
2868
2869 my (undef, $sindent) = line_stats("+" . $s);
2870 my $stat_real = raw_line($linenr, $cond_lines);
2871
2872 # Check if either of these lines are modified, else
2873 # this is not this patch's fault.
2874 if (!defined($stat_real) ||
2875 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2876 $check = 0;
2877 }
2878 if (defined($stat_real) && $cond_lines > 1) {
2879 $stat_real = "[...]\n$stat_real";
2880 }
2881
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002882 #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 -07002883
2884 if ($check && (($sindent % 8) != 0 ||
2885 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002886 WARN("SUSPECT_CODE_INDENT",
2887 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002888 }
2889 }
2890
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002891 # Track the 'values' across context and added lines.
2892 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002893 my ($curr_values, $curr_vars) =
2894 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002895 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002896 if ($dbg_values) {
2897 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002898 print "$linenr > .$outline\n";
2899 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002900 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002901 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002902 $prev_values = substr($curr_values, -1);
2903
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002904#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002905 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002906
Andy Whitcroft653d4872007-06-23 17:16:34 -07002907# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002908 if ($dbg_type) {
2909 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002910 ERROR("TEST_TYPE",
2911 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002912 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002913 ERROR("TEST_NOT_TYPE",
2914 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002915 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002916 next;
2917 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002918# TEST: allow direct testing of the attribute matcher.
2919 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002920 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002921 ERROR("TEST_ATTR",
2922 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002923 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002924 ERROR("TEST_NOT_ATTR",
2925 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002926 }
2927 next;
2928 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002929
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002930# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002931 if ($line =~ /^.\s*{/ &&
2932 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002933 if (ERROR("OPEN_BRACE",
2934 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002935 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2936 fix_delete_line($fixlinenr - 1, $prevrawline);
2937 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002938 my $fixedline = $prevrawline;
2939 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002940 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002941 $fixedline = $line;
2942 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002943 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002944 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002945 }
2946
Andy Whitcroft653d4872007-06-23 17:16:34 -07002947#
2948# Checks which are anchored on the added line.
2949#
2950
2951# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002952 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07002953 my $path = $1;
2954 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002955 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08002956 "malformed #include filename\n" . $herecurr);
2957 }
2958 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2959 ERROR("UAPI_INCLUDE",
2960 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002961 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002962 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002963
2964# no C99 // comments
2965 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07002966 if (ERROR("C99_COMMENTS",
2967 "do not use C99 // comments\n" . $herecurr) &&
2968 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002969 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07002970 if ($line =~ /\/\/(.*)$/) {
2971 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07002972 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07002973 }
2974 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002975 }
2976 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002977 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002978 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002979
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002980# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2981# the whole statement.
2982#print "APW <$lines[$realline_next - 1]>\n";
2983 if (defined $realline_next &&
2984 exists $lines[$realline_next - 1] &&
2985 !defined $suppress_export{$realline_next} &&
2986 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2987 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002988 # Handle definitions which produce identifiers with
2989 # a prefix:
2990 # XXX(foo);
2991 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07002992 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08002993 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07002994 $name =~ /^${Ident}_$2/) {
2995#print "FOO C name<$name>\n";
2996 $suppress_export{$realline_next} = 1;
2997
2998 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002999 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003000 ^.DEFINE_$Ident\(\Q$name\E\)|
3001 ^.DECLARE_$Ident\(\Q$name\E\)|
3002 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003003 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3004 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003005 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003006#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3007 $suppress_export{$realline_next} = 2;
3008 } else {
3009 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003010 }
3011 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003012 if (!defined $suppress_export{$linenr} &&
3013 $prevline =~ /^.\s*$/ &&
3014 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3015 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3016#print "FOO B <$lines[$linenr - 1]>\n";
3017 $suppress_export{$linenr} = 2;
3018 }
3019 if (defined $suppress_export{$linenr} &&
3020 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003021 WARN("EXPORT_SYMBOL",
3022 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003023 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003024
Joe Eloff5150bda2010-08-09 17:21:00 -07003025# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003026 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3027 if (ERROR("GLOBAL_INITIALISERS",
3028 "do not initialise globals to 0 or NULL\n" .
3029 $herecurr) &&
3030 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003031 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003032 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003033 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003034# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003035 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3036 if (ERROR("INITIALISED_STATIC",
3037 "do not initialise statics to 0 or NULL\n" .
3038 $herecurr) &&
3039 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003040 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003041 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003042 }
3043
Joe Perches18130872014-08-06 16:11:22 -07003044# check for misordered declarations of char/short/int/long with signed/unsigned
3045 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3046 my $tmp = trim($1);
3047 WARN("MISORDERED_TYPE",
3048 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3049 }
3050
Joe Perchescb710ec2010-10-26 14:23:20 -07003051# check for static const char * arrays.
3052 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003053 WARN("STATIC_CONST_CHAR_ARRAY",
3054 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003055 $herecurr);
3056 }
3057
3058# check for static char foo[] = "bar" declarations.
3059 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003060 WARN("STATIC_CONST_CHAR_ARRAY",
3061 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003062 $herecurr);
3063 }
3064
Joe Perches9b0fa602014-04-03 14:49:18 -07003065# check for non-global char *foo[] = {"bar", ...} declarations.
3066 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3067 WARN("STATIC_CONST_CHAR_ARRAY",
3068 "char * array declaration might be better as static const\n" .
3069 $herecurr);
3070 }
3071
Joe Perchesb36190c2014-01-27 17:07:18 -08003072# check for function declarations without arguments like "int foo()"
3073 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3074 if (ERROR("FUNCTION_WITHOUT_ARGS",
3075 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3076 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003077 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003078 }
3079 }
3080
Joe Perches92e112f2013-12-13 11:36:22 -07003081# check for uses of DEFINE_PCI_DEVICE_TABLE
3082 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3083 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3084 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3085 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003086 $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 -07003087 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003088 }
3089
Andy Whitcroft653d4872007-06-23 17:16:34 -07003090# check for new typedefs, only function parameters and sparse annotations
3091# make sense.
3092 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003093 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003094 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003095 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003096 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003097 WARN("NEW_TYPEDEFS",
3098 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003099 }
3100
3101# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003102 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003103 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3104 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003105 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003106
Andy Whitcroft65863862009-01-06 14:41:21 -08003107 # Should start with a space.
3108 $to =~ s/^(\S)/ $1/;
3109 # Should not end with a space.
3110 $to =~ s/\s+$//;
3111 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003112 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003113 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003114
Joe Perches3705ce52013-07-03 15:05:31 -07003115## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003116 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003117 if (ERROR("POINTER_LOCATION",
3118 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3119 $fix) {
3120 my $sub_from = $ident;
3121 my $sub_to = $ident;
3122 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003123 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003124 s@\Q$sub_from\E@$sub_to@;
3125 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003126 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003127 }
3128 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3129 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003130 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003131
Andy Whitcroft65863862009-01-06 14:41:21 -08003132 # Should start with a space.
3133 $to =~ s/^(\S)/ $1/;
3134 # Should not end with a space.
3135 $to =~ s/\s+$//;
3136 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003137 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003138 }
3139 # Modifiers should have spaces.
3140 $to =~ s/(\b$Modifier$)/$1 /;
3141
Joe Perches3705ce52013-07-03 15:05:31 -07003142## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003143 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003144 if (ERROR("POINTER_LOCATION",
3145 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3146 $fix) {
3147
3148 my $sub_from = $match;
3149 my $sub_to = $match;
3150 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003151 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003152 s@\Q$sub_from\E@$sub_to@;
3153 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003154 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003155 }
3156
3157# # no BUG() or BUG_ON()
3158# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3159# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3160# print "$herecurr";
3161# $clean = 0;
3162# }
3163
Andy Whitcroft8905a672007-11-28 16:21:06 -08003164 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003165 WARN("LINUX_VERSION_CODE",
3166 "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 -08003167 }
3168
Joe Perches17441222011-06-15 15:08:17 -07003169# check for uses of printk_ratelimit
3170 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003171 WARN("PRINTK_RATELIMITED",
3172"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003173 }
3174
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003175# printk should use KERN_* levels. Note that follow on printk's on the
3176# same line do not need a level, so we use the current block context
3177# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003178# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003179# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003180 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003181 my $ok = 0;
3182 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3183 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003184 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003185 # with "\n" ignore it, else it is to blame
3186 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3187 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3188 $ok = 1;
3189 }
3190 last;
3191 }
3192 }
3193 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003194 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3195 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003196 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003197 }
3198
Joe Perches243f3802012-05-31 16:26:09 -07003199 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3200 my $orig = $1;
3201 my $level = lc($orig);
3202 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003203 my $level2 = $level;
3204 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003205 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003206 "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 -07003207 }
3208
3209 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003210 if (WARN("PREFER_PR_LEVEL",
3211 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3212 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003213 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003214 s/\bpr_warning\b/pr_warn/;
3215 }
Joe Perches243f3802012-05-31 16:26:09 -07003216 }
3217
Joe Perchesdc139312013-02-21 16:44:13 -08003218 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3219 my $orig = $1;
3220 my $level = lc($orig);
3221 $level = "warn" if ($level eq "warning");
3222 $level = "dbg" if ($level eq "debug");
3223 WARN("PREFER_DEV_LEVEL",
3224 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3225 }
3226
Andy Whitcroft653d4872007-06-23 17:16:34 -07003227# function brace can't be on same line, except for #defines of do while,
3228# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003229 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003230 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003231 if (ERROR("OPEN_BRACE",
3232 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3233 $fix) {
3234 fix_delete_line($fixlinenr, $rawline);
3235 my $fixed_line = $rawline;
3236 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3237 my $line1 = $1;
3238 my $line2 = $2;
3239 fix_insert_line($fixlinenr, ltrim($line1));
3240 fix_insert_line($fixlinenr, "\+{");
3241 if ($line2 !~ /^\s*$/) {
3242 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3243 }
3244 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003245 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003246
Andy Whitcroft8905a672007-11-28 16:21:06 -08003247# open braces for enum, union and struct go on the same line.
3248 if ($line =~ /^.\s*{/ &&
3249 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003250 if (ERROR("OPEN_BRACE",
3251 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3252 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3253 fix_delete_line($fixlinenr - 1, $prevrawline);
3254 fix_delete_line($fixlinenr, $rawline);
3255 my $fixedline = rtrim($prevrawline) . " {";
3256 fix_insert_line($fixlinenr, $fixedline);
3257 $fixedline = $rawline;
3258 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3259 if ($fixedline !~ /^\+\s*$/) {
3260 fix_insert_line($fixlinenr, $fixedline);
3261 }
3262 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003263 }
3264
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003265# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003266 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3267 if (WARN("SPACING",
3268 "missing space after $1 definition\n" . $herecurr) &&
3269 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003270 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003271 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3272 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003273 }
3274
Joe Perches31070b52014-01-23 15:54:49 -08003275# Function pointer declarations
3276# check spacing between type, funcptr, and args
3277# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003278 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003279 my $declare = $1;
3280 my $pre_pointer_space = $2;
3281 my $post_pointer_space = $3;
3282 my $funcname = $4;
3283 my $post_funcname_space = $5;
3284 my $pre_args_space = $6;
3285
Joe Perches91f72e92014-04-03 14:49:12 -07003286# the $Declare variable will capture all spaces after the type
3287# so check it for a missing trailing missing space but pointer return types
3288# don't need a space so don't warn for those.
3289 my $post_declare_space = "";
3290 if ($declare =~ /(\s+)$/) {
3291 $post_declare_space = $1;
3292 $declare = rtrim($declare);
3293 }
3294 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003295 WARN("SPACING",
3296 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003297 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003298 }
3299
3300# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003301# This test is not currently implemented because these declarations are
3302# equivalent to
3303# int foo(int bar, ...)
3304# and this is form shouldn't/doesn't generate a checkpatch warning.
3305#
3306# elsif ($declare =~ /\s{2,}$/) {
3307# WARN("SPACING",
3308# "Multiple spaces after return type\n" . $herecurr);
3309# }
Joe Perches31070b52014-01-23 15:54:49 -08003310
3311# unnecessary space "type ( *funcptr)(args...)"
3312 if (defined $pre_pointer_space &&
3313 $pre_pointer_space =~ /^\s/) {
3314 WARN("SPACING",
3315 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3316 }
3317
3318# unnecessary space "type (* funcptr)(args...)"
3319 if (defined $post_pointer_space &&
3320 $post_pointer_space =~ /^\s/) {
3321 WARN("SPACING",
3322 "Unnecessary space before function pointer name\n" . $herecurr);
3323 }
3324
3325# unnecessary space "type (*funcptr )(args...)"
3326 if (defined $post_funcname_space &&
3327 $post_funcname_space =~ /^\s/) {
3328 WARN("SPACING",
3329 "Unnecessary space after function pointer name\n" . $herecurr);
3330 }
3331
3332# unnecessary space "type (*funcptr) (args...)"
3333 if (defined $pre_args_space &&
3334 $pre_args_space =~ /^\s/) {
3335 WARN("SPACING",
3336 "Unnecessary space before function pointer arguments\n" . $herecurr);
3337 }
3338
3339 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003340 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003341 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003342 }
3343 }
3344
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003345# check for spacing round square brackets; allowed:
3346# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003347# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3348# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003349 while ($line =~ /(.*?\s)\[/g) {
3350 my ($where, $prefix) = ($-[1], $1);
3351 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003352 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003353 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003354 if (ERROR("BRACKET_SPACE",
3355 "space prohibited before open square bracket '['\n" . $herecurr) &&
3356 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003357 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003358 s/^(\+.*?)\s+\[/$1\[/;
3359 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003360 }
3361 }
3362
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003363# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003364 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003365 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003366 my $ctx_before = substr($line, 0, $-[1]);
3367 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003368
3369 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003370 if ($name =~ /^(?:
3371 if|for|while|switch|return|case|
3372 volatile|__volatile__|
3373 __attribute__|format|__extension__|
3374 asm|__asm__)$/x)
3375 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003376 # cpp #define statements have non-optional spaces, ie
3377 # if there is a space between the name and the open
3378 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003379 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003380
3381 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003382 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003383
3384 # If this whole things ends with a type its most
3385 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003386 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003387
3388 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003389 if (WARN("SPACING",
3390 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3391 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003392 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003393 s/\b$name\s+\(/$name\(/;
3394 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003395 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003396 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003397
Andy Whitcroft653d4872007-06-23 17:16:34 -07003398# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003399 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003400 my $fixed_line = "";
3401 my $line_fixed = 0;
3402
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003403 my $ops = qr{
3404 <<=|>>=|<=|>=|==|!=|
3405 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3406 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003407 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003408 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003409 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003410 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003411
3412## print("element count: <" . $#elements . ">\n");
3413## foreach my $el (@elements) {
3414## print("el: <$el>\n");
3415## }
3416
3417 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003418 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003419
Joe Perches3705ce52013-07-03 15:05:31 -07003420 foreach my $el (@elements) {
3421 push(@fix_elements, substr($rawline, $off, length($el)));
3422 $off += length($el);
3423 }
3424
3425 $off = 0;
3426
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003427 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003428 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003429
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003430 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003431
3432 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3433
3434## print("n: <$n> good: <$good>\n");
3435
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003436 $off += length($elements[$n]);
3437
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003438 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003439 my $ca = substr($opline, 0, $off);
3440 my $cc = '';
3441 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3442 $cc = substr($opline, $off + length($elements[$n + 1]));
3443 }
3444 my $cb = "$ca$;$cc";
3445
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003446 my $a = '';
3447 $a = 'V' if ($elements[$n] ne '');
3448 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003449 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003450 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3451 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003452 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003453
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003454 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003455
3456 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003457 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003458 $c = 'V' if ($elements[$n + 2] ne '');
3459 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003460 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003461 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3462 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003463 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003464 } else {
3465 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003466 }
3467
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003468 my $ctx = "${a}x${c}";
3469
3470 my $at = "(ctx:$ctx)";
3471
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003472 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003473 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003474
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003475 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003476 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003477
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003478 # Get the full operator variant.
3479 my $opv = $op . substr($curr_vars, $off, 1);
3480
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003481 # Ignore operators passed as parameters.
3482 if ($op_type ne 'V' &&
3483 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3484
Andy Whitcroftcf655042008-03-04 14:28:20 -08003485# # Ignore comments
3486# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003487
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003488 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003489 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003490 if ($ctx !~ /.x[WEBC]/ &&
3491 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003492 if (ERROR("SPACING",
3493 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003494 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003495 $line_fixed = 1;
3496 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003497 }
3498
3499 # // is a comment
3500 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003501
Joe Perchesb00e4812014-04-03 14:49:33 -07003502 # : when part of a bitfield
3503 } elsif ($opv eq ':B') {
3504 # skip the bitfield test for now
3505
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003506 # No spaces for:
3507 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003508 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003509 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003510 if (ERROR("SPACING",
3511 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003512 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003513 if (defined $fix_elements[$n + 2]) {
3514 $fix_elements[$n + 2] =~ s/^\s+//;
3515 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003516 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003517 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003518 }
3519
3520 # , must have a space on the right.
3521 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003522 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003523 if (ERROR("SPACING",
3524 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003525 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003526 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003527 $last_after = $n;
Joe Perches3705ce52013-07-03 15:05:31 -07003528 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003529 }
3530
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003531 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003532 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003533 #warn "'*' is part of type\n";
3534
3535 # unary operators should have a space before and
3536 # none after. May be left adjacent to another
3537 # unary operator, or a cast
3538 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003539 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003540 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003541 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003542 if (ERROR("SPACING",
3543 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003544 if ($n != $last_after + 2) {
3545 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3546 $line_fixed = 1;
3547 }
Joe Perches3705ce52013-07-03 15:05:31 -07003548 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003549 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003550 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003551 # A unary '*' may be const
3552
3553 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003554 if (ERROR("SPACING",
3555 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003556 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003557 if (defined $fix_elements[$n + 2]) {
3558 $fix_elements[$n + 2] =~ s/^\s+//;
3559 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003560 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003561 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003562 }
3563
3564 # unary ++ and unary -- are allowed no space on one side.
3565 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003566 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003567 if (ERROR("SPACING",
3568 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003569 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003570 $line_fixed = 1;
3571 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003572 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003573 if ($ctx =~ /Wx[BE]/ ||
3574 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003575 if (ERROR("SPACING",
3576 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003577 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003578 $line_fixed = 1;
3579 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003580 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003581 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003582 if (ERROR("SPACING",
3583 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003584 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003585 if (defined $fix_elements[$n + 2]) {
3586 $fix_elements[$n + 2] =~ s/^\s+//;
3587 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003588 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003589 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003590 }
3591
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003592 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003593 } elsif ($op eq '<<' or $op eq '>>' or
3594 $op eq '&' or $op eq '^' or $op eq '|' or
3595 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003596 $op eq '*' or $op eq '/' or
3597 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003598 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003599 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003600 if (ERROR("SPACING",
3601 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003602 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3603 if (defined $fix_elements[$n + 2]) {
3604 $fix_elements[$n + 2] =~ s/^\s+//;
3605 }
Joe Perches3705ce52013-07-03 15:05:31 -07003606 $line_fixed = 1;
3607 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003608 }
3609
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003610 # A colon needs no spaces before when it is
3611 # terminating a case value or a label.
3612 } elsif ($opv eq ':C' || $opv eq ':L') {
3613 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003614 if (ERROR("SPACING",
3615 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003616 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003617 $line_fixed = 1;
3618 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003619 }
3620
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003621 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003622 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003623 my $ok = 0;
3624
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003625 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003626 if (($op eq '<' &&
3627 $cc =~ /^\S+\@\S+>/) ||
3628 ($op eq '>' &&
3629 $ca =~ /<\S+\@\S+$/))
3630 {
3631 $ok = 1;
3632 }
3633
Joe Perches84731622013-11-12 15:10:05 -08003634 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003635 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003636 my $msg_type = \&ERROR;
3637 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3638
3639 if (&{$msg_type}("SPACING",
3640 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003641 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3642 if (defined $fix_elements[$n + 2]) {
3643 $fix_elements[$n + 2] =~ s/^\s+//;
3644 }
Joe Perches3705ce52013-07-03 15:05:31 -07003645 $line_fixed = 1;
3646 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003647 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003648 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003649 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003650
3651## print("n: <$n> GOOD: <$good>\n");
3652
3653 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003654 }
Joe Perches3705ce52013-07-03 15:05:31 -07003655
3656 if (($#elements % 2) == 0) {
3657 $fixed_line = $fixed_line . $fix_elements[$#elements];
3658 }
3659
Joe Perches194f66f2014-08-06 16:11:03 -07003660 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3661 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003662 }
3663
3664
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003665 }
3666
Joe Perches786b6322013-07-03 15:05:32 -07003667# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003668 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003669 if (WARN("SPACING",
3670 "space prohibited before semicolon\n" . $herecurr) &&
3671 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003672 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003673 s/^(\+.*\S)\s+;/$1;/;
3674 }
3675 }
3676
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003677# check for multiple assignments
3678 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003679 CHK("MULTIPLE_ASSIGNMENTS",
3680 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003681 }
3682
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003683## # check for multiple declarations, allowing for a function declaration
3684## # continuation.
3685## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3686## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3687##
3688## # Remove any bracketed sections to ensure we do not
3689## # falsly report the parameters of functions.
3690## my $ln = $line;
3691## while ($ln =~ s/\([^\(\)]*\)//g) {
3692## }
3693## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003694## WARN("MULTIPLE_DECLARATION",
3695## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003696## }
3697## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003698
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003699#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003700 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3701 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003702 if (ERROR("SPACING",
3703 "space required before the open brace '{'\n" . $herecurr) &&
3704 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003705 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003706 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003707 }
3708
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003709## # check for blank lines before declarations
3710## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3711## $prevrawline =~ /^.\s*$/) {
3712## WARN("SPACING",
3713## "No blank lines before declarations\n" . $hereprev);
3714## }
3715##
3716
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003717# closing brace should have a space following it when it has anything
3718# on the line
3719 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003720 if (ERROR("SPACING",
3721 "space required after that close brace '}'\n" . $herecurr) &&
3722 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003723 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003724 s/}((?!(?:,|;|\)))\S)/} $1/;
3725 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003726 }
3727
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003728# check spacing on square brackets
3729 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003730 if (ERROR("SPACING",
3731 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3732 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003733 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003734 s/\[\s+/\[/;
3735 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003736 }
3737 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003738 if (ERROR("SPACING",
3739 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3740 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003741 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003742 s/\s+\]/\]/;
3743 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003744 }
3745
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003746# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003747 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3748 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003749 if (ERROR("SPACING",
3750 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3751 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003752 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003753 s/\(\s+/\(/;
3754 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003755 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003756 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003757 $line !~ /for\s*\(.*;\s+\)/ &&
3758 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003759 if (ERROR("SPACING",
3760 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003762 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003763 s/\s+\)/\)/;
3764 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003765 }
3766
Joe Perchese2826fd2014-08-06 16:10:48 -07003767# check unnecessary parentheses around addressof/dereference single $Lvals
3768# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3769
3770 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3771 CHK("UNNECESSARY_PARENTHESES",
3772 "Unnecessary parentheses around $1\n" . $herecurr);
3773 }
3774
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003775#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003776 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003777 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003778 if (WARN("INDENTED_LABEL",
3779 "labels should not be indented\n" . $herecurr) &&
3780 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003781 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003782 s/^(.)\s+/$1/;
3783 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003784 }
3785
Joe Perches5b9553a2014-04-03 14:49:21 -07003786# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003787 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003788 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003789 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003790 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3791 my $value = $1;
3792 $value = deparenthesize($value);
3793 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3794 ERROR("RETURN_PARENTHESES",
3795 "return is not a function, parentheses are not required\n" . $herecurr);
3796 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003797 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003798 ERROR("SPACING",
3799 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003800 }
3801 }
Joe Perches507e5142013-11-12 15:10:13 -08003802
Joe Perchesb43ae212014-06-23 13:22:07 -07003803# unnecessary return in a void function
3804# at end-of-function, with the previous line a single leading tab, then return;
3805# and the line before that not a goto label target like "out:"
3806 if ($sline =~ /^[ \+]}\s*$/ &&
3807 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3808 $linenr >= 3 &&
3809 $lines[$linenr - 3] =~ /^[ +]/ &&
3810 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003811 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003812 "void function return statements are not generally useful\n" . $hereprev);
3813 }
Joe Perches9819cf22014-06-04 16:12:09 -07003814
Joe Perches189248d2014-01-23 15:54:47 -08003815# if statements using unnecessary parentheses - ie: if ((foo == bar))
3816 if ($^V && $^V ge 5.10.0 &&
3817 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3818 my $openparens = $1;
3819 my $count = $openparens =~ tr@\(@\(@;
3820 my $msg = "";
3821 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3822 my $comp = $4; #Not $1 because of $LvalOrFunc
3823 $msg = " - maybe == should be = ?" if ($comp eq "==");
3824 WARN("UNNECESSARY_PARENTHESES",
3825 "Unnecessary parentheses$msg\n" . $herecurr);
3826 }
3827 }
3828
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003829# Return of what appears to be an errno should normally be -'ve
3830 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3831 my $name = $1;
3832 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003833 WARN("USE_NEGATIVE_ERRNO",
3834 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003835 }
3836 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003837
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003838# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003839 if ($line =~ /\b(if|while|for|switch)\(/) {
3840 if (ERROR("SPACING",
3841 "space required before the open parenthesis '('\n" . $herecurr) &&
3842 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003843 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003844 s/\b(if|while|for|switch)\(/$1 \(/;
3845 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003846 }
3847
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003848# Check for illegal assignment in if conditional -- and check for trailing
3849# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003850 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003851 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3852 ctx_statement_block($linenr, $realcnt, 0)
3853 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003854 my ($stat_next) = ctx_statement_block($line_nr_next,
3855 $remain_next, $off_next);
3856 $stat_next =~ s/\n./\n /g;
3857 ##print "stat<$stat> stat_next<$stat_next>\n";
3858
3859 if ($stat_next =~ /^\s*while\b/) {
3860 # If the statement carries leading newlines,
3861 # then count those as offsets.
3862 my ($whitespace) =
3863 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3864 my $offset =
3865 statement_rawlines($whitespace) - 1;
3866
3867 $suppress_whiletrailers{$line_nr_next +
3868 $offset} = 1;
3869 }
3870 }
3871 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003872 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003873 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003874 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003875
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003876 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003877 ERROR("ASSIGN_IN_IF",
3878 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003879 }
3880
3881 # Find out what is on the end of the line after the
3882 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003883 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003884 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003885 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003886 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3887 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003888 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003889 # Find out how long the conditional actually is.
3890 my @newlines = ($c =~ /\n/gs);
3891 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003892 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003893
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003894 $stat_real = raw_line($linenr, $cond_lines)
3895 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003896 if (defined($stat_real) && $cond_lines > 1) {
3897 $stat_real = "[...]\n$stat_real";
3898 }
3899
Joe Perches000d1cc12011-07-25 17:13:25 -07003900 ERROR("TRAILING_STATEMENTS",
3901 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003902 }
3903 }
3904
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003905# Check for bitwise tests written as boolean
3906 if ($line =~ /
3907 (?:
3908 (?:\[|\(|\&\&|\|\|)
3909 \s*0[xX][0-9]+\s*
3910 (?:\&\&|\|\|)
3911 |
3912 (?:\&\&|\|\|)
3913 \s*0[xX][0-9]+\s*
3914 (?:\&\&|\|\||\)|\])
3915 )/x)
3916 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003917 WARN("HEXADECIMAL_BOOLEAN_TEST",
3918 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003919 }
3920
Andy Whitcroft8905a672007-11-28 16:21:06 -08003921# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003922 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3923 my $s = $1;
3924 $s =~ s/$;//g; # Remove any comments
3925 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003926 ERROR("TRAILING_STATEMENTS",
3927 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003928 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003929 }
Andy Whitcroft39667782009-01-15 13:51:06 -08003930# if should not continue a brace
3931 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003932 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07003933 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08003934 $herecurr);
3935 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003936# case and default should not have general statements after them
3937 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3938 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07003939 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003940 \s*return\s+
3941 )/xg)
3942 {
Joe Perches000d1cc12011-07-25 17:13:25 -07003943 ERROR("TRAILING_STATEMENTS",
3944 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07003945 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003946
3947 # Check for }<nl>else {, these must be at the same
3948 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07003949 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
3950 $previndent == $indent) {
3951 if (ERROR("ELSE_AFTER_BRACE",
3952 "else should follow close brace '}'\n" . $hereprev) &&
3953 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3954 fix_delete_line($fixlinenr - 1, $prevrawline);
3955 fix_delete_line($fixlinenr, $rawline);
3956 my $fixedline = $prevrawline;
3957 $fixedline =~ s/}\s*$//;
3958 if ($fixedline !~ /^\+\s*$/) {
3959 fix_insert_line($fixlinenr, $fixedline);
3960 }
3961 $fixedline = $rawline;
3962 $fixedline =~ s/^(.\s*)else/$1} else/;
3963 fix_insert_line($fixlinenr, $fixedline);
3964 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003965 }
3966
Joe Perches8b8856f2014-08-06 16:11:14 -07003967 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
3968 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003969 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3970
3971 # Find out what is on the end of the line after the
3972 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003973 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003974 $s =~ s/\n.*//g;
3975
3976 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07003977 if (ERROR("WHILE_AFTER_BRACE",
3978 "while should follow close brace '}'\n" . $hereprev) &&
3979 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3980 fix_delete_line($fixlinenr - 1, $prevrawline);
3981 fix_delete_line($fixlinenr, $rawline);
3982 my $fixedline = $prevrawline;
3983 my $trailing = $rawline;
3984 $trailing =~ s/^\+//;
3985 $trailing = trim($trailing);
3986 $fixedline =~ s/}\s*$/} $trailing/;
3987 fix_insert_line($fixlinenr, $fixedline);
3988 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003989 }
3990 }
3991
Joe Perches95e2c602013-07-03 15:05:20 -07003992#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08003993 while ($line =~ m{($Constant|$Lval)}g) {
3994 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07003995
3996#gcc binary extension
3997 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003998 if (WARN("GCC_BINARY_CONSTANT",
3999 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4000 $fix) {
4001 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004002 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004003 s/\b$var\b/$hexval/;
4004 }
Joe Perches95e2c602013-07-03 15:05:20 -07004005 }
4006
4007#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004008 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004009 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004010#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004011 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004012#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Joe Perches34456862013-07-03 15:05:34 -07004013 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004014 while ($var =~ m{($Ident)}g) {
4015 my $word = $1;
4016 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004017 if ($check) {
4018 seed_camelcase_includes();
4019 if (!$file && !$camelcase_file_seeded) {
4020 seed_camelcase_file($realfile);
4021 $camelcase_file_seeded = 1;
4022 }
4023 }
Joe Perches7e781f62013-09-11 14:23:55 -07004024 if (!defined $camelcase{$word}) {
4025 $camelcase{$word} = 1;
4026 CHK("CAMELCASE",
4027 "Avoid CamelCase: <$word>\n" . $herecurr);
4028 }
Joe Perches34456862013-07-03 15:05:34 -07004029 }
Joe Perches323c1262012-12-17 16:02:07 -08004030 }
4031 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004032
4033#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004034 if ($line =~ /\#\s*define.*\\\s+$/) {
4035 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4036 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4037 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004038 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004039 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004040 }
4041
Andy Whitcroft653d4872007-06-23 17:16:34 -07004042#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004043 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004044 my $file = "$1.h";
4045 my $checkfile = "include/linux/$file";
4046 if (-f "$root/$checkfile" &&
4047 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004048 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004049 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004050 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004051 CHK("ARCH_INCLUDE_LINUX",
4052 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004053 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004054 WARN("INCLUDE_LINUX",
4055 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004056 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004057 }
4058 }
4059
Andy Whitcroft653d4872007-06-23 17:16:34 -07004060# multi-statement macros should be enclosed in a do while loop, grab the
4061# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004062# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004063 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4064 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004065 my $ln = $linenr;
4066 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004067 my ($off, $dstat, $dcond, $rest);
4068 my $ctx = '';
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004069 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004070 ctx_statement_block($linenr, $realcnt, 0);
4071 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004072 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004073 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004074
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004075 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004076 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004077 $dstat =~ s/\\\n.//g;
4078 $dstat =~ s/^\s*//s;
4079 $dstat =~ s/\s*$//s;
4080
4081 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004082 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4083 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004084 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004085 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004086 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004087
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004088 # Flatten any obvious string concatentation.
4089 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4090 $dstat =~ s/$Ident\s*("X*")/$1/)
4091 {
4092 }
4093
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004094 my $exceptions = qr{
4095 $Declare|
4096 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004097 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004098 DECLARE_PER_CPU|
4099 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004100 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004101 union|
4102 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004103 \.$Ident\s*=\s*|
4104 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004105 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004106 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004107 if ($dstat ne '' &&
4108 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4109 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004110 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004111 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004112 $dstat !~ /$exceptions/ &&
4113 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004114 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004115 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004116 $dstat !~ /^for\s*$Constant$/ && # for (...)
4117 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4118 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004119 $dstat !~ /^\({/ && # ({...
4120 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004121 {
4122 $ctx =~ s/\n*$//;
4123 my $herectx = $here . "\n";
4124 my $cnt = statement_rawlines($ctx);
4125
4126 for (my $n = 0; $n < $cnt; $n++) {
4127 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004128 }
4129
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004130 if ($dstat =~ /;/) {
4131 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4132 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4133 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004134 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004135 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004136 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004137 }
Joe Perches5023d342012-12-17 16:01:47 -08004138
Joe Perches481eb482012-12-17 16:01:56 -08004139# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004140
4141 } else {
4142 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004143 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4144 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004145 $line =~ /^\+.*\\$/) {
4146 WARN("LINE_CONTINUATIONS",
4147 "Avoid unnecessary line continuations\n" . $herecurr);
4148 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004149 }
4150
Joe Perchesb13edf72012-07-30 14:41:24 -07004151# do {} while (0) macro tests:
4152# single-statement macros do not need to be enclosed in do while (0) loop,
4153# macro should not end with a semicolon
4154 if ($^V && $^V ge 5.10.0 &&
4155 $realfile !~ m@/vmlinux.lds.h$@ &&
4156 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4157 my $ln = $linenr;
4158 my $cnt = $realcnt;
4159 my ($off, $dstat, $dcond, $rest);
4160 my $ctx = '';
4161 ($dstat, $dcond, $ln, $cnt, $off) =
4162 ctx_statement_block($linenr, $realcnt, 0);
4163 $ctx = $dstat;
4164
4165 $dstat =~ s/\\\n.//g;
4166
4167 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4168 my $stmts = $2;
4169 my $semis = $3;
4170
4171 $ctx =~ s/\n*$//;
4172 my $cnt = statement_rawlines($ctx);
4173 my $herectx = $here . "\n";
4174
4175 for (my $n = 0; $n < $cnt; $n++) {
4176 $herectx .= raw_line($linenr, $n) . "\n";
4177 }
4178
Joe Perchesac8e97f2012-08-21 16:15:53 -07004179 if (($stmts =~ tr/;/;/) == 1 &&
4180 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004181 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4182 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4183 }
4184 if (defined $semis && $semis ne "") {
4185 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4186 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4187 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004188 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4189 $ctx =~ s/\n*$//;
4190 my $cnt = statement_rawlines($ctx);
4191 my $herectx = $here . "\n";
4192
4193 for (my $n = 0; $n < $cnt; $n++) {
4194 $herectx .= raw_line($linenr, $n) . "\n";
4195 }
4196
4197 WARN("TRAILING_SEMICOLON",
4198 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004199 }
4200 }
4201
Mike Frysinger080ba922009-01-06 14:41:25 -08004202# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4203# all assignments may have only one of the following with an assignment:
4204# .
4205# ALIGN(...)
4206# VMLINUX_SYMBOL(...)
4207 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004208 WARN("MISSING_VMLINUX_SYMBOL",
4209 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004210 }
4211
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004212# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004213 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4214 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004215 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004216 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004217 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4218 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004219 my @allowed = ();
4220 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004221 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004222 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004223 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004224 for my $chunk (@chunks) {
4225 my ($cond, $block) = @{$chunk};
4226
Andy Whitcroft773647a2008-03-28 14:15:58 -07004227 # If the condition carries leading newlines, then count those as offsets.
4228 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4229 my $offset = statement_rawlines($whitespace) - 1;
4230
Joe Perchesaad4f612012-03-23 15:02:19 -07004231 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004232 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4233
4234 # We have looked at and allowed this specific line.
4235 $suppress_ifbraces{$ln + $offset} = 1;
4236
4237 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004238 $ln += statement_rawlines($block) - 1;
4239
Andy Whitcroft773647a2008-03-28 14:15:58 -07004240 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004241
4242 $seen++ if ($block =~ /^\s*{/);
4243
Joe Perchesaad4f612012-03-23 15:02:19 -07004244 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004245 if (statement_lines($cond) > 1) {
4246 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004247 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004248 }
4249 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004250 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004251 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004252 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004253 if (statement_block_size($block) > 1) {
4254 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004255 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004256 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004257 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004258 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004259 if ($seen) {
4260 my $sum_allowed = 0;
4261 foreach (@allowed) {
4262 $sum_allowed += $_;
4263 }
4264 if ($sum_allowed == 0) {
4265 WARN("BRACES",
4266 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4267 } elsif ($sum_allowed != $allow &&
4268 $seen != $allow) {
4269 CHK("BRACES",
4270 "braces {} should be used on all arms of this statement\n" . $herectx);
4271 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004272 }
4273 }
4274 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004275 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004276 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004277 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004278
Andy Whitcroftcf655042008-03-04 14:28:20 -08004279 # Check the pre-context.
4280 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4281 #print "APW: ALLOWED: pre<$1>\n";
4282 $allowed = 1;
4283 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004284
4285 my ($level, $endln, @chunks) =
4286 ctx_statement_full($linenr, $realcnt, $-[0]);
4287
Andy Whitcroftcf655042008-03-04 14:28:20 -08004288 # Check the condition.
4289 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004290 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004291 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004292 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004293 }
4294 if (statement_lines($cond) > 1) {
4295 #print "APW: ALLOWED: cond<$cond>\n";
4296 $allowed = 1;
4297 }
4298 if ($block =~/\b(?:if|for|while)\b/) {
4299 #print "APW: ALLOWED: block<$block>\n";
4300 $allowed = 1;
4301 }
4302 if (statement_block_size($block) > 1) {
4303 #print "APW: ALLOWED: lines block<$block>\n";
4304 $allowed = 1;
4305 }
4306 # Check the post-context.
4307 if (defined $chunks[1]) {
4308 my ($cond, $block) = @{$chunks[1]};
4309 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004310 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004311 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004312 if ($block =~ /^\s*\{/) {
4313 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4314 $allowed = 1;
4315 }
4316 }
4317 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004318 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004319 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004320
Andy Whitcroftf0556632008-10-15 22:02:23 -07004321 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004322 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004323 }
4324
Joe Perches000d1cc12011-07-25 17:13:25 -07004325 WARN("BRACES",
4326 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004327 }
4328 }
4329
Joe Perches0979ae62012-12-17 16:01:59 -08004330# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004331 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004332 CHK("BRACES",
4333 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4334 }
Joe Perches77b9a532013-07-03 15:05:29 -07004335 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004336 CHK("BRACES",
4337 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4338 }
4339
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004340# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004341 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4342 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004343 WARN("VOLATILE",
4344 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004345 }
4346
Joe Perchesf17dba42014-10-13 15:51:51 -07004347# concatenated string without spaces between elements
4348 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4349 CHK("CONCATENATED_STRING",
4350 "Concatenated strings should use spaces between elements\n" . $herecurr);
4351 }
4352
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004353# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004354 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004355 CHK("REDUNDANT_CODE",
4356 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004357 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004358 }
4359
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004360# check for needless "if (<foo>) fn(<foo>)" uses
4361 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4362 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4363 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4364 WARN('NEEDLESS_IF',
4365 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004366 }
4367 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004368
Joe Perchesebfdc402014-08-06 16:10:27 -07004369# check for unnecessary "Out of Memory" messages
4370 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4371 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4372 (defined $1 || defined $3) &&
4373 $linenr > 3) {
4374 my $testval = $2;
4375 my $testline = $lines[$linenr - 3];
4376
4377 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4378# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4379
4380 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4381 WARN("OOM_MESSAGE",
4382 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4383 }
4384 }
4385
Joe Perches8716de32013-09-11 14:24:05 -07004386# check for bad placement of section $InitAttribute (e.g.: __initdata)
4387 if ($line =~ /(\b$InitAttribute\b)/) {
4388 my $attr = $1;
4389 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4390 my $ptr = $1;
4391 my $var = $2;
4392 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4393 ERROR("MISPLACED_INIT",
4394 "$attr should be placed after $var\n" . $herecurr)) ||
4395 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4396 WARN("MISPLACED_INIT",
4397 "$attr should be placed after $var\n" . $herecurr))) &&
4398 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004399 $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 -07004400 }
4401 }
4402 }
4403
Joe Perchese970b8842013-11-12 15:10:10 -08004404# check for $InitAttributeData (ie: __initdata) with const
4405 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4406 my $attr = $1;
4407 $attr =~ /($InitAttributePrefix)(.*)/;
4408 my $attr_prefix = $1;
4409 my $attr_type = $2;
4410 if (ERROR("INIT_ATTRIBUTE",
4411 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4412 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004413 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004414 s/$InitAttributeData/${attr_prefix}initconst/;
4415 }
4416 }
4417
4418# check for $InitAttributeConst (ie: __initconst) without const
4419 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4420 my $attr = $1;
4421 if (ERROR("INIT_ATTRIBUTE",
4422 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4423 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004424 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004425 /(^\+\s*(?:static\s+))/;
4426 $lead = rtrim($1);
4427 $lead = "$lead " if ($lead !~ /^\+$/);
4428 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004429 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004430 }
4431 }
4432
Joe Perchesfbdb8132014-04-03 14:49:14 -07004433# don't use __constant_<foo> functions outside of include/uapi/
4434 if ($realfile !~ m@^include/uapi/@ &&
4435 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4436 my $constant_func = $1;
4437 my $func = $constant_func;
4438 $func =~ s/^__constant_//;
4439 if (WARN("CONSTANT_CONVERSION",
4440 "$constant_func should be $func\n" . $herecurr) &&
4441 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004442 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004443 }
4444 }
4445
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004446# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004447 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004448 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004449 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004450 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004451 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004452 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4453 }
4454 if ($delay > 2000) {
4455 WARN("LONG_UDELAY",
4456 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004457 }
4458 }
4459
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004460# warn about unexpectedly long msleep's
4461 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4462 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004463 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004464 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004465 }
4466 }
4467
Joe Perches36ec1932013-07-03 15:05:25 -07004468# check for comparisons of jiffies
4469 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4470 WARN("JIFFIES_COMPARISON",
4471 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4472 }
4473
Joe Perches9d7a34a2013-07-03 15:05:26 -07004474# check for comparisons of get_jiffies_64()
4475 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4476 WARN("JIFFIES_COMPARISON",
4477 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4478 }
4479
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004480# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004481# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004482# print "#ifdef in C files should be avoided\n";
4483# print "$herecurr";
4484# $clean = 0;
4485# }
4486
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004487# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004488 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004489 if (ERROR("SPACING",
4490 "exactly one space required after that #$1\n" . $herecurr) &&
4491 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004492 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004493 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4494 }
4495
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004496 }
4497
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004498# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004499 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4500 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004501 my $which = $1;
4502 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004503 CHK("UNCOMMENTED_DEFINITION",
4504 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004505 }
4506 }
4507# check for memory barriers without a comment.
4508 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4509 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004510 WARN("MEMORY_BARRIER",
4511 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004512 }
4513 }
4514# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004515 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004516 CHK("ARCH_DEFINES",
4517 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004518 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004519
Tobias Klauserd4977c72010-05-24 14:33:30 -07004520# Check that the storage class is at the beginning of a declaration
4521 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004522 WARN("STORAGE_CLASS",
4523 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004524 }
4525
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004526# check the location of the inline attribute, that it is between
4527# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004528 if ($line =~ /\b$Type\s+$Inline\b/ ||
4529 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004530 ERROR("INLINE_LOCATION",
4531 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004532 }
4533
Andy Whitcroft8905a672007-11-28 16:21:06 -08004534# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004535 if ($realfile !~ m@\binclude/uapi/@ &&
4536 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004537 if (WARN("INLINE",
4538 "plain inline is preferred over $1\n" . $herecurr) &&
4539 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004540 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004541
4542 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004543 }
4544
Joe Perches3d130fd2011-01-12 17:00:00 -08004545# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004546 if ($realfile !~ m@\binclude/uapi/@ &&
4547 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004548 WARN("PREFER_PACKED",
4549 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004550 }
4551
Joe Perches39b7e282011-07-25 17:13:24 -07004552# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004553 if ($realfile !~ m@\binclude/uapi/@ &&
4554 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004555 WARN("PREFER_ALIGNED",
4556 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004557 }
4558
Joe Perches5f14d3b2012-01-10 15:09:52 -08004559# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004560 if ($realfile !~ m@\binclude/uapi/@ &&
4561 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004562 if (WARN("PREFER_PRINTF",
4563 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4564 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004565 $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 -07004566
4567 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004568 }
4569
Joe Perches6061d942012-03-23 15:02:16 -07004570# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004571 if ($realfile !~ m@\binclude/uapi/@ &&
4572 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004573 if (WARN("PREFER_SCANF",
4574 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4575 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004576 $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 -07004577 }
Joe Perches6061d942012-03-23 15:02:16 -07004578 }
4579
Joe Perches8f53a9b2010-03-05 13:43:48 -08004580# check for sizeof(&)
4581 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004582 WARN("SIZEOF_ADDRESS",
4583 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004584 }
4585
Joe Perches66c80b62012-07-30 14:41:22 -07004586# check for sizeof without parenthesis
4587 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004588 if (WARN("SIZEOF_PARENTHESIS",
4589 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4590 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004591 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004592 }
Joe Perches66c80b62012-07-30 14:41:22 -07004593 }
4594
Joe Perches428e2fd2011-05-24 17:13:39 -07004595# check for line continuations in quoted strings with odd counts of "
4596 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004597 WARN("LINE_CONTINUATIONS",
4598 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004599 }
4600
Joe Perches88982fe2012-12-17 16:02:00 -08004601# check for struct spinlock declarations
4602 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4603 WARN("USE_SPINLOCK_T",
4604 "struct spinlock should be spinlock_t\n" . $herecurr);
4605 }
4606
Joe Perchesa6962d72013-04-29 16:18:13 -07004607# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004608 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004609 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004610 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004611 if (WARN("PREFER_SEQ_PUTS",
4612 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4613 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004614 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004615 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004616 }
4617 }
4618
Andy Whitcroft554e1652012-01-10 15:09:57 -08004619# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004620 if ($^V && $^V ge 5.10.0 &&
4621 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004622 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004623
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004624 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004625 my $ms_val = $7;
4626 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004627
Andy Whitcroft554e1652012-01-10 15:09:57 -08004628 if ($ms_size =~ /^(0x|)0$/i) {
4629 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004630 "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 -08004631 } elsif ($ms_size =~ /^(0x|)1$/i) {
4632 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004633 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4634 }
4635 }
4636
Joe Perches98a9bba2014-01-23 15:54:52 -08004637# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4638 if ($^V && $^V ge 5.10.0 &&
4639 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4640 if (WARN("PREFER_ETHER_ADDR_COPY",
4641 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4642 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004643 $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 -08004644 }
4645 }
4646
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004647# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004648 if ($^V && $^V ge 5.10.0 &&
4649 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004650 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004651 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004652 my $call = $1;
4653 my $cast1 = deparenthesize($2);
4654 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004655 my $cast2 = deparenthesize($7);
4656 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004657 my $cast;
4658
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004659 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004660 $cast = "$cast1 or $cast2";
4661 } elsif ($cast1 ne "") {
4662 $cast = $cast1;
4663 } else {
4664 $cast = $cast2;
4665 }
4666 WARN("MINMAX",
4667 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004668 }
4669 }
4670
Joe Perches4a273192012-07-30 14:41:20 -07004671# check usleep_range arguments
4672 if ($^V && $^V ge 5.10.0 &&
4673 defined $stat &&
4674 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4675 my $min = $1;
4676 my $max = $7;
4677 if ($min eq $max) {
4678 WARN("USLEEP_RANGE",
4679 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4680 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4681 $min > $max) {
4682 WARN("USLEEP_RANGE",
4683 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4684 }
4685 }
4686
Joe Perches823b7942013-11-12 15:10:15 -08004687# check for naked sscanf
4688 if ($^V && $^V ge 5.10.0 &&
4689 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004690 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004691 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4692 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4693 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4694 my $lc = $stat =~ tr@\n@@;
4695 $lc = $lc + $linenr;
4696 my $stat_real = raw_line($linenr, 0);
4697 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4698 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4699 }
4700 WARN("NAKED_SSCANF",
4701 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4702 }
4703
Joe Perchesafc819a2014-06-04 16:12:08 -07004704# check for simple sscanf that should be kstrto<foo>
4705 if ($^V && $^V ge 5.10.0 &&
4706 defined $stat &&
4707 $line =~ /\bsscanf\b/) {
4708 my $lc = $stat =~ tr@\n@@;
4709 $lc = $lc + $linenr;
4710 my $stat_real = raw_line($linenr, 0);
4711 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4712 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4713 }
4714 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4715 my $format = $6;
4716 my $count = $format =~ tr@%@%@;
4717 if ($count == 1 &&
4718 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4719 WARN("SSCANF_TO_KSTRTO",
4720 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4721 }
4722 }
4723 }
4724
Joe Perches70dc8a42013-09-11 14:23:58 -07004725# check for new externs in .h files.
4726 if ($realfile =~ /\.h$/ &&
4727 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004728 if (CHK("AVOID_EXTERNS",
4729 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004730 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004731 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004732 }
4733 }
4734
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004735# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004736 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004737 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004738 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004739 my $function_name = $1;
4740 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004741
4742 my $s = $stat;
4743 if (defined $cond) {
4744 substr($s, 0, length($cond), '');
4745 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004746 if ($s =~ /^\s*;/ &&
4747 $function_name ne 'uninitialized_var')
4748 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004749 WARN("AVOID_EXTERNS",
4750 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004751 }
4752
4753 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004754 WARN("FUNCTION_ARGUMENTS",
4755 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004756 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004757
4758 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4759 $stat =~ /^.\s*extern\s+/)
4760 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004761 WARN("AVOID_EXTERNS",
4762 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004763 }
4764
4765# checks for new __setup's
4766 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4767 my $name = $1;
4768
4769 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004770 CHK("UNDOCUMENTED_SETUP",
4771 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004772 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004773 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004774
4775# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004776 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004777 WARN("UNNECESSARY_CASTS",
4778 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004779 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004780
Joe Perchesa640d252013-07-03 15:05:21 -07004781# alloc style
4782# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4783 if ($^V && $^V ge 5.10.0 &&
4784 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4785 CHK("ALLOC_SIZEOF_STRUCT",
4786 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4787 }
4788
Joe Perches60a55362014-06-04 16:12:07 -07004789# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4790 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004791 $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 -07004792 my $oldfunc = $3;
4793 my $a1 = $4;
4794 my $a2 = $10;
4795 my $newfunc = "kmalloc_array";
4796 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004797 my $r1 = $a1;
4798 my $r2 = $a2;
4799 if ($a1 =~ /^sizeof\s*\S/) {
4800 $r1 = $a2;
4801 $r2 = $a1;
4802 }
4803 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4804 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004805 if (WARN("ALLOC_WITH_MULTIPLY",
4806 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4807 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004808 $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 -07004809
4810 }
4811 }
4812 }
4813
Joe Perches972fdea2013-04-29 16:18:12 -07004814# check for krealloc arg reuse
4815 if ($^V && $^V ge 5.10.0 &&
4816 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4817 WARN("KREALLOC_ARG_REUSE",
4818 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4819 }
4820
Joe Perches5ce59ae2013-02-21 16:44:18 -08004821# check for alloc argument mismatch
4822 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4823 WARN("ALLOC_ARRAY_ARGS",
4824 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4825 }
4826
Joe Perchescaf2a542011-01-12 16:59:56 -08004827# check for multiple semicolons
4828 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004829 if (WARN("ONE_SEMICOLON",
4830 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4831 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004832 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004833 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004834 }
4835
Joe Perchese81f2392014-08-06 16:11:25 -07004836# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08004837 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4838 my $has_break = 0;
4839 my $has_statement = 0;
4840 my $count = 0;
4841 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07004842 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08004843 $prevline--;
4844 my $rline = $rawlines[$prevline - 1];
4845 my $fline = $lines[$prevline - 1];
4846 last if ($fline =~ /^\@\@/);
4847 next if ($fline =~ /^\-/);
4848 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4849 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4850 next if ($fline =~ /^.[\s$;]*$/);
4851 $has_statement = 1;
4852 $count++;
4853 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4854 }
4855 if (!$has_break && $has_statement) {
4856 WARN("MISSING_BREAK",
4857 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4858 }
4859 }
4860
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004861# check for switch/default statements without a break;
4862 if ($^V && $^V ge 5.10.0 &&
4863 defined $stat &&
4864 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
4865 my $ctx = '';
4866 my $herectx = $here . "\n";
4867 my $cnt = statement_rawlines($stat);
4868 for (my $n = 0; $n < $cnt; $n++) {
4869 $herectx .= raw_line($linenr, $n) . "\n";
4870 }
4871 WARN("DEFAULT_NO_BREAK",
4872 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08004873 }
4874
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004875# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07004876 if ($line =~ /\b__FUNCTION__\b/) {
4877 if (WARN("USE_FUNC",
4878 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4879 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004880 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004881 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004882 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004883
Joe Perches2c924882012-03-23 15:02:20 -07004884# check for use of yield()
4885 if ($line =~ /\byield\s*\(\s*\)/) {
4886 WARN("YIELD",
4887 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
4888 }
4889
Joe Perches179f8f42013-07-03 15:05:30 -07004890# check for comparisons against true and false
4891 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
4892 my $lead = $1;
4893 my $arg = $2;
4894 my $test = $3;
4895 my $otype = $4;
4896 my $trail = $5;
4897 my $op = "!";
4898
4899 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
4900
4901 my $type = lc($otype);
4902 if ($type =~ /^(?:true|false)$/) {
4903 if (("$test" eq "==" && "$type" eq "true") ||
4904 ("$test" eq "!=" && "$type" eq "false")) {
4905 $op = "";
4906 }
4907
4908 CHK("BOOL_COMPARISON",
4909 "Using comparison to $otype is error prone\n" . $herecurr);
4910
4911## maybe suggesting a correct construct would better
4912## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
4913
4914 }
4915 }
4916
Thomas Gleixner4882720b2010-09-07 14:34:01 +00004917# check for semaphores initialized locked
4918 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004919 WARN("CONSIDER_COMPLETION",
4920 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004921 }
Joe Perches6712d852012-03-23 15:02:20 -07004922
Joe Perches67d0a072011-10-31 17:13:10 -07004923# recommend kstrto* over simple_strto* and strict_strto*
4924 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004925 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07004926 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07004927 }
Joe Perches6712d852012-03-23 15:02:20 -07004928
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004929# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07004930 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004931 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07004932 "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 -07004933 }
Joe Perches6712d852012-03-23 15:02:20 -07004934
Emese Revfy79404842010-03-05 13:43:53 -08004935# check for various ops structs, ensure they are const.
4936 my $struct_ops = qr{acpi_dock_ops|
4937 address_space_operations|
4938 backlight_ops|
4939 block_device_operations|
4940 dentry_operations|
4941 dev_pm_ops|
4942 dma_map_ops|
4943 extent_io_ops|
4944 file_lock_operations|
4945 file_operations|
4946 hv_ops|
4947 ide_dma_ops|
4948 intel_dvo_dev_ops|
4949 item_operations|
4950 iwl_ops|
4951 kgdb_arch|
4952 kgdb_io|
4953 kset_uevent_ops|
4954 lock_manager_operations|
4955 microcode_ops|
4956 mtrr_ops|
4957 neigh_ops|
4958 nlmsvc_binding|
4959 pci_raw_ops|
4960 pipe_buf_operations|
4961 platform_hibernation_ops|
4962 platform_suspend_ops|
4963 proto_ops|
4964 rpc_pipe_ops|
4965 seq_operations|
4966 snd_ac97_build_ops|
4967 soc_pcmcia_socket_ops|
4968 stacktrace_ops|
4969 sysfs_ops|
4970 tty_operations|
4971 usb_mon_operations|
4972 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004973 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08004974 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004975 WARN("CONST_STRUCT",
4976 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08004977 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08004978 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004979
4980# use of NR_CPUS is usually wrong
4981# ignore definitions of NR_CPUS and usage to define arrays as likely right
4982 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004983 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
4984 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004985 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
4986 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
4987 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004988 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004989 WARN("NR_CPUS",
4990 "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 -07004991 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004992
Joe Perches52ea8502013-11-12 15:10:09 -08004993# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4994 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4995 ERROR("DEFINE_ARCH_HAS",
4996 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4997 }
4998
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004999# check for %L{u,d,i} in strings
5000 my $string;
5001 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5002 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07005003 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005004 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005005 WARN("PRINTF_L",
5006 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005007 last;
5008 }
5009 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005010
5011# whine mightly about in_atomic
5012 if ($line =~ /\bin_atomic\s*\(/) {
5013 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005014 ERROR("IN_ATOMIC",
5015 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005016 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005017 WARN("IN_ATOMIC",
5018 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005019 }
5020 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005021
5022# check for lockdep_set_novalidate_class
5023 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5024 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5025 if ($realfile !~ m@^kernel/lockdep@ &&
5026 $realfile !~ m@^include/linux/lockdep@ &&
5027 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005028 ERROR("LOCKDEP",
5029 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005030 }
5031 }
Dave Jones88f88312011-01-12 16:59:59 -08005032
5033 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5034 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005035 WARN("EXPORTED_WORLD_WRITABLE",
5036 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005037 }
Joe Perches24358802014-04-03 14:49:13 -07005038
Joe Perches515a2352014-04-03 14:49:24 -07005039# Mode permission misuses where it seems decimal should be octal
5040# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5041 if ($^V && $^V ge 5.10.0 &&
5042 $line =~ /$mode_perms_search/) {
5043 foreach my $entry (@mode_permission_funcs) {
5044 my $func = $entry->[0];
5045 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005046
Joe Perches515a2352014-04-03 14:49:24 -07005047 my $skip_args = "";
5048 if ($arg_pos > 1) {
5049 $arg_pos--;
5050 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5051 }
5052 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5053 if ($line =~ /$test/) {
5054 my $val = $1;
5055 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005056
Joe Perches515a2352014-04-03 14:49:24 -07005057 if ($val !~ /^0$/ &&
5058 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5059 length($val) ne 4)) {
5060 ERROR("NON_OCTAL_PERMISSIONS",
5061 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5062 }
Joe Perches24358802014-04-03 14:49:13 -07005063 }
5064 }
5065 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005066 }
5067
5068 # If we have no input at all, then there is nothing to report on
5069 # so just keep quiet.
5070 if ($#rawlines == -1) {
5071 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005072 }
5073
Andy Whitcroft8905a672007-11-28 16:21:06 -08005074 # In mailback mode only produce a report in the negative, for
5075 # things that appear to be patches.
5076 if ($mailback && ($clean == 1 || !$is_patch)) {
5077 exit(0);
5078 }
5079
5080 # This is not a patch, and we are are in 'no-patch' mode so
5081 # just keep quiet.
5082 if (!$chk_patch && !$is_patch) {
5083 exit(0);
5084 }
5085
5086 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005087 ERROR("NOT_UNIFIED_DIFF",
5088 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005089 }
5090 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005091 ERROR("MISSING_SIGN_OFF",
5092 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005093 }
5094
Andy Whitcroft8905a672007-11-28 16:21:06 -08005095 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005096 if ($summary && !($clean == 1 && $quiet == 1)) {
5097 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005098 print "total: $cnt_error errors, $cnt_warn warnings, " .
5099 (($check)? "$cnt_chk checks, " : "") .
5100 "$cnt_lines lines checked\n";
5101 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005102 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005103
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005104 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005105
5106 if ($^V lt 5.10.0) {
5107 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5108 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5109 }
5110
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005111 # If there were whitespace errors which cleanpatch can fix
5112 # then suggest that.
5113 if ($rpt_cleaners) {
5114 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5115 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005116 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005117 }
5118 }
5119
Joe Perches91bfe482013-09-11 14:23:59 -07005120 hash_show_words(\%use_type, "Used");
5121 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005122
Joe Perchesd752fcc2014-08-06 16:11:05 -07005123 if ($clean == 0 && $fix &&
5124 ("@rawlines" ne "@fixed" ||
5125 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005126 my $newfile = $filename;
5127 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005128 my $linecount = 0;
5129 my $f;
5130
Joe Perchesd752fcc2014-08-06 16:11:05 -07005131 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5132
Joe Perches3705ce52013-07-03 15:05:31 -07005133 open($f, '>', $newfile)
5134 or die "$P: Can't open $newfile for write\n";
5135 foreach my $fixed_line (@fixed) {
5136 $linecount++;
5137 if ($file) {
5138 if ($linecount > 3) {
5139 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005140 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005141 }
5142 } else {
5143 print $f $fixed_line . "\n";
5144 }
5145 }
5146 close($f);
5147
5148 if (!$quiet) {
5149 print << "EOM";
5150Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5151
5152Do _NOT_ trust the results written to this file.
5153Do _NOT_ submit these changes without inspecting them for correctness.
5154
5155This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5156No warranties, expressed or implied...
5157
5158EOM
5159 }
5160 }
5161
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005162 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005163 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005164 }
5165 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005166 print << "EOM";
5167$vname has style problems, please review.
5168
5169If any of these errors are false positives, please report
5170them to the maintainer, see CHECKPATCH in MAINTAINERS.
5171EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005172 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005173
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005174 return $clean;
5175}