blob: 786017d4d0570b0b4ed1ee9a14b9e38508aa7e44 [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|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800281 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700282 __noclone|
283 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700284 __read_mostly|
285 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700286 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700287 ____cacheline_aligned|
288 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800289 ____cacheline_internodealigned_in_smp|
290 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700291 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700292our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700293our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700294our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
295our $Lval = qr{$Ident(?:$Member)*};
296
Joe Perches95e2c602013-07-03 15:05:20 -0700297our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
298our $Binary = qr{(?i)0b[01]+$Int_type?};
299our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
300our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700301our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800302our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800303our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
304our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
305our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800306our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700307our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800308our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700309our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700310our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700311our $Operators = qr{
312 <=|>=|==|!=|
313 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700314 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700315 }x;
316
Joe Perches91cb5192014-04-03 14:49:32 -0700317our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
318
Andy Whitcroft8905a672007-11-28 16:21:06 -0800319our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700320our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700321our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800322our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700323our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800324our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700325our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800326
Joe Perches15662b32011-10-31 17:13:12 -0700327our $NON_ASCII_UTF8 = qr{
328 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700329 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
330 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
331 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
332 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
333 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
334 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
335}x;
336
Joe Perches15662b32011-10-31 17:13:12 -0700337our $UTF8 = qr{
338 [\x09\x0A\x0D\x20-\x7E] # ASCII
339 | $NON_ASCII_UTF8
340}x;
341
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700342our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700343 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700344 atomic_t
345)};
346
Joe Perches691e6692010-03-05 13:43:51 -0800347our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700348 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700349 (?:[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 -0700350 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700351 panic|
Joe Perches06668722013-11-12 15:10:07 -0800352 MODULE_[A-Z_]+|
353 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800354)};
355
Joe Perches20112472011-07-25 17:13:23 -0700356our $signature_tags = qr{(?xi:
357 Signed-off-by:|
358 Acked-by:|
359 Tested-by:|
360 Reviewed-by:|
361 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700362 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700363 To:|
364 Cc:
365)};
366
Joe Perches18130872014-08-06 16:11:22 -0700367our @typeListMisordered = (
368 qr{char\s+(?:un)?signed},
369 qr{int\s+(?:(?:un)?signed\s+)?short\s},
370 qr{int\s+short(?:\s+(?:un)?signed)},
371 qr{short\s+int(?:\s+(?:un)?signed)},
372 qr{(?:un)?signed\s+int\s+short},
373 qr{short\s+(?:un)?signed},
374 qr{long\s+int\s+(?:un)?signed},
375 qr{int\s+long\s+(?:un)?signed},
376 qr{long\s+(?:un)?signed\s+int},
377 qr{int\s+(?:un)?signed\s+long},
378 qr{int\s+(?:un)?signed},
379 qr{int\s+long\s+long\s+(?:un)?signed},
380 qr{long\s+long\s+int\s+(?:un)?signed},
381 qr{long\s+long\s+(?:un)?signed\s+int},
382 qr{long\s+long\s+(?:un)?signed},
383 qr{long\s+(?:un)?signed},
384);
385
Andy Whitcroft8905a672007-11-28 16:21:06 -0800386our @typeList = (
387 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700388 qr{(?:(?:un)?signed\s+)?char},
389 qr{(?:(?:un)?signed\s+)?short\s+int},
390 qr{(?:(?:un)?signed\s+)?short},
391 qr{(?:(?:un)?signed\s+)?int},
392 qr{(?:(?:un)?signed\s+)?long\s+int},
393 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
394 qr{(?:(?:un)?signed\s+)?long\s+long},
395 qr{(?:(?:un)?signed\s+)?long},
396 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800397 qr{float},
398 qr{double},
399 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800400 qr{struct\s+$Ident},
401 qr{union\s+$Ident},
402 qr{enum\s+$Ident},
403 qr{${Ident}_t},
404 qr{${Ident}_handler},
405 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700406 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800407);
Joe Perches8716de32013-09-11 14:24:05 -0700408our @typeListWithAttr = (
409 @typeList,
410 qr{struct\s+$InitAttribute\s+$Ident},
411 qr{union\s+$InitAttribute\s+$Ident},
412);
413
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700414our @modifierList = (
415 qr{fastcall},
416);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800417
Joe Perches24358802014-04-03 14:49:13 -0700418our @mode_permission_funcs = (
419 ["module_param", 3],
420 ["module_param_(?:array|named|string)", 4],
421 ["module_param_array_named", 5],
422 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
423 ["proc_create(?:_data|)", 2],
424 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
425);
426
Joe Perches515a2352014-04-03 14:49:24 -0700427#Create a search pattern for all these functions to speed up a loop below
428our $mode_perms_search = "";
429foreach my $entry (@mode_permission_funcs) {
430 $mode_perms_search .= '|' if ($mode_perms_search ne "");
431 $mode_perms_search .= $entry->[0];
432}
433
Wolfram Sang7840a942010-08-09 17:20:57 -0700434our $allowed_asm_includes = qr{(?x:
435 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700436 memory|
437 time|
438 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700439)};
440# memory.h: ARM has a custom one
441
Kees Cook66b47b42014-10-13 15:51:57 -0700442# Load common spelling mistakes and build regular expression list.
443my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700444my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700445
Joe Perches36061e32014-12-10 15:51:43 -0800446if (open(my $spelling, '<', $spelling_file)) {
447 my @spelling_list;
448 while (<$spelling>) {
449 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700450
Joe Perches36061e32014-12-10 15:51:43 -0800451 $line =~ s/\s*\n?$//g;
452 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700453
Joe Perches36061e32014-12-10 15:51:43 -0800454 next if ($line =~ m/^\s*#/);
455 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700456
Joe Perches36061e32014-12-10 15:51:43 -0800457 my ($suspect, $fix) = split(/\|\|/, $line);
458
459 push(@spelling_list, $suspect);
460 $spelling_fix{$suspect} = $fix;
461 }
462 close($spelling);
463 $misspellings = join("|", @spelling_list);
464} else {
465 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700466}
Kees Cook66b47b42014-10-13 15:51:57 -0700467
Andy Whitcroft8905a672007-11-28 16:21:06 -0800468sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700469 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
470 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700471 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700472 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700473 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800474 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700475 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800476 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800477 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700478 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700479 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800480 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700481 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800482 }x;
Joe Perches18130872014-08-06 16:11:22 -0700483 $NonptrTypeMisordered = qr{
484 (?:$Modifier\s+|const\s+)*
485 (?:
486 (?:${Misordered}\b)
487 )
488 (?:\s+$Modifier|\s+const)*
489 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700490 $NonptrTypeWithAttr = qr{
491 (?:$Modifier\s+|const\s+)*
492 (?:
493 (?:typeof|__typeof__)\s*\([^\)]*\)|
494 (?:$typeTypedefs\b)|
495 (?:${allWithAttr}\b)
496 )
497 (?:\s+$Modifier|\s+const)*
498 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800499 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700500 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700501 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700502 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503 }x;
Joe Perches18130872014-08-06 16:11:22 -0700504 $TypeMisordered = qr{
505 $NonptrTypeMisordered
506 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
507 (?:\s+$Inline|\s+$Modifier)*
508 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700509 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700510 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800511}
512build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700513
Joe Perches7d2367a2011-07-25 17:13:22 -0700514our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700515
516# Using $balanced_parens, $LvalOrFunc, or $FuncArg
517# requires at least perl version v5.10.0
518# Any use must be runtime checked with $^V
519
520our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700521our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800522our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700523
Joe Perchesf8422302014-08-06 16:11:31 -0700524our $declaration_macros = qr{(?x:
525 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
526 (?:$Storage\s+)?LIST_HEAD\s*\(|
527 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
528)};
529
Joe Perches7d2367a2011-07-25 17:13:22 -0700530sub deparenthesize {
531 my ($string) = @_;
532 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700533
534 while ($string =~ /^\s*\(.*\)\s*$/) {
535 $string =~ s@^\s*\(\s*@@;
536 $string =~ s@\s*\)\s*$@@;
537 }
538
Joe Perches7d2367a2011-07-25 17:13:22 -0700539 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700540
Joe Perches7d2367a2011-07-25 17:13:22 -0700541 return $string;
542}
543
Joe Perches34456862013-07-03 15:05:34 -0700544sub seed_camelcase_file {
545 my ($file) = @_;
546
547 return if (!(-f $file));
548
549 local $/;
550
551 open(my $include_file, '<', "$file")
552 or warn "$P: Can't read '$file' $!\n";
553 my $text = <$include_file>;
554 close($include_file);
555
556 my @lines = split('\n', $text);
557
558 foreach my $line (@lines) {
559 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
560 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
561 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800562 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
563 $camelcase{$1} = 1;
564 } 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 -0700565 $camelcase{$1} = 1;
566 }
567 }
568}
569
570my $camelcase_seeded = 0;
571sub seed_camelcase_includes {
572 return if ($camelcase_seeded);
573
574 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700575 my $camelcase_cache = "";
576 my @include_files = ();
577
578 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700579
Richard Genoud3645e322014-02-10 14:25:32 -0800580 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700581 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
582 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700583 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700584 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700585 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700586 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700587 @include_files = split('\n', $files);
588 foreach my $file (@include_files) {
589 my $date = POSIX::strftime("%Y%m%d%H%M",
590 localtime((stat $file)[9]));
591 $last_mod_date = $date if ($last_mod_date < $date);
592 }
593 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700594 }
Joe Perchesc707a812013-07-08 16:00:43 -0700595
596 if ($camelcase_cache ne "" && -f $camelcase_cache) {
597 open(my $camelcase_file, '<', "$camelcase_cache")
598 or warn "$P: Can't read '$camelcase_cache' $!\n";
599 while (<$camelcase_file>) {
600 chomp;
601 $camelcase{$_} = 1;
602 }
603 close($camelcase_file);
604
605 return;
606 }
607
Richard Genoud3645e322014-02-10 14:25:32 -0800608 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700609 $files = `git ls-files "include/*.h"`;
610 @include_files = split('\n', $files);
611 }
612
Joe Perches34456862013-07-03 15:05:34 -0700613 foreach my $file (@include_files) {
614 seed_camelcase_file($file);
615 }
Joe Perches351b2a12013-07-03 15:05:36 -0700616
Joe Perchesc707a812013-07-08 16:00:43 -0700617 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700618 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700619 open(my $camelcase_file, '>', "$camelcase_cache")
620 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700621 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
622 print $camelcase_file ("$_\n");
623 }
624 close($camelcase_file);
625 }
Joe Perches34456862013-07-03 15:05:34 -0700626}
627
Joe Perchesd311cd42014-08-06 16:10:57 -0700628sub git_commit_info {
629 my ($commit, $id, $desc) = @_;
630
631 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
632
633 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
634 $output =~ s/^\s*//gm;
635 my @lines = split("\n", $output);
636
Joe Perches0d7835f2015-02-13 14:38:35 -0800637 return ($id, $desc) if ($#lines < 0);
638
Joe Perchesd311cd42014-08-06 16:10:57 -0700639 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
640# Maybe one day convert this block of bash into something that returns
641# all matching commit ids, but it's very slow...
642#
643# echo "checking commits $1..."
644# git rev-list --remotes | grep -i "^$1" |
645# while read line ; do
646# git log --format='%H %s' -1 $line |
647# echo "commit $(cut -c 1-12,41-)"
648# done
649 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
650 } else {
651 $id = substr($lines[0], 0, 12);
652 $desc = substr($lines[0], 41);
653 }
654
655 return ($id, $desc);
656}
657
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700658$chk_signoff = 0 if ($file);
659
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700660my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800661my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700662my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700663my @fixed_inserted = ();
664my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700665my $fixlinenr = -1;
666
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800667my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700668for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800669 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700670 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800671 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700672 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800673 } elsif ($filename eq '-') {
674 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700675 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800676 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700677 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700678 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800679 if ($filename eq '-') {
680 $vname = 'Your patch';
681 } else {
682 $vname = $filename;
683 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800684 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700685 chomp;
686 push(@rawlines, $_);
687 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800688 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800689 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700690 $exit = 1;
691 }
692 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800693 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700694 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700695 @fixed_inserted = ();
696 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700697 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700698}
699
700exit($exit);
701
702sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700703 my ($root) = @_;
704
705 my @tree_check = (
706 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
707 "README", "Documentation", "arch", "include", "drivers",
708 "fs", "init", "ipc", "kernel", "lib", "scripts",
709 );
710
711 foreach my $check (@tree_check) {
712 if (! -e $root . '/' . $check) {
713 return 0;
714 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700715 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700716 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700717}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700718
Joe Perches20112472011-07-25 17:13:23 -0700719sub parse_email {
720 my ($formatted_email) = @_;
721
722 my $name = "";
723 my $address = "";
724 my $comment = "";
725
726 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
727 $name = $1;
728 $address = $2;
729 $comment = $3 if defined $3;
730 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
731 $address = $1;
732 $comment = $2 if defined $2;
733 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
734 $address = $1;
735 $comment = $2 if defined $2;
736 $formatted_email =~ s/$address.*$//;
737 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700738 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700739 $name =~ s/^\"|\"$//g;
740 # If there's a name left after stripping spaces and
741 # leading quotes, and the address doesn't have both
742 # leading and trailing angle brackets, the address
743 # is invalid. ie:
744 # "joe smith joe@smith.com" bad
745 # "joe smith <joe@smith.com" bad
746 if ($name ne "" && $address !~ /^<[^>]+>$/) {
747 $name = "";
748 $address = "";
749 $comment = "";
750 }
751 }
752
Joe Perches3705ce52013-07-03 15:05:31 -0700753 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700754 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700755 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700756 $address =~ s/^\<|\>$//g;
757
758 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
759 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
760 $name = "\"$name\"";
761 }
762
763 return ($name, $address, $comment);
764}
765
766sub format_email {
767 my ($name, $address) = @_;
768
769 my $formatted_email;
770
Joe Perches3705ce52013-07-03 15:05:31 -0700771 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700772 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700773 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700774
775 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
776 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
777 $name = "\"$name\"";
778 }
779
780 if ("$name" eq "") {
781 $formatted_email = "$address";
782 } else {
783 $formatted_email = "$name <$address>";
784 }
785
786 return $formatted_email;
787}
788
Joe Perchesd311cd42014-08-06 16:10:57 -0700789sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700790 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700791
Joe Perchesbd474ca2014-08-06 16:11:10 -0700792 foreach my $path (split(/:/, $ENV{PATH})) {
793 if (-e "$path/$bin") {
794 return "$path/$bin";
795 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700796 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700797
Joe Perchesbd474ca2014-08-06 16:11:10 -0700798 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700799}
800
Joe Perches000d1cc12011-07-25 17:13:25 -0700801sub which_conf {
802 my ($conf) = @_;
803
804 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
805 if (-e "$path/$conf") {
806 return "$path/$conf";
807 }
808 }
809
810 return "";
811}
812
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700813sub expand_tabs {
814 my ($str) = @_;
815
816 my $res = '';
817 my $n = 0;
818 for my $c (split(//, $str)) {
819 if ($c eq "\t") {
820 $res .= ' ';
821 $n++;
822 for (; ($n % 8) != 0; $n++) {
823 $res .= ' ';
824 }
825 next;
826 }
827 $res .= $c;
828 $n++;
829 }
830
831 return $res;
832}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700833sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700834 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700835 return $res;
836}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700837
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700838sub line_stats {
839 my ($line) = @_;
840
841 # Drop the diff line leader and expand tabs
842 $line =~ s/^.//;
843 $line = expand_tabs($line);
844
845 # Pick the indent from the front of the line.
846 my ($white) = ($line =~ /^(\s*)/);
847
848 return (length($line), length($white));
849}
850
Andy Whitcroft773647a2008-03-28 14:15:58 -0700851my $sanitise_quote = '';
852
853sub sanitise_line_reset {
854 my ($in_comment) = @_;
855
856 if ($in_comment) {
857 $sanitise_quote = '*/';
858 } else {
859 $sanitise_quote = '';
860 }
861}
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700862sub sanitise_line {
863 my ($line) = @_;
864
865 my $res = '';
866 my $l = '';
867
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800868 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700869 my $off = 0;
870 my $c;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700871
Andy Whitcroft773647a2008-03-28 14:15:58 -0700872 # Always copy over the diff marker.
873 $res = substr($line, 0, 1);
874
875 for ($off = 1; $off < length($line); $off++) {
876 $c = substr($line, $off, 1);
877
878 # Comments we are wacking completly including the begin
879 # and end, all to $;.
880 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
881 $sanitise_quote = '*/';
882
883 substr($res, $off, 2, "$;$;");
884 $off++;
885 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800886 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700887 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700888 $sanitise_quote = '';
889 substr($res, $off, 2, "$;$;");
890 $off++;
891 next;
892 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700893 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
894 $sanitise_quote = '//';
895
896 substr($res, $off, 2, $sanitise_quote);
897 $off++;
898 next;
899 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700900
901 # A \ in a string means ignore the next character.
902 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
903 $c eq "\\") {
904 substr($res, $off, 2, 'XX');
905 $off++;
906 next;
907 }
908 # Regular quotes.
909 if ($c eq "'" || $c eq '"') {
910 if ($sanitise_quote eq '') {
911 $sanitise_quote = $c;
912
913 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700914 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700915 } elsif ($sanitise_quote eq $c) {
916 $sanitise_quote = '';
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700917 }
918 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700919
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800920 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700921 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
922 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700923 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
924 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700925 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
926 substr($res, $off, 1, 'X');
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700927 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700928 substr($res, $off, 1, $c);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700929 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800930 }
931
Daniel Walker113f04a2009-09-21 17:04:35 -0700932 if ($sanitise_quote eq '//') {
933 $sanitise_quote = '';
934 }
935
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700937 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800938 my $clean = 'X' x length($1);
939 $res =~ s@\<.*\>@<$clean>@;
940
941 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700942 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700944 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800945 }
946
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700947 return $res;
948}
949
Joe Perchesa6962d72013-04-29 16:18:13 -0700950sub get_quoted_string {
951 my ($line, $rawline) = @_;
952
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800953 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700954 return substr($rawline, $-[0], $+[0] - $-[0]);
955}
956
Andy Whitcroft8905a672007-11-28 16:21:06 -0800957sub ctx_statement_block {
958 my ($linenr, $remain, $off) = @_;
959 my $line = $linenr - 1;
960 my $blk = '';
961 my $soff = $off;
962 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700963 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800964
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800965 my $loff = 0;
966
Andy Whitcroft8905a672007-11-28 16:21:06 -0800967 my $type = '';
968 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800969 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800970 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800971 my $c;
972 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800973
974 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800975 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800976 @stack = (['', 0]) if ($#stack == -1);
977
Andy Whitcroft773647a2008-03-28 14:15:58 -0700978 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800979 # If we are about to drop off the end, pull in more
980 # context.
981 if ($off >= $len) {
982 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700983 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800984 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800985 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800986 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800987 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800988 $len = length($blk);
989 $line++;
990 last;
991 }
992 # Bail if there is no further context.
993 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800994 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800995 last;
996 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -0800997 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
998 $level++;
999 $type = '#';
1000 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001001 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001002 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001003 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001004 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001005
Andy Whitcroft773647a2008-03-28 14:15:58 -07001006 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001007
1008 # Handle nested #if/#else.
1009 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1010 push(@stack, [ $type, $level ]);
1011 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1012 ($type, $level) = @{$stack[$#stack - 1]};
1013 } elsif ($remainder =~ /^#\s*endif\b/) {
1014 ($type, $level) = @{pop(@stack)};
1015 }
1016
Andy Whitcroft8905a672007-11-28 16:21:06 -08001017 # Statement ends at the ';' or a close '}' at the
1018 # outermost level.
1019 if ($level == 0 && $c eq ';') {
1020 last;
1021 }
1022
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001023 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001024 if ($level == 0 && $coff_set == 0 &&
1025 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1026 $remainder =~ /^(else)(?:\s|{)/ &&
1027 $remainder !~ /^else\s+if\b/) {
1028 $coff = $off + length($1) - 1;
1029 $coff_set = 1;
1030 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1031 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001032 }
1033
Andy Whitcroft8905a672007-11-28 16:21:06 -08001034 if (($type eq '' || $type eq '(') && $c eq '(') {
1035 $level++;
1036 $type = '(';
1037 }
1038 if ($type eq '(' && $c eq ')') {
1039 $level--;
1040 $type = ($level != 0)? '(' : '';
1041
1042 if ($level == 0 && $coff < $soff) {
1043 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001044 $coff_set = 1;
1045 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001046 }
1047 }
1048 if (($type eq '' || $type eq '{') && $c eq '{') {
1049 $level++;
1050 $type = '{';
1051 }
1052 if ($type eq '{' && $c eq '}') {
1053 $level--;
1054 $type = ($level != 0)? '{' : '';
1055
1056 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001057 if (substr($blk, $off + 1, 1) eq ';') {
1058 $off++;
1059 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001060 last;
1061 }
1062 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001063 # Preprocessor commands end at the newline unless escaped.
1064 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1065 $level--;
1066 $type = '';
1067 $off++;
1068 last;
1069 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001070 $off++;
1071 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001072 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001073 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001074 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001075 $line++;
1076 $remain--;
1077 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001078
1079 my $statement = substr($blk, $soff, $off - $soff + 1);
1080 my $condition = substr($blk, $soff, $coff - $soff + 1);
1081
1082 #warn "STATEMENT<$statement>\n";
1083 #warn "CONDITION<$condition>\n";
1084
Andy Whitcroft773647a2008-03-28 14:15:58 -07001085 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001086
1087 return ($statement, $condition,
1088 $line, $remain + 1, $off - $loff + 1, $level);
1089}
1090
Andy Whitcroftcf655042008-03-04 14:28:20 -08001091sub statement_lines {
1092 my ($stmt) = @_;
1093
1094 # Strip the diff line prefixes and rip blank lines at start and end.
1095 $stmt =~ s/(^|\n)./$1/g;
1096 $stmt =~ s/^\s*//;
1097 $stmt =~ s/\s*$//;
1098
1099 my @stmt_lines = ($stmt =~ /\n/g);
1100
1101 return $#stmt_lines + 2;
1102}
1103
1104sub statement_rawlines {
1105 my ($stmt) = @_;
1106
1107 my @stmt_lines = ($stmt =~ /\n/g);
1108
1109 return $#stmt_lines + 2;
1110}
1111
1112sub statement_block_size {
1113 my ($stmt) = @_;
1114
1115 $stmt =~ s/(^|\n)./$1/g;
1116 $stmt =~ s/^\s*{//;
1117 $stmt =~ s/}\s*$//;
1118 $stmt =~ s/^\s*//;
1119 $stmt =~ s/\s*$//;
1120
1121 my @stmt_lines = ($stmt =~ /\n/g);
1122 my @stmt_statements = ($stmt =~ /;/g);
1123
1124 my $stmt_lines = $#stmt_lines + 2;
1125 my $stmt_statements = $#stmt_statements + 1;
1126
1127 if ($stmt_lines > $stmt_statements) {
1128 return $stmt_lines;
1129 } else {
1130 return $stmt_statements;
1131 }
1132}
1133
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001134sub ctx_statement_full {
1135 my ($linenr, $remain, $off) = @_;
1136 my ($statement, $condition, $level);
1137
1138 my (@chunks);
1139
Andy Whitcroftcf655042008-03-04 14:28:20 -08001140 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001141 ($statement, $condition, $linenr, $remain, $off, $level) =
1142 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001143 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001144 push(@chunks, [ $condition, $statement ]);
1145 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1146 return ($level, $linenr, @chunks);
1147 }
1148
1149 # Pull in the following conditional/block pairs and see if they
1150 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001151 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001152 ($statement, $condition, $linenr, $remain, $off, $level) =
1153 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001154 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001155 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001156 #print "C: push\n";
1157 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001158 }
1159
1160 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001161}
1162
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001163sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001164 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001165 my $line;
1166 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001167 my $blk = '';
1168 my @o;
1169 my @c;
1170 my @res = ();
1171
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001172 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001173 my @stack = ($level);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001174 for ($line = $start; $remain > 0; $line++) {
1175 next if ($rawlines[$line] =~ /^-/);
1176 $remain--;
1177
1178 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001179
1180 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001181 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001182 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001183 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001184 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001185 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001186 $level = pop(@stack);
1187 }
1188
Andy Whitcroft01464f32010-10-26 14:23:19 -07001189 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001190 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1191 if ($off > 0) {
1192 $off--;
1193 next;
1194 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001195
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001196 if ($c eq $close && $level > 0) {
1197 $level--;
1198 last if ($level == 0);
1199 } elsif ($c eq $open) {
1200 $level++;
1201 }
1202 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001203
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001204 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001205 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001206 }
1207
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001208 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001209 }
1210
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001211 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001212}
1213sub ctx_block_outer {
1214 my ($linenr, $remain) = @_;
1215
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001216 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1217 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001218}
1219sub ctx_block {
1220 my ($linenr, $remain) = @_;
1221
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001222 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1223 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001224}
1225sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001226 my ($linenr, $remain, $off) = @_;
1227
1228 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1229 return @r;
1230}
1231sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001232 my ($linenr, $remain) = @_;
1233
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001234 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001235}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001236sub ctx_statement_level {
1237 my ($linenr, $remain, $off) = @_;
1238
1239 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1240}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001241
1242sub ctx_locate_comment {
1243 my ($first_line, $end_line) = @_;
1244
1245 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001246 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001247 return $current_comment if (defined $current_comment);
1248
1249 # Look through the context and try and figure out if there is a
1250 # comment.
1251 my $in_comment = 0;
1252 $current_comment = '';
1253 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001254 my $line = $rawlines[$linenr - 1];
1255 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001256 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1257 $in_comment = 1;
1258 }
1259 if ($line =~ m@/\*@) {
1260 $in_comment = 1;
1261 }
1262 if (!$in_comment && $current_comment ne '') {
1263 $current_comment = '';
1264 }
1265 $current_comment .= $line . "\n" if ($in_comment);
1266 if ($line =~ m@\*/@) {
1267 $in_comment = 0;
1268 }
1269 }
1270
1271 chomp($current_comment);
1272 return($current_comment);
1273}
1274sub ctx_has_comment {
1275 my ($first_line, $end_line) = @_;
1276 my $cmt = ctx_locate_comment($first_line, $end_line);
1277
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001278 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001279 ##print "CMMT: $cmt\n";
1280
1281 return ($cmt ne '');
1282}
1283
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001284sub raw_line {
1285 my ($linenr, $cnt) = @_;
1286
1287 my $offset = $linenr - 1;
1288 $cnt++;
1289
1290 my $line;
1291 while ($cnt) {
1292 $line = $rawlines[$offset++];
1293 next if (defined($line) && $line =~ /^-/);
1294 $cnt--;
1295 }
1296
1297 return $line;
1298}
1299
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001300sub cat_vet {
1301 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001302 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001303
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001304 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001305 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1306 $res .= $1;
1307 if ($2 ne '') {
1308 $coded = sprintf("^%c", unpack('C', $2) + 64);
1309 $res .= $coded;
1310 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001311 }
1312 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001313
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001314 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001315}
1316
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001317my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001318my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001319my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001320my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001321
1322sub annotate_reset {
1323 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001324 $av_pending = '_';
1325 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001326 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001327}
1328
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001329sub annotate_values {
1330 my ($stream, $type) = @_;
1331
1332 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001333 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001334 my $cur = $stream;
1335
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001336 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001337
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001338 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001339 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001340 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001341 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001342 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343 print "WS($1)\n" if ($dbg_values > 1);
1344 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001345 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001346 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001347 }
1348
Florian Micklerc023e4732011-01-12 16:59:58 -08001349 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001350 print "CAST($1)\n" if ($dbg_values > 1);
1351 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001352 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001353
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001354 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001355 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001356 $type = 'T';
1357
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001358 } elsif ($cur =~ /^($Modifier)\s*/) {
1359 print "MODIFIER($1)\n" if ($dbg_values > 1);
1360 $type = 'T';
1361
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001362 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001363 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001364 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001365 push(@av_paren_type, $type);
1366 if ($2 ne '') {
1367 $av_pending = 'N';
1368 }
1369 $type = 'E';
1370
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001371 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001372 print "UNDEF($1)\n" if ($dbg_values > 1);
1373 $av_preprocessor = 1;
1374 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001375
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001376 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001377 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001378 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001379
1380 push(@av_paren_type, $type);
1381 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001382 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001383
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001384 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001385 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1386 $av_preprocessor = 1;
1387
1388 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1389
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001390 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001391
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001392 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001393 print "PRE_END($1)\n" if ($dbg_values > 1);
1394
1395 $av_preprocessor = 1;
1396
1397 # Assume all arms of the conditional end as this
1398 # one does, and continue as if the #endif was not here.
1399 pop(@av_paren_type);
1400 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001401 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001402
1403 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001405
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001406 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1407 print "ATTR($1)\n" if ($dbg_values > 1);
1408 $av_pending = $type;
1409 $type = 'N';
1410
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001411 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001412 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001414 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001415 }
1416 $type = 'N';
1417
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001418 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001419 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001420 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001421 $type = 'N';
1422
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001423 } elsif ($cur =~/^(case)/o) {
1424 print "CASE($1)\n" if ($dbg_values > 1);
1425 $av_pend_colon = 'C';
1426 $type = 'N';
1427
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001428 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001429 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001430 $type = 'N';
1431
1432 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001433 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001434 push(@av_paren_type, $av_pending);
1435 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001436 $type = 'N';
1437
1438 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001439 my $new_type = pop(@av_paren_type);
1440 if ($new_type ne '_') {
1441 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001442 print "PAREN('$1') -> $type\n"
1443 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001444 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001445 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001446 }
1447
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001448 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001450 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001451 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001452
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001453 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1454 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001455 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001456 } elsif ($type eq 'E') {
1457 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001458 }
1459 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1460 $type = 'V';
1461
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001462 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001463 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001464 $type = 'V';
1465
1466 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001467 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001468 $type = 'N';
1469
Andy Whitcroftcf655042008-03-04 14:28:20 -08001470 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001471 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001472 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001473 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001474
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001475 } elsif ($cur =~/^(,)/) {
1476 print "COMMA($1)\n" if ($dbg_values > 1);
1477 $type = 'C';
1478
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001479 } elsif ($cur =~ /^(\?)/o) {
1480 print "QUESTION($1)\n" if ($dbg_values > 1);
1481 $type = 'N';
1482
1483 } elsif ($cur =~ /^(:)/o) {
1484 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1485
1486 substr($var, length($res), 1, $av_pend_colon);
1487 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1488 $type = 'E';
1489 } else {
1490 $type = 'N';
1491 }
1492 $av_pend_colon = 'O';
1493
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001494 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001495 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001496 $type = 'N';
1497
Andy Whitcroft0d413862008-10-15 22:02:16 -07001498 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001499 my $variant;
1500
1501 print "OPV($1)\n" if ($dbg_values > 1);
1502 if ($type eq 'V') {
1503 $variant = 'B';
1504 } else {
1505 $variant = 'U';
1506 }
1507
1508 substr($var, length($res), 1, $variant);
1509 $type = 'N';
1510
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001511 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001512 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001513 if ($1 ne '++' && $1 ne '--') {
1514 $type = 'N';
1515 }
1516
1517 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001518 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001519 }
1520 if (defined $1) {
1521 $cur = substr($cur, length($1));
1522 $res .= $type x length($1);
1523 }
1524 }
1525
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001526 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001527}
1528
Andy Whitcroft8905a672007-11-28 16:21:06 -08001529sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001530 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001531 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001532 ^(?:
1533 $Modifier|
1534 $Storage|
1535 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001536 DEFINE_\S+
1537 )$|
1538 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001539 goto|
1540 return|
1541 case|
1542 else|
1543 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001544 do|
1545 \#|
1546 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001547 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001548 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001549 )}x;
1550 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1551 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001552 # Check for modifiers.
1553 $possible =~ s/\s*$Storage\s*//g;
1554 $possible =~ s/\s*$Sparse\s*//g;
1555 if ($possible =~ /^\s*$/) {
1556
1557 } elsif ($possible =~ /\s/) {
1558 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001559 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001560 if ($modifier !~ $notPermitted) {
1561 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1562 push(@modifierList, $modifier);
1563 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001564 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001565
1566 } else {
1567 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1568 push(@typeList, $possible);
1569 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001570 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001571 } else {
1572 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001573 }
1574}
1575
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001576my $prefix = '';
1577
Joe Perches000d1cc12011-07-25 17:13:25 -07001578sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001579 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001580
Joe Perchescbec18a2014-04-03 14:49:19 -07001581 return defined $use_type{$type} if (scalar keys %use_type > 0);
1582
1583 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001584}
1585
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001586sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001587 my ($level, $type, $msg) = @_;
1588
1589 if (!show_type($type) ||
1590 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001591 return 0;
1592 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001593 my $line;
1594 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001595 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001596 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001597 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001598 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001599 $line = (split('\n', $line))[0] . "\n" if ($terse);
1600
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001601 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001602
1603 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001604}
Joe Perchescbec18a2014-04-03 14:49:19 -07001605
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001606sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001607 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001608}
Joe Perches000d1cc12011-07-25 17:13:25 -07001609
Joe Perchesd752fcc2014-08-06 16:11:05 -07001610sub fixup_current_range {
1611 my ($lineRef, $offset, $length) = @_;
1612
1613 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1614 my $o = $1;
1615 my $l = $2;
1616 my $no = $o + $offset;
1617 my $nl = $l + $length;
1618 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1619 }
1620}
1621
1622sub fix_inserted_deleted_lines {
1623 my ($linesRef, $insertedRef, $deletedRef) = @_;
1624
1625 my $range_last_linenr = 0;
1626 my $delta_offset = 0;
1627
1628 my $old_linenr = 0;
1629 my $new_linenr = 0;
1630
1631 my $next_insert = 0;
1632 my $next_delete = 0;
1633
1634 my @lines = ();
1635
1636 my $inserted = @{$insertedRef}[$next_insert++];
1637 my $deleted = @{$deletedRef}[$next_delete++];
1638
1639 foreach my $old_line (@{$linesRef}) {
1640 my $save_line = 1;
1641 my $line = $old_line; #don't modify the array
1642 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1643 $delta_offset = 0;
1644 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1645 $range_last_linenr = $new_linenr;
1646 fixup_current_range(\$line, $delta_offset, 0);
1647 }
1648
1649 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1650 $deleted = @{$deletedRef}[$next_delete++];
1651 $save_line = 0;
1652 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1653 }
1654
1655 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1656 push(@lines, ${$inserted}{'LINE'});
1657 $inserted = @{$insertedRef}[$next_insert++];
1658 $new_linenr++;
1659 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1660 }
1661
1662 if ($save_line) {
1663 push(@lines, $line);
1664 $new_linenr++;
1665 }
1666
1667 $old_linenr++;
1668 }
1669
1670 return @lines;
1671}
1672
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001673sub fix_insert_line {
1674 my ($linenr, $line) = @_;
1675
1676 my $inserted = {
1677 LINENR => $linenr,
1678 LINE => $line,
1679 };
1680 push(@fixed_inserted, $inserted);
1681}
1682
1683sub fix_delete_line {
1684 my ($linenr, $line) = @_;
1685
1686 my $deleted = {
1687 LINENR => $linenr,
1688 LINE => $line,
1689 };
1690
1691 push(@fixed_deleted, $deleted);
1692}
1693
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001694sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001695 my ($type, $msg) = @_;
1696
1697 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001698 our $clean = 0;
1699 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001700 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001701 }
Joe Perches3705ce52013-07-03 15:05:31 -07001702 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001703}
1704sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001705 my ($type, $msg) = @_;
1706
1707 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001708 our $clean = 0;
1709 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001710 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001711 }
Joe Perches3705ce52013-07-03 15:05:31 -07001712 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001713}
1714sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001715 my ($type, $msg) = @_;
1716
1717 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718 our $clean = 0;
1719 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001720 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001721 }
Joe Perches3705ce52013-07-03 15:05:31 -07001722 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001723}
1724
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001725sub check_absolute_file {
1726 my ($absolute, $herecurr) = @_;
1727 my $file = $absolute;
1728
1729 ##print "absolute<$absolute>\n";
1730
1731 # See if any suffix of this path is a path within the tree.
1732 while ($file =~ s@^[^/]*/@@) {
1733 if (-f "$root/$file") {
1734 ##print "file<$file>\n";
1735 last;
1736 }
1737 }
1738 if (! -f _) {
1739 return 0;
1740 }
1741
1742 # It is, so see if the prefix is acceptable.
1743 my $prefix = $absolute;
1744 substr($prefix, -length($file)) = '';
1745
1746 ##print "prefix<$prefix>\n";
1747 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001748 WARN("USE_RELATIVE_PATH",
1749 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001750 }
1751}
1752
Joe Perches3705ce52013-07-03 15:05:31 -07001753sub trim {
1754 my ($string) = @_;
1755
Joe Perchesb34c6482013-09-11 14:24:01 -07001756 $string =~ s/^\s+|\s+$//g;
1757
1758 return $string;
1759}
1760
1761sub ltrim {
1762 my ($string) = @_;
1763
1764 $string =~ s/^\s+//;
1765
1766 return $string;
1767}
1768
1769sub rtrim {
1770 my ($string) = @_;
1771
1772 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001773
1774 return $string;
1775}
1776
Joe Perches52ea8502013-11-12 15:10:09 -08001777sub string_find_replace {
1778 my ($string, $find, $replace) = @_;
1779
1780 $string =~ s/$find/$replace/g;
1781
1782 return $string;
1783}
1784
Joe Perches3705ce52013-07-03 15:05:31 -07001785sub tabify {
1786 my ($leading) = @_;
1787
1788 my $source_indent = 8;
1789 my $max_spaces_before_tab = $source_indent - 1;
1790 my $spaces_to_tab = " " x $source_indent;
1791
1792 #convert leading spaces to tabs
1793 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1794 #Remove spaces before a tab
1795 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1796
1797 return "$leading";
1798}
1799
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001800sub pos_last_openparen {
1801 my ($line) = @_;
1802
1803 my $pos = 0;
1804
1805 my $opens = $line =~ tr/\(/\(/;
1806 my $closes = $line =~ tr/\)/\)/;
1807
1808 my $last_openparen = 0;
1809
1810 if (($opens == 0) || ($closes >= $opens)) {
1811 return -1;
1812 }
1813
1814 my $len = length($line);
1815
1816 for ($pos = 0; $pos < $len; $pos++) {
1817 my $string = substr($line, $pos);
1818 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1819 $pos += length($1) - 1;
1820 } elsif (substr($line, $pos, 1) eq '(') {
1821 $last_openparen = $pos;
1822 } elsif (index($string, '(') == -1) {
1823 last;
1824 }
1825 }
1826
Joe Perches91cb5192014-04-03 14:49:32 -07001827 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001828}
1829
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001830sub process {
1831 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001832
1833 my $linenr=0;
1834 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001835 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001836 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001837 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001838
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001839 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001840 my $indent;
1841 my $previndent=0;
1842 my $stashindent=0;
1843
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001844 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001845 my $signoff = 0;
1846 my $is_patch = 0;
1847
Joe Perches29ee1b02014-08-06 16:10:35 -07001848 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001849 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001850 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001851 my $non_utf8_charset = 0;
1852
Joe Perches365dd4e2014-08-06 16:10:42 -07001853 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001854 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001855
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001856 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001857 our $cnt_lines = 0;
1858 our $cnt_error = 0;
1859 our $cnt_warn = 0;
1860 our $cnt_chk = 0;
1861
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001862 # Trace the real file/line as we go.
1863 my $realfile = '';
1864 my $realline = 0;
1865 my $realcnt = 0;
1866 my $here = '';
1867 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001868 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001869 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001870 my $p1_prefix = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001871
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001872 my $prev_values = 'E';
1873
1874 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001875 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001876 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001877 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001878 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001879
Joe Perches7e51f192013-09-11 14:23:57 -07001880 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001881
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001882 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001883 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001884 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001885 my @setup_docs = ();
1886 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001887
Joe Perchesd8b07712013-11-12 15:10:06 -08001888 my $camelcase_file_seeded = 0;
1889
Andy Whitcroft773647a2008-03-28 14:15:58 -07001890 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001891 my $line;
1892 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001893 $linenr++;
1894 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001895
Joe Perches3705ce52013-07-03 15:05:31 -07001896 push(@fixed, $rawline) if ($fix);
1897
Andy Whitcroft773647a2008-03-28 14:15:58 -07001898 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001899 $setup_docs = 0;
1900 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1901 $setup_docs = 1;
1902 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001903 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001904 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001905 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1906 $realline=$1-1;
1907 if (defined $2) {
1908 $realcnt=$3+1;
1909 } else {
1910 $realcnt=1+1;
1911 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001912 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001913
1914 # Guestimate if this is a continuing comment. Run
1915 # the context looking for a comment "edge". If this
1916 # edge is a close comment then we must be in a comment
1917 # at context start.
1918 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001919 my $cnt = $realcnt;
1920 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1921 next if (defined $rawlines[$ln - 1] &&
1922 $rawlines[$ln - 1] =~ /^-/);
1923 $cnt--;
1924 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001925 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001926 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1927 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1928 ($edge) = $1;
1929 last;
1930 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001931 }
1932 if (defined $edge && $edge eq '*/') {
1933 $in_comment = 1;
1934 }
1935
1936 # Guestimate if this is a continuing comment. If this
1937 # is the start of a diff block and this line starts
1938 # ' *' then it is very likely a comment.
1939 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001940 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001941 {
1942 $in_comment = 1;
1943 }
1944
1945 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1946 sanitise_line_reset($in_comment);
1947
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001948 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001949 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001950 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001951 $line = sanitise_line($rawline);
1952 }
1953 push(@lines, $line);
1954
1955 if ($realcnt > 1) {
1956 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1957 } else {
1958 $realcnt = 0;
1959 }
1960
1961 #print "==>$rawline\n";
1962 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001963
1964 if ($setup_docs && $line =~ /^\+/) {
1965 push(@setup_docs, $line);
1966 }
1967 }
1968
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001969 $prefix = '';
1970
Andy Whitcroft773647a2008-03-28 14:15:58 -07001971 $realcnt = 0;
1972 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001973 $fixlinenr = -1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001974 foreach my $line (@lines) {
1975 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001976 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001977 my $sline = $line; #copy of $line
1978 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001979
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001980 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001981
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001982#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001983 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001984 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001985 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001986 $realline=$1-1;
1987 if (defined $2) {
1988 $realcnt=$3+1;
1989 } else {
1990 $realcnt=1+1;
1991 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001992 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001993 $prev_values = 'E';
1994
Andy Whitcroft773647a2008-03-28 14:15:58 -07001995 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001996 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001997 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001998 $suppress_statement = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001999 next;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002000
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002001# track the line number as we move through the hunk, note that
2002# new versions of GNU diff omit the leading space on completely
2003# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002004 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002005 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002006 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002007
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002008 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002009 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002010
2011 # Track the previous line.
2012 ($prevline, $stashline) = ($stashline, $line);
2013 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002014 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2015
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002017
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002018 } elsif ($realcnt == 1) {
2019 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002020 }
2021
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002022 my $hunk_line = ($realcnt != 0);
2023
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002024#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002025 $prefix = "$filename:$realline: " if ($emacs && $file);
2026 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2027
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002028 $here = "#$linenr: " if (!$file);
2029 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002031 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002032 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002033 if ($line =~ /^diff --git.*?(\S+)$/) {
2034 $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;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002037 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002038 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002039 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002040 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002041 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002042
2043 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002044 if (!$file && $tree && $p1_prefix ne '' &&
2045 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002046 WARN("PATCH_PREFIX",
2047 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002048 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002049
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002050 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002051 ERROR("MODIFIED_INCLUDE_ASM",
2052 "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 -07002053 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002054 $found_file = 1;
2055 }
2056
2057 if ($found_file) {
2058 if ($realfile =~ m@^(drivers/net/|net/)@) {
2059 $check = 1;
2060 } else {
2061 $check = $check_orig;
2062 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002063 next;
2064 }
2065
Randy Dunlap389834b2007-06-08 13:47:03 -07002066 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002067
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002068 my $hereline = "$here\n$rawline\n";
2069 my $herecurr = "$here\n$rawline\n";
2070 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002071
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002072 $cnt_lines++ if ($realcnt != 0);
2073
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002074# Check for incorrect file permissions
2075 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2076 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002077 if ($realfile !~ m@scripts/@ &&
2078 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002079 ERROR("EXECUTE_PERMISSIONS",
2080 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002081 }
2082 }
2083
Joe Perches20112472011-07-25 17:13:23 -07002084# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002085 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002086 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002087 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002088 }
2089
Joe Perchese0d975b2014-12-10 15:51:49 -08002090# Check if MAINTAINERS is being updated. If so, there's probably no need to
2091# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2092 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2093 $reported_maintainer_file = 1;
2094 }
2095
Joe Perches20112472011-07-25 17:13:23 -07002096# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002097 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002098 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002099 my $space_before = $1;
2100 my $sign_off = $2;
2101 my $space_after = $3;
2102 my $email = $4;
2103 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2104
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002105 if ($sign_off !~ /$signature_tags/) {
2106 WARN("BAD_SIGN_OFF",
2107 "Non-standard signature: $sign_off\n" . $herecurr);
2108 }
Joe Perches20112472011-07-25 17:13:23 -07002109 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002110 if (WARN("BAD_SIGN_OFF",
2111 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2112 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002113 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002114 "$ucfirst_sign_off $email";
2115 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002116 }
Joe Perches20112472011-07-25 17:13:23 -07002117 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002118 if (WARN("BAD_SIGN_OFF",
2119 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2120 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002121 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002122 "$ucfirst_sign_off $email";
2123 }
2124
Joe Perches20112472011-07-25 17:13:23 -07002125 }
2126 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002127 if (WARN("BAD_SIGN_OFF",
2128 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2129 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002130 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002131 "$ucfirst_sign_off $email";
2132 }
Joe Perches20112472011-07-25 17:13:23 -07002133 }
2134
2135 my ($email_name, $email_address, $comment) = parse_email($email);
2136 my $suggested_email = format_email(($email_name, $email_address));
2137 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002138 ERROR("BAD_SIGN_OFF",
2139 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002140 } else {
2141 my $dequoted = $suggested_email;
2142 $dequoted =~ s/^"//;
2143 $dequoted =~ s/" </ </;
2144 # Don't force email to have quotes
2145 # Allow just an angle bracketed address
2146 if ("$dequoted$comment" ne $email &&
2147 "<$email_address>$comment" ne $email &&
2148 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002149 WARN("BAD_SIGN_OFF",
2150 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002151 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002152 }
Joe Perches7e51f192013-09-11 14:23:57 -07002153
2154# Check for duplicate signatures
2155 my $sig_nospace = $line;
2156 $sig_nospace =~ s/\s//g;
2157 $sig_nospace = lc($sig_nospace);
2158 if (defined $signatures{$sig_nospace}) {
2159 WARN("BAD_SIGN_OFF",
2160 "Duplicate signature\n" . $herecurr);
2161 } else {
2162 $signatures{$sig_nospace} = 1;
2163 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002164 }
2165
Joe Perches9b3189eb2014-06-04 16:12:10 -07002166# Check for old stable address
2167 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2168 ERROR("STABLE_ADDRESS",
2169 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2170 }
2171
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002172# Check for unwanted Gerrit info
2173 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2174 ERROR("GERRIT_CHANGE_ID",
2175 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2176 }
2177
Joe Perches0d7835f2015-02-13 14:38:35 -08002178# Check for git id commit length and improperly formed commit descriptions
2179 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002180 my $init_char = $1;
2181 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002182 my $short = 1;
2183 my $long = 0;
2184 my $case = 1;
2185 my $space = 1;
2186 my $hasdesc = 0;
2187 my $id = '0123456789ab';
2188 my $orig_desc = "commit description";
2189 my $description = "";
2190
2191 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2192 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2193 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2194 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2195 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2196 $orig_desc = $1;
2197 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2198 defined $rawlines[$linenr] &&
2199 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2200 $orig_desc = $1;
2201 }
2202
2203 ($id, $description) = git_commit_info($orig_commit,
2204 $id, $orig_desc);
2205
2206 if ($short || $long || $space || $case || ($orig_desc ne $description)) {
2207 ERROR("GIT_COMMIT_ID",
2208 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2209 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002210 }
2211
Joe Perches13f19372014-08-06 16:10:59 -07002212# Check for added, moved or deleted files
2213 if (!$reported_maintainer_file && !$in_commit_log &&
2214 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2215 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2216 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2217 (defined($1) || defined($2))))) {
2218 $reported_maintainer_file = 1;
2219 WARN("FILE_PATH_CHANGES",
2220 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2221 }
2222
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002223# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002224 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002225 ERROR("CORRUPTED_PATCH",
2226 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002227 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002228 }
2229
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002230# Check for absolute kernel paths.
2231 if ($tree) {
2232 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2233 my $file = $1;
2234
2235 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2236 check_absolute_file($1, $herecurr)) {
2237 #
2238 } else {
2239 check_absolute_file($file, $herecurr);
2240 }
2241 }
2242 }
2243
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002244# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2245 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002246 $rawline !~ m/^$UTF8*$/) {
2247 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2248
2249 my $blank = copy_spacing($rawline);
2250 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2251 my $hereptr = "$hereline$ptr\n";
2252
Joe Perches34d99212011-07-25 17:13:26 -07002253 CHK("INVALID_UTF8",
2254 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002255 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002256
Joe Perches15662b32011-10-31 17:13:12 -07002257# Check if it's the start of a commit log
2258# (not a header line and we haven't seen the patch filename)
2259 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002260 !($rawline =~ /^\s+\S/ ||
2261 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002262 $in_header_lines = 0;
2263 $in_commit_log = 1;
2264 }
2265
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002266# Check if there is UTF-8 in a commit log when a mail header has explicitly
2267# declined it, i.e defined some charset where it is missing.
2268 if ($in_header_lines &&
2269 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2270 $1 !~ /utf-8/i) {
2271 $non_utf8_charset = 1;
2272 }
2273
2274 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002275 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002276 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002277 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2278 }
2279
Kees Cook66b47b42014-10-13 15:51:57 -07002280# Check for various typo / spelling mistakes
Joe Perches36061e32014-12-10 15:51:43 -08002281 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
Kees Cook66b47b42014-10-13 15:51:57 -07002282 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2283 my $typo = $1;
2284 my $typo_fix = $spelling_fix{lc($typo)};
2285 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2286 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2287 my $msg_type = \&WARN;
2288 $msg_type = \&CHK if ($file);
2289 if (&{$msg_type}("TYPO_SPELLING",
2290 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2291 $fix) {
2292 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2293 }
2294 }
2295 }
2296
Andy Whitcroft306708542008-10-15 22:02:28 -07002297# ignore non-hunk lines and lines being removed
2298 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002299
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002300#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002301 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002302 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002303 if (ERROR("DOS_LINE_ENDINGS",
2304 "DOS line endings\n" . $herevet) &&
2305 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002306 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002307 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002308 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2309 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002310 if (ERROR("TRAILING_WHITESPACE",
2311 "trailing whitespace\n" . $herevet) &&
2312 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002313 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002314 }
2315
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002316 $rpt_cleaners = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002317 }
Andy Whitcroft5368df202008-10-15 22:02:27 -07002318
Josh Triplett4783f892013-11-12 15:10:12 -08002319# Check for FSF mailing addresses.
Alexander Duyck109d8cb22014-01-23 15:54:50 -08002320 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002321 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2322 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002323 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2324 my $msg_type = \&ERROR;
2325 $msg_type = \&CHK if ($file);
2326 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002327 "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 -08002328 }
2329
Andi Kleen33549572010-05-24 14:33:29 -07002330# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002331# Only applies when adding the entry originally, after that we do not have
2332# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002333 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002334 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002335 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002336 my $cnt = $realcnt;
2337 my $ln = $linenr + 1;
2338 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002339 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002340 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002341 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002342 $f = $lines[$ln - 1];
2343 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2344 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002345
2346 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002347 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002348
Joe Perches8d73e0e2014-08-06 16:10:46 -07002349 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002350 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002351 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002352 $length = -1;
2353 }
2354
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002355 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002356 $f =~ s/#.*//;
2357 $f =~ s/^\s+//;
2358 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002359 if ($f =~ /^\s*config\s/) {
2360 $is_end = 1;
2361 last;
2362 }
Andi Kleen33549572010-05-24 14:33:29 -07002363 $length++;
2364 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002365 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2366 WARN("CONFIG_DESCRIPTION",
2367 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2368 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002369 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002370 }
2371
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002372# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2373 if ($realfile =~ /Kconfig/ &&
2374 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2375 WARN("CONFIG_EXPERIMENTAL",
2376 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2377 }
2378
Christoph Jaeger327953e2015-02-13 14:38:29 -08002379# discourage the use of boolean for type definition attributes of Kconfig options
2380 if ($realfile =~ /Kconfig/ &&
2381 $line =~ /^\+\s*\bboolean\b/) {
2382 WARN("CONFIG_TYPE_BOOLEAN",
2383 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2384 }
2385
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002386 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2387 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2388 my $flag = $1;
2389 my $replacement = {
2390 'EXTRA_AFLAGS' => 'asflags-y',
2391 'EXTRA_CFLAGS' => 'ccflags-y',
2392 'EXTRA_CPPFLAGS' => 'cppflags-y',
2393 'EXTRA_LDFLAGS' => 'ldflags-y',
2394 };
2395
2396 WARN("DEPRECATED_VARIABLE",
2397 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2398 }
2399
Rob Herringbff5da42014-01-23 15:54:51 -08002400# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002401 if (defined $root &&
2402 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2403 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2404
Rob Herringbff5da42014-01-23 15:54:51 -08002405 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2406
Florian Vaussardcc933192014-04-03 14:49:27 -07002407 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2408 my $vp_file = $dt_path . "vendor-prefixes.txt";
2409
Rob Herringbff5da42014-01-23 15:54:51 -08002410 foreach my $compat (@compats) {
2411 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002412 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2413 my $compat3 = $compat;
2414 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2415 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002416 if ( $? >> 8 ) {
2417 WARN("UNDOCUMENTED_DT_STRING",
2418 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2419 }
2420
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002421 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2422 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002423 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002424 if ( $? >> 8 ) {
2425 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002426 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002427 }
2428 }
2429 }
2430
Andy Whitcroft5368df202008-10-15 22:02:27 -07002431# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002432 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df202008-10-15 22:02:27 -07002433
Joe Perches6cd7f382012-12-17 16:01:54 -08002434#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002435 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002436 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002437 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002438 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002439 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002440 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002441 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002442 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002443 }
2444
Andy Whitcroft8905a672007-11-28 16:21:06 -08002445# check for adding lines without a newline.
2446 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002447 WARN("MISSING_EOF_NEWLINE",
2448 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002449 }
2450
Mike Frysinger42e41c52009-09-21 17:04:40 -07002451# Blackfin: use hi/lo macros
2452 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2453 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2454 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002455 ERROR("LO_MACRO",
2456 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002457 }
2458 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2459 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002460 ERROR("HI_MACRO",
2461 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002462 }
2463 }
2464
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002465# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002466 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002467
2468# at the beginning of a line any tabs must come first and anything
2469# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002470 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2471 $rawline =~ /^\+\s* \s*/) {
2472 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002473 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002474 if (ERROR("CODE_INDENT",
2475 "code indent should use tabs where possible\n" . $herevet) &&
2476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002477 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002478 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07002479 }
2480
Alberto Panizzo08e44362010-03-05 13:43:54 -08002481# check for space before tabs.
2482 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2483 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002484 if (WARN("SPACE_BEFORE_TAB",
2485 "please, no space before tabs\n" . $herevet) &&
2486 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002487 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002488 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002489 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002490 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002491 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002492 }
2493
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002494# check for && or || at the start of a line
2495 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2496 CHK("LOGICAL_CONTINUATIONS",
2497 "Logical continuations should be on the previous line\n" . $hereprev);
2498 }
2499
2500# check multi-line statement indentation matches previous line
2501 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002502 $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 -07002503 $prevline =~ /^\+(\t*)(.*)$/;
2504 my $oldindent = $1;
2505 my $rest = $2;
2506
2507 my $pos = pos_last_openparen($rest);
2508 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002509 $line =~ /^(\+| )([ \t]*)/;
2510 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002511
2512 my $goodtabindent = $oldindent .
2513 "\t" x ($pos / 8) .
2514 " " x ($pos % 8);
2515 my $goodspaceindent = $oldindent . " " x $pos;
2516
2517 if ($newindent ne $goodtabindent &&
2518 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002519
2520 if (CHK("PARENTHESIS_ALIGNMENT",
2521 "Alignment should match open parenthesis\n" . $hereprev) &&
2522 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002523 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002524 s/^\+[ \t]*/\+$goodtabindent/;
2525 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002526 }
2527 }
2528 }
2529
Joe Perches04941aa2014-12-10 15:51:37 -08002530 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2531 (!defined($1) || $1 !~ /sizeof\s*/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002532 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002533 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002534 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002535 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002536 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002537 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002538 }
2539
Joe Perches05880602012-10-04 17:13:35 -07002540 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002541 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002542 $rawline =~ /^\+[ \t]*\*/ &&
2543 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002544 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2545 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2546 }
2547
2548 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002549 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2550 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002551 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002552 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2553 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2554 "networking block comments start with * on subsequent lines\n" . $hereprev);
2555 }
2556
2557 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002558 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2559 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2560 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2561 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002562 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2563 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2564 }
2565
Joe Perches7f619192014-08-06 16:10:39 -07002566# check for missing blank lines after struct/union declarations
2567# with exceptions for various attributes and macros
2568 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2569 $line =~ /^\+/ &&
2570 !($line =~ /^\+\s*$/ ||
2571 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2572 $line =~ /^\+\s*MODULE_/i ||
2573 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2574 $line =~ /^\+[a-z_]*init/ ||
2575 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2576 $line =~ /^\+\s*DECLARE/ ||
2577 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002578 if (CHK("LINE_SPACING",
2579 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2580 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002581 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002582 }
Joe Perches7f619192014-08-06 16:10:39 -07002583 }
2584
Joe Perches365dd4e2014-08-06 16:10:42 -07002585# check for multiple consecutive blank lines
2586 if ($prevline =~ /^[\+ ]\s*$/ &&
2587 $line =~ /^\+\s*$/ &&
2588 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002589 if (CHK("LINE_SPACING",
2590 "Please don't use multiple blank lines\n" . $hereprev) &&
2591 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002592 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002593 }
2594
Joe Perches365dd4e2014-08-06 16:10:42 -07002595 $last_blank_line = $linenr;
2596 }
2597
Joe Perches3b617e32014-04-03 14:49:28 -07002598# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002599 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2600 # actual declarations
2601 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002602 # function pointer declarations
2603 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002604 # foo bar; where foo is some local typedef or #define
2605 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2606 # known declaration macros
2607 $prevline =~ /^\+\s+$declaration_macros/) &&
2608 # for "else if" which can look like "$Ident $Ident"
2609 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2610 # other possible extensions of declaration lines
2611 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2612 # not starting a section or a macro "\" extended line
2613 $prevline =~ /(?:\{\s*|\\)$/) &&
2614 # looks like a declaration
2615 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002616 # function pointer declarations
2617 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002618 # foo bar; where foo is some local typedef or #define
2619 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2620 # known declaration macros
2621 $sline =~ /^\+\s+$declaration_macros/ ||
2622 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002623 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002624 # start or end of block or continuation of declaration
2625 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2626 # bitfield continuation
2627 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2628 # other possible extensions of declaration lines
2629 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2630 # indentation of previous and current line are the same
2631 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002632 if (WARN("LINE_SPACING",
2633 "Missing a blank line after declarations\n" . $hereprev) &&
2634 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002635 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002636 }
Joe Perches3b617e32014-04-03 14:49:28 -07002637 }
2638
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002639# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002640# Exceptions:
2641# 1) within comments
2642# 2) indented preprocessor commands
2643# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002644 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002645 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002646 if (WARN("LEADING_SPACE",
2647 "please, no spaces at the start of a line\n" . $herevet) &&
2648 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002649 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002650 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002651 }
2652
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002653# check we are in a valid C source file if not then ignore this hunk
2654 next if ($realfile !~ /\.(h|c)$/);
2655
Joe Perches032a4c02014-08-06 16:10:29 -07002656# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002657# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002658# if the previous line is a break or return and is indented 1 tab more...
2659 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2660 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002661 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2662 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2663 defined $lines[$linenr] &&
2664 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002665 WARN("UNNECESSARY_ELSE",
2666 "else is not generally useful after a break or return\n" . $hereprev);
2667 }
2668 }
2669
Joe Perchesc00df192014-08-06 16:11:01 -07002670# check indentation of a line with a break;
2671# if the previous line is a goto or return and is indented the same # of tabs
2672 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2673 my $tabs = $1;
2674 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2675 WARN("UNNECESSARY_BREAK",
2676 "break is not useful after a goto or return\n" . $hereprev);
2677 }
2678 }
2679
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002680# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2681 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2682 WARN("CONFIG_EXPERIMENTAL",
2683 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2684 }
2685
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002686# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002687 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002688 WARN("CVS_KEYWORD",
2689 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002690 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002691
Mike Frysinger42e41c52009-09-21 17:04:40 -07002692# Blackfin: don't use __builtin_bfin_[cs]sync
2693 if ($line =~ /__builtin_bfin_csync/) {
2694 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002695 ERROR("CSYNC",
2696 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002697 }
2698 if ($line =~ /__builtin_bfin_ssync/) {
2699 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002700 ERROR("SSYNC",
2701 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002702 }
2703
Joe Perches56e77d72013-02-21 16:44:14 -08002704# check for old HOTPLUG __dev<foo> section markings
2705 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2706 WARN("HOTPLUG_SECTION",
2707 "Using $1 is unnecessary\n" . $herecurr);
2708 }
2709
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002710# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002711 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2712 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002713#print "LINE<$line>\n";
2714 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002715 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002716 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002717 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002718 $stat =~ s/\n./\n /g;
2719 $cond =~ s/\n./\n /g;
2720
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002721#print "linenr<$linenr> <$stat>\n";
2722 # If this statement has no statement boundaries within
2723 # it there is no point in retrying a statement scan
2724 # until we hit end of it.
2725 my $frag = $stat; $frag =~ s/;+\s*$//;
2726 if ($frag !~ /(?:{|;)/) {
2727#print "skip<$line_nr_next>\n";
2728 $suppress_statement = $line_nr_next;
2729 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002730
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002731 # Find the real next line.
2732 $realline_next = $line_nr_next;
2733 if (defined $realline_next &&
2734 (!defined $lines[$realline_next - 1] ||
2735 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2736 $realline_next++;
2737 }
2738
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002739 my $s = $stat;
2740 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002741
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002742 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002743 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002744
2745 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002746 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002747
Andy Whitcroft463f2862009-09-21 17:04:34 -07002748 } elsif ($s =~ /^.\s*else\b/s) {
2749
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002750 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002751 } 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 -07002752 my $type = $1;
2753 $type =~ s/\s+/ /g;
2754 possible($type, "A:" . $s);
2755
Andy Whitcroft8905a672007-11-28 16:21:06 -08002756 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002757 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002758 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002759 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002760
2761 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002762 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002763 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002764 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002765
2766 # Check for any sort of function declaration.
2767 # int foo(something bar, other baz);
2768 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002769 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 -08002770 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002771
Andy Whitcroftcf655042008-03-04 14:28:20 -08002772 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002773 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002774 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002775
Andy Whitcroft8905a672007-11-28 16:21:06 -08002776 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002777 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002778
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002779 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002780 }
2781 }
2782 }
2783
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002784 }
2785
Andy Whitcroft653d4872007-06-23 17:16:34 -07002786#
2787# Checks which may be anchored in the context.
2788#
2789
2790# Check for switch () and associated case and default
2791# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002792 if ($line=~/\bswitch\s*\(.*\)/) {
2793 my $err = '';
2794 my $sep = '';
2795 my @ctx = ctx_block_outer($linenr, $realcnt);
2796 shift(@ctx);
2797 for my $ctx (@ctx) {
2798 my ($clen, $cindent) = line_stats($ctx);
2799 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2800 $indent != $cindent) {
2801 $err .= "$sep$ctx\n";
2802 $sep = '';
2803 } else {
2804 $sep = "[...]\n";
2805 }
2806 }
2807 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002808 ERROR("SWITCH_CASE_INDENT_LEVEL",
2809 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002810 }
2811 }
2812
2813# if/while/etc brace do not go on next line, unless defining a do while loop,
2814# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002815 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 -07002816 my $pre_ctx = "$1$2";
2817
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002818 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002819
2820 if ($line =~ /^\+\t{6,}/) {
2821 WARN("DEEP_INDENTATION",
2822 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2823 }
2824
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002825 my $ctx_cnt = $realcnt - $#ctx - 1;
2826 my $ctx = join("\n", @ctx);
2827
Andy Whitcroft548596d2008-07-23 21:29:01 -07002828 my $ctx_ln = $linenr;
2829 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002830
Andy Whitcroft548596d2008-07-23 21:29:01 -07002831 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2832 defined $lines[$ctx_ln - 1] &&
2833 $lines[$ctx_ln - 1] =~ /^-/)) {
2834 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2835 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002836 $ctx_ln++;
2837 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002838
Andy Whitcroft53210162008-07-23 21:29:03 -07002839 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2840 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002841
Joe Perchesd752fcc2014-08-06 16:11:05 -07002842 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002843 ERROR("OPEN_BRACE",
2844 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002845 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002846 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002847 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2848 $ctx =~ /\)\s*\;\s*$/ &&
2849 defined $lines[$ctx_ln - 1])
2850 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002851 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2852 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002853 WARN("TRAILING_SEMICOLON",
2854 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002855 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002856 }
2857 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002858 }
2859
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002860# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002861 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 -08002862 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2863 ctx_statement_block($linenr, $realcnt, 0)
2864 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002865 my ($s, $c) = ($stat, $cond);
2866
2867 substr($s, 0, length($c), '');
2868
2869 # Make sure we remove the line prefixes as we have
2870 # none on the first line, and are going to readd them
2871 # where necessary.
2872 $s =~ s/\n./\n/gs;
2873
2874 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002875 my @newlines = ($c =~ /\n/gs);
2876 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002877
2878 # We want to check the first line inside the block
2879 # starting at the end of the conditional, so remove:
2880 # 1) any blank line termination
2881 # 2) any opening brace { on end of the line
2882 # 3) any do (...) {
2883 my $continuation = 0;
2884 my $check = 0;
2885 $s =~ s/^.*\bdo\b//;
2886 $s =~ s/^\s*{//;
2887 if ($s =~ s/^\s*\\//) {
2888 $continuation = 1;
2889 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002890 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002891 $check = 1;
2892 $cond_lines++;
2893 }
2894
2895 # Also ignore a loop construct at the end of a
2896 # preprocessor statement.
2897 if (($prevline =~ /^.\s*#\s*define\s/ ||
2898 $prevline =~ /\\\s*$/) && $continuation == 0) {
2899 $check = 0;
2900 }
2901
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002902 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002903 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002904 while ($cond_ptr != $cond_lines) {
2905 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002906
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002907 # If we see an #else/#elif then the code
2908 # is not linear.
2909 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2910 $check = 0;
2911 }
2912
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002913 # Ignore:
2914 # 1) blank lines, they should be at 0,
2915 # 2) preprocessor lines, and
2916 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002917 if ($continuation ||
2918 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002919 $s =~ /^\s*#\s*?/ ||
2920 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002921 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002922 if ($s =~ s/^.*?\n//) {
2923 $cond_lines++;
2924 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002925 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002926 }
2927
2928 my (undef, $sindent) = line_stats("+" . $s);
2929 my $stat_real = raw_line($linenr, $cond_lines);
2930
2931 # Check if either of these lines are modified, else
2932 # this is not this patch's fault.
2933 if (!defined($stat_real) ||
2934 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2935 $check = 0;
2936 }
2937 if (defined($stat_real) && $cond_lines > 1) {
2938 $stat_real = "[...]\n$stat_real";
2939 }
2940
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002941 #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 -07002942
2943 if ($check && (($sindent % 8) != 0 ||
2944 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002945 WARN("SUSPECT_CODE_INDENT",
2946 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002947 }
2948 }
2949
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002950 # Track the 'values' across context and added lines.
2951 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002952 my ($curr_values, $curr_vars) =
2953 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002954 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002955 if ($dbg_values) {
2956 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002957 print "$linenr > .$outline\n";
2958 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002959 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002960 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002961 $prev_values = substr($curr_values, -1);
2962
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002963#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002964 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07002965
Andy Whitcroft653d4872007-06-23 17:16:34 -07002966# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002967 if ($dbg_type) {
2968 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002969 ERROR("TEST_TYPE",
2970 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002971 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002972 ERROR("TEST_NOT_TYPE",
2973 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002974 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002975 next;
2976 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002977# TEST: allow direct testing of the attribute matcher.
2978 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002979 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002980 ERROR("TEST_ATTR",
2981 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08002982 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002983 ERROR("TEST_NOT_ATTR",
2984 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07002985 }
2986 next;
2987 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002988
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002989# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07002990 if ($line =~ /^.\s*{/ &&
2991 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002992 if (ERROR("OPEN_BRACE",
2993 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002994 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2995 fix_delete_line($fixlinenr - 1, $prevrawline);
2996 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002997 my $fixedline = $prevrawline;
2998 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002999 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003000 $fixedline = $line;
3001 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003002 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003003 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003004 }
3005
Andy Whitcroft653d4872007-06-23 17:16:34 -07003006#
3007# Checks which are anchored on the added line.
3008#
3009
3010# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003011 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003012 my $path = $1;
3013 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003014 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003015 "malformed #include filename\n" . $herecurr);
3016 }
3017 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3018 ERROR("UAPI_INCLUDE",
3019 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003020 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003021 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003022
3023# no C99 // comments
3024 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003025 if (ERROR("C99_COMMENTS",
3026 "do not use C99 // comments\n" . $herecurr) &&
3027 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003028 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003029 if ($line =~ /\/\/(.*)$/) {
3030 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003031 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003032 }
3033 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003034 }
3035 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003036 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003037 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003038
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003039# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3040# the whole statement.
3041#print "APW <$lines[$realline_next - 1]>\n";
3042 if (defined $realline_next &&
3043 exists $lines[$realline_next - 1] &&
3044 !defined $suppress_export{$realline_next} &&
3045 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3046 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003047 # Handle definitions which produce identifiers with
3048 # a prefix:
3049 # XXX(foo);
3050 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003051 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003052 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003053 $name =~ /^${Ident}_$2/) {
3054#print "FOO C name<$name>\n";
3055 $suppress_export{$realline_next} = 1;
3056
3057 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003058 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003059 ^.DEFINE_$Ident\(\Q$name\E\)|
3060 ^.DECLARE_$Ident\(\Q$name\E\)|
3061 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003062 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3063 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003064 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003065#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3066 $suppress_export{$realline_next} = 2;
3067 } else {
3068 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003069 }
3070 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003071 if (!defined $suppress_export{$linenr} &&
3072 $prevline =~ /^.\s*$/ &&
3073 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3074 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3075#print "FOO B <$lines[$linenr - 1]>\n";
3076 $suppress_export{$linenr} = 2;
3077 }
3078 if (defined $suppress_export{$linenr} &&
3079 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003080 WARN("EXPORT_SYMBOL",
3081 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003082 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003083
Joe Eloff5150bda2010-08-09 17:21:00 -07003084# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003085 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3086 if (ERROR("GLOBAL_INITIALISERS",
3087 "do not initialise globals to 0 or NULL\n" .
3088 $herecurr) &&
3089 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003090 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003091 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003092 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003093# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003094 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3095 if (ERROR("INITIALISED_STATIC",
3096 "do not initialise statics to 0 or NULL\n" .
3097 $herecurr) &&
3098 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003099 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003100 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003101 }
3102
Joe Perches18130872014-08-06 16:11:22 -07003103# check for misordered declarations of char/short/int/long with signed/unsigned
3104 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3105 my $tmp = trim($1);
3106 WARN("MISORDERED_TYPE",
3107 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3108 }
3109
Joe Perchescb710ec2010-10-26 14:23:20 -07003110# check for static const char * arrays.
3111 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003112 WARN("STATIC_CONST_CHAR_ARRAY",
3113 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003114 $herecurr);
3115 }
3116
3117# check for static char foo[] = "bar" declarations.
3118 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003119 WARN("STATIC_CONST_CHAR_ARRAY",
3120 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003121 $herecurr);
3122 }
3123
Joe Perches9b0fa602014-04-03 14:49:18 -07003124# check for non-global char *foo[] = {"bar", ...} declarations.
3125 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3126 WARN("STATIC_CONST_CHAR_ARRAY",
3127 "char * array declaration might be better as static const\n" .
3128 $herecurr);
3129 }
3130
Joe Perchesb36190c2014-01-27 17:07:18 -08003131# check for function declarations without arguments like "int foo()"
3132 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3133 if (ERROR("FUNCTION_WITHOUT_ARGS",
3134 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3135 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003136 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003137 }
3138 }
3139
Joe Perches92e112f2013-12-13 11:36:22 -07003140# check for uses of DEFINE_PCI_DEVICE_TABLE
3141 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3142 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3143 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3144 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003145 $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 -07003146 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003147 }
3148
Andy Whitcroft653d4872007-06-23 17:16:34 -07003149# check for new typedefs, only function parameters and sparse annotations
3150# make sense.
3151 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003152 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003153 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003154 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003155 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003156 WARN("NEW_TYPEDEFS",
3157 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003158 }
3159
3160# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003161 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003162 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3163 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003164 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003165
Andy Whitcroft65863862009-01-06 14:41:21 -08003166 # Should start with a space.
3167 $to =~ s/^(\S)/ $1/;
3168 # Should not end with a space.
3169 $to =~ s/\s+$//;
3170 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003171 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003172 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003173
Joe Perches3705ce52013-07-03 15:05:31 -07003174## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003175 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003176 if (ERROR("POINTER_LOCATION",
3177 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3178 $fix) {
3179 my $sub_from = $ident;
3180 my $sub_to = $ident;
3181 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003182 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003183 s@\Q$sub_from\E@$sub_to@;
3184 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003185 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003186 }
3187 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3188 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003189 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003190
Andy Whitcroft65863862009-01-06 14:41:21 -08003191 # Should start with a space.
3192 $to =~ s/^(\S)/ $1/;
3193 # Should not end with a space.
3194 $to =~ s/\s+$//;
3195 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003196 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003197 }
3198 # Modifiers should have spaces.
3199 $to =~ s/(\b$Modifier$)/$1 /;
3200
Joe Perches3705ce52013-07-03 15:05:31 -07003201## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003202 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003203 if (ERROR("POINTER_LOCATION",
3204 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3205 $fix) {
3206
3207 my $sub_from = $match;
3208 my $sub_to = $match;
3209 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003210 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003211 s@\Q$sub_from\E@$sub_to@;
3212 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003213 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003214 }
3215
3216# # no BUG() or BUG_ON()
3217# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3218# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3219# print "$herecurr";
3220# $clean = 0;
3221# }
3222
Andy Whitcroft8905a672007-11-28 16:21:06 -08003223 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003224 WARN("LINUX_VERSION_CODE",
3225 "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 -08003226 }
3227
Joe Perches17441222011-06-15 15:08:17 -07003228# check for uses of printk_ratelimit
3229 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003230 WARN("PRINTK_RATELIMITED",
3231"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003232 }
3233
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003234# printk should use KERN_* levels. Note that follow on printk's on the
3235# same line do not need a level, so we use the current block context
3236# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003237# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003238# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003239 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003240 my $ok = 0;
3241 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3242 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003243 # we have a preceding printk if it ends
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003244 # with "\n" ignore it, else it is to blame
3245 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3246 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3247 $ok = 1;
3248 }
3249 last;
3250 }
3251 }
3252 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003253 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3254 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003255 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003256 }
3257
Joe Perches243f3802012-05-31 16:26:09 -07003258 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3259 my $orig = $1;
3260 my $level = lc($orig);
3261 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003262 my $level2 = $level;
3263 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003264 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003265 "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 -07003266 }
3267
3268 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003269 if (WARN("PREFER_PR_LEVEL",
3270 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3271 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003272 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003273 s/\bpr_warning\b/pr_warn/;
3274 }
Joe Perches243f3802012-05-31 16:26:09 -07003275 }
3276
Joe Perchesdc139312013-02-21 16:44:13 -08003277 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3278 my $orig = $1;
3279 my $level = lc($orig);
3280 $level = "warn" if ($level eq "warning");
3281 $level = "dbg" if ($level eq "debug");
3282 WARN("PREFER_DEV_LEVEL",
3283 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3284 }
3285
Andy Whitcroft653d4872007-06-23 17:16:34 -07003286# function brace can't be on same line, except for #defines of do while,
3287# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003288 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003289 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003290 if (ERROR("OPEN_BRACE",
3291 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3292 $fix) {
3293 fix_delete_line($fixlinenr, $rawline);
3294 my $fixed_line = $rawline;
3295 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3296 my $line1 = $1;
3297 my $line2 = $2;
3298 fix_insert_line($fixlinenr, ltrim($line1));
3299 fix_insert_line($fixlinenr, "\+{");
3300 if ($line2 !~ /^\s*$/) {
3301 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3302 }
3303 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003304 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003305
Andy Whitcroft8905a672007-11-28 16:21:06 -08003306# open braces for enum, union and struct go on the same line.
3307 if ($line =~ /^.\s*{/ &&
3308 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003309 if (ERROR("OPEN_BRACE",
3310 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3311 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3312 fix_delete_line($fixlinenr - 1, $prevrawline);
3313 fix_delete_line($fixlinenr, $rawline);
3314 my $fixedline = rtrim($prevrawline) . " {";
3315 fix_insert_line($fixlinenr, $fixedline);
3316 $fixedline = $rawline;
3317 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3318 if ($fixedline !~ /^\+\s*$/) {
3319 fix_insert_line($fixlinenr, $fixedline);
3320 }
3321 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003322 }
3323
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003324# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003325 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3326 if (WARN("SPACING",
3327 "missing space after $1 definition\n" . $herecurr) &&
3328 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003329 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003330 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3331 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003332 }
3333
Joe Perches31070b52014-01-23 15:54:49 -08003334# Function pointer declarations
3335# check spacing between type, funcptr, and args
3336# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003337 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003338 my $declare = $1;
3339 my $pre_pointer_space = $2;
3340 my $post_pointer_space = $3;
3341 my $funcname = $4;
3342 my $post_funcname_space = $5;
3343 my $pre_args_space = $6;
3344
Joe Perches91f72e92014-04-03 14:49:12 -07003345# the $Declare variable will capture all spaces after the type
3346# so check it for a missing trailing missing space but pointer return types
3347# don't need a space so don't warn for those.
3348 my $post_declare_space = "";
3349 if ($declare =~ /(\s+)$/) {
3350 $post_declare_space = $1;
3351 $declare = rtrim($declare);
3352 }
3353 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003354 WARN("SPACING",
3355 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003356 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003357 }
3358
3359# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003360# This test is not currently implemented because these declarations are
3361# equivalent to
3362# int foo(int bar, ...)
3363# and this is form shouldn't/doesn't generate a checkpatch warning.
3364#
3365# elsif ($declare =~ /\s{2,}$/) {
3366# WARN("SPACING",
3367# "Multiple spaces after return type\n" . $herecurr);
3368# }
Joe Perches31070b52014-01-23 15:54:49 -08003369
3370# unnecessary space "type ( *funcptr)(args...)"
3371 if (defined $pre_pointer_space &&
3372 $pre_pointer_space =~ /^\s/) {
3373 WARN("SPACING",
3374 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3375 }
3376
3377# unnecessary space "type (* funcptr)(args...)"
3378 if (defined $post_pointer_space &&
3379 $post_pointer_space =~ /^\s/) {
3380 WARN("SPACING",
3381 "Unnecessary space before function pointer name\n" . $herecurr);
3382 }
3383
3384# unnecessary space "type (*funcptr )(args...)"
3385 if (defined $post_funcname_space &&
3386 $post_funcname_space =~ /^\s/) {
3387 WARN("SPACING",
3388 "Unnecessary space after function pointer name\n" . $herecurr);
3389 }
3390
3391# unnecessary space "type (*funcptr) (args...)"
3392 if (defined $pre_args_space &&
3393 $pre_args_space =~ /^\s/) {
3394 WARN("SPACING",
3395 "Unnecessary space before function pointer arguments\n" . $herecurr);
3396 }
3397
3398 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003399 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003400 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003401 }
3402 }
3403
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003404# check for spacing round square brackets; allowed:
3405# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003406# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3407# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003408 while ($line =~ /(.*?\s)\[/g) {
3409 my ($where, $prefix) = ($-[1], $1);
3410 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003411 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003412 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003413 if (ERROR("BRACKET_SPACE",
3414 "space prohibited before open square bracket '['\n" . $herecurr) &&
3415 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003416 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003417 s/^(\+.*?)\s+\[/$1\[/;
3418 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003419 }
3420 }
3421
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003422# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003423 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003424 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003425 my $ctx_before = substr($line, 0, $-[1]);
3426 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003427
3428 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003429 if ($name =~ /^(?:
3430 if|for|while|switch|return|case|
3431 volatile|__volatile__|
3432 __attribute__|format|__extension__|
3433 asm|__asm__)$/x)
3434 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003435 # cpp #define statements have non-optional spaces, ie
3436 # if there is a space between the name and the open
3437 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003438 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003439
3440 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003441 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003442
3443 # If this whole things ends with a type its most
3444 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003445 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003446
3447 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003448 if (WARN("SPACING",
3449 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3450 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003451 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003452 s/\b$name\s+\(/$name\(/;
3453 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003454 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003455 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003456
Andy Whitcroft653d4872007-06-23 17:16:34 -07003457# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003458 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003459 my $fixed_line = "";
3460 my $line_fixed = 0;
3461
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003462 my $ops = qr{
3463 <<=|>>=|<=|>=|==|!=|
3464 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3465 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003466 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003467 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003468 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003469 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003470
3471## print("element count: <" . $#elements . ">\n");
3472## foreach my $el (@elements) {
3473## print("el: <$el>\n");
3474## }
3475
3476 my @fix_elements = ();
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003477 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003478
Joe Perches3705ce52013-07-03 15:05:31 -07003479 foreach my $el (@elements) {
3480 push(@fix_elements, substr($rawline, $off, length($el)));
3481 $off += length($el);
3482 }
3483
3484 $off = 0;
3485
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003486 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003487 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003488
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003489 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003490
3491 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3492
3493## print("n: <$n> good: <$good>\n");
3494
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003495 $off += length($elements[$n]);
3496
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003497 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003498 my $ca = substr($opline, 0, $off);
3499 my $cc = '';
3500 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3501 $cc = substr($opline, $off + length($elements[$n + 1]));
3502 }
3503 my $cb = "$ca$;$cc";
3504
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003505 my $a = '';
3506 $a = 'V' if ($elements[$n] ne '');
3507 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003508 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003509 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3510 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003511 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003512
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003513 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003514
3515 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003516 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003517 $c = 'V' if ($elements[$n + 2] ne '');
3518 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003519 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003520 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3521 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003522 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003523 } else {
3524 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003525 }
3526
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003527 my $ctx = "${a}x${c}";
3528
3529 my $at = "(ctx:$ctx)";
3530
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003531 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003532 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003533
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003534 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003535 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003536
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003537 # Get the full operator variant.
3538 my $opv = $op . substr($curr_vars, $off, 1);
3539
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003540 # Ignore operators passed as parameters.
3541 if ($op_type ne 'V' &&
3542 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3543
Andy Whitcroftcf655042008-03-04 14:28:20 -08003544# # Ignore comments
3545# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003546
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003547 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003548 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003549 if ($ctx !~ /.x[WEBC]/ &&
3550 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003551 if (ERROR("SPACING",
3552 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003553 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003554 $line_fixed = 1;
3555 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003556 }
3557
3558 # // is a comment
3559 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003560
Joe Perchesb00e4812014-04-03 14:49:33 -07003561 # : when part of a bitfield
3562 } elsif ($opv eq ':B') {
3563 # skip the bitfield test for now
3564
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003565 # No spaces for:
3566 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003567 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003568 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003569 if (ERROR("SPACING",
3570 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003571 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003572 if (defined $fix_elements[$n + 2]) {
3573 $fix_elements[$n + 2] =~ s/^\s+//;
3574 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003575 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003576 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003577 }
3578
Joe Perches23810972014-12-10 15:51:32 -08003579 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003580 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003581 my $rtrim_before = 0;
3582 my $space_after = 0;
3583 if ($ctx =~ /Wx./) {
3584 if (ERROR("SPACING",
3585 "space prohibited before that '$op' $at\n" . $hereptr)) {
3586 $line_fixed = 1;
3587 $rtrim_before = 1;
3588 }
3589 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003590 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003591 if (ERROR("SPACING",
3592 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003593 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003594 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003595 $space_after = 1;
3596 }
3597 }
3598 if ($rtrim_before || $space_after) {
3599 if ($rtrim_before) {
3600 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3601 } else {
3602 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3603 }
3604 if ($space_after) {
3605 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003606 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003607 }
3608
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003609 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003610 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003611 #warn "'*' is part of type\n";
3612
3613 # unary operators should have a space before and
3614 # none after. May be left adjacent to another
3615 # unary operator, or a cast
3616 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003617 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003618 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003619 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003620 if (ERROR("SPACING",
3621 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003622 if ($n != $last_after + 2) {
3623 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3624 $line_fixed = 1;
3625 }
Joe Perches3705ce52013-07-03 15:05:31 -07003626 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003627 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003628 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003629 # A unary '*' may be const
3630
3631 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003632 if (ERROR("SPACING",
3633 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003634 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003635 if (defined $fix_elements[$n + 2]) {
3636 $fix_elements[$n + 2] =~ s/^\s+//;
3637 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003638 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003639 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003640 }
3641
3642 # unary ++ and unary -- are allowed no space on one side.
3643 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003644 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003645 if (ERROR("SPACING",
3646 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003647 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003648 $line_fixed = 1;
3649 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003650 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003651 if ($ctx =~ /Wx[BE]/ ||
3652 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003653 if (ERROR("SPACING",
3654 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003655 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003656 $line_fixed = 1;
3657 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003658 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003659 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003660 if (ERROR("SPACING",
3661 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003662 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003663 if (defined $fix_elements[$n + 2]) {
3664 $fix_elements[$n + 2] =~ s/^\s+//;
3665 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003666 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003667 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003668 }
3669
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003670 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003671 } elsif ($op eq '<<' or $op eq '>>' or
3672 $op eq '&' or $op eq '^' or $op eq '|' or
3673 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003674 $op eq '*' or $op eq '/' or
3675 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003676 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003677 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003678 if (ERROR("SPACING",
3679 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003680 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3681 if (defined $fix_elements[$n + 2]) {
3682 $fix_elements[$n + 2] =~ s/^\s+//;
3683 }
Joe Perches3705ce52013-07-03 15:05:31 -07003684 $line_fixed = 1;
3685 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003686 }
3687
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003688 # A colon needs no spaces before when it is
3689 # terminating a case value or a label.
3690 } elsif ($opv eq ':C' || $opv eq ':L') {
3691 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003692 if (ERROR("SPACING",
3693 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003694 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003695 $line_fixed = 1;
3696 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003697 }
3698
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003699 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003700 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003701 my $ok = 0;
3702
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003703 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003704 if (($op eq '<' &&
3705 $cc =~ /^\S+\@\S+>/) ||
3706 ($op eq '>' &&
3707 $ca =~ /<\S+\@\S+$/))
3708 {
3709 $ok = 1;
3710 }
3711
Joe Perches84731622013-11-12 15:10:05 -08003712 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003713 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003714 my $msg_type = \&ERROR;
3715 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3716
3717 if (&{$msg_type}("SPACING",
3718 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003719 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3720 if (defined $fix_elements[$n + 2]) {
3721 $fix_elements[$n + 2] =~ s/^\s+//;
3722 }
Joe Perches3705ce52013-07-03 15:05:31 -07003723 $line_fixed = 1;
3724 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003725 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003726 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003727 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003728
3729## print("n: <$n> GOOD: <$good>\n");
3730
3731 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003732 }
Joe Perches3705ce52013-07-03 15:05:31 -07003733
3734 if (($#elements % 2) == 0) {
3735 $fixed_line = $fixed_line . $fix_elements[$#elements];
3736 }
3737
Joe Perches194f66f2014-08-06 16:11:03 -07003738 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3739 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003740 }
3741
3742
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003743 }
3744
Joe Perches786b6322013-07-03 15:05:32 -07003745# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003746 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003747 if (WARN("SPACING",
3748 "space prohibited before semicolon\n" . $herecurr) &&
3749 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003750 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003751 s/^(\+.*\S)\s+;/$1;/;
3752 }
3753 }
3754
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003755# check for multiple assignments
3756 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003757 CHK("MULTIPLE_ASSIGNMENTS",
3758 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003759 }
3760
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003761## # check for multiple declarations, allowing for a function declaration
3762## # continuation.
3763## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3764## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3765##
3766## # Remove any bracketed sections to ensure we do not
3767## # falsly report the parameters of functions.
3768## my $ln = $line;
3769## while ($ln =~ s/\([^\(\)]*\)//g) {
3770## }
3771## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003772## WARN("MULTIPLE_DECLARATION",
3773## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003774## }
3775## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003776
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003777#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003778 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3779 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003780 if (ERROR("SPACING",
3781 "space required before the open brace '{'\n" . $herecurr) &&
3782 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003783 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003784 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003785 }
3786
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003787## # check for blank lines before declarations
3788## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3789## $prevrawline =~ /^.\s*$/) {
3790## WARN("SPACING",
3791## "No blank lines before declarations\n" . $hereprev);
3792## }
3793##
3794
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003795# closing brace should have a space following it when it has anything
3796# on the line
3797 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003798 if (ERROR("SPACING",
3799 "space required after that close brace '}'\n" . $herecurr) &&
3800 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003801 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003802 s/}((?!(?:,|;|\)))\S)/} $1/;
3803 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003804 }
3805
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003806# check spacing on square brackets
3807 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003808 if (ERROR("SPACING",
3809 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3810 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003811 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003812 s/\[\s+/\[/;
3813 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003814 }
3815 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003816 if (ERROR("SPACING",
3817 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3818 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003819 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003820 s/\s+\]/\]/;
3821 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003822 }
3823
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003824# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003825 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3826 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003827 if (ERROR("SPACING",
3828 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003830 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003831 s/\(\s+/\(/;
3832 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003833 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003834 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003835 $line !~ /for\s*\(.*;\s+\)/ &&
3836 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003837 if (ERROR("SPACING",
3838 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3839 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003840 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003841 s/\s+\)/\)/;
3842 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003843 }
3844
Joe Perchese2826fd2014-08-06 16:10:48 -07003845# check unnecessary parentheses around addressof/dereference single $Lvals
3846# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3847
3848 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003849 my $var = $1;
3850 if (CHK("UNNECESSARY_PARENTHESES",
3851 "Unnecessary parentheses around $var\n" . $herecurr) &&
3852 $fix) {
3853 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3854 }
3855 }
3856
3857# check for unnecessary parentheses around function pointer uses
3858# ie: (foo->bar)(); should be foo->bar();
3859# but not "if (foo->bar) (" to avoid some false positives
3860 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3861 my $var = $2;
3862 if (CHK("UNNECESSARY_PARENTHESES",
3863 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3864 $fix) {
3865 my $var2 = deparenthesize($var);
3866 $var2 =~ s/\s//g;
3867 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3868 }
3869 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003870
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003871#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003872 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003873 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003874 if (WARN("INDENTED_LABEL",
3875 "labels should not be indented\n" . $herecurr) &&
3876 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003877 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003878 s/^(.)\s+/$1/;
3879 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003880 }
3881
Joe Perches5b9553a2014-04-03 14:49:21 -07003882# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003883 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003884 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003885 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003886 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3887 my $value = $1;
3888 $value = deparenthesize($value);
3889 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3890 ERROR("RETURN_PARENTHESES",
3891 "return is not a function, parentheses are not required\n" . $herecurr);
3892 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003893 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003894 ERROR("SPACING",
3895 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003896 }
3897 }
Joe Perches507e5142013-11-12 15:10:13 -08003898
Joe Perchesb43ae212014-06-23 13:22:07 -07003899# unnecessary return in a void function
3900# at end-of-function, with the previous line a single leading tab, then return;
3901# and the line before that not a goto label target like "out:"
3902 if ($sline =~ /^[ \+]}\s*$/ &&
3903 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3904 $linenr >= 3 &&
3905 $lines[$linenr - 3] =~ /^[ +]/ &&
3906 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003907 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003908 "void function return statements are not generally useful\n" . $hereprev);
3909 }
Joe Perches9819cf22014-06-04 16:12:09 -07003910
Joe Perches189248d2014-01-23 15:54:47 -08003911# if statements using unnecessary parentheses - ie: if ((foo == bar))
3912 if ($^V && $^V ge 5.10.0 &&
3913 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3914 my $openparens = $1;
3915 my $count = $openparens =~ tr@\(@\(@;
3916 my $msg = "";
3917 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3918 my $comp = $4; #Not $1 because of $LvalOrFunc
3919 $msg = " - maybe == should be = ?" if ($comp eq "==");
3920 WARN("UNNECESSARY_PARENTHESES",
3921 "Unnecessary parentheses$msg\n" . $herecurr);
3922 }
3923 }
3924
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003925# Return of what appears to be an errno should normally be -'ve
3926 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3927 my $name = $1;
3928 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003929 WARN("USE_NEGATIVE_ERRNO",
3930 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003931 }
3932 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003933
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003934# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003935 if ($line =~ /\b(if|while|for|switch)\(/) {
3936 if (ERROR("SPACING",
3937 "space required before the open parenthesis '('\n" . $herecurr) &&
3938 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003939 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003940 s/\b(if|while|for|switch)\(/$1 \(/;
3941 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07003942 }
3943
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003944# Check for illegal assignment in if conditional -- and check for trailing
3945# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003946 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003947 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3948 ctx_statement_block($linenr, $realcnt, 0)
3949 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003950 my ($stat_next) = ctx_statement_block($line_nr_next,
3951 $remain_next, $off_next);
3952 $stat_next =~ s/\n./\n /g;
3953 ##print "stat<$stat> stat_next<$stat_next>\n";
3954
3955 if ($stat_next =~ /^\s*while\b/) {
3956 # If the statement carries leading newlines,
3957 # then count those as offsets.
3958 my ($whitespace) =
3959 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3960 my $offset =
3961 statement_rawlines($whitespace) - 1;
3962
3963 $suppress_whiletrailers{$line_nr_next +
3964 $offset} = 1;
3965 }
3966 }
3967 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08003968 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003969 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003970 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003971
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08003972 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003973 ERROR("ASSIGN_IN_IF",
3974 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003975 }
3976
3977 # Find out what is on the end of the line after the
3978 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003979 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003980 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003981 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07003982 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3983 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07003984 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003985 # Find out how long the conditional actually is.
3986 my @newlines = ($c =~ /\n/gs);
3987 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003988 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003989
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08003990 $stat_real = raw_line($linenr, $cond_lines)
3991 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07003992 if (defined($stat_real) && $cond_lines > 1) {
3993 $stat_real = "[...]\n$stat_real";
3994 }
3995
Joe Perches000d1cc12011-07-25 17:13:25 -07003996 ERROR("TRAILING_STATEMENTS",
3997 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003998 }
3999 }
4000
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004001# Check for bitwise tests written as boolean
4002 if ($line =~ /
4003 (?:
4004 (?:\[|\(|\&\&|\|\|)
4005 \s*0[xX][0-9]+\s*
4006 (?:\&\&|\|\|)
4007 |
4008 (?:\&\&|\|\|)
4009 \s*0[xX][0-9]+\s*
4010 (?:\&\&|\|\||\)|\])
4011 )/x)
4012 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004013 WARN("HEXADECIMAL_BOOLEAN_TEST",
4014 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004015 }
4016
Andy Whitcroft8905a672007-11-28 16:21:06 -08004017# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004018 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4019 my $s = $1;
4020 $s =~ s/$;//g; # Remove any comments
4021 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004022 ERROR("TRAILING_STATEMENTS",
4023 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004024 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004025 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004026# if should not continue a brace
4027 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004028 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004029 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004030 $herecurr);
4031 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004032# case and default should not have general statements after them
4033 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4034 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004035 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004036 \s*return\s+
4037 )/xg)
4038 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004039 ERROR("TRAILING_STATEMENTS",
4040 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004041 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004042
4043 # Check for }<nl>else {, these must be at the same
4044 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004045 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4046 $previndent == $indent) {
4047 if (ERROR("ELSE_AFTER_BRACE",
4048 "else should follow close brace '}'\n" . $hereprev) &&
4049 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4050 fix_delete_line($fixlinenr - 1, $prevrawline);
4051 fix_delete_line($fixlinenr, $rawline);
4052 my $fixedline = $prevrawline;
4053 $fixedline =~ s/}\s*$//;
4054 if ($fixedline !~ /^\+\s*$/) {
4055 fix_insert_line($fixlinenr, $fixedline);
4056 }
4057 $fixedline = $rawline;
4058 $fixedline =~ s/^(.\s*)else/$1} else/;
4059 fix_insert_line($fixlinenr, $fixedline);
4060 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004061 }
4062
Joe Perches8b8856f2014-08-06 16:11:14 -07004063 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4064 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004065 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4066
4067 # Find out what is on the end of the line after the
4068 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004069 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004070 $s =~ s/\n.*//g;
4071
4072 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004073 if (ERROR("WHILE_AFTER_BRACE",
4074 "while should follow close brace '}'\n" . $hereprev) &&
4075 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4076 fix_delete_line($fixlinenr - 1, $prevrawline);
4077 fix_delete_line($fixlinenr, $rawline);
4078 my $fixedline = $prevrawline;
4079 my $trailing = $rawline;
4080 $trailing =~ s/^\+//;
4081 $trailing = trim($trailing);
4082 $fixedline =~ s/}\s*$/} $trailing/;
4083 fix_insert_line($fixlinenr, $fixedline);
4084 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004085 }
4086 }
4087
Joe Perches95e2c602013-07-03 15:05:20 -07004088#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004089 while ($line =~ m{($Constant|$Lval)}g) {
4090 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004091
4092#gcc binary extension
4093 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004094 if (WARN("GCC_BINARY_CONSTANT",
4095 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4096 $fix) {
4097 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004098 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004099 s/\b$var\b/$hexval/;
4100 }
Joe Perches95e2c602013-07-03 15:05:20 -07004101 }
4102
4103#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004104 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004105 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004106#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004107 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004108#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004109 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4110#Ignore some three character SI units explicitly, like MiB and KHz
4111 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004112 while ($var =~ m{($Ident)}g) {
4113 my $word = $1;
4114 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004115 if ($check) {
4116 seed_camelcase_includes();
4117 if (!$file && !$camelcase_file_seeded) {
4118 seed_camelcase_file($realfile);
4119 $camelcase_file_seeded = 1;
4120 }
4121 }
Joe Perches7e781f62013-09-11 14:23:55 -07004122 if (!defined $camelcase{$word}) {
4123 $camelcase{$word} = 1;
4124 CHK("CAMELCASE",
4125 "Avoid CamelCase: <$word>\n" . $herecurr);
4126 }
Joe Perches34456862013-07-03 15:05:34 -07004127 }
Joe Perches323c1262012-12-17 16:02:07 -08004128 }
4129 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004130
4131#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004132 if ($line =~ /\#\s*define.*\\\s+$/) {
4133 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4134 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4135 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004136 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004137 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004138 }
4139
Andy Whitcroft653d4872007-06-23 17:16:34 -07004140#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004141 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004142 my $file = "$1.h";
4143 my $checkfile = "include/linux/$file";
4144 if (-f "$root/$checkfile" &&
4145 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004146 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004147 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004148 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004149 CHK("ARCH_INCLUDE_LINUX",
4150 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004151 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004152 WARN("INCLUDE_LINUX",
4153 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004154 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004155 }
4156 }
4157
Andy Whitcroft653d4872007-06-23 17:16:34 -07004158# multi-statement macros should be enclosed in a do while loop, grab the
4159# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004160# in a known good container
Andy Whitcroftb8f96a312008-07-23 21:29:07 -07004161 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4162 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004163 my $ln = $linenr;
4164 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004165 my ($off, $dstat, $dcond, $rest);
4166 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004167 my $has_flow_statement = 0;
4168 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004169 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004170 ctx_statement_block($linenr, $realcnt, 0);
4171 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004172 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004173 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004174
Joe Perches08a28432014-10-13 15:51:55 -07004175 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4176 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4177
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004178 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004179 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004180 $dstat =~ s/\\\n.//g;
4181 $dstat =~ s/^\s*//s;
4182 $dstat =~ s/\s*$//s;
4183
4184 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004185 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4186 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004187 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004188 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004189 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004190
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004191 # Flatten any obvious string concatentation.
4192 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4193 $dstat =~ s/$Ident\s*("X*")/$1/)
4194 {
4195 }
4196
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004197 my $exceptions = qr{
4198 $Declare|
4199 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004200 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004201 DECLARE_PER_CPU|
4202 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004203 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004204 union|
4205 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004206 \.$Ident\s*=\s*|
4207 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004208 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004209 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004210 if ($dstat ne '' &&
4211 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4212 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004213 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004214 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004215 $dstat !~ /$exceptions/ &&
4216 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004217 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004218 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004219 $dstat !~ /^for\s*$Constant$/ && # for (...)
4220 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4221 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004222 $dstat !~ /^\({/ && # ({...
4223 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004224 {
4225 $ctx =~ s/\n*$//;
4226 my $herectx = $here . "\n";
4227 my $cnt = statement_rawlines($ctx);
4228
4229 for (my $n = 0; $n < $cnt; $n++) {
4230 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004231 }
4232
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004233 if ($dstat =~ /;/) {
4234 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4235 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4236 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004237 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004238 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004239 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004240 }
Joe Perches5023d342012-12-17 16:01:47 -08004241
Joe Perches08a28432014-10-13 15:51:55 -07004242# check for macros with flow control, but without ## concatenation
4243# ## concatenation is commonly a macro that defines a function so ignore those
4244 if ($has_flow_statement && !$has_arg_concat) {
4245 my $herectx = $here . "\n";
4246 my $cnt = statement_rawlines($ctx);
4247
4248 for (my $n = 0; $n < $cnt; $n++) {
4249 $herectx .= raw_line($linenr, $n) . "\n";
4250 }
4251 WARN("MACRO_WITH_FLOW_CONTROL",
4252 "Macros with flow control statements should be avoided\n" . "$herectx");
4253 }
4254
Joe Perches481eb482012-12-17 16:01:56 -08004255# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004256
4257 } else {
4258 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004259 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4260 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004261 $line =~ /^\+.*\\$/) {
4262 WARN("LINE_CONTINUATIONS",
4263 "Avoid unnecessary line continuations\n" . $herecurr);
4264 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004265 }
4266
Joe Perchesb13edf72012-07-30 14:41:24 -07004267# do {} while (0) macro tests:
4268# single-statement macros do not need to be enclosed in do while (0) loop,
4269# macro should not end with a semicolon
4270 if ($^V && $^V ge 5.10.0 &&
4271 $realfile !~ m@/vmlinux.lds.h$@ &&
4272 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4273 my $ln = $linenr;
4274 my $cnt = $realcnt;
4275 my ($off, $dstat, $dcond, $rest);
4276 my $ctx = '';
4277 ($dstat, $dcond, $ln, $cnt, $off) =
4278 ctx_statement_block($linenr, $realcnt, 0);
4279 $ctx = $dstat;
4280
4281 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004282 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004283
4284 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4285 my $stmts = $2;
4286 my $semis = $3;
4287
4288 $ctx =~ s/\n*$//;
4289 my $cnt = statement_rawlines($ctx);
4290 my $herectx = $here . "\n";
4291
4292 for (my $n = 0; $n < $cnt; $n++) {
4293 $herectx .= raw_line($linenr, $n) . "\n";
4294 }
4295
Joe Perchesac8e97f2012-08-21 16:15:53 -07004296 if (($stmts =~ tr/;/;/) == 1 &&
4297 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004298 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4299 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4300 }
4301 if (defined $semis && $semis ne "") {
4302 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4303 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4304 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004305 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4306 $ctx =~ s/\n*$//;
4307 my $cnt = statement_rawlines($ctx);
4308 my $herectx = $here . "\n";
4309
4310 for (my $n = 0; $n < $cnt; $n++) {
4311 $herectx .= raw_line($linenr, $n) . "\n";
4312 }
4313
4314 WARN("TRAILING_SEMICOLON",
4315 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004316 }
4317 }
4318
Mike Frysinger080ba922009-01-06 14:41:25 -08004319# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4320# all assignments may have only one of the following with an assignment:
4321# .
4322# ALIGN(...)
4323# VMLINUX_SYMBOL(...)
4324 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004325 WARN("MISSING_VMLINUX_SYMBOL",
4326 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004327 }
4328
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004329# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004330 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4331 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004332 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004333 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004334 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4335 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004336 my @allowed = ();
4337 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004338 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004339 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004340 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004341 for my $chunk (@chunks) {
4342 my ($cond, $block) = @{$chunk};
4343
Andy Whitcroft773647a2008-03-28 14:15:58 -07004344 # If the condition carries leading newlines, then count those as offsets.
4345 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4346 my $offset = statement_rawlines($whitespace) - 1;
4347
Joe Perchesaad4f612012-03-23 15:02:19 -07004348 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004349 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4350
4351 # We have looked at and allowed this specific line.
4352 $suppress_ifbraces{$ln + $offset} = 1;
4353
4354 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004355 $ln += statement_rawlines($block) - 1;
4356
Andy Whitcroft773647a2008-03-28 14:15:58 -07004357 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004358
4359 $seen++ if ($block =~ /^\s*{/);
4360
Joe Perchesaad4f612012-03-23 15:02:19 -07004361 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004362 if (statement_lines($cond) > 1) {
4363 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004364 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004365 }
4366 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004367 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004368 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004369 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004370 if (statement_block_size($block) > 1) {
4371 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004372 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004373 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004374 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004375 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004376 if ($seen) {
4377 my $sum_allowed = 0;
4378 foreach (@allowed) {
4379 $sum_allowed += $_;
4380 }
4381 if ($sum_allowed == 0) {
4382 WARN("BRACES",
4383 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4384 } elsif ($sum_allowed != $allow &&
4385 $seen != $allow) {
4386 CHK("BRACES",
4387 "braces {} should be used on all arms of this statement\n" . $herectx);
4388 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004389 }
4390 }
4391 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004392 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004393 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004394 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004395
Andy Whitcroftcf655042008-03-04 14:28:20 -08004396 # Check the pre-context.
4397 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4398 #print "APW: ALLOWED: pre<$1>\n";
4399 $allowed = 1;
4400 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004401
4402 my ($level, $endln, @chunks) =
4403 ctx_statement_full($linenr, $realcnt, $-[0]);
4404
Andy Whitcroftcf655042008-03-04 14:28:20 -08004405 # Check the condition.
4406 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004407 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004408 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004409 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004410 }
4411 if (statement_lines($cond) > 1) {
4412 #print "APW: ALLOWED: cond<$cond>\n";
4413 $allowed = 1;
4414 }
4415 if ($block =~/\b(?:if|for|while)\b/) {
4416 #print "APW: ALLOWED: block<$block>\n";
4417 $allowed = 1;
4418 }
4419 if (statement_block_size($block) > 1) {
4420 #print "APW: ALLOWED: lines block<$block>\n";
4421 $allowed = 1;
4422 }
4423 # Check the post-context.
4424 if (defined $chunks[1]) {
4425 my ($cond, $block) = @{$chunks[1]};
4426 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004427 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004428 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004429 if ($block =~ /^\s*\{/) {
4430 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4431 $allowed = 1;
4432 }
4433 }
4434 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004435 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004436 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004437
Andy Whitcroftf0556632008-10-15 22:02:23 -07004438 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004439 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004440 }
4441
Joe Perches000d1cc12011-07-25 17:13:25 -07004442 WARN("BRACES",
4443 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004444 }
4445 }
4446
Joe Perches0979ae62012-12-17 16:01:59 -08004447# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004448 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004449 CHK("BRACES",
4450 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4451 }
Joe Perches77b9a532013-07-03 15:05:29 -07004452 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perches0979ae62012-12-17 16:01:59 -08004453 CHK("BRACES",
4454 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4455 }
4456
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004457# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004458 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4459 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004460 WARN("VOLATILE",
4461 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004462 }
4463
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004464# Check for user-visible strings broken across lines, which breaks the ability
4465# to grep for the string. Make exceptions when the previous string ends in a
4466# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4467# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4468 if ($line =~ /^\+\s*"[X\t]*"/ &&
4469 $prevline =~ /"\s*$/ &&
4470 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4471 if (WARN("SPLIT_STRING",
4472 "quoted string split across lines\n" . $hereprev) &&
4473 $fix &&
4474 $prevrawline =~ /^\+.*"\s*$/ &&
4475 $last_coalesced_string_linenr != $linenr - 1) {
4476 my $extracted_string = get_quoted_string($line, $rawline);
4477 my $comma_close = "";
4478 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4479 $comma_close = $1;
4480 }
4481
4482 fix_delete_line($fixlinenr - 1, $prevrawline);
4483 fix_delete_line($fixlinenr, $rawline);
4484 my $fixedline = $prevrawline;
4485 $fixedline =~ s/"\s*$//;
4486 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4487 fix_insert_line($fixlinenr - 1, $fixedline);
4488 $fixedline = $rawline;
4489 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4490 if ($fixedline !~ /\+\s*$/) {
4491 fix_insert_line($fixlinenr, $fixedline);
4492 }
4493 $last_coalesced_string_linenr = $linenr;
4494 }
4495 }
4496
4497# check for missing a space in a string concatenation
4498 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4499 WARN('MISSING_SPACE',
4500 "break quoted strings at a space character\n" . $hereprev);
4501 }
4502
4503# check for spaces before a quoted newline
4504 if ($rawline =~ /^.*\".*\s\\n/) {
4505 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4506 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4507 $fix) {
4508 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4509 }
4510
4511 }
4512
Joe Perchesf17dba42014-10-13 15:51:51 -07004513# concatenated string without spaces between elements
4514 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4515 CHK("CONCATENATED_STRING",
4516 "Concatenated strings should use spaces between elements\n" . $herecurr);
4517 }
4518
Joe Perches90ad30e2014-12-10 15:51:59 -08004519# uncoalesced string fragments
4520 if ($line =~ /"X*"\s*"/) {
4521 WARN("STRING_FRAGMENTS",
4522 "Consecutive strings are generally better as a single string\n" . $herecurr);
4523 }
4524
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004525# check for %L{u,d,i} in strings
4526 my $string;
4527 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4528 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4529 $string =~ s/%%/__/g;
4530 if ($string =~ /(?<!%)%L[udi]/) {
4531 WARN("PRINTF_L",
4532 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4533 last;
4534 }
4535 }
4536
4537# check for line continuations in quoted strings with odd counts of "
4538 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4539 WARN("LINE_CONTINUATIONS",
4540 "Avoid line continuations in quoted strings\n" . $herecurr);
4541 }
4542
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004543# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004544 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004545 CHK("REDUNDANT_CODE",
4546 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004547 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004548 }
4549
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004550# check for needless "if (<foo>) fn(<foo>)" uses
4551 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4552 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4553 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4554 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004555 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004556 }
4557 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004558
Joe Perchesebfdc402014-08-06 16:10:27 -07004559# check for unnecessary "Out of Memory" messages
4560 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4561 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4562 (defined $1 || defined $3) &&
4563 $linenr > 3) {
4564 my $testval = $2;
4565 my $testline = $lines[$linenr - 3];
4566
4567 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4568# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4569
4570 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4571 WARN("OOM_MESSAGE",
4572 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4573 }
4574 }
4575
Joe Perchesf78d98f2014-10-13 15:52:01 -07004576# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004577 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004578 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4579 my $level = $1;
4580 if (WARN("UNNECESSARY_KERN_LEVEL",
4581 "Possible unnecessary $level\n" . $herecurr) &&
4582 $fix) {
4583 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4584 }
4585 }
4586
Joe Perchesabb08a52014-12-10 15:51:46 -08004587# check for mask then right shift without a parentheses
4588 if ($^V && $^V ge 5.10.0 &&
4589 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4590 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4591 WARN("MASK_THEN_SHIFT",
4592 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4593 }
4594
Joe Perchesb75ac612014-12-10 15:52:02 -08004595# check for pointer comparisons to NULL
4596 if ($^V && $^V ge 5.10.0) {
4597 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4598 my $val = $1;
4599 my $equal = "!";
4600 $equal = "" if ($4 eq "!=");
4601 if (CHK("COMPARISON_TO_NULL",
4602 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4603 $fix) {
4604 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4605 }
4606 }
4607 }
4608
Joe Perches8716de32013-09-11 14:24:05 -07004609# check for bad placement of section $InitAttribute (e.g.: __initdata)
4610 if ($line =~ /(\b$InitAttribute\b)/) {
4611 my $attr = $1;
4612 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4613 my $ptr = $1;
4614 my $var = $2;
4615 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4616 ERROR("MISPLACED_INIT",
4617 "$attr should be placed after $var\n" . $herecurr)) ||
4618 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4619 WARN("MISPLACED_INIT",
4620 "$attr should be placed after $var\n" . $herecurr))) &&
4621 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004622 $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 -07004623 }
4624 }
4625 }
4626
Joe Perchese970b8842013-11-12 15:10:10 -08004627# check for $InitAttributeData (ie: __initdata) with const
4628 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4629 my $attr = $1;
4630 $attr =~ /($InitAttributePrefix)(.*)/;
4631 my $attr_prefix = $1;
4632 my $attr_type = $2;
4633 if (ERROR("INIT_ATTRIBUTE",
4634 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4635 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004636 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004637 s/$InitAttributeData/${attr_prefix}initconst/;
4638 }
4639 }
4640
4641# check for $InitAttributeConst (ie: __initconst) without const
4642 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4643 my $attr = $1;
4644 if (ERROR("INIT_ATTRIBUTE",
4645 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4646 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004647 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08004648 /(^\+\s*(?:static\s+))/;
4649 $lead = rtrim($1);
4650 $lead = "$lead " if ($lead !~ /^\+$/);
4651 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004652 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08004653 }
4654 }
4655
Joe Perchesfbdb8132014-04-03 14:49:14 -07004656# don't use __constant_<foo> functions outside of include/uapi/
4657 if ($realfile !~ m@^include/uapi/@ &&
4658 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4659 my $constant_func = $1;
4660 my $func = $constant_func;
4661 $func =~ s/^__constant_//;
4662 if (WARN("CONSTANT_CONVERSION",
4663 "$constant_func should be $func\n" . $herecurr) &&
4664 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004665 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004666 }
4667 }
4668
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004669# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004670 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004671 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004672 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004673 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004674 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004675 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4676 }
4677 if ($delay > 2000) {
4678 WARN("LONG_UDELAY",
4679 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004680 }
4681 }
4682
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004683# warn about unexpectedly long msleep's
4684 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4685 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004686 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004687 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004688 }
4689 }
4690
Joe Perches36ec1932013-07-03 15:05:25 -07004691# check for comparisons of jiffies
4692 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4693 WARN("JIFFIES_COMPARISON",
4694 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4695 }
4696
Joe Perches9d7a34a2013-07-03 15:05:26 -07004697# check for comparisons of get_jiffies_64()
4698 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4699 WARN("JIFFIES_COMPARISON",
4700 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4701 }
4702
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004703# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004704# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07004705# print "#ifdef in C files should be avoided\n";
4706# print "$herecurr";
4707# $clean = 0;
4708# }
4709
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004710# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004711 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004712 if (ERROR("SPACING",
4713 "exactly one space required after that #$1\n" . $herecurr) &&
4714 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004715 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004716 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4717 }
4718
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004719 }
4720
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004721# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004722 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4723 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004724 my $which = $1;
4725 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004726 CHK("UNCOMMENTED_DEFINITION",
4727 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004728 }
4729 }
4730# check for memory barriers without a comment.
4731 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4732 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004733 WARN("MEMORY_BARRIER",
4734 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004735 }
4736 }
4737# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004738 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004739 CHK("ARCH_DEFINES",
4740 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07004741 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004742
Tobias Klauserd4977c72010-05-24 14:33:30 -07004743# Check that the storage class is at the beginning of a declaration
4744 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004745 WARN("STORAGE_CLASS",
4746 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004747 }
4748
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004749# check the location of the inline attribute, that it is between
4750# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004751 if ($line =~ /\b$Type\s+$Inline\b/ ||
4752 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004753 ERROR("INLINE_LOCATION",
4754 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004755 }
4756
Andy Whitcroft8905a672007-11-28 16:21:06 -08004757# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004758 if ($realfile !~ m@\binclude/uapi/@ &&
4759 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004760 if (WARN("INLINE",
4761 "plain inline is preferred over $1\n" . $herecurr) &&
4762 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004763 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004764
4765 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004766 }
4767
Joe Perches3d130fd2011-01-12 17:00:00 -08004768# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004769 if ($realfile !~ m@\binclude/uapi/@ &&
4770 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004771 WARN("PREFER_PACKED",
4772 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004773 }
4774
Joe Perches39b7e282011-07-25 17:13:24 -07004775# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004776 if ($realfile !~ m@\binclude/uapi/@ &&
4777 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004778 WARN("PREFER_ALIGNED",
4779 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004780 }
4781
Joe Perches5f14d3b2012-01-10 15:09:52 -08004782# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004783 if ($realfile !~ m@\binclude/uapi/@ &&
4784 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004785 if (WARN("PREFER_PRINTF",
4786 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4787 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004788 $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 -07004789
4790 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004791 }
4792
Joe Perches6061d942012-03-23 15:02:16 -07004793# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004794 if ($realfile !~ m@\binclude/uapi/@ &&
4795 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004796 if (WARN("PREFER_SCANF",
4797 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4798 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004799 $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 -07004800 }
Joe Perches6061d942012-03-23 15:02:16 -07004801 }
4802
Joe Perches619a9082014-12-10 15:51:35 -08004803# Check for __attribute__ weak, or __weak declarations (may have link issues)
4804 if ($^V && $^V ge 5.10.0 &&
4805 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4806 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4807 $line =~ /\b__weak\b/)) {
4808 ERROR("WEAK_DECLARATION",
4809 "Using weak declarations can have unintended link defects\n" . $herecurr);
4810 }
4811
Joe Perches8f53a9b2010-03-05 13:43:48 -08004812# check for sizeof(&)
4813 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004814 WARN("SIZEOF_ADDRESS",
4815 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004816 }
4817
Joe Perches66c80b62012-07-30 14:41:22 -07004818# check for sizeof without parenthesis
4819 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004820 if (WARN("SIZEOF_PARENTHESIS",
4821 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4822 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004823 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004824 }
Joe Perches66c80b62012-07-30 14:41:22 -07004825 }
4826
Joe Perches88982fe2012-12-17 16:02:00 -08004827# check for struct spinlock declarations
4828 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4829 WARN("USE_SPINLOCK_T",
4830 "struct spinlock should be spinlock_t\n" . $herecurr);
4831 }
4832
Joe Perchesa6962d72013-04-29 16:18:13 -07004833# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004834 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004835 my $fmt = get_quoted_string($line, $rawline);
Joe Perches06668722013-11-12 15:10:07 -08004836 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004837 if (WARN("PREFER_SEQ_PUTS",
4838 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4839 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004840 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004841 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004842 }
4843 }
4844
Andy Whitcroft554e1652012-01-10 15:09:57 -08004845# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004846 if ($^V && $^V ge 5.10.0 &&
4847 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004848 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004849
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004850 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004851 my $ms_val = $7;
4852 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004853
Andy Whitcroft554e1652012-01-10 15:09:57 -08004854 if ($ms_size =~ /^(0x|)0$/i) {
4855 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004856 "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 -08004857 } elsif ($ms_size =~ /^(0x|)1$/i) {
4858 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004859 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4860 }
4861 }
4862
Joe Perches98a9bba2014-01-23 15:54:52 -08004863# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4864 if ($^V && $^V ge 5.10.0 &&
4865 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4866 if (WARN("PREFER_ETHER_ADDR_COPY",
4867 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4868 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004869 $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 -08004870 }
4871 }
4872
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004873# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004874 if ($^V && $^V ge 5.10.0 &&
4875 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004876 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004877 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004878 my $call = $1;
4879 my $cast1 = deparenthesize($2);
4880 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004881 my $cast2 = deparenthesize($7);
4882 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004883 my $cast;
4884
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004885 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004886 $cast = "$cast1 or $cast2";
4887 } elsif ($cast1 ne "") {
4888 $cast = $cast1;
4889 } else {
4890 $cast = $cast2;
4891 }
4892 WARN("MINMAX",
4893 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004894 }
4895 }
4896
Joe Perches4a273192012-07-30 14:41:20 -07004897# check usleep_range arguments
4898 if ($^V && $^V ge 5.10.0 &&
4899 defined $stat &&
4900 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4901 my $min = $1;
4902 my $max = $7;
4903 if ($min eq $max) {
4904 WARN("USLEEP_RANGE",
4905 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4906 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4907 $min > $max) {
4908 WARN("USLEEP_RANGE",
4909 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4910 }
4911 }
4912
Joe Perches823b7942013-11-12 15:10:15 -08004913# check for naked sscanf
4914 if ($^V && $^V ge 5.10.0 &&
4915 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004916 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004917 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4918 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4919 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4920 my $lc = $stat =~ tr@\n@@;
4921 $lc = $lc + $linenr;
4922 my $stat_real = raw_line($linenr, 0);
4923 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4924 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4925 }
4926 WARN("NAKED_SSCANF",
4927 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4928 }
4929
Joe Perchesafc819a2014-06-04 16:12:08 -07004930# check for simple sscanf that should be kstrto<foo>
4931 if ($^V && $^V ge 5.10.0 &&
4932 defined $stat &&
4933 $line =~ /\bsscanf\b/) {
4934 my $lc = $stat =~ tr@\n@@;
4935 $lc = $lc + $linenr;
4936 my $stat_real = raw_line($linenr, 0);
4937 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4938 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4939 }
4940 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4941 my $format = $6;
4942 my $count = $format =~ tr@%@%@;
4943 if ($count == 1 &&
4944 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4945 WARN("SSCANF_TO_KSTRTO",
4946 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4947 }
4948 }
4949 }
4950
Joe Perches70dc8a42013-09-11 14:23:58 -07004951# check for new externs in .h files.
4952 if ($realfile =~ /\.h$/ &&
4953 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07004954 if (CHK("AVOID_EXTERNS",
4955 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07004956 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004957 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07004958 }
4959 }
4960
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004961# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004962 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004963 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004964 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004965 my $function_name = $1;
4966 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004967
4968 my $s = $stat;
4969 if (defined $cond) {
4970 substr($s, 0, length($cond), '');
4971 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004972 if ($s =~ /^\s*;/ &&
4973 $function_name ne 'uninitialized_var')
4974 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004975 WARN("AVOID_EXTERNS",
4976 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004977 }
4978
4979 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004980 WARN("FUNCTION_ARGUMENTS",
4981 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004982 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07004983
4984 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4985 $stat =~ /^.\s*extern\s+/)
4986 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004987 WARN("AVOID_EXTERNS",
4988 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004989 }
4990
4991# checks for new __setup's
4992 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4993 my $name = $1;
4994
4995 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004996 CHK("UNDOCUMENTED_SETUP",
4997 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004998 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004999 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005000
5001# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005002 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005003 WARN("UNNECESSARY_CASTS",
5004 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005005 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005006
Joe Perchesa640d252013-07-03 15:05:21 -07005007# alloc style
5008# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5009 if ($^V && $^V ge 5.10.0 &&
5010 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5011 CHK("ALLOC_SIZEOF_STRUCT",
5012 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5013 }
5014
Joe Perches60a55362014-06-04 16:12:07 -07005015# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5016 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005017 $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 -07005018 my $oldfunc = $3;
5019 my $a1 = $4;
5020 my $a2 = $10;
5021 my $newfunc = "kmalloc_array";
5022 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005023 my $r1 = $a1;
5024 my $r2 = $a2;
5025 if ($a1 =~ /^sizeof\s*\S/) {
5026 $r1 = $a2;
5027 $r2 = $a1;
5028 }
5029 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5030 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005031 if (WARN("ALLOC_WITH_MULTIPLY",
5032 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5033 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005034 $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 -07005035
5036 }
5037 }
5038 }
5039
Joe Perches972fdea2013-04-29 16:18:12 -07005040# check for krealloc arg reuse
5041 if ($^V && $^V ge 5.10.0 &&
5042 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5043 WARN("KREALLOC_ARG_REUSE",
5044 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5045 }
5046
Joe Perches5ce59ae2013-02-21 16:44:18 -08005047# check for alloc argument mismatch
5048 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5049 WARN("ALLOC_ARRAY_ARGS",
5050 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5051 }
5052
Joe Perchescaf2a542011-01-12 16:59:56 -08005053# check for multiple semicolons
5054 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005055 if (WARN("ONE_SEMICOLON",
5056 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5057 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005058 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005059 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005060 }
5061
Joe Perches0ab90192014-12-10 15:51:57 -08005062# check for #defines like: 1 << <digit> that could be BIT(digit)
5063 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5064 my $ull = "";
5065 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5066 if (CHK("BIT_MACRO",
5067 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5068 $fix) {
5069 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5070 }
5071 }
5072
Joe Perchese81f2392014-08-06 16:11:25 -07005073# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005074 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5075 my $has_break = 0;
5076 my $has_statement = 0;
5077 my $count = 0;
5078 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005079 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005080 $prevline--;
5081 my $rline = $rawlines[$prevline - 1];
5082 my $fline = $lines[$prevline - 1];
5083 last if ($fline =~ /^\@\@/);
5084 next if ($fline =~ /^\-/);
5085 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5086 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5087 next if ($fline =~ /^.[\s$;]*$/);
5088 $has_statement = 1;
5089 $count++;
5090 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5091 }
5092 if (!$has_break && $has_statement) {
5093 WARN("MISSING_BREAK",
5094 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5095 }
5096 }
5097
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005098# check for switch/default statements without a break;
5099 if ($^V && $^V ge 5.10.0 &&
5100 defined $stat &&
5101 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5102 my $ctx = '';
5103 my $herectx = $here . "\n";
5104 my $cnt = statement_rawlines($stat);
5105 for (my $n = 0; $n < $cnt; $n++) {
5106 $herectx .= raw_line($linenr, $n) . "\n";
5107 }
5108 WARN("DEFAULT_NO_BREAK",
5109 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005110 }
5111
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005112# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005113 if ($line =~ /\b__FUNCTION__\b/) {
5114 if (WARN("USE_FUNC",
5115 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5116 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005117 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005118 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005119 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005120
Joe Perches62ec8182015-02-13 14:38:18 -08005121# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5122 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5123 ERROR("DATE_TIME",
5124 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5125 }
5126
Joe Perches2c924882012-03-23 15:02:20 -07005127# check for use of yield()
5128 if ($line =~ /\byield\s*\(\s*\)/) {
5129 WARN("YIELD",
5130 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5131 }
5132
Joe Perches179f8f42013-07-03 15:05:30 -07005133# check for comparisons against true and false
5134 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5135 my $lead = $1;
5136 my $arg = $2;
5137 my $test = $3;
5138 my $otype = $4;
5139 my $trail = $5;
5140 my $op = "!";
5141
5142 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5143
5144 my $type = lc($otype);
5145 if ($type =~ /^(?:true|false)$/) {
5146 if (("$test" eq "==" && "$type" eq "true") ||
5147 ("$test" eq "!=" && "$type" eq "false")) {
5148 $op = "";
5149 }
5150
5151 CHK("BOOL_COMPARISON",
5152 "Using comparison to $otype is error prone\n" . $herecurr);
5153
5154## maybe suggesting a correct construct would better
5155## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5156
5157 }
5158 }
5159
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005160# check for semaphores initialized locked
5161 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005162 WARN("CONSIDER_COMPLETION",
5163 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005164 }
Joe Perches6712d852012-03-23 15:02:20 -07005165
Joe Perches67d0a072011-10-31 17:13:10 -07005166# recommend kstrto* over simple_strto* and strict_strto*
5167 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005168 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005169 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005170 }
Joe Perches6712d852012-03-23 15:02:20 -07005171
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005172# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005173 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005174 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005175 "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 -07005176 }
Joe Perches6712d852012-03-23 15:02:20 -07005177
Emese Revfy79404842010-03-05 13:43:53 -08005178# check for various ops structs, ensure they are const.
5179 my $struct_ops = qr{acpi_dock_ops|
5180 address_space_operations|
5181 backlight_ops|
5182 block_device_operations|
5183 dentry_operations|
5184 dev_pm_ops|
5185 dma_map_ops|
5186 extent_io_ops|
5187 file_lock_operations|
5188 file_operations|
5189 hv_ops|
5190 ide_dma_ops|
5191 intel_dvo_dev_ops|
5192 item_operations|
5193 iwl_ops|
5194 kgdb_arch|
5195 kgdb_io|
5196 kset_uevent_ops|
5197 lock_manager_operations|
5198 microcode_ops|
5199 mtrr_ops|
5200 neigh_ops|
5201 nlmsvc_binding|
5202 pci_raw_ops|
5203 pipe_buf_operations|
5204 platform_hibernation_ops|
5205 platform_suspend_ops|
5206 proto_ops|
5207 rpc_pipe_ops|
5208 seq_operations|
5209 snd_ac97_build_ops|
5210 soc_pcmcia_socket_ops|
5211 stacktrace_ops|
5212 sysfs_ops|
5213 tty_operations|
5214 usb_mon_operations|
5215 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005216 if ($line !~ /\bconst\b/ &&
Emese Revfy79404842010-03-05 13:43:53 -08005217 $line =~ /\bstruct\s+($struct_ops)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005218 WARN("CONST_STRUCT",
5219 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005220 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005221 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005222
5223# use of NR_CPUS is usually wrong
5224# ignore definitions of NR_CPUS and usage to define arrays as likely right
5225 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005226 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5227 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005228 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5229 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5230 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005231 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005232 WARN("NR_CPUS",
5233 "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 -07005234 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005235
Joe Perches52ea8502013-11-12 15:10:09 -08005236# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5237 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5238 ERROR("DEFINE_ARCH_HAS",
5239 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5240 }
5241
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005242# whine mightly about in_atomic
5243 if ($line =~ /\bin_atomic\s*\(/) {
5244 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005245 ERROR("IN_ATOMIC",
5246 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005247 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005248 WARN("IN_ATOMIC",
5249 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005250 }
5251 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005252
5253# check for lockdep_set_novalidate_class
5254 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5255 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5256 if ($realfile !~ m@^kernel/lockdep@ &&
5257 $realfile !~ m@^include/linux/lockdep@ &&
5258 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005259 ERROR("LOCKDEP",
5260 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005261 }
5262 }
Dave Jones88f88312011-01-12 16:59:59 -08005263
5264 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5265 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005266 WARN("EXPORTED_WORLD_WRITABLE",
5267 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005268 }
Joe Perches24358802014-04-03 14:49:13 -07005269
Joe Perches515a2352014-04-03 14:49:24 -07005270# Mode permission misuses where it seems decimal should be octal
5271# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5272 if ($^V && $^V ge 5.10.0 &&
5273 $line =~ /$mode_perms_search/) {
5274 foreach my $entry (@mode_permission_funcs) {
5275 my $func = $entry->[0];
5276 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005277
Joe Perches515a2352014-04-03 14:49:24 -07005278 my $skip_args = "";
5279 if ($arg_pos > 1) {
5280 $arg_pos--;
5281 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5282 }
5283 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5284 if ($line =~ /$test/) {
5285 my $val = $1;
5286 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005287
Joe Perches515a2352014-04-03 14:49:24 -07005288 if ($val !~ /^0$/ &&
5289 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5290 length($val) ne 4)) {
5291 ERROR("NON_OCTAL_PERMISSIONS",
5292 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005293 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5294 ERROR("EXPORTED_WORLD_WRITABLE",
5295 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005296 }
Joe Perches24358802014-04-03 14:49:13 -07005297 }
5298 }
5299 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005300 }
5301
5302 # If we have no input at all, then there is nothing to report on
5303 # so just keep quiet.
5304 if ($#rawlines == -1) {
5305 exit(0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005306 }
5307
Andy Whitcroft8905a672007-11-28 16:21:06 -08005308 # In mailback mode only produce a report in the negative, for
5309 # things that appear to be patches.
5310 if ($mailback && ($clean == 1 || !$is_patch)) {
5311 exit(0);
5312 }
5313
5314 # This is not a patch, and we are are in 'no-patch' mode so
5315 # just keep quiet.
5316 if (!$chk_patch && !$is_patch) {
5317 exit(0);
5318 }
5319
5320 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005321 ERROR("NOT_UNIFIED_DIFF",
5322 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005323 }
5324 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005325 ERROR("MISSING_SIGN_OFF",
5326 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005327 }
5328
Andy Whitcroft8905a672007-11-28 16:21:06 -08005329 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005330 if ($summary && !($clean == 1 && $quiet == 1)) {
5331 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005332 print "total: $cnt_error errors, $cnt_warn warnings, " .
5333 (($check)? "$cnt_chk checks, " : "") .
5334 "$cnt_lines lines checked\n";
5335 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005336 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005337
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005338 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005339
5340 if ($^V lt 5.10.0) {
5341 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5342 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5343 }
5344
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005345 # If there were whitespace errors which cleanpatch can fix
5346 # then suggest that.
5347 if ($rpt_cleaners) {
5348 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5349 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005350 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005351 }
5352 }
5353
Joe Perches91bfe482013-09-11 14:23:59 -07005354 hash_show_words(\%use_type, "Used");
5355 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005356
Joe Perchesd752fcc2014-08-06 16:11:05 -07005357 if ($clean == 0 && $fix &&
5358 ("@rawlines" ne "@fixed" ||
5359 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005360 my $newfile = $filename;
5361 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005362 my $linecount = 0;
5363 my $f;
5364
Joe Perchesd752fcc2014-08-06 16:11:05 -07005365 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5366
Joe Perches3705ce52013-07-03 15:05:31 -07005367 open($f, '>', $newfile)
5368 or die "$P: Can't open $newfile for write\n";
5369 foreach my $fixed_line (@fixed) {
5370 $linecount++;
5371 if ($file) {
5372 if ($linecount > 3) {
5373 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005374 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005375 }
5376 } else {
5377 print $f $fixed_line . "\n";
5378 }
5379 }
5380 close($f);
5381
5382 if (!$quiet) {
5383 print << "EOM";
5384Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5385
5386Do _NOT_ trust the results written to this file.
5387Do _NOT_ submit these changes without inspecting them for correctness.
5388
5389This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5390No warranties, expressed or implied...
5391
5392EOM
5393 }
5394 }
5395
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005396 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005397 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005398 }
5399 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005400 print << "EOM";
5401$vname has style problems, please review.
5402
5403If any of these errors are false positives, please report
5404them to the maintainer, see CHECKPATCH in MAINTAINERS.
5405EOM
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005406 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005407
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07005408 return $clean;
5409}