blob: d12435992dea9d9910a007ec0bfd48e0230a26cd [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070012
13my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080014my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070015
Joe Perches000d1cc12011-07-25 17:13:25 -070016my $V = '0.32';
Andy Whitcroft0a920b52007-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 Whitcroft0a920b52007-06-01 00:46:48 -0700125GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700126 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-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 Whitcroft0a920b52007-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 Whitcroft0a920b52007-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 Whitcroft0a920b52007-06-01 00:46:48 -0700167if ($#ARGV < 0) {
Hannes Eder77f5b102009-09-21 17:04:37 -0700168 print "$P: no input files\n";
Andy Whitcroft0a920b52007-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 Whitcroft0a920b52007-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 Perchese970b882013-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
Joe Perches021158b2015-02-13 14:38:43 -0800342our $typeOtherOSTypedefs = qr{(?x:
343 u_(?:char|short|int|long) | # bsd
344 u(?:nchar|short|int|long) # sysv
345)};
346
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700347our $typeTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700348 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700349 atomic_t
350)};
351
Joe Perches691e6692010-03-05 13:43:51 -0800352our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700353 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700354 (?:[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 -0700355 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700356 panic|
Joe Perches06668722013-11-12 15:10:07 -0800357 MODULE_[A-Z_]+|
358 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800359)};
360
Joe Perches20112472011-07-25 17:13:23 -0700361our $signature_tags = qr{(?xi:
362 Signed-off-by:|
363 Acked-by:|
364 Tested-by:|
365 Reviewed-by:|
366 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700367 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700368 To:|
369 Cc:
370)};
371
Joe Perches18130872014-08-06 16:11:22 -0700372our @typeListMisordered = (
373 qr{char\s+(?:un)?signed},
374 qr{int\s+(?:(?:un)?signed\s+)?short\s},
375 qr{int\s+short(?:\s+(?:un)?signed)},
376 qr{short\s+int(?:\s+(?:un)?signed)},
377 qr{(?:un)?signed\s+int\s+short},
378 qr{short\s+(?:un)?signed},
379 qr{long\s+int\s+(?:un)?signed},
380 qr{int\s+long\s+(?:un)?signed},
381 qr{long\s+(?:un)?signed\s+int},
382 qr{int\s+(?:un)?signed\s+long},
383 qr{int\s+(?:un)?signed},
384 qr{int\s+long\s+long\s+(?:un)?signed},
385 qr{long\s+long\s+int\s+(?:un)?signed},
386 qr{long\s+long\s+(?:un)?signed\s+int},
387 qr{long\s+long\s+(?:un)?signed},
388 qr{long\s+(?:un)?signed},
389);
390
Andy Whitcroft8905a672007-11-28 16:21:06 -0800391our @typeList = (
392 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700393 qr{(?:(?:un)?signed\s+)?char},
394 qr{(?:(?:un)?signed\s+)?short\s+int},
395 qr{(?:(?:un)?signed\s+)?short},
396 qr{(?:(?:un)?signed\s+)?int},
397 qr{(?:(?:un)?signed\s+)?long\s+int},
398 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
399 qr{(?:(?:un)?signed\s+)?long\s+long},
400 qr{(?:(?:un)?signed\s+)?long},
401 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800402 qr{float},
403 qr{double},
404 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405 qr{struct\s+$Ident},
406 qr{union\s+$Ident},
407 qr{enum\s+$Ident},
408 qr{${Ident}_t},
409 qr{${Ident}_handler},
410 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700411 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800412);
Joe Perches8716de32013-09-11 14:24:05 -0700413our @typeListWithAttr = (
414 @typeList,
415 qr{struct\s+$InitAttribute\s+$Ident},
416 qr{union\s+$InitAttribute\s+$Ident},
417);
418
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700419our @modifierList = (
420 qr{fastcall},
421);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422
Joe Perches24358802014-04-03 14:49:13 -0700423our @mode_permission_funcs = (
424 ["module_param", 3],
425 ["module_param_(?:array|named|string)", 4],
426 ["module_param_array_named", 5],
427 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
428 ["proc_create(?:_data|)", 2],
429 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
430);
431
Joe Perches515a2352014-04-03 14:49:24 -0700432#Create a search pattern for all these functions to speed up a loop below
433our $mode_perms_search = "";
434foreach my $entry (@mode_permission_funcs) {
435 $mode_perms_search .= '|' if ($mode_perms_search ne "");
436 $mode_perms_search .= $entry->[0];
437}
438
Wolfram Sang7840a942010-08-09 17:20:57 -0700439our $allowed_asm_includes = qr{(?x:
440 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700441 memory|
442 time|
443 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700444)};
445# memory.h: ARM has a custom one
446
Kees Cook66b47b42014-10-13 15:51:57 -0700447# Load common spelling mistakes and build regular expression list.
448my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700449my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700450
Joe Perches36061e32014-12-10 15:51:43 -0800451if (open(my $spelling, '<', $spelling_file)) {
452 my @spelling_list;
453 while (<$spelling>) {
454 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700455
Joe Perches36061e32014-12-10 15:51:43 -0800456 $line =~ s/\s*\n?$//g;
457 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700458
Joe Perches36061e32014-12-10 15:51:43 -0800459 next if ($line =~ m/^\s*#/);
460 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700461
Joe Perches36061e32014-12-10 15:51:43 -0800462 my ($suspect, $fix) = split(/\|\|/, $line);
463
464 push(@spelling_list, $suspect);
465 $spelling_fix{$suspect} = $fix;
466 }
467 close($spelling);
468 $misspellings = join("|", @spelling_list);
469} else {
470 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700471}
Kees Cook66b47b42014-10-13 15:51:57 -0700472
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473sub build_types {
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700474 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
475 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700476 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700477 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700478 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800479 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700480 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800481 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800482 (?:typeof|__typeof__)\s*\([^\)]*\)|
Joe Perches021158b2015-02-13 14:38:43 -0800483 (?:$typeOtherOSTypedefs\b)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700484 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700485 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800486 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700487 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488 }x;
Joe Perches18130872014-08-06 16:11:22 -0700489 $NonptrTypeMisordered = qr{
490 (?:$Modifier\s+|const\s+)*
491 (?:
492 (?:${Misordered}\b)
493 )
494 (?:\s+$Modifier|\s+const)*
495 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700496 $NonptrTypeWithAttr = qr{
497 (?:$Modifier\s+|const\s+)*
498 (?:
499 (?:typeof|__typeof__)\s*\([^\)]*\)|
500 (?:$typeTypedefs\b)|
Joe Perches021158b2015-02-13 14:38:43 -0800501 (?:$typeOtherOSTypedefs\b)|
Joe Perches8716de32013-09-11 14:24:05 -0700502 (?:${allWithAttr}\b)
503 )
504 (?:\s+$Modifier|\s+const)*
505 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800506 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700507 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700508 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700509 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800510 }x;
Joe Perches18130872014-08-06 16:11:22 -0700511 $TypeMisordered = qr{
512 $NonptrTypeMisordered
513 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
514 (?:\s+$Inline|\s+$Modifier)*
515 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700516 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700517 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800518}
519build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700520
Joe Perches7d2367a2011-07-25 17:13:22 -0700521our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700522
523# Using $balanced_parens, $LvalOrFunc, or $FuncArg
524# requires at least perl version v5.10.0
525# Any use must be runtime checked with $^V
526
527our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700528our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800529our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700530
Joe Perchesf8422302014-08-06 16:11:31 -0700531our $declaration_macros = qr{(?x:
532 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
533 (?:$Storage\s+)?LIST_HEAD\s*\(|
534 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
535)};
536
Joe Perches7d2367a2011-07-25 17:13:22 -0700537sub deparenthesize {
538 my ($string) = @_;
539 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700540
541 while ($string =~ /^\s*\(.*\)\s*$/) {
542 $string =~ s@^\s*\(\s*@@;
543 $string =~ s@\s*\)\s*$@@;
544 }
545
Joe Perches7d2367a2011-07-25 17:13:22 -0700546 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700547
Joe Perches7d2367a2011-07-25 17:13:22 -0700548 return $string;
549}
550
Joe Perches34456862013-07-03 15:05:34 -0700551sub seed_camelcase_file {
552 my ($file) = @_;
553
554 return if (!(-f $file));
555
556 local $/;
557
558 open(my $include_file, '<', "$file")
559 or warn "$P: Can't read '$file' $!\n";
560 my $text = <$include_file>;
561 close($include_file);
562
563 my @lines = split('\n', $text);
564
565 foreach my $line (@lines) {
566 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
567 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
568 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800569 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
570 $camelcase{$1} = 1;
571 } 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 -0700572 $camelcase{$1} = 1;
573 }
574 }
575}
576
577my $camelcase_seeded = 0;
578sub seed_camelcase_includes {
579 return if ($camelcase_seeded);
580
581 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700582 my $camelcase_cache = "";
583 my @include_files = ();
584
585 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700586
Richard Genoud3645e322014-02-10 14:25:32 -0800587 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700588 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
589 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700590 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700591 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700592 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700593 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700594 @include_files = split('\n', $files);
595 foreach my $file (@include_files) {
596 my $date = POSIX::strftime("%Y%m%d%H%M",
597 localtime((stat $file)[9]));
598 $last_mod_date = $date if ($last_mod_date < $date);
599 }
600 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700601 }
Joe Perchesc707a812013-07-08 16:00:43 -0700602
603 if ($camelcase_cache ne "" && -f $camelcase_cache) {
604 open(my $camelcase_file, '<', "$camelcase_cache")
605 or warn "$P: Can't read '$camelcase_cache' $!\n";
606 while (<$camelcase_file>) {
607 chomp;
608 $camelcase{$_} = 1;
609 }
610 close($camelcase_file);
611
612 return;
613 }
614
Richard Genoud3645e322014-02-10 14:25:32 -0800615 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700616 $files = `git ls-files "include/*.h"`;
617 @include_files = split('\n', $files);
618 }
619
Joe Perches34456862013-07-03 15:05:34 -0700620 foreach my $file (@include_files) {
621 seed_camelcase_file($file);
622 }
Joe Perches351b2a12013-07-03 15:05:36 -0700623
Joe Perchesc707a812013-07-08 16:00:43 -0700624 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700625 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700626 open(my $camelcase_file, '>', "$camelcase_cache")
627 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700628 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
629 print $camelcase_file ("$_\n");
630 }
631 close($camelcase_file);
632 }
Joe Perches34456862013-07-03 15:05:34 -0700633}
634
Joe Perchesd311cd42014-08-06 16:10:57 -0700635sub git_commit_info {
636 my ($commit, $id, $desc) = @_;
637
638 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
639
640 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
641 $output =~ s/^\s*//gm;
642 my @lines = split("\n", $output);
643
Joe Perches0d7835f2015-02-13 14:38:35 -0800644 return ($id, $desc) if ($#lines < 0);
645
Joe Perchesd311cd42014-08-06 16:10:57 -0700646 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
647# Maybe one day convert this block of bash into something that returns
648# all matching commit ids, but it's very slow...
649#
650# echo "checking commits $1..."
651# git rev-list --remotes | grep -i "^$1" |
652# while read line ; do
653# git log --format='%H %s' -1 $line |
654# echo "commit $(cut -c 1-12,41-)"
655# done
656 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
657 } else {
658 $id = substr($lines[0], 0, 12);
659 $desc = substr($lines[0], 41);
660 }
661
662 return ($id, $desc);
663}
664
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700665$chk_signoff = 0 if ($file);
666
Andy Whitcroft00df3442007-06-08 13:47:06 -0700667my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800668my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700669my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700670my @fixed_inserted = ();
671my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700672my $fixlinenr = -1;
673
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800674my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700675for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800676 my $FILE;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700677 if ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800678 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700679 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800680 } elsif ($filename eq '-') {
681 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700682 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800683 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700684 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700685 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800686 if ($filename eq '-') {
687 $vname = 'Your patch';
688 } else {
689 $vname = $filename;
690 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800691 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700692 chomp;
693 push(@rawlines, $_);
694 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800695 close($FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800696 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700697 $exit = 1;
698 }
699 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800700 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700701 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700702 @fixed_inserted = ();
703 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700704 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700705}
706
707exit($exit);
708
709sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700710 my ($root) = @_;
711
712 my @tree_check = (
713 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
714 "README", "Documentation", "arch", "include", "drivers",
715 "fs", "init", "ipc", "kernel", "lib", "scripts",
716 );
717
718 foreach my $check (@tree_check) {
719 if (! -e $root . '/' . $check) {
720 return 0;
721 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700722 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700724}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700725
Joe Perches20112472011-07-25 17:13:23 -0700726sub parse_email {
727 my ($formatted_email) = @_;
728
729 my $name = "";
730 my $address = "";
731 my $comment = "";
732
733 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
734 $name = $1;
735 $address = $2;
736 $comment = $3 if defined $3;
737 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
738 $address = $1;
739 $comment = $2 if defined $2;
740 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
741 $address = $1;
742 $comment = $2 if defined $2;
743 $formatted_email =~ s/$address.*$//;
744 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700745 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700746 $name =~ s/^\"|\"$//g;
747 # If there's a name left after stripping spaces and
748 # leading quotes, and the address doesn't have both
749 # leading and trailing angle brackets, the address
750 # is invalid. ie:
751 # "joe smith joe@smith.com" bad
752 # "joe smith <joe@smith.com" bad
753 if ($name ne "" && $address !~ /^<[^>]+>$/) {
754 $name = "";
755 $address = "";
756 $comment = "";
757 }
758 }
759
Joe Perches3705ce52013-07-03 15:05:31 -0700760 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700761 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700762 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700763 $address =~ s/^\<|\>$//g;
764
765 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
766 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
767 $name = "\"$name\"";
768 }
769
770 return ($name, $address, $comment);
771}
772
773sub format_email {
774 my ($name, $address) = @_;
775
776 my $formatted_email;
777
Joe Perches3705ce52013-07-03 15:05:31 -0700778 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700779 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700780 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700781
782 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
783 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
784 $name = "\"$name\"";
785 }
786
787 if ("$name" eq "") {
788 $formatted_email = "$address";
789 } else {
790 $formatted_email = "$name <$address>";
791 }
792
793 return $formatted_email;
794}
795
Joe Perchesd311cd42014-08-06 16:10:57 -0700796sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700797 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700798
Joe Perchesbd474ca2014-08-06 16:11:10 -0700799 foreach my $path (split(/:/, $ENV{PATH})) {
800 if (-e "$path/$bin") {
801 return "$path/$bin";
802 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700803 }
Joe Perchesd311cd42014-08-06 16:10:57 -0700804
Joe Perchesbd474ca2014-08-06 16:11:10 -0700805 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -0700806}
807
Joe Perches000d1cc12011-07-25 17:13:25 -0700808sub which_conf {
809 my ($conf) = @_;
810
811 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
812 if (-e "$path/$conf") {
813 return "$path/$conf";
814 }
815 }
816
817 return "";
818}
819
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700820sub expand_tabs {
821 my ($str) = @_;
822
823 my $res = '';
824 my $n = 0;
825 for my $c (split(//, $str)) {
826 if ($c eq "\t") {
827 $res .= ' ';
828 $n++;
829 for (; ($n % 8) != 0; $n++) {
830 $res .= ' ';
831 }
832 next;
833 }
834 $res .= $c;
835 $n++;
836 }
837
838 return $res;
839}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700840sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700841 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700842 return $res;
843}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700844
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700845sub line_stats {
846 my ($line) = @_;
847
848 # Drop the diff line leader and expand tabs
849 $line =~ s/^.//;
850 $line = expand_tabs($line);
851
852 # Pick the indent from the front of the line.
853 my ($white) = ($line =~ /^(\s*)/);
854
855 return (length($line), length($white));
856}
857
Andy Whitcroft773647a2008-03-28 14:15:58 -0700858my $sanitise_quote = '';
859
860sub sanitise_line_reset {
861 my ($in_comment) = @_;
862
863 if ($in_comment) {
864 $sanitise_quote = '*/';
865 } else {
866 $sanitise_quote = '';
867 }
868}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700869sub sanitise_line {
870 my ($line) = @_;
871
872 my $res = '';
873 my $l = '';
874
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800875 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700876 my $off = 0;
877 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700878
Andy Whitcroft773647a2008-03-28 14:15:58 -0700879 # Always copy over the diff marker.
880 $res = substr($line, 0, 1);
881
882 for ($off = 1; $off < length($line); $off++) {
883 $c = substr($line, $off, 1);
884
885 # Comments we are wacking completly including the begin
886 # and end, all to $;.
887 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
888 $sanitise_quote = '*/';
889
890 substr($res, $off, 2, "$;$;");
891 $off++;
892 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800893 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -0700894 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700895 $sanitise_quote = '';
896 substr($res, $off, 2, "$;$;");
897 $off++;
898 next;
899 }
Daniel Walker113f04a2009-09-21 17:04:35 -0700900 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
901 $sanitise_quote = '//';
902
903 substr($res, $off, 2, $sanitise_quote);
904 $off++;
905 next;
906 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700907
908 # A \ in a string means ignore the next character.
909 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
910 $c eq "\\") {
911 substr($res, $off, 2, 'XX');
912 $off++;
913 next;
914 }
915 # Regular quotes.
916 if ($c eq "'" || $c eq '"') {
917 if ($sanitise_quote eq '') {
918 $sanitise_quote = $c;
919
920 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700921 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700922 } elsif ($sanitise_quote eq $c) {
923 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700924 }
925 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700926
Andy Whitcroftfae17da2009-01-06 14:41:20 -0800927 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700928 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
929 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -0700930 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
931 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700932 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
933 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700934 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700935 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700936 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800937 }
938
Daniel Walker113f04a2009-09-21 17:04:35 -0700939 if ($sanitise_quote eq '//') {
940 $sanitise_quote = '';
941 }
942
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800943 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700944 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800945 my $clean = 'X' x length($1);
946 $res =~ s@\<.*\>@<$clean>@;
947
948 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700949 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800950 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700951 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800952 }
953
Andy Whitcroft00df3442007-06-08 13:47:06 -0700954 return $res;
955}
956
Joe Perchesa6962d72013-04-29 16:18:13 -0700957sub get_quoted_string {
958 my ($line, $rawline) = @_;
959
Joe Perches5e4f6ba2014-12-10 15:52:05 -0800960 return "" if ($line !~ m/(\"[X\t]+\")/g);
Joe Perchesa6962d72013-04-29 16:18:13 -0700961 return substr($rawline, $-[0], $+[0] - $-[0]);
962}
963
Andy Whitcroft8905a672007-11-28 16:21:06 -0800964sub ctx_statement_block {
965 my ($linenr, $remain, $off) = @_;
966 my $line = $linenr - 1;
967 my $blk = '';
968 my $soff = $off;
969 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700970 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800971
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800972 my $loff = 0;
973
Andy Whitcroft8905a672007-11-28 16:21:06 -0800974 my $type = '';
975 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -0800976 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -0800977 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800978 my $c;
979 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800980
981 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800982 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -0800983 @stack = (['', 0]) if ($#stack == -1);
984
Andy Whitcroft773647a2008-03-28 14:15:58 -0700985 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800986 # If we are about to drop off the end, pull in more
987 # context.
988 if ($off >= $len) {
989 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -0700990 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800991 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800992 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800993 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800995 $len = length($blk);
996 $line++;
997 last;
998 }
999 # Bail if there is no further context.
1000 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001001 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001002 last;
1003 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001004 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1005 $level++;
1006 $type = '#';
1007 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001008 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001009 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001010 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001011 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001012
Andy Whitcroft773647a2008-03-28 14:15:58 -07001013 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001014
1015 # Handle nested #if/#else.
1016 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1017 push(@stack, [ $type, $level ]);
1018 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1019 ($type, $level) = @{$stack[$#stack - 1]};
1020 } elsif ($remainder =~ /^#\s*endif\b/) {
1021 ($type, $level) = @{pop(@stack)};
1022 }
1023
Andy Whitcroft8905a672007-11-28 16:21:06 -08001024 # Statement ends at the ';' or a close '}' at the
1025 # outermost level.
1026 if ($level == 0 && $c eq ';') {
1027 last;
1028 }
1029
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001030 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001031 if ($level == 0 && $coff_set == 0 &&
1032 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1033 $remainder =~ /^(else)(?:\s|{)/ &&
1034 $remainder !~ /^else\s+if\b/) {
1035 $coff = $off + length($1) - 1;
1036 $coff_set = 1;
1037 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1038 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001039 }
1040
Andy Whitcroft8905a672007-11-28 16:21:06 -08001041 if (($type eq '' || $type eq '(') && $c eq '(') {
1042 $level++;
1043 $type = '(';
1044 }
1045 if ($type eq '(' && $c eq ')') {
1046 $level--;
1047 $type = ($level != 0)? '(' : '';
1048
1049 if ($level == 0 && $coff < $soff) {
1050 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001051 $coff_set = 1;
1052 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001053 }
1054 }
1055 if (($type eq '' || $type eq '{') && $c eq '{') {
1056 $level++;
1057 $type = '{';
1058 }
1059 if ($type eq '{' && $c eq '}') {
1060 $level--;
1061 $type = ($level != 0)? '{' : '';
1062
1063 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001064 if (substr($blk, $off + 1, 1) eq ';') {
1065 $off++;
1066 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001067 last;
1068 }
1069 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001070 # Preprocessor commands end at the newline unless escaped.
1071 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1072 $level--;
1073 $type = '';
1074 $off++;
1075 last;
1076 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001077 $off++;
1078 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001079 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001080 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001081 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001082 $line++;
1083 $remain--;
1084 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001085
1086 my $statement = substr($blk, $soff, $off - $soff + 1);
1087 my $condition = substr($blk, $soff, $coff - $soff + 1);
1088
1089 #warn "STATEMENT<$statement>\n";
1090 #warn "CONDITION<$condition>\n";
1091
Andy Whitcroft773647a2008-03-28 14:15:58 -07001092 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001093
1094 return ($statement, $condition,
1095 $line, $remain + 1, $off - $loff + 1, $level);
1096}
1097
Andy Whitcroftcf655042008-03-04 14:28:20 -08001098sub statement_lines {
1099 my ($stmt) = @_;
1100
1101 # Strip the diff line prefixes and rip blank lines at start and end.
1102 $stmt =~ s/(^|\n)./$1/g;
1103 $stmt =~ s/^\s*//;
1104 $stmt =~ s/\s*$//;
1105
1106 my @stmt_lines = ($stmt =~ /\n/g);
1107
1108 return $#stmt_lines + 2;
1109}
1110
1111sub statement_rawlines {
1112 my ($stmt) = @_;
1113
1114 my @stmt_lines = ($stmt =~ /\n/g);
1115
1116 return $#stmt_lines + 2;
1117}
1118
1119sub statement_block_size {
1120 my ($stmt) = @_;
1121
1122 $stmt =~ s/(^|\n)./$1/g;
1123 $stmt =~ s/^\s*{//;
1124 $stmt =~ s/}\s*$//;
1125 $stmt =~ s/^\s*//;
1126 $stmt =~ s/\s*$//;
1127
1128 my @stmt_lines = ($stmt =~ /\n/g);
1129 my @stmt_statements = ($stmt =~ /;/g);
1130
1131 my $stmt_lines = $#stmt_lines + 2;
1132 my $stmt_statements = $#stmt_statements + 1;
1133
1134 if ($stmt_lines > $stmt_statements) {
1135 return $stmt_lines;
1136 } else {
1137 return $stmt_statements;
1138 }
1139}
1140
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001141sub ctx_statement_full {
1142 my ($linenr, $remain, $off) = @_;
1143 my ($statement, $condition, $level);
1144
1145 my (@chunks);
1146
Andy Whitcroftcf655042008-03-04 14:28:20 -08001147 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001148 ($statement, $condition, $linenr, $remain, $off, $level) =
1149 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001150 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001151 push(@chunks, [ $condition, $statement ]);
1152 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1153 return ($level, $linenr, @chunks);
1154 }
1155
1156 # Pull in the following conditional/block pairs and see if they
1157 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001158 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001159 ($statement, $condition, $linenr, $remain, $off, $level) =
1160 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001161 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001162 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001163 #print "C: push\n";
1164 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001165 }
1166
1167 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001168}
1169
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001170sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001171 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001172 my $line;
1173 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001174 my $blk = '';
1175 my @o;
1176 my @c;
1177 my @res = ();
1178
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001179 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001180 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001181 for ($line = $start; $remain > 0; $line++) {
1182 next if ($rawlines[$line] =~ /^-/);
1183 $remain--;
1184
1185 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001186
1187 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001188 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001189 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001190 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001191 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001192 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001193 $level = pop(@stack);
1194 }
1195
Andy Whitcroft01464f32010-10-26 14:23:19 -07001196 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001197 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1198 if ($off > 0) {
1199 $off--;
1200 next;
1201 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001202
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001203 if ($c eq $close && $level > 0) {
1204 $level--;
1205 last if ($level == 0);
1206 } elsif ($c eq $open) {
1207 $level++;
1208 }
1209 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001210
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001211 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001212 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001213 }
1214
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001215 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001216 }
1217
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001218 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001219}
1220sub ctx_block_outer {
1221 my ($linenr, $remain) = @_;
1222
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001223 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1224 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001225}
1226sub ctx_block {
1227 my ($linenr, $remain) = @_;
1228
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001229 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1230 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001231}
1232sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001233 my ($linenr, $remain, $off) = @_;
1234
1235 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1236 return @r;
1237}
1238sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001239 my ($linenr, $remain) = @_;
1240
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001241 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001242}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001243sub ctx_statement_level {
1244 my ($linenr, $remain, $off) = @_;
1245
1246 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1247}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001248
1249sub ctx_locate_comment {
1250 my ($first_line, $end_line) = @_;
1251
1252 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001253 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001254 return $current_comment if (defined $current_comment);
1255
1256 # Look through the context and try and figure out if there is a
1257 # comment.
1258 my $in_comment = 0;
1259 $current_comment = '';
1260 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001261 my $line = $rawlines[$linenr - 1];
1262 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001263 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1264 $in_comment = 1;
1265 }
1266 if ($line =~ m@/\*@) {
1267 $in_comment = 1;
1268 }
1269 if (!$in_comment && $current_comment ne '') {
1270 $current_comment = '';
1271 }
1272 $current_comment .= $line . "\n" if ($in_comment);
1273 if ($line =~ m@\*/@) {
1274 $in_comment = 0;
1275 }
1276 }
1277
1278 chomp($current_comment);
1279 return($current_comment);
1280}
1281sub ctx_has_comment {
1282 my ($first_line, $end_line) = @_;
1283 my $cmt = ctx_locate_comment($first_line, $end_line);
1284
Andy Whitcroft00df3442007-06-08 13:47:06 -07001285 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001286 ##print "CMMT: $cmt\n";
1287
1288 return ($cmt ne '');
1289}
1290
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001291sub raw_line {
1292 my ($linenr, $cnt) = @_;
1293
1294 my $offset = $linenr - 1;
1295 $cnt++;
1296
1297 my $line;
1298 while ($cnt) {
1299 $line = $rawlines[$offset++];
1300 next if (defined($line) && $line =~ /^-/);
1301 $cnt--;
1302 }
1303
1304 return $line;
1305}
1306
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001307sub cat_vet {
1308 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001309 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001310
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001311 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001312 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1313 $res .= $1;
1314 if ($2 ne '') {
1315 $coded = sprintf("^%c", unpack('C', $2) + 64);
1316 $res .= $coded;
1317 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001318 }
1319 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001320
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001321 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001322}
1323
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001324my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001325my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001327my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001328
1329sub annotate_reset {
1330 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001331 $av_pending = '_';
1332 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001333 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001334}
1335
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001336sub annotate_values {
1337 my ($stream, $type) = @_;
1338
1339 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001340 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001341 my $cur = $stream;
1342
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001343 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001344
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001345 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001346 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001347 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001348 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001349 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001350 print "WS($1)\n" if ($dbg_values > 1);
1351 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001352 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001353 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001354 }
1355
Florian Micklerc023e4732011-01-12 16:59:58 -08001356 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001357 print "CAST($1)\n" if ($dbg_values > 1);
1358 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001359 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001360
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001361 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001362 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001363 $type = 'T';
1364
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001365 } elsif ($cur =~ /^($Modifier)\s*/) {
1366 print "MODIFIER($1)\n" if ($dbg_values > 1);
1367 $type = 'T';
1368
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001369 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001370 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001371 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001372 push(@av_paren_type, $type);
1373 if ($2 ne '') {
1374 $av_pending = 'N';
1375 }
1376 $type = 'E';
1377
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001378 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001379 print "UNDEF($1)\n" if ($dbg_values > 1);
1380 $av_preprocessor = 1;
1381 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001382
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001383 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001384 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001385 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001386
1387 push(@av_paren_type, $type);
1388 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001389 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001390
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001391 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001392 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1393 $av_preprocessor = 1;
1394
1395 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1396
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001397 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001398
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001399 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001400 print "PRE_END($1)\n" if ($dbg_values > 1);
1401
1402 $av_preprocessor = 1;
1403
1404 # Assume all arms of the conditional end as this
1405 # one does, and continue as if the #endif was not here.
1406 pop(@av_paren_type);
1407 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001408 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001409
1410 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001411 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001412
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001413 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1414 print "ATTR($1)\n" if ($dbg_values > 1);
1415 $av_pending = $type;
1416 $type = 'N';
1417
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001418 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001419 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001420 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001421 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001422 }
1423 $type = 'N';
1424
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001425 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001426 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001427 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001428 $type = 'N';
1429
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001430 } elsif ($cur =~/^(case)/o) {
1431 print "CASE($1)\n" if ($dbg_values > 1);
1432 $av_pend_colon = 'C';
1433 $type = 'N';
1434
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001435 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001436 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001437 $type = 'N';
1438
1439 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001440 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001441 push(@av_paren_type, $av_pending);
1442 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001443 $type = 'N';
1444
1445 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001446 my $new_type = pop(@av_paren_type);
1447 if ($new_type ne '_') {
1448 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001449 print "PAREN('$1') -> $type\n"
1450 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001451 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001452 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001453 }
1454
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001455 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001456 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001457 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001458 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001459
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001460 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1461 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001462 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001463 } elsif ($type eq 'E') {
1464 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001465 }
1466 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1467 $type = 'V';
1468
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001469 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001470 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001471 $type = 'V';
1472
1473 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001474 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001475 $type = 'N';
1476
Andy Whitcroftcf655042008-03-04 14:28:20 -08001477 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001478 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001479 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001480 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001481
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001482 } elsif ($cur =~/^(,)/) {
1483 print "COMMA($1)\n" if ($dbg_values > 1);
1484 $type = 'C';
1485
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001486 } elsif ($cur =~ /^(\?)/o) {
1487 print "QUESTION($1)\n" if ($dbg_values > 1);
1488 $type = 'N';
1489
1490 } elsif ($cur =~ /^(:)/o) {
1491 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1492
1493 substr($var, length($res), 1, $av_pend_colon);
1494 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1495 $type = 'E';
1496 } else {
1497 $type = 'N';
1498 }
1499 $av_pend_colon = 'O';
1500
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001501 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001502 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001503 $type = 'N';
1504
Andy Whitcroft0d413862008-10-15 22:02:16 -07001505 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001506 my $variant;
1507
1508 print "OPV($1)\n" if ($dbg_values > 1);
1509 if ($type eq 'V') {
1510 $variant = 'B';
1511 } else {
1512 $variant = 'U';
1513 }
1514
1515 substr($var, length($res), 1, $variant);
1516 $type = 'N';
1517
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001518 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001519 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001520 if ($1 ne '++' && $1 ne '--') {
1521 $type = 'N';
1522 }
1523
1524 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001525 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001526 }
1527 if (defined $1) {
1528 $cur = substr($cur, length($1));
1529 $res .= $type x length($1);
1530 }
1531 }
1532
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001533 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001534}
1535
Andy Whitcroft8905a672007-11-28 16:21:06 -08001536sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001537 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001538 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001539 ^(?:
1540 $Modifier|
1541 $Storage|
1542 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001543 DEFINE_\S+
1544 )$|
1545 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001546 goto|
1547 return|
1548 case|
1549 else|
1550 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001551 do|
1552 \#|
1553 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001554 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001555 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001556 )}x;
1557 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1558 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001559 # Check for modifiers.
1560 $possible =~ s/\s*$Storage\s*//g;
1561 $possible =~ s/\s*$Sparse\s*//g;
1562 if ($possible =~ /^\s*$/) {
1563
1564 } elsif ($possible =~ /\s/) {
1565 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001566 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001567 if ($modifier !~ $notPermitted) {
1568 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1569 push(@modifierList, $modifier);
1570 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001571 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001572
1573 } else {
1574 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1575 push(@typeList, $possible);
1576 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001577 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001578 } else {
1579 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001580 }
1581}
1582
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001583my $prefix = '';
1584
Joe Perches000d1cc12011-07-25 17:13:25 -07001585sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001586 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001587
Joe Perchescbec18a2014-04-03 14:49:19 -07001588 return defined $use_type{$type} if (scalar keys %use_type > 0);
1589
1590 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001591}
1592
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001593sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001594 my ($level, $type, $msg) = @_;
1595
1596 if (!show_type($type) ||
1597 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001598 return 0;
1599 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001600 my $line;
1601 if ($show_types) {
Joe Perchescbec18a2014-04-03 14:49:19 -07001602 $line = "$prefix$level:$type: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001603 } else {
Joe Perchescbec18a2014-04-03 14:49:19 -07001604 $line = "$prefix$level: $msg\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07001605 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001606 $line = (split('\n', $line))[0] . "\n" if ($terse);
1607
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001608 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001609
1610 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001611}
Joe Perchescbec18a2014-04-03 14:49:19 -07001612
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001613sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001614 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001615}
Joe Perches000d1cc12011-07-25 17:13:25 -07001616
Joe Perchesd752fcc2014-08-06 16:11:05 -07001617sub fixup_current_range {
1618 my ($lineRef, $offset, $length) = @_;
1619
1620 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1621 my $o = $1;
1622 my $l = $2;
1623 my $no = $o + $offset;
1624 my $nl = $l + $length;
1625 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1626 }
1627}
1628
1629sub fix_inserted_deleted_lines {
1630 my ($linesRef, $insertedRef, $deletedRef) = @_;
1631
1632 my $range_last_linenr = 0;
1633 my $delta_offset = 0;
1634
1635 my $old_linenr = 0;
1636 my $new_linenr = 0;
1637
1638 my $next_insert = 0;
1639 my $next_delete = 0;
1640
1641 my @lines = ();
1642
1643 my $inserted = @{$insertedRef}[$next_insert++];
1644 my $deleted = @{$deletedRef}[$next_delete++];
1645
1646 foreach my $old_line (@{$linesRef}) {
1647 my $save_line = 1;
1648 my $line = $old_line; #don't modify the array
1649 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1650 $delta_offset = 0;
1651 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1652 $range_last_linenr = $new_linenr;
1653 fixup_current_range(\$line, $delta_offset, 0);
1654 }
1655
1656 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1657 $deleted = @{$deletedRef}[$next_delete++];
1658 $save_line = 0;
1659 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1660 }
1661
1662 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1663 push(@lines, ${$inserted}{'LINE'});
1664 $inserted = @{$insertedRef}[$next_insert++];
1665 $new_linenr++;
1666 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1667 }
1668
1669 if ($save_line) {
1670 push(@lines, $line);
1671 $new_linenr++;
1672 }
1673
1674 $old_linenr++;
1675 }
1676
1677 return @lines;
1678}
1679
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001680sub fix_insert_line {
1681 my ($linenr, $line) = @_;
1682
1683 my $inserted = {
1684 LINENR => $linenr,
1685 LINE => $line,
1686 };
1687 push(@fixed_inserted, $inserted);
1688}
1689
1690sub fix_delete_line {
1691 my ($linenr, $line) = @_;
1692
1693 my $deleted = {
1694 LINENR => $linenr,
1695 LINE => $line,
1696 };
1697
1698 push(@fixed_deleted, $deleted);
1699}
1700
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001701sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001702 my ($type, $msg) = @_;
1703
1704 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001705 our $clean = 0;
1706 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001707 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001708 }
Joe Perches3705ce52013-07-03 15:05:31 -07001709 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001710}
1711sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001712 my ($type, $msg) = @_;
1713
1714 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001715 our $clean = 0;
1716 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001717 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001718 }
Joe Perches3705ce52013-07-03 15:05:31 -07001719 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001720}
1721sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001722 my ($type, $msg) = @_;
1723
1724 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001725 our $clean = 0;
1726 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001727 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
Joe Perches3705ce52013-07-03 15:05:31 -07001729 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001730}
1731
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001732sub check_absolute_file {
1733 my ($absolute, $herecurr) = @_;
1734 my $file = $absolute;
1735
1736 ##print "absolute<$absolute>\n";
1737
1738 # See if any suffix of this path is a path within the tree.
1739 while ($file =~ s@^[^/]*/@@) {
1740 if (-f "$root/$file") {
1741 ##print "file<$file>\n";
1742 last;
1743 }
1744 }
1745 if (! -f _) {
1746 return 0;
1747 }
1748
1749 # It is, so see if the prefix is acceptable.
1750 my $prefix = $absolute;
1751 substr($prefix, -length($file)) = '';
1752
1753 ##print "prefix<$prefix>\n";
1754 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001755 WARN("USE_RELATIVE_PATH",
1756 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001757 }
1758}
1759
Joe Perches3705ce52013-07-03 15:05:31 -07001760sub trim {
1761 my ($string) = @_;
1762
Joe Perchesb34c6482013-09-11 14:24:01 -07001763 $string =~ s/^\s+|\s+$//g;
1764
1765 return $string;
1766}
1767
1768sub ltrim {
1769 my ($string) = @_;
1770
1771 $string =~ s/^\s+//;
1772
1773 return $string;
1774}
1775
1776sub rtrim {
1777 my ($string) = @_;
1778
1779 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001780
1781 return $string;
1782}
1783
Joe Perches52ea8502013-11-12 15:10:09 -08001784sub string_find_replace {
1785 my ($string, $find, $replace) = @_;
1786
1787 $string =~ s/$find/$replace/g;
1788
1789 return $string;
1790}
1791
Joe Perches3705ce52013-07-03 15:05:31 -07001792sub tabify {
1793 my ($leading) = @_;
1794
1795 my $source_indent = 8;
1796 my $max_spaces_before_tab = $source_indent - 1;
1797 my $spaces_to_tab = " " x $source_indent;
1798
1799 #convert leading spaces to tabs
1800 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1801 #Remove spaces before a tab
1802 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1803
1804 return "$leading";
1805}
1806
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001807sub pos_last_openparen {
1808 my ($line) = @_;
1809
1810 my $pos = 0;
1811
1812 my $opens = $line =~ tr/\(/\(/;
1813 my $closes = $line =~ tr/\)/\)/;
1814
1815 my $last_openparen = 0;
1816
1817 if (($opens == 0) || ($closes >= $opens)) {
1818 return -1;
1819 }
1820
1821 my $len = length($line);
1822
1823 for ($pos = 0; $pos < $len; $pos++) {
1824 my $string = substr($line, $pos);
1825 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1826 $pos += length($1) - 1;
1827 } elsif (substr($line, $pos, 1) eq '(') {
1828 $last_openparen = $pos;
1829 } elsif (index($string, '(') == -1) {
1830 last;
1831 }
1832 }
1833
Joe Perches91cb5192014-04-03 14:49:32 -07001834 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07001835}
1836
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001837sub process {
1838 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001839
1840 my $linenr=0;
1841 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001842 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001843 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001844 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001845
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001846 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001847 my $indent;
1848 my $previndent=0;
1849 my $stashindent=0;
1850
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001851 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001852 my $signoff = 0;
1853 my $is_patch = 0;
1854
Joe Perches29ee1b02014-08-06 16:10:35 -07001855 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07001856 my $in_commit_log = 0; #Scanning lines before patch
Joe Perches13f19372014-08-06 16:10:59 -07001857 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07001858 my $non_utf8_charset = 0;
1859
Joe Perches365dd4e2014-08-06 16:10:42 -07001860 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08001861 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07001862
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001863 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001864 our $cnt_lines = 0;
1865 our $cnt_error = 0;
1866 our $cnt_warn = 0;
1867 our $cnt_chk = 0;
1868
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001869 # Trace the real file/line as we go.
1870 my $realfile = '';
1871 my $realline = 0;
1872 my $realcnt = 0;
1873 my $here = '';
1874 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001875 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001876 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08001877 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001878
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001879 my $prev_values = 'E';
1880
1881 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07001882 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07001883 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07001884 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08001885 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001886
Joe Perches7e51f192013-09-11 14:23:57 -07001887 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08001888
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001889 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001890 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001891 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001892 my @setup_docs = ();
1893 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001894
Joe Perchesd8b07712013-11-12 15:10:06 -08001895 my $camelcase_file_seeded = 0;
1896
Andy Whitcroft773647a2008-03-28 14:15:58 -07001897 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001898 my $line;
1899 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001900 $linenr++;
1901 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001902
Joe Perches3705ce52013-07-03 15:05:31 -07001903 push(@fixed, $rawline) if ($fix);
1904
Andy Whitcroft773647a2008-03-28 14:15:58 -07001905 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001906 $setup_docs = 0;
1907 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1908 $setup_docs = 1;
1909 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001910 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001911 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001912 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1913 $realline=$1-1;
1914 if (defined $2) {
1915 $realcnt=$3+1;
1916 } else {
1917 $realcnt=1+1;
1918 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001919 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001920
1921 # Guestimate if this is a continuing comment. Run
1922 # the context looking for a comment "edge". If this
1923 # edge is a close comment then we must be in a comment
1924 # at context start.
1925 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07001926 my $cnt = $realcnt;
1927 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1928 next if (defined $rawlines[$ln - 1] &&
1929 $rawlines[$ln - 1] =~ /^-/);
1930 $cnt--;
1931 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08001932 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001933 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1934 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1935 ($edge) = $1;
1936 last;
1937 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001938 }
1939 if (defined $edge && $edge eq '*/') {
1940 $in_comment = 1;
1941 }
1942
1943 # Guestimate if this is a continuing comment. If this
1944 # is the start of a diff block and this line starts
1945 # ' *' then it is very likely a comment.
1946 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08001947 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07001948 {
1949 $in_comment = 1;
1950 }
1951
1952 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1953 sanitise_line_reset($in_comment);
1954
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001955 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001956 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001957 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001958 $line = sanitise_line($rawline);
1959 }
1960 push(@lines, $line);
1961
1962 if ($realcnt > 1) {
1963 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1964 } else {
1965 $realcnt = 0;
1966 }
1967
1968 #print "==>$rawline\n";
1969 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001970
1971 if ($setup_docs && $line =~ /^\+/) {
1972 push(@setup_docs, $line);
1973 }
1974 }
1975
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001976 $prefix = '';
1977
Andy Whitcroft773647a2008-03-28 14:15:58 -07001978 $realcnt = 0;
1979 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07001980 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001981 foreach my $line (@lines) {
1982 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07001983 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07001984 my $sline = $line; #copy of $line
1985 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001986
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001987 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001988
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001989#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001990 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001991 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001992 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001993 $realline=$1-1;
1994 if (defined $2) {
1995 $realcnt=$3+1;
1996 } else {
1997 $realcnt=1+1;
1998 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001999 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002000 $prev_values = 'E';
2001
Andy Whitcroft773647a2008-03-28 14:15:58 -07002002 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002003 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002004 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002005 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002006 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002007
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002008# track the line number as we move through the hunk, note that
2009# new versions of GNU diff omit the leading space on completely
2010# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002011 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002012 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002013 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002014
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002015 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002016 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002017
2018 # Track the previous line.
2019 ($prevline, $stashline) = ($stashline, $line);
2020 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002021 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2022
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002024
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002025 } elsif ($realcnt == 1) {
2026 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002027 }
2028
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002029 my $hunk_line = ($realcnt != 0);
2030
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002031#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07002032 $prefix = "$filename:$realline: " if ($emacs && $file);
2033 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2034
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002035 $here = "#$linenr: " if (!$file);
2036 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002037
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002038 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002039 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002040 if ($line =~ /^diff --git.*?(\S+)$/) {
2041 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002042 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002043 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002044 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002045 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002046 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002047 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002048 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002049
2050 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002051 if (!$file && $tree && $p1_prefix ne '' &&
2052 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002053 WARN("PATCH_PREFIX",
2054 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002055 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002056
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002057 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002058 ERROR("MODIFIED_INCLUDE_ASM",
2059 "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 -07002060 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002061 $found_file = 1;
2062 }
2063
2064 if ($found_file) {
2065 if ($realfile =~ m@^(drivers/net/|net/)@) {
2066 $check = 1;
2067 } else {
2068 $check = $check_orig;
2069 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002070 next;
2071 }
2072
Randy Dunlap389834b2007-06-08 13:47:03 -07002073 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002074
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002075 my $hereline = "$here\n$rawline\n";
2076 my $herecurr = "$here\n$rawline\n";
2077 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002078
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002079 $cnt_lines++ if ($realcnt != 0);
2080
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002081# Check for incorrect file permissions
2082 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2083 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002084 if ($realfile !~ m@scripts/@ &&
2085 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002086 ERROR("EXECUTE_PERMISSIONS",
2087 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002088 }
2089 }
2090
Joe Perches20112472011-07-25 17:13:23 -07002091# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002092 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002093 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002094 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002095 }
2096
Joe Perchese0d975b2014-12-10 15:51:49 -08002097# Check if MAINTAINERS is being updated. If so, there's probably no need to
2098# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2099 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2100 $reported_maintainer_file = 1;
2101 }
2102
Joe Perches20112472011-07-25 17:13:23 -07002103# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002104 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002105 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002106 my $space_before = $1;
2107 my $sign_off = $2;
2108 my $space_after = $3;
2109 my $email = $4;
2110 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2111
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002112 if ($sign_off !~ /$signature_tags/) {
2113 WARN("BAD_SIGN_OFF",
2114 "Non-standard signature: $sign_off\n" . $herecurr);
2115 }
Joe Perches20112472011-07-25 17:13:23 -07002116 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002117 if (WARN("BAD_SIGN_OFF",
2118 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2119 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002120 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002121 "$ucfirst_sign_off $email";
2122 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002123 }
Joe Perches20112472011-07-25 17:13:23 -07002124 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002125 if (WARN("BAD_SIGN_OFF",
2126 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2127 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002128 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002129 "$ucfirst_sign_off $email";
2130 }
2131
Joe Perches20112472011-07-25 17:13:23 -07002132 }
2133 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002134 if (WARN("BAD_SIGN_OFF",
2135 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2136 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002137 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002138 "$ucfirst_sign_off $email";
2139 }
Joe Perches20112472011-07-25 17:13:23 -07002140 }
2141
2142 my ($email_name, $email_address, $comment) = parse_email($email);
2143 my $suggested_email = format_email(($email_name, $email_address));
2144 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002145 ERROR("BAD_SIGN_OFF",
2146 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002147 } else {
2148 my $dequoted = $suggested_email;
2149 $dequoted =~ s/^"//;
2150 $dequoted =~ s/" </ </;
2151 # Don't force email to have quotes
2152 # Allow just an angle bracketed address
2153 if ("$dequoted$comment" ne $email &&
2154 "<$email_address>$comment" ne $email &&
2155 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002156 WARN("BAD_SIGN_OFF",
2157 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002158 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002159 }
Joe Perches7e51f192013-09-11 14:23:57 -07002160
2161# Check for duplicate signatures
2162 my $sig_nospace = $line;
2163 $sig_nospace =~ s/\s//g;
2164 $sig_nospace = lc($sig_nospace);
2165 if (defined $signatures{$sig_nospace}) {
2166 WARN("BAD_SIGN_OFF",
2167 "Duplicate signature\n" . $herecurr);
2168 } else {
2169 $signatures{$sig_nospace} = 1;
2170 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002171 }
2172
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002173# Check email subject for common tools that don't need to be mentioned
2174 if ($in_header_lines &&
2175 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2176 WARN("EMAIL_SUBJECT",
2177 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2178 }
2179
Joe Perches9b3189e2014-06-04 16:12:10 -07002180# Check for old stable address
2181 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2182 ERROR("STABLE_ADDRESS",
2183 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2184 }
2185
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002186# Check for unwanted Gerrit info
2187 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2188 ERROR("GERRIT_CHANGE_ID",
2189 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2190 }
2191
Joe Perches0d7835f2015-02-13 14:38:35 -08002192# Check for git id commit length and improperly formed commit descriptions
2193 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
Joe Perchesd311cd42014-08-06 16:10:57 -07002194 my $init_char = $1;
2195 my $orig_commit = lc($2);
Joe Perches0d7835f2015-02-13 14:38:35 -08002196 my $short = 1;
2197 my $long = 0;
2198 my $case = 1;
2199 my $space = 1;
2200 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002201 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002202 my $id = '0123456789ab';
2203 my $orig_desc = "commit description";
2204 my $description = "";
2205
2206 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2207 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2208 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2209 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2210 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2211 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002212 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002213 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2214 defined $rawlines[$linenr] &&
2215 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2216 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002217 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002218 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2219 defined $rawlines[$linenr] &&
2220 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2221 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2222 $orig_desc = $1;
2223 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2224 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002225 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002226 }
2227
2228 ($id, $description) = git_commit_info($orig_commit,
2229 $id, $orig_desc);
2230
Joe Perches19c146a2015-02-13 14:39:00 -08002231 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002232 ERROR("GIT_COMMIT_ID",
2233 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2234 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002235 }
2236
Joe Perches13f19372014-08-06 16:10:59 -07002237# Check for added, moved or deleted files
2238 if (!$reported_maintainer_file && !$in_commit_log &&
2239 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2240 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2241 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2242 (defined($1) || defined($2))))) {
2243 $reported_maintainer_file = 1;
2244 WARN("FILE_PATH_CHANGES",
2245 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2246 }
2247
Andy Whitcroft00df3442007-06-08 13:47:06 -07002248# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002249 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002250 ERROR("CORRUPTED_PATCH",
2251 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002252 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002253 }
2254
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002255# Check for absolute kernel paths.
2256 if ($tree) {
2257 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2258 my $file = $1;
2259
2260 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2261 check_absolute_file($1, $herecurr)) {
2262 #
2263 } else {
2264 check_absolute_file($file, $herecurr);
2265 }
2266 }
2267 }
2268
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002269# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2270 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002271 $rawline !~ m/^$UTF8*$/) {
2272 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2273
2274 my $blank = copy_spacing($rawline);
2275 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2276 my $hereptr = "$hereline$ptr\n";
2277
Joe Perches34d99212011-07-25 17:13:26 -07002278 CHK("INVALID_UTF8",
2279 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002280 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002281
Joe Perches15662b32011-10-31 17:13:12 -07002282# Check if it's the start of a commit log
2283# (not a header line and we haven't seen the patch filename)
2284 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002285 !($rawline =~ /^\s+\S/ ||
2286 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002287 $in_header_lines = 0;
2288 $in_commit_log = 1;
2289 }
2290
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002291# Check if there is UTF-8 in a commit log when a mail header has explicitly
2292# declined it, i.e defined some charset where it is missing.
2293 if ($in_header_lines &&
2294 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2295 $1 !~ /utf-8/i) {
2296 $non_utf8_charset = 1;
2297 }
2298
2299 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002300 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002301 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002302 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2303 }
2304
Kees Cook66b47b42014-10-13 15:51:57 -07002305# Check for various typo / spelling mistakes
Joe Perches36061e32014-12-10 15:51:43 -08002306 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
Kees Cook66b47b42014-10-13 15:51:57 -07002307 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2308 my $typo = $1;
2309 my $typo_fix = $spelling_fix{lc($typo)};
2310 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2311 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2312 my $msg_type = \&WARN;
2313 $msg_type = \&CHK if ($file);
2314 if (&{$msg_type}("TYPO_SPELLING",
2315 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2316 $fix) {
2317 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2318 }
2319 }
2320 }
2321
Andy Whitcroft306708542008-10-15 22:02:28 -07002322# ignore non-hunk lines and lines being removed
2323 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002324
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002325#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002326 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002327 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002328 if (ERROR("DOS_LINE_ENDINGS",
2329 "DOS line endings\n" . $herevet) &&
2330 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002331 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002332 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002333 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2334 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002335 if (ERROR("TRAILING_WHITESPACE",
2336 "trailing whitespace\n" . $herevet) &&
2337 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002338 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002339 }
2340
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002341 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002343
Josh Triplett4783f892013-11-12 15:10:12 -08002344# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002345 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002346 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2347 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002348 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2349 my $msg_type = \&ERROR;
2350 $msg_type = \&CHK if ($file);
2351 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002352 "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 -08002353 }
2354
Andi Kleen33549572010-05-24 14:33:29 -07002355# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002356# Only applies when adding the entry originally, after that we do not have
2357# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002358 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002359 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002360 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002361 my $cnt = $realcnt;
2362 my $ln = $linenr + 1;
2363 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002364 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002365 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002366 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002367 $f = $lines[$ln - 1];
2368 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2369 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002370
2371 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002372 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002373
Joe Perches8d73e0e2014-08-06 16:10:46 -07002374 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002375 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002376 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002377 $length = -1;
2378 }
2379
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002380 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002381 $f =~ s/#.*//;
2382 $f =~ s/^\s+//;
2383 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002384 if ($f =~ /^\s*config\s/) {
2385 $is_end = 1;
2386 last;
2387 }
Andi Kleen33549572010-05-24 14:33:29 -07002388 $length++;
2389 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002390 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2391 WARN("CONFIG_DESCRIPTION",
2392 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2393 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002394 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002395 }
2396
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002397# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2398 if ($realfile =~ /Kconfig/ &&
2399 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2400 WARN("CONFIG_EXPERIMENTAL",
2401 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2402 }
2403
Christoph Jaeger327953e2015-02-13 14:38:29 -08002404# discourage the use of boolean for type definition attributes of Kconfig options
2405 if ($realfile =~ /Kconfig/ &&
2406 $line =~ /^\+\s*\bboolean\b/) {
2407 WARN("CONFIG_TYPE_BOOLEAN",
2408 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2409 }
2410
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002411 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2412 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2413 my $flag = $1;
2414 my $replacement = {
2415 'EXTRA_AFLAGS' => 'asflags-y',
2416 'EXTRA_CFLAGS' => 'ccflags-y',
2417 'EXTRA_CPPFLAGS' => 'cppflags-y',
2418 'EXTRA_LDFLAGS' => 'ldflags-y',
2419 };
2420
2421 WARN("DEPRECATED_VARIABLE",
2422 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2423 }
2424
Rob Herringbff5da42014-01-23 15:54:51 -08002425# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002426 if (defined $root &&
2427 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2428 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2429
Rob Herringbff5da42014-01-23 15:54:51 -08002430 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2431
Florian Vaussardcc933192014-04-03 14:49:27 -07002432 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2433 my $vp_file = $dt_path . "vendor-prefixes.txt";
2434
Rob Herringbff5da42014-01-23 15:54:51 -08002435 foreach my $compat (@compats) {
2436 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002437 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2438 my $compat3 = $compat;
2439 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2440 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002441 if ( $? >> 8 ) {
2442 WARN("UNDOCUMENTED_DT_STRING",
2443 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2444 }
2445
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002446 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2447 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002448 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002449 if ( $? >> 8 ) {
2450 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002451 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002452 }
2453 }
2454 }
2455
Andy Whitcroft5368df22008-10-15 22:02:27 -07002456# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002457 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002458
Joe Perches6cd7f382012-12-17 16:01:54 -08002459#line length limit
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002460 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
Andy Whitcroftf4c014c2008-07-23 21:29:01 -07002461 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
Joe Perches0fccc622011-05-24 17:13:41 -07002462 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
Joe Perches8bbea962010-08-09 17:21:01 -07002463 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Joe Perches6cd7f382012-12-17 16:01:54 -08002464 $length > $max_line_length)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002465 {
Joe Perches000d1cc12011-07-25 17:13:25 -07002466 WARN("LONG_LINE",
Joe Perches6cd7f382012-12-17 16:01:54 -08002467 "line over $max_line_length characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002468 }
2469
Andy Whitcroft8905a672007-11-28 16:21:06 -08002470# check for adding lines without a newline.
2471 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002472 WARN("MISSING_EOF_NEWLINE",
2473 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002474 }
2475
Mike Frysinger42e41c52009-09-21 17:04:40 -07002476# Blackfin: use hi/lo macros
2477 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2478 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2479 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002480 ERROR("LO_MACRO",
2481 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002482 }
2483 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2484 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002485 ERROR("HI_MACRO",
2486 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002487 }
2488 }
2489
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002490# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002491 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002492
2493# at the beginning of a line any tabs must come first and anything
2494# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002495 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2496 $rawline =~ /^\+\s* \s*/) {
2497 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002498 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002499 if (ERROR("CODE_INDENT",
2500 "code indent should use tabs where possible\n" . $herevet) &&
2501 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002502 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002503 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002504 }
2505
Alberto Panizzo08e44362010-03-05 13:43:54 -08002506# check for space before tabs.
2507 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2508 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002509 if (WARN("SPACE_BEFORE_TAB",
2510 "please, no space before tabs\n" . $herevet) &&
2511 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002512 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002513 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002514 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002515 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002516 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002517 }
2518
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002519# check for && or || at the start of a line
2520 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2521 CHK("LOGICAL_CONTINUATIONS",
2522 "Logical continuations should be on the previous line\n" . $hereprev);
2523 }
2524
2525# check multi-line statement indentation matches previous line
2526 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002527 $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 -07002528 $prevline =~ /^\+(\t*)(.*)$/;
2529 my $oldindent = $1;
2530 my $rest = $2;
2531
2532 my $pos = pos_last_openparen($rest);
2533 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002534 $line =~ /^(\+| )([ \t]*)/;
2535 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002536
2537 my $goodtabindent = $oldindent .
2538 "\t" x ($pos / 8) .
2539 " " x ($pos % 8);
2540 my $goodspaceindent = $oldindent . " " x $pos;
2541
2542 if ($newindent ne $goodtabindent &&
2543 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002544
2545 if (CHK("PARENTHESIS_ALIGNMENT",
2546 "Alignment should match open parenthesis\n" . $hereprev) &&
2547 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002548 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002549 s/^\+[ \t]*/\+$goodtabindent/;
2550 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002551 }
2552 }
2553 }
2554
Joe Perches43f7fe52015-02-13 14:38:52 -08002555 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;:\?\(\{\}\[\<\>]|&&|\|\||\\$)/ &&
Joe Perches04941aa2014-12-10 15:51:37 -08002556 (!defined($1) || $1 !~ /sizeof\s*/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002557 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002558 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002559 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002560 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002561 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002562 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002563 }
2564
Joe Perches05880602012-10-04 17:13:35 -07002565 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002566 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002567 $rawline =~ /^\+[ \t]*\*/ &&
2568 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002569 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2570 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2571 }
2572
2573 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesa605e322013-07-03 15:05:24 -07002574 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2575 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002576 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002577 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2578 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2579 "networking block comments start with * on subsequent lines\n" . $hereprev);
2580 }
2581
2582 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesc24f9f12012-11-08 15:53:29 -08002583 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2584 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2585 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2586 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches05880602012-10-04 17:13:35 -07002587 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2588 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2589 }
2590
Joe Perches7f619192014-08-06 16:10:39 -07002591# check for missing blank lines after struct/union declarations
2592# with exceptions for various attributes and macros
2593 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2594 $line =~ /^\+/ &&
2595 !($line =~ /^\+\s*$/ ||
2596 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2597 $line =~ /^\+\s*MODULE_/i ||
2598 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2599 $line =~ /^\+[a-z_]*init/ ||
2600 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2601 $line =~ /^\+\s*DECLARE/ ||
2602 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002603 if (CHK("LINE_SPACING",
2604 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2605 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002606 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002607 }
Joe Perches7f619192014-08-06 16:10:39 -07002608 }
2609
Joe Perches365dd4e2014-08-06 16:10:42 -07002610# check for multiple consecutive blank lines
2611 if ($prevline =~ /^[\+ ]\s*$/ &&
2612 $line =~ /^\+\s*$/ &&
2613 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002614 if (CHK("LINE_SPACING",
2615 "Please don't use multiple blank lines\n" . $hereprev) &&
2616 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002617 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07002618 }
2619
Joe Perches365dd4e2014-08-06 16:10:42 -07002620 $last_blank_line = $linenr;
2621 }
2622
Joe Perches3b617e32014-04-03 14:49:28 -07002623# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07002624 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2625 # actual declarations
2626 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002627 # function pointer declarations
2628 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002629 # foo bar; where foo is some local typedef or #define
2630 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2631 # known declaration macros
2632 $prevline =~ /^\+\s+$declaration_macros/) &&
2633 # for "else if" which can look like "$Ident $Ident"
2634 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2635 # other possible extensions of declaration lines
2636 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2637 # not starting a section or a macro "\" extended line
2638 $prevline =~ /(?:\{\s*|\\)$/) &&
2639 # looks like a declaration
2640 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07002641 # function pointer declarations
2642 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002643 # foo bar; where foo is some local typedef or #define
2644 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2645 # known declaration macros
2646 $sline =~ /^\+\s+$declaration_macros/ ||
2647 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07002648 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07002649 # start or end of block or continuation of declaration
2650 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2651 # bitfield continuation
2652 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2653 # other possible extensions of declaration lines
2654 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2655 # indentation of previous and current line are the same
2656 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002657 if (WARN("LINE_SPACING",
2658 "Missing a blank line after declarations\n" . $hereprev) &&
2659 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002660 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002661 }
Joe Perches3b617e32014-04-03 14:49:28 -07002662 }
2663
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002664# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07002665# Exceptions:
2666# 1) within comments
2667# 2) indented preprocessor commands
2668# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07002669 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002670 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002671 if (WARN("LEADING_SPACE",
2672 "please, no spaces at the start of a line\n" . $herevet) &&
2673 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002674 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002675 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07002676 }
2677
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002678# check we are in a valid C source file if not then ignore this hunk
2679 next if ($realfile !~ /\.(h|c)$/);
2680
Joe Perches032a4c02014-08-06 16:10:29 -07002681# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07002682# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07002683# if the previous line is a break or return and is indented 1 tab more...
2684 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2685 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07002686 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2687 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2688 defined $lines[$linenr] &&
2689 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07002690 WARN("UNNECESSARY_ELSE",
2691 "else is not generally useful after a break or return\n" . $hereprev);
2692 }
2693 }
2694
Joe Perchesc00df192014-08-06 16:11:01 -07002695# check indentation of a line with a break;
2696# if the previous line is a goto or return and is indented the same # of tabs
2697 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2698 my $tabs = $1;
2699 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2700 WARN("UNNECESSARY_BREAK",
2701 "break is not useful after a goto or return\n" . $hereprev);
2702 }
2703 }
2704
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002705# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2706 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2707 WARN("CONFIG_EXPERIMENTAL",
2708 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2709 }
2710
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002711# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08002712 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002713 WARN("CVS_KEYWORD",
2714 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002715 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07002716
Mike Frysinger42e41c52009-09-21 17:04:40 -07002717# Blackfin: don't use __builtin_bfin_[cs]sync
2718 if ($line =~ /__builtin_bfin_csync/) {
2719 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002720 ERROR("CSYNC",
2721 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002722 }
2723 if ($line =~ /__builtin_bfin_ssync/) {
2724 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002725 ERROR("SSYNC",
2726 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002727 }
2728
Joe Perches56e77d72013-02-21 16:44:14 -08002729# check for old HOTPLUG __dev<foo> section markings
2730 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2731 WARN("HOTPLUG_SECTION",
2732 "Using $1 is unnecessary\n" . $herecurr);
2733 }
2734
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002735# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002736 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2737 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002738#print "LINE<$line>\n";
2739 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07002740 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002741 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07002742 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002743 $stat =~ s/\n./\n /g;
2744 $cond =~ s/\n./\n /g;
2745
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002746#print "linenr<$linenr> <$stat>\n";
2747 # If this statement has no statement boundaries within
2748 # it there is no point in retrying a statement scan
2749 # until we hit end of it.
2750 my $frag = $stat; $frag =~ s/;+\s*$//;
2751 if ($frag !~ /(?:{|;)/) {
2752#print "skip<$line_nr_next>\n";
2753 $suppress_statement = $line_nr_next;
2754 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08002755
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002756 # Find the real next line.
2757 $realline_next = $line_nr_next;
2758 if (defined $realline_next &&
2759 (!defined $lines[$realline_next - 1] ||
2760 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2761 $realline_next++;
2762 }
2763
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002764 my $s = $stat;
2765 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002766
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002767 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002768 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002769
2770 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002771 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002772
Andy Whitcroft463f2862009-09-21 17:04:34 -07002773 } elsif ($s =~ /^.\s*else\b/s) {
2774
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002775 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07002776 } 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 -07002777 my $type = $1;
2778 $type =~ s/\s+/ /g;
2779 possible($type, "A:" . $s);
2780
Andy Whitcroft8905a672007-11-28 16:21:06 -08002781 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07002782 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002783 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002784 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002785
2786 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08002787 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002788 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002789 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002790
2791 # Check for any sort of function declaration.
2792 # int foo(something bar, other baz);
2793 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002794 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 -08002795 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002796
Andy Whitcroftcf655042008-03-04 14:28:20 -08002797 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002798 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08002799 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002800
Andy Whitcroft8905a672007-11-28 16:21:06 -08002801 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002802 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08002803
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002804 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002805 }
2806 }
2807 }
2808
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002809 }
2810
Andy Whitcroft653d4872007-06-23 17:16:34 -07002811#
2812# Checks which may be anchored in the context.
2813#
2814
2815# Check for switch () and associated case and default
2816# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07002817 if ($line=~/\bswitch\s*\(.*\)/) {
2818 my $err = '';
2819 my $sep = '';
2820 my @ctx = ctx_block_outer($linenr, $realcnt);
2821 shift(@ctx);
2822 for my $ctx (@ctx) {
2823 my ($clen, $cindent) = line_stats($ctx);
2824 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2825 $indent != $cindent) {
2826 $err .= "$sep$ctx\n";
2827 $sep = '';
2828 } else {
2829 $sep = "[...]\n";
2830 }
2831 }
2832 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07002833 ERROR("SWITCH_CASE_INDENT_LEVEL",
2834 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002835 }
2836 }
2837
2838# if/while/etc brace do not go on next line, unless defining a do while loop,
2839# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07002840 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 -07002841 my $pre_ctx = "$1$2";
2842
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002843 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08002844
2845 if ($line =~ /^\+\t{6,}/) {
2846 WARN("DEEP_INDENTATION",
2847 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2848 }
2849
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002850 my $ctx_cnt = $realcnt - $#ctx - 1;
2851 my $ctx = join("\n", @ctx);
2852
Andy Whitcroft548596d2008-07-23 21:29:01 -07002853 my $ctx_ln = $linenr;
2854 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002855
Andy Whitcroft548596d2008-07-23 21:29:01 -07002856 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2857 defined $lines[$ctx_ln - 1] &&
2858 $lines[$ctx_ln - 1] =~ /^-/)) {
2859 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2860 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002861 $ctx_ln++;
2862 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07002863
Andy Whitcroft53210162008-07-23 21:29:03 -07002864 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2865 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07002866
Joe Perchesd752fcc2014-08-06 16:11:05 -07002867 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002868 ERROR("OPEN_BRACE",
2869 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002870 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07002871 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002872 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2873 $ctx =~ /\)\s*\;\s*$/ &&
2874 defined $lines[$ctx_ln - 1])
2875 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002876 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2877 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002878 WARN("TRAILING_SEMICOLON",
2879 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07002880 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002881 }
2882 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07002883 }
2884
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002885# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07002886 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 -08002887 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2888 ctx_statement_block($linenr, $realcnt, 0)
2889 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002890 my ($s, $c) = ($stat, $cond);
2891
2892 substr($s, 0, length($c), '');
2893
2894 # Make sure we remove the line prefixes as we have
2895 # none on the first line, and are going to readd them
2896 # where necessary.
2897 $s =~ s/\n./\n/gs;
2898
2899 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07002900 my @newlines = ($c =~ /\n/gs);
2901 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002902
2903 # We want to check the first line inside the block
2904 # starting at the end of the conditional, so remove:
2905 # 1) any blank line termination
2906 # 2) any opening brace { on end of the line
2907 # 3) any do (...) {
2908 my $continuation = 0;
2909 my $check = 0;
2910 $s =~ s/^.*\bdo\b//;
2911 $s =~ s/^\s*{//;
2912 if ($s =~ s/^\s*\\//) {
2913 $continuation = 1;
2914 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002915 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002916 $check = 1;
2917 $cond_lines++;
2918 }
2919
2920 # Also ignore a loop construct at the end of a
2921 # preprocessor statement.
2922 if (($prevline =~ /^.\s*#\s*define\s/ ||
2923 $prevline =~ /\\\s*$/) && $continuation == 0) {
2924 $check = 0;
2925 }
2926
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002927 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07002928 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002929 while ($cond_ptr != $cond_lines) {
2930 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002931
Andy Whitcroftf16fa282008-10-15 22:02:32 -07002932 # If we see an #else/#elif then the code
2933 # is not linear.
2934 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2935 $check = 0;
2936 }
2937
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002938 # Ignore:
2939 # 1) blank lines, they should be at 0,
2940 # 2) preprocessor lines, and
2941 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07002942 if ($continuation ||
2943 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002944 $s =~ /^\s*#\s*?/ ||
2945 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07002946 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07002947 if ($s =~ s/^.*?\n//) {
2948 $cond_lines++;
2949 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002950 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002951 }
2952
2953 my (undef, $sindent) = line_stats("+" . $s);
2954 my $stat_real = raw_line($linenr, $cond_lines);
2955
2956 # Check if either of these lines are modified, else
2957 # this is not this patch's fault.
2958 if (!defined($stat_real) ||
2959 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2960 $check = 0;
2961 }
2962 if (defined($stat_real) && $cond_lines > 1) {
2963 $stat_real = "[...]\n$stat_real";
2964 }
2965
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07002966 #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 -07002967
2968 if ($check && (($sindent % 8) != 0 ||
2969 ($sindent <= $indent && $s ne ''))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002970 WARN("SUSPECT_CODE_INDENT",
2971 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07002972 }
2973 }
2974
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002975 # Track the 'values' across context and added lines.
2976 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002977 my ($curr_values, $curr_vars) =
2978 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002979 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002980 if ($dbg_values) {
2981 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08002982 print "$linenr > .$outline\n";
2983 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07002984 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002985 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002986 $prev_values = substr($curr_values, -1);
2987
Andy Whitcroft00df3442007-06-08 13:47:06 -07002988#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07002989 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002990
Andy Whitcroft653d4872007-06-23 17:16:34 -07002991# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07002992 if ($dbg_type) {
2993 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002994 ERROR("TEST_TYPE",
2995 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002996 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002997 ERROR("TEST_NOT_TYPE",
2998 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07002999 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003000 next;
3001 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003002# TEST: allow direct testing of the attribute matcher.
3003 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003004 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003005 ERROR("TEST_ATTR",
3006 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003007 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003008 ERROR("TEST_NOT_ATTR",
3009 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003010 }
3011 next;
3012 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003013
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003014# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003015 if ($line =~ /^.\s*{/ &&
3016 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003017 if (ERROR("OPEN_BRACE",
3018 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003019 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3020 fix_delete_line($fixlinenr - 1, $prevrawline);
3021 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003022 my $fixedline = $prevrawline;
3023 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003024 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003025 $fixedline = $line;
3026 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003027 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003028 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003029 }
3030
Andy Whitcroft653d4872007-06-23 17:16:34 -07003031#
3032# Checks which are anchored on the added line.
3033#
3034
3035# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003036 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003037 my $path = $1;
3038 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003039 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003040 "malformed #include filename\n" . $herecurr);
3041 }
3042 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3043 ERROR("UAPI_INCLUDE",
3044 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003045 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003046 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003047
3048# no C99 // comments
3049 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003050 if (ERROR("C99_COMMENTS",
3051 "do not use C99 // comments\n" . $herecurr) &&
3052 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003053 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003054 if ($line =~ /\/\/(.*)$/) {
3055 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003056 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003057 }
3058 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003059 }
3060 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003061 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003062 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003063
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003064# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3065# the whole statement.
3066#print "APW <$lines[$realline_next - 1]>\n";
3067 if (defined $realline_next &&
3068 exists $lines[$realline_next - 1] &&
3069 !defined $suppress_export{$realline_next} &&
3070 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3071 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003072 # Handle definitions which produce identifiers with
3073 # a prefix:
3074 # XXX(foo);
3075 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003076 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003077 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003078 $name =~ /^${Ident}_$2/) {
3079#print "FOO C name<$name>\n";
3080 $suppress_export{$realline_next} = 1;
3081
3082 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003083 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003084 ^.DEFINE_$Ident\(\Q$name\E\)|
3085 ^.DECLARE_$Ident\(\Q$name\E\)|
3086 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003087 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3088 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003089 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003090#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3091 $suppress_export{$realline_next} = 2;
3092 } else {
3093 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003094 }
3095 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003096 if (!defined $suppress_export{$linenr} &&
3097 $prevline =~ /^.\s*$/ &&
3098 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3099 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3100#print "FOO B <$lines[$linenr - 1]>\n";
3101 $suppress_export{$linenr} = 2;
3102 }
3103 if (defined $suppress_export{$linenr} &&
3104 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003105 WARN("EXPORT_SYMBOL",
3106 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003107 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003108
Joe Eloff5150bda2010-08-09 17:21:00 -07003109# check for global initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003110 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3111 if (ERROR("GLOBAL_INITIALISERS",
3112 "do not initialise globals to 0 or NULL\n" .
3113 $herecurr) &&
3114 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003115 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003116 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003117 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003118# check for static initialisers.
Joe Perchesd5e616f2013-09-11 14:23:54 -07003119 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3120 if (ERROR("INITIALISED_STATIC",
3121 "do not initialise statics to 0 or NULL\n" .
3122 $herecurr) &&
3123 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003124 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003125 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003126 }
3127
Joe Perches18130872014-08-06 16:11:22 -07003128# check for misordered declarations of char/short/int/long with signed/unsigned
3129 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3130 my $tmp = trim($1);
3131 WARN("MISORDERED_TYPE",
3132 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3133 }
3134
Joe Perchescb710ec2010-10-26 14:23:20 -07003135# check for static const char * arrays.
3136 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003137 WARN("STATIC_CONST_CHAR_ARRAY",
3138 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003139 $herecurr);
3140 }
3141
3142# check for static char foo[] = "bar" declarations.
3143 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003144 WARN("STATIC_CONST_CHAR_ARRAY",
3145 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003146 $herecurr);
3147 }
3148
Joe Perches9b0fa602014-04-03 14:49:18 -07003149# check for non-global char *foo[] = {"bar", ...} declarations.
3150 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3151 WARN("STATIC_CONST_CHAR_ARRAY",
3152 "char * array declaration might be better as static const\n" .
3153 $herecurr);
3154 }
3155
Joe Perchesb36190c2014-01-27 17:07:18 -08003156# check for function declarations without arguments like "int foo()"
3157 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3158 if (ERROR("FUNCTION_WITHOUT_ARGS",
3159 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3160 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003161 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003162 }
3163 }
3164
Joe Perches92e112f2013-12-13 11:36:22 -07003165# check for uses of DEFINE_PCI_DEVICE_TABLE
3166 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3167 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3168 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3169 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003170 $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 -07003171 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003172 }
3173
Andy Whitcroft653d4872007-06-23 17:16:34 -07003174# check for new typedefs, only function parameters and sparse annotations
3175# make sense.
3176 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003177 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003178 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003179 $line !~ /\b$typeTypedefs\b/ &&
Joe Perches021158b2015-02-13 14:38:43 -08003180 $line !~ /\b$typeOtherOSTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003181 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003182 WARN("NEW_TYPEDEFS",
3183 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003184 }
3185
3186# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003187 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003188 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3189 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003190 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003191
Andy Whitcroft65863862009-01-06 14:41:21 -08003192 # Should start with a space.
3193 $to =~ s/^(\S)/ $1/;
3194 # Should not end with a space.
3195 $to =~ s/\s+$//;
3196 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003197 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003198 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003199
Joe Perches3705ce52013-07-03 15:05:31 -07003200## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003201 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003202 if (ERROR("POINTER_LOCATION",
3203 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3204 $fix) {
3205 my $sub_from = $ident;
3206 my $sub_to = $ident;
3207 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003208 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003209 s@\Q$sub_from\E@$sub_to@;
3210 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003211 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003212 }
3213 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3214 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003215 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003216
Andy Whitcroft65863862009-01-06 14:41:21 -08003217 # Should start with a space.
3218 $to =~ s/^(\S)/ $1/;
3219 # Should not end with a space.
3220 $to =~ s/\s+$//;
3221 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003222 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003223 }
3224 # Modifiers should have spaces.
3225 $to =~ s/(\b$Modifier$)/$1 /;
3226
Joe Perches3705ce52013-07-03 15:05:31 -07003227## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003228 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003229 if (ERROR("POINTER_LOCATION",
3230 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3231 $fix) {
3232
3233 my $sub_from = $match;
3234 my $sub_to = $match;
3235 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003236 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003237 s@\Q$sub_from\E@$sub_to@;
3238 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003239 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003240 }
3241
3242# # no BUG() or BUG_ON()
3243# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3244# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3245# print "$herecurr";
3246# $clean = 0;
3247# }
3248
Andy Whitcroft8905a672007-11-28 16:21:06 -08003249 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003250 WARN("LINUX_VERSION_CODE",
3251 "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 -08003252 }
3253
Joe Perches17441222011-06-15 15:08:17 -07003254# check for uses of printk_ratelimit
3255 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003256 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003257 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003258 }
3259
Andy Whitcroft00df3442007-06-08 13:47:06 -07003260# printk should use KERN_* levels. Note that follow on printk's on the
3261# same line do not need a level, so we use the current block context
3262# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003263# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003264# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003265 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003266 my $ok = 0;
3267 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3268 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003269 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003270 # with "\n" ignore it, else it is to blame
3271 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3272 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3273 $ok = 1;
3274 }
3275 last;
3276 }
3277 }
3278 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003279 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3280 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003281 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003282 }
3283
Joe Perches243f3802012-05-31 16:26:09 -07003284 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3285 my $orig = $1;
3286 my $level = lc($orig);
3287 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003288 my $level2 = $level;
3289 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003290 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003291 "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 -07003292 }
3293
3294 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003295 if (WARN("PREFER_PR_LEVEL",
3296 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3297 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003298 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003299 s/\bpr_warning\b/pr_warn/;
3300 }
Joe Perches243f3802012-05-31 16:26:09 -07003301 }
3302
Joe Perchesdc139312013-02-21 16:44:13 -08003303 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3304 my $orig = $1;
3305 my $level = lc($orig);
3306 $level = "warn" if ($level eq "warning");
3307 $level = "dbg" if ($level eq "debug");
3308 WARN("PREFER_DEV_LEVEL",
3309 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3310 }
3311
Andy Whitcroft653d4872007-06-23 17:16:34 -07003312# function brace can't be on same line, except for #defines of do while,
3313# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003314 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003315 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003316 if (ERROR("OPEN_BRACE",
3317 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3318 $fix) {
3319 fix_delete_line($fixlinenr, $rawline);
3320 my $fixed_line = $rawline;
3321 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3322 my $line1 = $1;
3323 my $line2 = $2;
3324 fix_insert_line($fixlinenr, ltrim($line1));
3325 fix_insert_line($fixlinenr, "\+{");
3326 if ($line2 !~ /^\s*$/) {
3327 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3328 }
3329 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003330 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003331
Andy Whitcroft8905a672007-11-28 16:21:06 -08003332# open braces for enum, union and struct go on the same line.
3333 if ($line =~ /^.\s*{/ &&
3334 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003335 if (ERROR("OPEN_BRACE",
3336 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3337 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3338 fix_delete_line($fixlinenr - 1, $prevrawline);
3339 fix_delete_line($fixlinenr, $rawline);
3340 my $fixedline = rtrim($prevrawline) . " {";
3341 fix_insert_line($fixlinenr, $fixedline);
3342 $fixedline = $rawline;
3343 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3344 if ($fixedline !~ /^\+\s*$/) {
3345 fix_insert_line($fixlinenr, $fixedline);
3346 }
3347 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003348 }
3349
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003350# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003351 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3352 if (WARN("SPACING",
3353 "missing space after $1 definition\n" . $herecurr) &&
3354 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003355 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003356 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3357 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003358 }
3359
Joe Perches31070b52014-01-23 15:54:49 -08003360# Function pointer declarations
3361# check spacing between type, funcptr, and args
3362# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003363 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003364 my $declare = $1;
3365 my $pre_pointer_space = $2;
3366 my $post_pointer_space = $3;
3367 my $funcname = $4;
3368 my $post_funcname_space = $5;
3369 my $pre_args_space = $6;
3370
Joe Perches91f72e92014-04-03 14:49:12 -07003371# the $Declare variable will capture all spaces after the type
3372# so check it for a missing trailing missing space but pointer return types
3373# don't need a space so don't warn for those.
3374 my $post_declare_space = "";
3375 if ($declare =~ /(\s+)$/) {
3376 $post_declare_space = $1;
3377 $declare = rtrim($declare);
3378 }
3379 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003380 WARN("SPACING",
3381 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003382 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003383 }
3384
3385# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003386# This test is not currently implemented because these declarations are
3387# equivalent to
3388# int foo(int bar, ...)
3389# and this is form shouldn't/doesn't generate a checkpatch warning.
3390#
3391# elsif ($declare =~ /\s{2,}$/) {
3392# WARN("SPACING",
3393# "Multiple spaces after return type\n" . $herecurr);
3394# }
Joe Perches31070b52014-01-23 15:54:49 -08003395
3396# unnecessary space "type ( *funcptr)(args...)"
3397 if (defined $pre_pointer_space &&
3398 $pre_pointer_space =~ /^\s/) {
3399 WARN("SPACING",
3400 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3401 }
3402
3403# unnecessary space "type (* funcptr)(args...)"
3404 if (defined $post_pointer_space &&
3405 $post_pointer_space =~ /^\s/) {
3406 WARN("SPACING",
3407 "Unnecessary space before function pointer name\n" . $herecurr);
3408 }
3409
3410# unnecessary space "type (*funcptr )(args...)"
3411 if (defined $post_funcname_space &&
3412 $post_funcname_space =~ /^\s/) {
3413 WARN("SPACING",
3414 "Unnecessary space after function pointer name\n" . $herecurr);
3415 }
3416
3417# unnecessary space "type (*funcptr) (args...)"
3418 if (defined $pre_args_space &&
3419 $pre_args_space =~ /^\s/) {
3420 WARN("SPACING",
3421 "Unnecessary space before function pointer arguments\n" . $herecurr);
3422 }
3423
3424 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003425 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003426 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003427 }
3428 }
3429
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003430# check for spacing round square brackets; allowed:
3431# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003432# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3433# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003434 while ($line =~ /(.*?\s)\[/g) {
3435 my ($where, $prefix) = ($-[1], $1);
3436 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003437 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003438 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003439 if (ERROR("BRACKET_SPACE",
3440 "space prohibited before open square bracket '['\n" . $herecurr) &&
3441 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003442 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003443 s/^(\+.*?)\s+\[/$1\[/;
3444 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003445 }
3446 }
3447
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003448# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003449 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003450 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003451 my $ctx_before = substr($line, 0, $-[1]);
3452 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003453
3454 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003455 if ($name =~ /^(?:
3456 if|for|while|switch|return|case|
3457 volatile|__volatile__|
3458 __attribute__|format|__extension__|
3459 asm|__asm__)$/x)
3460 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003461 # cpp #define statements have non-optional spaces, ie
3462 # if there is a space between the name and the open
3463 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003464 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003465
3466 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003467 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003468
3469 # If this whole things ends with a type its most
3470 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003471 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003472
3473 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003474 if (WARN("SPACING",
3475 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003477 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003478 s/\b$name\s+\(/$name\(/;
3479 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003480 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003481 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003482
Andy Whitcroft653d4872007-06-23 17:16:34 -07003483# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003484 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003485 my $fixed_line = "";
3486 my $line_fixed = 0;
3487
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003488 my $ops = qr{
3489 <<=|>>=|<=|>=|==|!=|
3490 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3491 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003492 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003493 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003494 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003495 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003496
3497## print("element count: <" . $#elements . ">\n");
3498## foreach my $el (@elements) {
3499## print("el: <$el>\n");
3500## }
3501
3502 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003503 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003504
Joe Perches3705ce52013-07-03 15:05:31 -07003505 foreach my $el (@elements) {
3506 push(@fix_elements, substr($rawline, $off, length($el)));
3507 $off += length($el);
3508 }
3509
3510 $off = 0;
3511
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003512 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003513 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003514
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003515 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003516
3517 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3518
3519## print("n: <$n> good: <$good>\n");
3520
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003521 $off += length($elements[$n]);
3522
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003523 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003524 my $ca = substr($opline, 0, $off);
3525 my $cc = '';
3526 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3527 $cc = substr($opline, $off + length($elements[$n + 1]));
3528 }
3529 my $cb = "$ca$;$cc";
3530
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003531 my $a = '';
3532 $a = 'V' if ($elements[$n] ne '');
3533 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003534 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003535 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3536 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003537 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003538
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003539 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003540
3541 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003542 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003543 $c = 'V' if ($elements[$n + 2] ne '');
3544 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003545 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003546 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3547 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08003548 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003549 } else {
3550 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003551 }
3552
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003553 my $ctx = "${a}x${c}";
3554
3555 my $at = "(ctx:$ctx)";
3556
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003557 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003558 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003559
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003560 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003561 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003562
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003563 # Get the full operator variant.
3564 my $opv = $op . substr($curr_vars, $off, 1);
3565
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003566 # Ignore operators passed as parameters.
3567 if ($op_type ne 'V' &&
3568 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3569
Andy Whitcroftcf655042008-03-04 14:28:20 -08003570# # Ignore comments
3571# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003572
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003573 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003574 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003575 if ($ctx !~ /.x[WEBC]/ &&
3576 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003577 if (ERROR("SPACING",
3578 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003579 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003580 $line_fixed = 1;
3581 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003582 }
3583
3584 # // is a comment
3585 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003586
Joe Perchesb00e4812014-04-03 14:49:33 -07003587 # : when part of a bitfield
3588 } elsif ($opv eq ':B') {
3589 # skip the bitfield test for now
3590
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003591 # No spaces for:
3592 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07003593 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003594 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003595 if (ERROR("SPACING",
3596 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003597 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003598 if (defined $fix_elements[$n + 2]) {
3599 $fix_elements[$n + 2] =~ s/^\s+//;
3600 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003601 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003602 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003603 }
3604
Joe Perches23810972014-12-10 15:51:32 -08003605 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003606 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08003607 my $rtrim_before = 0;
3608 my $space_after = 0;
3609 if ($ctx =~ /Wx./) {
3610 if (ERROR("SPACING",
3611 "space prohibited before that '$op' $at\n" . $hereptr)) {
3612 $line_fixed = 1;
3613 $rtrim_before = 1;
3614 }
3615 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08003616 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003617 if (ERROR("SPACING",
3618 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003619 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07003620 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08003621 $space_after = 1;
3622 }
3623 }
3624 if ($rtrim_before || $space_after) {
3625 if ($rtrim_before) {
3626 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3627 } else {
3628 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3629 }
3630 if ($space_after) {
3631 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003632 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003633 }
3634
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003635 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003636 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003637 #warn "'*' is part of type\n";
3638
3639 # unary operators should have a space before and
3640 # none after. May be left adjacent to another
3641 # unary operator, or a cast
3642 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07003643 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07003644 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08003645 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003646 if (ERROR("SPACING",
3647 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003648 if ($n != $last_after + 2) {
3649 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3650 $line_fixed = 1;
3651 }
Joe Perches3705ce52013-07-03 15:05:31 -07003652 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003653 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08003654 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003655 # A unary '*' may be const
3656
3657 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003658 if (ERROR("SPACING",
3659 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003660 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003661 if (defined $fix_elements[$n + 2]) {
3662 $fix_elements[$n + 2] =~ s/^\s+//;
3663 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003664 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003665 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003666 }
3667
3668 # unary ++ and unary -- are allowed no space on one side.
3669 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003670 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003671 if (ERROR("SPACING",
3672 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003673 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07003674 $line_fixed = 1;
3675 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003676 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003677 if ($ctx =~ /Wx[BE]/ ||
3678 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003679 if (ERROR("SPACING",
3680 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003681 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003682 $line_fixed = 1;
3683 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003684 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003685 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003686 if (ERROR("SPACING",
3687 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003688 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003689 if (defined $fix_elements[$n + 2]) {
3690 $fix_elements[$n + 2] =~ s/^\s+//;
3691 }
Joe Perchesb34c6482013-09-11 14:24:01 -07003692 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07003693 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003694 }
3695
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003696 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003697 } elsif ($op eq '<<' or $op eq '>>' or
3698 $op eq '&' or $op eq '^' or $op eq '|' or
3699 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003700 $op eq '*' or $op eq '/' or
3701 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003702 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08003703 if ($check) {
3704 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
3705 if (CHK("SPACING",
3706 "spaces preferred around that '$op' $at\n" . $hereptr)) {
3707 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3708 $fix_elements[$n + 2] =~ s/^\s+//;
3709 $line_fixed = 1;
3710 }
3711 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
3712 if (CHK("SPACING",
3713 "space preferred before that '$op' $at\n" . $hereptr)) {
3714 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
3715 $line_fixed = 1;
3716 }
3717 }
3718 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003719 if (ERROR("SPACING",
3720 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003721 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3722 if (defined $fix_elements[$n + 2]) {
3723 $fix_elements[$n + 2] =~ s/^\s+//;
3724 }
Joe Perches3705ce52013-07-03 15:05:31 -07003725 $line_fixed = 1;
3726 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003727 }
3728
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003729 # A colon needs no spaces before when it is
3730 # terminating a case value or a label.
3731 } elsif ($opv eq ':C' || $opv eq ':L') {
3732 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07003733 if (ERROR("SPACING",
3734 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003735 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003736 $line_fixed = 1;
3737 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003738 }
3739
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003740 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08003741 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003742 my $ok = 0;
3743
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003744 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003745 if (($op eq '<' &&
3746 $cc =~ /^\S+\@\S+>/) ||
3747 ($op eq '>' &&
3748 $ca =~ /<\S+\@\S+$/))
3749 {
3750 $ok = 1;
3751 }
3752
Joe Perches84731622013-11-12 15:10:05 -08003753 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003754 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08003755 my $msg_type = \&ERROR;
3756 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3757
3758 if (&{$msg_type}("SPACING",
3759 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07003760 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3761 if (defined $fix_elements[$n + 2]) {
3762 $fix_elements[$n + 2] =~ s/^\s+//;
3763 }
Joe Perches3705ce52013-07-03 15:05:31 -07003764 $line_fixed = 1;
3765 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003766 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003767 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003768 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07003769
3770## print("n: <$n> GOOD: <$good>\n");
3771
3772 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003773 }
Joe Perches3705ce52013-07-03 15:05:31 -07003774
3775 if (($#elements % 2) == 0) {
3776 $fixed_line = $fixed_line . $fix_elements[$#elements];
3777 }
3778
Joe Perches194f66f2014-08-06 16:11:03 -07003779 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3780 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07003781 }
3782
3783
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003784 }
3785
Joe Perches786b6322013-07-03 15:05:32 -07003786# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08003787 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07003788 if (WARN("SPACING",
3789 "space prohibited before semicolon\n" . $herecurr) &&
3790 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003791 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07003792 s/^(\+.*\S)\s+;/$1;/;
3793 }
3794 }
3795
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003796# check for multiple assignments
3797 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003798 CHK("MULTIPLE_ASSIGNMENTS",
3799 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003800 }
3801
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003802## # check for multiple declarations, allowing for a function declaration
3803## # continuation.
3804## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3805## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3806##
3807## # Remove any bracketed sections to ensure we do not
3808## # falsly report the parameters of functions.
3809## my $ln = $line;
3810## while ($ln =~ s/\([^\(\)]*\)//g) {
3811## }
3812## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003813## WARN("MULTIPLE_DECLARATION",
3814## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003815## }
3816## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003817
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003818#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003819 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3820 $line =~ /do{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003821 if (ERROR("SPACING",
3822 "space required before the open brace '{'\n" . $herecurr) &&
3823 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003824 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07003825 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003826 }
3827
Joe Perchesc4a62ef2013-07-03 15:05:28 -07003828## # check for blank lines before declarations
3829## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3830## $prevrawline =~ /^.\s*$/) {
3831## WARN("SPACING",
3832## "No blank lines before declarations\n" . $hereprev);
3833## }
3834##
3835
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003836# closing brace should have a space following it when it has anything
3837# on the line
3838 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003839 if (ERROR("SPACING",
3840 "space required after that close brace '}'\n" . $herecurr) &&
3841 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003842 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003843 s/}((?!(?:,|;|\)))\S)/} $1/;
3844 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003845 }
3846
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003847# check spacing on square brackets
3848 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003849 if (ERROR("SPACING",
3850 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3851 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003852 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003853 s/\[\s+/\[/;
3854 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003855 }
3856 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003857 if (ERROR("SPACING",
3858 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3859 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003860 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003861 s/\s+\]/\]/;
3862 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003863 }
3864
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003865# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003866 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3867 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003868 if (ERROR("SPACING",
3869 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3870 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003871 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003872 s/\(\s+/\(/;
3873 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003874 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08003875 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003876 $line !~ /for\s*\(.*;\s+\)/ &&
3877 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003878 if (ERROR("SPACING",
3879 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3880 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003881 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003882 s/\s+\)/\)/;
3883 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003884 }
3885
Joe Perchese2826fd2014-08-06 16:10:48 -07003886# check unnecessary parentheses around addressof/dereference single $Lvals
3887# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3888
3889 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08003890 my $var = $1;
3891 if (CHK("UNNECESSARY_PARENTHESES",
3892 "Unnecessary parentheses around $var\n" . $herecurr) &&
3893 $fix) {
3894 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3895 }
3896 }
3897
3898# check for unnecessary parentheses around function pointer uses
3899# ie: (foo->bar)(); should be foo->bar();
3900# but not "if (foo->bar) (" to avoid some false positives
3901 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3902 my $var = $2;
3903 if (CHK("UNNECESSARY_PARENTHESES",
3904 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3905 $fix) {
3906 my $var2 = deparenthesize($var);
3907 $var2 =~ s/\s//g;
3908 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3909 }
3910 }
Joe Perchese2826fd2014-08-06 16:10:48 -07003911
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003912#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003913 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003914 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003915 if (WARN("INDENTED_LABEL",
3916 "labels should not be indented\n" . $herecurr) &&
3917 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003918 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003919 s/^(.)\s+/$1/;
3920 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003921 }
3922
Joe Perches5b9553a2014-04-03 14:49:21 -07003923# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08003924 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003925 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08003926 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07003927 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3928 my $value = $1;
3929 $value = deparenthesize($value);
3930 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3931 ERROR("RETURN_PARENTHESES",
3932 "return is not a function, parentheses are not required\n" . $herecurr);
3933 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003934 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003935 ERROR("SPACING",
3936 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003937 }
3938 }
Joe Perches507e5142013-11-12 15:10:13 -08003939
Joe Perchesb43ae212014-06-23 13:22:07 -07003940# unnecessary return in a void function
3941# at end-of-function, with the previous line a single leading tab, then return;
3942# and the line before that not a goto label target like "out:"
3943 if ($sline =~ /^[ \+]}\s*$/ &&
3944 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3945 $linenr >= 3 &&
3946 $lines[$linenr - 3] =~ /^[ +]/ &&
3947 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07003948 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07003949 "void function return statements are not generally useful\n" . $hereprev);
3950 }
Joe Perches9819cf22014-06-04 16:12:09 -07003951
Joe Perches189248d2014-01-23 15:54:47 -08003952# if statements using unnecessary parentheses - ie: if ((foo == bar))
3953 if ($^V && $^V ge 5.10.0 &&
3954 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3955 my $openparens = $1;
3956 my $count = $openparens =~ tr@\(@\(@;
3957 my $msg = "";
3958 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3959 my $comp = $4; #Not $1 because of $LvalOrFunc
3960 $msg = " - maybe == should be = ?" if ($comp eq "==");
3961 WARN("UNNECESSARY_PARENTHESES",
3962 "Unnecessary parentheses$msg\n" . $herecurr);
3963 }
3964 }
3965
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003966# Return of what appears to be an errno should normally be -'ve
3967 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3968 my $name = $1;
3969 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003970 WARN("USE_NEGATIVE_ERRNO",
3971 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07003972 }
3973 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003974
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003975# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07003976 if ($line =~ /\b(if|while|for|switch)\(/) {
3977 if (ERROR("SPACING",
3978 "space required before the open parenthesis '('\n" . $herecurr) &&
3979 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003980 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003981 s/\b(if|while|for|switch)\(/$1 \(/;
3982 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003983 }
3984
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003985# Check for illegal assignment in if conditional -- and check for trailing
3986# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003987 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003988 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3989 ctx_statement_block($linenr, $realcnt, 0)
3990 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003991 my ($stat_next) = ctx_statement_block($line_nr_next,
3992 $remain_next, $off_next);
3993 $stat_next =~ s/\n./\n /g;
3994 ##print "stat<$stat> stat_next<$stat_next>\n";
3995
3996 if ($stat_next =~ /^\s*while\b/) {
3997 # If the statement carries leading newlines,
3998 # then count those as offsets.
3999 my ($whitespace) =
4000 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4001 my $offset =
4002 statement_rawlines($whitespace) - 1;
4003
4004 $suppress_whiletrailers{$line_nr_next +
4005 $offset} = 1;
4006 }
4007 }
4008 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004009 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004010 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004011 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004012
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004013 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004014 ERROR("ASSIGN_IN_IF",
4015 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004016 }
4017
4018 # Find out what is on the end of the line after the
4019 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004020 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004021 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004022 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004023 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4024 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004025 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004026 # Find out how long the conditional actually is.
4027 my @newlines = ($c =~ /\n/gs);
4028 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004029 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004030
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004031 $stat_real = raw_line($linenr, $cond_lines)
4032 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004033 if (defined($stat_real) && $cond_lines > 1) {
4034 $stat_real = "[...]\n$stat_real";
4035 }
4036
Joe Perches000d1cc12011-07-25 17:13:25 -07004037 ERROR("TRAILING_STATEMENTS",
4038 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004039 }
4040 }
4041
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004042# Check for bitwise tests written as boolean
4043 if ($line =~ /
4044 (?:
4045 (?:\[|\(|\&\&|\|\|)
4046 \s*0[xX][0-9]+\s*
4047 (?:\&\&|\|\|)
4048 |
4049 (?:\&\&|\|\|)
4050 \s*0[xX][0-9]+\s*
4051 (?:\&\&|\|\||\)|\])
4052 )/x)
4053 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004054 WARN("HEXADECIMAL_BOOLEAN_TEST",
4055 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004056 }
4057
Andy Whitcroft8905a672007-11-28 16:21:06 -08004058# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004059 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4060 my $s = $1;
4061 $s =~ s/$;//g; # Remove any comments
4062 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004063 ERROR("TRAILING_STATEMENTS",
4064 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004065 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004066 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004067# if should not continue a brace
4068 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004069 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004070 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004071 $herecurr);
4072 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004073# case and default should not have general statements after them
4074 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4075 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004076 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004077 \s*return\s+
4078 )/xg)
4079 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004080 ERROR("TRAILING_STATEMENTS",
4081 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004082 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004083
4084 # Check for }<nl>else {, these must be at the same
4085 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004086 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4087 $previndent == $indent) {
4088 if (ERROR("ELSE_AFTER_BRACE",
4089 "else should follow close brace '}'\n" . $hereprev) &&
4090 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4091 fix_delete_line($fixlinenr - 1, $prevrawline);
4092 fix_delete_line($fixlinenr, $rawline);
4093 my $fixedline = $prevrawline;
4094 $fixedline =~ s/}\s*$//;
4095 if ($fixedline !~ /^\+\s*$/) {
4096 fix_insert_line($fixlinenr, $fixedline);
4097 }
4098 $fixedline = $rawline;
4099 $fixedline =~ s/^(.\s*)else/$1} else/;
4100 fix_insert_line($fixlinenr, $fixedline);
4101 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004102 }
4103
Joe Perches8b8856f2014-08-06 16:11:14 -07004104 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4105 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004106 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4107
4108 # Find out what is on the end of the line after the
4109 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004110 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004111 $s =~ s/\n.*//g;
4112
4113 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004114 if (ERROR("WHILE_AFTER_BRACE",
4115 "while should follow close brace '}'\n" . $hereprev) &&
4116 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4117 fix_delete_line($fixlinenr - 1, $prevrawline);
4118 fix_delete_line($fixlinenr, $rawline);
4119 my $fixedline = $prevrawline;
4120 my $trailing = $rawline;
4121 $trailing =~ s/^\+//;
4122 $trailing = trim($trailing);
4123 $fixedline =~ s/}\s*$/} $trailing/;
4124 fix_insert_line($fixlinenr, $fixedline);
4125 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004126 }
4127 }
4128
Joe Perches95e2c602013-07-03 15:05:20 -07004129#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004130 while ($line =~ m{($Constant|$Lval)}g) {
4131 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004132
4133#gcc binary extension
4134 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004135 if (WARN("GCC_BINARY_CONSTANT",
4136 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4137 $fix) {
4138 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004139 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004140 s/\b$var\b/$hexval/;
4141 }
Joe Perches95e2c602013-07-03 15:05:20 -07004142 }
4143
4144#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004145 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004146 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004147#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004148 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004149#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004150 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4151#Ignore some three character SI units explicitly, like MiB and KHz
4152 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004153 while ($var =~ m{($Ident)}g) {
4154 my $word = $1;
4155 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004156 if ($check) {
4157 seed_camelcase_includes();
4158 if (!$file && !$camelcase_file_seeded) {
4159 seed_camelcase_file($realfile);
4160 $camelcase_file_seeded = 1;
4161 }
4162 }
Joe Perches7e781f62013-09-11 14:23:55 -07004163 if (!defined $camelcase{$word}) {
4164 $camelcase{$word} = 1;
4165 CHK("CAMELCASE",
4166 "Avoid CamelCase: <$word>\n" . $herecurr);
4167 }
Joe Perches34456862013-07-03 15:05:34 -07004168 }
Joe Perches323c1262012-12-17 16:02:07 -08004169 }
4170 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004171
4172#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004173 if ($line =~ /\#\s*define.*\\\s+$/) {
4174 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4175 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4176 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004177 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004178 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004179 }
4180
Andy Whitcroft653d4872007-06-23 17:16:34 -07004181#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004182 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004183 my $file = "$1.h";
4184 my $checkfile = "include/linux/$file";
4185 if (-f "$root/$checkfile" &&
4186 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004187 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004188 {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004189 if ($realfile =~ m{^arch/}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004190 CHK("ARCH_INCLUDE_LINUX",
4191 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004192 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004193 WARN("INCLUDE_LINUX",
4194 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004195 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004196 }
4197 }
4198
Andy Whitcroft653d4872007-06-23 17:16:34 -07004199# multi-statement macros should be enclosed in a do while loop, grab the
4200# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004201# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004202 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4203 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004204 my $ln = $linenr;
4205 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004206 my ($off, $dstat, $dcond, $rest);
4207 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004208 my $has_flow_statement = 0;
4209 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004210 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004211 ctx_statement_block($linenr, $realcnt, 0);
4212 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004213 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004214 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004215
Joe Perches08a28432014-10-13 15:51:55 -07004216 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4217 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4218
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004219 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004220 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004221 $dstat =~ s/\\\n.//g;
4222 $dstat =~ s/^\s*//s;
4223 $dstat =~ s/\s*$//s;
4224
4225 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004226 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4227 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Andy Whitcroftc81769f2012-01-10 15:10:10 -08004228 $dstat =~ s/\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004229 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004230 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004231
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004232 # Flatten any obvious string concatentation.
4233 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4234 $dstat =~ s/$Ident\s*("X*")/$1/)
4235 {
4236 }
4237
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004238 my $exceptions = qr{
4239 $Declare|
4240 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004241 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004242 DECLARE_PER_CPU|
4243 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004244 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004245 union|
4246 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004247 \.$Ident\s*=\s*|
4248 ^\"|\"$
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004249 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004250 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004251 if ($dstat ne '' &&
4252 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4253 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004254 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004255 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004256 $dstat !~ /$exceptions/ &&
4257 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004258 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004259 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004260 $dstat !~ /^for\s*$Constant$/ && # for (...)
4261 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4262 $dstat !~ /^do\s*{/ && # do {...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004263 $dstat !~ /^\({/ && # ({...
4264 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004265 {
4266 $ctx =~ s/\n*$//;
4267 my $herectx = $here . "\n";
4268 my $cnt = statement_rawlines($ctx);
4269
4270 for (my $n = 0; $n < $cnt; $n++) {
4271 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004272 }
4273
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004274 if ($dstat =~ /;/) {
4275 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4276 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4277 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004278 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004279 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004280 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004281 }
Joe Perches5023d342012-12-17 16:01:47 -08004282
Joe Perches08a28432014-10-13 15:51:55 -07004283# check for macros with flow control, but without ## concatenation
4284# ## concatenation is commonly a macro that defines a function so ignore those
4285 if ($has_flow_statement && !$has_arg_concat) {
4286 my $herectx = $here . "\n";
4287 my $cnt = statement_rawlines($ctx);
4288
4289 for (my $n = 0; $n < $cnt; $n++) {
4290 $herectx .= raw_line($linenr, $n) . "\n";
4291 }
4292 WARN("MACRO_WITH_FLOW_CONTROL",
4293 "Macros with flow control statements should be avoided\n" . "$herectx");
4294 }
4295
Joe Perches481eb482012-12-17 16:01:56 -08004296# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004297
4298 } else {
4299 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004300 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4301 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004302 $line =~ /^\+.*\\$/) {
4303 WARN("LINE_CONTINUATIONS",
4304 "Avoid unnecessary line continuations\n" . $herecurr);
4305 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004306 }
4307
Joe Perchesb13edf72012-07-30 14:41:24 -07004308# do {} while (0) macro tests:
4309# single-statement macros do not need to be enclosed in do while (0) loop,
4310# macro should not end with a semicolon
4311 if ($^V && $^V ge 5.10.0 &&
4312 $realfile !~ m@/vmlinux.lds.h$@ &&
4313 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4314 my $ln = $linenr;
4315 my $cnt = $realcnt;
4316 my ($off, $dstat, $dcond, $rest);
4317 my $ctx = '';
4318 ($dstat, $dcond, $ln, $cnt, $off) =
4319 ctx_statement_block($linenr, $realcnt, 0);
4320 $ctx = $dstat;
4321
4322 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004323 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004324
4325 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4326 my $stmts = $2;
4327 my $semis = $3;
4328
4329 $ctx =~ s/\n*$//;
4330 my $cnt = statement_rawlines($ctx);
4331 my $herectx = $here . "\n";
4332
4333 for (my $n = 0; $n < $cnt; $n++) {
4334 $herectx .= raw_line($linenr, $n) . "\n";
4335 }
4336
Joe Perchesac8e97f2012-08-21 16:15:53 -07004337 if (($stmts =~ tr/;/;/) == 1 &&
4338 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004339 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4340 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4341 }
4342 if (defined $semis && $semis ne "") {
4343 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4344 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4345 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004346 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4347 $ctx =~ s/\n*$//;
4348 my $cnt = statement_rawlines($ctx);
4349 my $herectx = $here . "\n";
4350
4351 for (my $n = 0; $n < $cnt; $n++) {
4352 $herectx .= raw_line($linenr, $n) . "\n";
4353 }
4354
4355 WARN("TRAILING_SEMICOLON",
4356 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004357 }
4358 }
4359
Mike Frysinger080ba922009-01-06 14:41:25 -08004360# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4361# all assignments may have only one of the following with an assignment:
4362# .
4363# ALIGN(...)
4364# VMLINUX_SYMBOL(...)
4365 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004366 WARN("MISSING_VMLINUX_SYMBOL",
4367 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004368 }
4369
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004370# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004371 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4372 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004373 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004374 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004375 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4376 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004377 my @allowed = ();
4378 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004379 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004380 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004381 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004382 for my $chunk (@chunks) {
4383 my ($cond, $block) = @{$chunk};
4384
Andy Whitcroft773647a2008-03-28 14:15:58 -07004385 # If the condition carries leading newlines, then count those as offsets.
4386 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4387 my $offset = statement_rawlines($whitespace) - 1;
4388
Joe Perchesaad4f612012-03-23 15:02:19 -07004389 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004390 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4391
4392 # We have looked at and allowed this specific line.
4393 $suppress_ifbraces{$ln + $offset} = 1;
4394
4395 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004396 $ln += statement_rawlines($block) - 1;
4397
Andy Whitcroft773647a2008-03-28 14:15:58 -07004398 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004399
4400 $seen++ if ($block =~ /^\s*{/);
4401
Joe Perchesaad4f612012-03-23 15:02:19 -07004402 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004403 if (statement_lines($cond) > 1) {
4404 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004405 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004406 }
4407 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004408 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004409 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004410 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004411 if (statement_block_size($block) > 1) {
4412 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004413 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004414 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004415 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004416 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004417 if ($seen) {
4418 my $sum_allowed = 0;
4419 foreach (@allowed) {
4420 $sum_allowed += $_;
4421 }
4422 if ($sum_allowed == 0) {
4423 WARN("BRACES",
4424 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4425 } elsif ($sum_allowed != $allow &&
4426 $seen != $allow) {
4427 CHK("BRACES",
4428 "braces {} should be used on all arms of this statement\n" . $herectx);
4429 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004430 }
4431 }
4432 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004433 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004434 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004435 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004436
Andy Whitcroftcf655042008-03-04 14:28:20 -08004437 # Check the pre-context.
4438 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4439 #print "APW: ALLOWED: pre<$1>\n";
4440 $allowed = 1;
4441 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004442
4443 my ($level, $endln, @chunks) =
4444 ctx_statement_full($linenr, $realcnt, $-[0]);
4445
Andy Whitcroftcf655042008-03-04 14:28:20 -08004446 # Check the condition.
4447 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004448 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004449 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004450 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004451 }
4452 if (statement_lines($cond) > 1) {
4453 #print "APW: ALLOWED: cond<$cond>\n";
4454 $allowed = 1;
4455 }
4456 if ($block =~/\b(?:if|for|while)\b/) {
4457 #print "APW: ALLOWED: block<$block>\n";
4458 $allowed = 1;
4459 }
4460 if (statement_block_size($block) > 1) {
4461 #print "APW: ALLOWED: lines block<$block>\n";
4462 $allowed = 1;
4463 }
4464 # Check the post-context.
4465 if (defined $chunks[1]) {
4466 my ($cond, $block) = @{$chunks[1]};
4467 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004468 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004469 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004470 if ($block =~ /^\s*\{/) {
4471 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4472 $allowed = 1;
4473 }
4474 }
4475 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004476 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004477 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004478
Andy Whitcroftf0556632008-10-15 22:02:23 -07004479 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004480 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004481 }
4482
Joe Perches000d1cc12011-07-25 17:13:25 -07004483 WARN("BRACES",
4484 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004485 }
4486 }
4487
Joe Perches0979ae62012-12-17 16:01:59 -08004488# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004489 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004490 if (CHK("BRACES",
4491 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4492 $fix && $prevrawline =~ /^\+/) {
4493 fix_delete_line($fixlinenr - 1, $prevrawline);
4494 }
Joe Perches0979ae62012-12-17 16:01:59 -08004495 }
Joe Perches77b9a532013-07-03 15:05:29 -07004496 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004497 if (CHK("BRACES",
4498 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4499 $fix) {
4500 fix_delete_line($fixlinenr, $rawline);
4501 }
Joe Perches0979ae62012-12-17 16:01:59 -08004502 }
4503
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004504# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004505 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4506 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004507 WARN("VOLATILE",
4508 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004509 }
4510
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004511# Check for user-visible strings broken across lines, which breaks the ability
4512# to grep for the string. Make exceptions when the previous string ends in a
4513# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4514# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4515 if ($line =~ /^\+\s*"[X\t]*"/ &&
4516 $prevline =~ /"\s*$/ &&
4517 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4518 if (WARN("SPLIT_STRING",
4519 "quoted string split across lines\n" . $hereprev) &&
4520 $fix &&
4521 $prevrawline =~ /^\+.*"\s*$/ &&
4522 $last_coalesced_string_linenr != $linenr - 1) {
4523 my $extracted_string = get_quoted_string($line, $rawline);
4524 my $comma_close = "";
4525 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4526 $comma_close = $1;
4527 }
4528
4529 fix_delete_line($fixlinenr - 1, $prevrawline);
4530 fix_delete_line($fixlinenr, $rawline);
4531 my $fixedline = $prevrawline;
4532 $fixedline =~ s/"\s*$//;
4533 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4534 fix_insert_line($fixlinenr - 1, $fixedline);
4535 $fixedline = $rawline;
4536 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4537 if ($fixedline !~ /\+\s*$/) {
4538 fix_insert_line($fixlinenr, $fixedline);
4539 }
4540 $last_coalesced_string_linenr = $linenr;
4541 }
4542 }
4543
4544# check for missing a space in a string concatenation
4545 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4546 WARN('MISSING_SPACE',
4547 "break quoted strings at a space character\n" . $hereprev);
4548 }
4549
4550# check for spaces before a quoted newline
4551 if ($rawline =~ /^.*\".*\s\\n/) {
4552 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4553 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4554 $fix) {
4555 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4556 }
4557
4558 }
4559
Joe Perchesf17dba42014-10-13 15:51:51 -07004560# concatenated string without spaces between elements
4561 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4562 CHK("CONCATENATED_STRING",
4563 "Concatenated strings should use spaces between elements\n" . $herecurr);
4564 }
4565
Joe Perches90ad30e2014-12-10 15:51:59 -08004566# uncoalesced string fragments
4567 if ($line =~ /"X*"\s*"/) {
4568 WARN("STRING_FRAGMENTS",
4569 "Consecutive strings are generally better as a single string\n" . $herecurr);
4570 }
4571
Joe Perches5e4f6ba2014-12-10 15:52:05 -08004572# check for %L{u,d,i} in strings
4573 my $string;
4574 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4575 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4576 $string =~ s/%%/__/g;
4577 if ($string =~ /(?<!%)%L[udi]/) {
4578 WARN("PRINTF_L",
4579 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4580 last;
4581 }
4582 }
4583
4584# check for line continuations in quoted strings with odd counts of "
4585 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4586 WARN("LINE_CONTINUATIONS",
4587 "Avoid line continuations in quoted strings\n" . $herecurr);
4588 }
4589
Andy Whitcroft00df3442007-06-08 13:47:06 -07004590# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004591 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004592 CHK("REDUNDANT_CODE",
4593 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004594 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004595 }
4596
Andy Whitcroft03df4b52012-12-17 16:01:52 -08004597# check for needless "if (<foo>) fn(<foo>)" uses
4598 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4599 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4600 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4601 WARN('NEEDLESS_IF',
Fabio Estevam15160f92014-12-10 15:51:40 -08004602 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07004603 }
4604 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004605
Joe Perchesebfdc402014-08-06 16:10:27 -07004606# check for unnecessary "Out of Memory" messages
4607 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4608 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4609 (defined $1 || defined $3) &&
4610 $linenr > 3) {
4611 my $testval = $2;
4612 my $testline = $lines[$linenr - 3];
4613
4614 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4615# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4616
4617 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4618 WARN("OOM_MESSAGE",
4619 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4620 }
4621 }
4622
Joe Perchesf78d98f2014-10-13 15:52:01 -07004623# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08004624 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07004625 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4626 my $level = $1;
4627 if (WARN("UNNECESSARY_KERN_LEVEL",
4628 "Possible unnecessary $level\n" . $herecurr) &&
4629 $fix) {
4630 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4631 }
4632 }
4633
Joe Perchesabb08a52014-12-10 15:51:46 -08004634# check for mask then right shift without a parentheses
4635 if ($^V && $^V ge 5.10.0 &&
4636 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4637 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4638 WARN("MASK_THEN_SHIFT",
4639 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4640 }
4641
Joe Perchesb75ac612014-12-10 15:52:02 -08004642# check for pointer comparisons to NULL
4643 if ($^V && $^V ge 5.10.0) {
4644 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4645 my $val = $1;
4646 my $equal = "!";
4647 $equal = "" if ($4 eq "!=");
4648 if (CHK("COMPARISON_TO_NULL",
4649 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4650 $fix) {
4651 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4652 }
4653 }
4654 }
4655
Joe Perches8716de32013-09-11 14:24:05 -07004656# check for bad placement of section $InitAttribute (e.g.: __initdata)
4657 if ($line =~ /(\b$InitAttribute\b)/) {
4658 my $attr = $1;
4659 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4660 my $ptr = $1;
4661 my $var = $2;
4662 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4663 ERROR("MISPLACED_INIT",
4664 "$attr should be placed after $var\n" . $herecurr)) ||
4665 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4666 WARN("MISPLACED_INIT",
4667 "$attr should be placed after $var\n" . $herecurr))) &&
4668 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004669 $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 -07004670 }
4671 }
4672 }
4673
Joe Perchese970b882013-11-12 15:10:10 -08004674# check for $InitAttributeData (ie: __initdata) with const
4675 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4676 my $attr = $1;
4677 $attr =~ /($InitAttributePrefix)(.*)/;
4678 my $attr_prefix = $1;
4679 my $attr_type = $2;
4680 if (ERROR("INIT_ATTRIBUTE",
4681 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4682 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004683 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004684 s/$InitAttributeData/${attr_prefix}initconst/;
4685 }
4686 }
4687
4688# check for $InitAttributeConst (ie: __initconst) without const
4689 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4690 my $attr = $1;
4691 if (ERROR("INIT_ATTRIBUTE",
4692 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4693 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004694 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08004695 /(^\+\s*(?:static\s+))/;
4696 $lead = rtrim($1);
4697 $lead = "$lead " if ($lead !~ /^\+$/);
4698 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07004699 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08004700 }
4701 }
4702
Joe Perchesfbdb8132014-04-03 14:49:14 -07004703# don't use __constant_<foo> functions outside of include/uapi/
4704 if ($realfile !~ m@^include/uapi/@ &&
4705 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4706 my $constant_func = $1;
4707 my $func = $constant_func;
4708 $func =~ s/^__constant_//;
4709 if (WARN("CONSTANT_CONVERSION",
4710 "$constant_func should be $func\n" . $herecurr) &&
4711 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004712 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07004713 }
4714 }
4715
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004716# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08004717 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07004718 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004719 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07004720 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004721 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07004722 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4723 }
4724 if ($delay > 2000) {
4725 WARN("LONG_UDELAY",
4726 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07004727 }
4728 }
4729
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004730# warn about unexpectedly long msleep's
4731 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4732 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004733 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07004734 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07004735 }
4736 }
4737
Joe Perches36ec1932013-07-03 15:05:25 -07004738# check for comparisons of jiffies
4739 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4740 WARN("JIFFIES_COMPARISON",
4741 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4742 }
4743
Joe Perches9d7a34a2013-07-03 15:05:26 -07004744# check for comparisons of get_jiffies_64()
4745 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4746 WARN("JIFFIES_COMPARISON",
4747 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4748 }
4749
Andy Whitcroft00df3442007-06-08 13:47:06 -07004750# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004751# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07004752# print "#ifdef in C files should be avoided\n";
4753# print "$herecurr";
4754# $clean = 0;
4755# }
4756
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004757# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004758 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004759 if (ERROR("SPACING",
4760 "exactly one space required after that #$1\n" . $herecurr) &&
4761 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004762 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004763 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4764 }
4765
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004766 }
4767
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004768# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004769 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4770 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004771 my $which = $1;
4772 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004773 CHK("UNCOMMENTED_DEFINITION",
4774 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004775 }
4776 }
4777# check for memory barriers without a comment.
4778 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4779 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08004780 WARN("MEMORY_BARRIER",
4781 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004782 }
4783 }
4784# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004785 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004786 CHK("ARCH_DEFINES",
4787 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004788 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004789
Tobias Klauserd4977c72010-05-24 14:33:30 -07004790# Check that the storage class is at the beginning of a declaration
4791 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004792 WARN("STORAGE_CLASS",
4793 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07004794 }
4795
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004796# check the location of the inline attribute, that it is between
4797# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004798 if ($line =~ /\b$Type\s+$Inline\b/ ||
4799 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004800 ERROR("INLINE_LOCATION",
4801 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004802 }
4803
Andy Whitcroft8905a672007-11-28 16:21:06 -08004804# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08004805 if ($realfile !~ m@\binclude/uapi/@ &&
4806 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004807 if (WARN("INLINE",
4808 "plain inline is preferred over $1\n" . $herecurr) &&
4809 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004810 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004811
4812 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08004813 }
4814
Joe Perches3d130fd2011-01-12 17:00:00 -08004815# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08004816 if ($realfile !~ m@\binclude/uapi/@ &&
4817 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004818 WARN("PREFER_PACKED",
4819 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08004820 }
4821
Joe Perches39b7e282011-07-25 17:13:24 -07004822# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08004823 if ($realfile !~ m@\binclude/uapi/@ &&
4824 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004825 WARN("PREFER_ALIGNED",
4826 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07004827 }
4828
Joe Perches5f14d3b2012-01-10 15:09:52 -08004829# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08004830 if ($realfile !~ m@\binclude/uapi/@ &&
4831 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004832 if (WARN("PREFER_PRINTF",
4833 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4834 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004835 $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 -07004836
4837 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08004838 }
4839
Joe Perches6061d942012-03-23 15:02:16 -07004840# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08004841 if ($realfile !~ m@\binclude/uapi/@ &&
4842 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004843 if (WARN("PREFER_SCANF",
4844 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4845 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004846 $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 -07004847 }
Joe Perches6061d942012-03-23 15:02:16 -07004848 }
4849
Joe Perches619a9082014-12-10 15:51:35 -08004850# Check for __attribute__ weak, or __weak declarations (may have link issues)
4851 if ($^V && $^V ge 5.10.0 &&
4852 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4853 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4854 $line =~ /\b__weak\b/)) {
4855 ERROR("WEAK_DECLARATION",
4856 "Using weak declarations can have unintended link defects\n" . $herecurr);
4857 }
4858
Joe Perches8f53a9b2010-03-05 13:43:48 -08004859# check for sizeof(&)
4860 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004861 WARN("SIZEOF_ADDRESS",
4862 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08004863 }
4864
Joe Perches66c80b62012-07-30 14:41:22 -07004865# check for sizeof without parenthesis
4866 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004867 if (WARN("SIZEOF_PARENTHESIS",
4868 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4869 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004870 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004871 }
Joe Perches66c80b62012-07-30 14:41:22 -07004872 }
4873
Joe Perches88982fe2012-12-17 16:02:00 -08004874# check for struct spinlock declarations
4875 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4876 WARN("USE_SPINLOCK_T",
4877 "struct spinlock should be spinlock_t\n" . $herecurr);
4878 }
4879
Joe Perchesa6962d72013-04-29 16:18:13 -07004880# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08004881 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07004882 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08004883 $fmt =~ s/%%//g;
4884 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004885 if (WARN("PREFER_SEQ_PUTS",
4886 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4887 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004888 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004889 }
Joe Perchesa6962d72013-04-29 16:18:13 -07004890 }
4891 }
4892
Andy Whitcroft554e1652012-01-10 15:09:57 -08004893# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004894 if ($^V && $^V ge 5.10.0 &&
4895 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004896 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08004897
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004898 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004899 my $ms_val = $7;
4900 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004901
Andy Whitcroft554e1652012-01-10 15:09:57 -08004902 if ($ms_size =~ /^(0x|)0$/i) {
4903 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004904 "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 -08004905 } elsif ($ms_size =~ /^(0x|)1$/i) {
4906 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004907 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4908 }
4909 }
4910
Joe Perches98a9bba2014-01-23 15:54:52 -08004911# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4912 if ($^V && $^V ge 5.10.0 &&
4913 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4914 if (WARN("PREFER_ETHER_ADDR_COPY",
4915 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4916 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004917 $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 -08004918 }
4919 }
4920
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004921# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004922 if ($^V && $^V ge 5.10.0 &&
4923 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004924 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004925 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004926 my $call = $1;
4927 my $cast1 = deparenthesize($2);
4928 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004929 my $cast2 = deparenthesize($7);
4930 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004931 my $cast;
4932
Joe Perchesd1fe9c02012-03-23 15:02:16 -07004933 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08004934 $cast = "$cast1 or $cast2";
4935 } elsif ($cast1 ne "") {
4936 $cast = $cast1;
4937 } else {
4938 $cast = $cast2;
4939 }
4940 WARN("MINMAX",
4941 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08004942 }
4943 }
4944
Joe Perches4a273192012-07-30 14:41:20 -07004945# check usleep_range arguments
4946 if ($^V && $^V ge 5.10.0 &&
4947 defined $stat &&
4948 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4949 my $min = $1;
4950 my $max = $7;
4951 if ($min eq $max) {
4952 WARN("USLEEP_RANGE",
4953 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4954 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4955 $min > $max) {
4956 WARN("USLEEP_RANGE",
4957 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4958 }
4959 }
4960
Joe Perches823b7942013-11-12 15:10:15 -08004961# check for naked sscanf
4962 if ($^V && $^V ge 5.10.0 &&
4963 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07004964 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08004965 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4966 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4967 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4968 my $lc = $stat =~ tr@\n@@;
4969 $lc = $lc + $linenr;
4970 my $stat_real = raw_line($linenr, 0);
4971 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4972 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4973 }
4974 WARN("NAKED_SSCANF",
4975 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4976 }
4977
Joe Perchesafc819a2014-06-04 16:12:08 -07004978# check for simple sscanf that should be kstrto<foo>
4979 if ($^V && $^V ge 5.10.0 &&
4980 defined $stat &&
4981 $line =~ /\bsscanf\b/) {
4982 my $lc = $stat =~ tr@\n@@;
4983 $lc = $lc + $linenr;
4984 my $stat_real = raw_line($linenr, 0);
4985 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4986 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4987 }
4988 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4989 my $format = $6;
4990 my $count = $format =~ tr@%@%@;
4991 if ($count == 1 &&
4992 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4993 WARN("SSCANF_TO_KSTRTO",
4994 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4995 }
4996 }
4997 }
4998
Joe Perches70dc8a42013-09-11 14:23:58 -07004999# check for new externs in .h files.
5000 if ($realfile =~ /\.h$/ &&
5001 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005002 if (CHK("AVOID_EXTERNS",
5003 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005004 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005005 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005006 }
5007 }
5008
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005009# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005010 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005011 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005012 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005013 my $function_name = $1;
5014 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005015
5016 my $s = $stat;
5017 if (defined $cond) {
5018 substr($s, 0, length($cond), '');
5019 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005020 if ($s =~ /^\s*;/ &&
5021 $function_name ne 'uninitialized_var')
5022 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005023 WARN("AVOID_EXTERNS",
5024 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005025 }
5026
5027 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005028 WARN("FUNCTION_ARGUMENTS",
5029 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005030 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005031
5032 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5033 $stat =~ /^.\s*extern\s+/)
5034 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005035 WARN("AVOID_EXTERNS",
5036 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005037 }
5038
5039# checks for new __setup's
5040 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5041 my $name = $1;
5042
5043 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005044 CHK("UNDOCUMENTED_SETUP",
5045 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005046 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005047 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005048
5049# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005050 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005051 WARN("UNNECESSARY_CASTS",
5052 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005053 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005054
Joe Perchesa640d252013-07-03 15:05:21 -07005055# alloc style
5056# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5057 if ($^V && $^V ge 5.10.0 &&
5058 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5059 CHK("ALLOC_SIZEOF_STRUCT",
5060 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5061 }
5062
Joe Perches60a55362014-06-04 16:12:07 -07005063# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5064 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005065 $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 -07005066 my $oldfunc = $3;
5067 my $a1 = $4;
5068 my $a2 = $10;
5069 my $newfunc = "kmalloc_array";
5070 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005071 my $r1 = $a1;
5072 my $r2 = $a2;
5073 if ($a1 =~ /^sizeof\s*\S/) {
5074 $r1 = $a2;
5075 $r2 = $a1;
5076 }
5077 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5078 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005079 if (WARN("ALLOC_WITH_MULTIPLY",
5080 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5081 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005082 $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 -07005083
5084 }
5085 }
5086 }
5087
Joe Perches972fdea2013-04-29 16:18:12 -07005088# check for krealloc arg reuse
5089 if ($^V && $^V ge 5.10.0 &&
5090 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5091 WARN("KREALLOC_ARG_REUSE",
5092 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5093 }
5094
Joe Perches5ce59ae2013-02-21 16:44:18 -08005095# check for alloc argument mismatch
5096 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5097 WARN("ALLOC_ARRAY_ARGS",
5098 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5099 }
5100
Joe Perchescaf2a542011-01-12 16:59:56 -08005101# check for multiple semicolons
5102 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005103 if (WARN("ONE_SEMICOLON",
5104 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5105 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005106 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005107 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005108 }
5109
Joe Perches0ab90192014-12-10 15:51:57 -08005110# check for #defines like: 1 << <digit> that could be BIT(digit)
5111 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5112 my $ull = "";
5113 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5114 if (CHK("BIT_MACRO",
5115 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5116 $fix) {
5117 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5118 }
5119 }
5120
Joe Perchese81f2392014-08-06 16:11:25 -07005121# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005122 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5123 my $has_break = 0;
5124 my $has_statement = 0;
5125 my $count = 0;
5126 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005127 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005128 $prevline--;
5129 my $rline = $rawlines[$prevline - 1];
5130 my $fline = $lines[$prevline - 1];
5131 last if ($fline =~ /^\@\@/);
5132 next if ($fline =~ /^\-/);
5133 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5134 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5135 next if ($fline =~ /^.[\s$;]*$/);
5136 $has_statement = 1;
5137 $count++;
5138 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5139 }
5140 if (!$has_break && $has_statement) {
5141 WARN("MISSING_BREAK",
5142 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5143 }
5144 }
5145
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005146# check for switch/default statements without a break;
5147 if ($^V && $^V ge 5.10.0 &&
5148 defined $stat &&
5149 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5150 my $ctx = '';
5151 my $herectx = $here . "\n";
5152 my $cnt = statement_rawlines($stat);
5153 for (my $n = 0; $n < $cnt; $n++) {
5154 $herectx .= raw_line($linenr, $n) . "\n";
5155 }
5156 WARN("DEFAULT_NO_BREAK",
5157 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005158 }
5159
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005160# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005161 if ($line =~ /\b__FUNCTION__\b/) {
5162 if (WARN("USE_FUNC",
5163 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5164 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005165 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005166 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005167 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005168
Joe Perches62ec8182015-02-13 14:38:18 -08005169# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5170 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5171 ERROR("DATE_TIME",
5172 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5173 }
5174
Joe Perches2c924882012-03-23 15:02:20 -07005175# check for use of yield()
5176 if ($line =~ /\byield\s*\(\s*\)/) {
5177 WARN("YIELD",
5178 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5179 }
5180
Joe Perches179f8f42013-07-03 15:05:30 -07005181# check for comparisons against true and false
5182 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5183 my $lead = $1;
5184 my $arg = $2;
5185 my $test = $3;
5186 my $otype = $4;
5187 my $trail = $5;
5188 my $op = "!";
5189
5190 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5191
5192 my $type = lc($otype);
5193 if ($type =~ /^(?:true|false)$/) {
5194 if (("$test" eq "==" && "$type" eq "true") ||
5195 ("$test" eq "!=" && "$type" eq "false")) {
5196 $op = "";
5197 }
5198
5199 CHK("BOOL_COMPARISON",
5200 "Using comparison to $otype is error prone\n" . $herecurr);
5201
5202## maybe suggesting a correct construct would better
5203## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5204
5205 }
5206 }
5207
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005208# check for semaphores initialized locked
5209 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005210 WARN("CONSIDER_COMPLETION",
5211 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005212 }
Joe Perches6712d852012-03-23 15:02:20 -07005213
Joe Perches67d0a072011-10-31 17:13:10 -07005214# recommend kstrto* over simple_strto* and strict_strto*
5215 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005216 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005217 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005218 }
Joe Perches6712d852012-03-23 15:02:20 -07005219
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005220# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005221 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005222 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005223 "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 -07005224 }
Joe Perches6712d852012-03-23 15:02:20 -07005225
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005226# check for various structs that are normally const (ops, kgdb, device_tree)
5227 my $const_structs = qr{
5228 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005229 address_space_operations|
5230 backlight_ops|
5231 block_device_operations|
5232 dentry_operations|
5233 dev_pm_ops|
5234 dma_map_ops|
5235 extent_io_ops|
5236 file_lock_operations|
5237 file_operations|
5238 hv_ops|
5239 ide_dma_ops|
5240 intel_dvo_dev_ops|
5241 item_operations|
5242 iwl_ops|
5243 kgdb_arch|
5244 kgdb_io|
5245 kset_uevent_ops|
5246 lock_manager_operations|
5247 microcode_ops|
5248 mtrr_ops|
5249 neigh_ops|
5250 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005251 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08005252 pci_raw_ops|
5253 pipe_buf_operations|
5254 platform_hibernation_ops|
5255 platform_suspend_ops|
5256 proto_ops|
5257 rpc_pipe_ops|
5258 seq_operations|
5259 snd_ac97_build_ops|
5260 soc_pcmcia_socket_ops|
5261 stacktrace_ops|
5262 sysfs_ops|
5263 tty_operations|
5264 usb_mon_operations|
5265 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005266 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005267 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005268 WARN("CONST_STRUCT",
5269 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08005270 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08005271 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005272
5273# use of NR_CPUS is usually wrong
5274# ignore definitions of NR_CPUS and usage to define arrays as likely right
5275 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005276 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5277 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005278 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5279 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5280 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07005281 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005282 WARN("NR_CPUS",
5283 "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 -07005284 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005285
Joe Perches52ea8502013-11-12 15:10:09 -08005286# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5287 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5288 ERROR("DEFINE_ARCH_HAS",
5289 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5290 }
5291
Joe Perchesacd93622015-02-13 14:38:38 -08005292# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5293 if ($^V && $^V ge 5.10.0 &&
5294 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5295 WARN("LIKELY_MISUSE",
5296 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5297 }
5298
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005299# whine mightly about in_atomic
5300 if ($line =~ /\bin_atomic\s*\(/) {
5301 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005302 ERROR("IN_ATOMIC",
5303 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08005304 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005305 WARN("IN_ATOMIC",
5306 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08005307 }
5308 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01005309
5310# check for lockdep_set_novalidate_class
5311 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5312 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5313 if ($realfile !~ m@^kernel/lockdep@ &&
5314 $realfile !~ m@^include/linux/lockdep@ &&
5315 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005316 ERROR("LOCKDEP",
5317 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01005318 }
5319 }
Dave Jones88f88312011-01-12 16:59:59 -08005320
5321 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5322 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005323 WARN("EXPORTED_WORLD_WRITABLE",
5324 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08005325 }
Joe Perches24358802014-04-03 14:49:13 -07005326
Joe Perches515a2352014-04-03 14:49:24 -07005327# Mode permission misuses where it seems decimal should be octal
5328# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5329 if ($^V && $^V ge 5.10.0 &&
5330 $line =~ /$mode_perms_search/) {
5331 foreach my $entry (@mode_permission_funcs) {
5332 my $func = $entry->[0];
5333 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07005334
Joe Perches515a2352014-04-03 14:49:24 -07005335 my $skip_args = "";
5336 if ($arg_pos > 1) {
5337 $arg_pos--;
5338 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5339 }
5340 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5341 if ($line =~ /$test/) {
5342 my $val = $1;
5343 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07005344
Joe Perches515a2352014-04-03 14:49:24 -07005345 if ($val !~ /^0$/ &&
5346 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5347 length($val) ne 4)) {
5348 ERROR("NON_OCTAL_PERMISSIONS",
5349 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08005350 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5351 ERROR("EXPORTED_WORLD_WRITABLE",
5352 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07005353 }
Joe Perches24358802014-04-03 14:49:13 -07005354 }
5355 }
5356 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005357 }
5358
5359 # If we have no input at all, then there is nothing to report on
5360 # so just keep quiet.
5361 if ($#rawlines == -1) {
5362 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005363 }
5364
Andy Whitcroft8905a672007-11-28 16:21:06 -08005365 # In mailback mode only produce a report in the negative, for
5366 # things that appear to be patches.
5367 if ($mailback && ($clean == 1 || !$is_patch)) {
5368 exit(0);
5369 }
5370
5371 # This is not a patch, and we are are in 'no-patch' mode so
5372 # just keep quiet.
5373 if (!$chk_patch && !$is_patch) {
5374 exit(0);
5375 }
5376
5377 if (!$is_patch) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005378 ERROR("NOT_UNIFIED_DIFF",
5379 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005380 }
5381 if ($is_patch && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005382 ERROR("MISSING_SIGN_OFF",
5383 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005384 }
5385
Andy Whitcroft8905a672007-11-28 16:21:06 -08005386 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005387 if ($summary && !($clean == 1 && $quiet == 1)) {
5388 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08005389 print "total: $cnt_error errors, $cnt_warn warnings, " .
5390 (($check)? "$cnt_chk checks, " : "") .
5391 "$cnt_lines lines checked\n";
5392 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005393 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005394
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005395 if ($quiet == 0) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005396
5397 if ($^V lt 5.10.0) {
5398 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5399 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5400 }
5401
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005402 # If there were whitespace errors which cleanpatch can fix
5403 # then suggest that.
5404 if ($rpt_cleaners) {
5405 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5406 print " scripts/cleanfile\n\n";
Mike Frysingerb0781212011-03-22 16:34:43 -07005407 $rpt_cleaners = 0;
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07005408 }
5409 }
5410
Joe Perches91bfe482013-09-11 14:23:59 -07005411 hash_show_words(\%use_type, "Used");
5412 hash_show_words(\%ignore_type, "Ignored");
Joe Perches000d1cc12011-07-25 17:13:25 -07005413
Joe Perchesd752fcc2014-08-06 16:11:05 -07005414 if ($clean == 0 && $fix &&
5415 ("@rawlines" ne "@fixed" ||
5416 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08005417 my $newfile = $filename;
5418 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07005419 my $linecount = 0;
5420 my $f;
5421
Joe Perchesd752fcc2014-08-06 16:11:05 -07005422 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5423
Joe Perches3705ce52013-07-03 15:05:31 -07005424 open($f, '>', $newfile)
5425 or die "$P: Can't open $newfile for write\n";
5426 foreach my $fixed_line (@fixed) {
5427 $linecount++;
5428 if ($file) {
5429 if ($linecount > 3) {
5430 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07005431 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07005432 }
5433 } else {
5434 print $f $fixed_line . "\n";
5435 }
5436 }
5437 close($f);
5438
5439 if (!$quiet) {
5440 print << "EOM";
5441Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5442
5443Do _NOT_ trust the results written to this file.
5444Do _NOT_ submit these changes without inspecting them for correctness.
5445
5446This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5447No warranties, expressed or implied...
5448
5449EOM
5450 }
5451 }
5452
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005453 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08005454 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005455 }
5456 if ($clean == 0 && $quiet == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005457 print << "EOM";
5458$vname has style problems, please review.
5459
5460If any of these errors are false positives, please report
5461them to the maintainer, see CHECKPATCH in MAINTAINERS.
5462EOM
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005463 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005464
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005465 return $clean;
5466}