blob: 5e63dce2e4281c0afc8c9e34db570bf731b2c020 [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;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070024my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070025my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $file = 0;
28my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070029my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080030my $summary = 1;
31my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080032my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070033my $show_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070034my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080035my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080037my %debug;
Joe Perches34456862013-07-03 15:05:34 -070038my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070039my %use_type = ();
40my @use = ();
41my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070042my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070043my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070044my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080045my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070046my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070048my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070049my $spelling_file = "$D/spelling.txt";
Hannes Eder77f5b102009-09-21 17:04:37 -070050
51sub help {
52 my ($exitcode) = @_;
53
54 print << "EOM";
55Usage: $P [OPTION]... [FILE]...
56Version: $V
57
58Options:
59 -q, --quiet quiet
60 --no-tree run without a kernel tree
61 --no-signoff do not check for 'Signed-off-by' line
62 --patch treat FILE as patchfile (default)
63 --emacs emacs compile window format
64 --terse one line per report
65 -f, --file treat FILE as regular source file
66 --subjective, --strict enable more subjective tests
Joe Perches91bfe482013-09-11 14:23:59 -070067 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070068 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches6cd7f382012-12-17 16:01:54 -080069 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070070 --min-conf-desc-length=n set the min description length, if shorter, warn
Joe Perches000d1cc12011-07-25 17:13:25 -070071 --show-types show the message "types" in the output
Hannes Eder77f5b102009-09-21 17:04:37 -070072 --root=PATH PATH to the kernel tree root
73 --no-summary suppress the per-file summary
74 --mailback only produce a report in case of warnings/errors
75 --summary-file include the filename in summary
76 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
77 'values', 'possible', 'type', and 'attr' (default
78 is all off)
79 --test-only=WORD report only warnings/errors containing WORD
80 literally
Joe Perches3705ce52013-07-03 15:05:31 -070081 --fix EXPERIMENTAL - may create horrible results
82 If correctable single-line errors exist, create
83 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
84 with potential errors corrected to the preferred
85 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -080086 --fix-inplace EXPERIMENTAL - may create horrible results
87 Is the same as --fix, but overwrites the input
88 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -070089 --ignore-perl-version override checking of perl version. expect
90 runtime errors.
Hannes Eder77f5b102009-09-21 17:04:37 -070091 -h, --help, --version display this help and exit
92
93When FILE is - read standard input.
94EOM
95
96 exit($exitcode);
97}
98
Joe Perches000d1cc12011-07-25 17:13:25 -070099my $conf = which_conf($configuration_file);
100if (-f $conf) {
101 my @conf_args;
102 open(my $conffile, '<', "$conf")
103 or warn "$P: Can't find a readable $configuration_file file $!\n";
104
105 while (<$conffile>) {
106 my $line = $_;
107
108 $line =~ s/\s*\n?$//g;
109 $line =~ s/^\s*//g;
110 $line =~ s/\s+/ /g;
111
112 next if ($line =~ m/^\s*#/);
113 next if ($line =~ m/^\s*$/);
114
115 my @words = split(" ", $line);
116 foreach my $word (@words) {
117 last if ($word =~ m/^#/);
118 push (@conf_args, $word);
119 }
120 }
121 close($conffile);
122 unshift(@ARGV, @conf_args) if @conf_args;
123}
124
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700125GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700126 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700127 'tree!' => \$tree,
128 'signoff!' => \$chk_signoff,
129 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700130 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800131 'terse!' => \$terse,
Hannes Eder77f5b102009-09-21 17:04:37 -0700132 'f|file!' => \$file,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700133 'subjective!' => \$check,
134 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700135 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700136 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700137 'show-types!' => \$show_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800138 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700139 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700140 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800141 'summary!' => \$summary,
142 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800143 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700144 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800145 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700146 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800147 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700148 'test-only=s' => \$tst_only,
Hannes Eder77f5b102009-09-21 17:04:37 -0700149 'h|help' => \$help,
150 'version' => \$help
151) or help(1);
152
153help(0) if ($help);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700154
Joe Perches9624b8d2014-01-23 15:54:44 -0800155$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700156$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800157
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700158my $exit = 0;
159
Dave Hansend62a2012013-09-11 14:23:56 -0700160if ($^V && $^V lt $minimum_perl_version) {
161 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162 if (!$ignore_perl_version) {
163 exit(1);
164 }
165}
166
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700167if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700168 print "$P: no input files\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700169 exit(1);
170}
171
Joe Perches91bfe482013-09-11 14:23:59 -0700172sub hash_save_array_words {
173 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700174
Joe Perches91bfe482013-09-11 14:23:59 -0700175 my @array = split(/,/, join(',', @$arrayRef));
176 foreach my $word (@array) {
177 $word =~ s/\s*\n?$//g;
178 $word =~ s/^\s*//g;
179 $word =~ s/\s+/ /g;
180 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700181
Joe Perches91bfe482013-09-11 14:23:59 -0700182 next if ($word =~ m/^\s*#/);
183 next if ($word =~ m/^\s*$/);
184
185 $hashRef->{$word}++;
186 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700187}
188
Joe Perches91bfe482013-09-11 14:23:59 -0700189sub hash_show_words {
190 my ($hashRef, $prefix) = @_;
191
Joe Perches58cb3cf2013-09-11 14:24:04 -0700192 if ($quiet == 0 && keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700193 print "NOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700194 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700195 print " $word";
196 }
197 print "\n\n";
198 }
199}
200
201hash_save_array_words(\%ignore_type, \@ignore);
202hash_save_array_words(\%use_type, \@use);
203
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800204my $dbg_values = 0;
205my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700206my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700207my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800208for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800209 ## no critic
210 eval "\${dbg_$key} = '$debug{$key}';";
211 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800212}
213
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700214my $rpt_cleaners = 0;
215
Andy Whitcroft8905a672007-11-28 16:21:06 -0800216if ($terse) {
217 $emacs = 1;
218 $quiet++;
219}
220
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700221if ($tree) {
222 if (defined $root) {
223 if (!top_of_kernel_tree($root)) {
224 die "$P: $root: --root does not point at a valid tree\n";
225 }
226 } else {
227 if (top_of_kernel_tree('.')) {
228 $root = '.';
229 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
230 top_of_kernel_tree($1)) {
231 $root = $1;
232 }
233 }
234
235 if (!defined $root) {
236 print "Must be run from the top-level dir. of a kernel tree\n";
237 exit(2);
238 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700239}
240
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700241my $emitted_corrupt = 0;
242
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700243our $Ident = qr{
244 [A-Za-z_][A-Za-z\d_]*
245 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
246 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700247our $Storage = qr{extern|static|asmlinkage};
248our $Sparse = qr{
249 __user|
250 __kernel|
251 __force|
252 __iomem|
253 __must_check|
254 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800255 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700256 __ref|
257 __rcu
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700258 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800259our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700264
Wolfram Sang52131292010-03-05 13:43:51 -0800265# Notes to $Attribute:
266# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700267our $Attribute = qr{
268 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700269 __percpu|
270 __nocast|
271 __safe|
272 __bitwise__|
273 __packed__|
274 __packed2__|
275 __naked|
276 __maybe_unused|
277 __always_unused|
278 __noreturn|
279 __used|
280 __cold|
281 __noclone|
282 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700283 __read_mostly|
284 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700285 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700286 ____cacheline_aligned|
287 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800288 ____cacheline_internodealigned_in_smp|
289 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700290 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700291our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700292our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700293our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
294our $Lval = qr{$Ident(?:$Member)*};
295
Joe Perches95e2c602013-07-03 15:05:20 -0700296our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
297our $Binary = qr{(?i)0b[01]+$Int_type?};
298our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
299our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700300our $Octal = qr{0[0-7]+$Int_type?};
Joe Perches326b1ff2013-02-04 14:28:51 -0800301our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
302our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
303our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800304our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700305our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800306our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700307our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700308our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700309our $Operators = qr{
310 <=|>=|==|!=|
311 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700312 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313 }x;
314
Joe Perches91cb5192014-04-03 14:49:32 -0700315our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
316
Andy Whitcroft8905a672007-11-28 16:21:06 -0800317our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700318our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700319our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800320our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700321our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700323our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324
Joe Perches15662b32011-10-31 17:13:12 -0700325our $NON_ASCII_UTF8 = qr{
326 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700327 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
328 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
329 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
330 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
331 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
332 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
333}x;
334
Joe Perches15662b32011-10-31 17:13:12 -0700335our $UTF8 = qr{
336 [\x09\x0A\x0D\x20-\x7E] # ASCII
337 | $NON_ASCII_UTF8
338}x;
339
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700340our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700341 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700342 atomic_t
343)};
344
Joe Perches691e6692010-03-05 13:43:51 -0800345our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700346 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700347 (?:[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 -0700348 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700349 panic|
Joe Perches06668722013-11-12 15:10:07 -0800350 MODULE_[A-Z_]+|
351 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800352)};
353
Joe Perches20112472011-07-25 17:13:23 -0700354our $signature_tags = qr{(?xi:
355 Signed-off-by:|
356 Acked-by:|
357 Tested-by:|
358 Reviewed-by:|
359 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700360 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700361 To:|
362 Cc:
363)};
364
Joe Perches18130872014-08-06 16:11:22 -0700365our @typeListMisordered = (
366 qr{char\s+(?:un)?signed},
367 qr{int\s+(?:(?:un)?signed\s+)?short\s},
368 qr{int\s+short(?:\s+(?:un)?signed)},
369 qr{short\s+int(?:\s+(?:un)?signed)},
370 qr{(?:un)?signed\s+int\s+short},
371 qr{short\s+(?:un)?signed},
372 qr{long\s+int\s+(?:un)?signed},
373 qr{int\s+long\s+(?:un)?signed},
374 qr{long\s+(?:un)?signed\s+int},
375 qr{int\s+(?:un)?signed\s+long},
376 qr{int\s+(?:un)?signed},
377 qr{int\s+long\s+long\s+(?:un)?signed},
378 qr{long\s+long\s+int\s+(?:un)?signed},
379 qr{long\s+long\s+(?:un)?signed\s+int},
380 qr{long\s+long\s+(?:un)?signed},
381 qr{long\s+(?:un)?signed},
382);
383
Andy Whitcroft8905a672007-11-28 16:21:06 -0800384our @typeList = (
385 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700386 qr{(?:(?:un)?signed\s+)?char},
387 qr{(?:(?:un)?signed\s+)?short\s+int},
388 qr{(?:(?:un)?signed\s+)?short},
389 qr{(?:(?:un)?signed\s+)?int},
390 qr{(?:(?:un)?signed\s+)?long\s+int},
391 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
392 qr{(?:(?:un)?signed\s+)?long\s+long},
393 qr{(?:(?:un)?signed\s+)?long},
394 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395 qr{float},
396 qr{double},
397 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398 qr{struct\s+$Ident},
399 qr{union\s+$Ident},
400 qr{enum\s+$Ident},
401 qr{${Ident}_t},
402 qr{${Ident}_handler},
403 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700404 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405);
Joe Perches8716de32013-09-11 14:24:05 -0700406our @typeListWithAttr = (
407 @typeList,
408 qr{struct\s+$InitAttribute\s+$Ident},
409 qr{union\s+$InitAttribute\s+$Ident},
410);
411
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700412our @modifierList = (
413 qr{fastcall},
414);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800415
Joe Perches24358802014-04-03 14:49:13 -0700416our @mode_permission_funcs = (
417 ["module_param", 3],
418 ["module_param_(?:array|named|string)", 4],
419 ["module_param_array_named", 5],
420 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
421 ["proc_create(?:_data|)", 2],
422 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
423);
424
Joe Perches515a2352014-04-03 14:49:24 -0700425#Create a search pattern for all these functions to speed up a loop below
426our $mode_perms_search = "";
427foreach my $entry (@mode_permission_funcs) {
428 $mode_perms_search .= '|' if ($mode_perms_search ne "");
429 $mode_perms_search .= $entry->[0];
430}
431
Wolfram Sang7840a942010-08-09 17:20:57 -0700432our $allowed_asm_includes = qr{(?x:
433 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700434 memory|
435 time|
436 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700437)};
438# memory.h: ARM has a custom one
439
Kees Cook66b47b42014-10-13 15:51:57 -0700440# Load common spelling mistakes and build regular expression list.
441my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700442my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700443
Joe Perches36061e32014-12-10 15:51:43 -0800444if (open(my $spelling, '<', $spelling_file)) {
445 my @spelling_list;
446 while (<$spelling>) {
447 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700448
Joe Perches36061e32014-12-10 15:51:43 -0800449 $line =~ s/\s*\n?$//g;
450 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700451
Joe Perches36061e32014-12-10 15:51:43 -0800452 next if ($line =~ m/^\s*#/);
453 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700454
Joe Perches36061e32014-12-10 15:51:43 -0800455 my ($suspect, $fix) = split(/\|\|/, $line);
456
457 push(@spelling_list, $suspect);
458 $spelling_fix{$suspect} = $fix;
459 }
460 close($spelling);
461 $misspellings = join("|", @spelling_list);
462} else {
463 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700464}
Kees Cook66b47b42014-10-13 15:51:57 -0700465
Andy Whitcroft8905a672007-11-28 16:21:06 -0800466sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700467 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
468 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700469 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700470 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700471 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800472 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700473 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800474 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800475 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700476 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700477 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800478 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700479 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800480 }x;
Joe Perches18130872014-08-06 16:11:22 -0700481 $NonptrTypeMisordered = qr{
482 (?:$Modifier\s+|const\s+)*
483 (?:
484 (?:${Misordered}\b)
485 )
486 (?:\s+$Modifier|\s+const)*
487 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700488 $NonptrTypeWithAttr = qr{
489 (?:$Modifier\s+|const\s+)*
490 (?:
491 (?:typeof|__typeof__)\s*\([^\)]*\)|
492 (?:$typeTypedefs\b)|
493 (?:${allWithAttr}\b)
494 )
495 (?:\s+$Modifier|\s+const)*
496 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800497 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700498 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700499 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700500 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800501 }x;
Joe Perches18130872014-08-06 16:11:22 -0700502 $TypeMisordered = qr{
503 $NonptrTypeMisordered
504 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
505 (?:\s+$Inline|\s+$Modifier)*
506 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700507 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700508 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800509}
510build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700511
Joe Perches7d2367a2011-07-25 17:13:22 -0700512our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700513
514# Using $balanced_parens, $LvalOrFunc, or $FuncArg
515# requires at least perl version v5.10.0
516# Any use must be runtime checked with $^V
517
518our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700519our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesd7c76ba2012-01-10 15:09:58 -0800520our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700521
Joe Perchesf8422302014-08-06 16:11:31 -0700522our $declaration_macros = qr{(?x:
523 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
524 (?:$Storage\s+)?LIST_HEAD\s*\(|
525 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
526)};
527
Joe Perches7d2367a2011-07-25 17:13:22 -0700528sub deparenthesize {
529 my ($string) = @_;
530 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700531
532 while ($string =~ /^\s*\(.*\)\s*$/) {
533 $string =~ s@^\s*\(\s*@@;
534 $string =~ s@\s*\)\s*$@@;
535 }
536
Joe Perches7d2367a2011-07-25 17:13:22 -0700537 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700538
Joe Perches7d2367a2011-07-25 17:13:22 -0700539 return $string;
540}
541
Joe Perches34456862013-07-03 15:05:34 -0700542sub seed_camelcase_file {
543 my ($file) = @_;
544
545 return if (!(-f $file));
546
547 local $/;
548
549 open(my $include_file, '<', "$file")
550 or warn "$P: Can't read '$file' $!\n";
551 my $text = <$include_file>;
552 close($include_file);
553
554 my @lines = split('\n', $text);
555
556 foreach my $line (@lines) {
557 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
558 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
559 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800560 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
561 $camelcase{$1} = 1;
562 } 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 -0700563 $camelcase{$1} = 1;
564 }
565 }
566}
567
568my $camelcase_seeded = 0;
569sub seed_camelcase_includes {
570 return if ($camelcase_seeded);
571
572 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700573 my $camelcase_cache = "";
574 my @include_files = ();
575
576 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700577
Richard Genoud3645e322014-02-10 14:25:32 -0800578 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700579 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
580 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700581 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700582 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700583 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700584 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700585 @include_files = split('\n', $files);
586 foreach my $file (@include_files) {
587 my $date = POSIX::strftime("%Y%m%d%H%M",
588 localtime((stat $file)[9]));
589 $last_mod_date = $date if ($last_mod_date < $date);
590 }
591 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700592 }
Joe Perchesc707a812013-07-08 16:00:43 -0700593
594 if ($camelcase_cache ne "" && -f $camelcase_cache) {
595 open(my $camelcase_file, '<', "$camelcase_cache")
596 or warn "$P: Can't read '$camelcase_cache' $!\n";
597 while (<$camelcase_file>) {
598 chomp;
599 $camelcase{$_} = 1;
600 }
601 close($camelcase_file);
602
603 return;
604 }
605
Richard Genoud3645e322014-02-10 14:25:32 -0800606 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700607 $files = `git ls-files "include/*.h"`;
608 @include_files = split('\n', $files);
609 }
610
Joe Perches34456862013-07-03 15:05:34 -0700611 foreach my $file (@include_files) {
612 seed_camelcase_file($file);
613 }
Joe Perches351b2a12013-07-03 15:05:36 -0700614
Joe Perchesc707a812013-07-08 16:00:43 -0700615 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700616 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700617 open(my $camelcase_file, '>', "$camelcase_cache")
618 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700619 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
620 print $camelcase_file ("$_\n");
621 }
622 close($camelcase_file);
623 }
Joe Perches34456862013-07-03 15:05:34 -0700624}
625
Joe Perchesd311cd42014-08-06 16:10:57 -0700626sub git_commit_info {
627 my ($commit, $id, $desc) = @_;
628
629 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
630
631 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
632 $output =~ s/^\s*//gm;
633 my @lines = split("\n", $output);
634
635 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
636# Maybe one day convert this block of bash into something that returns
637# all matching commit ids, but it's very slow...
638#
639# echo "checking commits $1..."
640# git rev-list --remotes | grep -i "^$1" |
641# while read line ; do
642# git log --format='%H %s' -1 $line |
643# echo "commit $(cut -c 1-12,41-)"
644# done
645 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
646 } else {
647 $id = substr($lines[0], 0, 12);
648 $desc = substr($lines[0], 41);
649 }
650
651 return ($id, $desc);
652}
653
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700654$chk_signoff = 0 if ($file);
655
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700656my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800657my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700658my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700659my @fixed_inserted = ();
660my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700661my $fixlinenr = -1;
662
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800663my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700664for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800665 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700666 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800667 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700668 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800669 } elsif ($filename eq '-') {
670 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700671 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800672 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700673 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700674 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800675 if ($filename eq '-') {
676 $vname = 'Your patch';
677 } else {
678 $vname = $filename;
679 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800680 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700681 chomp;
682 push(@rawlines, $_);
683 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800684 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800685 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700686 $exit = 1;
687 }
688 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800689 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700690 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700691 @fixed_inserted = ();
692 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700693 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700694}
695
696exit($exit);
697
698sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700699 my ($root) = @_;
700
701 my @tree_check = (
702 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
703 "README", "Documentation", "arch", "include", "drivers",
704 "fs", "init", "ipc", "kernel", "lib", "scripts",
705 );
706
707 foreach my $check (@tree_check) {
708 if (! -e $root . '/' . $check) {
709 return 0;
710 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700711 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700712 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700713}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700714
Joe Perches20112472011-07-25 17:13:23 -0700715sub parse_email {
716 my ($formatted_email) = @_;
717
718 my $name = "";
719 my $address = "";
720 my $comment = "";
721
722 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
723 $name = $1;
724 $address = $2;
725 $comment = $3 if defined $3;
726 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
727 $address = $1;
728 $comment = $2 if defined $2;
729 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
730 $address = $1;
731 $comment = $2 if defined $2;
732 $formatted_email =~ s/$address.*$//;
733 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700734 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700735 $name =~ s/^\"|\"$//g;
736 # If there's a name left after stripping spaces and
737 # leading quotes, and the address doesn't have both
738 # leading and trailing angle brackets, the address
739 # is invalid. ie:
740 # "joe smith joe@smith.com" bad
741 # "joe smith <joe@smith.com" bad
742 if ($name ne "" && $address !~ /^<[^>]+>$/) {
743 $name = "";
744 $address = "";
745 $comment = "";
746 }
747 }
748
Joe Perches3705ce52013-07-03 15:05:31 -0700749 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700750 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700751 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700752 $address =~ s/^\<|\>$//g;
753
754 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
755 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
756 $name = "\"$name\"";
757 }
758
759 return ($name, $address, $comment);
760}
761
762sub format_email {
763 my ($name, $address) = @_;
764
765 my $formatted_email;
766
Joe Perches3705ce52013-07-03 15:05:31 -0700767 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700768 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700769 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700770
771 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
772 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
773 $name = "\"$name\"";
774 }
775
776 if ("$name" eq "") {
777 $formatted_email = "$address";
778 } else {
779 $formatted_email = "$name <$address>";
780 }
781
782 return $formatted_email;
783}
784
Joe Perchesd311cd42014-08-06 16:10:57 -0700785sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700786 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700787
Joe Perchesbd474ca2014-08-06 16:11:10 -0700788 foreach my $path (split(/:/, $ENV{PATH})) {
789 if (-e "$path/$bin") {
790 return "$path/$bin";
791 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700792 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700793
Joe Perchesbd474ca2014-08-06 16:11:10 -0700794 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700795}
796
Joe Perches000d1cc12011-07-25 17:13:25 -0700797sub which_conf {
798 my ($conf) = @_;
799
800 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
801 if (-e "$path/$conf") {
802 return "$path/$conf";
803 }
804 }
805
806 return "";
807}
808
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700809sub expand_tabs {
810 my ($str) = @_;
811
812 my $res = '';
813 my $n = 0;
814 for my $c (split(//, $str)) {
815 if ($c eq "\t") {
816 $res .= ' ';
817 $n++;
818 for (; ($n % 8) != 0; $n++) {
819 $res .= ' ';
820 }
821 next;
822 }
823 $res .= $c;
824 $n++;
825 }
826
827 return $res;
828}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700829sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700830 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700831 return $res;
832}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700833
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700834sub line_stats {
835 my ($line) = @_;
836
837 # Drop the diff line leader and expand tabs
838 $line =~ s/^.//;
839 $line = expand_tabs($line);
840
841 # Pick the indent from the front of the line.
842 my ($white) = ($line =~ /^(\s*)/);
843
844 return (length($line), length($white));
845}
846
Andy Whitcroft773647a2008-03-28 14:15:58 -0700847my $sanitise_quote = '';
848
849sub sanitise_line_reset {
850 my ($in_comment) = @_;
851
852 if ($in_comment) {
853 $sanitise_quote = '*/';
854 } else {
855 $sanitise_quote = '';
856 }
857}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700858sub sanitise_line {
859 my ($line) = @_;
860
861 my $res = '';
862 my $l = '';
863
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800864 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700865 my $off = 0;
866 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700867
Andy Whitcroft773647a2008-03-28 14:15:58 -0700868 # Always copy over the diff marker.
869 $res = substr($line, 0, 1);
870
871 for ($off = 1; $off < length($line); $off++) {
872 $c = substr($line, $off, 1);
873
874 # Comments we are wacking completly including the begin
875 # and end, all to $;.
876 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
877 $sanitise_quote = '*/';
878
879 substr($res, $off, 2, "$;$;");
880 $off++;
881 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800882 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700883 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700884 $sanitise_quote = '';
885 substr($res, $off, 2, "$;$;");
886 $off++;
887 next;
888 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700889 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
890 $sanitise_quote = '//';
891
892 substr($res, $off, 2, $sanitise_quote);
893 $off++;
894 next;
895 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700896
897 # A \ in a string means ignore the next character.
898 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
899 $c eq "\\") {
900 substr($res, $off, 2, 'XX');
901 $off++;
902 next;
903 }
904 # Regular quotes.
905 if ($c eq "'" || $c eq '"') {
906 if ($sanitise_quote eq '') {
907 $sanitise_quote = $c;
908
909 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700910 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700911 } elsif ($sanitise_quote eq $c) {
912 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700913 }
914 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700915
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800916 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700917 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
918 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700919 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
920 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700921 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
922 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700923 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700924 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700925 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800926 }
927
Daniel Walker113f04a2009-09-21 17:04:35 -0700928 if ($sanitise_quote eq '//') {
929 $sanitise_quote = '';
930 }
931
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800932 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700933 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800934 my $clean = 'X' x length($1);
935 $res =~ s@\<.*\>@<$clean>@;
936
937 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700938 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800939 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700940 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800941 }
942
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700943 return $res;
944}
945
Joe Perchesa6962d72013-04-29 16:18:13 -0700946sub get_quoted_string {
947 my ($line, $rawline) = @_;
948
949 return "" if ($line !~ m/(\"[X]+\")/g);
950 return substr($rawline, $-[0], $+[0] - $-[0]);
951}
952
Andy Whitcroft8905a672007-11-28 16:21:06 -0800953sub ctx_statement_block {
954 my ($linenr, $remain, $off) = @_;
955 my $line = $linenr - 1;
956 my $blk = '';
957 my $soff = $off;
958 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700959 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800960
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800961 my $loff = 0;
962
Andy Whitcroft8905a672007-11-28 16:21:06 -0800963 my $type = '';
964 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800965 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800966 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800967 my $c;
968 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800969
970 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800971 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800972 @stack = (['', 0]) if ($#stack == -1);
973
Andy Whitcroft773647a2008-03-28 14:15:58 -0700974 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800975 # If we are about to drop off the end, pull in more
976 # context.
977 if ($off >= $len) {
978 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700979 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800980 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800981 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800982 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800983 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800984 $len = length($blk);
985 $line++;
986 last;
987 }
988 # Bail if there is no further context.
989 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800990 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800991 last;
992 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800993 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
994 $level++;
995 $type = '#';
996 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800997 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800998 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800999 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001000 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001001
Andy Whitcroft773647a2008-03-28 14:15:58 -07001002 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001003
1004 # Handle nested #if/#else.
1005 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1006 push(@stack, [ $type, $level ]);
1007 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1008 ($type, $level) = @{$stack[$#stack - 1]};
1009 } elsif ($remainder =~ /^#\s*endif\b/) {
1010 ($type, $level) = @{pop(@stack)};
1011 }
1012
Andy Whitcroft8905a672007-11-28 16:21:06 -08001013 # Statement ends at the ';' or a close '}' at the
1014 # outermost level.
1015 if ($level == 0 && $c eq ';') {
1016 last;
1017 }
1018
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001019 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001020 if ($level == 0 && $coff_set == 0 &&
1021 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1022 $remainder =~ /^(else)(?:\s|{)/ &&
1023 $remainder !~ /^else\s+if\b/) {
1024 $coff = $off + length($1) - 1;
1025 $coff_set = 1;
1026 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1027 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001028 }
1029
Andy Whitcroft8905a672007-11-28 16:21:06 -08001030 if (($type eq '' || $type eq '(') && $c eq '(') {
1031 $level++;
1032 $type = '(';
1033 }
1034 if ($type eq '(' && $c eq ')') {
1035 $level--;
1036 $type = ($level != 0)? '(' : '';
1037
1038 if ($level == 0 && $coff < $soff) {
1039 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001040 $coff_set = 1;
1041 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001042 }
1043 }
1044 if (($type eq '' || $type eq '{') && $c eq '{') {
1045 $level++;
1046 $type = '{';
1047 }
1048 if ($type eq '{' && $c eq '}') {
1049 $level--;
1050 $type = ($level != 0)? '{' : '';
1051
1052 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001053 if (substr($blk, $off + 1, 1) eq ';') {
1054 $off++;
1055 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001056 last;
1057 }
1058 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001059 # Preprocessor commands end at the newline unless escaped.
1060 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1061 $level--;
1062 $type = '';
1063 $off++;
1064 last;
1065 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001066 $off++;
1067 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001068 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001069 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001070 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001071 $line++;
1072 $remain--;
1073 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001074
1075 my $statement = substr($blk, $soff, $off - $soff + 1);
1076 my $condition = substr($blk, $soff, $coff - $soff + 1);
1077
1078 #warn "STATEMENT<$statement>\n";
1079 #warn "CONDITION<$condition>\n";
1080
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001082
1083 return ($statement, $condition,
1084 $line, $remain + 1, $off - $loff + 1, $level);
1085}
1086
Andy Whitcroftcf655042008-03-04 14:28:20 -08001087sub statement_lines {
1088 my ($stmt) = @_;
1089
1090 # Strip the diff line prefixes and rip blank lines at start and end.
1091 $stmt =~ s/(^|\n)./$1/g;
1092 $stmt =~ s/^\s*//;
1093 $stmt =~ s/\s*$//;
1094
1095 my @stmt_lines = ($stmt =~ /\n/g);
1096
1097 return $#stmt_lines + 2;
1098}
1099
1100sub statement_rawlines {
1101 my ($stmt) = @_;
1102
1103 my @stmt_lines = ($stmt =~ /\n/g);
1104
1105 return $#stmt_lines + 2;
1106}
1107
1108sub statement_block_size {
1109 my ($stmt) = @_;
1110
1111 $stmt =~ s/(^|\n)./$1/g;
1112 $stmt =~ s/^\s*{//;
1113 $stmt =~ s/}\s*$//;
1114 $stmt =~ s/^\s*//;
1115 $stmt =~ s/\s*$//;
1116
1117 my @stmt_lines = ($stmt =~ /\n/g);
1118 my @stmt_statements = ($stmt =~ /;/g);
1119
1120 my $stmt_lines = $#stmt_lines + 2;
1121 my $stmt_statements = $#stmt_statements + 1;
1122
1123 if ($stmt_lines > $stmt_statements) {
1124 return $stmt_lines;
1125 } else {
1126 return $stmt_statements;
1127 }
1128}
1129
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001130sub ctx_statement_full {
1131 my ($linenr, $remain, $off) = @_;
1132 my ($statement, $condition, $level);
1133
1134 my (@chunks);
1135
Andy Whitcroftcf655042008-03-04 14:28:20 -08001136 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001137 ($statement, $condition, $linenr, $remain, $off, $level) =
1138 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001139 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001140 push(@chunks, [ $condition, $statement ]);
1141 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1142 return ($level, $linenr, @chunks);
1143 }
1144
1145 # Pull in the following conditional/block pairs and see if they
1146 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001147 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001148 ($statement, $condition, $linenr, $remain, $off, $level) =
1149 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001150 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001151 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001152 #print "C: push\n";
1153 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001154 }
1155
1156 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001157}
1158
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001159sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001160 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001161 my $line;
1162 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001163 my $blk = '';
1164 my @o;
1165 my @c;
1166 my @res = ();
1167
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001168 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001169 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001170 for ($line = $start; $remain > 0; $line++) {
1171 next if ($rawlines[$line] =~ /^-/);
1172 $remain--;
1173
1174 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001175
1176 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001177 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001178 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001179 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001180 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001181 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001182 $level = pop(@stack);
1183 }
1184
Andy Whitcroft01464f32010-10-26 14:23:19 -07001185 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001186 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1187 if ($off > 0) {
1188 $off--;
1189 next;
1190 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001191
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001192 if ($c eq $close && $level > 0) {
1193 $level--;
1194 last if ($level == 0);
1195 } elsif ($c eq $open) {
1196 $level++;
1197 }
1198 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001199
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001200 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001201 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001202 }
1203
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001204 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001205 }
1206
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001207 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001208}
1209sub ctx_block_outer {
1210 my ($linenr, $remain) = @_;
1211
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001212 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1213 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001214}
1215sub ctx_block {
1216 my ($linenr, $remain) = @_;
1217
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001218 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1219 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001220}
1221sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001222 my ($linenr, $remain, $off) = @_;
1223
1224 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1225 return @r;
1226}
1227sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001228 my ($linenr, $remain) = @_;
1229
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001230 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001231}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001232sub ctx_statement_level {
1233 my ($linenr, $remain, $off) = @_;
1234
1235 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1236}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001237
1238sub ctx_locate_comment {
1239 my ($first_line, $end_line) = @_;
1240
1241 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001242 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001243 return $current_comment if (defined $current_comment);
1244
1245 # Look through the context and try and figure out if there is a
1246 # comment.
1247 my $in_comment = 0;
1248 $current_comment = '';
1249 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001250 my $line = $rawlines[$linenr - 1];
1251 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001252 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1253 $in_comment = 1;
1254 }
1255 if ($line =~ m@/\*@) {
1256 $in_comment = 1;
1257 }
1258 if (!$in_comment && $current_comment ne '') {
1259 $current_comment = '';
1260 }
1261 $current_comment .= $line . "\n" if ($in_comment);
1262 if ($line =~ m@\*/@) {
1263 $in_comment = 0;
1264 }
1265 }
1266
1267 chomp($current_comment);
1268 return($current_comment);
1269}
1270sub ctx_has_comment {
1271 my ($first_line, $end_line) = @_;
1272 my $cmt = ctx_locate_comment($first_line, $end_line);
1273
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001274 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001275 ##print "CMMT: $cmt\n";
1276
1277 return ($cmt ne '');
1278}
1279
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001280sub raw_line {
1281 my ($linenr, $cnt) = @_;
1282
1283 my $offset = $linenr - 1;
1284 $cnt++;
1285
1286 my $line;
1287 while ($cnt) {
1288 $line = $rawlines[$offset++];
1289 next if (defined($line) && $line =~ /^-/);
1290 $cnt--;
1291 }
1292
1293 return $line;
1294}
1295
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001296sub cat_vet {
1297 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001298 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001299
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001300 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001301 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1302 $res .= $1;
1303 if ($2 ne '') {
1304 $coded = sprintf("^%c", unpack('C', $2) + 64);
1305 $res .= $coded;
1306 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001307 }
1308 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001309
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001310 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001311}
1312
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001313my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001314my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001315my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001316my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317
1318sub annotate_reset {
1319 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001320 $av_pending = '_';
1321 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001322 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001323}
1324
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001325sub annotate_values {
1326 my ($stream, $type) = @_;
1327
1328 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001329 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001330 my $cur = $stream;
1331
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001332 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001333
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001335 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001336 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001337 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001338 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001339 print "WS($1)\n" if ($dbg_values > 1);
1340 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001341 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001342 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001343 }
1344
Florian Micklerc023e4732011-01-12 16:59:58 -08001345 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001346 print "CAST($1)\n" if ($dbg_values > 1);
1347 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001348 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001349
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001350 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001351 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001352 $type = 'T';
1353
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001354 } elsif ($cur =~ /^($Modifier)\s*/) {
1355 print "MODIFIER($1)\n" if ($dbg_values > 1);
1356 $type = 'T';
1357
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001358 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001359 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001360 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001361 push(@av_paren_type, $type);
1362 if ($2 ne '') {
1363 $av_pending = 'N';
1364 }
1365 $type = 'E';
1366
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001367 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001368 print "UNDEF($1)\n" if ($dbg_values > 1);
1369 $av_preprocessor = 1;
1370 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001371
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001372 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001373 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001374 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001375
1376 push(@av_paren_type, $type);
1377 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001378 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001379
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001380 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001381 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1382 $av_preprocessor = 1;
1383
1384 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1385
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001386 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001387
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001388 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001389 print "PRE_END($1)\n" if ($dbg_values > 1);
1390
1391 $av_preprocessor = 1;
1392
1393 # Assume all arms of the conditional end as this
1394 # one does, and continue as if the #endif was not here.
1395 pop(@av_paren_type);
1396 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001397 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001398
1399 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001400 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001401
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001402 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1403 print "ATTR($1)\n" if ($dbg_values > 1);
1404 $av_pending = $type;
1405 $type = 'N';
1406
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001407 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001408 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001409 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001410 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411 }
1412 $type = 'N';
1413
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001414 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001415 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001416 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001417 $type = 'N';
1418
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001419 } elsif ($cur =~/^(case)/o) {
1420 print "CASE($1)\n" if ($dbg_values > 1);
1421 $av_pend_colon = 'C';
1422 $type = 'N';
1423
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001424 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001425 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001426 $type = 'N';
1427
1428 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001429 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001430 push(@av_paren_type, $av_pending);
1431 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001432 $type = 'N';
1433
1434 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001435 my $new_type = pop(@av_paren_type);
1436 if ($new_type ne '_') {
1437 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001438 print "PAREN('$1') -> $type\n"
1439 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001440 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001441 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001442 }
1443
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001444 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001445 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001446 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001447 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001448
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001449 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1450 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001451 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001452 } elsif ($type eq 'E') {
1453 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001454 }
1455 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1456 $type = 'V';
1457
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001458 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001459 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 $type = 'V';
1461
1462 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001464 $type = 'N';
1465
Andy Whitcroftcf655042008-03-04 14:28:20 -08001466 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001467 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001468 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001469 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001470
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001471 } elsif ($cur =~/^(,)/) {
1472 print "COMMA($1)\n" if ($dbg_values > 1);
1473 $type = 'C';
1474
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001475 } elsif ($cur =~ /^(\?)/o) {
1476 print "QUESTION($1)\n" if ($dbg_values > 1);
1477 $type = 'N';
1478
1479 } elsif ($cur =~ /^(:)/o) {
1480 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1481
1482 substr($var, length($res), 1, $av_pend_colon);
1483 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1484 $type = 'E';
1485 } else {
1486 $type = 'N';
1487 }
1488 $av_pend_colon = 'O';
1489
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001490 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001491 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001492 $type = 'N';
1493
Andy Whitcroft0d413862008-10-15 22:02:16 -07001494 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001495 my $variant;
1496
1497 print "OPV($1)\n" if ($dbg_values > 1);
1498 if ($type eq 'V') {
1499 $variant = 'B';
1500 } else {
1501 $variant = 'U';
1502 }
1503
1504 substr($var, length($res), 1, $variant);
1505 $type = 'N';
1506
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001507 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001508 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001509 if ($1 ne '++' && $1 ne '--') {
1510 $type = 'N';
1511 }
1512
1513 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001514 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001515 }
1516 if (defined $1) {
1517 $cur = substr($cur, length($1));
1518 $res .= $type x length($1);
1519 }
1520 }
1521
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001522 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001523}
1524
Andy Whitcroft8905a672007-11-28 16:21:06 -08001525sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001526 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001527 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001528 ^(?:
1529 $Modifier|
1530 $Storage|
1531 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001532 DEFINE_\S+
1533 )$|
1534 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001535 goto|
1536 return|
1537 case|
1538 else|
1539 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001540 do|
1541 \#|
1542 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001543 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001544 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001545 )}x;
1546 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1547 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001548 # Check for modifiers.
1549 $possible =~ s/\s*$Storage\s*//g;
1550 $possible =~ s/\s*$Sparse\s*//g;
1551 if ($possible =~ /^\s*$/) {
1552
1553 } elsif ($possible =~ /\s/) {
1554 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001555 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001556 if ($modifier !~ $notPermitted) {
1557 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1558 push(@modifierList, $modifier);
1559 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001560 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001561
1562 } else {
1563 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1564 push(@typeList, $possible);
1565 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001566 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001567 } else {
1568 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001569 }
1570}
1571
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001572my $prefix = '';
1573
Joe Perches000d1cc12011-07-25 17:13:25 -07001574sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001575 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001576
Joe Perchescbec18a2014-04-03 14:49:19 -07001577 return defined $use_type{$type} if (scalar keys %use_type > 0);
1578
1579 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001580}
1581
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001582sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001583 my ($level, $type, $msg) = @_;
1584
1585 if (!show_type($type) ||
1586 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001587 return 0;
1588 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001589 my $line;
1590 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001591 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001592 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001593 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001594 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001595 $line = (split('\n', $line))[0] . "\n" if ($terse);
1596
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001597 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001598
1599 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001600}
Joe Perchescbec18a2014-04-03 14:49:19 -07001601
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001602sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001603 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001604}
Joe Perches000d1cc12011-07-25 17:13:25 -07001605
Joe Perchesd752fcc2014-08-06 16:11:05 -07001606sub fixup_current_range {
1607 my ($lineRef, $offset, $length) = @_;
1608
1609 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1610 my $o = $1;
1611 my $l = $2;
1612 my $no = $o + $offset;
1613 my $nl = $l + $length;
1614 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1615 }
1616}
1617
1618sub fix_inserted_deleted_lines {
1619 my ($linesRef, $insertedRef, $deletedRef) = @_;
1620
1621 my $range_last_linenr = 0;
1622 my $delta_offset = 0;
1623
1624 my $old_linenr = 0;
1625 my $new_linenr = 0;
1626
1627 my $next_insert = 0;
1628 my $next_delete = 0;
1629
1630 my @lines = ();
1631
1632 my $inserted = @{$insertedRef}[$next_insert++];
1633 my $deleted = @{$deletedRef}[$next_delete++];
1634
1635 foreach my $old_line (@{$linesRef}) {
1636 my $save_line = 1;
1637 my $line = $old_line; #don't modify the array
1638 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1639 $delta_offset = 0;
1640 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1641 $range_last_linenr = $new_linenr;
1642 fixup_current_range(\$line, $delta_offset, 0);
1643 }
1644
1645 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1646 $deleted = @{$deletedRef}[$next_delete++];
1647 $save_line = 0;
1648 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1649 }
1650
1651 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1652 push(@lines, ${$inserted}{'LINE'});
1653 $inserted = @{$insertedRef}[$next_insert++];
1654 $new_linenr++;
1655 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1656 }
1657
1658 if ($save_line) {
1659 push(@lines, $line);
1660 $new_linenr++;
1661 }
1662
1663 $old_linenr++;
1664 }
1665
1666 return @lines;
1667}
1668
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001669sub fix_insert_line {
1670 my ($linenr, $line) = @_;
1671
1672 my $inserted = {
1673 LINENR => $linenr,
1674 LINE => $line,
1675 };
1676 push(@fixed_inserted, $inserted);
1677}
1678
1679sub fix_delete_line {
1680 my ($linenr, $line) = @_;
1681
1682 my $deleted = {
1683 LINENR => $linenr,
1684 LINE => $line,
1685 };
1686
1687 push(@fixed_deleted, $deleted);
1688}
1689
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001690sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001691 my ($type, $msg) = @_;
1692
1693 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001694 our $clean = 0;
1695 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001696 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001697 }
Joe Perches3705ce52013-07-03 15:05:31 -07001698 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001699}
1700sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001701 my ($type, $msg) = @_;
1702
1703 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001704 our $clean = 0;
1705 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001706 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001707 }
Joe Perches3705ce52013-07-03 15:05:31 -07001708 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001709}
1710sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001711 my ($type, $msg) = @_;
1712
1713 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001714 our $clean = 0;
1715 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001716 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001717 }
Joe Perches3705ce52013-07-03 15:05:31 -07001718 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001719}
1720
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001721sub check_absolute_file {
1722 my ($absolute, $herecurr) = @_;
1723 my $file = $absolute;
1724
1725 ##print "absolute<$absolute>\n";
1726
1727 # See if any suffix of this path is a path within the tree.
1728 while ($file =~ s@^[^/]*/@@) {
1729 if (-f "$root/$file") {
1730 ##print "file<$file>\n";
1731 last;
1732 }
1733 }
1734 if (! -f _) {
1735 return 0;
1736 }
1737
1738 # It is, so see if the prefix is acceptable.
1739 my $prefix = $absolute;
1740 substr($prefix, -length($file)) = '';
1741
1742 ##print "prefix<$prefix>\n";
1743 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001744 WARN("USE_RELATIVE_PATH",
1745 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001746 }
1747}
1748
Joe Perches3705ce52013-07-03 15:05:31 -07001749sub trim {
1750 my ($string) = @_;
1751
Joe Perchesb34c6482013-09-11 14:24:01 -07001752 $string =~ s/^\s+|\s+$//g;
1753
1754 return $string;
1755}
1756
1757sub ltrim {
1758 my ($string) = @_;
1759
1760 $string =~ s/^\s+//;
1761
1762 return $string;
1763}
1764
1765sub rtrim {
1766 my ($string) = @_;
1767
1768 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001769
1770 return $string;
1771}
1772
Joe Perches52ea8502013-11-12 15:10:09 -08001773sub string_find_replace {
1774 my ($string, $find, $replace) = @_;
1775
1776 $string =~ s/$find/$replace/g;
1777
1778 return $string;
1779}
1780
Joe Perches3705ce52013-07-03 15:05:31 -07001781sub tabify {
1782 my ($leading) = @_;
1783
1784 my $source_indent = 8;
1785 my $max_spaces_before_tab = $source_indent - 1;
1786 my $spaces_to_tab = " " x $source_indent;
1787
1788 #convert leading spaces to tabs
1789 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1790 #Remove spaces before a tab
1791 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1792
1793 return "$leading";
1794}
1795
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001796sub pos_last_openparen {
1797 my ($line) = @_;
1798
1799 my $pos = 0;
1800
1801 my $opens = $line =~ tr/\(/\(/;
1802 my $closes = $line =~ tr/\)/\)/;
1803
1804 my $last_openparen = 0;
1805
1806 if (($opens == 0) || ($closes >= $opens)) {
1807 return -1;
1808 }
1809
1810 my $len = length($line);
1811
1812 for ($pos = 0; $pos < $len; $pos++) {
1813 my $string = substr($line, $pos);
1814 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1815 $pos += length($1) - 1;
1816 } elsif (substr($line, $pos, 1) eq '(') {
1817 $last_openparen = $pos;
1818 } elsif (index($string, '(') == -1) {
1819 last;
1820 }
1821 }
1822
Joe Perches91cb5192014-04-03 14:49:32 -07001823 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001824}
1825
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001826sub process {
1827 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001828
1829 my $linenr=0;
1830 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001832 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001833 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001834
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001835 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001836 my $indent;
1837 my $previndent=0;
1838 my $stashindent=0;
1839
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001840 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001841 my $signoff = 0;
1842 my $is_patch = 0;
1843
Joe Perches29ee1b02014-08-06 16:10:35 -07001844 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001845 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001846 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001847 my $non_utf8_charset = 0;
1848
Joe Perches365dd4e2014-08-06 16:10:42 -07001849 my $last_blank_line = 0;
1850
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001851 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001852 our $cnt_lines = 0;
1853 our $cnt_error = 0;
1854 our $cnt_warn = 0;
1855 our $cnt_chk = 0;
1856
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001857 # Trace the real file/line as we go.
1858 my $realfile = '';
1859 my $realline = 0;
1860 my $realcnt = 0;
1861 my $here = '';
1862 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001863 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001864 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001865 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001866
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001867 my $prev_values = 'E';
1868
1869 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001870 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001871 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001872 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001873 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001874
Joe Perches7e51f192013-09-11 14:23:57 -07001875 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001876
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001877 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001878 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001879 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001880 my @setup_docs = ();
1881 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001882
Joe Perchesd8b07712013-11-12 15:10:06 -08001883 my $camelcase_file_seeded = 0;
1884
Andy Whitcroft773647a2008-03-28 14:15:58 -07001885 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001886 my $line;
1887 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001888 $linenr++;
1889 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001890
Joe Perches3705ce52013-07-03 15:05:31 -07001891 push(@fixed, $rawline) if ($fix);
1892
Andy Whitcroft773647a2008-03-28 14:15:58 -07001893 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001894 $setup_docs = 0;
1895 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1896 $setup_docs = 1;
1897 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001898 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001899 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001900 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1901 $realline=$1-1;
1902 if (defined $2) {
1903 $realcnt=$3+1;
1904 } else {
1905 $realcnt=1+1;
1906 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001907 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001908
1909 # Guestimate if this is a continuing comment. Run
1910 # the context looking for a comment "edge". If this
1911 # edge is a close comment then we must be in a comment
1912 # at context start.
1913 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001914 my $cnt = $realcnt;
1915 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1916 next if (defined $rawlines[$ln - 1] &&
1917 $rawlines[$ln - 1] =~ /^-/);
1918 $cnt--;
1919 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001920 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001921 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1922 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1923 ($edge) = $1;
1924 last;
1925 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001926 }
1927 if (defined $edge && $edge eq '*/') {
1928 $in_comment = 1;
1929 }
1930
1931 # Guestimate if this is a continuing comment. If this
1932 # is the start of a diff block and this line starts
1933 # ' *' then it is very likely a comment.
1934 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001935 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001936 {
1937 $in_comment = 1;
1938 }
1939
1940 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1941 sanitise_line_reset($in_comment);
1942
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001943 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001944 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001945 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001946 $line = sanitise_line($rawline);
1947 }
1948 push(@lines, $line);
1949
1950 if ($realcnt > 1) {
1951 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1952 } else {
1953 $realcnt = 0;
1954 }
1955
1956 #print "==>$rawline\n";
1957 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001958
1959 if ($setup_docs && $line =~ /^\+/) {
1960 push(@setup_docs, $line);
1961 }
1962 }
1963
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001964 $prefix = '';
1965
Andy Whitcroft773647a2008-03-28 14:15:58 -07001966 $realcnt = 0;
1967 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001968 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001969 foreach my $line (@lines) {
1970 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001971 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001972 my $sline = $line; #copy of $line
1973 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001974
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001975 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001976
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001977#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001978 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001979 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001980 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001981 $realline=$1-1;
1982 if (defined $2) {
1983 $realcnt=$3+1;
1984 } else {
1985 $realcnt=1+1;
1986 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001987 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001988 $prev_values = 'E';
1989
Andy Whitcroft773647a2008-03-28 14:15:58 -07001990 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001991 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001992 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001993 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001994 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001995
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001996# track the line number as we move through the hunk, note that
1997# new versions of GNU diff omit the leading space on completely
1998# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001999 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002000 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002001 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002002
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002003 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002004 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002005
2006 # Track the previous line.
2007 ($prevline, $stashline) = ($stashline, $line);
2008 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002009 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2010
Andy Whitcroft773647a2008-03-28 14:15:58 -07002011 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002012
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002013 } elsif ($realcnt == 1) {
2014 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002015 }
2016
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002017 my $hunk_line = ($realcnt != 0);
2018
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002019#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002020 $prefix = "$filename:$realline: " if ($emacs && $file);
2021 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2022
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002023 $here = "#$linenr: " if (!$file);
2024 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002025
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002026 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002027 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002028 if ($line =~ /^diff --git.*?(\S+)$/) {
2029 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002030 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002031 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002032 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002033 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002034 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002035 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002036 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002037
2038 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002039 if (!$file && $tree && $p1_prefix ne '' &&
2040 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002041 WARN("PATCH_PREFIX",
2042 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002043 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002044
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002045 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002046 ERROR("MODIFIED_INCLUDE_ASM",
2047 "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 -07002048 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002049 $found_file = 1;
2050 }
2051
2052 if ($found_file) {
2053 if ($realfile =~ m@^(drivers/net/|net/)@) {
2054 $check = 1;
2055 } else {
2056 $check = $check_orig;
2057 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002058 next;
2059 }
2060
Randy Dunlap389834b2007-06-08 13:47:03 -07002061 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002062
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002063 my $hereline = "$here\n$rawline\n";
2064 my $herecurr = "$here\n$rawline\n";
2065 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002066
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002067 $cnt_lines++ if ($realcnt != 0);
2068
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002069# Check for incorrect file permissions
2070 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2071 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002072 if ($realfile !~ m@scripts/@ &&
2073 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002074 ERROR("EXECUTE_PERMISSIONS",
2075 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002076 }
2077 }
2078
Joe Perches20112472011-07-25 17:13:23 -07002079# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002080 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002081 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002082 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002083 }
2084
Joe Perchese0d975b2014-12-10 15:51:49 -08002085# Check if MAINTAINERS is being updated. If so, there's probably no need to
2086# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2087 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2088 $reported_maintainer_file = 1;
2089 }
2090
Joe Perches20112472011-07-25 17:13:23 -07002091# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002092 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002093 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002094 my $space_before = $1;
2095 my $sign_off = $2;
2096 my $space_after = $3;
2097 my $email = $4;
2098 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2099
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002100 if ($sign_off !~ /$signature_tags/) {
2101 WARN("BAD_SIGN_OFF",
2102 "Non-standard signature: $sign_off\n" . $herecurr);
2103 }
Joe Perches20112472011-07-25 17:13:23 -07002104 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002105 if (WARN("BAD_SIGN_OFF",
2106 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2107 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002108 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002109 "$ucfirst_sign_off $email";
2110 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002111 }
Joe Perches20112472011-07-25 17:13:23 -07002112 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002113 if (WARN("BAD_SIGN_OFF",
2114 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2115 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002116 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002117 "$ucfirst_sign_off $email";
2118 }
2119
Joe Perches20112472011-07-25 17:13:23 -07002120 }
2121 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002122 if (WARN("BAD_SIGN_OFF",
2123 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2124 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002125 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002126 "$ucfirst_sign_off $email";
2127 }
Joe Perches20112472011-07-25 17:13:23 -07002128 }
2129
2130 my ($email_name, $email_address, $comment) = parse_email($email);
2131 my $suggested_email = format_email(($email_name, $email_address));
2132 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002133 ERROR("BAD_SIGN_OFF",
2134 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002135 } else {
2136 my $dequoted = $suggested_email;
2137 $dequoted =~ s/^"//;
2138 $dequoted =~ s/" </ </;
2139 # Don't force email to have quotes
2140 # Allow just an angle bracketed address
2141 if ("$dequoted$comment" ne $email &&
2142 "<$email_address>$comment" ne $email &&
2143 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002144 WARN("BAD_SIGN_OFF",
2145 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002146 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002147 }
Joe Perches7e51f192013-09-11 14:23:57 -07002148
2149# Check for duplicate signatures
2150 my $sig_nospace = $line;
2151 $sig_nospace =~ s/\s//g;
2152 $sig_nospace = lc($sig_nospace);
2153 if (defined $signatures{$sig_nospace}) {
2154 WARN("BAD_SIGN_OFF",
2155 "Duplicate signature\n" . $herecurr);
2156 } else {
2157 $signatures{$sig_nospace} = 1;
2158 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002159 }
2160
Joe Perches9b3189eb2014-06-04 16:12:10 -07002161# Check for old stable address
2162 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2163 ERROR("STABLE_ADDRESS",
2164 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2165 }
2166
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002167# Check for unwanted Gerrit info
2168 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2169 ERROR("GERRIT_CHANGE_ID",
2170 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2171 }
2172
Joe Perchesd311cd42014-08-06 16:10:57 -07002173# Check for improperly formed commit descriptions
2174 if ($in_commit_log &&
2175 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
Joe Perches66881732014-09-09 14:50:55 -07002176 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2177 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2178 defined $rawlines[$linenr] &&
2179 $rawlines[$linenr] =~ /^\s*\("/))) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002180 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2181 my $init_char = $1;
2182 my $orig_commit = lc($2);
2183 my $id = '01234567890ab';
2184 my $desc = 'commit description';
2185 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2186 ERROR("GIT_COMMIT_ID",
Joe Perches3f6316b42014-08-29 15:18:26 -07002187 "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 -07002188 }
2189
Joe Perches13f19372014-08-06 16:10:59 -07002190# Check for added, moved or deleted files
2191 if (!$reported_maintainer_file && !$in_commit_log &&
2192 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2193 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2194 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2195 (defined($1) || defined($2))))) {
2196 $reported_maintainer_file = 1;
2197 WARN("FILE_PATH_CHANGES",
2198 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2199 }
2200
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002201# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002202 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002203 ERROR("CORRUPTED_PATCH",
2204 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002205 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002206 }
2207
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002208# Check for absolute kernel paths.
2209 if ($tree) {
2210 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2211 my $file = $1;
2212
2213 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2214 check_absolute_file($1, $herecurr)) {
2215 #
2216 } else {
2217 check_absolute_file($file, $herecurr);
2218 }
2219 }
2220 }
2221
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002222# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2223 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002224 $rawline !~ m/^$UTF8*$/) {
2225 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2226
2227 my $blank = copy_spacing($rawline);
2228 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2229 my $hereptr = "$hereline$ptr\n";
2230
Joe Perches34d99212011-07-25 17:13:26 -07002231 CHK("INVALID_UTF8",
2232 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002233 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002234
Joe Perches15662b32011-10-31 17:13:12 -07002235# Check if it's the start of a commit log
2236# (not a header line and we haven't seen the patch filename)
2237 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002238 !($rawline =~ /^\s+\S/ ||
2239 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002240 $in_header_lines = 0;
2241 $in_commit_log = 1;
2242 }
2243
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002244# Check if there is UTF-8 in a commit log when a mail header has explicitly
2245# declined it, i.e defined some charset where it is missing.
2246 if ($in_header_lines &&
2247 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2248 $1 !~ /utf-8/i) {
2249 $non_utf8_charset = 1;
2250 }
2251
2252 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002253 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002254 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002255 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2256 }
2257
Kees Cook66b47b42014-10-13 15:51:57 -07002258# Check for various typo / spelling mistakes
Joe Perches36061e32014-12-10 15:51:43 -08002259 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
Kees Cook66b47b42014-10-13 15:51:57 -07002260 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2261 my $typo = $1;
2262 my $typo_fix = $spelling_fix{lc($typo)};
2263 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2264 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2265 my $msg_type = \&WARN;
2266 $msg_type = \&CHK if ($file);
2267 if (&{$msg_type}("TYPO_SPELLING",
2268 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2269 $fix) {
2270 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2271 }
2272 }
2273 }
2274
Andy Whitcroft306708542008-10-15 22:02:28 -07002275# ignore non-hunk lines and lines being removed
2276 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002277
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002278#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002279 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002280 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002281 if (ERROR("DOS_LINE_ENDINGS",
2282 "DOS line endings\n" . $herevet) &&
2283 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002284 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002285 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002286 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2287 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002288 if (ERROR("TRAILING_WHITESPACE",
2289 "trailing whitespace\n" . $herevet) &&
2290 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002291 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002292 }
2293
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002294 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002295 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002296
Josh Triplett4783f892013-11-12 15:10:12 -08002297# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002298 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002299 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2300 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002301 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2302 my $msg_type = \&ERROR;
2303 $msg_type = \&CHK if ($file);
2304 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002305 "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 -08002306 }
2307
Andi Kleen33549572010-05-24 14:33:29 -07002308# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002309# Only applies when adding the entry originally, after that we do not have
2310# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002311 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002312 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002313 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002314 my $cnt = $realcnt;
2315 my $ln = $linenr + 1;
2316 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002317 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002318 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002319 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002320 $f = $lines[$ln - 1];
2321 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2322 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002323
2324 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002325 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002326
Joe Perches8d73e0e2014-08-06 16:10:46 -07002327 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002328 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002329 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002330 $length = -1;
2331 }
2332
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002333 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002334 $f =~ s/#.*//;
2335 $f =~ s/^\s+//;
2336 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002337 if ($f =~ /^\s*config\s/) {
2338 $is_end = 1;
2339 last;
2340 }
Andi Kleen33549572010-05-24 14:33:29 -07002341 $length++;
2342 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002343 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2344 WARN("CONFIG_DESCRIPTION",
2345 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2346 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002347 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002348 }
2349
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002350# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2351 if ($realfile =~ /Kconfig/ &&
2352 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2353 WARN("CONFIG_EXPERIMENTAL",
2354 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2355 }
2356
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002357 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2358 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2359 my $flag = $1;
2360 my $replacement = {
2361 'EXTRA_AFLAGS' => 'asflags-y',
2362 'EXTRA_CFLAGS' => 'ccflags-y',
2363 'EXTRA_CPPFLAGS' => 'cppflags-y',
2364 'EXTRA_LDFLAGS' => 'ldflags-y',
2365 };
2366
2367 WARN("DEPRECATED_VARIABLE",
2368 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2369 }
2370
Rob Herringbff5da42014-01-23 15:54:51 -08002371# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002372 if (defined $root &&
2373 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2374 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2375
Rob Herringbff5da42014-01-23 15:54:51 -08002376 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2377
Florian Vaussardcc933192014-04-03 14:49:27 -07002378 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2379 my $vp_file = $dt_path . "vendor-prefixes.txt";
2380
Rob Herringbff5da42014-01-23 15:54:51 -08002381 foreach my $compat (@compats) {
2382 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002383 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2384 my $compat3 = $compat;
2385 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2386 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002387 if ( $? >> 8 ) {
2388 WARN("UNDOCUMENTED_DT_STRING",
2389 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2390 }
2391
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002392 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2393 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002394 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002395 if ( $? >> 8 ) {
2396 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002397 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002398 }
2399 }
2400 }
2401
Andy Whitcroft5368df202008-10-15 22:02:27 -07002402# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002403 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002404
Joe Perches6cd7f382012-12-17 16:01:54 -08002405#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002406 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002407 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002408 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002409 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002410 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002411 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002412 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002413 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002414 }
2415
Josh Triplettca56dc02012-03-23 15:02:21 -07002416# Check for user-visible strings broken across lines, which breaks the ability
Joe Perches8c5fcd22014-01-23 15:54:40 -08002417# to grep for the string. Make exceptions when the previous string ends in a
2418# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2419# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Josh Triplettca56dc02012-03-23 15:02:21 -07002420 if ($line =~ /^\+\s*"/ &&
2421 $prevline =~ /"\s*$/ &&
Joe Perches8c5fcd22014-01-23 15:54:40 -08002422 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
Josh Triplettca56dc02012-03-23 15:02:21 -07002423 WARN("SPLIT_STRING",
2424 "quoted string split across lines\n" . $hereprev);
2425 }
2426
Dan Carpenterece96592014-08-06 16:11:29 -07002427# check for missing a space in a string concatination
2428 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2429 WARN('MISSING_SPACE',
2430 "break quoted strings at a space character\n" . $hereprev);
2431 }
2432
Joe Perches5e79d962010-03-05 13:43:55 -08002433# check for spaces before a quoted newline
2434 if ($rawline =~ /^.*\".*\s\\n/) {
Joe Perches3705ce52013-07-03 15:05:31 -07002435 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2436 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2437 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002438 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
Joe Perches3705ce52013-07-03 15:05:31 -07002439 }
2440
Joe Perches5e79d962010-03-05 13:43:55 -08002441 }
2442
Andy Whitcroft8905a672007-11-28 16:21:06 -08002443# check for adding lines without a newline.
2444 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002445 WARN("MISSING_EOF_NEWLINE",
2446 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002447 }
2448
Mike Frysinger42e41c52009-09-21 17:04:40 -07002449# Blackfin: use hi/lo macros
2450 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2451 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2452 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002453 ERROR("LO_MACRO",
2454 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002455 }
2456 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2457 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002458 ERROR("HI_MACRO",
2459 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002460 }
2461 }
2462
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002463# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002464 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002465
2466# at the beginning of a line any tabs must come first and anything
2467# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002468 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2469 $rawline =~ /^\+\s* \s*/) {
2470 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002471 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002472 if (ERROR("CODE_INDENT",
2473 "code indent should use tabs where possible\n" . $herevet) &&
2474 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002475 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002476 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002477 }
2478
Alberto Panizzo08e44362010-03-05 13:43:54 -08002479# check for space before tabs.
2480 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2481 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002482 if (WARN("SPACE_BEFORE_TAB",
2483 "please, no space before tabs\n" . $herevet) &&
2484 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002485 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002486 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002487 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002488 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002489 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002490 }
2491
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002492# check for && or || at the start of a line
2493 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2494 CHK("LOGICAL_CONTINUATIONS",
2495 "Logical continuations should be on the previous line\n" . $hereprev);
2496 }
2497
2498# check multi-line statement indentation matches previous line
2499 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002500 $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 -07002501 $prevline =~ /^\+(\t*)(.*)$/;
2502 my $oldindent = $1;
2503 my $rest = $2;
2504
2505 my $pos = pos_last_openparen($rest);
2506 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002507 $line =~ /^(\+| )([ \t]*)/;
2508 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002509
2510 my $goodtabindent = $oldindent .
2511 "\t" x ($pos / 8) .
2512 " " x ($pos % 8);
2513 my $goodspaceindent = $oldindent . " " x $pos;
2514
2515 if ($newindent ne $goodtabindent &&
2516 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002517
2518 if (CHK("PARENTHESIS_ALIGNMENT",
2519 "Alignment should match open parenthesis\n" . $hereprev) &&
2520 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002521 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002522 s/^\+[ \t]*/\+$goodtabindent/;
2523 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002524 }
2525 }
2526 }
2527
Joe Perches04941aa2014-12-10 15:51:37 -08002528 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2529 (!defined($1) || $1 !~ /sizeof\s*/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002530 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002531 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002532 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002533 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002534 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002535 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002536 }
2537
Joe Perches05880602012-10-04 17:13:35 -07002538 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002539 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002540 $rawline =~ /^\+[ \t]*\*/ &&
2541 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002542 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2543 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2544 }
2545
2546 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002547 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2548 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002549 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002550 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2551 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2552 "networking block comments start with * on subsequent lines\n" . $hereprev);
2553 }
2554
2555 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002556 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2557 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2558 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2559 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002560 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2561 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2562 }
2563
Joe Perches7f619192014-08-06 16:10:39 -07002564# check for missing blank lines after struct/union declarations
2565# with exceptions for various attributes and macros
2566 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2567 $line =~ /^\+/ &&
2568 !($line =~ /^\+\s*$/ ||
2569 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2570 $line =~ /^\+\s*MODULE_/i ||
2571 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2572 $line =~ /^\+[a-z_]*init/ ||
2573 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2574 $line =~ /^\+\s*DECLARE/ ||
2575 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002576 if (CHK("LINE_SPACING",
2577 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2578 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002579 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002580 }
Joe Perches7f619192014-08-06 16:10:39 -07002581 }
2582
Joe Perches365dd4e2014-08-06 16:10:42 -07002583# check for multiple consecutive blank lines
2584 if ($prevline =~ /^[\+ ]\s*$/ &&
2585 $line =~ /^\+\s*$/ &&
2586 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002587 if (CHK("LINE_SPACING",
2588 "Please don't use multiple blank lines\n" . $hereprev) &&
2589 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002590 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002591 }
2592
Joe Perches365dd4e2014-08-06 16:10:42 -07002593 $last_blank_line = $linenr;
2594 }
2595
Joe Perches3b617e32014-04-03 14:49:28 -07002596# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002597 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2598 # actual declarations
2599 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002600 # function pointer declarations
2601 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002602 # foo bar; where foo is some local typedef or #define
2603 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2604 # known declaration macros
2605 $prevline =~ /^\+\s+$declaration_macros/) &&
2606 # for "else if" which can look like "$Ident $Ident"
2607 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2608 # other possible extensions of declaration lines
2609 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2610 # not starting a section or a macro "\" extended line
2611 $prevline =~ /(?:\{\s*|\\)$/) &&
2612 # looks like a declaration
2613 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002614 # function pointer declarations
2615 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002616 # foo bar; where foo is some local typedef or #define
2617 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2618 # known declaration macros
2619 $sline =~ /^\+\s+$declaration_macros/ ||
2620 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002621 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002622 # start or end of block or continuation of declaration
2623 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2624 # bitfield continuation
2625 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2626 # other possible extensions of declaration lines
2627 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2628 # indentation of previous and current line are the same
2629 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002630 if (WARN("LINE_SPACING",
2631 "Missing a blank line after declarations\n" . $hereprev) &&
2632 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002633 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002634 }
Joe Perches3b617e32014-04-03 14:49:28 -07002635 }
2636
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002637# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002638# Exceptions:
2639# 1) within comments
2640# 2) indented preprocessor commands
2641# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002642 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002643 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002644 if (WARN("LEADING_SPACE",
2645 "please, no spaces at the start of a line\n" . $herevet) &&
2646 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002647 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002648 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002649 }
2650
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002651# check we are in a valid C source file if not then ignore this hunk
2652 next if ($realfile !~ /\.(h|c)$/);
2653
Joe Perches032a4c02014-08-06 16:10:29 -07002654# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002655# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002656# if the previous line is a break or return and is indented 1 tab more...
2657 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2658 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002659 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2660 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2661 defined $lines[$linenr] &&
2662 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002663 WARN("UNNECESSARY_ELSE",
2664 "else is not generally useful after a break or return\n" . $hereprev);
2665 }
2666 }
2667
Joe Perchesc00df192014-08-06 16:11:01 -07002668# check indentation of a line with a break;
2669# if the previous line is a goto or return and is indented the same # of tabs
2670 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2671 my $tabs = $1;
2672 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2673 WARN("UNNECESSARY_BREAK",
2674 "break is not useful after a goto or return\n" . $hereprev);
2675 }
2676 }
2677
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002678# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2679 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2680 WARN("CONFIG_EXPERIMENTAL",
2681 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2682 }
2683
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002684# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002685 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002686 WARN("CVS_KEYWORD",
2687 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002688 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002689
Mike Frysinger42e41c52009-09-21 17:04:40 -07002690# Blackfin: don't use __builtin_bfin_[cs]sync
2691 if ($line =~ /__builtin_bfin_csync/) {
2692 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002693 ERROR("CSYNC",
2694 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002695 }
2696 if ($line =~ /__builtin_bfin_ssync/) {
2697 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002698 ERROR("SSYNC",
2699 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002700 }
2701
Joe Perches56e77d72013-02-21 16:44:14 -08002702# check for old HOTPLUG __dev<foo> section markings
2703 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2704 WARN("HOTPLUG_SECTION",
2705 "Using $1 is unnecessary\n" . $herecurr);
2706 }
2707
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002708# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002709 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2710 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002711#print "LINE<$line>\n";
2712 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002713 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002714 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002715 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002716 $stat =~ s/\n./\n /g;
2717 $cond =~ s/\n./\n /g;
2718
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002719#print "linenr<$linenr> <$stat>\n";
2720 # If this statement has no statement boundaries within
2721 # it there is no point in retrying a statement scan
2722 # until we hit end of it.
2723 my $frag = $stat; $frag =~ s/;+\s*$//;
2724 if ($frag !~ /(?:{|;)/) {
2725#print "skip<$line_nr_next>\n";
2726 $suppress_statement = $line_nr_next;
2727 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002728
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002729 # Find the real next line.
2730 $realline_next = $line_nr_next;
2731 if (defined $realline_next &&
2732 (!defined $lines[$realline_next - 1] ||
2733 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2734 $realline_next++;
2735 }
2736
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002737 my $s = $stat;
2738 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002739
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002740 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002741 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002742
2743 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002744 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002745
Andy Whitcroft463f2862009-09-21 17:04:34 -07002746 } elsif ($s =~ /^.\s*else\b/s) {
2747
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002748 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002749 } 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 -07002750 my $type = $1;
2751 $type =~ s/\s+/ /g;
2752 possible($type, "A:" . $s);
2753
Andy Whitcroft8905a672007-11-28 16:21:06 -08002754 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002755 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002756 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002757 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002758
2759 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002760 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002761 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002762 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002763
2764 # Check for any sort of function declaration.
2765 # int foo(something bar, other baz);
2766 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002767 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 -08002768 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002769
Andy Whitcroftcf655042008-03-04 14:28:20 -08002770 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002771 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002772 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002773
Andy Whitcroft8905a672007-11-28 16:21:06 -08002774 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002775 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002776
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002777 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002778 }
2779 }
2780 }
2781
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002782 }
2783
Andy Whitcroft653d4872007-06-23 17:16:34 -07002784#
2785# Checks which may be anchored in the context.
2786#
2787
2788# Check for switch () and associated case and default
2789# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002790 if ($line=~/\bswitch\s*\(.*\)/) {
2791 my $err = '';
2792 my $sep = '';
2793 my @ctx = ctx_block_outer($linenr, $realcnt);
2794 shift(@ctx);
2795 for my $ctx (@ctx) {
2796 my ($clen, $cindent) = line_stats($ctx);
2797 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2798 $indent != $cindent) {
2799 $err .= "$sep$ctx\n";
2800 $sep = '';
2801 } else {
2802 $sep = "[...]\n";
2803 }
2804 }
2805 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002806 ERROR("SWITCH_CASE_INDENT_LEVEL",
2807 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002808 }
2809 }
2810
2811# if/while/etc brace do not go on next line, unless defining a do while loop,
2812# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002813 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 -07002814 my $pre_ctx = "$1$2";
2815
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002816 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002817
2818 if ($line =~ /^\+\t{6,}/) {
2819 WARN("DEEP_INDENTATION",
2820 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2821 }
2822
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002823 my $ctx_cnt = $realcnt - $#ctx - 1;
2824 my $ctx = join("\n", @ctx);
2825
Andy Whitcroft548596d2008-07-23 21:29:01 -07002826 my $ctx_ln = $linenr;
2827 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002828
Andy Whitcroft548596d2008-07-23 21:29:01 -07002829 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2830 defined $lines[$ctx_ln - 1] &&
2831 $lines[$ctx_ln - 1] =~ /^-/)) {
2832 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2833 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002834 $ctx_ln++;
2835 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002836
Andy Whitcroft53210162008-07-23 21:29:03 -07002837 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2838 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002839
Joe Perchesd752fcc2014-08-06 16:11:05 -07002840 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002841 ERROR("OPEN_BRACE",
2842 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002843 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002844 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002845 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2846 $ctx =~ /\)\s*\;\s*$/ &&
2847 defined $lines[$ctx_ln - 1])
2848 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002849 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2850 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002851 WARN("TRAILING_SEMICOLON",
2852 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002853 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002854 }
2855 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002856 }
2857
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002858# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002859 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 -08002860 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2861 ctx_statement_block($linenr, $realcnt, 0)
2862 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002863 my ($s, $c) = ($stat, $cond);
2864
2865 substr($s, 0, length($c), '');
2866
2867 # Make sure we remove the line prefixes as we have
2868 # none on the first line, and are going to readd them
2869 # where necessary.
2870 $s =~ s/\n./\n/gs;
2871
2872 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002873 my @newlines = ($c =~ /\n/gs);
2874 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002875
2876 # We want to check the first line inside the block
2877 # starting at the end of the conditional, so remove:
2878 # 1) any blank line termination
2879 # 2) any opening brace { on end of the line
2880 # 3) any do (...) {
2881 my $continuation = 0;
2882 my $check = 0;
2883 $s =~ s/^.*\bdo\b//;
2884 $s =~ s/^\s*{//;
2885 if ($s =~ s/^\s*\\//) {
2886 $continuation = 1;
2887 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002888 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002889 $check = 1;
2890 $cond_lines++;
2891 }
2892
2893 # Also ignore a loop construct at the end of a
2894 # preprocessor statement.
2895 if (($prevline =~ /^.\s*#\s*define\s/ ||
2896 $prevline =~ /\\\s*$/) && $continuation == 0) {
2897 $check = 0;
2898 }
2899
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002900 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002901 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002902 while ($cond_ptr != $cond_lines) {
2903 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002904
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002905 # If we see an #else/#elif then the code
2906 # is not linear.
2907 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2908 $check = 0;
2909 }
2910
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002911 # Ignore:
2912 # 1) blank lines, they should be at 0,
2913 # 2) preprocessor lines, and
2914 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002915 if ($continuation ||
2916 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002917 $s =~ /^\s*#\s*?/ ||
2918 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002919 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002920 if ($s =~ s/^.*?\n//) {
2921 $cond_lines++;
2922 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002923 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002924 }
2925
2926 my (undef, $sindent) = line_stats("+" . $s);
2927 my $stat_real = raw_line($linenr, $cond_lines);
2928
2929 # Check if either of these lines are modified, else
2930 # this is not this patch's fault.
2931 if (!defined($stat_real) ||
2932 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2933 $check = 0;
2934 }
2935 if (defined($stat_real) && $cond_lines > 1) {
2936 $stat_real = "[...]\n$stat_real";
2937 }
2938
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002939 #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 -07002940
2941 if ($check && (($sindent % 8) != 0 ||
2942 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002943 WARN("SUSPECT_CODE_INDENT",
2944 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002945 }
2946 }
2947
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002948 # Track the 'values' across context and added lines.
2949 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002950 my ($curr_values, $curr_vars) =
2951 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002952 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002953 if ($dbg_values) {
2954 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002955 print "$linenr > .$outline\n";
2956 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002957 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002958 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002959 $prev_values = substr($curr_values, -1);
2960
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002961#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002962 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002963
Andy Whitcroft653d4872007-06-23 17:16:34 -07002964# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002965 if ($dbg_type) {
2966 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002967 ERROR("TEST_TYPE",
2968 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002969 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002970 ERROR("TEST_NOT_TYPE",
2971 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002972 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002973 next;
2974 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002975# TEST: allow direct testing of the attribute matcher.
2976 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002977 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002978 ERROR("TEST_ATTR",
2979 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002980 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002981 ERROR("TEST_NOT_ATTR",
2982 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002983 }
2984 next;
2985 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002986
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002987# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002988 if ($line =~ /^.\s*{/ &&
2989 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002990 if (ERROR("OPEN_BRACE",
2991 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002992 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2993 fix_delete_line($fixlinenr - 1, $prevrawline);
2994 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002995 my $fixedline = $prevrawline;
2996 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002997 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002998 $fixedline = $line;
2999 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003000 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003001 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003002 }
3003
Andy Whitcroft653d4872007-06-23 17:16:34 -07003004#
3005# Checks which are anchored on the added line.
3006#
3007
3008# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003009 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003010 my $path = $1;
3011 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003012 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003013 "malformed #include filename\n" . $herecurr);
3014 }
3015 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3016 ERROR("UAPI_INCLUDE",
3017 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003018 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003019 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003020
3021# no C99 // comments
3022 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003023 if (ERROR("C99_COMMENTS",
3024 "do not use C99 // comments\n" . $herecurr) &&
3025 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003026 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003027 if ($line =~ /\/\/(.*)$/) {
3028 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003029 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003030 }
3031 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003032 }
3033 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003034 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003035 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003036
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003037# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3038# the whole statement.
3039#print "APW <$lines[$realline_next - 1]>\n";
3040 if (defined $realline_next &&
3041 exists $lines[$realline_next - 1] &&
3042 !defined $suppress_export{$realline_next} &&
3043 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3044 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003045 # Handle definitions which produce identifiers with
3046 # a prefix:
3047 # XXX(foo);
3048 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003049 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003050 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003051 $name =~ /^${Ident}_$2/) {
3052#print "FOO C name<$name>\n";
3053 $suppress_export{$realline_next} = 1;
3054
3055 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003056 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003057 ^.DEFINE_$Ident\(\Q$name\E\)|
3058 ^.DECLARE_$Ident\(\Q$name\E\)|
3059 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003060 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3061 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003062 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003063#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3064 $suppress_export{$realline_next} = 2;
3065 } else {
3066 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003067 }
3068 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003069 if (!defined $suppress_export{$linenr} &&
3070 $prevline =~ /^.\s*$/ &&
3071 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3072 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3073#print "FOO B <$lines[$linenr - 1]>\n";
3074 $suppress_export{$linenr} = 2;
3075 }
3076 if (defined $suppress_export{$linenr} &&
3077 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003078 WARN("EXPORT_SYMBOL",
3079 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003080 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003081
Joe Eloff5150bda2010-08-09 17:21:00 -07003082# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003083 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3084 if (ERROR("GLOBAL_INITIALISERS",
3085 "do not initialise globals to 0 or NULL\n" .
3086 $herecurr) &&
3087 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003088 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003089 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003090 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003091# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003092 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3093 if (ERROR("INITIALISED_STATIC",
3094 "do not initialise statics to 0 or NULL\n" .
3095 $herecurr) &&
3096 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003097 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003098 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003099 }
3100
Joe Perches18130872014-08-06 16:11:22 -07003101# check for misordered declarations of char/short/int/long with signed/unsigned
3102 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3103 my $tmp = trim($1);
3104 WARN("MISORDERED_TYPE",
3105 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3106 }
3107
Joe Perchescb710ec2010-10-26 14:23:20 -07003108# check for static const char * arrays.
3109 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003110 WARN("STATIC_CONST_CHAR_ARRAY",
3111 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003112 $herecurr);
3113 }
3114
3115# check for static char foo[] = "bar" declarations.
3116 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003117 WARN("STATIC_CONST_CHAR_ARRAY",
3118 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003119 $herecurr);
3120 }
3121
Joe Perches9b0fa602014-04-03 14:49:18 -07003122# check for non-global char *foo[] = {"bar", ...} declarations.
3123 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3124 WARN("STATIC_CONST_CHAR_ARRAY",
3125 "char * array declaration might be better as static const\n" .
3126 $herecurr);
3127 }
3128
Joe Perchesb36190c2014-01-27 17:07:18 -08003129# check for function declarations without arguments like "int foo()"
3130 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3131 if (ERROR("FUNCTION_WITHOUT_ARGS",
3132 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3133 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003134 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003135 }
3136 }
3137
Joe Perches92e112f2013-12-13 11:36:22 -07003138# check for uses of DEFINE_PCI_DEVICE_TABLE
3139 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3140 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3141 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3142 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003143 $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 -07003144 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003145 }
3146
Andy Whitcroft653d4872007-06-23 17:16:34 -07003147# check for new typedefs, only function parameters and sparse annotations
3148# make sense.
3149 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003150 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003151 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003152 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003153 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003154 WARN("NEW_TYPEDEFS",
3155 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003156 }
3157
3158# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003159 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003160 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3161 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003162 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003163
Andy Whitcroft65863862009-01-06 14:41:21 -08003164 # Should start with a space.
3165 $to =~ s/^(\S)/ $1/;
3166 # Should not end with a space.
3167 $to =~ s/\s+$//;
3168 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003169 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003170 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003171
Joe Perches3705ce52013-07-03 15:05:31 -07003172## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003173 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003174 if (ERROR("POINTER_LOCATION",
3175 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3176 $fix) {
3177 my $sub_from = $ident;
3178 my $sub_to = $ident;
3179 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003180 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003181 s@\Q$sub_from\E@$sub_to@;
3182 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003183 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003184 }
3185 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3186 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003187 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003188
Andy Whitcroft65863862009-01-06 14:41:21 -08003189 # Should start with a space.
3190 $to =~ s/^(\S)/ $1/;
3191 # Should not end with a space.
3192 $to =~ s/\s+$//;
3193 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003194 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003195 }
3196 # Modifiers should have spaces.
3197 $to =~ s/(\b$Modifier$)/$1 /;
3198
Joe Perches3705ce52013-07-03 15:05:31 -07003199## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003200 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003201 if (ERROR("POINTER_LOCATION",
3202 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3203 $fix) {
3204
3205 my $sub_from = $match;
3206 my $sub_to = $match;
3207 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003208 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003209 s@\Q$sub_from\E@$sub_to@;
3210 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003211 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003212 }
3213
3214# # no BUG() or BUG_ON()
3215# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3216# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3217# print "$herecurr";
3218# $clean = 0;
3219# }
3220
Andy Whitcroft8905a672007-11-28 16:21:06 -08003221 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003222 WARN("LINUX_VERSION_CODE",
3223 "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 -08003224 }
3225
Joe Perches17441222011-06-15 15:08:17 -07003226# check for uses of printk_ratelimit
3227 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003228 WARN("PRINTK_RATELIMITED",
3229"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003230 }
3231
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003232# printk should use KERN_* levels. Note that follow on printk's on the
3233# same line do not need a level, so we use the current block context
3234# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003235# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003236# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003237 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003238 my $ok = 0;
3239 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3240 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003241 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003242 # with "\n" ignore it, else it is to blame
3243 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3244 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3245 $ok = 1;
3246 }
3247 last;
3248 }
3249 }
3250 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003251 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3252 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003253 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003254 }
3255
Joe Perches243f3802012-05-31 16:26:09 -07003256 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3257 my $orig = $1;
3258 my $level = lc($orig);
3259 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003260 my $level2 = $level;
3261 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003262 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003263 "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 -07003264 }
3265
3266 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003267 if (WARN("PREFER_PR_LEVEL",
3268 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3269 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003270 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003271 s/\bpr_warning\b/pr_warn/;
3272 }
Joe Perches243f3802012-05-31 16:26:09 -07003273 }
3274
Joe Perchesdc139312013-02-21 16:44:13 -08003275 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3276 my $orig = $1;
3277 my $level = lc($orig);
3278 $level = "warn" if ($level eq "warning");
3279 $level = "dbg" if ($level eq "debug");
3280 WARN("PREFER_DEV_LEVEL",
3281 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3282 }
3283
Andy Whitcroft653d4872007-06-23 17:16:34 -07003284# function brace can't be on same line, except for #defines of do while,
3285# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003286 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003287 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003288 if (ERROR("OPEN_BRACE",
3289 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3290 $fix) {
3291 fix_delete_line($fixlinenr, $rawline);
3292 my $fixed_line = $rawline;
3293 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3294 my $line1 = $1;
3295 my $line2 = $2;
3296 fix_insert_line($fixlinenr, ltrim($line1));
3297 fix_insert_line($fixlinenr, "\+{");
3298 if ($line2 !~ /^\s*$/) {
3299 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3300 }
3301 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003302 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003303
Andy Whitcroft8905a672007-11-28 16:21:06 -08003304# open braces for enum, union and struct go on the same line.
3305 if ($line =~ /^.\s*{/ &&
3306 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003307 if (ERROR("OPEN_BRACE",
3308 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3309 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3310 fix_delete_line($fixlinenr - 1, $prevrawline);
3311 fix_delete_line($fixlinenr, $rawline);
3312 my $fixedline = rtrim($prevrawline) . " {";
3313 fix_insert_line($fixlinenr, $fixedline);
3314 $fixedline = $rawline;
3315 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3316 if ($fixedline !~ /^\+\s*$/) {
3317 fix_insert_line($fixlinenr, $fixedline);
3318 }
3319 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003320 }
3321
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003322# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003323 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3324 if (WARN("SPACING",
3325 "missing space after $1 definition\n" . $herecurr) &&
3326 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003327 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003328 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3329 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003330 }
3331
Joe Perches31070b52014-01-23 15:54:49 -08003332# Function pointer declarations
3333# check spacing between type, funcptr, and args
3334# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003335 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003336 my $declare = $1;
3337 my $pre_pointer_space = $2;
3338 my $post_pointer_space = $3;
3339 my $funcname = $4;
3340 my $post_funcname_space = $5;
3341 my $pre_args_space = $6;
3342
Joe Perches91f72e92014-04-03 14:49:12 -07003343# the $Declare variable will capture all spaces after the type
3344# so check it for a missing trailing missing space but pointer return types
3345# don't need a space so don't warn for those.
3346 my $post_declare_space = "";
3347 if ($declare =~ /(\s+)$/) {
3348 $post_declare_space = $1;
3349 $declare = rtrim($declare);
3350 }
3351 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003352 WARN("SPACING",
3353 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003354 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003355 }
3356
3357# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003358# This test is not currently implemented because these declarations are
3359# equivalent to
3360# int foo(int bar, ...)
3361# and this is form shouldn't/doesn't generate a checkpatch warning.
3362#
3363# elsif ($declare =~ /\s{2,}$/) {
3364# WARN("SPACING",
3365# "Multiple spaces after return type\n" . $herecurr);
3366# }
Joe Perches31070b52014-01-23 15:54:49 -08003367
3368# unnecessary space "type ( *funcptr)(args...)"
3369 if (defined $pre_pointer_space &&
3370 $pre_pointer_space =~ /^\s/) {
3371 WARN("SPACING",
3372 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3373 }
3374
3375# unnecessary space "type (* funcptr)(args...)"
3376 if (defined $post_pointer_space &&
3377 $post_pointer_space =~ /^\s/) {
3378 WARN("SPACING",
3379 "Unnecessary space before function pointer name\n" . $herecurr);
3380 }
3381
3382# unnecessary space "type (*funcptr )(args...)"
3383 if (defined $post_funcname_space &&
3384 $post_funcname_space =~ /^\s/) {
3385 WARN("SPACING",
3386 "Unnecessary space after function pointer name\n" . $herecurr);
3387 }
3388
3389# unnecessary space "type (*funcptr) (args...)"
3390 if (defined $pre_args_space &&
3391 $pre_args_space =~ /^\s/) {
3392 WARN("SPACING",
3393 "Unnecessary space before function pointer arguments\n" . $herecurr);
3394 }
3395
3396 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003397 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003398 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003399 }
3400 }
3401
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003402# check for spacing round square brackets; allowed:
3403# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003404# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3405# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003406 while ($line =~ /(.*?\s)\[/g) {
3407 my ($where, $prefix) = ($-[1], $1);
3408 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003409 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003410 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003411 if (ERROR("BRACKET_SPACE",
3412 "space prohibited before open square bracket '['\n" . $herecurr) &&
3413 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003414 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003415 s/^(\+.*?)\s+\[/$1\[/;
3416 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003417 }
3418 }
3419
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003420# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003421 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003422 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003423 my $ctx_before = substr($line, 0, $-[1]);
3424 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003425
3426 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003427 if ($name =~ /^(?:
3428 if|for|while|switch|return|case|
3429 volatile|__volatile__|
3430 __attribute__|format|__extension__|
3431 asm|__asm__)$/x)
3432 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003433 # cpp #define statements have non-optional spaces, ie
3434 # if there is a space between the name and the open
3435 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003436 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003437
3438 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003439 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003440
3441 # If this whole things ends with a type its most
3442 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003443 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003444
3445 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003446 if (WARN("SPACING",
3447 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3448 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003449 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003450 s/\b$name\s+\(/$name\(/;
3451 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003452 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003453 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003454
Andy Whitcroft653d4872007-06-23 17:16:34 -07003455# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003456 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003457 my $fixed_line = "";
3458 my $line_fixed = 0;
3459
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003460 my $ops = qr{
3461 <<=|>>=|<=|>=|==|!=|
3462 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3463 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003464 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003465 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003466 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003467 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003468
3469## print("element count: <" . $#elements . ">\n");
3470## foreach my $el (@elements) {
3471## print("el: <$el>\n");
3472## }
3473
3474 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003475 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003476
Joe Perches3705ce52013-07-03 15:05:31 -07003477 foreach my $el (@elements) {
3478 push(@fix_elements, substr($rawline, $off, length($el)));
3479 $off += length($el);
3480 }
3481
3482 $off = 0;
3483
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003484 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003485 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003486
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003487 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003488
3489 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3490
3491## print("n: <$n> good: <$good>\n");
3492
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003493 $off += length($elements[$n]);
3494
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003495 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003496 my $ca = substr($opline, 0, $off);
3497 my $cc = '';
3498 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3499 $cc = substr($opline, $off + length($elements[$n + 1]));
3500 }
3501 my $cb = "$ca$;$cc";
3502
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003503 my $a = '';
3504 $a = 'V' if ($elements[$n] ne '');
3505 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003506 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003507 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3508 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003509 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003510
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003511 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003512
3513 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003514 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003515 $c = 'V' if ($elements[$n + 2] ne '');
3516 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003517 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003518 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3519 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003520 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003521 } else {
3522 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003523 }
3524
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003525 my $ctx = "${a}x${c}";
3526
3527 my $at = "(ctx:$ctx)";
3528
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003529 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003530 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003531
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003532 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003533 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003534
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003535 # Get the full operator variant.
3536 my $opv = $op . substr($curr_vars, $off, 1);
3537
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003538 # Ignore operators passed as parameters.
3539 if ($op_type ne 'V' &&
3540 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3541
Andy Whitcroftcf655042008-03-04 14:28:20 -08003542# # Ignore comments
3543# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003544
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003545 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003546 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003547 if ($ctx !~ /.x[WEBC]/ &&
3548 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003549 if (ERROR("SPACING",
3550 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003551 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003552 $line_fixed = 1;
3553 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003554 }
3555
3556 # // is a comment
3557 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003558
Joe Perchesb00e4812014-04-03 14:49:33 -07003559 # : when part of a bitfield
3560 } elsif ($opv eq ':B') {
3561 # skip the bitfield test for now
3562
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003563 # No spaces for:
3564 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003565 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003566 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003567 if (ERROR("SPACING",
3568 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003569 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003570 if (defined $fix_elements[$n + 2]) {
3571 $fix_elements[$n + 2] =~ s/^\s+//;
3572 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003573 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003574 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003575 }
3576
Joe Perches23810972014-12-10 15:51:32 -08003577 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003578 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003579 my $rtrim_before = 0;
3580 my $space_after = 0;
3581 if ($ctx =~ /Wx./) {
3582 if (ERROR("SPACING",
3583 "space prohibited before that '$op' $at\n" . $hereptr)) {
3584 $line_fixed = 1;
3585 $rtrim_before = 1;
3586 }
3587 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003588 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003589 if (ERROR("SPACING",
3590 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003591 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003592 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003593 $space_after = 1;
3594 }
3595 }
3596 if ($rtrim_before || $space_after) {
3597 if ($rtrim_before) {
3598 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3599 } else {
3600 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3601 }
3602 if ($space_after) {
3603 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003604 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003605 }
3606
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003607 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003608 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003609 #warn "'*' is part of type\n";
3610
3611 # unary operators should have a space before and
3612 # none after. May be left adjacent to another
3613 # unary operator, or a cast
3614 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003615 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003616 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003617 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003618 if (ERROR("SPACING",
3619 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003620 if ($n != $last_after + 2) {
3621 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3622 $line_fixed = 1;
3623 }
Joe Perches3705ce52013-07-03 15:05:31 -07003624 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003625 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003626 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003627 # A unary '*' may be const
3628
3629 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003630 if (ERROR("SPACING",
3631 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003632 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003633 if (defined $fix_elements[$n + 2]) {
3634 $fix_elements[$n + 2] =~ s/^\s+//;
3635 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003636 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003637 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003638 }
3639
3640 # unary ++ and unary -- are allowed no space on one side.
3641 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003642 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003643 if (ERROR("SPACING",
3644 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003645 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003646 $line_fixed = 1;
3647 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003648 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003649 if ($ctx =~ /Wx[BE]/ ||
3650 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003651 if (ERROR("SPACING",
3652 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003653 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003654 $line_fixed = 1;
3655 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003656 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003657 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003658 if (ERROR("SPACING",
3659 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003660 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003661 if (defined $fix_elements[$n + 2]) {
3662 $fix_elements[$n + 2] =~ s/^\s+//;
3663 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003664 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003665 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003666 }
3667
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003668 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003669 } elsif ($op eq '<<' or $op eq '>>' or
3670 $op eq '&' or $op eq '^' or $op eq '|' or
3671 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003672 $op eq '*' or $op eq '/' or
3673 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003674 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003675 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003676 if (ERROR("SPACING",
3677 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003678 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3679 if (defined $fix_elements[$n + 2]) {
3680 $fix_elements[$n + 2] =~ s/^\s+//;
3681 }
Joe Perches3705ce52013-07-03 15:05:31 -07003682 $line_fixed = 1;
3683 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003684 }
3685
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003686 # A colon needs no spaces before when it is
3687 # terminating a case value or a label.
3688 } elsif ($opv eq ':C' || $opv eq ':L') {
3689 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003690 if (ERROR("SPACING",
3691 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003692 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003693 $line_fixed = 1;
3694 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003695 }
3696
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003697 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003698 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003699 my $ok = 0;
3700
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003701 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003702 if (($op eq '<' &&
3703 $cc =~ /^\S+\@\S+>/) ||
3704 ($op eq '>' &&
3705 $ca =~ /<\S+\@\S+$/))
3706 {
3707 $ok = 1;
3708 }
3709
Joe Perches84731622013-11-12 15:10:05 -08003710 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003711 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003712 my $msg_type = \&ERROR;
3713 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3714
3715 if (&{$msg_type}("SPACING",
3716 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003717 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3718 if (defined $fix_elements[$n + 2]) {
3719 $fix_elements[$n + 2] =~ s/^\s+//;
3720 }
Joe Perches3705ce52013-07-03 15:05:31 -07003721 $line_fixed = 1;
3722 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003723 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003724 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003725 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003726
3727## print("n: <$n> GOOD: <$good>\n");
3728
3729 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003730 }
Joe Perches3705ce52013-07-03 15:05:31 -07003731
3732 if (($#elements % 2) == 0) {
3733 $fixed_line = $fixed_line . $fix_elements[$#elements];
3734 }
3735
Joe Perches194f66f2014-08-06 16:11:03 -07003736 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3737 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003738 }
3739
3740
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003741 }
3742
Joe Perches786b6322013-07-03 15:05:32 -07003743# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003744 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003745 if (WARN("SPACING",
3746 "space prohibited before semicolon\n" . $herecurr) &&
3747 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003748 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003749 s/^(\+.*\S)\s+;/$1;/;
3750 }
3751 }
3752
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003753# check for multiple assignments
3754 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003755 CHK("MULTIPLE_ASSIGNMENTS",
3756 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003757 }
3758
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003759## # check for multiple declarations, allowing for a function declaration
3760## # continuation.
3761## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3762## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3763##
3764## # Remove any bracketed sections to ensure we do not
3765## # falsly report the parameters of functions.
3766## my $ln = $line;
3767## while ($ln =~ s/\([^\(\)]*\)//g) {
3768## }
3769## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003770## WARN("MULTIPLE_DECLARATION",
3771## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003772## }
3773## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003774
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003775#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003776 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3777 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003778 if (ERROR("SPACING",
3779 "space required before the open brace '{'\n" . $herecurr) &&
3780 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003781 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003782 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003783 }
3784
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003785## # check for blank lines before declarations
3786## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3787## $prevrawline =~ /^.\s*$/) {
3788## WARN("SPACING",
3789## "No blank lines before declarations\n" . $hereprev);
3790## }
3791##
3792
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003793# closing brace should have a space following it when it has anything
3794# on the line
3795 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003796 if (ERROR("SPACING",
3797 "space required after that close brace '}'\n" . $herecurr) &&
3798 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003799 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003800 s/}((?!(?:,|;|\)))\S)/} $1/;
3801 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003802 }
3803
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003804# check spacing on square brackets
3805 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003806 if (ERROR("SPACING",
3807 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3808 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003809 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003810 s/\[\s+/\[/;
3811 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003812 }
3813 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003814 if (ERROR("SPACING",
3815 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3816 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003817 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003818 s/\s+\]/\]/;
3819 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003820 }
3821
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003822# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003823 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3824 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003825 if (ERROR("SPACING",
3826 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3827 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003828 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003829 s/\(\s+/\(/;
3830 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003831 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003832 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003833 $line !~ /for\s*\(.*;\s+\)/ &&
3834 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003835 if (ERROR("SPACING",
3836 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3837 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003838 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003839 s/\s+\)/\)/;
3840 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003841 }
3842
Joe Perchese2826fd2014-08-06 16:10:48 -07003843# check unnecessary parentheses around addressof/dereference single $Lvals
3844# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3845
3846 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003847 my $var = $1;
3848 if (CHK("UNNECESSARY_PARENTHESES",
3849 "Unnecessary parentheses around $var\n" . $herecurr) &&
3850 $fix) {
3851 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3852 }
3853 }
3854
3855# check for unnecessary parentheses around function pointer uses
3856# ie: (foo->bar)(); should be foo->bar();
3857# but not "if (foo->bar) (" to avoid some false positives
3858 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3859 my $var = $2;
3860 if (CHK("UNNECESSARY_PARENTHESES",
3861 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3862 $fix) {
3863 my $var2 = deparenthesize($var);
3864 $var2 =~ s/\s//g;
3865 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3866 }
3867 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003868
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003869#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003870 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003871 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003872 if (WARN("INDENTED_LABEL",
3873 "labels should not be indented\n" . $herecurr) &&
3874 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003875 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003876 s/^(.)\s+/$1/;
3877 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003878 }
3879
Joe Perches5b9553a2014-04-03 14:49:21 -07003880# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003881 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003882 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003883 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003884 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3885 my $value = $1;
3886 $value = deparenthesize($value);
3887 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3888 ERROR("RETURN_PARENTHESES",
3889 "return is not a function, parentheses are not required\n" . $herecurr);
3890 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003891 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003892 ERROR("SPACING",
3893 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003894 }
3895 }
Joe Perches507e5142013-11-12 15:10:13 -08003896
Joe Perchesb43ae212014-06-23 13:22:07 -07003897# unnecessary return in a void function
3898# at end-of-function, with the previous line a single leading tab, then return;
3899# and the line before that not a goto label target like "out:"
3900 if ($sline =~ /^[ \+]}\s*$/ &&
3901 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3902 $linenr >= 3 &&
3903 $lines[$linenr - 3] =~ /^[ +]/ &&
3904 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003905 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003906 "void function return statements are not generally useful\n" . $hereprev);
3907 }
Joe Perches9819cf22014-06-04 16:12:09 -07003908
Joe Perches189248d2014-01-23 15:54:47 -08003909# if statements using unnecessary parentheses - ie: if ((foo == bar))
3910 if ($^V && $^V ge 5.10.0 &&
3911 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3912 my $openparens = $1;
3913 my $count = $openparens =~ tr@\(@\(@;
3914 my $msg = "";
3915 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3916 my $comp = $4; #Not $1 because of $LvalOrFunc
3917 $msg = " - maybe == should be = ?" if ($comp eq "==");
3918 WARN("UNNECESSARY_PARENTHESES",
3919 "Unnecessary parentheses$msg\n" . $herecurr);
3920 }
3921 }
3922
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003923# Return of what appears to be an errno should normally be -'ve
3924 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3925 my $name = $1;
3926 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003927 WARN("USE_NEGATIVE_ERRNO",
3928 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003929 }
3930 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003931
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003932# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003933 if ($line =~ /\b(if|while|for|switch)\(/) {
3934 if (ERROR("SPACING",
3935 "space required before the open parenthesis '('\n" . $herecurr) &&
3936 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003937 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003938 s/\b(if|while|for|switch)\(/$1 \(/;
3939 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003940 }
3941
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003942# Check for illegal assignment in if conditional -- and check for trailing
3943# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003944 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003945 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3946 ctx_statement_block($linenr, $realcnt, 0)
3947 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003948 my ($stat_next) = ctx_statement_block($line_nr_next,
3949 $remain_next, $off_next);
3950 $stat_next =~ s/\n./\n /g;
3951 ##print "stat<$stat> stat_next<$stat_next>\n";
3952
3953 if ($stat_next =~ /^\s*while\b/) {
3954 # If the statement carries leading newlines,
3955 # then count those as offsets.
3956 my ($whitespace) =
3957 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3958 my $offset =
3959 statement_rawlines($whitespace) - 1;
3960
3961 $suppress_whiletrailers{$line_nr_next +
3962 $offset} = 1;
3963 }
3964 }
3965 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003966 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003967 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003968 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003969
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003970 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003971 ERROR("ASSIGN_IN_IF",
3972 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003973 }
3974
3975 # Find out what is on the end of the line after the
3976 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003977 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003978 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003979 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003980 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3981 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003982 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003983 # Find out how long the conditional actually is.
3984 my @newlines = ($c =~ /\n/gs);
3985 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003986 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003987
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003988 $stat_real = raw_line($linenr, $cond_lines)
3989 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003990 if (defined($stat_real) && $cond_lines > 1) {
3991 $stat_real = "[...]\n$stat_real";
3992 }
3993
Joe Perches000d1cc12011-07-25 17:13:25 -07003994 ERROR("TRAILING_STATEMENTS",
3995 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003996 }
3997 }
3998
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003999# Check for bitwise tests written as boolean
4000 if ($line =~ /
4001 (?:
4002 (?:\[|\(|\&\&|\|\|)
4003 \s*0[xX][0-9]+\s*
4004 (?:\&\&|\|\|)
4005 |
4006 (?:\&\&|\|\|)
4007 \s*0[xX][0-9]+\s*
4008 (?:\&\&|\|\||\)|\])
4009 )/x)
4010 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004011 WARN("HEXADECIMAL_BOOLEAN_TEST",
4012 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004013 }
4014
Andy Whitcroft8905a672007-11-28 16:21:06 -08004015# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004016 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4017 my $s = $1;
4018 $s =~ s/$;//g; # Remove any comments
4019 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004020 ERROR("TRAILING_STATEMENTS",
4021 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004022 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004023 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004024# if should not continue a brace
4025 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004026 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004027 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004028 $herecurr);
4029 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004030# case and default should not have general statements after them
4031 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4032 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004033 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004034 \s*return\s+
4035 )/xg)
4036 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004037 ERROR("TRAILING_STATEMENTS",
4038 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004039 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004040
4041 # Check for }<nl>else {, these must be at the same
4042 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004043 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4044 $previndent == $indent) {
4045 if (ERROR("ELSE_AFTER_BRACE",
4046 "else should follow close brace '}'\n" . $hereprev) &&
4047 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4048 fix_delete_line($fixlinenr - 1, $prevrawline);
4049 fix_delete_line($fixlinenr, $rawline);
4050 my $fixedline = $prevrawline;
4051 $fixedline =~ s/}\s*$//;
4052 if ($fixedline !~ /^\+\s*$/) {
4053 fix_insert_line($fixlinenr, $fixedline);
4054 }
4055 $fixedline = $rawline;
4056 $fixedline =~ s/^(.\s*)else/$1} else/;
4057 fix_insert_line($fixlinenr, $fixedline);
4058 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004059 }
4060
Joe Perches8b8856f2014-08-06 16:11:14 -07004061 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4062 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004063 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4064
4065 # Find out what is on the end of the line after the
4066 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004067 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004068 $s =~ s/\n.*//g;
4069
4070 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004071 if (ERROR("WHILE_AFTER_BRACE",
4072 "while should follow close brace '}'\n" . $hereprev) &&
4073 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4074 fix_delete_line($fixlinenr - 1, $prevrawline);
4075 fix_delete_line($fixlinenr, $rawline);
4076 my $fixedline = $prevrawline;
4077 my $trailing = $rawline;
4078 $trailing =~ s/^\+//;
4079 $trailing = trim($trailing);
4080 $fixedline =~ s/}\s*$/} $trailing/;
4081 fix_insert_line($fixlinenr, $fixedline);
4082 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004083 }
4084 }
4085
Joe Perches95e2c602013-07-03 15:05:20 -07004086#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004087 while ($line =~ m{($Constant|$Lval)}g) {
4088 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004089
4090#gcc binary extension
4091 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004092 if (WARN("GCC_BINARY_CONSTANT",
4093 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4094 $fix) {
4095 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004096 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004097 s/\b$var\b/$hexval/;
4098 }
Joe Perches95e2c602013-07-03 15:05:20 -07004099 }
4100
4101#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004102 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004103 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004104#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004105 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004106#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004107 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4108#Ignore some three character SI units explicitly, like MiB and KHz
4109 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004110 while ($var =~ m{($Ident)}g) {
4111 my $word = $1;
4112 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004113 if ($check) {
4114 seed_camelcase_includes();
4115 if (!$file && !$camelcase_file_seeded) {
4116 seed_camelcase_file($realfile);
4117 $camelcase_file_seeded = 1;
4118 }
4119 }
Joe Perches7e781f62013-09-11 14:23:55 -07004120 if (!defined $camelcase{$word}) {
4121 $camelcase{$word} = 1;
4122 CHK("CAMELCASE",
4123 "Avoid CamelCase: <$word>\n" . $herecurr);
4124 }
Joe Perches34456862013-07-03 15:05:34 -07004125 }
Joe Perches323c1262012-12-17 16:02:07 -08004126 }
4127 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004128
4129#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004130 if ($line =~ /\#\s*define.*\\\s+$/) {
4131 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4132 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4133 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004134 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004135 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004136 }
4137
Andy Whitcroft653d4872007-06-23 17:16:34 -07004138#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004139 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004140 my $file = "$1.h";
4141 my $checkfile = "include/linux/$file";
4142 if (-f "$root/$checkfile" &&
4143 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004144 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004145 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004146 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004147 CHK("ARCH_INCLUDE_LINUX",
4148 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004149 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004150 WARN("INCLUDE_LINUX",
4151 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004152 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004153 }
4154 }
4155
Andy Whitcroft653d4872007-06-23 17:16:34 -07004156# multi-statement macros should be enclosed in a do while loop, grab the
4157# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004158# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004159 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4160 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004161 my $ln = $linenr;
4162 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004163 my ($off, $dstat, $dcond, $rest);
4164 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004165 my $has_flow_statement = 0;
4166 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004167 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004168 ctx_statement_block($linenr, $realcnt, 0);
4169 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004170 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004171 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004172
Joe Perches08a28432014-10-13 15:51:55 -07004173 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4174 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4175
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004176 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004177 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004178 $dstat =~ s/\\\n.//g;
4179 $dstat =~ s/^\s*//s;
4180 $dstat =~ s/\s*$//s;
4181
4182 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004183 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4184 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004185 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004186 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004187 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004188
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004189 # Flatten any obvious string concatentation.
4190 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4191 $dstat =~ s/$Ident\s*("X*")/$1/)
4192 {
4193 }
4194
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004195 my $exceptions = qr{
4196 $Declare|
4197 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004198 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004199 DECLARE_PER_CPU|
4200 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004201 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004202 union|
4203 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004204 \.$Ident\s*=\s*|
4205 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004206 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004207 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004208 if ($dstat ne '' &&
4209 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4210 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004211 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004212 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004213 $dstat !~ /$exceptions/ &&
4214 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004215 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004216 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004217 $dstat !~ /^for\s*$Constant$/ && # for (...)
4218 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4219 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004220 $dstat !~ /^\({/ && # ({...
4221 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004222 {
4223 $ctx =~ s/\n*$//;
4224 my $herectx = $here . "\n";
4225 my $cnt = statement_rawlines($ctx);
4226
4227 for (my $n = 0; $n < $cnt; $n++) {
4228 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004229 }
4230
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004231 if ($dstat =~ /;/) {
4232 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4233 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4234 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004235 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004236 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004237 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004238 }
Joe Perches5023d342012-12-17 16:01:47 -08004239
Joe Perches08a28432014-10-13 15:51:55 -07004240# check for macros with flow control, but without ## concatenation
4241# ## concatenation is commonly a macro that defines a function so ignore those
4242 if ($has_flow_statement && !$has_arg_concat) {
4243 my $herectx = $here . "\n";
4244 my $cnt = statement_rawlines($ctx);
4245
4246 for (my $n = 0; $n < $cnt; $n++) {
4247 $herectx .= raw_line($linenr, $n) . "\n";
4248 }
4249 WARN("MACRO_WITH_FLOW_CONTROL",
4250 "Macros with flow control statements should be avoided\n" . "$herectx");
4251 }
4252
Joe Perches481eb482012-12-17 16:01:56 -08004253# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004254
4255 } else {
4256 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004257 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4258 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004259 $line =~ /^\+.*\\$/) {
4260 WARN("LINE_CONTINUATIONS",
4261 "Avoid unnecessary line continuations\n" . $herecurr);
4262 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004263 }
4264
Joe Perchesb13edf72012-07-30 14:41:24 -07004265# do {} while (0) macro tests:
4266# single-statement macros do not need to be enclosed in do while (0) loop,
4267# macro should not end with a semicolon
4268 if ($^V && $^V ge 5.10.0 &&
4269 $realfile !~ m@/vmlinux.lds.h$@ &&
4270 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4271 my $ln = $linenr;
4272 my $cnt = $realcnt;
4273 my ($off, $dstat, $dcond, $rest);
4274 my $ctx = '';
4275 ($dstat, $dcond, $ln, $cnt, $off) =
4276 ctx_statement_block($linenr, $realcnt, 0);
4277 $ctx = $dstat;
4278
4279 $dstat =~ s/\\\n.//g;
4280
4281 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4282 my $stmts = $2;
4283 my $semis = $3;
4284
4285 $ctx =~ s/\n*$//;
4286 my $cnt = statement_rawlines($ctx);
4287 my $herectx = $here . "\n";
4288
4289 for (my $n = 0; $n < $cnt; $n++) {
4290 $herectx .= raw_line($linenr, $n) . "\n";
4291 }
4292
Joe Perchesac8e97f2012-08-21 16:15:53 -07004293 if (($stmts =~ tr/;/;/) == 1 &&
4294 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004295 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4296 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4297 }
4298 if (defined $semis && $semis ne "") {
4299 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4300 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4301 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004302 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4303 $ctx =~ s/\n*$//;
4304 my $cnt = statement_rawlines($ctx);
4305 my $herectx = $here . "\n";
4306
4307 for (my $n = 0; $n < $cnt; $n++) {
4308 $herectx .= raw_line($linenr, $n) . "\n";
4309 }
4310
4311 WARN("TRAILING_SEMICOLON",
4312 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004313 }
4314 }
4315
Mike Frysinger080ba922009-01-06 14:41:25 -08004316# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4317# all assignments may have only one of the following with an assignment:
4318# .
4319# ALIGN(...)
4320# VMLINUX_SYMBOL(...)
4321 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004322 WARN("MISSING_VMLINUX_SYMBOL",
4323 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004324 }
4325
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004326# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004327 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4328 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004329 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004330 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004331 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4332 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004333 my @allowed = ();
4334 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004335 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004336 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004337 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004338 for my $chunk (@chunks) {
4339 my ($cond, $block) = @{$chunk};
4340
Andy Whitcroft773647a2008-03-28 14:15:58 -07004341 # If the condition carries leading newlines, then count those as offsets.
4342 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4343 my $offset = statement_rawlines($whitespace) - 1;
4344
Joe Perchesaad4f612012-03-23 15:02:19 -07004345 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004346 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4347
4348 # We have looked at and allowed this specific line.
4349 $suppress_ifbraces{$ln + $offset} = 1;
4350
4351 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004352 $ln += statement_rawlines($block) - 1;
4353
Andy Whitcroft773647a2008-03-28 14:15:58 -07004354 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004355
4356 $seen++ if ($block =~ /^\s*{/);
4357
Joe Perchesaad4f612012-03-23 15:02:19 -07004358 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004359 if (statement_lines($cond) > 1) {
4360 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004361 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004362 }
4363 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004364 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004365 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004366 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004367 if (statement_block_size($block) > 1) {
4368 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004369 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004370 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004371 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004372 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004373 if ($seen) {
4374 my $sum_allowed = 0;
4375 foreach (@allowed) {
4376 $sum_allowed += $_;
4377 }
4378 if ($sum_allowed == 0) {
4379 WARN("BRACES",
4380 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4381 } elsif ($sum_allowed != $allow &&
4382 $seen != $allow) {
4383 CHK("BRACES",
4384 "braces {} should be used on all arms of this statement\n" . $herectx);
4385 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004386 }
4387 }
4388 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004389 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004390 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004391 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004392
Andy Whitcroftcf655042008-03-04 14:28:20 -08004393 # Check the pre-context.
4394 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4395 #print "APW: ALLOWED: pre<$1>\n";
4396 $allowed = 1;
4397 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004398
4399 my ($level, $endln, @chunks) =
4400 ctx_statement_full($linenr, $realcnt, $-[0]);
4401
Andy Whitcroftcf655042008-03-04 14:28:20 -08004402 # Check the condition.
4403 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004404 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004405 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004406 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004407 }
4408 if (statement_lines($cond) > 1) {
4409 #print "APW: ALLOWED: cond<$cond>\n";
4410 $allowed = 1;
4411 }
4412 if ($block =~/\b(?:if|for|while)\b/) {
4413 #print "APW: ALLOWED: block<$block>\n";
4414 $allowed = 1;
4415 }
4416 if (statement_block_size($block) > 1) {
4417 #print "APW: ALLOWED: lines block<$block>\n";
4418 $allowed = 1;
4419 }
4420 # Check the post-context.
4421 if (defined $chunks[1]) {
4422 my ($cond, $block) = @{$chunks[1]};
4423 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004424 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004425 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004426 if ($block =~ /^\s*\{/) {
4427 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4428 $allowed = 1;
4429 }
4430 }
4431 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004432 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004433 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004434
Andy Whitcroftf0556632008-10-15 22:02:23 -07004435 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004436 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004437 }
4438
Joe Perches000d1cc12011-07-25 17:13:25 -07004439 WARN("BRACES",
4440 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004441 }
4442 }
4443
Joe Perches0979ae62012-12-17 16:01:59 -08004444# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004445 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004446 CHK("BRACES",
4447 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4448 }
Joe Perches77b9a532013-07-03 15:05:29 -07004449 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004450 CHK("BRACES",
4451 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4452 }
4453
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004454# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004455 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4456 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004457 WARN("VOLATILE",
4458 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004459 }
4460
Joe Perchesf17dba42014-10-13 15:51:51 -07004461# concatenated string without spaces between elements
4462 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4463 CHK("CONCATENATED_STRING",
4464 "Concatenated strings should use spaces between elements\n" . $herecurr);
4465 }
4466
Joe Perches90ad30e2014-12-10 15:51:59 -08004467# uncoalesced string fragments
4468 if ($line =~ /"X*"\s*"/) {
4469 WARN("STRING_FRAGMENTS",
4470 "Consecutive strings are generally better as a single string\n" . $herecurr);
4471 }
4472
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004473# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004474 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004475 CHK("REDUNDANT_CODE",
4476 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004477 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004478 }
4479
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004480# check for needless "if (<foo>) fn(<foo>)" uses
4481 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4482 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4483 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4484 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004485 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004486 }
4487 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004488
Joe Perchesebfdc402014-08-06 16:10:27 -07004489# check for unnecessary "Out of Memory" messages
4490 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4491 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4492 (defined $1 || defined $3) &&
4493 $linenr > 3) {
4494 my $testval = $2;
4495 my $testline = $lines[$linenr - 3];
4496
4497 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4498# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4499
4500 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4501 WARN("OOM_MESSAGE",
4502 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4503 }
4504 }
4505
Joe Perchesf78d98f2014-10-13 15:52:01 -07004506# check for logging functions with KERN_<LEVEL>
4507 if ($line !~ /printk\s*\(/ &&
4508 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4509 my $level = $1;
4510 if (WARN("UNNECESSARY_KERN_LEVEL",
4511 "Possible unnecessary $level\n" . $herecurr) &&
4512 $fix) {
4513 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4514 }
4515 }
4516
Joe Perchesabb08a52014-12-10 15:51:46 -08004517# check for mask then right shift without a parentheses
4518 if ($^V && $^V ge 5.10.0 &&
4519 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4520 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4521 WARN("MASK_THEN_SHIFT",
4522 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4523 }
4524
Joe Perches8716de32013-09-11 14:24:05 -07004525# check for bad placement of section $InitAttribute (e.g.: __initdata)
4526 if ($line =~ /(\b$InitAttribute\b)/) {
4527 my $attr = $1;
4528 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4529 my $ptr = $1;
4530 my $var = $2;
4531 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4532 ERROR("MISPLACED_INIT",
4533 "$attr should be placed after $var\n" . $herecurr)) ||
4534 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4535 WARN("MISPLACED_INIT",
4536 "$attr should be placed after $var\n" . $herecurr))) &&
4537 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004538 $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 -07004539 }
4540 }
4541 }
4542
Joe Perchese970b8842013-11-12 15:10:10 -08004543# check for $InitAttributeData (ie: __initdata) with const
4544 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4545 my $attr = $1;
4546 $attr =~ /($InitAttributePrefix)(.*)/;
4547 my $attr_prefix = $1;
4548 my $attr_type = $2;
4549 if (ERROR("INIT_ATTRIBUTE",
4550 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4551 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004552 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004553 s/$InitAttributeData/${attr_prefix}initconst/;
4554 }
4555 }
4556
4557# check for $InitAttributeConst (ie: __initconst) without const
4558 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4559 my $attr = $1;
4560 if (ERROR("INIT_ATTRIBUTE",
4561 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4562 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004563 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004564 /(^\+\s*(?:static\s+))/;
4565 $lead = rtrim($1);
4566 $lead = "$lead " if ($lead !~ /^\+$/);
4567 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004568 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004569 }
4570 }
4571
Joe Perchesfbdb8132014-04-03 14:49:14 -07004572# don't use __constant_<foo> functions outside of include/uapi/
4573 if ($realfile !~ m@^include/uapi/@ &&
4574 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4575 my $constant_func = $1;
4576 my $func = $constant_func;
4577 $func =~ s/^__constant_//;
4578 if (WARN("CONSTANT_CONVERSION",
4579 "$constant_func should be $func\n" . $herecurr) &&
4580 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004581 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004582 }
4583 }
4584
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004585# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004586 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004587 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004588 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004589 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004590 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004591 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4592 }
4593 if ($delay > 2000) {
4594 WARN("LONG_UDELAY",
4595 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004596 }
4597 }
4598
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004599# warn about unexpectedly long msleep's
4600 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4601 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004602 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004603 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004604 }
4605 }
4606
Joe Perches36ec1932013-07-03 15:05:25 -07004607# check for comparisons of jiffies
4608 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4609 WARN("JIFFIES_COMPARISON",
4610 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4611 }
4612
Joe Perches9d7a34a2013-07-03 15:05:26 -07004613# check for comparisons of get_jiffies_64()
4614 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4615 WARN("JIFFIES_COMPARISON",
4616 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4617 }
4618
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004619# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004620# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004621# print "#ifdef in C files should be avoided\n";
4622# print "$herecurr";
4623# $clean = 0;
4624# }
4625
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004626# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004627 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004628 if (ERROR("SPACING",
4629 "exactly one space required after that #$1\n" . $herecurr) &&
4630 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004631 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004632 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4633 }
4634
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004635 }
4636
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004637# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004638 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4639 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004640 my $which = $1;
4641 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004642 CHK("UNCOMMENTED_DEFINITION",
4643 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004644 }
4645 }
4646# check for memory barriers without a comment.
4647 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4648 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004649 WARN("MEMORY_BARRIER",
4650 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004651 }
4652 }
4653# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004654 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004655 CHK("ARCH_DEFINES",
4656 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004657 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004658
Tobias Klauserd4977c72010-05-24 14:33:30 -07004659# Check that the storage class is at the beginning of a declaration
4660 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004661 WARN("STORAGE_CLASS",
4662 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004663 }
4664
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004665# check the location of the inline attribute, that it is between
4666# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004667 if ($line =~ /\b$Type\s+$Inline\b/ ||
4668 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004669 ERROR("INLINE_LOCATION",
4670 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004671 }
4672
Andy Whitcroft8905a672007-11-28 16:21:06 -08004673# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004674 if ($realfile !~ m@\binclude/uapi/@ &&
4675 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004676 if (WARN("INLINE",
4677 "plain inline is preferred over $1\n" . $herecurr) &&
4678 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004679 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004680
4681 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004682 }
4683
Joe Perches3d130fd2011-01-12 17:00:00 -08004684# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004685 if ($realfile !~ m@\binclude/uapi/@ &&
4686 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004687 WARN("PREFER_PACKED",
4688 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004689 }
4690
Joe Perches39b7e282011-07-25 17:13:24 -07004691# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004692 if ($realfile !~ m@\binclude/uapi/@ &&
4693 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004694 WARN("PREFER_ALIGNED",
4695 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004696 }
4697
Joe Perches5f14d3b2012-01-10 15:09:52 -08004698# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004699 if ($realfile !~ m@\binclude/uapi/@ &&
4700 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004701 if (WARN("PREFER_PRINTF",
4702 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4703 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004704 $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 -07004705
4706 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004707 }
4708
Joe Perches6061d942012-03-23 15:02:16 -07004709# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004710 if ($realfile !~ m@\binclude/uapi/@ &&
4711 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004712 if (WARN("PREFER_SCANF",
4713 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4714 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004715 $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 -07004716 }
Joe Perches6061d942012-03-23 15:02:16 -07004717 }
4718
Joe Perches619a9082014-12-10 15:51:35 -08004719# Check for __attribute__ weak, or __weak declarations (may have link issues)
4720 if ($^V && $^V ge 5.10.0 &&
4721 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4722 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4723 $line =~ /\b__weak\b/)) {
4724 ERROR("WEAK_DECLARATION",
4725 "Using weak declarations can have unintended link defects\n" . $herecurr);
4726 }
4727
Joe Perches8f53a9b2010-03-05 13:43:48 -08004728# check for sizeof(&)
4729 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004730 WARN("SIZEOF_ADDRESS",
4731 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004732 }
4733
Joe Perches66c80b62012-07-30 14:41:22 -07004734# check for sizeof without parenthesis
4735 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004736 if (WARN("SIZEOF_PARENTHESIS",
4737 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4738 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004739 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004740 }
Joe Perches66c80b62012-07-30 14:41:22 -07004741 }
4742
Joe Perches428e2fd2011-05-24 17:13:39 -07004743# check for line continuations in quoted strings with odd counts of "
4744 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004745 WARN("LINE_CONTINUATIONS",
4746 "Avoid line continuations in quoted strings\n" . $herecurr);
Joe Perches428e2fd2011-05-24 17:13:39 -07004747 }
4748
Joe Perches88982fe2012-12-17 16:02:00 -08004749# check for struct spinlock declarations
4750 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4751 WARN("USE_SPINLOCK_T",
4752 "struct spinlock should be spinlock_t\n" . $herecurr);
4753 }
4754
Joe Perchesa6962d72013-04-29 16:18:13 -07004755# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004756 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004757 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004758 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004759 if (WARN("PREFER_SEQ_PUTS",
4760 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004762 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004763 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004764 }
4765 }
4766
Andy Whitcroft554e1652012-01-10 15:09:57 -08004767# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004768 if ($^V && $^V ge 5.10.0 &&
4769 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004770 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004771
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004772 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004773 my $ms_val = $7;
4774 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004775
Andy Whitcroft554e1652012-01-10 15:09:57 -08004776 if ($ms_size =~ /^(0x|)0$/i) {
4777 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004778 "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 -08004779 } elsif ($ms_size =~ /^(0x|)1$/i) {
4780 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004781 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4782 }
4783 }
4784
Joe Perches98a9bba2014-01-23 15:54:52 -08004785# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4786 if ($^V && $^V ge 5.10.0 &&
4787 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4788 if (WARN("PREFER_ETHER_ADDR_COPY",
4789 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4790 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004791 $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 -08004792 }
4793 }
4794
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004795# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004796 if ($^V && $^V ge 5.10.0 &&
4797 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004798 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004799 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004800 my $call = $1;
4801 my $cast1 = deparenthesize($2);
4802 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004803 my $cast2 = deparenthesize($7);
4804 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004805 my $cast;
4806
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004807 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004808 $cast = "$cast1 or $cast2";
4809 } elsif ($cast1 ne "") {
4810 $cast = $cast1;
4811 } else {
4812 $cast = $cast2;
4813 }
4814 WARN("MINMAX",
4815 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004816 }
4817 }
4818
Joe Perches4a273192012-07-30 14:41:20 -07004819# check usleep_range arguments
4820 if ($^V && $^V ge 5.10.0 &&
4821 defined $stat &&
4822 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4823 my $min = $1;
4824 my $max = $7;
4825 if ($min eq $max) {
4826 WARN("USLEEP_RANGE",
4827 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4828 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4829 $min > $max) {
4830 WARN("USLEEP_RANGE",
4831 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4832 }
4833 }
4834
Joe Perches823b7942013-11-12 15:10:15 -08004835# check for naked sscanf
4836 if ($^V && $^V ge 5.10.0 &&
4837 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004838 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004839 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4840 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4841 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4842 my $lc = $stat =~ tr@\n@@;
4843 $lc = $lc + $linenr;
4844 my $stat_real = raw_line($linenr, 0);
4845 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4846 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4847 }
4848 WARN("NAKED_SSCANF",
4849 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4850 }
4851
Joe Perchesafc819a2014-06-04 16:12:08 -07004852# check for simple sscanf that should be kstrto<foo>
4853 if ($^V && $^V ge 5.10.0 &&
4854 defined $stat &&
4855 $line =~ /\bsscanf\b/) {
4856 my $lc = $stat =~ tr@\n@@;
4857 $lc = $lc + $linenr;
4858 my $stat_real = raw_line($linenr, 0);
4859 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4860 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4861 }
4862 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4863 my $format = $6;
4864 my $count = $format =~ tr@%@%@;
4865 if ($count == 1 &&
4866 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4867 WARN("SSCANF_TO_KSTRTO",
4868 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4869 }
4870 }
4871 }
4872
Joe Perches70dc8a42013-09-11 14:23:58 -07004873# check for new externs in .h files.
4874 if ($realfile =~ /\.h$/ &&
4875 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004876 if (CHK("AVOID_EXTERNS",
4877 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004878 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004879 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004880 }
4881 }
4882
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004883# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004884 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004885 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004886 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004887 my $function_name = $1;
4888 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004889
4890 my $s = $stat;
4891 if (defined $cond) {
4892 substr($s, 0, length($cond), '');
4893 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004894 if ($s =~ /^\s*;/ &&
4895 $function_name ne 'uninitialized_var')
4896 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004897 WARN("AVOID_EXTERNS",
4898 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004899 }
4900
4901 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004902 WARN("FUNCTION_ARGUMENTS",
4903 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004904 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004905
4906 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4907 $stat =~ /^.\s*extern\s+/)
4908 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004909 WARN("AVOID_EXTERNS",
4910 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004911 }
4912
4913# checks for new __setup's
4914 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4915 my $name = $1;
4916
4917 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004918 CHK("UNDOCUMENTED_SETUP",
4919 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004920 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004921 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004922
4923# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08004924 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004925 WARN("UNNECESSARY_CASTS",
4926 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004927 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004928
Joe Perchesa640d252013-07-03 15:05:21 -07004929# alloc style
4930# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4931 if ($^V && $^V ge 5.10.0 &&
4932 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4933 CHK("ALLOC_SIZEOF_STRUCT",
4934 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4935 }
4936
Joe Perches60a55362014-06-04 16:12:07 -07004937# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4938 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07004939 $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 -07004940 my $oldfunc = $3;
4941 my $a1 = $4;
4942 my $a2 = $10;
4943 my $newfunc = "kmalloc_array";
4944 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07004945 my $r1 = $a1;
4946 my $r2 = $a2;
4947 if ($a1 =~ /^sizeof\s*\S/) {
4948 $r1 = $a2;
4949 $r2 = $a1;
4950 }
4951 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
4952 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07004953 if (WARN("ALLOC_WITH_MULTIPLY",
4954 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
4955 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004956 $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 -07004957
4958 }
4959 }
4960 }
4961
Joe Perches972fdea2013-04-29 16:18:12 -07004962# check for krealloc arg reuse
4963 if ($^V && $^V ge 5.10.0 &&
4964 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
4965 WARN("KREALLOC_ARG_REUSE",
4966 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
4967 }
4968
Joe Perches5ce59ae2013-02-21 16:44:18 -08004969# check for alloc argument mismatch
4970 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
4971 WARN("ALLOC_ARRAY_ARGS",
4972 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4973 }
4974
Joe Perchescaf2a542011-01-12 16:59:56 -08004975# check for multiple semicolons
4976 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004977 if (WARN("ONE_SEMICOLON",
4978 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4979 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004980 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004981 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08004982 }
4983
Joe Perches0ab90192014-12-10 15:51:57 -08004984# check for #defines like: 1 << <digit> that could be BIT(digit)
4985 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
4986 my $ull = "";
4987 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
4988 if (CHK("BIT_MACRO",
4989 "Prefer using the BIT$ull macro\n" . $herecurr) &&
4990 $fix) {
4991 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
4992 }
4993 }
4994
Joe Perchese81f2392014-08-06 16:11:25 -07004995# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08004996 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4997 my $has_break = 0;
4998 my $has_statement = 0;
4999 my $count = 0;
5000 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005001 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005002 $prevline--;
5003 my $rline = $rawlines[$prevline - 1];
5004 my $fline = $lines[$prevline - 1];
5005 last if ($fline =~ /^\@\@/);
5006 next if ($fline =~ /^\-/);
5007 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5008 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5009 next if ($fline =~ /^.[\s$;]*$/);
5010 $has_statement = 1;
5011 $count++;
5012 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5013 }
5014 if (!$has_break && $has_statement) {
5015 WARN("MISSING_BREAK",
5016 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5017 }
5018 }
5019
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005020# check for switch/default statements without a break;
5021 if ($^V && $^V ge 5.10.0 &&
5022 defined $stat &&
5023 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5024 my $ctx = '';
5025 my $herectx = $here . "\n";
5026 my $cnt = statement_rawlines($stat);
5027 for (my $n = 0; $n < $cnt; $n++) {
5028 $herectx .= raw_line($linenr, $n) . "\n";
5029 }
5030 WARN("DEFAULT_NO_BREAK",
5031 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005032 }
5033
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005034# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005035 if ($line =~ /\b__FUNCTION__\b/) {
5036 if (WARN("USE_FUNC",
5037 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5038 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005039 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005040 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005041 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005042
Joe Perches2c924882012-03-23 15:02:20 -07005043# check for use of yield()
5044 if ($line =~ /\byield\s*\(\s*\)/) {
5045 WARN("YIELD",
5046 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5047 }
5048
Joe Perches179f8f42013-07-03 15:05:30 -07005049# check for comparisons against true and false
5050 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5051 my $lead = $1;
5052 my $arg = $2;
5053 my $test = $3;
5054 my $otype = $4;
5055 my $trail = $5;
5056 my $op = "!";
5057
5058 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5059
5060 my $type = lc($otype);
5061 if ($type =~ /^(?:true|false)$/) {
5062 if (("$test" eq "==" && "$type" eq "true") ||
5063 ("$test" eq "!=" && "$type" eq "false")) {
5064 $op = "";
5065 }
5066
5067 CHK("BOOL_COMPARISON",
5068 "Using comparison to $otype is error prone\n" . $herecurr);
5069
5070## maybe suggesting a correct construct would better
5071## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5072
5073 }
5074 }
5075
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005076# check for semaphores initialized locked
5077 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005078 WARN("CONSIDER_COMPLETION",
5079 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005080 }
Joe Perches6712d852012-03-23 15:02:20 -07005081
Joe Perches67d0a072011-10-31 17:13:10 -07005082# recommend kstrto* over simple_strto* and strict_strto*
5083 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005084 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005085 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005086 }
Joe Perches6712d852012-03-23 15:02:20 -07005087
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005088# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005089 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005090 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005091 "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 -07005092 }
Joe Perches6712d852012-03-23 15:02:20 -07005093
Emese Revfy79404842010-03-05 13:43:53 -08005094# check for various ops structs, ensure they are const.
5095 my $struct_ops = qr{acpi_dock_ops|
5096 address_space_operations|
5097 backlight_ops|
5098 block_device_operations|
5099 dentry_operations|
5100 dev_pm_ops|
5101 dma_map_ops|
5102 extent_io_ops|
5103 file_lock_operations|
5104 file_operations|
5105 hv_ops|
5106 ide_dma_ops|
5107 intel_dvo_dev_ops|
5108 item_operations|
5109 iwl_ops|
5110 kgdb_arch|
5111 kgdb_io|
5112 kset_uevent_ops|
5113 lock_manager_operations|
5114 microcode_ops|
5115 mtrr_ops|
5116 neigh_ops|
5117 nlmsvc_binding|
5118 pci_raw_ops|
5119 pipe_buf_operations|
5120 platform_hibernation_ops|
5121 platform_suspend_ops|
5122 proto_ops|
5123 rpc_pipe_ops|
5124 seq_operations|
5125 snd_ac97_build_ops|
5126 soc_pcmcia_socket_ops|
5127 stacktrace_ops|
5128 sysfs_ops|
5129 tty_operations|
5130 usb_mon_operations|
5131 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005132 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08005133 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005134 WARN("CONST_STRUCT",
5135 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005136 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005137 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005138
5139# use of NR_CPUS is usually wrong
5140# ignore definitions of NR_CPUS and usage to define arrays as likely right
5141 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005142 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5143 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005144 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5145 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5146 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005147 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005148 WARN("NR_CPUS",
5149 "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 -07005150 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005151
Joe Perches52ea8502013-11-12 15:10:09 -08005152# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5153 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5154 ERROR("DEFINE_ARCH_HAS",
5155 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5156 }
5157
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005158# check for %L{u,d,i} in strings
5159 my $string;
5160 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5161 $string = substr($rawline, $-[1], $+[1] - $-[1]);
Andy Whitcroft2a1bc5d2008-10-15 22:02:23 -07005162 $string =~ s/%%/__/g;
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005163 if ($string =~ /(?<!%)%L[udi]/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005164 WARN("PRINTF_L",
5165 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005166 last;
5167 }
5168 }
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005169
5170# whine mightly about in_atomic
5171 if ($line =~ /\bin_atomic\s*\(/) {
5172 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005173 ERROR("IN_ATOMIC",
5174 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005175 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005176 WARN("IN_ATOMIC",
5177 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005178 }
5179 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005180
5181# check for lockdep_set_novalidate_class
5182 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5183 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5184 if ($realfile !~ m@^kernel/lockdep@ &&
5185 $realfile !~ m@^include/linux/lockdep@ &&
5186 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005187 ERROR("LOCKDEP",
5188 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005189 }
5190 }
Dave Jones88f88312011-01-12 16:59:59 -08005191
5192 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5193 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005194 WARN("EXPORTED_WORLD_WRITABLE",
5195 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005196 }
Joe Perches24358802014-04-03 14:49:13 -07005197
Joe Perches515a2352014-04-03 14:49:24 -07005198# Mode permission misuses where it seems decimal should be octal
5199# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5200 if ($^V && $^V ge 5.10.0 &&
5201 $line =~ /$mode_perms_search/) {
5202 foreach my $entry (@mode_permission_funcs) {
5203 my $func = $entry->[0];
5204 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005205
Joe Perches515a2352014-04-03 14:49:24 -07005206 my $skip_args = "";
5207 if ($arg_pos > 1) {
5208 $arg_pos--;
5209 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5210 }
5211 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5212 if ($line =~ /$test/) {
5213 my $val = $1;
5214 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005215
Joe Perches515a2352014-04-03 14:49:24 -07005216 if ($val !~ /^0$/ &&
5217 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5218 length($val) ne 4)) {
5219 ERROR("NON_OCTAL_PERMISSIONS",
5220 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5221 }
Joe Perches24358802014-04-03 14:49:13 -07005222 }
5223 }
5224 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005225 }
5226
5227 # If we have no input at all, then there is nothing to report on
5228 # so just keep quiet.
5229 if ($#rawlines == -1) {
5230 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005231 }
5232
Andy Whitcroft8905a672007-11-28 16:21:06 -08005233 # In mailback mode only produce a report in the negative, for
5234 # things that appear to be patches.
5235 if ($mailback && ($clean == 1 || !$is_patch)) {
5236 exit(0);
5237 }
5238
5239 # This is not a patch, and we are are in 'no-patch' mode so
5240 # just keep quiet.
5241 if (!$chk_patch && !$is_patch) {
5242 exit(0);
5243 }
5244
5245 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005246 ERROR("NOT_UNIFIED_DIFF",
5247 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005248 }
5249 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005250 ERROR("MISSING_SIGN_OFF",
5251 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005252 }
5253
Andy Whitcroft8905a672007-11-28 16:21:06 -08005254 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005255 if ($summary && !($clean == 1 && $quiet == 1)) {
5256 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005257 print "total: $cnt_error errors, $cnt_warn warnings, " .
5258 (($check)? "$cnt_chk checks, " : "") .
5259 "$cnt_lines lines checked\n";
5260 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005261 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005262
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005263 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005264
5265 if ($^V lt 5.10.0) {
5266 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5267 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5268 }
5269
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005270 # If there were whitespace errors which cleanpatch can fix
5271 # then suggest that.
5272 if ($rpt_cleaners) {
5273 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5274 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005275 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005276 }
5277 }
5278
Joe Perches91bfe482013-09-11 14:23:59 -07005279 hash_show_words(\%use_type, "Used");
5280 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005281
Joe Perchesd752fcc2014-08-06 16:11:05 -07005282 if ($clean == 0 && $fix &&
5283 ("@rawlines" ne "@fixed" ||
5284 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005285 my $newfile = $filename;
5286 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005287 my $linecount = 0;
5288 my $f;
5289
Joe Perchesd752fcc2014-08-06 16:11:05 -07005290 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5291
Joe Perches3705ce52013-07-03 15:05:31 -07005292 open($f, '>', $newfile)
5293 or die "$P: Can't open $newfile for write\n";
5294 foreach my $fixed_line (@fixed) {
5295 $linecount++;
5296 if ($file) {
5297 if ($linecount > 3) {
5298 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005299 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005300 }
5301 } else {
5302 print $f $fixed_line . "\n";
5303 }
5304 }
5305 close($f);
5306
5307 if (!$quiet) {
5308 print << "EOM";
5309Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5310
5311Do _NOT_ trust the results written to this file.
5312Do _NOT_ submit these changes without inspecting them for correctness.
5313
5314This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5315No warranties, expressed or implied...
5316
5317EOM
5318 }
5319 }
5320
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005321 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005322 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005323 }
5324 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005325 print << "EOM";
5326$vname has style problems, please review.
5327
5328If any of these errors are false positives, please report
5329them to the maintainer, see CHECKPATCH in MAINTAINERS.
5330EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005331 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005332
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005333 return $clean;
5334}